gcc/
[official-gcc.git] / gcc / dwarf2out.c
blob9d4b39ec00c0d713ec8364b91aec263150085b20
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 "cgraph.h"
100 #include "ira.h"
101 #include "lra.h"
102 #include "dumpfile.h"
103 #include "opts.h"
104 #include "tree-dfa.h"
105 #include "gdb/gdb-index.h"
106 #include "rtl-iter.h"
108 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
109 static rtx_insn *last_var_location_insn;
110 static rtx_insn *cached_next_real_insn;
111 static void dwarf2out_decl (tree);
113 #ifdef VMS_DEBUGGING_INFO
114 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
116 /* Define this macro to be a nonzero value if the directory specifications
117 which are output in the debug info should end with a separator. */
118 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
119 /* Define this macro to evaluate to a nonzero value if GCC should refrain
120 from generating indirect strings in DWARF2 debug information, for instance
121 if your target is stuck with an old version of GDB that is unable to
122 process them properly or uses VMS Debug. */
123 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
124 #else
125 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
126 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
127 #endif
129 /* ??? Poison these here until it can be done generically. They've been
130 totally replaced in this file; make sure it stays that way. */
131 #undef DWARF2_UNWIND_INFO
132 #undef DWARF2_FRAME_INFO
133 #if (GCC_VERSION >= 3000)
134 #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
135 #endif
137 /* The size of the target's pointer type. */
138 #ifndef PTR_SIZE
139 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
140 #endif
142 /* Array of RTXes referenced by the debugging information, which therefore
143 must be kept around forever. */
144 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
146 /* A pointer to the base of a list of incomplete types which might be
147 completed at some later time. incomplete_types_list needs to be a
148 vec<tree, va_gc> *because we want to tell the garbage collector about
149 it. */
150 static GTY(()) vec<tree, va_gc> *incomplete_types;
152 /* A pointer to the base of a table of references to declaration
153 scopes. This table is a display which tracks the nesting
154 of declaration scopes at the current scope and containing
155 scopes. This table is used to find the proper place to
156 define type declaration DIE's. */
157 static GTY(()) vec<tree, va_gc> *decl_scope_table;
159 /* Pointers to various DWARF2 sections. */
160 static GTY(()) section *debug_info_section;
161 static GTY(()) section *debug_skeleton_info_section;
162 static GTY(()) section *debug_abbrev_section;
163 static GTY(()) section *debug_skeleton_abbrev_section;
164 static GTY(()) section *debug_aranges_section;
165 static GTY(()) section *debug_addr_section;
166 static GTY(()) section *debug_macinfo_section;
167 static GTY(()) section *debug_line_section;
168 static GTY(()) section *debug_skeleton_line_section;
169 static GTY(()) section *debug_loc_section;
170 static GTY(()) section *debug_pubnames_section;
171 static GTY(()) section *debug_pubtypes_section;
172 static GTY(()) section *debug_str_section;
173 static GTY(()) section *debug_str_dwo_section;
174 static GTY(()) section *debug_str_offsets_section;
175 static GTY(()) section *debug_ranges_section;
176 static GTY(()) section *debug_frame_section;
178 /* Maximum size (in bytes) of an artificially generated label. */
179 #define MAX_ARTIFICIAL_LABEL_BYTES 30
181 /* According to the (draft) DWARF 3 specification, the initial length
182 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
183 bytes are 0xffffffff, followed by the length stored in the next 8
184 bytes.
186 However, the SGI/MIPS ABI uses an initial length which is equal to
187 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
189 #ifndef DWARF_INITIAL_LENGTH_SIZE
190 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
191 #endif
193 /* Round SIZE up to the nearest BOUNDARY. */
194 #define DWARF_ROUND(SIZE,BOUNDARY) \
195 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
197 /* CIE identifier. */
198 #if HOST_BITS_PER_WIDE_INT >= 64
199 #define DWARF_CIE_ID \
200 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
201 #else
202 #define DWARF_CIE_ID DW_CIE_ID
203 #endif
206 /* A vector for a table that contains frame description
207 information for each routine. */
208 #define NOT_INDEXED (-1U)
209 #define NO_INDEX_ASSIGNED (-2U)
211 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
213 struct GTY((for_user)) indirect_string_node {
214 const char *str;
215 unsigned int refcount;
216 enum dwarf_form form;
217 char *label;
218 unsigned int index;
221 struct indirect_string_hasher : ggc_ptr_hash<indirect_string_node>
223 typedef const char *compare_type;
225 static hashval_t hash (indirect_string_node *);
226 static bool equal (indirect_string_node *, const char *);
229 static GTY (()) hash_table<indirect_string_hasher> *debug_str_hash;
231 /* With split_debug_info, both the comp_dir and dwo_name go in the
232 main object file, rather than the dwo, similar to the force_direct
233 parameter elsewhere but with additional complications:
235 1) The string is needed in both the main object file and the dwo.
236 That is, the comp_dir and dwo_name will appear in both places.
238 2) Strings can use three forms: DW_FORM_string, DW_FORM_strp or
239 DW_FORM_GNU_str_index.
241 3) GCC chooses the form to use late, depending on the size and
242 reference count.
244 Rather than forcing the all debug string handling functions and
245 callers to deal with these complications, simply use a separate,
246 special-cased string table for any attribute that should go in the
247 main object file. This limits the complexity to just the places
248 that need it. */
250 static GTY (()) hash_table<indirect_string_hasher> *skeleton_debug_str_hash;
252 static GTY(()) int dw2_string_counter;
254 /* True if the compilation unit places functions in more than one section. */
255 static GTY(()) bool have_multiple_function_sections = false;
257 /* Whether the default text and cold text sections have been used at all. */
259 static GTY(()) bool text_section_used = false;
260 static GTY(()) bool cold_text_section_used = false;
262 /* The default cold text section. */
263 static GTY(()) section *cold_text_section;
265 /* The DIE for C++14 'auto' in a function return type. */
266 static GTY(()) dw_die_ref auto_die;
268 /* The DIE for C++14 'decltype(auto)' in a function return type. */
269 static GTY(()) dw_die_ref decltype_auto_die;
271 /* Forward declarations for functions defined in this file. */
273 static char *stripattributes (const char *);
274 static void output_call_frame_info (int);
275 static void dwarf2out_note_section_used (void);
277 /* Personality decl of current unit. Used only when assembler does not support
278 personality CFI. */
279 static GTY(()) rtx current_unit_personality;
281 /* Data and reference forms for relocatable data. */
282 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
283 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
285 #ifndef DEBUG_FRAME_SECTION
286 #define DEBUG_FRAME_SECTION ".debug_frame"
287 #endif
289 #ifndef FUNC_BEGIN_LABEL
290 #define FUNC_BEGIN_LABEL "LFB"
291 #endif
293 #ifndef FUNC_END_LABEL
294 #define FUNC_END_LABEL "LFE"
295 #endif
297 #ifndef PROLOGUE_END_LABEL
298 #define PROLOGUE_END_LABEL "LPE"
299 #endif
301 #ifndef EPILOGUE_BEGIN_LABEL
302 #define EPILOGUE_BEGIN_LABEL "LEB"
303 #endif
305 #ifndef FRAME_BEGIN_LABEL
306 #define FRAME_BEGIN_LABEL "Lframe"
307 #endif
308 #define CIE_AFTER_SIZE_LABEL "LSCIE"
309 #define CIE_END_LABEL "LECIE"
310 #define FDE_LABEL "LSFDE"
311 #define FDE_AFTER_SIZE_LABEL "LASFDE"
312 #define FDE_END_LABEL "LEFDE"
313 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
314 #define LINE_NUMBER_END_LABEL "LELT"
315 #define LN_PROLOG_AS_LABEL "LASLTP"
316 #define LN_PROLOG_END_LABEL "LELTP"
317 #define DIE_LABEL_PREFIX "DW"
319 /* Match the base name of a file to the base name of a compilation unit. */
321 static int
322 matches_main_base (const char *path)
324 /* Cache the last query. */
325 static const char *last_path = NULL;
326 static int last_match = 0;
327 if (path != last_path)
329 const char *base;
330 int length = base_of_path (path, &base);
331 last_path = path;
332 last_match = (length == main_input_baselength
333 && memcmp (base, main_input_basename, length) == 0);
335 return last_match;
338 #ifdef DEBUG_DEBUG_STRUCT
340 static int
341 dump_struct_debug (tree type, enum debug_info_usage usage,
342 enum debug_struct_file criterion, int generic,
343 int matches, int result)
345 /* Find the type name. */
346 tree type_decl = TYPE_STUB_DECL (type);
347 tree t = type_decl;
348 const char *name = 0;
349 if (TREE_CODE (t) == TYPE_DECL)
350 t = DECL_NAME (t);
351 if (t)
352 name = IDENTIFIER_POINTER (t);
354 fprintf (stderr, " struct %d %s %s %s %s %d %p %s\n",
355 criterion,
356 DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
357 matches ? "bas" : "hdr",
358 generic ? "gen" : "ord",
359 usage == DINFO_USAGE_DFN ? ";" :
360 usage == DINFO_USAGE_DIR_USE ? "." : "*",
361 result,
362 (void*) type_decl, name);
363 return result;
365 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
366 dump_struct_debug (type, usage, criterion, generic, matches, result)
368 #else
370 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
371 (result)
373 #endif
375 /* Get the number of HOST_WIDE_INTs needed to represent the precision
376 of the number. */
378 static unsigned int
379 get_full_len (const wide_int &op)
381 return ((op.get_precision () + HOST_BITS_PER_WIDE_INT - 1)
382 / HOST_BITS_PER_WIDE_INT);
385 static bool
386 should_emit_struct_debug (tree type, enum debug_info_usage usage)
388 enum debug_struct_file criterion;
389 tree type_decl;
390 bool generic = lang_hooks.types.generic_p (type);
392 if (generic)
393 criterion = debug_struct_generic[usage];
394 else
395 criterion = debug_struct_ordinary[usage];
397 if (criterion == DINFO_STRUCT_FILE_NONE)
398 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
399 if (criterion == DINFO_STRUCT_FILE_ANY)
400 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
402 type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
404 if (type_decl != NULL)
406 if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
407 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
409 if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
410 return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
413 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
416 /* Return a pointer to a copy of the section string name S with all
417 attributes stripped off, and an asterisk prepended (for assemble_name). */
419 static inline char *
420 stripattributes (const char *s)
422 char *stripped = XNEWVEC (char, strlen (s) + 2);
423 char *p = stripped;
425 *p++ = '*';
427 while (*s && *s != ',')
428 *p++ = *s++;
430 *p = '\0';
431 return stripped;
434 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
435 switch to the data section instead, and write out a synthetic start label
436 for collect2 the first time around. */
438 static void
439 switch_to_eh_frame_section (bool back)
441 tree label;
443 #ifdef EH_FRAME_SECTION_NAME
444 if (eh_frame_section == 0)
446 int flags;
448 if (EH_TABLES_CAN_BE_READ_ONLY)
450 int fde_encoding;
451 int per_encoding;
452 int lsda_encoding;
454 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
455 /*global=*/0);
456 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
457 /*global=*/1);
458 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
459 /*global=*/0);
460 flags = ((! flag_pic
461 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
462 && (fde_encoding & 0x70) != DW_EH_PE_aligned
463 && (per_encoding & 0x70) != DW_EH_PE_absptr
464 && (per_encoding & 0x70) != DW_EH_PE_aligned
465 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
466 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
467 ? 0 : SECTION_WRITE);
469 else
470 flags = SECTION_WRITE;
471 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
473 #endif /* EH_FRAME_SECTION_NAME */
475 if (eh_frame_section)
476 switch_to_section (eh_frame_section);
477 else
479 /* We have no special eh_frame section. Put the information in
480 the data section and emit special labels to guide collect2. */
481 switch_to_section (data_section);
483 if (!back)
485 label = get_file_function_name ("F");
486 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
487 targetm.asm_out.globalize_label (asm_out_file,
488 IDENTIFIER_POINTER (label));
489 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
494 /* Switch [BACK] to the eh or debug frame table section, depending on
495 FOR_EH. */
497 static void
498 switch_to_frame_table_section (int for_eh, bool back)
500 if (for_eh)
501 switch_to_eh_frame_section (back);
502 else
504 if (!debug_frame_section)
505 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
506 SECTION_DEBUG, NULL);
507 switch_to_section (debug_frame_section);
511 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
513 enum dw_cfi_oprnd_type
514 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
516 switch (cfi)
518 case DW_CFA_nop:
519 case DW_CFA_GNU_window_save:
520 case DW_CFA_remember_state:
521 case DW_CFA_restore_state:
522 return dw_cfi_oprnd_unused;
524 case DW_CFA_set_loc:
525 case DW_CFA_advance_loc1:
526 case DW_CFA_advance_loc2:
527 case DW_CFA_advance_loc4:
528 case DW_CFA_MIPS_advance_loc8:
529 return dw_cfi_oprnd_addr;
531 case DW_CFA_offset:
532 case DW_CFA_offset_extended:
533 case DW_CFA_def_cfa:
534 case DW_CFA_offset_extended_sf:
535 case DW_CFA_def_cfa_sf:
536 case DW_CFA_restore:
537 case DW_CFA_restore_extended:
538 case DW_CFA_undefined:
539 case DW_CFA_same_value:
540 case DW_CFA_def_cfa_register:
541 case DW_CFA_register:
542 case DW_CFA_expression:
543 return dw_cfi_oprnd_reg_num;
545 case DW_CFA_def_cfa_offset:
546 case DW_CFA_GNU_args_size:
547 case DW_CFA_def_cfa_offset_sf:
548 return dw_cfi_oprnd_offset;
550 case DW_CFA_def_cfa_expression:
551 return dw_cfi_oprnd_loc;
553 default:
554 gcc_unreachable ();
558 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
560 enum dw_cfi_oprnd_type
561 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
563 switch (cfi)
565 case DW_CFA_def_cfa:
566 case DW_CFA_def_cfa_sf:
567 case DW_CFA_offset:
568 case DW_CFA_offset_extended_sf:
569 case DW_CFA_offset_extended:
570 return dw_cfi_oprnd_offset;
572 case DW_CFA_register:
573 return dw_cfi_oprnd_reg_num;
575 case DW_CFA_expression:
576 return dw_cfi_oprnd_loc;
578 default:
579 return dw_cfi_oprnd_unused;
583 /* Output one FDE. */
585 static void
586 output_fde (dw_fde_ref fde, bool for_eh, bool second,
587 char *section_start_label, int fde_encoding, char *augmentation,
588 bool any_lsda_needed, int lsda_encoding)
590 const char *begin, *end;
591 static unsigned int j;
592 char l1[20], l2[20];
594 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
595 /* empty */ 0);
596 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
597 for_eh + j);
598 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
599 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
600 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
601 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
602 " indicating 64-bit DWARF extension");
603 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
604 "FDE Length");
605 ASM_OUTPUT_LABEL (asm_out_file, l1);
607 if (for_eh)
608 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
609 else
610 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
611 debug_frame_section, "FDE CIE offset");
613 begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
614 end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
616 if (for_eh)
618 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
619 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
620 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
621 "FDE initial location");
622 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
623 end, begin, "FDE address range");
625 else
627 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
628 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
631 if (augmentation[0])
633 if (any_lsda_needed)
635 int size = size_of_encoded_value (lsda_encoding);
637 if (lsda_encoding == DW_EH_PE_aligned)
639 int offset = ( 4 /* Length */
640 + 4 /* CIE offset */
641 + 2 * size_of_encoded_value (fde_encoding)
642 + 1 /* Augmentation size */ );
643 int pad = -offset & (PTR_SIZE - 1);
645 size += pad;
646 gcc_assert (size_of_uleb128 (size) == 1);
649 dw2_asm_output_data_uleb128 (size, "Augmentation size");
651 if (fde->uses_eh_lsda)
653 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
654 fde->funcdef_number);
655 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
656 gen_rtx_SYMBOL_REF (Pmode, l1),
657 false,
658 "Language Specific Data Area");
660 else
662 if (lsda_encoding == DW_EH_PE_aligned)
663 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
664 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
665 "Language Specific Data Area (none)");
668 else
669 dw2_asm_output_data_uleb128 (0, "Augmentation size");
672 /* Loop through the Call Frame Instructions associated with this FDE. */
673 fde->dw_fde_current_label = begin;
675 size_t from, until, i;
677 from = 0;
678 until = vec_safe_length (fde->dw_fde_cfi);
680 if (fde->dw_fde_second_begin == NULL)
682 else if (!second)
683 until = fde->dw_fde_switch_cfi_index;
684 else
685 from = fde->dw_fde_switch_cfi_index;
687 for (i = from; i < until; i++)
688 output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
691 /* If we are to emit a ref/link from function bodies to their frame tables,
692 do it now. This is typically performed to make sure that tables
693 associated with functions are dragged with them and not discarded in
694 garbage collecting links. We need to do this on a per function basis to
695 cope with -ffunction-sections. */
697 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
698 /* Switch to the function section, emit the ref to the tables, and
699 switch *back* into the table section. */
700 switch_to_section (function_section (fde->decl));
701 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
702 switch_to_frame_table_section (for_eh, true);
703 #endif
705 /* Pad the FDE out to an address sized boundary. */
706 ASM_OUTPUT_ALIGN (asm_out_file,
707 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
708 ASM_OUTPUT_LABEL (asm_out_file, l2);
710 j += 2;
713 /* Return true if frame description entry FDE is needed for EH. */
715 static bool
716 fde_needed_for_eh_p (dw_fde_ref fde)
718 if (flag_asynchronous_unwind_tables)
719 return true;
721 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
722 return true;
724 if (fde->uses_eh_lsda)
725 return true;
727 /* If exceptions are enabled, we have collected nothrow info. */
728 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
729 return false;
731 return true;
734 /* Output the call frame information used to record information
735 that relates to calculating the frame pointer, and records the
736 location of saved registers. */
738 static void
739 output_call_frame_info (int for_eh)
741 unsigned int i;
742 dw_fde_ref fde;
743 dw_cfi_ref cfi;
744 char l1[20], l2[20], section_start_label[20];
745 bool any_lsda_needed = false;
746 char augmentation[6];
747 int augmentation_size;
748 int fde_encoding = DW_EH_PE_absptr;
749 int per_encoding = DW_EH_PE_absptr;
750 int lsda_encoding = DW_EH_PE_absptr;
751 int return_reg;
752 rtx personality = NULL;
753 int dw_cie_version;
755 /* Don't emit a CIE if there won't be any FDEs. */
756 if (!fde_vec)
757 return;
759 /* Nothing to do if the assembler's doing it all. */
760 if (dwarf2out_do_cfi_asm ())
761 return;
763 /* If we don't have any functions we'll want to unwind out of, don't emit
764 any EH unwind information. If we make FDEs linkonce, we may have to
765 emit an empty label for an FDE that wouldn't otherwise be emitted. We
766 want to avoid having an FDE kept around when the function it refers to
767 is discarded. Example where this matters: a primary function template
768 in C++ requires EH information, an explicit specialization doesn't. */
769 if (for_eh)
771 bool any_eh_needed = false;
773 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
775 if (fde->uses_eh_lsda)
776 any_eh_needed = any_lsda_needed = true;
777 else if (fde_needed_for_eh_p (fde))
778 any_eh_needed = true;
779 else if (TARGET_USES_WEAK_UNWIND_INFO)
780 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
783 if (!any_eh_needed)
784 return;
787 /* We're going to be generating comments, so turn on app. */
788 if (flag_debug_asm)
789 app_enable ();
791 /* Switch to the proper frame section, first time. */
792 switch_to_frame_table_section (for_eh, false);
794 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
795 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
797 /* Output the CIE. */
798 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
799 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
800 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
801 dw2_asm_output_data (4, 0xffffffff,
802 "Initial length escape value indicating 64-bit DWARF extension");
803 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
804 "Length of Common Information Entry");
805 ASM_OUTPUT_LABEL (asm_out_file, l1);
807 /* Now that the CIE pointer is PC-relative for EH,
808 use 0 to identify the CIE. */
809 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
810 (for_eh ? 0 : DWARF_CIE_ID),
811 "CIE Identifier Tag");
813 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
814 use CIE version 1, unless that would produce incorrect results
815 due to overflowing the return register column. */
816 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
817 dw_cie_version = 1;
818 if (return_reg >= 256 || dwarf_version > 2)
819 dw_cie_version = 3;
820 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
822 augmentation[0] = 0;
823 augmentation_size = 0;
825 personality = current_unit_personality;
826 if (for_eh)
828 char *p;
830 /* Augmentation:
831 z Indicates that a uleb128 is present to size the
832 augmentation section.
833 L Indicates the encoding (and thus presence) of
834 an LSDA pointer in the FDE augmentation.
835 R Indicates a non-default pointer encoding for
836 FDE code pointers.
837 P Indicates the presence of an encoding + language
838 personality routine in the CIE augmentation. */
840 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
841 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
842 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
844 p = augmentation + 1;
845 if (personality)
847 *p++ = 'P';
848 augmentation_size += 1 + size_of_encoded_value (per_encoding);
849 assemble_external_libcall (personality);
851 if (any_lsda_needed)
853 *p++ = 'L';
854 augmentation_size += 1;
856 if (fde_encoding != DW_EH_PE_absptr)
858 *p++ = 'R';
859 augmentation_size += 1;
861 if (p > augmentation + 1)
863 augmentation[0] = 'z';
864 *p = '\0';
867 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
868 if (personality && per_encoding == DW_EH_PE_aligned)
870 int offset = ( 4 /* Length */
871 + 4 /* CIE Id */
872 + 1 /* CIE version */
873 + strlen (augmentation) + 1 /* Augmentation */
874 + size_of_uleb128 (1) /* Code alignment */
875 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
876 + 1 /* RA column */
877 + 1 /* Augmentation size */
878 + 1 /* Personality encoding */ );
879 int pad = -offset & (PTR_SIZE - 1);
881 augmentation_size += pad;
883 /* Augmentations should be small, so there's scarce need to
884 iterate for a solution. Die if we exceed one uleb128 byte. */
885 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
889 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
890 if (dw_cie_version >= 4)
892 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
893 dw2_asm_output_data (1, 0, "CIE Segment Size");
895 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
896 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
897 "CIE Data Alignment Factor");
899 if (dw_cie_version == 1)
900 dw2_asm_output_data (1, return_reg, "CIE RA Column");
901 else
902 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
904 if (augmentation[0])
906 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
907 if (personality)
909 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
910 eh_data_format_name (per_encoding));
911 dw2_asm_output_encoded_addr_rtx (per_encoding,
912 personality,
913 true, NULL);
916 if (any_lsda_needed)
917 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
918 eh_data_format_name (lsda_encoding));
920 if (fde_encoding != DW_EH_PE_absptr)
921 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
922 eh_data_format_name (fde_encoding));
925 FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
926 output_cfi (cfi, NULL, for_eh);
928 /* Pad the CIE out to an address sized boundary. */
929 ASM_OUTPUT_ALIGN (asm_out_file,
930 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
931 ASM_OUTPUT_LABEL (asm_out_file, l2);
933 /* Loop through all of the FDE's. */
934 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
936 unsigned int k;
938 /* Don't emit EH unwind info for leaf functions that don't need it. */
939 if (for_eh && !fde_needed_for_eh_p (fde))
940 continue;
942 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
943 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
944 augmentation, any_lsda_needed, lsda_encoding);
947 if (for_eh && targetm.terminate_dw2_eh_frame_info)
948 dw2_asm_output_data (4, 0, "End of Table");
950 /* Turn off app to make assembly quicker. */
951 if (flag_debug_asm)
952 app_disable ();
955 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
957 static void
958 dwarf2out_do_cfi_startproc (bool second)
960 int enc;
961 rtx ref;
962 rtx personality = get_personality_function (current_function_decl);
964 fprintf (asm_out_file, "\t.cfi_startproc\n");
966 if (personality)
968 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
969 ref = personality;
971 /* ??? The GAS support isn't entirely consistent. We have to
972 handle indirect support ourselves, but PC-relative is done
973 in the assembler. Further, the assembler can't handle any
974 of the weirder relocation types. */
975 if (enc & DW_EH_PE_indirect)
976 ref = dw2_force_const_mem (ref, true);
978 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
979 output_addr_const (asm_out_file, ref);
980 fputc ('\n', asm_out_file);
983 if (crtl->uses_eh_lsda)
985 char lab[20];
987 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
988 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
989 current_function_funcdef_no);
990 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
991 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
993 if (enc & DW_EH_PE_indirect)
994 ref = dw2_force_const_mem (ref, true);
996 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
997 output_addr_const (asm_out_file, ref);
998 fputc ('\n', asm_out_file);
1002 /* Allocate CURRENT_FDE. Immediately initialize all we can, noting that
1003 this allocation may be done before pass_final. */
1005 dw_fde_ref
1006 dwarf2out_alloc_current_fde (void)
1008 dw_fde_ref fde;
1010 fde = ggc_cleared_alloc<dw_fde_node> ();
1011 fde->decl = current_function_decl;
1012 fde->funcdef_number = current_function_funcdef_no;
1013 fde->fde_index = vec_safe_length (fde_vec);
1014 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
1015 fde->uses_eh_lsda = crtl->uses_eh_lsda;
1016 fde->nothrow = crtl->nothrow;
1017 fde->drap_reg = INVALID_REGNUM;
1018 fde->vdrap_reg = INVALID_REGNUM;
1020 /* Record the FDE associated with this function. */
1021 cfun->fde = fde;
1022 vec_safe_push (fde_vec, fde);
1024 return fde;
1027 /* Output a marker (i.e. a label) for the beginning of a function, before
1028 the prologue. */
1030 void
1031 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1032 const char *file ATTRIBUTE_UNUSED)
1034 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1035 char * dup_label;
1036 dw_fde_ref fde;
1037 section *fnsec;
1038 bool do_frame;
1040 current_function_func_begin_label = NULL;
1042 do_frame = dwarf2out_do_frame ();
1044 /* ??? current_function_func_begin_label is also used by except.c for
1045 call-site information. We must emit this label if it might be used. */
1046 if (!do_frame
1047 && (!flag_exceptions
1048 || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1049 return;
1051 fnsec = function_section (current_function_decl);
1052 switch_to_section (fnsec);
1053 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1054 current_function_funcdef_no);
1055 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1056 current_function_funcdef_no);
1057 dup_label = xstrdup (label);
1058 current_function_func_begin_label = dup_label;
1060 /* We can elide the fde allocation if we're not emitting debug info. */
1061 if (!do_frame)
1062 return;
1064 /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1065 emit insns as rtx but bypass the bulk of rest_of_compilation, which
1066 would include pass_dwarf2_frame. If we've not created the FDE yet,
1067 do so now. */
1068 fde = cfun->fde;
1069 if (fde == NULL)
1070 fde = dwarf2out_alloc_current_fde ();
1072 /* Initialize the bits of CURRENT_FDE that were not available earlier. */
1073 fde->dw_fde_begin = dup_label;
1074 fde->dw_fde_current_label = dup_label;
1075 fde->in_std_section = (fnsec == text_section
1076 || (cold_text_section && fnsec == cold_text_section));
1078 /* We only want to output line number information for the genuine dwarf2
1079 prologue case, not the eh frame case. */
1080 #ifdef DWARF2_DEBUGGING_INFO
1081 if (file)
1082 dwarf2out_source_line (line, file, 0, true);
1083 #endif
1085 if (dwarf2out_do_cfi_asm ())
1086 dwarf2out_do_cfi_startproc (false);
1087 else
1089 rtx personality = get_personality_function (current_function_decl);
1090 if (!current_unit_personality)
1091 current_unit_personality = personality;
1093 /* We cannot keep a current personality per function as without CFI
1094 asm, at the point where we emit the CFI data, there is no current
1095 function anymore. */
1096 if (personality && current_unit_personality != personality)
1097 sorry ("multiple EH personalities are supported only with assemblers "
1098 "supporting .cfi_personality directive");
1102 /* Output a marker (i.e. a label) for the end of the generated code
1103 for a function prologue. This gets called *after* the prologue code has
1104 been generated. */
1106 void
1107 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1108 const char *file ATTRIBUTE_UNUSED)
1110 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1112 /* Output a label to mark the endpoint of the code generated for this
1113 function. */
1114 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1115 current_function_funcdef_no);
1116 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1117 current_function_funcdef_no);
1118 cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1121 /* Output a marker (i.e. a label) for the beginning of the generated code
1122 for a function epilogue. This gets called *before* the prologue code has
1123 been generated. */
1125 void
1126 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1127 const char *file ATTRIBUTE_UNUSED)
1129 dw_fde_ref fde = cfun->fde;
1130 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1132 if (fde->dw_fde_vms_begin_epilogue)
1133 return;
1135 /* Output a label to mark the endpoint of the code generated for this
1136 function. */
1137 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1138 current_function_funcdef_no);
1139 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1140 current_function_funcdef_no);
1141 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1144 /* Output a marker (i.e. a label) for the absolute end of the generated code
1145 for a function definition. This gets called *after* the epilogue code has
1146 been generated. */
1148 void
1149 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1150 const char *file ATTRIBUTE_UNUSED)
1152 dw_fde_ref fde;
1153 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1155 last_var_location_insn = NULL;
1156 cached_next_real_insn = NULL;
1158 if (dwarf2out_do_cfi_asm ())
1159 fprintf (asm_out_file, "\t.cfi_endproc\n");
1161 /* Output a label to mark the endpoint of the code generated for this
1162 function. */
1163 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1164 current_function_funcdef_no);
1165 ASM_OUTPUT_LABEL (asm_out_file, label);
1166 fde = cfun->fde;
1167 gcc_assert (fde != NULL);
1168 if (fde->dw_fde_second_begin == NULL)
1169 fde->dw_fde_end = xstrdup (label);
1172 void
1173 dwarf2out_frame_finish (void)
1175 /* Output call frame information. */
1176 if (targetm.debug_unwind_info () == UI_DWARF2)
1177 output_call_frame_info (0);
1179 /* Output another copy for the unwinder. */
1180 if ((flag_unwind_tables || flag_exceptions)
1181 && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1182 output_call_frame_info (1);
1185 /* Note that the current function section is being used for code. */
1187 static void
1188 dwarf2out_note_section_used (void)
1190 section *sec = current_function_section ();
1191 if (sec == text_section)
1192 text_section_used = true;
1193 else if (sec == cold_text_section)
1194 cold_text_section_used = true;
1197 static void var_location_switch_text_section (void);
1198 static void set_cur_line_info_table (section *);
1200 void
1201 dwarf2out_switch_text_section (void)
1203 section *sect;
1204 dw_fde_ref fde = cfun->fde;
1206 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1208 if (!in_cold_section_p)
1210 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1211 fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1212 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1214 else
1216 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1217 fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1218 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1220 have_multiple_function_sections = true;
1222 /* There is no need to mark used sections when not debugging. */
1223 if (cold_text_section != NULL)
1224 dwarf2out_note_section_used ();
1226 if (dwarf2out_do_cfi_asm ())
1227 fprintf (asm_out_file, "\t.cfi_endproc\n");
1229 /* Now do the real section switch. */
1230 sect = current_function_section ();
1231 switch_to_section (sect);
1233 fde->second_in_std_section
1234 = (sect == text_section
1235 || (cold_text_section && sect == cold_text_section));
1237 if (dwarf2out_do_cfi_asm ())
1238 dwarf2out_do_cfi_startproc (true);
1240 var_location_switch_text_section ();
1242 if (cold_text_section != NULL)
1243 set_cur_line_info_table (sect);
1246 /* And now, the subset of the debugging information support code necessary
1247 for emitting location expressions. */
1249 /* Data about a single source file. */
1250 struct GTY((for_user)) dwarf_file_data {
1251 const char * filename;
1252 int emitted_number;
1255 /* Describe an entry into the .debug_addr section. */
1257 enum ate_kind {
1258 ate_kind_rtx,
1259 ate_kind_rtx_dtprel,
1260 ate_kind_label
1263 typedef struct GTY((for_user)) addr_table_entry_struct {
1264 enum ate_kind kind;
1265 unsigned int refcount;
1266 unsigned int index;
1267 union addr_table_entry_struct_union
1269 rtx GTY ((tag ("0"))) rtl;
1270 char * GTY ((tag ("1"))) label;
1272 GTY ((desc ("%1.kind"))) addr;
1274 addr_table_entry;
1276 /* Location lists are ranges + location descriptions for that range,
1277 so you can track variables that are in different places over
1278 their entire life. */
1279 typedef struct GTY(()) dw_loc_list_struct {
1280 dw_loc_list_ref dw_loc_next;
1281 const char *begin; /* Label and addr_entry for start of range */
1282 addr_table_entry *begin_entry;
1283 const char *end; /* Label for end of range */
1284 char *ll_symbol; /* Label for beginning of location list.
1285 Only on head of list */
1286 const char *section; /* Section this loclist is relative to */
1287 dw_loc_descr_ref expr;
1288 hashval_t hash;
1289 /* True if all addresses in this and subsequent lists are known to be
1290 resolved. */
1291 bool resolved_addr;
1292 /* True if this list has been replaced by dw_loc_next. */
1293 bool replaced;
1294 bool emitted;
1295 /* True if the range should be emitted even if begin and end
1296 are the same. */
1297 bool force;
1298 } dw_loc_list_node;
1300 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1302 /* Convert a DWARF stack opcode into its string name. */
1304 static const char *
1305 dwarf_stack_op_name (unsigned int op)
1307 const char *name = get_DW_OP_name (op);
1309 if (name != NULL)
1310 return name;
1312 return "OP_<unknown>";
1315 /* Return a pointer to a newly allocated location description. Location
1316 descriptions are simple expression terms that can be strung
1317 together to form more complicated location (address) descriptions. */
1319 static inline dw_loc_descr_ref
1320 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1321 unsigned HOST_WIDE_INT oprnd2)
1323 dw_loc_descr_ref descr = ggc_cleared_alloc<dw_loc_descr_node> ();
1325 descr->dw_loc_opc = op;
1326 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1327 descr->dw_loc_oprnd1.val_entry = NULL;
1328 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1329 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1330 descr->dw_loc_oprnd2.val_entry = NULL;
1331 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1333 return descr;
1336 /* Return a pointer to a newly allocated location description for
1337 REG and OFFSET. */
1339 static inline dw_loc_descr_ref
1340 new_reg_loc_descr (unsigned int reg, unsigned HOST_WIDE_INT offset)
1342 if (reg <= 31)
1343 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1344 offset, 0);
1345 else
1346 return new_loc_descr (DW_OP_bregx, reg, offset);
1349 /* Add a location description term to a location description expression. */
1351 static inline void
1352 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1354 dw_loc_descr_ref *d;
1356 /* Find the end of the chain. */
1357 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1360 *d = descr;
1363 /* Compare two location operands for exact equality. */
1365 static bool
1366 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1368 if (a->val_class != b->val_class)
1369 return false;
1370 switch (a->val_class)
1372 case dw_val_class_none:
1373 return true;
1374 case dw_val_class_addr:
1375 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1377 case dw_val_class_offset:
1378 case dw_val_class_unsigned_const:
1379 case dw_val_class_const:
1380 case dw_val_class_range_list:
1381 case dw_val_class_lineptr:
1382 case dw_val_class_macptr:
1383 /* These are all HOST_WIDE_INT, signed or unsigned. */
1384 return a->v.val_unsigned == b->v.val_unsigned;
1386 case dw_val_class_loc:
1387 return a->v.val_loc == b->v.val_loc;
1388 case dw_val_class_loc_list:
1389 return a->v.val_loc_list == b->v.val_loc_list;
1390 case dw_val_class_die_ref:
1391 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1392 case dw_val_class_fde_ref:
1393 return a->v.val_fde_index == b->v.val_fde_index;
1394 case dw_val_class_lbl_id:
1395 case dw_val_class_high_pc:
1396 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1397 case dw_val_class_str:
1398 return a->v.val_str == b->v.val_str;
1399 case dw_val_class_flag:
1400 return a->v.val_flag == b->v.val_flag;
1401 case dw_val_class_file:
1402 return a->v.val_file == b->v.val_file;
1403 case dw_val_class_decl_ref:
1404 return a->v.val_decl_ref == b->v.val_decl_ref;
1406 case dw_val_class_const_double:
1407 return (a->v.val_double.high == b->v.val_double.high
1408 && a->v.val_double.low == b->v.val_double.low);
1410 case dw_val_class_wide_int:
1411 return *a->v.val_wide == *b->v.val_wide;
1413 case dw_val_class_vec:
1415 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1416 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1418 return (a_len == b_len
1419 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1422 case dw_val_class_data8:
1423 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1425 case dw_val_class_vms_delta:
1426 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1427 && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1429 gcc_unreachable ();
1432 /* Compare two location atoms for exact equality. */
1434 static bool
1435 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1437 if (a->dw_loc_opc != b->dw_loc_opc)
1438 return false;
1440 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1441 address size, but since we always allocate cleared storage it
1442 should be zero for other types of locations. */
1443 if (a->dtprel != b->dtprel)
1444 return false;
1446 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1447 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1450 /* Compare two complete location expressions for exact equality. */
1452 bool
1453 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1455 while (1)
1457 if (a == b)
1458 return true;
1459 if (a == NULL || b == NULL)
1460 return false;
1461 if (!loc_descr_equal_p_1 (a, b))
1462 return false;
1464 a = a->dw_loc_next;
1465 b = b->dw_loc_next;
1470 /* Add a constant OFFSET to a location expression. */
1472 static void
1473 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1475 dw_loc_descr_ref loc;
1476 HOST_WIDE_INT *p;
1478 gcc_assert (*list_head != NULL);
1480 if (!offset)
1481 return;
1483 /* Find the end of the chain. */
1484 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1487 p = NULL;
1488 if (loc->dw_loc_opc == DW_OP_fbreg
1489 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1490 p = &loc->dw_loc_oprnd1.v.val_int;
1491 else if (loc->dw_loc_opc == DW_OP_bregx)
1492 p = &loc->dw_loc_oprnd2.v.val_int;
1494 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1495 offset. Don't optimize if an signed integer overflow would happen. */
1496 if (p != NULL
1497 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1498 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1499 *p += offset;
1501 else if (offset > 0)
1502 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1504 else
1506 loc->dw_loc_next = int_loc_descriptor (-offset);
1507 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1511 /* Add a constant OFFSET to a location list. */
1513 static void
1514 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1516 dw_loc_list_ref d;
1517 for (d = list_head; d != NULL; d = d->dw_loc_next)
1518 loc_descr_plus_const (&d->expr, offset);
1521 #define DWARF_REF_SIZE \
1522 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1524 static unsigned long int get_base_type_offset (dw_die_ref);
1526 /* Return the size of a location descriptor. */
1528 static unsigned long
1529 size_of_loc_descr (dw_loc_descr_ref loc)
1531 unsigned long size = 1;
1533 switch (loc->dw_loc_opc)
1535 case DW_OP_addr:
1536 size += DWARF2_ADDR_SIZE;
1537 break;
1538 case DW_OP_GNU_addr_index:
1539 case DW_OP_GNU_const_index:
1540 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1541 size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1542 break;
1543 case DW_OP_const1u:
1544 case DW_OP_const1s:
1545 size += 1;
1546 break;
1547 case DW_OP_const2u:
1548 case DW_OP_const2s:
1549 size += 2;
1550 break;
1551 case DW_OP_const4u:
1552 case DW_OP_const4s:
1553 size += 4;
1554 break;
1555 case DW_OP_const8u:
1556 case DW_OP_const8s:
1557 size += 8;
1558 break;
1559 case DW_OP_constu:
1560 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1561 break;
1562 case DW_OP_consts:
1563 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1564 break;
1565 case DW_OP_pick:
1566 size += 1;
1567 break;
1568 case DW_OP_plus_uconst:
1569 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1570 break;
1571 case DW_OP_skip:
1572 case DW_OP_bra:
1573 size += 2;
1574 break;
1575 case DW_OP_breg0:
1576 case DW_OP_breg1:
1577 case DW_OP_breg2:
1578 case DW_OP_breg3:
1579 case DW_OP_breg4:
1580 case DW_OP_breg5:
1581 case DW_OP_breg6:
1582 case DW_OP_breg7:
1583 case DW_OP_breg8:
1584 case DW_OP_breg9:
1585 case DW_OP_breg10:
1586 case DW_OP_breg11:
1587 case DW_OP_breg12:
1588 case DW_OP_breg13:
1589 case DW_OP_breg14:
1590 case DW_OP_breg15:
1591 case DW_OP_breg16:
1592 case DW_OP_breg17:
1593 case DW_OP_breg18:
1594 case DW_OP_breg19:
1595 case DW_OP_breg20:
1596 case DW_OP_breg21:
1597 case DW_OP_breg22:
1598 case DW_OP_breg23:
1599 case DW_OP_breg24:
1600 case DW_OP_breg25:
1601 case DW_OP_breg26:
1602 case DW_OP_breg27:
1603 case DW_OP_breg28:
1604 case DW_OP_breg29:
1605 case DW_OP_breg30:
1606 case DW_OP_breg31:
1607 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1608 break;
1609 case DW_OP_regx:
1610 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1611 break;
1612 case DW_OP_fbreg:
1613 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1614 break;
1615 case DW_OP_bregx:
1616 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1617 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1618 break;
1619 case DW_OP_piece:
1620 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1621 break;
1622 case DW_OP_bit_piece:
1623 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1624 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1625 break;
1626 case DW_OP_deref_size:
1627 case DW_OP_xderef_size:
1628 size += 1;
1629 break;
1630 case DW_OP_call2:
1631 size += 2;
1632 break;
1633 case DW_OP_call4:
1634 size += 4;
1635 break;
1636 case DW_OP_call_ref:
1637 size += DWARF_REF_SIZE;
1638 break;
1639 case DW_OP_implicit_value:
1640 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1641 + loc->dw_loc_oprnd1.v.val_unsigned;
1642 break;
1643 case DW_OP_GNU_implicit_pointer:
1644 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1645 break;
1646 case DW_OP_GNU_entry_value:
1648 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1649 size += size_of_uleb128 (op_size) + op_size;
1650 break;
1652 case DW_OP_GNU_const_type:
1654 unsigned long o
1655 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1656 size += size_of_uleb128 (o) + 1;
1657 switch (loc->dw_loc_oprnd2.val_class)
1659 case dw_val_class_vec:
1660 size += loc->dw_loc_oprnd2.v.val_vec.length
1661 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1662 break;
1663 case dw_val_class_const:
1664 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1665 break;
1666 case dw_val_class_const_double:
1667 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1668 break;
1669 case dw_val_class_wide_int:
1670 size += (get_full_len (*loc->dw_loc_oprnd2.v.val_wide)
1671 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
1672 break;
1673 default:
1674 gcc_unreachable ();
1676 break;
1678 case DW_OP_GNU_regval_type:
1680 unsigned long o
1681 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1682 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1683 + size_of_uleb128 (o);
1685 break;
1686 case DW_OP_GNU_deref_type:
1688 unsigned long o
1689 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1690 size += 1 + size_of_uleb128 (o);
1692 break;
1693 case DW_OP_GNU_convert:
1694 case DW_OP_GNU_reinterpret:
1695 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1696 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1697 else
1699 unsigned long o
1700 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1701 size += size_of_uleb128 (o);
1703 break;
1704 case DW_OP_GNU_parameter_ref:
1705 size += 4;
1706 break;
1707 default:
1708 break;
1711 return size;
1714 /* Return the size of a series of location descriptors. */
1716 unsigned long
1717 size_of_locs (dw_loc_descr_ref loc)
1719 dw_loc_descr_ref l;
1720 unsigned long size;
1722 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1723 field, to avoid writing to a PCH file. */
1724 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1726 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1727 break;
1728 size += size_of_loc_descr (l);
1730 if (! l)
1731 return size;
1733 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1735 l->dw_loc_addr = size;
1736 size += size_of_loc_descr (l);
1739 return size;
1742 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1743 static void get_ref_die_offset_label (char *, dw_die_ref);
1744 static unsigned long int get_ref_die_offset (dw_die_ref);
1746 /* Output location description stack opcode's operands (if any).
1747 The for_eh_or_skip parameter controls whether register numbers are
1748 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1749 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1750 info). This should be suppressed for the cases that have not been converted
1751 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
1753 static void
1754 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1756 dw_val_ref val1 = &loc->dw_loc_oprnd1;
1757 dw_val_ref val2 = &loc->dw_loc_oprnd2;
1759 switch (loc->dw_loc_opc)
1761 #ifdef DWARF2_DEBUGGING_INFO
1762 case DW_OP_const2u:
1763 case DW_OP_const2s:
1764 dw2_asm_output_data (2, val1->v.val_int, NULL);
1765 break;
1766 case DW_OP_const4u:
1767 if (loc->dtprel)
1769 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1770 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1771 val1->v.val_addr);
1772 fputc ('\n', asm_out_file);
1773 break;
1775 /* FALLTHRU */
1776 case DW_OP_const4s:
1777 dw2_asm_output_data (4, val1->v.val_int, NULL);
1778 break;
1779 case DW_OP_const8u:
1780 if (loc->dtprel)
1782 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1783 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1784 val1->v.val_addr);
1785 fputc ('\n', asm_out_file);
1786 break;
1788 /* FALLTHRU */
1789 case DW_OP_const8s:
1790 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
1791 dw2_asm_output_data (8, val1->v.val_int, NULL);
1792 break;
1793 case DW_OP_skip:
1794 case DW_OP_bra:
1796 int offset;
1798 gcc_assert (val1->val_class == dw_val_class_loc);
1799 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
1801 dw2_asm_output_data (2, offset, NULL);
1803 break;
1804 case DW_OP_implicit_value:
1805 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1806 switch (val2->val_class)
1808 case dw_val_class_const:
1809 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
1810 break;
1811 case dw_val_class_vec:
1813 unsigned int elt_size = val2->v.val_vec.elt_size;
1814 unsigned int len = val2->v.val_vec.length;
1815 unsigned int i;
1816 unsigned char *p;
1818 if (elt_size > sizeof (HOST_WIDE_INT))
1820 elt_size /= 2;
1821 len *= 2;
1823 for (i = 0, p = val2->v.val_vec.array;
1824 i < len;
1825 i++, p += elt_size)
1826 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
1827 "fp or vector constant word %u", i);
1829 break;
1830 case dw_val_class_const_double:
1832 unsigned HOST_WIDE_INT first, second;
1834 if (WORDS_BIG_ENDIAN)
1836 first = val2->v.val_double.high;
1837 second = val2->v.val_double.low;
1839 else
1841 first = val2->v.val_double.low;
1842 second = val2->v.val_double.high;
1844 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1845 first, NULL);
1846 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1847 second, NULL);
1849 break;
1850 case dw_val_class_wide_int:
1852 int i;
1853 int len = get_full_len (*val2->v.val_wide);
1854 if (WORDS_BIG_ENDIAN)
1855 for (i = len - 1; i >= 0; --i)
1856 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1857 val2->v.val_wide->elt (i), NULL);
1858 else
1859 for (i = 0; i < len; ++i)
1860 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1861 val2->v.val_wide->elt (i), NULL);
1863 break;
1864 case dw_val_class_addr:
1865 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
1866 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
1867 break;
1868 default:
1869 gcc_unreachable ();
1871 break;
1872 #else
1873 case DW_OP_const2u:
1874 case DW_OP_const2s:
1875 case DW_OP_const4u:
1876 case DW_OP_const4s:
1877 case DW_OP_const8u:
1878 case DW_OP_const8s:
1879 case DW_OP_skip:
1880 case DW_OP_bra:
1881 case DW_OP_implicit_value:
1882 /* We currently don't make any attempt to make sure these are
1883 aligned properly like we do for the main unwind info, so
1884 don't support emitting things larger than a byte if we're
1885 only doing unwinding. */
1886 gcc_unreachable ();
1887 #endif
1888 case DW_OP_const1u:
1889 case DW_OP_const1s:
1890 dw2_asm_output_data (1, val1->v.val_int, NULL);
1891 break;
1892 case DW_OP_constu:
1893 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1894 break;
1895 case DW_OP_consts:
1896 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1897 break;
1898 case DW_OP_pick:
1899 dw2_asm_output_data (1, val1->v.val_int, NULL);
1900 break;
1901 case DW_OP_plus_uconst:
1902 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1903 break;
1904 case DW_OP_breg0:
1905 case DW_OP_breg1:
1906 case DW_OP_breg2:
1907 case DW_OP_breg3:
1908 case DW_OP_breg4:
1909 case DW_OP_breg5:
1910 case DW_OP_breg6:
1911 case DW_OP_breg7:
1912 case DW_OP_breg8:
1913 case DW_OP_breg9:
1914 case DW_OP_breg10:
1915 case DW_OP_breg11:
1916 case DW_OP_breg12:
1917 case DW_OP_breg13:
1918 case DW_OP_breg14:
1919 case DW_OP_breg15:
1920 case DW_OP_breg16:
1921 case DW_OP_breg17:
1922 case DW_OP_breg18:
1923 case DW_OP_breg19:
1924 case DW_OP_breg20:
1925 case DW_OP_breg21:
1926 case DW_OP_breg22:
1927 case DW_OP_breg23:
1928 case DW_OP_breg24:
1929 case DW_OP_breg25:
1930 case DW_OP_breg26:
1931 case DW_OP_breg27:
1932 case DW_OP_breg28:
1933 case DW_OP_breg29:
1934 case DW_OP_breg30:
1935 case DW_OP_breg31:
1936 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1937 break;
1938 case DW_OP_regx:
1940 unsigned r = val1->v.val_unsigned;
1941 if (for_eh_or_skip >= 0)
1942 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1943 gcc_assert (size_of_uleb128 (r)
1944 == size_of_uleb128 (val1->v.val_unsigned));
1945 dw2_asm_output_data_uleb128 (r, NULL);
1947 break;
1948 case DW_OP_fbreg:
1949 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1950 break;
1951 case DW_OP_bregx:
1953 unsigned r = val1->v.val_unsigned;
1954 if (for_eh_or_skip >= 0)
1955 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1956 gcc_assert (size_of_uleb128 (r)
1957 == size_of_uleb128 (val1->v.val_unsigned));
1958 dw2_asm_output_data_uleb128 (r, NULL);
1959 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
1961 break;
1962 case DW_OP_piece:
1963 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1964 break;
1965 case DW_OP_bit_piece:
1966 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1967 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
1968 break;
1969 case DW_OP_deref_size:
1970 case DW_OP_xderef_size:
1971 dw2_asm_output_data (1, val1->v.val_int, NULL);
1972 break;
1974 case DW_OP_addr:
1975 if (loc->dtprel)
1977 if (targetm.asm_out.output_dwarf_dtprel)
1979 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
1980 DWARF2_ADDR_SIZE,
1981 val1->v.val_addr);
1982 fputc ('\n', asm_out_file);
1984 else
1985 gcc_unreachable ();
1987 else
1989 #ifdef DWARF2_DEBUGGING_INFO
1990 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
1991 #else
1992 gcc_unreachable ();
1993 #endif
1995 break;
1997 case DW_OP_GNU_addr_index:
1998 case DW_OP_GNU_const_index:
1999 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
2000 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
2001 "(index into .debug_addr)");
2002 break;
2004 case DW_OP_GNU_implicit_pointer:
2006 char label[MAX_ARTIFICIAL_LABEL_BYTES
2007 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2008 gcc_assert (val1->val_class == dw_val_class_die_ref);
2009 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2010 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2011 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2013 break;
2015 case DW_OP_GNU_entry_value:
2016 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2017 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2018 break;
2020 case DW_OP_GNU_const_type:
2022 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2023 gcc_assert (o);
2024 dw2_asm_output_data_uleb128 (o, NULL);
2025 switch (val2->val_class)
2027 case dw_val_class_const:
2028 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2029 dw2_asm_output_data (1, l, NULL);
2030 dw2_asm_output_data (l, val2->v.val_int, NULL);
2031 break;
2032 case dw_val_class_vec:
2034 unsigned int elt_size = val2->v.val_vec.elt_size;
2035 unsigned int len = val2->v.val_vec.length;
2036 unsigned int i;
2037 unsigned char *p;
2039 l = len * elt_size;
2040 dw2_asm_output_data (1, l, NULL);
2041 if (elt_size > sizeof (HOST_WIDE_INT))
2043 elt_size /= 2;
2044 len *= 2;
2046 for (i = 0, p = val2->v.val_vec.array;
2047 i < len;
2048 i++, p += elt_size)
2049 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2050 "fp or vector constant word %u", i);
2052 break;
2053 case dw_val_class_const_double:
2055 unsigned HOST_WIDE_INT first, second;
2056 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2058 dw2_asm_output_data (1, 2 * l, NULL);
2059 if (WORDS_BIG_ENDIAN)
2061 first = val2->v.val_double.high;
2062 second = val2->v.val_double.low;
2064 else
2066 first = val2->v.val_double.low;
2067 second = val2->v.val_double.high;
2069 dw2_asm_output_data (l, first, NULL);
2070 dw2_asm_output_data (l, second, NULL);
2072 break;
2073 case dw_val_class_wide_int:
2075 int i;
2076 int len = get_full_len (*val2->v.val_wide);
2077 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2079 dw2_asm_output_data (1, len * l, NULL);
2080 if (WORDS_BIG_ENDIAN)
2081 for (i = len - 1; i >= 0; --i)
2082 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2083 else
2084 for (i = 0; i < len; ++i)
2085 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2087 break;
2088 default:
2089 gcc_unreachable ();
2092 break;
2093 case DW_OP_GNU_regval_type:
2095 unsigned r = val1->v.val_unsigned;
2096 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2097 gcc_assert (o);
2098 if (for_eh_or_skip >= 0)
2100 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2101 gcc_assert (size_of_uleb128 (r)
2102 == size_of_uleb128 (val1->v.val_unsigned));
2104 dw2_asm_output_data_uleb128 (r, NULL);
2105 dw2_asm_output_data_uleb128 (o, NULL);
2107 break;
2108 case DW_OP_GNU_deref_type:
2110 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2111 gcc_assert (o);
2112 dw2_asm_output_data (1, val1->v.val_int, NULL);
2113 dw2_asm_output_data_uleb128 (o, NULL);
2115 break;
2116 case DW_OP_GNU_convert:
2117 case DW_OP_GNU_reinterpret:
2118 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2119 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2120 else
2122 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2123 gcc_assert (o);
2124 dw2_asm_output_data_uleb128 (o, NULL);
2126 break;
2128 case DW_OP_GNU_parameter_ref:
2130 unsigned long o;
2131 gcc_assert (val1->val_class == dw_val_class_die_ref);
2132 o = get_ref_die_offset (val1->v.val_die_ref.die);
2133 dw2_asm_output_data (4, o, NULL);
2135 break;
2137 default:
2138 /* Other codes have no operands. */
2139 break;
2143 /* Output a sequence of location operations.
2144 The for_eh_or_skip parameter controls whether register numbers are
2145 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2146 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2147 info). This should be suppressed for the cases that have not been converted
2148 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2150 void
2151 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2153 for (; loc != NULL; loc = loc->dw_loc_next)
2155 enum dwarf_location_atom opc = loc->dw_loc_opc;
2156 /* Output the opcode. */
2157 if (for_eh_or_skip >= 0
2158 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2160 unsigned r = (opc - DW_OP_breg0);
2161 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2162 gcc_assert (r <= 31);
2163 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2165 else if (for_eh_or_skip >= 0
2166 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2168 unsigned r = (opc - DW_OP_reg0);
2169 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2170 gcc_assert (r <= 31);
2171 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2174 dw2_asm_output_data (1, opc,
2175 "%s", dwarf_stack_op_name (opc));
2177 /* Output the operand(s) (if any). */
2178 output_loc_operands (loc, for_eh_or_skip);
2182 /* Output location description stack opcode's operands (if any).
2183 The output is single bytes on a line, suitable for .cfi_escape. */
2185 static void
2186 output_loc_operands_raw (dw_loc_descr_ref loc)
2188 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2189 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2191 switch (loc->dw_loc_opc)
2193 case DW_OP_addr:
2194 case DW_OP_GNU_addr_index:
2195 case DW_OP_GNU_const_index:
2196 case DW_OP_implicit_value:
2197 /* We cannot output addresses in .cfi_escape, only bytes. */
2198 gcc_unreachable ();
2200 case DW_OP_const1u:
2201 case DW_OP_const1s:
2202 case DW_OP_pick:
2203 case DW_OP_deref_size:
2204 case DW_OP_xderef_size:
2205 fputc (',', asm_out_file);
2206 dw2_asm_output_data_raw (1, val1->v.val_int);
2207 break;
2209 case DW_OP_const2u:
2210 case DW_OP_const2s:
2211 fputc (',', asm_out_file);
2212 dw2_asm_output_data_raw (2, val1->v.val_int);
2213 break;
2215 case DW_OP_const4u:
2216 case DW_OP_const4s:
2217 fputc (',', asm_out_file);
2218 dw2_asm_output_data_raw (4, val1->v.val_int);
2219 break;
2221 case DW_OP_const8u:
2222 case DW_OP_const8s:
2223 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2224 fputc (',', asm_out_file);
2225 dw2_asm_output_data_raw (8, val1->v.val_int);
2226 break;
2228 case DW_OP_skip:
2229 case DW_OP_bra:
2231 int offset;
2233 gcc_assert (val1->val_class == dw_val_class_loc);
2234 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2236 fputc (',', asm_out_file);
2237 dw2_asm_output_data_raw (2, offset);
2239 break;
2241 case DW_OP_regx:
2243 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2244 gcc_assert (size_of_uleb128 (r)
2245 == size_of_uleb128 (val1->v.val_unsigned));
2246 fputc (',', asm_out_file);
2247 dw2_asm_output_data_uleb128_raw (r);
2249 break;
2251 case DW_OP_constu:
2252 case DW_OP_plus_uconst:
2253 case DW_OP_piece:
2254 fputc (',', asm_out_file);
2255 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2256 break;
2258 case DW_OP_bit_piece:
2259 fputc (',', asm_out_file);
2260 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2261 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2262 break;
2264 case DW_OP_consts:
2265 case DW_OP_breg0:
2266 case DW_OP_breg1:
2267 case DW_OP_breg2:
2268 case DW_OP_breg3:
2269 case DW_OP_breg4:
2270 case DW_OP_breg5:
2271 case DW_OP_breg6:
2272 case DW_OP_breg7:
2273 case DW_OP_breg8:
2274 case DW_OP_breg9:
2275 case DW_OP_breg10:
2276 case DW_OP_breg11:
2277 case DW_OP_breg12:
2278 case DW_OP_breg13:
2279 case DW_OP_breg14:
2280 case DW_OP_breg15:
2281 case DW_OP_breg16:
2282 case DW_OP_breg17:
2283 case DW_OP_breg18:
2284 case DW_OP_breg19:
2285 case DW_OP_breg20:
2286 case DW_OP_breg21:
2287 case DW_OP_breg22:
2288 case DW_OP_breg23:
2289 case DW_OP_breg24:
2290 case DW_OP_breg25:
2291 case DW_OP_breg26:
2292 case DW_OP_breg27:
2293 case DW_OP_breg28:
2294 case DW_OP_breg29:
2295 case DW_OP_breg30:
2296 case DW_OP_breg31:
2297 case DW_OP_fbreg:
2298 fputc (',', asm_out_file);
2299 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2300 break;
2302 case DW_OP_bregx:
2304 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2305 gcc_assert (size_of_uleb128 (r)
2306 == size_of_uleb128 (val1->v.val_unsigned));
2307 fputc (',', asm_out_file);
2308 dw2_asm_output_data_uleb128_raw (r);
2309 fputc (',', asm_out_file);
2310 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2312 break;
2314 case DW_OP_GNU_implicit_pointer:
2315 case DW_OP_GNU_entry_value:
2316 case DW_OP_GNU_const_type:
2317 case DW_OP_GNU_regval_type:
2318 case DW_OP_GNU_deref_type:
2319 case DW_OP_GNU_convert:
2320 case DW_OP_GNU_reinterpret:
2321 case DW_OP_GNU_parameter_ref:
2322 gcc_unreachable ();
2323 break;
2325 default:
2326 /* Other codes have no operands. */
2327 break;
2331 void
2332 output_loc_sequence_raw (dw_loc_descr_ref loc)
2334 while (1)
2336 enum dwarf_location_atom opc = loc->dw_loc_opc;
2337 /* Output the opcode. */
2338 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2340 unsigned r = (opc - DW_OP_breg0);
2341 r = DWARF2_FRAME_REG_OUT (r, 1);
2342 gcc_assert (r <= 31);
2343 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2345 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2347 unsigned r = (opc - DW_OP_reg0);
2348 r = DWARF2_FRAME_REG_OUT (r, 1);
2349 gcc_assert (r <= 31);
2350 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2352 /* Output the opcode. */
2353 fprintf (asm_out_file, "%#x", opc);
2354 output_loc_operands_raw (loc);
2356 if (!loc->dw_loc_next)
2357 break;
2358 loc = loc->dw_loc_next;
2360 fputc (',', asm_out_file);
2364 /* This function builds a dwarf location descriptor sequence from a
2365 dw_cfa_location, adding the given OFFSET to the result of the
2366 expression. */
2368 struct dw_loc_descr_node *
2369 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2371 struct dw_loc_descr_node *head, *tmp;
2373 offset += cfa->offset;
2375 if (cfa->indirect)
2377 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2378 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2379 head->dw_loc_oprnd1.val_entry = NULL;
2380 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2381 add_loc_descr (&head, tmp);
2382 if (offset != 0)
2384 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2385 add_loc_descr (&head, tmp);
2388 else
2389 head = new_reg_loc_descr (cfa->reg, offset);
2391 return head;
2394 /* This function builds a dwarf location descriptor sequence for
2395 the address at OFFSET from the CFA when stack is aligned to
2396 ALIGNMENT byte. */
2398 struct dw_loc_descr_node *
2399 build_cfa_aligned_loc (dw_cfa_location *cfa,
2400 HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2402 struct dw_loc_descr_node *head;
2403 unsigned int dwarf_fp
2404 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2406 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2407 if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2409 head = new_reg_loc_descr (dwarf_fp, 0);
2410 add_loc_descr (&head, int_loc_descriptor (alignment));
2411 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2412 loc_descr_plus_const (&head, offset);
2414 else
2415 head = new_reg_loc_descr (dwarf_fp, offset);
2416 return head;
2419 /* And now, the support for symbolic debugging information. */
2421 /* .debug_str support. */
2423 static void dwarf2out_init (const char *);
2424 static void dwarf2out_finish (const char *);
2425 static void dwarf2out_early_finish (void);
2426 static void dwarf2out_assembly_start (void);
2427 static void dwarf2out_define (unsigned int, const char *);
2428 static void dwarf2out_undef (unsigned int, const char *);
2429 static void dwarf2out_start_source_file (unsigned, const char *);
2430 static void dwarf2out_end_source_file (unsigned);
2431 static void dwarf2out_function_decl (tree);
2432 static void dwarf2out_begin_block (unsigned, unsigned);
2433 static void dwarf2out_end_block (unsigned, unsigned);
2434 static bool dwarf2out_ignore_block (const_tree);
2435 static void dwarf2out_early_global_decl (tree);
2436 static void dwarf2out_late_global_decl (tree);
2437 static void dwarf2out_type_decl (tree, int);
2438 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
2439 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2440 dw_die_ref);
2441 static void dwarf2out_abstract_function (tree);
2442 static void dwarf2out_var_location (rtx_insn *);
2443 static void dwarf2out_begin_function (tree);
2444 static void dwarf2out_end_function (unsigned int);
2445 static void dwarf2out_register_main_translation_unit (tree unit);
2446 static void dwarf2out_set_name (tree, tree);
2448 /* The debug hooks structure. */
2450 const struct gcc_debug_hooks dwarf2_debug_hooks =
2452 dwarf2out_init,
2453 dwarf2out_finish,
2454 dwarf2out_early_finish,
2455 dwarf2out_assembly_start,
2456 dwarf2out_define,
2457 dwarf2out_undef,
2458 dwarf2out_start_source_file,
2459 dwarf2out_end_source_file,
2460 dwarf2out_begin_block,
2461 dwarf2out_end_block,
2462 dwarf2out_ignore_block,
2463 dwarf2out_source_line,
2464 dwarf2out_begin_prologue,
2465 #if VMS_DEBUGGING_INFO
2466 dwarf2out_vms_end_prologue,
2467 dwarf2out_vms_begin_epilogue,
2468 #else
2469 debug_nothing_int_charstar,
2470 debug_nothing_int_charstar,
2471 #endif
2472 dwarf2out_end_epilogue,
2473 dwarf2out_begin_function,
2474 dwarf2out_end_function, /* end_function */
2475 dwarf2out_register_main_translation_unit,
2476 dwarf2out_function_decl, /* function_decl */
2477 dwarf2out_early_global_decl,
2478 dwarf2out_late_global_decl,
2479 dwarf2out_type_decl, /* type_decl */
2480 dwarf2out_imported_module_or_decl,
2481 debug_nothing_tree, /* deferred_inline_function */
2482 /* The DWARF 2 backend tries to reduce debugging bloat by not
2483 emitting the abstract description of inline functions until
2484 something tries to reference them. */
2485 dwarf2out_abstract_function, /* outlining_inline_function */
2486 debug_nothing_rtx_code_label, /* label */
2487 debug_nothing_int, /* handle_pch */
2488 dwarf2out_var_location,
2489 dwarf2out_switch_text_section,
2490 dwarf2out_set_name,
2491 1, /* start_end_main_source_file */
2492 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2495 /* NOTE: In the comments in this file, many references are made to
2496 "Debugging Information Entries". This term is abbreviated as `DIE'
2497 throughout the remainder of this file. */
2499 /* An internal representation of the DWARF output is built, and then
2500 walked to generate the DWARF debugging info. The walk of the internal
2501 representation is done after the entire program has been compiled.
2502 The types below are used to describe the internal representation. */
2504 /* Whether to put type DIEs into their own section .debug_types instead
2505 of making them part of the .debug_info section. Only supported for
2506 Dwarf V4 or higher and the user didn't disable them through
2507 -fno-debug-types-section. It is more efficient to put them in a
2508 separate comdat sections since the linker will then be able to
2509 remove duplicates. But not all tools support .debug_types sections
2510 yet. */
2512 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2514 /* Various DIE's use offsets relative to the beginning of the
2515 .debug_info section to refer to each other. */
2517 typedef long int dw_offset;
2519 /* Define typedefs here to avoid circular dependencies. */
2521 typedef struct dw_attr_struct *dw_attr_ref;
2522 typedef struct dw_line_info_struct *dw_line_info_ref;
2523 typedef struct pubname_struct *pubname_ref;
2524 typedef struct dw_ranges_struct *dw_ranges_ref;
2525 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
2526 typedef struct comdat_type_struct *comdat_type_node_ref;
2528 /* The entries in the line_info table more-or-less mirror the opcodes
2529 that are used in the real dwarf line table. Arrays of these entries
2530 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2531 supported. */
2533 enum dw_line_info_opcode {
2534 /* Emit DW_LNE_set_address; the operand is the label index. */
2535 LI_set_address,
2537 /* Emit a row to the matrix with the given line. This may be done
2538 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2539 special opcodes. */
2540 LI_set_line,
2542 /* Emit a DW_LNS_set_file. */
2543 LI_set_file,
2545 /* Emit a DW_LNS_set_column. */
2546 LI_set_column,
2548 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
2549 LI_negate_stmt,
2551 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
2552 LI_set_prologue_end,
2553 LI_set_epilogue_begin,
2555 /* Emit a DW_LNE_set_discriminator. */
2556 LI_set_discriminator
2559 typedef struct GTY(()) dw_line_info_struct {
2560 enum dw_line_info_opcode opcode;
2561 unsigned int val;
2562 } dw_line_info_entry;
2565 typedef struct GTY(()) dw_line_info_table_struct {
2566 /* The label that marks the end of this section. */
2567 const char *end_label;
2569 /* The values for the last row of the matrix, as collected in the table.
2570 These are used to minimize the changes to the next row. */
2571 unsigned int file_num;
2572 unsigned int line_num;
2573 unsigned int column_num;
2574 int discrim_num;
2575 bool is_stmt;
2576 bool in_use;
2578 vec<dw_line_info_entry, va_gc> *entries;
2579 } dw_line_info_table;
2581 typedef dw_line_info_table *dw_line_info_table_p;
2584 /* Each DIE attribute has a field specifying the attribute kind,
2585 a link to the next attribute in the chain, and an attribute value.
2586 Attributes are typically linked below the DIE they modify. */
2588 typedef struct GTY(()) dw_attr_struct {
2589 enum dwarf_attribute dw_attr;
2590 dw_val_node dw_attr_val;
2592 dw_attr_node;
2595 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
2596 The children of each node form a circular list linked by
2597 die_sib. die_child points to the node *before* the "first" child node. */
2599 typedef struct GTY((chain_circular ("%h.die_sib"), for_user)) die_struct {
2600 union die_symbol_or_type_node
2602 const char * GTY ((tag ("0"))) die_symbol;
2603 comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
2605 GTY ((desc ("%0.comdat_type_p"))) die_id;
2606 vec<dw_attr_node, va_gc> *die_attr;
2607 dw_die_ref die_parent;
2608 dw_die_ref die_child;
2609 dw_die_ref die_sib;
2610 dw_die_ref die_definition; /* ref from a specification to its definition */
2611 dw_offset die_offset;
2612 unsigned long die_abbrev;
2613 int die_mark;
2614 unsigned int decl_id;
2615 enum dwarf_tag die_tag;
2616 /* Die is used and must not be pruned as unused. */
2617 BOOL_BITFIELD die_perennial_p : 1;
2618 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2619 /* Lots of spare bits. */
2621 die_node;
2623 /* Set to TRUE while dwarf2out_early_global_decl is running. */
2624 static bool early_dwarf;
2625 struct set_early_dwarf {
2626 bool saved;
2627 set_early_dwarf () : saved(early_dwarf) { early_dwarf = true; }
2628 ~set_early_dwarf () { early_dwarf = saved; }
2631 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
2632 #define FOR_EACH_CHILD(die, c, expr) do { \
2633 c = die->die_child; \
2634 if (c) do { \
2635 c = c->die_sib; \
2636 expr; \
2637 } while (c != die->die_child); \
2638 } while (0)
2640 /* The pubname structure */
2642 typedef struct GTY(()) pubname_struct {
2643 dw_die_ref die;
2644 const char *name;
2646 pubname_entry;
2649 struct GTY(()) dw_ranges_struct {
2650 /* If this is positive, it's a block number, otherwise it's a
2651 bitwise-negated index into dw_ranges_by_label. */
2652 int num;
2655 /* A structure to hold a macinfo entry. */
2657 typedef struct GTY(()) macinfo_struct {
2658 unsigned char code;
2659 unsigned HOST_WIDE_INT lineno;
2660 const char *info;
2662 macinfo_entry;
2665 struct GTY(()) dw_ranges_by_label_struct {
2666 const char *begin;
2667 const char *end;
2670 /* The comdat type node structure. */
2671 typedef struct GTY(()) comdat_type_struct
2673 dw_die_ref root_die;
2674 dw_die_ref type_die;
2675 dw_die_ref skeleton_die;
2676 char signature[DWARF_TYPE_SIGNATURE_SIZE];
2677 struct comdat_type_struct *next;
2679 comdat_type_node;
2681 /* A list of DIEs for which we can't determine ancestry (parent_die
2682 field) just yet. Later in dwarf2out_finish we will fill in the
2683 missing bits. */
2684 typedef struct GTY(()) limbo_die_struct {
2685 dw_die_ref die;
2686 /* The tree for which this DIE was created. We use this to
2687 determine ancestry later. */
2688 tree created_for;
2689 struct limbo_die_struct *next;
2691 limbo_die_node;
2693 typedef struct skeleton_chain_struct
2695 dw_die_ref old_die;
2696 dw_die_ref new_die;
2697 struct skeleton_chain_struct *parent;
2699 skeleton_chain_node;
2701 /* Define a macro which returns nonzero for a TYPE_DECL which was
2702 implicitly generated for a type.
2704 Note that, unlike the C front-end (which generates a NULL named
2705 TYPE_DECL node for each complete tagged type, each array type,
2706 and each function type node created) the C++ front-end generates
2707 a _named_ TYPE_DECL node for each tagged type node created.
2708 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
2709 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
2710 front-end, but for each type, tagged or not. */
2712 #define TYPE_DECL_IS_STUB(decl) \
2713 (DECL_NAME (decl) == NULL_TREE \
2714 || (DECL_ARTIFICIAL (decl) \
2715 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
2716 /* This is necessary for stub decls that \
2717 appear in nested inline functions. */ \
2718 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
2719 && (decl_ultimate_origin (decl) \
2720 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
2722 /* Information concerning the compilation unit's programming
2723 language, and compiler version. */
2725 /* Fixed size portion of the DWARF compilation unit header. */
2726 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
2727 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
2729 /* Fixed size portion of the DWARF comdat type unit header. */
2730 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
2731 (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
2732 + DWARF_OFFSET_SIZE)
2734 /* Fixed size portion of public names info. */
2735 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
2737 /* Fixed size portion of the address range info. */
2738 #define DWARF_ARANGES_HEADER_SIZE \
2739 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2740 DWARF2_ADDR_SIZE * 2) \
2741 - DWARF_INITIAL_LENGTH_SIZE)
2743 /* Size of padding portion in the address range info. It must be
2744 aligned to twice the pointer size. */
2745 #define DWARF_ARANGES_PAD_SIZE \
2746 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2747 DWARF2_ADDR_SIZE * 2) \
2748 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
2750 /* Use assembler line directives if available. */
2751 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
2752 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
2753 #define DWARF2_ASM_LINE_DEBUG_INFO 1
2754 #else
2755 #define DWARF2_ASM_LINE_DEBUG_INFO 0
2756 #endif
2757 #endif
2759 /* Minimum line offset in a special line info. opcode.
2760 This value was chosen to give a reasonable range of values. */
2761 #define DWARF_LINE_BASE -10
2763 /* First special line opcode - leave room for the standard opcodes. */
2764 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
2766 /* Range of line offsets in a special line info. opcode. */
2767 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
2769 /* Flag that indicates the initial value of the is_stmt_start flag.
2770 In the present implementation, we do not mark any lines as
2771 the beginning of a source statement, because that information
2772 is not made available by the GCC front-end. */
2773 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
2775 /* Maximum number of operations per instruction bundle. */
2776 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
2777 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
2778 #endif
2780 /* This location is used by calc_die_sizes() to keep track
2781 the offset of each DIE within the .debug_info section. */
2782 static unsigned long next_die_offset;
2784 /* Record the root of the DIE's built for the current compilation unit. */
2785 static GTY(()) dw_die_ref single_comp_unit_die;
2787 /* A list of type DIEs that have been separated into comdat sections. */
2788 static GTY(()) comdat_type_node *comdat_type_list;
2790 /* A list of DIEs with a NULL parent waiting to be relocated. */
2791 static GTY(()) limbo_die_node *limbo_die_list;
2793 /* A list of DIEs for which we may have to generate
2794 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
2795 static GTY(()) limbo_die_node *deferred_asm_name;
2797 struct dwarf_file_hasher : ggc_ptr_hash<dwarf_file_data>
2799 typedef const char *compare_type;
2801 static hashval_t hash (dwarf_file_data *);
2802 static bool equal (dwarf_file_data *, const char *);
2805 /* Filenames referenced by this compilation unit. */
2806 static GTY(()) hash_table<dwarf_file_hasher> *file_table;
2808 struct decl_die_hasher : ggc_ptr_hash<die_node>
2810 typedef tree compare_type;
2812 static hashval_t hash (die_node *);
2813 static bool equal (die_node *, tree);
2815 /* A hash table of references to DIE's that describe declarations.
2816 The key is a DECL_UID() which is a unique number identifying each decl. */
2817 static GTY (()) hash_table<decl_die_hasher> *decl_die_table;
2819 struct block_die_hasher : ggc_ptr_hash<die_struct>
2821 static hashval_t hash (die_struct *);
2822 static bool equal (die_struct *, die_struct *);
2825 /* A hash table of references to DIE's that describe COMMON blocks.
2826 The key is DECL_UID() ^ die_parent. */
2827 static GTY (()) hash_table<block_die_hasher> *common_block_die_table;
2829 typedef struct GTY(()) die_arg_entry_struct {
2830 dw_die_ref die;
2831 tree arg;
2832 } die_arg_entry;
2835 /* Node of the variable location list. */
2836 struct GTY ((chain_next ("%h.next"))) var_loc_node {
2837 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
2838 EXPR_LIST chain. For small bitsizes, bitsize is encoded
2839 in mode of the EXPR_LIST node and first EXPR_LIST operand
2840 is either NOTE_INSN_VAR_LOCATION for a piece with a known
2841 location or NULL for padding. For larger bitsizes,
2842 mode is 0 and first operand is a CONCAT with bitsize
2843 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
2844 NULL as second operand. */
2845 rtx GTY (()) loc;
2846 const char * GTY (()) label;
2847 struct var_loc_node * GTY (()) next;
2850 /* Variable location list. */
2851 struct GTY ((for_user)) var_loc_list_def {
2852 struct var_loc_node * GTY (()) first;
2854 /* Pointer to the last but one or last element of the
2855 chained list. If the list is empty, both first and
2856 last are NULL, if the list contains just one node
2857 or the last node certainly is not redundant, it points
2858 to the last node, otherwise points to the last but one.
2859 Do not mark it for GC because it is marked through the chain. */
2860 struct var_loc_node * GTY ((skip ("%h"))) last;
2862 /* Pointer to the last element before section switch,
2863 if NULL, either sections weren't switched or first
2864 is after section switch. */
2865 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
2867 /* DECL_UID of the variable decl. */
2868 unsigned int decl_id;
2870 typedef struct var_loc_list_def var_loc_list;
2872 /* Call argument location list. */
2873 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
2874 rtx GTY (()) call_arg_loc_note;
2875 const char * GTY (()) label;
2876 tree GTY (()) block;
2877 bool tail_call_p;
2878 rtx GTY (()) symbol_ref;
2879 struct call_arg_loc_node * GTY (()) next;
2883 struct decl_loc_hasher : ggc_ptr_hash<var_loc_list>
2885 typedef const_tree compare_type;
2887 static hashval_t hash (var_loc_list *);
2888 static bool equal (var_loc_list *, const_tree);
2891 /* Table of decl location linked lists. */
2892 static GTY (()) hash_table<decl_loc_hasher> *decl_loc_table;
2894 /* Head and tail of call_arg_loc chain. */
2895 static GTY (()) struct call_arg_loc_node *call_arg_locations;
2896 static struct call_arg_loc_node *call_arg_loc_last;
2898 /* Number of call sites in the current function. */
2899 static int call_site_count = -1;
2900 /* Number of tail call sites in the current function. */
2901 static int tail_call_site_count = -1;
2903 /* A cached location list. */
2904 struct GTY ((for_user)) cached_dw_loc_list_def {
2905 /* The DECL_UID of the decl that this entry describes. */
2906 unsigned int decl_id;
2908 /* The cached location list. */
2909 dw_loc_list_ref loc_list;
2911 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
2913 struct dw_loc_list_hasher : ggc_ptr_hash<cached_dw_loc_list>
2916 typedef const_tree compare_type;
2918 static hashval_t hash (cached_dw_loc_list *);
2919 static bool equal (cached_dw_loc_list *, const_tree);
2922 /* Table of cached location lists. */
2923 static GTY (()) hash_table<dw_loc_list_hasher> *cached_dw_loc_list_table;
2925 /* A pointer to the base of a list of references to DIE's that
2926 are uniquely identified by their tag, presence/absence of
2927 children DIE's, and list of attribute/value pairs. */
2928 static GTY((length ("abbrev_die_table_allocated")))
2929 dw_die_ref *abbrev_die_table;
2931 /* Number of elements currently allocated for abbrev_die_table. */
2932 static GTY(()) unsigned abbrev_die_table_allocated;
2934 /* Number of elements in abbrev_die_table currently in use. */
2935 static GTY(()) unsigned abbrev_die_table_in_use;
2937 /* Size (in elements) of increments by which we may expand the
2938 abbrev_die_table. */
2939 #define ABBREV_DIE_TABLE_INCREMENT 256
2941 /* A global counter for generating labels for line number data. */
2942 static unsigned int line_info_label_num;
2944 /* The current table to which we should emit line number information
2945 for the current function. This will be set up at the beginning of
2946 assembly for the function. */
2947 static dw_line_info_table *cur_line_info_table;
2949 /* The two default tables of line number info. */
2950 static GTY(()) dw_line_info_table *text_section_line_info;
2951 static GTY(()) dw_line_info_table *cold_text_section_line_info;
2953 /* The set of all non-default tables of line number info. */
2954 static GTY(()) vec<dw_line_info_table_p, va_gc> *separate_line_info;
2956 /* A flag to tell pubnames/types export if there is an info section to
2957 refer to. */
2958 static bool info_section_emitted;
2960 /* A pointer to the base of a table that contains a list of publicly
2961 accessible names. */
2962 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
2964 /* A pointer to the base of a table that contains a list of publicly
2965 accessible types. */
2966 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
2968 /* A pointer to the base of a table that contains a list of macro
2969 defines/undefines (and file start/end markers). */
2970 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
2972 /* True if .debug_macinfo or .debug_macros section is going to be
2973 emitted. */
2974 #define have_macinfo \
2975 (debug_info_level >= DINFO_LEVEL_VERBOSE \
2976 && !macinfo_table->is_empty ())
2978 /* Array of dies for which we should generate .debug_ranges info. */
2979 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
2981 /* Number of elements currently allocated for ranges_table. */
2982 static GTY(()) unsigned ranges_table_allocated;
2984 /* Number of elements in ranges_table currently in use. */
2985 static GTY(()) unsigned ranges_table_in_use;
2987 /* Array of pairs of labels referenced in ranges_table. */
2988 static GTY ((length ("ranges_by_label_allocated")))
2989 dw_ranges_by_label_ref ranges_by_label;
2991 /* Number of elements currently allocated for ranges_by_label. */
2992 static GTY(()) unsigned ranges_by_label_allocated;
2994 /* Number of elements in ranges_by_label currently in use. */
2995 static GTY(()) unsigned ranges_by_label_in_use;
2997 /* Size (in elements) of increments by which we may expand the
2998 ranges_table. */
2999 #define RANGES_TABLE_INCREMENT 64
3001 /* Whether we have location lists that need outputting */
3002 static GTY(()) bool have_location_lists;
3004 /* Unique label counter. */
3005 static GTY(()) unsigned int loclabel_num;
3007 /* Unique label counter for point-of-call tables. */
3008 static GTY(()) unsigned int poc_label_num;
3010 /* The last file entry emitted by maybe_emit_file(). */
3011 static GTY(()) struct dwarf_file_data * last_emitted_file;
3013 /* Number of internal labels generated by gen_internal_sym(). */
3014 static GTY(()) int label_num;
3016 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
3018 /* Instances of generic types for which we need to generate debug
3019 info that describe their generic parameters and arguments. That
3020 generation needs to happen once all types are properly laid out so
3021 we do it at the end of compilation. */
3022 static GTY(()) vec<tree, va_gc> *generic_type_instances;
3024 /* Offset from the "steady-state frame pointer" to the frame base,
3025 within the current function. */
3026 static HOST_WIDE_INT frame_pointer_fb_offset;
3027 static bool frame_pointer_fb_offset_valid;
3029 static vec<dw_die_ref> base_types;
3031 /* Flags to represent a set of attribute classes for attributes that represent
3032 a scalar value (bounds, pointers, ...). */
3033 enum dw_scalar_form
3035 dw_scalar_form_constant = 0x01,
3036 dw_scalar_form_exprloc = 0x02,
3037 dw_scalar_form_reference = 0x04
3040 /* Forward declarations for functions defined in this file. */
3042 static int is_pseudo_reg (const_rtx);
3043 static tree type_main_variant (tree);
3044 static int is_tagged_type (const_tree);
3045 static const char *dwarf_tag_name (unsigned);
3046 static const char *dwarf_attr_name (unsigned);
3047 static const char *dwarf_form_name (unsigned);
3048 static tree decl_ultimate_origin (const_tree);
3049 static tree decl_class_context (tree);
3050 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
3051 static inline enum dw_val_class AT_class (dw_attr_ref);
3052 static inline unsigned int AT_index (dw_attr_ref);
3053 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3054 static inline unsigned AT_flag (dw_attr_ref);
3055 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3056 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
3057 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3058 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
3059 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3060 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3061 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3062 unsigned int, unsigned char *);
3063 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3064 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3065 static inline const char *AT_string (dw_attr_ref);
3066 static enum dwarf_form AT_string_form (dw_attr_ref);
3067 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3068 static void add_AT_specification (dw_die_ref, dw_die_ref);
3069 static inline dw_die_ref AT_ref (dw_attr_ref);
3070 static inline int AT_ref_external (dw_attr_ref);
3071 static inline void set_AT_ref_external (dw_attr_ref, int);
3072 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3073 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3074 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
3075 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3076 dw_loc_list_ref);
3077 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
3078 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3079 static void remove_addr_table_entry (addr_table_entry *);
3080 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3081 static inline rtx AT_addr (dw_attr_ref);
3082 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3083 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3084 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3085 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3086 unsigned HOST_WIDE_INT);
3087 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3088 unsigned long, bool);
3089 static inline const char *AT_lbl (dw_attr_ref);
3090 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
3091 static const char *get_AT_low_pc (dw_die_ref);
3092 static const char *get_AT_hi_pc (dw_die_ref);
3093 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3094 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3095 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3096 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3097 static bool is_cxx (void);
3098 static bool is_fortran (void);
3099 static bool is_ada (void);
3100 static bool remove_AT (dw_die_ref, enum dwarf_attribute);
3101 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3102 static void add_child_die (dw_die_ref, dw_die_ref);
3103 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3104 static dw_die_ref lookup_type_die (tree);
3105 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3106 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3107 static void equate_type_number_to_die (tree, dw_die_ref);
3108 static dw_die_ref lookup_decl_die (tree);
3109 static var_loc_list *lookup_decl_loc (const_tree);
3110 static void equate_decl_number_to_die (tree, dw_die_ref);
3111 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3112 static void print_spaces (FILE *);
3113 static void print_die (dw_die_ref, FILE *);
3114 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3115 static dw_die_ref pop_compile_unit (dw_die_ref);
3116 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3117 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
3118 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3119 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3120 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3121 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3122 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
3123 struct md5_ctx *, int *);
3124 struct checksum_attributes;
3125 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3126 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3127 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3128 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3129 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3130 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3131 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
3132 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3133 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3134 static void compute_section_prefix (dw_die_ref);
3135 static int is_type_die (dw_die_ref);
3136 static int is_comdat_die (dw_die_ref);
3137 static int is_symbol_die (dw_die_ref);
3138 static inline bool is_template_instantiation (dw_die_ref);
3139 static void assign_symbol_names (dw_die_ref);
3140 static void break_out_includes (dw_die_ref);
3141 static int is_declaration_die (dw_die_ref);
3142 static int should_move_die_to_comdat (dw_die_ref);
3143 static dw_die_ref clone_as_declaration (dw_die_ref);
3144 static dw_die_ref clone_die (dw_die_ref);
3145 static dw_die_ref clone_tree (dw_die_ref);
3146 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3147 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3148 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3149 static dw_die_ref generate_skeleton (dw_die_ref);
3150 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3151 dw_die_ref,
3152 dw_die_ref);
3153 static void break_out_comdat_types (dw_die_ref);
3154 static void copy_decls_for_unworthy_types (dw_die_ref);
3156 static void add_sibling_attributes (dw_die_ref);
3157 static void output_location_lists (dw_die_ref);
3158 static int constant_size (unsigned HOST_WIDE_INT);
3159 static unsigned long size_of_die (dw_die_ref);
3160 static void calc_die_sizes (dw_die_ref);
3161 static void calc_base_type_die_sizes (void);
3162 static void mark_dies (dw_die_ref);
3163 static void unmark_dies (dw_die_ref);
3164 static void unmark_all_dies (dw_die_ref);
3165 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3166 static unsigned long size_of_aranges (void);
3167 static enum dwarf_form value_format (dw_attr_ref);
3168 static void output_value_format (dw_attr_ref);
3169 static void output_abbrev_section (void);
3170 static void output_die_abbrevs (unsigned long, dw_die_ref);
3171 static void output_die_symbol (dw_die_ref);
3172 static void output_die (dw_die_ref);
3173 static void output_compilation_unit_header (void);
3174 static void output_comp_unit (dw_die_ref, int);
3175 static void output_comdat_type_unit (comdat_type_node *);
3176 static const char *dwarf2_name (tree, int);
3177 static void add_pubname (tree, dw_die_ref);
3178 static void add_enumerator_pubname (const char *, dw_die_ref);
3179 static void add_pubname_string (const char *, dw_die_ref);
3180 static void add_pubtype (tree, dw_die_ref);
3181 static void output_pubnames (vec<pubname_entry, va_gc> *);
3182 static void output_aranges (unsigned long);
3183 static unsigned int add_ranges_num (int);
3184 static unsigned int add_ranges (const_tree);
3185 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3186 bool *, bool);
3187 static void output_ranges (void);
3188 static dw_line_info_table *new_line_info_table (void);
3189 static void output_line_info (bool);
3190 static void output_file_names (void);
3191 static dw_die_ref base_type_die (tree);
3192 static int is_base_type (tree);
3193 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
3194 static int decl_quals (const_tree);
3195 static dw_die_ref modified_type_die (tree, int, dw_die_ref);
3196 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3197 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3198 static int type_is_enum (const_tree);
3199 static unsigned int dbx_reg_number (const_rtx);
3200 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3201 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3202 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3203 enum var_init_status);
3204 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3205 enum var_init_status);
3206 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3207 enum var_init_status);
3208 static int is_based_loc (const_rtx);
3209 static bool resolve_one_addr (rtx *);
3210 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3211 enum var_init_status);
3212 static dw_loc_descr_ref loc_descriptor (rtx, machine_mode mode,
3213 enum var_init_status);
3214 struct loc_descr_context;
3215 static dw_loc_list_ref loc_list_from_tree (tree, int,
3216 const struct loc_descr_context *);
3217 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int,
3218 const struct loc_descr_context *);
3219 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3220 static tree field_type (const_tree);
3221 static unsigned int simple_type_align_in_bits (const_tree);
3222 static unsigned int simple_decl_align_in_bits (const_tree);
3223 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3224 static HOST_WIDE_INT field_byte_offset (const_tree);
3225 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3226 dw_loc_list_ref);
3227 static void add_data_member_location_attribute (dw_die_ref, tree);
3228 static bool add_const_value_attribute (dw_die_ref, rtx);
3229 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3230 static void insert_wide_int (const wide_int &, unsigned char *, int);
3231 static void insert_float (const_rtx, unsigned char *);
3232 static rtx rtl_for_decl_location (tree);
3233 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool,
3234 enum dwarf_attribute);
3235 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3236 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3237 static void add_name_attribute (dw_die_ref, const char *);
3238 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3239 static void add_comp_dir_attribute (dw_die_ref);
3240 static void add_scalar_info (dw_die_ref, enum dwarf_attribute, tree, int,
3241 const struct loc_descr_context *);
3242 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree,
3243 const struct loc_descr_context *);
3244 static void add_subscript_info (dw_die_ref, tree, bool);
3245 static void add_byte_size_attribute (dw_die_ref, tree);
3246 static void add_bit_offset_attribute (dw_die_ref, tree);
3247 static void add_bit_size_attribute (dw_die_ref, tree);
3248 static void add_prototyped_attribute (dw_die_ref, tree);
3249 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3250 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3251 static void add_src_coords_attributes (dw_die_ref, tree);
3252 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3253 static void push_decl_scope (tree);
3254 static void pop_decl_scope (void);
3255 static dw_die_ref scope_die_for (tree, dw_die_ref);
3256 static inline int local_scope_p (dw_die_ref);
3257 static inline int class_scope_p (dw_die_ref);
3258 static inline int class_or_namespace_scope_p (dw_die_ref);
3259 static void add_type_attribute (dw_die_ref, tree, int, dw_die_ref);
3260 static void add_calling_convention_attribute (dw_die_ref, tree);
3261 static const char *type_tag (const_tree);
3262 static tree member_declared_type (const_tree);
3263 #if 0
3264 static const char *decl_start_label (tree);
3265 #endif
3266 static void gen_array_type_die (tree, dw_die_ref);
3267 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3268 #if 0
3269 static void gen_entry_point_die (tree, dw_die_ref);
3270 #endif
3271 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3272 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3273 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3274 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3275 static void gen_formal_types_die (tree, dw_die_ref);
3276 static void gen_subprogram_die (tree, dw_die_ref);
3277 static void gen_variable_die (tree, tree, dw_die_ref);
3278 static void gen_const_die (tree, dw_die_ref);
3279 static void gen_label_die (tree, dw_die_ref);
3280 static void gen_lexical_block_die (tree, dw_die_ref);
3281 static void gen_inlined_subroutine_die (tree, dw_die_ref);
3282 static void gen_field_die (tree, dw_die_ref);
3283 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3284 static dw_die_ref gen_compile_unit_die (const char *);
3285 static void gen_inheritance_die (tree, tree, dw_die_ref);
3286 static void gen_member_die (tree, dw_die_ref);
3287 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3288 enum debug_info_usage);
3289 static void gen_subroutine_type_die (tree, dw_die_ref);
3290 static void gen_typedef_die (tree, dw_die_ref);
3291 static void gen_type_die (tree, dw_die_ref);
3292 static void gen_block_die (tree, dw_die_ref);
3293 static void decls_for_scope (tree, dw_die_ref);
3294 static inline int is_redundant_typedef (const_tree);
3295 static bool is_naming_typedef_decl (const_tree);
3296 static inline dw_die_ref get_context_die (tree);
3297 static void gen_namespace_die (tree, dw_die_ref);
3298 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3299 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
3300 static dw_die_ref force_decl_die (tree);
3301 static dw_die_ref force_type_die (tree);
3302 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3303 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3304 static struct dwarf_file_data * lookup_filename (const char *);
3305 static void retry_incomplete_types (void);
3306 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3307 static void gen_generic_params_dies (tree);
3308 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3309 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3310 static void splice_child_die (dw_die_ref, dw_die_ref);
3311 static int file_info_cmp (const void *, const void *);
3312 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3313 const char *, const char *);
3314 static void output_loc_list (dw_loc_list_ref);
3315 static char *gen_internal_sym (const char *);
3316 static bool want_pubnames (void);
3318 static void prune_unmark_dies (dw_die_ref);
3319 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3320 static void prune_unused_types_mark (dw_die_ref, int);
3321 static void prune_unused_types_walk (dw_die_ref);
3322 static void prune_unused_types_walk_attribs (dw_die_ref);
3323 static void prune_unused_types_prune (dw_die_ref);
3324 static void prune_unused_types (void);
3325 static int maybe_emit_file (struct dwarf_file_data *fd);
3326 static inline const char *AT_vms_delta1 (dw_attr_ref);
3327 static inline const char *AT_vms_delta2 (dw_attr_ref);
3328 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3329 const char *, const char *);
3330 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3331 static void gen_remaining_tmpl_value_param_die_attribute (void);
3332 static bool generic_type_p (tree);
3333 static void schedule_generic_params_dies_gen (tree t);
3334 static void gen_scheduled_generic_parms_dies (void);
3336 static const char *comp_dir_string (void);
3338 static void hash_loc_operands (dw_loc_descr_ref, inchash::hash &);
3340 /* enum for tracking thread-local variables whose address is really an offset
3341 relative to the TLS pointer, which will need link-time relocation, but will
3342 not need relocation by the DWARF consumer. */
3344 enum dtprel_bool
3346 dtprel_false = 0,
3347 dtprel_true = 1
3350 /* Return the operator to use for an address of a variable. For dtprel_true, we
3351 use DW_OP_const*. For regular variables, which need both link-time
3352 relocation and consumer-level relocation (e.g., to account for shared objects
3353 loaded at a random address), we use DW_OP_addr*. */
3355 static inline enum dwarf_location_atom
3356 dw_addr_op (enum dtprel_bool dtprel)
3358 if (dtprel == dtprel_true)
3359 return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3360 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3361 else
3362 return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3365 /* Return a pointer to a newly allocated address location description. If
3366 dwarf_split_debug_info is true, then record the address with the appropriate
3367 relocation. */
3368 static inline dw_loc_descr_ref
3369 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3371 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3373 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3374 ref->dw_loc_oprnd1.v.val_addr = addr;
3375 ref->dtprel = dtprel;
3376 if (dwarf_split_debug_info)
3377 ref->dw_loc_oprnd1.val_entry
3378 = add_addr_table_entry (addr,
3379 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3380 else
3381 ref->dw_loc_oprnd1.val_entry = NULL;
3383 return ref;
3386 /* Section names used to hold DWARF debugging information. */
3388 #ifndef DEBUG_INFO_SECTION
3389 #define DEBUG_INFO_SECTION ".debug_info"
3390 #endif
3391 #ifndef DEBUG_DWO_INFO_SECTION
3392 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3393 #endif
3394 #ifndef DEBUG_ABBREV_SECTION
3395 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3396 #endif
3397 #ifndef DEBUG_DWO_ABBREV_SECTION
3398 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3399 #endif
3400 #ifndef DEBUG_ARANGES_SECTION
3401 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3402 #endif
3403 #ifndef DEBUG_ADDR_SECTION
3404 #define DEBUG_ADDR_SECTION ".debug_addr"
3405 #endif
3406 #ifndef DEBUG_NORM_MACINFO_SECTION
3407 #define DEBUG_NORM_MACINFO_SECTION ".debug_macinfo"
3408 #endif
3409 #ifndef DEBUG_DWO_MACINFO_SECTION
3410 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
3411 #endif
3412 #ifndef DEBUG_MACINFO_SECTION
3413 #define DEBUG_MACINFO_SECTION \
3414 (!dwarf_split_debug_info \
3415 ? (DEBUG_NORM_MACINFO_SECTION) : (DEBUG_DWO_MACINFO_SECTION))
3416 #endif
3417 #ifndef DEBUG_NORM_MACRO_SECTION
3418 #define DEBUG_NORM_MACRO_SECTION ".debug_macro"
3419 #endif
3420 #ifndef DEBUG_DWO_MACRO_SECTION
3421 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
3422 #endif
3423 #ifndef DEBUG_MACRO_SECTION
3424 #define DEBUG_MACRO_SECTION \
3425 (!dwarf_split_debug_info \
3426 ? (DEBUG_NORM_MACRO_SECTION) : (DEBUG_DWO_MACRO_SECTION))
3427 #endif
3428 #ifndef DEBUG_LINE_SECTION
3429 #define DEBUG_LINE_SECTION ".debug_line"
3430 #endif
3431 #ifndef DEBUG_DWO_LINE_SECTION
3432 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3433 #endif
3434 #ifndef DEBUG_LOC_SECTION
3435 #define DEBUG_LOC_SECTION ".debug_loc"
3436 #endif
3437 #ifndef DEBUG_DWO_LOC_SECTION
3438 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
3439 #endif
3440 #ifndef DEBUG_PUBNAMES_SECTION
3441 #define DEBUG_PUBNAMES_SECTION \
3442 ((debug_generate_pub_sections == 2) \
3443 ? ".debug_gnu_pubnames" : ".debug_pubnames")
3444 #endif
3445 #ifndef DEBUG_PUBTYPES_SECTION
3446 #define DEBUG_PUBTYPES_SECTION \
3447 ((debug_generate_pub_sections == 2) \
3448 ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
3449 #endif
3450 #define DEBUG_NORM_STR_OFFSETS_SECTION ".debug_str_offsets"
3451 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3452 #ifndef DEBUG_STR_OFFSETS_SECTION
3453 #define DEBUG_STR_OFFSETS_SECTION \
3454 (!dwarf_split_debug_info \
3455 ? (DEBUG_NORM_STR_OFFSETS_SECTION) : (DEBUG_DWO_STR_OFFSETS_SECTION))
3456 #endif
3457 #ifndef DEBUG_STR_DWO_SECTION
3458 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
3459 #endif
3460 #ifndef DEBUG_STR_SECTION
3461 #define DEBUG_STR_SECTION ".debug_str"
3462 #endif
3463 #ifndef DEBUG_RANGES_SECTION
3464 #define DEBUG_RANGES_SECTION ".debug_ranges"
3465 #endif
3467 /* Standard ELF section names for compiled code and data. */
3468 #ifndef TEXT_SECTION_NAME
3469 #define TEXT_SECTION_NAME ".text"
3470 #endif
3472 /* Section flags for .debug_macinfo/.debug_macro section. */
3473 #define DEBUG_MACRO_SECTION_FLAGS \
3474 (dwarf_split_debug_info ? SECTION_DEBUG | SECTION_EXCLUDE : SECTION_DEBUG)
3476 /* Section flags for .debug_str section. */
3477 #define DEBUG_STR_SECTION_FLAGS \
3478 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
3479 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3480 : SECTION_DEBUG)
3482 /* Section flags for .debug_str.dwo section. */
3483 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
3485 /* Labels we insert at beginning sections we can reference instead of
3486 the section names themselves. */
3488 #ifndef TEXT_SECTION_LABEL
3489 #define TEXT_SECTION_LABEL "Ltext"
3490 #endif
3491 #ifndef COLD_TEXT_SECTION_LABEL
3492 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
3493 #endif
3494 #ifndef DEBUG_LINE_SECTION_LABEL
3495 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3496 #endif
3497 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3498 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
3499 #endif
3500 #ifndef DEBUG_INFO_SECTION_LABEL
3501 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3502 #endif
3503 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3504 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
3505 #endif
3506 #ifndef DEBUG_ABBREV_SECTION_LABEL
3507 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3508 #endif
3509 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3510 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3511 #endif
3512 #ifndef DEBUG_ADDR_SECTION_LABEL
3513 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
3514 #endif
3515 #ifndef DEBUG_LOC_SECTION_LABEL
3516 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3517 #endif
3518 #ifndef DEBUG_RANGES_SECTION_LABEL
3519 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3520 #endif
3521 #ifndef DEBUG_MACINFO_SECTION_LABEL
3522 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3523 #endif
3524 #ifndef DEBUG_MACRO_SECTION_LABEL
3525 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
3526 #endif
3527 #define SKELETON_COMP_DIE_ABBREV 1
3528 #define SKELETON_TYPE_DIE_ABBREV 2
3530 /* Definitions of defaults for formats and names of various special
3531 (artificial) labels which may be generated within this file (when the -g
3532 options is used and DWARF2_DEBUGGING_INFO is in effect.
3533 If necessary, these may be overridden from within the tm.h file, but
3534 typically, overriding these defaults is unnecessary. */
3536 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3537 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3538 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3539 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3540 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3541 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3542 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3543 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3544 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3545 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3546 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3547 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3548 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3549 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3551 #ifndef TEXT_END_LABEL
3552 #define TEXT_END_LABEL "Letext"
3553 #endif
3554 #ifndef COLD_END_LABEL
3555 #define COLD_END_LABEL "Letext_cold"
3556 #endif
3557 #ifndef BLOCK_BEGIN_LABEL
3558 #define BLOCK_BEGIN_LABEL "LBB"
3559 #endif
3560 #ifndef BLOCK_END_LABEL
3561 #define BLOCK_END_LABEL "LBE"
3562 #endif
3563 #ifndef LINE_CODE_LABEL
3564 #define LINE_CODE_LABEL "LM"
3565 #endif
3568 /* Return the root of the DIE's built for the current compilation unit. */
3569 static dw_die_ref
3570 comp_unit_die (void)
3572 if (!single_comp_unit_die)
3573 single_comp_unit_die = gen_compile_unit_die (NULL);
3574 return single_comp_unit_die;
3577 /* We allow a language front-end to designate a function that is to be
3578 called to "demangle" any name before it is put into a DIE. */
3580 static const char *(*demangle_name_func) (const char *);
3582 void
3583 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3585 demangle_name_func = func;
3588 /* Test if rtl node points to a pseudo register. */
3590 static inline int
3591 is_pseudo_reg (const_rtx rtl)
3593 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3594 || (GET_CODE (rtl) == SUBREG
3595 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3598 /* Return a reference to a type, with its const and volatile qualifiers
3599 removed. */
3601 static inline tree
3602 type_main_variant (tree type)
3604 type = TYPE_MAIN_VARIANT (type);
3606 /* ??? There really should be only one main variant among any group of
3607 variants of a given type (and all of the MAIN_VARIANT values for all
3608 members of the group should point to that one type) but sometimes the C
3609 front-end messes this up for array types, so we work around that bug
3610 here. */
3611 if (TREE_CODE (type) == ARRAY_TYPE)
3612 while (type != TYPE_MAIN_VARIANT (type))
3613 type = TYPE_MAIN_VARIANT (type);
3615 return type;
3618 /* Return nonzero if the given type node represents a tagged type. */
3620 static inline int
3621 is_tagged_type (const_tree type)
3623 enum tree_code code = TREE_CODE (type);
3625 return (code == RECORD_TYPE || code == UNION_TYPE
3626 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3629 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
3631 static void
3632 get_ref_die_offset_label (char *label, dw_die_ref ref)
3634 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3637 /* Return die_offset of a DIE reference to a base type. */
3639 static unsigned long int
3640 get_base_type_offset (dw_die_ref ref)
3642 if (ref->die_offset)
3643 return ref->die_offset;
3644 if (comp_unit_die ()->die_abbrev)
3646 calc_base_type_die_sizes ();
3647 gcc_assert (ref->die_offset);
3649 return ref->die_offset;
3652 /* Return die_offset of a DIE reference other than base type. */
3654 static unsigned long int
3655 get_ref_die_offset (dw_die_ref ref)
3657 gcc_assert (ref->die_offset);
3658 return ref->die_offset;
3661 /* Convert a DIE tag into its string name. */
3663 static const char *
3664 dwarf_tag_name (unsigned int tag)
3666 const char *name = get_DW_TAG_name (tag);
3668 if (name != NULL)
3669 return name;
3671 return "DW_TAG_<unknown>";
3674 /* Convert a DWARF attribute code into its string name. */
3676 static const char *
3677 dwarf_attr_name (unsigned int attr)
3679 const char *name;
3681 switch (attr)
3683 #if VMS_DEBUGGING_INFO
3684 case DW_AT_HP_prologue:
3685 return "DW_AT_HP_prologue";
3686 #else
3687 case DW_AT_MIPS_loop_unroll_factor:
3688 return "DW_AT_MIPS_loop_unroll_factor";
3689 #endif
3691 #if VMS_DEBUGGING_INFO
3692 case DW_AT_HP_epilogue:
3693 return "DW_AT_HP_epilogue";
3694 #else
3695 case DW_AT_MIPS_stride:
3696 return "DW_AT_MIPS_stride";
3697 #endif
3700 name = get_DW_AT_name (attr);
3702 if (name != NULL)
3703 return name;
3705 return "DW_AT_<unknown>";
3708 /* Convert a DWARF value form code into its string name. */
3710 static const char *
3711 dwarf_form_name (unsigned int form)
3713 const char *name = get_DW_FORM_name (form);
3715 if (name != NULL)
3716 return name;
3718 return "DW_FORM_<unknown>";
3721 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
3722 instance of an inlined instance of a decl which is local to an inline
3723 function, so we have to trace all of the way back through the origin chain
3724 to find out what sort of node actually served as the original seed for the
3725 given block. */
3727 static tree
3728 decl_ultimate_origin (const_tree decl)
3730 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
3731 return NULL_TREE;
3733 /* DECL_ABSTRACT_ORIGIN can point to itself; ignore that if
3734 we're trying to output the abstract instance of this function. */
3735 if (DECL_ABSTRACT_P (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
3736 return NULL_TREE;
3738 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
3739 most distant ancestor, this should never happen. */
3740 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
3742 return DECL_ABSTRACT_ORIGIN (decl);
3745 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
3746 of a virtual function may refer to a base class, so we check the 'this'
3747 parameter. */
3749 static tree
3750 decl_class_context (tree decl)
3752 tree context = NULL_TREE;
3754 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
3755 context = DECL_CONTEXT (decl);
3756 else
3757 context = TYPE_MAIN_VARIANT
3758 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
3760 if (context && !TYPE_P (context))
3761 context = NULL_TREE;
3763 return context;
3766 /* Add an attribute/value pair to a DIE. */
3768 static inline void
3769 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
3771 /* Maybe this should be an assert? */
3772 if (die == NULL)
3773 return;
3775 vec_safe_reserve (die->die_attr, 1);
3776 vec_safe_push (die->die_attr, *attr);
3779 static inline enum dw_val_class
3780 AT_class (dw_attr_ref a)
3782 return a->dw_attr_val.val_class;
3785 /* Return the index for any attribute that will be referenced with a
3786 DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index. String indices
3787 are stored in dw_attr_val.v.val_str for reference counting
3788 pruning. */
3790 static inline unsigned int
3791 AT_index (dw_attr_ref a)
3793 if (AT_class (a) == dw_val_class_str)
3794 return a->dw_attr_val.v.val_str->index;
3795 else if (a->dw_attr_val.val_entry != NULL)
3796 return a->dw_attr_val.val_entry->index;
3797 return NOT_INDEXED;
3800 /* Add a flag value attribute to a DIE. */
3802 static inline void
3803 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
3805 dw_attr_node attr;
3807 attr.dw_attr = attr_kind;
3808 attr.dw_attr_val.val_class = dw_val_class_flag;
3809 attr.dw_attr_val.val_entry = NULL;
3810 attr.dw_attr_val.v.val_flag = flag;
3811 add_dwarf_attr (die, &attr);
3814 static inline unsigned
3815 AT_flag (dw_attr_ref a)
3817 gcc_assert (a && AT_class (a) == dw_val_class_flag);
3818 return a->dw_attr_val.v.val_flag;
3821 /* Add a signed integer attribute value to a DIE. */
3823 static inline void
3824 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
3826 dw_attr_node attr;
3828 attr.dw_attr = attr_kind;
3829 attr.dw_attr_val.val_class = dw_val_class_const;
3830 attr.dw_attr_val.val_entry = NULL;
3831 attr.dw_attr_val.v.val_int = int_val;
3832 add_dwarf_attr (die, &attr);
3835 static inline HOST_WIDE_INT
3836 AT_int (dw_attr_ref a)
3838 gcc_assert (a && AT_class (a) == dw_val_class_const);
3839 return a->dw_attr_val.v.val_int;
3842 /* Add an unsigned integer attribute value to a DIE. */
3844 static inline void
3845 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
3846 unsigned HOST_WIDE_INT unsigned_val)
3848 dw_attr_node attr;
3850 attr.dw_attr = attr_kind;
3851 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
3852 attr.dw_attr_val.val_entry = NULL;
3853 attr.dw_attr_val.v.val_unsigned = unsigned_val;
3854 add_dwarf_attr (die, &attr);
3857 static inline unsigned HOST_WIDE_INT
3858 AT_unsigned (dw_attr_ref a)
3860 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
3861 return a->dw_attr_val.v.val_unsigned;
3864 /* Add an unsigned wide integer attribute value to a DIE. */
3866 static inline void
3867 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
3868 const wide_int& w)
3870 dw_attr_node attr;
3872 attr.dw_attr = attr_kind;
3873 attr.dw_attr_val.val_class = dw_val_class_wide_int;
3874 attr.dw_attr_val.val_entry = NULL;
3875 attr.dw_attr_val.v.val_wide = ggc_alloc<wide_int> ();
3876 *attr.dw_attr_val.v.val_wide = w;
3877 add_dwarf_attr (die, &attr);
3880 /* Add an unsigned double integer attribute value to a DIE. */
3882 static inline void
3883 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
3884 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
3886 dw_attr_node attr;
3888 attr.dw_attr = attr_kind;
3889 attr.dw_attr_val.val_class = dw_val_class_const_double;
3890 attr.dw_attr_val.val_entry = NULL;
3891 attr.dw_attr_val.v.val_double.high = high;
3892 attr.dw_attr_val.v.val_double.low = low;
3893 add_dwarf_attr (die, &attr);
3896 /* Add a floating point attribute value to a DIE and return it. */
3898 static inline void
3899 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
3900 unsigned int length, unsigned int elt_size, unsigned char *array)
3902 dw_attr_node attr;
3904 attr.dw_attr = attr_kind;
3905 attr.dw_attr_val.val_class = dw_val_class_vec;
3906 attr.dw_attr_val.val_entry = NULL;
3907 attr.dw_attr_val.v.val_vec.length = length;
3908 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
3909 attr.dw_attr_val.v.val_vec.array = array;
3910 add_dwarf_attr (die, &attr);
3913 /* Add an 8-byte data attribute value to a DIE. */
3915 static inline void
3916 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
3917 unsigned char data8[8])
3919 dw_attr_node attr;
3921 attr.dw_attr = attr_kind;
3922 attr.dw_attr_val.val_class = dw_val_class_data8;
3923 attr.dw_attr_val.val_entry = NULL;
3924 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
3925 add_dwarf_attr (die, &attr);
3928 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
3929 dwarf_split_debug_info, address attributes in dies destined for the
3930 final executable have force_direct set to avoid using indexed
3931 references. */
3933 static inline void
3934 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
3935 bool force_direct)
3937 dw_attr_node attr;
3938 char * lbl_id;
3940 lbl_id = xstrdup (lbl_low);
3941 attr.dw_attr = DW_AT_low_pc;
3942 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3943 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3944 if (dwarf_split_debug_info && !force_direct)
3945 attr.dw_attr_val.val_entry
3946 = add_addr_table_entry (lbl_id, ate_kind_label);
3947 else
3948 attr.dw_attr_val.val_entry = NULL;
3949 add_dwarf_attr (die, &attr);
3951 attr.dw_attr = DW_AT_high_pc;
3952 if (dwarf_version < 4)
3953 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3954 else
3955 attr.dw_attr_val.val_class = dw_val_class_high_pc;
3956 lbl_id = xstrdup (lbl_high);
3957 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3958 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
3959 && dwarf_split_debug_info && !force_direct)
3960 attr.dw_attr_val.val_entry
3961 = add_addr_table_entry (lbl_id, ate_kind_label);
3962 else
3963 attr.dw_attr_val.val_entry = NULL;
3964 add_dwarf_attr (die, &attr);
3967 /* Hash and equality functions for debug_str_hash. */
3969 hashval_t
3970 indirect_string_hasher::hash (indirect_string_node *x)
3972 return htab_hash_string (x->str);
3975 bool
3976 indirect_string_hasher::equal (indirect_string_node *x1, const char *x2)
3978 return strcmp (x1->str, x2) == 0;
3981 /* Add STR to the given string hash table. */
3983 static struct indirect_string_node *
3984 find_AT_string_in_table (const char *str,
3985 hash_table<indirect_string_hasher> *table)
3987 struct indirect_string_node *node;
3989 indirect_string_node **slot
3990 = table->find_slot_with_hash (str, htab_hash_string (str), INSERT);
3991 if (*slot == NULL)
3993 node = ggc_cleared_alloc<indirect_string_node> ();
3994 node->str = ggc_strdup (str);
3995 *slot = node;
3997 else
3998 node = *slot;
4000 node->refcount++;
4001 return node;
4004 /* Add STR to the indirect string hash table. */
4006 static struct indirect_string_node *
4007 find_AT_string (const char *str)
4009 if (! debug_str_hash)
4010 debug_str_hash = hash_table<indirect_string_hasher>::create_ggc (10);
4012 return find_AT_string_in_table (str, debug_str_hash);
4015 /* Add a string attribute value to a DIE. */
4017 static inline void
4018 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4020 dw_attr_node attr;
4021 struct indirect_string_node *node;
4023 node = find_AT_string (str);
4025 attr.dw_attr = attr_kind;
4026 attr.dw_attr_val.val_class = dw_val_class_str;
4027 attr.dw_attr_val.val_entry = NULL;
4028 attr.dw_attr_val.v.val_str = node;
4029 add_dwarf_attr (die, &attr);
4032 static inline const char *
4033 AT_string (dw_attr_ref a)
4035 gcc_assert (a && AT_class (a) == dw_val_class_str);
4036 return a->dw_attr_val.v.val_str->str;
4039 /* Call this function directly to bypass AT_string_form's logic to put
4040 the string inline in the die. */
4042 static void
4043 set_indirect_string (struct indirect_string_node *node)
4045 char label[32];
4046 /* Already indirect is a no op. */
4047 if (node->form == DW_FORM_strp || node->form == DW_FORM_GNU_str_index)
4049 gcc_assert (node->label);
4050 return;
4052 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4053 ++dw2_string_counter;
4054 node->label = xstrdup (label);
4056 if (!dwarf_split_debug_info)
4058 node->form = DW_FORM_strp;
4059 node->index = NOT_INDEXED;
4061 else
4063 node->form = DW_FORM_GNU_str_index;
4064 node->index = NO_INDEX_ASSIGNED;
4068 /* Find out whether a string should be output inline in DIE
4069 or out-of-line in .debug_str section. */
4071 static enum dwarf_form
4072 find_string_form (struct indirect_string_node *node)
4074 unsigned int len;
4076 if (node->form)
4077 return node->form;
4079 len = strlen (node->str) + 1;
4081 /* If the string is shorter or equal to the size of the reference, it is
4082 always better to put it inline. */
4083 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4084 return node->form = DW_FORM_string;
4086 /* If we cannot expect the linker to merge strings in .debug_str
4087 section, only put it into .debug_str if it is worth even in this
4088 single module. */
4089 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4090 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4091 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
4092 return node->form = DW_FORM_string;
4094 set_indirect_string (node);
4096 return node->form;
4099 /* Find out whether the string referenced from the attribute should be
4100 output inline in DIE or out-of-line in .debug_str section. */
4102 static enum dwarf_form
4103 AT_string_form (dw_attr_ref a)
4105 gcc_assert (a && AT_class (a) == dw_val_class_str);
4106 return find_string_form (a->dw_attr_val.v.val_str);
4109 /* Add a DIE reference attribute value to a DIE. */
4111 static inline void
4112 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4114 dw_attr_node attr;
4116 #ifdef ENABLE_CHECKING
4117 gcc_assert (targ_die != NULL);
4118 #else
4119 /* With LTO we can end up trying to reference something we didn't create
4120 a DIE for. Avoid crashing later on a NULL referenced DIE. */
4121 if (targ_die == NULL)
4122 return;
4123 #endif
4125 attr.dw_attr = attr_kind;
4126 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4127 attr.dw_attr_val.val_entry = NULL;
4128 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4129 attr.dw_attr_val.v.val_die_ref.external = 0;
4130 add_dwarf_attr (die, &attr);
4133 /* Change DIE reference REF to point to NEW_DIE instead. */
4135 static inline void
4136 change_AT_die_ref (dw_attr_ref ref, dw_die_ref new_die)
4138 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4139 ref->dw_attr_val.v.val_die_ref.die = new_die;
4140 ref->dw_attr_val.v.val_die_ref.external = 0;
4143 /* Add an AT_specification attribute to a DIE, and also make the back
4144 pointer from the specification to the definition. */
4146 static inline void
4147 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4149 add_AT_die_ref (die, DW_AT_specification, targ_die);
4150 gcc_assert (!targ_die->die_definition);
4151 targ_die->die_definition = die;
4154 static inline dw_die_ref
4155 AT_ref (dw_attr_ref a)
4157 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4158 return a->dw_attr_val.v.val_die_ref.die;
4161 static inline int
4162 AT_ref_external (dw_attr_ref a)
4164 if (a && AT_class (a) == dw_val_class_die_ref)
4165 return a->dw_attr_val.v.val_die_ref.external;
4167 return 0;
4170 static inline void
4171 set_AT_ref_external (dw_attr_ref a, int i)
4173 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4174 a->dw_attr_val.v.val_die_ref.external = i;
4177 /* Add an FDE reference attribute value to a DIE. */
4179 static inline void
4180 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4182 dw_attr_node attr;
4184 attr.dw_attr = attr_kind;
4185 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4186 attr.dw_attr_val.val_entry = NULL;
4187 attr.dw_attr_val.v.val_fde_index = targ_fde;
4188 add_dwarf_attr (die, &attr);
4191 /* Add a location description attribute value to a DIE. */
4193 static inline void
4194 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4196 dw_attr_node attr;
4198 attr.dw_attr = attr_kind;
4199 attr.dw_attr_val.val_class = dw_val_class_loc;
4200 attr.dw_attr_val.val_entry = NULL;
4201 attr.dw_attr_val.v.val_loc = loc;
4202 add_dwarf_attr (die, &attr);
4205 static inline dw_loc_descr_ref
4206 AT_loc (dw_attr_ref a)
4208 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4209 return a->dw_attr_val.v.val_loc;
4212 static inline void
4213 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4215 dw_attr_node attr;
4217 attr.dw_attr = attr_kind;
4218 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4219 attr.dw_attr_val.val_entry = NULL;
4220 attr.dw_attr_val.v.val_loc_list = loc_list;
4221 add_dwarf_attr (die, &attr);
4222 have_location_lists = true;
4225 static inline dw_loc_list_ref
4226 AT_loc_list (dw_attr_ref a)
4228 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4229 return a->dw_attr_val.v.val_loc_list;
4232 static inline dw_loc_list_ref *
4233 AT_loc_list_ptr (dw_attr_ref a)
4235 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4236 return &a->dw_attr_val.v.val_loc_list;
4239 struct addr_hasher : ggc_ptr_hash<addr_table_entry>
4241 static hashval_t hash (addr_table_entry *);
4242 static bool equal (addr_table_entry *, addr_table_entry *);
4245 /* Table of entries into the .debug_addr section. */
4247 static GTY (()) hash_table<addr_hasher> *addr_index_table;
4249 /* Hash an address_table_entry. */
4251 hashval_t
4252 addr_hasher::hash (addr_table_entry *a)
4254 inchash::hash hstate;
4255 switch (a->kind)
4257 case ate_kind_rtx:
4258 hstate.add_int (0);
4259 break;
4260 case ate_kind_rtx_dtprel:
4261 hstate.add_int (1);
4262 break;
4263 case ate_kind_label:
4264 return htab_hash_string (a->addr.label);
4265 default:
4266 gcc_unreachable ();
4268 inchash::add_rtx (a->addr.rtl, hstate);
4269 return hstate.end ();
4272 /* Determine equality for two address_table_entries. */
4274 bool
4275 addr_hasher::equal (addr_table_entry *a1, addr_table_entry *a2)
4277 if (a1->kind != a2->kind)
4278 return 0;
4279 switch (a1->kind)
4281 case ate_kind_rtx:
4282 case ate_kind_rtx_dtprel:
4283 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4284 case ate_kind_label:
4285 return strcmp (a1->addr.label, a2->addr.label) == 0;
4286 default:
4287 gcc_unreachable ();
4291 /* Initialize an addr_table_entry. */
4293 void
4294 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4296 e->kind = kind;
4297 switch (kind)
4299 case ate_kind_rtx:
4300 case ate_kind_rtx_dtprel:
4301 e->addr.rtl = (rtx) addr;
4302 break;
4303 case ate_kind_label:
4304 e->addr.label = (char *) addr;
4305 break;
4307 e->refcount = 0;
4308 e->index = NO_INDEX_ASSIGNED;
4311 /* Add attr to the address table entry to the table. Defer setting an
4312 index until output time. */
4314 static addr_table_entry *
4315 add_addr_table_entry (void *addr, enum ate_kind kind)
4317 addr_table_entry *node;
4318 addr_table_entry finder;
4320 gcc_assert (dwarf_split_debug_info);
4321 if (! addr_index_table)
4322 addr_index_table = hash_table<addr_hasher>::create_ggc (10);
4323 init_addr_table_entry (&finder, kind, addr);
4324 addr_table_entry **slot = addr_index_table->find_slot (&finder, INSERT);
4326 if (*slot == HTAB_EMPTY_ENTRY)
4328 node = ggc_cleared_alloc<addr_table_entry> ();
4329 init_addr_table_entry (node, kind, addr);
4330 *slot = node;
4332 else
4333 node = *slot;
4335 node->refcount++;
4336 return node;
4339 /* Remove an entry from the addr table by decrementing its refcount.
4340 Strictly, decrementing the refcount would be enough, but the
4341 assertion that the entry is actually in the table has found
4342 bugs. */
4344 static void
4345 remove_addr_table_entry (addr_table_entry *entry)
4347 gcc_assert (dwarf_split_debug_info && addr_index_table);
4348 /* After an index is assigned, the table is frozen. */
4349 gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
4350 entry->refcount--;
4353 /* Given a location list, remove all addresses it refers to from the
4354 address_table. */
4356 static void
4357 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4359 for (; descr; descr = descr->dw_loc_next)
4360 if (descr->dw_loc_oprnd1.val_entry != NULL)
4362 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4363 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4367 /* A helper function for dwarf2out_finish called through
4368 htab_traverse. Assign an addr_table_entry its index. All entries
4369 must be collected into the table when this function is called,
4370 because the indexing code relies on htab_traverse to traverse nodes
4371 in the same order for each run. */
4374 index_addr_table_entry (addr_table_entry **h, unsigned int *index)
4376 addr_table_entry *node = *h;
4378 /* Don't index unreferenced nodes. */
4379 if (node->refcount == 0)
4380 return 1;
4382 gcc_assert (node->index == NO_INDEX_ASSIGNED);
4383 node->index = *index;
4384 *index += 1;
4386 return 1;
4389 /* Add an address constant attribute value to a DIE. When using
4390 dwarf_split_debug_info, address attributes in dies destined for the
4391 final executable should be direct references--setting the parameter
4392 force_direct ensures this behavior. */
4394 static inline void
4395 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4396 bool force_direct)
4398 dw_attr_node attr;
4400 attr.dw_attr = attr_kind;
4401 attr.dw_attr_val.val_class = dw_val_class_addr;
4402 attr.dw_attr_val.v.val_addr = addr;
4403 if (dwarf_split_debug_info && !force_direct)
4404 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4405 else
4406 attr.dw_attr_val.val_entry = NULL;
4407 add_dwarf_attr (die, &attr);
4410 /* Get the RTX from to an address DIE attribute. */
4412 static inline rtx
4413 AT_addr (dw_attr_ref a)
4415 gcc_assert (a && AT_class (a) == dw_val_class_addr);
4416 return a->dw_attr_val.v.val_addr;
4419 /* Add a file attribute value to a DIE. */
4421 static inline void
4422 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4423 struct dwarf_file_data *fd)
4425 dw_attr_node attr;
4427 attr.dw_attr = attr_kind;
4428 attr.dw_attr_val.val_class = dw_val_class_file;
4429 attr.dw_attr_val.val_entry = NULL;
4430 attr.dw_attr_val.v.val_file = fd;
4431 add_dwarf_attr (die, &attr);
4434 /* Get the dwarf_file_data from a file DIE attribute. */
4436 static inline struct dwarf_file_data *
4437 AT_file (dw_attr_ref a)
4439 gcc_assert (a && AT_class (a) == dw_val_class_file);
4440 return a->dw_attr_val.v.val_file;
4443 /* Add a vms delta attribute value to a DIE. */
4445 static inline void
4446 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4447 const char *lbl1, const char *lbl2)
4449 dw_attr_node attr;
4451 attr.dw_attr = attr_kind;
4452 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4453 attr.dw_attr_val.val_entry = NULL;
4454 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4455 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4456 add_dwarf_attr (die, &attr);
4459 /* Add a label identifier attribute value to a DIE. */
4461 static inline void
4462 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4463 const char *lbl_id)
4465 dw_attr_node attr;
4467 attr.dw_attr = attr_kind;
4468 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4469 attr.dw_attr_val.val_entry = NULL;
4470 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4471 if (dwarf_split_debug_info)
4472 attr.dw_attr_val.val_entry
4473 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4474 ate_kind_label);
4475 add_dwarf_attr (die, &attr);
4478 /* Add a section offset attribute value to a DIE, an offset into the
4479 debug_line section. */
4481 static inline void
4482 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4483 const char *label)
4485 dw_attr_node attr;
4487 attr.dw_attr = attr_kind;
4488 attr.dw_attr_val.val_class = dw_val_class_lineptr;
4489 attr.dw_attr_val.val_entry = NULL;
4490 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4491 add_dwarf_attr (die, &attr);
4494 /* Add a section offset attribute value to a DIE, an offset into the
4495 debug_macinfo section. */
4497 static inline void
4498 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4499 const char *label)
4501 dw_attr_node attr;
4503 attr.dw_attr = attr_kind;
4504 attr.dw_attr_val.val_class = dw_val_class_macptr;
4505 attr.dw_attr_val.val_entry = NULL;
4506 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4507 add_dwarf_attr (die, &attr);
4510 /* Add an offset attribute value to a DIE. */
4512 static inline void
4513 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4514 unsigned HOST_WIDE_INT offset)
4516 dw_attr_node attr;
4518 attr.dw_attr = attr_kind;
4519 attr.dw_attr_val.val_class = dw_val_class_offset;
4520 attr.dw_attr_val.val_entry = NULL;
4521 attr.dw_attr_val.v.val_offset = offset;
4522 add_dwarf_attr (die, &attr);
4525 /* Add a range_list attribute value to a DIE. When using
4526 dwarf_split_debug_info, address attributes in dies destined for the
4527 final executable should be direct references--setting the parameter
4528 force_direct ensures this behavior. */
4530 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4531 #define RELOCATED_OFFSET (NULL)
4533 static void
4534 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4535 long unsigned int offset, bool force_direct)
4537 dw_attr_node attr;
4539 attr.dw_attr = attr_kind;
4540 attr.dw_attr_val.val_class = dw_val_class_range_list;
4541 /* For the range_list attribute, use val_entry to store whether the
4542 offset should follow split-debug-info or normal semantics. This
4543 value is read in output_range_list_offset. */
4544 if (dwarf_split_debug_info && !force_direct)
4545 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4546 else
4547 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4548 attr.dw_attr_val.v.val_offset = offset;
4549 add_dwarf_attr (die, &attr);
4552 /* Return the start label of a delta attribute. */
4554 static inline const char *
4555 AT_vms_delta1 (dw_attr_ref a)
4557 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4558 return a->dw_attr_val.v.val_vms_delta.lbl1;
4561 /* Return the end label of a delta attribute. */
4563 static inline const char *
4564 AT_vms_delta2 (dw_attr_ref a)
4566 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4567 return a->dw_attr_val.v.val_vms_delta.lbl2;
4570 static inline const char *
4571 AT_lbl (dw_attr_ref a)
4573 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4574 || AT_class (a) == dw_val_class_lineptr
4575 || AT_class (a) == dw_val_class_macptr
4576 || AT_class (a) == dw_val_class_high_pc));
4577 return a->dw_attr_val.v.val_lbl_id;
4580 /* Get the attribute of type attr_kind. */
4582 static dw_attr_ref
4583 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4585 dw_attr_ref a;
4586 unsigned ix;
4587 dw_die_ref spec = NULL;
4589 if (! die)
4590 return NULL;
4592 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4593 if (a->dw_attr == attr_kind)
4594 return a;
4595 else if (a->dw_attr == DW_AT_specification
4596 || a->dw_attr == DW_AT_abstract_origin)
4597 spec = AT_ref (a);
4599 if (spec)
4600 return get_AT (spec, attr_kind);
4602 return NULL;
4605 /* Returns the parent of the declaration of DIE. */
4607 static dw_die_ref
4608 get_die_parent (dw_die_ref die)
4610 dw_die_ref t;
4612 if (!die)
4613 return NULL;
4615 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4616 || (t = get_AT_ref (die, DW_AT_specification)))
4617 die = t;
4619 return die->die_parent;
4622 /* Return the "low pc" attribute value, typically associated with a subprogram
4623 DIE. Return null if the "low pc" attribute is either not present, or if it
4624 cannot be represented as an assembler label identifier. */
4626 static inline const char *
4627 get_AT_low_pc (dw_die_ref die)
4629 dw_attr_ref a = get_AT (die, DW_AT_low_pc);
4631 return a ? AT_lbl (a) : NULL;
4634 /* Return the "high pc" attribute value, typically associated with a subprogram
4635 DIE. Return null if the "high pc" attribute is either not present, or if it
4636 cannot be represented as an assembler label identifier. */
4638 static inline const char *
4639 get_AT_hi_pc (dw_die_ref die)
4641 dw_attr_ref a = get_AT (die, DW_AT_high_pc);
4643 return a ? AT_lbl (a) : NULL;
4646 /* Return the value of the string attribute designated by ATTR_KIND, or
4647 NULL if it is not present. */
4649 static inline const char *
4650 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4652 dw_attr_ref a = get_AT (die, attr_kind);
4654 return a ? AT_string (a) : NULL;
4657 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4658 if it is not present. */
4660 static inline int
4661 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4663 dw_attr_ref a = get_AT (die, attr_kind);
4665 return a ? AT_flag (a) : 0;
4668 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4669 if it is not present. */
4671 static inline unsigned
4672 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4674 dw_attr_ref a = get_AT (die, attr_kind);
4676 return a ? AT_unsigned (a) : 0;
4679 static inline dw_die_ref
4680 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4682 dw_attr_ref a = get_AT (die, attr_kind);
4684 return a ? AT_ref (a) : NULL;
4687 static inline struct dwarf_file_data *
4688 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4690 dw_attr_ref a = get_AT (die, attr_kind);
4692 return a ? AT_file (a) : NULL;
4695 /* Return TRUE if the language is C++. */
4697 static inline bool
4698 is_cxx (void)
4700 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4702 return (lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus
4703 || lang == DW_LANG_C_plus_plus_11 || lang == DW_LANG_C_plus_plus_14);
4706 /* Return TRUE if the language is Java. */
4708 static inline bool
4709 is_java (void)
4711 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4713 return lang == DW_LANG_Java;
4716 /* Return TRUE if the language is Fortran. */
4718 static inline bool
4719 is_fortran (void)
4721 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4723 return (lang == DW_LANG_Fortran77
4724 || lang == DW_LANG_Fortran90
4725 || lang == DW_LANG_Fortran95
4726 || lang == DW_LANG_Fortran03
4727 || lang == DW_LANG_Fortran08);
4730 /* Return TRUE if the language is Ada. */
4732 static inline bool
4733 is_ada (void)
4735 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4737 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4740 /* Remove the specified attribute if present. Return TRUE if removal
4741 was successful. */
4743 static bool
4744 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4746 dw_attr_ref a;
4747 unsigned ix;
4749 if (! die)
4750 return false;
4752 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4753 if (a->dw_attr == attr_kind)
4755 if (AT_class (a) == dw_val_class_str)
4756 if (a->dw_attr_val.v.val_str->refcount)
4757 a->dw_attr_val.v.val_str->refcount--;
4759 /* vec::ordered_remove should help reduce the number of abbrevs
4760 that are needed. */
4761 die->die_attr->ordered_remove (ix);
4762 return true;
4764 return false;
4767 /* Remove CHILD from its parent. PREV must have the property that
4768 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
4770 static void
4771 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4773 gcc_assert (child->die_parent == prev->die_parent);
4774 gcc_assert (prev->die_sib == child);
4775 if (prev == child)
4777 gcc_assert (child->die_parent->die_child == child);
4778 prev = NULL;
4780 else
4781 prev->die_sib = child->die_sib;
4782 if (child->die_parent->die_child == child)
4783 child->die_parent->die_child = prev;
4786 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
4787 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
4789 static void
4790 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4792 dw_die_ref parent = old_child->die_parent;
4794 gcc_assert (parent == prev->die_parent);
4795 gcc_assert (prev->die_sib == old_child);
4797 new_child->die_parent = parent;
4798 if (prev == old_child)
4800 gcc_assert (parent->die_child == old_child);
4801 new_child->die_sib = new_child;
4803 else
4805 prev->die_sib = new_child;
4806 new_child->die_sib = old_child->die_sib;
4808 if (old_child->die_parent->die_child == old_child)
4809 old_child->die_parent->die_child = new_child;
4812 /* Move all children from OLD_PARENT to NEW_PARENT. */
4814 static void
4815 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4817 dw_die_ref c;
4818 new_parent->die_child = old_parent->die_child;
4819 old_parent->die_child = NULL;
4820 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4823 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
4824 matches TAG. */
4826 static void
4827 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4829 dw_die_ref c;
4831 c = die->die_child;
4832 if (c) do {
4833 dw_die_ref prev = c;
4834 c = c->die_sib;
4835 while (c->die_tag == tag)
4837 remove_child_with_prev (c, prev);
4838 c->die_parent = NULL;
4839 /* Might have removed every child. */
4840 if (c == c->die_sib)
4841 return;
4842 c = c->die_sib;
4844 } while (c != die->die_child);
4847 /* Add a CHILD_DIE as the last child of DIE. */
4849 static void
4850 add_child_die (dw_die_ref die, dw_die_ref child_die)
4852 /* FIXME this should probably be an assert. */
4853 if (! die || ! child_die)
4854 return;
4855 gcc_assert (die != child_die);
4857 child_die->die_parent = die;
4858 if (die->die_child)
4860 child_die->die_sib = die->die_child->die_sib;
4861 die->die_child->die_sib = child_die;
4863 else
4864 child_die->die_sib = child_die;
4865 die->die_child = child_die;
4868 /* Unassociate CHILD from its parent, and make its parent be
4869 NEW_PARENT. */
4871 static void
4872 reparent_child (dw_die_ref child, dw_die_ref new_parent)
4874 for (dw_die_ref p = child->die_parent->die_child; ; p = p->die_sib)
4875 if (p->die_sib == child)
4877 remove_child_with_prev (child, p);
4878 break;
4880 add_child_die (new_parent, child);
4883 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
4884 is the specification, to the end of PARENT's list of children.
4885 This is done by removing and re-adding it. */
4887 static void
4888 splice_child_die (dw_die_ref parent, dw_die_ref child)
4890 /* We want the declaration DIE from inside the class, not the
4891 specification DIE at toplevel. */
4892 if (child->die_parent != parent)
4894 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
4896 if (tmp)
4897 child = tmp;
4900 gcc_assert (child->die_parent == parent
4901 || (child->die_parent
4902 == get_AT_ref (parent, DW_AT_specification)));
4904 reparent_child (child, parent);
4907 /* Create and return a new die with a parent of PARENT_DIE. If
4908 PARENT_DIE is NULL, the new DIE is placed in limbo and an
4909 associated tree T must be supplied to determine parenthood
4910 later. */
4912 static inline dw_die_ref
4913 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
4915 dw_die_ref die = ggc_cleared_alloc<die_node> ();
4917 die->die_tag = tag_value;
4919 if (parent_die != NULL)
4920 add_child_die (parent_die, die);
4921 else
4923 limbo_die_node *limbo_node;
4925 /* No DIEs created after early dwarf should end up in limbo,
4926 because the limbo list should not persist past LTO
4927 streaming. */
4928 if (tag_value != DW_TAG_compile_unit
4929 /* These are allowed because they're generated while
4930 breaking out COMDAT units late. */
4931 && tag_value != DW_TAG_type_unit
4932 && !early_dwarf
4933 /* Allow nested functions to live in limbo because they will
4934 only temporarily live there, as decls_for_scope will fix
4935 them up. */
4936 && (TREE_CODE (t) != FUNCTION_DECL
4937 || !decl_function_context (t))
4938 /* Same as nested functions above but for types. Types that
4939 are local to a function will be fixed in
4940 decls_for_scope. */
4941 && (!RECORD_OR_UNION_TYPE_P (t)
4942 || !TYPE_CONTEXT (t)
4943 || TREE_CODE (TYPE_CONTEXT (t)) != FUNCTION_DECL)
4944 /* FIXME debug-early: Allow late limbo DIE creation for LTO,
4945 especially in the ltrans stage, but once we implement LTO
4946 dwarf streaming, we should remove this exception. */
4947 && !in_lto_p)
4949 fprintf (stderr, "symbol ended up in limbo too late:");
4950 debug_generic_stmt (t);
4951 gcc_unreachable ();
4954 limbo_node = ggc_cleared_alloc<limbo_die_node> ();
4955 limbo_node->die = die;
4956 limbo_node->created_for = t;
4957 limbo_node->next = limbo_die_list;
4958 limbo_die_list = limbo_node;
4961 return die;
4964 /* Return the DIE associated with the given type specifier. */
4966 static inline dw_die_ref
4967 lookup_type_die (tree type)
4969 return TYPE_SYMTAB_DIE (type);
4972 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
4973 anonymous type named by the typedef TYPE_DIE, return the DIE of the
4974 anonymous type instead the one of the naming typedef. */
4976 static inline dw_die_ref
4977 strip_naming_typedef (tree type, dw_die_ref type_die)
4979 if (type
4980 && TREE_CODE (type) == RECORD_TYPE
4981 && type_die
4982 && type_die->die_tag == DW_TAG_typedef
4983 && is_naming_typedef_decl (TYPE_NAME (type)))
4984 type_die = get_AT_ref (type_die, DW_AT_type);
4985 return type_die;
4988 /* Like lookup_type_die, but if type is an anonymous type named by a
4989 typedef[1], return the DIE of the anonymous type instead the one of
4990 the naming typedef. This is because in gen_typedef_die, we did
4991 equate the anonymous struct named by the typedef with the DIE of
4992 the naming typedef. So by default, lookup_type_die on an anonymous
4993 struct yields the DIE of the naming typedef.
4995 [1]: Read the comment of is_naming_typedef_decl to learn about what
4996 a naming typedef is. */
4998 static inline dw_die_ref
4999 lookup_type_die_strip_naming_typedef (tree type)
5001 dw_die_ref die = lookup_type_die (type);
5002 return strip_naming_typedef (type, die);
5005 /* Equate a DIE to a given type specifier. */
5007 static inline void
5008 equate_type_number_to_die (tree type, dw_die_ref type_die)
5010 TYPE_SYMTAB_DIE (type) = type_die;
5013 /* Returns a hash value for X (which really is a die_struct). */
5015 inline hashval_t
5016 decl_die_hasher::hash (die_node *x)
5018 return (hashval_t) x->decl_id;
5021 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
5023 inline bool
5024 decl_die_hasher::equal (die_node *x, tree y)
5026 return (x->decl_id == DECL_UID (y));
5029 /* Return the DIE associated with a given declaration. */
5031 static inline dw_die_ref
5032 lookup_decl_die (tree decl)
5034 return decl_die_table->find_with_hash (decl, DECL_UID (decl));
5037 /* Returns a hash value for X (which really is a var_loc_list). */
5039 inline hashval_t
5040 decl_loc_hasher::hash (var_loc_list *x)
5042 return (hashval_t) x->decl_id;
5045 /* Return nonzero if decl_id of var_loc_list X is the same as
5046 UID of decl *Y. */
5048 inline bool
5049 decl_loc_hasher::equal (var_loc_list *x, const_tree y)
5051 return (x->decl_id == DECL_UID (y));
5054 /* Return the var_loc list associated with a given declaration. */
5056 static inline var_loc_list *
5057 lookup_decl_loc (const_tree decl)
5059 if (!decl_loc_table)
5060 return NULL;
5061 return decl_loc_table->find_with_hash (decl, DECL_UID (decl));
5064 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
5066 inline hashval_t
5067 dw_loc_list_hasher::hash (cached_dw_loc_list *x)
5069 return (hashval_t) x->decl_id;
5072 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
5073 UID of decl *Y. */
5075 inline bool
5076 dw_loc_list_hasher::equal (cached_dw_loc_list *x, const_tree y)
5078 return (x->decl_id == DECL_UID (y));
5081 /* Equate a DIE to a particular declaration. */
5083 static void
5084 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5086 unsigned int decl_id = DECL_UID (decl);
5088 *decl_die_table->find_slot_with_hash (decl, decl_id, INSERT) = decl_die;
5089 decl_die->decl_id = decl_id;
5092 /* Return how many bits covers PIECE EXPR_LIST. */
5094 static HOST_WIDE_INT
5095 decl_piece_bitsize (rtx piece)
5097 int ret = (int) GET_MODE (piece);
5098 if (ret)
5099 return ret;
5100 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
5101 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
5102 return INTVAL (XEXP (XEXP (piece, 0), 0));
5105 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
5107 static rtx *
5108 decl_piece_varloc_ptr (rtx piece)
5110 if ((int) GET_MODE (piece))
5111 return &XEXP (piece, 0);
5112 else
5113 return &XEXP (XEXP (piece, 0), 1);
5116 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
5117 Next is the chain of following piece nodes. */
5119 static rtx_expr_list *
5120 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
5122 if (bitsize > 0 && bitsize <= (int) MAX_MACHINE_MODE)
5123 return alloc_EXPR_LIST (bitsize, loc_note, next);
5124 else
5125 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
5126 GEN_INT (bitsize),
5127 loc_note), next);
5130 /* Return rtx that should be stored into loc field for
5131 LOC_NOTE and BITPOS/BITSIZE. */
5133 static rtx
5134 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
5135 HOST_WIDE_INT bitsize)
5137 if (bitsize != -1)
5139 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
5140 if (bitpos != 0)
5141 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
5143 return loc_note;
5146 /* This function either modifies location piece list *DEST in
5147 place (if SRC and INNER is NULL), or copies location piece list
5148 *SRC to *DEST while modifying it. Location BITPOS is modified
5149 to contain LOC_NOTE, any pieces overlapping it are removed resp.
5150 not copied and if needed some padding around it is added.
5151 When modifying in place, DEST should point to EXPR_LIST where
5152 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
5153 to the start of the whole list and INNER points to the EXPR_LIST
5154 where earlier pieces cover PIECE_BITPOS bits. */
5156 static void
5157 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5158 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5159 HOST_WIDE_INT bitsize, rtx loc_note)
5161 HOST_WIDE_INT diff;
5162 bool copy = inner != NULL;
5164 if (copy)
5166 /* First copy all nodes preceding the current bitpos. */
5167 while (src != inner)
5169 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5170 decl_piece_bitsize (*src), NULL_RTX);
5171 dest = &XEXP (*dest, 1);
5172 src = &XEXP (*src, 1);
5175 /* Add padding if needed. */
5176 if (bitpos != piece_bitpos)
5178 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5179 copy ? NULL_RTX : *dest);
5180 dest = &XEXP (*dest, 1);
5182 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5184 gcc_assert (!copy);
5185 /* A piece with correct bitpos and bitsize already exist,
5186 just update the location for it and return. */
5187 *decl_piece_varloc_ptr (*dest) = loc_note;
5188 return;
5190 /* Add the piece that changed. */
5191 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5192 dest = &XEXP (*dest, 1);
5193 /* Skip over pieces that overlap it. */
5194 diff = bitpos - piece_bitpos + bitsize;
5195 if (!copy)
5196 src = dest;
5197 while (diff > 0 && *src)
5199 rtx piece = *src;
5200 diff -= decl_piece_bitsize (piece);
5201 if (copy)
5202 src = &XEXP (piece, 1);
5203 else
5205 *src = XEXP (piece, 1);
5206 free_EXPR_LIST_node (piece);
5209 /* Add padding if needed. */
5210 if (diff < 0 && *src)
5212 if (!copy)
5213 dest = src;
5214 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5215 dest = &XEXP (*dest, 1);
5217 if (!copy)
5218 return;
5219 /* Finally copy all nodes following it. */
5220 while (*src)
5222 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5223 decl_piece_bitsize (*src), NULL_RTX);
5224 dest = &XEXP (*dest, 1);
5225 src = &XEXP (*src, 1);
5229 /* Add a variable location node to the linked list for DECL. */
5231 static struct var_loc_node *
5232 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5234 unsigned int decl_id;
5235 var_loc_list *temp;
5236 struct var_loc_node *loc = NULL;
5237 HOST_WIDE_INT bitsize = -1, bitpos = -1;
5239 if (TREE_CODE (decl) == VAR_DECL
5240 && DECL_HAS_DEBUG_EXPR_P (decl))
5242 tree realdecl = DECL_DEBUG_EXPR (decl);
5243 if (handled_component_p (realdecl)
5244 || (TREE_CODE (realdecl) == MEM_REF
5245 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5247 HOST_WIDE_INT maxsize;
5248 tree innerdecl;
5249 innerdecl
5250 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
5251 if (!DECL_P (innerdecl)
5252 || DECL_IGNORED_P (innerdecl)
5253 || TREE_STATIC (innerdecl)
5254 || bitsize <= 0
5255 || bitpos + bitsize > 256
5256 || bitsize != maxsize)
5257 return NULL;
5258 decl = innerdecl;
5262 decl_id = DECL_UID (decl);
5263 var_loc_list **slot
5264 = decl_loc_table->find_slot_with_hash (decl, decl_id, INSERT);
5265 if (*slot == NULL)
5267 temp = ggc_cleared_alloc<var_loc_list> ();
5268 temp->decl_id = decl_id;
5269 *slot = temp;
5271 else
5272 temp = *slot;
5274 /* For PARM_DECLs try to keep around the original incoming value,
5275 even if that means we'll emit a zero-range .debug_loc entry. */
5276 if (temp->last
5277 && temp->first == temp->last
5278 && TREE_CODE (decl) == PARM_DECL
5279 && NOTE_P (temp->first->loc)
5280 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5281 && DECL_INCOMING_RTL (decl)
5282 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5283 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5284 == GET_CODE (DECL_INCOMING_RTL (decl))
5285 && prev_real_insn (temp->first->loc) == NULL_RTX
5286 && (bitsize != -1
5287 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5288 NOTE_VAR_LOCATION_LOC (loc_note))
5289 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5290 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5292 loc = ggc_cleared_alloc<var_loc_node> ();
5293 temp->first->next = loc;
5294 temp->last = loc;
5295 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5297 else if (temp->last)
5299 struct var_loc_node *last = temp->last, *unused = NULL;
5300 rtx *piece_loc = NULL, last_loc_note;
5301 HOST_WIDE_INT piece_bitpos = 0;
5302 if (last->next)
5304 last = last->next;
5305 gcc_assert (last->next == NULL);
5307 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5309 piece_loc = &last->loc;
5312 HOST_WIDE_INT cur_bitsize = decl_piece_bitsize (*piece_loc);
5313 if (piece_bitpos + cur_bitsize > bitpos)
5314 break;
5315 piece_bitpos += cur_bitsize;
5316 piece_loc = &XEXP (*piece_loc, 1);
5318 while (*piece_loc);
5320 /* TEMP->LAST here is either pointer to the last but one or
5321 last element in the chained list, LAST is pointer to the
5322 last element. */
5323 if (label && strcmp (last->label, label) == 0)
5325 /* For SRA optimized variables if there weren't any real
5326 insns since last note, just modify the last node. */
5327 if (piece_loc != NULL)
5329 adjust_piece_list (piece_loc, NULL, NULL,
5330 bitpos, piece_bitpos, bitsize, loc_note);
5331 return NULL;
5333 /* If the last note doesn't cover any instructions, remove it. */
5334 if (temp->last != last)
5336 temp->last->next = NULL;
5337 unused = last;
5338 last = temp->last;
5339 gcc_assert (strcmp (last->label, label) != 0);
5341 else
5343 gcc_assert (temp->first == temp->last
5344 || (temp->first->next == temp->last
5345 && TREE_CODE (decl) == PARM_DECL));
5346 memset (temp->last, '\0', sizeof (*temp->last));
5347 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5348 return temp->last;
5351 if (bitsize == -1 && NOTE_P (last->loc))
5352 last_loc_note = last->loc;
5353 else if (piece_loc != NULL
5354 && *piece_loc != NULL_RTX
5355 && piece_bitpos == bitpos
5356 && decl_piece_bitsize (*piece_loc) == bitsize)
5357 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5358 else
5359 last_loc_note = NULL_RTX;
5360 /* If the current location is the same as the end of the list,
5361 and either both or neither of the locations is uninitialized,
5362 we have nothing to do. */
5363 if (last_loc_note == NULL_RTX
5364 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5365 NOTE_VAR_LOCATION_LOC (loc_note)))
5366 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5367 != NOTE_VAR_LOCATION_STATUS (loc_note))
5368 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5369 == VAR_INIT_STATUS_UNINITIALIZED)
5370 || (NOTE_VAR_LOCATION_STATUS (loc_note)
5371 == VAR_INIT_STATUS_UNINITIALIZED))))
5373 /* Add LOC to the end of list and update LAST. If the last
5374 element of the list has been removed above, reuse its
5375 memory for the new node, otherwise allocate a new one. */
5376 if (unused)
5378 loc = unused;
5379 memset (loc, '\0', sizeof (*loc));
5381 else
5382 loc = ggc_cleared_alloc<var_loc_node> ();
5383 if (bitsize == -1 || piece_loc == NULL)
5384 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5385 else
5386 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5387 bitpos, piece_bitpos, bitsize, loc_note);
5388 last->next = loc;
5389 /* Ensure TEMP->LAST will point either to the new last but one
5390 element of the chain, or to the last element in it. */
5391 if (last != temp->last)
5392 temp->last = last;
5394 else if (unused)
5395 ggc_free (unused);
5397 else
5399 loc = ggc_cleared_alloc<var_loc_node> ();
5400 temp->first = loc;
5401 temp->last = loc;
5402 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5404 return loc;
5407 /* Keep track of the number of spaces used to indent the
5408 output of the debugging routines that print the structure of
5409 the DIE internal representation. */
5410 static int print_indent;
5412 /* Indent the line the number of spaces given by print_indent. */
5414 static inline void
5415 print_spaces (FILE *outfile)
5417 fprintf (outfile, "%*s", print_indent, "");
5420 /* Print a type signature in hex. */
5422 static inline void
5423 print_signature (FILE *outfile, char *sig)
5425 int i;
5427 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5428 fprintf (outfile, "%02x", sig[i] & 0xff);
5431 static void print_loc_descr (dw_loc_descr_ref, FILE *);
5433 /* Print the value associated to the VAL DWARF value node to OUTFILE. If
5434 RECURSE, output location descriptor operations. */
5436 static void
5437 print_dw_val (dw_val_node *val, bool recurse, FILE *outfile)
5439 switch (val->val_class)
5441 case dw_val_class_addr:
5442 fprintf (outfile, "address");
5443 break;
5444 case dw_val_class_offset:
5445 fprintf (outfile, "offset");
5446 break;
5447 case dw_val_class_loc:
5448 fprintf (outfile, "location descriptor");
5449 if (val->v.val_loc == NULL)
5450 fprintf (outfile, " -> <null>\n");
5451 else if (recurse)
5453 fprintf (outfile, ":\n");
5454 print_indent += 4;
5455 print_loc_descr (val->v.val_loc, outfile);
5456 print_indent -= 4;
5458 else
5459 fprintf (outfile, " (%p)\n", (void *) val->v.val_loc);
5460 break;
5461 case dw_val_class_loc_list:
5462 fprintf (outfile, "location list -> label:%s",
5463 val->v.val_loc_list->ll_symbol);
5464 break;
5465 case dw_val_class_range_list:
5466 fprintf (outfile, "range list");
5467 break;
5468 case dw_val_class_const:
5469 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, val->v.val_int);
5470 break;
5471 case dw_val_class_unsigned_const:
5472 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, val->v.val_unsigned);
5473 break;
5474 case dw_val_class_const_double:
5475 fprintf (outfile, "constant (" HOST_WIDE_INT_PRINT_DEC","\
5476 HOST_WIDE_INT_PRINT_UNSIGNED")",
5477 val->v.val_double.high,
5478 val->v.val_double.low);
5479 break;
5480 case dw_val_class_wide_int:
5482 int i = val->v.val_wide->get_len ();
5483 fprintf (outfile, "constant (");
5484 gcc_assert (i > 0);
5485 if (val->v.val_wide->elt (i - 1) == 0)
5486 fprintf (outfile, "0x");
5487 fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
5488 val->v.val_wide->elt (--i));
5489 while (--i >= 0)
5490 fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
5491 val->v.val_wide->elt (i));
5492 fprintf (outfile, ")");
5493 break;
5495 case dw_val_class_vec:
5496 fprintf (outfile, "floating-point or vector constant");
5497 break;
5498 case dw_val_class_flag:
5499 fprintf (outfile, "%u", val->v.val_flag);
5500 break;
5501 case dw_val_class_die_ref:
5502 if (val->v.val_die_ref.die != NULL)
5504 dw_die_ref die = val->v.val_die_ref.die;
5506 if (die->comdat_type_p)
5508 fprintf (outfile, "die -> signature: ");
5509 print_signature (outfile,
5510 die->die_id.die_type_node->signature);
5512 else if (die->die_id.die_symbol)
5513 fprintf (outfile, "die -> label: %s", die->die_id.die_symbol);
5514 else
5515 fprintf (outfile, "die -> %ld", die->die_offset);
5516 fprintf (outfile, " (%p)", (void *) die);
5518 else
5519 fprintf (outfile, "die -> <null>");
5520 break;
5521 case dw_val_class_vms_delta:
5522 fprintf (outfile, "delta: @slotcount(%s-%s)",
5523 val->v.val_vms_delta.lbl2, val->v.val_vms_delta.lbl1);
5524 break;
5525 case dw_val_class_lbl_id:
5526 case dw_val_class_lineptr:
5527 case dw_val_class_macptr:
5528 case dw_val_class_high_pc:
5529 fprintf (outfile, "label: %s", val->v.val_lbl_id);
5530 break;
5531 case dw_val_class_str:
5532 if (val->v.val_str->str != NULL)
5533 fprintf (outfile, "\"%s\"", val->v.val_str->str);
5534 else
5535 fprintf (outfile, "<null>");
5536 break;
5537 case dw_val_class_file:
5538 fprintf (outfile, "\"%s\" (%d)", val->v.val_file->filename,
5539 val->v.val_file->emitted_number);
5540 break;
5541 case dw_val_class_data8:
5543 int i;
5545 for (i = 0; i < 8; i++)
5546 fprintf (outfile, "%02x", val->v.val_data8[i]);
5547 break;
5549 default:
5550 break;
5554 /* Likewise, for a DIE attribute. */
5556 static void
5557 print_attribute (dw_attr_ref a, bool recurse, FILE *outfile)
5559 print_dw_val (&a->dw_attr_val, recurse, outfile);
5563 /* Print the list of operands in the LOC location description to OUTFILE. This
5564 routine is a debugging aid only. */
5566 static void
5567 print_loc_descr (dw_loc_descr_ref loc, FILE *outfile)
5569 dw_loc_descr_ref l = loc;
5571 if (loc == NULL)
5573 print_spaces (outfile);
5574 fprintf (outfile, "<null>\n");
5575 return;
5578 for (l = loc; l != NULL; l = l->dw_loc_next)
5580 print_spaces (outfile);
5581 fprintf (outfile, "(%p) %s",
5582 (void *) l,
5583 dwarf_stack_op_name (l->dw_loc_opc));
5584 if (l->dw_loc_oprnd1.val_class != dw_val_class_none)
5586 fprintf (outfile, " ");
5587 print_dw_val (&l->dw_loc_oprnd1, false, outfile);
5589 if (l->dw_loc_oprnd2.val_class != dw_val_class_none)
5591 fprintf (outfile, ", ");
5592 print_dw_val (&l->dw_loc_oprnd2, false, outfile);
5594 fprintf (outfile, "\n");
5598 /* Print the information associated with a given DIE, and its children.
5599 This routine is a debugging aid only. */
5601 static void
5602 print_die (dw_die_ref die, FILE *outfile)
5604 dw_attr_ref a;
5605 dw_die_ref c;
5606 unsigned ix;
5608 print_spaces (outfile);
5609 fprintf (outfile, "DIE %4ld: %s (%p)\n",
5610 die->die_offset, dwarf_tag_name (die->die_tag),
5611 (void*) die);
5612 print_spaces (outfile);
5613 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5614 fprintf (outfile, " offset: %ld", die->die_offset);
5615 fprintf (outfile, " mark: %d\n", die->die_mark);
5617 if (die->comdat_type_p)
5619 print_spaces (outfile);
5620 fprintf (outfile, " signature: ");
5621 print_signature (outfile, die->die_id.die_type_node->signature);
5622 fprintf (outfile, "\n");
5625 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5627 print_spaces (outfile);
5628 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5630 print_attribute (a, true, outfile);
5631 fprintf (outfile, "\n");
5634 if (die->die_child != NULL)
5636 print_indent += 4;
5637 FOR_EACH_CHILD (die, c, print_die (c, outfile));
5638 print_indent -= 4;
5640 if (print_indent == 0)
5641 fprintf (outfile, "\n");
5644 /* Print the list of operations in the LOC location description. */
5646 DEBUG_FUNCTION void
5647 debug_dwarf_loc_descr (dw_loc_descr_ref loc)
5649 print_loc_descr (loc, stderr);
5652 /* Print the information collected for a given DIE. */
5654 DEBUG_FUNCTION void
5655 debug_dwarf_die (dw_die_ref die)
5657 print_die (die, stderr);
5660 DEBUG_FUNCTION void
5661 debug (die_struct &ref)
5663 print_die (&ref, stderr);
5666 DEBUG_FUNCTION void
5667 debug (die_struct *ptr)
5669 if (ptr)
5670 debug (*ptr);
5671 else
5672 fprintf (stderr, "<nil>\n");
5676 /* Print all DWARF information collected for the compilation unit.
5677 This routine is a debugging aid only. */
5679 DEBUG_FUNCTION void
5680 debug_dwarf (void)
5682 print_indent = 0;
5683 print_die (comp_unit_die (), stderr);
5686 #ifdef ENABLE_CHECKING
5687 /* Sanity checks on DIEs. */
5689 static void
5690 check_die (dw_die_ref die)
5692 unsigned ix;
5693 dw_attr_ref a;
5694 bool inline_found = false;
5695 int n_location = 0, n_low_pc = 0, n_high_pc = 0, n_artificial = 0;
5696 int n_decl_line = 0, n_decl_file = 0;
5697 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5699 switch (a->dw_attr)
5701 case DW_AT_inline:
5702 if (a->dw_attr_val.v.val_unsigned)
5703 inline_found = true;
5704 break;
5705 case DW_AT_location:
5706 ++n_location;
5707 break;
5708 case DW_AT_low_pc:
5709 ++n_low_pc;
5710 break;
5711 case DW_AT_high_pc:
5712 ++n_high_pc;
5713 break;
5714 case DW_AT_artificial:
5715 ++n_artificial;
5716 break;
5717 case DW_AT_decl_line:
5718 ++n_decl_line;
5719 break;
5720 case DW_AT_decl_file:
5721 ++n_decl_file;
5722 break;
5723 default:
5724 break;
5727 if (n_location > 1 || n_low_pc > 1 || n_high_pc > 1 || n_artificial > 1
5728 || n_decl_line > 1 || n_decl_file > 1)
5730 fprintf (stderr, "Duplicate attributes in DIE:\n");
5731 debug_dwarf_die (die);
5732 gcc_unreachable ();
5734 if (inline_found)
5736 /* A debugging information entry that is a member of an abstract
5737 instance tree [that has DW_AT_inline] should not contain any
5738 attributes which describe aspects of the subroutine which vary
5739 between distinct inlined expansions or distinct out-of-line
5740 expansions. */
5741 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5742 gcc_assert (a->dw_attr != DW_AT_low_pc
5743 && a->dw_attr != DW_AT_high_pc
5744 && a->dw_attr != DW_AT_location
5745 && a->dw_attr != DW_AT_frame_base
5746 && a->dw_attr != DW_AT_GNU_all_call_sites);
5749 #endif
5751 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
5752 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
5753 DIE that marks the start of the DIEs for this include file. */
5755 static dw_die_ref
5756 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5758 const char *filename = get_AT_string (bincl_die, DW_AT_name);
5759 dw_die_ref new_unit = gen_compile_unit_die (filename);
5761 new_unit->die_sib = old_unit;
5762 return new_unit;
5765 /* Close an include-file CU and reopen the enclosing one. */
5767 static dw_die_ref
5768 pop_compile_unit (dw_die_ref old_unit)
5770 dw_die_ref new_unit = old_unit->die_sib;
5772 old_unit->die_sib = NULL;
5773 return new_unit;
5776 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5777 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5778 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5780 /* Calculate the checksum of a location expression. */
5782 static inline void
5783 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5785 int tem;
5786 inchash::hash hstate;
5787 hashval_t hash;
5789 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
5790 CHECKSUM (tem);
5791 hash_loc_operands (loc, hstate);
5792 hash = hstate.end();
5793 CHECKSUM (hash);
5796 /* Calculate the checksum of an attribute. */
5798 static void
5799 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5801 dw_loc_descr_ref loc;
5802 rtx r;
5804 CHECKSUM (at->dw_attr);
5806 /* We don't care that this was compiled with a different compiler
5807 snapshot; if the output is the same, that's what matters. */
5808 if (at->dw_attr == DW_AT_producer)
5809 return;
5811 switch (AT_class (at))
5813 case dw_val_class_const:
5814 CHECKSUM (at->dw_attr_val.v.val_int);
5815 break;
5816 case dw_val_class_unsigned_const:
5817 CHECKSUM (at->dw_attr_val.v.val_unsigned);
5818 break;
5819 case dw_val_class_const_double:
5820 CHECKSUM (at->dw_attr_val.v.val_double);
5821 break;
5822 case dw_val_class_wide_int:
5823 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
5824 get_full_len (*at->dw_attr_val.v.val_wide)
5825 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
5826 break;
5827 case dw_val_class_vec:
5828 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5829 (at->dw_attr_val.v.val_vec.length
5830 * at->dw_attr_val.v.val_vec.elt_size));
5831 break;
5832 case dw_val_class_flag:
5833 CHECKSUM (at->dw_attr_val.v.val_flag);
5834 break;
5835 case dw_val_class_str:
5836 CHECKSUM_STRING (AT_string (at));
5837 break;
5839 case dw_val_class_addr:
5840 r = AT_addr (at);
5841 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5842 CHECKSUM_STRING (XSTR (r, 0));
5843 break;
5845 case dw_val_class_offset:
5846 CHECKSUM (at->dw_attr_val.v.val_offset);
5847 break;
5849 case dw_val_class_loc:
5850 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5851 loc_checksum (loc, ctx);
5852 break;
5854 case dw_val_class_die_ref:
5855 die_checksum (AT_ref (at), ctx, mark);
5856 break;
5858 case dw_val_class_fde_ref:
5859 case dw_val_class_vms_delta:
5860 case dw_val_class_lbl_id:
5861 case dw_val_class_lineptr:
5862 case dw_val_class_macptr:
5863 case dw_val_class_high_pc:
5864 break;
5866 case dw_val_class_file:
5867 CHECKSUM_STRING (AT_file (at)->filename);
5868 break;
5870 case dw_val_class_data8:
5871 CHECKSUM (at->dw_attr_val.v.val_data8);
5872 break;
5874 default:
5875 break;
5879 /* Calculate the checksum of a DIE. */
5881 static void
5882 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5884 dw_die_ref c;
5885 dw_attr_ref a;
5886 unsigned ix;
5888 /* To avoid infinite recursion. */
5889 if (die->die_mark)
5891 CHECKSUM (die->die_mark);
5892 return;
5894 die->die_mark = ++(*mark);
5896 CHECKSUM (die->die_tag);
5898 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5899 attr_checksum (a, ctx, mark);
5901 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
5904 #undef CHECKSUM
5905 #undef CHECKSUM_BLOCK
5906 #undef CHECKSUM_STRING
5908 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
5909 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5910 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5911 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
5912 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
5913 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
5914 #define CHECKSUM_ATTR(FOO) \
5915 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
5917 /* Calculate the checksum of a number in signed LEB128 format. */
5919 static void
5920 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
5922 unsigned char byte;
5923 bool more;
5925 while (1)
5927 byte = (value & 0x7f);
5928 value >>= 7;
5929 more = !((value == 0 && (byte & 0x40) == 0)
5930 || (value == -1 && (byte & 0x40) != 0));
5931 if (more)
5932 byte |= 0x80;
5933 CHECKSUM (byte);
5934 if (!more)
5935 break;
5939 /* Calculate the checksum of a number in unsigned LEB128 format. */
5941 static void
5942 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
5944 while (1)
5946 unsigned char byte = (value & 0x7f);
5947 value >>= 7;
5948 if (value != 0)
5949 /* More bytes to follow. */
5950 byte |= 0x80;
5951 CHECKSUM (byte);
5952 if (value == 0)
5953 break;
5957 /* Checksum the context of the DIE. This adds the names of any
5958 surrounding namespaces or structures to the checksum. */
5960 static void
5961 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
5963 const char *name;
5964 dw_die_ref spec;
5965 int tag = die->die_tag;
5967 if (tag != DW_TAG_namespace
5968 && tag != DW_TAG_structure_type
5969 && tag != DW_TAG_class_type)
5970 return;
5972 name = get_AT_string (die, DW_AT_name);
5974 spec = get_AT_ref (die, DW_AT_specification);
5975 if (spec != NULL)
5976 die = spec;
5978 if (die->die_parent != NULL)
5979 checksum_die_context (die->die_parent, ctx);
5981 CHECKSUM_ULEB128 ('C');
5982 CHECKSUM_ULEB128 (tag);
5983 if (name != NULL)
5984 CHECKSUM_STRING (name);
5987 /* Calculate the checksum of a location expression. */
5989 static inline void
5990 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5992 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
5993 were emitted as a DW_FORM_sdata instead of a location expression. */
5994 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
5996 CHECKSUM_ULEB128 (DW_FORM_sdata);
5997 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
5998 return;
6001 /* Otherwise, just checksum the raw location expression. */
6002 while (loc != NULL)
6004 inchash::hash hstate;
6005 hashval_t hash;
6007 CHECKSUM_ULEB128 (loc->dtprel);
6008 CHECKSUM_ULEB128 (loc->dw_loc_opc);
6009 hash_loc_operands (loc, hstate);
6010 hash = hstate.end ();
6011 CHECKSUM (hash);
6012 loc = loc->dw_loc_next;
6016 /* Calculate the checksum of an attribute. */
6018 static void
6019 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
6020 struct md5_ctx *ctx, int *mark)
6022 dw_loc_descr_ref loc;
6023 rtx r;
6025 if (AT_class (at) == dw_val_class_die_ref)
6027 dw_die_ref target_die = AT_ref (at);
6029 /* For pointer and reference types, we checksum only the (qualified)
6030 name of the target type (if there is a name). For friend entries,
6031 we checksum only the (qualified) name of the target type or function.
6032 This allows the checksum to remain the same whether the target type
6033 is complete or not. */
6034 if ((at->dw_attr == DW_AT_type
6035 && (tag == DW_TAG_pointer_type
6036 || tag == DW_TAG_reference_type
6037 || tag == DW_TAG_rvalue_reference_type
6038 || tag == DW_TAG_ptr_to_member_type))
6039 || (at->dw_attr == DW_AT_friend
6040 && tag == DW_TAG_friend))
6042 dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
6044 if (name_attr != NULL)
6046 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6048 if (decl == NULL)
6049 decl = target_die;
6050 CHECKSUM_ULEB128 ('N');
6051 CHECKSUM_ULEB128 (at->dw_attr);
6052 if (decl->die_parent != NULL)
6053 checksum_die_context (decl->die_parent, ctx);
6054 CHECKSUM_ULEB128 ('E');
6055 CHECKSUM_STRING (AT_string (name_attr));
6056 return;
6060 /* For all other references to another DIE, we check to see if the
6061 target DIE has already been visited. If it has, we emit a
6062 backward reference; if not, we descend recursively. */
6063 if (target_die->die_mark > 0)
6065 CHECKSUM_ULEB128 ('R');
6066 CHECKSUM_ULEB128 (at->dw_attr);
6067 CHECKSUM_ULEB128 (target_die->die_mark);
6069 else
6071 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6073 if (decl == NULL)
6074 decl = target_die;
6075 target_die->die_mark = ++(*mark);
6076 CHECKSUM_ULEB128 ('T');
6077 CHECKSUM_ULEB128 (at->dw_attr);
6078 if (decl->die_parent != NULL)
6079 checksum_die_context (decl->die_parent, ctx);
6080 die_checksum_ordered (target_die, ctx, mark);
6082 return;
6085 CHECKSUM_ULEB128 ('A');
6086 CHECKSUM_ULEB128 (at->dw_attr);
6088 switch (AT_class (at))
6090 case dw_val_class_const:
6091 CHECKSUM_ULEB128 (DW_FORM_sdata);
6092 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
6093 break;
6095 case dw_val_class_unsigned_const:
6096 CHECKSUM_ULEB128 (DW_FORM_sdata);
6097 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
6098 break;
6100 case dw_val_class_const_double:
6101 CHECKSUM_ULEB128 (DW_FORM_block);
6102 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
6103 CHECKSUM (at->dw_attr_val.v.val_double);
6104 break;
6106 case dw_val_class_wide_int:
6107 CHECKSUM_ULEB128 (DW_FORM_block);
6108 CHECKSUM_ULEB128 (get_full_len (*at->dw_attr_val.v.val_wide)
6109 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
6110 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6111 get_full_len (*at->dw_attr_val.v.val_wide)
6112 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6113 break;
6115 case dw_val_class_vec:
6116 CHECKSUM_ULEB128 (DW_FORM_block);
6117 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
6118 * at->dw_attr_val.v.val_vec.elt_size);
6119 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6120 (at->dw_attr_val.v.val_vec.length
6121 * at->dw_attr_val.v.val_vec.elt_size));
6122 break;
6124 case dw_val_class_flag:
6125 CHECKSUM_ULEB128 (DW_FORM_flag);
6126 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
6127 break;
6129 case dw_val_class_str:
6130 CHECKSUM_ULEB128 (DW_FORM_string);
6131 CHECKSUM_STRING (AT_string (at));
6132 break;
6134 case dw_val_class_addr:
6135 r = AT_addr (at);
6136 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6137 CHECKSUM_ULEB128 (DW_FORM_string);
6138 CHECKSUM_STRING (XSTR (r, 0));
6139 break;
6141 case dw_val_class_offset:
6142 CHECKSUM_ULEB128 (DW_FORM_sdata);
6143 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
6144 break;
6146 case dw_val_class_loc:
6147 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6148 loc_checksum_ordered (loc, ctx);
6149 break;
6151 case dw_val_class_fde_ref:
6152 case dw_val_class_lbl_id:
6153 case dw_val_class_lineptr:
6154 case dw_val_class_macptr:
6155 case dw_val_class_high_pc:
6156 break;
6158 case dw_val_class_file:
6159 CHECKSUM_ULEB128 (DW_FORM_string);
6160 CHECKSUM_STRING (AT_file (at)->filename);
6161 break;
6163 case dw_val_class_data8:
6164 CHECKSUM (at->dw_attr_val.v.val_data8);
6165 break;
6167 default:
6168 break;
6172 struct checksum_attributes
6174 dw_attr_ref at_name;
6175 dw_attr_ref at_type;
6176 dw_attr_ref at_friend;
6177 dw_attr_ref at_accessibility;
6178 dw_attr_ref at_address_class;
6179 dw_attr_ref at_allocated;
6180 dw_attr_ref at_artificial;
6181 dw_attr_ref at_associated;
6182 dw_attr_ref at_binary_scale;
6183 dw_attr_ref at_bit_offset;
6184 dw_attr_ref at_bit_size;
6185 dw_attr_ref at_bit_stride;
6186 dw_attr_ref at_byte_size;
6187 dw_attr_ref at_byte_stride;
6188 dw_attr_ref at_const_value;
6189 dw_attr_ref at_containing_type;
6190 dw_attr_ref at_count;
6191 dw_attr_ref at_data_location;
6192 dw_attr_ref at_data_member_location;
6193 dw_attr_ref at_decimal_scale;
6194 dw_attr_ref at_decimal_sign;
6195 dw_attr_ref at_default_value;
6196 dw_attr_ref at_digit_count;
6197 dw_attr_ref at_discr;
6198 dw_attr_ref at_discr_list;
6199 dw_attr_ref at_discr_value;
6200 dw_attr_ref at_encoding;
6201 dw_attr_ref at_endianity;
6202 dw_attr_ref at_explicit;
6203 dw_attr_ref at_is_optional;
6204 dw_attr_ref at_location;
6205 dw_attr_ref at_lower_bound;
6206 dw_attr_ref at_mutable;
6207 dw_attr_ref at_ordering;
6208 dw_attr_ref at_picture_string;
6209 dw_attr_ref at_prototyped;
6210 dw_attr_ref at_small;
6211 dw_attr_ref at_segment;
6212 dw_attr_ref at_string_length;
6213 dw_attr_ref at_threads_scaled;
6214 dw_attr_ref at_upper_bound;
6215 dw_attr_ref at_use_location;
6216 dw_attr_ref at_use_UTF8;
6217 dw_attr_ref at_variable_parameter;
6218 dw_attr_ref at_virtuality;
6219 dw_attr_ref at_visibility;
6220 dw_attr_ref at_vtable_elem_location;
6223 /* Collect the attributes that we will want to use for the checksum. */
6225 static void
6226 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
6228 dw_attr_ref a;
6229 unsigned ix;
6231 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6233 switch (a->dw_attr)
6235 case DW_AT_name:
6236 attrs->at_name = a;
6237 break;
6238 case DW_AT_type:
6239 attrs->at_type = a;
6240 break;
6241 case DW_AT_friend:
6242 attrs->at_friend = a;
6243 break;
6244 case DW_AT_accessibility:
6245 attrs->at_accessibility = a;
6246 break;
6247 case DW_AT_address_class:
6248 attrs->at_address_class = a;
6249 break;
6250 case DW_AT_allocated:
6251 attrs->at_allocated = a;
6252 break;
6253 case DW_AT_artificial:
6254 attrs->at_artificial = a;
6255 break;
6256 case DW_AT_associated:
6257 attrs->at_associated = a;
6258 break;
6259 case DW_AT_binary_scale:
6260 attrs->at_binary_scale = a;
6261 break;
6262 case DW_AT_bit_offset:
6263 attrs->at_bit_offset = a;
6264 break;
6265 case DW_AT_bit_size:
6266 attrs->at_bit_size = a;
6267 break;
6268 case DW_AT_bit_stride:
6269 attrs->at_bit_stride = a;
6270 break;
6271 case DW_AT_byte_size:
6272 attrs->at_byte_size = a;
6273 break;
6274 case DW_AT_byte_stride:
6275 attrs->at_byte_stride = a;
6276 break;
6277 case DW_AT_const_value:
6278 attrs->at_const_value = a;
6279 break;
6280 case DW_AT_containing_type:
6281 attrs->at_containing_type = a;
6282 break;
6283 case DW_AT_count:
6284 attrs->at_count = a;
6285 break;
6286 case DW_AT_data_location:
6287 attrs->at_data_location = a;
6288 break;
6289 case DW_AT_data_member_location:
6290 attrs->at_data_member_location = a;
6291 break;
6292 case DW_AT_decimal_scale:
6293 attrs->at_decimal_scale = a;
6294 break;
6295 case DW_AT_decimal_sign:
6296 attrs->at_decimal_sign = a;
6297 break;
6298 case DW_AT_default_value:
6299 attrs->at_default_value = a;
6300 break;
6301 case DW_AT_digit_count:
6302 attrs->at_digit_count = a;
6303 break;
6304 case DW_AT_discr:
6305 attrs->at_discr = a;
6306 break;
6307 case DW_AT_discr_list:
6308 attrs->at_discr_list = a;
6309 break;
6310 case DW_AT_discr_value:
6311 attrs->at_discr_value = a;
6312 break;
6313 case DW_AT_encoding:
6314 attrs->at_encoding = a;
6315 break;
6316 case DW_AT_endianity:
6317 attrs->at_endianity = a;
6318 break;
6319 case DW_AT_explicit:
6320 attrs->at_explicit = a;
6321 break;
6322 case DW_AT_is_optional:
6323 attrs->at_is_optional = a;
6324 break;
6325 case DW_AT_location:
6326 attrs->at_location = a;
6327 break;
6328 case DW_AT_lower_bound:
6329 attrs->at_lower_bound = a;
6330 break;
6331 case DW_AT_mutable:
6332 attrs->at_mutable = a;
6333 break;
6334 case DW_AT_ordering:
6335 attrs->at_ordering = a;
6336 break;
6337 case DW_AT_picture_string:
6338 attrs->at_picture_string = a;
6339 break;
6340 case DW_AT_prototyped:
6341 attrs->at_prototyped = a;
6342 break;
6343 case DW_AT_small:
6344 attrs->at_small = a;
6345 break;
6346 case DW_AT_segment:
6347 attrs->at_segment = a;
6348 break;
6349 case DW_AT_string_length:
6350 attrs->at_string_length = a;
6351 break;
6352 case DW_AT_threads_scaled:
6353 attrs->at_threads_scaled = a;
6354 break;
6355 case DW_AT_upper_bound:
6356 attrs->at_upper_bound = a;
6357 break;
6358 case DW_AT_use_location:
6359 attrs->at_use_location = a;
6360 break;
6361 case DW_AT_use_UTF8:
6362 attrs->at_use_UTF8 = a;
6363 break;
6364 case DW_AT_variable_parameter:
6365 attrs->at_variable_parameter = a;
6366 break;
6367 case DW_AT_virtuality:
6368 attrs->at_virtuality = a;
6369 break;
6370 case DW_AT_visibility:
6371 attrs->at_visibility = a;
6372 break;
6373 case DW_AT_vtable_elem_location:
6374 attrs->at_vtable_elem_location = a;
6375 break;
6376 default:
6377 break;
6382 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
6384 static void
6385 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6387 dw_die_ref c;
6388 dw_die_ref decl;
6389 struct checksum_attributes attrs;
6391 CHECKSUM_ULEB128 ('D');
6392 CHECKSUM_ULEB128 (die->die_tag);
6394 memset (&attrs, 0, sizeof (attrs));
6396 decl = get_AT_ref (die, DW_AT_specification);
6397 if (decl != NULL)
6398 collect_checksum_attributes (&attrs, decl);
6399 collect_checksum_attributes (&attrs, die);
6401 CHECKSUM_ATTR (attrs.at_name);
6402 CHECKSUM_ATTR (attrs.at_accessibility);
6403 CHECKSUM_ATTR (attrs.at_address_class);
6404 CHECKSUM_ATTR (attrs.at_allocated);
6405 CHECKSUM_ATTR (attrs.at_artificial);
6406 CHECKSUM_ATTR (attrs.at_associated);
6407 CHECKSUM_ATTR (attrs.at_binary_scale);
6408 CHECKSUM_ATTR (attrs.at_bit_offset);
6409 CHECKSUM_ATTR (attrs.at_bit_size);
6410 CHECKSUM_ATTR (attrs.at_bit_stride);
6411 CHECKSUM_ATTR (attrs.at_byte_size);
6412 CHECKSUM_ATTR (attrs.at_byte_stride);
6413 CHECKSUM_ATTR (attrs.at_const_value);
6414 CHECKSUM_ATTR (attrs.at_containing_type);
6415 CHECKSUM_ATTR (attrs.at_count);
6416 CHECKSUM_ATTR (attrs.at_data_location);
6417 CHECKSUM_ATTR (attrs.at_data_member_location);
6418 CHECKSUM_ATTR (attrs.at_decimal_scale);
6419 CHECKSUM_ATTR (attrs.at_decimal_sign);
6420 CHECKSUM_ATTR (attrs.at_default_value);
6421 CHECKSUM_ATTR (attrs.at_digit_count);
6422 CHECKSUM_ATTR (attrs.at_discr);
6423 CHECKSUM_ATTR (attrs.at_discr_list);
6424 CHECKSUM_ATTR (attrs.at_discr_value);
6425 CHECKSUM_ATTR (attrs.at_encoding);
6426 CHECKSUM_ATTR (attrs.at_endianity);
6427 CHECKSUM_ATTR (attrs.at_explicit);
6428 CHECKSUM_ATTR (attrs.at_is_optional);
6429 CHECKSUM_ATTR (attrs.at_location);
6430 CHECKSUM_ATTR (attrs.at_lower_bound);
6431 CHECKSUM_ATTR (attrs.at_mutable);
6432 CHECKSUM_ATTR (attrs.at_ordering);
6433 CHECKSUM_ATTR (attrs.at_picture_string);
6434 CHECKSUM_ATTR (attrs.at_prototyped);
6435 CHECKSUM_ATTR (attrs.at_small);
6436 CHECKSUM_ATTR (attrs.at_segment);
6437 CHECKSUM_ATTR (attrs.at_string_length);
6438 CHECKSUM_ATTR (attrs.at_threads_scaled);
6439 CHECKSUM_ATTR (attrs.at_upper_bound);
6440 CHECKSUM_ATTR (attrs.at_use_location);
6441 CHECKSUM_ATTR (attrs.at_use_UTF8);
6442 CHECKSUM_ATTR (attrs.at_variable_parameter);
6443 CHECKSUM_ATTR (attrs.at_virtuality);
6444 CHECKSUM_ATTR (attrs.at_visibility);
6445 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6446 CHECKSUM_ATTR (attrs.at_type);
6447 CHECKSUM_ATTR (attrs.at_friend);
6449 /* Checksum the child DIEs. */
6450 c = die->die_child;
6451 if (c) do {
6452 dw_attr_ref name_attr;
6454 c = c->die_sib;
6455 name_attr = get_AT (c, DW_AT_name);
6456 if (is_template_instantiation (c))
6458 /* Ignore instantiations of member type and function templates. */
6460 else if (name_attr != NULL
6461 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
6463 /* Use a shallow checksum for named nested types and member
6464 functions. */
6465 CHECKSUM_ULEB128 ('S');
6466 CHECKSUM_ULEB128 (c->die_tag);
6467 CHECKSUM_STRING (AT_string (name_attr));
6469 else
6471 /* Use a deep checksum for other children. */
6472 /* Mark this DIE so it gets processed when unmarking. */
6473 if (c->die_mark == 0)
6474 c->die_mark = -1;
6475 die_checksum_ordered (c, ctx, mark);
6477 } while (c != die->die_child);
6479 CHECKSUM_ULEB128 (0);
6482 /* Add a type name and tag to a hash. */
6483 static void
6484 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
6486 CHECKSUM_ULEB128 (tag);
6487 CHECKSUM_STRING (name);
6490 #undef CHECKSUM
6491 #undef CHECKSUM_STRING
6492 #undef CHECKSUM_ATTR
6493 #undef CHECKSUM_LEB128
6494 #undef CHECKSUM_ULEB128
6496 /* Generate the type signature for DIE. This is computed by generating an
6497 MD5 checksum over the DIE's tag, its relevant attributes, and its
6498 children. Attributes that are references to other DIEs are processed
6499 by recursion, using the MARK field to prevent infinite recursion.
6500 If the DIE is nested inside a namespace or another type, we also
6501 need to include that context in the signature. The lower 64 bits
6502 of the resulting MD5 checksum comprise the signature. */
6504 static void
6505 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6507 int mark;
6508 const char *name;
6509 unsigned char checksum[16];
6510 struct md5_ctx ctx;
6511 dw_die_ref decl;
6512 dw_die_ref parent;
6514 name = get_AT_string (die, DW_AT_name);
6515 decl = get_AT_ref (die, DW_AT_specification);
6516 parent = get_die_parent (die);
6518 /* First, compute a signature for just the type name (and its surrounding
6519 context, if any. This is stored in the type unit DIE for link-time
6520 ODR (one-definition rule) checking. */
6522 if (is_cxx () && name != NULL)
6524 md5_init_ctx (&ctx);
6526 /* Checksum the names of surrounding namespaces and structures. */
6527 if (parent != NULL)
6528 checksum_die_context (parent, &ctx);
6530 /* Checksum the current DIE. */
6531 die_odr_checksum (die->die_tag, name, &ctx);
6532 md5_finish_ctx (&ctx, checksum);
6534 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6537 /* Next, compute the complete type signature. */
6539 md5_init_ctx (&ctx);
6540 mark = 1;
6541 die->die_mark = mark;
6543 /* Checksum the names of surrounding namespaces and structures. */
6544 if (parent != NULL)
6545 checksum_die_context (parent, &ctx);
6547 /* Checksum the DIE and its children. */
6548 die_checksum_ordered (die, &ctx, &mark);
6549 unmark_all_dies (die);
6550 md5_finish_ctx (&ctx, checksum);
6552 /* Store the signature in the type node and link the type DIE and the
6553 type node together. */
6554 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6555 DWARF_TYPE_SIGNATURE_SIZE);
6556 die->comdat_type_p = true;
6557 die->die_id.die_type_node = type_node;
6558 type_node->type_die = die;
6560 /* If the DIE is a specification, link its declaration to the type node
6561 as well. */
6562 if (decl != NULL)
6564 decl->comdat_type_p = true;
6565 decl->die_id.die_type_node = type_node;
6569 /* Do the location expressions look same? */
6570 static inline int
6571 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6573 return loc1->dw_loc_opc == loc2->dw_loc_opc
6574 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6575 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6578 /* Do the values look the same? */
6579 static int
6580 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6582 dw_loc_descr_ref loc1, loc2;
6583 rtx r1, r2;
6585 if (v1->val_class != v2->val_class)
6586 return 0;
6588 switch (v1->val_class)
6590 case dw_val_class_const:
6591 return v1->v.val_int == v2->v.val_int;
6592 case dw_val_class_unsigned_const:
6593 return v1->v.val_unsigned == v2->v.val_unsigned;
6594 case dw_val_class_const_double:
6595 return v1->v.val_double.high == v2->v.val_double.high
6596 && v1->v.val_double.low == v2->v.val_double.low;
6597 case dw_val_class_wide_int:
6598 return *v1->v.val_wide == *v2->v.val_wide;
6599 case dw_val_class_vec:
6600 if (v1->v.val_vec.length != v2->v.val_vec.length
6601 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6602 return 0;
6603 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6604 v1->v.val_vec.length * v1->v.val_vec.elt_size))
6605 return 0;
6606 return 1;
6607 case dw_val_class_flag:
6608 return v1->v.val_flag == v2->v.val_flag;
6609 case dw_val_class_str:
6610 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
6612 case dw_val_class_addr:
6613 r1 = v1->v.val_addr;
6614 r2 = v2->v.val_addr;
6615 if (GET_CODE (r1) != GET_CODE (r2))
6616 return 0;
6617 return !rtx_equal_p (r1, r2);
6619 case dw_val_class_offset:
6620 return v1->v.val_offset == v2->v.val_offset;
6622 case dw_val_class_loc:
6623 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6624 loc1 && loc2;
6625 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6626 if (!same_loc_p (loc1, loc2, mark))
6627 return 0;
6628 return !loc1 && !loc2;
6630 case dw_val_class_die_ref:
6631 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6633 case dw_val_class_fde_ref:
6634 case dw_val_class_vms_delta:
6635 case dw_val_class_lbl_id:
6636 case dw_val_class_lineptr:
6637 case dw_val_class_macptr:
6638 case dw_val_class_high_pc:
6639 return 1;
6641 case dw_val_class_file:
6642 return v1->v.val_file == v2->v.val_file;
6644 case dw_val_class_data8:
6645 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
6647 default:
6648 return 1;
6652 /* Do the attributes look the same? */
6654 static int
6655 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6657 if (at1->dw_attr != at2->dw_attr)
6658 return 0;
6660 /* We don't care that this was compiled with a different compiler
6661 snapshot; if the output is the same, that's what matters. */
6662 if (at1->dw_attr == DW_AT_producer)
6663 return 1;
6665 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6668 /* Do the dies look the same? */
6670 static int
6671 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6673 dw_die_ref c1, c2;
6674 dw_attr_ref a1;
6675 unsigned ix;
6677 /* To avoid infinite recursion. */
6678 if (die1->die_mark)
6679 return die1->die_mark == die2->die_mark;
6680 die1->die_mark = die2->die_mark = ++(*mark);
6682 if (die1->die_tag != die2->die_tag)
6683 return 0;
6685 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
6686 return 0;
6688 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
6689 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
6690 return 0;
6692 c1 = die1->die_child;
6693 c2 = die2->die_child;
6694 if (! c1)
6696 if (c2)
6697 return 0;
6699 else
6700 for (;;)
6702 if (!same_die_p (c1, c2, mark))
6703 return 0;
6704 c1 = c1->die_sib;
6705 c2 = c2->die_sib;
6706 if (c1 == die1->die_child)
6708 if (c2 == die2->die_child)
6709 break;
6710 else
6711 return 0;
6715 return 1;
6718 /* Do the dies look the same? Wrapper around same_die_p. */
6720 static int
6721 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6723 int mark = 0;
6724 int ret = same_die_p (die1, die2, &mark);
6726 unmark_all_dies (die1);
6727 unmark_all_dies (die2);
6729 return ret;
6732 /* The prefix to attach to symbols on DIEs in the current comdat debug
6733 info section. */
6734 static const char *comdat_symbol_id;
6736 /* The index of the current symbol within the current comdat CU. */
6737 static unsigned int comdat_symbol_number;
6739 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6740 children, and set comdat_symbol_id accordingly. */
6742 static void
6743 compute_section_prefix (dw_die_ref unit_die)
6745 const char *die_name = get_AT_string (unit_die, DW_AT_name);
6746 const char *base = die_name ? lbasename (die_name) : "anonymous";
6747 char *name = XALLOCAVEC (char, strlen (base) + 64);
6748 char *p;
6749 int i, mark;
6750 unsigned char checksum[16];
6751 struct md5_ctx ctx;
6753 /* Compute the checksum of the DIE, then append part of it as hex digits to
6754 the name filename of the unit. */
6756 md5_init_ctx (&ctx);
6757 mark = 0;
6758 die_checksum (unit_die, &ctx, &mark);
6759 unmark_all_dies (unit_die);
6760 md5_finish_ctx (&ctx, checksum);
6762 sprintf (name, "%s.", base);
6763 clean_symbol_name (name);
6765 p = name + strlen (name);
6766 for (i = 0; i < 4; i++)
6768 sprintf (p, "%.2x", checksum[i]);
6769 p += 2;
6772 comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
6773 comdat_symbol_number = 0;
6776 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
6778 static int
6779 is_type_die (dw_die_ref die)
6781 switch (die->die_tag)
6783 case DW_TAG_array_type:
6784 case DW_TAG_class_type:
6785 case DW_TAG_interface_type:
6786 case DW_TAG_enumeration_type:
6787 case DW_TAG_pointer_type:
6788 case DW_TAG_reference_type:
6789 case DW_TAG_rvalue_reference_type:
6790 case DW_TAG_string_type:
6791 case DW_TAG_structure_type:
6792 case DW_TAG_subroutine_type:
6793 case DW_TAG_union_type:
6794 case DW_TAG_ptr_to_member_type:
6795 case DW_TAG_set_type:
6796 case DW_TAG_subrange_type:
6797 case DW_TAG_base_type:
6798 case DW_TAG_const_type:
6799 case DW_TAG_file_type:
6800 case DW_TAG_packed_type:
6801 case DW_TAG_volatile_type:
6802 case DW_TAG_typedef:
6803 return 1;
6804 default:
6805 return 0;
6809 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6810 Basically, we want to choose the bits that are likely to be shared between
6811 compilations (types) and leave out the bits that are specific to individual
6812 compilations (functions). */
6814 static int
6815 is_comdat_die (dw_die_ref c)
6817 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6818 we do for stabs. The advantage is a greater likelihood of sharing between
6819 objects that don't include headers in the same order (and therefore would
6820 put the base types in a different comdat). jason 8/28/00 */
6822 if (c->die_tag == DW_TAG_base_type)
6823 return 0;
6825 if (c->die_tag == DW_TAG_pointer_type
6826 || c->die_tag == DW_TAG_reference_type
6827 || c->die_tag == DW_TAG_rvalue_reference_type
6828 || c->die_tag == DW_TAG_const_type
6829 || c->die_tag == DW_TAG_volatile_type)
6831 dw_die_ref t = get_AT_ref (c, DW_AT_type);
6833 return t ? is_comdat_die (t) : 0;
6836 return is_type_die (c);
6839 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6840 compilation unit. */
6842 static int
6843 is_symbol_die (dw_die_ref c)
6845 return (is_type_die (c)
6846 || is_declaration_die (c)
6847 || c->die_tag == DW_TAG_namespace
6848 || c->die_tag == DW_TAG_module);
6851 /* Returns true iff C is a compile-unit DIE. */
6853 static inline bool
6854 is_cu_die (dw_die_ref c)
6856 return c && c->die_tag == DW_TAG_compile_unit;
6859 /* Returns true iff C is a unit DIE of some sort. */
6861 static inline bool
6862 is_unit_die (dw_die_ref c)
6864 return c && (c->die_tag == DW_TAG_compile_unit
6865 || c->die_tag == DW_TAG_partial_unit
6866 || c->die_tag == DW_TAG_type_unit);
6869 /* Returns true iff C is a namespace DIE. */
6871 static inline bool
6872 is_namespace_die (dw_die_ref c)
6874 return c && c->die_tag == DW_TAG_namespace;
6877 /* Returns true iff C is a class or structure DIE. */
6879 static inline bool
6880 is_class_die (dw_die_ref c)
6882 return c && (c->die_tag == DW_TAG_class_type
6883 || c->die_tag == DW_TAG_structure_type);
6886 /* Return non-zero if this DIE is a template parameter. */
6888 static inline bool
6889 is_template_parameter (dw_die_ref die)
6891 switch (die->die_tag)
6893 case DW_TAG_template_type_param:
6894 case DW_TAG_template_value_param:
6895 case DW_TAG_GNU_template_template_param:
6896 case DW_TAG_GNU_template_parameter_pack:
6897 return true;
6898 default:
6899 return false;
6903 /* Return non-zero if this DIE represents a template instantiation. */
6905 static inline bool
6906 is_template_instantiation (dw_die_ref die)
6908 dw_die_ref c;
6910 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
6911 return false;
6912 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
6913 return false;
6916 static char *
6917 gen_internal_sym (const char *prefix)
6919 char buf[256];
6921 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6922 return xstrdup (buf);
6925 /* Assign symbols to all worthy DIEs under DIE. */
6927 static void
6928 assign_symbol_names (dw_die_ref die)
6930 dw_die_ref c;
6932 if (is_symbol_die (die) && !die->comdat_type_p)
6934 if (comdat_symbol_id)
6936 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
6938 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6939 comdat_symbol_id, comdat_symbol_number++);
6940 die->die_id.die_symbol = xstrdup (p);
6942 else
6943 die->die_id.die_symbol = gen_internal_sym ("LDIE");
6946 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6949 struct cu_hash_table_entry
6951 dw_die_ref cu;
6952 unsigned min_comdat_num, max_comdat_num;
6953 struct cu_hash_table_entry *next;
6956 /* Helpers to manipulate hash table of CUs. */
6958 struct cu_hash_table_entry_hasher : pointer_hash <cu_hash_table_entry>
6960 typedef die_struct *compare_type;
6961 static inline hashval_t hash (const cu_hash_table_entry *);
6962 static inline bool equal (const cu_hash_table_entry *, const die_struct *);
6963 static inline void remove (cu_hash_table_entry *);
6966 inline hashval_t
6967 cu_hash_table_entry_hasher::hash (const cu_hash_table_entry *entry)
6969 return htab_hash_string (entry->cu->die_id.die_symbol);
6972 inline bool
6973 cu_hash_table_entry_hasher::equal (const cu_hash_table_entry *entry1,
6974 const die_struct *entry2)
6976 return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
6979 inline void
6980 cu_hash_table_entry_hasher::remove (cu_hash_table_entry *entry)
6982 struct cu_hash_table_entry *next;
6984 while (entry)
6986 next = entry->next;
6987 free (entry);
6988 entry = next;
6992 typedef hash_table<cu_hash_table_entry_hasher> cu_hash_type;
6994 /* Check whether we have already seen this CU and set up SYM_NUM
6995 accordingly. */
6996 static int
6997 check_duplicate_cu (dw_die_ref cu, cu_hash_type *htable, unsigned int *sym_num)
6999 struct cu_hash_table_entry dummy;
7000 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
7002 dummy.max_comdat_num = 0;
7004 slot = htable->find_slot_with_hash (cu,
7005 htab_hash_string (cu->die_id.die_symbol),
7006 INSERT);
7007 entry = *slot;
7009 for (; entry; last = entry, entry = entry->next)
7011 if (same_die_p_wrap (cu, entry->cu))
7012 break;
7015 if (entry)
7017 *sym_num = entry->min_comdat_num;
7018 return 1;
7021 entry = XCNEW (struct cu_hash_table_entry);
7022 entry->cu = cu;
7023 entry->min_comdat_num = *sym_num = last->max_comdat_num;
7024 entry->next = *slot;
7025 *slot = entry;
7027 return 0;
7030 /* Record SYM_NUM to record of CU in HTABLE. */
7031 static void
7032 record_comdat_symbol_number (dw_die_ref cu, cu_hash_type *htable,
7033 unsigned int sym_num)
7035 struct cu_hash_table_entry **slot, *entry;
7037 slot = htable->find_slot_with_hash (cu,
7038 htab_hash_string (cu->die_id.die_symbol),
7039 NO_INSERT);
7040 entry = *slot;
7042 entry->max_comdat_num = sym_num;
7045 /* Traverse the DIE (which is always comp_unit_die), and set up
7046 additional compilation units for each of the include files we see
7047 bracketed by BINCL/EINCL. */
7049 static void
7050 break_out_includes (dw_die_ref die)
7052 dw_die_ref c;
7053 dw_die_ref unit = NULL;
7054 limbo_die_node *node, **pnode;
7056 c = die->die_child;
7057 if (c) do {
7058 dw_die_ref prev = c;
7059 c = c->die_sib;
7060 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
7061 || (unit && is_comdat_die (c)))
7063 dw_die_ref next = c->die_sib;
7065 /* This DIE is for a secondary CU; remove it from the main one. */
7066 remove_child_with_prev (c, prev);
7068 if (c->die_tag == DW_TAG_GNU_BINCL)
7069 unit = push_new_compile_unit (unit, c);
7070 else if (c->die_tag == DW_TAG_GNU_EINCL)
7071 unit = pop_compile_unit (unit);
7072 else
7073 add_child_die (unit, c);
7074 c = next;
7075 if (c == die->die_child)
7076 break;
7078 } while (c != die->die_child);
7080 #if 0
7081 /* We can only use this in debugging, since the frontend doesn't check
7082 to make sure that we leave every include file we enter. */
7083 gcc_assert (!unit);
7084 #endif
7086 assign_symbol_names (die);
7087 cu_hash_type cu_hash_table (10);
7088 for (node = limbo_die_list, pnode = &limbo_die_list;
7089 node;
7090 node = node->next)
7092 int is_dupl;
7094 compute_section_prefix (node->die);
7095 is_dupl = check_duplicate_cu (node->die, &cu_hash_table,
7096 &comdat_symbol_number);
7097 assign_symbol_names (node->die);
7098 if (is_dupl)
7099 *pnode = node->next;
7100 else
7102 pnode = &node->next;
7103 record_comdat_symbol_number (node->die, &cu_hash_table,
7104 comdat_symbol_number);
7109 /* Return non-zero if this DIE is a declaration. */
7111 static int
7112 is_declaration_die (dw_die_ref die)
7114 dw_attr_ref a;
7115 unsigned ix;
7117 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7118 if (a->dw_attr == DW_AT_declaration)
7119 return 1;
7121 return 0;
7124 /* Return non-zero if this DIE is nested inside a subprogram. */
7126 static int
7127 is_nested_in_subprogram (dw_die_ref die)
7129 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
7131 if (decl == NULL)
7132 decl = die;
7133 return local_scope_p (decl);
7136 /* Return non-zero if this DIE contains a defining declaration of a
7137 subprogram. */
7139 static int
7140 contains_subprogram_definition (dw_die_ref die)
7142 dw_die_ref c;
7144 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
7145 return 1;
7146 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
7147 return 0;
7150 /* Return non-zero if this is a type DIE that should be moved to a
7151 COMDAT .debug_types section. */
7153 static int
7154 should_move_die_to_comdat (dw_die_ref die)
7156 switch (die->die_tag)
7158 case DW_TAG_class_type:
7159 case DW_TAG_structure_type:
7160 case DW_TAG_enumeration_type:
7161 case DW_TAG_union_type:
7162 /* Don't move declarations, inlined instances, types nested in a
7163 subprogram, or types that contain subprogram definitions. */
7164 if (is_declaration_die (die)
7165 || get_AT (die, DW_AT_abstract_origin)
7166 || is_nested_in_subprogram (die)
7167 || contains_subprogram_definition (die))
7168 return 0;
7169 return 1;
7170 case DW_TAG_array_type:
7171 case DW_TAG_interface_type:
7172 case DW_TAG_pointer_type:
7173 case DW_TAG_reference_type:
7174 case DW_TAG_rvalue_reference_type:
7175 case DW_TAG_string_type:
7176 case DW_TAG_subroutine_type:
7177 case DW_TAG_ptr_to_member_type:
7178 case DW_TAG_set_type:
7179 case DW_TAG_subrange_type:
7180 case DW_TAG_base_type:
7181 case DW_TAG_const_type:
7182 case DW_TAG_file_type:
7183 case DW_TAG_packed_type:
7184 case DW_TAG_volatile_type:
7185 case DW_TAG_typedef:
7186 default:
7187 return 0;
7191 /* Make a clone of DIE. */
7193 static dw_die_ref
7194 clone_die (dw_die_ref die)
7196 dw_die_ref clone;
7197 dw_attr_ref a;
7198 unsigned ix;
7200 clone = ggc_cleared_alloc<die_node> ();
7201 clone->die_tag = die->die_tag;
7203 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7204 add_dwarf_attr (clone, a);
7206 return clone;
7209 /* Make a clone of the tree rooted at DIE. */
7211 static dw_die_ref
7212 clone_tree (dw_die_ref die)
7214 dw_die_ref c;
7215 dw_die_ref clone = clone_die (die);
7217 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
7219 return clone;
7222 /* Make a clone of DIE as a declaration. */
7224 static dw_die_ref
7225 clone_as_declaration (dw_die_ref die)
7227 dw_die_ref clone;
7228 dw_die_ref decl;
7229 dw_attr_ref a;
7230 unsigned ix;
7232 /* If the DIE is already a declaration, just clone it. */
7233 if (is_declaration_die (die))
7234 return clone_die (die);
7236 /* If the DIE is a specification, just clone its declaration DIE. */
7237 decl = get_AT_ref (die, DW_AT_specification);
7238 if (decl != NULL)
7240 clone = clone_die (decl);
7241 if (die->comdat_type_p)
7242 add_AT_die_ref (clone, DW_AT_signature, die);
7243 return clone;
7246 clone = ggc_cleared_alloc<die_node> ();
7247 clone->die_tag = die->die_tag;
7249 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7251 /* We don't want to copy over all attributes.
7252 For example we don't want DW_AT_byte_size because otherwise we will no
7253 longer have a declaration and GDB will treat it as a definition. */
7255 switch (a->dw_attr)
7257 case DW_AT_abstract_origin:
7258 case DW_AT_artificial:
7259 case DW_AT_containing_type:
7260 case DW_AT_external:
7261 case DW_AT_name:
7262 case DW_AT_type:
7263 case DW_AT_virtuality:
7264 case DW_AT_linkage_name:
7265 case DW_AT_MIPS_linkage_name:
7266 add_dwarf_attr (clone, a);
7267 break;
7268 case DW_AT_byte_size:
7269 default:
7270 break;
7274 if (die->comdat_type_p)
7275 add_AT_die_ref (clone, DW_AT_signature, die);
7277 add_AT_flag (clone, DW_AT_declaration, 1);
7278 return clone;
7282 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
7284 struct decl_table_entry
7286 dw_die_ref orig;
7287 dw_die_ref copy;
7290 /* Helpers to manipulate hash table of copied declarations. */
7292 /* Hashtable helpers. */
7294 struct decl_table_entry_hasher : free_ptr_hash <decl_table_entry>
7296 typedef die_struct *compare_type;
7297 static inline hashval_t hash (const decl_table_entry *);
7298 static inline bool equal (const decl_table_entry *, const die_struct *);
7301 inline hashval_t
7302 decl_table_entry_hasher::hash (const decl_table_entry *entry)
7304 return htab_hash_pointer (entry->orig);
7307 inline bool
7308 decl_table_entry_hasher::equal (const decl_table_entry *entry1,
7309 const die_struct *entry2)
7311 return entry1->orig == entry2;
7314 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
7316 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7317 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
7318 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
7319 to check if the ancestor has already been copied into UNIT. */
7321 static dw_die_ref
7322 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
7323 decl_hash_type *decl_table)
7325 dw_die_ref parent = die->die_parent;
7326 dw_die_ref new_parent = unit;
7327 dw_die_ref copy;
7328 decl_table_entry **slot = NULL;
7329 struct decl_table_entry *entry = NULL;
7331 if (decl_table)
7333 /* Check if the entry has already been copied to UNIT. */
7334 slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
7335 INSERT);
7336 if (*slot != HTAB_EMPTY_ENTRY)
7338 entry = *slot;
7339 return entry->copy;
7342 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
7343 entry = XCNEW (struct decl_table_entry);
7344 entry->orig = die;
7345 entry->copy = NULL;
7346 *slot = entry;
7349 if (parent != NULL)
7351 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7352 if (spec != NULL)
7353 parent = spec;
7354 if (!is_unit_die (parent))
7355 new_parent = copy_ancestor_tree (unit, parent, decl_table);
7358 copy = clone_as_declaration (die);
7359 add_child_die (new_parent, copy);
7361 if (decl_table)
7363 /* Record the pointer to the copy. */
7364 entry->copy = copy;
7367 return copy;
7369 /* Copy the declaration context to the new type unit DIE. This includes
7370 any surrounding namespace or type declarations. If the DIE has an
7371 AT_specification attribute, it also includes attributes and children
7372 attached to the specification, and returns a pointer to the original
7373 parent of the declaration DIE. Returns NULL otherwise. */
7375 static dw_die_ref
7376 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7378 dw_die_ref decl;
7379 dw_die_ref new_decl;
7380 dw_die_ref orig_parent = NULL;
7382 decl = get_AT_ref (die, DW_AT_specification);
7383 if (decl == NULL)
7384 decl = die;
7385 else
7387 unsigned ix;
7388 dw_die_ref c;
7389 dw_attr_ref a;
7391 /* The original DIE will be changed to a declaration, and must
7392 be moved to be a child of the original declaration DIE. */
7393 orig_parent = decl->die_parent;
7395 /* Copy the type node pointer from the new DIE to the original
7396 declaration DIE so we can forward references later. */
7397 decl->comdat_type_p = true;
7398 decl->die_id.die_type_node = die->die_id.die_type_node;
7400 remove_AT (die, DW_AT_specification);
7402 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7404 if (a->dw_attr != DW_AT_name
7405 && a->dw_attr != DW_AT_declaration
7406 && a->dw_attr != DW_AT_external)
7407 add_dwarf_attr (die, a);
7410 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7413 if (decl->die_parent != NULL
7414 && !is_unit_die (decl->die_parent))
7416 new_decl = copy_ancestor_tree (unit, decl, NULL);
7417 if (new_decl != NULL)
7419 remove_AT (new_decl, DW_AT_signature);
7420 add_AT_specification (die, new_decl);
7424 return orig_parent;
7427 /* Generate the skeleton ancestor tree for the given NODE, then clone
7428 the DIE and add the clone into the tree. */
7430 static void
7431 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7433 if (node->new_die != NULL)
7434 return;
7436 node->new_die = clone_as_declaration (node->old_die);
7438 if (node->parent != NULL)
7440 generate_skeleton_ancestor_tree (node->parent);
7441 add_child_die (node->parent->new_die, node->new_die);
7445 /* Generate a skeleton tree of DIEs containing any declarations that are
7446 found in the original tree. We traverse the tree looking for declaration
7447 DIEs, and construct the skeleton from the bottom up whenever we find one. */
7449 static void
7450 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7452 skeleton_chain_node node;
7453 dw_die_ref c;
7454 dw_die_ref first;
7455 dw_die_ref prev = NULL;
7456 dw_die_ref next = NULL;
7458 node.parent = parent;
7460 first = c = parent->old_die->die_child;
7461 if (c)
7462 next = c->die_sib;
7463 if (c) do {
7464 if (prev == NULL || prev->die_sib == c)
7465 prev = c;
7466 c = next;
7467 next = (c == first ? NULL : c->die_sib);
7468 node.old_die = c;
7469 node.new_die = NULL;
7470 if (is_declaration_die (c))
7472 if (is_template_instantiation (c))
7474 /* Instantiated templates do not need to be cloned into the
7475 type unit. Just move the DIE and its children back to
7476 the skeleton tree (in the main CU). */
7477 remove_child_with_prev (c, prev);
7478 add_child_die (parent->new_die, c);
7479 c = prev;
7481 else
7483 /* Clone the existing DIE, move the original to the skeleton
7484 tree (which is in the main CU), and put the clone, with
7485 all the original's children, where the original came from
7486 (which is about to be moved to the type unit). */
7487 dw_die_ref clone = clone_die (c);
7488 move_all_children (c, clone);
7490 /* If the original has a DW_AT_object_pointer attribute,
7491 it would now point to a child DIE just moved to the
7492 cloned tree, so we need to remove that attribute from
7493 the original. */
7494 remove_AT (c, DW_AT_object_pointer);
7496 replace_child (c, clone, prev);
7497 generate_skeleton_ancestor_tree (parent);
7498 add_child_die (parent->new_die, c);
7499 node.new_die = c;
7500 c = clone;
7503 generate_skeleton_bottom_up (&node);
7504 } while (next != NULL);
7507 /* Wrapper function for generate_skeleton_bottom_up. */
7509 static dw_die_ref
7510 generate_skeleton (dw_die_ref die)
7512 skeleton_chain_node node;
7514 node.old_die = die;
7515 node.new_die = NULL;
7516 node.parent = NULL;
7518 /* If this type definition is nested inside another type,
7519 and is not an instantiation of a template, always leave
7520 at least a declaration in its place. */
7521 if (die->die_parent != NULL
7522 && is_type_die (die->die_parent)
7523 && !is_template_instantiation (die))
7524 node.new_die = clone_as_declaration (die);
7526 generate_skeleton_bottom_up (&node);
7527 return node.new_die;
7530 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
7531 declaration. The original DIE is moved to a new compile unit so that
7532 existing references to it follow it to the new location. If any of the
7533 original DIE's descendants is a declaration, we need to replace the
7534 original DIE with a skeleton tree and move the declarations back into the
7535 skeleton tree. */
7537 static dw_die_ref
7538 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
7539 dw_die_ref prev)
7541 dw_die_ref skeleton, orig_parent;
7543 /* Copy the declaration context to the type unit DIE. If the returned
7544 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
7545 that DIE. */
7546 orig_parent = copy_declaration_context (unit, child);
7548 skeleton = generate_skeleton (child);
7549 if (skeleton == NULL)
7550 remove_child_with_prev (child, prev);
7551 else
7553 skeleton->comdat_type_p = true;
7554 skeleton->die_id.die_type_node = child->die_id.die_type_node;
7556 /* If the original DIE was a specification, we need to put
7557 the skeleton under the parent DIE of the declaration.
7558 This leaves the original declaration in the tree, but
7559 it will be pruned later since there are no longer any
7560 references to it. */
7561 if (orig_parent != NULL)
7563 remove_child_with_prev (child, prev);
7564 add_child_die (orig_parent, skeleton);
7566 else
7567 replace_child (child, skeleton, prev);
7570 return skeleton;
7573 /* Traverse the DIE and set up additional .debug_types sections for each
7574 type worthy of being placed in a COMDAT section. */
7576 static void
7577 break_out_comdat_types (dw_die_ref die)
7579 dw_die_ref c;
7580 dw_die_ref first;
7581 dw_die_ref prev = NULL;
7582 dw_die_ref next = NULL;
7583 dw_die_ref unit = NULL;
7585 first = c = die->die_child;
7586 if (c)
7587 next = c->die_sib;
7588 if (c) do {
7589 if (prev == NULL || prev->die_sib == c)
7590 prev = c;
7591 c = next;
7592 next = (c == first ? NULL : c->die_sib);
7593 if (should_move_die_to_comdat (c))
7595 dw_die_ref replacement;
7596 comdat_type_node_ref type_node;
7598 /* Break out nested types into their own type units. */
7599 break_out_comdat_types (c);
7601 /* Create a new type unit DIE as the root for the new tree, and
7602 add it to the list of comdat types. */
7603 unit = new_die (DW_TAG_type_unit, NULL, NULL);
7604 add_AT_unsigned (unit, DW_AT_language,
7605 get_AT_unsigned (comp_unit_die (), DW_AT_language));
7606 type_node = ggc_cleared_alloc<comdat_type_node> ();
7607 type_node->root_die = unit;
7608 type_node->next = comdat_type_list;
7609 comdat_type_list = type_node;
7611 /* Generate the type signature. */
7612 generate_type_signature (c, type_node);
7614 /* Copy the declaration context, attributes, and children of the
7615 declaration into the new type unit DIE, then remove this DIE
7616 from the main CU (or replace it with a skeleton if necessary). */
7617 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
7618 type_node->skeleton_die = replacement;
7620 /* Add the DIE to the new compunit. */
7621 add_child_die (unit, c);
7623 if (replacement != NULL)
7624 c = replacement;
7626 else if (c->die_tag == DW_TAG_namespace
7627 || c->die_tag == DW_TAG_class_type
7628 || c->die_tag == DW_TAG_structure_type
7629 || c->die_tag == DW_TAG_union_type)
7631 /* Look for nested types that can be broken out. */
7632 break_out_comdat_types (c);
7634 } while (next != NULL);
7637 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
7638 Enter all the cloned children into the hash table decl_table. */
7640 static dw_die_ref
7641 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
7643 dw_die_ref c;
7644 dw_die_ref clone;
7645 struct decl_table_entry *entry;
7646 decl_table_entry **slot;
7648 if (die->die_tag == DW_TAG_subprogram)
7649 clone = clone_as_declaration (die);
7650 else
7651 clone = clone_die (die);
7653 slot = decl_table->find_slot_with_hash (die,
7654 htab_hash_pointer (die), INSERT);
7656 /* Assert that DIE isn't in the hash table yet. If it would be there
7657 before, the ancestors would be necessarily there as well, therefore
7658 clone_tree_partial wouldn't be called. */
7659 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
7661 entry = XCNEW (struct decl_table_entry);
7662 entry->orig = die;
7663 entry->copy = clone;
7664 *slot = entry;
7666 if (die->die_tag != DW_TAG_subprogram)
7667 FOR_EACH_CHILD (die, c,
7668 add_child_die (clone, clone_tree_partial (c, decl_table)));
7670 return clone;
7673 /* Walk the DIE and its children, looking for references to incomplete
7674 or trivial types that are unmarked (i.e., that are not in the current
7675 type_unit). */
7677 static void
7678 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
7680 dw_die_ref c;
7681 dw_attr_ref a;
7682 unsigned ix;
7684 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7686 if (AT_class (a) == dw_val_class_die_ref)
7688 dw_die_ref targ = AT_ref (a);
7689 decl_table_entry **slot;
7690 struct decl_table_entry *entry;
7692 if (targ->die_mark != 0 || targ->comdat_type_p)
7693 continue;
7695 slot = decl_table->find_slot_with_hash (targ,
7696 htab_hash_pointer (targ),
7697 INSERT);
7699 if (*slot != HTAB_EMPTY_ENTRY)
7701 /* TARG has already been copied, so we just need to
7702 modify the reference to point to the copy. */
7703 entry = *slot;
7704 a->dw_attr_val.v.val_die_ref.die = entry->copy;
7706 else
7708 dw_die_ref parent = unit;
7709 dw_die_ref copy = clone_die (targ);
7711 /* Record in DECL_TABLE that TARG has been copied.
7712 Need to do this now, before the recursive call,
7713 because DECL_TABLE may be expanded and SLOT
7714 would no longer be a valid pointer. */
7715 entry = XCNEW (struct decl_table_entry);
7716 entry->orig = targ;
7717 entry->copy = copy;
7718 *slot = entry;
7720 /* If TARG is not a declaration DIE, we need to copy its
7721 children. */
7722 if (!is_declaration_die (targ))
7724 FOR_EACH_CHILD (
7725 targ, c,
7726 add_child_die (copy,
7727 clone_tree_partial (c, decl_table)));
7730 /* Make sure the cloned tree is marked as part of the
7731 type unit. */
7732 mark_dies (copy);
7734 /* If TARG has surrounding context, copy its ancestor tree
7735 into the new type unit. */
7736 if (targ->die_parent != NULL
7737 && !is_unit_die (targ->die_parent))
7738 parent = copy_ancestor_tree (unit, targ->die_parent,
7739 decl_table);
7741 add_child_die (parent, copy);
7742 a->dw_attr_val.v.val_die_ref.die = copy;
7744 /* Make sure the newly-copied DIE is walked. If it was
7745 installed in a previously-added context, it won't
7746 get visited otherwise. */
7747 if (parent != unit)
7749 /* Find the highest point of the newly-added tree,
7750 mark each node along the way, and walk from there. */
7751 parent->die_mark = 1;
7752 while (parent->die_parent
7753 && parent->die_parent->die_mark == 0)
7755 parent = parent->die_parent;
7756 parent->die_mark = 1;
7758 copy_decls_walk (unit, parent, decl_table);
7764 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
7767 /* Copy declarations for "unworthy" types into the new comdat section.
7768 Incomplete types, modified types, and certain other types aren't broken
7769 out into comdat sections of their own, so they don't have a signature,
7770 and we need to copy the declaration into the same section so that we
7771 don't have an external reference. */
7773 static void
7774 copy_decls_for_unworthy_types (dw_die_ref unit)
7776 mark_dies (unit);
7777 decl_hash_type decl_table (10);
7778 copy_decls_walk (unit, unit, &decl_table);
7779 unmark_dies (unit);
7782 /* Traverse the DIE and add a sibling attribute if it may have the
7783 effect of speeding up access to siblings. To save some space,
7784 avoid generating sibling attributes for DIE's without children. */
7786 static void
7787 add_sibling_attributes (dw_die_ref die)
7789 dw_die_ref c;
7791 if (! die->die_child)
7792 return;
7794 if (die->die_parent && die != die->die_parent->die_child)
7795 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7797 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7800 /* Output all location lists for the DIE and its children. */
7802 static void
7803 output_location_lists (dw_die_ref die)
7805 dw_die_ref c;
7806 dw_attr_ref a;
7807 unsigned ix;
7809 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7810 if (AT_class (a) == dw_val_class_loc_list)
7811 output_loc_list (AT_loc_list (a));
7813 FOR_EACH_CHILD (die, c, output_location_lists (c));
7816 /* We want to limit the number of external references, because they are
7817 larger than local references: a relocation takes multiple words, and
7818 even a sig8 reference is always eight bytes, whereas a local reference
7819 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
7820 So if we encounter multiple external references to the same type DIE, we
7821 make a local typedef stub for it and redirect all references there.
7823 This is the element of the hash table for keeping track of these
7824 references. */
7826 struct external_ref
7828 dw_die_ref type;
7829 dw_die_ref stub;
7830 unsigned n_refs;
7833 /* Hashtable helpers. */
7835 struct external_ref_hasher : free_ptr_hash <external_ref>
7837 static inline hashval_t hash (const external_ref *);
7838 static inline bool equal (const external_ref *, const external_ref *);
7841 inline hashval_t
7842 external_ref_hasher::hash (const external_ref *r)
7844 dw_die_ref die = r->type;
7845 hashval_t h = 0;
7847 /* We can't use the address of the DIE for hashing, because
7848 that will make the order of the stub DIEs non-deterministic. */
7849 if (! die->comdat_type_p)
7850 /* We have a symbol; use it to compute a hash. */
7851 h = htab_hash_string (die->die_id.die_symbol);
7852 else
7854 /* We have a type signature; use a subset of the bits as the hash.
7855 The 8-byte signature is at least as large as hashval_t. */
7856 comdat_type_node_ref type_node = die->die_id.die_type_node;
7857 memcpy (&h, type_node->signature, sizeof (h));
7859 return h;
7862 inline bool
7863 external_ref_hasher::equal (const external_ref *r1, const external_ref *r2)
7865 return r1->type == r2->type;
7868 typedef hash_table<external_ref_hasher> external_ref_hash_type;
7870 /* Return a pointer to the external_ref for references to DIE. */
7872 static struct external_ref *
7873 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
7875 struct external_ref ref, *ref_p;
7876 external_ref **slot;
7878 ref.type = die;
7879 slot = map->find_slot (&ref, INSERT);
7880 if (*slot != HTAB_EMPTY_ENTRY)
7881 return *slot;
7883 ref_p = XCNEW (struct external_ref);
7884 ref_p->type = die;
7885 *slot = ref_p;
7886 return ref_p;
7889 /* Subroutine of optimize_external_refs, below.
7891 If we see a type skeleton, record it as our stub. If we see external
7892 references, remember how many we've seen. */
7894 static void
7895 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
7897 dw_die_ref c;
7898 dw_attr_ref a;
7899 unsigned ix;
7900 struct external_ref *ref_p;
7902 if (is_type_die (die)
7903 && (c = get_AT_ref (die, DW_AT_signature)))
7905 /* This is a local skeleton; use it for local references. */
7906 ref_p = lookup_external_ref (map, c);
7907 ref_p->stub = die;
7910 /* Scan the DIE references, and remember any that refer to DIEs from
7911 other CUs (i.e. those which are not marked). */
7912 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7913 if (AT_class (a) == dw_val_class_die_ref
7914 && (c = AT_ref (a))->die_mark == 0
7915 && is_type_die (c))
7917 ref_p = lookup_external_ref (map, c);
7918 ref_p->n_refs++;
7921 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
7924 /* htab_traverse callback function for optimize_external_refs, below. SLOT
7925 points to an external_ref, DATA is the CU we're processing. If we don't
7926 already have a local stub, and we have multiple refs, build a stub. */
7929 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
7931 struct external_ref *ref_p = *slot;
7933 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
7935 /* We have multiple references to this type, so build a small stub.
7936 Both of these forms are a bit dodgy from the perspective of the
7937 DWARF standard, since technically they should have names. */
7938 dw_die_ref cu = data;
7939 dw_die_ref type = ref_p->type;
7940 dw_die_ref stub = NULL;
7942 if (type->comdat_type_p)
7944 /* If we refer to this type via sig8, use AT_signature. */
7945 stub = new_die (type->die_tag, cu, NULL_TREE);
7946 add_AT_die_ref (stub, DW_AT_signature, type);
7948 else
7950 /* Otherwise, use a typedef with no name. */
7951 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
7952 add_AT_die_ref (stub, DW_AT_type, type);
7955 stub->die_mark++;
7956 ref_p->stub = stub;
7958 return 1;
7961 /* DIE is a unit; look through all the DIE references to see if there are
7962 any external references to types, and if so, create local stubs for
7963 them which will be applied in build_abbrev_table. This is useful because
7964 references to local DIEs are smaller. */
7966 static external_ref_hash_type *
7967 optimize_external_refs (dw_die_ref die)
7969 external_ref_hash_type *map = new external_ref_hash_type (10);
7970 optimize_external_refs_1 (die, map);
7971 map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
7972 return map;
7975 /* The format of each DIE (and its attribute value pairs) is encoded in an
7976 abbreviation table. This routine builds the abbreviation table and assigns
7977 a unique abbreviation id for each abbreviation entry. The children of each
7978 die are visited recursively. */
7980 static void
7981 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
7983 unsigned long abbrev_id;
7984 unsigned int n_alloc;
7985 dw_die_ref c;
7986 dw_attr_ref a;
7987 unsigned ix;
7989 /* Scan the DIE references, and replace any that refer to
7990 DIEs from other CUs (i.e. those which are not marked) with
7991 the local stubs we built in optimize_external_refs. */
7992 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7993 if (AT_class (a) == dw_val_class_die_ref
7994 && (c = AT_ref (a))->die_mark == 0)
7996 struct external_ref *ref_p;
7997 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
7999 ref_p = lookup_external_ref (extern_map, c);
8000 if (ref_p->stub && ref_p->stub != die)
8001 change_AT_die_ref (a, ref_p->stub);
8002 else
8003 /* We aren't changing this reference, so mark it external. */
8004 set_AT_ref_external (a, 1);
8007 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8009 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
8010 dw_attr_ref die_a, abbrev_a;
8011 unsigned ix;
8012 bool ok = true;
8014 if (abbrev->die_tag != die->die_tag)
8015 continue;
8016 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
8017 continue;
8019 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
8020 continue;
8022 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
8024 abbrev_a = &(*abbrev->die_attr)[ix];
8025 if ((abbrev_a->dw_attr != die_a->dw_attr)
8026 || (value_format (abbrev_a) != value_format (die_a)))
8028 ok = false;
8029 break;
8032 if (ok)
8033 break;
8036 if (abbrev_id >= abbrev_die_table_in_use)
8038 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
8040 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
8041 abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
8042 n_alloc);
8044 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
8045 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
8046 abbrev_die_table_allocated = n_alloc;
8049 ++abbrev_die_table_in_use;
8050 abbrev_die_table[abbrev_id] = die;
8053 die->die_abbrev = abbrev_id;
8054 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
8057 /* Return the power-of-two number of bytes necessary to represent VALUE. */
8059 static int
8060 constant_size (unsigned HOST_WIDE_INT value)
8062 int log;
8064 if (value == 0)
8065 log = 0;
8066 else
8067 log = floor_log2 (value);
8069 log = log / 8;
8070 log = 1 << (floor_log2 (log) + 1);
8072 return log;
8075 /* Return the size of a DIE as it is represented in the
8076 .debug_info section. */
8078 static unsigned long
8079 size_of_die (dw_die_ref die)
8081 unsigned long size = 0;
8082 dw_attr_ref a;
8083 unsigned ix;
8084 enum dwarf_form form;
8086 size += size_of_uleb128 (die->die_abbrev);
8087 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8089 switch (AT_class (a))
8091 case dw_val_class_addr:
8092 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8094 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8095 size += size_of_uleb128 (AT_index (a));
8097 else
8098 size += DWARF2_ADDR_SIZE;
8099 break;
8100 case dw_val_class_offset:
8101 size += DWARF_OFFSET_SIZE;
8102 break;
8103 case dw_val_class_loc:
8105 unsigned long lsize = size_of_locs (AT_loc (a));
8107 /* Block length. */
8108 if (dwarf_version >= 4)
8109 size += size_of_uleb128 (lsize);
8110 else
8111 size += constant_size (lsize);
8112 size += lsize;
8114 break;
8115 case dw_val_class_loc_list:
8116 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8118 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8119 size += size_of_uleb128 (AT_index (a));
8121 else
8122 size += DWARF_OFFSET_SIZE;
8123 break;
8124 case dw_val_class_range_list:
8125 size += DWARF_OFFSET_SIZE;
8126 break;
8127 case dw_val_class_const:
8128 size += size_of_sleb128 (AT_int (a));
8129 break;
8130 case dw_val_class_unsigned_const:
8132 int csize = constant_size (AT_unsigned (a));
8133 if (dwarf_version == 3
8134 && a->dw_attr == DW_AT_data_member_location
8135 && csize >= 4)
8136 size += size_of_uleb128 (AT_unsigned (a));
8137 else
8138 size += csize;
8140 break;
8141 case dw_val_class_const_double:
8142 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
8143 if (HOST_BITS_PER_WIDE_INT >= 64)
8144 size++; /* block */
8145 break;
8146 case dw_val_class_wide_int:
8147 size += (get_full_len (*a->dw_attr_val.v.val_wide)
8148 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
8149 if (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT
8150 > 64)
8151 size++; /* block */
8152 break;
8153 case dw_val_class_vec:
8154 size += constant_size (a->dw_attr_val.v.val_vec.length
8155 * a->dw_attr_val.v.val_vec.elt_size)
8156 + a->dw_attr_val.v.val_vec.length
8157 * a->dw_attr_val.v.val_vec.elt_size; /* block */
8158 break;
8159 case dw_val_class_flag:
8160 if (dwarf_version >= 4)
8161 /* Currently all add_AT_flag calls pass in 1 as last argument,
8162 so DW_FORM_flag_present can be used. If that ever changes,
8163 we'll need to use DW_FORM_flag and have some optimization
8164 in build_abbrev_table that will change those to
8165 DW_FORM_flag_present if it is set to 1 in all DIEs using
8166 the same abbrev entry. */
8167 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8168 else
8169 size += 1;
8170 break;
8171 case dw_val_class_die_ref:
8172 if (AT_ref_external (a))
8174 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
8175 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
8176 is sized by target address length, whereas in DWARF3
8177 it's always sized as an offset. */
8178 if (use_debug_types)
8179 size += DWARF_TYPE_SIGNATURE_SIZE;
8180 else if (dwarf_version == 2)
8181 size += DWARF2_ADDR_SIZE;
8182 else
8183 size += DWARF_OFFSET_SIZE;
8185 else
8186 size += DWARF_OFFSET_SIZE;
8187 break;
8188 case dw_val_class_fde_ref:
8189 size += DWARF_OFFSET_SIZE;
8190 break;
8191 case dw_val_class_lbl_id:
8192 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8194 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8195 size += size_of_uleb128 (AT_index (a));
8197 else
8198 size += DWARF2_ADDR_SIZE;
8199 break;
8200 case dw_val_class_lineptr:
8201 case dw_val_class_macptr:
8202 size += DWARF_OFFSET_SIZE;
8203 break;
8204 case dw_val_class_str:
8205 form = AT_string_form (a);
8206 if (form == DW_FORM_strp)
8207 size += DWARF_OFFSET_SIZE;
8208 else if (form == DW_FORM_GNU_str_index)
8209 size += size_of_uleb128 (AT_index (a));
8210 else
8211 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
8212 break;
8213 case dw_val_class_file:
8214 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
8215 break;
8216 case dw_val_class_data8:
8217 size += 8;
8218 break;
8219 case dw_val_class_vms_delta:
8220 size += DWARF_OFFSET_SIZE;
8221 break;
8222 case dw_val_class_high_pc:
8223 size += DWARF2_ADDR_SIZE;
8224 break;
8225 default:
8226 gcc_unreachable ();
8230 return size;
8233 /* Size the debugging information associated with a given DIE. Visits the
8234 DIE's children recursively. Updates the global variable next_die_offset, on
8235 each time through. Uses the current value of next_die_offset to update the
8236 die_offset field in each DIE. */
8238 static void
8239 calc_die_sizes (dw_die_ref die)
8241 dw_die_ref c;
8243 gcc_assert (die->die_offset == 0
8244 || (unsigned long int) die->die_offset == next_die_offset);
8245 die->die_offset = next_die_offset;
8246 next_die_offset += size_of_die (die);
8248 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
8250 if (die->die_child != NULL)
8251 /* Count the null byte used to terminate sibling lists. */
8252 next_die_offset += 1;
8255 /* Size just the base type children at the start of the CU.
8256 This is needed because build_abbrev needs to size locs
8257 and sizing of type based stack ops needs to know die_offset
8258 values for the base types. */
8260 static void
8261 calc_base_type_die_sizes (void)
8263 unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8264 unsigned int i;
8265 dw_die_ref base_type;
8266 #if ENABLE_ASSERT_CHECKING
8267 dw_die_ref prev = comp_unit_die ()->die_child;
8268 #endif
8270 die_offset += size_of_die (comp_unit_die ());
8271 for (i = 0; base_types.iterate (i, &base_type); i++)
8273 #if ENABLE_ASSERT_CHECKING
8274 gcc_assert (base_type->die_offset == 0
8275 && prev->die_sib == base_type
8276 && base_type->die_child == NULL
8277 && base_type->die_abbrev);
8278 prev = base_type;
8279 #endif
8280 base_type->die_offset = die_offset;
8281 die_offset += size_of_die (base_type);
8285 /* Set the marks for a die and its children. We do this so
8286 that we know whether or not a reference needs to use FORM_ref_addr; only
8287 DIEs in the same CU will be marked. We used to clear out the offset
8288 and use that as the flag, but ran into ordering problems. */
8290 static void
8291 mark_dies (dw_die_ref die)
8293 dw_die_ref c;
8295 gcc_assert (!die->die_mark);
8297 die->die_mark = 1;
8298 FOR_EACH_CHILD (die, c, mark_dies (c));
8301 /* Clear the marks for a die and its children. */
8303 static void
8304 unmark_dies (dw_die_ref die)
8306 dw_die_ref c;
8308 if (! use_debug_types)
8309 gcc_assert (die->die_mark);
8311 die->die_mark = 0;
8312 FOR_EACH_CHILD (die, c, unmark_dies (c));
8315 /* Clear the marks for a die, its children and referred dies. */
8317 static void
8318 unmark_all_dies (dw_die_ref die)
8320 dw_die_ref c;
8321 dw_attr_ref a;
8322 unsigned ix;
8324 if (!die->die_mark)
8325 return;
8326 die->die_mark = 0;
8328 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
8330 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8331 if (AT_class (a) == dw_val_class_die_ref)
8332 unmark_all_dies (AT_ref (a));
8335 /* Calculate if the entry should appear in the final output file. It may be
8336 from a pruned a type. */
8338 static bool
8339 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
8341 /* By limiting gnu pubnames to definitions only, gold can generate a
8342 gdb index without entries for declarations, which don't include
8343 enough information to be useful. */
8344 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
8345 return false;
8347 if (table == pubname_table)
8349 /* Enumerator names are part of the pubname table, but the
8350 parent DW_TAG_enumeration_type die may have been pruned.
8351 Don't output them if that is the case. */
8352 if (p->die->die_tag == DW_TAG_enumerator &&
8353 (p->die->die_parent == NULL
8354 || !p->die->die_parent->die_perennial_p))
8355 return false;
8357 /* Everything else in the pubname table is included. */
8358 return true;
8361 /* The pubtypes table shouldn't include types that have been
8362 pruned. */
8363 return (p->die->die_offset != 0
8364 || !flag_eliminate_unused_debug_types);
8367 /* Return the size of the .debug_pubnames or .debug_pubtypes table
8368 generated for the compilation unit. */
8370 static unsigned long
8371 size_of_pubnames (vec<pubname_entry, va_gc> *names)
8373 unsigned long size;
8374 unsigned i;
8375 pubname_ref p;
8376 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
8378 size = DWARF_PUBNAMES_HEADER_SIZE;
8379 FOR_EACH_VEC_ELT (*names, i, p)
8380 if (include_pubname_in_output (names, p))
8381 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
8383 size += DWARF_OFFSET_SIZE;
8384 return size;
8387 /* Return the size of the information in the .debug_aranges section. */
8389 static unsigned long
8390 size_of_aranges (void)
8392 unsigned long size;
8394 size = DWARF_ARANGES_HEADER_SIZE;
8396 /* Count the address/length pair for this compilation unit. */
8397 if (text_section_used)
8398 size += 2 * DWARF2_ADDR_SIZE;
8399 if (cold_text_section_used)
8400 size += 2 * DWARF2_ADDR_SIZE;
8401 if (have_multiple_function_sections)
8403 unsigned fde_idx;
8404 dw_fde_ref fde;
8406 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
8408 if (DECL_IGNORED_P (fde->decl))
8409 continue;
8410 if (!fde->in_std_section)
8411 size += 2 * DWARF2_ADDR_SIZE;
8412 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
8413 size += 2 * DWARF2_ADDR_SIZE;
8417 /* Count the two zero words used to terminated the address range table. */
8418 size += 2 * DWARF2_ADDR_SIZE;
8419 return size;
8422 /* Select the encoding of an attribute value. */
8424 static enum dwarf_form
8425 value_format (dw_attr_ref a)
8427 switch (AT_class (a))
8429 case dw_val_class_addr:
8430 /* Only very few attributes allow DW_FORM_addr. */
8431 switch (a->dw_attr)
8433 case DW_AT_low_pc:
8434 case DW_AT_high_pc:
8435 case DW_AT_entry_pc:
8436 case DW_AT_trampoline:
8437 return (AT_index (a) == NOT_INDEXED
8438 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8439 default:
8440 break;
8442 switch (DWARF2_ADDR_SIZE)
8444 case 1:
8445 return DW_FORM_data1;
8446 case 2:
8447 return DW_FORM_data2;
8448 case 4:
8449 return DW_FORM_data4;
8450 case 8:
8451 return DW_FORM_data8;
8452 default:
8453 gcc_unreachable ();
8455 case dw_val_class_range_list:
8456 case dw_val_class_loc_list:
8457 if (dwarf_version >= 4)
8458 return DW_FORM_sec_offset;
8459 /* FALLTHRU */
8460 case dw_val_class_vms_delta:
8461 case dw_val_class_offset:
8462 switch (DWARF_OFFSET_SIZE)
8464 case 4:
8465 return DW_FORM_data4;
8466 case 8:
8467 return DW_FORM_data8;
8468 default:
8469 gcc_unreachable ();
8471 case dw_val_class_loc:
8472 if (dwarf_version >= 4)
8473 return DW_FORM_exprloc;
8474 switch (constant_size (size_of_locs (AT_loc (a))))
8476 case 1:
8477 return DW_FORM_block1;
8478 case 2:
8479 return DW_FORM_block2;
8480 case 4:
8481 return DW_FORM_block4;
8482 default:
8483 gcc_unreachable ();
8485 case dw_val_class_const:
8486 return DW_FORM_sdata;
8487 case dw_val_class_unsigned_const:
8488 switch (constant_size (AT_unsigned (a)))
8490 case 1:
8491 return DW_FORM_data1;
8492 case 2:
8493 return DW_FORM_data2;
8494 case 4:
8495 /* In DWARF3 DW_AT_data_member_location with
8496 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
8497 constant, so we need to use DW_FORM_udata if we need
8498 a large constant. */
8499 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8500 return DW_FORM_udata;
8501 return DW_FORM_data4;
8502 case 8:
8503 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8504 return DW_FORM_udata;
8505 return DW_FORM_data8;
8506 default:
8507 gcc_unreachable ();
8509 case dw_val_class_const_double:
8510 switch (HOST_BITS_PER_WIDE_INT)
8512 case 8:
8513 return DW_FORM_data2;
8514 case 16:
8515 return DW_FORM_data4;
8516 case 32:
8517 return DW_FORM_data8;
8518 case 64:
8519 default:
8520 return DW_FORM_block1;
8522 case dw_val_class_wide_int:
8523 switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
8525 case 8:
8526 return DW_FORM_data1;
8527 case 16:
8528 return DW_FORM_data2;
8529 case 32:
8530 return DW_FORM_data4;
8531 case 64:
8532 return DW_FORM_data8;
8533 default:
8534 return DW_FORM_block1;
8536 case dw_val_class_vec:
8537 switch (constant_size (a->dw_attr_val.v.val_vec.length
8538 * a->dw_attr_val.v.val_vec.elt_size))
8540 case 1:
8541 return DW_FORM_block1;
8542 case 2:
8543 return DW_FORM_block2;
8544 case 4:
8545 return DW_FORM_block4;
8546 default:
8547 gcc_unreachable ();
8549 case dw_val_class_flag:
8550 if (dwarf_version >= 4)
8552 /* Currently all add_AT_flag calls pass in 1 as last argument,
8553 so DW_FORM_flag_present can be used. If that ever changes,
8554 we'll need to use DW_FORM_flag and have some optimization
8555 in build_abbrev_table that will change those to
8556 DW_FORM_flag_present if it is set to 1 in all DIEs using
8557 the same abbrev entry. */
8558 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8559 return DW_FORM_flag_present;
8561 return DW_FORM_flag;
8562 case dw_val_class_die_ref:
8563 if (AT_ref_external (a))
8564 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
8565 else
8566 return DW_FORM_ref;
8567 case dw_val_class_fde_ref:
8568 return DW_FORM_data;
8569 case dw_val_class_lbl_id:
8570 return (AT_index (a) == NOT_INDEXED
8571 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8572 case dw_val_class_lineptr:
8573 case dw_val_class_macptr:
8574 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
8575 case dw_val_class_str:
8576 return AT_string_form (a);
8577 case dw_val_class_file:
8578 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8580 case 1:
8581 return DW_FORM_data1;
8582 case 2:
8583 return DW_FORM_data2;
8584 case 4:
8585 return DW_FORM_data4;
8586 default:
8587 gcc_unreachable ();
8590 case dw_val_class_data8:
8591 return DW_FORM_data8;
8593 case dw_val_class_high_pc:
8594 switch (DWARF2_ADDR_SIZE)
8596 case 1:
8597 return DW_FORM_data1;
8598 case 2:
8599 return DW_FORM_data2;
8600 case 4:
8601 return DW_FORM_data4;
8602 case 8:
8603 return DW_FORM_data8;
8604 default:
8605 gcc_unreachable ();
8608 default:
8609 gcc_unreachable ();
8613 /* Output the encoding of an attribute value. */
8615 static void
8616 output_value_format (dw_attr_ref a)
8618 enum dwarf_form form = value_format (a);
8620 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8623 /* Given a die and id, produce the appropriate abbreviations. */
8625 static void
8626 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
8628 unsigned ix;
8629 dw_attr_ref a_attr;
8631 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8632 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8633 dwarf_tag_name (abbrev->die_tag));
8635 if (abbrev->die_child != NULL)
8636 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8637 else
8638 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8640 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
8642 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8643 dwarf_attr_name (a_attr->dw_attr));
8644 output_value_format (a_attr);
8647 dw2_asm_output_data (1, 0, NULL);
8648 dw2_asm_output_data (1, 0, NULL);
8652 /* Output the .debug_abbrev section which defines the DIE abbreviation
8653 table. */
8655 static void
8656 output_abbrev_section (void)
8658 unsigned long abbrev_id;
8660 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8661 output_die_abbrevs (abbrev_id, abbrev_die_table[abbrev_id]);
8663 /* Terminate the table. */
8664 dw2_asm_output_data (1, 0, NULL);
8667 /* Output a symbol we can use to refer to this DIE from another CU. */
8669 static inline void
8670 output_die_symbol (dw_die_ref die)
8672 const char *sym = die->die_id.die_symbol;
8674 gcc_assert (!die->comdat_type_p);
8676 if (sym == 0)
8677 return;
8679 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8680 /* We make these global, not weak; if the target doesn't support
8681 .linkonce, it doesn't support combining the sections, so debugging
8682 will break. */
8683 targetm.asm_out.globalize_label (asm_out_file, sym);
8685 ASM_OUTPUT_LABEL (asm_out_file, sym);
8688 /* Return a new location list, given the begin and end range, and the
8689 expression. */
8691 static inline dw_loc_list_ref
8692 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8693 const char *section)
8695 dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
8697 retlist->begin = begin;
8698 retlist->begin_entry = NULL;
8699 retlist->end = end;
8700 retlist->expr = expr;
8701 retlist->section = section;
8703 return retlist;
8706 /* Generate a new internal symbol for this location list node, if it
8707 hasn't got one yet. */
8709 static inline void
8710 gen_llsym (dw_loc_list_ref list)
8712 gcc_assert (!list->ll_symbol);
8713 list->ll_symbol = gen_internal_sym ("LLST");
8716 /* Output the location list given to us. */
8718 static void
8719 output_loc_list (dw_loc_list_ref list_head)
8721 dw_loc_list_ref curr = list_head;
8723 if (list_head->emitted)
8724 return;
8725 list_head->emitted = true;
8727 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8729 /* Walk the location list, and output each range + expression. */
8730 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8732 unsigned long size;
8733 /* Don't output an entry that starts and ends at the same address. */
8734 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
8735 continue;
8736 size = size_of_locs (curr->expr);
8737 /* If the expression is too large, drop it on the floor. We could
8738 perhaps put it into DW_TAG_dwarf_procedure and refer to that
8739 in the expression, but >= 64KB expressions for a single value
8740 in a single range are unlikely very useful. */
8741 if (size > 0xffff)
8742 continue;
8743 if (dwarf_split_debug_info)
8745 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
8746 "Location list start/length entry (%s)",
8747 list_head->ll_symbol);
8748 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
8749 "Location list range start index (%s)",
8750 curr->begin);
8751 /* The length field is 4 bytes. If we ever need to support
8752 an 8-byte length, we can add a new DW_LLE code or fall back
8753 to DW_LLE_GNU_start_end_entry. */
8754 dw2_asm_output_delta (4, curr->end, curr->begin,
8755 "Location list range length (%s)",
8756 list_head->ll_symbol);
8758 else if (!have_multiple_function_sections)
8760 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8761 "Location list begin address (%s)",
8762 list_head->ll_symbol);
8763 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8764 "Location list end address (%s)",
8765 list_head->ll_symbol);
8767 else
8769 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8770 "Location list begin address (%s)",
8771 list_head->ll_symbol);
8772 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8773 "Location list end address (%s)",
8774 list_head->ll_symbol);
8777 /* Output the block length for this list of location operations. */
8778 gcc_assert (size <= 0xffff);
8779 dw2_asm_output_data (2, size, "%s", "Location expression size");
8781 output_loc_sequence (curr->expr, -1);
8784 if (dwarf_split_debug_info)
8785 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
8786 "Location list terminator (%s)",
8787 list_head->ll_symbol);
8788 else
8790 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8791 "Location list terminator begin (%s)",
8792 list_head->ll_symbol);
8793 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8794 "Location list terminator end (%s)",
8795 list_head->ll_symbol);
8799 /* Output a range_list offset into the debug_range section. Emit a
8800 relocated reference if val_entry is NULL, otherwise, emit an
8801 indirect reference. */
8803 static void
8804 output_range_list_offset (dw_attr_ref a)
8806 const char *name = dwarf_attr_name (a->dw_attr);
8808 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
8810 char *p = strchr (ranges_section_label, '\0');
8811 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX, a->dw_attr_val.v.val_offset);
8812 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8813 debug_ranges_section, "%s", name);
8814 *p = '\0';
8816 else
8817 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8818 "%s (offset from %s)", name, ranges_section_label);
8821 /* Output the offset into the debug_loc section. */
8823 static void
8824 output_loc_list_offset (dw_attr_ref a)
8826 char *sym = AT_loc_list (a)->ll_symbol;
8828 gcc_assert (sym);
8829 if (dwarf_split_debug_info)
8830 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
8831 "%s", dwarf_attr_name (a->dw_attr));
8832 else
8833 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8834 "%s", dwarf_attr_name (a->dw_attr));
8837 /* Output an attribute's index or value appropriately. */
8839 static void
8840 output_attr_index_or_value (dw_attr_ref a)
8842 const char *name = dwarf_attr_name (a->dw_attr);
8844 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8846 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
8847 return;
8849 switch (AT_class (a))
8851 case dw_val_class_addr:
8852 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8853 break;
8854 case dw_val_class_high_pc:
8855 case dw_val_class_lbl_id:
8856 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8857 break;
8858 case dw_val_class_loc_list:
8859 output_loc_list_offset (a);
8860 break;
8861 default:
8862 gcc_unreachable ();
8866 /* Output a type signature. */
8868 static inline void
8869 output_signature (const char *sig, const char *name)
8871 int i;
8873 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8874 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
8877 /* Output the DIE and its attributes. Called recursively to generate
8878 the definitions of each child DIE. */
8880 static void
8881 output_die (dw_die_ref die)
8883 dw_attr_ref a;
8884 dw_die_ref c;
8885 unsigned long size;
8886 unsigned ix;
8888 /* If someone in another CU might refer to us, set up a symbol for
8889 them to point to. */
8890 if (! die->comdat_type_p && die->die_id.die_symbol)
8891 output_die_symbol (die);
8893 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
8894 (unsigned long)die->die_offset,
8895 dwarf_tag_name (die->die_tag));
8897 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8899 const char *name = dwarf_attr_name (a->dw_attr);
8901 switch (AT_class (a))
8903 case dw_val_class_addr:
8904 output_attr_index_or_value (a);
8905 break;
8907 case dw_val_class_offset:
8908 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8909 "%s", name);
8910 break;
8912 case dw_val_class_range_list:
8913 output_range_list_offset (a);
8914 break;
8916 case dw_val_class_loc:
8917 size = size_of_locs (AT_loc (a));
8919 /* Output the block length for this list of location operations. */
8920 if (dwarf_version >= 4)
8921 dw2_asm_output_data_uleb128 (size, "%s", name);
8922 else
8923 dw2_asm_output_data (constant_size (size), size, "%s", name);
8925 output_loc_sequence (AT_loc (a), -1);
8926 break;
8928 case dw_val_class_const:
8929 /* ??? It would be slightly more efficient to use a scheme like is
8930 used for unsigned constants below, but gdb 4.x does not sign
8931 extend. Gdb 5.x does sign extend. */
8932 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8933 break;
8935 case dw_val_class_unsigned_const:
8937 int csize = constant_size (AT_unsigned (a));
8938 if (dwarf_version == 3
8939 && a->dw_attr == DW_AT_data_member_location
8940 && csize >= 4)
8941 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
8942 else
8943 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
8945 break;
8947 case dw_val_class_const_double:
8949 unsigned HOST_WIDE_INT first, second;
8951 if (HOST_BITS_PER_WIDE_INT >= 64)
8952 dw2_asm_output_data (1,
8953 HOST_BITS_PER_DOUBLE_INT
8954 / HOST_BITS_PER_CHAR,
8955 NULL);
8957 if (WORDS_BIG_ENDIAN)
8959 first = a->dw_attr_val.v.val_double.high;
8960 second = a->dw_attr_val.v.val_double.low;
8962 else
8964 first = a->dw_attr_val.v.val_double.low;
8965 second = a->dw_attr_val.v.val_double.high;
8968 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8969 first, "%s", name);
8970 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8971 second, NULL);
8973 break;
8975 case dw_val_class_wide_int:
8977 int i;
8978 int len = get_full_len (*a->dw_attr_val.v.val_wide);
8979 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
8980 if (len * HOST_BITS_PER_WIDE_INT > 64)
8981 dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide) * l,
8982 NULL);
8984 if (WORDS_BIG_ENDIAN)
8985 for (i = len - 1; i >= 0; --i)
8987 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
8988 "%s", name);
8989 name = NULL;
8991 else
8992 for (i = 0; i < len; ++i)
8994 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
8995 "%s", name);
8996 name = NULL;
8999 break;
9001 case dw_val_class_vec:
9003 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
9004 unsigned int len = a->dw_attr_val.v.val_vec.length;
9005 unsigned int i;
9006 unsigned char *p;
9008 dw2_asm_output_data (constant_size (len * elt_size),
9009 len * elt_size, "%s", name);
9010 if (elt_size > sizeof (HOST_WIDE_INT))
9012 elt_size /= 2;
9013 len *= 2;
9015 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
9016 i < len;
9017 i++, p += elt_size)
9018 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
9019 "fp or vector constant word %u", i);
9020 break;
9023 case dw_val_class_flag:
9024 if (dwarf_version >= 4)
9026 /* Currently all add_AT_flag calls pass in 1 as last argument,
9027 so DW_FORM_flag_present can be used. If that ever changes,
9028 we'll need to use DW_FORM_flag and have some optimization
9029 in build_abbrev_table that will change those to
9030 DW_FORM_flag_present if it is set to 1 in all DIEs using
9031 the same abbrev entry. */
9032 gcc_assert (AT_flag (a) == 1);
9033 if (flag_debug_asm)
9034 fprintf (asm_out_file, "\t\t\t%s %s\n",
9035 ASM_COMMENT_START, name);
9036 break;
9038 dw2_asm_output_data (1, AT_flag (a), "%s", name);
9039 break;
9041 case dw_val_class_loc_list:
9042 output_attr_index_or_value (a);
9043 break;
9045 case dw_val_class_die_ref:
9046 if (AT_ref_external (a))
9048 if (AT_ref (a)->comdat_type_p)
9050 comdat_type_node_ref type_node =
9051 AT_ref (a)->die_id.die_type_node;
9053 gcc_assert (type_node);
9054 output_signature (type_node->signature, name);
9056 else
9058 const char *sym = AT_ref (a)->die_id.die_symbol;
9059 int size;
9061 gcc_assert (sym);
9062 /* In DWARF2, DW_FORM_ref_addr is sized by target address
9063 length, whereas in DWARF3 it's always sized as an
9064 offset. */
9065 if (dwarf_version == 2)
9066 size = DWARF2_ADDR_SIZE;
9067 else
9068 size = DWARF_OFFSET_SIZE;
9069 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
9070 name);
9073 else
9075 gcc_assert (AT_ref (a)->die_offset);
9076 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
9077 "%s", name);
9079 break;
9081 case dw_val_class_fde_ref:
9083 char l1[20];
9085 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
9086 a->dw_attr_val.v.val_fde_index * 2);
9087 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
9088 "%s", name);
9090 break;
9092 case dw_val_class_vms_delta:
9093 #ifdef ASM_OUTPUT_DWARF_VMS_DELTA
9094 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
9095 AT_vms_delta2 (a), AT_vms_delta1 (a),
9096 "%s", name);
9097 #else
9098 dw2_asm_output_delta (DWARF_OFFSET_SIZE,
9099 AT_vms_delta2 (a), AT_vms_delta1 (a),
9100 "%s", name);
9101 #endif
9102 break;
9104 case dw_val_class_lbl_id:
9105 output_attr_index_or_value (a);
9106 break;
9108 case dw_val_class_lineptr:
9109 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
9110 debug_line_section, "%s", name);
9111 break;
9113 case dw_val_class_macptr:
9114 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
9115 debug_macinfo_section, "%s", name);
9116 break;
9118 case dw_val_class_str:
9119 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
9120 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
9121 a->dw_attr_val.v.val_str->label,
9122 debug_str_section,
9123 "%s: \"%s\"", name, AT_string (a));
9124 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
9125 dw2_asm_output_data_uleb128 (AT_index (a),
9126 "%s: \"%s\"", name, AT_string (a));
9127 else
9128 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
9129 break;
9131 case dw_val_class_file:
9133 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
9135 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
9136 a->dw_attr_val.v.val_file->filename);
9137 break;
9140 case dw_val_class_data8:
9142 int i;
9144 for (i = 0; i < 8; i++)
9145 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
9146 i == 0 ? "%s" : NULL, name);
9147 break;
9150 case dw_val_class_high_pc:
9151 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
9152 get_AT_low_pc (die), "DW_AT_high_pc");
9153 break;
9155 default:
9156 gcc_unreachable ();
9160 FOR_EACH_CHILD (die, c, output_die (c));
9162 /* Add null byte to terminate sibling list. */
9163 if (die->die_child != NULL)
9164 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
9165 (unsigned long) die->die_offset);
9168 /* Output the compilation unit that appears at the beginning of the
9169 .debug_info section, and precedes the DIE descriptions. */
9171 static void
9172 output_compilation_unit_header (void)
9174 /* We don't support actual DWARFv5 units yet, we just use some
9175 DWARFv5 draft DIE tags in DWARFv4 format. */
9176 int ver = dwarf_version < 5 ? dwarf_version : 4;
9178 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9179 dw2_asm_output_data (4, 0xffffffff,
9180 "Initial length escape value indicating 64-bit DWARF extension");
9181 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9182 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
9183 "Length of Compilation Unit Info");
9184 dw2_asm_output_data (2, ver, "DWARF version number");
9185 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
9186 debug_abbrev_section,
9187 "Offset Into Abbrev. Section");
9188 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9191 /* Output the compilation unit DIE and its children. */
9193 static void
9194 output_comp_unit (dw_die_ref die, int output_if_empty)
9196 const char *secname, *oldsym;
9197 char *tmp;
9199 /* Unless we are outputting main CU, we may throw away empty ones. */
9200 if (!output_if_empty && die->die_child == NULL)
9201 return;
9203 /* Even if there are no children of this DIE, we must output the information
9204 about the compilation unit. Otherwise, on an empty translation unit, we
9205 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
9206 will then complain when examining the file. First mark all the DIEs in
9207 this CU so we know which get local refs. */
9208 mark_dies (die);
9210 external_ref_hash_type *extern_map = optimize_external_refs (die);
9212 build_abbrev_table (die, extern_map);
9214 delete extern_map;
9216 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
9217 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
9218 calc_die_sizes (die);
9220 oldsym = die->die_id.die_symbol;
9221 if (oldsym)
9223 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
9225 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
9226 secname = tmp;
9227 die->die_id.die_symbol = NULL;
9228 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9230 else
9232 switch_to_section (debug_info_section);
9233 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
9234 info_section_emitted = true;
9237 /* Output debugging information. */
9238 output_compilation_unit_header ();
9239 output_die (die);
9241 /* Leave the marks on the main CU, so we can check them in
9242 output_pubnames. */
9243 if (oldsym)
9245 unmark_dies (die);
9246 die->die_id.die_symbol = oldsym;
9250 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
9251 and .debug_pubtypes. This is configured per-target, but can be
9252 overridden by the -gpubnames or -gno-pubnames options. */
9254 static inline bool
9255 want_pubnames (void)
9257 if (debug_info_level <= DINFO_LEVEL_TERSE)
9258 return false;
9259 if (debug_generate_pub_sections != -1)
9260 return debug_generate_pub_sections;
9261 return targetm.want_debug_pub_sections;
9264 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
9266 static void
9267 add_AT_pubnames (dw_die_ref die)
9269 if (want_pubnames ())
9270 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
9273 /* Add a string attribute value to a skeleton DIE. */
9275 static inline void
9276 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
9277 const char *str)
9279 dw_attr_node attr;
9280 struct indirect_string_node *node;
9282 if (! skeleton_debug_str_hash)
9283 skeleton_debug_str_hash
9284 = hash_table<indirect_string_hasher>::create_ggc (10);
9286 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
9287 find_string_form (node);
9288 if (node->form == DW_FORM_GNU_str_index)
9289 node->form = DW_FORM_strp;
9291 attr.dw_attr = attr_kind;
9292 attr.dw_attr_val.val_class = dw_val_class_str;
9293 attr.dw_attr_val.val_entry = NULL;
9294 attr.dw_attr_val.v.val_str = node;
9295 add_dwarf_attr (die, &attr);
9298 /* Helper function to generate top-level dies for skeleton debug_info and
9299 debug_types. */
9301 static void
9302 add_top_level_skeleton_die_attrs (dw_die_ref die)
9304 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
9305 const char *comp_dir = comp_dir_string ();
9307 add_skeleton_AT_string (die, DW_AT_GNU_dwo_name, dwo_file_name);
9308 if (comp_dir != NULL)
9309 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
9310 add_AT_pubnames (die);
9311 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
9314 /* Output skeleton debug sections that point to the dwo file. */
9316 static void
9317 output_skeleton_debug_sections (dw_die_ref comp_unit)
9319 /* We don't support actual DWARFv5 units yet, we just use some
9320 DWARFv5 draft DIE tags in DWARFv4 format. */
9321 int ver = dwarf_version < 5 ? dwarf_version : 4;
9323 /* These attributes will be found in the full debug_info section. */
9324 remove_AT (comp_unit, DW_AT_producer);
9325 remove_AT (comp_unit, DW_AT_language);
9327 switch_to_section (debug_skeleton_info_section);
9328 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
9330 /* Produce the skeleton compilation-unit header. This one differs enough from
9331 a normal CU header that it's better not to call output_compilation_unit
9332 header. */
9333 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9334 dw2_asm_output_data (4, 0xffffffff,
9335 "Initial length escape value indicating 64-bit DWARF extension");
9337 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9338 DWARF_COMPILE_UNIT_HEADER_SIZE
9339 - DWARF_INITIAL_LENGTH_SIZE
9340 + size_of_die (comp_unit),
9341 "Length of Compilation Unit Info");
9342 dw2_asm_output_data (2, ver, "DWARF version number");
9343 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
9344 debug_abbrev_section,
9345 "Offset Into Abbrev. Section");
9346 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9348 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
9349 output_die (comp_unit);
9351 /* Build the skeleton debug_abbrev section. */
9352 switch_to_section (debug_skeleton_abbrev_section);
9353 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
9355 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
9357 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
9360 /* Output a comdat type unit DIE and its children. */
9362 static void
9363 output_comdat_type_unit (comdat_type_node *node)
9365 const char *secname;
9366 char *tmp;
9367 int i;
9368 #if defined (OBJECT_FORMAT_ELF)
9369 tree comdat_key;
9370 #endif
9372 /* First mark all the DIEs in this CU so we know which get local refs. */
9373 mark_dies (node->root_die);
9375 external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
9377 build_abbrev_table (node->root_die, extern_map);
9379 delete extern_map;
9380 extern_map = NULL;
9382 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
9383 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
9384 calc_die_sizes (node->root_die);
9386 #if defined (OBJECT_FORMAT_ELF)
9387 if (!dwarf_split_debug_info)
9388 secname = ".debug_types";
9389 else
9390 secname = ".debug_types.dwo";
9392 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9393 sprintf (tmp, "wt.");
9394 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9395 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
9396 comdat_key = get_identifier (tmp);
9397 targetm.asm_out.named_section (secname,
9398 SECTION_DEBUG | SECTION_LINKONCE,
9399 comdat_key);
9400 #else
9401 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9402 sprintf (tmp, ".gnu.linkonce.wt.");
9403 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9404 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
9405 secname = tmp;
9406 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9407 #endif
9409 /* Output debugging information. */
9410 output_compilation_unit_header ();
9411 output_signature (node->signature, "Type Signature");
9412 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
9413 "Offset to Type DIE");
9414 output_die (node->root_die);
9416 unmark_dies (node->root_die);
9419 /* Return the DWARF2/3 pubname associated with a decl. */
9421 static const char *
9422 dwarf2_name (tree decl, int scope)
9424 if (DECL_NAMELESS (decl))
9425 return NULL;
9426 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
9429 /* Add a new entry to .debug_pubnames if appropriate. */
9431 static void
9432 add_pubname_string (const char *str, dw_die_ref die)
9434 pubname_entry e;
9436 e.die = die;
9437 e.name = xstrdup (str);
9438 vec_safe_push (pubname_table, e);
9441 static void
9442 add_pubname (tree decl, dw_die_ref die)
9444 if (!want_pubnames ())
9445 return;
9447 /* Don't add items to the table when we expect that the consumer will have
9448 just read the enclosing die. For example, if the consumer is looking at a
9449 class_member, it will either be inside the class already, or will have just
9450 looked up the class to find the member. Either way, searching the class is
9451 faster than searching the index. */
9452 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
9453 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9455 const char *name = dwarf2_name (decl, 1);
9457 if (name)
9458 add_pubname_string (name, die);
9462 /* Add an enumerator to the pubnames section. */
9464 static void
9465 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
9467 pubname_entry e;
9469 gcc_assert (scope_name);
9470 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
9471 e.die = die;
9472 vec_safe_push (pubname_table, e);
9475 /* Add a new entry to .debug_pubtypes if appropriate. */
9477 static void
9478 add_pubtype (tree decl, dw_die_ref die)
9480 pubname_entry e;
9482 if (!want_pubnames ())
9483 return;
9485 if ((TREE_PUBLIC (decl)
9486 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9487 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
9489 tree scope = NULL;
9490 const char *scope_name = "";
9491 const char *sep = is_cxx () ? "::" : ".";
9492 const char *name;
9494 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
9495 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
9497 scope_name = lang_hooks.dwarf_name (scope, 1);
9498 if (scope_name != NULL && scope_name[0] != '\0')
9499 scope_name = concat (scope_name, sep, NULL);
9500 else
9501 scope_name = "";
9504 if (TYPE_P (decl))
9505 name = type_tag (decl);
9506 else
9507 name = lang_hooks.dwarf_name (decl, 1);
9509 /* If we don't have a name for the type, there's no point in adding
9510 it to the table. */
9511 if (name != NULL && name[0] != '\0')
9513 e.die = die;
9514 e.name = concat (scope_name, name, NULL);
9515 vec_safe_push (pubtype_table, e);
9518 /* Although it might be more consistent to add the pubinfo for the
9519 enumerators as their dies are created, they should only be added if the
9520 enum type meets the criteria above. So rather than re-check the parent
9521 enum type whenever an enumerator die is created, just output them all
9522 here. This isn't protected by the name conditional because anonymous
9523 enums don't have names. */
9524 if (die->die_tag == DW_TAG_enumeration_type)
9526 dw_die_ref c;
9528 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
9533 /* Output a single entry in the pubnames table. */
9535 static void
9536 output_pubname (dw_offset die_offset, pubname_entry *entry)
9538 dw_die_ref die = entry->die;
9539 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
9541 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
9543 if (debug_generate_pub_sections == 2)
9545 /* This logic follows gdb's method for determining the value of the flag
9546 byte. */
9547 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
9548 switch (die->die_tag)
9550 case DW_TAG_typedef:
9551 case DW_TAG_base_type:
9552 case DW_TAG_subrange_type:
9553 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9554 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9555 break;
9556 case DW_TAG_enumerator:
9557 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9558 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9559 if (!is_cxx () && !is_java ())
9560 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9561 break;
9562 case DW_TAG_subprogram:
9563 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9564 GDB_INDEX_SYMBOL_KIND_FUNCTION);
9565 if (!is_ada ())
9566 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9567 break;
9568 case DW_TAG_constant:
9569 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9570 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9571 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9572 break;
9573 case DW_TAG_variable:
9574 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9575 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9576 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9577 break;
9578 case DW_TAG_namespace:
9579 case DW_TAG_imported_declaration:
9580 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9581 break;
9582 case DW_TAG_class_type:
9583 case DW_TAG_interface_type:
9584 case DW_TAG_structure_type:
9585 case DW_TAG_union_type:
9586 case DW_TAG_enumeration_type:
9587 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9588 if (!is_cxx () && !is_java ())
9589 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9590 break;
9591 default:
9592 /* An unusual tag. Leave the flag-byte empty. */
9593 break;
9595 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
9596 "GDB-index flags");
9599 dw2_asm_output_nstring (entry->name, -1, "external name");
9603 /* Output the public names table used to speed up access to externally
9604 visible names; or the public types table used to find type definitions. */
9606 static void
9607 output_pubnames (vec<pubname_entry, va_gc> *names)
9609 unsigned i;
9610 unsigned long pubnames_length = size_of_pubnames (names);
9611 pubname_ref pub;
9613 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9614 dw2_asm_output_data (4, 0xffffffff,
9615 "Initial length escape value indicating 64-bit DWARF extension");
9616 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length, "Pub Info Length");
9618 /* Version number for pubnames/pubtypes is independent of dwarf version. */
9619 dw2_asm_output_data (2, 2, "DWARF Version");
9621 if (dwarf_split_debug_info)
9622 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9623 debug_skeleton_info_section,
9624 "Offset of Compilation Unit Info");
9625 else
9626 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9627 debug_info_section,
9628 "Offset of Compilation Unit Info");
9629 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
9630 "Compilation Unit Length");
9632 FOR_EACH_VEC_ELT (*names, i, pub)
9634 if (include_pubname_in_output (names, pub))
9636 dw_offset die_offset = pub->die->die_offset;
9638 /* We shouldn't see pubnames for DIEs outside of the main CU. */
9639 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
9640 gcc_assert (pub->die->die_mark);
9642 /* If we're putting types in their own .debug_types sections,
9643 the .debug_pubtypes table will still point to the compile
9644 unit (not the type unit), so we want to use the offset of
9645 the skeleton DIE (if there is one). */
9646 if (pub->die->comdat_type_p && names == pubtype_table)
9648 comdat_type_node_ref type_node = pub->die->die_id.die_type_node;
9650 if (type_node != NULL)
9651 die_offset = (type_node->skeleton_die != NULL
9652 ? type_node->skeleton_die->die_offset
9653 : comp_unit_die ()->die_offset);
9656 output_pubname (die_offset, pub);
9660 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
9663 /* Output public names and types tables if necessary. */
9665 static void
9666 output_pubtables (void)
9668 if (!want_pubnames () || !info_section_emitted)
9669 return;
9671 switch_to_section (debug_pubnames_section);
9672 output_pubnames (pubname_table);
9673 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
9674 It shouldn't hurt to emit it always, since pure DWARF2 consumers
9675 simply won't look for the section. */
9676 switch_to_section (debug_pubtypes_section);
9677 output_pubnames (pubtype_table);
9681 /* Output the information that goes into the .debug_aranges table.
9682 Namely, define the beginning and ending address range of the
9683 text section generated for this compilation unit. */
9685 static void
9686 output_aranges (unsigned long aranges_length)
9688 unsigned i;
9690 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9691 dw2_asm_output_data (4, 0xffffffff,
9692 "Initial length escape value indicating 64-bit DWARF extension");
9693 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
9694 "Length of Address Ranges Info");
9695 /* Version number for aranges is still 2, even up to DWARF5. */
9696 dw2_asm_output_data (2, 2, "DWARF Version");
9697 if (dwarf_split_debug_info)
9698 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9699 debug_skeleton_info_section,
9700 "Offset of Compilation Unit Info");
9701 else
9702 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9703 debug_info_section,
9704 "Offset of Compilation Unit Info");
9705 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
9706 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
9708 /* We need to align to twice the pointer size here. */
9709 if (DWARF_ARANGES_PAD_SIZE)
9711 /* Pad using a 2 byte words so that padding is correct for any
9712 pointer size. */
9713 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
9714 2 * DWARF2_ADDR_SIZE);
9715 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
9716 dw2_asm_output_data (2, 0, NULL);
9719 /* It is necessary not to output these entries if the sections were
9720 not used; if the sections were not used, the length will be 0 and
9721 the address may end up as 0 if the section is discarded by ld
9722 --gc-sections, leaving an invalid (0, 0) entry that can be
9723 confused with the terminator. */
9724 if (text_section_used)
9726 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
9727 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
9728 text_section_label, "Length");
9730 if (cold_text_section_used)
9732 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
9733 "Address");
9734 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
9735 cold_text_section_label, "Length");
9738 if (have_multiple_function_sections)
9740 unsigned fde_idx;
9741 dw_fde_ref fde;
9743 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9745 if (DECL_IGNORED_P (fde->decl))
9746 continue;
9747 if (!fde->in_std_section)
9749 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
9750 "Address");
9751 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
9752 fde->dw_fde_begin, "Length");
9754 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9756 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
9757 "Address");
9758 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
9759 fde->dw_fde_second_begin, "Length");
9764 /* Output the terminator words. */
9765 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9766 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9769 /* Add a new entry to .debug_ranges. Return the offset at which it
9770 was placed. */
9772 static unsigned int
9773 add_ranges_num (int num)
9775 unsigned int in_use = ranges_table_in_use;
9777 if (in_use == ranges_table_allocated)
9779 ranges_table_allocated += RANGES_TABLE_INCREMENT;
9780 ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
9781 ranges_table_allocated);
9782 memset (ranges_table + ranges_table_in_use, 0,
9783 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
9786 ranges_table[in_use].num = num;
9787 ranges_table_in_use = in_use + 1;
9789 return in_use * 2 * DWARF2_ADDR_SIZE;
9792 /* Add a new entry to .debug_ranges corresponding to a block, or a
9793 range terminator if BLOCK is NULL. */
9795 static unsigned int
9796 add_ranges (const_tree block)
9798 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
9801 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
9802 When using dwarf_split_debug_info, address attributes in dies destined
9803 for the final executable should be direct references--setting the
9804 parameter force_direct ensures this behavior. */
9806 static void
9807 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
9808 bool *added, bool force_direct)
9810 unsigned int in_use = ranges_by_label_in_use;
9811 unsigned int offset;
9813 if (in_use == ranges_by_label_allocated)
9815 ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
9816 ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
9817 ranges_by_label,
9818 ranges_by_label_allocated);
9819 memset (ranges_by_label + ranges_by_label_in_use, 0,
9820 RANGES_TABLE_INCREMENT
9821 * sizeof (struct dw_ranges_by_label_struct));
9824 ranges_by_label[in_use].begin = begin;
9825 ranges_by_label[in_use].end = end;
9826 ranges_by_label_in_use = in_use + 1;
9828 offset = add_ranges_num (-(int)in_use - 1);
9829 if (!*added)
9831 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
9832 *added = true;
9836 static void
9837 output_ranges (void)
9839 unsigned i;
9840 static const char *const start_fmt = "Offset %#x";
9841 const char *fmt = start_fmt;
9843 for (i = 0; i < ranges_table_in_use; i++)
9845 int block_num = ranges_table[i].num;
9847 if (block_num > 0)
9849 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
9850 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
9852 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
9853 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
9855 /* If all code is in the text section, then the compilation
9856 unit base address defaults to DW_AT_low_pc, which is the
9857 base of the text section. */
9858 if (!have_multiple_function_sections)
9860 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
9861 text_section_label,
9862 fmt, i * 2 * DWARF2_ADDR_SIZE);
9863 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
9864 text_section_label, NULL);
9867 /* Otherwise, the compilation unit base address is zero,
9868 which allows us to use absolute addresses, and not worry
9869 about whether the target supports cross-section
9870 arithmetic. */
9871 else
9873 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
9874 fmt, i * 2 * DWARF2_ADDR_SIZE);
9875 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
9878 fmt = NULL;
9881 /* Negative block_num stands for an index into ranges_by_label. */
9882 else if (block_num < 0)
9884 int lab_idx = - block_num - 1;
9886 if (!have_multiple_function_sections)
9888 gcc_unreachable ();
9889 #if 0
9890 /* If we ever use add_ranges_by_labels () for a single
9891 function section, all we have to do is to take out
9892 the #if 0 above. */
9893 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9894 ranges_by_label[lab_idx].begin,
9895 text_section_label,
9896 fmt, i * 2 * DWARF2_ADDR_SIZE);
9897 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9898 ranges_by_label[lab_idx].end,
9899 text_section_label, NULL);
9900 #endif
9902 else
9904 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9905 ranges_by_label[lab_idx].begin,
9906 fmt, i * 2 * DWARF2_ADDR_SIZE);
9907 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9908 ranges_by_label[lab_idx].end,
9909 NULL);
9912 else
9914 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9915 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9916 fmt = start_fmt;
9921 /* Data structure containing information about input files. */
9922 struct file_info
9924 const char *path; /* Complete file name. */
9925 const char *fname; /* File name part. */
9926 int length; /* Length of entire string. */
9927 struct dwarf_file_data * file_idx; /* Index in input file table. */
9928 int dir_idx; /* Index in directory table. */
9931 /* Data structure containing information about directories with source
9932 files. */
9933 struct dir_info
9935 const char *path; /* Path including directory name. */
9936 int length; /* Path length. */
9937 int prefix; /* Index of directory entry which is a prefix. */
9938 int count; /* Number of files in this directory. */
9939 int dir_idx; /* Index of directory used as base. */
9942 /* Callback function for file_info comparison. We sort by looking at
9943 the directories in the path. */
9945 static int
9946 file_info_cmp (const void *p1, const void *p2)
9948 const struct file_info *const s1 = (const struct file_info *) p1;
9949 const struct file_info *const s2 = (const struct file_info *) p2;
9950 const unsigned char *cp1;
9951 const unsigned char *cp2;
9953 /* Take care of file names without directories. We need to make sure that
9954 we return consistent values to qsort since some will get confused if
9955 we return the same value when identical operands are passed in opposite
9956 orders. So if neither has a directory, return 0 and otherwise return
9957 1 or -1 depending on which one has the directory. */
9958 if ((s1->path == s1->fname || s2->path == s2->fname))
9959 return (s2->path == s2->fname) - (s1->path == s1->fname);
9961 cp1 = (const unsigned char *) s1->path;
9962 cp2 = (const unsigned char *) s2->path;
9964 while (1)
9966 ++cp1;
9967 ++cp2;
9968 /* Reached the end of the first path? If so, handle like above. */
9969 if ((cp1 == (const unsigned char *) s1->fname)
9970 || (cp2 == (const unsigned char *) s2->fname))
9971 return ((cp2 == (const unsigned char *) s2->fname)
9972 - (cp1 == (const unsigned char *) s1->fname));
9974 /* Character of current path component the same? */
9975 else if (*cp1 != *cp2)
9976 return *cp1 - *cp2;
9980 struct file_name_acquire_data
9982 struct file_info *files;
9983 int used_files;
9984 int max_files;
9987 /* Traversal function for the hash table. */
9990 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
9992 struct dwarf_file_data *d = *slot;
9993 struct file_info *fi;
9994 const char *f;
9996 gcc_assert (fnad->max_files >= d->emitted_number);
9998 if (! d->emitted_number)
9999 return 1;
10001 gcc_assert (fnad->max_files != fnad->used_files);
10003 fi = fnad->files + fnad->used_files++;
10005 /* Skip all leading "./". */
10006 f = d->filename;
10007 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
10008 f += 2;
10010 /* Create a new array entry. */
10011 fi->path = f;
10012 fi->length = strlen (f);
10013 fi->file_idx = d;
10015 /* Search for the file name part. */
10016 f = strrchr (f, DIR_SEPARATOR);
10017 #if defined (DIR_SEPARATOR_2)
10019 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
10021 if (g != NULL)
10023 if (f == NULL || f < g)
10024 f = g;
10027 #endif
10029 fi->fname = f == NULL ? fi->path : f + 1;
10030 return 1;
10033 /* Output the directory table and the file name table. We try to minimize
10034 the total amount of memory needed. A heuristic is used to avoid large
10035 slowdowns with many input files. */
10037 static void
10038 output_file_names (void)
10040 struct file_name_acquire_data fnad;
10041 int numfiles;
10042 struct file_info *files;
10043 struct dir_info *dirs;
10044 int *saved;
10045 int *savehere;
10046 int *backmap;
10047 int ndirs;
10048 int idx_offset;
10049 int i;
10051 if (!last_emitted_file)
10053 dw2_asm_output_data (1, 0, "End directory table");
10054 dw2_asm_output_data (1, 0, "End file name table");
10055 return;
10058 numfiles = last_emitted_file->emitted_number;
10060 /* Allocate the various arrays we need. */
10061 files = XALLOCAVEC (struct file_info, numfiles);
10062 dirs = XALLOCAVEC (struct dir_info, numfiles);
10064 fnad.files = files;
10065 fnad.used_files = 0;
10066 fnad.max_files = numfiles;
10067 file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
10068 gcc_assert (fnad.used_files == fnad.max_files);
10070 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
10072 /* Find all the different directories used. */
10073 dirs[0].path = files[0].path;
10074 dirs[0].length = files[0].fname - files[0].path;
10075 dirs[0].prefix = -1;
10076 dirs[0].count = 1;
10077 dirs[0].dir_idx = 0;
10078 files[0].dir_idx = 0;
10079 ndirs = 1;
10081 for (i = 1; i < numfiles; i++)
10082 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
10083 && memcmp (dirs[ndirs - 1].path, files[i].path,
10084 dirs[ndirs - 1].length) == 0)
10086 /* Same directory as last entry. */
10087 files[i].dir_idx = ndirs - 1;
10088 ++dirs[ndirs - 1].count;
10090 else
10092 int j;
10094 /* This is a new directory. */
10095 dirs[ndirs].path = files[i].path;
10096 dirs[ndirs].length = files[i].fname - files[i].path;
10097 dirs[ndirs].count = 1;
10098 dirs[ndirs].dir_idx = ndirs;
10099 files[i].dir_idx = ndirs;
10101 /* Search for a prefix. */
10102 dirs[ndirs].prefix = -1;
10103 for (j = 0; j < ndirs; j++)
10104 if (dirs[j].length < dirs[ndirs].length
10105 && dirs[j].length > 1
10106 && (dirs[ndirs].prefix == -1
10107 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
10108 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
10109 dirs[ndirs].prefix = j;
10111 ++ndirs;
10114 /* Now to the actual work. We have to find a subset of the directories which
10115 allow expressing the file name using references to the directory table
10116 with the least amount of characters. We do not do an exhaustive search
10117 where we would have to check out every combination of every single
10118 possible prefix. Instead we use a heuristic which provides nearly optimal
10119 results in most cases and never is much off. */
10120 saved = XALLOCAVEC (int, ndirs);
10121 savehere = XALLOCAVEC (int, ndirs);
10123 memset (saved, '\0', ndirs * sizeof (saved[0]));
10124 for (i = 0; i < ndirs; i++)
10126 int j;
10127 int total;
10129 /* We can always save some space for the current directory. But this
10130 does not mean it will be enough to justify adding the directory. */
10131 savehere[i] = dirs[i].length;
10132 total = (savehere[i] - saved[i]) * dirs[i].count;
10134 for (j = i + 1; j < ndirs; j++)
10136 savehere[j] = 0;
10137 if (saved[j] < dirs[i].length)
10139 /* Determine whether the dirs[i] path is a prefix of the
10140 dirs[j] path. */
10141 int k;
10143 k = dirs[j].prefix;
10144 while (k != -1 && k != (int) i)
10145 k = dirs[k].prefix;
10147 if (k == (int) i)
10149 /* Yes it is. We can possibly save some memory by
10150 writing the filenames in dirs[j] relative to
10151 dirs[i]. */
10152 savehere[j] = dirs[i].length;
10153 total += (savehere[j] - saved[j]) * dirs[j].count;
10158 /* Check whether we can save enough to justify adding the dirs[i]
10159 directory. */
10160 if (total > dirs[i].length + 1)
10162 /* It's worthwhile adding. */
10163 for (j = i; j < ndirs; j++)
10164 if (savehere[j] > 0)
10166 /* Remember how much we saved for this directory so far. */
10167 saved[j] = savehere[j];
10169 /* Remember the prefix directory. */
10170 dirs[j].dir_idx = i;
10175 /* Emit the directory name table. */
10176 idx_offset = dirs[0].length > 0 ? 1 : 0;
10177 for (i = 1 - idx_offset; i < ndirs; i++)
10178 dw2_asm_output_nstring (dirs[i].path,
10179 dirs[i].length
10180 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
10181 "Directory Entry: %#x", i + idx_offset);
10183 dw2_asm_output_data (1, 0, "End directory table");
10185 /* We have to emit them in the order of emitted_number since that's
10186 used in the debug info generation. To do this efficiently we
10187 generate a back-mapping of the indices first. */
10188 backmap = XALLOCAVEC (int, numfiles);
10189 for (i = 0; i < numfiles; i++)
10190 backmap[files[i].file_idx->emitted_number - 1] = i;
10192 /* Now write all the file names. */
10193 for (i = 0; i < numfiles; i++)
10195 int file_idx = backmap[i];
10196 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
10198 #ifdef VMS_DEBUGGING_INFO
10199 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
10201 /* Setting these fields can lead to debugger miscomparisons,
10202 but VMS Debug requires them to be set correctly. */
10204 int ver;
10205 long long cdt;
10206 long siz;
10207 int maxfilelen = strlen (files[file_idx].path)
10208 + dirs[dir_idx].length
10209 + MAX_VMS_VERSION_LEN + 1;
10210 char *filebuf = XALLOCAVEC (char, maxfilelen);
10212 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
10213 snprintf (filebuf, maxfilelen, "%s;%d",
10214 files[file_idx].path + dirs[dir_idx].length, ver);
10216 dw2_asm_output_nstring
10217 (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
10219 /* Include directory index. */
10220 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10222 /* Modification time. */
10223 dw2_asm_output_data_uleb128
10224 ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
10225 ? cdt : 0,
10226 NULL);
10228 /* File length in bytes. */
10229 dw2_asm_output_data_uleb128
10230 ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
10231 ? siz : 0,
10232 NULL);
10233 #else
10234 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
10235 "File Entry: %#x", (unsigned) i + 1);
10237 /* Include directory index. */
10238 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10240 /* Modification time. */
10241 dw2_asm_output_data_uleb128 (0, NULL);
10243 /* File length in bytes. */
10244 dw2_asm_output_data_uleb128 (0, NULL);
10245 #endif /* VMS_DEBUGGING_INFO */
10248 dw2_asm_output_data (1, 0, "End file name table");
10252 /* Output one line number table into the .debug_line section. */
10254 static void
10255 output_one_line_info_table (dw_line_info_table *table)
10257 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
10258 unsigned int current_line = 1;
10259 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
10260 dw_line_info_entry *ent;
10261 size_t i;
10263 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
10265 switch (ent->opcode)
10267 case LI_set_address:
10268 /* ??? Unfortunately, we have little choice here currently, and
10269 must always use the most general form. GCC does not know the
10270 address delta itself, so we can't use DW_LNS_advance_pc. Many
10271 ports do have length attributes which will give an upper bound
10272 on the address range. We could perhaps use length attributes
10273 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
10274 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
10276 /* This can handle any delta. This takes
10277 4+DWARF2_ADDR_SIZE bytes. */
10278 dw2_asm_output_data (1, 0, "set address %s", line_label);
10279 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10280 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10281 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
10282 break;
10284 case LI_set_line:
10285 if (ent->val == current_line)
10287 /* We still need to start a new row, so output a copy insn. */
10288 dw2_asm_output_data (1, DW_LNS_copy,
10289 "copy line %u", current_line);
10291 else
10293 int line_offset = ent->val - current_line;
10294 int line_delta = line_offset - DWARF_LINE_BASE;
10296 current_line = ent->val;
10297 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
10299 /* This can handle deltas from -10 to 234, using the current
10300 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
10301 This takes 1 byte. */
10302 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
10303 "line %u", current_line);
10305 else
10307 /* This can handle any delta. This takes at least 4 bytes,
10308 depending on the value being encoded. */
10309 dw2_asm_output_data (1, DW_LNS_advance_line,
10310 "advance to line %u", current_line);
10311 dw2_asm_output_data_sleb128 (line_offset, NULL);
10312 dw2_asm_output_data (1, DW_LNS_copy, NULL);
10315 break;
10317 case LI_set_file:
10318 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
10319 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10320 break;
10322 case LI_set_column:
10323 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
10324 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10325 break;
10327 case LI_negate_stmt:
10328 current_is_stmt = !current_is_stmt;
10329 dw2_asm_output_data (1, DW_LNS_negate_stmt,
10330 "is_stmt %d", current_is_stmt);
10331 break;
10333 case LI_set_prologue_end:
10334 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
10335 "set prologue end");
10336 break;
10338 case LI_set_epilogue_begin:
10339 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
10340 "set epilogue begin");
10341 break;
10343 case LI_set_discriminator:
10344 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
10345 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
10346 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
10347 dw2_asm_output_data_uleb128 (ent->val, NULL);
10348 break;
10352 /* Emit debug info for the address of the end of the table. */
10353 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
10354 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10355 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10356 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
10358 dw2_asm_output_data (1, 0, "end sequence");
10359 dw2_asm_output_data_uleb128 (1, NULL);
10360 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
10363 /* Output the source line number correspondence information. This
10364 information goes into the .debug_line section. */
10366 static void
10367 output_line_info (bool prologue_only)
10369 char l1[20], l2[20], p1[20], p2[20];
10370 /* We don't support DWARFv5 line tables yet. */
10371 int ver = dwarf_version < 5 ? dwarf_version : 4;
10372 bool saw_one = false;
10373 int opc;
10375 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
10376 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
10377 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
10378 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
10380 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10381 dw2_asm_output_data (4, 0xffffffff,
10382 "Initial length escape value indicating 64-bit DWARF extension");
10383 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
10384 "Length of Source Line Info");
10385 ASM_OUTPUT_LABEL (asm_out_file, l1);
10387 dw2_asm_output_data (2, ver, "DWARF Version");
10388 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
10389 ASM_OUTPUT_LABEL (asm_out_file, p1);
10391 /* Define the architecture-dependent minimum instruction length (in bytes).
10392 In this implementation of DWARF, this field is used for information
10393 purposes only. Since GCC generates assembly language, we have no
10394 a priori knowledge of how many instruction bytes are generated for each
10395 source line, and therefore can use only the DW_LNE_set_address and
10396 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
10397 this as '1', which is "correct enough" for all architectures,
10398 and don't let the target override. */
10399 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
10401 if (ver >= 4)
10402 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
10403 "Maximum Operations Per Instruction");
10404 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
10405 "Default is_stmt_start flag");
10406 dw2_asm_output_data (1, DWARF_LINE_BASE,
10407 "Line Base Value (Special Opcodes)");
10408 dw2_asm_output_data (1, DWARF_LINE_RANGE,
10409 "Line Range Value (Special Opcodes)");
10410 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
10411 "Special Opcode Base");
10413 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
10415 int n_op_args;
10416 switch (opc)
10418 case DW_LNS_advance_pc:
10419 case DW_LNS_advance_line:
10420 case DW_LNS_set_file:
10421 case DW_LNS_set_column:
10422 case DW_LNS_fixed_advance_pc:
10423 case DW_LNS_set_isa:
10424 n_op_args = 1;
10425 break;
10426 default:
10427 n_op_args = 0;
10428 break;
10431 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
10432 opc, n_op_args);
10435 /* Write out the information about the files we use. */
10436 output_file_names ();
10437 ASM_OUTPUT_LABEL (asm_out_file, p2);
10438 if (prologue_only)
10440 /* Output the marker for the end of the line number info. */
10441 ASM_OUTPUT_LABEL (asm_out_file, l2);
10442 return;
10445 if (separate_line_info)
10447 dw_line_info_table *table;
10448 size_t i;
10450 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
10451 if (table->in_use)
10453 output_one_line_info_table (table);
10454 saw_one = true;
10457 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
10459 output_one_line_info_table (cold_text_section_line_info);
10460 saw_one = true;
10463 /* ??? Some Darwin linkers crash on a .debug_line section with no
10464 sequences. Further, merely a DW_LNE_end_sequence entry is not
10465 sufficient -- the address column must also be initialized.
10466 Make sure to output at least one set_address/end_sequence pair,
10467 choosing .text since that section is always present. */
10468 if (text_section_line_info->in_use || !saw_one)
10469 output_one_line_info_table (text_section_line_info);
10471 /* Output the marker for the end of the line number info. */
10472 ASM_OUTPUT_LABEL (asm_out_file, l2);
10475 /* Given a pointer to a tree node for some base type, return a pointer to
10476 a DIE that describes the given type.
10478 This routine must only be called for GCC type nodes that correspond to
10479 Dwarf base (fundamental) types. */
10481 static dw_die_ref
10482 base_type_die (tree type)
10484 dw_die_ref base_type_result;
10485 enum dwarf_type encoding;
10487 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
10488 return 0;
10490 /* If this is a subtype that should not be emitted as a subrange type,
10491 use the base type. See subrange_type_for_debug_p. */
10492 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
10493 type = TREE_TYPE (type);
10495 switch (TREE_CODE (type))
10497 case INTEGER_TYPE:
10498 if ((dwarf_version >= 4 || !dwarf_strict)
10499 && TYPE_NAME (type)
10500 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10501 && DECL_IS_BUILTIN (TYPE_NAME (type))
10502 && DECL_NAME (TYPE_NAME (type)))
10504 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
10505 if (strcmp (name, "char16_t") == 0
10506 || strcmp (name, "char32_t") == 0)
10508 encoding = DW_ATE_UTF;
10509 break;
10512 if (TYPE_STRING_FLAG (type))
10514 if (TYPE_UNSIGNED (type))
10515 encoding = DW_ATE_unsigned_char;
10516 else
10517 encoding = DW_ATE_signed_char;
10519 else if (TYPE_UNSIGNED (type))
10520 encoding = DW_ATE_unsigned;
10521 else
10522 encoding = DW_ATE_signed;
10523 break;
10525 case REAL_TYPE:
10526 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
10528 if (dwarf_version >= 3 || !dwarf_strict)
10529 encoding = DW_ATE_decimal_float;
10530 else
10531 encoding = DW_ATE_lo_user;
10533 else
10534 encoding = DW_ATE_float;
10535 break;
10537 case FIXED_POINT_TYPE:
10538 if (!(dwarf_version >= 3 || !dwarf_strict))
10539 encoding = DW_ATE_lo_user;
10540 else if (TYPE_UNSIGNED (type))
10541 encoding = DW_ATE_unsigned_fixed;
10542 else
10543 encoding = DW_ATE_signed_fixed;
10544 break;
10546 /* Dwarf2 doesn't know anything about complex ints, so use
10547 a user defined type for it. */
10548 case COMPLEX_TYPE:
10549 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
10550 encoding = DW_ATE_complex_float;
10551 else
10552 encoding = DW_ATE_lo_user;
10553 break;
10555 case BOOLEAN_TYPE:
10556 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
10557 encoding = DW_ATE_boolean;
10558 break;
10560 default:
10561 /* No other TREE_CODEs are Dwarf fundamental types. */
10562 gcc_unreachable ();
10565 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
10567 add_AT_unsigned (base_type_result, DW_AT_byte_size,
10568 int_size_in_bytes (type));
10569 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
10570 add_pubtype (type, base_type_result);
10572 return base_type_result;
10575 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
10576 named 'auto' in its type: return true for it, false otherwise. */
10578 static inline bool
10579 is_cxx_auto (tree type)
10581 if (is_cxx ())
10583 tree name = TYPE_IDENTIFIER (type);
10584 if (name == get_identifier ("auto")
10585 || name == get_identifier ("decltype(auto)"))
10586 return true;
10588 return false;
10591 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
10592 given input type is a Dwarf "fundamental" type. Otherwise return null. */
10594 static inline int
10595 is_base_type (tree type)
10597 switch (TREE_CODE (type))
10599 case ERROR_MARK:
10600 case VOID_TYPE:
10601 case INTEGER_TYPE:
10602 case REAL_TYPE:
10603 case FIXED_POINT_TYPE:
10604 case COMPLEX_TYPE:
10605 case BOOLEAN_TYPE:
10606 case POINTER_BOUNDS_TYPE:
10607 return 1;
10609 case ARRAY_TYPE:
10610 case RECORD_TYPE:
10611 case UNION_TYPE:
10612 case QUAL_UNION_TYPE:
10613 case ENUMERAL_TYPE:
10614 case FUNCTION_TYPE:
10615 case METHOD_TYPE:
10616 case POINTER_TYPE:
10617 case REFERENCE_TYPE:
10618 case NULLPTR_TYPE:
10619 case OFFSET_TYPE:
10620 case LANG_TYPE:
10621 case VECTOR_TYPE:
10622 return 0;
10624 default:
10625 if (is_cxx_auto (type))
10626 return 0;
10627 gcc_unreachable ();
10630 return 0;
10633 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
10634 node, return the size in bits for the type if it is a constant, or else
10635 return the alignment for the type if the type's size is not constant, or
10636 else return BITS_PER_WORD if the type actually turns out to be an
10637 ERROR_MARK node. */
10639 static inline unsigned HOST_WIDE_INT
10640 simple_type_size_in_bits (const_tree type)
10642 if (TREE_CODE (type) == ERROR_MARK)
10643 return BITS_PER_WORD;
10644 else if (TYPE_SIZE (type) == NULL_TREE)
10645 return 0;
10646 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
10647 return tree_to_uhwi (TYPE_SIZE (type));
10648 else
10649 return TYPE_ALIGN (type);
10652 /* Similarly, but return an offset_int instead of UHWI. */
10654 static inline offset_int
10655 offset_int_type_size_in_bits (const_tree type)
10657 if (TREE_CODE (type) == ERROR_MARK)
10658 return BITS_PER_WORD;
10659 else if (TYPE_SIZE (type) == NULL_TREE)
10660 return 0;
10661 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
10662 return wi::to_offset (TYPE_SIZE (type));
10663 else
10664 return TYPE_ALIGN (type);
10667 /* Given a pointer to a tree node for a subrange type, return a pointer
10668 to a DIE that describes the given type. */
10670 static dw_die_ref
10671 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
10673 dw_die_ref subrange_die;
10674 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
10676 if (context_die == NULL)
10677 context_die = comp_unit_die ();
10679 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
10681 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
10683 /* The size of the subrange type and its base type do not match,
10684 so we need to generate a size attribute for the subrange type. */
10685 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
10688 if (low)
10689 add_bound_info (subrange_die, DW_AT_lower_bound, low, NULL);
10690 if (high)
10691 add_bound_info (subrange_die, DW_AT_upper_bound, high, NULL);
10693 return subrange_die;
10696 /* Returns the (const and/or volatile) cv_qualifiers associated with
10697 the decl node. This will normally be augmented with the
10698 cv_qualifiers of the underlying type in add_type_attribute. */
10700 static int
10701 decl_quals (const_tree decl)
10703 return ((TREE_READONLY (decl)
10704 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
10705 | (TREE_THIS_VOLATILE (decl)
10706 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
10709 /* Determine the TYPE whose qualifiers match the largest strict subset
10710 of the given TYPE_QUALS, and return its qualifiers. Ignore all
10711 qualifiers outside QUAL_MASK. */
10713 static int
10714 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
10716 tree t;
10717 int best_rank = 0, best_qual = 0, max_rank;
10719 type_quals &= qual_mask;
10720 max_rank = popcount_hwi (type_quals) - 1;
10722 for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
10723 t = TYPE_NEXT_VARIANT (t))
10725 int q = TYPE_QUALS (t) & qual_mask;
10727 if ((q & type_quals) == q && q != type_quals
10728 && check_base_type (t, type))
10730 int rank = popcount_hwi (q);
10732 if (rank > best_rank)
10734 best_rank = rank;
10735 best_qual = q;
10740 return best_qual;
10743 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
10744 entry that chains various modifiers in front of the given type. */
10746 static dw_die_ref
10747 modified_type_die (tree type, int cv_quals, dw_die_ref context_die)
10749 enum tree_code code = TREE_CODE (type);
10750 dw_die_ref mod_type_die;
10751 dw_die_ref sub_die = NULL;
10752 tree item_type = NULL;
10753 tree qualified_type;
10754 tree name, low, high;
10755 dw_die_ref mod_scope;
10756 /* Only these cv-qualifiers are currently handled. */
10757 const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
10758 | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC);
10760 if (code == ERROR_MARK)
10761 return NULL;
10763 cv_quals &= cv_qual_mask;
10765 /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
10766 tag modifier (and not an attribute) old consumers won't be able
10767 to handle it. */
10768 if (dwarf_version < 3)
10769 cv_quals &= ~TYPE_QUAL_RESTRICT;
10771 /* Likewise for DW_TAG_atomic_type for DWARFv5. */
10772 if (dwarf_version < 5)
10773 cv_quals &= ~TYPE_QUAL_ATOMIC;
10775 /* See if we already have the appropriately qualified variant of
10776 this type. */
10777 qualified_type = get_qualified_type (type, cv_quals);
10779 if (qualified_type == sizetype
10780 && TYPE_NAME (qualified_type)
10781 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
10783 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
10785 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
10786 && TYPE_PRECISION (t)
10787 == TYPE_PRECISION (qualified_type)
10788 && TYPE_UNSIGNED (t)
10789 == TYPE_UNSIGNED (qualified_type));
10790 qualified_type = t;
10793 /* If we do, then we can just use its DIE, if it exists. */
10794 if (qualified_type)
10796 mod_type_die = lookup_type_die (qualified_type);
10797 if (mod_type_die)
10798 return mod_type_die;
10801 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
10803 /* Handle C typedef types. */
10804 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
10805 && !DECL_ARTIFICIAL (name))
10807 tree dtype = TREE_TYPE (name);
10809 if (qualified_type == dtype)
10811 /* For a named type, use the typedef. */
10812 gen_type_die (qualified_type, context_die);
10813 return lookup_type_die (qualified_type);
10815 else
10817 int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
10818 dquals &= cv_qual_mask;
10819 if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
10820 || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
10821 /* cv-unqualified version of named type. Just use
10822 the unnamed type to which it refers. */
10823 return modified_type_die (DECL_ORIGINAL_TYPE (name),
10824 cv_quals, context_die);
10825 /* Else cv-qualified version of named type; fall through. */
10829 mod_scope = scope_die_for (type, context_die);
10831 if (cv_quals)
10833 struct qual_info { int q; enum dwarf_tag t; };
10834 static const struct qual_info qual_info[] =
10836 { TYPE_QUAL_ATOMIC, DW_TAG_atomic_type },
10837 { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
10838 { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
10839 { TYPE_QUAL_CONST, DW_TAG_const_type },
10841 int sub_quals;
10842 unsigned i;
10844 /* Determine a lesser qualified type that most closely matches
10845 this one. Then generate DW_TAG_* entries for the remaining
10846 qualifiers. */
10847 sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
10848 cv_qual_mask);
10849 mod_type_die = modified_type_die (type, sub_quals, context_die);
10851 for (i = 0; i < sizeof (qual_info) / sizeof (qual_info[0]); i++)
10852 if (qual_info[i].q & cv_quals & ~sub_quals)
10854 dw_die_ref d = new_die (qual_info[i].t, mod_scope, type);
10855 if (mod_type_die)
10856 add_AT_die_ref (d, DW_AT_type, mod_type_die);
10857 mod_type_die = d;
10860 else if (code == POINTER_TYPE)
10862 mod_type_die = new_die (DW_TAG_pointer_type, mod_scope, type);
10863 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10864 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10865 item_type = TREE_TYPE (type);
10866 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10867 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10868 TYPE_ADDR_SPACE (item_type));
10870 else if (code == REFERENCE_TYPE)
10872 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
10873 mod_type_die = new_die (DW_TAG_rvalue_reference_type, mod_scope,
10874 type);
10875 else
10876 mod_type_die = new_die (DW_TAG_reference_type, mod_scope, type);
10877 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10878 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10879 item_type = TREE_TYPE (type);
10880 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10881 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10882 TYPE_ADDR_SPACE (item_type));
10884 else if (code == INTEGER_TYPE
10885 && TREE_TYPE (type) != NULL_TREE
10886 && subrange_type_for_debug_p (type, &low, &high))
10888 mod_type_die = subrange_type_die (type, low, high, context_die);
10889 item_type = TREE_TYPE (type);
10891 else if (is_base_type (type))
10892 mod_type_die = base_type_die (type);
10893 else
10895 gen_type_die (type, context_die);
10897 /* We have to get the type_main_variant here (and pass that to the
10898 `lookup_type_die' routine) because the ..._TYPE node we have
10899 might simply be a *copy* of some original type node (where the
10900 copy was created to help us keep track of typedef names) and
10901 that copy might have a different TYPE_UID from the original
10902 ..._TYPE node. */
10903 if (TREE_CODE (type) != VECTOR_TYPE)
10904 return lookup_type_die (type_main_variant (type));
10905 else
10906 /* Vectors have the debugging information in the type,
10907 not the main variant. */
10908 return lookup_type_die (type);
10911 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
10912 don't output a DW_TAG_typedef, since there isn't one in the
10913 user's program; just attach a DW_AT_name to the type.
10914 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
10915 if the base type already has the same name. */
10916 if (name
10917 && ((TREE_CODE (name) != TYPE_DECL
10918 && (qualified_type == TYPE_MAIN_VARIANT (type)
10919 || (cv_quals == TYPE_UNQUALIFIED)))
10920 || (TREE_CODE (name) == TYPE_DECL
10921 && TREE_TYPE (name) == qualified_type
10922 && DECL_NAME (name))))
10924 if (TREE_CODE (name) == TYPE_DECL)
10925 /* Could just call add_name_and_src_coords_attributes here,
10926 but since this is a builtin type it doesn't have any
10927 useful source coordinates anyway. */
10928 name = DECL_NAME (name);
10929 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
10931 /* This probably indicates a bug. */
10932 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
10934 name = TYPE_IDENTIFIER (type);
10935 add_name_attribute (mod_type_die,
10936 name ? IDENTIFIER_POINTER (name) : "__unknown__");
10939 if (qualified_type)
10940 equate_type_number_to_die (qualified_type, mod_type_die);
10942 if (item_type)
10943 /* We must do this after the equate_type_number_to_die call, in case
10944 this is a recursive type. This ensures that the modified_type_die
10945 recursion will terminate even if the type is recursive. Recursive
10946 types are possible in Ada. */
10947 sub_die = modified_type_die (item_type,
10948 TYPE_QUALS_NO_ADDR_SPACE (item_type),
10949 context_die);
10951 if (sub_die != NULL)
10952 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
10954 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
10955 if (TYPE_ARTIFICIAL (type))
10956 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
10958 return mod_type_die;
10961 /* Generate DIEs for the generic parameters of T.
10962 T must be either a generic type or a generic function.
10963 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
10965 static void
10966 gen_generic_params_dies (tree t)
10968 tree parms, args;
10969 int parms_num, i;
10970 dw_die_ref die = NULL;
10971 int non_default;
10973 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
10974 return;
10976 if (TYPE_P (t))
10977 die = lookup_type_die (t);
10978 else if (DECL_P (t))
10979 die = lookup_decl_die (t);
10981 gcc_assert (die);
10983 parms = lang_hooks.get_innermost_generic_parms (t);
10984 if (!parms)
10985 /* T has no generic parameter. It means T is neither a generic type
10986 or function. End of story. */
10987 return;
10989 parms_num = TREE_VEC_LENGTH (parms);
10990 args = lang_hooks.get_innermost_generic_args (t);
10991 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
10992 non_default = int_cst_value (TREE_CHAIN (args));
10993 else
10994 non_default = TREE_VEC_LENGTH (args);
10995 for (i = 0; i < parms_num; i++)
10997 tree parm, arg, arg_pack_elems;
10998 dw_die_ref parm_die;
11000 parm = TREE_VEC_ELT (parms, i);
11001 arg = TREE_VEC_ELT (args, i);
11002 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
11003 gcc_assert (parm && TREE_VALUE (parm) && arg);
11005 if (parm && TREE_VALUE (parm) && arg)
11007 /* If PARM represents a template parameter pack,
11008 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
11009 by DW_TAG_template_*_parameter DIEs for the argument
11010 pack elements of ARG. Note that ARG would then be
11011 an argument pack. */
11012 if (arg_pack_elems)
11013 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
11014 arg_pack_elems,
11015 die);
11016 else
11017 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
11018 true /* emit name */, die);
11019 if (i >= non_default)
11020 add_AT_flag (parm_die, DW_AT_default_value, 1);
11025 /* Create and return a DIE for PARM which should be
11026 the representation of a generic type parameter.
11027 For instance, in the C++ front end, PARM would be a template parameter.
11028 ARG is the argument to PARM.
11029 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
11030 name of the PARM.
11031 PARENT_DIE is the parent DIE which the new created DIE should be added to,
11032 as a child node. */
11034 static dw_die_ref
11035 generic_parameter_die (tree parm, tree arg,
11036 bool emit_name_p,
11037 dw_die_ref parent_die)
11039 dw_die_ref tmpl_die = NULL;
11040 const char *name = NULL;
11042 if (!parm || !DECL_NAME (parm) || !arg)
11043 return NULL;
11045 /* We support non-type generic parameters and arguments,
11046 type generic parameters and arguments, as well as
11047 generic generic parameters (a.k.a. template template parameters in C++)
11048 and arguments. */
11049 if (TREE_CODE (parm) == PARM_DECL)
11050 /* PARM is a nontype generic parameter */
11051 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
11052 else if (TREE_CODE (parm) == TYPE_DECL)
11053 /* PARM is a type generic parameter. */
11054 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
11055 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
11056 /* PARM is a generic generic parameter.
11057 Its DIE is a GNU extension. It shall have a
11058 DW_AT_name attribute to represent the name of the template template
11059 parameter, and a DW_AT_GNU_template_name attribute to represent the
11060 name of the template template argument. */
11061 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
11062 parent_die, parm);
11063 else
11064 gcc_unreachable ();
11066 if (tmpl_die)
11068 tree tmpl_type;
11070 /* If PARM is a generic parameter pack, it means we are
11071 emitting debug info for a template argument pack element.
11072 In other terms, ARG is a template argument pack element.
11073 In that case, we don't emit any DW_AT_name attribute for
11074 the die. */
11075 if (emit_name_p)
11077 name = IDENTIFIER_POINTER (DECL_NAME (parm));
11078 gcc_assert (name);
11079 add_AT_string (tmpl_die, DW_AT_name, name);
11082 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
11084 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
11085 TMPL_DIE should have a child DW_AT_type attribute that is set
11086 to the type of the argument to PARM, which is ARG.
11087 If PARM is a type generic parameter, TMPL_DIE should have a
11088 child DW_AT_type that is set to ARG. */
11089 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
11090 add_type_attribute (tmpl_die, tmpl_type,
11091 (TREE_THIS_VOLATILE (tmpl_type)
11092 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
11093 parent_die);
11095 else
11097 /* So TMPL_DIE is a DIE representing a
11098 a generic generic template parameter, a.k.a template template
11099 parameter in C++ and arg is a template. */
11101 /* The DW_AT_GNU_template_name attribute of the DIE must be set
11102 to the name of the argument. */
11103 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
11104 if (name)
11105 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
11108 if (TREE_CODE (parm) == PARM_DECL)
11109 /* So PARM is a non-type generic parameter.
11110 DWARF3 5.6.8 says we must set a DW_AT_const_value child
11111 attribute of TMPL_DIE which value represents the value
11112 of ARG.
11113 We must be careful here:
11114 The value of ARG might reference some function decls.
11115 We might currently be emitting debug info for a generic
11116 type and types are emitted before function decls, we don't
11117 know if the function decls referenced by ARG will actually be
11118 emitted after cgraph computations.
11119 So must defer the generation of the DW_AT_const_value to
11120 after cgraph is ready. */
11121 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
11124 return tmpl_die;
11127 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
11128 PARM_PACK must be a template parameter pack. The returned DIE
11129 will be child DIE of PARENT_DIE. */
11131 static dw_die_ref
11132 template_parameter_pack_die (tree parm_pack,
11133 tree parm_pack_args,
11134 dw_die_ref parent_die)
11136 dw_die_ref die;
11137 int j;
11139 gcc_assert (parent_die && parm_pack);
11141 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
11142 add_name_and_src_coords_attributes (die, parm_pack);
11143 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
11144 generic_parameter_die (parm_pack,
11145 TREE_VEC_ELT (parm_pack_args, j),
11146 false /* Don't emit DW_AT_name */,
11147 die);
11148 return die;
11151 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
11152 an enumerated type. */
11154 static inline int
11155 type_is_enum (const_tree type)
11157 return TREE_CODE (type) == ENUMERAL_TYPE;
11160 /* Return the DBX register number described by a given RTL node. */
11162 static unsigned int
11163 dbx_reg_number (const_rtx rtl)
11165 unsigned regno = REGNO (rtl);
11167 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
11169 #ifdef LEAF_REG_REMAP
11170 if (crtl->uses_only_leaf_regs)
11172 int leaf_reg = LEAF_REG_REMAP (regno);
11173 if (leaf_reg != -1)
11174 regno = (unsigned) leaf_reg;
11176 #endif
11178 regno = DBX_REGISTER_NUMBER (regno);
11179 gcc_assert (regno != INVALID_REGNUM);
11180 return regno;
11183 /* Optionally add a DW_OP_piece term to a location description expression.
11184 DW_OP_piece is only added if the location description expression already
11185 doesn't end with DW_OP_piece. */
11187 static void
11188 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
11190 dw_loc_descr_ref loc;
11192 if (*list_head != NULL)
11194 /* Find the end of the chain. */
11195 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
11198 if (loc->dw_loc_opc != DW_OP_piece)
11199 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
11203 /* Return a location descriptor that designates a machine register or
11204 zero if there is none. */
11206 static dw_loc_descr_ref
11207 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
11209 rtx regs;
11211 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
11212 return 0;
11214 /* We only use "frame base" when we're sure we're talking about the
11215 post-prologue local stack frame. We do this by *not* running
11216 register elimination until this point, and recognizing the special
11217 argument pointer and soft frame pointer rtx's.
11218 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
11219 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
11220 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
11222 dw_loc_descr_ref result = NULL;
11224 if (dwarf_version >= 4 || !dwarf_strict)
11226 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
11227 initialized);
11228 if (result)
11229 add_loc_descr (&result,
11230 new_loc_descr (DW_OP_stack_value, 0, 0));
11232 return result;
11235 regs = targetm.dwarf_register_span (rtl);
11237 if (REG_NREGS (rtl) > 1 || regs)
11238 return multiple_reg_loc_descriptor (rtl, regs, initialized);
11239 else
11241 unsigned int dbx_regnum = dbx_reg_number (rtl);
11242 if (dbx_regnum == IGNORED_DWARF_REGNUM)
11243 return 0;
11244 return one_reg_loc_descriptor (dbx_regnum, initialized);
11248 /* Return a location descriptor that designates a machine register for
11249 a given hard register number. */
11251 static dw_loc_descr_ref
11252 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
11254 dw_loc_descr_ref reg_loc_descr;
11256 if (regno <= 31)
11257 reg_loc_descr
11258 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
11259 else
11260 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
11262 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11263 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11265 return reg_loc_descr;
11268 /* Given an RTL of a register, return a location descriptor that
11269 designates a value that spans more than one register. */
11271 static dw_loc_descr_ref
11272 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
11273 enum var_init_status initialized)
11275 int size, i;
11276 dw_loc_descr_ref loc_result = NULL;
11278 /* Simple, contiguous registers. */
11279 if (regs == NULL_RTX)
11281 unsigned reg = REGNO (rtl);
11282 int nregs;
11284 #ifdef LEAF_REG_REMAP
11285 if (crtl->uses_only_leaf_regs)
11287 int leaf_reg = LEAF_REG_REMAP (reg);
11288 if (leaf_reg != -1)
11289 reg = (unsigned) leaf_reg;
11291 #endif
11293 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
11294 nregs = REG_NREGS (rtl);
11296 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
11298 loc_result = NULL;
11299 while (nregs--)
11301 dw_loc_descr_ref t;
11303 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
11304 VAR_INIT_STATUS_INITIALIZED);
11305 add_loc_descr (&loc_result, t);
11306 add_loc_descr_op_piece (&loc_result, size);
11307 ++reg;
11309 return loc_result;
11312 /* Now onto stupid register sets in non contiguous locations. */
11314 gcc_assert (GET_CODE (regs) == PARALLEL);
11316 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
11317 loc_result = NULL;
11319 for (i = 0; i < XVECLEN (regs, 0); ++i)
11321 dw_loc_descr_ref t;
11323 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
11324 VAR_INIT_STATUS_INITIALIZED);
11325 add_loc_descr (&loc_result, t);
11326 add_loc_descr_op_piece (&loc_result, size);
11329 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
11330 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11331 return loc_result;
11334 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
11336 /* Return a location descriptor that designates a constant i,
11337 as a compound operation from constant (i >> shift), constant shift
11338 and DW_OP_shl. */
11340 static dw_loc_descr_ref
11341 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11343 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
11344 add_loc_descr (&ret, int_loc_descriptor (shift));
11345 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11346 return ret;
11349 /* Return a location descriptor that designates a constant. */
11351 static dw_loc_descr_ref
11352 int_loc_descriptor (HOST_WIDE_INT i)
11354 enum dwarf_location_atom op;
11356 /* Pick the smallest representation of a constant, rather than just
11357 defaulting to the LEB encoding. */
11358 if (i >= 0)
11360 int clz = clz_hwi (i);
11361 int ctz = ctz_hwi (i);
11362 if (i <= 31)
11363 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
11364 else if (i <= 0xff)
11365 op = DW_OP_const1u;
11366 else if (i <= 0xffff)
11367 op = DW_OP_const2u;
11368 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11369 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11370 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
11371 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
11372 while DW_OP_const4u is 5 bytes. */
11373 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
11374 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11375 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11376 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
11377 while DW_OP_const4u is 5 bytes. */
11378 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11379 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11380 op = DW_OP_const4u;
11381 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11382 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11383 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
11384 while DW_OP_constu of constant >= 0x100000000 takes at least
11385 6 bytes. */
11386 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11387 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11388 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
11389 >= HOST_BITS_PER_WIDE_INT)
11390 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
11391 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
11392 while DW_OP_constu takes in this case at least 6 bytes. */
11393 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
11394 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11395 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11396 && size_of_uleb128 (i) > 6)
11397 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
11398 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
11399 else
11400 op = DW_OP_constu;
11402 else
11404 if (i >= -0x80)
11405 op = DW_OP_const1s;
11406 else if (i >= -0x8000)
11407 op = DW_OP_const2s;
11408 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11410 if (size_of_int_loc_descriptor (i) < 5)
11412 dw_loc_descr_ref ret = int_loc_descriptor (-i);
11413 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11414 return ret;
11416 op = DW_OP_const4s;
11418 else
11420 if (size_of_int_loc_descriptor (i)
11421 < (unsigned long) 1 + size_of_sleb128 (i))
11423 dw_loc_descr_ref ret = int_loc_descriptor (-i);
11424 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11425 return ret;
11427 op = DW_OP_consts;
11431 return new_loc_descr (op, i, 0);
11434 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
11435 without actually allocating it. */
11437 static unsigned long
11438 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11440 return size_of_int_loc_descriptor (i >> shift)
11441 + size_of_int_loc_descriptor (shift)
11442 + 1;
11445 /* Return size_of_locs (int_loc_descriptor (i)) without
11446 actually allocating it. */
11448 static unsigned long
11449 size_of_int_loc_descriptor (HOST_WIDE_INT i)
11451 unsigned long s;
11453 if (i >= 0)
11455 int clz, ctz;
11456 if (i <= 31)
11457 return 1;
11458 else if (i <= 0xff)
11459 return 2;
11460 else if (i <= 0xffff)
11461 return 3;
11462 clz = clz_hwi (i);
11463 ctz = ctz_hwi (i);
11464 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11465 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11466 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11467 - clz - 5);
11468 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11469 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11470 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11471 - clz - 8);
11472 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11473 return 5;
11474 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
11475 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11476 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11477 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11478 - clz - 8);
11479 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11480 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
11481 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11482 - clz - 16);
11483 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11484 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11485 && s > 6)
11486 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11487 - clz - 32);
11488 else
11489 return 1 + s;
11491 else
11493 if (i >= -0x80)
11494 return 2;
11495 else if (i >= -0x8000)
11496 return 3;
11497 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11499 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11501 s = size_of_int_loc_descriptor (-i) + 1;
11502 if (s < 5)
11503 return s;
11505 return 5;
11507 else
11509 unsigned long r = 1 + size_of_sleb128 (i);
11510 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11512 s = size_of_int_loc_descriptor (-i) + 1;
11513 if (s < r)
11514 return s;
11516 return r;
11521 /* Return loc description representing "address" of integer value.
11522 This can appear only as toplevel expression. */
11524 static dw_loc_descr_ref
11525 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
11527 int litsize;
11528 dw_loc_descr_ref loc_result = NULL;
11530 if (!(dwarf_version >= 4 || !dwarf_strict))
11531 return NULL;
11533 litsize = size_of_int_loc_descriptor (i);
11534 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
11535 is more compact. For DW_OP_stack_value we need:
11536 litsize + 1 (DW_OP_stack_value)
11537 and for DW_OP_implicit_value:
11538 1 (DW_OP_implicit_value) + 1 (length) + size. */
11539 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
11541 loc_result = int_loc_descriptor (i);
11542 add_loc_descr (&loc_result,
11543 new_loc_descr (DW_OP_stack_value, 0, 0));
11544 return loc_result;
11547 loc_result = new_loc_descr (DW_OP_implicit_value,
11548 size, 0);
11549 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
11550 loc_result->dw_loc_oprnd2.v.val_int = i;
11551 return loc_result;
11554 /* Return a location descriptor that designates a base+offset location. */
11556 static dw_loc_descr_ref
11557 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
11558 enum var_init_status initialized)
11560 unsigned int regno;
11561 dw_loc_descr_ref result;
11562 dw_fde_ref fde = cfun->fde;
11564 /* We only use "frame base" when we're sure we're talking about the
11565 post-prologue local stack frame. We do this by *not* running
11566 register elimination until this point, and recognizing the special
11567 argument pointer and soft frame pointer rtx's. */
11568 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
11570 rtx elim = (ira_use_lra_p
11571 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
11572 : eliminate_regs (reg, VOIDmode, NULL_RTX));
11574 if (elim != reg)
11576 if (GET_CODE (elim) == PLUS)
11578 offset += INTVAL (XEXP (elim, 1));
11579 elim = XEXP (elim, 0);
11581 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
11582 && (elim == hard_frame_pointer_rtx
11583 || elim == stack_pointer_rtx))
11584 || elim == (frame_pointer_needed
11585 ? hard_frame_pointer_rtx
11586 : stack_pointer_rtx));
11588 /* If drap register is used to align stack, use frame
11589 pointer + offset to access stack variables. If stack
11590 is aligned without drap, use stack pointer + offset to
11591 access stack variables. */
11592 if (crtl->stack_realign_tried
11593 && reg == frame_pointer_rtx)
11595 int base_reg
11596 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
11597 ? HARD_FRAME_POINTER_REGNUM
11598 : REGNO (elim));
11599 return new_reg_loc_descr (base_reg, offset);
11602 gcc_assert (frame_pointer_fb_offset_valid);
11603 offset += frame_pointer_fb_offset;
11604 return new_loc_descr (DW_OP_fbreg, offset, 0);
11608 regno = REGNO (reg);
11609 #ifdef LEAF_REG_REMAP
11610 if (crtl->uses_only_leaf_regs)
11612 int leaf_reg = LEAF_REG_REMAP (regno);
11613 if (leaf_reg != -1)
11614 regno = (unsigned) leaf_reg;
11616 #endif
11617 regno = DWARF_FRAME_REGNUM (regno);
11619 if (!optimize && fde
11620 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
11622 /* Use cfa+offset to represent the location of arguments passed
11623 on the stack when drap is used to align stack.
11624 Only do this when not optimizing, for optimized code var-tracking
11625 is supposed to track where the arguments live and the register
11626 used as vdrap or drap in some spot might be used for something
11627 else in other part of the routine. */
11628 return new_loc_descr (DW_OP_fbreg, offset, 0);
11631 if (regno <= 31)
11632 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
11633 offset, 0);
11634 else
11635 result = new_loc_descr (DW_OP_bregx, regno, offset);
11637 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11638 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11640 return result;
11643 /* Return true if this RTL expression describes a base+offset calculation. */
11645 static inline int
11646 is_based_loc (const_rtx rtl)
11648 return (GET_CODE (rtl) == PLUS
11649 && ((REG_P (XEXP (rtl, 0))
11650 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
11651 && CONST_INT_P (XEXP (rtl, 1)))));
11654 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
11655 failed. */
11657 static dw_loc_descr_ref
11658 tls_mem_loc_descriptor (rtx mem)
11660 tree base;
11661 dw_loc_descr_ref loc_result;
11663 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
11664 return NULL;
11666 base = get_base_address (MEM_EXPR (mem));
11667 if (base == NULL
11668 || TREE_CODE (base) != VAR_DECL
11669 || !DECL_THREAD_LOCAL_P (base))
11670 return NULL;
11672 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
11673 if (loc_result == NULL)
11674 return NULL;
11676 if (MEM_OFFSET (mem))
11677 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
11679 return loc_result;
11682 /* Output debug info about reason why we failed to expand expression as dwarf
11683 expression. */
11685 static void
11686 expansion_failed (tree expr, rtx rtl, char const *reason)
11688 if (dump_file && (dump_flags & TDF_DETAILS))
11690 fprintf (dump_file, "Failed to expand as dwarf: ");
11691 if (expr)
11692 print_generic_expr (dump_file, expr, dump_flags);
11693 if (rtl)
11695 fprintf (dump_file, "\n");
11696 print_rtl (dump_file, rtl);
11698 fprintf (dump_file, "\nReason: %s\n", reason);
11702 /* Helper function for const_ok_for_output. */
11704 static bool
11705 const_ok_for_output_1 (rtx rtl)
11707 if (GET_CODE (rtl) == UNSPEC)
11709 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
11710 we can't express it in the debug info. */
11711 #ifdef ENABLE_CHECKING
11712 /* Don't complain about TLS UNSPECs, those are just too hard to
11713 delegitimize. Note this could be a non-decl SYMBOL_REF such as
11714 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
11715 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
11716 if (XVECLEN (rtl, 0) == 0
11717 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
11718 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE)
11719 inform (current_function_decl
11720 ? DECL_SOURCE_LOCATION (current_function_decl)
11721 : UNKNOWN_LOCATION,
11722 #if NUM_UNSPEC_VALUES > 0
11723 "non-delegitimized UNSPEC %s (%d) found in variable location",
11724 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
11725 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
11726 XINT (rtl, 1));
11727 #else
11728 "non-delegitimized UNSPEC %d found in variable location",
11729 XINT (rtl, 1));
11730 #endif
11731 #endif
11732 expansion_failed (NULL_TREE, rtl,
11733 "UNSPEC hasn't been delegitimized.\n");
11734 return false;
11737 if (targetm.const_not_ok_for_debug_p (rtl))
11739 expansion_failed (NULL_TREE, rtl,
11740 "Expression rejected for debug by the backend.\n");
11741 return false;
11744 /* FIXME: Refer to PR60655. It is possible for simplification
11745 of rtl expressions in var tracking to produce such expressions.
11746 We should really identify / validate expressions
11747 enclosed in CONST that can be handled by assemblers on various
11748 targets and only handle legitimate cases here. */
11749 if (GET_CODE (rtl) != SYMBOL_REF)
11751 if (GET_CODE (rtl) == NOT)
11752 return false;
11753 return true;
11756 if (CONSTANT_POOL_ADDRESS_P (rtl))
11758 bool marked;
11759 get_pool_constant_mark (rtl, &marked);
11760 /* If all references to this pool constant were optimized away,
11761 it was not output and thus we can't represent it. */
11762 if (!marked)
11764 expansion_failed (NULL_TREE, rtl,
11765 "Constant was removed from constant pool.\n");
11766 return false;
11770 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11771 return false;
11773 /* Avoid references to external symbols in debug info, on several targets
11774 the linker might even refuse to link when linking a shared library,
11775 and in many other cases the relocations for .debug_info/.debug_loc are
11776 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
11777 to be defined within the same shared library or executable are fine. */
11778 if (SYMBOL_REF_EXTERNAL_P (rtl))
11780 tree decl = SYMBOL_REF_DECL (rtl);
11782 if (decl == NULL || !targetm.binds_local_p (decl))
11784 expansion_failed (NULL_TREE, rtl,
11785 "Symbol not defined in current TU.\n");
11786 return false;
11790 return true;
11793 /* Return true if constant RTL can be emitted in DW_OP_addr or
11794 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
11795 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
11797 static bool
11798 const_ok_for_output (rtx rtl)
11800 if (GET_CODE (rtl) == SYMBOL_REF)
11801 return const_ok_for_output_1 (rtl);
11803 if (GET_CODE (rtl) == CONST)
11805 subrtx_var_iterator::array_type array;
11806 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
11807 if (!const_ok_for_output_1 (*iter))
11808 return false;
11809 return true;
11812 return true;
11815 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
11816 if possible, NULL otherwise. */
11818 static dw_die_ref
11819 base_type_for_mode (machine_mode mode, bool unsignedp)
11821 dw_die_ref type_die;
11822 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11824 if (type == NULL)
11825 return NULL;
11826 switch (TREE_CODE (type))
11828 case INTEGER_TYPE:
11829 case REAL_TYPE:
11830 break;
11831 default:
11832 return NULL;
11834 type_die = lookup_type_die (type);
11835 if (!type_die)
11836 type_die = modified_type_die (type, TYPE_UNQUALIFIED, comp_unit_die ());
11837 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
11838 return NULL;
11839 return type_die;
11842 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
11843 type matching MODE, or, if MODE is narrower than or as wide as
11844 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
11845 possible. */
11847 static dw_loc_descr_ref
11848 convert_descriptor_to_mode (machine_mode mode, dw_loc_descr_ref op)
11850 machine_mode outer_mode = mode;
11851 dw_die_ref type_die;
11852 dw_loc_descr_ref cvt;
11854 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11856 add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
11857 return op;
11859 type_die = base_type_for_mode (outer_mode, 1);
11860 if (type_die == NULL)
11861 return NULL;
11862 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11863 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11864 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11865 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11866 add_loc_descr (&op, cvt);
11867 return op;
11870 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
11872 static dw_loc_descr_ref
11873 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
11874 dw_loc_descr_ref op1)
11876 dw_loc_descr_ref ret = op0;
11877 add_loc_descr (&ret, op1);
11878 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11879 if (STORE_FLAG_VALUE != 1)
11881 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
11882 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
11884 return ret;
11887 /* Return location descriptor for signed comparison OP RTL. */
11889 static dw_loc_descr_ref
11890 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11891 machine_mode mem_mode)
11893 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11894 dw_loc_descr_ref op0, op1;
11895 int shift;
11897 if (op_mode == VOIDmode)
11898 op_mode = GET_MODE (XEXP (rtl, 1));
11899 if (op_mode == VOIDmode)
11900 return NULL;
11902 if (dwarf_strict
11903 && (GET_MODE_CLASS (op_mode) != MODE_INT
11904 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
11905 return NULL;
11907 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11908 VAR_INIT_STATUS_INITIALIZED);
11909 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11910 VAR_INIT_STATUS_INITIALIZED);
11912 if (op0 == NULL || op1 == NULL)
11913 return NULL;
11915 if (GET_MODE_CLASS (op_mode) != MODE_INT
11916 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11917 return compare_loc_descriptor (op, op0, op1);
11919 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11921 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
11922 dw_loc_descr_ref cvt;
11924 if (type_die == NULL)
11925 return NULL;
11926 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11927 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11928 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11929 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11930 add_loc_descr (&op0, cvt);
11931 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11932 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11933 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11934 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11935 add_loc_descr (&op1, cvt);
11936 return compare_loc_descriptor (op, op0, op1);
11939 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
11940 /* For eq/ne, if the operands are known to be zero-extended,
11941 there is no need to do the fancy shifting up. */
11942 if (op == DW_OP_eq || op == DW_OP_ne)
11944 dw_loc_descr_ref last0, last1;
11945 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11947 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11949 /* deref_size zero extends, and for constants we can check
11950 whether they are zero extended or not. */
11951 if (((last0->dw_loc_opc == DW_OP_deref_size
11952 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11953 || (CONST_INT_P (XEXP (rtl, 0))
11954 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
11955 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
11956 && ((last1->dw_loc_opc == DW_OP_deref_size
11957 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11958 || (CONST_INT_P (XEXP (rtl, 1))
11959 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
11960 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
11961 return compare_loc_descriptor (op, op0, op1);
11963 /* EQ/NE comparison against constant in narrower type than
11964 DWARF2_ADDR_SIZE can be performed either as
11965 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
11966 DW_OP_{eq,ne}
11968 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
11969 DW_OP_{eq,ne}. Pick whatever is shorter. */
11970 if (CONST_INT_P (XEXP (rtl, 1))
11971 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
11972 && (size_of_int_loc_descriptor (shift) + 1
11973 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
11974 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
11975 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11976 & GET_MODE_MASK (op_mode))))
11978 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
11979 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11980 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11981 & GET_MODE_MASK (op_mode));
11982 return compare_loc_descriptor (op, op0, op1);
11985 add_loc_descr (&op0, int_loc_descriptor (shift));
11986 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11987 if (CONST_INT_P (XEXP (rtl, 1)))
11988 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
11989 else
11991 add_loc_descr (&op1, int_loc_descriptor (shift));
11992 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11994 return compare_loc_descriptor (op, op0, op1);
11997 /* Return location descriptor for unsigned comparison OP RTL. */
11999 static dw_loc_descr_ref
12000 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
12001 machine_mode mem_mode)
12003 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
12004 dw_loc_descr_ref op0, op1;
12006 if (op_mode == VOIDmode)
12007 op_mode = GET_MODE (XEXP (rtl, 1));
12008 if (op_mode == VOIDmode)
12009 return NULL;
12010 if (GET_MODE_CLASS (op_mode) != MODE_INT)
12011 return NULL;
12013 if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
12014 return NULL;
12016 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
12017 VAR_INIT_STATUS_INITIALIZED);
12018 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
12019 VAR_INIT_STATUS_INITIALIZED);
12021 if (op0 == NULL || op1 == NULL)
12022 return NULL;
12024 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
12026 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
12027 dw_loc_descr_ref last0, last1;
12028 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
12030 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
12032 if (CONST_INT_P (XEXP (rtl, 0)))
12033 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
12034 /* deref_size zero extends, so no need to mask it again. */
12035 else if (last0->dw_loc_opc != DW_OP_deref_size
12036 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
12038 add_loc_descr (&op0, int_loc_descriptor (mask));
12039 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
12041 if (CONST_INT_P (XEXP (rtl, 1)))
12042 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
12043 /* deref_size zero extends, so no need to mask it again. */
12044 else if (last1->dw_loc_opc != DW_OP_deref_size
12045 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
12047 add_loc_descr (&op1, int_loc_descriptor (mask));
12048 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
12051 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
12053 HOST_WIDE_INT bias = 1;
12054 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
12055 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12056 if (CONST_INT_P (XEXP (rtl, 1)))
12057 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
12058 + INTVAL (XEXP (rtl, 1)));
12059 else
12060 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
12061 bias, 0));
12063 return compare_loc_descriptor (op, op0, op1);
12066 /* Return location descriptor for {U,S}{MIN,MAX}. */
12068 static dw_loc_descr_ref
12069 minmax_loc_descriptor (rtx rtl, machine_mode mode,
12070 machine_mode mem_mode)
12072 enum dwarf_location_atom op;
12073 dw_loc_descr_ref op0, op1, ret;
12074 dw_loc_descr_ref bra_node, drop_node;
12076 if (dwarf_strict
12077 && (GET_MODE_CLASS (mode) != MODE_INT
12078 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
12079 return NULL;
12081 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12082 VAR_INIT_STATUS_INITIALIZED);
12083 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12084 VAR_INIT_STATUS_INITIALIZED);
12086 if (op0 == NULL || op1 == NULL)
12087 return NULL;
12089 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
12090 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
12091 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
12092 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
12094 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12096 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
12097 add_loc_descr (&op0, int_loc_descriptor (mask));
12098 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
12099 add_loc_descr (&op1, int_loc_descriptor (mask));
12100 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
12102 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12104 HOST_WIDE_INT bias = 1;
12105 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
12106 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12107 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12110 else if (GET_MODE_CLASS (mode) == MODE_INT
12111 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12113 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
12114 add_loc_descr (&op0, int_loc_descriptor (shift));
12115 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
12116 add_loc_descr (&op1, int_loc_descriptor (shift));
12117 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
12119 else if (GET_MODE_CLASS (mode) == MODE_INT
12120 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12122 dw_die_ref type_die = base_type_for_mode (mode, 0);
12123 dw_loc_descr_ref cvt;
12124 if (type_die == NULL)
12125 return NULL;
12126 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12127 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12128 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12129 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12130 add_loc_descr (&op0, cvt);
12131 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12132 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12133 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12134 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12135 add_loc_descr (&op1, cvt);
12138 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
12139 op = DW_OP_lt;
12140 else
12141 op = DW_OP_gt;
12142 ret = op0;
12143 add_loc_descr (&ret, op1);
12144 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
12145 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12146 add_loc_descr (&ret, bra_node);
12147 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12148 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
12149 add_loc_descr (&ret, drop_node);
12150 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12151 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
12152 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
12153 && GET_MODE_CLASS (mode) == MODE_INT
12154 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12155 ret = convert_descriptor_to_mode (mode, ret);
12156 return ret;
12159 /* Helper function for mem_loc_descriptor. Perform OP binary op,
12160 but after converting arguments to type_die, afterwards
12161 convert back to unsigned. */
12163 static dw_loc_descr_ref
12164 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
12165 machine_mode mode, machine_mode mem_mode)
12167 dw_loc_descr_ref cvt, op0, op1;
12169 if (type_die == NULL)
12170 return NULL;
12171 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12172 VAR_INIT_STATUS_INITIALIZED);
12173 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12174 VAR_INIT_STATUS_INITIALIZED);
12175 if (op0 == NULL || op1 == NULL)
12176 return NULL;
12177 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12178 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12179 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12180 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12181 add_loc_descr (&op0, cvt);
12182 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12183 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12184 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12185 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12186 add_loc_descr (&op1, cvt);
12187 add_loc_descr (&op0, op1);
12188 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
12189 return convert_descriptor_to_mode (mode, op0);
12192 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
12193 const0 is DW_OP_lit0 or corresponding typed constant,
12194 const1 is DW_OP_lit1 or corresponding typed constant
12195 and constMSB is constant with just the MSB bit set
12196 for the mode):
12197 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
12198 L1: const0 DW_OP_swap
12199 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
12200 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12201 L3: DW_OP_drop
12202 L4: DW_OP_nop
12204 CTZ is similar:
12205 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
12206 L1: const0 DW_OP_swap
12207 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
12208 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12209 L3: DW_OP_drop
12210 L4: DW_OP_nop
12212 FFS is similar:
12213 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
12214 L1: const1 DW_OP_swap
12215 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
12216 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12217 L3: DW_OP_drop
12218 L4: DW_OP_nop */
12220 static dw_loc_descr_ref
12221 clz_loc_descriptor (rtx rtl, machine_mode mode,
12222 machine_mode mem_mode)
12224 dw_loc_descr_ref op0, ret, tmp;
12225 HOST_WIDE_INT valv;
12226 dw_loc_descr_ref l1jump, l1label;
12227 dw_loc_descr_ref l2jump, l2label;
12228 dw_loc_descr_ref l3jump, l3label;
12229 dw_loc_descr_ref l4jump, l4label;
12230 rtx msb;
12232 if (GET_MODE_CLASS (mode) != MODE_INT
12233 || GET_MODE (XEXP (rtl, 0)) != mode)
12234 return NULL;
12236 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12237 VAR_INIT_STATUS_INITIALIZED);
12238 if (op0 == NULL)
12239 return NULL;
12240 ret = op0;
12241 if (GET_CODE (rtl) == CLZ)
12243 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12244 valv = GET_MODE_BITSIZE (mode);
12246 else if (GET_CODE (rtl) == FFS)
12247 valv = 0;
12248 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12249 valv = GET_MODE_BITSIZE (mode);
12250 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12251 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
12252 add_loc_descr (&ret, l1jump);
12253 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12254 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
12255 VAR_INIT_STATUS_INITIALIZED);
12256 if (tmp == NULL)
12257 return NULL;
12258 add_loc_descr (&ret, tmp);
12259 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
12260 add_loc_descr (&ret, l4jump);
12261 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
12262 ? const1_rtx : const0_rtx,
12263 mode, mem_mode,
12264 VAR_INIT_STATUS_INITIALIZED);
12265 if (l1label == NULL)
12266 return NULL;
12267 add_loc_descr (&ret, l1label);
12268 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12269 l2label = new_loc_descr (DW_OP_dup, 0, 0);
12270 add_loc_descr (&ret, l2label);
12271 if (GET_CODE (rtl) != CLZ)
12272 msb = const1_rtx;
12273 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
12274 msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
12275 << (GET_MODE_BITSIZE (mode) - 1));
12276 else
12277 msb = immed_wide_int_const
12278 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
12279 GET_MODE_PRECISION (mode)), mode);
12280 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
12281 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12282 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
12283 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
12284 else
12285 tmp = mem_loc_descriptor (msb, mode, mem_mode,
12286 VAR_INIT_STATUS_INITIALIZED);
12287 if (tmp == NULL)
12288 return NULL;
12289 add_loc_descr (&ret, tmp);
12290 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12291 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
12292 add_loc_descr (&ret, l3jump);
12293 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12294 VAR_INIT_STATUS_INITIALIZED);
12295 if (tmp == NULL)
12296 return NULL;
12297 add_loc_descr (&ret, tmp);
12298 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
12299 ? DW_OP_shl : DW_OP_shr, 0, 0));
12300 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12301 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
12302 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12303 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
12304 add_loc_descr (&ret, l2jump);
12305 l3label = new_loc_descr (DW_OP_drop, 0, 0);
12306 add_loc_descr (&ret, l3label);
12307 l4label = new_loc_descr (DW_OP_nop, 0, 0);
12308 add_loc_descr (&ret, l4label);
12309 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12310 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12311 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12312 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12313 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12314 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
12315 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12316 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
12317 return ret;
12320 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
12321 const1 is DW_OP_lit1 or corresponding typed constant):
12322 const0 DW_OP_swap
12323 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
12324 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
12325 L2: DW_OP_drop
12327 PARITY is similar:
12328 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
12329 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
12330 L2: DW_OP_drop */
12332 static dw_loc_descr_ref
12333 popcount_loc_descriptor (rtx rtl, machine_mode mode,
12334 machine_mode mem_mode)
12336 dw_loc_descr_ref op0, ret, tmp;
12337 dw_loc_descr_ref l1jump, l1label;
12338 dw_loc_descr_ref l2jump, l2label;
12340 if (GET_MODE_CLASS (mode) != MODE_INT
12341 || GET_MODE (XEXP (rtl, 0)) != mode)
12342 return NULL;
12344 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12345 VAR_INIT_STATUS_INITIALIZED);
12346 if (op0 == NULL)
12347 return NULL;
12348 ret = op0;
12349 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12350 VAR_INIT_STATUS_INITIALIZED);
12351 if (tmp == NULL)
12352 return NULL;
12353 add_loc_descr (&ret, tmp);
12354 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12355 l1label = new_loc_descr (DW_OP_dup, 0, 0);
12356 add_loc_descr (&ret, l1label);
12357 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12358 add_loc_descr (&ret, l2jump);
12359 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12360 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12361 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12362 VAR_INIT_STATUS_INITIALIZED);
12363 if (tmp == NULL)
12364 return NULL;
12365 add_loc_descr (&ret, tmp);
12366 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12367 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
12368 ? DW_OP_plus : DW_OP_xor, 0, 0));
12369 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12370 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12371 VAR_INIT_STATUS_INITIALIZED);
12372 add_loc_descr (&ret, tmp);
12373 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12374 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12375 add_loc_descr (&ret, l1jump);
12376 l2label = new_loc_descr (DW_OP_drop, 0, 0);
12377 add_loc_descr (&ret, l2label);
12378 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12379 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12380 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12381 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12382 return ret;
12385 /* BSWAP (constS is initial shift count, either 56 or 24):
12386 constS const0
12387 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
12388 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
12389 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
12390 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
12391 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
12393 static dw_loc_descr_ref
12394 bswap_loc_descriptor (rtx rtl, machine_mode mode,
12395 machine_mode mem_mode)
12397 dw_loc_descr_ref op0, ret, tmp;
12398 dw_loc_descr_ref l1jump, l1label;
12399 dw_loc_descr_ref l2jump, l2label;
12401 if (GET_MODE_CLASS (mode) != MODE_INT
12402 || BITS_PER_UNIT != 8
12403 || (GET_MODE_BITSIZE (mode) != 32
12404 && GET_MODE_BITSIZE (mode) != 64))
12405 return NULL;
12407 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12408 VAR_INIT_STATUS_INITIALIZED);
12409 if (op0 == NULL)
12410 return NULL;
12412 ret = op0;
12413 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12414 mode, mem_mode,
12415 VAR_INIT_STATUS_INITIALIZED);
12416 if (tmp == NULL)
12417 return NULL;
12418 add_loc_descr (&ret, tmp);
12419 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12420 VAR_INIT_STATUS_INITIALIZED);
12421 if (tmp == NULL)
12422 return NULL;
12423 add_loc_descr (&ret, tmp);
12424 l1label = new_loc_descr (DW_OP_pick, 2, 0);
12425 add_loc_descr (&ret, l1label);
12426 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12427 mode, mem_mode,
12428 VAR_INIT_STATUS_INITIALIZED);
12429 add_loc_descr (&ret, tmp);
12430 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
12431 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12432 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12433 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
12434 VAR_INIT_STATUS_INITIALIZED);
12435 if (tmp == NULL)
12436 return NULL;
12437 add_loc_descr (&ret, tmp);
12438 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12439 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
12440 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12441 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12442 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12443 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12444 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12445 VAR_INIT_STATUS_INITIALIZED);
12446 add_loc_descr (&ret, tmp);
12447 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
12448 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12449 add_loc_descr (&ret, l2jump);
12450 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
12451 VAR_INIT_STATUS_INITIALIZED);
12452 add_loc_descr (&ret, tmp);
12453 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12454 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12455 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12456 add_loc_descr (&ret, l1jump);
12457 l2label = new_loc_descr (DW_OP_drop, 0, 0);
12458 add_loc_descr (&ret, l2label);
12459 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12460 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12461 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12462 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12463 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12464 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12465 return ret;
12468 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
12469 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12470 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
12471 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
12473 ROTATERT is similar:
12474 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
12475 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12476 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
12478 static dw_loc_descr_ref
12479 rotate_loc_descriptor (rtx rtl, machine_mode mode,
12480 machine_mode mem_mode)
12482 rtx rtlop1 = XEXP (rtl, 1);
12483 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
12484 int i;
12486 if (GET_MODE_CLASS (mode) != MODE_INT)
12487 return NULL;
12489 if (GET_MODE (rtlop1) != VOIDmode
12490 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
12491 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12492 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12493 VAR_INIT_STATUS_INITIALIZED);
12494 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12495 VAR_INIT_STATUS_INITIALIZED);
12496 if (op0 == NULL || op1 == NULL)
12497 return NULL;
12498 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12499 for (i = 0; i < 2; i++)
12501 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
12502 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
12503 mode, mem_mode,
12504 VAR_INIT_STATUS_INITIALIZED);
12505 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12506 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12507 ? DW_OP_const4u
12508 : HOST_BITS_PER_WIDE_INT == 64
12509 ? DW_OP_const8u : DW_OP_constu,
12510 GET_MODE_MASK (mode), 0);
12511 else
12512 mask[i] = NULL;
12513 if (mask[i] == NULL)
12514 return NULL;
12515 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
12517 ret = op0;
12518 add_loc_descr (&ret, op1);
12519 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12520 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12521 if (GET_CODE (rtl) == ROTATERT)
12523 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12524 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12525 GET_MODE_BITSIZE (mode), 0));
12527 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12528 if (mask[0] != NULL)
12529 add_loc_descr (&ret, mask[0]);
12530 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12531 if (mask[1] != NULL)
12533 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12534 add_loc_descr (&ret, mask[1]);
12535 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12537 if (GET_CODE (rtl) == ROTATE)
12539 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12540 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12541 GET_MODE_BITSIZE (mode), 0));
12543 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12544 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12545 return ret;
12548 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
12549 for DEBUG_PARAMETER_REF RTL. */
12551 static dw_loc_descr_ref
12552 parameter_ref_descriptor (rtx rtl)
12554 dw_loc_descr_ref ret;
12555 dw_die_ref ref;
12557 if (dwarf_strict)
12558 return NULL;
12559 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
12560 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
12561 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
12562 if (ref)
12564 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12565 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
12566 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
12568 else
12570 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
12571 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
12573 return ret;
12576 /* The following routine converts the RTL for a variable or parameter
12577 (resident in memory) into an equivalent Dwarf representation of a
12578 mechanism for getting the address of that same variable onto the top of a
12579 hypothetical "address evaluation" stack.
12581 When creating memory location descriptors, we are effectively transforming
12582 the RTL for a memory-resident object into its Dwarf postfix expression
12583 equivalent. This routine recursively descends an RTL tree, turning
12584 it into Dwarf postfix code as it goes.
12586 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
12588 MEM_MODE is the mode of the memory reference, needed to handle some
12589 autoincrement addressing modes.
12591 Return 0 if we can't represent the location. */
12593 dw_loc_descr_ref
12594 mem_loc_descriptor (rtx rtl, machine_mode mode,
12595 machine_mode mem_mode,
12596 enum var_init_status initialized)
12598 dw_loc_descr_ref mem_loc_result = NULL;
12599 enum dwarf_location_atom op;
12600 dw_loc_descr_ref op0, op1;
12601 rtx inner = NULL_RTX;
12603 if (mode == VOIDmode)
12604 mode = GET_MODE (rtl);
12606 /* Note that for a dynamically sized array, the location we will generate a
12607 description of here will be the lowest numbered location which is
12608 actually within the array. That's *not* necessarily the same as the
12609 zeroth element of the array. */
12611 rtl = targetm.delegitimize_address (rtl);
12613 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
12614 return NULL;
12616 switch (GET_CODE (rtl))
12618 case POST_INC:
12619 case POST_DEC:
12620 case POST_MODIFY:
12621 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
12623 case SUBREG:
12624 /* The case of a subreg may arise when we have a local (register)
12625 variable or a formal (register) parameter which doesn't quite fill
12626 up an entire register. For now, just assume that it is
12627 legitimate to make the Dwarf info refer to the whole register which
12628 contains the given subreg. */
12629 if (!subreg_lowpart_p (rtl))
12630 break;
12631 inner = SUBREG_REG (rtl);
12632 case TRUNCATE:
12633 if (inner == NULL_RTX)
12634 inner = XEXP (rtl, 0);
12635 if (GET_MODE_CLASS (mode) == MODE_INT
12636 && GET_MODE_CLASS (GET_MODE (inner)) == MODE_INT
12637 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12638 #ifdef POINTERS_EXTEND_UNSIGNED
12639 || (mode == Pmode && mem_mode != VOIDmode)
12640 #endif
12642 && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
12644 mem_loc_result = mem_loc_descriptor (inner,
12645 GET_MODE (inner),
12646 mem_mode, initialized);
12647 break;
12649 if (dwarf_strict)
12650 break;
12651 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
12652 break;
12653 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
12654 && (GET_MODE_CLASS (mode) != MODE_INT
12655 || GET_MODE_CLASS (GET_MODE (inner)) != MODE_INT))
12656 break;
12657 else
12659 dw_die_ref type_die;
12660 dw_loc_descr_ref cvt;
12662 mem_loc_result = mem_loc_descriptor (inner,
12663 GET_MODE (inner),
12664 mem_mode, initialized);
12665 if (mem_loc_result == NULL)
12666 break;
12667 type_die = base_type_for_mode (mode,
12668 GET_MODE_CLASS (mode) == MODE_INT);
12669 if (type_die == NULL)
12671 mem_loc_result = NULL;
12672 break;
12674 if (GET_MODE_SIZE (mode)
12675 != GET_MODE_SIZE (GET_MODE (inner)))
12676 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12677 else
12678 cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
12679 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12680 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12681 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12682 add_loc_descr (&mem_loc_result, cvt);
12684 break;
12686 case REG:
12687 if (GET_MODE_CLASS (mode) != MODE_INT
12688 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12689 && rtl != arg_pointer_rtx
12690 && rtl != frame_pointer_rtx
12691 #ifdef POINTERS_EXTEND_UNSIGNED
12692 && (mode != Pmode || mem_mode == VOIDmode)
12693 #endif
12696 dw_die_ref type_die;
12697 unsigned int dbx_regnum;
12699 if (dwarf_strict)
12700 break;
12701 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
12702 break;
12703 type_die = base_type_for_mode (mode,
12704 GET_MODE_CLASS (mode) == MODE_INT);
12705 if (type_die == NULL)
12706 break;
12708 dbx_regnum = dbx_reg_number (rtl);
12709 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12710 break;
12711 mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
12712 dbx_regnum, 0);
12713 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12714 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12715 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
12716 break;
12718 /* Whenever a register number forms a part of the description of the
12719 method for calculating the (dynamic) address of a memory resident
12720 object, DWARF rules require the register number be referred to as
12721 a "base register". This distinction is not based in any way upon
12722 what category of register the hardware believes the given register
12723 belongs to. This is strictly DWARF terminology we're dealing with
12724 here. Note that in cases where the location of a memory-resident
12725 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
12726 OP_CONST (0)) the actual DWARF location descriptor that we generate
12727 may just be OP_BASEREG (basereg). This may look deceptively like
12728 the object in question was allocated to a register (rather than in
12729 memory) so DWARF consumers need to be aware of the subtle
12730 distinction between OP_REG and OP_BASEREG. */
12731 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
12732 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
12733 else if (stack_realign_drap
12734 && crtl->drap_reg
12735 && crtl->args.internal_arg_pointer == rtl
12736 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
12738 /* If RTL is internal_arg_pointer, which has been optimized
12739 out, use DRAP instead. */
12740 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
12741 VAR_INIT_STATUS_INITIALIZED);
12743 break;
12745 case SIGN_EXTEND:
12746 case ZERO_EXTEND:
12747 if (GET_MODE_CLASS (mode) != MODE_INT)
12748 break;
12749 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12750 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12751 if (op0 == 0)
12752 break;
12753 else if (GET_CODE (rtl) == ZERO_EXTEND
12754 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12755 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12756 < HOST_BITS_PER_WIDE_INT
12757 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
12758 to expand zero extend as two shifts instead of
12759 masking. */
12760 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
12762 machine_mode imode = GET_MODE (XEXP (rtl, 0));
12763 mem_loc_result = op0;
12764 add_loc_descr (&mem_loc_result,
12765 int_loc_descriptor (GET_MODE_MASK (imode)));
12766 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
12768 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12770 int shift = DWARF2_ADDR_SIZE
12771 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
12772 shift *= BITS_PER_UNIT;
12773 if (GET_CODE (rtl) == SIGN_EXTEND)
12774 op = DW_OP_shra;
12775 else
12776 op = DW_OP_shr;
12777 mem_loc_result = op0;
12778 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12779 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12780 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12781 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12783 else if (!dwarf_strict)
12785 dw_die_ref type_die1, type_die2;
12786 dw_loc_descr_ref cvt;
12788 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12789 GET_CODE (rtl) == ZERO_EXTEND);
12790 if (type_die1 == NULL)
12791 break;
12792 type_die2 = base_type_for_mode (mode, 1);
12793 if (type_die2 == NULL)
12794 break;
12795 mem_loc_result = op0;
12796 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12797 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12798 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
12799 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12800 add_loc_descr (&mem_loc_result, cvt);
12801 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12802 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12803 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
12804 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12805 add_loc_descr (&mem_loc_result, cvt);
12807 break;
12809 case MEM:
12811 rtx new_rtl = avoid_constant_pool_reference (rtl);
12812 if (new_rtl != rtl)
12814 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
12815 initialized);
12816 if (mem_loc_result != NULL)
12817 return mem_loc_result;
12820 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
12821 get_address_mode (rtl), mode,
12822 VAR_INIT_STATUS_INITIALIZED);
12823 if (mem_loc_result == NULL)
12824 mem_loc_result = tls_mem_loc_descriptor (rtl);
12825 if (mem_loc_result != NULL)
12827 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12828 || GET_MODE_CLASS (mode) != MODE_INT)
12830 dw_die_ref type_die;
12831 dw_loc_descr_ref deref;
12833 if (dwarf_strict)
12834 return NULL;
12835 type_die
12836 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
12837 if (type_die == NULL)
12838 return NULL;
12839 deref = new_loc_descr (DW_OP_GNU_deref_type,
12840 GET_MODE_SIZE (mode), 0);
12841 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12842 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12843 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
12844 add_loc_descr (&mem_loc_result, deref);
12846 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12847 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
12848 else
12849 add_loc_descr (&mem_loc_result,
12850 new_loc_descr (DW_OP_deref_size,
12851 GET_MODE_SIZE (mode), 0));
12853 break;
12855 case LO_SUM:
12856 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
12858 case LABEL_REF:
12859 /* Some ports can transform a symbol ref into a label ref, because
12860 the symbol ref is too far away and has to be dumped into a constant
12861 pool. */
12862 case CONST:
12863 case SYMBOL_REF:
12864 if ((GET_MODE_CLASS (mode) != MODE_INT
12865 && GET_MODE_CLASS (mode) != MODE_PARTIAL_INT)
12866 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12867 #ifdef POINTERS_EXTEND_UNSIGNED
12868 && (mode != Pmode || mem_mode == VOIDmode)
12869 #endif
12871 break;
12872 if (GET_CODE (rtl) == SYMBOL_REF
12873 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12875 dw_loc_descr_ref temp;
12877 /* If this is not defined, we have no way to emit the data. */
12878 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
12879 break;
12881 temp = new_addr_loc_descr (rtl, dtprel_true);
12883 mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
12884 add_loc_descr (&mem_loc_result, temp);
12886 break;
12889 if (!const_ok_for_output (rtl))
12891 if (GET_CODE (rtl) == CONST)
12892 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12893 initialized);
12894 break;
12897 symref:
12898 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
12899 vec_safe_push (used_rtx_array, rtl);
12900 break;
12902 case CONCAT:
12903 case CONCATN:
12904 case VAR_LOCATION:
12905 case DEBUG_IMPLICIT_PTR:
12906 expansion_failed (NULL_TREE, rtl,
12907 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
12908 return 0;
12910 case ENTRY_VALUE:
12911 if (dwarf_strict)
12912 return NULL;
12913 if (REG_P (ENTRY_VALUE_EXP (rtl)))
12915 if (GET_MODE_CLASS (mode) != MODE_INT
12916 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12917 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12918 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12919 else
12921 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
12922 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12923 return NULL;
12924 op0 = one_reg_loc_descriptor (dbx_regnum,
12925 VAR_INIT_STATUS_INITIALIZED);
12928 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
12929 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
12931 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12932 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12933 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
12934 return NULL;
12936 else
12937 gcc_unreachable ();
12938 if (op0 == NULL)
12939 return NULL;
12940 mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
12941 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
12942 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
12943 break;
12945 case DEBUG_PARAMETER_REF:
12946 mem_loc_result = parameter_ref_descriptor (rtl);
12947 break;
12949 case PRE_MODIFY:
12950 /* Extract the PLUS expression nested inside and fall into
12951 PLUS code below. */
12952 rtl = XEXP (rtl, 1);
12953 goto plus;
12955 case PRE_INC:
12956 case PRE_DEC:
12957 /* Turn these into a PLUS expression and fall into the PLUS code
12958 below. */
12959 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
12960 gen_int_mode (GET_CODE (rtl) == PRE_INC
12961 ? GET_MODE_UNIT_SIZE (mem_mode)
12962 : -GET_MODE_UNIT_SIZE (mem_mode),
12963 mode));
12965 /* ... fall through ... */
12967 case PLUS:
12968 plus:
12969 if (is_based_loc (rtl)
12970 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12971 || XEXP (rtl, 0) == arg_pointer_rtx
12972 || XEXP (rtl, 0) == frame_pointer_rtx)
12973 && GET_MODE_CLASS (mode) == MODE_INT)
12974 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
12975 INTVAL (XEXP (rtl, 1)),
12976 VAR_INIT_STATUS_INITIALIZED);
12977 else
12979 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12980 VAR_INIT_STATUS_INITIALIZED);
12981 if (mem_loc_result == 0)
12982 break;
12984 if (CONST_INT_P (XEXP (rtl, 1))
12985 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12986 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
12987 else
12989 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12990 VAR_INIT_STATUS_INITIALIZED);
12991 if (op1 == 0)
12992 return NULL;
12993 add_loc_descr (&mem_loc_result, op1);
12994 add_loc_descr (&mem_loc_result,
12995 new_loc_descr (DW_OP_plus, 0, 0));
12998 break;
13000 /* If a pseudo-reg is optimized away, it is possible for it to
13001 be replaced with a MEM containing a multiply or shift. */
13002 case MINUS:
13003 op = DW_OP_minus;
13004 goto do_binop;
13006 case MULT:
13007 op = DW_OP_mul;
13008 goto do_binop;
13010 case DIV:
13011 if (!dwarf_strict
13012 && GET_MODE_CLASS (mode) == MODE_INT
13013 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
13015 mem_loc_result = typed_binop (DW_OP_div, rtl,
13016 base_type_for_mode (mode, 0),
13017 mode, mem_mode);
13018 break;
13020 op = DW_OP_div;
13021 goto do_binop;
13023 case UMOD:
13024 op = DW_OP_mod;
13025 goto do_binop;
13027 case ASHIFT:
13028 op = DW_OP_shl;
13029 goto do_shift;
13031 case ASHIFTRT:
13032 op = DW_OP_shra;
13033 goto do_shift;
13035 case LSHIFTRT:
13036 op = DW_OP_shr;
13037 goto do_shift;
13039 do_shift:
13040 if (GET_MODE_CLASS (mode) != MODE_INT)
13041 break;
13042 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13043 VAR_INIT_STATUS_INITIALIZED);
13045 rtx rtlop1 = XEXP (rtl, 1);
13046 if (GET_MODE (rtlop1) != VOIDmode
13047 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
13048 < GET_MODE_BITSIZE (mode))
13049 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
13050 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
13051 VAR_INIT_STATUS_INITIALIZED);
13054 if (op0 == 0 || op1 == 0)
13055 break;
13057 mem_loc_result = op0;
13058 add_loc_descr (&mem_loc_result, op1);
13059 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13060 break;
13062 case AND:
13063 op = DW_OP_and;
13064 goto do_binop;
13066 case IOR:
13067 op = DW_OP_or;
13068 goto do_binop;
13070 case XOR:
13071 op = DW_OP_xor;
13072 goto do_binop;
13074 do_binop:
13075 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13076 VAR_INIT_STATUS_INITIALIZED);
13077 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13078 VAR_INIT_STATUS_INITIALIZED);
13080 if (op0 == 0 || op1 == 0)
13081 break;
13083 mem_loc_result = op0;
13084 add_loc_descr (&mem_loc_result, op1);
13085 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13086 break;
13088 case MOD:
13089 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
13091 mem_loc_result = typed_binop (DW_OP_mod, rtl,
13092 base_type_for_mode (mode, 0),
13093 mode, mem_mode);
13094 break;
13097 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13098 VAR_INIT_STATUS_INITIALIZED);
13099 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13100 VAR_INIT_STATUS_INITIALIZED);
13102 if (op0 == 0 || op1 == 0)
13103 break;
13105 mem_loc_result = op0;
13106 add_loc_descr (&mem_loc_result, op1);
13107 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13108 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13109 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
13110 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
13111 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
13112 break;
13114 case UDIV:
13115 if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
13117 if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
13119 op = DW_OP_div;
13120 goto do_binop;
13122 mem_loc_result = typed_binop (DW_OP_div, rtl,
13123 base_type_for_mode (mode, 1),
13124 mode, mem_mode);
13126 break;
13128 case NOT:
13129 op = DW_OP_not;
13130 goto do_unop;
13132 case ABS:
13133 op = DW_OP_abs;
13134 goto do_unop;
13136 case NEG:
13137 op = DW_OP_neg;
13138 goto do_unop;
13140 do_unop:
13141 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13142 VAR_INIT_STATUS_INITIALIZED);
13144 if (op0 == 0)
13145 break;
13147 mem_loc_result = op0;
13148 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13149 break;
13151 case CONST_INT:
13152 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13153 #ifdef POINTERS_EXTEND_UNSIGNED
13154 || (mode == Pmode
13155 && mem_mode != VOIDmode
13156 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
13157 #endif
13160 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13161 break;
13163 if (!dwarf_strict
13164 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
13165 || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
13167 dw_die_ref type_die = base_type_for_mode (mode, 1);
13168 machine_mode amode;
13169 if (type_die == NULL)
13170 return NULL;
13171 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
13172 MODE_INT, 0);
13173 if (INTVAL (rtl) >= 0
13174 && amode != BLKmode
13175 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
13176 /* const DW_OP_GNU_convert <XXX> vs.
13177 DW_OP_GNU_const_type <XXX, 1, const>. */
13178 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
13179 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
13181 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13182 op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13183 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13184 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13185 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
13186 add_loc_descr (&mem_loc_result, op0);
13187 return mem_loc_result;
13189 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
13190 INTVAL (rtl));
13191 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13192 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13193 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13194 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
13195 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13196 else
13198 mem_loc_result->dw_loc_oprnd2.val_class
13199 = dw_val_class_const_double;
13200 mem_loc_result->dw_loc_oprnd2.v.val_double
13201 = double_int::from_shwi (INTVAL (rtl));
13204 break;
13206 case CONST_DOUBLE:
13207 if (!dwarf_strict)
13209 dw_die_ref type_die;
13211 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
13212 CONST_DOUBLE rtx could represent either a large integer
13213 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
13214 the value is always a floating point constant.
13216 When it is an integer, a CONST_DOUBLE is used whenever
13217 the constant requires 2 HWIs to be adequately represented.
13218 We output CONST_DOUBLEs as blocks. */
13219 if (mode == VOIDmode
13220 || (GET_MODE (rtl) == VOIDmode
13221 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
13222 break;
13223 type_die = base_type_for_mode (mode,
13224 GET_MODE_CLASS (mode) == MODE_INT);
13225 if (type_die == NULL)
13226 return NULL;
13227 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
13228 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13229 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13230 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13231 #if TARGET_SUPPORTS_WIDE_INT == 0
13232 if (!SCALAR_FLOAT_MODE_P (mode))
13234 mem_loc_result->dw_loc_oprnd2.val_class
13235 = dw_val_class_const_double;
13236 mem_loc_result->dw_loc_oprnd2.v.val_double
13237 = rtx_to_double_int (rtl);
13239 else
13240 #endif
13242 unsigned int length = GET_MODE_SIZE (mode);
13243 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
13245 insert_float (rtl, array);
13246 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13247 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13248 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13249 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13252 break;
13254 case CONST_WIDE_INT:
13255 if (!dwarf_strict)
13257 dw_die_ref type_die;
13259 type_die = base_type_for_mode (mode,
13260 GET_MODE_CLASS (mode) == MODE_INT);
13261 if (type_die == NULL)
13262 return NULL;
13263 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
13264 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13265 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13266 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13267 mem_loc_result->dw_loc_oprnd2.val_class
13268 = dw_val_class_wide_int;
13269 mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
13270 *mem_loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
13272 break;
13274 case EQ:
13275 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
13276 break;
13278 case GE:
13279 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
13280 break;
13282 case GT:
13283 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
13284 break;
13286 case LE:
13287 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
13288 break;
13290 case LT:
13291 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
13292 break;
13294 case NE:
13295 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
13296 break;
13298 case GEU:
13299 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
13300 break;
13302 case GTU:
13303 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
13304 break;
13306 case LEU:
13307 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
13308 break;
13310 case LTU:
13311 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
13312 break;
13314 case UMIN:
13315 case UMAX:
13316 if (GET_MODE_CLASS (mode) != MODE_INT)
13317 break;
13318 /* FALLTHRU */
13319 case SMIN:
13320 case SMAX:
13321 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
13322 break;
13324 case ZERO_EXTRACT:
13325 case SIGN_EXTRACT:
13326 if (CONST_INT_P (XEXP (rtl, 1))
13327 && CONST_INT_P (XEXP (rtl, 2))
13328 && ((unsigned) INTVAL (XEXP (rtl, 1))
13329 + (unsigned) INTVAL (XEXP (rtl, 2))
13330 <= GET_MODE_BITSIZE (mode))
13331 && GET_MODE_CLASS (mode) == MODE_INT
13332 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13333 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
13335 int shift, size;
13336 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13337 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13338 if (op0 == 0)
13339 break;
13340 if (GET_CODE (rtl) == SIGN_EXTRACT)
13341 op = DW_OP_shra;
13342 else
13343 op = DW_OP_shr;
13344 mem_loc_result = op0;
13345 size = INTVAL (XEXP (rtl, 1));
13346 shift = INTVAL (XEXP (rtl, 2));
13347 if (BITS_BIG_ENDIAN)
13348 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
13349 - shift - size;
13350 if (shift + size != (int) DWARF2_ADDR_SIZE)
13352 add_loc_descr (&mem_loc_result,
13353 int_loc_descriptor (DWARF2_ADDR_SIZE
13354 - shift - size));
13355 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13357 if (size != (int) DWARF2_ADDR_SIZE)
13359 add_loc_descr (&mem_loc_result,
13360 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
13361 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13364 break;
13366 case IF_THEN_ELSE:
13368 dw_loc_descr_ref op2, bra_node, drop_node;
13369 op0 = mem_loc_descriptor (XEXP (rtl, 0),
13370 GET_MODE (XEXP (rtl, 0)) == VOIDmode
13371 ? word_mode : GET_MODE (XEXP (rtl, 0)),
13372 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13373 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13374 VAR_INIT_STATUS_INITIALIZED);
13375 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
13376 VAR_INIT_STATUS_INITIALIZED);
13377 if (op0 == NULL || op1 == NULL || op2 == NULL)
13378 break;
13380 mem_loc_result = op1;
13381 add_loc_descr (&mem_loc_result, op2);
13382 add_loc_descr (&mem_loc_result, op0);
13383 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13384 add_loc_descr (&mem_loc_result, bra_node);
13385 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
13386 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
13387 add_loc_descr (&mem_loc_result, drop_node);
13388 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13389 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
13391 break;
13393 case FLOAT_EXTEND:
13394 case FLOAT_TRUNCATE:
13395 case FLOAT:
13396 case UNSIGNED_FLOAT:
13397 case FIX:
13398 case UNSIGNED_FIX:
13399 if (!dwarf_strict)
13401 dw_die_ref type_die;
13402 dw_loc_descr_ref cvt;
13404 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13405 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13406 if (op0 == NULL)
13407 break;
13408 if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
13409 && (GET_CODE (rtl) == FLOAT
13410 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
13411 <= DWARF2_ADDR_SIZE))
13413 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
13414 GET_CODE (rtl) == UNSIGNED_FLOAT);
13415 if (type_die == NULL)
13416 break;
13417 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13418 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13419 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13420 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13421 add_loc_descr (&op0, cvt);
13423 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
13424 if (type_die == NULL)
13425 break;
13426 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13427 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13428 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13429 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13430 add_loc_descr (&op0, cvt);
13431 if (GET_MODE_CLASS (mode) == MODE_INT
13432 && (GET_CODE (rtl) == FIX
13433 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
13435 op0 = convert_descriptor_to_mode (mode, op0);
13436 if (op0 == NULL)
13437 break;
13439 mem_loc_result = op0;
13441 break;
13443 case CLZ:
13444 case CTZ:
13445 case FFS:
13446 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
13447 break;
13449 case POPCOUNT:
13450 case PARITY:
13451 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
13452 break;
13454 case BSWAP:
13455 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
13456 break;
13458 case ROTATE:
13459 case ROTATERT:
13460 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
13461 break;
13463 case COMPARE:
13464 /* In theory, we could implement the above. */
13465 /* DWARF cannot represent the unsigned compare operations
13466 natively. */
13467 case SS_MULT:
13468 case US_MULT:
13469 case SS_DIV:
13470 case US_DIV:
13471 case SS_PLUS:
13472 case US_PLUS:
13473 case SS_MINUS:
13474 case US_MINUS:
13475 case SS_NEG:
13476 case US_NEG:
13477 case SS_ABS:
13478 case SS_ASHIFT:
13479 case US_ASHIFT:
13480 case SS_TRUNCATE:
13481 case US_TRUNCATE:
13482 case UNORDERED:
13483 case ORDERED:
13484 case UNEQ:
13485 case UNGE:
13486 case UNGT:
13487 case UNLE:
13488 case UNLT:
13489 case LTGT:
13490 case FRACT_CONVERT:
13491 case UNSIGNED_FRACT_CONVERT:
13492 case SAT_FRACT:
13493 case UNSIGNED_SAT_FRACT:
13494 case SQRT:
13495 case ASM_OPERANDS:
13496 case VEC_MERGE:
13497 case VEC_SELECT:
13498 case VEC_CONCAT:
13499 case VEC_DUPLICATE:
13500 case UNSPEC:
13501 case HIGH:
13502 case FMA:
13503 case STRICT_LOW_PART:
13504 case CONST_VECTOR:
13505 case CONST_FIXED:
13506 case CLRSB:
13507 case CLOBBER:
13508 /* If delegitimize_address couldn't do anything with the UNSPEC, we
13509 can't express it in the debug info. This can happen e.g. with some
13510 TLS UNSPECs. */
13511 break;
13513 case CONST_STRING:
13514 resolve_one_addr (&rtl);
13515 goto symref;
13517 default:
13518 #ifdef ENABLE_CHECKING
13519 print_rtl (stderr, rtl);
13520 gcc_unreachable ();
13521 #else
13522 break;
13523 #endif
13526 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13527 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13529 return mem_loc_result;
13532 /* Return a descriptor that describes the concatenation of two locations.
13533 This is typically a complex variable. */
13535 static dw_loc_descr_ref
13536 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
13538 dw_loc_descr_ref cc_loc_result = NULL;
13539 dw_loc_descr_ref x0_ref
13540 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13541 dw_loc_descr_ref x1_ref
13542 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13544 if (x0_ref == 0 || x1_ref == 0)
13545 return 0;
13547 cc_loc_result = x0_ref;
13548 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
13550 add_loc_descr (&cc_loc_result, x1_ref);
13551 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
13553 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13554 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13556 return cc_loc_result;
13559 /* Return a descriptor that describes the concatenation of N
13560 locations. */
13562 static dw_loc_descr_ref
13563 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
13565 unsigned int i;
13566 dw_loc_descr_ref cc_loc_result = NULL;
13567 unsigned int n = XVECLEN (concatn, 0);
13569 for (i = 0; i < n; ++i)
13571 dw_loc_descr_ref ref;
13572 rtx x = XVECEXP (concatn, 0, i);
13574 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13575 if (ref == NULL)
13576 return NULL;
13578 add_loc_descr (&cc_loc_result, ref);
13579 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
13582 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13583 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13585 return cc_loc_result;
13588 /* Helper function for loc_descriptor. Return DW_OP_GNU_implicit_pointer
13589 for DEBUG_IMPLICIT_PTR RTL. */
13591 static dw_loc_descr_ref
13592 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
13594 dw_loc_descr_ref ret;
13595 dw_die_ref ref;
13597 if (dwarf_strict)
13598 return NULL;
13599 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
13600 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
13601 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
13602 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
13603 ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
13604 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
13605 if (ref)
13607 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13608 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
13609 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
13611 else
13613 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
13614 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
13616 return ret;
13619 /* Output a proper Dwarf location descriptor for a variable or parameter
13620 which is either allocated in a register or in a memory location. For a
13621 register, we just generate an OP_REG and the register number. For a
13622 memory location we provide a Dwarf postfix expression describing how to
13623 generate the (dynamic) address of the object onto the address stack.
13625 MODE is mode of the decl if this loc_descriptor is going to be used in
13626 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
13627 allowed, VOIDmode otherwise.
13629 If we don't know how to describe it, return 0. */
13631 static dw_loc_descr_ref
13632 loc_descriptor (rtx rtl, machine_mode mode,
13633 enum var_init_status initialized)
13635 dw_loc_descr_ref loc_result = NULL;
13637 switch (GET_CODE (rtl))
13639 case SUBREG:
13640 /* The case of a subreg may arise when we have a local (register)
13641 variable or a formal (register) parameter which doesn't quite fill
13642 up an entire register. For now, just assume that it is
13643 legitimate to make the Dwarf info refer to the whole register which
13644 contains the given subreg. */
13645 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
13646 loc_result = loc_descriptor (SUBREG_REG (rtl),
13647 GET_MODE (SUBREG_REG (rtl)), initialized);
13648 else
13649 goto do_default;
13650 break;
13652 case REG:
13653 loc_result = reg_loc_descriptor (rtl, initialized);
13654 break;
13656 case MEM:
13657 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13658 GET_MODE (rtl), initialized);
13659 if (loc_result == NULL)
13660 loc_result = tls_mem_loc_descriptor (rtl);
13661 if (loc_result == NULL)
13663 rtx new_rtl = avoid_constant_pool_reference (rtl);
13664 if (new_rtl != rtl)
13665 loc_result = loc_descriptor (new_rtl, mode, initialized);
13667 break;
13669 case CONCAT:
13670 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
13671 initialized);
13672 break;
13674 case CONCATN:
13675 loc_result = concatn_loc_descriptor (rtl, initialized);
13676 break;
13678 case VAR_LOCATION:
13679 /* Single part. */
13680 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
13682 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
13683 if (GET_CODE (loc) == EXPR_LIST)
13684 loc = XEXP (loc, 0);
13685 loc_result = loc_descriptor (loc, mode, initialized);
13686 break;
13689 rtl = XEXP (rtl, 1);
13690 /* FALLTHRU */
13692 case PARALLEL:
13694 rtvec par_elems = XVEC (rtl, 0);
13695 int num_elem = GET_NUM_ELEM (par_elems);
13696 machine_mode mode;
13697 int i;
13699 /* Create the first one, so we have something to add to. */
13700 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
13701 VOIDmode, initialized);
13702 if (loc_result == NULL)
13703 return NULL;
13704 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
13705 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13706 for (i = 1; i < num_elem; i++)
13708 dw_loc_descr_ref temp;
13710 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
13711 VOIDmode, initialized);
13712 if (temp == NULL)
13713 return NULL;
13714 add_loc_descr (&loc_result, temp);
13715 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
13716 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13719 break;
13721 case CONST_INT:
13722 if (mode != VOIDmode && mode != BLKmode)
13723 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
13724 INTVAL (rtl));
13725 break;
13727 case CONST_DOUBLE:
13728 if (mode == VOIDmode)
13729 mode = GET_MODE (rtl);
13731 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13733 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13735 /* Note that a CONST_DOUBLE rtx could represent either an integer
13736 or a floating-point constant. A CONST_DOUBLE is used whenever
13737 the constant requires more than one word in order to be
13738 adequately represented. We output CONST_DOUBLEs as blocks. */
13739 loc_result = new_loc_descr (DW_OP_implicit_value,
13740 GET_MODE_SIZE (mode), 0);
13741 #if TARGET_SUPPORTS_WIDE_INT == 0
13742 if (!SCALAR_FLOAT_MODE_P (mode))
13744 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
13745 loc_result->dw_loc_oprnd2.v.val_double
13746 = rtx_to_double_int (rtl);
13748 else
13749 #endif
13751 unsigned int length = GET_MODE_SIZE (mode);
13752 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
13754 insert_float (rtl, array);
13755 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13756 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13757 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13758 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13761 break;
13763 case CONST_WIDE_INT:
13764 if (mode == VOIDmode)
13765 mode = GET_MODE (rtl);
13767 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13769 loc_result = new_loc_descr (DW_OP_implicit_value,
13770 GET_MODE_SIZE (mode), 0);
13771 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
13772 loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
13773 *loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
13775 break;
13777 case CONST_VECTOR:
13778 if (mode == VOIDmode)
13779 mode = GET_MODE (rtl);
13781 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13783 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
13784 unsigned int length = CONST_VECTOR_NUNITS (rtl);
13785 unsigned char *array
13786 = ggc_vec_alloc<unsigned char> (length * elt_size);
13787 unsigned int i;
13788 unsigned char *p;
13789 machine_mode imode = GET_MODE_INNER (mode);
13791 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13792 switch (GET_MODE_CLASS (mode))
13794 case MODE_VECTOR_INT:
13795 for (i = 0, p = array; i < length; i++, p += elt_size)
13797 rtx elt = CONST_VECTOR_ELT (rtl, i);
13798 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
13800 break;
13802 case MODE_VECTOR_FLOAT:
13803 for (i = 0, p = array; i < length; i++, p += elt_size)
13805 rtx elt = CONST_VECTOR_ELT (rtl, i);
13806 insert_float (elt, p);
13808 break;
13810 default:
13811 gcc_unreachable ();
13814 loc_result = new_loc_descr (DW_OP_implicit_value,
13815 length * elt_size, 0);
13816 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13817 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
13818 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
13819 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13821 break;
13823 case CONST:
13824 if (mode == VOIDmode
13825 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
13826 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
13827 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
13829 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
13830 break;
13832 /* FALLTHROUGH */
13833 case SYMBOL_REF:
13834 if (!const_ok_for_output (rtl))
13835 break;
13836 case LABEL_REF:
13837 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
13838 && (dwarf_version >= 4 || !dwarf_strict))
13840 loc_result = new_addr_loc_descr (rtl, dtprel_false);
13841 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
13842 vec_safe_push (used_rtx_array, rtl);
13844 break;
13846 case DEBUG_IMPLICIT_PTR:
13847 loc_result = implicit_ptr_descriptor (rtl, 0);
13848 break;
13850 case PLUS:
13851 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
13852 && CONST_INT_P (XEXP (rtl, 1)))
13854 loc_result
13855 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
13856 break;
13858 /* FALLTHRU */
13859 do_default:
13860 default:
13861 if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
13862 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
13863 && dwarf_version >= 4)
13864 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
13866 /* Value expression. */
13867 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
13868 if (loc_result)
13869 add_loc_descr (&loc_result,
13870 new_loc_descr (DW_OP_stack_value, 0, 0));
13872 break;
13875 return loc_result;
13878 /* We need to figure out what section we should use as the base for the
13879 address ranges where a given location is valid.
13880 1. If this particular DECL has a section associated with it, use that.
13881 2. If this function has a section associated with it, use that.
13882 3. Otherwise, use the text section.
13883 XXX: If you split a variable across multiple sections, we won't notice. */
13885 static const char *
13886 secname_for_decl (const_tree decl)
13888 const char *secname;
13890 if (VAR_OR_FUNCTION_DECL_P (decl)
13891 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
13892 && DECL_SECTION_NAME (decl))
13893 secname = DECL_SECTION_NAME (decl);
13894 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
13895 secname = DECL_SECTION_NAME (current_function_decl);
13896 else if (cfun && in_cold_section_p)
13897 secname = crtl->subsections.cold_section_label;
13898 else
13899 secname = text_section_label;
13901 return secname;
13904 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
13906 static bool
13907 decl_by_reference_p (tree decl)
13909 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
13910 || TREE_CODE (decl) == VAR_DECL)
13911 && DECL_BY_REFERENCE (decl));
13914 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13915 for VARLOC. */
13917 static dw_loc_descr_ref
13918 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
13919 enum var_init_status initialized)
13921 int have_address = 0;
13922 dw_loc_descr_ref descr;
13923 machine_mode mode;
13925 if (want_address != 2)
13927 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
13928 /* Single part. */
13929 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13931 varloc = PAT_VAR_LOCATION_LOC (varloc);
13932 if (GET_CODE (varloc) == EXPR_LIST)
13933 varloc = XEXP (varloc, 0);
13934 mode = GET_MODE (varloc);
13935 if (MEM_P (varloc))
13937 rtx addr = XEXP (varloc, 0);
13938 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
13939 mode, initialized);
13940 if (descr)
13941 have_address = 1;
13942 else
13944 rtx x = avoid_constant_pool_reference (varloc);
13945 if (x != varloc)
13946 descr = mem_loc_descriptor (x, mode, VOIDmode,
13947 initialized);
13950 else
13951 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
13953 else
13954 return 0;
13956 else
13958 if (GET_CODE (varloc) == VAR_LOCATION)
13959 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
13960 else
13961 mode = DECL_MODE (loc);
13962 descr = loc_descriptor (varloc, mode, initialized);
13963 have_address = 1;
13966 if (!descr)
13967 return 0;
13969 if (want_address == 2 && !have_address
13970 && (dwarf_version >= 4 || !dwarf_strict))
13972 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
13974 expansion_failed (loc, NULL_RTX,
13975 "DWARF address size mismatch");
13976 return 0;
13978 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
13979 have_address = 1;
13981 /* Show if we can't fill the request for an address. */
13982 if (want_address && !have_address)
13984 expansion_failed (loc, NULL_RTX,
13985 "Want address and only have value");
13986 return 0;
13989 /* If we've got an address and don't want one, dereference. */
13990 if (!want_address && have_address)
13992 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
13993 enum dwarf_location_atom op;
13995 if (size > DWARF2_ADDR_SIZE || size == -1)
13997 expansion_failed (loc, NULL_RTX,
13998 "DWARF address size mismatch");
13999 return 0;
14001 else if (size == DWARF2_ADDR_SIZE)
14002 op = DW_OP_deref;
14003 else
14004 op = DW_OP_deref_size;
14006 add_loc_descr (&descr, new_loc_descr (op, size, 0));
14009 return descr;
14012 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
14013 if it is not possible. */
14015 static dw_loc_descr_ref
14016 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
14018 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
14019 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
14020 else if (dwarf_version >= 3 || !dwarf_strict)
14021 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
14022 else
14023 return NULL;
14026 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
14027 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
14029 static dw_loc_descr_ref
14030 dw_sra_loc_expr (tree decl, rtx loc)
14032 rtx p;
14033 unsigned HOST_WIDE_INT padsize = 0;
14034 dw_loc_descr_ref descr, *descr_tail;
14035 unsigned HOST_WIDE_INT decl_size;
14036 rtx varloc;
14037 enum var_init_status initialized;
14039 if (DECL_SIZE (decl) == NULL
14040 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
14041 return NULL;
14043 decl_size = tree_to_uhwi (DECL_SIZE (decl));
14044 descr = NULL;
14045 descr_tail = &descr;
14047 for (p = loc; p; p = XEXP (p, 1))
14049 unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
14050 rtx loc_note = *decl_piece_varloc_ptr (p);
14051 dw_loc_descr_ref cur_descr;
14052 dw_loc_descr_ref *tail, last = NULL;
14053 unsigned HOST_WIDE_INT opsize = 0;
14055 if (loc_note == NULL_RTX
14056 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
14058 padsize += bitsize;
14059 continue;
14061 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
14062 varloc = NOTE_VAR_LOCATION (loc_note);
14063 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
14064 if (cur_descr == NULL)
14066 padsize += bitsize;
14067 continue;
14070 /* Check that cur_descr either doesn't use
14071 DW_OP_*piece operations, or their sum is equal
14072 to bitsize. Otherwise we can't embed it. */
14073 for (tail = &cur_descr; *tail != NULL;
14074 tail = &(*tail)->dw_loc_next)
14075 if ((*tail)->dw_loc_opc == DW_OP_piece)
14077 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
14078 * BITS_PER_UNIT;
14079 last = *tail;
14081 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
14083 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
14084 last = *tail;
14087 if (last != NULL && opsize != bitsize)
14089 padsize += bitsize;
14090 /* Discard the current piece of the descriptor and release any
14091 addr_table entries it uses. */
14092 remove_loc_list_addr_table_entries (cur_descr);
14093 continue;
14096 /* If there is a hole, add DW_OP_*piece after empty DWARF
14097 expression, which means that those bits are optimized out. */
14098 if (padsize)
14100 if (padsize > decl_size)
14102 remove_loc_list_addr_table_entries (cur_descr);
14103 goto discard_descr;
14105 decl_size -= padsize;
14106 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
14107 if (*descr_tail == NULL)
14109 remove_loc_list_addr_table_entries (cur_descr);
14110 goto discard_descr;
14112 descr_tail = &(*descr_tail)->dw_loc_next;
14113 padsize = 0;
14115 *descr_tail = cur_descr;
14116 descr_tail = tail;
14117 if (bitsize > decl_size)
14118 goto discard_descr;
14119 decl_size -= bitsize;
14120 if (last == NULL)
14122 HOST_WIDE_INT offset = 0;
14123 if (GET_CODE (varloc) == VAR_LOCATION
14124 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14126 varloc = PAT_VAR_LOCATION_LOC (varloc);
14127 if (GET_CODE (varloc) == EXPR_LIST)
14128 varloc = XEXP (varloc, 0);
14132 if (GET_CODE (varloc) == CONST
14133 || GET_CODE (varloc) == SIGN_EXTEND
14134 || GET_CODE (varloc) == ZERO_EXTEND)
14135 varloc = XEXP (varloc, 0);
14136 else if (GET_CODE (varloc) == SUBREG)
14137 varloc = SUBREG_REG (varloc);
14138 else
14139 break;
14141 while (1);
14142 /* DW_OP_bit_size offset should be zero for register
14143 or implicit location descriptions and empty location
14144 descriptions, but for memory addresses needs big endian
14145 adjustment. */
14146 if (MEM_P (varloc))
14148 unsigned HOST_WIDE_INT memsize
14149 = MEM_SIZE (varloc) * BITS_PER_UNIT;
14150 if (memsize != bitsize)
14152 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
14153 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
14154 goto discard_descr;
14155 if (memsize < bitsize)
14156 goto discard_descr;
14157 if (BITS_BIG_ENDIAN)
14158 offset = memsize - bitsize;
14162 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
14163 if (*descr_tail == NULL)
14164 goto discard_descr;
14165 descr_tail = &(*descr_tail)->dw_loc_next;
14169 /* If there were any non-empty expressions, add padding till the end of
14170 the decl. */
14171 if (descr != NULL && decl_size != 0)
14173 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
14174 if (*descr_tail == NULL)
14175 goto discard_descr;
14177 return descr;
14179 discard_descr:
14180 /* Discard the descriptor and release any addr_table entries it uses. */
14181 remove_loc_list_addr_table_entries (descr);
14182 return NULL;
14185 /* Return the dwarf representation of the location list LOC_LIST of
14186 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
14187 function. */
14189 static dw_loc_list_ref
14190 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
14192 const char *endname, *secname;
14193 rtx varloc;
14194 enum var_init_status initialized;
14195 struct var_loc_node *node;
14196 dw_loc_descr_ref descr;
14197 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
14198 dw_loc_list_ref list = NULL;
14199 dw_loc_list_ref *listp = &list;
14201 /* Now that we know what section we are using for a base,
14202 actually construct the list of locations.
14203 The first location information is what is passed to the
14204 function that creates the location list, and the remaining
14205 locations just get added on to that list.
14206 Note that we only know the start address for a location
14207 (IE location changes), so to build the range, we use
14208 the range [current location start, next location start].
14209 This means we have to special case the last node, and generate
14210 a range of [last location start, end of function label]. */
14212 secname = secname_for_decl (decl);
14214 for (node = loc_list->first; node; node = node->next)
14215 if (GET_CODE (node->loc) == EXPR_LIST
14216 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
14218 if (GET_CODE (node->loc) == EXPR_LIST)
14220 /* This requires DW_OP_{,bit_}piece, which is not usable
14221 inside DWARF expressions. */
14222 if (want_address != 2)
14223 continue;
14224 descr = dw_sra_loc_expr (decl, node->loc);
14225 if (descr == NULL)
14226 continue;
14228 else
14230 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14231 varloc = NOTE_VAR_LOCATION (node->loc);
14232 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
14234 if (descr)
14236 bool range_across_switch = false;
14237 /* If section switch happens in between node->label
14238 and node->next->label (or end of function) and
14239 we can't emit it as a single entry list,
14240 emit two ranges, first one ending at the end
14241 of first partition and second one starting at the
14242 beginning of second partition. */
14243 if (node == loc_list->last_before_switch
14244 && (node != loc_list->first || loc_list->first->next)
14245 && current_function_decl)
14247 endname = cfun->fde->dw_fde_end;
14248 range_across_switch = true;
14250 /* The variable has a location between NODE->LABEL and
14251 NODE->NEXT->LABEL. */
14252 else if (node->next)
14253 endname = node->next->label;
14254 /* If the variable has a location at the last label
14255 it keeps its location until the end of function. */
14256 else if (!current_function_decl)
14257 endname = text_end_label;
14258 else
14260 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
14261 current_function_funcdef_no);
14262 endname = ggc_strdup (label_id);
14265 *listp = new_loc_list (descr, node->label, endname, secname);
14266 if (TREE_CODE (decl) == PARM_DECL
14267 && node == loc_list->first
14268 && NOTE_P (node->loc)
14269 && strcmp (node->label, endname) == 0)
14270 (*listp)->force = true;
14271 listp = &(*listp)->dw_loc_next;
14273 if (range_across_switch)
14275 if (GET_CODE (node->loc) == EXPR_LIST)
14276 descr = dw_sra_loc_expr (decl, node->loc);
14277 else
14279 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14280 varloc = NOTE_VAR_LOCATION (node->loc);
14281 descr = dw_loc_list_1 (decl, varloc, want_address,
14282 initialized);
14284 gcc_assert (descr);
14285 /* The variable has a location between NODE->LABEL and
14286 NODE->NEXT->LABEL. */
14287 if (node->next)
14288 endname = node->next->label;
14289 else
14290 endname = cfun->fde->dw_fde_second_end;
14291 *listp = new_loc_list (descr,
14292 cfun->fde->dw_fde_second_begin,
14293 endname, secname);
14294 listp = &(*listp)->dw_loc_next;
14299 /* Try to avoid the overhead of a location list emitting a location
14300 expression instead, but only if we didn't have more than one
14301 location entry in the first place. If some entries were not
14302 representable, we don't want to pretend a single entry that was
14303 applies to the entire scope in which the variable is
14304 available. */
14305 if (list && loc_list->first->next)
14306 gen_llsym (list);
14308 return list;
14311 /* Return if the loc_list has only single element and thus can be represented
14312 as location description. */
14314 static bool
14315 single_element_loc_list_p (dw_loc_list_ref list)
14317 gcc_assert (!list->dw_loc_next || list->ll_symbol);
14318 return !list->ll_symbol;
14321 /* To each location in list LIST add loc descr REF. */
14323 static void
14324 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
14326 dw_loc_descr_ref copy;
14327 add_loc_descr (&list->expr, ref);
14328 list = list->dw_loc_next;
14329 while (list)
14331 copy = ggc_alloc<dw_loc_descr_node> ();
14332 memcpy (copy, ref, sizeof (dw_loc_descr_node));
14333 add_loc_descr (&list->expr, copy);
14334 while (copy->dw_loc_next)
14336 dw_loc_descr_ref new_copy = ggc_alloc<dw_loc_descr_node> ();
14337 memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
14338 copy->dw_loc_next = new_copy;
14339 copy = new_copy;
14341 list = list->dw_loc_next;
14345 /* Given two lists RET and LIST
14346 produce location list that is result of adding expression in LIST
14347 to expression in RET on each position in program.
14348 Might be destructive on both RET and LIST.
14350 TODO: We handle only simple cases of RET or LIST having at most one
14351 element. General case would inolve sorting the lists in program order
14352 and merging them that will need some additional work.
14353 Adding that will improve quality of debug info especially for SRA-ed
14354 structures. */
14356 static void
14357 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
14359 if (!list)
14360 return;
14361 if (!*ret)
14363 *ret = list;
14364 return;
14366 if (!list->dw_loc_next)
14368 add_loc_descr_to_each (*ret, list->expr);
14369 return;
14371 if (!(*ret)->dw_loc_next)
14373 add_loc_descr_to_each (list, (*ret)->expr);
14374 *ret = list;
14375 return;
14377 expansion_failed (NULL_TREE, NULL_RTX,
14378 "Don't know how to merge two non-trivial"
14379 " location lists.\n");
14380 *ret = NULL;
14381 return;
14384 /* LOC is constant expression. Try a luck, look it up in constant
14385 pool and return its loc_descr of its address. */
14387 static dw_loc_descr_ref
14388 cst_pool_loc_descr (tree loc)
14390 /* Get an RTL for this, if something has been emitted. */
14391 rtx rtl = lookup_constant_def (loc);
14393 if (!rtl || !MEM_P (rtl))
14395 gcc_assert (!rtl);
14396 return 0;
14398 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
14400 /* TODO: We might get more coverage if we was actually delaying expansion
14401 of all expressions till end of compilation when constant pools are fully
14402 populated. */
14403 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
14405 expansion_failed (loc, NULL_RTX,
14406 "CST value in contant pool but not marked.");
14407 return 0;
14409 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
14410 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
14413 /* Return dw_loc_list representing address of addr_expr LOC
14414 by looking for inner INDIRECT_REF expression and turning
14415 it into simple arithmetics.
14417 See loc_list_from_tree for the meaning of CONTEXT. */
14419 static dw_loc_list_ref
14420 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
14421 const loc_descr_context *context)
14423 tree obj, offset;
14424 HOST_WIDE_INT bitsize, bitpos, bytepos;
14425 machine_mode mode;
14426 int unsignedp, volatilep = 0;
14427 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14429 obj = get_inner_reference (TREE_OPERAND (loc, 0),
14430 &bitsize, &bitpos, &offset, &mode,
14431 &unsignedp, &volatilep, false);
14432 STRIP_NOPS (obj);
14433 if (bitpos % BITS_PER_UNIT)
14435 expansion_failed (loc, NULL_RTX, "bitfield access");
14436 return 0;
14438 if (!INDIRECT_REF_P (obj))
14440 expansion_failed (obj,
14441 NULL_RTX, "no indirect ref in inner refrence");
14442 return 0;
14444 if (!offset && !bitpos)
14445 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
14446 context);
14447 else if (toplev
14448 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
14449 && (dwarf_version >= 4 || !dwarf_strict))
14451 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
14452 if (!list_ret)
14453 return 0;
14454 if (offset)
14456 /* Variable offset. */
14457 list_ret1 = loc_list_from_tree (offset, 0, context);
14458 if (list_ret1 == 0)
14459 return 0;
14460 add_loc_list (&list_ret, list_ret1);
14461 if (!list_ret)
14462 return 0;
14463 add_loc_descr_to_each (list_ret,
14464 new_loc_descr (DW_OP_plus, 0, 0));
14466 bytepos = bitpos / BITS_PER_UNIT;
14467 if (bytepos > 0)
14468 add_loc_descr_to_each (list_ret,
14469 new_loc_descr (DW_OP_plus_uconst,
14470 bytepos, 0));
14471 else if (bytepos < 0)
14472 loc_list_plus_const (list_ret, bytepos);
14473 add_loc_descr_to_each (list_ret,
14474 new_loc_descr (DW_OP_stack_value, 0, 0));
14476 return list_ret;
14480 /* Helper structure for location descriptions generation. */
14481 struct loc_descr_context
14483 /* The type that is implicitly referenced by DW_OP_push_object_address, or
14484 NULL_TREE if DW_OP_push_object_address in invalid for this location
14485 description. This is used when processing PLACEHOLDER_EXPR nodes. */
14486 tree context_type;
14487 /* The ..._DECL node that should be translated as a
14488 DW_OP_push_object_address operation. */
14489 tree base_decl;
14492 /* Generate Dwarf location list representing LOC.
14493 If WANT_ADDRESS is false, expression computing LOC will be computed
14494 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
14495 if WANT_ADDRESS is 2, expression computing address useable in location
14496 will be returned (i.e. DW_OP_reg can be used
14497 to refer to register values).
14499 CONTEXT provides information to customize the location descriptions
14500 generation. Its context_type field specifies what type is implicitly
14501 referenced by DW_OP_push_object_address. If it is NULL_TREE, this operation
14502 will not be generated.
14504 If CONTEXT is NULL, the behavior is the same as if both context_type and
14505 base_decl fields were NULL_TREE. */
14507 static dw_loc_list_ref
14508 loc_list_from_tree (tree loc, int want_address,
14509 const struct loc_descr_context *context)
14511 dw_loc_descr_ref ret = NULL, ret1 = NULL;
14512 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14513 int have_address = 0;
14514 enum dwarf_location_atom op;
14516 /* ??? Most of the time we do not take proper care for sign/zero
14517 extending the values properly. Hopefully this won't be a real
14518 problem... */
14520 if (context != NULL
14521 && context->base_decl == loc
14522 && want_address == 0)
14524 if (dwarf_version >= 3 || !dwarf_strict)
14525 return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
14526 NULL, NULL, NULL);
14527 else
14528 return NULL;
14531 switch (TREE_CODE (loc))
14533 case ERROR_MARK:
14534 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
14535 return 0;
14537 case PLACEHOLDER_EXPR:
14538 /* This case involves extracting fields from an object to determine the
14539 position of other fields. It is supposed to appear only as the first
14540 operand of COMPONENT_REF nodes and to reference precisely the type
14541 that the context allows. */
14542 if (context != NULL
14543 && TREE_TYPE (loc) == context->context_type
14544 && want_address >= 1)
14546 if (dwarf_version >= 3 || !dwarf_strict)
14548 ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
14549 have_address = 1;
14550 break;
14552 else
14553 return NULL;
14555 else
14556 expansion_failed (loc, NULL_RTX,
14557 "PLACEHOLDER_EXPR for an unexpected type");
14558 break;
14560 case CALL_EXPR:
14561 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
14562 /* There are no opcodes for these operations. */
14563 return 0;
14565 case PREINCREMENT_EXPR:
14566 case PREDECREMENT_EXPR:
14567 case POSTINCREMENT_EXPR:
14568 case POSTDECREMENT_EXPR:
14569 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
14570 /* There are no opcodes for these operations. */
14571 return 0;
14573 case ADDR_EXPR:
14574 /* If we already want an address, see if there is INDIRECT_REF inside
14575 e.g. for &this->field. */
14576 if (want_address)
14578 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
14579 (loc, want_address == 2, context);
14580 if (list_ret)
14581 have_address = 1;
14582 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
14583 && (ret = cst_pool_loc_descr (loc)))
14584 have_address = 1;
14586 /* Otherwise, process the argument and look for the address. */
14587 if (!list_ret && !ret)
14588 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1, context);
14589 else
14591 if (want_address)
14592 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
14593 return NULL;
14595 break;
14597 case VAR_DECL:
14598 if (DECL_THREAD_LOCAL_P (loc))
14600 rtx rtl;
14601 enum dwarf_location_atom tls_op;
14602 enum dtprel_bool dtprel = dtprel_false;
14604 if (targetm.have_tls)
14606 /* If this is not defined, we have no way to emit the
14607 data. */
14608 if (!targetm.asm_out.output_dwarf_dtprel)
14609 return 0;
14611 /* The way DW_OP_GNU_push_tls_address is specified, we
14612 can only look up addresses of objects in the current
14613 module. We used DW_OP_addr as first op, but that's
14614 wrong, because DW_OP_addr is relocated by the debug
14615 info consumer, while DW_OP_GNU_push_tls_address
14616 operand shouldn't be. */
14617 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
14618 return 0;
14619 dtprel = dtprel_true;
14620 tls_op = DW_OP_GNU_push_tls_address;
14622 else
14624 if (!targetm.emutls.debug_form_tls_address
14625 || !(dwarf_version >= 3 || !dwarf_strict))
14626 return 0;
14627 /* We stuffed the control variable into the DECL_VALUE_EXPR
14628 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
14629 no longer appear in gimple code. We used the control
14630 variable in specific so that we could pick it up here. */
14631 loc = DECL_VALUE_EXPR (loc);
14632 tls_op = DW_OP_form_tls_address;
14635 rtl = rtl_for_decl_location (loc);
14636 if (rtl == NULL_RTX)
14637 return 0;
14639 if (!MEM_P (rtl))
14640 return 0;
14641 rtl = XEXP (rtl, 0);
14642 if (! CONSTANT_P (rtl))
14643 return 0;
14645 ret = new_addr_loc_descr (rtl, dtprel);
14646 ret1 = new_loc_descr (tls_op, 0, 0);
14647 add_loc_descr (&ret, ret1);
14649 have_address = 1;
14650 break;
14652 /* FALLTHRU */
14654 case PARM_DECL:
14655 case RESULT_DECL:
14656 if (DECL_HAS_VALUE_EXPR_P (loc))
14657 return loc_list_from_tree (DECL_VALUE_EXPR (loc),
14658 want_address, context);
14659 /* FALLTHRU */
14661 case FUNCTION_DECL:
14663 rtx rtl;
14664 var_loc_list *loc_list = lookup_decl_loc (loc);
14666 if (loc_list && loc_list->first)
14668 list_ret = dw_loc_list (loc_list, loc, want_address);
14669 have_address = want_address != 0;
14670 break;
14672 rtl = rtl_for_decl_location (loc);
14673 if (rtl == NULL_RTX)
14675 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
14676 return 0;
14678 else if (CONST_INT_P (rtl))
14680 HOST_WIDE_INT val = INTVAL (rtl);
14681 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14682 val &= GET_MODE_MASK (DECL_MODE (loc));
14683 ret = int_loc_descriptor (val);
14685 else if (GET_CODE (rtl) == CONST_STRING)
14687 expansion_failed (loc, NULL_RTX, "CONST_STRING");
14688 return 0;
14690 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
14691 ret = new_addr_loc_descr (rtl, dtprel_false);
14692 else
14694 machine_mode mode, mem_mode;
14696 /* Certain constructs can only be represented at top-level. */
14697 if (want_address == 2)
14699 ret = loc_descriptor (rtl, VOIDmode,
14700 VAR_INIT_STATUS_INITIALIZED);
14701 have_address = 1;
14703 else
14705 mode = GET_MODE (rtl);
14706 mem_mode = VOIDmode;
14707 if (MEM_P (rtl))
14709 mem_mode = mode;
14710 mode = get_address_mode (rtl);
14711 rtl = XEXP (rtl, 0);
14712 have_address = 1;
14714 ret = mem_loc_descriptor (rtl, mode, mem_mode,
14715 VAR_INIT_STATUS_INITIALIZED);
14717 if (!ret)
14718 expansion_failed (loc, rtl,
14719 "failed to produce loc descriptor for rtl");
14722 break;
14724 case MEM_REF:
14725 if (!integer_zerop (TREE_OPERAND (loc, 1)))
14727 have_address = 1;
14728 goto do_plus;
14730 /* Fallthru. */
14731 case INDIRECT_REF:
14732 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14733 have_address = 1;
14734 break;
14736 case TARGET_MEM_REF:
14737 case SSA_NAME:
14738 case DEBUG_EXPR_DECL:
14739 return NULL;
14741 case COMPOUND_EXPR:
14742 return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address, context);
14744 CASE_CONVERT:
14745 case VIEW_CONVERT_EXPR:
14746 case SAVE_EXPR:
14747 case MODIFY_EXPR:
14748 return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address, context);
14750 case COMPONENT_REF:
14751 case BIT_FIELD_REF:
14752 case ARRAY_REF:
14753 case ARRAY_RANGE_REF:
14754 case REALPART_EXPR:
14755 case IMAGPART_EXPR:
14757 tree obj, offset;
14758 HOST_WIDE_INT bitsize, bitpos, bytepos;
14759 machine_mode mode;
14760 int unsignedp, volatilep = 0;
14762 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
14763 &unsignedp, &volatilep, false);
14765 gcc_assert (obj != loc);
14767 list_ret = loc_list_from_tree (obj,
14768 want_address == 2
14769 && !bitpos && !offset ? 2 : 1,
14770 context);
14771 /* TODO: We can extract value of the small expression via shifting even
14772 for nonzero bitpos. */
14773 if (list_ret == 0)
14774 return 0;
14775 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
14777 expansion_failed (loc, NULL_RTX,
14778 "bitfield access");
14779 return 0;
14782 if (offset != NULL_TREE)
14784 /* Variable offset. */
14785 list_ret1 = loc_list_from_tree (offset, 0, context);
14786 if (list_ret1 == 0)
14787 return 0;
14788 add_loc_list (&list_ret, list_ret1);
14789 if (!list_ret)
14790 return 0;
14791 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
14794 bytepos = bitpos / BITS_PER_UNIT;
14795 if (bytepos > 0)
14796 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
14797 else if (bytepos < 0)
14798 loc_list_plus_const (list_ret, bytepos);
14800 have_address = 1;
14801 break;
14804 case INTEGER_CST:
14805 if ((want_address || !tree_fits_shwi_p (loc))
14806 && (ret = cst_pool_loc_descr (loc)))
14807 have_address = 1;
14808 else if (want_address == 2
14809 && tree_fits_shwi_p (loc)
14810 && (ret = address_of_int_loc_descriptor
14811 (int_size_in_bytes (TREE_TYPE (loc)),
14812 tree_to_shwi (loc))))
14813 have_address = 1;
14814 else if (tree_fits_shwi_p (loc))
14815 ret = int_loc_descriptor (tree_to_shwi (loc));
14816 else
14818 expansion_failed (loc, NULL_RTX,
14819 "Integer operand is not host integer");
14820 return 0;
14822 break;
14824 case CONSTRUCTOR:
14825 case REAL_CST:
14826 case STRING_CST:
14827 case COMPLEX_CST:
14828 if ((ret = cst_pool_loc_descr (loc)))
14829 have_address = 1;
14830 else
14831 /* We can construct small constants here using int_loc_descriptor. */
14832 expansion_failed (loc, NULL_RTX,
14833 "constructor or constant not in constant pool");
14834 break;
14836 case TRUTH_AND_EXPR:
14837 case TRUTH_ANDIF_EXPR:
14838 case BIT_AND_EXPR:
14839 op = DW_OP_and;
14840 goto do_binop;
14842 case TRUTH_XOR_EXPR:
14843 case BIT_XOR_EXPR:
14844 op = DW_OP_xor;
14845 goto do_binop;
14847 case TRUTH_OR_EXPR:
14848 case TRUTH_ORIF_EXPR:
14849 case BIT_IOR_EXPR:
14850 op = DW_OP_or;
14851 goto do_binop;
14853 case FLOOR_DIV_EXPR:
14854 case CEIL_DIV_EXPR:
14855 case ROUND_DIV_EXPR:
14856 case TRUNC_DIV_EXPR:
14857 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14858 return 0;
14859 op = DW_OP_div;
14860 goto do_binop;
14862 case MINUS_EXPR:
14863 op = DW_OP_minus;
14864 goto do_binop;
14866 case FLOOR_MOD_EXPR:
14867 case CEIL_MOD_EXPR:
14868 case ROUND_MOD_EXPR:
14869 case TRUNC_MOD_EXPR:
14870 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14872 op = DW_OP_mod;
14873 goto do_binop;
14875 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14876 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
14877 if (list_ret == 0 || list_ret1 == 0)
14878 return 0;
14880 add_loc_list (&list_ret, list_ret1);
14881 if (list_ret == 0)
14882 return 0;
14883 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14884 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14885 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
14886 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
14887 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
14888 break;
14890 case MULT_EXPR:
14891 op = DW_OP_mul;
14892 goto do_binop;
14894 case LSHIFT_EXPR:
14895 op = DW_OP_shl;
14896 goto do_binop;
14898 case RSHIFT_EXPR:
14899 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
14900 goto do_binop;
14902 case POINTER_PLUS_EXPR:
14903 case PLUS_EXPR:
14904 do_plus:
14905 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
14907 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14908 if (list_ret == 0)
14909 return 0;
14911 loc_list_plus_const (list_ret, tree_to_shwi (TREE_OPERAND (loc, 1)));
14912 break;
14915 op = DW_OP_plus;
14916 goto do_binop;
14918 case LE_EXPR:
14919 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14920 return 0;
14922 op = DW_OP_le;
14923 goto do_binop;
14925 case GE_EXPR:
14926 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14927 return 0;
14929 op = DW_OP_ge;
14930 goto do_binop;
14932 case LT_EXPR:
14933 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14934 return 0;
14936 op = DW_OP_lt;
14937 goto do_binop;
14939 case GT_EXPR:
14940 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14941 return 0;
14943 op = DW_OP_gt;
14944 goto do_binop;
14946 case EQ_EXPR:
14947 op = DW_OP_eq;
14948 goto do_binop;
14950 case NE_EXPR:
14951 op = DW_OP_ne;
14952 goto do_binop;
14954 do_binop:
14955 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14956 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
14957 if (list_ret == 0 || list_ret1 == 0)
14958 return 0;
14960 add_loc_list (&list_ret, list_ret1);
14961 if (list_ret == 0)
14962 return 0;
14963 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14964 break;
14966 case TRUTH_NOT_EXPR:
14967 case BIT_NOT_EXPR:
14968 op = DW_OP_not;
14969 goto do_unop;
14971 case ABS_EXPR:
14972 op = DW_OP_abs;
14973 goto do_unop;
14975 case NEGATE_EXPR:
14976 op = DW_OP_neg;
14977 goto do_unop;
14979 do_unop:
14980 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14981 if (list_ret == 0)
14982 return 0;
14984 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14985 break;
14987 case MIN_EXPR:
14988 case MAX_EXPR:
14990 const enum tree_code code =
14991 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
14993 loc = build3 (COND_EXPR, TREE_TYPE (loc),
14994 build2 (code, integer_type_node,
14995 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
14996 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
14999 /* ... fall through ... */
15001 case COND_EXPR:
15003 dw_loc_descr_ref lhs
15004 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
15005 dw_loc_list_ref rhs
15006 = loc_list_from_tree (TREE_OPERAND (loc, 2), 0, context);
15007 dw_loc_descr_ref bra_node, jump_node, tmp;
15009 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
15010 if (list_ret == 0 || lhs == 0 || rhs == 0)
15011 return 0;
15013 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15014 add_loc_descr_to_each (list_ret, bra_node);
15016 add_loc_list (&list_ret, rhs);
15017 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
15018 add_loc_descr_to_each (list_ret, jump_node);
15020 add_loc_descr_to_each (list_ret, lhs);
15021 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15022 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
15024 /* ??? Need a node to point the skip at. Use a nop. */
15025 tmp = new_loc_descr (DW_OP_nop, 0, 0);
15026 add_loc_descr_to_each (list_ret, tmp);
15027 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15028 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
15030 break;
15032 case FIX_TRUNC_EXPR:
15033 return 0;
15035 default:
15036 /* Leave front-end specific codes as simply unknown. This comes
15037 up, for instance, with the C STMT_EXPR. */
15038 if ((unsigned int) TREE_CODE (loc)
15039 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
15041 expansion_failed (loc, NULL_RTX,
15042 "language specific tree node");
15043 return 0;
15046 #ifdef ENABLE_CHECKING
15047 /* Otherwise this is a generic code; we should just lists all of
15048 these explicitly. We forgot one. */
15049 gcc_unreachable ();
15050 #else
15051 /* In a release build, we want to degrade gracefully: better to
15052 generate incomplete debugging information than to crash. */
15053 return NULL;
15054 #endif
15057 if (!ret && !list_ret)
15058 return 0;
15060 if (want_address == 2 && !have_address
15061 && (dwarf_version >= 4 || !dwarf_strict))
15063 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
15065 expansion_failed (loc, NULL_RTX,
15066 "DWARF address size mismatch");
15067 return 0;
15069 if (ret)
15070 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
15071 else
15072 add_loc_descr_to_each (list_ret,
15073 new_loc_descr (DW_OP_stack_value, 0, 0));
15074 have_address = 1;
15076 /* Show if we can't fill the request for an address. */
15077 if (want_address && !have_address)
15079 expansion_failed (loc, NULL_RTX,
15080 "Want address and only have value");
15081 return 0;
15084 gcc_assert (!ret || !list_ret);
15086 /* If we've got an address and don't want one, dereference. */
15087 if (!want_address && have_address)
15089 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
15091 if (size > DWARF2_ADDR_SIZE || size == -1)
15093 expansion_failed (loc, NULL_RTX,
15094 "DWARF address size mismatch");
15095 return 0;
15097 else if (size == DWARF2_ADDR_SIZE)
15098 op = DW_OP_deref;
15099 else
15100 op = DW_OP_deref_size;
15102 if (ret)
15103 add_loc_descr (&ret, new_loc_descr (op, size, 0));
15104 else
15105 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
15107 if (ret)
15108 list_ret = new_loc_list (ret, NULL, NULL, NULL);
15110 return list_ret;
15113 /* Same as above but return only single location expression. */
15114 static dw_loc_descr_ref
15115 loc_descriptor_from_tree (tree loc, int want_address,
15116 const struct loc_descr_context *context)
15118 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
15119 if (!ret)
15120 return NULL;
15121 if (ret->dw_loc_next)
15123 expansion_failed (loc, NULL_RTX,
15124 "Location list where only loc descriptor needed");
15125 return NULL;
15127 return ret->expr;
15130 /* Given a value, round it up to the lowest multiple of `boundary'
15131 which is not less than the value itself. */
15133 static inline HOST_WIDE_INT
15134 ceiling (HOST_WIDE_INT value, unsigned int boundary)
15136 return (((value + boundary - 1) / boundary) * boundary);
15139 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
15140 pointer to the declared type for the relevant field variable, or return
15141 `integer_type_node' if the given node turns out to be an
15142 ERROR_MARK node. */
15144 static inline tree
15145 field_type (const_tree decl)
15147 tree type;
15149 if (TREE_CODE (decl) == ERROR_MARK)
15150 return integer_type_node;
15152 type = DECL_BIT_FIELD_TYPE (decl);
15153 if (type == NULL_TREE)
15154 type = TREE_TYPE (decl);
15156 return type;
15159 /* Given a pointer to a tree node, return the alignment in bits for
15160 it, or else return BITS_PER_WORD if the node actually turns out to
15161 be an ERROR_MARK node. */
15163 static inline unsigned
15164 simple_type_align_in_bits (const_tree type)
15166 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
15169 static inline unsigned
15170 simple_decl_align_in_bits (const_tree decl)
15172 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
15175 /* Return the result of rounding T up to ALIGN. */
15177 static inline offset_int
15178 round_up_to_align (const offset_int &t, unsigned int align)
15180 return wi::udiv_trunc (t + align - 1, align) * align;
15183 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
15184 lowest addressed byte of the "containing object" for the given FIELD_DECL,
15185 or return 0 if we are unable to determine what that offset is, either
15186 because the argument turns out to be a pointer to an ERROR_MARK node, or
15187 because the offset is actually variable. (We can't handle the latter case
15188 just yet). */
15190 static HOST_WIDE_INT
15191 field_byte_offset (const_tree decl)
15193 offset_int object_offset_in_bits;
15194 offset_int object_offset_in_bytes;
15195 offset_int bitpos_int;
15197 if (TREE_CODE (decl) == ERROR_MARK)
15198 return 0;
15200 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
15202 /* We cannot yet cope with fields whose positions are variable, so
15203 for now, when we see such things, we simply return 0. Someday, we may
15204 be able to handle such cases, but it will be damn difficult. */
15205 if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
15206 return 0;
15208 bitpos_int = wi::to_offset (bit_position (decl));
15210 if (PCC_BITFIELD_TYPE_MATTERS)
15212 tree type;
15213 tree field_size_tree;
15214 offset_int deepest_bitpos;
15215 offset_int field_size_in_bits;
15216 unsigned int type_align_in_bits;
15217 unsigned int decl_align_in_bits;
15218 offset_int type_size_in_bits;
15220 type = field_type (decl);
15221 type_size_in_bits = offset_int_type_size_in_bits (type);
15222 type_align_in_bits = simple_type_align_in_bits (type);
15224 field_size_tree = DECL_SIZE (decl);
15226 /* The size could be unspecified if there was an error, or for
15227 a flexible array member. */
15228 if (!field_size_tree)
15229 field_size_tree = bitsize_zero_node;
15231 /* If the size of the field is not constant, use the type size. */
15232 if (TREE_CODE (field_size_tree) == INTEGER_CST)
15233 field_size_in_bits = wi::to_offset (field_size_tree);
15234 else
15235 field_size_in_bits = type_size_in_bits;
15237 decl_align_in_bits = simple_decl_align_in_bits (decl);
15239 /* The GCC front-end doesn't make any attempt to keep track of the
15240 starting bit offset (relative to the start of the containing
15241 structure type) of the hypothetical "containing object" for a
15242 bit-field. Thus, when computing the byte offset value for the
15243 start of the "containing object" of a bit-field, we must deduce
15244 this information on our own. This can be rather tricky to do in
15245 some cases. For example, handling the following structure type
15246 definition when compiling for an i386/i486 target (which only
15247 aligns long long's to 32-bit boundaries) can be very tricky:
15249 struct S { int field1; long long field2:31; };
15251 Fortunately, there is a simple rule-of-thumb which can be used
15252 in such cases. When compiling for an i386/i486, GCC will
15253 allocate 8 bytes for the structure shown above. It decides to
15254 do this based upon one simple rule for bit-field allocation.
15255 GCC allocates each "containing object" for each bit-field at
15256 the first (i.e. lowest addressed) legitimate alignment boundary
15257 (based upon the required minimum alignment for the declared
15258 type of the field) which it can possibly use, subject to the
15259 condition that there is still enough available space remaining
15260 in the containing object (when allocated at the selected point)
15261 to fully accommodate all of the bits of the bit-field itself.
15263 This simple rule makes it obvious why GCC allocates 8 bytes for
15264 each object of the structure type shown above. When looking
15265 for a place to allocate the "containing object" for `field2',
15266 the compiler simply tries to allocate a 64-bit "containing
15267 object" at each successive 32-bit boundary (starting at zero)
15268 until it finds a place to allocate that 64- bit field such that
15269 at least 31 contiguous (and previously unallocated) bits remain
15270 within that selected 64 bit field. (As it turns out, for the
15271 example above, the compiler finds it is OK to allocate the
15272 "containing object" 64-bit field at bit-offset zero within the
15273 structure type.)
15275 Here we attempt to work backwards from the limited set of facts
15276 we're given, and we try to deduce from those facts, where GCC
15277 must have believed that the containing object started (within
15278 the structure type). The value we deduce is then used (by the
15279 callers of this routine) to generate DW_AT_location and
15280 DW_AT_bit_offset attributes for fields (both bit-fields and, in
15281 the case of DW_AT_location, regular fields as well). */
15283 /* Figure out the bit-distance from the start of the structure to
15284 the "deepest" bit of the bit-field. */
15285 deepest_bitpos = bitpos_int + field_size_in_bits;
15287 /* This is the tricky part. Use some fancy footwork to deduce
15288 where the lowest addressed bit of the containing object must
15289 be. */
15290 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
15292 /* Round up to type_align by default. This works best for
15293 bitfields. */
15294 object_offset_in_bits
15295 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
15297 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
15299 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
15301 /* Round up to decl_align instead. */
15302 object_offset_in_bits
15303 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
15306 else
15307 object_offset_in_bits = bitpos_int;
15309 object_offset_in_bytes
15310 = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
15311 return object_offset_in_bytes.to_shwi ();
15314 /* The following routines define various Dwarf attributes and any data
15315 associated with them. */
15317 /* Add a location description attribute value to a DIE.
15319 This emits location attributes suitable for whole variables and
15320 whole parameters. Note that the location attributes for struct fields are
15321 generated by the routine `data_member_location_attribute' below. */
15323 static inline void
15324 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
15325 dw_loc_list_ref descr)
15327 if (descr == 0)
15328 return;
15329 if (single_element_loc_list_p (descr))
15330 add_AT_loc (die, attr_kind, descr->expr);
15331 else
15332 add_AT_loc_list (die, attr_kind, descr);
15335 /* Add DW_AT_accessibility attribute to DIE if needed. */
15337 static void
15338 add_accessibility_attribute (dw_die_ref die, tree decl)
15340 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
15341 children, otherwise the default is DW_ACCESS_public. In DWARF2
15342 the default has always been DW_ACCESS_public. */
15343 if (TREE_PROTECTED (decl))
15344 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
15345 else if (TREE_PRIVATE (decl))
15347 if (dwarf_version == 2
15348 || die->die_parent == NULL
15349 || die->die_parent->die_tag != DW_TAG_class_type)
15350 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
15352 else if (dwarf_version > 2
15353 && die->die_parent
15354 && die->die_parent->die_tag == DW_TAG_class_type)
15355 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
15358 /* Attach the specialized form of location attribute used for data members of
15359 struct and union types. In the special case of a FIELD_DECL node which
15360 represents a bit-field, the "offset" part of this special location
15361 descriptor must indicate the distance in bytes from the lowest-addressed
15362 byte of the containing struct or union type to the lowest-addressed byte of
15363 the "containing object" for the bit-field. (See the `field_byte_offset'
15364 function above).
15366 For any given bit-field, the "containing object" is a hypothetical object
15367 (of some integral or enum type) within which the given bit-field lives. The
15368 type of this hypothetical "containing object" is always the same as the
15369 declared type of the individual bit-field itself (for GCC anyway... the
15370 DWARF spec doesn't actually mandate this). Note that it is the size (in
15371 bytes) of the hypothetical "containing object" which will be given in the
15372 DW_AT_byte_size attribute for this bit-field. (See the
15373 `byte_size_attribute' function below.) It is also used when calculating the
15374 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
15375 function below.) */
15377 static void
15378 add_data_member_location_attribute (dw_die_ref die, tree decl)
15380 HOST_WIDE_INT offset;
15381 dw_loc_descr_ref loc_descr = 0;
15383 if (TREE_CODE (decl) == TREE_BINFO)
15385 /* We're working on the TAG_inheritance for a base class. */
15386 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
15388 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
15389 aren't at a fixed offset from all (sub)objects of the same
15390 type. We need to extract the appropriate offset from our
15391 vtable. The following dwarf expression means
15393 BaseAddr = ObAddr + *((*ObAddr) - Offset)
15395 This is specific to the V3 ABI, of course. */
15397 dw_loc_descr_ref tmp;
15399 /* Make a copy of the object address. */
15400 tmp = new_loc_descr (DW_OP_dup, 0, 0);
15401 add_loc_descr (&loc_descr, tmp);
15403 /* Extract the vtable address. */
15404 tmp = new_loc_descr (DW_OP_deref, 0, 0);
15405 add_loc_descr (&loc_descr, tmp);
15407 /* Calculate the address of the offset. */
15408 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
15409 gcc_assert (offset < 0);
15411 tmp = int_loc_descriptor (-offset);
15412 add_loc_descr (&loc_descr, tmp);
15413 tmp = new_loc_descr (DW_OP_minus, 0, 0);
15414 add_loc_descr (&loc_descr, tmp);
15416 /* Extract the offset. */
15417 tmp = new_loc_descr (DW_OP_deref, 0, 0);
15418 add_loc_descr (&loc_descr, tmp);
15420 /* Add it to the object address. */
15421 tmp = new_loc_descr (DW_OP_plus, 0, 0);
15422 add_loc_descr (&loc_descr, tmp);
15424 else
15425 offset = tree_to_shwi (BINFO_OFFSET (decl));
15427 else
15428 offset = field_byte_offset (decl);
15430 if (! loc_descr)
15432 if (dwarf_version > 2)
15434 /* Don't need to output a location expression, just the constant. */
15435 if (offset < 0)
15436 add_AT_int (die, DW_AT_data_member_location, offset);
15437 else
15438 add_AT_unsigned (die, DW_AT_data_member_location, offset);
15439 return;
15441 else
15443 enum dwarf_location_atom op;
15445 /* The DWARF2 standard says that we should assume that the structure
15446 address is already on the stack, so we can specify a structure
15447 field address by using DW_OP_plus_uconst. */
15448 op = DW_OP_plus_uconst;
15449 loc_descr = new_loc_descr (op, offset, 0);
15453 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
15456 /* Writes integer values to dw_vec_const array. */
15458 static void
15459 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
15461 while (size != 0)
15463 *dest++ = val & 0xff;
15464 val >>= 8;
15465 --size;
15469 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
15471 static HOST_WIDE_INT
15472 extract_int (const unsigned char *src, unsigned int size)
15474 HOST_WIDE_INT val = 0;
15476 src += size;
15477 while (size != 0)
15479 val <<= 8;
15480 val |= *--src & 0xff;
15481 --size;
15483 return val;
15486 /* Writes wide_int values to dw_vec_const array. */
15488 static void
15489 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
15491 int i;
15493 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
15495 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
15496 return;
15499 /* We'd have to extend this code to support odd sizes. */
15500 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
15502 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
15504 if (WORDS_BIG_ENDIAN)
15505 for (i = n - 1; i >= 0; i--)
15507 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
15508 dest += sizeof (HOST_WIDE_INT);
15510 else
15511 for (i = 0; i < n; i++)
15513 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
15514 dest += sizeof (HOST_WIDE_INT);
15518 /* Writes floating point values to dw_vec_const array. */
15520 static void
15521 insert_float (const_rtx rtl, unsigned char *array)
15523 REAL_VALUE_TYPE rv;
15524 long val[4];
15525 int i;
15527 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
15528 real_to_target (val, &rv, GET_MODE (rtl));
15530 /* real_to_target puts 32-bit pieces in each long. Pack them. */
15531 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
15533 insert_int (val[i], 4, array);
15534 array += 4;
15538 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
15539 does not have a "location" either in memory or in a register. These
15540 things can arise in GNU C when a constant is passed as an actual parameter
15541 to an inlined function. They can also arise in C++ where declared
15542 constants do not necessarily get memory "homes". */
15544 static bool
15545 add_const_value_attribute (dw_die_ref die, rtx rtl)
15547 switch (GET_CODE (rtl))
15549 case CONST_INT:
15551 HOST_WIDE_INT val = INTVAL (rtl);
15553 if (val < 0)
15554 add_AT_int (die, DW_AT_const_value, val);
15555 else
15556 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
15558 return true;
15560 case CONST_WIDE_INT:
15561 add_AT_wide (die, DW_AT_const_value,
15562 std::make_pair (rtl, GET_MODE (rtl)));
15563 return true;
15565 case CONST_DOUBLE:
15566 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
15567 floating-point constant. A CONST_DOUBLE is used whenever the
15568 constant requires more than one word in order to be adequately
15569 represented. */
15571 machine_mode mode = GET_MODE (rtl);
15573 if (TARGET_SUPPORTS_WIDE_INT == 0 && !SCALAR_FLOAT_MODE_P (mode))
15574 add_AT_double (die, DW_AT_const_value,
15575 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
15576 else
15578 unsigned int length = GET_MODE_SIZE (mode);
15579 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15581 insert_float (rtl, array);
15582 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
15585 return true;
15587 case CONST_VECTOR:
15589 machine_mode mode = GET_MODE (rtl);
15590 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
15591 unsigned int length = CONST_VECTOR_NUNITS (rtl);
15592 unsigned char *array
15593 = ggc_vec_alloc<unsigned char> (length * elt_size);
15594 unsigned int i;
15595 unsigned char *p;
15596 machine_mode imode = GET_MODE_INNER (mode);
15598 switch (GET_MODE_CLASS (mode))
15600 case MODE_VECTOR_INT:
15601 for (i = 0, p = array; i < length; i++, p += elt_size)
15603 rtx elt = CONST_VECTOR_ELT (rtl, i);
15604 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
15606 break;
15608 case MODE_VECTOR_FLOAT:
15609 for (i = 0, p = array; i < length; i++, p += elt_size)
15611 rtx elt = CONST_VECTOR_ELT (rtl, i);
15612 insert_float (elt, p);
15614 break;
15616 default:
15617 gcc_unreachable ();
15620 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
15622 return true;
15624 case CONST_STRING:
15625 if (dwarf_version >= 4 || !dwarf_strict)
15627 dw_loc_descr_ref loc_result;
15628 resolve_one_addr (&rtl);
15629 rtl_addr:
15630 loc_result = new_addr_loc_descr (rtl, dtprel_false);
15631 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
15632 add_AT_loc (die, DW_AT_location, loc_result);
15633 vec_safe_push (used_rtx_array, rtl);
15634 return true;
15636 return false;
15638 case CONST:
15639 if (CONSTANT_P (XEXP (rtl, 0)))
15640 return add_const_value_attribute (die, XEXP (rtl, 0));
15641 /* FALLTHROUGH */
15642 case SYMBOL_REF:
15643 if (!const_ok_for_output (rtl))
15644 return false;
15645 case LABEL_REF:
15646 if (dwarf_version >= 4 || !dwarf_strict)
15647 goto rtl_addr;
15648 return false;
15650 case PLUS:
15651 /* In cases where an inlined instance of an inline function is passed
15652 the address of an `auto' variable (which is local to the caller) we
15653 can get a situation where the DECL_RTL of the artificial local
15654 variable (for the inlining) which acts as a stand-in for the
15655 corresponding formal parameter (of the inline function) will look
15656 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
15657 exactly a compile-time constant expression, but it isn't the address
15658 of the (artificial) local variable either. Rather, it represents the
15659 *value* which the artificial local variable always has during its
15660 lifetime. We currently have no way to represent such quasi-constant
15661 values in Dwarf, so for now we just punt and generate nothing. */
15662 return false;
15664 case HIGH:
15665 case CONST_FIXED:
15666 return false;
15668 case MEM:
15669 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
15670 && MEM_READONLY_P (rtl)
15671 && GET_MODE (rtl) == BLKmode)
15673 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
15674 return true;
15676 return false;
15678 default:
15679 /* No other kinds of rtx should be possible here. */
15680 gcc_unreachable ();
15682 return false;
15685 /* Determine whether the evaluation of EXPR references any variables
15686 or functions which aren't otherwise used (and therefore may not be
15687 output). */
15688 static tree
15689 reference_to_unused (tree * tp, int * walk_subtrees,
15690 void * data ATTRIBUTE_UNUSED)
15692 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
15693 *walk_subtrees = 0;
15695 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
15696 && ! TREE_ASM_WRITTEN (*tp))
15697 return *tp;
15698 /* ??? The C++ FE emits debug information for using decls, so
15699 putting gcc_unreachable here falls over. See PR31899. For now
15700 be conservative. */
15701 else if (!symtab->global_info_ready
15702 && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
15703 return *tp;
15704 else if (TREE_CODE (*tp) == VAR_DECL)
15706 varpool_node *node = varpool_node::get (*tp);
15707 if (!node || !node->definition)
15708 return *tp;
15710 else if (TREE_CODE (*tp) == FUNCTION_DECL
15711 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
15713 /* The call graph machinery must have finished analyzing,
15714 optimizing and gimplifying the CU by now.
15715 So if *TP has no call graph node associated
15716 to it, it means *TP will not be emitted. */
15717 if (!cgraph_node::get (*tp))
15718 return *tp;
15720 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
15721 return *tp;
15723 return NULL_TREE;
15726 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
15727 for use in a later add_const_value_attribute call. */
15729 static rtx
15730 rtl_for_decl_init (tree init, tree type)
15732 rtx rtl = NULL_RTX;
15734 STRIP_NOPS (init);
15736 /* If a variable is initialized with a string constant without embedded
15737 zeros, build CONST_STRING. */
15738 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
15740 tree enttype = TREE_TYPE (type);
15741 tree domain = TYPE_DOMAIN (type);
15742 machine_mode mode = TYPE_MODE (enttype);
15744 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
15745 && domain
15746 && integer_zerop (TYPE_MIN_VALUE (domain))
15747 && compare_tree_int (TYPE_MAX_VALUE (domain),
15748 TREE_STRING_LENGTH (init) - 1) == 0
15749 && ((size_t) TREE_STRING_LENGTH (init)
15750 == strlen (TREE_STRING_POINTER (init)) + 1))
15752 rtl = gen_rtx_CONST_STRING (VOIDmode,
15753 ggc_strdup (TREE_STRING_POINTER (init)));
15754 rtl = gen_rtx_MEM (BLKmode, rtl);
15755 MEM_READONLY_P (rtl) = 1;
15758 /* Other aggregates, and complex values, could be represented using
15759 CONCAT: FIXME! */
15760 else if (AGGREGATE_TYPE_P (type)
15761 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
15762 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
15763 || TREE_CODE (type) == COMPLEX_TYPE)
15765 /* Vectors only work if their mode is supported by the target.
15766 FIXME: generic vectors ought to work too. */
15767 else if (TREE_CODE (type) == VECTOR_TYPE
15768 && !VECTOR_MODE_P (TYPE_MODE (type)))
15770 /* If the initializer is something that we know will expand into an
15771 immediate RTL constant, expand it now. We must be careful not to
15772 reference variables which won't be output. */
15773 else if (initializer_constant_valid_p (init, type)
15774 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
15776 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
15777 possible. */
15778 if (TREE_CODE (type) == VECTOR_TYPE)
15779 switch (TREE_CODE (init))
15781 case VECTOR_CST:
15782 break;
15783 case CONSTRUCTOR:
15784 if (TREE_CONSTANT (init))
15786 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
15787 bool constant_p = true;
15788 tree value;
15789 unsigned HOST_WIDE_INT ix;
15791 /* Even when ctor is constant, it might contain non-*_CST
15792 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
15793 belong into VECTOR_CST nodes. */
15794 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
15795 if (!CONSTANT_CLASS_P (value))
15797 constant_p = false;
15798 break;
15801 if (constant_p)
15803 init = build_vector_from_ctor (type, elts);
15804 break;
15807 /* FALLTHRU */
15809 default:
15810 return NULL;
15813 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
15815 /* If expand_expr returns a MEM, it wasn't immediate. */
15816 gcc_assert (!rtl || !MEM_P (rtl));
15819 return rtl;
15822 /* Generate RTL for the variable DECL to represent its location. */
15824 static rtx
15825 rtl_for_decl_location (tree decl)
15827 rtx rtl;
15829 /* Here we have to decide where we are going to say the parameter "lives"
15830 (as far as the debugger is concerned). We only have a couple of
15831 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
15833 DECL_RTL normally indicates where the parameter lives during most of the
15834 activation of the function. If optimization is enabled however, this
15835 could be either NULL or else a pseudo-reg. Both of those cases indicate
15836 that the parameter doesn't really live anywhere (as far as the code
15837 generation parts of GCC are concerned) during most of the function's
15838 activation. That will happen (for example) if the parameter is never
15839 referenced within the function.
15841 We could just generate a location descriptor here for all non-NULL
15842 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
15843 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
15844 where DECL_RTL is NULL or is a pseudo-reg.
15846 Note however that we can only get away with using DECL_INCOMING_RTL as
15847 a backup substitute for DECL_RTL in certain limited cases. In cases
15848 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
15849 we can be sure that the parameter was passed using the same type as it is
15850 declared to have within the function, and that its DECL_INCOMING_RTL
15851 points us to a place where a value of that type is passed.
15853 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
15854 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
15855 because in these cases DECL_INCOMING_RTL points us to a value of some
15856 type which is *different* from the type of the parameter itself. Thus,
15857 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
15858 such cases, the debugger would end up (for example) trying to fetch a
15859 `float' from a place which actually contains the first part of a
15860 `double'. That would lead to really incorrect and confusing
15861 output at debug-time.
15863 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
15864 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
15865 are a couple of exceptions however. On little-endian machines we can
15866 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
15867 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
15868 an integral type that is smaller than TREE_TYPE (decl). These cases arise
15869 when (on a little-endian machine) a non-prototyped function has a
15870 parameter declared to be of type `short' or `char'. In such cases,
15871 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
15872 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
15873 passed `int' value. If the debugger then uses that address to fetch
15874 a `short' or a `char' (on a little-endian machine) the result will be
15875 the correct data, so we allow for such exceptional cases below.
15877 Note that our goal here is to describe the place where the given formal
15878 parameter lives during most of the function's activation (i.e. between the
15879 end of the prologue and the start of the epilogue). We'll do that as best
15880 as we can. Note however that if the given formal parameter is modified
15881 sometime during the execution of the function, then a stack backtrace (at
15882 debug-time) will show the function as having been called with the *new*
15883 value rather than the value which was originally passed in. This happens
15884 rarely enough that it is not a major problem, but it *is* a problem, and
15885 I'd like to fix it.
15887 A future version of dwarf2out.c may generate two additional attributes for
15888 any given DW_TAG_formal_parameter DIE which will describe the "passed
15889 type" and the "passed location" for the given formal parameter in addition
15890 to the attributes we now generate to indicate the "declared type" and the
15891 "active location" for each parameter. This additional set of attributes
15892 could be used by debuggers for stack backtraces. Separately, note that
15893 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
15894 This happens (for example) for inlined-instances of inline function formal
15895 parameters which are never referenced. This really shouldn't be
15896 happening. All PARM_DECL nodes should get valid non-NULL
15897 DECL_INCOMING_RTL values. FIXME. */
15899 /* Use DECL_RTL as the "location" unless we find something better. */
15900 rtl = DECL_RTL_IF_SET (decl);
15902 /* When generating abstract instances, ignore everything except
15903 constants, symbols living in memory, and symbols living in
15904 fixed registers. */
15905 if (! reload_completed)
15907 if (rtl
15908 && (CONSTANT_P (rtl)
15909 || (MEM_P (rtl)
15910 && CONSTANT_P (XEXP (rtl, 0)))
15911 || (REG_P (rtl)
15912 && TREE_CODE (decl) == VAR_DECL
15913 && TREE_STATIC (decl))))
15915 rtl = targetm.delegitimize_address (rtl);
15916 return rtl;
15918 rtl = NULL_RTX;
15920 else if (TREE_CODE (decl) == PARM_DECL)
15922 if (rtl == NULL_RTX
15923 || is_pseudo_reg (rtl)
15924 || (MEM_P (rtl)
15925 && is_pseudo_reg (XEXP (rtl, 0))
15926 && DECL_INCOMING_RTL (decl)
15927 && MEM_P (DECL_INCOMING_RTL (decl))
15928 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
15930 tree declared_type = TREE_TYPE (decl);
15931 tree passed_type = DECL_ARG_TYPE (decl);
15932 machine_mode dmode = TYPE_MODE (declared_type);
15933 machine_mode pmode = TYPE_MODE (passed_type);
15935 /* This decl represents a formal parameter which was optimized out.
15936 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
15937 all cases where (rtl == NULL_RTX) just below. */
15938 if (dmode == pmode)
15939 rtl = DECL_INCOMING_RTL (decl);
15940 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
15941 && SCALAR_INT_MODE_P (dmode)
15942 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
15943 && DECL_INCOMING_RTL (decl))
15945 rtx inc = DECL_INCOMING_RTL (decl);
15946 if (REG_P (inc))
15947 rtl = inc;
15948 else if (MEM_P (inc))
15950 if (BYTES_BIG_ENDIAN)
15951 rtl = adjust_address_nv (inc, dmode,
15952 GET_MODE_SIZE (pmode)
15953 - GET_MODE_SIZE (dmode));
15954 else
15955 rtl = inc;
15960 /* If the parm was passed in registers, but lives on the stack, then
15961 make a big endian correction if the mode of the type of the
15962 parameter is not the same as the mode of the rtl. */
15963 /* ??? This is the same series of checks that are made in dbxout.c before
15964 we reach the big endian correction code there. It isn't clear if all
15965 of these checks are necessary here, but keeping them all is the safe
15966 thing to do. */
15967 else if (MEM_P (rtl)
15968 && XEXP (rtl, 0) != const0_rtx
15969 && ! CONSTANT_P (XEXP (rtl, 0))
15970 /* Not passed in memory. */
15971 && !MEM_P (DECL_INCOMING_RTL (decl))
15972 /* Not passed by invisible reference. */
15973 && (!REG_P (XEXP (rtl, 0))
15974 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
15975 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
15976 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
15977 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
15978 #endif
15980 /* Big endian correction check. */
15981 && BYTES_BIG_ENDIAN
15982 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
15983 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
15984 < UNITS_PER_WORD))
15986 machine_mode addr_mode = get_address_mode (rtl);
15987 int offset = (UNITS_PER_WORD
15988 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
15990 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15991 plus_constant (addr_mode, XEXP (rtl, 0), offset));
15994 else if (TREE_CODE (decl) == VAR_DECL
15995 && rtl
15996 && MEM_P (rtl)
15997 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
15998 && BYTES_BIG_ENDIAN)
16000 machine_mode addr_mode = get_address_mode (rtl);
16001 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
16002 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
16004 /* If a variable is declared "register" yet is smaller than
16005 a register, then if we store the variable to memory, it
16006 looks like we're storing a register-sized value, when in
16007 fact we are not. We need to adjust the offset of the
16008 storage location to reflect the actual value's bytes,
16009 else gdb will not be able to display it. */
16010 if (rsize > dsize)
16011 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
16012 plus_constant (addr_mode, XEXP (rtl, 0),
16013 rsize - dsize));
16016 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
16017 and will have been substituted directly into all expressions that use it.
16018 C does not have such a concept, but C++ and other languages do. */
16019 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
16020 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
16022 if (rtl)
16023 rtl = targetm.delegitimize_address (rtl);
16025 /* If we don't look past the constant pool, we risk emitting a
16026 reference to a constant pool entry that isn't referenced from
16027 code, and thus is not emitted. */
16028 if (rtl)
16029 rtl = avoid_constant_pool_reference (rtl);
16031 /* Try harder to get a rtl. If this symbol ends up not being emitted
16032 in the current CU, resolve_addr will remove the expression referencing
16033 it. */
16034 if (rtl == NULL_RTX
16035 && TREE_CODE (decl) == VAR_DECL
16036 && !DECL_EXTERNAL (decl)
16037 && TREE_STATIC (decl)
16038 && DECL_NAME (decl)
16039 && !DECL_HARD_REGISTER (decl)
16040 && DECL_MODE (decl) != VOIDmode)
16042 rtl = make_decl_rtl_for_debug (decl);
16043 if (!MEM_P (rtl)
16044 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
16045 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
16046 rtl = NULL_RTX;
16049 return rtl;
16052 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
16053 returned. If so, the decl for the COMMON block is returned, and the
16054 value is the offset into the common block for the symbol. */
16056 static tree
16057 fortran_common (tree decl, HOST_WIDE_INT *value)
16059 tree val_expr, cvar;
16060 machine_mode mode;
16061 HOST_WIDE_INT bitsize, bitpos;
16062 tree offset;
16063 int unsignedp, volatilep = 0;
16065 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
16066 it does not have a value (the offset into the common area), or if it
16067 is thread local (as opposed to global) then it isn't common, and shouldn't
16068 be handled as such. */
16069 if (TREE_CODE (decl) != VAR_DECL
16070 || !TREE_STATIC (decl)
16071 || !DECL_HAS_VALUE_EXPR_P (decl)
16072 || !is_fortran ())
16073 return NULL_TREE;
16075 val_expr = DECL_VALUE_EXPR (decl);
16076 if (TREE_CODE (val_expr) != COMPONENT_REF)
16077 return NULL_TREE;
16079 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
16080 &mode, &unsignedp, &volatilep, true);
16082 if (cvar == NULL_TREE
16083 || TREE_CODE (cvar) != VAR_DECL
16084 || DECL_ARTIFICIAL (cvar)
16085 || !TREE_PUBLIC (cvar))
16086 return NULL_TREE;
16088 *value = 0;
16089 if (offset != NULL)
16091 if (!tree_fits_shwi_p (offset))
16092 return NULL_TREE;
16093 *value = tree_to_shwi (offset);
16095 if (bitpos != 0)
16096 *value += bitpos / BITS_PER_UNIT;
16098 return cvar;
16101 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
16102 data attribute for a variable or a parameter. We generate the
16103 DW_AT_const_value attribute only in those cases where the given variable
16104 or parameter does not have a true "location" either in memory or in a
16105 register. This can happen (for example) when a constant is passed as an
16106 actual argument in a call to an inline function. (It's possible that
16107 these things can crop up in other ways also.) Note that one type of
16108 constant value which can be passed into an inlined function is a constant
16109 pointer. This can happen for example if an actual argument in an inlined
16110 function call evaluates to a compile-time constant address.
16112 CACHE_P is true if it is worth caching the location list for DECL,
16113 so that future calls can reuse it rather than regenerate it from scratch.
16114 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
16115 since we will need to refer to them each time the function is inlined. */
16117 static bool
16118 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
16119 enum dwarf_attribute attr)
16121 rtx rtl;
16122 dw_loc_list_ref list;
16123 var_loc_list *loc_list;
16124 cached_dw_loc_list *cache;
16126 if (TREE_CODE (decl) == ERROR_MARK)
16127 return false;
16129 if (get_AT (die, attr))
16130 return true;
16132 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
16133 || TREE_CODE (decl) == RESULT_DECL);
16135 /* Try to get some constant RTL for this decl, and use that as the value of
16136 the location. */
16138 rtl = rtl_for_decl_location (decl);
16139 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16140 && add_const_value_attribute (die, rtl))
16141 return true;
16143 /* See if we have single element location list that is equivalent to
16144 a constant value. That way we are better to use add_const_value_attribute
16145 rather than expanding constant value equivalent. */
16146 loc_list = lookup_decl_loc (decl);
16147 if (loc_list
16148 && loc_list->first
16149 && loc_list->first->next == NULL
16150 && NOTE_P (loc_list->first->loc)
16151 && NOTE_VAR_LOCATION (loc_list->first->loc)
16152 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
16154 struct var_loc_node *node;
16156 node = loc_list->first;
16157 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
16158 if (GET_CODE (rtl) == EXPR_LIST)
16159 rtl = XEXP (rtl, 0);
16160 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16161 && add_const_value_attribute (die, rtl))
16162 return true;
16164 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
16165 list several times. See if we've already cached the contents. */
16166 list = NULL;
16167 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
16168 cache_p = false;
16169 if (cache_p)
16171 cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
16172 if (cache)
16173 list = cache->loc_list;
16175 if (list == NULL)
16177 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
16178 NULL);
16179 /* It is usually worth caching this result if the decl is from
16180 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
16181 if (cache_p && list && list->dw_loc_next)
16183 cached_dw_loc_list **slot
16184 = cached_dw_loc_list_table->find_slot_with_hash (decl,
16185 DECL_UID (decl),
16186 INSERT);
16187 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
16188 cache->decl_id = DECL_UID (decl);
16189 cache->loc_list = list;
16190 *slot = cache;
16193 if (list)
16195 add_AT_location_description (die, attr, list);
16196 return true;
16198 /* None of that worked, so it must not really have a location;
16199 try adding a constant value attribute from the DECL_INITIAL. */
16200 return tree_add_const_value_attribute_for_decl (die, decl);
16203 /* Helper function for tree_add_const_value_attribute. Natively encode
16204 initializer INIT into an array. Return true if successful. */
16206 static bool
16207 native_encode_initializer (tree init, unsigned char *array, int size)
16209 tree type;
16211 if (init == NULL_TREE)
16212 return false;
16214 STRIP_NOPS (init);
16215 switch (TREE_CODE (init))
16217 case STRING_CST:
16218 type = TREE_TYPE (init);
16219 if (TREE_CODE (type) == ARRAY_TYPE)
16221 tree enttype = TREE_TYPE (type);
16222 machine_mode mode = TYPE_MODE (enttype);
16224 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
16225 return false;
16226 if (int_size_in_bytes (type) != size)
16227 return false;
16228 if (size > TREE_STRING_LENGTH (init))
16230 memcpy (array, TREE_STRING_POINTER (init),
16231 TREE_STRING_LENGTH (init));
16232 memset (array + TREE_STRING_LENGTH (init),
16233 '\0', size - TREE_STRING_LENGTH (init));
16235 else
16236 memcpy (array, TREE_STRING_POINTER (init), size);
16237 return true;
16239 return false;
16240 case CONSTRUCTOR:
16241 type = TREE_TYPE (init);
16242 if (int_size_in_bytes (type) != size)
16243 return false;
16244 if (TREE_CODE (type) == ARRAY_TYPE)
16246 HOST_WIDE_INT min_index;
16247 unsigned HOST_WIDE_INT cnt;
16248 int curpos = 0, fieldsize;
16249 constructor_elt *ce;
16251 if (TYPE_DOMAIN (type) == NULL_TREE
16252 || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
16253 return false;
16255 fieldsize = int_size_in_bytes (TREE_TYPE (type));
16256 if (fieldsize <= 0)
16257 return false;
16259 min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
16260 memset (array, '\0', size);
16261 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
16263 tree val = ce->value;
16264 tree index = ce->index;
16265 int pos = curpos;
16266 if (index && TREE_CODE (index) == RANGE_EXPR)
16267 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
16268 * fieldsize;
16269 else if (index)
16270 pos = (tree_to_shwi (index) - min_index) * fieldsize;
16272 if (val)
16274 STRIP_NOPS (val);
16275 if (!native_encode_initializer (val, array + pos, fieldsize))
16276 return false;
16278 curpos = pos + fieldsize;
16279 if (index && TREE_CODE (index) == RANGE_EXPR)
16281 int count = tree_to_shwi (TREE_OPERAND (index, 1))
16282 - tree_to_shwi (TREE_OPERAND (index, 0));
16283 while (count-- > 0)
16285 if (val)
16286 memcpy (array + curpos, array + pos, fieldsize);
16287 curpos += fieldsize;
16290 gcc_assert (curpos <= size);
16292 return true;
16294 else if (TREE_CODE (type) == RECORD_TYPE
16295 || TREE_CODE (type) == UNION_TYPE)
16297 tree field = NULL_TREE;
16298 unsigned HOST_WIDE_INT cnt;
16299 constructor_elt *ce;
16301 if (int_size_in_bytes (type) != size)
16302 return false;
16304 if (TREE_CODE (type) == RECORD_TYPE)
16305 field = TYPE_FIELDS (type);
16307 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
16309 tree val = ce->value;
16310 int pos, fieldsize;
16312 if (ce->index != 0)
16313 field = ce->index;
16315 if (val)
16316 STRIP_NOPS (val);
16318 if (field == NULL_TREE || DECL_BIT_FIELD (field))
16319 return false;
16321 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
16322 && TYPE_DOMAIN (TREE_TYPE (field))
16323 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
16324 return false;
16325 else if (DECL_SIZE_UNIT (field) == NULL_TREE
16326 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
16327 return false;
16328 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
16329 pos = int_byte_position (field);
16330 gcc_assert (pos + fieldsize <= size);
16331 if (val
16332 && !native_encode_initializer (val, array + pos, fieldsize))
16333 return false;
16335 return true;
16337 return false;
16338 case VIEW_CONVERT_EXPR:
16339 case NON_LVALUE_EXPR:
16340 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
16341 default:
16342 return native_encode_expr (init, array, size) == size;
16346 /* Attach a DW_AT_const_value attribute to DIE. The value of the
16347 attribute is the const value T. */
16349 static bool
16350 tree_add_const_value_attribute (dw_die_ref die, tree t)
16352 tree init;
16353 tree type = TREE_TYPE (t);
16354 rtx rtl;
16356 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
16357 return false;
16359 init = t;
16360 gcc_assert (!DECL_P (init));
16362 rtl = rtl_for_decl_init (init, type);
16363 if (rtl)
16364 return add_const_value_attribute (die, rtl);
16365 /* If the host and target are sane, try harder. */
16366 else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
16367 && initializer_constant_valid_p (init, type))
16369 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
16370 if (size > 0 && (int) size == size)
16372 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
16374 if (native_encode_initializer (init, array, size))
16376 add_AT_vec (die, DW_AT_const_value, size, 1, array);
16377 return true;
16379 ggc_free (array);
16382 return false;
16385 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
16386 attribute is the const value of T, where T is an integral constant
16387 variable with static storage duration
16388 (so it can't be a PARM_DECL or a RESULT_DECL). */
16390 static bool
16391 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
16394 if (!decl
16395 || (TREE_CODE (decl) != VAR_DECL
16396 && TREE_CODE (decl) != CONST_DECL)
16397 || (TREE_CODE (decl) == VAR_DECL
16398 && !TREE_STATIC (decl)))
16399 return false;
16401 if (TREE_READONLY (decl)
16402 && ! TREE_THIS_VOLATILE (decl)
16403 && DECL_INITIAL (decl))
16404 /* OK */;
16405 else
16406 return false;
16408 /* Don't add DW_AT_const_value if abstract origin already has one. */
16409 if (get_AT (var_die, DW_AT_const_value))
16410 return false;
16412 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
16415 /* Convert the CFI instructions for the current function into a
16416 location list. This is used for DW_AT_frame_base when we targeting
16417 a dwarf2 consumer that does not support the dwarf3
16418 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
16419 expressions. */
16421 static dw_loc_list_ref
16422 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
16424 int ix;
16425 dw_fde_ref fde;
16426 dw_loc_list_ref list, *list_tail;
16427 dw_cfi_ref cfi;
16428 dw_cfa_location last_cfa, next_cfa;
16429 const char *start_label, *last_label, *section;
16430 dw_cfa_location remember;
16432 fde = cfun->fde;
16433 gcc_assert (fde != NULL);
16435 section = secname_for_decl (current_function_decl);
16436 list_tail = &list;
16437 list = NULL;
16439 memset (&next_cfa, 0, sizeof (next_cfa));
16440 next_cfa.reg = INVALID_REGNUM;
16441 remember = next_cfa;
16443 start_label = fde->dw_fde_begin;
16445 /* ??? Bald assumption that the CIE opcode list does not contain
16446 advance opcodes. */
16447 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
16448 lookup_cfa_1 (cfi, &next_cfa, &remember);
16450 last_cfa = next_cfa;
16451 last_label = start_label;
16453 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
16455 /* If the first partition contained no CFI adjustments, the
16456 CIE opcodes apply to the whole first partition. */
16457 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16458 fde->dw_fde_begin, fde->dw_fde_end, section);
16459 list_tail =&(*list_tail)->dw_loc_next;
16460 start_label = last_label = fde->dw_fde_second_begin;
16463 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
16465 switch (cfi->dw_cfi_opc)
16467 case DW_CFA_set_loc:
16468 case DW_CFA_advance_loc1:
16469 case DW_CFA_advance_loc2:
16470 case DW_CFA_advance_loc4:
16471 if (!cfa_equal_p (&last_cfa, &next_cfa))
16473 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16474 start_label, last_label, section);
16476 list_tail = &(*list_tail)->dw_loc_next;
16477 last_cfa = next_cfa;
16478 start_label = last_label;
16480 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
16481 break;
16483 case DW_CFA_advance_loc:
16484 /* The encoding is complex enough that we should never emit this. */
16485 gcc_unreachable ();
16487 default:
16488 lookup_cfa_1 (cfi, &next_cfa, &remember);
16489 break;
16491 if (ix + 1 == fde->dw_fde_switch_cfi_index)
16493 if (!cfa_equal_p (&last_cfa, &next_cfa))
16495 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16496 start_label, last_label, section);
16498 list_tail = &(*list_tail)->dw_loc_next;
16499 last_cfa = next_cfa;
16500 start_label = last_label;
16502 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16503 start_label, fde->dw_fde_end, section);
16504 list_tail = &(*list_tail)->dw_loc_next;
16505 start_label = last_label = fde->dw_fde_second_begin;
16509 if (!cfa_equal_p (&last_cfa, &next_cfa))
16511 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16512 start_label, last_label, section);
16513 list_tail = &(*list_tail)->dw_loc_next;
16514 start_label = last_label;
16517 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
16518 start_label,
16519 fde->dw_fde_second_begin
16520 ? fde->dw_fde_second_end : fde->dw_fde_end,
16521 section);
16523 if (list && list->dw_loc_next)
16524 gen_llsym (list);
16526 return list;
16529 /* Compute a displacement from the "steady-state frame pointer" to the
16530 frame base (often the same as the CFA), and store it in
16531 frame_pointer_fb_offset. OFFSET is added to the displacement
16532 before the latter is negated. */
16534 static void
16535 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
16537 rtx reg, elim;
16539 #ifdef FRAME_POINTER_CFA_OFFSET
16540 reg = frame_pointer_rtx;
16541 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
16542 #else
16543 reg = arg_pointer_rtx;
16544 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
16545 #endif
16547 elim = (ira_use_lra_p
16548 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
16549 : eliminate_regs (reg, VOIDmode, NULL_RTX));
16550 if (GET_CODE (elim) == PLUS)
16552 offset += INTVAL (XEXP (elim, 1));
16553 elim = XEXP (elim, 0);
16556 frame_pointer_fb_offset = -offset;
16558 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
16559 in which to eliminate. This is because it's stack pointer isn't
16560 directly accessible as a register within the ISA. To work around
16561 this, assume that while we cannot provide a proper value for
16562 frame_pointer_fb_offset, we won't need one either. */
16563 frame_pointer_fb_offset_valid
16564 = ((SUPPORTS_STACK_ALIGNMENT
16565 && (elim == hard_frame_pointer_rtx
16566 || elim == stack_pointer_rtx))
16567 || elim == (frame_pointer_needed
16568 ? hard_frame_pointer_rtx
16569 : stack_pointer_rtx));
16572 /* Generate a DW_AT_name attribute given some string value to be included as
16573 the value of the attribute. */
16575 static void
16576 add_name_attribute (dw_die_ref die, const char *name_string)
16578 if (name_string != NULL && *name_string != 0)
16580 if (demangle_name_func)
16581 name_string = (*demangle_name_func) (name_string);
16583 add_AT_string (die, DW_AT_name, name_string);
16587 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
16588 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
16589 of TYPE accordingly.
16591 ??? This is a temporary measure until after we're able to generate
16592 regular DWARF for the complex Ada type system. */
16594 static void
16595 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
16596 dw_die_ref context_die)
16598 tree dtype;
16599 dw_die_ref dtype_die;
16601 if (!lang_hooks.types.descriptive_type)
16602 return;
16604 dtype = lang_hooks.types.descriptive_type (type);
16605 if (!dtype)
16606 return;
16608 dtype_die = lookup_type_die (dtype);
16609 if (!dtype_die)
16611 gen_type_die (dtype, context_die);
16612 dtype_die = lookup_type_die (dtype);
16613 gcc_assert (dtype_die);
16616 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
16619 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
16621 static const char *
16622 comp_dir_string (void)
16624 const char *wd;
16625 char *wd1;
16626 static const char *cached_wd = NULL;
16628 if (cached_wd != NULL)
16629 return cached_wd;
16631 wd = get_src_pwd ();
16632 if (wd == NULL)
16633 return NULL;
16635 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
16637 int wdlen;
16639 wdlen = strlen (wd);
16640 wd1 = ggc_vec_alloc<char> (wdlen + 2);
16641 strcpy (wd1, wd);
16642 wd1 [wdlen] = DIR_SEPARATOR;
16643 wd1 [wdlen + 1] = 0;
16644 wd = wd1;
16647 cached_wd = remap_debug_filename (wd);
16648 return cached_wd;
16651 /* Generate a DW_AT_comp_dir attribute for DIE. */
16653 static void
16654 add_comp_dir_attribute (dw_die_ref die)
16656 const char * wd = comp_dir_string ();
16657 if (wd != NULL)
16658 add_AT_string (die, DW_AT_comp_dir, wd);
16661 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
16662 pointer computation, ...), output a representation for that bound according
16663 to the accepted FORMS (see enum dw_scalar_form) and add it to DIE. See
16664 loc_list_from_tree for the meaning of CONTEXT. */
16666 static void
16667 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
16668 int forms, const struct loc_descr_context *context)
16670 dw_die_ref ctx, decl_die;
16671 dw_loc_list_ref list;
16673 bool strip_conversions = true;
16675 while (strip_conversions)
16676 switch (TREE_CODE (value))
16678 case ERROR_MARK:
16679 case SAVE_EXPR:
16680 return;
16682 CASE_CONVERT:
16683 case VIEW_CONVERT_EXPR:
16684 value = TREE_OPERAND (value, 0);
16685 break;
16687 default:
16688 strip_conversions = false;
16689 break;
16692 /* If possible and permitted, output the attribute as a constant. */
16693 if ((forms & dw_scalar_form_constant) != 0
16694 && TREE_CODE (value) == INTEGER_CST)
16696 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
16698 /* If HOST_WIDE_INT is big enough then represent the bound as
16699 a constant value. We need to choose a form based on
16700 whether the type is signed or unsigned. We cannot just
16701 call add_AT_unsigned if the value itself is positive
16702 (add_AT_unsigned might add the unsigned value encoded as
16703 DW_FORM_data[1248]). Some DWARF consumers will lookup the
16704 bounds type and then sign extend any unsigned values found
16705 for signed types. This is needed only for
16706 DW_AT_{lower,upper}_bound, since for most other attributes,
16707 consumers will treat DW_FORM_data[1248] as unsigned values,
16708 regardless of the underlying type. */
16709 if (prec <= HOST_BITS_PER_WIDE_INT
16710 || tree_fits_uhwi_p (value))
16712 if (TYPE_UNSIGNED (TREE_TYPE (value)))
16713 add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
16714 else
16715 add_AT_int (die, attr, TREE_INT_CST_LOW (value));
16717 else
16718 /* Otherwise represent the bound as an unsigned value with
16719 the precision of its type. The precision and signedness
16720 of the type will be necessary to re-interpret it
16721 unambiguously. */
16722 add_AT_wide (die, attr, value);
16723 return;
16726 /* Otherwise, if it's possible and permitted too, output a reference to
16727 another DIE. */
16728 if ((forms & dw_scalar_form_reference) != 0)
16730 tree decl = NULL_TREE;
16732 /* Some type attributes reference an outer type. For instance, the upper
16733 bound of an array may reference an embedding record (this happens in
16734 Ada). */
16735 if (TREE_CODE (value) == COMPONENT_REF
16736 && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
16737 && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
16738 decl = TREE_OPERAND (value, 1);
16740 else if (TREE_CODE (value) == VAR_DECL
16741 || TREE_CODE (value) == PARM_DECL
16742 || TREE_CODE (value) == RESULT_DECL)
16743 decl = value;
16745 if (decl != NULL_TREE)
16747 dw_die_ref decl_die = lookup_decl_die (decl);
16749 /* ??? Can this happen, or should the variable have been bound
16750 first? Probably it can, since I imagine that we try to create
16751 the types of parameters in the order in which they exist in
16752 the list, and won't have created a forward reference to a
16753 later parameter. */
16754 if (decl_die != NULL)
16756 add_AT_die_ref (die, attr, decl_die);
16757 return;
16762 /* Last chance: try to create a stack operation procedure to evaluate the
16763 value. Do nothing if even that is not possible or permitted. */
16764 if ((forms & dw_scalar_form_exprloc) == 0)
16765 return;
16767 list = loc_list_from_tree (value, 2, context);
16768 if (list == NULL || single_element_loc_list_p (list))
16770 /* If this attribute is not a reference nor constant, it is
16771 a DWARF expression rather than location description. For that
16772 loc_list_from_tree (value, 0, &context) is needed. */
16773 dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
16774 if (list2 && single_element_loc_list_p (list2))
16776 add_AT_loc (die, attr, list2->expr);
16777 return;
16781 /* If that failed to give a single element location list, fall back to
16782 outputting this as a reference... still if permitted. */
16783 if (list == NULL || (forms & dw_scalar_form_reference) == 0)
16784 return;
16786 if (current_function_decl == 0)
16787 ctx = comp_unit_die ();
16788 else
16789 ctx = lookup_decl_die (current_function_decl);
16791 decl_die = new_die (DW_TAG_variable, ctx, value);
16792 add_AT_flag (decl_die, DW_AT_artificial, 1);
16793 add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, ctx);
16794 add_AT_location_description (decl_die, DW_AT_location, list);
16795 add_AT_die_ref (die, attr, decl_die);
16798 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
16799 default. */
16801 static int
16802 lower_bound_default (void)
16804 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
16806 case DW_LANG_C:
16807 case DW_LANG_C89:
16808 case DW_LANG_C99:
16809 case DW_LANG_C11:
16810 case DW_LANG_C_plus_plus:
16811 case DW_LANG_C_plus_plus_11:
16812 case DW_LANG_C_plus_plus_14:
16813 case DW_LANG_ObjC:
16814 case DW_LANG_ObjC_plus_plus:
16815 case DW_LANG_Java:
16816 return 0;
16817 case DW_LANG_Fortran77:
16818 case DW_LANG_Fortran90:
16819 case DW_LANG_Fortran95:
16820 case DW_LANG_Fortran03:
16821 case DW_LANG_Fortran08:
16822 return 1;
16823 case DW_LANG_UPC:
16824 case DW_LANG_D:
16825 case DW_LANG_Python:
16826 return dwarf_version >= 4 ? 0 : -1;
16827 case DW_LANG_Ada95:
16828 case DW_LANG_Ada83:
16829 case DW_LANG_Cobol74:
16830 case DW_LANG_Cobol85:
16831 case DW_LANG_Pascal83:
16832 case DW_LANG_Modula2:
16833 case DW_LANG_PLI:
16834 return dwarf_version >= 4 ? 1 : -1;
16835 default:
16836 return -1;
16840 /* Given a tree node describing an array bound (either lower or upper) output
16841 a representation for that bound. */
16843 static void
16844 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
16845 tree bound, const struct loc_descr_context *context)
16847 int dflt;
16849 while (1)
16850 switch (TREE_CODE (bound))
16852 /* Strip all conversions. */
16853 CASE_CONVERT:
16854 case VIEW_CONVERT_EXPR:
16855 bound = TREE_OPERAND (bound, 0);
16856 break;
16858 /* All fixed-bounds are represented by INTEGER_CST nodes. Lower bounds
16859 are even omitted when they are the default. */
16860 case INTEGER_CST:
16861 /* If the value for this bound is the default one, we can even omit the
16862 attribute. */
16863 if (bound_attr == DW_AT_lower_bound
16864 && tree_fits_shwi_p (bound)
16865 && (dflt = lower_bound_default ()) != -1
16866 && tree_to_shwi (bound) == dflt)
16867 return;
16869 /* FALLTHRU */
16871 default:
16872 add_scalar_info (subrange_die, bound_attr, bound,
16873 dw_scalar_form_constant
16874 | dw_scalar_form_exprloc
16875 | dw_scalar_form_reference,
16876 context);
16877 return;
16881 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
16882 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
16883 Note that the block of subscript information for an array type also
16884 includes information about the element type of the given array type.
16886 This function reuses previously set type and bound information if
16887 available. */
16889 static void
16890 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
16892 unsigned dimension_number;
16893 tree lower, upper;
16894 dw_die_ref child = type_die->die_child;
16896 for (dimension_number = 0;
16897 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
16898 type = TREE_TYPE (type), dimension_number++)
16900 tree domain = TYPE_DOMAIN (type);
16902 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
16903 break;
16905 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
16906 and (in GNU C only) variable bounds. Handle all three forms
16907 here. */
16909 /* Find and reuse a previously generated DW_TAG_subrange_type if
16910 available.
16912 For multi-dimensional arrays, as we iterate through the
16913 various dimensions in the enclosing for loop above, we also
16914 iterate through the DIE children and pick at each
16915 DW_TAG_subrange_type previously generated (if available).
16916 Each child DW_TAG_subrange_type DIE describes the range of
16917 the current dimension. At this point we should have as many
16918 DW_TAG_subrange_type's as we have dimensions in the
16919 array. */
16920 dw_die_ref subrange_die = NULL;
16921 if (child)
16922 while (1)
16924 child = child->die_sib;
16925 if (child->die_tag == DW_TAG_subrange_type)
16926 subrange_die = child;
16927 if (child == type_die->die_child)
16929 /* If we wrapped around, stop looking next time. */
16930 child = NULL;
16931 break;
16933 if (child->die_tag == DW_TAG_subrange_type)
16934 break;
16936 if (!subrange_die)
16937 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
16939 if (domain)
16941 /* We have an array type with specified bounds. */
16942 lower = TYPE_MIN_VALUE (domain);
16943 upper = TYPE_MAX_VALUE (domain);
16945 /* Define the index type. */
16946 if (TREE_TYPE (domain)
16947 && !get_AT (subrange_die, DW_AT_type))
16949 /* ??? This is probably an Ada unnamed subrange type. Ignore the
16950 TREE_TYPE field. We can't emit debug info for this
16951 because it is an unnamed integral type. */
16952 if (TREE_CODE (domain) == INTEGER_TYPE
16953 && TYPE_NAME (domain) == NULL_TREE
16954 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
16955 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
16957 else
16958 add_type_attribute (subrange_die, TREE_TYPE (domain),
16959 TYPE_UNQUALIFIED, type_die);
16962 /* ??? If upper is NULL, the array has unspecified length,
16963 but it does have a lower bound. This happens with Fortran
16964 dimension arr(N:*)
16965 Since the debugger is definitely going to need to know N
16966 to produce useful results, go ahead and output the lower
16967 bound solo, and hope the debugger can cope. */
16969 if (!get_AT (subrange_die, DW_AT_lower_bound))
16970 add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
16971 if (upper && !get_AT (subrange_die, DW_AT_upper_bound))
16972 add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
16975 /* Otherwise we have an array type with an unspecified length. The
16976 DWARF-2 spec does not say how to handle this; let's just leave out the
16977 bounds. */
16981 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
16983 static void
16984 add_byte_size_attribute (dw_die_ref die, tree tree_node)
16986 dw_die_ref decl_die;
16987 HOST_WIDE_INT size;
16989 switch (TREE_CODE (tree_node))
16991 case ERROR_MARK:
16992 size = 0;
16993 break;
16994 case ENUMERAL_TYPE:
16995 case RECORD_TYPE:
16996 case UNION_TYPE:
16997 case QUAL_UNION_TYPE:
16998 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
16999 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
17001 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
17002 return;
17004 size = int_size_in_bytes (tree_node);
17005 break;
17006 case FIELD_DECL:
17007 /* For a data member of a struct or union, the DW_AT_byte_size is
17008 generally given as the number of bytes normally allocated for an
17009 object of the *declared* type of the member itself. This is true
17010 even for bit-fields. */
17011 size = int_size_in_bytes (field_type (tree_node));
17012 break;
17013 default:
17014 gcc_unreachable ();
17017 /* Note that `size' might be -1 when we get to this point. If it is, that
17018 indicates that the byte size of the entity in question is variable. We
17019 have no good way of expressing this fact in Dwarf at the present time,
17020 when location description was not used by the caller code instead. */
17021 if (size >= 0)
17022 add_AT_unsigned (die, DW_AT_byte_size, size);
17025 /* For a FIELD_DECL node which represents a bit-field, output an attribute
17026 which specifies the distance in bits from the highest order bit of the
17027 "containing object" for the bit-field to the highest order bit of the
17028 bit-field itself.
17030 For any given bit-field, the "containing object" is a hypothetical object
17031 (of some integral or enum type) within which the given bit-field lives. The
17032 type of this hypothetical "containing object" is always the same as the
17033 declared type of the individual bit-field itself. The determination of the
17034 exact location of the "containing object" for a bit-field is rather
17035 complicated. It's handled by the `field_byte_offset' function (above).
17037 Note that it is the size (in bytes) of the hypothetical "containing object"
17038 which will be given in the DW_AT_byte_size attribute for this bit-field.
17039 (See `byte_size_attribute' above). */
17041 static inline void
17042 add_bit_offset_attribute (dw_die_ref die, tree decl)
17044 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
17045 tree type = DECL_BIT_FIELD_TYPE (decl);
17046 HOST_WIDE_INT bitpos_int;
17047 HOST_WIDE_INT highest_order_object_bit_offset;
17048 HOST_WIDE_INT highest_order_field_bit_offset;
17049 HOST_WIDE_INT bit_offset;
17051 /* Must be a field and a bit field. */
17052 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
17054 /* We can't yet handle bit-fields whose offsets are variable, so if we
17055 encounter such things, just return without generating any attribute
17056 whatsoever. Likewise for variable or too large size. */
17057 if (! tree_fits_shwi_p (bit_position (decl))
17058 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
17059 return;
17061 bitpos_int = int_bit_position (decl);
17063 /* Note that the bit offset is always the distance (in bits) from the
17064 highest-order bit of the "containing object" to the highest-order bit of
17065 the bit-field itself. Since the "high-order end" of any object or field
17066 is different on big-endian and little-endian machines, the computation
17067 below must take account of these differences. */
17068 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
17069 highest_order_field_bit_offset = bitpos_int;
17071 if (! BYTES_BIG_ENDIAN)
17073 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
17074 highest_order_object_bit_offset += simple_type_size_in_bits (type);
17077 bit_offset
17078 = (! BYTES_BIG_ENDIAN
17079 ? highest_order_object_bit_offset - highest_order_field_bit_offset
17080 : highest_order_field_bit_offset - highest_order_object_bit_offset);
17082 if (bit_offset < 0)
17083 add_AT_int (die, DW_AT_bit_offset, bit_offset);
17084 else
17085 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
17088 /* For a FIELD_DECL node which represents a bit field, output an attribute
17089 which specifies the length in bits of the given field. */
17091 static inline void
17092 add_bit_size_attribute (dw_die_ref die, tree decl)
17094 /* Must be a field and a bit field. */
17095 gcc_assert (TREE_CODE (decl) == FIELD_DECL
17096 && DECL_BIT_FIELD_TYPE (decl));
17098 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
17099 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
17102 /* If the compiled language is ANSI C, then add a 'prototyped'
17103 attribute, if arg types are given for the parameters of a function. */
17105 static inline void
17106 add_prototyped_attribute (dw_die_ref die, tree func_type)
17108 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
17110 case DW_LANG_C:
17111 case DW_LANG_C89:
17112 case DW_LANG_C99:
17113 case DW_LANG_C11:
17114 case DW_LANG_ObjC:
17115 if (prototype_p (func_type))
17116 add_AT_flag (die, DW_AT_prototyped, 1);
17117 break;
17118 default:
17119 break;
17123 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
17124 by looking in either the type declaration or object declaration
17125 equate table. */
17127 static inline dw_die_ref
17128 add_abstract_origin_attribute (dw_die_ref die, tree origin)
17130 dw_die_ref origin_die = NULL;
17132 if (TREE_CODE (origin) != FUNCTION_DECL)
17134 /* We may have gotten separated from the block for the inlined
17135 function, if we're in an exception handler or some such; make
17136 sure that the abstract function has been written out.
17138 Doing this for nested functions is wrong, however; functions are
17139 distinct units, and our context might not even be inline. */
17140 tree fn = origin;
17142 if (TYPE_P (fn))
17143 fn = TYPE_STUB_DECL (fn);
17145 fn = decl_function_context (fn);
17146 if (fn)
17147 dwarf2out_abstract_function (fn);
17150 if (DECL_P (origin))
17151 origin_die = lookup_decl_die (origin);
17152 else if (TYPE_P (origin))
17153 origin_die = lookup_type_die (origin);
17155 /* XXX: Functions that are never lowered don't always have correct block
17156 trees (in the case of java, they simply have no block tree, in some other
17157 languages). For these functions, there is nothing we can really do to
17158 output correct debug info for inlined functions in all cases. Rather
17159 than die, we'll just produce deficient debug info now, in that we will
17160 have variables without a proper abstract origin. In the future, when all
17161 functions are lowered, we should re-add a gcc_assert (origin_die)
17162 here. */
17164 if (origin_die)
17165 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
17166 return origin_die;
17169 /* We do not currently support the pure_virtual attribute. */
17171 static inline void
17172 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
17174 if (DECL_VINDEX (func_decl))
17176 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
17178 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
17179 add_AT_loc (die, DW_AT_vtable_elem_location,
17180 new_loc_descr (DW_OP_constu,
17181 tree_to_shwi (DECL_VINDEX (func_decl)),
17182 0));
17184 /* GNU extension: Record what type this method came from originally. */
17185 if (debug_info_level > DINFO_LEVEL_TERSE
17186 && DECL_CONTEXT (func_decl))
17187 add_AT_die_ref (die, DW_AT_containing_type,
17188 lookup_type_die (DECL_CONTEXT (func_decl)));
17192 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
17193 given decl. This used to be a vendor extension until after DWARF 4
17194 standardized it. */
17196 static void
17197 add_linkage_attr (dw_die_ref die, tree decl)
17199 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
17201 /* Mimic what assemble_name_raw does with a leading '*'. */
17202 if (name[0] == '*')
17203 name = &name[1];
17205 if (dwarf_version >= 4)
17206 add_AT_string (die, DW_AT_linkage_name, name);
17207 else
17208 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
17211 /* Add source coordinate attributes for the given decl. */
17213 static void
17214 add_src_coords_attributes (dw_die_ref die, tree decl)
17216 expanded_location s;
17218 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
17219 return;
17220 s = expand_location (DECL_SOURCE_LOCATION (decl));
17221 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
17222 add_AT_unsigned (die, DW_AT_decl_line, s.line);
17225 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
17227 static void
17228 add_linkage_name (dw_die_ref die, tree decl)
17230 if (debug_info_level > DINFO_LEVEL_NONE
17231 && (TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
17232 && TREE_PUBLIC (decl)
17233 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
17234 && die->die_tag != DW_TAG_member)
17236 /* Defer until we have an assembler name set. */
17237 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
17239 limbo_die_node *asm_name;
17241 asm_name = ggc_cleared_alloc<limbo_die_node> ();
17242 asm_name->die = die;
17243 asm_name->created_for = decl;
17244 asm_name->next = deferred_asm_name;
17245 deferred_asm_name = asm_name;
17247 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
17248 add_linkage_attr (die, decl);
17252 /* Add a DW_AT_name attribute and source coordinate attribute for the
17253 given decl, but only if it actually has a name. */
17255 static void
17256 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
17258 tree decl_name;
17260 decl_name = DECL_NAME (decl);
17261 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
17263 const char *name = dwarf2_name (decl, 0);
17264 if (name)
17265 add_name_attribute (die, name);
17266 if (! DECL_ARTIFICIAL (decl))
17267 add_src_coords_attributes (die, decl);
17269 add_linkage_name (die, decl);
17272 #ifdef VMS_DEBUGGING_INFO
17273 /* Get the function's name, as described by its RTL. This may be different
17274 from the DECL_NAME name used in the source file. */
17275 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
17277 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
17278 XEXP (DECL_RTL (decl), 0), false);
17279 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
17281 #endif /* VMS_DEBUGGING_INFO */
17284 #ifdef VMS_DEBUGGING_INFO
17285 /* Output the debug main pointer die for VMS */
17287 void
17288 dwarf2out_vms_debug_main_pointer (void)
17290 char label[MAX_ARTIFICIAL_LABEL_BYTES];
17291 dw_die_ref die;
17293 /* Allocate the VMS debug main subprogram die. */
17294 die = ggc_cleared_alloc<die_node> ();
17295 die->die_tag = DW_TAG_subprogram;
17296 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
17297 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
17298 current_function_funcdef_no);
17299 add_AT_lbl_id (die, DW_AT_entry_pc, label);
17301 /* Make it the first child of comp_unit_die (). */
17302 die->die_parent = comp_unit_die ();
17303 if (comp_unit_die ()->die_child)
17305 die->die_sib = comp_unit_die ()->die_child->die_sib;
17306 comp_unit_die ()->die_child->die_sib = die;
17308 else
17310 die->die_sib = die;
17311 comp_unit_die ()->die_child = die;
17314 #endif /* VMS_DEBUGGING_INFO */
17316 /* Push a new declaration scope. */
17318 static void
17319 push_decl_scope (tree scope)
17321 vec_safe_push (decl_scope_table, scope);
17324 /* Pop a declaration scope. */
17326 static inline void
17327 pop_decl_scope (void)
17329 decl_scope_table->pop ();
17332 /* walk_tree helper function for uses_local_type, below. */
17334 static tree
17335 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
17337 if (!TYPE_P (*tp))
17338 *walk_subtrees = 0;
17339 else
17341 tree name = TYPE_NAME (*tp);
17342 if (name && DECL_P (name) && decl_function_context (name))
17343 return *tp;
17345 return NULL_TREE;
17348 /* If TYPE involves a function-local type (including a local typedef to a
17349 non-local type), returns that type; otherwise returns NULL_TREE. */
17351 static tree
17352 uses_local_type (tree type)
17354 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
17355 return used;
17358 /* Return the DIE for the scope that immediately contains this type.
17359 Non-named types that do not involve a function-local type get global
17360 scope. Named types nested in namespaces or other types get their
17361 containing scope. All other types (i.e. function-local named types) get
17362 the current active scope. */
17364 static dw_die_ref
17365 scope_die_for (tree t, dw_die_ref context_die)
17367 dw_die_ref scope_die = NULL;
17368 tree containing_scope;
17370 /* Non-types always go in the current scope. */
17371 gcc_assert (TYPE_P (t));
17373 /* Use the scope of the typedef, rather than the scope of the type
17374 it refers to. */
17375 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
17376 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
17377 else
17378 containing_scope = TYPE_CONTEXT (t);
17380 /* Use the containing namespace if there is one. */
17381 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
17383 if (context_die == lookup_decl_die (containing_scope))
17384 /* OK */;
17385 else if (debug_info_level > DINFO_LEVEL_TERSE)
17386 context_die = get_context_die (containing_scope);
17387 else
17388 containing_scope = NULL_TREE;
17391 /* Ignore function type "scopes" from the C frontend. They mean that
17392 a tagged type is local to a parmlist of a function declarator, but
17393 that isn't useful to DWARF. */
17394 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
17395 containing_scope = NULL_TREE;
17397 if (SCOPE_FILE_SCOPE_P (containing_scope))
17399 /* If T uses a local type keep it local as well, to avoid references
17400 to function-local DIEs from outside the function. */
17401 if (current_function_decl && uses_local_type (t))
17402 scope_die = context_die;
17403 else
17404 scope_die = comp_unit_die ();
17406 else if (TYPE_P (containing_scope))
17408 /* For types, we can just look up the appropriate DIE. */
17409 if (debug_info_level > DINFO_LEVEL_TERSE)
17410 scope_die = get_context_die (containing_scope);
17411 else
17413 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
17414 if (scope_die == NULL)
17415 scope_die = comp_unit_die ();
17418 else
17419 scope_die = context_die;
17421 return scope_die;
17424 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
17426 static inline int
17427 local_scope_p (dw_die_ref context_die)
17429 for (; context_die; context_die = context_die->die_parent)
17430 if (context_die->die_tag == DW_TAG_inlined_subroutine
17431 || context_die->die_tag == DW_TAG_subprogram)
17432 return 1;
17434 return 0;
17437 /* Returns nonzero if CONTEXT_DIE is a class. */
17439 static inline int
17440 class_scope_p (dw_die_ref context_die)
17442 return (context_die
17443 && (context_die->die_tag == DW_TAG_structure_type
17444 || context_die->die_tag == DW_TAG_class_type
17445 || context_die->die_tag == DW_TAG_interface_type
17446 || context_die->die_tag == DW_TAG_union_type));
17449 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
17450 whether or not to treat a DIE in this context as a declaration. */
17452 static inline int
17453 class_or_namespace_scope_p (dw_die_ref context_die)
17455 return (class_scope_p (context_die)
17456 || (context_die && context_die->die_tag == DW_TAG_namespace));
17459 /* Many forms of DIEs require a "type description" attribute. This
17460 routine locates the proper "type descriptor" die for the type given
17461 by 'type' plus any additional qualifiers given by 'cv_quals', and
17462 adds a DW_AT_type attribute below the given die. */
17464 static void
17465 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
17466 dw_die_ref context_die)
17468 enum tree_code code = TREE_CODE (type);
17469 dw_die_ref type_die = NULL;
17471 /* ??? If this type is an unnamed subrange type of an integral, floating-point
17472 or fixed-point type, use the inner type. This is because we have no
17473 support for unnamed types in base_type_die. This can happen if this is
17474 an Ada subrange type. Correct solution is emit a subrange type die. */
17475 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
17476 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
17477 type = TREE_TYPE (type), code = TREE_CODE (type);
17479 if (code == ERROR_MARK
17480 /* Handle a special case. For functions whose return type is void, we
17481 generate *no* type attribute. (Note that no object may have type
17482 `void', so this only applies to function return types). */
17483 || code == VOID_TYPE)
17484 return;
17486 type_die = modified_type_die (type,
17487 cv_quals | TYPE_QUALS_NO_ADDR_SPACE (type),
17488 context_die);
17490 if (type_die != NULL)
17491 add_AT_die_ref (object_die, DW_AT_type, type_die);
17494 /* Given an object die, add the calling convention attribute for the
17495 function call type. */
17496 static void
17497 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
17499 enum dwarf_calling_convention value = DW_CC_normal;
17501 value = ((enum dwarf_calling_convention)
17502 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
17504 if (is_fortran ()
17505 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
17507 /* DWARF 2 doesn't provide a way to identify a program's source-level
17508 entry point. DW_AT_calling_convention attributes are only meant
17509 to describe functions' calling conventions. However, lacking a
17510 better way to signal the Fortran main program, we used this for
17511 a long time, following existing custom. Now, DWARF 4 has
17512 DW_AT_main_subprogram, which we add below, but some tools still
17513 rely on the old way, which we thus keep. */
17514 value = DW_CC_program;
17516 if (dwarf_version >= 4 || !dwarf_strict)
17517 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
17520 /* Only add the attribute if the backend requests it, and
17521 is not DW_CC_normal. */
17522 if (value && (value != DW_CC_normal))
17523 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
17526 /* Given a tree pointer to a struct, class, union, or enum type node, return
17527 a pointer to the (string) tag name for the given type, or zero if the type
17528 was declared without a tag. */
17530 static const char *
17531 type_tag (const_tree type)
17533 const char *name = 0;
17535 if (TYPE_NAME (type) != 0)
17537 tree t = 0;
17539 /* Find the IDENTIFIER_NODE for the type name. */
17540 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
17541 && !TYPE_NAMELESS (type))
17542 t = TYPE_NAME (type);
17544 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
17545 a TYPE_DECL node, regardless of whether or not a `typedef' was
17546 involved. */
17547 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
17548 && ! DECL_IGNORED_P (TYPE_NAME (type)))
17550 /* We want to be extra verbose. Don't call dwarf_name if
17551 DECL_NAME isn't set. The default hook for decl_printable_name
17552 doesn't like that, and in this context it's correct to return
17553 0, instead of "<anonymous>" or the like. */
17554 if (DECL_NAME (TYPE_NAME (type))
17555 && !DECL_NAMELESS (TYPE_NAME (type)))
17556 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
17559 /* Now get the name as a string, or invent one. */
17560 if (!name && t != 0)
17561 name = IDENTIFIER_POINTER (t);
17564 return (name == 0 || *name == '\0') ? 0 : name;
17567 /* Return the type associated with a data member, make a special check
17568 for bit field types. */
17570 static inline tree
17571 member_declared_type (const_tree member)
17573 return (DECL_BIT_FIELD_TYPE (member)
17574 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
17577 /* Get the decl's label, as described by its RTL. This may be different
17578 from the DECL_NAME name used in the source file. */
17580 #if 0
17581 static const char *
17582 decl_start_label (tree decl)
17584 rtx x;
17585 const char *fnname;
17587 x = DECL_RTL (decl);
17588 gcc_assert (MEM_P (x));
17590 x = XEXP (x, 0);
17591 gcc_assert (GET_CODE (x) == SYMBOL_REF);
17593 fnname = XSTR (x, 0);
17594 return fnname;
17596 #endif
17598 /* For variable-length arrays that have been previously generated, but
17599 may be incomplete due to missing subscript info, fill the subscript
17600 info. Return TRUE if this is one of those cases. */
17601 static bool
17602 fill_variable_array_bounds (tree type)
17604 if (TREE_ASM_WRITTEN (type)
17605 && TREE_CODE (type) == ARRAY_TYPE
17606 && variably_modified_type_p (type, NULL))
17608 dw_die_ref array_die = lookup_type_die (type);
17609 if (!array_die)
17610 return false;
17611 add_subscript_info (array_die, type, !is_ada ());
17612 return true;
17614 return false;
17617 /* These routines generate the internal representation of the DIE's for
17618 the compilation unit. Debugging information is collected by walking
17619 the declaration trees passed in from dwarf2out_decl(). */
17621 static void
17622 gen_array_type_die (tree type, dw_die_ref context_die)
17624 dw_die_ref array_die;
17626 /* GNU compilers represent multidimensional array types as sequences of one
17627 dimensional array types whose element types are themselves array types.
17628 We sometimes squish that down to a single array_type DIE with multiple
17629 subscripts in the Dwarf debugging info. The draft Dwarf specification
17630 say that we are allowed to do this kind of compression in C, because
17631 there is no difference between an array of arrays and a multidimensional
17632 array. We don't do this for Ada to remain as close as possible to the
17633 actual representation, which is especially important against the language
17634 flexibilty wrt arrays of variable size. */
17636 bool collapse_nested_arrays = !is_ada ();
17638 if (fill_variable_array_bounds (type))
17639 return;
17641 dw_die_ref scope_die = scope_die_for (type, context_die);
17642 tree element_type;
17644 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
17645 DW_TAG_string_type doesn't have DW_AT_type attribute). */
17646 if (TYPE_STRING_FLAG (type)
17647 && TREE_CODE (type) == ARRAY_TYPE
17648 && is_fortran ()
17649 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
17651 HOST_WIDE_INT size;
17653 array_die = new_die (DW_TAG_string_type, scope_die, type);
17654 add_name_attribute (array_die, type_tag (type));
17655 equate_type_number_to_die (type, array_die);
17656 size = int_size_in_bytes (type);
17657 if (size >= 0)
17658 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17659 else if (TYPE_DOMAIN (type) != NULL_TREE
17660 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
17661 && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
17663 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
17664 dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2, NULL);
17666 size = int_size_in_bytes (TREE_TYPE (szdecl));
17667 if (loc && size > 0)
17669 add_AT_location_description (array_die, DW_AT_string_length, loc);
17670 if (size != DWARF2_ADDR_SIZE)
17671 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17674 return;
17677 array_die = new_die (DW_TAG_array_type, scope_die, type);
17678 add_name_attribute (array_die, type_tag (type));
17679 equate_type_number_to_die (type, array_die);
17681 if (TREE_CODE (type) == VECTOR_TYPE)
17682 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
17684 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
17685 if (is_fortran ()
17686 && TREE_CODE (type) == ARRAY_TYPE
17687 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
17688 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
17689 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17691 #if 0
17692 /* We default the array ordering. SDB will probably do
17693 the right things even if DW_AT_ordering is not present. It's not even
17694 an issue until we start to get into multidimensional arrays anyway. If
17695 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
17696 then we'll have to put the DW_AT_ordering attribute back in. (But if
17697 and when we find out that we need to put these in, we will only do so
17698 for multidimensional arrays. */
17699 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17700 #endif
17702 if (TREE_CODE (type) == VECTOR_TYPE)
17704 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
17705 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
17706 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node, NULL);
17707 add_bound_info (subrange_die, DW_AT_upper_bound,
17708 size_int (TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
17710 else
17711 add_subscript_info (array_die, type, collapse_nested_arrays);
17713 /* Add representation of the type of the elements of this array type and
17714 emit the corresponding DIE if we haven't done it already. */
17715 element_type = TREE_TYPE (type);
17716 if (collapse_nested_arrays)
17717 while (TREE_CODE (element_type) == ARRAY_TYPE)
17719 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
17720 break;
17721 element_type = TREE_TYPE (element_type);
17724 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED, context_die);
17726 add_gnat_descriptive_type_attribute (array_die, type, context_die);
17727 if (TYPE_ARTIFICIAL (type))
17728 add_AT_flag (array_die, DW_AT_artificial, 1);
17730 if (get_AT (array_die, DW_AT_name))
17731 add_pubtype (type, array_die);
17734 /* This routine generates DIE for array with hidden descriptor, details
17735 are filled into *info by a langhook. */
17737 static void
17738 gen_descr_array_type_die (tree type, struct array_descr_info *info,
17739 dw_die_ref context_die)
17741 const dw_die_ref scope_die = scope_die_for (type, context_die);
17742 const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
17743 const struct loc_descr_context context = { type, info->base_decl };
17744 int dim;
17746 add_name_attribute (array_die, type_tag (type));
17747 equate_type_number_to_die (type, array_die);
17749 if (info->ndimensions > 1)
17750 switch (info->ordering)
17752 case array_descr_ordering_row_major:
17753 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17754 break;
17755 case array_descr_ordering_column_major:
17756 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17757 break;
17758 default:
17759 break;
17762 if (dwarf_version >= 3 || !dwarf_strict)
17764 if (info->data_location)
17765 add_scalar_info (array_die, DW_AT_data_location, info->data_location,
17766 dw_scalar_form_exprloc, &context);
17767 if (info->associated)
17768 add_scalar_info (array_die, DW_AT_associated, info->associated,
17769 dw_scalar_form_constant
17770 | dw_scalar_form_exprloc
17771 | dw_scalar_form_reference, &context);
17772 if (info->allocated)
17773 add_scalar_info (array_die, DW_AT_allocated, info->allocated,
17774 dw_scalar_form_constant
17775 | dw_scalar_form_exprloc
17776 | dw_scalar_form_reference, &context);
17779 add_gnat_descriptive_type_attribute (array_die, type, context_die);
17781 for (dim = 0; dim < info->ndimensions; dim++)
17783 dw_die_ref subrange_die
17784 = new_die (DW_TAG_subrange_type, array_die, NULL);
17786 if (info->dimen[dim].bounds_type)
17787 add_type_attribute (subrange_die,
17788 info->dimen[dim].bounds_type, 0,
17789 context_die);
17790 if (info->dimen[dim].lower_bound)
17791 add_bound_info (subrange_die, DW_AT_lower_bound,
17792 info->dimen[dim].lower_bound, &context);
17793 if (info->dimen[dim].upper_bound)
17794 add_bound_info (subrange_die, DW_AT_upper_bound,
17795 info->dimen[dim].upper_bound, &context);
17796 if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
17797 add_scalar_info (subrange_die, DW_AT_byte_stride,
17798 info->dimen[dim].stride,
17799 dw_scalar_form_constant
17800 | dw_scalar_form_exprloc
17801 | dw_scalar_form_reference,
17802 &context);
17805 gen_type_die (info->element_type, context_die);
17806 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
17807 context_die);
17809 if (get_AT (array_die, DW_AT_name))
17810 add_pubtype (type, array_die);
17813 #if 0
17814 static void
17815 gen_entry_point_die (tree decl, dw_die_ref context_die)
17817 tree origin = decl_ultimate_origin (decl);
17818 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
17820 if (origin != NULL)
17821 add_abstract_origin_attribute (decl_die, origin);
17822 else
17824 add_name_and_src_coords_attributes (decl_die, decl);
17825 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
17826 TYPE_UNQUALIFIED, context_die);
17829 if (DECL_ABSTRACT_P (decl))
17830 equate_decl_number_to_die (decl, decl_die);
17831 else
17832 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
17834 #endif
17836 /* Walk through the list of incomplete types again, trying once more to
17837 emit full debugging info for them. */
17839 static void
17840 retry_incomplete_types (void)
17842 int i;
17844 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
17845 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
17846 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
17849 /* Determine what tag to use for a record type. */
17851 static enum dwarf_tag
17852 record_type_tag (tree type)
17854 if (! lang_hooks.types.classify_record)
17855 return DW_TAG_structure_type;
17857 switch (lang_hooks.types.classify_record (type))
17859 case RECORD_IS_STRUCT:
17860 return DW_TAG_structure_type;
17862 case RECORD_IS_CLASS:
17863 return DW_TAG_class_type;
17865 case RECORD_IS_INTERFACE:
17866 if (dwarf_version >= 3 || !dwarf_strict)
17867 return DW_TAG_interface_type;
17868 return DW_TAG_structure_type;
17870 default:
17871 gcc_unreachable ();
17875 /* Generate a DIE to represent an enumeration type. Note that these DIEs
17876 include all of the information about the enumeration values also. Each
17877 enumerated type name/value is listed as a child of the enumerated type
17878 DIE. */
17880 static dw_die_ref
17881 gen_enumeration_type_die (tree type, dw_die_ref context_die)
17883 dw_die_ref type_die = lookup_type_die (type);
17885 if (type_die == NULL)
17887 type_die = new_die (DW_TAG_enumeration_type,
17888 scope_die_for (type, context_die), type);
17889 equate_type_number_to_die (type, type_die);
17890 add_name_attribute (type_die, type_tag (type));
17891 if (dwarf_version >= 4 || !dwarf_strict)
17893 if (ENUM_IS_SCOPED (type))
17894 add_AT_flag (type_die, DW_AT_enum_class, 1);
17895 if (ENUM_IS_OPAQUE (type))
17896 add_AT_flag (type_die, DW_AT_declaration, 1);
17899 else if (! TYPE_SIZE (type))
17900 return type_die;
17901 else
17902 remove_AT (type_die, DW_AT_declaration);
17904 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
17905 given enum type is incomplete, do not generate the DW_AT_byte_size
17906 attribute or the DW_AT_element_list attribute. */
17907 if (TYPE_SIZE (type))
17909 tree link;
17911 TREE_ASM_WRITTEN (type) = 1;
17912 add_byte_size_attribute (type_die, type);
17913 if (dwarf_version >= 3 || !dwarf_strict)
17915 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
17916 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED,
17917 context_die);
17919 if (TYPE_STUB_DECL (type) != NULL_TREE)
17921 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
17922 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
17925 /* If the first reference to this type was as the return type of an
17926 inline function, then it may not have a parent. Fix this now. */
17927 if (type_die->die_parent == NULL)
17928 add_child_die (scope_die_for (type, context_die), type_die);
17930 for (link = TYPE_VALUES (type);
17931 link != NULL; link = TREE_CHAIN (link))
17933 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
17934 tree value = TREE_VALUE (link);
17936 add_name_attribute (enum_die,
17937 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
17939 if (TREE_CODE (value) == CONST_DECL)
17940 value = DECL_INITIAL (value);
17942 if (simple_type_size_in_bits (TREE_TYPE (value))
17943 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
17945 /* For constant forms created by add_AT_unsigned DWARF
17946 consumers (GDB, elfutils, etc.) always zero extend
17947 the value. Only when the actual value is negative
17948 do we need to use add_AT_int to generate a constant
17949 form that can represent negative values. */
17950 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
17951 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
17952 add_AT_unsigned (enum_die, DW_AT_const_value,
17953 (unsigned HOST_WIDE_INT) val);
17954 else
17955 add_AT_int (enum_die, DW_AT_const_value, val);
17957 else
17958 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
17959 that here. TODO: This should be re-worked to use correct
17960 signed/unsigned double tags for all cases. */
17961 add_AT_wide (enum_die, DW_AT_const_value, value);
17964 add_gnat_descriptive_type_attribute (type_die, type, context_die);
17965 if (TYPE_ARTIFICIAL (type))
17966 add_AT_flag (type_die, DW_AT_artificial, 1);
17968 else
17969 add_AT_flag (type_die, DW_AT_declaration, 1);
17971 add_pubtype (type, type_die);
17973 return type_die;
17976 /* Generate a DIE to represent either a real live formal parameter decl or to
17977 represent just the type of some formal parameter position in some function
17978 type.
17980 Note that this routine is a bit unusual because its argument may be a
17981 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
17982 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
17983 node. If it's the former then this function is being called to output a
17984 DIE to represent a formal parameter object (or some inlining thereof). If
17985 it's the latter, then this function is only being called to output a
17986 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
17987 argument type of some subprogram type.
17988 If EMIT_NAME_P is true, name and source coordinate attributes
17989 are emitted. */
17991 static dw_die_ref
17992 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
17993 dw_die_ref context_die)
17995 tree node_or_origin = node ? node : origin;
17996 tree ultimate_origin;
17997 dw_die_ref parm_die = NULL;
17999 if (TREE_CODE_CLASS (TREE_CODE (node_or_origin)) == tcc_declaration)
18001 parm_die = lookup_decl_die (node);
18003 /* If the contexts differ, we may not be talking about the same
18004 thing. */
18005 if (parm_die && parm_die->die_parent != context_die)
18007 if (!DECL_ABSTRACT_P (node))
18009 /* This can happen when creating an inlined instance, in
18010 which case we need to create a new DIE that will get
18011 annotated with DW_AT_abstract_origin. */
18012 parm_die = NULL;
18014 else
18016 /* FIXME: Reuse DIE even with a differing context.
18018 This can happen when calling
18019 dwarf2out_abstract_function to build debug info for
18020 the abstract instance of a function for which we have
18021 already generated a DIE in
18022 dwarf2out_early_global_decl.
18024 Once we remove dwarf2out_abstract_function, we should
18025 have a call to gcc_unreachable here. */
18029 if (parm_die && parm_die->die_parent == NULL)
18031 /* Check that parm_die already has the right attributes that
18032 we would have added below. If any attributes are
18033 missing, fall through to add them. */
18034 if (! DECL_ABSTRACT_P (node_or_origin)
18035 && !get_AT (parm_die, DW_AT_location)
18036 && !get_AT (parm_die, DW_AT_const_value))
18037 /* We are missing location info, and are about to add it. */
18039 else
18041 add_child_die (context_die, parm_die);
18042 return parm_die;
18047 /* If we have a previously generated DIE, use it, unless this is an
18048 concrete instance (origin != NULL), in which case we need a new
18049 DIE with a corresponding DW_AT_abstract_origin. */
18050 bool reusing_die;
18051 if (parm_die && origin == NULL)
18052 reusing_die = true;
18053 else
18055 parm_die = new_die (DW_TAG_formal_parameter, context_die, node);
18056 reusing_die = false;
18059 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
18061 case tcc_declaration:
18062 ultimate_origin = decl_ultimate_origin (node_or_origin);
18063 if (node || ultimate_origin)
18064 origin = ultimate_origin;
18066 if (reusing_die)
18067 goto add_location;
18069 if (origin != NULL)
18070 add_abstract_origin_attribute (parm_die, origin);
18071 else if (emit_name_p)
18072 add_name_and_src_coords_attributes (parm_die, node);
18073 if (origin == NULL
18074 || (! DECL_ABSTRACT_P (node_or_origin)
18075 && variably_modified_type_p (TREE_TYPE (node_or_origin),
18076 decl_function_context
18077 (node_or_origin))))
18079 tree type = TREE_TYPE (node_or_origin);
18080 if (decl_by_reference_p (node_or_origin))
18081 add_type_attribute (parm_die, TREE_TYPE (type),
18082 TYPE_UNQUALIFIED, context_die);
18083 else
18084 add_type_attribute (parm_die, type,
18085 decl_quals (node_or_origin),
18086 context_die);
18088 if (origin == NULL && DECL_ARTIFICIAL (node))
18089 add_AT_flag (parm_die, DW_AT_artificial, 1);
18090 add_location:
18091 if (node && node != origin)
18092 equate_decl_number_to_die (node, parm_die);
18093 if (! DECL_ABSTRACT_P (node_or_origin))
18094 add_location_or_const_value_attribute (parm_die, node_or_origin,
18095 node == NULL, DW_AT_location);
18097 break;
18099 case tcc_type:
18100 /* We were called with some kind of a ..._TYPE node. */
18101 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED,
18102 context_die);
18103 break;
18105 default:
18106 gcc_unreachable ();
18109 return parm_die;
18112 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
18113 children DW_TAG_formal_parameter DIEs representing the arguments of the
18114 parameter pack.
18116 PARM_PACK must be a function parameter pack.
18117 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
18118 must point to the subsequent arguments of the function PACK_ARG belongs to.
18119 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
18120 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
18121 following the last one for which a DIE was generated. */
18123 static dw_die_ref
18124 gen_formal_parameter_pack_die (tree parm_pack,
18125 tree pack_arg,
18126 dw_die_ref subr_die,
18127 tree *next_arg)
18129 tree arg;
18130 dw_die_ref parm_pack_die;
18132 gcc_assert (parm_pack
18133 && lang_hooks.function_parameter_pack_p (parm_pack)
18134 && subr_die);
18136 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
18137 add_src_coords_attributes (parm_pack_die, parm_pack);
18139 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
18141 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
18142 parm_pack))
18143 break;
18144 gen_formal_parameter_die (arg, NULL,
18145 false /* Don't emit name attribute. */,
18146 parm_pack_die);
18148 if (next_arg)
18149 *next_arg = arg;
18150 return parm_pack_die;
18153 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
18154 at the end of an (ANSI prototyped) formal parameters list. */
18156 static void
18157 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
18159 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
18162 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
18163 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
18164 parameters as specified in some function type specification (except for
18165 those which appear as part of a function *definition*). */
18167 static void
18168 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
18170 tree link;
18171 tree formal_type = NULL;
18172 tree first_parm_type;
18173 tree arg;
18175 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
18177 arg = DECL_ARGUMENTS (function_or_method_type);
18178 function_or_method_type = TREE_TYPE (function_or_method_type);
18180 else
18181 arg = NULL_TREE;
18183 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
18185 /* Make our first pass over the list of formal parameter types and output a
18186 DW_TAG_formal_parameter DIE for each one. */
18187 for (link = first_parm_type; link; )
18189 dw_die_ref parm_die;
18191 formal_type = TREE_VALUE (link);
18192 if (formal_type == void_type_node)
18193 break;
18195 /* Output a (nameless) DIE to represent the formal parameter itself. */
18196 if (!POINTER_BOUNDS_TYPE_P (formal_type))
18198 parm_die = gen_formal_parameter_die (formal_type, NULL,
18199 true /* Emit name attribute. */,
18200 context_die);
18201 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
18202 && link == first_parm_type)
18204 add_AT_flag (parm_die, DW_AT_artificial, 1);
18205 if (dwarf_version >= 3 || !dwarf_strict)
18206 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
18208 else if (arg && DECL_ARTIFICIAL (arg))
18209 add_AT_flag (parm_die, DW_AT_artificial, 1);
18212 link = TREE_CHAIN (link);
18213 if (arg)
18214 arg = DECL_CHAIN (arg);
18217 /* If this function type has an ellipsis, add a
18218 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
18219 if (formal_type != void_type_node)
18220 gen_unspecified_parameters_die (function_or_method_type, context_die);
18222 /* Make our second (and final) pass over the list of formal parameter types
18223 and output DIEs to represent those types (as necessary). */
18224 for (link = TYPE_ARG_TYPES (function_or_method_type);
18225 link && TREE_VALUE (link);
18226 link = TREE_CHAIN (link))
18227 gen_type_die (TREE_VALUE (link), context_die);
18230 /* We want to generate the DIE for TYPE so that we can generate the
18231 die for MEMBER, which has been defined; we will need to refer back
18232 to the member declaration nested within TYPE. If we're trying to
18233 generate minimal debug info for TYPE, processing TYPE won't do the
18234 trick; we need to attach the member declaration by hand. */
18236 static void
18237 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
18239 gen_type_die (type, context_die);
18241 /* If we're trying to avoid duplicate debug info, we may not have
18242 emitted the member decl for this function. Emit it now. */
18243 if (TYPE_STUB_DECL (type)
18244 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
18245 && ! lookup_decl_die (member))
18247 dw_die_ref type_die;
18248 gcc_assert (!decl_ultimate_origin (member));
18250 push_decl_scope (type);
18251 type_die = lookup_type_die_strip_naming_typedef (type);
18252 if (TREE_CODE (member) == FUNCTION_DECL)
18253 gen_subprogram_die (member, type_die);
18254 else if (TREE_CODE (member) == FIELD_DECL)
18256 /* Ignore the nameless fields that are used to skip bits but handle
18257 C++ anonymous unions and structs. */
18258 if (DECL_NAME (member) != NULL_TREE
18259 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
18260 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
18262 gen_type_die (member_declared_type (member), type_die);
18263 gen_field_die (member, type_die);
18266 else
18267 gen_variable_die (member, NULL_TREE, type_die);
18269 pop_decl_scope ();
18273 /* Forward declare these functions, because they are mutually recursive
18274 with their set_block_* pairing functions. */
18275 static void set_decl_origin_self (tree);
18276 static void set_decl_abstract_flags (tree, vec<tree> &);
18278 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
18279 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
18280 that it points to the node itself, thus indicating that the node is its
18281 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
18282 the given node is NULL, recursively descend the decl/block tree which
18283 it is the root of, and for each other ..._DECL or BLOCK node contained
18284 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
18285 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
18286 values to point to themselves. */
18288 static void
18289 set_block_origin_self (tree stmt)
18291 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
18293 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
18296 tree local_decl;
18298 for (local_decl = BLOCK_VARS (stmt);
18299 local_decl != NULL_TREE;
18300 local_decl = DECL_CHAIN (local_decl))
18301 /* Do not recurse on nested functions since the inlining status
18302 of parent and child can be different as per the DWARF spec. */
18303 if (TREE_CODE (local_decl) != FUNCTION_DECL
18304 && !DECL_EXTERNAL (local_decl))
18305 set_decl_origin_self (local_decl);
18309 tree subblock;
18311 for (subblock = BLOCK_SUBBLOCKS (stmt);
18312 subblock != NULL_TREE;
18313 subblock = BLOCK_CHAIN (subblock))
18314 set_block_origin_self (subblock); /* Recurse. */
18319 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
18320 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
18321 node to so that it points to the node itself, thus indicating that the
18322 node represents its own (abstract) origin. Additionally, if the
18323 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
18324 the decl/block tree of which the given node is the root of, and for
18325 each other ..._DECL or BLOCK node contained therein whose
18326 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
18327 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
18328 point to themselves. */
18330 static void
18331 set_decl_origin_self (tree decl)
18333 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
18335 DECL_ABSTRACT_ORIGIN (decl) = decl;
18336 if (TREE_CODE (decl) == FUNCTION_DECL)
18338 tree arg;
18340 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
18341 DECL_ABSTRACT_ORIGIN (arg) = arg;
18342 if (DECL_INITIAL (decl) != NULL_TREE
18343 && DECL_INITIAL (decl) != error_mark_node)
18344 set_block_origin_self (DECL_INITIAL (decl));
18349 /* Given a pointer to some BLOCK node, set the BLOCK_ABSTRACT flag to 1
18350 and if it wasn't 1 before, push it to abstract_vec vector.
18351 For all local decls and all local sub-blocks (recursively) do it
18352 too. */
18354 static void
18355 set_block_abstract_flags (tree stmt, vec<tree> &abstract_vec)
18357 tree local_decl;
18358 tree subblock;
18359 unsigned int i;
18361 if (!BLOCK_ABSTRACT (stmt))
18363 abstract_vec.safe_push (stmt);
18364 BLOCK_ABSTRACT (stmt) = 1;
18367 for (local_decl = BLOCK_VARS (stmt);
18368 local_decl != NULL_TREE;
18369 local_decl = DECL_CHAIN (local_decl))
18370 if (! DECL_EXTERNAL (local_decl))
18371 set_decl_abstract_flags (local_decl, abstract_vec);
18373 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
18375 local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
18376 if ((TREE_CODE (local_decl) == VAR_DECL && !TREE_STATIC (local_decl))
18377 || TREE_CODE (local_decl) == PARM_DECL)
18378 set_decl_abstract_flags (local_decl, abstract_vec);
18381 for (subblock = BLOCK_SUBBLOCKS (stmt);
18382 subblock != NULL_TREE;
18383 subblock = BLOCK_CHAIN (subblock))
18384 set_block_abstract_flags (subblock, abstract_vec);
18387 /* Given a pointer to some ..._DECL node, set DECL_ABSTRACT_P flag on it
18388 to 1 and if it wasn't 1 before, push to abstract_vec vector.
18389 In the case where the decl is a FUNCTION_DECL also set the abstract
18390 flags for all of the parameters, local vars, local
18391 blocks and sub-blocks (recursively). */
18393 static void
18394 set_decl_abstract_flags (tree decl, vec<tree> &abstract_vec)
18396 if (!DECL_ABSTRACT_P (decl))
18398 abstract_vec.safe_push (decl);
18399 DECL_ABSTRACT_P (decl) = 1;
18402 if (TREE_CODE (decl) == FUNCTION_DECL)
18404 tree arg;
18406 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
18407 if (!DECL_ABSTRACT_P (arg))
18409 abstract_vec.safe_push (arg);
18410 DECL_ABSTRACT_P (arg) = 1;
18412 if (DECL_INITIAL (decl) != NULL_TREE
18413 && DECL_INITIAL (decl) != error_mark_node)
18414 set_block_abstract_flags (DECL_INITIAL (decl), abstract_vec);
18418 /* Generate the DWARF2 info for the "abstract" instance of a function which we
18419 may later generate inlined and/or out-of-line instances of.
18421 FIXME: In the early-dwarf world, this function, and most of the
18422 DECL_ABSTRACT code should be obsoleted. The early DIE _is_
18423 the abstract instance. All we would need to do is annotate
18424 the early DIE with the appropriate DW_AT_inline in late
18425 dwarf (perhaps in gen_inlined_subroutine_die).
18427 However, we can't do this yet, because LTO streaming of DIEs
18428 has not been implemented yet. */
18430 static void
18431 dwarf2out_abstract_function (tree decl)
18433 dw_die_ref old_die;
18434 tree save_fn;
18435 tree context;
18436 hash_table<decl_loc_hasher> *old_decl_loc_table;
18437 hash_table<dw_loc_list_hasher> *old_cached_dw_loc_list_table;
18438 int old_call_site_count, old_tail_call_site_count;
18439 struct call_arg_loc_node *old_call_arg_locations;
18441 /* Make sure we have the actual abstract inline, not a clone. */
18442 decl = DECL_ORIGIN (decl);
18444 old_die = lookup_decl_die (decl);
18445 if (old_die && get_AT (old_die, DW_AT_inline))
18446 /* We've already generated the abstract instance. */
18447 return;
18449 /* We can be called while recursively when seeing block defining inlined subroutine
18450 DIE. Be sure to not clobber the outer location table nor use it or we would
18451 get locations in abstract instantces. */
18452 old_decl_loc_table = decl_loc_table;
18453 decl_loc_table = NULL;
18454 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
18455 cached_dw_loc_list_table = NULL;
18456 old_call_arg_locations = call_arg_locations;
18457 call_arg_locations = NULL;
18458 old_call_site_count = call_site_count;
18459 call_site_count = -1;
18460 old_tail_call_site_count = tail_call_site_count;
18461 tail_call_site_count = -1;
18463 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
18464 we don't get confused by DECL_ABSTRACT_P. */
18465 if (debug_info_level > DINFO_LEVEL_TERSE)
18467 context = decl_class_context (decl);
18468 if (context)
18469 gen_type_die_for_member
18470 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
18473 /* Pretend we've just finished compiling this function. */
18474 save_fn = current_function_decl;
18475 current_function_decl = decl;
18477 auto_vec<tree, 64> abstract_vec;
18478 set_decl_abstract_flags (decl, abstract_vec);
18479 dwarf2out_decl (decl);
18480 unsigned int i;
18481 tree t;
18482 FOR_EACH_VEC_ELT (abstract_vec, i, t)
18483 if (TREE_CODE (t) == BLOCK)
18484 BLOCK_ABSTRACT (t) = 0;
18485 else
18486 DECL_ABSTRACT_P (t) = 0;
18488 current_function_decl = save_fn;
18489 decl_loc_table = old_decl_loc_table;
18490 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
18491 call_arg_locations = old_call_arg_locations;
18492 call_site_count = old_call_site_count;
18493 tail_call_site_count = old_tail_call_site_count;
18496 /* Helper function of premark_used_types() which gets called through
18497 htab_traverse.
18499 Marks the DIE of a given type in *SLOT as perennial, so it never gets
18500 marked as unused by prune_unused_types. */
18502 bool
18503 premark_used_types_helper (tree const &type, void *)
18505 dw_die_ref die;
18507 die = lookup_type_die (type);
18508 if (die != NULL)
18509 die->die_perennial_p = 1;
18510 return true;
18513 /* Helper function of premark_types_used_by_global_vars which gets called
18514 through htab_traverse.
18516 Marks the DIE of a given type in *SLOT as perennial, so it never gets
18517 marked as unused by prune_unused_types. The DIE of the type is marked
18518 only if the global variable using the type will actually be emitted. */
18521 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
18522 void *)
18524 struct types_used_by_vars_entry *entry;
18525 dw_die_ref die;
18527 entry = (struct types_used_by_vars_entry *) *slot;
18528 gcc_assert (entry->type != NULL
18529 && entry->var_decl != NULL);
18530 die = lookup_type_die (entry->type);
18531 if (die)
18533 /* Ask cgraph if the global variable really is to be emitted.
18534 If yes, then we'll keep the DIE of ENTRY->TYPE. */
18535 varpool_node *node = varpool_node::get (entry->var_decl);
18536 if (node && node->definition)
18538 die->die_perennial_p = 1;
18539 /* Keep the parent DIEs as well. */
18540 while ((die = die->die_parent) && die->die_perennial_p == 0)
18541 die->die_perennial_p = 1;
18544 return 1;
18547 /* Mark all members of used_types_hash as perennial. */
18549 static void
18550 premark_used_types (struct function *fun)
18552 if (fun && fun->used_types_hash)
18553 fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
18556 /* Mark all members of types_used_by_vars_entry as perennial. */
18558 static void
18559 premark_types_used_by_global_vars (void)
18561 if (types_used_by_vars_hash)
18562 types_used_by_vars_hash
18563 ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
18566 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
18567 for CA_LOC call arg loc node. */
18569 static dw_die_ref
18570 gen_call_site_die (tree decl, dw_die_ref subr_die,
18571 struct call_arg_loc_node *ca_loc)
18573 dw_die_ref stmt_die = NULL, die;
18574 tree block = ca_loc->block;
18576 while (block
18577 && block != DECL_INITIAL (decl)
18578 && TREE_CODE (block) == BLOCK)
18580 stmt_die = BLOCK_DIE (block);
18581 if (stmt_die)
18582 break;
18583 block = BLOCK_SUPERCONTEXT (block);
18585 if (stmt_die == NULL)
18586 stmt_die = subr_die;
18587 die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
18588 add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
18589 if (ca_loc->tail_call_p)
18590 add_AT_flag (die, DW_AT_GNU_tail_call, 1);
18591 if (ca_loc->symbol_ref)
18593 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
18594 if (tdie)
18595 add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
18596 else
18597 add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref, false);
18599 return die;
18602 /* Generate a DIE to represent a declared function (either file-scope or
18603 block-local). */
18605 static void
18606 gen_subprogram_die (tree decl, dw_die_ref context_die)
18608 tree origin = decl_ultimate_origin (decl);
18609 dw_die_ref subr_die;
18610 dw_die_ref old_die = lookup_decl_die (decl);
18612 /* This function gets called multiple times for different stages of
18613 the debug process. For example, for func() in this code:
18615 namespace S
18617 void func() { ... }
18620 ...we get called 4 times. Twice in early debug and twice in
18621 late debug:
18623 Early debug
18624 -----------
18626 1. Once while generating func() within the namespace. This is
18627 the declaration. The declaration bit below is set, as the
18628 context is the namespace.
18630 A new DIE will be generated with DW_AT_declaration set.
18632 2. Once for func() itself. This is the specification. The
18633 declaration bit below is clear as the context is the CU.
18635 We will use the cached DIE from (1) to create a new DIE with
18636 DW_AT_specification pointing to the declaration in (1).
18638 Late debug via rest_of_handle_final()
18639 -------------------------------------
18641 3. Once generating func() within the namespace. This is also the
18642 declaration, as in (1), but this time we will early exit below
18643 as we have a cached DIE and a declaration needs no additional
18644 annotations (no locations), as the source declaration line
18645 info is enough.
18647 4. Once for func() itself. As in (2), this is the specification,
18648 but this time we will re-use the cached DIE, and just annotate
18649 it with the location information that should now be available.
18651 For something without namespaces, but with abstract instances, we
18652 are also called a multiple times:
18654 class Base
18656 public:
18657 Base (); // constructor declaration (1)
18660 Base::Base () { } // constructor specification (2)
18662 Early debug
18663 -----------
18665 1. Once for the Base() constructor by virtue of it being a
18666 member of the Base class. This is done via
18667 rest_of_type_compilation.
18669 This is a declaration, so a new DIE will be created with
18670 DW_AT_declaration.
18672 2. Once for the Base() constructor definition, but this time
18673 while generating the abstract instance of the base
18674 constructor (__base_ctor) which is being generated via early
18675 debug of reachable functions.
18677 Even though we have a cached version of the declaration (1),
18678 we will create a DW_AT_specification of the declaration DIE
18679 in (1).
18681 3. Once for the __base_ctor itself, but this time, we generate
18682 an DW_AT_abstract_origin version of the DW_AT_specification in
18683 (2).
18685 Late debug via rest_of_handle_final
18686 -----------------------------------
18688 4. One final time for the __base_ctor (which will have a cached
18689 DIE with DW_AT_abstract_origin created in (3). This time,
18690 we will just annotate the location information now
18691 available.
18693 int declaration = (current_function_decl != decl
18694 || class_or_namespace_scope_p (context_die));
18696 premark_used_types (DECL_STRUCT_FUNCTION (decl));
18698 /* Now that the C++ front end lazily declares artificial member fns, we
18699 might need to retrofit the declaration into its class. */
18700 if (!declaration && !origin && !old_die
18701 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
18702 && !class_or_namespace_scope_p (context_die)
18703 && debug_info_level > DINFO_LEVEL_TERSE)
18704 old_die = force_decl_die (decl);
18706 /* An inlined instance, tag a new DIE with DW_AT_abstract_origin. */
18707 if (origin != NULL)
18709 gcc_assert (!declaration || local_scope_p (context_die));
18711 /* Fixup die_parent for the abstract instance of a nested
18712 inline function. */
18713 if (old_die && old_die->die_parent == NULL)
18714 add_child_die (context_die, old_die);
18716 if (old_die && get_AT_ref (old_die, DW_AT_abstract_origin))
18718 /* If we have a DW_AT_abstract_origin we have a working
18719 cached version. */
18720 subr_die = old_die;
18722 else
18724 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18725 add_abstract_origin_attribute (subr_die, origin);
18726 /* This is where the actual code for a cloned function is.
18727 Let's emit linkage name attribute for it. This helps
18728 debuggers to e.g, set breakpoints into
18729 constructors/destructors when the user asks "break
18730 K::K". */
18731 add_linkage_name (subr_die, decl);
18734 /* A cached copy, possibly from early dwarf generation. Reuse as
18735 much as possible. */
18736 else if (old_die)
18738 /* A declaration that has been previously dumped needs no
18739 additional information. */
18740 if (declaration)
18741 return;
18743 if (!get_AT_flag (old_die, DW_AT_declaration)
18744 /* We can have a normal definition following an inline one in the
18745 case of redefinition of GNU C extern inlines.
18746 It seems reasonable to use AT_specification in this case. */
18747 && !get_AT (old_die, DW_AT_inline))
18749 /* Detect and ignore this case, where we are trying to output
18750 something we have already output. */
18751 if (get_AT (old_die, DW_AT_low_pc)
18752 || get_AT (old_die, DW_AT_ranges))
18753 return;
18755 /* If we have no location information, this must be a
18756 partially generated DIE from early dwarf generation.
18757 Fall through and generate it. */
18760 /* If the definition comes from the same place as the declaration,
18761 maybe use the old DIE. We always want the DIE for this function
18762 that has the *_pc attributes to be under comp_unit_die so the
18763 debugger can find it. We also need to do this for abstract
18764 instances of inlines, since the spec requires the out-of-line copy
18765 to have the same parent. For local class methods, this doesn't
18766 apply; we just use the old DIE. */
18767 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18768 struct dwarf_file_data * file_index = lookup_filename (s.file);
18769 if ((is_cu_die (old_die->die_parent)
18770 /* This condition fixes the inconsistency/ICE with the
18771 following Fortran test (or some derivative thereof) while
18772 building libgfortran:
18774 module some_m
18775 contains
18776 logical function funky (FLAG)
18777 funky = .true.
18778 end function
18779 end module
18781 || (old_die->die_parent
18782 && old_die->die_parent->die_tag == DW_TAG_module)
18783 || context_die == NULL)
18784 && (DECL_ARTIFICIAL (decl)
18785 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
18786 && (get_AT_unsigned (old_die, DW_AT_decl_line)
18787 == (unsigned) s.line))))
18789 subr_die = old_die;
18791 /* Clear out the declaration attribute, but leave the
18792 parameters so they can be augmented with location
18793 information later. Unless this was a declaration, in
18794 which case, wipe out the nameless parameters and recreate
18795 them further down. */
18796 if (remove_AT (subr_die, DW_AT_declaration))
18799 remove_AT (subr_die, DW_AT_object_pointer);
18800 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
18803 /* Make a specification pointing to the previously built
18804 declaration. */
18805 else
18807 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18808 add_AT_specification (subr_die, old_die);
18809 add_pubname (decl, subr_die);
18810 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18811 add_AT_file (subr_die, DW_AT_decl_file, file_index);
18812 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18813 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
18815 /* If the prototype had an 'auto' or 'decltype(auto)' return type,
18816 emit the real type on the definition die. */
18817 if (is_cxx() && debug_info_level > DINFO_LEVEL_TERSE)
18819 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
18820 if (die == auto_die || die == decltype_auto_die)
18821 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18822 TYPE_UNQUALIFIED, context_die);
18826 /* Create a fresh DIE for anything else. */
18827 else
18829 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18831 if (TREE_PUBLIC (decl))
18832 add_AT_flag (subr_die, DW_AT_external, 1);
18834 add_name_and_src_coords_attributes (subr_die, decl);
18835 add_pubname (decl, subr_die);
18836 if (debug_info_level > DINFO_LEVEL_TERSE)
18838 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
18839 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18840 TYPE_UNQUALIFIED, context_die);
18843 add_pure_or_virtual_attribute (subr_die, decl);
18844 if (DECL_ARTIFICIAL (decl))
18845 add_AT_flag (subr_die, DW_AT_artificial, 1);
18847 if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
18848 add_AT_flag (subr_die, DW_AT_noreturn, 1);
18850 add_accessibility_attribute (subr_die, decl);
18853 /* Unless we have an existing non-declaration DIE, equate the new
18854 DIE. */
18855 if (!old_die || is_declaration_die (old_die))
18856 equate_decl_number_to_die (decl, subr_die);
18858 if (declaration)
18860 if (!old_die || !get_AT (old_die, DW_AT_inline))
18862 add_AT_flag (subr_die, DW_AT_declaration, 1);
18864 /* If this is an explicit function declaration then generate
18865 a DW_AT_explicit attribute. */
18866 if (lang_hooks.decls.function_decl_explicit_p (decl)
18867 && (dwarf_version >= 3 || !dwarf_strict))
18868 add_AT_flag (subr_die, DW_AT_explicit, 1);
18870 /* If this is a C++11 deleted special function member then generate
18871 a DW_AT_GNU_deleted attribute. */
18872 if (lang_hooks.decls.function_decl_deleted_p (decl)
18873 && (! dwarf_strict))
18874 add_AT_flag (subr_die, DW_AT_GNU_deleted, 1);
18877 /* Tag abstract instances with DW_AT_inline. */
18878 else if (DECL_ABSTRACT_P (decl))
18880 if (DECL_DECLARED_INLINE_P (decl))
18882 if (cgraph_function_possibly_inlined_p (decl))
18883 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
18884 else
18885 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
18887 else
18889 if (cgraph_function_possibly_inlined_p (decl))
18890 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
18891 else
18892 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
18895 if (DECL_DECLARED_INLINE_P (decl)
18896 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
18897 add_AT_flag (subr_die, DW_AT_artificial, 1);
18899 /* For non DECL_EXTERNALs, if range information is available, fill
18900 the DIE with it. */
18901 else if (!DECL_EXTERNAL (decl) && !early_dwarf)
18903 HOST_WIDE_INT cfa_fb_offset;
18905 struct function *fun = DECL_STRUCT_FUNCTION (decl);
18907 if (!flag_reorder_blocks_and_partition)
18909 dw_fde_ref fde = fun->fde;
18910 if (fde->dw_fde_begin)
18912 /* We have already generated the labels. */
18913 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18914 fde->dw_fde_end, false);
18916 else
18918 /* Create start/end labels and add the range. */
18919 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
18920 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
18921 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
18922 current_function_funcdef_no);
18923 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
18924 current_function_funcdef_no);
18925 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
18926 false);
18929 #if VMS_DEBUGGING_INFO
18930 /* HP OpenVMS Industry Standard 64: DWARF Extensions
18931 Section 2.3 Prologue and Epilogue Attributes:
18932 When a breakpoint is set on entry to a function, it is generally
18933 desirable for execution to be suspended, not on the very first
18934 instruction of the function, but rather at a point after the
18935 function's frame has been set up, after any language defined local
18936 declaration processing has been completed, and before execution of
18937 the first statement of the function begins. Debuggers generally
18938 cannot properly determine where this point is. Similarly for a
18939 breakpoint set on exit from a function. The prologue and epilogue
18940 attributes allow a compiler to communicate the location(s) to use. */
18943 if (fde->dw_fde_vms_end_prologue)
18944 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
18945 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
18947 if (fde->dw_fde_vms_begin_epilogue)
18948 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
18949 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
18951 #endif
18954 else
18956 /* Generate pubnames entries for the split function code ranges. */
18957 dw_fde_ref fde = fun->fde;
18959 if (fde->dw_fde_second_begin)
18961 if (dwarf_version >= 3 || !dwarf_strict)
18963 /* We should use ranges for non-contiguous code section
18964 addresses. Use the actual code range for the initial
18965 section, since the HOT/COLD labels might precede an
18966 alignment offset. */
18967 bool range_list_added = false;
18968 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
18969 fde->dw_fde_end, &range_list_added,
18970 false);
18971 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
18972 fde->dw_fde_second_end,
18973 &range_list_added, false);
18974 if (range_list_added)
18975 add_ranges (NULL);
18977 else
18979 /* There is no real support in DW2 for this .. so we make
18980 a work-around. First, emit the pub name for the segment
18981 containing the function label. Then make and emit a
18982 simplified subprogram DIE for the second segment with the
18983 name pre-fixed by __hot/cold_sect_of_. We use the same
18984 linkage name for the second die so that gdb will find both
18985 sections when given "b foo". */
18986 const char *name = NULL;
18987 tree decl_name = DECL_NAME (decl);
18988 dw_die_ref seg_die;
18990 /* Do the 'primary' section. */
18991 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18992 fde->dw_fde_end, false);
18994 /* Build a minimal DIE for the secondary section. */
18995 seg_die = new_die (DW_TAG_subprogram,
18996 subr_die->die_parent, decl);
18998 if (TREE_PUBLIC (decl))
18999 add_AT_flag (seg_die, DW_AT_external, 1);
19001 if (decl_name != NULL
19002 && IDENTIFIER_POINTER (decl_name) != NULL)
19004 name = dwarf2_name (decl, 1);
19005 if (! DECL_ARTIFICIAL (decl))
19006 add_src_coords_attributes (seg_die, decl);
19008 add_linkage_name (seg_die, decl);
19010 gcc_assert (name != NULL);
19011 add_pure_or_virtual_attribute (seg_die, decl);
19012 if (DECL_ARTIFICIAL (decl))
19013 add_AT_flag (seg_die, DW_AT_artificial, 1);
19015 name = concat ("__second_sect_of_", name, NULL);
19016 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
19017 fde->dw_fde_second_end, false);
19018 add_name_attribute (seg_die, name);
19019 if (want_pubnames ())
19020 add_pubname_string (name, seg_die);
19023 else
19024 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
19025 false);
19028 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
19030 /* We define the "frame base" as the function's CFA. This is more
19031 convenient for several reasons: (1) It's stable across the prologue
19032 and epilogue, which makes it better than just a frame pointer,
19033 (2) With dwarf3, there exists a one-byte encoding that allows us
19034 to reference the .debug_frame data by proxy, but failing that,
19035 (3) We can at least reuse the code inspection and interpretation
19036 code that determines the CFA position at various points in the
19037 function. */
19038 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
19040 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
19041 add_AT_loc (subr_die, DW_AT_frame_base, op);
19043 else
19045 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
19046 if (list->dw_loc_next)
19047 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
19048 else
19049 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
19052 /* Compute a displacement from the "steady-state frame pointer" to
19053 the CFA. The former is what all stack slots and argument slots
19054 will reference in the rtl; the latter is what we've told the
19055 debugger about. We'll need to adjust all frame_base references
19056 by this displacement. */
19057 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
19059 if (fun->static_chain_decl)
19060 add_AT_location_description
19061 (subr_die, DW_AT_static_link,
19062 loc_list_from_tree (fun->static_chain_decl, 2, NULL));
19065 /* Generate child dies for template paramaters. */
19066 if (early_dwarf && debug_info_level > DINFO_LEVEL_TERSE)
19067 gen_generic_params_dies (decl);
19069 /* Now output descriptions of the arguments for this function. This gets
19070 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
19071 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
19072 `...' at the end of the formal parameter list. In order to find out if
19073 there was a trailing ellipsis or not, we must instead look at the type
19074 associated with the FUNCTION_DECL. This will be a node of type
19075 FUNCTION_TYPE. If the chain of type nodes hanging off of this
19076 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
19077 an ellipsis at the end. */
19079 /* In the case where we are describing a mere function declaration, all we
19080 need to do here (and all we *can* do here) is to describe the *types* of
19081 its formal parameters. */
19082 if (debug_info_level <= DINFO_LEVEL_TERSE)
19084 else if (declaration)
19085 gen_formal_types_die (decl, subr_die);
19086 else
19088 /* Generate DIEs to represent all known formal parameters. */
19089 tree parm = DECL_ARGUMENTS (decl);
19090 tree generic_decl = early_dwarf
19091 ? lang_hooks.decls.get_generic_function_decl (decl) : NULL;
19092 tree generic_decl_parm = generic_decl
19093 ? DECL_ARGUMENTS (generic_decl)
19094 : NULL;
19096 /* Now we want to walk the list of parameters of the function and
19097 emit their relevant DIEs.
19099 We consider the case of DECL being an instance of a generic function
19100 as well as it being a normal function.
19102 If DECL is an instance of a generic function we walk the
19103 parameters of the generic function declaration _and_ the parameters of
19104 DECL itself. This is useful because we want to emit specific DIEs for
19105 function parameter packs and those are declared as part of the
19106 generic function declaration. In that particular case,
19107 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
19108 That DIE has children DIEs representing the set of arguments
19109 of the pack. Note that the set of pack arguments can be empty.
19110 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
19111 children DIE.
19113 Otherwise, we just consider the parameters of DECL. */
19114 while (generic_decl_parm || parm)
19116 if (generic_decl_parm
19117 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
19118 gen_formal_parameter_pack_die (generic_decl_parm,
19119 parm, subr_die,
19120 &parm);
19121 else if (parm && !POINTER_BOUNDS_P (parm))
19123 dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
19125 if (parm == DECL_ARGUMENTS (decl)
19126 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
19127 && parm_die
19128 && (dwarf_version >= 3 || !dwarf_strict))
19129 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
19131 parm = DECL_CHAIN (parm);
19133 else if (parm)
19134 parm = DECL_CHAIN (parm);
19136 if (generic_decl_parm)
19137 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
19140 /* Decide whether we need an unspecified_parameters DIE at the end.
19141 There are 2 more cases to do this for: 1) the ansi ... declaration -
19142 this is detectable when the end of the arg list is not a
19143 void_type_node 2) an unprototyped function declaration (not a
19144 definition). This just means that we have no info about the
19145 parameters at all. */
19146 if (prototype_p (TREE_TYPE (decl)))
19148 /* This is the prototyped case, check for.... */
19149 if (stdarg_p (TREE_TYPE (decl)))
19150 gen_unspecified_parameters_die (decl, subr_die);
19152 else if (DECL_INITIAL (decl) == NULL_TREE)
19153 gen_unspecified_parameters_die (decl, subr_die);
19156 if (subr_die != old_die)
19157 /* Add the calling convention attribute if requested. */
19158 add_calling_convention_attribute (subr_die, decl);
19160 /* Output Dwarf info for all of the stuff within the body of the function
19161 (if it has one - it may be just a declaration).
19163 OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
19164 a function. This BLOCK actually represents the outermost binding contour
19165 for the function, i.e. the contour in which the function's formal
19166 parameters and labels get declared. Curiously, it appears that the front
19167 end doesn't actually put the PARM_DECL nodes for the current function onto
19168 the BLOCK_VARS list for this outer scope, but are strung off of the
19169 DECL_ARGUMENTS list for the function instead.
19171 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
19172 the LABEL_DECL nodes for the function however, and we output DWARF info
19173 for those in decls_for_scope. Just within the `outer_scope' there will be
19174 a BLOCK node representing the function's outermost pair of curly braces,
19175 and any blocks used for the base and member initializers of a C++
19176 constructor function. */
19177 tree outer_scope = DECL_INITIAL (decl);
19178 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
19180 int call_site_note_count = 0;
19181 int tail_call_site_note_count = 0;
19183 /* Emit a DW_TAG_variable DIE for a named return value. */
19184 if (DECL_NAME (DECL_RESULT (decl)))
19185 gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
19187 /* The first time through decls_for_scope we will generate the
19188 DIEs for the locals. The second time, we fill in the
19189 location info. */
19190 decls_for_scope (outer_scope, subr_die);
19192 if (call_arg_locations && !dwarf_strict)
19194 struct call_arg_loc_node *ca_loc;
19195 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
19197 dw_die_ref die = NULL;
19198 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
19199 rtx arg, next_arg;
19201 for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
19202 arg; arg = next_arg)
19204 dw_loc_descr_ref reg, val;
19205 machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
19206 dw_die_ref cdie, tdie = NULL;
19208 next_arg = XEXP (arg, 1);
19209 if (REG_P (XEXP (XEXP (arg, 0), 0))
19210 && next_arg
19211 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
19212 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
19213 && REGNO (XEXP (XEXP (arg, 0), 0))
19214 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
19215 next_arg = XEXP (next_arg, 1);
19216 if (mode == VOIDmode)
19218 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
19219 if (mode == VOIDmode)
19220 mode = GET_MODE (XEXP (arg, 0));
19222 if (mode == VOIDmode || mode == BLKmode)
19223 continue;
19224 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
19226 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
19227 tloc = XEXP (XEXP (arg, 0), 1);
19228 continue;
19230 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
19231 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
19233 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
19234 tlocc = XEXP (XEXP (arg, 0), 1);
19235 continue;
19237 reg = NULL;
19238 if (REG_P (XEXP (XEXP (arg, 0), 0)))
19239 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
19240 VAR_INIT_STATUS_INITIALIZED);
19241 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
19243 rtx mem = XEXP (XEXP (arg, 0), 0);
19244 reg = mem_loc_descriptor (XEXP (mem, 0),
19245 get_address_mode (mem),
19246 GET_MODE (mem),
19247 VAR_INIT_STATUS_INITIALIZED);
19249 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
19250 == DEBUG_PARAMETER_REF)
19252 tree tdecl
19253 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
19254 tdie = lookup_decl_die (tdecl);
19255 if (tdie == NULL)
19256 continue;
19258 else
19259 continue;
19260 if (reg == NULL
19261 && GET_CODE (XEXP (XEXP (arg, 0), 0))
19262 != DEBUG_PARAMETER_REF)
19263 continue;
19264 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
19265 VOIDmode,
19266 VAR_INIT_STATUS_INITIALIZED);
19267 if (val == NULL)
19268 continue;
19269 if (die == NULL)
19270 die = gen_call_site_die (decl, subr_die, ca_loc);
19271 cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
19272 NULL_TREE);
19273 if (reg != NULL)
19274 add_AT_loc (cdie, DW_AT_location, reg);
19275 else if (tdie != NULL)
19276 add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
19277 add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
19278 if (next_arg != XEXP (arg, 1))
19280 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
19281 if (mode == VOIDmode)
19282 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
19283 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
19284 0), 1),
19285 mode, VOIDmode,
19286 VAR_INIT_STATUS_INITIALIZED);
19287 if (val != NULL)
19288 add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
19291 if (die == NULL
19292 && (ca_loc->symbol_ref || tloc))
19293 die = gen_call_site_die (decl, subr_die, ca_loc);
19294 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
19296 dw_loc_descr_ref tval = NULL;
19298 if (tloc != NULL_RTX)
19299 tval = mem_loc_descriptor (tloc,
19300 GET_MODE (tloc) == VOIDmode
19301 ? Pmode : GET_MODE (tloc),
19302 VOIDmode,
19303 VAR_INIT_STATUS_INITIALIZED);
19304 if (tval)
19305 add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
19306 else if (tlocc != NULL_RTX)
19308 tval = mem_loc_descriptor (tlocc,
19309 GET_MODE (tlocc) == VOIDmode
19310 ? Pmode : GET_MODE (tlocc),
19311 VOIDmode,
19312 VAR_INIT_STATUS_INITIALIZED);
19313 if (tval)
19314 add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
19315 tval);
19318 if (die != NULL)
19320 call_site_note_count++;
19321 if (ca_loc->tail_call_p)
19322 tail_call_site_note_count++;
19326 call_arg_locations = NULL;
19327 call_arg_loc_last = NULL;
19328 if (tail_call_site_count >= 0
19329 && tail_call_site_count == tail_call_site_note_count
19330 && !dwarf_strict)
19332 if (call_site_count >= 0
19333 && call_site_count == call_site_note_count)
19334 add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
19335 else
19336 add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
19338 call_site_count = -1;
19339 tail_call_site_count = -1;
19343 /* Returns a hash value for X (which really is a die_struct). */
19345 hashval_t
19346 block_die_hasher::hash (die_struct *d)
19348 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
19351 /* Return nonzero if decl_id and die_parent of die_struct X is the same
19352 as decl_id and die_parent of die_struct Y. */
19354 bool
19355 block_die_hasher::equal (die_struct *x, die_struct *y)
19357 return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
19360 /* Return TRUE if DECL, which may have been previously generated as
19361 OLD_DIE, is a candidate for a DW_AT_specification. DECLARATION is
19362 true if decl (or its origin) is either an extern declaration or a
19363 class/namespace scoped declaration.
19365 The declare_in_namespace support causes us to get two DIEs for one
19366 variable, both of which are declarations. We want to avoid
19367 considering one to be a specification, so we must test for
19368 DECLARATION and DW_AT_declaration. */
19369 static inline bool
19370 decl_will_get_specification_p (dw_die_ref old_die, tree decl, bool declaration)
19372 return (old_die && TREE_STATIC (decl) && !declaration
19373 && get_AT_flag (old_die, DW_AT_declaration) == 1);
19376 /* Return true if DECL is a local static. */
19378 static inline bool
19379 local_function_static (tree decl)
19381 gcc_assert (TREE_CODE (decl) == VAR_DECL);
19382 return TREE_STATIC (decl)
19383 && DECL_CONTEXT (decl)
19384 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL;
19387 /* Generate a DIE to represent a declared data object.
19388 Either DECL or ORIGIN must be non-null. */
19390 static void
19391 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
19393 HOST_WIDE_INT off = 0;
19394 tree com_decl;
19395 tree decl_or_origin = decl ? decl : origin;
19396 tree ultimate_origin;
19397 dw_die_ref var_die;
19398 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
19399 dw_die_ref origin_die = NULL;
19400 bool declaration = (DECL_EXTERNAL (decl_or_origin)
19401 || class_or_namespace_scope_p (context_die));
19402 bool specialization_p = false;
19404 ultimate_origin = decl_ultimate_origin (decl_or_origin);
19405 if (decl || ultimate_origin)
19406 origin = ultimate_origin;
19407 com_decl = fortran_common (decl_or_origin, &off);
19409 /* Symbol in common gets emitted as a child of the common block, in the form
19410 of a data member. */
19411 if (com_decl)
19413 dw_die_ref com_die;
19414 dw_loc_list_ref loc;
19415 die_node com_die_arg;
19417 var_die = lookup_decl_die (decl_or_origin);
19418 if (var_die)
19420 if (get_AT (var_die, DW_AT_location) == NULL)
19422 loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
19423 if (loc)
19425 if (off)
19427 /* Optimize the common case. */
19428 if (single_element_loc_list_p (loc)
19429 && loc->expr->dw_loc_opc == DW_OP_addr
19430 && loc->expr->dw_loc_next == NULL
19431 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
19432 == SYMBOL_REF)
19434 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
19435 loc->expr->dw_loc_oprnd1.v.val_addr
19436 = plus_constant (GET_MODE (x), x , off);
19438 else
19439 loc_list_plus_const (loc, off);
19441 add_AT_location_description (var_die, DW_AT_location, loc);
19442 remove_AT (var_die, DW_AT_declaration);
19445 return;
19448 if (common_block_die_table == NULL)
19449 common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
19451 com_die_arg.decl_id = DECL_UID (com_decl);
19452 com_die_arg.die_parent = context_die;
19453 com_die = common_block_die_table->find (&com_die_arg);
19454 loc = loc_list_from_tree (com_decl, 2, NULL);
19455 if (com_die == NULL)
19457 const char *cnam
19458 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
19459 die_node **slot;
19461 com_die = new_die (DW_TAG_common_block, context_die, decl);
19462 add_name_and_src_coords_attributes (com_die, com_decl);
19463 if (loc)
19465 add_AT_location_description (com_die, DW_AT_location, loc);
19466 /* Avoid sharing the same loc descriptor between
19467 DW_TAG_common_block and DW_TAG_variable. */
19468 loc = loc_list_from_tree (com_decl, 2, NULL);
19470 else if (DECL_EXTERNAL (decl))
19471 add_AT_flag (com_die, DW_AT_declaration, 1);
19472 if (want_pubnames ())
19473 add_pubname_string (cnam, com_die); /* ??? needed? */
19474 com_die->decl_id = DECL_UID (com_decl);
19475 slot = common_block_die_table->find_slot (com_die, INSERT);
19476 *slot = com_die;
19478 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
19480 add_AT_location_description (com_die, DW_AT_location, loc);
19481 loc = loc_list_from_tree (com_decl, 2, NULL);
19482 remove_AT (com_die, DW_AT_declaration);
19484 var_die = new_die (DW_TAG_variable, com_die, decl);
19485 add_name_and_src_coords_attributes (var_die, decl);
19486 add_type_attribute (var_die, TREE_TYPE (decl), decl_quals (decl),
19487 context_die);
19488 add_AT_flag (var_die, DW_AT_external, 1);
19489 if (loc)
19491 if (off)
19493 /* Optimize the common case. */
19494 if (single_element_loc_list_p (loc)
19495 && loc->expr->dw_loc_opc == DW_OP_addr
19496 && loc->expr->dw_loc_next == NULL
19497 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
19499 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
19500 loc->expr->dw_loc_oprnd1.v.val_addr
19501 = plus_constant (GET_MODE (x), x, off);
19503 else
19504 loc_list_plus_const (loc, off);
19506 add_AT_location_description (var_die, DW_AT_location, loc);
19508 else if (DECL_EXTERNAL (decl))
19509 add_AT_flag (var_die, DW_AT_declaration, 1);
19510 equate_decl_number_to_die (decl, var_die);
19511 return;
19514 if (old_die)
19516 if (declaration)
19518 /* A declaration that has been previously dumped, needs no
19519 further annotations, since it doesn't need location on
19520 the second pass. */
19521 return;
19523 else if (decl_will_get_specification_p (old_die, decl, declaration)
19524 && !get_AT (old_die, DW_AT_specification))
19526 /* Fall-thru so we can make a new variable die along with a
19527 DW_AT_specification. */
19529 else if (origin && old_die->die_parent != context_die)
19531 /* If we will be creating an inlined instance, we need a
19532 new DIE that will get annotated with
19533 DW_AT_abstract_origin. Clear things so we can get a
19534 new DIE. */
19535 gcc_assert (!DECL_ABSTRACT_P (decl));
19536 old_die = NULL;
19538 else
19540 /* If a DIE was dumped early, it still needs location info.
19541 Skip to where we fill the location bits. */
19542 var_die = old_die;
19543 goto gen_variable_die_location;
19547 /* For static data members, the declaration in the class is supposed
19548 to have DW_TAG_member tag; the specification should still be
19549 DW_TAG_variable referencing the DW_TAG_member DIE. */
19550 if (declaration && class_scope_p (context_die))
19551 var_die = new_die (DW_TAG_member, context_die, decl);
19552 else
19553 var_die = new_die (DW_TAG_variable, context_die, decl);
19555 if (origin != NULL)
19556 origin_die = add_abstract_origin_attribute (var_die, origin);
19558 /* Loop unrolling can create multiple blocks that refer to the same
19559 static variable, so we must test for the DW_AT_declaration flag.
19561 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
19562 copy decls and set the DECL_ABSTRACT_P flag on them instead of
19563 sharing them.
19565 ??? Duplicated blocks have been rewritten to use .debug_ranges. */
19566 else if (decl_will_get_specification_p (old_die, decl, declaration))
19568 /* This is a definition of a C++ class level static. */
19569 add_AT_specification (var_die, old_die);
19570 specialization_p = true;
19571 if (DECL_NAME (decl))
19573 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
19574 struct dwarf_file_data * file_index = lookup_filename (s.file);
19576 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
19577 add_AT_file (var_die, DW_AT_decl_file, file_index);
19579 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
19580 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
19582 if (old_die->die_tag == DW_TAG_member)
19583 add_linkage_name (var_die, decl);
19586 else
19587 add_name_and_src_coords_attributes (var_die, decl);
19589 if ((origin == NULL && !specialization_p)
19590 || (origin != NULL
19591 && !DECL_ABSTRACT_P (decl_or_origin)
19592 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
19593 decl_function_context
19594 (decl_or_origin))))
19596 tree type = TREE_TYPE (decl_or_origin);
19598 if (decl_by_reference_p (decl_or_origin))
19599 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19600 context_die);
19601 else
19602 add_type_attribute (var_die, type, decl_quals (decl_or_origin),
19603 context_die);
19606 if (origin == NULL && !specialization_p)
19608 if (TREE_PUBLIC (decl))
19609 add_AT_flag (var_die, DW_AT_external, 1);
19611 if (DECL_ARTIFICIAL (decl))
19612 add_AT_flag (var_die, DW_AT_artificial, 1);
19614 add_accessibility_attribute (var_die, decl);
19617 if (declaration)
19618 add_AT_flag (var_die, DW_AT_declaration, 1);
19620 if (decl && (DECL_ABSTRACT_P (decl)
19621 || !old_die || is_declaration_die (old_die)))
19622 equate_decl_number_to_die (decl, var_die);
19624 gen_variable_die_location:
19625 if (! declaration
19626 && (! DECL_ABSTRACT_P (decl_or_origin)
19627 /* Local static vars are shared between all clones/inlines,
19628 so emit DW_AT_location on the abstract DIE if DECL_RTL is
19629 already set. */
19630 || (TREE_CODE (decl_or_origin) == VAR_DECL
19631 && TREE_STATIC (decl_or_origin)
19632 && DECL_RTL_SET_P (decl_or_origin)))
19633 /* When abstract origin already has DW_AT_location attribute, no need
19634 to add it again. */
19635 && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
19637 if (early_dwarf)
19638 add_pubname (decl_or_origin, var_die);
19639 else
19640 add_location_or_const_value_attribute (var_die, decl_or_origin,
19641 decl == NULL, DW_AT_location);
19643 else
19644 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
19647 /* Generate a DIE to represent a named constant. */
19649 static void
19650 gen_const_die (tree decl, dw_die_ref context_die)
19652 dw_die_ref const_die;
19653 tree type = TREE_TYPE (decl);
19655 const_die = lookup_decl_die (decl);
19656 if (const_die)
19657 return;
19659 const_die = new_die (DW_TAG_constant, context_die, decl);
19660 equate_decl_number_to_die (decl, const_die);
19661 add_name_and_src_coords_attributes (const_die, decl);
19662 add_type_attribute (const_die, type, TYPE_QUAL_CONST, context_die);
19663 if (TREE_PUBLIC (decl))
19664 add_AT_flag (const_die, DW_AT_external, 1);
19665 if (DECL_ARTIFICIAL (decl))
19666 add_AT_flag (const_die, DW_AT_artificial, 1);
19667 tree_add_const_value_attribute_for_decl (const_die, decl);
19670 /* Generate a DIE to represent a label identifier. */
19672 static void
19673 gen_label_die (tree decl, dw_die_ref context_die)
19675 tree origin = decl_ultimate_origin (decl);
19676 dw_die_ref lbl_die = lookup_decl_die (decl);
19677 rtx insn;
19678 char label[MAX_ARTIFICIAL_LABEL_BYTES];
19680 if (!lbl_die)
19682 lbl_die = new_die (DW_TAG_label, context_die, decl);
19683 equate_decl_number_to_die (decl, lbl_die);
19685 if (origin != NULL)
19686 add_abstract_origin_attribute (lbl_die, origin);
19687 else
19688 add_name_and_src_coords_attributes (lbl_die, decl);
19691 if (DECL_ABSTRACT_P (decl))
19692 equate_decl_number_to_die (decl, lbl_die);
19693 else
19695 insn = DECL_RTL_IF_SET (decl);
19697 /* Deleted labels are programmer specified labels which have been
19698 eliminated because of various optimizations. We still emit them
19699 here so that it is possible to put breakpoints on them. */
19700 if (insn
19701 && (LABEL_P (insn)
19702 || ((NOTE_P (insn)
19703 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
19705 /* When optimization is enabled (via -O) some parts of the compiler
19706 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
19707 represent source-level labels which were explicitly declared by
19708 the user. This really shouldn't be happening though, so catch
19709 it if it ever does happen. */
19710 gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
19712 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
19713 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19715 else if (insn
19716 && NOTE_P (insn)
19717 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
19718 && CODE_LABEL_NUMBER (insn) != -1)
19720 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
19721 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19726 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
19727 attributes to the DIE for a block STMT, to describe where the inlined
19728 function was called from. This is similar to add_src_coords_attributes. */
19730 static inline void
19731 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
19733 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
19735 if (dwarf_version >= 3 || !dwarf_strict)
19737 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
19738 add_AT_unsigned (die, DW_AT_call_line, s.line);
19743 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
19744 Add low_pc and high_pc attributes to the DIE for a block STMT. */
19746 static inline void
19747 add_high_low_attributes (tree stmt, dw_die_ref die)
19749 char label[MAX_ARTIFICIAL_LABEL_BYTES];
19751 if (BLOCK_FRAGMENT_CHAIN (stmt)
19752 && (dwarf_version >= 3 || !dwarf_strict))
19754 tree chain, superblock = NULL_TREE;
19755 dw_die_ref pdie;
19756 dw_attr_ref attr = NULL;
19758 if (inlined_function_outer_scope_p (stmt))
19760 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19761 BLOCK_NUMBER (stmt));
19762 add_AT_lbl_id (die, DW_AT_entry_pc, label);
19765 /* Optimize duplicate .debug_ranges lists or even tails of
19766 lists. If this BLOCK has same ranges as its supercontext,
19767 lookup DW_AT_ranges attribute in the supercontext (and
19768 recursively so), verify that the ranges_table contains the
19769 right values and use it instead of adding a new .debug_range. */
19770 for (chain = stmt, pdie = die;
19771 BLOCK_SAME_RANGE (chain);
19772 chain = BLOCK_SUPERCONTEXT (chain))
19774 dw_attr_ref new_attr;
19776 pdie = pdie->die_parent;
19777 if (pdie == NULL)
19778 break;
19779 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
19780 break;
19781 new_attr = get_AT (pdie, DW_AT_ranges);
19782 if (new_attr == NULL
19783 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
19784 break;
19785 attr = new_attr;
19786 superblock = BLOCK_SUPERCONTEXT (chain);
19788 if (attr != NULL
19789 && (ranges_table[attr->dw_attr_val.v.val_offset
19790 / 2 / DWARF2_ADDR_SIZE].num
19791 == BLOCK_NUMBER (superblock))
19792 && BLOCK_FRAGMENT_CHAIN (superblock))
19794 unsigned long off = attr->dw_attr_val.v.val_offset
19795 / 2 / DWARF2_ADDR_SIZE;
19796 unsigned long supercnt = 0, thiscnt = 0;
19797 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
19798 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
19800 ++supercnt;
19801 gcc_checking_assert (ranges_table[off + supercnt].num
19802 == BLOCK_NUMBER (chain));
19804 gcc_checking_assert (ranges_table[off + supercnt + 1].num == 0);
19805 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
19806 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
19807 ++thiscnt;
19808 gcc_assert (supercnt >= thiscnt);
19809 add_AT_range_list (die, DW_AT_ranges,
19810 ((off + supercnt - thiscnt)
19811 * 2 * DWARF2_ADDR_SIZE),
19812 false);
19813 return;
19816 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt), false);
19818 chain = BLOCK_FRAGMENT_CHAIN (stmt);
19821 add_ranges (chain);
19822 chain = BLOCK_FRAGMENT_CHAIN (chain);
19824 while (chain);
19825 add_ranges (NULL);
19827 else
19829 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
19830 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19831 BLOCK_NUMBER (stmt));
19832 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
19833 BLOCK_NUMBER (stmt));
19834 add_AT_low_high_pc (die, label, label_high, false);
19838 /* Generate a DIE for a lexical block. */
19840 static void
19841 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
19843 dw_die_ref old_die = BLOCK_DIE (stmt);
19844 dw_die_ref stmt_die = NULL;
19845 if (!old_die)
19847 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
19848 BLOCK_DIE (stmt) = stmt_die;
19851 if (BLOCK_ABSTRACT (stmt))
19853 if (old_die)
19855 #ifdef ENABLE_CHECKING
19856 /* This must have been generated early and it won't even
19857 need location information since it's a DW_AT_inline
19858 function. */
19859 for (dw_die_ref c = context_die; c; c = c->die_parent)
19860 if (c->die_tag == DW_TAG_inlined_subroutine
19861 || c->die_tag == DW_TAG_subprogram)
19863 gcc_assert (get_AT (c, DW_AT_inline));
19864 break;
19866 #endif
19867 return;
19870 else if (BLOCK_ABSTRACT_ORIGIN (stmt))
19872 /* If this is an inlined instance, create a new lexical die for
19873 anything below to attach DW_AT_abstract_origin to. */
19874 if (old_die)
19876 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
19877 BLOCK_DIE (stmt) = stmt_die;
19878 old_die = NULL;
19882 if (old_die)
19883 stmt_die = old_die;
19885 /* A non abstract block whose blocks have already been reordered
19886 should have the instruction range for this block. If so, set the
19887 high/low attributes. */
19888 if (!early_dwarf && !BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
19890 gcc_assert (stmt_die);
19891 add_high_low_attributes (stmt, stmt_die);
19894 decls_for_scope (stmt, stmt_die);
19897 /* Generate a DIE for an inlined subprogram. */
19899 static void
19900 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
19902 tree decl;
19904 /* The instance of function that is effectively being inlined shall not
19905 be abstract. */
19906 gcc_assert (! BLOCK_ABSTRACT (stmt));
19908 decl = block_ultimate_origin (stmt);
19910 /* Emit info for the abstract instance first, if we haven't yet. We
19911 must emit this even if the block is abstract, otherwise when we
19912 emit the block below (or elsewhere), we may end up trying to emit
19913 a die whose origin die hasn't been emitted, and crashing. */
19914 dwarf2out_abstract_function (decl);
19916 if (! BLOCK_ABSTRACT (stmt))
19918 dw_die_ref subr_die
19919 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
19921 if (call_arg_locations)
19922 BLOCK_DIE (stmt) = subr_die;
19923 add_abstract_origin_attribute (subr_die, decl);
19924 if (TREE_ASM_WRITTEN (stmt))
19925 add_high_low_attributes (stmt, subr_die);
19926 add_call_src_coords_attributes (stmt, subr_die);
19928 decls_for_scope (stmt, subr_die);
19932 /* Generate a DIE for a field in a record, or structure. */
19934 static void
19935 gen_field_die (tree decl, dw_die_ref context_die)
19937 dw_die_ref decl_die;
19939 if (TREE_TYPE (decl) == error_mark_node)
19940 return;
19942 decl_die = new_die (DW_TAG_member, context_die, decl);
19943 add_name_and_src_coords_attributes (decl_die, decl);
19944 add_type_attribute (decl_die, member_declared_type (decl),
19945 decl_quals (decl), context_die);
19947 if (DECL_BIT_FIELD_TYPE (decl))
19949 add_byte_size_attribute (decl_die, decl);
19950 add_bit_size_attribute (decl_die, decl);
19951 add_bit_offset_attribute (decl_die, decl);
19954 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
19955 add_data_member_location_attribute (decl_die, decl);
19957 if (DECL_ARTIFICIAL (decl))
19958 add_AT_flag (decl_die, DW_AT_artificial, 1);
19960 add_accessibility_attribute (decl_die, decl);
19962 /* Equate decl number to die, so that we can look up this decl later on. */
19963 equate_decl_number_to_die (decl, decl_die);
19966 #if 0
19967 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19968 Use modified_type_die instead.
19969 We keep this code here just in case these types of DIEs may be needed to
19970 represent certain things in other languages (e.g. Pascal) someday. */
19972 static void
19973 gen_pointer_type_die (tree type, dw_die_ref context_die)
19975 dw_die_ref ptr_die
19976 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
19978 equate_type_number_to_die (type, ptr_die);
19979 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19980 context_die);
19981 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19984 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19985 Use modified_type_die instead.
19986 We keep this code here just in case these types of DIEs may be needed to
19987 represent certain things in other languages (e.g. Pascal) someday. */
19989 static void
19990 gen_reference_type_die (tree type, dw_die_ref context_die)
19992 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
19994 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
19995 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
19996 else
19997 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
19999 equate_type_number_to_die (type, ref_die);
20000 add_type_attribute (ref_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
20001 context_die);
20002 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
20004 #endif
20006 /* Generate a DIE for a pointer to a member type. */
20008 static void
20009 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
20011 dw_die_ref ptr_die
20012 = new_die (DW_TAG_ptr_to_member_type,
20013 scope_die_for (type, context_die), type);
20015 equate_type_number_to_die (type, ptr_die);
20016 add_AT_die_ref (ptr_die, DW_AT_containing_type,
20017 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
20018 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
20019 context_die);
20022 typedef const char *dchar_p; /* For DEF_VEC_P. */
20024 static char *producer_string;
20026 /* Return a heap allocated producer string including command line options
20027 if -grecord-gcc-switches. */
20029 static char *
20030 gen_producer_string (void)
20032 size_t j;
20033 auto_vec<dchar_p> switches;
20034 const char *language_string = lang_hooks.name;
20035 char *producer, *tail;
20036 const char *p;
20037 size_t len = dwarf_record_gcc_switches ? 0 : 3;
20038 size_t plen = strlen (language_string) + 1 + strlen (version_string);
20040 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
20041 switch (save_decoded_options[j].opt_index)
20043 case OPT_o:
20044 case OPT_d:
20045 case OPT_dumpbase:
20046 case OPT_dumpdir:
20047 case OPT_auxbase:
20048 case OPT_auxbase_strip:
20049 case OPT_quiet:
20050 case OPT_version:
20051 case OPT_v:
20052 case OPT_w:
20053 case OPT_L:
20054 case OPT_D:
20055 case OPT_I:
20056 case OPT_U:
20057 case OPT_SPECIAL_unknown:
20058 case OPT_SPECIAL_ignore:
20059 case OPT_SPECIAL_program_name:
20060 case OPT_SPECIAL_input_file:
20061 case OPT_grecord_gcc_switches:
20062 case OPT_gno_record_gcc_switches:
20063 case OPT__output_pch_:
20064 case OPT_fdiagnostics_show_location_:
20065 case OPT_fdiagnostics_show_option:
20066 case OPT_fdiagnostics_show_caret:
20067 case OPT_fdiagnostics_color_:
20068 case OPT_fverbose_asm:
20069 case OPT____:
20070 case OPT__sysroot_:
20071 case OPT_nostdinc:
20072 case OPT_nostdinc__:
20073 case OPT_fpreprocessed:
20074 case OPT_fltrans_output_list_:
20075 case OPT_fresolution_:
20076 /* Ignore these. */
20077 continue;
20078 default:
20079 if (cl_options[save_decoded_options[j].opt_index].flags
20080 & CL_NO_DWARF_RECORD)
20081 continue;
20082 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
20083 == '-');
20084 switch (save_decoded_options[j].canonical_option[0][1])
20086 case 'M':
20087 case 'i':
20088 case 'W':
20089 continue;
20090 case 'f':
20091 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
20092 "dump", 4) == 0)
20093 continue;
20094 break;
20095 default:
20096 break;
20098 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
20099 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
20100 break;
20103 producer = XNEWVEC (char, plen + 1 + len + 1);
20104 tail = producer;
20105 sprintf (tail, "%s %s", language_string, version_string);
20106 tail += plen;
20108 FOR_EACH_VEC_ELT (switches, j, p)
20110 len = strlen (p);
20111 *tail = ' ';
20112 memcpy (tail + 1, p, len);
20113 tail += len + 1;
20116 *tail = '\0';
20117 return producer;
20120 /* Given a C and/or C++ language/version string return the "highest".
20121 C++ is assumed to be "higher" than C in this case. Used for merging
20122 LTO translation unit languages. */
20123 static const char *
20124 highest_c_language (const char *lang1, const char *lang2)
20126 if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
20127 return "GNU C++14";
20128 if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
20129 return "GNU C++11";
20130 if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
20131 return "GNU C++98";
20133 if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
20134 return "GNU C11";
20135 if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
20136 return "GNU C99";
20137 if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
20138 return "GNU C89";
20140 gcc_unreachable ();
20144 /* Generate the DIE for the compilation unit. */
20146 static dw_die_ref
20147 gen_compile_unit_die (const char *filename)
20149 dw_die_ref die;
20150 const char *language_string = lang_hooks.name;
20151 int language;
20153 die = new_die (DW_TAG_compile_unit, NULL, NULL);
20155 if (filename)
20157 add_name_attribute (die, filename);
20158 /* Don't add cwd for <built-in>. */
20159 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
20160 add_comp_dir_attribute (die);
20163 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
20165 /* If our producer is LTO try to figure out a common language to use
20166 from the global list of translation units. */
20167 if (strcmp (language_string, "GNU GIMPLE") == 0)
20169 unsigned i;
20170 tree t;
20171 const char *common_lang = NULL;
20173 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
20175 if (!TRANSLATION_UNIT_LANGUAGE (t))
20176 continue;
20177 if (!common_lang)
20178 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
20179 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
20181 else if (strncmp (common_lang, "GNU C", 5) == 0
20182 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
20183 /* Mixing C and C++ is ok, use C++ in that case. */
20184 common_lang = highest_c_language (common_lang,
20185 TRANSLATION_UNIT_LANGUAGE (t));
20186 else
20188 /* Fall back to C. */
20189 common_lang = NULL;
20190 break;
20194 if (common_lang)
20195 language_string = common_lang;
20198 language = DW_LANG_C;
20199 if (strncmp (language_string, "GNU C", 5) == 0
20200 && ISDIGIT (language_string[5]))
20202 language = DW_LANG_C89;
20203 if (dwarf_version >= 3 || !dwarf_strict)
20205 if (strcmp (language_string, "GNU C89") != 0)
20206 language = DW_LANG_C99;
20208 if (dwarf_version >= 5 /* || !dwarf_strict */)
20209 if (strcmp (language_string, "GNU C11") == 0)
20210 language = DW_LANG_C11;
20213 else if (strncmp (language_string, "GNU C++", 7) == 0)
20215 language = DW_LANG_C_plus_plus;
20216 if (dwarf_version >= 5 /* || !dwarf_strict */)
20218 if (strcmp (language_string, "GNU C++11") == 0)
20219 language = DW_LANG_C_plus_plus_11;
20220 else if (strcmp (language_string, "GNU C++14") == 0)
20221 language = DW_LANG_C_plus_plus_14;
20224 else if (strcmp (language_string, "GNU F77") == 0)
20225 language = DW_LANG_Fortran77;
20226 else if (strcmp (language_string, "GNU Pascal") == 0)
20227 language = DW_LANG_Pascal83;
20228 else if (dwarf_version >= 3 || !dwarf_strict)
20230 if (strcmp (language_string, "GNU Ada") == 0)
20231 language = DW_LANG_Ada95;
20232 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
20234 language = DW_LANG_Fortran95;
20235 if (dwarf_version >= 5 /* || !dwarf_strict */)
20237 if (strcmp (language_string, "GNU Fortran2003") == 0)
20238 language = DW_LANG_Fortran03;
20239 else if (strcmp (language_string, "GNU Fortran2008") == 0)
20240 language = DW_LANG_Fortran08;
20243 else if (strcmp (language_string, "GNU Java") == 0)
20244 language = DW_LANG_Java;
20245 else if (strcmp (language_string, "GNU Objective-C") == 0)
20246 language = DW_LANG_ObjC;
20247 else if (strcmp (language_string, "GNU Objective-C++") == 0)
20248 language = DW_LANG_ObjC_plus_plus;
20249 else if (dwarf_version >= 5 || !dwarf_strict)
20251 if (strcmp (language_string, "GNU Go") == 0)
20252 language = DW_LANG_Go;
20255 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
20256 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
20257 language = DW_LANG_Fortran90;
20259 add_AT_unsigned (die, DW_AT_language, language);
20261 switch (language)
20263 case DW_LANG_Fortran77:
20264 case DW_LANG_Fortran90:
20265 case DW_LANG_Fortran95:
20266 case DW_LANG_Fortran03:
20267 case DW_LANG_Fortran08:
20268 /* Fortran has case insensitive identifiers and the front-end
20269 lowercases everything. */
20270 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
20271 break;
20272 default:
20273 /* The default DW_ID_case_sensitive doesn't need to be specified. */
20274 break;
20276 return die;
20279 /* Generate the DIE for a base class. */
20281 static void
20282 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
20284 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
20286 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, context_die);
20287 add_data_member_location_attribute (die, binfo);
20289 if (BINFO_VIRTUAL_P (binfo))
20290 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
20292 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
20293 children, otherwise the default is DW_ACCESS_public. In DWARF2
20294 the default has always been DW_ACCESS_private. */
20295 if (access == access_public_node)
20297 if (dwarf_version == 2
20298 || context_die->die_tag == DW_TAG_class_type)
20299 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
20301 else if (access == access_protected_node)
20302 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
20303 else if (dwarf_version > 2
20304 && context_die->die_tag != DW_TAG_class_type)
20305 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
20308 /* Generate a DIE for a class member. */
20310 static void
20311 gen_member_die (tree type, dw_die_ref context_die)
20313 tree member;
20314 tree binfo = TYPE_BINFO (type);
20315 dw_die_ref child;
20317 /* If this is not an incomplete type, output descriptions of each of its
20318 members. Note that as we output the DIEs necessary to represent the
20319 members of this record or union type, we will also be trying to output
20320 DIEs to represent the *types* of those members. However the `type'
20321 function (above) will specifically avoid generating type DIEs for member
20322 types *within* the list of member DIEs for this (containing) type except
20323 for those types (of members) which are explicitly marked as also being
20324 members of this (containing) type themselves. The g++ front- end can
20325 force any given type to be treated as a member of some other (containing)
20326 type by setting the TYPE_CONTEXT of the given (member) type to point to
20327 the TREE node representing the appropriate (containing) type. */
20329 /* First output info about the base classes. */
20330 if (binfo)
20332 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
20333 int i;
20334 tree base;
20336 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
20337 gen_inheritance_die (base,
20338 (accesses ? (*accesses)[i] : access_public_node),
20339 context_die);
20342 /* Now output info about the data members and type members. */
20343 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
20345 /* If we thought we were generating minimal debug info for TYPE
20346 and then changed our minds, some of the member declarations
20347 may have already been defined. Don't define them again, but
20348 do put them in the right order. */
20350 child = lookup_decl_die (member);
20351 if (child)
20352 splice_child_die (context_die, child);
20353 else
20354 gen_decl_die (member, NULL, context_die);
20357 /* We do not keep type methods in type variants. */
20358 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
20359 /* Now output info about the function members (if any). */
20360 if (TYPE_METHODS (type) != error_mark_node)
20361 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
20363 /* Don't include clones in the member list. */
20364 if (DECL_ABSTRACT_ORIGIN (member))
20365 continue;
20366 /* Nor constructors for anonymous classes. */
20367 if (DECL_ARTIFICIAL (member)
20368 && dwarf2_name (member, 0) == NULL)
20369 continue;
20371 child = lookup_decl_die (member);
20372 if (child)
20373 splice_child_die (context_die, child);
20374 else
20375 gen_decl_die (member, NULL, context_die);
20379 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
20380 is set, we pretend that the type was never defined, so we only get the
20381 member DIEs needed by later specification DIEs. */
20383 static void
20384 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
20385 enum debug_info_usage usage)
20387 if (TREE_ASM_WRITTEN (type))
20389 /* Fill in the bound of variable-length fields in late dwarf if
20390 still incomplete. */
20391 if (!early_dwarf && variably_modified_type_p (type, NULL))
20392 for (tree member = TYPE_FIELDS (type);
20393 member;
20394 member = DECL_CHAIN (member))
20395 fill_variable_array_bounds (TREE_TYPE (member));
20396 return;
20399 dw_die_ref type_die = lookup_type_die (type);
20400 dw_die_ref scope_die = 0;
20401 int nested = 0;
20402 int complete = (TYPE_SIZE (type)
20403 && (! TYPE_STUB_DECL (type)
20404 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
20405 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
20406 complete = complete && should_emit_struct_debug (type, usage);
20408 if (type_die && ! complete)
20409 return;
20411 if (TYPE_CONTEXT (type) != NULL_TREE
20412 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
20413 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
20414 nested = 1;
20416 scope_die = scope_die_for (type, context_die);
20418 /* Generate child dies for template paramaters. */
20419 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
20420 schedule_generic_params_dies_gen (type);
20422 if (! type_die || (nested && is_cu_die (scope_die)))
20423 /* First occurrence of type or toplevel definition of nested class. */
20425 dw_die_ref old_die = type_die;
20427 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
20428 ? record_type_tag (type) : DW_TAG_union_type,
20429 scope_die, type);
20430 equate_type_number_to_die (type, type_die);
20431 if (old_die)
20432 add_AT_specification (type_die, old_die);
20433 else
20434 add_name_attribute (type_die, type_tag (type));
20436 else
20437 remove_AT (type_die, DW_AT_declaration);
20439 /* If this type has been completed, then give it a byte_size attribute and
20440 then give a list of members. */
20441 if (complete && !ns_decl)
20443 /* Prevent infinite recursion in cases where the type of some member of
20444 this type is expressed in terms of this type itself. */
20445 TREE_ASM_WRITTEN (type) = 1;
20446 add_byte_size_attribute (type_die, type);
20447 if (TYPE_STUB_DECL (type) != NULL_TREE)
20449 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
20450 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
20453 /* If the first reference to this type was as the return type of an
20454 inline function, then it may not have a parent. Fix this now. */
20455 if (type_die->die_parent == NULL)
20456 add_child_die (scope_die, type_die);
20458 push_decl_scope (type);
20459 gen_member_die (type, type_die);
20460 pop_decl_scope ();
20462 add_gnat_descriptive_type_attribute (type_die, type, context_die);
20463 if (TYPE_ARTIFICIAL (type))
20464 add_AT_flag (type_die, DW_AT_artificial, 1);
20466 /* GNU extension: Record what type our vtable lives in. */
20467 if (TYPE_VFIELD (type))
20469 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
20471 gen_type_die (vtype, context_die);
20472 add_AT_die_ref (type_die, DW_AT_containing_type,
20473 lookup_type_die (vtype));
20476 else
20478 add_AT_flag (type_die, DW_AT_declaration, 1);
20480 /* We don't need to do this for function-local types. */
20481 if (TYPE_STUB_DECL (type)
20482 && ! decl_function_context (TYPE_STUB_DECL (type)))
20483 vec_safe_push (incomplete_types, type);
20486 if (get_AT (type_die, DW_AT_name))
20487 add_pubtype (type, type_die);
20490 /* Generate a DIE for a subroutine _type_. */
20492 static void
20493 gen_subroutine_type_die (tree type, dw_die_ref context_die)
20495 tree return_type = TREE_TYPE (type);
20496 dw_die_ref subr_die
20497 = new_die (DW_TAG_subroutine_type,
20498 scope_die_for (type, context_die), type);
20500 equate_type_number_to_die (type, subr_die);
20501 add_prototyped_attribute (subr_die, type);
20502 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, context_die);
20503 gen_formal_types_die (type, subr_die);
20505 if (get_AT (subr_die, DW_AT_name))
20506 add_pubtype (type, subr_die);
20509 /* Generate a DIE for a type definition. */
20511 static void
20512 gen_typedef_die (tree decl, dw_die_ref context_die)
20514 dw_die_ref type_die;
20515 tree origin;
20517 if (TREE_ASM_WRITTEN (decl))
20519 if (DECL_ORIGINAL_TYPE (decl))
20520 fill_variable_array_bounds (DECL_ORIGINAL_TYPE (decl));
20521 return;
20524 TREE_ASM_WRITTEN (decl) = 1;
20525 type_die = new_die (DW_TAG_typedef, context_die, decl);
20526 origin = decl_ultimate_origin (decl);
20527 if (origin != NULL)
20528 add_abstract_origin_attribute (type_die, origin);
20529 else
20531 tree type;
20533 add_name_and_src_coords_attributes (type_die, decl);
20534 if (DECL_ORIGINAL_TYPE (decl))
20536 type = DECL_ORIGINAL_TYPE (decl);
20538 if (type == error_mark_node)
20539 return;
20541 gcc_assert (type != TREE_TYPE (decl));
20542 equate_type_number_to_die (TREE_TYPE (decl), type_die);
20544 else
20546 type = TREE_TYPE (decl);
20548 if (type == error_mark_node)
20549 return;
20551 if (is_naming_typedef_decl (TYPE_NAME (type)))
20553 /* Here, we are in the case of decl being a typedef naming
20554 an anonymous type, e.g:
20555 typedef struct {...} foo;
20556 In that case TREE_TYPE (decl) is not a typedef variant
20557 type and TYPE_NAME of the anonymous type is set to the
20558 TYPE_DECL of the typedef. This construct is emitted by
20559 the C++ FE.
20561 TYPE is the anonymous struct named by the typedef
20562 DECL. As we need the DW_AT_type attribute of the
20563 DW_TAG_typedef to point to the DIE of TYPE, let's
20564 generate that DIE right away. add_type_attribute
20565 called below will then pick (via lookup_type_die) that
20566 anonymous struct DIE. */
20567 if (!TREE_ASM_WRITTEN (type))
20568 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
20570 /* This is a GNU Extension. We are adding a
20571 DW_AT_linkage_name attribute to the DIE of the
20572 anonymous struct TYPE. The value of that attribute
20573 is the name of the typedef decl naming the anonymous
20574 struct. This greatly eases the work of consumers of
20575 this debug info. */
20576 add_linkage_attr (lookup_type_die (type), decl);
20580 add_type_attribute (type_die, type, decl_quals (decl), context_die);
20582 if (is_naming_typedef_decl (decl))
20583 /* We want that all subsequent calls to lookup_type_die with
20584 TYPE in argument yield the DW_TAG_typedef we have just
20585 created. */
20586 equate_type_number_to_die (type, type_die);
20588 add_accessibility_attribute (type_die, decl);
20591 if (DECL_ABSTRACT_P (decl))
20592 equate_decl_number_to_die (decl, type_die);
20594 if (get_AT (type_die, DW_AT_name))
20595 add_pubtype (decl, type_die);
20598 /* Generate a DIE for a struct, class, enum or union type. */
20600 static void
20601 gen_tagged_type_die (tree type,
20602 dw_die_ref context_die,
20603 enum debug_info_usage usage)
20605 int need_pop;
20607 if (type == NULL_TREE
20608 || !is_tagged_type (type))
20609 return;
20611 if (TREE_ASM_WRITTEN (type))
20612 need_pop = 0;
20613 /* If this is a nested type whose containing class hasn't been written
20614 out yet, writing it out will cover this one, too. This does not apply
20615 to instantiations of member class templates; they need to be added to
20616 the containing class as they are generated. FIXME: This hurts the
20617 idea of combining type decls from multiple TUs, since we can't predict
20618 what set of template instantiations we'll get. */
20619 else if (TYPE_CONTEXT (type)
20620 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
20621 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
20623 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
20625 if (TREE_ASM_WRITTEN (type))
20626 return;
20628 /* If that failed, attach ourselves to the stub. */
20629 push_decl_scope (TYPE_CONTEXT (type));
20630 context_die = lookup_type_die (TYPE_CONTEXT (type));
20631 need_pop = 1;
20633 else if (TYPE_CONTEXT (type) != NULL_TREE
20634 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
20636 /* If this type is local to a function that hasn't been written
20637 out yet, use a NULL context for now; it will be fixed up in
20638 decls_for_scope. */
20639 context_die = lookup_decl_die (TYPE_CONTEXT (type));
20640 /* A declaration DIE doesn't count; nested types need to go in the
20641 specification. */
20642 if (context_die && is_declaration_die (context_die))
20643 context_die = NULL;
20644 need_pop = 0;
20646 else
20648 context_die = declare_in_namespace (type, context_die);
20649 need_pop = 0;
20652 if (TREE_CODE (type) == ENUMERAL_TYPE)
20654 /* This might have been written out by the call to
20655 declare_in_namespace. */
20656 if (!TREE_ASM_WRITTEN (type))
20657 gen_enumeration_type_die (type, context_die);
20659 else
20660 gen_struct_or_union_type_die (type, context_die, usage);
20662 if (need_pop)
20663 pop_decl_scope ();
20665 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
20666 it up if it is ever completed. gen_*_type_die will set it for us
20667 when appropriate. */
20670 /* Generate a type description DIE. */
20672 static void
20673 gen_type_die_with_usage (tree type, dw_die_ref context_die,
20674 enum debug_info_usage usage)
20676 struct array_descr_info info;
20678 if (type == NULL_TREE || type == error_mark_node)
20679 return;
20681 #ifdef ENABLE_CHECKING
20682 if (type)
20683 verify_type (type);
20684 #endif
20686 if (TYPE_NAME (type) != NULL_TREE
20687 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
20688 && is_redundant_typedef (TYPE_NAME (type))
20689 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
20690 /* The DECL of this type is a typedef we don't want to emit debug
20691 info for but we want debug info for its underlying typedef.
20692 This can happen for e.g, the injected-class-name of a C++
20693 type. */
20694 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
20696 /* If TYPE is a typedef type variant, let's generate debug info
20697 for the parent typedef which TYPE is a type of. */
20698 if (typedef_variant_p (type))
20700 if (TREE_ASM_WRITTEN (type))
20701 return;
20703 /* Prevent broken recursion; we can't hand off to the same type. */
20704 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
20706 /* Give typedefs the right scope. */
20707 context_die = scope_die_for (type, context_die);
20709 TREE_ASM_WRITTEN (type) = 1;
20711 gen_decl_die (TYPE_NAME (type), NULL, context_die);
20712 return;
20715 /* If type is an anonymous tagged type named by a typedef, let's
20716 generate debug info for the typedef. */
20717 if (is_naming_typedef_decl (TYPE_NAME (type)))
20719 /* Use the DIE of the containing namespace as the parent DIE of
20720 the type description DIE we want to generate. */
20721 if (DECL_CONTEXT (TYPE_NAME (type))
20722 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
20723 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
20725 gen_decl_die (TYPE_NAME (type), NULL, context_die);
20726 return;
20729 /* We are going to output a DIE to represent the unqualified version
20730 of this type (i.e. without any const or volatile qualifiers) so
20731 get the main variant (i.e. the unqualified version) of this type
20732 now. (Vectors are special because the debugging info is in the
20733 cloned type itself). */
20734 if (TREE_CODE (type) != VECTOR_TYPE)
20735 type = type_main_variant (type);
20737 /* If this is an array type with hidden descriptor, handle it first. */
20738 if (!TREE_ASM_WRITTEN (type)
20739 && lang_hooks.types.get_array_descr_info)
20741 memset (&info, 0, sizeof (info));
20742 if (lang_hooks.types.get_array_descr_info (type, &info))
20744 gen_descr_array_type_die (type, &info, context_die);
20745 TREE_ASM_WRITTEN (type) = 1;
20746 return;
20750 if (TREE_ASM_WRITTEN (type))
20752 /* Variable-length types may be incomplete even if
20753 TREE_ASM_WRITTEN. For such types, fall through to
20754 gen_array_type_die() and possibly fill in
20755 DW_AT_{upper,lower}_bound attributes. */
20756 if ((TREE_CODE (type) != ARRAY_TYPE
20757 && TREE_CODE (type) != RECORD_TYPE
20758 && TREE_CODE (type) != UNION_TYPE
20759 && TREE_CODE (type) != QUAL_UNION_TYPE)
20760 || !variably_modified_type_p (type, NULL))
20761 return;
20764 switch (TREE_CODE (type))
20766 case ERROR_MARK:
20767 break;
20769 case POINTER_TYPE:
20770 case REFERENCE_TYPE:
20771 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
20772 ensures that the gen_type_die recursion will terminate even if the
20773 type is recursive. Recursive types are possible in Ada. */
20774 /* ??? We could perhaps do this for all types before the switch
20775 statement. */
20776 TREE_ASM_WRITTEN (type) = 1;
20778 /* For these types, all that is required is that we output a DIE (or a
20779 set of DIEs) to represent the "basis" type. */
20780 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20781 DINFO_USAGE_IND_USE);
20782 break;
20784 case OFFSET_TYPE:
20785 /* This code is used for C++ pointer-to-data-member types.
20786 Output a description of the relevant class type. */
20787 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
20788 DINFO_USAGE_IND_USE);
20790 /* Output a description of the type of the object pointed to. */
20791 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20792 DINFO_USAGE_IND_USE);
20794 /* Now output a DIE to represent this pointer-to-data-member type
20795 itself. */
20796 gen_ptr_to_mbr_type_die (type, context_die);
20797 break;
20799 case FUNCTION_TYPE:
20800 /* Force out return type (in case it wasn't forced out already). */
20801 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20802 DINFO_USAGE_DIR_USE);
20803 gen_subroutine_type_die (type, context_die);
20804 break;
20806 case METHOD_TYPE:
20807 /* Force out return type (in case it wasn't forced out already). */
20808 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20809 DINFO_USAGE_DIR_USE);
20810 gen_subroutine_type_die (type, context_die);
20811 break;
20813 case ARRAY_TYPE:
20814 case VECTOR_TYPE:
20815 gen_array_type_die (type, context_die);
20816 break;
20818 case ENUMERAL_TYPE:
20819 case RECORD_TYPE:
20820 case UNION_TYPE:
20821 case QUAL_UNION_TYPE:
20822 gen_tagged_type_die (type, context_die, usage);
20823 return;
20825 case VOID_TYPE:
20826 case INTEGER_TYPE:
20827 case REAL_TYPE:
20828 case FIXED_POINT_TYPE:
20829 case COMPLEX_TYPE:
20830 case BOOLEAN_TYPE:
20831 case POINTER_BOUNDS_TYPE:
20832 /* No DIEs needed for fundamental types. */
20833 break;
20835 case NULLPTR_TYPE:
20836 case LANG_TYPE:
20837 /* Just use DW_TAG_unspecified_type. */
20839 dw_die_ref type_die = lookup_type_die (type);
20840 if (type_die == NULL)
20842 tree name = TYPE_IDENTIFIER (type);
20843 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
20844 type);
20845 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
20846 equate_type_number_to_die (type, type_die);
20849 break;
20851 default:
20852 if (is_cxx_auto (type))
20854 tree name = TYPE_IDENTIFIER (type);
20855 dw_die_ref *die = (name == get_identifier ("auto")
20856 ? &auto_die : &decltype_auto_die);
20857 if (!*die)
20859 *die = new_die (DW_TAG_unspecified_type,
20860 comp_unit_die (), NULL_TREE);
20861 add_name_attribute (*die, IDENTIFIER_POINTER (name));
20863 equate_type_number_to_die (type, *die);
20864 break;
20866 gcc_unreachable ();
20869 TREE_ASM_WRITTEN (type) = 1;
20872 static void
20873 gen_type_die (tree type, dw_die_ref context_die)
20875 if (type != error_mark_node)
20877 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
20878 #ifdef ENABLE_CHECKING
20879 dw_die_ref die = lookup_type_die (type);
20880 if (die)
20881 check_die (die);
20882 #endif
20886 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
20887 things which are local to the given block. */
20889 static void
20890 gen_block_die (tree stmt, dw_die_ref context_die)
20892 int must_output_die = 0;
20893 bool inlined_func;
20895 /* Ignore blocks that are NULL. */
20896 if (stmt == NULL_TREE)
20897 return;
20899 inlined_func = inlined_function_outer_scope_p (stmt);
20901 /* If the block is one fragment of a non-contiguous block, do not
20902 process the variables, since they will have been done by the
20903 origin block. Do process subblocks. */
20904 if (BLOCK_FRAGMENT_ORIGIN (stmt))
20906 tree sub;
20908 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
20909 gen_block_die (sub, context_die);
20911 return;
20914 /* Determine if we need to output any Dwarf DIEs at all to represent this
20915 block. */
20916 if (inlined_func)
20917 /* The outer scopes for inlinings *must* always be represented. We
20918 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
20919 must_output_die = 1;
20920 else
20922 /* Determine if this block directly contains any "significant"
20923 local declarations which we will need to output DIEs for. */
20924 if (debug_info_level > DINFO_LEVEL_TERSE)
20925 /* We are not in terse mode so *any* local declaration counts
20926 as being a "significant" one. */
20927 must_output_die = ((BLOCK_VARS (stmt) != NULL
20928 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
20929 && (TREE_USED (stmt)
20930 || TREE_ASM_WRITTEN (stmt)
20931 || BLOCK_ABSTRACT (stmt)));
20932 else if ((TREE_USED (stmt)
20933 || TREE_ASM_WRITTEN (stmt)
20934 || BLOCK_ABSTRACT (stmt))
20935 && !dwarf2out_ignore_block (stmt))
20936 must_output_die = 1;
20939 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
20940 DIE for any block which contains no significant local declarations at
20941 all. Rather, in such cases we just call `decls_for_scope' so that any
20942 needed Dwarf info for any sub-blocks will get properly generated. Note
20943 that in terse mode, our definition of what constitutes a "significant"
20944 local declaration gets restricted to include only inlined function
20945 instances and local (nested) function definitions. */
20946 if (must_output_die)
20948 if (inlined_func)
20950 /* If STMT block is abstract, that means we have been called
20951 indirectly from dwarf2out_abstract_function.
20952 That function rightfully marks the descendent blocks (of
20953 the abstract function it is dealing with) as being abstract,
20954 precisely to prevent us from emitting any
20955 DW_TAG_inlined_subroutine DIE as a descendent
20956 of an abstract function instance. So in that case, we should
20957 not call gen_inlined_subroutine_die.
20959 Later though, when cgraph asks dwarf2out to emit info
20960 for the concrete instance of the function decl into which
20961 the concrete instance of STMT got inlined, the later will lead
20962 to the generation of a DW_TAG_inlined_subroutine DIE. */
20963 if (! BLOCK_ABSTRACT (stmt))
20964 gen_inlined_subroutine_die (stmt, context_die);
20966 else
20967 gen_lexical_block_die (stmt, context_die);
20969 else
20970 decls_for_scope (stmt, context_die);
20973 /* Process variable DECL (or variable with origin ORIGIN) within
20974 block STMT and add it to CONTEXT_DIE. */
20975 static void
20976 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
20978 dw_die_ref die;
20979 tree decl_or_origin = decl ? decl : origin;
20981 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
20982 die = lookup_decl_die (decl_or_origin);
20983 else if (TREE_CODE (decl_or_origin) == TYPE_DECL
20984 && TYPE_DECL_IS_STUB (decl_or_origin))
20985 die = lookup_type_die (TREE_TYPE (decl_or_origin));
20986 else
20987 die = NULL;
20989 if (die != NULL && die->die_parent == NULL)
20990 add_child_die (context_die, die);
20991 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
20993 if (early_dwarf)
20994 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
20995 stmt, context_die);
20997 else
20998 gen_decl_die (decl, origin, context_die);
21001 /* Generate all of the decls declared within a given scope and (recursively)
21002 all of its sub-blocks. */
21004 static void
21005 decls_for_scope (tree stmt, dw_die_ref context_die)
21007 tree decl;
21008 unsigned int i;
21009 tree subblocks;
21011 /* Ignore NULL blocks. */
21012 if (stmt == NULL_TREE)
21013 return;
21015 /* Output the DIEs to represent all of the data objects and typedefs
21016 declared directly within this block but not within any nested
21017 sub-blocks. Also, nested function and tag DIEs have been
21018 generated with a parent of NULL; fix that up now. We don't
21019 have to do this if we're at -g1. */
21020 if (debug_info_level > DINFO_LEVEL_TERSE)
21022 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
21023 process_scope_var (stmt, decl, NULL_TREE, context_die);
21024 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
21025 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
21026 context_die);
21029 /* Even if we're at -g1, we need to process the subblocks in order to get
21030 inlined call information. */
21032 /* Output the DIEs to represent all sub-blocks (and the items declared
21033 therein) of this block. */
21034 for (subblocks = BLOCK_SUBBLOCKS (stmt);
21035 subblocks != NULL;
21036 subblocks = BLOCK_CHAIN (subblocks))
21037 gen_block_die (subblocks, context_die);
21040 /* Is this a typedef we can avoid emitting? */
21042 static inline int
21043 is_redundant_typedef (const_tree decl)
21045 if (TYPE_DECL_IS_STUB (decl))
21046 return 1;
21048 if (DECL_ARTIFICIAL (decl)
21049 && DECL_CONTEXT (decl)
21050 && is_tagged_type (DECL_CONTEXT (decl))
21051 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
21052 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
21053 /* Also ignore the artificial member typedef for the class name. */
21054 return 1;
21056 return 0;
21059 /* Return TRUE if TYPE is a typedef that names a type for linkage
21060 purposes. This kind of typedefs is produced by the C++ FE for
21061 constructs like:
21063 typedef struct {...} foo;
21065 In that case, there is no typedef variant type produced for foo.
21066 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
21067 struct type. */
21069 static bool
21070 is_naming_typedef_decl (const_tree decl)
21072 if (decl == NULL_TREE
21073 || TREE_CODE (decl) != TYPE_DECL
21074 || !is_tagged_type (TREE_TYPE (decl))
21075 || DECL_IS_BUILTIN (decl)
21076 || is_redundant_typedef (decl)
21077 /* It looks like Ada produces TYPE_DECLs that are very similar
21078 to C++ naming typedefs but that have different
21079 semantics. Let's be specific to c++ for now. */
21080 || !is_cxx ())
21081 return FALSE;
21083 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
21084 && TYPE_NAME (TREE_TYPE (decl)) == decl
21085 && (TYPE_STUB_DECL (TREE_TYPE (decl))
21086 != TYPE_NAME (TREE_TYPE (decl))));
21089 /* Looks up the DIE for a context. */
21091 static inline dw_die_ref
21092 lookup_context_die (tree context)
21094 if (context)
21096 /* Find die that represents this context. */
21097 if (TYPE_P (context))
21099 context = TYPE_MAIN_VARIANT (context);
21100 dw_die_ref ctx = lookup_type_die (context);
21101 if (!ctx)
21102 return NULL;
21103 return strip_naming_typedef (context, ctx);
21105 else
21106 return lookup_decl_die (context);
21108 return comp_unit_die ();
21111 /* Returns the DIE for a context. */
21113 static inline dw_die_ref
21114 get_context_die (tree context)
21116 if (context)
21118 /* Find die that represents this context. */
21119 if (TYPE_P (context))
21121 context = TYPE_MAIN_VARIANT (context);
21122 return strip_naming_typedef (context, force_type_die (context));
21124 else
21125 return force_decl_die (context);
21127 return comp_unit_die ();
21130 /* Returns the DIE for decl. A DIE will always be returned. */
21132 static dw_die_ref
21133 force_decl_die (tree decl)
21135 dw_die_ref decl_die;
21136 unsigned saved_external_flag;
21137 tree save_fn = NULL_TREE;
21138 decl_die = lookup_decl_die (decl);
21139 if (!decl_die)
21141 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
21143 decl_die = lookup_decl_die (decl);
21144 if (decl_die)
21145 return decl_die;
21147 switch (TREE_CODE (decl))
21149 case FUNCTION_DECL:
21150 /* Clear current_function_decl, so that gen_subprogram_die thinks
21151 that this is a declaration. At this point, we just want to force
21152 declaration die. */
21153 save_fn = current_function_decl;
21154 current_function_decl = NULL_TREE;
21155 gen_subprogram_die (decl, context_die);
21156 current_function_decl = save_fn;
21157 break;
21159 case VAR_DECL:
21160 /* Set external flag to force declaration die. Restore it after
21161 gen_decl_die() call. */
21162 saved_external_flag = DECL_EXTERNAL (decl);
21163 DECL_EXTERNAL (decl) = 1;
21164 gen_decl_die (decl, NULL, context_die);
21165 DECL_EXTERNAL (decl) = saved_external_flag;
21166 break;
21168 case NAMESPACE_DECL:
21169 if (dwarf_version >= 3 || !dwarf_strict)
21170 dwarf2out_decl (decl);
21171 else
21172 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
21173 decl_die = comp_unit_die ();
21174 break;
21176 case TRANSLATION_UNIT_DECL:
21177 decl_die = comp_unit_die ();
21178 break;
21180 default:
21181 gcc_unreachable ();
21184 /* We should be able to find the DIE now. */
21185 if (!decl_die)
21186 decl_die = lookup_decl_die (decl);
21187 gcc_assert (decl_die);
21190 return decl_die;
21193 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
21194 always returned. */
21196 static dw_die_ref
21197 force_type_die (tree type)
21199 dw_die_ref type_die;
21201 type_die = lookup_type_die (type);
21202 if (!type_die)
21204 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
21206 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
21207 context_die);
21208 gcc_assert (type_die);
21210 return type_die;
21213 /* Force out any required namespaces to be able to output DECL,
21214 and return the new context_die for it, if it's changed. */
21216 static dw_die_ref
21217 setup_namespace_context (tree thing, dw_die_ref context_die)
21219 tree context = (DECL_P (thing)
21220 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
21221 if (context && TREE_CODE (context) == NAMESPACE_DECL)
21222 /* Force out the namespace. */
21223 context_die = force_decl_die (context);
21225 return context_die;
21228 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
21229 type) within its namespace, if appropriate.
21231 For compatibility with older debuggers, namespace DIEs only contain
21232 declarations; all definitions are emitted at CU scope, with
21233 DW_AT_specification pointing to the declaration (like with class
21234 members). */
21236 static dw_die_ref
21237 declare_in_namespace (tree thing, dw_die_ref context_die)
21239 dw_die_ref ns_context;
21241 if (debug_info_level <= DINFO_LEVEL_TERSE)
21242 return context_die;
21244 /* External declarations in the local scope only need to be emitted
21245 once, not once in the namespace and once in the scope.
21247 This avoids declaring the `extern' below in the
21248 namespace DIE as well as in the innermost scope:
21250 namespace S
21252 int i=5;
21253 int foo()
21255 int i=8;
21256 extern int i;
21257 return i;
21261 if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
21262 return context_die;
21264 /* If this decl is from an inlined function, then don't try to emit it in its
21265 namespace, as we will get confused. It would have already been emitted
21266 when the abstract instance of the inline function was emitted anyways. */
21267 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
21268 return context_die;
21270 ns_context = setup_namespace_context (thing, context_die);
21272 if (ns_context != context_die)
21274 if (is_fortran ())
21275 return ns_context;
21276 if (DECL_P (thing))
21277 gen_decl_die (thing, NULL, ns_context);
21278 else
21279 gen_type_die (thing, ns_context);
21281 return context_die;
21284 /* Generate a DIE for a namespace or namespace alias. */
21286 static void
21287 gen_namespace_die (tree decl, dw_die_ref context_die)
21289 dw_die_ref namespace_die;
21291 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
21292 they are an alias of. */
21293 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
21295 /* Output a real namespace or module. */
21296 context_die = setup_namespace_context (decl, comp_unit_die ());
21297 namespace_die = new_die (is_fortran ()
21298 ? DW_TAG_module : DW_TAG_namespace,
21299 context_die, decl);
21300 /* For Fortran modules defined in different CU don't add src coords. */
21301 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
21303 const char *name = dwarf2_name (decl, 0);
21304 if (name)
21305 add_name_attribute (namespace_die, name);
21307 else
21308 add_name_and_src_coords_attributes (namespace_die, decl);
21309 if (DECL_EXTERNAL (decl))
21310 add_AT_flag (namespace_die, DW_AT_declaration, 1);
21311 equate_decl_number_to_die (decl, namespace_die);
21313 else
21315 /* Output a namespace alias. */
21317 /* Force out the namespace we are an alias of, if necessary. */
21318 dw_die_ref origin_die
21319 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
21321 if (DECL_FILE_SCOPE_P (decl)
21322 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
21323 context_die = setup_namespace_context (decl, comp_unit_die ());
21324 /* Now create the namespace alias DIE. */
21325 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
21326 add_name_and_src_coords_attributes (namespace_die, decl);
21327 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
21328 equate_decl_number_to_die (decl, namespace_die);
21330 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
21331 if (want_pubnames ())
21332 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
21335 /* Generate Dwarf debug information for a decl described by DECL.
21336 The return value is currently only meaningful for PARM_DECLs,
21337 for all other decls it returns NULL. */
21339 static dw_die_ref
21340 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
21342 tree decl_or_origin = decl ? decl : origin;
21343 tree class_origin = NULL, ultimate_origin;
21345 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
21346 return NULL;
21348 /* Ignore pointer bounds decls. */
21349 if (DECL_P (decl_or_origin)
21350 && TREE_TYPE (decl_or_origin)
21351 && POINTER_BOUNDS_P (decl_or_origin))
21352 return NULL;
21354 switch (TREE_CODE (decl_or_origin))
21356 case ERROR_MARK:
21357 break;
21359 case CONST_DECL:
21360 if (!is_fortran () && !is_ada ())
21362 /* The individual enumerators of an enum type get output when we output
21363 the Dwarf representation of the relevant enum type itself. */
21364 break;
21367 /* Emit its type. */
21368 gen_type_die (TREE_TYPE (decl), context_die);
21370 /* And its containing namespace. */
21371 context_die = declare_in_namespace (decl, context_die);
21373 gen_const_die (decl, context_die);
21374 break;
21376 case FUNCTION_DECL:
21377 /* Don't output any DIEs to represent mere function declarations,
21378 unless they are class members or explicit block externs. */
21379 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
21380 && DECL_FILE_SCOPE_P (decl_or_origin)
21381 && (current_function_decl == NULL_TREE
21382 || DECL_ARTIFICIAL (decl_or_origin)))
21383 break;
21385 #if 0
21386 /* FIXME */
21387 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
21388 on local redeclarations of global functions. That seems broken. */
21389 if (current_function_decl != decl)
21390 /* This is only a declaration. */;
21391 #endif
21393 /* If we're emitting a clone, emit info for the abstract instance. */
21394 if (origin || DECL_ORIGIN (decl) != decl)
21395 dwarf2out_abstract_function (origin
21396 ? DECL_ORIGIN (origin)
21397 : DECL_ABSTRACT_ORIGIN (decl));
21399 /* If we're emitting an out-of-line copy of an inline function,
21400 emit info for the abstract instance and set up to refer to it. */
21401 else if (cgraph_function_possibly_inlined_p (decl)
21402 && ! DECL_ABSTRACT_P (decl)
21403 && ! class_or_namespace_scope_p (context_die)
21404 /* dwarf2out_abstract_function won't emit a die if this is just
21405 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
21406 that case, because that works only if we have a die. */
21407 && DECL_INITIAL (decl) != NULL_TREE)
21409 dwarf2out_abstract_function (decl);
21410 set_decl_origin_self (decl);
21413 /* Otherwise we're emitting the primary DIE for this decl. */
21414 else if (debug_info_level > DINFO_LEVEL_TERSE)
21416 /* Before we describe the FUNCTION_DECL itself, make sure that we
21417 have its containing type. */
21418 if (!origin)
21419 origin = decl_class_context (decl);
21420 if (origin != NULL_TREE)
21421 gen_type_die (origin, context_die);
21423 /* And its return type. */
21424 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
21426 /* And its virtual context. */
21427 if (DECL_VINDEX (decl) != NULL_TREE)
21428 gen_type_die (DECL_CONTEXT (decl), context_die);
21430 /* Make sure we have a member DIE for decl. */
21431 if (origin != NULL_TREE)
21432 gen_type_die_for_member (origin, decl, context_die);
21434 /* And its containing namespace. */
21435 context_die = declare_in_namespace (decl, context_die);
21438 /* Now output a DIE to represent the function itself. */
21439 if (decl)
21440 gen_subprogram_die (decl, context_die);
21441 break;
21443 case TYPE_DECL:
21444 /* If we are in terse mode, don't generate any DIEs to represent any
21445 actual typedefs. */
21446 if (debug_info_level <= DINFO_LEVEL_TERSE)
21447 break;
21449 /* In the special case of a TYPE_DECL node representing the declaration
21450 of some type tag, if the given TYPE_DECL is marked as having been
21451 instantiated from some other (original) TYPE_DECL node (e.g. one which
21452 was generated within the original definition of an inline function) we
21453 used to generate a special (abbreviated) DW_TAG_structure_type,
21454 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
21455 should be actually referencing those DIEs, as variable DIEs with that
21456 type would be emitted already in the abstract origin, so it was always
21457 removed during unused type prunning. Don't add anything in this
21458 case. */
21459 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
21460 break;
21462 if (is_redundant_typedef (decl))
21463 gen_type_die (TREE_TYPE (decl), context_die);
21464 else
21465 /* Output a DIE to represent the typedef itself. */
21466 gen_typedef_die (decl, context_die);
21467 break;
21469 case LABEL_DECL:
21470 if (debug_info_level >= DINFO_LEVEL_NORMAL)
21471 gen_label_die (decl, context_die);
21472 break;
21474 case VAR_DECL:
21475 case RESULT_DECL:
21476 /* If we are in terse mode, don't generate any DIEs to represent any
21477 variable declarations or definitions. */
21478 if (debug_info_level <= DINFO_LEVEL_TERSE)
21479 break;
21481 /* Output any DIEs that are needed to specify the type of this data
21482 object. */
21483 if (decl_by_reference_p (decl_or_origin))
21484 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
21485 else
21486 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
21488 /* And its containing type. */
21489 class_origin = decl_class_context (decl_or_origin);
21490 if (class_origin != NULL_TREE)
21491 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
21493 /* And its containing namespace. */
21494 context_die = declare_in_namespace (decl_or_origin, context_die);
21496 /* Now output the DIE to represent the data object itself. This gets
21497 complicated because of the possibility that the VAR_DECL really
21498 represents an inlined instance of a formal parameter for an inline
21499 function. */
21500 ultimate_origin = decl_ultimate_origin (decl_or_origin);
21501 if (ultimate_origin != NULL_TREE
21502 && TREE_CODE (ultimate_origin) == PARM_DECL)
21503 gen_formal_parameter_die (decl, origin,
21504 true /* Emit name attribute. */,
21505 context_die);
21506 else
21507 gen_variable_die (decl, origin, context_die);
21508 break;
21510 case FIELD_DECL:
21511 /* Ignore the nameless fields that are used to skip bits but handle C++
21512 anonymous unions and structs. */
21513 if (DECL_NAME (decl) != NULL_TREE
21514 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
21515 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
21517 gen_type_die (member_declared_type (decl), context_die);
21518 gen_field_die (decl, context_die);
21520 break;
21522 case PARM_DECL:
21523 if (DECL_BY_REFERENCE (decl_or_origin))
21524 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
21525 else
21526 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
21527 return gen_formal_parameter_die (decl, origin,
21528 true /* Emit name attribute. */,
21529 context_die);
21531 case NAMESPACE_DECL:
21532 case IMPORTED_DECL:
21533 if (dwarf_version >= 3 || !dwarf_strict)
21534 gen_namespace_die (decl, context_die);
21535 break;
21537 case NAMELIST_DECL:
21538 gen_namelist_decl (DECL_NAME (decl), context_die,
21539 NAMELIST_DECL_ASSOCIATED_DECL (decl));
21540 break;
21542 default:
21543 /* Probably some frontend-internal decl. Assume we don't care. */
21544 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
21545 break;
21548 return NULL;
21551 /* Output initial debug information for global DECL. Called at the
21552 end of the parsing process.
21554 This is the initial debug generation process. As such, the DIEs
21555 generated may be incomplete. A later debug generation pass
21556 (dwarf2out_late_global_decl) will augment the information generated
21557 in this pass (e.g., with complete location info). */
21559 static void
21560 dwarf2out_early_global_decl (tree decl)
21562 set_early_dwarf s;
21564 /* gen_decl_die() will set DECL_ABSTRACT because
21565 cgraph_function_possibly_inlined_p() returns true. This is in
21566 turn will cause DW_AT_inline attributes to be set.
21568 This happens because at early dwarf generation, there is no
21569 cgraph information, causing cgraph_function_possibly_inlined_p()
21570 to return true. Trick cgraph_function_possibly_inlined_p()
21571 while we generate dwarf early. */
21572 bool save = symtab->global_info_ready;
21573 symtab->global_info_ready = true;
21575 /* We don't handle TYPE_DECLs. If required, they'll be reached via
21576 other DECLs and they can point to template types or other things
21577 that dwarf2out can't handle when done via dwarf2out_decl. */
21578 if (TREE_CODE (decl) != TYPE_DECL
21579 && TREE_CODE (decl) != PARM_DECL)
21581 tree save_fndecl = current_function_decl;
21582 if (TREE_CODE (decl) == FUNCTION_DECL)
21584 /* No cfun means the symbol has no body, so there's nothing
21585 to emit. */
21586 if (!DECL_STRUCT_FUNCTION (decl))
21587 goto early_decl_exit;
21589 current_function_decl = decl;
21591 dwarf2out_decl (decl);
21592 if (TREE_CODE (decl) == FUNCTION_DECL)
21593 current_function_decl = save_fndecl;
21595 early_decl_exit:
21596 symtab->global_info_ready = save;
21599 /* Output debug information for global decl DECL. Called from
21600 toplev.c after compilation proper has finished. */
21602 static void
21603 dwarf2out_late_global_decl (tree decl)
21605 /* Output any global decls we missed or fill-in any location
21606 information we were unable to determine on the first pass.
21608 Skip over functions because they were handled by the
21609 debug_hooks->function_decl() call in rest_of_handle_final. */
21610 if ((TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
21611 && !POINTER_BOUNDS_P (decl))
21612 dwarf2out_decl (decl);
21615 /* Output debug information for type decl DECL. Called from toplev.c
21616 and from language front ends (to record built-in types). */
21617 static void
21618 dwarf2out_type_decl (tree decl, int local)
21620 if (!local)
21622 set_early_dwarf s;
21623 dwarf2out_decl (decl);
21627 /* Output debug information for imported module or decl DECL.
21628 NAME is non-NULL name in the lexical block if the decl has been renamed.
21629 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
21630 that DECL belongs to.
21631 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
21632 static void
21633 dwarf2out_imported_module_or_decl_1 (tree decl,
21634 tree name,
21635 tree lexical_block,
21636 dw_die_ref lexical_block_die)
21638 expanded_location xloc;
21639 dw_die_ref imported_die = NULL;
21640 dw_die_ref at_import_die;
21642 if (TREE_CODE (decl) == IMPORTED_DECL)
21644 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
21645 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
21646 gcc_assert (decl);
21648 else
21649 xloc = expand_location (input_location);
21651 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
21653 at_import_die = force_type_die (TREE_TYPE (decl));
21654 /* For namespace N { typedef void T; } using N::T; base_type_die
21655 returns NULL, but DW_TAG_imported_declaration requires
21656 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
21657 if (!at_import_die)
21659 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
21660 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
21661 at_import_die = lookup_type_die (TREE_TYPE (decl));
21662 gcc_assert (at_import_die);
21665 else
21667 at_import_die = lookup_decl_die (decl);
21668 if (!at_import_die)
21670 /* If we're trying to avoid duplicate debug info, we may not have
21671 emitted the member decl for this field. Emit it now. */
21672 if (TREE_CODE (decl) == FIELD_DECL)
21674 tree type = DECL_CONTEXT (decl);
21676 if (TYPE_CONTEXT (type)
21677 && TYPE_P (TYPE_CONTEXT (type))
21678 && !should_emit_struct_debug (TYPE_CONTEXT (type),
21679 DINFO_USAGE_DIR_USE))
21680 return;
21681 gen_type_die_for_member (type, decl,
21682 get_context_die (TYPE_CONTEXT (type)));
21684 if (TREE_CODE (decl) == NAMELIST_DECL)
21685 at_import_die = gen_namelist_decl (DECL_NAME (decl),
21686 get_context_die (DECL_CONTEXT (decl)),
21687 NULL_TREE);
21688 else
21689 at_import_die = force_decl_die (decl);
21693 if (TREE_CODE (decl) == NAMESPACE_DECL)
21695 if (dwarf_version >= 3 || !dwarf_strict)
21696 imported_die = new_die (DW_TAG_imported_module,
21697 lexical_block_die,
21698 lexical_block);
21699 else
21700 return;
21702 else
21703 imported_die = new_die (DW_TAG_imported_declaration,
21704 lexical_block_die,
21705 lexical_block);
21707 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
21708 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
21709 if (name)
21710 add_AT_string (imported_die, DW_AT_name,
21711 IDENTIFIER_POINTER (name));
21712 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
21715 /* Output debug information for imported module or decl DECL.
21716 NAME is non-NULL name in context if the decl has been renamed.
21717 CHILD is true if decl is one of the renamed decls as part of
21718 importing whole module. */
21720 static void
21721 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
21722 bool child)
21724 /* dw_die_ref at_import_die; */
21725 dw_die_ref scope_die;
21727 if (debug_info_level <= DINFO_LEVEL_TERSE)
21728 return;
21730 gcc_assert (decl);
21732 set_early_dwarf s;
21734 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
21735 We need decl DIE for reference and scope die. First, get DIE for the decl
21736 itself. */
21738 /* Get the scope die for decl context. Use comp_unit_die for global module
21739 or decl. If die is not found for non globals, force new die. */
21740 if (context
21741 && TYPE_P (context)
21742 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
21743 return;
21745 if (!(dwarf_version >= 3 || !dwarf_strict))
21746 return;
21748 scope_die = get_context_die (context);
21750 if (child)
21752 gcc_assert (scope_die->die_child);
21753 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
21754 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
21755 scope_die = scope_die->die_child;
21758 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
21759 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
21762 /* Output debug information for namelists. */
21764 static dw_die_ref
21765 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
21767 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
21768 tree value;
21769 unsigned i;
21771 if (debug_info_level <= DINFO_LEVEL_TERSE)
21772 return NULL;
21774 gcc_assert (scope_die != NULL);
21775 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
21776 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
21778 /* If there are no item_decls, we have a nondefining namelist, e.g.
21779 with USE association; hence, set DW_AT_declaration. */
21780 if (item_decls == NULL_TREE)
21782 add_AT_flag (nml_die, DW_AT_declaration, 1);
21783 return nml_die;
21786 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
21788 nml_item_ref_die = lookup_decl_die (value);
21789 if (!nml_item_ref_die)
21790 nml_item_ref_die = force_decl_die (value);
21792 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
21793 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
21795 return nml_die;
21799 /* Write the debugging output for DECL and return the DIE. */
21801 static void
21802 dwarf2out_decl (tree decl)
21804 dw_die_ref context_die = comp_unit_die ();
21806 switch (TREE_CODE (decl))
21808 case ERROR_MARK:
21809 return;
21811 case FUNCTION_DECL:
21812 /* What we would really like to do here is to filter out all mere
21813 file-scope declarations of file-scope functions which are never
21814 referenced later within this translation unit (and keep all of ones
21815 that *are* referenced later on) but we aren't clairvoyant, so we have
21816 no idea which functions will be referenced in the future (i.e. later
21817 on within the current translation unit). So here we just ignore all
21818 file-scope function declarations which are not also definitions. If
21819 and when the debugger needs to know something about these functions,
21820 it will have to hunt around and find the DWARF information associated
21821 with the definition of the function.
21823 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
21824 nodes represent definitions and which ones represent mere
21825 declarations. We have to check DECL_INITIAL instead. That's because
21826 the C front-end supports some weird semantics for "extern inline"
21827 function definitions. These can get inlined within the current
21828 translation unit (and thus, we need to generate Dwarf info for their
21829 abstract instances so that the Dwarf info for the concrete inlined
21830 instances can have something to refer to) but the compiler never
21831 generates any out-of-lines instances of such things (despite the fact
21832 that they *are* definitions).
21834 The important point is that the C front-end marks these "extern
21835 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
21836 them anyway. Note that the C++ front-end also plays some similar games
21837 for inline function definitions appearing within include files which
21838 also contain `#pragma interface' pragmas.
21840 If we are called from dwarf2out_abstract_function output a DIE
21841 anyway. We can end up here this way with early inlining and LTO
21842 where the inlined function is output in a different LTRANS unit
21843 or not at all. */
21844 if (DECL_INITIAL (decl) == NULL_TREE
21845 && ! DECL_ABSTRACT_P (decl))
21846 return;
21848 /* If we're a nested function, initially use a parent of NULL; if we're
21849 a plain function, this will be fixed up in decls_for_scope. If
21850 we're a method, it will be ignored, since we already have a DIE. */
21851 if (decl_function_context (decl)
21852 /* But if we're in terse mode, we don't care about scope. */
21853 && debug_info_level > DINFO_LEVEL_TERSE)
21854 context_die = NULL;
21855 break;
21857 case VAR_DECL:
21858 /* For local statics lookup proper context die. */
21859 if (local_function_static (decl))
21860 context_die = lookup_decl_die (DECL_CONTEXT (decl));
21862 /* If we are in terse mode, don't generate any DIEs to represent any
21863 variable declarations or definitions. */
21864 if (debug_info_level <= DINFO_LEVEL_TERSE)
21865 return;
21866 break;
21868 case CONST_DECL:
21869 if (debug_info_level <= DINFO_LEVEL_TERSE)
21870 return;
21871 if (!is_fortran () && !is_ada ())
21872 return;
21873 if (TREE_STATIC (decl) && decl_function_context (decl))
21874 context_die = lookup_decl_die (DECL_CONTEXT (decl));
21875 break;
21877 case NAMESPACE_DECL:
21878 case IMPORTED_DECL:
21879 if (debug_info_level <= DINFO_LEVEL_TERSE)
21880 return;
21881 if (lookup_decl_die (decl) != NULL)
21882 return;
21883 break;
21885 case TYPE_DECL:
21886 /* Don't emit stubs for types unless they are needed by other DIEs. */
21887 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
21888 return;
21890 /* Don't bother trying to generate any DIEs to represent any of the
21891 normal built-in types for the language we are compiling. */
21892 if (DECL_IS_BUILTIN (decl))
21893 return;
21895 /* If we are in terse mode, don't generate any DIEs for types. */
21896 if (debug_info_level <= DINFO_LEVEL_TERSE)
21897 return;
21899 /* If we're a function-scope tag, initially use a parent of NULL;
21900 this will be fixed up in decls_for_scope. */
21901 if (decl_function_context (decl))
21902 context_die = NULL;
21904 break;
21906 case NAMELIST_DECL:
21907 break;
21909 default:
21910 return;
21913 gen_decl_die (decl, NULL, context_die);
21915 #ifdef ENABLE_CHECKING
21916 dw_die_ref die = lookup_decl_die (decl);
21917 if (die)
21918 check_die (die);
21919 #endif
21922 /* Write the debugging output for DECL. */
21924 static void
21925 dwarf2out_function_decl (tree decl)
21927 dwarf2out_decl (decl);
21928 call_arg_locations = NULL;
21929 call_arg_loc_last = NULL;
21930 call_site_count = -1;
21931 tail_call_site_count = -1;
21932 decl_loc_table->empty ();
21933 cached_dw_loc_list_table->empty ();
21936 /* Output a marker (i.e. a label) for the beginning of the generated code for
21937 a lexical block. */
21939 static void
21940 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
21941 unsigned int blocknum)
21943 switch_to_section (current_function_section ());
21944 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
21947 /* Output a marker (i.e. a label) for the end of the generated code for a
21948 lexical block. */
21950 static void
21951 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
21953 switch_to_section (current_function_section ());
21954 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
21957 /* Returns nonzero if it is appropriate not to emit any debugging
21958 information for BLOCK, because it doesn't contain any instructions.
21960 Don't allow this for blocks with nested functions or local classes
21961 as we would end up with orphans, and in the presence of scheduling
21962 we may end up calling them anyway. */
21964 static bool
21965 dwarf2out_ignore_block (const_tree block)
21967 tree decl;
21968 unsigned int i;
21970 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
21971 if (TREE_CODE (decl) == FUNCTION_DECL
21972 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21973 return 0;
21974 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
21976 decl = BLOCK_NONLOCALIZED_VAR (block, i);
21977 if (TREE_CODE (decl) == FUNCTION_DECL
21978 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21979 return 0;
21982 return 1;
21985 /* Hash table routines for file_hash. */
21987 bool
21988 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
21990 return filename_cmp (p1->filename, p2) == 0;
21993 hashval_t
21994 dwarf_file_hasher::hash (dwarf_file_data *p)
21996 return htab_hash_string (p->filename);
21999 /* Lookup FILE_NAME (in the list of filenames that we know about here in
22000 dwarf2out.c) and return its "index". The index of each (known) filename is
22001 just a unique number which is associated with only that one filename. We
22002 need such numbers for the sake of generating labels (in the .debug_sfnames
22003 section) and references to those files numbers (in the .debug_srcinfo
22004 and.debug_macinfo sections). If the filename given as an argument is not
22005 found in our current list, add it to the list and assign it the next
22006 available unique index number. */
22008 static struct dwarf_file_data *
22009 lookup_filename (const char *file_name)
22011 struct dwarf_file_data * created;
22013 if (!file_name)
22014 return NULL;
22016 dwarf_file_data **slot
22017 = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
22018 INSERT);
22019 if (*slot)
22020 return *slot;
22022 created = ggc_alloc<dwarf_file_data> ();
22023 created->filename = file_name;
22024 created->emitted_number = 0;
22025 *slot = created;
22026 return created;
22029 /* If the assembler will construct the file table, then translate the compiler
22030 internal file table number into the assembler file table number, and emit
22031 a .file directive if we haven't already emitted one yet. The file table
22032 numbers are different because we prune debug info for unused variables and
22033 types, which may include filenames. */
22035 static int
22036 maybe_emit_file (struct dwarf_file_data * fd)
22038 if (! fd->emitted_number)
22040 if (last_emitted_file)
22041 fd->emitted_number = last_emitted_file->emitted_number + 1;
22042 else
22043 fd->emitted_number = 1;
22044 last_emitted_file = fd;
22046 if (DWARF2_ASM_LINE_DEBUG_INFO)
22048 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
22049 output_quoted_string (asm_out_file,
22050 remap_debug_filename (fd->filename));
22051 fputc ('\n', asm_out_file);
22055 return fd->emitted_number;
22058 /* Schedule generation of a DW_AT_const_value attribute to DIE.
22059 That generation should happen after function debug info has been
22060 generated. The value of the attribute is the constant value of ARG. */
22062 static void
22063 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
22065 die_arg_entry entry;
22067 if (!die || !arg)
22068 return;
22070 if (!tmpl_value_parm_die_table)
22071 vec_alloc (tmpl_value_parm_die_table, 32);
22073 entry.die = die;
22074 entry.arg = arg;
22075 vec_safe_push (tmpl_value_parm_die_table, entry);
22078 /* Return TRUE if T is an instance of generic type, FALSE
22079 otherwise. */
22081 static bool
22082 generic_type_p (tree t)
22084 if (t == NULL_TREE || !TYPE_P (t))
22085 return false;
22086 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
22089 /* Schedule the generation of the generic parameter dies for the
22090 instance of generic type T. The proper generation itself is later
22091 done by gen_scheduled_generic_parms_dies. */
22093 static void
22094 schedule_generic_params_dies_gen (tree t)
22096 if (!generic_type_p (t))
22097 return;
22099 if (!generic_type_instances)
22100 vec_alloc (generic_type_instances, 256);
22102 vec_safe_push (generic_type_instances, t);
22105 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
22106 by append_entry_to_tmpl_value_parm_die_table. This function must
22107 be called after function DIEs have been generated. */
22109 static void
22110 gen_remaining_tmpl_value_param_die_attribute (void)
22112 if (tmpl_value_parm_die_table)
22114 unsigned i;
22115 die_arg_entry *e;
22117 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
22118 tree_add_const_value_attribute (e->die, e->arg);
22122 /* Generate generic parameters DIEs for instances of generic types
22123 that have been previously scheduled by
22124 schedule_generic_params_dies_gen. This function must be called
22125 after all the types of the CU have been laid out. */
22127 static void
22128 gen_scheduled_generic_parms_dies (void)
22130 unsigned i;
22131 tree t;
22133 if (!generic_type_instances)
22134 return;
22136 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
22137 if (COMPLETE_TYPE_P (t))
22138 gen_generic_params_dies (t);
22142 /* Replace DW_AT_name for the decl with name. */
22144 static void
22145 dwarf2out_set_name (tree decl, tree name)
22147 dw_die_ref die;
22148 dw_attr_ref attr;
22149 const char *dname;
22151 die = TYPE_SYMTAB_DIE (decl);
22152 if (!die)
22153 return;
22155 dname = dwarf2_name (name, 0);
22156 if (!dname)
22157 return;
22159 attr = get_AT (die, DW_AT_name);
22160 if (attr)
22162 struct indirect_string_node *node;
22164 node = find_AT_string (dname);
22165 /* replace the string. */
22166 attr->dw_attr_val.v.val_str = node;
22169 else
22170 add_name_attribute (die, dname);
22173 /* True if before or during processing of the first function being emitted. */
22174 static bool in_first_function_p = true;
22175 /* True if loc_note during dwarf2out_var_location call might still be
22176 before first real instruction at address equal to .Ltext0. */
22177 static bool maybe_at_text_label_p = true;
22178 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
22179 static unsigned int first_loclabel_num_not_at_text_label;
22181 /* Called by the final INSN scan whenever we see a var location. We
22182 use it to drop labels in the right places, and throw the location in
22183 our lookup table. */
22185 static void
22186 dwarf2out_var_location (rtx_insn *loc_note)
22188 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
22189 struct var_loc_node *newloc;
22190 rtx_insn *next_real, *next_note;
22191 static const char *last_label;
22192 static const char *last_postcall_label;
22193 static bool last_in_cold_section_p;
22194 static rtx_insn *expected_next_loc_note;
22195 tree decl;
22196 bool var_loc_p;
22198 if (!NOTE_P (loc_note))
22200 if (CALL_P (loc_note))
22202 call_site_count++;
22203 if (SIBLING_CALL_P (loc_note))
22204 tail_call_site_count++;
22206 return;
22209 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
22210 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
22211 return;
22213 /* Optimize processing a large consecutive sequence of location
22214 notes so we don't spend too much time in next_real_insn. If the
22215 next insn is another location note, remember the next_real_insn
22216 calculation for next time. */
22217 next_real = cached_next_real_insn;
22218 if (next_real)
22220 if (expected_next_loc_note != loc_note)
22221 next_real = NULL;
22224 next_note = NEXT_INSN (loc_note);
22225 if (! next_note
22226 || next_note->deleted ()
22227 || ! NOTE_P (next_note)
22228 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
22229 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
22230 next_note = NULL;
22232 if (! next_real)
22233 next_real = next_real_insn (loc_note);
22235 if (next_note)
22237 expected_next_loc_note = next_note;
22238 cached_next_real_insn = next_real;
22240 else
22241 cached_next_real_insn = NULL;
22243 /* If there are no instructions which would be affected by this note,
22244 don't do anything. */
22245 if (var_loc_p
22246 && next_real == NULL_RTX
22247 && !NOTE_DURING_CALL_P (loc_note))
22248 return;
22250 if (next_real == NULL_RTX)
22251 next_real = get_last_insn ();
22253 /* If there were any real insns between note we processed last time
22254 and this note (or if it is the first note), clear
22255 last_{,postcall_}label so that they are not reused this time. */
22256 if (last_var_location_insn == NULL_RTX
22257 || last_var_location_insn != next_real
22258 || last_in_cold_section_p != in_cold_section_p)
22260 last_label = NULL;
22261 last_postcall_label = NULL;
22264 if (var_loc_p)
22266 decl = NOTE_VAR_LOCATION_DECL (loc_note);
22267 newloc = add_var_loc_to_decl (decl, loc_note,
22268 NOTE_DURING_CALL_P (loc_note)
22269 ? last_postcall_label : last_label);
22270 if (newloc == NULL)
22271 return;
22273 else
22275 decl = NULL_TREE;
22276 newloc = NULL;
22279 /* If there were no real insns between note we processed last time
22280 and this note, use the label we emitted last time. Otherwise
22281 create a new label and emit it. */
22282 if (last_label == NULL)
22284 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
22285 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
22286 loclabel_num++;
22287 last_label = ggc_strdup (loclabel);
22288 /* See if loclabel might be equal to .Ltext0. If yes,
22289 bump first_loclabel_num_not_at_text_label. */
22290 if (!have_multiple_function_sections
22291 && in_first_function_p
22292 && maybe_at_text_label_p)
22294 static rtx_insn *last_start;
22295 rtx_insn *insn;
22296 for (insn = loc_note; insn; insn = previous_insn (insn))
22297 if (insn == last_start)
22298 break;
22299 else if (!NONDEBUG_INSN_P (insn))
22300 continue;
22301 else
22303 rtx body = PATTERN (insn);
22304 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
22305 continue;
22306 /* Inline asm could occupy zero bytes. */
22307 else if (GET_CODE (body) == ASM_INPUT
22308 || asm_noperands (body) >= 0)
22309 continue;
22310 #ifdef HAVE_attr_length
22311 else if (get_attr_min_length (insn) == 0)
22312 continue;
22313 #endif
22314 else
22316 /* Assume insn has non-zero length. */
22317 maybe_at_text_label_p = false;
22318 break;
22321 if (maybe_at_text_label_p)
22323 last_start = loc_note;
22324 first_loclabel_num_not_at_text_label = loclabel_num;
22329 if (!var_loc_p)
22331 struct call_arg_loc_node *ca_loc
22332 = ggc_cleared_alloc<call_arg_loc_node> ();
22333 rtx_insn *prev = prev_real_insn (loc_note);
22334 rtx x;
22335 ca_loc->call_arg_loc_note = loc_note;
22336 ca_loc->next = NULL;
22337 ca_loc->label = last_label;
22338 gcc_assert (prev
22339 && (CALL_P (prev)
22340 || (NONJUMP_INSN_P (prev)
22341 && GET_CODE (PATTERN (prev)) == SEQUENCE
22342 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
22343 if (!CALL_P (prev))
22344 prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
22345 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
22346 x = get_call_rtx_from (PATTERN (prev));
22347 if (x)
22349 x = XEXP (XEXP (x, 0), 0);
22350 if (GET_CODE (x) == SYMBOL_REF
22351 && SYMBOL_REF_DECL (x)
22352 && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
22353 ca_loc->symbol_ref = x;
22355 ca_loc->block = insn_scope (prev);
22356 if (call_arg_locations)
22357 call_arg_loc_last->next = ca_loc;
22358 else
22359 call_arg_locations = ca_loc;
22360 call_arg_loc_last = ca_loc;
22362 else if (!NOTE_DURING_CALL_P (loc_note))
22363 newloc->label = last_label;
22364 else
22366 if (!last_postcall_label)
22368 sprintf (loclabel, "%s-1", last_label);
22369 last_postcall_label = ggc_strdup (loclabel);
22371 newloc->label = last_postcall_label;
22374 last_var_location_insn = next_real;
22375 last_in_cold_section_p = in_cold_section_p;
22378 /* Note in one location list that text section has changed. */
22381 var_location_switch_text_section_1 (var_loc_list **slot, void *)
22383 var_loc_list *list = *slot;
22384 if (list->first)
22385 list->last_before_switch
22386 = list->last->next ? list->last->next : list->last;
22387 return 1;
22390 /* Note in all location lists that text section has changed. */
22392 static void
22393 var_location_switch_text_section (void)
22395 if (decl_loc_table == NULL)
22396 return;
22398 decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
22401 /* Create a new line number table. */
22403 static dw_line_info_table *
22404 new_line_info_table (void)
22406 dw_line_info_table *table;
22408 table = ggc_cleared_alloc<dw_line_info_table_struct> ();
22409 table->file_num = 1;
22410 table->line_num = 1;
22411 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
22413 return table;
22416 /* Lookup the "current" table into which we emit line info, so
22417 that we don't have to do it for every source line. */
22419 static void
22420 set_cur_line_info_table (section *sec)
22422 dw_line_info_table *table;
22424 if (sec == text_section)
22425 table = text_section_line_info;
22426 else if (sec == cold_text_section)
22428 table = cold_text_section_line_info;
22429 if (!table)
22431 cold_text_section_line_info = table = new_line_info_table ();
22432 table->end_label = cold_end_label;
22435 else
22437 const char *end_label;
22439 if (flag_reorder_blocks_and_partition)
22441 if (in_cold_section_p)
22442 end_label = crtl->subsections.cold_section_end_label;
22443 else
22444 end_label = crtl->subsections.hot_section_end_label;
22446 else
22448 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22449 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
22450 current_function_funcdef_no);
22451 end_label = ggc_strdup (label);
22454 table = new_line_info_table ();
22455 table->end_label = end_label;
22457 vec_safe_push (separate_line_info, table);
22460 if (DWARF2_ASM_LINE_DEBUG_INFO)
22461 table->is_stmt = (cur_line_info_table
22462 ? cur_line_info_table->is_stmt
22463 : DWARF_LINE_DEFAULT_IS_STMT_START);
22464 cur_line_info_table = table;
22468 /* We need to reset the locations at the beginning of each
22469 function. We can't do this in the end_function hook, because the
22470 declarations that use the locations won't have been output when
22471 that hook is called. Also compute have_multiple_function_sections here. */
22473 static void
22474 dwarf2out_begin_function (tree fun)
22476 section *sec = function_section (fun);
22478 if (sec != text_section)
22479 have_multiple_function_sections = true;
22481 if (flag_reorder_blocks_and_partition && !cold_text_section)
22483 gcc_assert (current_function_decl == fun);
22484 cold_text_section = unlikely_text_section ();
22485 switch_to_section (cold_text_section);
22486 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
22487 switch_to_section (sec);
22490 dwarf2out_note_section_used ();
22491 call_site_count = 0;
22492 tail_call_site_count = 0;
22494 set_cur_line_info_table (sec);
22497 /* Helper function of dwarf2out_end_function, called only after emitting
22498 the very first function into assembly. Check if some .debug_loc range
22499 might end with a .LVL* label that could be equal to .Ltext0.
22500 In that case we must force using absolute addresses in .debug_loc ranges,
22501 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
22502 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
22503 list terminator.
22504 Set have_multiple_function_sections to true in that case and
22505 terminate htab traversal. */
22508 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
22510 var_loc_list *entry = *slot;
22511 struct var_loc_node *node;
22513 node = entry->first;
22514 if (node && node->next && node->next->label)
22516 unsigned int i;
22517 const char *label = node->next->label;
22518 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
22520 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
22522 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
22523 if (strcmp (label, loclabel) == 0)
22525 have_multiple_function_sections = true;
22526 return 0;
22530 return 1;
22533 /* Hook called after emitting a function into assembly.
22534 This does something only for the very first function emitted. */
22536 static void
22537 dwarf2out_end_function (unsigned int)
22539 if (in_first_function_p
22540 && !have_multiple_function_sections
22541 && first_loclabel_num_not_at_text_label
22542 && decl_loc_table)
22543 decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
22544 in_first_function_p = false;
22545 maybe_at_text_label_p = false;
22548 /* Temporary holder for dwarf2out_register_main_translation_unit. Used to let
22549 front-ends register a translation unit even before dwarf2out_init is
22550 called. */
22551 static tree main_translation_unit = NULL_TREE;
22553 /* Hook called by front-ends after they built their main translation unit.
22554 Associate comp_unit_die to UNIT. */
22556 static void
22557 dwarf2out_register_main_translation_unit (tree unit)
22559 gcc_assert (TREE_CODE (unit) == TRANSLATION_UNIT_DECL
22560 && main_translation_unit == NULL_TREE);
22561 main_translation_unit = unit;
22562 /* If dwarf2out_init has not been called yet, it will perform the association
22563 itself looking at main_translation_unit. */
22564 if (decl_die_table != NULL)
22565 equate_decl_number_to_die (unit, comp_unit_die ());
22568 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
22570 static void
22571 push_dw_line_info_entry (dw_line_info_table *table,
22572 enum dw_line_info_opcode opcode, unsigned int val)
22574 dw_line_info_entry e;
22575 e.opcode = opcode;
22576 e.val = val;
22577 vec_safe_push (table->entries, e);
22580 /* Output a label to mark the beginning of a source code line entry
22581 and record information relating to this source line, in
22582 'line_info_table' for later output of the .debug_line section. */
22583 /* ??? The discriminator parameter ought to be unsigned. */
22585 static void
22586 dwarf2out_source_line (unsigned int line, const char *filename,
22587 int discriminator, bool is_stmt)
22589 unsigned int file_num;
22590 dw_line_info_table *table;
22592 if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
22593 return;
22595 /* The discriminator column was added in dwarf4. Simplify the below
22596 by simply removing it if we're not supposed to output it. */
22597 if (dwarf_version < 4 && dwarf_strict)
22598 discriminator = 0;
22600 table = cur_line_info_table;
22601 file_num = maybe_emit_file (lookup_filename (filename));
22603 /* ??? TODO: Elide duplicate line number entries. Traditionally,
22604 the debugger has used the second (possibly duplicate) line number
22605 at the beginning of the function to mark the end of the prologue.
22606 We could eliminate any other duplicates within the function. For
22607 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
22608 that second line number entry. */
22609 /* Recall that this end-of-prologue indication is *not* the same thing
22610 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
22611 to which the hook corresponds, follows the last insn that was
22612 emitted by gen_prologue. What we need is to precede the first insn
22613 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
22614 insn that corresponds to something the user wrote. These may be
22615 very different locations once scheduling is enabled. */
22617 if (0 && file_num == table->file_num
22618 && line == table->line_num
22619 && discriminator == table->discrim_num
22620 && is_stmt == table->is_stmt)
22621 return;
22623 switch_to_section (current_function_section ());
22625 /* If requested, emit something human-readable. */
22626 if (flag_debug_asm)
22627 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
22629 if (DWARF2_ASM_LINE_DEBUG_INFO)
22631 /* Emit the .loc directive understood by GNU as. */
22632 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
22633 file_num, line, is_stmt, discriminator */
22634 fputs ("\t.loc ", asm_out_file);
22635 fprint_ul (asm_out_file, file_num);
22636 putc (' ', asm_out_file);
22637 fprint_ul (asm_out_file, line);
22638 putc (' ', asm_out_file);
22639 putc ('0', asm_out_file);
22641 if (is_stmt != table->is_stmt)
22643 fputs (" is_stmt ", asm_out_file);
22644 putc (is_stmt ? '1' : '0', asm_out_file);
22646 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
22648 gcc_assert (discriminator > 0);
22649 fputs (" discriminator ", asm_out_file);
22650 fprint_ul (asm_out_file, (unsigned long) discriminator);
22652 putc ('\n', asm_out_file);
22654 else
22656 unsigned int label_num = ++line_info_label_num;
22658 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
22660 push_dw_line_info_entry (table, LI_set_address, label_num);
22661 if (file_num != table->file_num)
22662 push_dw_line_info_entry (table, LI_set_file, file_num);
22663 if (discriminator != table->discrim_num)
22664 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
22665 if (is_stmt != table->is_stmt)
22666 push_dw_line_info_entry (table, LI_negate_stmt, 0);
22667 push_dw_line_info_entry (table, LI_set_line, line);
22670 table->file_num = file_num;
22671 table->line_num = line;
22672 table->discrim_num = discriminator;
22673 table->is_stmt = is_stmt;
22674 table->in_use = true;
22677 /* Record the beginning of a new source file. */
22679 static void
22680 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
22682 if (flag_eliminate_dwarf2_dups)
22684 /* Record the beginning of the file for break_out_includes. */
22685 dw_die_ref bincl_die;
22687 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
22688 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
22691 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22693 macinfo_entry e;
22694 e.code = DW_MACINFO_start_file;
22695 e.lineno = lineno;
22696 e.info = ggc_strdup (filename);
22697 vec_safe_push (macinfo_table, e);
22701 /* Record the end of a source file. */
22703 static void
22704 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
22706 if (flag_eliminate_dwarf2_dups)
22707 /* Record the end of the file for break_out_includes. */
22708 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
22710 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22712 macinfo_entry e;
22713 e.code = DW_MACINFO_end_file;
22714 e.lineno = lineno;
22715 e.info = NULL;
22716 vec_safe_push (macinfo_table, e);
22720 /* Called from debug_define in toplev.c. The `buffer' parameter contains
22721 the tail part of the directive line, i.e. the part which is past the
22722 initial whitespace, #, whitespace, directive-name, whitespace part. */
22724 static void
22725 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
22726 const char *buffer ATTRIBUTE_UNUSED)
22728 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22730 macinfo_entry e;
22731 /* Insert a dummy first entry to be able to optimize the whole
22732 predefined macro block using DW_MACRO_GNU_transparent_include. */
22733 if (macinfo_table->is_empty () && lineno <= 1)
22735 e.code = 0;
22736 e.lineno = 0;
22737 e.info = NULL;
22738 vec_safe_push (macinfo_table, e);
22740 e.code = DW_MACINFO_define;
22741 e.lineno = lineno;
22742 e.info = ggc_strdup (buffer);
22743 vec_safe_push (macinfo_table, e);
22747 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
22748 the tail part of the directive line, i.e. the part which is past the
22749 initial whitespace, #, whitespace, directive-name, whitespace part. */
22751 static void
22752 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
22753 const char *buffer ATTRIBUTE_UNUSED)
22755 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22757 macinfo_entry e;
22758 /* Insert a dummy first entry to be able to optimize the whole
22759 predefined macro block using DW_MACRO_GNU_transparent_include. */
22760 if (macinfo_table->is_empty () && lineno <= 1)
22762 e.code = 0;
22763 e.lineno = 0;
22764 e.info = NULL;
22765 vec_safe_push (macinfo_table, e);
22767 e.code = DW_MACINFO_undef;
22768 e.lineno = lineno;
22769 e.info = ggc_strdup (buffer);
22770 vec_safe_push (macinfo_table, e);
22774 /* Helpers to manipulate hash table of CUs. */
22776 struct macinfo_entry_hasher : nofree_ptr_hash <macinfo_entry>
22778 static inline hashval_t hash (const macinfo_entry *);
22779 static inline bool equal (const macinfo_entry *, const macinfo_entry *);
22782 inline hashval_t
22783 macinfo_entry_hasher::hash (const macinfo_entry *entry)
22785 return htab_hash_string (entry->info);
22788 inline bool
22789 macinfo_entry_hasher::equal (const macinfo_entry *entry1,
22790 const macinfo_entry *entry2)
22792 return !strcmp (entry1->info, entry2->info);
22795 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
22797 /* Output a single .debug_macinfo entry. */
22799 static void
22800 output_macinfo_op (macinfo_entry *ref)
22802 int file_num;
22803 size_t len;
22804 struct indirect_string_node *node;
22805 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22806 struct dwarf_file_data *fd;
22808 switch (ref->code)
22810 case DW_MACINFO_start_file:
22811 fd = lookup_filename (ref->info);
22812 file_num = maybe_emit_file (fd);
22813 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
22814 dw2_asm_output_data_uleb128 (ref->lineno,
22815 "Included from line number %lu",
22816 (unsigned long) ref->lineno);
22817 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
22818 break;
22819 case DW_MACINFO_end_file:
22820 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
22821 break;
22822 case DW_MACINFO_define:
22823 case DW_MACINFO_undef:
22824 len = strlen (ref->info) + 1;
22825 if (!dwarf_strict
22826 && len > DWARF_OFFSET_SIZE
22827 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
22828 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
22830 ref->code = ref->code == DW_MACINFO_define
22831 ? DW_MACRO_GNU_define_indirect
22832 : DW_MACRO_GNU_undef_indirect;
22833 output_macinfo_op (ref);
22834 return;
22836 dw2_asm_output_data (1, ref->code,
22837 ref->code == DW_MACINFO_define
22838 ? "Define macro" : "Undefine macro");
22839 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
22840 (unsigned long) ref->lineno);
22841 dw2_asm_output_nstring (ref->info, -1, "The macro");
22842 break;
22843 case DW_MACRO_GNU_define_indirect:
22844 case DW_MACRO_GNU_undef_indirect:
22845 node = find_AT_string (ref->info);
22846 gcc_assert (node
22847 && ((node->form == DW_FORM_strp)
22848 || (node->form == DW_FORM_GNU_str_index)));
22849 dw2_asm_output_data (1, ref->code,
22850 ref->code == DW_MACRO_GNU_define_indirect
22851 ? "Define macro indirect"
22852 : "Undefine macro indirect");
22853 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
22854 (unsigned long) ref->lineno);
22855 if (node->form == DW_FORM_strp)
22856 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
22857 debug_str_section, "The macro: \"%s\"",
22858 ref->info);
22859 else
22860 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
22861 ref->info);
22862 break;
22863 case DW_MACRO_GNU_transparent_include:
22864 dw2_asm_output_data (1, ref->code, "Transparent include");
22865 ASM_GENERATE_INTERNAL_LABEL (label,
22866 DEBUG_MACRO_SECTION_LABEL, ref->lineno);
22867 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
22868 break;
22869 default:
22870 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
22871 ASM_COMMENT_START, (unsigned long) ref->code);
22872 break;
22876 /* Attempt to make a sequence of define/undef macinfo ops shareable with
22877 other compilation unit .debug_macinfo sections. IDX is the first
22878 index of a define/undef, return the number of ops that should be
22879 emitted in a comdat .debug_macinfo section and emit
22880 a DW_MACRO_GNU_transparent_include entry referencing it.
22881 If the define/undef entry should be emitted normally, return 0. */
22883 static unsigned
22884 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
22885 macinfo_hash_type **macinfo_htab)
22887 macinfo_entry *first, *second, *cur, *inc;
22888 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
22889 unsigned char checksum[16];
22890 struct md5_ctx ctx;
22891 char *grp_name, *tail;
22892 const char *base;
22893 unsigned int i, count, encoded_filename_len, linebuf_len;
22894 macinfo_entry **slot;
22896 first = &(*macinfo_table)[idx];
22897 second = &(*macinfo_table)[idx + 1];
22899 /* Optimize only if there are at least two consecutive define/undef ops,
22900 and either all of them are before first DW_MACINFO_start_file
22901 with lineno {0,1} (i.e. predefined macro block), or all of them are
22902 in some included header file. */
22903 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
22904 return 0;
22905 if (vec_safe_is_empty (files))
22907 if (first->lineno > 1 || second->lineno > 1)
22908 return 0;
22910 else if (first->lineno == 0)
22911 return 0;
22913 /* Find the last define/undef entry that can be grouped together
22914 with first and at the same time compute md5 checksum of their
22915 codes, linenumbers and strings. */
22916 md5_init_ctx (&ctx);
22917 for (i = idx; macinfo_table->iterate (i, &cur); i++)
22918 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
22919 break;
22920 else if (vec_safe_is_empty (files) && cur->lineno > 1)
22921 break;
22922 else
22924 unsigned char code = cur->code;
22925 md5_process_bytes (&code, 1, &ctx);
22926 checksum_uleb128 (cur->lineno, &ctx);
22927 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
22929 md5_finish_ctx (&ctx, checksum);
22930 count = i - idx;
22932 /* From the containing include filename (if any) pick up just
22933 usable characters from its basename. */
22934 if (vec_safe_is_empty (files))
22935 base = "";
22936 else
22937 base = lbasename (files->last ().info);
22938 for (encoded_filename_len = 0, i = 0; base[i]; i++)
22939 if (ISIDNUM (base[i]) || base[i] == '.')
22940 encoded_filename_len++;
22941 /* Count . at the end. */
22942 if (encoded_filename_len)
22943 encoded_filename_len++;
22945 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
22946 linebuf_len = strlen (linebuf);
22948 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
22949 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
22950 + 16 * 2 + 1);
22951 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
22952 tail = grp_name + 4;
22953 if (encoded_filename_len)
22955 for (i = 0; base[i]; i++)
22956 if (ISIDNUM (base[i]) || base[i] == '.')
22957 *tail++ = base[i];
22958 *tail++ = '.';
22960 memcpy (tail, linebuf, linebuf_len);
22961 tail += linebuf_len;
22962 *tail++ = '.';
22963 for (i = 0; i < 16; i++)
22964 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
22966 /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
22967 in the empty vector entry before the first define/undef. */
22968 inc = &(*macinfo_table)[idx - 1];
22969 inc->code = DW_MACRO_GNU_transparent_include;
22970 inc->lineno = 0;
22971 inc->info = ggc_strdup (grp_name);
22972 if (!*macinfo_htab)
22973 *macinfo_htab = new macinfo_hash_type (10);
22974 /* Avoid emitting duplicates. */
22975 slot = (*macinfo_htab)->find_slot (inc, INSERT);
22976 if (*slot != NULL)
22978 inc->code = 0;
22979 inc->info = NULL;
22980 /* If such an entry has been used before, just emit
22981 a DW_MACRO_GNU_transparent_include op. */
22982 inc = *slot;
22983 output_macinfo_op (inc);
22984 /* And clear all macinfo_entry in the range to avoid emitting them
22985 in the second pass. */
22986 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
22988 cur->code = 0;
22989 cur->info = NULL;
22992 else
22994 *slot = inc;
22995 inc->lineno = (*macinfo_htab)->elements ();
22996 output_macinfo_op (inc);
22998 return count;
23001 /* Save any strings needed by the macinfo table in the debug str
23002 table. All strings must be collected into the table by the time
23003 index_string is called. */
23005 static void
23006 save_macinfo_strings (void)
23008 unsigned len;
23009 unsigned i;
23010 macinfo_entry *ref;
23012 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
23014 switch (ref->code)
23016 /* Match the logic in output_macinfo_op to decide on
23017 indirect strings. */
23018 case DW_MACINFO_define:
23019 case DW_MACINFO_undef:
23020 len = strlen (ref->info) + 1;
23021 if (!dwarf_strict
23022 && len > DWARF_OFFSET_SIZE
23023 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
23024 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
23025 set_indirect_string (find_AT_string (ref->info));
23026 break;
23027 case DW_MACRO_GNU_define_indirect:
23028 case DW_MACRO_GNU_undef_indirect:
23029 set_indirect_string (find_AT_string (ref->info));
23030 break;
23031 default:
23032 break;
23037 /* Output macinfo section(s). */
23039 static void
23040 output_macinfo (void)
23042 unsigned i;
23043 unsigned long length = vec_safe_length (macinfo_table);
23044 macinfo_entry *ref;
23045 vec<macinfo_entry, va_gc> *files = NULL;
23046 macinfo_hash_type *macinfo_htab = NULL;
23048 if (! length)
23049 return;
23051 /* output_macinfo* uses these interchangeably. */
23052 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
23053 && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
23054 && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
23055 && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
23057 /* For .debug_macro emit the section header. */
23058 if (!dwarf_strict)
23060 dw2_asm_output_data (2, 4, "DWARF macro version number");
23061 if (DWARF_OFFSET_SIZE == 8)
23062 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
23063 else
23064 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
23065 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
23066 (!dwarf_split_debug_info ? debug_line_section_label
23067 : debug_skeleton_line_section_label),
23068 debug_line_section, NULL);
23071 /* In the first loop, it emits the primary .debug_macinfo section
23072 and after each emitted op the macinfo_entry is cleared.
23073 If a longer range of define/undef ops can be optimized using
23074 DW_MACRO_GNU_transparent_include, the
23075 DW_MACRO_GNU_transparent_include op is emitted and kept in
23076 the vector before the first define/undef in the range and the
23077 whole range of define/undef ops is not emitted and kept. */
23078 for (i = 0; macinfo_table->iterate (i, &ref); i++)
23080 switch (ref->code)
23082 case DW_MACINFO_start_file:
23083 vec_safe_push (files, *ref);
23084 break;
23085 case DW_MACINFO_end_file:
23086 if (!vec_safe_is_empty (files))
23087 files->pop ();
23088 break;
23089 case DW_MACINFO_define:
23090 case DW_MACINFO_undef:
23091 if (!dwarf_strict
23092 && HAVE_COMDAT_GROUP
23093 && vec_safe_length (files) != 1
23094 && i > 0
23095 && i + 1 < length
23096 && (*macinfo_table)[i - 1].code == 0)
23098 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
23099 if (count)
23101 i += count - 1;
23102 continue;
23105 break;
23106 case 0:
23107 /* A dummy entry may be inserted at the beginning to be able
23108 to optimize the whole block of predefined macros. */
23109 if (i == 0)
23110 continue;
23111 default:
23112 break;
23114 output_macinfo_op (ref);
23115 ref->info = NULL;
23116 ref->code = 0;
23119 if (!macinfo_htab)
23120 return;
23122 delete macinfo_htab;
23123 macinfo_htab = NULL;
23125 /* If any DW_MACRO_GNU_transparent_include were used, on those
23126 DW_MACRO_GNU_transparent_include entries terminate the
23127 current chain and switch to a new comdat .debug_macinfo
23128 section and emit the define/undef entries within it. */
23129 for (i = 0; macinfo_table->iterate (i, &ref); i++)
23130 switch (ref->code)
23132 case 0:
23133 continue;
23134 case DW_MACRO_GNU_transparent_include:
23136 char label[MAX_ARTIFICIAL_LABEL_BYTES];
23137 tree comdat_key = get_identifier (ref->info);
23138 /* Terminate the previous .debug_macinfo section. */
23139 dw2_asm_output_data (1, 0, "End compilation unit");
23140 targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
23141 SECTION_DEBUG
23142 | SECTION_LINKONCE,
23143 comdat_key);
23144 ASM_GENERATE_INTERNAL_LABEL (label,
23145 DEBUG_MACRO_SECTION_LABEL,
23146 ref->lineno);
23147 ASM_OUTPUT_LABEL (asm_out_file, label);
23148 ref->code = 0;
23149 ref->info = NULL;
23150 dw2_asm_output_data (2, 4, "DWARF macro version number");
23151 if (DWARF_OFFSET_SIZE == 8)
23152 dw2_asm_output_data (1, 1, "Flags: 64-bit");
23153 else
23154 dw2_asm_output_data (1, 0, "Flags: 32-bit");
23156 break;
23157 case DW_MACINFO_define:
23158 case DW_MACINFO_undef:
23159 output_macinfo_op (ref);
23160 ref->code = 0;
23161 ref->info = NULL;
23162 break;
23163 default:
23164 gcc_unreachable ();
23168 /* Set up for Dwarf output at the start of compilation. */
23170 static void
23171 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
23173 /* This option is currently broken, see (PR53118 and PR46102). */
23174 if (flag_eliminate_dwarf2_dups
23175 && strstr (lang_hooks.name, "C++"))
23177 warning (0, "-feliminate-dwarf2-dups is broken for C++, ignoring");
23178 flag_eliminate_dwarf2_dups = 0;
23181 /* Allocate the file_table. */
23182 file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
23184 /* Allocate the decl_die_table. */
23185 decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
23187 /* Allocate the decl_loc_table. */
23188 decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
23190 /* Allocate the cached_dw_loc_list_table. */
23191 cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
23193 /* Allocate the initial hunk of the decl_scope_table. */
23194 vec_alloc (decl_scope_table, 256);
23196 /* Allocate the initial hunk of the abbrev_die_table. */
23197 abbrev_die_table = ggc_cleared_vec_alloc<dw_die_ref>
23198 (ABBREV_DIE_TABLE_INCREMENT);
23199 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
23200 /* Zero-th entry is allocated, but unused. */
23201 abbrev_die_table_in_use = 1;
23203 /* Allocate the pubtypes and pubnames vectors. */
23204 vec_alloc (pubname_table, 32);
23205 vec_alloc (pubtype_table, 32);
23207 vec_alloc (incomplete_types, 64);
23209 vec_alloc (used_rtx_array, 32);
23211 if (!dwarf_split_debug_info)
23213 debug_info_section = get_section (DEBUG_INFO_SECTION,
23214 SECTION_DEBUG, NULL);
23215 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
23216 SECTION_DEBUG, NULL);
23217 debug_loc_section = get_section (DEBUG_LOC_SECTION,
23218 SECTION_DEBUG, NULL);
23220 else
23222 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
23223 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
23224 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
23225 SECTION_DEBUG | SECTION_EXCLUDE,
23226 NULL);
23227 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
23228 SECTION_DEBUG, NULL);
23229 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
23230 SECTION_DEBUG, NULL);
23231 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
23232 SECTION_DEBUG, NULL);
23233 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
23234 DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
23236 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
23237 the main .o, but the skeleton_line goes into the split off dwo. */
23238 debug_skeleton_line_section
23239 = get_section (DEBUG_DWO_LINE_SECTION,
23240 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
23241 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
23242 DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
23243 debug_str_offsets_section = get_section (DEBUG_STR_OFFSETS_SECTION,
23244 SECTION_DEBUG | SECTION_EXCLUDE,
23245 NULL);
23246 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
23247 DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
23248 debug_loc_section = get_section (DEBUG_DWO_LOC_SECTION,
23249 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
23250 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
23251 DEBUG_STR_DWO_SECTION_FLAGS, NULL);
23253 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
23254 SECTION_DEBUG, NULL);
23255 debug_macinfo_section = get_section (dwarf_strict
23256 ? DEBUG_MACINFO_SECTION
23257 : DEBUG_MACRO_SECTION,
23258 DEBUG_MACRO_SECTION_FLAGS, NULL);
23259 debug_line_section = get_section (DEBUG_LINE_SECTION,
23260 SECTION_DEBUG, NULL);
23261 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
23262 SECTION_DEBUG, NULL);
23263 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
23264 SECTION_DEBUG, NULL);
23265 debug_str_section = get_section (DEBUG_STR_SECTION,
23266 DEBUG_STR_SECTION_FLAGS, NULL);
23267 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
23268 SECTION_DEBUG, NULL);
23269 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
23270 SECTION_DEBUG, NULL);
23272 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
23273 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
23274 DEBUG_ABBREV_SECTION_LABEL, 0);
23275 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
23276 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
23277 COLD_TEXT_SECTION_LABEL, 0);
23278 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
23280 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
23281 DEBUG_INFO_SECTION_LABEL, 0);
23282 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
23283 DEBUG_LINE_SECTION_LABEL, 0);
23284 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
23285 DEBUG_RANGES_SECTION_LABEL, 0);
23286 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
23287 DEBUG_ADDR_SECTION_LABEL, 0);
23288 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
23289 dwarf_strict
23290 ? DEBUG_MACINFO_SECTION_LABEL
23291 : DEBUG_MACRO_SECTION_LABEL, 0);
23292 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
23294 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
23295 vec_alloc (macinfo_table, 64);
23297 switch_to_section (text_section);
23298 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
23300 /* Make sure the line number table for .text always exists. */
23301 text_section_line_info = new_line_info_table ();
23302 text_section_line_info->end_label = text_end_label;
23304 /* If front-ends already registered a main translation unit but we were not
23305 ready to perform the association, do this now. */
23306 if (main_translation_unit != NULL_TREE)
23307 equate_decl_number_to_die (main_translation_unit, comp_unit_die ());
23310 /* Called before compile () starts outputtting functions, variables
23311 and toplevel asms into assembly. */
23313 static void
23314 dwarf2out_assembly_start (void)
23316 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
23317 && dwarf2out_do_cfi_asm ()
23318 && (!(flag_unwind_tables || flag_exceptions)
23319 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
23320 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
23323 /* A helper function for dwarf2out_finish called through
23324 htab_traverse. Assign a string its index. All strings must be
23325 collected into the table by the time index_string is called,
23326 because the indexing code relies on htab_traverse to traverse nodes
23327 in the same order for each run. */
23330 index_string (indirect_string_node **h, unsigned int *index)
23332 indirect_string_node *node = *h;
23334 find_string_form (node);
23335 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
23337 gcc_assert (node->index == NO_INDEX_ASSIGNED);
23338 node->index = *index;
23339 *index += 1;
23341 return 1;
23344 /* A helper function for output_indirect_strings called through
23345 htab_traverse. Output the offset to a string and update the
23346 current offset. */
23349 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
23351 indirect_string_node *node = *h;
23353 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
23355 /* Assert that this node has been assigned an index. */
23356 gcc_assert (node->index != NO_INDEX_ASSIGNED
23357 && node->index != NOT_INDEXED);
23358 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
23359 "indexed string 0x%x: %s", node->index, node->str);
23360 *offset += strlen (node->str) + 1;
23362 return 1;
23365 /* A helper function for dwarf2out_finish called through
23366 htab_traverse. Output the indexed string. */
23369 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
23371 struct indirect_string_node *node = *h;
23373 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
23375 /* Assert that the strings are output in the same order as their
23376 indexes were assigned. */
23377 gcc_assert (*cur_idx == node->index);
23378 assemble_string (node->str, strlen (node->str) + 1);
23379 *cur_idx += 1;
23381 return 1;
23384 /* A helper function for dwarf2out_finish called through
23385 htab_traverse. Emit one queued .debug_str string. */
23388 output_indirect_string (indirect_string_node **h, void *)
23390 struct indirect_string_node *node = *h;
23392 node->form = find_string_form (node);
23393 if (node->form == DW_FORM_strp && node->refcount > 0)
23395 ASM_OUTPUT_LABEL (asm_out_file, node->label);
23396 assemble_string (node->str, strlen (node->str) + 1);
23399 return 1;
23402 /* Output the indexed string table. */
23404 static void
23405 output_indirect_strings (void)
23407 switch_to_section (debug_str_section);
23408 if (!dwarf_split_debug_info)
23409 debug_str_hash->traverse<void *, output_indirect_string> (NULL);
23410 else
23412 unsigned int offset = 0;
23413 unsigned int cur_idx = 0;
23415 skeleton_debug_str_hash->traverse<void *, output_indirect_string> (NULL);
23417 switch_to_section (debug_str_offsets_section);
23418 debug_str_hash->traverse_noresize
23419 <unsigned int *, output_index_string_offset> (&offset);
23420 switch_to_section (debug_str_dwo_section);
23421 debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
23422 (&cur_idx);
23426 /* Callback for htab_traverse to assign an index to an entry in the
23427 table, and to write that entry to the .debug_addr section. */
23430 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
23432 addr_table_entry *entry = *slot;
23434 if (entry->refcount == 0)
23436 gcc_assert (entry->index == NO_INDEX_ASSIGNED
23437 || entry->index == NOT_INDEXED);
23438 return 1;
23441 gcc_assert (entry->index == *cur_index);
23442 (*cur_index)++;
23444 switch (entry->kind)
23446 case ate_kind_rtx:
23447 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
23448 "0x%x", entry->index);
23449 break;
23450 case ate_kind_rtx_dtprel:
23451 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
23452 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
23453 DWARF2_ADDR_SIZE,
23454 entry->addr.rtl);
23455 fputc ('\n', asm_out_file);
23456 break;
23457 case ate_kind_label:
23458 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
23459 "0x%x", entry->index);
23460 break;
23461 default:
23462 gcc_unreachable ();
23464 return 1;
23467 /* Produce the .debug_addr section. */
23469 static void
23470 output_addr_table (void)
23472 unsigned int index = 0;
23473 if (addr_index_table == NULL || addr_index_table->size () == 0)
23474 return;
23476 switch_to_section (debug_addr_section);
23477 addr_index_table
23478 ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
23481 #if ENABLE_ASSERT_CHECKING
23482 /* Verify that all marks are clear. */
23484 static void
23485 verify_marks_clear (dw_die_ref die)
23487 dw_die_ref c;
23489 gcc_assert (! die->die_mark);
23490 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
23492 #endif /* ENABLE_ASSERT_CHECKING */
23494 /* Clear the marks for a die and its children.
23495 Be cool if the mark isn't set. */
23497 static void
23498 prune_unmark_dies (dw_die_ref die)
23500 dw_die_ref c;
23502 if (die->die_mark)
23503 die->die_mark = 0;
23504 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
23507 /* Given DIE that we're marking as used, find any other dies
23508 it references as attributes and mark them as used. */
23510 static void
23511 prune_unused_types_walk_attribs (dw_die_ref die)
23513 dw_attr_ref a;
23514 unsigned ix;
23516 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23518 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
23520 /* A reference to another DIE.
23521 Make sure that it will get emitted.
23522 If it was broken out into a comdat group, don't follow it. */
23523 if (! AT_ref (a)->comdat_type_p
23524 || a->dw_attr == DW_AT_specification)
23525 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
23527 /* Set the string's refcount to 0 so that prune_unused_types_mark
23528 accounts properly for it. */
23529 if (AT_class (a) == dw_val_class_str)
23530 a->dw_attr_val.v.val_str->refcount = 0;
23534 /* Mark the generic parameters and arguments children DIEs of DIE. */
23536 static void
23537 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
23539 dw_die_ref c;
23541 if (die == NULL || die->die_child == NULL)
23542 return;
23543 c = die->die_child;
23546 if (is_template_parameter (c))
23547 prune_unused_types_mark (c, 1);
23548 c = c->die_sib;
23549 } while (c && c != die->die_child);
23552 /* Mark DIE as being used. If DOKIDS is true, then walk down
23553 to DIE's children. */
23555 static void
23556 prune_unused_types_mark (dw_die_ref die, int dokids)
23558 dw_die_ref c;
23560 if (die->die_mark == 0)
23562 /* We haven't done this node yet. Mark it as used. */
23563 die->die_mark = 1;
23564 /* If this is the DIE of a generic type instantiation,
23565 mark the children DIEs that describe its generic parms and
23566 args. */
23567 prune_unused_types_mark_generic_parms_dies (die);
23569 /* We also have to mark its parents as used.
23570 (But we don't want to mark our parent's kids due to this,
23571 unless it is a class.) */
23572 if (die->die_parent)
23573 prune_unused_types_mark (die->die_parent,
23574 class_scope_p (die->die_parent));
23576 /* Mark any referenced nodes. */
23577 prune_unused_types_walk_attribs (die);
23579 /* If this node is a specification,
23580 also mark the definition, if it exists. */
23581 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
23582 prune_unused_types_mark (die->die_definition, 1);
23585 if (dokids && die->die_mark != 2)
23587 /* We need to walk the children, but haven't done so yet.
23588 Remember that we've walked the kids. */
23589 die->die_mark = 2;
23591 /* If this is an array type, we need to make sure our
23592 kids get marked, even if they're types. If we're
23593 breaking out types into comdat sections, do this
23594 for all type definitions. */
23595 if (die->die_tag == DW_TAG_array_type
23596 || (use_debug_types
23597 && is_type_die (die) && ! is_declaration_die (die)))
23598 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
23599 else
23600 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
23604 /* For local classes, look if any static member functions were emitted
23605 and if so, mark them. */
23607 static void
23608 prune_unused_types_walk_local_classes (dw_die_ref die)
23610 dw_die_ref c;
23612 if (die->die_mark == 2)
23613 return;
23615 switch (die->die_tag)
23617 case DW_TAG_structure_type:
23618 case DW_TAG_union_type:
23619 case DW_TAG_class_type:
23620 break;
23622 case DW_TAG_subprogram:
23623 if (!get_AT_flag (die, DW_AT_declaration)
23624 || die->die_definition != NULL)
23625 prune_unused_types_mark (die, 1);
23626 return;
23628 default:
23629 return;
23632 /* Mark children. */
23633 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
23636 /* Walk the tree DIE and mark types that we actually use. */
23638 static void
23639 prune_unused_types_walk (dw_die_ref die)
23641 dw_die_ref c;
23643 /* Don't do anything if this node is already marked and
23644 children have been marked as well. */
23645 if (die->die_mark == 2)
23646 return;
23648 switch (die->die_tag)
23650 case DW_TAG_structure_type:
23651 case DW_TAG_union_type:
23652 case DW_TAG_class_type:
23653 if (die->die_perennial_p)
23654 break;
23656 for (c = die->die_parent; c; c = c->die_parent)
23657 if (c->die_tag == DW_TAG_subprogram)
23658 break;
23660 /* Finding used static member functions inside of classes
23661 is needed just for local classes, because for other classes
23662 static member function DIEs with DW_AT_specification
23663 are emitted outside of the DW_TAG_*_type. If we ever change
23664 it, we'd need to call this even for non-local classes. */
23665 if (c)
23666 prune_unused_types_walk_local_classes (die);
23668 /* It's a type node --- don't mark it. */
23669 return;
23671 case DW_TAG_const_type:
23672 case DW_TAG_packed_type:
23673 case DW_TAG_pointer_type:
23674 case DW_TAG_reference_type:
23675 case DW_TAG_rvalue_reference_type:
23676 case DW_TAG_volatile_type:
23677 case DW_TAG_typedef:
23678 case DW_TAG_array_type:
23679 case DW_TAG_interface_type:
23680 case DW_TAG_friend:
23681 case DW_TAG_variant_part:
23682 case DW_TAG_enumeration_type:
23683 case DW_TAG_subroutine_type:
23684 case DW_TAG_string_type:
23685 case DW_TAG_set_type:
23686 case DW_TAG_subrange_type:
23687 case DW_TAG_ptr_to_member_type:
23688 case DW_TAG_file_type:
23689 if (die->die_perennial_p)
23690 break;
23692 /* It's a type node --- don't mark it. */
23693 return;
23695 default:
23696 /* Mark everything else. */
23697 break;
23700 if (die->die_mark == 0)
23702 die->die_mark = 1;
23704 /* Now, mark any dies referenced from here. */
23705 prune_unused_types_walk_attribs (die);
23708 die->die_mark = 2;
23710 /* Mark children. */
23711 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
23714 /* Increment the string counts on strings referred to from DIE's
23715 attributes. */
23717 static void
23718 prune_unused_types_update_strings (dw_die_ref die)
23720 dw_attr_ref a;
23721 unsigned ix;
23723 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23724 if (AT_class (a) == dw_val_class_str)
23726 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
23727 s->refcount++;
23728 /* Avoid unnecessarily putting strings that are used less than
23729 twice in the hash table. */
23730 if (s->refcount
23731 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
23733 indirect_string_node **slot
23734 = debug_str_hash->find_slot_with_hash (s->str,
23735 htab_hash_string (s->str),
23736 INSERT);
23737 gcc_assert (*slot == NULL);
23738 *slot = s;
23743 /* Remove from the tree DIE any dies that aren't marked. */
23745 static void
23746 prune_unused_types_prune (dw_die_ref die)
23748 dw_die_ref c;
23750 gcc_assert (die->die_mark);
23751 prune_unused_types_update_strings (die);
23753 if (! die->die_child)
23754 return;
23756 c = die->die_child;
23757 do {
23758 dw_die_ref prev = c;
23759 for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
23760 if (c == die->die_child)
23762 /* No marked children between 'prev' and the end of the list. */
23763 if (prev == c)
23764 /* No marked children at all. */
23765 die->die_child = NULL;
23766 else
23768 prev->die_sib = c->die_sib;
23769 die->die_child = prev;
23771 return;
23774 if (c != prev->die_sib)
23775 prev->die_sib = c;
23776 prune_unused_types_prune (c);
23777 } while (c != die->die_child);
23780 /* Remove dies representing declarations that we never use. */
23782 static void
23783 prune_unused_types (void)
23785 unsigned int i;
23786 limbo_die_node *node;
23787 comdat_type_node *ctnode;
23788 pubname_ref pub;
23789 dw_die_ref base_type;
23791 #if ENABLE_ASSERT_CHECKING
23792 /* All the marks should already be clear. */
23793 verify_marks_clear (comp_unit_die ());
23794 for (node = limbo_die_list; node; node = node->next)
23795 verify_marks_clear (node->die);
23796 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23797 verify_marks_clear (ctnode->root_die);
23798 #endif /* ENABLE_ASSERT_CHECKING */
23800 /* Mark types that are used in global variables. */
23801 premark_types_used_by_global_vars ();
23803 /* Set the mark on nodes that are actually used. */
23804 prune_unused_types_walk (comp_unit_die ());
23805 for (node = limbo_die_list; node; node = node->next)
23806 prune_unused_types_walk (node->die);
23807 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23809 prune_unused_types_walk (ctnode->root_die);
23810 prune_unused_types_mark (ctnode->type_die, 1);
23813 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
23814 are unusual in that they are pubnames that are the children of pubtypes.
23815 They should only be marked via their parent DW_TAG_enumeration_type die,
23816 not as roots in themselves. */
23817 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
23818 if (pub->die->die_tag != DW_TAG_enumerator)
23819 prune_unused_types_mark (pub->die, 1);
23820 for (i = 0; base_types.iterate (i, &base_type); i++)
23821 prune_unused_types_mark (base_type, 1);
23823 if (debug_str_hash)
23824 debug_str_hash->empty ();
23825 if (skeleton_debug_str_hash)
23826 skeleton_debug_str_hash->empty ();
23827 prune_unused_types_prune (comp_unit_die ());
23828 for (limbo_die_node **pnode = &limbo_die_list; *pnode; )
23830 node = *pnode;
23831 if (!node->die->die_mark)
23832 *pnode = node->next;
23833 else
23835 prune_unused_types_prune (node->die);
23836 pnode = &node->next;
23839 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23840 prune_unused_types_prune (ctnode->root_die);
23842 /* Leave the marks clear. */
23843 prune_unmark_dies (comp_unit_die ());
23844 for (node = limbo_die_list; node; node = node->next)
23845 prune_unmark_dies (node->die);
23846 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23847 prune_unmark_dies (ctnode->root_die);
23850 /* Set the parameter to true if there are any relative pathnames in
23851 the file table. */
23853 file_table_relative_p (dwarf_file_data **slot, bool *p)
23855 struct dwarf_file_data *d = *slot;
23856 if (!IS_ABSOLUTE_PATH (d->filename))
23858 *p = true;
23859 return 0;
23861 return 1;
23864 /* Helpers to manipulate hash table of comdat type units. */
23866 struct comdat_type_hasher : nofree_ptr_hash <comdat_type_node>
23868 static inline hashval_t hash (const comdat_type_node *);
23869 static inline bool equal (const comdat_type_node *, const comdat_type_node *);
23872 inline hashval_t
23873 comdat_type_hasher::hash (const comdat_type_node *type_node)
23875 hashval_t h;
23876 memcpy (&h, type_node->signature, sizeof (h));
23877 return h;
23880 inline bool
23881 comdat_type_hasher::equal (const comdat_type_node *type_node_1,
23882 const comdat_type_node *type_node_2)
23884 return (! memcmp (type_node_1->signature, type_node_2->signature,
23885 DWARF_TYPE_SIGNATURE_SIZE));
23888 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
23889 to the location it would have been added, should we know its
23890 DECL_ASSEMBLER_NAME when we added other attributes. This will
23891 probably improve compactness of debug info, removing equivalent
23892 abbrevs, and hide any differences caused by deferring the
23893 computation of the assembler name, triggered by e.g. PCH. */
23895 static inline void
23896 move_linkage_attr (dw_die_ref die)
23898 unsigned ix = vec_safe_length (die->die_attr);
23899 dw_attr_node linkage = (*die->die_attr)[ix - 1];
23901 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
23902 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
23904 while (--ix > 0)
23906 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
23908 if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
23909 break;
23912 if (ix != vec_safe_length (die->die_attr) - 1)
23914 die->die_attr->pop ();
23915 die->die_attr->quick_insert (ix, linkage);
23919 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
23920 referenced from typed stack ops and count how often they are used. */
23922 static void
23923 mark_base_types (dw_loc_descr_ref loc)
23925 dw_die_ref base_type = NULL;
23927 for (; loc; loc = loc->dw_loc_next)
23929 switch (loc->dw_loc_opc)
23931 case DW_OP_GNU_regval_type:
23932 case DW_OP_GNU_deref_type:
23933 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
23934 break;
23935 case DW_OP_GNU_convert:
23936 case DW_OP_GNU_reinterpret:
23937 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
23938 continue;
23939 /* FALLTHRU */
23940 case DW_OP_GNU_const_type:
23941 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
23942 break;
23943 case DW_OP_GNU_entry_value:
23944 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
23945 continue;
23946 default:
23947 continue;
23949 gcc_assert (base_type->die_parent == comp_unit_die ());
23950 if (base_type->die_mark)
23951 base_type->die_mark++;
23952 else
23954 base_types.safe_push (base_type);
23955 base_type->die_mark = 1;
23960 /* Comparison function for sorting marked base types. */
23962 static int
23963 base_type_cmp (const void *x, const void *y)
23965 dw_die_ref dx = *(const dw_die_ref *) x;
23966 dw_die_ref dy = *(const dw_die_ref *) y;
23967 unsigned int byte_size1, byte_size2;
23968 unsigned int encoding1, encoding2;
23969 if (dx->die_mark > dy->die_mark)
23970 return -1;
23971 if (dx->die_mark < dy->die_mark)
23972 return 1;
23973 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
23974 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
23975 if (byte_size1 < byte_size2)
23976 return 1;
23977 if (byte_size1 > byte_size2)
23978 return -1;
23979 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
23980 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
23981 if (encoding1 < encoding2)
23982 return 1;
23983 if (encoding1 > encoding2)
23984 return -1;
23985 return 0;
23988 /* Move base types marked by mark_base_types as early as possible
23989 in the CU, sorted by decreasing usage count both to make the
23990 uleb128 references as small as possible and to make sure they
23991 will have die_offset already computed by calc_die_sizes when
23992 sizes of typed stack loc ops is computed. */
23994 static void
23995 move_marked_base_types (void)
23997 unsigned int i;
23998 dw_die_ref base_type, die, c;
24000 if (base_types.is_empty ())
24001 return;
24003 /* Sort by decreasing usage count, they will be added again in that
24004 order later on. */
24005 base_types.qsort (base_type_cmp);
24006 die = comp_unit_die ();
24007 c = die->die_child;
24010 dw_die_ref prev = c;
24011 c = c->die_sib;
24012 while (c->die_mark)
24014 remove_child_with_prev (c, prev);
24015 /* As base types got marked, there must be at least
24016 one node other than DW_TAG_base_type. */
24017 gcc_assert (c != c->die_sib);
24018 c = c->die_sib;
24021 while (c != die->die_child);
24022 gcc_assert (die->die_child);
24023 c = die->die_child;
24024 for (i = 0; base_types.iterate (i, &base_type); i++)
24026 base_type->die_mark = 0;
24027 base_type->die_sib = c->die_sib;
24028 c->die_sib = base_type;
24029 c = base_type;
24033 /* Helper function for resolve_addr, attempt to resolve
24034 one CONST_STRING, return true if successful. Similarly verify that
24035 SYMBOL_REFs refer to variables emitted in the current CU. */
24037 static bool
24038 resolve_one_addr (rtx *addr)
24040 rtx rtl = *addr;
24042 if (GET_CODE (rtl) == CONST_STRING)
24044 size_t len = strlen (XSTR (rtl, 0)) + 1;
24045 tree t = build_string (len, XSTR (rtl, 0));
24046 tree tlen = size_int (len - 1);
24047 TREE_TYPE (t)
24048 = build_array_type (char_type_node, build_index_type (tlen));
24049 rtl = lookup_constant_def (t);
24050 if (!rtl || !MEM_P (rtl))
24051 return false;
24052 rtl = XEXP (rtl, 0);
24053 if (GET_CODE (rtl) == SYMBOL_REF
24054 && SYMBOL_REF_DECL (rtl)
24055 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
24056 return false;
24057 vec_safe_push (used_rtx_array, rtl);
24058 *addr = rtl;
24059 return true;
24062 if (GET_CODE (rtl) == SYMBOL_REF
24063 && SYMBOL_REF_DECL (rtl))
24065 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
24067 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
24068 return false;
24070 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
24071 return false;
24074 if (GET_CODE (rtl) == CONST)
24076 subrtx_ptr_iterator::array_type array;
24077 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
24078 if (!resolve_one_addr (*iter))
24079 return false;
24082 return true;
24085 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
24086 if possible, and create DW_TAG_dwarf_procedure that can be referenced
24087 from DW_OP_GNU_implicit_pointer if the string hasn't been seen yet. */
24089 static rtx
24090 string_cst_pool_decl (tree t)
24092 rtx rtl = output_constant_def (t, 1);
24093 unsigned char *array;
24094 dw_loc_descr_ref l;
24095 tree decl;
24096 size_t len;
24097 dw_die_ref ref;
24099 if (!rtl || !MEM_P (rtl))
24100 return NULL_RTX;
24101 rtl = XEXP (rtl, 0);
24102 if (GET_CODE (rtl) != SYMBOL_REF
24103 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
24104 return NULL_RTX;
24106 decl = SYMBOL_REF_DECL (rtl);
24107 if (!lookup_decl_die (decl))
24109 len = TREE_STRING_LENGTH (t);
24110 vec_safe_push (used_rtx_array, rtl);
24111 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
24112 array = ggc_vec_alloc<unsigned char> (len);
24113 memcpy (array, TREE_STRING_POINTER (t), len);
24114 l = new_loc_descr (DW_OP_implicit_value, len, 0);
24115 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
24116 l->dw_loc_oprnd2.v.val_vec.length = len;
24117 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
24118 l->dw_loc_oprnd2.v.val_vec.array = array;
24119 add_AT_loc (ref, DW_AT_location, l);
24120 equate_decl_number_to_die (decl, ref);
24122 return rtl;
24125 /* Helper function of resolve_addr_in_expr. LOC is
24126 a DW_OP_addr followed by DW_OP_stack_value, either at the start
24127 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
24128 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
24129 with DW_OP_GNU_implicit_pointer if possible
24130 and return true, if unsuccessful, return false. */
24132 static bool
24133 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
24135 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
24136 HOST_WIDE_INT offset = 0;
24137 dw_die_ref ref = NULL;
24138 tree decl;
24140 if (GET_CODE (rtl) == CONST
24141 && GET_CODE (XEXP (rtl, 0)) == PLUS
24142 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
24144 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
24145 rtl = XEXP (XEXP (rtl, 0), 0);
24147 if (GET_CODE (rtl) == CONST_STRING)
24149 size_t len = strlen (XSTR (rtl, 0)) + 1;
24150 tree t = build_string (len, XSTR (rtl, 0));
24151 tree tlen = size_int (len - 1);
24153 TREE_TYPE (t)
24154 = build_array_type (char_type_node, build_index_type (tlen));
24155 rtl = string_cst_pool_decl (t);
24156 if (!rtl)
24157 return false;
24159 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
24161 decl = SYMBOL_REF_DECL (rtl);
24162 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
24164 ref = lookup_decl_die (decl);
24165 if (ref && (get_AT (ref, DW_AT_location)
24166 || get_AT (ref, DW_AT_const_value)))
24168 loc->dw_loc_opc = DW_OP_GNU_implicit_pointer;
24169 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
24170 loc->dw_loc_oprnd1.val_entry = NULL;
24171 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
24172 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
24173 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
24174 loc->dw_loc_oprnd2.v.val_int = offset;
24175 return true;
24179 return false;
24182 /* Helper function for resolve_addr, handle one location
24183 expression, return false if at least one CONST_STRING or SYMBOL_REF in
24184 the location list couldn't be resolved. */
24186 static bool
24187 resolve_addr_in_expr (dw_loc_descr_ref loc)
24189 dw_loc_descr_ref keep = NULL;
24190 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
24191 switch (loc->dw_loc_opc)
24193 case DW_OP_addr:
24194 if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
24196 if ((prev == NULL
24197 || prev->dw_loc_opc == DW_OP_piece
24198 || prev->dw_loc_opc == DW_OP_bit_piece)
24199 && loc->dw_loc_next
24200 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
24201 && !dwarf_strict
24202 && optimize_one_addr_into_implicit_ptr (loc))
24203 break;
24204 return false;
24206 break;
24207 case DW_OP_GNU_addr_index:
24208 case DW_OP_GNU_const_index:
24209 if (loc->dw_loc_opc == DW_OP_GNU_addr_index
24210 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
24212 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
24213 if (!resolve_one_addr (&rtl))
24214 return false;
24215 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
24216 loc->dw_loc_oprnd1.val_entry =
24217 add_addr_table_entry (rtl, ate_kind_rtx);
24219 break;
24220 case DW_OP_const4u:
24221 case DW_OP_const8u:
24222 if (loc->dtprel
24223 && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
24224 return false;
24225 break;
24226 case DW_OP_plus_uconst:
24227 if (size_of_loc_descr (loc)
24228 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
24230 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
24232 dw_loc_descr_ref repl
24233 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
24234 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
24235 add_loc_descr (&repl, loc->dw_loc_next);
24236 *loc = *repl;
24238 break;
24239 case DW_OP_implicit_value:
24240 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
24241 && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
24242 return false;
24243 break;
24244 case DW_OP_GNU_implicit_pointer:
24245 case DW_OP_GNU_parameter_ref:
24246 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
24248 dw_die_ref ref
24249 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
24250 if (ref == NULL)
24251 return false;
24252 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
24253 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
24254 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
24256 break;
24257 case DW_OP_GNU_const_type:
24258 case DW_OP_GNU_regval_type:
24259 case DW_OP_GNU_deref_type:
24260 case DW_OP_GNU_convert:
24261 case DW_OP_GNU_reinterpret:
24262 while (loc->dw_loc_next
24263 && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
24265 dw_die_ref base1, base2;
24266 unsigned enc1, enc2, size1, size2;
24267 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
24268 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
24269 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
24270 else if (loc->dw_loc_oprnd1.val_class
24271 == dw_val_class_unsigned_const)
24272 break;
24273 else
24274 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
24275 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
24276 == dw_val_class_unsigned_const)
24277 break;
24278 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
24279 gcc_assert (base1->die_tag == DW_TAG_base_type
24280 && base2->die_tag == DW_TAG_base_type);
24281 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
24282 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
24283 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
24284 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
24285 if (size1 == size2
24286 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
24287 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
24288 && loc != keep)
24289 || enc1 == enc2))
24291 /* Optimize away next DW_OP_GNU_convert after
24292 adjusting LOC's base type die reference. */
24293 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
24294 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
24295 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
24296 else
24297 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
24298 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
24299 continue;
24301 /* Don't change integer DW_OP_GNU_convert after e.g. floating
24302 point typed stack entry. */
24303 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
24304 keep = loc->dw_loc_next;
24305 break;
24307 break;
24308 default:
24309 break;
24311 return true;
24314 /* Helper function of resolve_addr. DIE had DW_AT_location of
24315 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
24316 and DW_OP_addr couldn't be resolved. resolve_addr has already
24317 removed the DW_AT_location attribute. This function attempts to
24318 add a new DW_AT_location attribute with DW_OP_GNU_implicit_pointer
24319 to it or DW_AT_const_value attribute, if possible. */
24321 static void
24322 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
24324 if (TREE_CODE (decl) != VAR_DECL
24325 || lookup_decl_die (decl) != die
24326 || DECL_EXTERNAL (decl)
24327 || !TREE_STATIC (decl)
24328 || DECL_INITIAL (decl) == NULL_TREE
24329 || DECL_P (DECL_INITIAL (decl))
24330 || get_AT (die, DW_AT_const_value))
24331 return;
24333 tree init = DECL_INITIAL (decl);
24334 HOST_WIDE_INT offset = 0;
24335 /* For variables that have been optimized away and thus
24336 don't have a memory location, see if we can emit
24337 DW_AT_const_value instead. */
24338 if (tree_add_const_value_attribute (die, init))
24339 return;
24340 if (dwarf_strict)
24341 return;
24342 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
24343 and ADDR_EXPR refers to a decl that has DW_AT_location or
24344 DW_AT_const_value (but isn't addressable, otherwise
24345 resolving the original DW_OP_addr wouldn't fail), see if
24346 we can add DW_OP_GNU_implicit_pointer. */
24347 STRIP_NOPS (init);
24348 if (TREE_CODE (init) == POINTER_PLUS_EXPR
24349 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
24351 offset = tree_to_shwi (TREE_OPERAND (init, 1));
24352 init = TREE_OPERAND (init, 0);
24353 STRIP_NOPS (init);
24355 if (TREE_CODE (init) != ADDR_EXPR)
24356 return;
24357 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
24358 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
24359 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
24360 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
24361 && TREE_OPERAND (init, 0) != decl))
24363 dw_die_ref ref;
24364 dw_loc_descr_ref l;
24366 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
24368 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
24369 if (!rtl)
24370 return;
24371 decl = SYMBOL_REF_DECL (rtl);
24373 else
24374 decl = TREE_OPERAND (init, 0);
24375 ref = lookup_decl_die (decl);
24376 if (ref == NULL
24377 || (!get_AT (ref, DW_AT_location)
24378 && !get_AT (ref, DW_AT_const_value)))
24379 return;
24380 l = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
24381 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
24382 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
24383 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
24384 add_AT_loc (die, DW_AT_location, l);
24388 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
24389 an address in .rodata section if the string literal is emitted there,
24390 or remove the containing location list or replace DW_AT_const_value
24391 with DW_AT_location and empty location expression, if it isn't found
24392 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
24393 to something that has been emitted in the current CU. */
24395 static void
24396 resolve_addr (dw_die_ref die)
24398 dw_die_ref c;
24399 dw_attr_ref a;
24400 dw_loc_list_ref *curr, *start, loc;
24401 unsigned ix;
24403 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
24404 switch (AT_class (a))
24406 case dw_val_class_loc_list:
24407 start = curr = AT_loc_list_ptr (a);
24408 loc = *curr;
24409 gcc_assert (loc);
24410 /* The same list can be referenced more than once. See if we have
24411 already recorded the result from a previous pass. */
24412 if (loc->replaced)
24413 *curr = loc->dw_loc_next;
24414 else if (!loc->resolved_addr)
24416 /* As things stand, we do not expect or allow one die to
24417 reference a suffix of another die's location list chain.
24418 References must be identical or completely separate.
24419 There is therefore no need to cache the result of this
24420 pass on any list other than the first; doing so
24421 would lead to unnecessary writes. */
24422 while (*curr)
24424 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
24425 if (!resolve_addr_in_expr ((*curr)->expr))
24427 dw_loc_list_ref next = (*curr)->dw_loc_next;
24428 dw_loc_descr_ref l = (*curr)->expr;
24430 if (next && (*curr)->ll_symbol)
24432 gcc_assert (!next->ll_symbol);
24433 next->ll_symbol = (*curr)->ll_symbol;
24435 if (dwarf_split_debug_info)
24436 remove_loc_list_addr_table_entries (l);
24437 *curr = next;
24439 else
24441 mark_base_types ((*curr)->expr);
24442 curr = &(*curr)->dw_loc_next;
24445 if (loc == *start)
24446 loc->resolved_addr = 1;
24447 else
24449 loc->replaced = 1;
24450 loc->dw_loc_next = *start;
24453 if (!*start)
24455 remove_AT (die, a->dw_attr);
24456 ix--;
24458 break;
24459 case dw_val_class_loc:
24461 dw_loc_descr_ref l = AT_loc (a);
24462 /* For -gdwarf-2 don't attempt to optimize
24463 DW_AT_data_member_location containing
24464 DW_OP_plus_uconst - older consumers might
24465 rely on it being that op instead of a more complex,
24466 but shorter, location description. */
24467 if ((dwarf_version > 2
24468 || a->dw_attr != DW_AT_data_member_location
24469 || l == NULL
24470 || l->dw_loc_opc != DW_OP_plus_uconst
24471 || l->dw_loc_next != NULL)
24472 && !resolve_addr_in_expr (l))
24474 if (dwarf_split_debug_info)
24475 remove_loc_list_addr_table_entries (l);
24476 if (l != NULL
24477 && l->dw_loc_next == NULL
24478 && l->dw_loc_opc == DW_OP_addr
24479 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
24480 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
24481 && a->dw_attr == DW_AT_location)
24483 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
24484 remove_AT (die, a->dw_attr);
24485 ix--;
24486 optimize_location_into_implicit_ptr (die, decl);
24487 break;
24489 remove_AT (die, a->dw_attr);
24490 ix--;
24492 else
24493 mark_base_types (l);
24495 break;
24496 case dw_val_class_addr:
24497 if (a->dw_attr == DW_AT_const_value
24498 && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
24500 if (AT_index (a) != NOT_INDEXED)
24501 remove_addr_table_entry (a->dw_attr_val.val_entry);
24502 remove_AT (die, a->dw_attr);
24503 ix--;
24505 if (die->die_tag == DW_TAG_GNU_call_site
24506 && a->dw_attr == DW_AT_abstract_origin)
24508 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
24509 dw_die_ref tdie = lookup_decl_die (tdecl);
24510 dw_die_ref cdie;
24511 if (tdie == NULL
24512 && DECL_EXTERNAL (tdecl)
24513 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE
24514 && (cdie = lookup_context_die (DECL_CONTEXT (tdecl))))
24516 /* Creating a full DIE for tdecl is overly expensive and
24517 at this point even wrong when in the LTO phase
24518 as it can end up generating new type DIEs we didn't
24519 output and thus optimize_external_refs will crash. */
24520 tdie = new_die (DW_TAG_subprogram, cdie, NULL_TREE);
24521 add_AT_flag (tdie, DW_AT_external, 1);
24522 add_AT_flag (tdie, DW_AT_declaration, 1);
24523 add_linkage_attr (tdie, tdecl);
24524 add_name_and_src_coords_attributes (tdie, tdecl);
24525 equate_decl_number_to_die (tdecl, tdie);
24527 if (tdie)
24529 a->dw_attr_val.val_class = dw_val_class_die_ref;
24530 a->dw_attr_val.v.val_die_ref.die = tdie;
24531 a->dw_attr_val.v.val_die_ref.external = 0;
24533 else
24535 if (AT_index (a) != NOT_INDEXED)
24536 remove_addr_table_entry (a->dw_attr_val.val_entry);
24537 remove_AT (die, a->dw_attr);
24538 ix--;
24541 break;
24542 default:
24543 break;
24546 FOR_EACH_CHILD (die, c, resolve_addr (c));
24549 /* Helper routines for optimize_location_lists.
24550 This pass tries to share identical local lists in .debug_loc
24551 section. */
24553 /* Iteratively hash operands of LOC opcode into HSTATE. */
24555 static void
24556 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
24558 dw_val_ref val1 = &loc->dw_loc_oprnd1;
24559 dw_val_ref val2 = &loc->dw_loc_oprnd2;
24561 switch (loc->dw_loc_opc)
24563 case DW_OP_const4u:
24564 case DW_OP_const8u:
24565 if (loc->dtprel)
24566 goto hash_addr;
24567 /* FALLTHRU */
24568 case DW_OP_const1u:
24569 case DW_OP_const1s:
24570 case DW_OP_const2u:
24571 case DW_OP_const2s:
24572 case DW_OP_const4s:
24573 case DW_OP_const8s:
24574 case DW_OP_constu:
24575 case DW_OP_consts:
24576 case DW_OP_pick:
24577 case DW_OP_plus_uconst:
24578 case DW_OP_breg0:
24579 case DW_OP_breg1:
24580 case DW_OP_breg2:
24581 case DW_OP_breg3:
24582 case DW_OP_breg4:
24583 case DW_OP_breg5:
24584 case DW_OP_breg6:
24585 case DW_OP_breg7:
24586 case DW_OP_breg8:
24587 case DW_OP_breg9:
24588 case DW_OP_breg10:
24589 case DW_OP_breg11:
24590 case DW_OP_breg12:
24591 case DW_OP_breg13:
24592 case DW_OP_breg14:
24593 case DW_OP_breg15:
24594 case DW_OP_breg16:
24595 case DW_OP_breg17:
24596 case DW_OP_breg18:
24597 case DW_OP_breg19:
24598 case DW_OP_breg20:
24599 case DW_OP_breg21:
24600 case DW_OP_breg22:
24601 case DW_OP_breg23:
24602 case DW_OP_breg24:
24603 case DW_OP_breg25:
24604 case DW_OP_breg26:
24605 case DW_OP_breg27:
24606 case DW_OP_breg28:
24607 case DW_OP_breg29:
24608 case DW_OP_breg30:
24609 case DW_OP_breg31:
24610 case DW_OP_regx:
24611 case DW_OP_fbreg:
24612 case DW_OP_piece:
24613 case DW_OP_deref_size:
24614 case DW_OP_xderef_size:
24615 hstate.add_object (val1->v.val_int);
24616 break;
24617 case DW_OP_skip:
24618 case DW_OP_bra:
24620 int offset;
24622 gcc_assert (val1->val_class == dw_val_class_loc);
24623 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
24624 hstate.add_object (offset);
24626 break;
24627 case DW_OP_implicit_value:
24628 hstate.add_object (val1->v.val_unsigned);
24629 switch (val2->val_class)
24631 case dw_val_class_const:
24632 hstate.add_object (val2->v.val_int);
24633 break;
24634 case dw_val_class_vec:
24636 unsigned int elt_size = val2->v.val_vec.elt_size;
24637 unsigned int len = val2->v.val_vec.length;
24639 hstate.add_int (elt_size);
24640 hstate.add_int (len);
24641 hstate.add (val2->v.val_vec.array, len * elt_size);
24643 break;
24644 case dw_val_class_const_double:
24645 hstate.add_object (val2->v.val_double.low);
24646 hstate.add_object (val2->v.val_double.high);
24647 break;
24648 case dw_val_class_wide_int:
24649 hstate.add (val2->v.val_wide->get_val (),
24650 get_full_len (*val2->v.val_wide)
24651 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
24652 break;
24653 case dw_val_class_addr:
24654 inchash::add_rtx (val2->v.val_addr, hstate);
24655 break;
24656 default:
24657 gcc_unreachable ();
24659 break;
24660 case DW_OP_bregx:
24661 case DW_OP_bit_piece:
24662 hstate.add_object (val1->v.val_int);
24663 hstate.add_object (val2->v.val_int);
24664 break;
24665 case DW_OP_addr:
24666 hash_addr:
24667 if (loc->dtprel)
24669 unsigned char dtprel = 0xd1;
24670 hstate.add_object (dtprel);
24672 inchash::add_rtx (val1->v.val_addr, hstate);
24673 break;
24674 case DW_OP_GNU_addr_index:
24675 case DW_OP_GNU_const_index:
24677 if (loc->dtprel)
24679 unsigned char dtprel = 0xd1;
24680 hstate.add_object (dtprel);
24682 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
24684 break;
24685 case DW_OP_GNU_implicit_pointer:
24686 hstate.add_int (val2->v.val_int);
24687 break;
24688 case DW_OP_GNU_entry_value:
24689 hstate.add_object (val1->v.val_loc);
24690 break;
24691 case DW_OP_GNU_regval_type:
24692 case DW_OP_GNU_deref_type:
24694 unsigned int byte_size
24695 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
24696 unsigned int encoding
24697 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
24698 hstate.add_object (val1->v.val_int);
24699 hstate.add_object (byte_size);
24700 hstate.add_object (encoding);
24702 break;
24703 case DW_OP_GNU_convert:
24704 case DW_OP_GNU_reinterpret:
24705 if (val1->val_class == dw_val_class_unsigned_const)
24707 hstate.add_object (val1->v.val_unsigned);
24708 break;
24710 /* FALLTHRU */
24711 case DW_OP_GNU_const_type:
24713 unsigned int byte_size
24714 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
24715 unsigned int encoding
24716 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
24717 hstate.add_object (byte_size);
24718 hstate.add_object (encoding);
24719 if (loc->dw_loc_opc != DW_OP_GNU_const_type)
24720 break;
24721 hstate.add_object (val2->val_class);
24722 switch (val2->val_class)
24724 case dw_val_class_const:
24725 hstate.add_object (val2->v.val_int);
24726 break;
24727 case dw_val_class_vec:
24729 unsigned int elt_size = val2->v.val_vec.elt_size;
24730 unsigned int len = val2->v.val_vec.length;
24732 hstate.add_object (elt_size);
24733 hstate.add_object (len);
24734 hstate.add (val2->v.val_vec.array, len * elt_size);
24736 break;
24737 case dw_val_class_const_double:
24738 hstate.add_object (val2->v.val_double.low);
24739 hstate.add_object (val2->v.val_double.high);
24740 break;
24741 case dw_val_class_wide_int:
24742 hstate.add (val2->v.val_wide->get_val (),
24743 get_full_len (*val2->v.val_wide)
24744 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
24745 break;
24746 default:
24747 gcc_unreachable ();
24750 break;
24752 default:
24753 /* Other codes have no operands. */
24754 break;
24758 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
24760 static inline void
24761 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
24763 dw_loc_descr_ref l;
24764 bool sizes_computed = false;
24765 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
24766 size_of_locs (loc);
24768 for (l = loc; l != NULL; l = l->dw_loc_next)
24770 enum dwarf_location_atom opc = l->dw_loc_opc;
24771 hstate.add_object (opc);
24772 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
24774 size_of_locs (loc);
24775 sizes_computed = true;
24777 hash_loc_operands (l, hstate);
24781 /* Compute hash of the whole location list LIST_HEAD. */
24783 static inline void
24784 hash_loc_list (dw_loc_list_ref list_head)
24786 dw_loc_list_ref curr = list_head;
24787 inchash::hash hstate;
24789 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
24791 hstate.add (curr->begin, strlen (curr->begin) + 1);
24792 hstate.add (curr->end, strlen (curr->end) + 1);
24793 if (curr->section)
24794 hstate.add (curr->section, strlen (curr->section) + 1);
24795 hash_locs (curr->expr, hstate);
24797 list_head->hash = hstate.end ();
24800 /* Return true if X and Y opcodes have the same operands. */
24802 static inline bool
24803 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
24805 dw_val_ref valx1 = &x->dw_loc_oprnd1;
24806 dw_val_ref valx2 = &x->dw_loc_oprnd2;
24807 dw_val_ref valy1 = &y->dw_loc_oprnd1;
24808 dw_val_ref valy2 = &y->dw_loc_oprnd2;
24810 switch (x->dw_loc_opc)
24812 case DW_OP_const4u:
24813 case DW_OP_const8u:
24814 if (x->dtprel)
24815 goto hash_addr;
24816 /* FALLTHRU */
24817 case DW_OP_const1u:
24818 case DW_OP_const1s:
24819 case DW_OP_const2u:
24820 case DW_OP_const2s:
24821 case DW_OP_const4s:
24822 case DW_OP_const8s:
24823 case DW_OP_constu:
24824 case DW_OP_consts:
24825 case DW_OP_pick:
24826 case DW_OP_plus_uconst:
24827 case DW_OP_breg0:
24828 case DW_OP_breg1:
24829 case DW_OP_breg2:
24830 case DW_OP_breg3:
24831 case DW_OP_breg4:
24832 case DW_OP_breg5:
24833 case DW_OP_breg6:
24834 case DW_OP_breg7:
24835 case DW_OP_breg8:
24836 case DW_OP_breg9:
24837 case DW_OP_breg10:
24838 case DW_OP_breg11:
24839 case DW_OP_breg12:
24840 case DW_OP_breg13:
24841 case DW_OP_breg14:
24842 case DW_OP_breg15:
24843 case DW_OP_breg16:
24844 case DW_OP_breg17:
24845 case DW_OP_breg18:
24846 case DW_OP_breg19:
24847 case DW_OP_breg20:
24848 case DW_OP_breg21:
24849 case DW_OP_breg22:
24850 case DW_OP_breg23:
24851 case DW_OP_breg24:
24852 case DW_OP_breg25:
24853 case DW_OP_breg26:
24854 case DW_OP_breg27:
24855 case DW_OP_breg28:
24856 case DW_OP_breg29:
24857 case DW_OP_breg30:
24858 case DW_OP_breg31:
24859 case DW_OP_regx:
24860 case DW_OP_fbreg:
24861 case DW_OP_piece:
24862 case DW_OP_deref_size:
24863 case DW_OP_xderef_size:
24864 return valx1->v.val_int == valy1->v.val_int;
24865 case DW_OP_skip:
24866 case DW_OP_bra:
24867 /* If splitting debug info, the use of DW_OP_GNU_addr_index
24868 can cause irrelevant differences in dw_loc_addr. */
24869 gcc_assert (valx1->val_class == dw_val_class_loc
24870 && valy1->val_class == dw_val_class_loc
24871 && (dwarf_split_debug_info
24872 || x->dw_loc_addr == y->dw_loc_addr));
24873 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
24874 case DW_OP_implicit_value:
24875 if (valx1->v.val_unsigned != valy1->v.val_unsigned
24876 || valx2->val_class != valy2->val_class)
24877 return false;
24878 switch (valx2->val_class)
24880 case dw_val_class_const:
24881 return valx2->v.val_int == valy2->v.val_int;
24882 case dw_val_class_vec:
24883 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
24884 && valx2->v.val_vec.length == valy2->v.val_vec.length
24885 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
24886 valx2->v.val_vec.elt_size
24887 * valx2->v.val_vec.length) == 0;
24888 case dw_val_class_const_double:
24889 return valx2->v.val_double.low == valy2->v.val_double.low
24890 && valx2->v.val_double.high == valy2->v.val_double.high;
24891 case dw_val_class_wide_int:
24892 return *valx2->v.val_wide == *valy2->v.val_wide;
24893 case dw_val_class_addr:
24894 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
24895 default:
24896 gcc_unreachable ();
24898 case DW_OP_bregx:
24899 case DW_OP_bit_piece:
24900 return valx1->v.val_int == valy1->v.val_int
24901 && valx2->v.val_int == valy2->v.val_int;
24902 case DW_OP_addr:
24903 hash_addr:
24904 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
24905 case DW_OP_GNU_addr_index:
24906 case DW_OP_GNU_const_index:
24908 rtx ax1 = valx1->val_entry->addr.rtl;
24909 rtx ay1 = valy1->val_entry->addr.rtl;
24910 return rtx_equal_p (ax1, ay1);
24912 case DW_OP_GNU_implicit_pointer:
24913 return valx1->val_class == dw_val_class_die_ref
24914 && valx1->val_class == valy1->val_class
24915 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
24916 && valx2->v.val_int == valy2->v.val_int;
24917 case DW_OP_GNU_entry_value:
24918 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
24919 case DW_OP_GNU_const_type:
24920 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
24921 || valx2->val_class != valy2->val_class)
24922 return false;
24923 switch (valx2->val_class)
24925 case dw_val_class_const:
24926 return valx2->v.val_int == valy2->v.val_int;
24927 case dw_val_class_vec:
24928 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
24929 && valx2->v.val_vec.length == valy2->v.val_vec.length
24930 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
24931 valx2->v.val_vec.elt_size
24932 * valx2->v.val_vec.length) == 0;
24933 case dw_val_class_const_double:
24934 return valx2->v.val_double.low == valy2->v.val_double.low
24935 && valx2->v.val_double.high == valy2->v.val_double.high;
24936 case dw_val_class_wide_int:
24937 return *valx2->v.val_wide == *valy2->v.val_wide;
24938 default:
24939 gcc_unreachable ();
24941 case DW_OP_GNU_regval_type:
24942 case DW_OP_GNU_deref_type:
24943 return valx1->v.val_int == valy1->v.val_int
24944 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
24945 case DW_OP_GNU_convert:
24946 case DW_OP_GNU_reinterpret:
24947 if (valx1->val_class != valy1->val_class)
24948 return false;
24949 if (valx1->val_class == dw_val_class_unsigned_const)
24950 return valx1->v.val_unsigned == valy1->v.val_unsigned;
24951 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
24952 case DW_OP_GNU_parameter_ref:
24953 return valx1->val_class == dw_val_class_die_ref
24954 && valx1->val_class == valy1->val_class
24955 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
24956 default:
24957 /* Other codes have no operands. */
24958 return true;
24962 /* Return true if DWARF location expressions X and Y are the same. */
24964 static inline bool
24965 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
24967 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
24968 if (x->dw_loc_opc != y->dw_loc_opc
24969 || x->dtprel != y->dtprel
24970 || !compare_loc_operands (x, y))
24971 break;
24972 return x == NULL && y == NULL;
24975 /* Hashtable helpers. */
24977 struct loc_list_hasher : nofree_ptr_hash <dw_loc_list_struct>
24979 static inline hashval_t hash (const dw_loc_list_struct *);
24980 static inline bool equal (const dw_loc_list_struct *,
24981 const dw_loc_list_struct *);
24984 /* Return precomputed hash of location list X. */
24986 inline hashval_t
24987 loc_list_hasher::hash (const dw_loc_list_struct *x)
24989 return x->hash;
24992 /* Return true if location lists A and B are the same. */
24994 inline bool
24995 loc_list_hasher::equal (const dw_loc_list_struct *a,
24996 const dw_loc_list_struct *b)
24998 if (a == b)
24999 return 1;
25000 if (a->hash != b->hash)
25001 return 0;
25002 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
25003 if (strcmp (a->begin, b->begin) != 0
25004 || strcmp (a->end, b->end) != 0
25005 || (a->section == NULL) != (b->section == NULL)
25006 || (a->section && strcmp (a->section, b->section) != 0)
25007 || !compare_locs (a->expr, b->expr))
25008 break;
25009 return a == NULL && b == NULL;
25012 typedef hash_table<loc_list_hasher> loc_list_hash_type;
25015 /* Recursively optimize location lists referenced from DIE
25016 children and share them whenever possible. */
25018 static void
25019 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
25021 dw_die_ref c;
25022 dw_attr_ref a;
25023 unsigned ix;
25024 dw_loc_list_struct **slot;
25026 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
25027 if (AT_class (a) == dw_val_class_loc_list)
25029 dw_loc_list_ref list = AT_loc_list (a);
25030 /* TODO: perform some optimizations here, before hashing
25031 it and storing into the hash table. */
25032 hash_loc_list (list);
25033 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
25034 if (*slot == NULL)
25035 *slot = list;
25036 else
25037 a->dw_attr_val.v.val_loc_list = *slot;
25040 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
25044 /* Recursively assign each location list a unique index into the debug_addr
25045 section. */
25047 static void
25048 index_location_lists (dw_die_ref die)
25050 dw_die_ref c;
25051 dw_attr_ref a;
25052 unsigned ix;
25054 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
25055 if (AT_class (a) == dw_val_class_loc_list)
25057 dw_loc_list_ref list = AT_loc_list (a);
25058 dw_loc_list_ref curr;
25059 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
25061 /* Don't index an entry that has already been indexed
25062 or won't be output. */
25063 if (curr->begin_entry != NULL
25064 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
25065 continue;
25067 curr->begin_entry
25068 = add_addr_table_entry (xstrdup (curr->begin),
25069 ate_kind_label);
25073 FOR_EACH_CHILD (die, c, index_location_lists (c));
25076 /* Optimize location lists referenced from DIE
25077 children and share them whenever possible. */
25079 static void
25080 optimize_location_lists (dw_die_ref die)
25082 loc_list_hash_type htab (500);
25083 optimize_location_lists_1 (die, &htab);
25086 /* Output stuff that dwarf requires at the end of every file,
25087 and generate the DWARF-2 debugging info. */
25089 static void
25090 dwarf2out_finish (const char *filename)
25092 comdat_type_node *ctnode;
25093 dw_die_ref main_comp_unit_die;
25095 /* Flush out any latecomers to the limbo party. */
25096 dwarf2out_early_finish ();
25098 /* PCH might result in DW_AT_producer string being restored from the
25099 header compilation, so always fill it with empty string initially
25100 and overwrite only here. */
25101 dw_attr_ref producer = get_AT (comp_unit_die (), DW_AT_producer);
25102 producer_string = gen_producer_string ();
25103 producer->dw_attr_val.v.val_str->refcount--;
25104 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
25106 gen_scheduled_generic_parms_dies ();
25107 gen_remaining_tmpl_value_param_die_attribute ();
25109 /* Add the name for the main input file now. We delayed this from
25110 dwarf2out_init to avoid complications with PCH.
25111 For LTO produced units use a fixed artificial name to avoid
25112 leaking tempfile names into the dwarf. */
25113 if (!in_lto_p)
25114 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
25115 else
25116 add_name_attribute (comp_unit_die (), "<artificial>");
25117 if (!IS_ABSOLUTE_PATH (filename) || targetm.force_at_comp_dir)
25118 add_comp_dir_attribute (comp_unit_die ());
25119 else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
25121 bool p = false;
25122 file_table->traverse<bool *, file_table_relative_p> (&p);
25123 if (p)
25124 add_comp_dir_attribute (comp_unit_die ());
25127 #if ENABLE_ASSERT_CHECKING
25129 dw_die_ref die = comp_unit_die (), c;
25130 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
25132 #endif
25133 resolve_addr (comp_unit_die ());
25134 move_marked_base_types ();
25136 /* Walk through the list of incomplete types again, trying once more to
25137 emit full debugging info for them. */
25138 retry_incomplete_types ();
25140 if (flag_eliminate_unused_debug_types)
25141 prune_unused_types ();
25143 /* Generate separate COMDAT sections for type DIEs. */
25144 if (use_debug_types)
25146 break_out_comdat_types (comp_unit_die ());
25148 /* Each new type_unit DIE was added to the limbo die list when created.
25149 Since these have all been added to comdat_type_list, clear the
25150 limbo die list. */
25151 limbo_die_list = NULL;
25153 /* For each new comdat type unit, copy declarations for incomplete
25154 types to make the new unit self-contained (i.e., no direct
25155 references to the main compile unit). */
25156 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
25157 copy_decls_for_unworthy_types (ctnode->root_die);
25158 copy_decls_for_unworthy_types (comp_unit_die ());
25160 /* In the process of copying declarations from one unit to another,
25161 we may have left some declarations behind that are no longer
25162 referenced. Prune them. */
25163 prune_unused_types ();
25166 /* Generate separate CUs for each of the include files we've seen.
25167 They will go into limbo_die_list. */
25168 if (flag_eliminate_dwarf2_dups)
25169 break_out_includes (comp_unit_die ());
25171 /* Traverse the DIE's and add add sibling attributes to those DIE's
25172 that have children. */
25173 add_sibling_attributes (comp_unit_die ());
25174 limbo_die_node *node;
25175 for (node = limbo_die_list; node; node = node->next)
25176 add_sibling_attributes (node->die);
25177 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
25178 add_sibling_attributes (ctnode->root_die);
25180 /* When splitting DWARF info, we put some attributes in the
25181 skeleton compile_unit DIE that remains in the .o, while
25182 most attributes go in the DWO compile_unit_die. */
25183 if (dwarf_split_debug_info)
25184 main_comp_unit_die = gen_compile_unit_die (NULL);
25185 else
25186 main_comp_unit_die = comp_unit_die ();
25188 /* Output a terminator label for the .text section. */
25189 switch_to_section (text_section);
25190 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
25191 if (cold_text_section)
25193 switch_to_section (cold_text_section);
25194 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
25197 /* We can only use the low/high_pc attributes if all of the code was
25198 in .text. */
25199 if (!have_multiple_function_sections
25200 || (dwarf_version < 3 && dwarf_strict))
25202 /* Don't add if the CU has no associated code. */
25203 if (text_section_used)
25204 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
25205 text_end_label, true);
25207 else
25209 unsigned fde_idx;
25210 dw_fde_ref fde;
25211 bool range_list_added = false;
25213 if (text_section_used)
25214 add_ranges_by_labels (main_comp_unit_die, text_section_label,
25215 text_end_label, &range_list_added, true);
25216 if (cold_text_section_used)
25217 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
25218 cold_end_label, &range_list_added, true);
25220 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
25222 if (DECL_IGNORED_P (fde->decl))
25223 continue;
25224 if (!fde->in_std_section)
25225 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
25226 fde->dw_fde_end, &range_list_added,
25227 true);
25228 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
25229 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
25230 fde->dw_fde_second_end, &range_list_added,
25231 true);
25234 if (range_list_added)
25236 /* We need to give .debug_loc and .debug_ranges an appropriate
25237 "base address". Use zero so that these addresses become
25238 absolute. Historically, we've emitted the unexpected
25239 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
25240 Emit both to give time for other tools to adapt. */
25241 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
25242 if (! dwarf_strict && dwarf_version < 4)
25243 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
25245 add_ranges (NULL);
25249 if (debug_info_level >= DINFO_LEVEL_TERSE)
25250 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
25251 debug_line_section_label);
25253 if (have_macinfo)
25254 add_AT_macptr (comp_unit_die (),
25255 dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
25256 macinfo_section_label);
25258 if (dwarf_split_debug_info)
25260 /* optimize_location_lists calculates the size of the lists,
25261 so index them first, and assign indices to the entries.
25262 Although optimize_location_lists will remove entries from
25263 the table, it only does so for duplicates, and therefore
25264 only reduces ref_counts to 1. */
25265 index_location_lists (comp_unit_die ());
25267 if (addr_index_table != NULL)
25269 unsigned int index = 0;
25270 addr_index_table
25271 ->traverse_noresize<unsigned int *, index_addr_table_entry>
25272 (&index);
25276 if (have_location_lists)
25277 optimize_location_lists (comp_unit_die ());
25279 save_macinfo_strings ();
25281 if (dwarf_split_debug_info)
25283 unsigned int index = 0;
25285 /* Add attributes common to skeleton compile_units and
25286 type_units. Because these attributes include strings, it
25287 must be done before freezing the string table. Top-level
25288 skeleton die attrs are added when the skeleton type unit is
25289 created, so ensure it is created by this point. */
25290 add_top_level_skeleton_die_attrs (main_comp_unit_die);
25291 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
25294 /* Output all of the compilation units. We put the main one last so that
25295 the offsets are available to output_pubnames. */
25296 for (node = limbo_die_list; node; node = node->next)
25297 output_comp_unit (node->die, 0);
25299 hash_table<comdat_type_hasher> comdat_type_table (100);
25300 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
25302 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
25304 /* Don't output duplicate types. */
25305 if (*slot != HTAB_EMPTY_ENTRY)
25306 continue;
25308 /* Add a pointer to the line table for the main compilation unit
25309 so that the debugger can make sense of DW_AT_decl_file
25310 attributes. */
25311 if (debug_info_level >= DINFO_LEVEL_TERSE)
25312 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
25313 (!dwarf_split_debug_info
25314 ? debug_line_section_label
25315 : debug_skeleton_line_section_label));
25317 output_comdat_type_unit (ctnode);
25318 *slot = ctnode;
25321 /* The AT_pubnames attribute needs to go in all skeleton dies, including
25322 both the main_cu and all skeleton TUs. Making this call unconditional
25323 would end up either adding a second copy of the AT_pubnames attribute, or
25324 requiring a special case in add_top_level_skeleton_die_attrs. */
25325 if (!dwarf_split_debug_info)
25326 add_AT_pubnames (comp_unit_die ());
25328 if (dwarf_split_debug_info)
25330 int mark;
25331 unsigned char checksum[16];
25332 struct md5_ctx ctx;
25334 /* Compute a checksum of the comp_unit to use as the dwo_id. */
25335 md5_init_ctx (&ctx);
25336 mark = 0;
25337 die_checksum (comp_unit_die (), &ctx, &mark);
25338 unmark_all_dies (comp_unit_die ());
25339 md5_finish_ctx (&ctx, checksum);
25341 /* Use the first 8 bytes of the checksum as the dwo_id,
25342 and add it to both comp-unit DIEs. */
25343 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
25344 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
25346 /* Add the base offset of the ranges table to the skeleton
25347 comp-unit DIE. */
25348 if (ranges_table_in_use)
25349 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
25350 ranges_section_label);
25352 switch_to_section (debug_addr_section);
25353 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
25354 output_addr_table ();
25357 /* Output the main compilation unit if non-empty or if .debug_macinfo
25358 or .debug_macro will be emitted. */
25359 output_comp_unit (comp_unit_die (), have_macinfo);
25361 if (dwarf_split_debug_info && info_section_emitted)
25362 output_skeleton_debug_sections (main_comp_unit_die);
25364 /* Output the abbreviation table. */
25365 if (abbrev_die_table_in_use != 1)
25367 switch_to_section (debug_abbrev_section);
25368 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
25369 output_abbrev_section ();
25372 /* Output location list section if necessary. */
25373 if (have_location_lists)
25375 /* Output the location lists info. */
25376 switch_to_section (debug_loc_section);
25377 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
25378 output_location_lists (comp_unit_die ());
25381 output_pubtables ();
25383 /* Output the address range information if a CU (.debug_info section)
25384 was emitted. We output an empty table even if we had no functions
25385 to put in it. This because the consumer has no way to tell the
25386 difference between an empty table that we omitted and failure to
25387 generate a table that would have contained data. */
25388 if (info_section_emitted)
25390 unsigned long aranges_length = size_of_aranges ();
25392 switch_to_section (debug_aranges_section);
25393 output_aranges (aranges_length);
25396 /* Output ranges section if necessary. */
25397 if (ranges_table_in_use)
25399 switch_to_section (debug_ranges_section);
25400 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
25401 output_ranges ();
25404 /* Have to end the macro section. */
25405 if (have_macinfo)
25407 switch_to_section (debug_macinfo_section);
25408 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
25409 output_macinfo ();
25410 dw2_asm_output_data (1, 0, "End compilation unit");
25413 /* Output the source line correspondence table. We must do this
25414 even if there is no line information. Otherwise, on an empty
25415 translation unit, we will generate a present, but empty,
25416 .debug_info section. IRIX 6.5 `nm' will then complain when
25417 examining the file. This is done late so that any filenames
25418 used by the debug_info section are marked as 'used'. */
25419 switch_to_section (debug_line_section);
25420 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
25421 if (! DWARF2_ASM_LINE_DEBUG_INFO)
25422 output_line_info (false);
25424 if (dwarf_split_debug_info && info_section_emitted)
25426 switch_to_section (debug_skeleton_line_section);
25427 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
25428 output_line_info (true);
25431 /* If we emitted any indirect strings, output the string table too. */
25432 if (debug_str_hash || skeleton_debug_str_hash)
25433 output_indirect_strings ();
25436 /* Perform any cleanups needed after the early debug generation pass
25437 has run. */
25439 static void
25440 dwarf2out_early_finish (void)
25442 limbo_die_node *node, *next_node;
25444 /* Add DW_AT_linkage_name for all deferred DIEs. */
25445 for (node = deferred_asm_name; node; node = node->next)
25447 tree decl = node->created_for;
25448 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
25449 /* A missing DECL_ASSEMBLER_NAME can be a constant DIE that
25450 ended up in in deferred_asm_name before we knew it was
25451 constant and never written to disk. */
25452 && DECL_ASSEMBLER_NAME (decl))
25454 add_linkage_attr (node->die, decl);
25455 move_linkage_attr (node->die);
25458 deferred_asm_name = NULL;
25460 /* Traverse the limbo die list, and add parent/child links. The only
25461 dies without parents that should be here are concrete instances of
25462 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
25463 For concrete instances, we can get the parent die from the abstract
25464 instance.
25466 The point here is to flush out the limbo list so that it is empty
25467 and we don't need to stream it for LTO. */
25468 for (node = limbo_die_list; node; node = next_node)
25470 dw_die_ref die = node->die;
25471 next_node = node->next;
25473 if (die->die_parent == NULL)
25475 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
25477 if (origin && origin->die_parent)
25478 add_child_die (origin->die_parent, die);
25479 else if (is_cu_die (die))
25481 else if (seen_error ())
25482 /* It's OK to be confused by errors in the input. */
25483 add_child_die (comp_unit_die (), die);
25484 else
25486 /* In certain situations, the lexical block containing a
25487 nested function can be optimized away, which results
25488 in the nested function die being orphaned. Likewise
25489 with the return type of that nested function. Force
25490 this to be a child of the containing function.
25492 It may happen that even the containing function got fully
25493 inlined and optimized out. In that case we are lost and
25494 assign the empty child. This should not be big issue as
25495 the function is likely unreachable too. */
25496 gcc_assert (node->created_for);
25498 if (DECL_P (node->created_for))
25499 origin = get_context_die (DECL_CONTEXT (node->created_for));
25500 else if (TYPE_P (node->created_for))
25501 origin = scope_die_for (node->created_for, comp_unit_die ());
25502 else
25503 origin = comp_unit_die ();
25505 add_child_die (origin, die);
25510 limbo_die_list = NULL;
25513 /* Reset all state within dwarf2out.c so that we can rerun the compiler
25514 within the same process. For use by toplev::finalize. */
25516 void
25517 dwarf2out_c_finalize (void)
25519 last_var_location_insn = NULL;
25520 cached_next_real_insn = NULL;
25521 used_rtx_array = NULL;
25522 incomplete_types = NULL;
25523 decl_scope_table = NULL;
25524 debug_info_section = NULL;
25525 debug_skeleton_info_section = NULL;
25526 debug_abbrev_section = NULL;
25527 debug_skeleton_abbrev_section = NULL;
25528 debug_aranges_section = NULL;
25529 debug_addr_section = NULL;
25530 debug_macinfo_section = NULL;
25531 debug_line_section = NULL;
25532 debug_skeleton_line_section = NULL;
25533 debug_loc_section = NULL;
25534 debug_pubnames_section = NULL;
25535 debug_pubtypes_section = NULL;
25536 debug_str_section = NULL;
25537 debug_str_dwo_section = NULL;
25538 debug_str_offsets_section = NULL;
25539 debug_ranges_section = NULL;
25540 debug_frame_section = NULL;
25541 fde_vec = NULL;
25542 debug_str_hash = NULL;
25543 skeleton_debug_str_hash = NULL;
25544 dw2_string_counter = 0;
25545 have_multiple_function_sections = false;
25546 text_section_used = false;
25547 cold_text_section_used = false;
25548 cold_text_section = NULL;
25549 current_unit_personality = NULL;
25551 next_die_offset = 0;
25552 single_comp_unit_die = NULL;
25553 comdat_type_list = NULL;
25554 limbo_die_list = NULL;
25555 file_table = NULL;
25556 decl_die_table = NULL;
25557 common_block_die_table = NULL;
25558 decl_loc_table = NULL;
25559 call_arg_locations = NULL;
25560 call_arg_loc_last = NULL;
25561 call_site_count = -1;
25562 tail_call_site_count = -1;
25563 cached_dw_loc_list_table = NULL;
25564 abbrev_die_table = NULL;
25565 abbrev_die_table_allocated = 0;
25566 abbrev_die_table_in_use = 0;
25567 line_info_label_num = 0;
25568 cur_line_info_table = NULL;
25569 text_section_line_info = NULL;
25570 cold_text_section_line_info = NULL;
25571 separate_line_info = NULL;
25572 info_section_emitted = false;
25573 pubname_table = NULL;
25574 pubtype_table = NULL;
25575 macinfo_table = NULL;
25576 ranges_table = NULL;
25577 ranges_table_allocated = 0;
25578 ranges_table_in_use = 0;
25579 ranges_by_label = 0;
25580 ranges_by_label_allocated = 0;
25581 ranges_by_label_in_use = 0;
25582 have_location_lists = false;
25583 loclabel_num = 0;
25584 poc_label_num = 0;
25585 last_emitted_file = NULL;
25586 label_num = 0;
25587 tmpl_value_parm_die_table = NULL;
25588 generic_type_instances = NULL;
25589 frame_pointer_fb_offset = 0;
25590 frame_pointer_fb_offset_valid = false;
25591 base_types.release ();
25592 XDELETEVEC (producer_string);
25593 producer_string = NULL;
25596 #include "gt-dwarf2out.h"