Merged from trunk from revision r222717 up to r222905.
[official-gcc.git] / gcc / dwarf2out.c
blob1ead84c86a10e483f19215d52f0fbd031f0fa149
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 "hash-set.h"
64 #include "machmode.h"
65 #include "vec.h"
66 #include "double-int.h"
67 #include "input.h"
68 #include "alias.h"
69 #include "symtab.h"
70 #include "wide-int.h"
71 #include "inchash.h"
72 #include "real.h"
73 #include "tree.h"
74 #include "fold-const.h"
75 #include "stringpool.h"
76 #include "stor-layout.h"
77 #include "varasm.h"
78 #include "hashtab.h"
79 #include "hard-reg-set.h"
80 #include "function.h"
81 #include "emit-rtl.h"
82 #include "hash-table.h"
83 #include "version.h"
84 #include "flags.h"
85 #include "regs.h"
86 #include "rtlhash.h"
87 #include "insn-config.h"
88 #include "reload.h"
89 #include "output.h"
90 #include "statistics.h"
91 #include "fixed-value.h"
92 #include "expmed.h"
93 #include "dojump.h"
94 #include "explow.h"
95 #include "calls.h"
96 #include "stmt.h"
97 #include "expr.h"
98 #include "except.h"
99 #include "dwarf2.h"
100 #include "dwarf2out.h"
101 #include "dwarf2asm.h"
102 #include "toplev.h"
103 #include "md5.h"
104 #include "tm_p.h"
105 #include "diagnostic.h"
106 #include "tree-pretty-print.h"
107 #include "debug.h"
108 #include "target.h"
109 #include "common/common-target.h"
110 #include "langhooks.h"
111 #include "hash-map.h"
112 #include "is-a.h"
113 #include "plugin-api.h"
114 #include "ipa-ref.h"
115 #include "cgraph.h"
116 #include "ira.h"
117 #include "lra.h"
118 #include "dumpfile.h"
119 #include "opts.h"
120 #include "tree-dfa.h"
121 #include "gdb/gdb-index.h"
122 #include "rtl-iter.h"
124 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
125 static rtx_insn *last_var_location_insn;
126 static rtx_insn *cached_next_real_insn;
127 static void dwarf2out_decl (tree);
129 #ifdef VMS_DEBUGGING_INFO
130 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
132 /* Define this macro to be a nonzero value if the directory specifications
133 which are output in the debug info should end with a separator. */
134 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
135 /* Define this macro to evaluate to a nonzero value if GCC should refrain
136 from generating indirect strings in DWARF2 debug information, for instance
137 if your target is stuck with an old version of GDB that is unable to
138 process them properly or uses VMS Debug. */
139 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
140 #else
141 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
142 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
143 #endif
145 /* ??? Poison these here until it can be done generically. They've been
146 totally replaced in this file; make sure it stays that way. */
147 #undef DWARF2_UNWIND_INFO
148 #undef DWARF2_FRAME_INFO
149 #if (GCC_VERSION >= 3000)
150 #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
151 #endif
153 /* The size of the target's pointer type. */
154 #ifndef PTR_SIZE
155 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
156 #endif
158 /* Array of RTXes referenced by the debugging information, which therefore
159 must be kept around forever. */
160 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
162 /* A pointer to the base of a list of incomplete types which might be
163 completed at some later time. incomplete_types_list needs to be a
164 vec<tree, va_gc> *because we want to tell the garbage collector about
165 it. */
166 static GTY(()) vec<tree, va_gc> *incomplete_types;
168 /* A pointer to the base of a table of references to declaration
169 scopes. This table is a display which tracks the nesting
170 of declaration scopes at the current scope and containing
171 scopes. This table is used to find the proper place to
172 define type declaration DIE's. */
173 static GTY(()) vec<tree, va_gc> *decl_scope_table;
175 /* Pointers to various DWARF2 sections. */
176 static GTY(()) section *debug_info_section;
177 static GTY(()) section *debug_skeleton_info_section;
178 static GTY(()) section *debug_abbrev_section;
179 static GTY(()) section *debug_skeleton_abbrev_section;
180 static GTY(()) section *debug_aranges_section;
181 static GTY(()) section *debug_addr_section;
182 static GTY(()) section *debug_macinfo_section;
183 static GTY(()) section *debug_line_section;
184 static GTY(()) section *debug_skeleton_line_section;
185 static GTY(()) section *debug_loc_section;
186 static GTY(()) section *debug_pubnames_section;
187 static GTY(()) section *debug_pubtypes_section;
188 static GTY(()) section *debug_str_section;
189 static GTY(()) section *debug_str_dwo_section;
190 static GTY(()) section *debug_str_offsets_section;
191 static GTY(()) section *debug_ranges_section;
192 static GTY(()) section *debug_frame_section;
194 /* Maximum size (in bytes) of an artificially generated label. */
195 #define MAX_ARTIFICIAL_LABEL_BYTES 30
197 /* According to the (draft) DWARF 3 specification, the initial length
198 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
199 bytes are 0xffffffff, followed by the length stored in the next 8
200 bytes.
202 However, the SGI/MIPS ABI uses an initial length which is equal to
203 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
205 #ifndef DWARF_INITIAL_LENGTH_SIZE
206 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
207 #endif
209 /* Round SIZE up to the nearest BOUNDARY. */
210 #define DWARF_ROUND(SIZE,BOUNDARY) \
211 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
213 /* CIE identifier. */
214 #if HOST_BITS_PER_WIDE_INT >= 64
215 #define DWARF_CIE_ID \
216 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
217 #else
218 #define DWARF_CIE_ID DW_CIE_ID
219 #endif
222 /* A vector for a table that contains frame description
223 information for each routine. */
224 #define NOT_INDEXED (-1U)
225 #define NO_INDEX_ASSIGNED (-2U)
227 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
229 struct GTY((for_user)) indirect_string_node {
230 const char *str;
231 unsigned int refcount;
232 enum dwarf_form form;
233 char *label;
234 unsigned int index;
237 struct indirect_string_hasher : ggc_hasher<indirect_string_node *>
239 typedef const char *compare_type;
241 static hashval_t hash (indirect_string_node *);
242 static bool equal (indirect_string_node *, const char *);
245 static GTY (()) hash_table<indirect_string_hasher> *debug_str_hash;
247 /* With split_debug_info, both the comp_dir and dwo_name go in the
248 main object file, rather than the dwo, similar to the force_direct
249 parameter elsewhere but with additional complications:
251 1) The string is needed in both the main object file and the dwo.
252 That is, the comp_dir and dwo_name will appear in both places.
254 2) Strings can use three forms: DW_FORM_string, DW_FORM_strp or
255 DW_FORM_GNU_str_index.
257 3) GCC chooses the form to use late, depending on the size and
258 reference count.
260 Rather than forcing the all debug string handling functions and
261 callers to deal with these complications, simply use a separate,
262 special-cased string table for any attribute that should go in the
263 main object file. This limits the complexity to just the places
264 that need it. */
266 static GTY (()) hash_table<indirect_string_hasher> *skeleton_debug_str_hash;
268 static GTY(()) int dw2_string_counter;
270 /* True if the compilation unit places functions in more than one section. */
271 static GTY(()) bool have_multiple_function_sections = false;
273 /* Whether the default text and cold text sections have been used at all. */
275 static GTY(()) bool text_section_used = false;
276 static GTY(()) bool cold_text_section_used = false;
278 /* The default cold text section. */
279 static GTY(()) section *cold_text_section;
281 /* The DIE for C++14 'auto' in a function return type. */
282 static GTY(()) dw_die_ref auto_die;
284 /* The DIE for C++14 'decltype(auto)' in a function return type. */
285 static GTY(()) dw_die_ref decltype_auto_die;
287 /* Forward declarations for functions defined in this file. */
289 static char *stripattributes (const char *);
290 static void output_call_frame_info (int);
291 static void dwarf2out_note_section_used (void);
293 /* Personality decl of current unit. Used only when assembler does not support
294 personality CFI. */
295 static GTY(()) rtx current_unit_personality;
297 /* Data and reference forms for relocatable data. */
298 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
299 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
301 #ifndef DEBUG_FRAME_SECTION
302 #define DEBUG_FRAME_SECTION ".debug_frame"
303 #endif
305 #ifndef FUNC_BEGIN_LABEL
306 #define FUNC_BEGIN_LABEL "LFB"
307 #endif
309 #ifndef FUNC_END_LABEL
310 #define FUNC_END_LABEL "LFE"
311 #endif
313 #ifndef PROLOGUE_END_LABEL
314 #define PROLOGUE_END_LABEL "LPE"
315 #endif
317 #ifndef EPILOGUE_BEGIN_LABEL
318 #define EPILOGUE_BEGIN_LABEL "LEB"
319 #endif
321 #ifndef FRAME_BEGIN_LABEL
322 #define FRAME_BEGIN_LABEL "Lframe"
323 #endif
324 #define CIE_AFTER_SIZE_LABEL "LSCIE"
325 #define CIE_END_LABEL "LECIE"
326 #define FDE_LABEL "LSFDE"
327 #define FDE_AFTER_SIZE_LABEL "LASFDE"
328 #define FDE_END_LABEL "LEFDE"
329 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
330 #define LINE_NUMBER_END_LABEL "LELT"
331 #define LN_PROLOG_AS_LABEL "LASLTP"
332 #define LN_PROLOG_END_LABEL "LELTP"
333 #define DIE_LABEL_PREFIX "DW"
335 /* Match the base name of a file to the base name of a compilation unit. */
337 static int
338 matches_main_base (const char *path)
340 /* Cache the last query. */
341 static const char *last_path = NULL;
342 static int last_match = 0;
343 if (path != last_path)
345 const char *base;
346 int length = base_of_path (path, &base);
347 last_path = path;
348 last_match = (length == main_input_baselength
349 && memcmp (base, main_input_basename, length) == 0);
351 return last_match;
354 #ifdef DEBUG_DEBUG_STRUCT
356 static int
357 dump_struct_debug (tree type, enum debug_info_usage usage,
358 enum debug_struct_file criterion, int generic,
359 int matches, int result)
361 /* Find the type name. */
362 tree type_decl = TYPE_STUB_DECL (type);
363 tree t = type_decl;
364 const char *name = 0;
365 if (TREE_CODE (t) == TYPE_DECL)
366 t = DECL_NAME (t);
367 if (t)
368 name = IDENTIFIER_POINTER (t);
370 fprintf (stderr, " struct %d %s %s %s %s %d %p %s\n",
371 criterion,
372 DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
373 matches ? "bas" : "hdr",
374 generic ? "gen" : "ord",
375 usage == DINFO_USAGE_DFN ? ";" :
376 usage == DINFO_USAGE_DIR_USE ? "." : "*",
377 result,
378 (void*) type_decl, name);
379 return result;
381 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
382 dump_struct_debug (type, usage, criterion, generic, matches, result)
384 #else
386 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
387 (result)
389 #endif
391 /* Get the number of HOST_WIDE_INTs needed to represent the precision
392 of the number. */
394 static unsigned int
395 get_full_len (const wide_int &op)
397 return ((op.get_precision () + HOST_BITS_PER_WIDE_INT - 1)
398 / HOST_BITS_PER_WIDE_INT);
401 static bool
402 should_emit_struct_debug (tree type, enum debug_info_usage usage)
404 enum debug_struct_file criterion;
405 tree type_decl;
406 bool generic = lang_hooks.types.generic_p (type);
408 if (generic)
409 criterion = debug_struct_generic[usage];
410 else
411 criterion = debug_struct_ordinary[usage];
413 if (criterion == DINFO_STRUCT_FILE_NONE)
414 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
415 if (criterion == DINFO_STRUCT_FILE_ANY)
416 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
418 type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
420 if (type_decl != NULL)
422 if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
423 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
425 if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
426 return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
429 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
432 /* Return a pointer to a copy of the section string name S with all
433 attributes stripped off, and an asterisk prepended (for assemble_name). */
435 static inline char *
436 stripattributes (const char *s)
438 char *stripped = XNEWVEC (char, strlen (s) + 2);
439 char *p = stripped;
441 *p++ = '*';
443 while (*s && *s != ',')
444 *p++ = *s++;
446 *p = '\0';
447 return stripped;
450 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
451 switch to the data section instead, and write out a synthetic start label
452 for collect2 the first time around. */
454 static void
455 switch_to_eh_frame_section (bool back)
457 tree label;
459 #ifdef EH_FRAME_SECTION_NAME
460 if (eh_frame_section == 0)
462 int flags;
464 if (EH_TABLES_CAN_BE_READ_ONLY)
466 int fde_encoding;
467 int per_encoding;
468 int lsda_encoding;
470 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
471 /*global=*/0);
472 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
473 /*global=*/1);
474 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
475 /*global=*/0);
476 flags = ((! flag_pic
477 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
478 && (fde_encoding & 0x70) != DW_EH_PE_aligned
479 && (per_encoding & 0x70) != DW_EH_PE_absptr
480 && (per_encoding & 0x70) != DW_EH_PE_aligned
481 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
482 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
483 ? 0 : SECTION_WRITE);
485 else
486 flags = SECTION_WRITE;
487 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
489 #endif /* EH_FRAME_SECTION_NAME */
491 if (eh_frame_section)
492 switch_to_section (eh_frame_section);
493 else
495 /* We have no special eh_frame section. Put the information in
496 the data section and emit special labels to guide collect2. */
497 switch_to_section (data_section);
499 if (!back)
501 label = get_file_function_name ("F");
502 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
503 targetm.asm_out.globalize_label (asm_out_file,
504 IDENTIFIER_POINTER (label));
505 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
510 /* Switch [BACK] to the eh or debug frame table section, depending on
511 FOR_EH. */
513 static void
514 switch_to_frame_table_section (int for_eh, bool back)
516 if (for_eh)
517 switch_to_eh_frame_section (back);
518 else
520 if (!debug_frame_section)
521 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
522 SECTION_DEBUG, NULL);
523 switch_to_section (debug_frame_section);
527 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
529 enum dw_cfi_oprnd_type
530 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
532 switch (cfi)
534 case DW_CFA_nop:
535 case DW_CFA_GNU_window_save:
536 case DW_CFA_remember_state:
537 case DW_CFA_restore_state:
538 return dw_cfi_oprnd_unused;
540 case DW_CFA_set_loc:
541 case DW_CFA_advance_loc1:
542 case DW_CFA_advance_loc2:
543 case DW_CFA_advance_loc4:
544 case DW_CFA_MIPS_advance_loc8:
545 return dw_cfi_oprnd_addr;
547 case DW_CFA_offset:
548 case DW_CFA_offset_extended:
549 case DW_CFA_def_cfa:
550 case DW_CFA_offset_extended_sf:
551 case DW_CFA_def_cfa_sf:
552 case DW_CFA_restore:
553 case DW_CFA_restore_extended:
554 case DW_CFA_undefined:
555 case DW_CFA_same_value:
556 case DW_CFA_def_cfa_register:
557 case DW_CFA_register:
558 case DW_CFA_expression:
559 return dw_cfi_oprnd_reg_num;
561 case DW_CFA_def_cfa_offset:
562 case DW_CFA_GNU_args_size:
563 case DW_CFA_def_cfa_offset_sf:
564 return dw_cfi_oprnd_offset;
566 case DW_CFA_def_cfa_expression:
567 return dw_cfi_oprnd_loc;
569 default:
570 gcc_unreachable ();
574 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
576 enum dw_cfi_oprnd_type
577 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
579 switch (cfi)
581 case DW_CFA_def_cfa:
582 case DW_CFA_def_cfa_sf:
583 case DW_CFA_offset:
584 case DW_CFA_offset_extended_sf:
585 case DW_CFA_offset_extended:
586 return dw_cfi_oprnd_offset;
588 case DW_CFA_register:
589 return dw_cfi_oprnd_reg_num;
591 case DW_CFA_expression:
592 return dw_cfi_oprnd_loc;
594 default:
595 return dw_cfi_oprnd_unused;
599 /* Output one FDE. */
601 static void
602 output_fde (dw_fde_ref fde, bool for_eh, bool second,
603 char *section_start_label, int fde_encoding, char *augmentation,
604 bool any_lsda_needed, int lsda_encoding)
606 const char *begin, *end;
607 static unsigned int j;
608 char l1[20], l2[20];
610 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
611 /* empty */ 0);
612 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
613 for_eh + j);
614 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
615 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
616 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
617 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
618 " indicating 64-bit DWARF extension");
619 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
620 "FDE Length");
621 ASM_OUTPUT_LABEL (asm_out_file, l1);
623 if (for_eh)
624 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
625 else
626 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
627 debug_frame_section, "FDE CIE offset");
629 begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
630 end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
632 if (for_eh)
634 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
635 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
636 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
637 "FDE initial location");
638 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
639 end, begin, "FDE address range");
641 else
643 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
644 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
647 if (augmentation[0])
649 if (any_lsda_needed)
651 int size = size_of_encoded_value (lsda_encoding);
653 if (lsda_encoding == DW_EH_PE_aligned)
655 int offset = ( 4 /* Length */
656 + 4 /* CIE offset */
657 + 2 * size_of_encoded_value (fde_encoding)
658 + 1 /* Augmentation size */ );
659 int pad = -offset & (PTR_SIZE - 1);
661 size += pad;
662 gcc_assert (size_of_uleb128 (size) == 1);
665 dw2_asm_output_data_uleb128 (size, "Augmentation size");
667 if (fde->uses_eh_lsda)
669 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
670 fde->funcdef_number);
671 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
672 gen_rtx_SYMBOL_REF (Pmode, l1),
673 false,
674 "Language Specific Data Area");
676 else
678 if (lsda_encoding == DW_EH_PE_aligned)
679 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
680 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
681 "Language Specific Data Area (none)");
684 else
685 dw2_asm_output_data_uleb128 (0, "Augmentation size");
688 /* Loop through the Call Frame Instructions associated with this FDE. */
689 fde->dw_fde_current_label = begin;
691 size_t from, until, i;
693 from = 0;
694 until = vec_safe_length (fde->dw_fde_cfi);
696 if (fde->dw_fde_second_begin == NULL)
698 else if (!second)
699 until = fde->dw_fde_switch_cfi_index;
700 else
701 from = fde->dw_fde_switch_cfi_index;
703 for (i = from; i < until; i++)
704 output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
707 /* If we are to emit a ref/link from function bodies to their frame tables,
708 do it now. This is typically performed to make sure that tables
709 associated with functions are dragged with them and not discarded in
710 garbage collecting links. We need to do this on a per function basis to
711 cope with -ffunction-sections. */
713 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
714 /* Switch to the function section, emit the ref to the tables, and
715 switch *back* into the table section. */
716 switch_to_section (function_section (fde->decl));
717 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
718 switch_to_frame_table_section (for_eh, true);
719 #endif
721 /* Pad the FDE out to an address sized boundary. */
722 ASM_OUTPUT_ALIGN (asm_out_file,
723 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
724 ASM_OUTPUT_LABEL (asm_out_file, l2);
726 j += 2;
729 /* Return true if frame description entry FDE is needed for EH. */
731 static bool
732 fde_needed_for_eh_p (dw_fde_ref fde)
734 if (flag_asynchronous_unwind_tables)
735 return true;
737 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
738 return true;
740 if (fde->uses_eh_lsda)
741 return true;
743 /* If exceptions are enabled, we have collected nothrow info. */
744 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
745 return false;
747 return true;
750 /* Output the call frame information used to record information
751 that relates to calculating the frame pointer, and records the
752 location of saved registers. */
754 static void
755 output_call_frame_info (int for_eh)
757 unsigned int i;
758 dw_fde_ref fde;
759 dw_cfi_ref cfi;
760 char l1[20], l2[20], section_start_label[20];
761 bool any_lsda_needed = false;
762 char augmentation[6];
763 int augmentation_size;
764 int fde_encoding = DW_EH_PE_absptr;
765 int per_encoding = DW_EH_PE_absptr;
766 int lsda_encoding = DW_EH_PE_absptr;
767 int return_reg;
768 rtx personality = NULL;
769 int dw_cie_version;
771 /* Don't emit a CIE if there won't be any FDEs. */
772 if (!fde_vec)
773 return;
775 /* Nothing to do if the assembler's doing it all. */
776 if (dwarf2out_do_cfi_asm ())
777 return;
779 /* If we don't have any functions we'll want to unwind out of, don't emit
780 any EH unwind information. If we make FDEs linkonce, we may have to
781 emit an empty label for an FDE that wouldn't otherwise be emitted. We
782 want to avoid having an FDE kept around when the function it refers to
783 is discarded. Example where this matters: a primary function template
784 in C++ requires EH information, an explicit specialization doesn't. */
785 if (for_eh)
787 bool any_eh_needed = false;
789 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
791 if (fde->uses_eh_lsda)
792 any_eh_needed = any_lsda_needed = true;
793 else if (fde_needed_for_eh_p (fde))
794 any_eh_needed = true;
795 else if (TARGET_USES_WEAK_UNWIND_INFO)
796 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
799 if (!any_eh_needed)
800 return;
803 /* We're going to be generating comments, so turn on app. */
804 if (flag_debug_asm)
805 app_enable ();
807 /* Switch to the proper frame section, first time. */
808 switch_to_frame_table_section (for_eh, false);
810 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
811 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
813 /* Output the CIE. */
814 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
815 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
816 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
817 dw2_asm_output_data (4, 0xffffffff,
818 "Initial length escape value indicating 64-bit DWARF extension");
819 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
820 "Length of Common Information Entry");
821 ASM_OUTPUT_LABEL (asm_out_file, l1);
823 /* Now that the CIE pointer is PC-relative for EH,
824 use 0 to identify the CIE. */
825 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
826 (for_eh ? 0 : DWARF_CIE_ID),
827 "CIE Identifier Tag");
829 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
830 use CIE version 1, unless that would produce incorrect results
831 due to overflowing the return register column. */
832 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
833 dw_cie_version = 1;
834 if (return_reg >= 256 || dwarf_version > 2)
835 dw_cie_version = 3;
836 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
838 augmentation[0] = 0;
839 augmentation_size = 0;
841 personality = current_unit_personality;
842 if (for_eh)
844 char *p;
846 /* Augmentation:
847 z Indicates that a uleb128 is present to size the
848 augmentation section.
849 L Indicates the encoding (and thus presence) of
850 an LSDA pointer in the FDE augmentation.
851 R Indicates a non-default pointer encoding for
852 FDE code pointers.
853 P Indicates the presence of an encoding + language
854 personality routine in the CIE augmentation. */
856 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
857 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
858 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
860 p = augmentation + 1;
861 if (personality)
863 *p++ = 'P';
864 augmentation_size += 1 + size_of_encoded_value (per_encoding);
865 assemble_external_libcall (personality);
867 if (any_lsda_needed)
869 *p++ = 'L';
870 augmentation_size += 1;
872 if (fde_encoding != DW_EH_PE_absptr)
874 *p++ = 'R';
875 augmentation_size += 1;
877 if (p > augmentation + 1)
879 augmentation[0] = 'z';
880 *p = '\0';
883 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
884 if (personality && per_encoding == DW_EH_PE_aligned)
886 int offset = ( 4 /* Length */
887 + 4 /* CIE Id */
888 + 1 /* CIE version */
889 + strlen (augmentation) + 1 /* Augmentation */
890 + size_of_uleb128 (1) /* Code alignment */
891 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
892 + 1 /* RA column */
893 + 1 /* Augmentation size */
894 + 1 /* Personality encoding */ );
895 int pad = -offset & (PTR_SIZE - 1);
897 augmentation_size += pad;
899 /* Augmentations should be small, so there's scarce need to
900 iterate for a solution. Die if we exceed one uleb128 byte. */
901 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
905 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
906 if (dw_cie_version >= 4)
908 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
909 dw2_asm_output_data (1, 0, "CIE Segment Size");
911 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
912 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
913 "CIE Data Alignment Factor");
915 if (dw_cie_version == 1)
916 dw2_asm_output_data (1, return_reg, "CIE RA Column");
917 else
918 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
920 if (augmentation[0])
922 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
923 if (personality)
925 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
926 eh_data_format_name (per_encoding));
927 dw2_asm_output_encoded_addr_rtx (per_encoding,
928 personality,
929 true, NULL);
932 if (any_lsda_needed)
933 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
934 eh_data_format_name (lsda_encoding));
936 if (fde_encoding != DW_EH_PE_absptr)
937 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
938 eh_data_format_name (fde_encoding));
941 FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
942 output_cfi (cfi, NULL, for_eh);
944 /* Pad the CIE out to an address sized boundary. */
945 ASM_OUTPUT_ALIGN (asm_out_file,
946 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
947 ASM_OUTPUT_LABEL (asm_out_file, l2);
949 /* Loop through all of the FDE's. */
950 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
952 unsigned int k;
954 /* Don't emit EH unwind info for leaf functions that don't need it. */
955 if (for_eh && !fde_needed_for_eh_p (fde))
956 continue;
958 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
959 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
960 augmentation, any_lsda_needed, lsda_encoding);
963 if (for_eh && targetm.terminate_dw2_eh_frame_info)
964 dw2_asm_output_data (4, 0, "End of Table");
966 /* Turn off app to make assembly quicker. */
967 if (flag_debug_asm)
968 app_disable ();
971 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
973 static void
974 dwarf2out_do_cfi_startproc (bool second)
976 int enc;
977 rtx ref;
978 rtx personality = get_personality_function (current_function_decl);
980 fprintf (asm_out_file, "\t.cfi_startproc\n");
982 if (personality)
984 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
985 ref = personality;
987 /* ??? The GAS support isn't entirely consistent. We have to
988 handle indirect support ourselves, but PC-relative is done
989 in the assembler. Further, the assembler can't handle any
990 of the weirder relocation types. */
991 if (enc & DW_EH_PE_indirect)
992 ref = dw2_force_const_mem (ref, true);
994 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
995 output_addr_const (asm_out_file, ref);
996 fputc ('\n', asm_out_file);
999 if (crtl->uses_eh_lsda)
1001 char lab[20];
1003 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
1004 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
1005 current_function_funcdef_no);
1006 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
1007 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
1009 if (enc & DW_EH_PE_indirect)
1010 ref = dw2_force_const_mem (ref, true);
1012 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
1013 output_addr_const (asm_out_file, ref);
1014 fputc ('\n', asm_out_file);
1018 /* Allocate CURRENT_FDE. Immediately initialize all we can, noting that
1019 this allocation may be done before pass_final. */
1021 dw_fde_ref
1022 dwarf2out_alloc_current_fde (void)
1024 dw_fde_ref fde;
1026 fde = ggc_cleared_alloc<dw_fde_node> ();
1027 fde->decl = current_function_decl;
1028 fde->funcdef_number = current_function_funcdef_no;
1029 fde->fde_index = vec_safe_length (fde_vec);
1030 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
1031 fde->uses_eh_lsda = crtl->uses_eh_lsda;
1032 fde->nothrow = crtl->nothrow;
1033 fde->drap_reg = INVALID_REGNUM;
1034 fde->vdrap_reg = INVALID_REGNUM;
1036 /* Record the FDE associated with this function. */
1037 cfun->fde = fde;
1038 vec_safe_push (fde_vec, fde);
1040 return fde;
1043 /* Output a marker (i.e. a label) for the beginning of a function, before
1044 the prologue. */
1046 void
1047 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1048 const char *file ATTRIBUTE_UNUSED)
1050 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1051 char * dup_label;
1052 dw_fde_ref fde;
1053 section *fnsec;
1054 bool do_frame;
1056 current_function_func_begin_label = NULL;
1058 do_frame = dwarf2out_do_frame ();
1060 /* ??? current_function_func_begin_label is also used by except.c for
1061 call-site information. We must emit this label if it might be used. */
1062 if (!do_frame
1063 && (!flag_exceptions
1064 || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1065 return;
1067 fnsec = function_section (current_function_decl);
1068 switch_to_section (fnsec);
1069 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1070 current_function_funcdef_no);
1071 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1072 current_function_funcdef_no);
1073 dup_label = xstrdup (label);
1074 current_function_func_begin_label = dup_label;
1076 /* We can elide the fde allocation if we're not emitting debug info. */
1077 if (!do_frame)
1078 return;
1080 /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1081 emit insns as rtx but bypass the bulk of rest_of_compilation, which
1082 would include pass_dwarf2_frame. If we've not created the FDE yet,
1083 do so now. */
1084 fde = cfun->fde;
1085 if (fde == NULL)
1086 fde = dwarf2out_alloc_current_fde ();
1088 /* Initialize the bits of CURRENT_FDE that were not available earlier. */
1089 fde->dw_fde_begin = dup_label;
1090 fde->dw_fde_current_label = dup_label;
1091 fde->in_std_section = (fnsec == text_section
1092 || (cold_text_section && fnsec == cold_text_section));
1094 /* We only want to output line number information for the genuine dwarf2
1095 prologue case, not the eh frame case. */
1096 #ifdef DWARF2_DEBUGGING_INFO
1097 if (file)
1098 dwarf2out_source_line (line, file, 0, true);
1099 #endif
1101 if (dwarf2out_do_cfi_asm ())
1102 dwarf2out_do_cfi_startproc (false);
1103 else
1105 rtx personality = get_personality_function (current_function_decl);
1106 if (!current_unit_personality)
1107 current_unit_personality = personality;
1109 /* We cannot keep a current personality per function as without CFI
1110 asm, at the point where we emit the CFI data, there is no current
1111 function anymore. */
1112 if (personality && current_unit_personality != personality)
1113 sorry ("multiple EH personalities are supported only with assemblers "
1114 "supporting .cfi_personality directive");
1118 /* Output a marker (i.e. a label) for the end of the generated code
1119 for a function prologue. This gets called *after* the prologue code has
1120 been generated. */
1122 void
1123 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1124 const char *file ATTRIBUTE_UNUSED)
1126 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1128 /* Output a label to mark the endpoint of the code generated for this
1129 function. */
1130 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1131 current_function_funcdef_no);
1132 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1133 current_function_funcdef_no);
1134 cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1137 /* Output a marker (i.e. a label) for the beginning of the generated code
1138 for a function epilogue. This gets called *before* the prologue code has
1139 been generated. */
1141 void
1142 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1143 const char *file ATTRIBUTE_UNUSED)
1145 dw_fde_ref fde = cfun->fde;
1146 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1148 if (fde->dw_fde_vms_begin_epilogue)
1149 return;
1151 /* Output a label to mark the endpoint of the code generated for this
1152 function. */
1153 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1154 current_function_funcdef_no);
1155 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1156 current_function_funcdef_no);
1157 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1160 /* Output a marker (i.e. a label) for the absolute end of the generated code
1161 for a function definition. This gets called *after* the epilogue code has
1162 been generated. */
1164 void
1165 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1166 const char *file ATTRIBUTE_UNUSED)
1168 dw_fde_ref fde;
1169 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1171 last_var_location_insn = NULL;
1172 cached_next_real_insn = NULL;
1174 if (dwarf2out_do_cfi_asm ())
1175 fprintf (asm_out_file, "\t.cfi_endproc\n");
1177 /* Output a label to mark the endpoint of the code generated for this
1178 function. */
1179 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1180 current_function_funcdef_no);
1181 ASM_OUTPUT_LABEL (asm_out_file, label);
1182 fde = cfun->fde;
1183 gcc_assert (fde != NULL);
1184 if (fde->dw_fde_second_begin == NULL)
1185 fde->dw_fde_end = xstrdup (label);
1188 void
1189 dwarf2out_frame_finish (void)
1191 /* Output call frame information. */
1192 if (targetm.debug_unwind_info () == UI_DWARF2)
1193 output_call_frame_info (0);
1195 /* Output another copy for the unwinder. */
1196 if ((flag_unwind_tables || flag_exceptions)
1197 && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1198 output_call_frame_info (1);
1201 /* Note that the current function section is being used for code. */
1203 static void
1204 dwarf2out_note_section_used (void)
1206 section *sec = current_function_section ();
1207 if (sec == text_section)
1208 text_section_used = true;
1209 else if (sec == cold_text_section)
1210 cold_text_section_used = true;
1213 static void var_location_switch_text_section (void);
1214 static void set_cur_line_info_table (section *);
1216 void
1217 dwarf2out_switch_text_section (void)
1219 section *sect;
1220 dw_fde_ref fde = cfun->fde;
1222 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1224 if (!in_cold_section_p)
1226 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1227 fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1228 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1230 else
1232 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1233 fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1234 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1236 have_multiple_function_sections = true;
1238 /* There is no need to mark used sections when not debugging. */
1239 if (cold_text_section != NULL)
1240 dwarf2out_note_section_used ();
1242 if (dwarf2out_do_cfi_asm ())
1243 fprintf (asm_out_file, "\t.cfi_endproc\n");
1245 /* Now do the real section switch. */
1246 sect = current_function_section ();
1247 switch_to_section (sect);
1249 fde->second_in_std_section
1250 = (sect == text_section
1251 || (cold_text_section && sect == cold_text_section));
1253 if (dwarf2out_do_cfi_asm ())
1254 dwarf2out_do_cfi_startproc (true);
1256 var_location_switch_text_section ();
1258 if (cold_text_section != NULL)
1259 set_cur_line_info_table (sect);
1262 /* And now, the subset of the debugging information support code necessary
1263 for emitting location expressions. */
1265 /* Data about a single source file. */
1266 struct GTY((for_user)) dwarf_file_data {
1267 const char * filename;
1268 int emitted_number;
1271 typedef struct GTY(()) deferred_locations_struct
1273 tree variable;
1274 dw_die_ref die;
1275 } deferred_locations;
1278 static GTY(()) vec<deferred_locations, va_gc> *deferred_locations_list;
1281 /* Describe an entry into the .debug_addr section. */
1283 enum ate_kind {
1284 ate_kind_rtx,
1285 ate_kind_rtx_dtprel,
1286 ate_kind_label
1289 typedef struct GTY((for_user)) addr_table_entry_struct {
1290 enum ate_kind kind;
1291 unsigned int refcount;
1292 unsigned int index;
1293 union addr_table_entry_struct_union
1295 rtx GTY ((tag ("0"))) rtl;
1296 char * GTY ((tag ("1"))) label;
1298 GTY ((desc ("%1.kind"))) addr;
1300 addr_table_entry;
1302 /* Location lists are ranges + location descriptions for that range,
1303 so you can track variables that are in different places over
1304 their entire life. */
1305 typedef struct GTY(()) dw_loc_list_struct {
1306 dw_loc_list_ref dw_loc_next;
1307 const char *begin; /* Label and addr_entry for start of range */
1308 addr_table_entry *begin_entry;
1309 const char *end; /* Label for end of range */
1310 char *ll_symbol; /* Label for beginning of location list.
1311 Only on head of list */
1312 const char *section; /* Section this loclist is relative to */
1313 dw_loc_descr_ref expr;
1314 hashval_t hash;
1315 /* True if all addresses in this and subsequent lists are known to be
1316 resolved. */
1317 bool resolved_addr;
1318 /* True if this list has been replaced by dw_loc_next. */
1319 bool replaced;
1320 bool emitted;
1321 /* True if the range should be emitted even if begin and end
1322 are the same. */
1323 bool force;
1324 } dw_loc_list_node;
1326 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1328 /* Convert a DWARF stack opcode into its string name. */
1330 static const char *
1331 dwarf_stack_op_name (unsigned int op)
1333 const char *name = get_DW_OP_name (op);
1335 if (name != NULL)
1336 return name;
1338 return "OP_<unknown>";
1341 /* Return a pointer to a newly allocated location description. Location
1342 descriptions are simple expression terms that can be strung
1343 together to form more complicated location (address) descriptions. */
1345 static inline dw_loc_descr_ref
1346 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1347 unsigned HOST_WIDE_INT oprnd2)
1349 dw_loc_descr_ref descr = ggc_cleared_alloc<dw_loc_descr_node> ();
1351 descr->dw_loc_opc = op;
1352 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1353 descr->dw_loc_oprnd1.val_entry = NULL;
1354 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1355 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1356 descr->dw_loc_oprnd2.val_entry = NULL;
1357 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1359 return descr;
1362 /* Return a pointer to a newly allocated location description for
1363 REG and OFFSET. */
1365 static inline dw_loc_descr_ref
1366 new_reg_loc_descr (unsigned int reg, unsigned HOST_WIDE_INT offset)
1368 if (reg <= 31)
1369 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1370 offset, 0);
1371 else
1372 return new_loc_descr (DW_OP_bregx, reg, offset);
1375 /* Add a location description term to a location description expression. */
1377 static inline void
1378 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1380 dw_loc_descr_ref *d;
1382 /* Find the end of the chain. */
1383 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1386 *d = descr;
1389 /* Compare two location operands for exact equality. */
1391 static bool
1392 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1394 if (a->val_class != b->val_class)
1395 return false;
1396 switch (a->val_class)
1398 case dw_val_class_none:
1399 return true;
1400 case dw_val_class_addr:
1401 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1403 case dw_val_class_offset:
1404 case dw_val_class_unsigned_const:
1405 case dw_val_class_const:
1406 case dw_val_class_range_list:
1407 case dw_val_class_lineptr:
1408 case dw_val_class_macptr:
1409 /* These are all HOST_WIDE_INT, signed or unsigned. */
1410 return a->v.val_unsigned == b->v.val_unsigned;
1412 case dw_val_class_loc:
1413 return a->v.val_loc == b->v.val_loc;
1414 case dw_val_class_loc_list:
1415 return a->v.val_loc_list == b->v.val_loc_list;
1416 case dw_val_class_die_ref:
1417 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1418 case dw_val_class_fde_ref:
1419 return a->v.val_fde_index == b->v.val_fde_index;
1420 case dw_val_class_lbl_id:
1421 case dw_val_class_high_pc:
1422 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1423 case dw_val_class_str:
1424 return a->v.val_str == b->v.val_str;
1425 case dw_val_class_flag:
1426 return a->v.val_flag == b->v.val_flag;
1427 case dw_val_class_file:
1428 return a->v.val_file == b->v.val_file;
1429 case dw_val_class_decl_ref:
1430 return a->v.val_decl_ref == b->v.val_decl_ref;
1432 case dw_val_class_const_double:
1433 return (a->v.val_double.high == b->v.val_double.high
1434 && a->v.val_double.low == b->v.val_double.low);
1436 case dw_val_class_wide_int:
1437 return *a->v.val_wide == *b->v.val_wide;
1439 case dw_val_class_vec:
1441 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1442 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1444 return (a_len == b_len
1445 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1448 case dw_val_class_data8:
1449 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1451 case dw_val_class_vms_delta:
1452 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1453 && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1455 gcc_unreachable ();
1458 /* Compare two location atoms for exact equality. */
1460 static bool
1461 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1463 if (a->dw_loc_opc != b->dw_loc_opc)
1464 return false;
1466 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1467 address size, but since we always allocate cleared storage it
1468 should be zero for other types of locations. */
1469 if (a->dtprel != b->dtprel)
1470 return false;
1472 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1473 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1476 /* Compare two complete location expressions for exact equality. */
1478 bool
1479 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1481 while (1)
1483 if (a == b)
1484 return true;
1485 if (a == NULL || b == NULL)
1486 return false;
1487 if (!loc_descr_equal_p_1 (a, b))
1488 return false;
1490 a = a->dw_loc_next;
1491 b = b->dw_loc_next;
1496 /* Add a constant OFFSET to a location expression. */
1498 static void
1499 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1501 dw_loc_descr_ref loc;
1502 HOST_WIDE_INT *p;
1504 gcc_assert (*list_head != NULL);
1506 if (!offset)
1507 return;
1509 /* Find the end of the chain. */
1510 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1513 p = NULL;
1514 if (loc->dw_loc_opc == DW_OP_fbreg
1515 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1516 p = &loc->dw_loc_oprnd1.v.val_int;
1517 else if (loc->dw_loc_opc == DW_OP_bregx)
1518 p = &loc->dw_loc_oprnd2.v.val_int;
1520 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1521 offset. Don't optimize if an signed integer overflow would happen. */
1522 if (p != NULL
1523 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1524 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1525 *p += offset;
1527 else if (offset > 0)
1528 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1530 else
1532 loc->dw_loc_next = int_loc_descriptor (-offset);
1533 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1537 /* Add a constant OFFSET to a location list. */
1539 static void
1540 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1542 dw_loc_list_ref d;
1543 for (d = list_head; d != NULL; d = d->dw_loc_next)
1544 loc_descr_plus_const (&d->expr, offset);
1547 #define DWARF_REF_SIZE \
1548 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1550 static unsigned long int get_base_type_offset (dw_die_ref);
1552 /* Return the size of a location descriptor. */
1554 static unsigned long
1555 size_of_loc_descr (dw_loc_descr_ref loc)
1557 unsigned long size = 1;
1559 switch (loc->dw_loc_opc)
1561 case DW_OP_addr:
1562 size += DWARF2_ADDR_SIZE;
1563 break;
1564 case DW_OP_GNU_addr_index:
1565 case DW_OP_GNU_const_index:
1566 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1567 size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1568 break;
1569 case DW_OP_const1u:
1570 case DW_OP_const1s:
1571 size += 1;
1572 break;
1573 case DW_OP_const2u:
1574 case DW_OP_const2s:
1575 size += 2;
1576 break;
1577 case DW_OP_const4u:
1578 case DW_OP_const4s:
1579 size += 4;
1580 break;
1581 case DW_OP_const8u:
1582 case DW_OP_const8s:
1583 size += 8;
1584 break;
1585 case DW_OP_constu:
1586 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1587 break;
1588 case DW_OP_consts:
1589 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1590 break;
1591 case DW_OP_pick:
1592 size += 1;
1593 break;
1594 case DW_OP_plus_uconst:
1595 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1596 break;
1597 case DW_OP_skip:
1598 case DW_OP_bra:
1599 size += 2;
1600 break;
1601 case DW_OP_breg0:
1602 case DW_OP_breg1:
1603 case DW_OP_breg2:
1604 case DW_OP_breg3:
1605 case DW_OP_breg4:
1606 case DW_OP_breg5:
1607 case DW_OP_breg6:
1608 case DW_OP_breg7:
1609 case DW_OP_breg8:
1610 case DW_OP_breg9:
1611 case DW_OP_breg10:
1612 case DW_OP_breg11:
1613 case DW_OP_breg12:
1614 case DW_OP_breg13:
1615 case DW_OP_breg14:
1616 case DW_OP_breg15:
1617 case DW_OP_breg16:
1618 case DW_OP_breg17:
1619 case DW_OP_breg18:
1620 case DW_OP_breg19:
1621 case DW_OP_breg20:
1622 case DW_OP_breg21:
1623 case DW_OP_breg22:
1624 case DW_OP_breg23:
1625 case DW_OP_breg24:
1626 case DW_OP_breg25:
1627 case DW_OP_breg26:
1628 case DW_OP_breg27:
1629 case DW_OP_breg28:
1630 case DW_OP_breg29:
1631 case DW_OP_breg30:
1632 case DW_OP_breg31:
1633 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1634 break;
1635 case DW_OP_regx:
1636 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1637 break;
1638 case DW_OP_fbreg:
1639 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1640 break;
1641 case DW_OP_bregx:
1642 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1643 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1644 break;
1645 case DW_OP_piece:
1646 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1647 break;
1648 case DW_OP_bit_piece:
1649 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1650 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1651 break;
1652 case DW_OP_deref_size:
1653 case DW_OP_xderef_size:
1654 size += 1;
1655 break;
1656 case DW_OP_call2:
1657 size += 2;
1658 break;
1659 case DW_OP_call4:
1660 size += 4;
1661 break;
1662 case DW_OP_call_ref:
1663 size += DWARF_REF_SIZE;
1664 break;
1665 case DW_OP_implicit_value:
1666 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1667 + loc->dw_loc_oprnd1.v.val_unsigned;
1668 break;
1669 case DW_OP_GNU_implicit_pointer:
1670 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1671 break;
1672 case DW_OP_GNU_entry_value:
1674 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1675 size += size_of_uleb128 (op_size) + op_size;
1676 break;
1678 case DW_OP_GNU_const_type:
1680 unsigned long o
1681 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1682 size += size_of_uleb128 (o) + 1;
1683 switch (loc->dw_loc_oprnd2.val_class)
1685 case dw_val_class_vec:
1686 size += loc->dw_loc_oprnd2.v.val_vec.length
1687 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1688 break;
1689 case dw_val_class_const:
1690 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1691 break;
1692 case dw_val_class_const_double:
1693 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1694 break;
1695 case dw_val_class_wide_int:
1696 size += (get_full_len (*loc->dw_loc_oprnd2.v.val_wide)
1697 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
1698 break;
1699 default:
1700 gcc_unreachable ();
1702 break;
1704 case DW_OP_GNU_regval_type:
1706 unsigned long o
1707 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1708 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1709 + size_of_uleb128 (o);
1711 break;
1712 case DW_OP_GNU_deref_type:
1714 unsigned long o
1715 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1716 size += 1 + size_of_uleb128 (o);
1718 break;
1719 case DW_OP_GNU_convert:
1720 case DW_OP_GNU_reinterpret:
1721 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1722 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1723 else
1725 unsigned long o
1726 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1727 size += size_of_uleb128 (o);
1729 break;
1730 case DW_OP_GNU_parameter_ref:
1731 size += 4;
1732 break;
1733 default:
1734 break;
1737 return size;
1740 /* Return the size of a series of location descriptors. */
1742 unsigned long
1743 size_of_locs (dw_loc_descr_ref loc)
1745 dw_loc_descr_ref l;
1746 unsigned long size;
1748 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1749 field, to avoid writing to a PCH file. */
1750 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1752 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1753 break;
1754 size += size_of_loc_descr (l);
1756 if (! l)
1757 return size;
1759 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1761 l->dw_loc_addr = size;
1762 size += size_of_loc_descr (l);
1765 return size;
1768 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1769 static void get_ref_die_offset_label (char *, dw_die_ref);
1770 static unsigned long int get_ref_die_offset (dw_die_ref);
1772 /* Output location description stack opcode's operands (if any).
1773 The for_eh_or_skip parameter controls whether register numbers are
1774 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1775 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1776 info). This should be suppressed for the cases that have not been converted
1777 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
1779 static void
1780 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1782 dw_val_ref val1 = &loc->dw_loc_oprnd1;
1783 dw_val_ref val2 = &loc->dw_loc_oprnd2;
1785 switch (loc->dw_loc_opc)
1787 #ifdef DWARF2_DEBUGGING_INFO
1788 case DW_OP_const2u:
1789 case DW_OP_const2s:
1790 dw2_asm_output_data (2, val1->v.val_int, NULL);
1791 break;
1792 case DW_OP_const4u:
1793 if (loc->dtprel)
1795 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1796 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1797 val1->v.val_addr);
1798 fputc ('\n', asm_out_file);
1799 break;
1801 /* FALLTHRU */
1802 case DW_OP_const4s:
1803 dw2_asm_output_data (4, val1->v.val_int, NULL);
1804 break;
1805 case DW_OP_const8u:
1806 if (loc->dtprel)
1808 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1809 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1810 val1->v.val_addr);
1811 fputc ('\n', asm_out_file);
1812 break;
1814 /* FALLTHRU */
1815 case DW_OP_const8s:
1816 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
1817 dw2_asm_output_data (8, val1->v.val_int, NULL);
1818 break;
1819 case DW_OP_skip:
1820 case DW_OP_bra:
1822 int offset;
1824 gcc_assert (val1->val_class == dw_val_class_loc);
1825 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
1827 dw2_asm_output_data (2, offset, NULL);
1829 break;
1830 case DW_OP_implicit_value:
1831 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1832 switch (val2->val_class)
1834 case dw_val_class_const:
1835 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
1836 break;
1837 case dw_val_class_vec:
1839 unsigned int elt_size = val2->v.val_vec.elt_size;
1840 unsigned int len = val2->v.val_vec.length;
1841 unsigned int i;
1842 unsigned char *p;
1844 if (elt_size > sizeof (HOST_WIDE_INT))
1846 elt_size /= 2;
1847 len *= 2;
1849 for (i = 0, p = val2->v.val_vec.array;
1850 i < len;
1851 i++, p += elt_size)
1852 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
1853 "fp or vector constant word %u", i);
1855 break;
1856 case dw_val_class_const_double:
1858 unsigned HOST_WIDE_INT first, second;
1860 if (WORDS_BIG_ENDIAN)
1862 first = val2->v.val_double.high;
1863 second = val2->v.val_double.low;
1865 else
1867 first = val2->v.val_double.low;
1868 second = val2->v.val_double.high;
1870 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1871 first, NULL);
1872 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1873 second, NULL);
1875 break;
1876 case dw_val_class_wide_int:
1878 int i;
1879 int len = get_full_len (*val2->v.val_wide);
1880 if (WORDS_BIG_ENDIAN)
1881 for (i = len - 1; i >= 0; --i)
1882 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1883 val2->v.val_wide->elt (i), NULL);
1884 else
1885 for (i = 0; i < len; ++i)
1886 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1887 val2->v.val_wide->elt (i), NULL);
1889 break;
1890 case dw_val_class_addr:
1891 gcc_assert (val1->v.val_unsigned
1892 == (unsigned HOST_WIDE_INT) DWARF2_ADDR_SIZE);
1893 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
1894 break;
1895 default:
1896 gcc_unreachable ();
1898 break;
1899 #else
1900 case DW_OP_const2u:
1901 case DW_OP_const2s:
1902 case DW_OP_const4u:
1903 case DW_OP_const4s:
1904 case DW_OP_const8u:
1905 case DW_OP_const8s:
1906 case DW_OP_skip:
1907 case DW_OP_bra:
1908 case DW_OP_implicit_value:
1909 /* We currently don't make any attempt to make sure these are
1910 aligned properly like we do for the main unwind info, so
1911 don't support emitting things larger than a byte if we're
1912 only doing unwinding. */
1913 gcc_unreachable ();
1914 #endif
1915 case DW_OP_const1u:
1916 case DW_OP_const1s:
1917 dw2_asm_output_data (1, val1->v.val_int, NULL);
1918 break;
1919 case DW_OP_constu:
1920 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1921 break;
1922 case DW_OP_consts:
1923 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1924 break;
1925 case DW_OP_pick:
1926 dw2_asm_output_data (1, val1->v.val_int, NULL);
1927 break;
1928 case DW_OP_plus_uconst:
1929 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1930 break;
1931 case DW_OP_breg0:
1932 case DW_OP_breg1:
1933 case DW_OP_breg2:
1934 case DW_OP_breg3:
1935 case DW_OP_breg4:
1936 case DW_OP_breg5:
1937 case DW_OP_breg6:
1938 case DW_OP_breg7:
1939 case DW_OP_breg8:
1940 case DW_OP_breg9:
1941 case DW_OP_breg10:
1942 case DW_OP_breg11:
1943 case DW_OP_breg12:
1944 case DW_OP_breg13:
1945 case DW_OP_breg14:
1946 case DW_OP_breg15:
1947 case DW_OP_breg16:
1948 case DW_OP_breg17:
1949 case DW_OP_breg18:
1950 case DW_OP_breg19:
1951 case DW_OP_breg20:
1952 case DW_OP_breg21:
1953 case DW_OP_breg22:
1954 case DW_OP_breg23:
1955 case DW_OP_breg24:
1956 case DW_OP_breg25:
1957 case DW_OP_breg26:
1958 case DW_OP_breg27:
1959 case DW_OP_breg28:
1960 case DW_OP_breg29:
1961 case DW_OP_breg30:
1962 case DW_OP_breg31:
1963 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1964 break;
1965 case DW_OP_regx:
1967 unsigned r = val1->v.val_unsigned;
1968 if (for_eh_or_skip >= 0)
1969 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1970 gcc_assert (size_of_uleb128 (r)
1971 == size_of_uleb128 (val1->v.val_unsigned));
1972 dw2_asm_output_data_uleb128 (r, NULL);
1974 break;
1975 case DW_OP_fbreg:
1976 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1977 break;
1978 case DW_OP_bregx:
1980 unsigned r = val1->v.val_unsigned;
1981 if (for_eh_or_skip >= 0)
1982 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1983 gcc_assert (size_of_uleb128 (r)
1984 == size_of_uleb128 (val1->v.val_unsigned));
1985 dw2_asm_output_data_uleb128 (r, NULL);
1986 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
1988 break;
1989 case DW_OP_piece:
1990 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1991 break;
1992 case DW_OP_bit_piece:
1993 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1994 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
1995 break;
1996 case DW_OP_deref_size:
1997 case DW_OP_xderef_size:
1998 dw2_asm_output_data (1, val1->v.val_int, NULL);
1999 break;
2001 case DW_OP_addr:
2002 if (loc->dtprel)
2004 if (targetm.asm_out.output_dwarf_dtprel)
2006 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
2007 DWARF2_ADDR_SIZE,
2008 val1->v.val_addr);
2009 fputc ('\n', asm_out_file);
2011 else
2012 gcc_unreachable ();
2014 else
2016 #ifdef DWARF2_DEBUGGING_INFO
2017 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
2018 #else
2019 gcc_unreachable ();
2020 #endif
2022 break;
2024 case DW_OP_GNU_addr_index:
2025 case DW_OP_GNU_const_index:
2026 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
2027 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
2028 "(index into .debug_addr)");
2029 break;
2031 case DW_OP_GNU_implicit_pointer:
2033 char label[MAX_ARTIFICIAL_LABEL_BYTES
2034 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2035 gcc_assert (val1->val_class == dw_val_class_die_ref);
2036 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2037 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2038 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2040 break;
2042 case DW_OP_GNU_entry_value:
2043 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2044 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2045 break;
2047 case DW_OP_GNU_const_type:
2049 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2050 gcc_assert (o);
2051 dw2_asm_output_data_uleb128 (o, NULL);
2052 switch (val2->val_class)
2054 case dw_val_class_const:
2055 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2056 dw2_asm_output_data (1, l, NULL);
2057 dw2_asm_output_data (l, val2->v.val_int, NULL);
2058 break;
2059 case dw_val_class_vec:
2061 unsigned int elt_size = val2->v.val_vec.elt_size;
2062 unsigned int len = val2->v.val_vec.length;
2063 unsigned int i;
2064 unsigned char *p;
2066 l = len * elt_size;
2067 dw2_asm_output_data (1, l, NULL);
2068 if (elt_size > sizeof (HOST_WIDE_INT))
2070 elt_size /= 2;
2071 len *= 2;
2073 for (i = 0, p = val2->v.val_vec.array;
2074 i < len;
2075 i++, p += elt_size)
2076 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2077 "fp or vector constant word %u", i);
2079 break;
2080 case dw_val_class_const_double:
2082 unsigned HOST_WIDE_INT first, second;
2083 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2085 dw2_asm_output_data (1, 2 * l, NULL);
2086 if (WORDS_BIG_ENDIAN)
2088 first = val2->v.val_double.high;
2089 second = val2->v.val_double.low;
2091 else
2093 first = val2->v.val_double.low;
2094 second = val2->v.val_double.high;
2096 dw2_asm_output_data (l, first, NULL);
2097 dw2_asm_output_data (l, second, NULL);
2099 break;
2100 case dw_val_class_wide_int:
2102 int i;
2103 int len = get_full_len (*val2->v.val_wide);
2104 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2106 dw2_asm_output_data (1, len * l, NULL);
2107 if (WORDS_BIG_ENDIAN)
2108 for (i = len - 1; i >= 0; --i)
2109 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2110 else
2111 for (i = 0; i < len; ++i)
2112 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2114 break;
2115 default:
2116 gcc_unreachable ();
2119 break;
2120 case DW_OP_GNU_regval_type:
2122 unsigned r = val1->v.val_unsigned;
2123 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2124 gcc_assert (o);
2125 if (for_eh_or_skip >= 0)
2127 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2128 gcc_assert (size_of_uleb128 (r)
2129 == size_of_uleb128 (val1->v.val_unsigned));
2131 dw2_asm_output_data_uleb128 (r, NULL);
2132 dw2_asm_output_data_uleb128 (o, NULL);
2134 break;
2135 case DW_OP_GNU_deref_type:
2137 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2138 gcc_assert (o);
2139 dw2_asm_output_data (1, val1->v.val_int, NULL);
2140 dw2_asm_output_data_uleb128 (o, NULL);
2142 break;
2143 case DW_OP_GNU_convert:
2144 case DW_OP_GNU_reinterpret:
2145 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2146 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2147 else
2149 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2150 gcc_assert (o);
2151 dw2_asm_output_data_uleb128 (o, NULL);
2153 break;
2155 case DW_OP_GNU_parameter_ref:
2157 unsigned long o;
2158 gcc_assert (val1->val_class == dw_val_class_die_ref);
2159 o = get_ref_die_offset (val1->v.val_die_ref.die);
2160 dw2_asm_output_data (4, o, NULL);
2162 break;
2164 default:
2165 /* Other codes have no operands. */
2166 break;
2170 /* Output a sequence of location operations.
2171 The for_eh_or_skip parameter controls whether register numbers are
2172 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2173 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2174 info). This should be suppressed for the cases that have not been converted
2175 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2177 void
2178 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2180 for (; loc != NULL; loc = loc->dw_loc_next)
2182 enum dwarf_location_atom opc = loc->dw_loc_opc;
2183 /* Output the opcode. */
2184 if (for_eh_or_skip >= 0
2185 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2187 unsigned r = (opc - DW_OP_breg0);
2188 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2189 gcc_assert (r <= 31);
2190 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2192 else if (for_eh_or_skip >= 0
2193 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2195 unsigned r = (opc - DW_OP_reg0);
2196 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2197 gcc_assert (r <= 31);
2198 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2201 dw2_asm_output_data (1, opc,
2202 "%s", dwarf_stack_op_name (opc));
2204 /* Output the operand(s) (if any). */
2205 output_loc_operands (loc, for_eh_or_skip);
2209 /* Output location description stack opcode's operands (if any).
2210 The output is single bytes on a line, suitable for .cfi_escape. */
2212 static void
2213 output_loc_operands_raw (dw_loc_descr_ref loc)
2215 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2216 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2218 switch (loc->dw_loc_opc)
2220 case DW_OP_addr:
2221 case DW_OP_GNU_addr_index:
2222 case DW_OP_GNU_const_index:
2223 case DW_OP_implicit_value:
2224 /* We cannot output addresses in .cfi_escape, only bytes. */
2225 gcc_unreachable ();
2227 case DW_OP_const1u:
2228 case DW_OP_const1s:
2229 case DW_OP_pick:
2230 case DW_OP_deref_size:
2231 case DW_OP_xderef_size:
2232 fputc (',', asm_out_file);
2233 dw2_asm_output_data_raw (1, val1->v.val_int);
2234 break;
2236 case DW_OP_const2u:
2237 case DW_OP_const2s:
2238 fputc (',', asm_out_file);
2239 dw2_asm_output_data_raw (2, val1->v.val_int);
2240 break;
2242 case DW_OP_const4u:
2243 case DW_OP_const4s:
2244 fputc (',', asm_out_file);
2245 dw2_asm_output_data_raw (4, val1->v.val_int);
2246 break;
2248 case DW_OP_const8u:
2249 case DW_OP_const8s:
2250 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2251 fputc (',', asm_out_file);
2252 dw2_asm_output_data_raw (8, val1->v.val_int);
2253 break;
2255 case DW_OP_skip:
2256 case DW_OP_bra:
2258 int offset;
2260 gcc_assert (val1->val_class == dw_val_class_loc);
2261 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2263 fputc (',', asm_out_file);
2264 dw2_asm_output_data_raw (2, offset);
2266 break;
2268 case DW_OP_regx:
2270 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2271 gcc_assert (size_of_uleb128 (r)
2272 == size_of_uleb128 (val1->v.val_unsigned));
2273 fputc (',', asm_out_file);
2274 dw2_asm_output_data_uleb128_raw (r);
2276 break;
2278 case DW_OP_constu:
2279 case DW_OP_plus_uconst:
2280 case DW_OP_piece:
2281 fputc (',', asm_out_file);
2282 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2283 break;
2285 case DW_OP_bit_piece:
2286 fputc (',', asm_out_file);
2287 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2288 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2289 break;
2291 case DW_OP_consts:
2292 case DW_OP_breg0:
2293 case DW_OP_breg1:
2294 case DW_OP_breg2:
2295 case DW_OP_breg3:
2296 case DW_OP_breg4:
2297 case DW_OP_breg5:
2298 case DW_OP_breg6:
2299 case DW_OP_breg7:
2300 case DW_OP_breg8:
2301 case DW_OP_breg9:
2302 case DW_OP_breg10:
2303 case DW_OP_breg11:
2304 case DW_OP_breg12:
2305 case DW_OP_breg13:
2306 case DW_OP_breg14:
2307 case DW_OP_breg15:
2308 case DW_OP_breg16:
2309 case DW_OP_breg17:
2310 case DW_OP_breg18:
2311 case DW_OP_breg19:
2312 case DW_OP_breg20:
2313 case DW_OP_breg21:
2314 case DW_OP_breg22:
2315 case DW_OP_breg23:
2316 case DW_OP_breg24:
2317 case DW_OP_breg25:
2318 case DW_OP_breg26:
2319 case DW_OP_breg27:
2320 case DW_OP_breg28:
2321 case DW_OP_breg29:
2322 case DW_OP_breg30:
2323 case DW_OP_breg31:
2324 case DW_OP_fbreg:
2325 fputc (',', asm_out_file);
2326 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2327 break;
2329 case DW_OP_bregx:
2331 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2332 gcc_assert (size_of_uleb128 (r)
2333 == size_of_uleb128 (val1->v.val_unsigned));
2334 fputc (',', asm_out_file);
2335 dw2_asm_output_data_uleb128_raw (r);
2336 fputc (',', asm_out_file);
2337 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2339 break;
2341 case DW_OP_GNU_implicit_pointer:
2342 case DW_OP_GNU_entry_value:
2343 case DW_OP_GNU_const_type:
2344 case DW_OP_GNU_regval_type:
2345 case DW_OP_GNU_deref_type:
2346 case DW_OP_GNU_convert:
2347 case DW_OP_GNU_reinterpret:
2348 case DW_OP_GNU_parameter_ref:
2349 gcc_unreachable ();
2350 break;
2352 default:
2353 /* Other codes have no operands. */
2354 break;
2358 void
2359 output_loc_sequence_raw (dw_loc_descr_ref loc)
2361 while (1)
2363 enum dwarf_location_atom opc = loc->dw_loc_opc;
2364 /* Output the opcode. */
2365 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2367 unsigned r = (opc - DW_OP_breg0);
2368 r = DWARF2_FRAME_REG_OUT (r, 1);
2369 gcc_assert (r <= 31);
2370 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2372 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2374 unsigned r = (opc - DW_OP_reg0);
2375 r = DWARF2_FRAME_REG_OUT (r, 1);
2376 gcc_assert (r <= 31);
2377 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2379 /* Output the opcode. */
2380 fprintf (asm_out_file, "%#x", opc);
2381 output_loc_operands_raw (loc);
2383 if (!loc->dw_loc_next)
2384 break;
2385 loc = loc->dw_loc_next;
2387 fputc (',', asm_out_file);
2391 /* This function builds a dwarf location descriptor sequence from a
2392 dw_cfa_location, adding the given OFFSET to the result of the
2393 expression. */
2395 struct dw_loc_descr_node *
2396 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2398 struct dw_loc_descr_node *head, *tmp;
2400 offset += cfa->offset;
2402 if (cfa->indirect)
2404 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2405 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2406 head->dw_loc_oprnd1.val_entry = NULL;
2407 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2408 add_loc_descr (&head, tmp);
2409 if (offset != 0)
2411 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2412 add_loc_descr (&head, tmp);
2415 else
2416 head = new_reg_loc_descr (cfa->reg, offset);
2418 return head;
2421 /* This function builds a dwarf location descriptor sequence for
2422 the address at OFFSET from the CFA when stack is aligned to
2423 ALIGNMENT byte. */
2425 struct dw_loc_descr_node *
2426 build_cfa_aligned_loc (dw_cfa_location *cfa,
2427 HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2429 struct dw_loc_descr_node *head;
2430 unsigned int dwarf_fp
2431 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2433 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2434 if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2436 head = new_reg_loc_descr (dwarf_fp, 0);
2437 add_loc_descr (&head, int_loc_descriptor (alignment));
2438 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2439 loc_descr_plus_const (&head, offset);
2441 else
2442 head = new_reg_loc_descr (dwarf_fp, offset);
2443 return head;
2446 /* And now, the support for symbolic debugging information. */
2448 /* .debug_str support. */
2450 static void dwarf2out_init (const char *);
2451 static void dwarf2out_finish (const char *);
2452 static void dwarf2out_assembly_start (void);
2453 static void dwarf2out_define (unsigned int, const char *);
2454 static void dwarf2out_undef (unsigned int, const char *);
2455 static void dwarf2out_start_source_file (unsigned, const char *);
2456 static void dwarf2out_end_source_file (unsigned);
2457 static void dwarf2out_function_decl (tree);
2458 static void dwarf2out_begin_block (unsigned, unsigned);
2459 static void dwarf2out_end_block (unsigned, unsigned);
2460 static bool dwarf2out_ignore_block (const_tree);
2461 static void dwarf2out_global_decl (tree);
2462 static void dwarf2out_type_decl (tree, int);
2463 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
2464 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2465 dw_die_ref);
2466 static void dwarf2out_abstract_function (tree);
2467 static void dwarf2out_var_location (rtx_insn *);
2468 static void dwarf2out_begin_function (tree);
2469 static void dwarf2out_end_function (unsigned int);
2470 static void dwarf2out_set_name (tree, tree);
2472 /* The debug hooks structure. */
2474 const struct gcc_debug_hooks dwarf2_debug_hooks =
2476 dwarf2out_init,
2477 dwarf2out_finish,
2478 dwarf2out_assembly_start,
2479 dwarf2out_define,
2480 dwarf2out_undef,
2481 dwarf2out_start_source_file,
2482 dwarf2out_end_source_file,
2483 dwarf2out_begin_block,
2484 dwarf2out_end_block,
2485 dwarf2out_ignore_block,
2486 dwarf2out_source_line,
2487 dwarf2out_begin_prologue,
2488 #if VMS_DEBUGGING_INFO
2489 dwarf2out_vms_end_prologue,
2490 dwarf2out_vms_begin_epilogue,
2491 #else
2492 debug_nothing_int_charstar,
2493 debug_nothing_int_charstar,
2494 #endif
2495 dwarf2out_end_epilogue,
2496 dwarf2out_begin_function,
2497 dwarf2out_end_function, /* end_function */
2498 dwarf2out_function_decl, /* function_decl */
2499 dwarf2out_global_decl,
2500 dwarf2out_type_decl, /* type_decl */
2501 dwarf2out_imported_module_or_decl,
2502 debug_nothing_tree, /* deferred_inline_function */
2503 /* The DWARF 2 backend tries to reduce debugging bloat by not
2504 emitting the abstract description of inline functions until
2505 something tries to reference them. */
2506 dwarf2out_abstract_function, /* outlining_inline_function */
2507 debug_nothing_rtx_code_label, /* label */
2508 debug_nothing_int, /* handle_pch */
2509 dwarf2out_var_location,
2510 dwarf2out_switch_text_section,
2511 dwarf2out_set_name,
2512 1, /* start_end_main_source_file */
2513 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2516 /* NOTE: In the comments in this file, many references are made to
2517 "Debugging Information Entries". This term is abbreviated as `DIE'
2518 throughout the remainder of this file. */
2520 /* An internal representation of the DWARF output is built, and then
2521 walked to generate the DWARF debugging info. The walk of the internal
2522 representation is done after the entire program has been compiled.
2523 The types below are used to describe the internal representation. */
2525 /* Whether to put type DIEs into their own section .debug_types instead
2526 of making them part of the .debug_info section. Only supported for
2527 Dwarf V4 or higher and the user didn't disable them through
2528 -fno-debug-types-section. It is more efficient to put them in a
2529 separate comdat sections since the linker will then be able to
2530 remove duplicates. But not all tools support .debug_types sections
2531 yet. */
2533 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2535 /* Various DIE's use offsets relative to the beginning of the
2536 .debug_info section to refer to each other. */
2538 typedef long int dw_offset;
2540 /* Define typedefs here to avoid circular dependencies. */
2542 typedef struct dw_attr_struct *dw_attr_ref;
2543 typedef struct dw_line_info_struct *dw_line_info_ref;
2544 typedef struct pubname_struct *pubname_ref;
2545 typedef struct dw_ranges_struct *dw_ranges_ref;
2546 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
2547 typedef struct comdat_type_struct *comdat_type_node_ref;
2549 /* The entries in the line_info table more-or-less mirror the opcodes
2550 that are used in the real dwarf line table. Arrays of these entries
2551 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2552 supported. */
2554 enum dw_line_info_opcode {
2555 /* Emit DW_LNE_set_address; the operand is the label index. */
2556 LI_set_address,
2558 /* Emit a row to the matrix with the given line. This may be done
2559 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2560 special opcodes. */
2561 LI_set_line,
2563 /* Emit a DW_LNS_set_file. */
2564 LI_set_file,
2566 /* Emit a DW_LNS_set_column. */
2567 LI_set_column,
2569 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
2570 LI_negate_stmt,
2572 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
2573 LI_set_prologue_end,
2574 LI_set_epilogue_begin,
2576 /* Emit a DW_LNE_set_discriminator. */
2577 LI_set_discriminator
2580 typedef struct GTY(()) dw_line_info_struct {
2581 enum dw_line_info_opcode opcode;
2582 unsigned int val;
2583 } dw_line_info_entry;
2586 typedef struct GTY(()) dw_line_info_table_struct {
2587 /* The label that marks the end of this section. */
2588 const char *end_label;
2590 /* The values for the last row of the matrix, as collected in the table.
2591 These are used to minimize the changes to the next row. */
2592 unsigned int file_num;
2593 unsigned int line_num;
2594 unsigned int column_num;
2595 int discrim_num;
2596 bool is_stmt;
2597 bool in_use;
2599 vec<dw_line_info_entry, va_gc> *entries;
2600 } dw_line_info_table;
2602 typedef dw_line_info_table *dw_line_info_table_p;
2605 /* Each DIE attribute has a field specifying the attribute kind,
2606 a link to the next attribute in the chain, and an attribute value.
2607 Attributes are typically linked below the DIE they modify. */
2609 typedef struct GTY(()) dw_attr_struct {
2610 enum dwarf_attribute dw_attr;
2611 dw_val_node dw_attr_val;
2613 dw_attr_node;
2616 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
2617 The children of each node form a circular list linked by
2618 die_sib. die_child points to the node *before* the "first" child node. */
2620 typedef struct GTY((chain_circular ("%h.die_sib"), for_user)) die_struct {
2621 union die_symbol_or_type_node
2623 const char * GTY ((tag ("0"))) die_symbol;
2624 comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
2626 GTY ((desc ("%0.comdat_type_p"))) die_id;
2627 vec<dw_attr_node, va_gc> *die_attr;
2628 dw_die_ref die_parent;
2629 dw_die_ref die_child;
2630 dw_die_ref die_sib;
2631 dw_die_ref die_definition; /* ref from a specification to its definition */
2632 dw_offset die_offset;
2633 unsigned long die_abbrev;
2634 int die_mark;
2635 unsigned int decl_id;
2636 enum dwarf_tag die_tag;
2637 /* Die is used and must not be pruned as unused. */
2638 BOOL_BITFIELD die_perennial_p : 1;
2639 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2640 /* Lots of spare bits. */
2642 die_node;
2644 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
2645 #define FOR_EACH_CHILD(die, c, expr) do { \
2646 c = die->die_child; \
2647 if (c) do { \
2648 c = c->die_sib; \
2649 expr; \
2650 } while (c != die->die_child); \
2651 } while (0)
2653 /* The pubname structure */
2655 typedef struct GTY(()) pubname_struct {
2656 dw_die_ref die;
2657 const char *name;
2659 pubname_entry;
2662 struct GTY(()) dw_ranges_struct {
2663 /* If this is positive, it's a block number, otherwise it's a
2664 bitwise-negated index into dw_ranges_by_label. */
2665 int num;
2668 /* A structure to hold a macinfo entry. */
2670 typedef struct GTY(()) macinfo_struct {
2671 unsigned char code;
2672 unsigned HOST_WIDE_INT lineno;
2673 const char *info;
2675 macinfo_entry;
2678 struct GTY(()) dw_ranges_by_label_struct {
2679 const char *begin;
2680 const char *end;
2683 /* The comdat type node structure. */
2684 typedef struct GTY(()) comdat_type_struct
2686 dw_die_ref root_die;
2687 dw_die_ref type_die;
2688 dw_die_ref skeleton_die;
2689 char signature[DWARF_TYPE_SIGNATURE_SIZE];
2690 struct comdat_type_struct *next;
2692 comdat_type_node;
2694 /* The limbo die list structure. */
2695 typedef struct GTY(()) limbo_die_struct {
2696 dw_die_ref die;
2697 tree created_for;
2698 struct limbo_die_struct *next;
2700 limbo_die_node;
2702 typedef struct skeleton_chain_struct
2704 dw_die_ref old_die;
2705 dw_die_ref new_die;
2706 struct skeleton_chain_struct *parent;
2708 skeleton_chain_node;
2710 /* Define a macro which returns nonzero for a TYPE_DECL which was
2711 implicitly generated for a type.
2713 Note that, unlike the C front-end (which generates a NULL named
2714 TYPE_DECL node for each complete tagged type, each array type,
2715 and each function type node created) the C++ front-end generates
2716 a _named_ TYPE_DECL node for each tagged type node created.
2717 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
2718 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
2719 front-end, but for each type, tagged or not. */
2721 #define TYPE_DECL_IS_STUB(decl) \
2722 (DECL_NAME (decl) == NULL_TREE \
2723 || (DECL_ARTIFICIAL (decl) \
2724 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
2725 /* This is necessary for stub decls that \
2726 appear in nested inline functions. */ \
2727 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
2728 && (decl_ultimate_origin (decl) \
2729 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
2731 /* Information concerning the compilation unit's programming
2732 language, and compiler version. */
2734 /* Fixed size portion of the DWARF compilation unit header. */
2735 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
2736 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
2738 /* Fixed size portion of the DWARF comdat type unit header. */
2739 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
2740 (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
2741 + DWARF_OFFSET_SIZE)
2743 /* Fixed size portion of public names info. */
2744 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
2746 /* Fixed size portion of the address range info. */
2747 #define DWARF_ARANGES_HEADER_SIZE \
2748 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2749 DWARF2_ADDR_SIZE * 2) \
2750 - DWARF_INITIAL_LENGTH_SIZE)
2752 /* Size of padding portion in the address range info. It must be
2753 aligned to twice the pointer size. */
2754 #define DWARF_ARANGES_PAD_SIZE \
2755 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2756 DWARF2_ADDR_SIZE * 2) \
2757 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
2759 /* Use assembler line directives if available. */
2760 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
2761 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
2762 #define DWARF2_ASM_LINE_DEBUG_INFO 1
2763 #else
2764 #define DWARF2_ASM_LINE_DEBUG_INFO 0
2765 #endif
2766 #endif
2768 /* Minimum line offset in a special line info. opcode.
2769 This value was chosen to give a reasonable range of values. */
2770 #define DWARF_LINE_BASE -10
2772 /* First special line opcode - leave room for the standard opcodes. */
2773 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
2775 /* Range of line offsets in a special line info. opcode. */
2776 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
2778 /* Flag that indicates the initial value of the is_stmt_start flag.
2779 In the present implementation, we do not mark any lines as
2780 the beginning of a source statement, because that information
2781 is not made available by the GCC front-end. */
2782 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
2784 /* Maximum number of operations per instruction bundle. */
2785 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
2786 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
2787 #endif
2789 /* This location is used by calc_die_sizes() to keep track
2790 the offset of each DIE within the .debug_info section. */
2791 static unsigned long next_die_offset;
2793 /* Record the root of the DIE's built for the current compilation unit. */
2794 static GTY(()) dw_die_ref single_comp_unit_die;
2796 /* A list of type DIEs that have been separated into comdat sections. */
2797 static GTY(()) comdat_type_node *comdat_type_list;
2799 /* A list of DIEs with a NULL parent waiting to be relocated. */
2800 static GTY(()) limbo_die_node *limbo_die_list;
2802 /* A list of DIEs for which we may have to generate
2803 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
2804 static GTY(()) limbo_die_node *deferred_asm_name;
2806 struct dwarf_file_hasher : ggc_hasher<dwarf_file_data *>
2808 typedef const char *compare_type;
2810 static hashval_t hash (dwarf_file_data *);
2811 static bool equal (dwarf_file_data *, const char *);
2814 /* Filenames referenced by this compilation unit. */
2815 static GTY(()) hash_table<dwarf_file_hasher> *file_table;
2817 struct decl_die_hasher : ggc_hasher<die_node *>
2819 typedef tree compare_type;
2821 static hashval_t hash (die_node *);
2822 static bool equal (die_node *, tree);
2824 /* A hash table of references to DIE's that describe declarations.
2825 The key is a DECL_UID() which is a unique number identifying each decl. */
2826 static GTY (()) hash_table<decl_die_hasher> *decl_die_table;
2828 struct block_die_hasher : ggc_hasher<die_struct *>
2830 static hashval_t hash (die_struct *);
2831 static bool equal (die_struct *, die_struct *);
2834 /* A hash table of references to DIE's that describe COMMON blocks.
2835 The key is DECL_UID() ^ die_parent. */
2836 static GTY (()) hash_table<block_die_hasher> *common_block_die_table;
2838 typedef struct GTY(()) die_arg_entry_struct {
2839 dw_die_ref die;
2840 tree arg;
2841 } die_arg_entry;
2844 /* Node of the variable location list. */
2845 struct GTY ((chain_next ("%h.next"))) var_loc_node {
2846 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
2847 EXPR_LIST chain. For small bitsizes, bitsize is encoded
2848 in mode of the EXPR_LIST node and first EXPR_LIST operand
2849 is either NOTE_INSN_VAR_LOCATION for a piece with a known
2850 location or NULL for padding. For larger bitsizes,
2851 mode is 0 and first operand is a CONCAT with bitsize
2852 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
2853 NULL as second operand. */
2854 rtx GTY (()) loc;
2855 const char * GTY (()) label;
2856 struct var_loc_node * GTY (()) next;
2859 /* Variable location list. */
2860 struct GTY ((for_user)) var_loc_list_def {
2861 struct var_loc_node * GTY (()) first;
2863 /* Pointer to the last but one or last element of the
2864 chained list. If the list is empty, both first and
2865 last are NULL, if the list contains just one node
2866 or the last node certainly is not redundant, it points
2867 to the last node, otherwise points to the last but one.
2868 Do not mark it for GC because it is marked through the chain. */
2869 struct var_loc_node * GTY ((skip ("%h"))) last;
2871 /* Pointer to the last element before section switch,
2872 if NULL, either sections weren't switched or first
2873 is after section switch. */
2874 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
2876 /* DECL_UID of the variable decl. */
2877 unsigned int decl_id;
2879 typedef struct var_loc_list_def var_loc_list;
2881 /* Call argument location list. */
2882 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
2883 rtx GTY (()) call_arg_loc_note;
2884 const char * GTY (()) label;
2885 tree GTY (()) block;
2886 bool tail_call_p;
2887 rtx GTY (()) symbol_ref;
2888 struct call_arg_loc_node * GTY (()) next;
2892 struct decl_loc_hasher : ggc_hasher<var_loc_list *>
2894 typedef const_tree compare_type;
2896 static hashval_t hash (var_loc_list *);
2897 static bool equal (var_loc_list *, const_tree);
2900 /* Table of decl location linked lists. */
2901 static GTY (()) hash_table<decl_loc_hasher> *decl_loc_table;
2903 /* Head and tail of call_arg_loc chain. */
2904 static GTY (()) struct call_arg_loc_node *call_arg_locations;
2905 static struct call_arg_loc_node *call_arg_loc_last;
2907 /* Number of call sites in the current function. */
2908 static int call_site_count = -1;
2909 /* Number of tail call sites in the current function. */
2910 static int tail_call_site_count = -1;
2912 /* Vector mapping block numbers to DW_TAG_{lexical_block,inlined_subroutine}
2913 DIEs. */
2914 static vec<dw_die_ref> block_map;
2916 /* A cached location list. */
2917 struct GTY ((for_user)) cached_dw_loc_list_def {
2918 /* The DECL_UID of the decl that this entry describes. */
2919 unsigned int decl_id;
2921 /* The cached location list. */
2922 dw_loc_list_ref loc_list;
2924 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
2926 struct dw_loc_list_hasher : ggc_hasher<cached_dw_loc_list *>
2929 typedef const_tree compare_type;
2931 static hashval_t hash (cached_dw_loc_list *);
2932 static bool equal (cached_dw_loc_list *, const_tree);
2935 /* Table of cached location lists. */
2936 static GTY (()) hash_table<dw_loc_list_hasher> *cached_dw_loc_list_table;
2938 /* A pointer to the base of a list of references to DIE's that
2939 are uniquely identified by their tag, presence/absence of
2940 children DIE's, and list of attribute/value pairs. */
2941 static GTY((length ("abbrev_die_table_allocated")))
2942 dw_die_ref *abbrev_die_table;
2944 /* Number of elements currently allocated for abbrev_die_table. */
2945 static GTY(()) unsigned abbrev_die_table_allocated;
2947 /* Number of elements in type_die_table currently in use. */
2948 static GTY(()) unsigned abbrev_die_table_in_use;
2950 /* Size (in elements) of increments by which we may expand the
2951 abbrev_die_table. */
2952 #define ABBREV_DIE_TABLE_INCREMENT 256
2954 /* A global counter for generating labels for line number data. */
2955 static unsigned int line_info_label_num;
2957 /* The current table to which we should emit line number information
2958 for the current function. This will be set up at the beginning of
2959 assembly for the function. */
2960 static dw_line_info_table *cur_line_info_table;
2962 /* The two default tables of line number info. */
2963 static GTY(()) dw_line_info_table *text_section_line_info;
2964 static GTY(()) dw_line_info_table *cold_text_section_line_info;
2966 /* The set of all non-default tables of line number info. */
2967 static GTY(()) vec<dw_line_info_table_p, va_gc> *separate_line_info;
2969 /* A flag to tell pubnames/types export if there is an info section to
2970 refer to. */
2971 static bool info_section_emitted;
2973 /* A pointer to the base of a table that contains a list of publicly
2974 accessible names. */
2975 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
2977 /* A pointer to the base of a table that contains a list of publicly
2978 accessible types. */
2979 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
2981 /* A pointer to the base of a table that contains a list of macro
2982 defines/undefines (and file start/end markers). */
2983 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
2985 /* True if .debug_macinfo or .debug_macros section is going to be
2986 emitted. */
2987 #define have_macinfo \
2988 (debug_info_level >= DINFO_LEVEL_VERBOSE \
2989 && !macinfo_table->is_empty ())
2991 /* Array of dies for which we should generate .debug_ranges info. */
2992 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
2994 /* Number of elements currently allocated for ranges_table. */
2995 static GTY(()) unsigned ranges_table_allocated;
2997 /* Number of elements in ranges_table currently in use. */
2998 static GTY(()) unsigned ranges_table_in_use;
3000 /* Array of pairs of labels referenced in ranges_table. */
3001 static GTY ((length ("ranges_by_label_allocated")))
3002 dw_ranges_by_label_ref ranges_by_label;
3004 /* Number of elements currently allocated for ranges_by_label. */
3005 static GTY(()) unsigned ranges_by_label_allocated;
3007 /* Number of elements in ranges_by_label currently in use. */
3008 static GTY(()) unsigned ranges_by_label_in_use;
3010 /* Size (in elements) of increments by which we may expand the
3011 ranges_table. */
3012 #define RANGES_TABLE_INCREMENT 64
3014 /* Whether we have location lists that need outputting */
3015 static GTY(()) bool have_location_lists;
3017 /* Unique label counter. */
3018 static GTY(()) unsigned int loclabel_num;
3020 /* Unique label counter for point-of-call tables. */
3021 static GTY(()) unsigned int poc_label_num;
3023 /* The last file entry emitted by maybe_emit_file(). */
3024 static GTY(()) struct dwarf_file_data * last_emitted_file;
3026 /* Number of internal labels generated by gen_internal_sym(). */
3027 static GTY(()) int label_num;
3029 /* Cached result of previous call to lookup_filename. */
3030 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
3032 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
3034 /* Instances of generic types for which we need to generate debug
3035 info that describe their generic parameters and arguments. That
3036 generation needs to happen once all types are properly laid out so
3037 we do it at the end of compilation. */
3038 static GTY(()) vec<tree, va_gc> *generic_type_instances;
3040 /* Offset from the "steady-state frame pointer" to the frame base,
3041 within the current function. */
3042 static HOST_WIDE_INT frame_pointer_fb_offset;
3043 static bool frame_pointer_fb_offset_valid;
3045 static vec<dw_die_ref> base_types;
3047 /* Flags to represent a set of attribute classes for attributes that represent
3048 a scalar value (bounds, pointers, ...). */
3049 enum dw_scalar_form
3051 dw_scalar_form_constant = 0x01,
3052 dw_scalar_form_exprloc = 0x02,
3053 dw_scalar_form_reference = 0x04
3056 /* Forward declarations for functions defined in this file. */
3058 static int is_pseudo_reg (const_rtx);
3059 static tree type_main_variant (tree);
3060 static int is_tagged_type (const_tree);
3061 static const char *dwarf_tag_name (unsigned);
3062 static const char *dwarf_attr_name (unsigned);
3063 static const char *dwarf_form_name (unsigned);
3064 static tree decl_ultimate_origin (const_tree);
3065 static tree decl_class_context (tree);
3066 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
3067 static inline enum dw_val_class AT_class (dw_attr_ref);
3068 static inline unsigned int AT_index (dw_attr_ref);
3069 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3070 static inline unsigned AT_flag (dw_attr_ref);
3071 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3072 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
3073 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3074 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
3075 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3076 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3077 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3078 unsigned int, unsigned char *);
3079 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3080 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3081 static inline const char *AT_string (dw_attr_ref);
3082 static enum dwarf_form AT_string_form (dw_attr_ref);
3083 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3084 static void add_AT_specification (dw_die_ref, dw_die_ref);
3085 static inline dw_die_ref AT_ref (dw_attr_ref);
3086 static inline int AT_ref_external (dw_attr_ref);
3087 static inline void set_AT_ref_external (dw_attr_ref, int);
3088 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3089 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3090 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
3091 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3092 dw_loc_list_ref);
3093 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
3094 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3095 static void remove_addr_table_entry (addr_table_entry *);
3096 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3097 static inline rtx AT_addr (dw_attr_ref);
3098 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3099 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3100 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3101 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3102 unsigned HOST_WIDE_INT);
3103 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3104 unsigned long, bool);
3105 static inline const char *AT_lbl (dw_attr_ref);
3106 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
3107 static const char *get_AT_low_pc (dw_die_ref);
3108 static const char *get_AT_hi_pc (dw_die_ref);
3109 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3110 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3111 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3112 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3113 static bool is_cxx (void);
3114 static bool is_fortran (void);
3115 static bool is_ada (void);
3116 static void remove_AT (dw_die_ref, enum dwarf_attribute);
3117 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3118 static void add_child_die (dw_die_ref, dw_die_ref);
3119 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3120 static dw_die_ref lookup_type_die (tree);
3121 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3122 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3123 static void equate_type_number_to_die (tree, dw_die_ref);
3124 static dw_die_ref lookup_decl_die (tree);
3125 static var_loc_list *lookup_decl_loc (const_tree);
3126 static void equate_decl_number_to_die (tree, dw_die_ref);
3127 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3128 static void print_spaces (FILE *);
3129 static void print_die (dw_die_ref, FILE *);
3130 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3131 static dw_die_ref pop_compile_unit (dw_die_ref);
3132 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3133 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
3134 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3135 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3136 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3137 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3138 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
3139 struct md5_ctx *, int *);
3140 struct checksum_attributes;
3141 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3142 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3143 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3144 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3145 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3146 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3147 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
3148 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3149 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3150 static void compute_section_prefix (dw_die_ref);
3151 static int is_type_die (dw_die_ref);
3152 static int is_comdat_die (dw_die_ref);
3153 static int is_symbol_die (dw_die_ref);
3154 static inline bool is_template_instantiation (dw_die_ref);
3155 static void assign_symbol_names (dw_die_ref);
3156 static void break_out_includes (dw_die_ref);
3157 static int is_declaration_die (dw_die_ref);
3158 static int should_move_die_to_comdat (dw_die_ref);
3159 static dw_die_ref clone_as_declaration (dw_die_ref);
3160 static dw_die_ref clone_die (dw_die_ref);
3161 static dw_die_ref clone_tree (dw_die_ref);
3162 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3163 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3164 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3165 static dw_die_ref generate_skeleton (dw_die_ref);
3166 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3167 dw_die_ref,
3168 dw_die_ref);
3169 static void break_out_comdat_types (dw_die_ref);
3170 static void copy_decls_for_unworthy_types (dw_die_ref);
3172 static void add_sibling_attributes (dw_die_ref);
3173 static void output_location_lists (dw_die_ref);
3174 static int constant_size (unsigned HOST_WIDE_INT);
3175 static unsigned long size_of_die (dw_die_ref);
3176 static void calc_die_sizes (dw_die_ref);
3177 static void calc_base_type_die_sizes (void);
3178 static void mark_dies (dw_die_ref);
3179 static void unmark_dies (dw_die_ref);
3180 static void unmark_all_dies (dw_die_ref);
3181 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3182 static unsigned long size_of_aranges (void);
3183 static enum dwarf_form value_format (dw_attr_ref);
3184 static void output_value_format (dw_attr_ref);
3185 static void output_abbrev_section (void);
3186 static void output_die_abbrevs (unsigned long, dw_die_ref);
3187 static void output_die_symbol (dw_die_ref);
3188 static void output_die (dw_die_ref);
3189 static void output_compilation_unit_header (void);
3190 static void output_comp_unit (dw_die_ref, int);
3191 static void output_comdat_type_unit (comdat_type_node *);
3192 static const char *dwarf2_name (tree, int);
3193 static void add_pubname (tree, dw_die_ref);
3194 static void add_enumerator_pubname (const char *, dw_die_ref);
3195 static void add_pubname_string (const char *, dw_die_ref);
3196 static void add_pubtype (tree, dw_die_ref);
3197 static void output_pubnames (vec<pubname_entry, va_gc> *);
3198 static void output_aranges (unsigned long);
3199 static unsigned int add_ranges_num (int);
3200 static unsigned int add_ranges (const_tree);
3201 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3202 bool *, bool);
3203 static void output_ranges (void);
3204 static dw_line_info_table *new_line_info_table (void);
3205 static void output_line_info (bool);
3206 static void output_file_names (void);
3207 static dw_die_ref base_type_die (tree);
3208 static int is_base_type (tree);
3209 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
3210 static int decl_quals (const_tree);
3211 static dw_die_ref modified_type_die (tree, int, dw_die_ref);
3212 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3213 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3214 static int type_is_enum (const_tree);
3215 static unsigned int dbx_reg_number (const_rtx);
3216 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3217 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3218 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3219 enum var_init_status);
3220 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3221 enum var_init_status);
3222 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3223 enum var_init_status);
3224 static int is_based_loc (const_rtx);
3225 static bool resolve_one_addr (rtx *);
3226 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3227 enum var_init_status);
3228 static dw_loc_descr_ref loc_descriptor (rtx, machine_mode mode,
3229 enum var_init_status);
3230 struct loc_descr_context;
3231 static dw_loc_list_ref loc_list_from_tree (tree, int,
3232 const struct loc_descr_context *);
3233 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int,
3234 const struct loc_descr_context *);
3235 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3236 static tree field_type (const_tree);
3237 static unsigned int simple_type_align_in_bits (const_tree);
3238 static unsigned int simple_decl_align_in_bits (const_tree);
3239 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3240 static HOST_WIDE_INT field_byte_offset (const_tree);
3241 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3242 dw_loc_list_ref);
3243 static void add_data_member_location_attribute (dw_die_ref, tree);
3244 static bool add_const_value_attribute (dw_die_ref, rtx);
3245 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3246 static void insert_wide_int (const wide_int &, unsigned char *, int);
3247 static void insert_float (const_rtx, unsigned char *);
3248 static rtx rtl_for_decl_location (tree);
3249 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool,
3250 enum dwarf_attribute);
3251 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3252 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3253 static void add_name_attribute (dw_die_ref, const char *);
3254 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3255 static void add_comp_dir_attribute (dw_die_ref);
3256 static void add_scalar_info (dw_die_ref, enum dwarf_attribute, tree, int,
3257 const struct loc_descr_context *);
3258 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree,
3259 const struct loc_descr_context *);
3260 static void add_subscript_info (dw_die_ref, tree, bool);
3261 static void add_byte_size_attribute (dw_die_ref, tree);
3262 static void add_bit_offset_attribute (dw_die_ref, tree);
3263 static void add_bit_size_attribute (dw_die_ref, tree);
3264 static void add_prototyped_attribute (dw_die_ref, tree);
3265 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3266 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3267 static void add_src_coords_attributes (dw_die_ref, tree);
3268 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3269 static void push_decl_scope (tree);
3270 static void pop_decl_scope (void);
3271 static dw_die_ref scope_die_for (tree, dw_die_ref);
3272 static inline int local_scope_p (dw_die_ref);
3273 static inline int class_scope_p (dw_die_ref);
3274 static inline int class_or_namespace_scope_p (dw_die_ref);
3275 static void add_type_attribute (dw_die_ref, tree, int, dw_die_ref);
3276 static void add_calling_convention_attribute (dw_die_ref, tree);
3277 static const char *type_tag (const_tree);
3278 static tree member_declared_type (const_tree);
3279 #if 0
3280 static const char *decl_start_label (tree);
3281 #endif
3282 static void gen_array_type_die (tree, dw_die_ref);
3283 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3284 #if 0
3285 static void gen_entry_point_die (tree, dw_die_ref);
3286 #endif
3287 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3288 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3289 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3290 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3291 static void gen_formal_types_die (tree, dw_die_ref);
3292 static void gen_subprogram_die (tree, dw_die_ref);
3293 static void gen_variable_die (tree, tree, dw_die_ref);
3294 static void gen_const_die (tree, dw_die_ref);
3295 static void gen_label_die (tree, dw_die_ref);
3296 static void gen_lexical_block_die (tree, dw_die_ref);
3297 static void gen_inlined_subroutine_die (tree, dw_die_ref);
3298 static void gen_field_die (tree, dw_die_ref);
3299 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3300 static dw_die_ref gen_compile_unit_die (const char *);
3301 static void gen_inheritance_die (tree, tree, dw_die_ref);
3302 static void gen_member_die (tree, dw_die_ref);
3303 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3304 enum debug_info_usage);
3305 static void gen_subroutine_type_die (tree, dw_die_ref);
3306 static void gen_typedef_die (tree, dw_die_ref);
3307 static void gen_type_die (tree, dw_die_ref);
3308 static void gen_block_die (tree, dw_die_ref);
3309 static void decls_for_scope (tree, dw_die_ref);
3310 static inline int is_redundant_typedef (const_tree);
3311 static bool is_naming_typedef_decl (const_tree);
3312 static inline dw_die_ref get_context_die (tree);
3313 static void gen_namespace_die (tree, dw_die_ref);
3314 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3315 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
3316 static dw_die_ref force_decl_die (tree);
3317 static dw_die_ref force_type_die (tree);
3318 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3319 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3320 static struct dwarf_file_data * lookup_filename (const char *);
3321 static void retry_incomplete_types (void);
3322 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3323 static void gen_generic_params_dies (tree);
3324 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3325 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3326 static void splice_child_die (dw_die_ref, dw_die_ref);
3327 static int file_info_cmp (const void *, const void *);
3328 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3329 const char *, const char *);
3330 static void output_loc_list (dw_loc_list_ref);
3331 static char *gen_internal_sym (const char *);
3332 static bool want_pubnames (void);
3334 static void prune_unmark_dies (dw_die_ref);
3335 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3336 static void prune_unused_types_mark (dw_die_ref, int);
3337 static void prune_unused_types_walk (dw_die_ref);
3338 static void prune_unused_types_walk_attribs (dw_die_ref);
3339 static void prune_unused_types_prune (dw_die_ref);
3340 static void prune_unused_types (void);
3341 static int maybe_emit_file (struct dwarf_file_data *fd);
3342 static inline const char *AT_vms_delta1 (dw_attr_ref);
3343 static inline const char *AT_vms_delta2 (dw_attr_ref);
3344 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3345 const char *, const char *);
3346 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3347 static void gen_remaining_tmpl_value_param_die_attribute (void);
3348 static bool generic_type_p (tree);
3349 static void schedule_generic_params_dies_gen (tree t);
3350 static void gen_scheduled_generic_parms_dies (void);
3352 static const char *comp_dir_string (void);
3354 static void hash_loc_operands (dw_loc_descr_ref, inchash::hash &);
3356 /* enum for tracking thread-local variables whose address is really an offset
3357 relative to the TLS pointer, which will need link-time relocation, but will
3358 not need relocation by the DWARF consumer. */
3360 enum dtprel_bool
3362 dtprel_false = 0,
3363 dtprel_true = 1
3366 /* Return the operator to use for an address of a variable. For dtprel_true, we
3367 use DW_OP_const*. For regular variables, which need both link-time
3368 relocation and consumer-level relocation (e.g., to account for shared objects
3369 loaded at a random address), we use DW_OP_addr*. */
3371 static inline enum dwarf_location_atom
3372 dw_addr_op (enum dtprel_bool dtprel)
3374 if (dtprel == dtprel_true)
3375 return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3376 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3377 else
3378 return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3381 /* Return a pointer to a newly allocated address location description. If
3382 dwarf_split_debug_info is true, then record the address with the appropriate
3383 relocation. */
3384 static inline dw_loc_descr_ref
3385 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3387 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3389 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3390 ref->dw_loc_oprnd1.v.val_addr = addr;
3391 ref->dtprel = dtprel;
3392 if (dwarf_split_debug_info)
3393 ref->dw_loc_oprnd1.val_entry
3394 = add_addr_table_entry (addr,
3395 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3396 else
3397 ref->dw_loc_oprnd1.val_entry = NULL;
3399 return ref;
3402 /* Section names used to hold DWARF debugging information. */
3404 #ifndef DEBUG_INFO_SECTION
3405 #define DEBUG_INFO_SECTION ".debug_info"
3406 #endif
3407 #ifndef DEBUG_DWO_INFO_SECTION
3408 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3409 #endif
3410 #ifndef DEBUG_ABBREV_SECTION
3411 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3412 #endif
3413 #ifndef DEBUG_DWO_ABBREV_SECTION
3414 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3415 #endif
3416 #ifndef DEBUG_ARANGES_SECTION
3417 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3418 #endif
3419 #ifndef DEBUG_ADDR_SECTION
3420 #define DEBUG_ADDR_SECTION ".debug_addr"
3421 #endif
3422 #ifndef DEBUG_NORM_MACINFO_SECTION
3423 #define DEBUG_NORM_MACINFO_SECTION ".debug_macinfo"
3424 #endif
3425 #ifndef DEBUG_DWO_MACINFO_SECTION
3426 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
3427 #endif
3428 #ifndef DEBUG_MACINFO_SECTION
3429 #define DEBUG_MACINFO_SECTION \
3430 (!dwarf_split_debug_info \
3431 ? (DEBUG_NORM_MACINFO_SECTION) : (DEBUG_DWO_MACINFO_SECTION))
3432 #endif
3433 #ifndef DEBUG_NORM_MACRO_SECTION
3434 #define DEBUG_NORM_MACRO_SECTION ".debug_macro"
3435 #endif
3436 #ifndef DEBUG_DWO_MACRO_SECTION
3437 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
3438 #endif
3439 #ifndef DEBUG_MACRO_SECTION
3440 #define DEBUG_MACRO_SECTION \
3441 (!dwarf_split_debug_info \
3442 ? (DEBUG_NORM_MACRO_SECTION) : (DEBUG_DWO_MACRO_SECTION))
3443 #endif
3444 #ifndef DEBUG_LINE_SECTION
3445 #define DEBUG_LINE_SECTION ".debug_line"
3446 #endif
3447 #ifndef DEBUG_DWO_LINE_SECTION
3448 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3449 #endif
3450 #ifndef DEBUG_LOC_SECTION
3451 #define DEBUG_LOC_SECTION ".debug_loc"
3452 #endif
3453 #ifndef DEBUG_DWO_LOC_SECTION
3454 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
3455 #endif
3456 #ifndef DEBUG_PUBNAMES_SECTION
3457 #define DEBUG_PUBNAMES_SECTION \
3458 ((debug_generate_pub_sections == 2) \
3459 ? ".debug_gnu_pubnames" : ".debug_pubnames")
3460 #endif
3461 #ifndef DEBUG_PUBTYPES_SECTION
3462 #define DEBUG_PUBTYPES_SECTION \
3463 ((debug_generate_pub_sections == 2) \
3464 ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
3465 #endif
3466 #define DEBUG_NORM_STR_OFFSETS_SECTION ".debug_str_offsets"
3467 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3468 #ifndef DEBUG_STR_OFFSETS_SECTION
3469 #define DEBUG_STR_OFFSETS_SECTION \
3470 (!dwarf_split_debug_info \
3471 ? (DEBUG_NORM_STR_OFFSETS_SECTION) : (DEBUG_DWO_STR_OFFSETS_SECTION))
3472 #endif
3473 #ifndef DEBUG_STR_DWO_SECTION
3474 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
3475 #endif
3476 #ifndef DEBUG_STR_SECTION
3477 #define DEBUG_STR_SECTION ".debug_str"
3478 #endif
3479 #ifndef DEBUG_RANGES_SECTION
3480 #define DEBUG_RANGES_SECTION ".debug_ranges"
3481 #endif
3483 /* Standard ELF section names for compiled code and data. */
3484 #ifndef TEXT_SECTION_NAME
3485 #define TEXT_SECTION_NAME ".text"
3486 #endif
3488 /* Section flags for .debug_macinfo/.debug_macro section. */
3489 #define DEBUG_MACRO_SECTION_FLAGS \
3490 (dwarf_split_debug_info ? SECTION_DEBUG | SECTION_EXCLUDE : SECTION_DEBUG)
3492 /* Section flags for .debug_str section. */
3493 #define DEBUG_STR_SECTION_FLAGS \
3494 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
3495 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3496 : SECTION_DEBUG)
3498 /* Section flags for .debug_str.dwo section. */
3499 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
3501 /* Labels we insert at beginning sections we can reference instead of
3502 the section names themselves. */
3504 #ifndef TEXT_SECTION_LABEL
3505 #define TEXT_SECTION_LABEL "Ltext"
3506 #endif
3507 #ifndef COLD_TEXT_SECTION_LABEL
3508 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
3509 #endif
3510 #ifndef DEBUG_LINE_SECTION_LABEL
3511 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3512 #endif
3513 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3514 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
3515 #endif
3516 #ifndef DEBUG_INFO_SECTION_LABEL
3517 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3518 #endif
3519 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3520 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
3521 #endif
3522 #ifndef DEBUG_ABBREV_SECTION_LABEL
3523 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3524 #endif
3525 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3526 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3527 #endif
3528 #ifndef DEBUG_ADDR_SECTION_LABEL
3529 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
3530 #endif
3531 #ifndef DEBUG_LOC_SECTION_LABEL
3532 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3533 #endif
3534 #ifndef DEBUG_RANGES_SECTION_LABEL
3535 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3536 #endif
3537 #ifndef DEBUG_MACINFO_SECTION_LABEL
3538 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3539 #endif
3540 #ifndef DEBUG_MACRO_SECTION_LABEL
3541 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
3542 #endif
3543 #define SKELETON_COMP_DIE_ABBREV 1
3544 #define SKELETON_TYPE_DIE_ABBREV 2
3546 /* Definitions of defaults for formats and names of various special
3547 (artificial) labels which may be generated within this file (when the -g
3548 options is used and DWARF2_DEBUGGING_INFO is in effect.
3549 If necessary, these may be overridden from within the tm.h file, but
3550 typically, overriding these defaults is unnecessary. */
3552 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3553 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3554 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3555 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3556 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3557 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3558 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3559 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3560 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3561 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3562 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3563 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3564 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3565 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3567 #ifndef TEXT_END_LABEL
3568 #define TEXT_END_LABEL "Letext"
3569 #endif
3570 #ifndef COLD_END_LABEL
3571 #define COLD_END_LABEL "Letext_cold"
3572 #endif
3573 #ifndef BLOCK_BEGIN_LABEL
3574 #define BLOCK_BEGIN_LABEL "LBB"
3575 #endif
3576 #ifndef BLOCK_END_LABEL
3577 #define BLOCK_END_LABEL "LBE"
3578 #endif
3579 #ifndef LINE_CODE_LABEL
3580 #define LINE_CODE_LABEL "LM"
3581 #endif
3584 /* Return the root of the DIE's built for the current compilation unit. */
3585 static dw_die_ref
3586 comp_unit_die (void)
3588 if (!single_comp_unit_die)
3589 single_comp_unit_die = gen_compile_unit_die (NULL);
3590 return single_comp_unit_die;
3593 /* We allow a language front-end to designate a function that is to be
3594 called to "demangle" any name before it is put into a DIE. */
3596 static const char *(*demangle_name_func) (const char *);
3598 void
3599 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3601 demangle_name_func = func;
3604 /* Test if rtl node points to a pseudo register. */
3606 static inline int
3607 is_pseudo_reg (const_rtx rtl)
3609 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3610 || (GET_CODE (rtl) == SUBREG
3611 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3614 /* Return a reference to a type, with its const and volatile qualifiers
3615 removed. */
3617 static inline tree
3618 type_main_variant (tree type)
3620 type = TYPE_MAIN_VARIANT (type);
3622 /* ??? There really should be only one main variant among any group of
3623 variants of a given type (and all of the MAIN_VARIANT values for all
3624 members of the group should point to that one type) but sometimes the C
3625 front-end messes this up for array types, so we work around that bug
3626 here. */
3627 if (TREE_CODE (type) == ARRAY_TYPE)
3628 while (type != TYPE_MAIN_VARIANT (type))
3629 type = TYPE_MAIN_VARIANT (type);
3631 return type;
3634 /* Return nonzero if the given type node represents a tagged type. */
3636 static inline int
3637 is_tagged_type (const_tree type)
3639 enum tree_code code = TREE_CODE (type);
3641 return (code == RECORD_TYPE || code == UNION_TYPE
3642 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3645 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
3647 static void
3648 get_ref_die_offset_label (char *label, dw_die_ref ref)
3650 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3653 /* Return die_offset of a DIE reference to a base type. */
3655 static unsigned long int
3656 get_base_type_offset (dw_die_ref ref)
3658 if (ref->die_offset)
3659 return ref->die_offset;
3660 if (comp_unit_die ()->die_abbrev)
3662 calc_base_type_die_sizes ();
3663 gcc_assert (ref->die_offset);
3665 return ref->die_offset;
3668 /* Return die_offset of a DIE reference other than base type. */
3670 static unsigned long int
3671 get_ref_die_offset (dw_die_ref ref)
3673 gcc_assert (ref->die_offset);
3674 return ref->die_offset;
3677 /* Convert a DIE tag into its string name. */
3679 static const char *
3680 dwarf_tag_name (unsigned int tag)
3682 const char *name = get_DW_TAG_name (tag);
3684 if (name != NULL)
3685 return name;
3687 return "DW_TAG_<unknown>";
3690 /* Convert a DWARF attribute code into its string name. */
3692 static const char *
3693 dwarf_attr_name (unsigned int attr)
3695 const char *name;
3697 switch (attr)
3699 #if VMS_DEBUGGING_INFO
3700 case DW_AT_HP_prologue:
3701 return "DW_AT_HP_prologue";
3702 #else
3703 case DW_AT_MIPS_loop_unroll_factor:
3704 return "DW_AT_MIPS_loop_unroll_factor";
3705 #endif
3707 #if VMS_DEBUGGING_INFO
3708 case DW_AT_HP_epilogue:
3709 return "DW_AT_HP_epilogue";
3710 #else
3711 case DW_AT_MIPS_stride:
3712 return "DW_AT_MIPS_stride";
3713 #endif
3716 name = get_DW_AT_name (attr);
3718 if (name != NULL)
3719 return name;
3721 return "DW_AT_<unknown>";
3724 /* Convert a DWARF value form code into its string name. */
3726 static const char *
3727 dwarf_form_name (unsigned int form)
3729 const char *name = get_DW_FORM_name (form);
3731 if (name != NULL)
3732 return name;
3734 return "DW_FORM_<unknown>";
3737 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
3738 instance of an inlined instance of a decl which is local to an inline
3739 function, so we have to trace all of the way back through the origin chain
3740 to find out what sort of node actually served as the original seed for the
3741 given block. */
3743 static tree
3744 decl_ultimate_origin (const_tree decl)
3746 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
3747 return NULL_TREE;
3749 /* DECL_ABSTRACT_ORIGIN can point to itself; ignore that if
3750 we're trying to output the abstract instance of this function. */
3751 if (DECL_ABSTRACT_P (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
3752 return NULL_TREE;
3754 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
3755 most distant ancestor, this should never happen. */
3756 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
3758 return DECL_ABSTRACT_ORIGIN (decl);
3761 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
3762 of a virtual function may refer to a base class, so we check the 'this'
3763 parameter. */
3765 static tree
3766 decl_class_context (tree decl)
3768 tree context = NULL_TREE;
3770 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
3771 context = DECL_CONTEXT (decl);
3772 else
3773 context = TYPE_MAIN_VARIANT
3774 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
3776 if (context && !TYPE_P (context))
3777 context = NULL_TREE;
3779 return context;
3782 /* Add an attribute/value pair to a DIE. */
3784 static inline void
3785 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
3787 /* Maybe this should be an assert? */
3788 if (die == NULL)
3789 return;
3791 vec_safe_reserve (die->die_attr, 1);
3792 vec_safe_push (die->die_attr, *attr);
3795 static inline enum dw_val_class
3796 AT_class (dw_attr_ref a)
3798 return a->dw_attr_val.val_class;
3801 /* Return the index for any attribute that will be referenced with a
3802 DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index. String indices
3803 are stored in dw_attr_val.v.val_str for reference counting
3804 pruning. */
3806 static inline unsigned int
3807 AT_index (dw_attr_ref a)
3809 if (AT_class (a) == dw_val_class_str)
3810 return a->dw_attr_val.v.val_str->index;
3811 else if (a->dw_attr_val.val_entry != NULL)
3812 return a->dw_attr_val.val_entry->index;
3813 return NOT_INDEXED;
3816 /* Add a flag value attribute to a DIE. */
3818 static inline void
3819 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
3821 dw_attr_node attr;
3823 attr.dw_attr = attr_kind;
3824 attr.dw_attr_val.val_class = dw_val_class_flag;
3825 attr.dw_attr_val.val_entry = NULL;
3826 attr.dw_attr_val.v.val_flag = flag;
3827 add_dwarf_attr (die, &attr);
3830 static inline unsigned
3831 AT_flag (dw_attr_ref a)
3833 gcc_assert (a && AT_class (a) == dw_val_class_flag);
3834 return a->dw_attr_val.v.val_flag;
3837 /* Add a signed integer attribute value to a DIE. */
3839 static inline void
3840 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
3842 dw_attr_node attr;
3844 attr.dw_attr = attr_kind;
3845 attr.dw_attr_val.val_class = dw_val_class_const;
3846 attr.dw_attr_val.val_entry = NULL;
3847 attr.dw_attr_val.v.val_int = int_val;
3848 add_dwarf_attr (die, &attr);
3851 static inline HOST_WIDE_INT
3852 AT_int (dw_attr_ref a)
3854 gcc_assert (a && AT_class (a) == dw_val_class_const);
3855 return a->dw_attr_val.v.val_int;
3858 /* Add an unsigned integer attribute value to a DIE. */
3860 static inline void
3861 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
3862 unsigned HOST_WIDE_INT unsigned_val)
3864 dw_attr_node attr;
3866 attr.dw_attr = attr_kind;
3867 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
3868 attr.dw_attr_val.val_entry = NULL;
3869 attr.dw_attr_val.v.val_unsigned = unsigned_val;
3870 add_dwarf_attr (die, &attr);
3873 static inline unsigned HOST_WIDE_INT
3874 AT_unsigned (dw_attr_ref a)
3876 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
3877 return a->dw_attr_val.v.val_unsigned;
3880 /* Add an unsigned wide integer attribute value to a DIE. */
3882 static inline void
3883 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
3884 const wide_int& w)
3886 dw_attr_node attr;
3888 attr.dw_attr = attr_kind;
3889 attr.dw_attr_val.val_class = dw_val_class_wide_int;
3890 attr.dw_attr_val.val_entry = NULL;
3891 attr.dw_attr_val.v.val_wide = ggc_alloc<wide_int> ();
3892 *attr.dw_attr_val.v.val_wide = w;
3893 add_dwarf_attr (die, &attr);
3896 /* Add an unsigned double integer attribute value to a DIE. */
3898 static inline void
3899 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
3900 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
3902 dw_attr_node attr;
3904 attr.dw_attr = attr_kind;
3905 attr.dw_attr_val.val_class = dw_val_class_const_double;
3906 attr.dw_attr_val.val_entry = NULL;
3907 attr.dw_attr_val.v.val_double.high = high;
3908 attr.dw_attr_val.v.val_double.low = low;
3909 add_dwarf_attr (die, &attr);
3912 /* Add a floating point attribute value to a DIE and return it. */
3914 static inline void
3915 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
3916 unsigned int length, unsigned int elt_size, unsigned char *array)
3918 dw_attr_node attr;
3920 attr.dw_attr = attr_kind;
3921 attr.dw_attr_val.val_class = dw_val_class_vec;
3922 attr.dw_attr_val.val_entry = NULL;
3923 attr.dw_attr_val.v.val_vec.length = length;
3924 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
3925 attr.dw_attr_val.v.val_vec.array = array;
3926 add_dwarf_attr (die, &attr);
3929 /* Add an 8-byte data attribute value to a DIE. */
3931 static inline void
3932 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
3933 unsigned char data8[8])
3935 dw_attr_node attr;
3937 attr.dw_attr = attr_kind;
3938 attr.dw_attr_val.val_class = dw_val_class_data8;
3939 attr.dw_attr_val.val_entry = NULL;
3940 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
3941 add_dwarf_attr (die, &attr);
3944 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
3945 dwarf_split_debug_info, address attributes in dies destined for the
3946 final executable have force_direct set to avoid using indexed
3947 references. */
3949 static inline void
3950 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
3951 bool force_direct)
3953 dw_attr_node attr;
3954 char * lbl_id;
3956 lbl_id = xstrdup (lbl_low);
3957 attr.dw_attr = DW_AT_low_pc;
3958 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3959 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3960 if (dwarf_split_debug_info && !force_direct)
3961 attr.dw_attr_val.val_entry
3962 = add_addr_table_entry (lbl_id, ate_kind_label);
3963 else
3964 attr.dw_attr_val.val_entry = NULL;
3965 add_dwarf_attr (die, &attr);
3967 attr.dw_attr = DW_AT_high_pc;
3968 if (dwarf_version < 4)
3969 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3970 else
3971 attr.dw_attr_val.val_class = dw_val_class_high_pc;
3972 lbl_id = xstrdup (lbl_high);
3973 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3974 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
3975 && dwarf_split_debug_info && !force_direct)
3976 attr.dw_attr_val.val_entry
3977 = add_addr_table_entry (lbl_id, ate_kind_label);
3978 else
3979 attr.dw_attr_val.val_entry = NULL;
3980 add_dwarf_attr (die, &attr);
3983 /* Hash and equality functions for debug_str_hash. */
3985 hashval_t
3986 indirect_string_hasher::hash (indirect_string_node *x)
3988 return htab_hash_string (x->str);
3991 bool
3992 indirect_string_hasher::equal (indirect_string_node *x1, const char *x2)
3994 return strcmp (x1->str, x2) == 0;
3997 /* Add STR to the given string hash table. */
3999 static struct indirect_string_node *
4000 find_AT_string_in_table (const char *str,
4001 hash_table<indirect_string_hasher> *table)
4003 struct indirect_string_node *node;
4005 indirect_string_node **slot
4006 = table->find_slot_with_hash (str, htab_hash_string (str), INSERT);
4007 if (*slot == NULL)
4009 node = ggc_cleared_alloc<indirect_string_node> ();
4010 node->str = ggc_strdup (str);
4011 *slot = node;
4013 else
4014 node = *slot;
4016 node->refcount++;
4017 return node;
4020 /* Add STR to the indirect string hash table. */
4022 static struct indirect_string_node *
4023 find_AT_string (const char *str)
4025 if (! debug_str_hash)
4026 debug_str_hash = hash_table<indirect_string_hasher>::create_ggc (10);
4028 return find_AT_string_in_table (str, debug_str_hash);
4031 /* Add a string attribute value to a DIE. */
4033 static inline void
4034 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4036 dw_attr_node attr;
4037 struct indirect_string_node *node;
4039 node = find_AT_string (str);
4041 attr.dw_attr = attr_kind;
4042 attr.dw_attr_val.val_class = dw_val_class_str;
4043 attr.dw_attr_val.val_entry = NULL;
4044 attr.dw_attr_val.v.val_str = node;
4045 add_dwarf_attr (die, &attr);
4048 static inline const char *
4049 AT_string (dw_attr_ref a)
4051 gcc_assert (a && AT_class (a) == dw_val_class_str);
4052 return a->dw_attr_val.v.val_str->str;
4055 /* Call this function directly to bypass AT_string_form's logic to put
4056 the string inline in the die. */
4058 static void
4059 set_indirect_string (struct indirect_string_node *node)
4061 char label[32];
4062 /* Already indirect is a no op. */
4063 if (node->form == DW_FORM_strp || node->form == DW_FORM_GNU_str_index)
4065 gcc_assert (node->label);
4066 return;
4068 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4069 ++dw2_string_counter;
4070 node->label = xstrdup (label);
4072 if (!dwarf_split_debug_info)
4074 node->form = DW_FORM_strp;
4075 node->index = NOT_INDEXED;
4077 else
4079 node->form = DW_FORM_GNU_str_index;
4080 node->index = NO_INDEX_ASSIGNED;
4084 /* Find out whether a string should be output inline in DIE
4085 or out-of-line in .debug_str section. */
4087 static enum dwarf_form
4088 find_string_form (struct indirect_string_node *node)
4090 unsigned int len;
4092 if (node->form)
4093 return node->form;
4095 len = strlen (node->str) + 1;
4097 /* If the string is shorter or equal to the size of the reference, it is
4098 always better to put it inline. */
4099 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4100 return node->form = DW_FORM_string;
4102 /* If we cannot expect the linker to merge strings in .debug_str
4103 section, only put it into .debug_str if it is worth even in this
4104 single module. */
4105 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4106 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4107 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
4108 return node->form = DW_FORM_string;
4110 set_indirect_string (node);
4112 return node->form;
4115 /* Find out whether the string referenced from the attribute should be
4116 output inline in DIE or out-of-line in .debug_str section. */
4118 static enum dwarf_form
4119 AT_string_form (dw_attr_ref a)
4121 gcc_assert (a && AT_class (a) == dw_val_class_str);
4122 return find_string_form (a->dw_attr_val.v.val_str);
4125 /* Add a DIE reference attribute value to a DIE. */
4127 static inline void
4128 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4130 dw_attr_node attr;
4132 #ifdef ENABLE_CHECKING
4133 gcc_assert (targ_die != NULL);
4134 #else
4135 /* With LTO we can end up trying to reference something we didn't create
4136 a DIE for. Avoid crashing later on a NULL referenced DIE. */
4137 if (targ_die == NULL)
4138 return;
4139 #endif
4141 attr.dw_attr = attr_kind;
4142 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4143 attr.dw_attr_val.val_entry = NULL;
4144 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4145 attr.dw_attr_val.v.val_die_ref.external = 0;
4146 add_dwarf_attr (die, &attr);
4149 /* Change DIE reference REF to point to NEW_DIE instead. */
4151 static inline void
4152 change_AT_die_ref (dw_attr_ref ref, dw_die_ref new_die)
4154 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4155 ref->dw_attr_val.v.val_die_ref.die = new_die;
4156 ref->dw_attr_val.v.val_die_ref.external = 0;
4159 /* Add an AT_specification attribute to a DIE, and also make the back
4160 pointer from the specification to the definition. */
4162 static inline void
4163 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4165 add_AT_die_ref (die, DW_AT_specification, targ_die);
4166 gcc_assert (!targ_die->die_definition);
4167 targ_die->die_definition = die;
4170 static inline dw_die_ref
4171 AT_ref (dw_attr_ref a)
4173 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4174 return a->dw_attr_val.v.val_die_ref.die;
4177 static inline int
4178 AT_ref_external (dw_attr_ref a)
4180 if (a && AT_class (a) == dw_val_class_die_ref)
4181 return a->dw_attr_val.v.val_die_ref.external;
4183 return 0;
4186 static inline void
4187 set_AT_ref_external (dw_attr_ref a, int i)
4189 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4190 a->dw_attr_val.v.val_die_ref.external = i;
4193 /* Add an FDE reference attribute value to a DIE. */
4195 static inline void
4196 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4198 dw_attr_node attr;
4200 attr.dw_attr = attr_kind;
4201 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4202 attr.dw_attr_val.val_entry = NULL;
4203 attr.dw_attr_val.v.val_fde_index = targ_fde;
4204 add_dwarf_attr (die, &attr);
4207 /* Add a location description attribute value to a DIE. */
4209 static inline void
4210 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4212 dw_attr_node attr;
4214 attr.dw_attr = attr_kind;
4215 attr.dw_attr_val.val_class = dw_val_class_loc;
4216 attr.dw_attr_val.val_entry = NULL;
4217 attr.dw_attr_val.v.val_loc = loc;
4218 add_dwarf_attr (die, &attr);
4221 static inline dw_loc_descr_ref
4222 AT_loc (dw_attr_ref a)
4224 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4225 return a->dw_attr_val.v.val_loc;
4228 static inline void
4229 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4231 dw_attr_node attr;
4233 attr.dw_attr = attr_kind;
4234 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4235 attr.dw_attr_val.val_entry = NULL;
4236 attr.dw_attr_val.v.val_loc_list = loc_list;
4237 add_dwarf_attr (die, &attr);
4238 have_location_lists = true;
4241 static inline dw_loc_list_ref
4242 AT_loc_list (dw_attr_ref a)
4244 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4245 return a->dw_attr_val.v.val_loc_list;
4248 static inline dw_loc_list_ref *
4249 AT_loc_list_ptr (dw_attr_ref a)
4251 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4252 return &a->dw_attr_val.v.val_loc_list;
4255 struct addr_hasher : ggc_hasher<addr_table_entry *>
4257 static hashval_t hash (addr_table_entry *);
4258 static bool equal (addr_table_entry *, addr_table_entry *);
4261 /* Table of entries into the .debug_addr section. */
4263 static GTY (()) hash_table<addr_hasher> *addr_index_table;
4265 /* Hash an address_table_entry. */
4267 hashval_t
4268 addr_hasher::hash (addr_table_entry *a)
4270 inchash::hash hstate;
4271 switch (a->kind)
4273 case ate_kind_rtx:
4274 hstate.add_int (0);
4275 break;
4276 case ate_kind_rtx_dtprel:
4277 hstate.add_int (1);
4278 break;
4279 case ate_kind_label:
4280 return htab_hash_string (a->addr.label);
4281 default:
4282 gcc_unreachable ();
4284 inchash::add_rtx (a->addr.rtl, hstate);
4285 return hstate.end ();
4288 /* Determine equality for two address_table_entries. */
4290 bool
4291 addr_hasher::equal (addr_table_entry *a1, addr_table_entry *a2)
4293 if (a1->kind != a2->kind)
4294 return 0;
4295 switch (a1->kind)
4297 case ate_kind_rtx:
4298 case ate_kind_rtx_dtprel:
4299 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4300 case ate_kind_label:
4301 return strcmp (a1->addr.label, a2->addr.label) == 0;
4302 default:
4303 gcc_unreachable ();
4307 /* Initialize an addr_table_entry. */
4309 void
4310 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4312 e->kind = kind;
4313 switch (kind)
4315 case ate_kind_rtx:
4316 case ate_kind_rtx_dtprel:
4317 e->addr.rtl = (rtx) addr;
4318 break;
4319 case ate_kind_label:
4320 e->addr.label = (char *) addr;
4321 break;
4323 e->refcount = 0;
4324 e->index = NO_INDEX_ASSIGNED;
4327 /* Add attr to the address table entry to the table. Defer setting an
4328 index until output time. */
4330 static addr_table_entry *
4331 add_addr_table_entry (void *addr, enum ate_kind kind)
4333 addr_table_entry *node;
4334 addr_table_entry finder;
4336 gcc_assert (dwarf_split_debug_info);
4337 if (! addr_index_table)
4338 addr_index_table = hash_table<addr_hasher>::create_ggc (10);
4339 init_addr_table_entry (&finder, kind, addr);
4340 addr_table_entry **slot = addr_index_table->find_slot (&finder, INSERT);
4342 if (*slot == HTAB_EMPTY_ENTRY)
4344 node = ggc_cleared_alloc<addr_table_entry> ();
4345 init_addr_table_entry (node, kind, addr);
4346 *slot = node;
4348 else
4349 node = *slot;
4351 node->refcount++;
4352 return node;
4355 /* Remove an entry from the addr table by decrementing its refcount.
4356 Strictly, decrementing the refcount would be enough, but the
4357 assertion that the entry is actually in the table has found
4358 bugs. */
4360 static void
4361 remove_addr_table_entry (addr_table_entry *entry)
4363 gcc_assert (dwarf_split_debug_info && addr_index_table);
4364 /* After an index is assigned, the table is frozen. */
4365 gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
4366 entry->refcount--;
4369 /* Given a location list, remove all addresses it refers to from the
4370 address_table. */
4372 static void
4373 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4375 for (; descr; descr = descr->dw_loc_next)
4376 if (descr->dw_loc_oprnd1.val_entry != NULL)
4378 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4379 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4383 /* A helper function for dwarf2out_finish called through
4384 htab_traverse. Assign an addr_table_entry its index. All entries
4385 must be collected into the table when this function is called,
4386 because the indexing code relies on htab_traverse to traverse nodes
4387 in the same order for each run. */
4390 index_addr_table_entry (addr_table_entry **h, unsigned int *index)
4392 addr_table_entry *node = *h;
4394 /* Don't index unreferenced nodes. */
4395 if (node->refcount == 0)
4396 return 1;
4398 gcc_assert (node->index == NO_INDEX_ASSIGNED);
4399 node->index = *index;
4400 *index += 1;
4402 return 1;
4405 /* Add an address constant attribute value to a DIE. When using
4406 dwarf_split_debug_info, address attributes in dies destined for the
4407 final executable should be direct references--setting the parameter
4408 force_direct ensures this behavior. */
4410 static inline void
4411 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4412 bool force_direct)
4414 dw_attr_node attr;
4416 attr.dw_attr = attr_kind;
4417 attr.dw_attr_val.val_class = dw_val_class_addr;
4418 attr.dw_attr_val.v.val_addr = addr;
4419 if (dwarf_split_debug_info && !force_direct)
4420 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4421 else
4422 attr.dw_attr_val.val_entry = NULL;
4423 add_dwarf_attr (die, &attr);
4426 /* Get the RTX from to an address DIE attribute. */
4428 static inline rtx
4429 AT_addr (dw_attr_ref a)
4431 gcc_assert (a && AT_class (a) == dw_val_class_addr);
4432 return a->dw_attr_val.v.val_addr;
4435 /* Add a file attribute value to a DIE. */
4437 static inline void
4438 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4439 struct dwarf_file_data *fd)
4441 dw_attr_node attr;
4443 attr.dw_attr = attr_kind;
4444 attr.dw_attr_val.val_class = dw_val_class_file;
4445 attr.dw_attr_val.val_entry = NULL;
4446 attr.dw_attr_val.v.val_file = fd;
4447 add_dwarf_attr (die, &attr);
4450 /* Get the dwarf_file_data from a file DIE attribute. */
4452 static inline struct dwarf_file_data *
4453 AT_file (dw_attr_ref a)
4455 gcc_assert (a && AT_class (a) == dw_val_class_file);
4456 return a->dw_attr_val.v.val_file;
4459 /* Add a vms delta attribute value to a DIE. */
4461 static inline void
4462 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4463 const char *lbl1, const char *lbl2)
4465 dw_attr_node attr;
4467 attr.dw_attr = attr_kind;
4468 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4469 attr.dw_attr_val.val_entry = NULL;
4470 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4471 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4472 add_dwarf_attr (die, &attr);
4475 /* Add a label identifier attribute value to a DIE. */
4477 static inline void
4478 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4479 const char *lbl_id)
4481 dw_attr_node attr;
4483 attr.dw_attr = attr_kind;
4484 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4485 attr.dw_attr_val.val_entry = NULL;
4486 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4487 if (dwarf_split_debug_info)
4488 attr.dw_attr_val.val_entry
4489 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4490 ate_kind_label);
4491 add_dwarf_attr (die, &attr);
4494 /* Add a section offset attribute value to a DIE, an offset into the
4495 debug_line section. */
4497 static inline void
4498 add_AT_lineptr (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_lineptr;
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 a section offset attribute value to a DIE, an offset into the
4511 debug_macinfo section. */
4513 static inline void
4514 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4515 const char *label)
4517 dw_attr_node attr;
4519 attr.dw_attr = attr_kind;
4520 attr.dw_attr_val.val_class = dw_val_class_macptr;
4521 attr.dw_attr_val.val_entry = NULL;
4522 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4523 add_dwarf_attr (die, &attr);
4526 /* Add an offset attribute value to a DIE. */
4528 static inline void
4529 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4530 unsigned HOST_WIDE_INT offset)
4532 dw_attr_node attr;
4534 attr.dw_attr = attr_kind;
4535 attr.dw_attr_val.val_class = dw_val_class_offset;
4536 attr.dw_attr_val.val_entry = NULL;
4537 attr.dw_attr_val.v.val_offset = offset;
4538 add_dwarf_attr (die, &attr);
4541 /* Add a range_list attribute value to a DIE. When using
4542 dwarf_split_debug_info, address attributes in dies destined for the
4543 final executable should be direct references--setting the parameter
4544 force_direct ensures this behavior. */
4546 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4547 #define RELOCATED_OFFSET (NULL)
4549 static void
4550 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4551 long unsigned int offset, bool force_direct)
4553 dw_attr_node attr;
4555 attr.dw_attr = attr_kind;
4556 attr.dw_attr_val.val_class = dw_val_class_range_list;
4557 /* For the range_list attribute, use val_entry to store whether the
4558 offset should follow split-debug-info or normal semantics. This
4559 value is read in output_range_list_offset. */
4560 if (dwarf_split_debug_info && !force_direct)
4561 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4562 else
4563 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4564 attr.dw_attr_val.v.val_offset = offset;
4565 add_dwarf_attr (die, &attr);
4568 /* Return the start label of a delta attribute. */
4570 static inline const char *
4571 AT_vms_delta1 (dw_attr_ref a)
4573 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4574 return a->dw_attr_val.v.val_vms_delta.lbl1;
4577 /* Return the end label of a delta attribute. */
4579 static inline const char *
4580 AT_vms_delta2 (dw_attr_ref a)
4582 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4583 return a->dw_attr_val.v.val_vms_delta.lbl2;
4586 static inline const char *
4587 AT_lbl (dw_attr_ref a)
4589 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4590 || AT_class (a) == dw_val_class_lineptr
4591 || AT_class (a) == dw_val_class_macptr
4592 || AT_class (a) == dw_val_class_high_pc));
4593 return a->dw_attr_val.v.val_lbl_id;
4596 /* Get the attribute of type attr_kind. */
4598 static dw_attr_ref
4599 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4601 dw_attr_ref a;
4602 unsigned ix;
4603 dw_die_ref spec = NULL;
4605 if (! die)
4606 return NULL;
4608 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4609 if (a->dw_attr == attr_kind)
4610 return a;
4611 else if (a->dw_attr == DW_AT_specification
4612 || a->dw_attr == DW_AT_abstract_origin)
4613 spec = AT_ref (a);
4615 if (spec)
4616 return get_AT (spec, attr_kind);
4618 return NULL;
4621 /* Returns the parent of the declaration of DIE. */
4623 static dw_die_ref
4624 get_die_parent (dw_die_ref die)
4626 dw_die_ref t;
4628 if (!die)
4629 return NULL;
4631 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4632 || (t = get_AT_ref (die, DW_AT_specification)))
4633 die = t;
4635 return die->die_parent;
4638 /* Return the "low pc" attribute value, typically associated with a subprogram
4639 DIE. Return null if the "low pc" attribute is either not present, or if it
4640 cannot be represented as an assembler label identifier. */
4642 static inline const char *
4643 get_AT_low_pc (dw_die_ref die)
4645 dw_attr_ref a = get_AT (die, DW_AT_low_pc);
4647 return a ? AT_lbl (a) : NULL;
4650 /* Return the "high pc" attribute value, typically associated with a subprogram
4651 DIE. Return null if the "high pc" attribute is either not present, or if it
4652 cannot be represented as an assembler label identifier. */
4654 static inline const char *
4655 get_AT_hi_pc (dw_die_ref die)
4657 dw_attr_ref a = get_AT (die, DW_AT_high_pc);
4659 return a ? AT_lbl (a) : NULL;
4662 /* Return the value of the string attribute designated by ATTR_KIND, or
4663 NULL if it is not present. */
4665 static inline const char *
4666 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4668 dw_attr_ref a = get_AT (die, attr_kind);
4670 return a ? AT_string (a) : NULL;
4673 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4674 if it is not present. */
4676 static inline int
4677 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4679 dw_attr_ref a = get_AT (die, attr_kind);
4681 return a ? AT_flag (a) : 0;
4684 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4685 if it is not present. */
4687 static inline unsigned
4688 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4690 dw_attr_ref a = get_AT (die, attr_kind);
4692 return a ? AT_unsigned (a) : 0;
4695 static inline dw_die_ref
4696 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4698 dw_attr_ref a = get_AT (die, attr_kind);
4700 return a ? AT_ref (a) : NULL;
4703 static inline struct dwarf_file_data *
4704 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4706 dw_attr_ref a = get_AT (die, attr_kind);
4708 return a ? AT_file (a) : NULL;
4711 /* Return TRUE if the language is C++. */
4713 static inline bool
4714 is_cxx (void)
4716 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4718 return (lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus
4719 || lang == DW_LANG_C_plus_plus_11 || lang == DW_LANG_C_plus_plus_14);
4722 /* Return TRUE if the language is Java. */
4724 static inline bool
4725 is_java (void)
4727 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4729 return lang == DW_LANG_Java;
4732 /* Return TRUE if the language is Fortran. */
4734 static inline bool
4735 is_fortran (void)
4737 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4739 return (lang == DW_LANG_Fortran77
4740 || lang == DW_LANG_Fortran90
4741 || lang == DW_LANG_Fortran95
4742 || lang == DW_LANG_Fortran03
4743 || lang == DW_LANG_Fortran08);
4746 /* Return TRUE if the language is Ada. */
4748 static inline bool
4749 is_ada (void)
4751 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4753 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4756 /* Remove the specified attribute if present. */
4758 static void
4759 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4761 dw_attr_ref a;
4762 unsigned ix;
4764 if (! die)
4765 return;
4767 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4768 if (a->dw_attr == attr_kind)
4770 if (AT_class (a) == dw_val_class_str)
4771 if (a->dw_attr_val.v.val_str->refcount)
4772 a->dw_attr_val.v.val_str->refcount--;
4774 /* vec::ordered_remove should help reduce the number of abbrevs
4775 that are needed. */
4776 die->die_attr->ordered_remove (ix);
4777 return;
4781 /* Remove CHILD from its parent. PREV must have the property that
4782 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
4784 static void
4785 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4787 gcc_assert (child->die_parent == prev->die_parent);
4788 gcc_assert (prev->die_sib == child);
4789 if (prev == child)
4791 gcc_assert (child->die_parent->die_child == child);
4792 prev = NULL;
4794 else
4795 prev->die_sib = child->die_sib;
4796 if (child->die_parent->die_child == child)
4797 child->die_parent->die_child = prev;
4800 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
4801 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
4803 static void
4804 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4806 dw_die_ref parent = old_child->die_parent;
4808 gcc_assert (parent == prev->die_parent);
4809 gcc_assert (prev->die_sib == old_child);
4811 new_child->die_parent = parent;
4812 if (prev == old_child)
4814 gcc_assert (parent->die_child == old_child);
4815 new_child->die_sib = new_child;
4817 else
4819 prev->die_sib = new_child;
4820 new_child->die_sib = old_child->die_sib;
4822 if (old_child->die_parent->die_child == old_child)
4823 old_child->die_parent->die_child = new_child;
4826 /* Move all children from OLD_PARENT to NEW_PARENT. */
4828 static void
4829 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4831 dw_die_ref c;
4832 new_parent->die_child = old_parent->die_child;
4833 old_parent->die_child = NULL;
4834 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4837 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
4838 matches TAG. */
4840 static void
4841 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4843 dw_die_ref c;
4845 c = die->die_child;
4846 if (c) do {
4847 dw_die_ref prev = c;
4848 c = c->die_sib;
4849 while (c->die_tag == tag)
4851 remove_child_with_prev (c, prev);
4852 /* Might have removed every child. */
4853 if (c == c->die_sib)
4854 return;
4855 c = c->die_sib;
4857 } while (c != die->die_child);
4860 /* Add a CHILD_DIE as the last child of DIE. */
4862 static void
4863 add_child_die (dw_die_ref die, dw_die_ref child_die)
4865 /* FIXME this should probably be an assert. */
4866 if (! die || ! child_die)
4867 return;
4868 gcc_assert (die != child_die);
4870 child_die->die_parent = die;
4871 if (die->die_child)
4873 child_die->die_sib = die->die_child->die_sib;
4874 die->die_child->die_sib = child_die;
4876 else
4877 child_die->die_sib = child_die;
4878 die->die_child = child_die;
4881 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
4882 is the specification, to the end of PARENT's list of children.
4883 This is done by removing and re-adding it. */
4885 static void
4886 splice_child_die (dw_die_ref parent, dw_die_ref child)
4888 dw_die_ref p;
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 for (p = child->die_parent->die_child; ; p = p->die_sib)
4905 if (p->die_sib == child)
4907 remove_child_with_prev (child, p);
4908 break;
4911 add_child_die (parent, child);
4914 /* Return a pointer to a newly created DIE node. */
4916 static inline dw_die_ref
4917 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
4919 dw_die_ref die = ggc_cleared_alloc<die_node> ();
4921 die->die_tag = tag_value;
4923 if (parent_die != NULL)
4924 add_child_die (parent_die, die);
4925 else
4927 limbo_die_node *limbo_node;
4929 limbo_node = ggc_cleared_alloc<limbo_die_node> ();
4930 limbo_node->die = die;
4931 limbo_node->created_for = t;
4932 limbo_node->next = limbo_die_list;
4933 limbo_die_list = limbo_node;
4936 return die;
4939 /* Return the DIE associated with the given type specifier. */
4941 static inline dw_die_ref
4942 lookup_type_die (tree type)
4944 return TYPE_SYMTAB_DIE (type);
4947 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
4948 anonymous type named by the typedef TYPE_DIE, return the DIE of the
4949 anonymous type instead the one of the naming typedef. */
4951 static inline dw_die_ref
4952 strip_naming_typedef (tree type, dw_die_ref type_die)
4954 if (type
4955 && TREE_CODE (type) == RECORD_TYPE
4956 && type_die
4957 && type_die->die_tag == DW_TAG_typedef
4958 && is_naming_typedef_decl (TYPE_NAME (type)))
4959 type_die = get_AT_ref (type_die, DW_AT_type);
4960 return type_die;
4963 /* Like lookup_type_die, but if type is an anonymous type named by a
4964 typedef[1], return the DIE of the anonymous type instead the one of
4965 the naming typedef. This is because in gen_typedef_die, we did
4966 equate the anonymous struct named by the typedef with the DIE of
4967 the naming typedef. So by default, lookup_type_die on an anonymous
4968 struct yields the DIE of the naming typedef.
4970 [1]: Read the comment of is_naming_typedef_decl to learn about what
4971 a naming typedef is. */
4973 static inline dw_die_ref
4974 lookup_type_die_strip_naming_typedef (tree type)
4976 dw_die_ref die = lookup_type_die (type);
4977 return strip_naming_typedef (type, die);
4980 /* Equate a DIE to a given type specifier. */
4982 static inline void
4983 equate_type_number_to_die (tree type, dw_die_ref type_die)
4985 TYPE_SYMTAB_DIE (type) = type_die;
4988 /* Returns a hash value for X (which really is a die_struct). */
4990 inline hashval_t
4991 decl_die_hasher::hash (die_node *x)
4993 return (hashval_t) x->decl_id;
4996 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
4998 inline bool
4999 decl_die_hasher::equal (die_node *x, tree y)
5001 return (x->decl_id == DECL_UID (y));
5004 /* Return the DIE associated with a given declaration. */
5006 static inline dw_die_ref
5007 lookup_decl_die (tree decl)
5009 return decl_die_table->find_with_hash (decl, DECL_UID (decl));
5012 /* Returns a hash value for X (which really is a var_loc_list). */
5014 inline hashval_t
5015 decl_loc_hasher::hash (var_loc_list *x)
5017 return (hashval_t) x->decl_id;
5020 /* Return nonzero if decl_id of var_loc_list X is the same as
5021 UID of decl *Y. */
5023 inline bool
5024 decl_loc_hasher::equal (var_loc_list *x, const_tree y)
5026 return (x->decl_id == DECL_UID (y));
5029 /* Return the var_loc list associated with a given declaration. */
5031 static inline var_loc_list *
5032 lookup_decl_loc (const_tree decl)
5034 if (!decl_loc_table)
5035 return NULL;
5036 return decl_loc_table->find_with_hash (decl, DECL_UID (decl));
5039 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
5041 inline hashval_t
5042 dw_loc_list_hasher::hash (cached_dw_loc_list *x)
5044 return (hashval_t) x->decl_id;
5047 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
5048 UID of decl *Y. */
5050 inline bool
5051 dw_loc_list_hasher::equal (cached_dw_loc_list *x, const_tree y)
5053 return (x->decl_id == DECL_UID (y));
5056 /* Equate a DIE to a particular declaration. */
5058 static void
5059 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5061 unsigned int decl_id = DECL_UID (decl);
5063 *decl_die_table->find_slot_with_hash (decl, decl_id, INSERT) = decl_die;
5064 decl_die->decl_id = decl_id;
5067 /* Return how many bits covers PIECE EXPR_LIST. */
5069 static HOST_WIDE_INT
5070 decl_piece_bitsize (rtx piece)
5072 int ret = (int) GET_MODE (piece);
5073 if (ret)
5074 return ret;
5075 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
5076 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
5077 return INTVAL (XEXP (XEXP (piece, 0), 0));
5080 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
5082 static rtx *
5083 decl_piece_varloc_ptr (rtx piece)
5085 if ((int) GET_MODE (piece))
5086 return &XEXP (piece, 0);
5087 else
5088 return &XEXP (XEXP (piece, 0), 1);
5091 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
5092 Next is the chain of following piece nodes. */
5094 static rtx_expr_list *
5095 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
5097 if (bitsize > 0 && bitsize <= (int) MAX_MACHINE_MODE)
5098 return alloc_EXPR_LIST (bitsize, loc_note, next);
5099 else
5100 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
5101 GEN_INT (bitsize),
5102 loc_note), next);
5105 /* Return rtx that should be stored into loc field for
5106 LOC_NOTE and BITPOS/BITSIZE. */
5108 static rtx
5109 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
5110 HOST_WIDE_INT bitsize)
5112 if (bitsize != -1)
5114 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
5115 if (bitpos != 0)
5116 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
5118 return loc_note;
5121 /* This function either modifies location piece list *DEST in
5122 place (if SRC and INNER is NULL), or copies location piece list
5123 *SRC to *DEST while modifying it. Location BITPOS is modified
5124 to contain LOC_NOTE, any pieces overlapping it are removed resp.
5125 not copied and if needed some padding around it is added.
5126 When modifying in place, DEST should point to EXPR_LIST where
5127 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
5128 to the start of the whole list and INNER points to the EXPR_LIST
5129 where earlier pieces cover PIECE_BITPOS bits. */
5131 static void
5132 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5133 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5134 HOST_WIDE_INT bitsize, rtx loc_note)
5136 HOST_WIDE_INT diff;
5137 bool copy = inner != NULL;
5139 if (copy)
5141 /* First copy all nodes preceding the current bitpos. */
5142 while (src != inner)
5144 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5145 decl_piece_bitsize (*src), NULL_RTX);
5146 dest = &XEXP (*dest, 1);
5147 src = &XEXP (*src, 1);
5150 /* Add padding if needed. */
5151 if (bitpos != piece_bitpos)
5153 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5154 copy ? NULL_RTX : *dest);
5155 dest = &XEXP (*dest, 1);
5157 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5159 gcc_assert (!copy);
5160 /* A piece with correct bitpos and bitsize already exist,
5161 just update the location for it and return. */
5162 *decl_piece_varloc_ptr (*dest) = loc_note;
5163 return;
5165 /* Add the piece that changed. */
5166 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5167 dest = &XEXP (*dest, 1);
5168 /* Skip over pieces that overlap it. */
5169 diff = bitpos - piece_bitpos + bitsize;
5170 if (!copy)
5171 src = dest;
5172 while (diff > 0 && *src)
5174 rtx piece = *src;
5175 diff -= decl_piece_bitsize (piece);
5176 if (copy)
5177 src = &XEXP (piece, 1);
5178 else
5180 *src = XEXP (piece, 1);
5181 free_EXPR_LIST_node (piece);
5184 /* Add padding if needed. */
5185 if (diff < 0 && *src)
5187 if (!copy)
5188 dest = src;
5189 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5190 dest = &XEXP (*dest, 1);
5192 if (!copy)
5193 return;
5194 /* Finally copy all nodes following it. */
5195 while (*src)
5197 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5198 decl_piece_bitsize (*src), NULL_RTX);
5199 dest = &XEXP (*dest, 1);
5200 src = &XEXP (*src, 1);
5204 /* Add a variable location node to the linked list for DECL. */
5206 static struct var_loc_node *
5207 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5209 unsigned int decl_id;
5210 var_loc_list *temp;
5211 struct var_loc_node *loc = NULL;
5212 HOST_WIDE_INT bitsize = -1, bitpos = -1;
5214 if (TREE_CODE (decl) == VAR_DECL
5215 && DECL_HAS_DEBUG_EXPR_P (decl))
5217 tree realdecl = DECL_DEBUG_EXPR (decl);
5218 if (handled_component_p (realdecl)
5219 || (TREE_CODE (realdecl) == MEM_REF
5220 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5222 HOST_WIDE_INT maxsize;
5223 tree innerdecl;
5224 innerdecl
5225 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
5226 if (!DECL_P (innerdecl)
5227 || DECL_IGNORED_P (innerdecl)
5228 || TREE_STATIC (innerdecl)
5229 || bitsize <= 0
5230 || bitpos + bitsize > 256
5231 || bitsize != maxsize)
5232 return NULL;
5233 decl = innerdecl;
5237 decl_id = DECL_UID (decl);
5238 var_loc_list **slot
5239 = decl_loc_table->find_slot_with_hash (decl, decl_id, INSERT);
5240 if (*slot == NULL)
5242 temp = ggc_cleared_alloc<var_loc_list> ();
5243 temp->decl_id = decl_id;
5244 *slot = temp;
5246 else
5247 temp = *slot;
5249 /* For PARM_DECLs try to keep around the original incoming value,
5250 even if that means we'll emit a zero-range .debug_loc entry. */
5251 if (temp->last
5252 && temp->first == temp->last
5253 && TREE_CODE (decl) == PARM_DECL
5254 && NOTE_P (temp->first->loc)
5255 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5256 && DECL_INCOMING_RTL (decl)
5257 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5258 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5259 == GET_CODE (DECL_INCOMING_RTL (decl))
5260 && prev_real_insn (temp->first->loc) == NULL_RTX
5261 && (bitsize != -1
5262 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5263 NOTE_VAR_LOCATION_LOC (loc_note))
5264 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5265 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5267 loc = ggc_cleared_alloc<var_loc_node> ();
5268 temp->first->next = loc;
5269 temp->last = loc;
5270 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5272 else if (temp->last)
5274 struct var_loc_node *last = temp->last, *unused = NULL;
5275 rtx *piece_loc = NULL, last_loc_note;
5276 HOST_WIDE_INT piece_bitpos = 0;
5277 if (last->next)
5279 last = last->next;
5280 gcc_assert (last->next == NULL);
5282 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5284 piece_loc = &last->loc;
5287 HOST_WIDE_INT cur_bitsize = decl_piece_bitsize (*piece_loc);
5288 if (piece_bitpos + cur_bitsize > bitpos)
5289 break;
5290 piece_bitpos += cur_bitsize;
5291 piece_loc = &XEXP (*piece_loc, 1);
5293 while (*piece_loc);
5295 /* TEMP->LAST here is either pointer to the last but one or
5296 last element in the chained list, LAST is pointer to the
5297 last element. */
5298 if (label && strcmp (last->label, label) == 0)
5300 /* For SRA optimized variables if there weren't any real
5301 insns since last note, just modify the last node. */
5302 if (piece_loc != NULL)
5304 adjust_piece_list (piece_loc, NULL, NULL,
5305 bitpos, piece_bitpos, bitsize, loc_note);
5306 return NULL;
5308 /* If the last note doesn't cover any instructions, remove it. */
5309 if (temp->last != last)
5311 temp->last->next = NULL;
5312 unused = last;
5313 last = temp->last;
5314 gcc_assert (strcmp (last->label, label) != 0);
5316 else
5318 gcc_assert (temp->first == temp->last
5319 || (temp->first->next == temp->last
5320 && TREE_CODE (decl) == PARM_DECL));
5321 memset (temp->last, '\0', sizeof (*temp->last));
5322 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5323 return temp->last;
5326 if (bitsize == -1 && NOTE_P (last->loc))
5327 last_loc_note = last->loc;
5328 else if (piece_loc != NULL
5329 && *piece_loc != NULL_RTX
5330 && piece_bitpos == bitpos
5331 && decl_piece_bitsize (*piece_loc) == bitsize)
5332 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5333 else
5334 last_loc_note = NULL_RTX;
5335 /* If the current location is the same as the end of the list,
5336 and either both or neither of the locations is uninitialized,
5337 we have nothing to do. */
5338 if (last_loc_note == NULL_RTX
5339 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5340 NOTE_VAR_LOCATION_LOC (loc_note)))
5341 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5342 != NOTE_VAR_LOCATION_STATUS (loc_note))
5343 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5344 == VAR_INIT_STATUS_UNINITIALIZED)
5345 || (NOTE_VAR_LOCATION_STATUS (loc_note)
5346 == VAR_INIT_STATUS_UNINITIALIZED))))
5348 /* Add LOC to the end of list and update LAST. If the last
5349 element of the list has been removed above, reuse its
5350 memory for the new node, otherwise allocate a new one. */
5351 if (unused)
5353 loc = unused;
5354 memset (loc, '\0', sizeof (*loc));
5356 else
5357 loc = ggc_cleared_alloc<var_loc_node> ();
5358 if (bitsize == -1 || piece_loc == NULL)
5359 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5360 else
5361 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5362 bitpos, piece_bitpos, bitsize, loc_note);
5363 last->next = loc;
5364 /* Ensure TEMP->LAST will point either to the new last but one
5365 element of the chain, or to the last element in it. */
5366 if (last != temp->last)
5367 temp->last = last;
5369 else if (unused)
5370 ggc_free (unused);
5372 else
5374 loc = ggc_cleared_alloc<var_loc_node> ();
5375 temp->first = loc;
5376 temp->last = loc;
5377 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5379 return loc;
5382 /* Keep track of the number of spaces used to indent the
5383 output of the debugging routines that print the structure of
5384 the DIE internal representation. */
5385 static int print_indent;
5387 /* Indent the line the number of spaces given by print_indent. */
5389 static inline void
5390 print_spaces (FILE *outfile)
5392 fprintf (outfile, "%*s", print_indent, "");
5395 /* Print a type signature in hex. */
5397 static inline void
5398 print_signature (FILE *outfile, char *sig)
5400 int i;
5402 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5403 fprintf (outfile, "%02x", sig[i] & 0xff);
5406 static void print_loc_descr (dw_loc_descr_ref, FILE *);
5408 /* Print the value associated to the VAL DWARF value node to OUTFILE. If
5409 RECURSE, output location descriptor operations. */
5411 static void
5412 print_dw_val (dw_val_node *val, bool recurse, FILE *outfile)
5414 switch (val->val_class)
5416 case dw_val_class_addr:
5417 fprintf (outfile, "address");
5418 break;
5419 case dw_val_class_offset:
5420 fprintf (outfile, "offset");
5421 break;
5422 case dw_val_class_loc:
5423 fprintf (outfile, "location descriptor");
5424 if (val->v.val_loc == NULL)
5425 fprintf (outfile, " -> <null>\n");
5426 else if (recurse)
5428 fprintf (outfile, ":\n");
5429 print_indent += 4;
5430 print_loc_descr (val->v.val_loc, outfile);
5431 print_indent -= 4;
5433 else
5434 fprintf (outfile, " (%p)\n", (void *) val->v.val_loc);
5435 break;
5436 case dw_val_class_loc_list:
5437 fprintf (outfile, "location list -> label:%s",
5438 val->v.val_loc_list->ll_symbol);
5439 break;
5440 case dw_val_class_range_list:
5441 fprintf (outfile, "range list");
5442 break;
5443 case dw_val_class_const:
5444 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, val->v.val_int);
5445 break;
5446 case dw_val_class_unsigned_const:
5447 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, val->v.val_unsigned);
5448 break;
5449 case dw_val_class_const_double:
5450 fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
5451 HOST_WIDE_INT_PRINT_UNSIGNED")",
5452 val->v.val_double.high,
5453 val->v.val_double.low);
5454 break;
5455 case dw_val_class_wide_int:
5457 int i = val->v.val_wide->get_len ();
5458 fprintf (outfile, "constant (");
5459 gcc_assert (i > 0);
5460 if (val->v.val_wide->elt (i - 1) == 0)
5461 fprintf (outfile, "0x");
5462 fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
5463 val->v.val_wide->elt (--i));
5464 while (--i >= 0)
5465 fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
5466 val->v.val_wide->elt (i));
5467 fprintf (outfile, ")");
5468 break;
5470 case dw_val_class_vec:
5471 fprintf (outfile, "floating-point or vector constant");
5472 break;
5473 case dw_val_class_flag:
5474 fprintf (outfile, "%u", val->v.val_flag);
5475 break;
5476 case dw_val_class_die_ref:
5477 if (val->v.val_die_ref.die != NULL)
5479 dw_die_ref die = val->v.val_die_ref.die;
5481 if (die->comdat_type_p)
5483 fprintf (outfile, "die -> signature: ");
5484 print_signature (outfile,
5485 die->die_id.die_type_node->signature);
5487 else if (die->die_id.die_symbol)
5488 fprintf (outfile, "die -> label: %s", die->die_id.die_symbol);
5489 else
5490 fprintf (outfile, "die -> %ld", die->die_offset);
5491 fprintf (outfile, " (%p)", (void *) die);
5493 else
5494 fprintf (outfile, "die -> <null>");
5495 break;
5496 case dw_val_class_vms_delta:
5497 fprintf (outfile, "delta: @slotcount(%s-%s)",
5498 val->v.val_vms_delta.lbl2, val->v.val_vms_delta.lbl1);
5499 break;
5500 case dw_val_class_lbl_id:
5501 case dw_val_class_lineptr:
5502 case dw_val_class_macptr:
5503 case dw_val_class_high_pc:
5504 fprintf (outfile, "label: %s", val->v.val_lbl_id);
5505 break;
5506 case dw_val_class_str:
5507 if (val->v.val_str->str != NULL)
5508 fprintf (outfile, "\"%s\"", val->v.val_str->str);
5509 else
5510 fprintf (outfile, "<null>");
5511 break;
5512 case dw_val_class_file:
5513 fprintf (outfile, "\"%s\" (%d)", val->v.val_file->filename,
5514 val->v.val_file->emitted_number);
5515 break;
5516 case dw_val_class_data8:
5518 int i;
5520 for (i = 0; i < 8; i++)
5521 fprintf (outfile, "%02x", val->v.val_data8[i]);
5522 break;
5524 default:
5525 break;
5529 /* Likewise, for a DIE attribute. */
5531 static void
5532 print_attribute (dw_attr_ref a, bool recurse, FILE *outfile)
5534 print_dw_val (&a->dw_attr_val, recurse, outfile);
5538 /* Print the list of operands in the LOC location description to OUTFILE. This
5539 routine is a debugging aid only. */
5541 static void
5542 print_loc_descr (dw_loc_descr_ref loc, FILE *outfile)
5544 dw_loc_descr_ref l = loc;
5546 if (loc == NULL)
5548 print_spaces (outfile);
5549 fprintf (outfile, "<null>\n");
5550 return;
5553 for (l = loc; l != NULL; l = l->dw_loc_next)
5555 print_spaces (outfile);
5556 fprintf (outfile, "(%p) %s",
5557 (void *) l,
5558 dwarf_stack_op_name (l->dw_loc_opc));
5559 if (l->dw_loc_oprnd1.val_class != dw_val_class_none)
5561 fprintf (outfile, " ");
5562 print_dw_val (&l->dw_loc_oprnd1, false, outfile);
5564 if (l->dw_loc_oprnd2.val_class != dw_val_class_none)
5566 fprintf (outfile, ", ");
5567 print_dw_val (&l->dw_loc_oprnd2, false, outfile);
5569 fprintf (outfile, "\n");
5573 /* Print the information associated with a given DIE, and its children.
5574 This routine is a debugging aid only. */
5576 static void
5577 print_die (dw_die_ref die, FILE *outfile)
5579 dw_attr_ref a;
5580 dw_die_ref c;
5581 unsigned ix;
5583 print_spaces (outfile);
5584 fprintf (outfile, "DIE %4ld: %s (%p)\n",
5585 die->die_offset, dwarf_tag_name (die->die_tag),
5586 (void*) die);
5587 print_spaces (outfile);
5588 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5589 fprintf (outfile, " offset: %ld", die->die_offset);
5590 fprintf (outfile, " mark: %d\n", die->die_mark);
5592 if (die->comdat_type_p)
5594 print_spaces (outfile);
5595 fprintf (outfile, " signature: ");
5596 print_signature (outfile, die->die_id.die_type_node->signature);
5597 fprintf (outfile, "\n");
5600 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5602 print_spaces (outfile);
5603 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5605 print_attribute (a, true, outfile);
5606 fprintf (outfile, "\n");
5609 if (die->die_child != NULL)
5611 print_indent += 4;
5612 FOR_EACH_CHILD (die, c, print_die (c, outfile));
5613 print_indent -= 4;
5615 if (print_indent == 0)
5616 fprintf (outfile, "\n");
5619 /* Print the list of operations in the LOC location description. */
5621 DEBUG_FUNCTION void
5622 debug_dwarf_loc_descr (dw_loc_descr_ref loc)
5624 print_loc_descr (loc, stderr);
5627 /* Print the information collected for a given DIE. */
5629 DEBUG_FUNCTION void
5630 debug_dwarf_die (dw_die_ref die)
5632 print_die (die, stderr);
5635 DEBUG_FUNCTION void
5636 debug (die_struct &ref)
5638 print_die (&ref, stderr);
5641 DEBUG_FUNCTION void
5642 debug (die_struct *ptr)
5644 if (ptr)
5645 debug (*ptr);
5646 else
5647 fprintf (stderr, "<nil>\n");
5651 /* Print all DWARF information collected for the compilation unit.
5652 This routine is a debugging aid only. */
5654 DEBUG_FUNCTION void
5655 debug_dwarf (void)
5657 print_indent = 0;
5658 print_die (comp_unit_die (), stderr);
5661 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
5662 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
5663 DIE that marks the start of the DIEs for this include file. */
5665 static dw_die_ref
5666 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5668 const char *filename = get_AT_string (bincl_die, DW_AT_name);
5669 dw_die_ref new_unit = gen_compile_unit_die (filename);
5671 new_unit->die_sib = old_unit;
5672 return new_unit;
5675 /* Close an include-file CU and reopen the enclosing one. */
5677 static dw_die_ref
5678 pop_compile_unit (dw_die_ref old_unit)
5680 dw_die_ref new_unit = old_unit->die_sib;
5682 old_unit->die_sib = NULL;
5683 return new_unit;
5686 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5687 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5688 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5690 /* Calculate the checksum of a location expression. */
5692 static inline void
5693 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5695 int tem;
5696 inchash::hash hstate;
5697 hashval_t hash;
5699 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
5700 CHECKSUM (tem);
5701 hash_loc_operands (loc, hstate);
5702 hash = hstate.end();
5703 CHECKSUM (hash);
5706 /* Calculate the checksum of an attribute. */
5708 static void
5709 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5711 dw_loc_descr_ref loc;
5712 rtx r;
5714 CHECKSUM (at->dw_attr);
5716 /* We don't care that this was compiled with a different compiler
5717 snapshot; if the output is the same, that's what matters. */
5718 if (at->dw_attr == DW_AT_producer)
5719 return;
5721 switch (AT_class (at))
5723 case dw_val_class_const:
5724 CHECKSUM (at->dw_attr_val.v.val_int);
5725 break;
5726 case dw_val_class_unsigned_const:
5727 CHECKSUM (at->dw_attr_val.v.val_unsigned);
5728 break;
5729 case dw_val_class_const_double:
5730 CHECKSUM (at->dw_attr_val.v.val_double);
5731 break;
5732 case dw_val_class_wide_int:
5733 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
5734 get_full_len (*at->dw_attr_val.v.val_wide)
5735 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
5736 break;
5737 case dw_val_class_vec:
5738 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5739 (at->dw_attr_val.v.val_vec.length
5740 * at->dw_attr_val.v.val_vec.elt_size));
5741 break;
5742 case dw_val_class_flag:
5743 CHECKSUM (at->dw_attr_val.v.val_flag);
5744 break;
5745 case dw_val_class_str:
5746 CHECKSUM_STRING (AT_string (at));
5747 break;
5749 case dw_val_class_addr:
5750 r = AT_addr (at);
5751 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5752 CHECKSUM_STRING (XSTR (r, 0));
5753 break;
5755 case dw_val_class_offset:
5756 CHECKSUM (at->dw_attr_val.v.val_offset);
5757 break;
5759 case dw_val_class_loc:
5760 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5761 loc_checksum (loc, ctx);
5762 break;
5764 case dw_val_class_die_ref:
5765 die_checksum (AT_ref (at), ctx, mark);
5766 break;
5768 case dw_val_class_fde_ref:
5769 case dw_val_class_vms_delta:
5770 case dw_val_class_lbl_id:
5771 case dw_val_class_lineptr:
5772 case dw_val_class_macptr:
5773 case dw_val_class_high_pc:
5774 break;
5776 case dw_val_class_file:
5777 CHECKSUM_STRING (AT_file (at)->filename);
5778 break;
5780 case dw_val_class_data8:
5781 CHECKSUM (at->dw_attr_val.v.val_data8);
5782 break;
5784 default:
5785 break;
5789 /* Calculate the checksum of a DIE. */
5791 static void
5792 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5794 dw_die_ref c;
5795 dw_attr_ref a;
5796 unsigned ix;
5798 /* To avoid infinite recursion. */
5799 if (die->die_mark)
5801 CHECKSUM (die->die_mark);
5802 return;
5804 die->die_mark = ++(*mark);
5806 CHECKSUM (die->die_tag);
5808 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5809 attr_checksum (a, ctx, mark);
5811 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
5814 #undef CHECKSUM
5815 #undef CHECKSUM_BLOCK
5816 #undef CHECKSUM_STRING
5818 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
5819 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5820 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5821 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
5822 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
5823 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
5824 #define CHECKSUM_ATTR(FOO) \
5825 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
5827 /* Calculate the checksum of a number in signed LEB128 format. */
5829 static void
5830 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
5832 unsigned char byte;
5833 bool more;
5835 while (1)
5837 byte = (value & 0x7f);
5838 value >>= 7;
5839 more = !((value == 0 && (byte & 0x40) == 0)
5840 || (value == -1 && (byte & 0x40) != 0));
5841 if (more)
5842 byte |= 0x80;
5843 CHECKSUM (byte);
5844 if (!more)
5845 break;
5849 /* Calculate the checksum of a number in unsigned LEB128 format. */
5851 static void
5852 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
5854 while (1)
5856 unsigned char byte = (value & 0x7f);
5857 value >>= 7;
5858 if (value != 0)
5859 /* More bytes to follow. */
5860 byte |= 0x80;
5861 CHECKSUM (byte);
5862 if (value == 0)
5863 break;
5867 /* Checksum the context of the DIE. This adds the names of any
5868 surrounding namespaces or structures to the checksum. */
5870 static void
5871 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
5873 const char *name;
5874 dw_die_ref spec;
5875 int tag = die->die_tag;
5877 if (tag != DW_TAG_namespace
5878 && tag != DW_TAG_structure_type
5879 && tag != DW_TAG_class_type)
5880 return;
5882 name = get_AT_string (die, DW_AT_name);
5884 spec = get_AT_ref (die, DW_AT_specification);
5885 if (spec != NULL)
5886 die = spec;
5888 if (die->die_parent != NULL)
5889 checksum_die_context (die->die_parent, ctx);
5891 CHECKSUM_ULEB128 ('C');
5892 CHECKSUM_ULEB128 (tag);
5893 if (name != NULL)
5894 CHECKSUM_STRING (name);
5897 /* Calculate the checksum of a location expression. */
5899 static inline void
5900 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5902 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
5903 were emitted as a DW_FORM_sdata instead of a location expression. */
5904 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
5906 CHECKSUM_ULEB128 (DW_FORM_sdata);
5907 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
5908 return;
5911 /* Otherwise, just checksum the raw location expression. */
5912 while (loc != NULL)
5914 inchash::hash hstate;
5915 hashval_t hash;
5917 CHECKSUM_ULEB128 (loc->dtprel);
5918 CHECKSUM_ULEB128 (loc->dw_loc_opc);
5919 hash_loc_operands (loc, hstate);
5920 hash = hstate.end ();
5921 CHECKSUM (hash);
5922 loc = loc->dw_loc_next;
5926 /* Calculate the checksum of an attribute. */
5928 static void
5929 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
5930 struct md5_ctx *ctx, int *mark)
5932 dw_loc_descr_ref loc;
5933 rtx r;
5935 if (AT_class (at) == dw_val_class_die_ref)
5937 dw_die_ref target_die = AT_ref (at);
5939 /* For pointer and reference types, we checksum only the (qualified)
5940 name of the target type (if there is a name). For friend entries,
5941 we checksum only the (qualified) name of the target type or function.
5942 This allows the checksum to remain the same whether the target type
5943 is complete or not. */
5944 if ((at->dw_attr == DW_AT_type
5945 && (tag == DW_TAG_pointer_type
5946 || tag == DW_TAG_reference_type
5947 || tag == DW_TAG_rvalue_reference_type
5948 || tag == DW_TAG_ptr_to_member_type))
5949 || (at->dw_attr == DW_AT_friend
5950 && tag == DW_TAG_friend))
5952 dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
5954 if (name_attr != NULL)
5956 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5958 if (decl == NULL)
5959 decl = target_die;
5960 CHECKSUM_ULEB128 ('N');
5961 CHECKSUM_ULEB128 (at->dw_attr);
5962 if (decl->die_parent != NULL)
5963 checksum_die_context (decl->die_parent, ctx);
5964 CHECKSUM_ULEB128 ('E');
5965 CHECKSUM_STRING (AT_string (name_attr));
5966 return;
5970 /* For all other references to another DIE, we check to see if the
5971 target DIE has already been visited. If it has, we emit a
5972 backward reference; if not, we descend recursively. */
5973 if (target_die->die_mark > 0)
5975 CHECKSUM_ULEB128 ('R');
5976 CHECKSUM_ULEB128 (at->dw_attr);
5977 CHECKSUM_ULEB128 (target_die->die_mark);
5979 else
5981 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5983 if (decl == NULL)
5984 decl = target_die;
5985 target_die->die_mark = ++(*mark);
5986 CHECKSUM_ULEB128 ('T');
5987 CHECKSUM_ULEB128 (at->dw_attr);
5988 if (decl->die_parent != NULL)
5989 checksum_die_context (decl->die_parent, ctx);
5990 die_checksum_ordered (target_die, ctx, mark);
5992 return;
5995 CHECKSUM_ULEB128 ('A');
5996 CHECKSUM_ULEB128 (at->dw_attr);
5998 switch (AT_class (at))
6000 case dw_val_class_const:
6001 CHECKSUM_ULEB128 (DW_FORM_sdata);
6002 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
6003 break;
6005 case dw_val_class_unsigned_const:
6006 CHECKSUM_ULEB128 (DW_FORM_sdata);
6007 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
6008 break;
6010 case dw_val_class_const_double:
6011 CHECKSUM_ULEB128 (DW_FORM_block);
6012 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
6013 CHECKSUM (at->dw_attr_val.v.val_double);
6014 break;
6016 case dw_val_class_wide_int:
6017 CHECKSUM_ULEB128 (DW_FORM_block);
6018 CHECKSUM_ULEB128 (get_full_len (*at->dw_attr_val.v.val_wide)
6019 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
6020 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6021 get_full_len (*at->dw_attr_val.v.val_wide)
6022 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6023 break;
6025 case dw_val_class_vec:
6026 CHECKSUM_ULEB128 (DW_FORM_block);
6027 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
6028 * at->dw_attr_val.v.val_vec.elt_size);
6029 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6030 (at->dw_attr_val.v.val_vec.length
6031 * at->dw_attr_val.v.val_vec.elt_size));
6032 break;
6034 case dw_val_class_flag:
6035 CHECKSUM_ULEB128 (DW_FORM_flag);
6036 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
6037 break;
6039 case dw_val_class_str:
6040 CHECKSUM_ULEB128 (DW_FORM_string);
6041 CHECKSUM_STRING (AT_string (at));
6042 break;
6044 case dw_val_class_addr:
6045 r = AT_addr (at);
6046 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6047 CHECKSUM_ULEB128 (DW_FORM_string);
6048 CHECKSUM_STRING (XSTR (r, 0));
6049 break;
6051 case dw_val_class_offset:
6052 CHECKSUM_ULEB128 (DW_FORM_sdata);
6053 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
6054 break;
6056 case dw_val_class_loc:
6057 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6058 loc_checksum_ordered (loc, ctx);
6059 break;
6061 case dw_val_class_fde_ref:
6062 case dw_val_class_lbl_id:
6063 case dw_val_class_lineptr:
6064 case dw_val_class_macptr:
6065 case dw_val_class_high_pc:
6066 break;
6068 case dw_val_class_file:
6069 CHECKSUM_ULEB128 (DW_FORM_string);
6070 CHECKSUM_STRING (AT_file (at)->filename);
6071 break;
6073 case dw_val_class_data8:
6074 CHECKSUM (at->dw_attr_val.v.val_data8);
6075 break;
6077 default:
6078 break;
6082 struct checksum_attributes
6084 dw_attr_ref at_name;
6085 dw_attr_ref at_type;
6086 dw_attr_ref at_friend;
6087 dw_attr_ref at_accessibility;
6088 dw_attr_ref at_address_class;
6089 dw_attr_ref at_allocated;
6090 dw_attr_ref at_artificial;
6091 dw_attr_ref at_associated;
6092 dw_attr_ref at_binary_scale;
6093 dw_attr_ref at_bit_offset;
6094 dw_attr_ref at_bit_size;
6095 dw_attr_ref at_bit_stride;
6096 dw_attr_ref at_byte_size;
6097 dw_attr_ref at_byte_stride;
6098 dw_attr_ref at_const_value;
6099 dw_attr_ref at_containing_type;
6100 dw_attr_ref at_count;
6101 dw_attr_ref at_data_location;
6102 dw_attr_ref at_data_member_location;
6103 dw_attr_ref at_decimal_scale;
6104 dw_attr_ref at_decimal_sign;
6105 dw_attr_ref at_default_value;
6106 dw_attr_ref at_digit_count;
6107 dw_attr_ref at_discr;
6108 dw_attr_ref at_discr_list;
6109 dw_attr_ref at_discr_value;
6110 dw_attr_ref at_encoding;
6111 dw_attr_ref at_endianity;
6112 dw_attr_ref at_explicit;
6113 dw_attr_ref at_is_optional;
6114 dw_attr_ref at_location;
6115 dw_attr_ref at_lower_bound;
6116 dw_attr_ref at_mutable;
6117 dw_attr_ref at_ordering;
6118 dw_attr_ref at_picture_string;
6119 dw_attr_ref at_prototyped;
6120 dw_attr_ref at_small;
6121 dw_attr_ref at_segment;
6122 dw_attr_ref at_string_length;
6123 dw_attr_ref at_threads_scaled;
6124 dw_attr_ref at_upper_bound;
6125 dw_attr_ref at_use_location;
6126 dw_attr_ref at_use_UTF8;
6127 dw_attr_ref at_variable_parameter;
6128 dw_attr_ref at_virtuality;
6129 dw_attr_ref at_visibility;
6130 dw_attr_ref at_vtable_elem_location;
6133 /* Collect the attributes that we will want to use for the checksum. */
6135 static void
6136 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
6138 dw_attr_ref a;
6139 unsigned ix;
6141 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6143 switch (a->dw_attr)
6145 case DW_AT_name:
6146 attrs->at_name = a;
6147 break;
6148 case DW_AT_type:
6149 attrs->at_type = a;
6150 break;
6151 case DW_AT_friend:
6152 attrs->at_friend = a;
6153 break;
6154 case DW_AT_accessibility:
6155 attrs->at_accessibility = a;
6156 break;
6157 case DW_AT_address_class:
6158 attrs->at_address_class = a;
6159 break;
6160 case DW_AT_allocated:
6161 attrs->at_allocated = a;
6162 break;
6163 case DW_AT_artificial:
6164 attrs->at_artificial = a;
6165 break;
6166 case DW_AT_associated:
6167 attrs->at_associated = a;
6168 break;
6169 case DW_AT_binary_scale:
6170 attrs->at_binary_scale = a;
6171 break;
6172 case DW_AT_bit_offset:
6173 attrs->at_bit_offset = a;
6174 break;
6175 case DW_AT_bit_size:
6176 attrs->at_bit_size = a;
6177 break;
6178 case DW_AT_bit_stride:
6179 attrs->at_bit_stride = a;
6180 break;
6181 case DW_AT_byte_size:
6182 attrs->at_byte_size = a;
6183 break;
6184 case DW_AT_byte_stride:
6185 attrs->at_byte_stride = a;
6186 break;
6187 case DW_AT_const_value:
6188 attrs->at_const_value = a;
6189 break;
6190 case DW_AT_containing_type:
6191 attrs->at_containing_type = a;
6192 break;
6193 case DW_AT_count:
6194 attrs->at_count = a;
6195 break;
6196 case DW_AT_data_location:
6197 attrs->at_data_location = a;
6198 break;
6199 case DW_AT_data_member_location:
6200 attrs->at_data_member_location = a;
6201 break;
6202 case DW_AT_decimal_scale:
6203 attrs->at_decimal_scale = a;
6204 break;
6205 case DW_AT_decimal_sign:
6206 attrs->at_decimal_sign = a;
6207 break;
6208 case DW_AT_default_value:
6209 attrs->at_default_value = a;
6210 break;
6211 case DW_AT_digit_count:
6212 attrs->at_digit_count = a;
6213 break;
6214 case DW_AT_discr:
6215 attrs->at_discr = a;
6216 break;
6217 case DW_AT_discr_list:
6218 attrs->at_discr_list = a;
6219 break;
6220 case DW_AT_discr_value:
6221 attrs->at_discr_value = a;
6222 break;
6223 case DW_AT_encoding:
6224 attrs->at_encoding = a;
6225 break;
6226 case DW_AT_endianity:
6227 attrs->at_endianity = a;
6228 break;
6229 case DW_AT_explicit:
6230 attrs->at_explicit = a;
6231 break;
6232 case DW_AT_is_optional:
6233 attrs->at_is_optional = a;
6234 break;
6235 case DW_AT_location:
6236 attrs->at_location = a;
6237 break;
6238 case DW_AT_lower_bound:
6239 attrs->at_lower_bound = a;
6240 break;
6241 case DW_AT_mutable:
6242 attrs->at_mutable = a;
6243 break;
6244 case DW_AT_ordering:
6245 attrs->at_ordering = a;
6246 break;
6247 case DW_AT_picture_string:
6248 attrs->at_picture_string = a;
6249 break;
6250 case DW_AT_prototyped:
6251 attrs->at_prototyped = a;
6252 break;
6253 case DW_AT_small:
6254 attrs->at_small = a;
6255 break;
6256 case DW_AT_segment:
6257 attrs->at_segment = a;
6258 break;
6259 case DW_AT_string_length:
6260 attrs->at_string_length = a;
6261 break;
6262 case DW_AT_threads_scaled:
6263 attrs->at_threads_scaled = a;
6264 break;
6265 case DW_AT_upper_bound:
6266 attrs->at_upper_bound = a;
6267 break;
6268 case DW_AT_use_location:
6269 attrs->at_use_location = a;
6270 break;
6271 case DW_AT_use_UTF8:
6272 attrs->at_use_UTF8 = a;
6273 break;
6274 case DW_AT_variable_parameter:
6275 attrs->at_variable_parameter = a;
6276 break;
6277 case DW_AT_virtuality:
6278 attrs->at_virtuality = a;
6279 break;
6280 case DW_AT_visibility:
6281 attrs->at_visibility = a;
6282 break;
6283 case DW_AT_vtable_elem_location:
6284 attrs->at_vtable_elem_location = a;
6285 break;
6286 default:
6287 break;
6292 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
6294 static void
6295 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6297 dw_die_ref c;
6298 dw_die_ref decl;
6299 struct checksum_attributes attrs;
6301 CHECKSUM_ULEB128 ('D');
6302 CHECKSUM_ULEB128 (die->die_tag);
6304 memset (&attrs, 0, sizeof (attrs));
6306 decl = get_AT_ref (die, DW_AT_specification);
6307 if (decl != NULL)
6308 collect_checksum_attributes (&attrs, decl);
6309 collect_checksum_attributes (&attrs, die);
6311 CHECKSUM_ATTR (attrs.at_name);
6312 CHECKSUM_ATTR (attrs.at_accessibility);
6313 CHECKSUM_ATTR (attrs.at_address_class);
6314 CHECKSUM_ATTR (attrs.at_allocated);
6315 CHECKSUM_ATTR (attrs.at_artificial);
6316 CHECKSUM_ATTR (attrs.at_associated);
6317 CHECKSUM_ATTR (attrs.at_binary_scale);
6318 CHECKSUM_ATTR (attrs.at_bit_offset);
6319 CHECKSUM_ATTR (attrs.at_bit_size);
6320 CHECKSUM_ATTR (attrs.at_bit_stride);
6321 CHECKSUM_ATTR (attrs.at_byte_size);
6322 CHECKSUM_ATTR (attrs.at_byte_stride);
6323 CHECKSUM_ATTR (attrs.at_const_value);
6324 CHECKSUM_ATTR (attrs.at_containing_type);
6325 CHECKSUM_ATTR (attrs.at_count);
6326 CHECKSUM_ATTR (attrs.at_data_location);
6327 CHECKSUM_ATTR (attrs.at_data_member_location);
6328 CHECKSUM_ATTR (attrs.at_decimal_scale);
6329 CHECKSUM_ATTR (attrs.at_decimal_sign);
6330 CHECKSUM_ATTR (attrs.at_default_value);
6331 CHECKSUM_ATTR (attrs.at_digit_count);
6332 CHECKSUM_ATTR (attrs.at_discr);
6333 CHECKSUM_ATTR (attrs.at_discr_list);
6334 CHECKSUM_ATTR (attrs.at_discr_value);
6335 CHECKSUM_ATTR (attrs.at_encoding);
6336 CHECKSUM_ATTR (attrs.at_endianity);
6337 CHECKSUM_ATTR (attrs.at_explicit);
6338 CHECKSUM_ATTR (attrs.at_is_optional);
6339 CHECKSUM_ATTR (attrs.at_location);
6340 CHECKSUM_ATTR (attrs.at_lower_bound);
6341 CHECKSUM_ATTR (attrs.at_mutable);
6342 CHECKSUM_ATTR (attrs.at_ordering);
6343 CHECKSUM_ATTR (attrs.at_picture_string);
6344 CHECKSUM_ATTR (attrs.at_prototyped);
6345 CHECKSUM_ATTR (attrs.at_small);
6346 CHECKSUM_ATTR (attrs.at_segment);
6347 CHECKSUM_ATTR (attrs.at_string_length);
6348 CHECKSUM_ATTR (attrs.at_threads_scaled);
6349 CHECKSUM_ATTR (attrs.at_upper_bound);
6350 CHECKSUM_ATTR (attrs.at_use_location);
6351 CHECKSUM_ATTR (attrs.at_use_UTF8);
6352 CHECKSUM_ATTR (attrs.at_variable_parameter);
6353 CHECKSUM_ATTR (attrs.at_virtuality);
6354 CHECKSUM_ATTR (attrs.at_visibility);
6355 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6356 CHECKSUM_ATTR (attrs.at_type);
6357 CHECKSUM_ATTR (attrs.at_friend);
6359 /* Checksum the child DIEs. */
6360 c = die->die_child;
6361 if (c) do {
6362 dw_attr_ref name_attr;
6364 c = c->die_sib;
6365 name_attr = get_AT (c, DW_AT_name);
6366 if (is_template_instantiation (c))
6368 /* Ignore instantiations of member type and function templates. */
6370 else if (name_attr != NULL
6371 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
6373 /* Use a shallow checksum for named nested types and member
6374 functions. */
6375 CHECKSUM_ULEB128 ('S');
6376 CHECKSUM_ULEB128 (c->die_tag);
6377 CHECKSUM_STRING (AT_string (name_attr));
6379 else
6381 /* Use a deep checksum for other children. */
6382 /* Mark this DIE so it gets processed when unmarking. */
6383 if (c->die_mark == 0)
6384 c->die_mark = -1;
6385 die_checksum_ordered (c, ctx, mark);
6387 } while (c != die->die_child);
6389 CHECKSUM_ULEB128 (0);
6392 /* Add a type name and tag to a hash. */
6393 static void
6394 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
6396 CHECKSUM_ULEB128 (tag);
6397 CHECKSUM_STRING (name);
6400 #undef CHECKSUM
6401 #undef CHECKSUM_STRING
6402 #undef CHECKSUM_ATTR
6403 #undef CHECKSUM_LEB128
6404 #undef CHECKSUM_ULEB128
6406 /* Generate the type signature for DIE. This is computed by generating an
6407 MD5 checksum over the DIE's tag, its relevant attributes, and its
6408 children. Attributes that are references to other DIEs are processed
6409 by recursion, using the MARK field to prevent infinite recursion.
6410 If the DIE is nested inside a namespace or another type, we also
6411 need to include that context in the signature. The lower 64 bits
6412 of the resulting MD5 checksum comprise the signature. */
6414 static void
6415 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6417 int mark;
6418 const char *name;
6419 unsigned char checksum[16];
6420 struct md5_ctx ctx;
6421 dw_die_ref decl;
6422 dw_die_ref parent;
6424 name = get_AT_string (die, DW_AT_name);
6425 decl = get_AT_ref (die, DW_AT_specification);
6426 parent = get_die_parent (die);
6428 /* First, compute a signature for just the type name (and its surrounding
6429 context, if any. This is stored in the type unit DIE for link-time
6430 ODR (one-definition rule) checking. */
6432 if (is_cxx () && name != NULL)
6434 md5_init_ctx (&ctx);
6436 /* Checksum the names of surrounding namespaces and structures. */
6437 if (parent != NULL)
6438 checksum_die_context (parent, &ctx);
6440 /* Checksum the current DIE. */
6441 die_odr_checksum (die->die_tag, name, &ctx);
6442 md5_finish_ctx (&ctx, checksum);
6444 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6447 /* Next, compute the complete type signature. */
6449 md5_init_ctx (&ctx);
6450 mark = 1;
6451 die->die_mark = mark;
6453 /* Checksum the names of surrounding namespaces and structures. */
6454 if (parent != NULL)
6455 checksum_die_context (parent, &ctx);
6457 /* Checksum the DIE and its children. */
6458 die_checksum_ordered (die, &ctx, &mark);
6459 unmark_all_dies (die);
6460 md5_finish_ctx (&ctx, checksum);
6462 /* Store the signature in the type node and link the type DIE and the
6463 type node together. */
6464 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6465 DWARF_TYPE_SIGNATURE_SIZE);
6466 die->comdat_type_p = true;
6467 die->die_id.die_type_node = type_node;
6468 type_node->type_die = die;
6470 /* If the DIE is a specification, link its declaration to the type node
6471 as well. */
6472 if (decl != NULL)
6474 decl->comdat_type_p = true;
6475 decl->die_id.die_type_node = type_node;
6479 /* Do the location expressions look same? */
6480 static inline int
6481 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6483 return loc1->dw_loc_opc == loc2->dw_loc_opc
6484 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6485 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6488 /* Do the values look the same? */
6489 static int
6490 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6492 dw_loc_descr_ref loc1, loc2;
6493 rtx r1, r2;
6495 if (v1->val_class != v2->val_class)
6496 return 0;
6498 switch (v1->val_class)
6500 case dw_val_class_const:
6501 return v1->v.val_int == v2->v.val_int;
6502 case dw_val_class_unsigned_const:
6503 return v1->v.val_unsigned == v2->v.val_unsigned;
6504 case dw_val_class_const_double:
6505 return v1->v.val_double.high == v2->v.val_double.high
6506 && v1->v.val_double.low == v2->v.val_double.low;
6507 case dw_val_class_wide_int:
6508 return *v1->v.val_wide == *v2->v.val_wide;
6509 case dw_val_class_vec:
6510 if (v1->v.val_vec.length != v2->v.val_vec.length
6511 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6512 return 0;
6513 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6514 v1->v.val_vec.length * v1->v.val_vec.elt_size))
6515 return 0;
6516 return 1;
6517 case dw_val_class_flag:
6518 return v1->v.val_flag == v2->v.val_flag;
6519 case dw_val_class_str:
6520 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
6522 case dw_val_class_addr:
6523 r1 = v1->v.val_addr;
6524 r2 = v2->v.val_addr;
6525 if (GET_CODE (r1) != GET_CODE (r2))
6526 return 0;
6527 return !rtx_equal_p (r1, r2);
6529 case dw_val_class_offset:
6530 return v1->v.val_offset == v2->v.val_offset;
6532 case dw_val_class_loc:
6533 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6534 loc1 && loc2;
6535 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6536 if (!same_loc_p (loc1, loc2, mark))
6537 return 0;
6538 return !loc1 && !loc2;
6540 case dw_val_class_die_ref:
6541 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6543 case dw_val_class_fde_ref:
6544 case dw_val_class_vms_delta:
6545 case dw_val_class_lbl_id:
6546 case dw_val_class_lineptr:
6547 case dw_val_class_macptr:
6548 case dw_val_class_high_pc:
6549 return 1;
6551 case dw_val_class_file:
6552 return v1->v.val_file == v2->v.val_file;
6554 case dw_val_class_data8:
6555 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
6557 default:
6558 return 1;
6562 /* Do the attributes look the same? */
6564 static int
6565 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6567 if (at1->dw_attr != at2->dw_attr)
6568 return 0;
6570 /* We don't care that this was compiled with a different compiler
6571 snapshot; if the output is the same, that's what matters. */
6572 if (at1->dw_attr == DW_AT_producer)
6573 return 1;
6575 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6578 /* Do the dies look the same? */
6580 static int
6581 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6583 dw_die_ref c1, c2;
6584 dw_attr_ref a1;
6585 unsigned ix;
6587 /* To avoid infinite recursion. */
6588 if (die1->die_mark)
6589 return die1->die_mark == die2->die_mark;
6590 die1->die_mark = die2->die_mark = ++(*mark);
6592 if (die1->die_tag != die2->die_tag)
6593 return 0;
6595 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
6596 return 0;
6598 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
6599 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
6600 return 0;
6602 c1 = die1->die_child;
6603 c2 = die2->die_child;
6604 if (! c1)
6606 if (c2)
6607 return 0;
6609 else
6610 for (;;)
6612 if (!same_die_p (c1, c2, mark))
6613 return 0;
6614 c1 = c1->die_sib;
6615 c2 = c2->die_sib;
6616 if (c1 == die1->die_child)
6618 if (c2 == die2->die_child)
6619 break;
6620 else
6621 return 0;
6625 return 1;
6628 /* Do the dies look the same? Wrapper around same_die_p. */
6630 static int
6631 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6633 int mark = 0;
6634 int ret = same_die_p (die1, die2, &mark);
6636 unmark_all_dies (die1);
6637 unmark_all_dies (die2);
6639 return ret;
6642 /* The prefix to attach to symbols on DIEs in the current comdat debug
6643 info section. */
6644 static const char *comdat_symbol_id;
6646 /* The index of the current symbol within the current comdat CU. */
6647 static unsigned int comdat_symbol_number;
6649 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6650 children, and set comdat_symbol_id accordingly. */
6652 static void
6653 compute_section_prefix (dw_die_ref unit_die)
6655 const char *die_name = get_AT_string (unit_die, DW_AT_name);
6656 const char *base = die_name ? lbasename (die_name) : "anonymous";
6657 char *name = XALLOCAVEC (char, strlen (base) + 64);
6658 char *p;
6659 int i, mark;
6660 unsigned char checksum[16];
6661 struct md5_ctx ctx;
6663 /* Compute the checksum of the DIE, then append part of it as hex digits to
6664 the name filename of the unit. */
6666 md5_init_ctx (&ctx);
6667 mark = 0;
6668 die_checksum (unit_die, &ctx, &mark);
6669 unmark_all_dies (unit_die);
6670 md5_finish_ctx (&ctx, checksum);
6672 sprintf (name, "%s.", base);
6673 clean_symbol_name (name);
6675 p = name + strlen (name);
6676 for (i = 0; i < 4; i++)
6678 sprintf (p, "%.2x", checksum[i]);
6679 p += 2;
6682 comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
6683 comdat_symbol_number = 0;
6686 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
6688 static int
6689 is_type_die (dw_die_ref die)
6691 switch (die->die_tag)
6693 case DW_TAG_array_type:
6694 case DW_TAG_class_type:
6695 case DW_TAG_interface_type:
6696 case DW_TAG_enumeration_type:
6697 case DW_TAG_pointer_type:
6698 case DW_TAG_reference_type:
6699 case DW_TAG_rvalue_reference_type:
6700 case DW_TAG_string_type:
6701 case DW_TAG_structure_type:
6702 case DW_TAG_subroutine_type:
6703 case DW_TAG_union_type:
6704 case DW_TAG_ptr_to_member_type:
6705 case DW_TAG_set_type:
6706 case DW_TAG_subrange_type:
6707 case DW_TAG_base_type:
6708 case DW_TAG_const_type:
6709 case DW_TAG_file_type:
6710 case DW_TAG_packed_type:
6711 case DW_TAG_volatile_type:
6712 case DW_TAG_typedef:
6713 return 1;
6714 default:
6715 return 0;
6719 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6720 Basically, we want to choose the bits that are likely to be shared between
6721 compilations (types) and leave out the bits that are specific to individual
6722 compilations (functions). */
6724 static int
6725 is_comdat_die (dw_die_ref c)
6727 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6728 we do for stabs. The advantage is a greater likelihood of sharing between
6729 objects that don't include headers in the same order (and therefore would
6730 put the base types in a different comdat). jason 8/28/00 */
6732 if (c->die_tag == DW_TAG_base_type)
6733 return 0;
6735 if (c->die_tag == DW_TAG_pointer_type
6736 || c->die_tag == DW_TAG_reference_type
6737 || c->die_tag == DW_TAG_rvalue_reference_type
6738 || c->die_tag == DW_TAG_const_type
6739 || c->die_tag == DW_TAG_volatile_type)
6741 dw_die_ref t = get_AT_ref (c, DW_AT_type);
6743 return t ? is_comdat_die (t) : 0;
6746 return is_type_die (c);
6749 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6750 compilation unit. */
6752 static int
6753 is_symbol_die (dw_die_ref c)
6755 return (is_type_die (c)
6756 || is_declaration_die (c)
6757 || c->die_tag == DW_TAG_namespace
6758 || c->die_tag == DW_TAG_module);
6761 /* Returns true iff C is a compile-unit DIE. */
6763 static inline bool
6764 is_cu_die (dw_die_ref c)
6766 return c && c->die_tag == DW_TAG_compile_unit;
6769 /* Returns true iff C is a unit DIE of some sort. */
6771 static inline bool
6772 is_unit_die (dw_die_ref c)
6774 return c && (c->die_tag == DW_TAG_compile_unit
6775 || c->die_tag == DW_TAG_partial_unit
6776 || c->die_tag == DW_TAG_type_unit);
6779 /* Returns true iff C is a namespace DIE. */
6781 static inline bool
6782 is_namespace_die (dw_die_ref c)
6784 return c && c->die_tag == DW_TAG_namespace;
6787 /* Returns true iff C is a class or structure DIE. */
6789 static inline bool
6790 is_class_die (dw_die_ref c)
6792 return c && (c->die_tag == DW_TAG_class_type
6793 || c->die_tag == DW_TAG_structure_type);
6796 /* Return non-zero if this DIE is a template parameter. */
6798 static inline bool
6799 is_template_parameter (dw_die_ref die)
6801 switch (die->die_tag)
6803 case DW_TAG_template_type_param:
6804 case DW_TAG_template_value_param:
6805 case DW_TAG_GNU_template_template_param:
6806 case DW_TAG_GNU_template_parameter_pack:
6807 return true;
6808 default:
6809 return false;
6813 /* Return non-zero if this DIE represents a template instantiation. */
6815 static inline bool
6816 is_template_instantiation (dw_die_ref die)
6818 dw_die_ref c;
6820 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
6821 return false;
6822 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
6823 return false;
6826 static char *
6827 gen_internal_sym (const char *prefix)
6829 char buf[256];
6831 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6832 return xstrdup (buf);
6835 /* Assign symbols to all worthy DIEs under DIE. */
6837 static void
6838 assign_symbol_names (dw_die_ref die)
6840 dw_die_ref c;
6842 if (is_symbol_die (die) && !die->comdat_type_p)
6844 if (comdat_symbol_id)
6846 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
6848 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6849 comdat_symbol_id, comdat_symbol_number++);
6850 die->die_id.die_symbol = xstrdup (p);
6852 else
6853 die->die_id.die_symbol = gen_internal_sym ("LDIE");
6856 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6859 struct cu_hash_table_entry
6861 dw_die_ref cu;
6862 unsigned min_comdat_num, max_comdat_num;
6863 struct cu_hash_table_entry *next;
6866 /* Helpers to manipulate hash table of CUs. */
6868 struct cu_hash_table_entry_hasher
6870 typedef cu_hash_table_entry *value_type;
6871 typedef die_struct *compare_type;
6872 static inline hashval_t hash (const cu_hash_table_entry *);
6873 static inline bool equal (const cu_hash_table_entry *, const die_struct *);
6874 static inline void remove (cu_hash_table_entry *);
6877 inline hashval_t
6878 cu_hash_table_entry_hasher::hash (const cu_hash_table_entry *entry)
6880 return htab_hash_string (entry->cu->die_id.die_symbol);
6883 inline bool
6884 cu_hash_table_entry_hasher::equal (const cu_hash_table_entry *entry1,
6885 const die_struct *entry2)
6887 return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
6890 inline void
6891 cu_hash_table_entry_hasher::remove (cu_hash_table_entry *entry)
6893 struct cu_hash_table_entry *next;
6895 while (entry)
6897 next = entry->next;
6898 free (entry);
6899 entry = next;
6903 typedef hash_table<cu_hash_table_entry_hasher> cu_hash_type;
6905 /* Check whether we have already seen this CU and set up SYM_NUM
6906 accordingly. */
6907 static int
6908 check_duplicate_cu (dw_die_ref cu, cu_hash_type *htable, unsigned int *sym_num)
6910 struct cu_hash_table_entry dummy;
6911 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6913 dummy.max_comdat_num = 0;
6915 slot = htable->find_slot_with_hash (cu,
6916 htab_hash_string (cu->die_id.die_symbol),
6917 INSERT);
6918 entry = *slot;
6920 for (; entry; last = entry, entry = entry->next)
6922 if (same_die_p_wrap (cu, entry->cu))
6923 break;
6926 if (entry)
6928 *sym_num = entry->min_comdat_num;
6929 return 1;
6932 entry = XCNEW (struct cu_hash_table_entry);
6933 entry->cu = cu;
6934 entry->min_comdat_num = *sym_num = last->max_comdat_num;
6935 entry->next = *slot;
6936 *slot = entry;
6938 return 0;
6941 /* Record SYM_NUM to record of CU in HTABLE. */
6942 static void
6943 record_comdat_symbol_number (dw_die_ref cu, cu_hash_type *htable,
6944 unsigned int sym_num)
6946 struct cu_hash_table_entry **slot, *entry;
6948 slot = htable->find_slot_with_hash (cu,
6949 htab_hash_string (cu->die_id.die_symbol),
6950 NO_INSERT);
6951 entry = *slot;
6953 entry->max_comdat_num = sym_num;
6956 /* Traverse the DIE (which is always comp_unit_die), and set up
6957 additional compilation units for each of the include files we see
6958 bracketed by BINCL/EINCL. */
6960 static void
6961 break_out_includes (dw_die_ref die)
6963 dw_die_ref c;
6964 dw_die_ref unit = NULL;
6965 limbo_die_node *node, **pnode;
6967 c = die->die_child;
6968 if (c) do {
6969 dw_die_ref prev = c;
6970 c = c->die_sib;
6971 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6972 || (unit && is_comdat_die (c)))
6974 dw_die_ref next = c->die_sib;
6976 /* This DIE is for a secondary CU; remove it from the main one. */
6977 remove_child_with_prev (c, prev);
6979 if (c->die_tag == DW_TAG_GNU_BINCL)
6980 unit = push_new_compile_unit (unit, c);
6981 else if (c->die_tag == DW_TAG_GNU_EINCL)
6982 unit = pop_compile_unit (unit);
6983 else
6984 add_child_die (unit, c);
6985 c = next;
6986 if (c == die->die_child)
6987 break;
6989 } while (c != die->die_child);
6991 #if 0
6992 /* We can only use this in debugging, since the frontend doesn't check
6993 to make sure that we leave every include file we enter. */
6994 gcc_assert (!unit);
6995 #endif
6997 assign_symbol_names (die);
6998 cu_hash_type cu_hash_table (10);
6999 for (node = limbo_die_list, pnode = &limbo_die_list;
7000 node;
7001 node = node->next)
7003 int is_dupl;
7005 compute_section_prefix (node->die);
7006 is_dupl = check_duplicate_cu (node->die, &cu_hash_table,
7007 &comdat_symbol_number);
7008 assign_symbol_names (node->die);
7009 if (is_dupl)
7010 *pnode = node->next;
7011 else
7013 pnode = &node->next;
7014 record_comdat_symbol_number (node->die, &cu_hash_table,
7015 comdat_symbol_number);
7020 /* Return non-zero if this DIE is a declaration. */
7022 static int
7023 is_declaration_die (dw_die_ref die)
7025 dw_attr_ref a;
7026 unsigned ix;
7028 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7029 if (a->dw_attr == DW_AT_declaration)
7030 return 1;
7032 return 0;
7035 /* Return non-zero if this DIE is nested inside a subprogram. */
7037 static int
7038 is_nested_in_subprogram (dw_die_ref die)
7040 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
7042 if (decl == NULL)
7043 decl = die;
7044 return local_scope_p (decl);
7047 /* Return non-zero if this DIE contains a defining declaration of a
7048 subprogram. */
7050 static int
7051 contains_subprogram_definition (dw_die_ref die)
7053 dw_die_ref c;
7055 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
7056 return 1;
7057 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
7058 return 0;
7061 /* Return non-zero if this is a type DIE that should be moved to a
7062 COMDAT .debug_types section. */
7064 static int
7065 should_move_die_to_comdat (dw_die_ref die)
7067 switch (die->die_tag)
7069 case DW_TAG_class_type:
7070 case DW_TAG_structure_type:
7071 case DW_TAG_enumeration_type:
7072 case DW_TAG_union_type:
7073 /* Don't move declarations, inlined instances, types nested in a
7074 subprogram, or types that contain subprogram definitions. */
7075 if (is_declaration_die (die)
7076 || get_AT (die, DW_AT_abstract_origin)
7077 || is_nested_in_subprogram (die)
7078 || contains_subprogram_definition (die))
7079 return 0;
7080 return 1;
7081 case DW_TAG_array_type:
7082 case DW_TAG_interface_type:
7083 case DW_TAG_pointer_type:
7084 case DW_TAG_reference_type:
7085 case DW_TAG_rvalue_reference_type:
7086 case DW_TAG_string_type:
7087 case DW_TAG_subroutine_type:
7088 case DW_TAG_ptr_to_member_type:
7089 case DW_TAG_set_type:
7090 case DW_TAG_subrange_type:
7091 case DW_TAG_base_type:
7092 case DW_TAG_const_type:
7093 case DW_TAG_file_type:
7094 case DW_TAG_packed_type:
7095 case DW_TAG_volatile_type:
7096 case DW_TAG_typedef:
7097 default:
7098 return 0;
7102 /* Make a clone of DIE. */
7104 static dw_die_ref
7105 clone_die (dw_die_ref die)
7107 dw_die_ref clone;
7108 dw_attr_ref a;
7109 unsigned ix;
7111 clone = ggc_cleared_alloc<die_node> ();
7112 clone->die_tag = die->die_tag;
7114 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7115 add_dwarf_attr (clone, a);
7117 return clone;
7120 /* Make a clone of the tree rooted at DIE. */
7122 static dw_die_ref
7123 clone_tree (dw_die_ref die)
7125 dw_die_ref c;
7126 dw_die_ref clone = clone_die (die);
7128 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
7130 return clone;
7133 /* Make a clone of DIE as a declaration. */
7135 static dw_die_ref
7136 clone_as_declaration (dw_die_ref die)
7138 dw_die_ref clone;
7139 dw_die_ref decl;
7140 dw_attr_ref a;
7141 unsigned ix;
7143 /* If the DIE is already a declaration, just clone it. */
7144 if (is_declaration_die (die))
7145 return clone_die (die);
7147 /* If the DIE is a specification, just clone its declaration DIE. */
7148 decl = get_AT_ref (die, DW_AT_specification);
7149 if (decl != NULL)
7151 clone = clone_die (decl);
7152 if (die->comdat_type_p)
7153 add_AT_die_ref (clone, DW_AT_signature, die);
7154 return clone;
7157 clone = ggc_cleared_alloc<die_node> ();
7158 clone->die_tag = die->die_tag;
7160 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7162 /* We don't want to copy over all attributes.
7163 For example we don't want DW_AT_byte_size because otherwise we will no
7164 longer have a declaration and GDB will treat it as a definition. */
7166 switch (a->dw_attr)
7168 case DW_AT_abstract_origin:
7169 case DW_AT_artificial:
7170 case DW_AT_containing_type:
7171 case DW_AT_external:
7172 case DW_AT_name:
7173 case DW_AT_type:
7174 case DW_AT_virtuality:
7175 case DW_AT_linkage_name:
7176 case DW_AT_MIPS_linkage_name:
7177 add_dwarf_attr (clone, a);
7178 break;
7179 case DW_AT_byte_size:
7180 default:
7181 break;
7185 if (die->comdat_type_p)
7186 add_AT_die_ref (clone, DW_AT_signature, die);
7188 add_AT_flag (clone, DW_AT_declaration, 1);
7189 return clone;
7193 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
7195 struct decl_table_entry
7197 dw_die_ref orig;
7198 dw_die_ref copy;
7201 /* Helpers to manipulate hash table of copied declarations. */
7203 /* Hashtable helpers. */
7205 struct decl_table_entry_hasher : typed_free_remove <decl_table_entry>
7207 typedef decl_table_entry *value_type;
7208 typedef die_struct *compare_type;
7209 static inline hashval_t hash (const decl_table_entry *);
7210 static inline bool equal (const decl_table_entry *, const die_struct *);
7213 inline hashval_t
7214 decl_table_entry_hasher::hash (const decl_table_entry *entry)
7216 return htab_hash_pointer (entry->orig);
7219 inline bool
7220 decl_table_entry_hasher::equal (const decl_table_entry *entry1,
7221 const die_struct *entry2)
7223 return entry1->orig == entry2;
7226 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
7228 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7229 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
7230 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
7231 to check if the ancestor has already been copied into UNIT. */
7233 static dw_die_ref
7234 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
7235 decl_hash_type *decl_table)
7237 dw_die_ref parent = die->die_parent;
7238 dw_die_ref new_parent = unit;
7239 dw_die_ref copy;
7240 decl_table_entry **slot = NULL;
7241 struct decl_table_entry *entry = NULL;
7243 if (decl_table)
7245 /* Check if the entry has already been copied to UNIT. */
7246 slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
7247 INSERT);
7248 if (*slot != HTAB_EMPTY_ENTRY)
7250 entry = *slot;
7251 return entry->copy;
7254 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
7255 entry = XCNEW (struct decl_table_entry);
7256 entry->orig = die;
7257 entry->copy = NULL;
7258 *slot = entry;
7261 if (parent != NULL)
7263 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7264 if (spec != NULL)
7265 parent = spec;
7266 if (!is_unit_die (parent))
7267 new_parent = copy_ancestor_tree (unit, parent, decl_table);
7270 copy = clone_as_declaration (die);
7271 add_child_die (new_parent, copy);
7273 if (decl_table)
7275 /* Record the pointer to the copy. */
7276 entry->copy = copy;
7279 return copy;
7281 /* Copy the declaration context to the new type unit DIE. This includes
7282 any surrounding namespace or type declarations. If the DIE has an
7283 AT_specification attribute, it also includes attributes and children
7284 attached to the specification, and returns a pointer to the original
7285 parent of the declaration DIE. Returns NULL otherwise. */
7287 static dw_die_ref
7288 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7290 dw_die_ref decl;
7291 dw_die_ref new_decl;
7292 dw_die_ref orig_parent = NULL;
7294 decl = get_AT_ref (die, DW_AT_specification);
7295 if (decl == NULL)
7296 decl = die;
7297 else
7299 unsigned ix;
7300 dw_die_ref c;
7301 dw_attr_ref a;
7303 /* The original DIE will be changed to a declaration, and must
7304 be moved to be a child of the original declaration DIE. */
7305 orig_parent = decl->die_parent;
7307 /* Copy the type node pointer from the new DIE to the original
7308 declaration DIE so we can forward references later. */
7309 decl->comdat_type_p = true;
7310 decl->die_id.die_type_node = die->die_id.die_type_node;
7312 remove_AT (die, DW_AT_specification);
7314 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7316 if (a->dw_attr != DW_AT_name
7317 && a->dw_attr != DW_AT_declaration
7318 && a->dw_attr != DW_AT_external)
7319 add_dwarf_attr (die, a);
7322 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7325 if (decl->die_parent != NULL
7326 && !is_unit_die (decl->die_parent))
7328 new_decl = copy_ancestor_tree (unit, decl, NULL);
7329 if (new_decl != NULL)
7331 remove_AT (new_decl, DW_AT_signature);
7332 add_AT_specification (die, new_decl);
7336 return orig_parent;
7339 /* Generate the skeleton ancestor tree for the given NODE, then clone
7340 the DIE and add the clone into the tree. */
7342 static void
7343 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7345 if (node->new_die != NULL)
7346 return;
7348 node->new_die = clone_as_declaration (node->old_die);
7350 if (node->parent != NULL)
7352 generate_skeleton_ancestor_tree (node->parent);
7353 add_child_die (node->parent->new_die, node->new_die);
7357 /* Generate a skeleton tree of DIEs containing any declarations that are
7358 found in the original tree. We traverse the tree looking for declaration
7359 DIEs, and construct the skeleton from the bottom up whenever we find one. */
7361 static void
7362 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7364 skeleton_chain_node node;
7365 dw_die_ref c;
7366 dw_die_ref first;
7367 dw_die_ref prev = NULL;
7368 dw_die_ref next = NULL;
7370 node.parent = parent;
7372 first = c = parent->old_die->die_child;
7373 if (c)
7374 next = c->die_sib;
7375 if (c) do {
7376 if (prev == NULL || prev->die_sib == c)
7377 prev = c;
7378 c = next;
7379 next = (c == first ? NULL : c->die_sib);
7380 node.old_die = c;
7381 node.new_die = NULL;
7382 if (is_declaration_die (c))
7384 if (is_template_instantiation (c))
7386 /* Instantiated templates do not need to be cloned into the
7387 type unit. Just move the DIE and its children back to
7388 the skeleton tree (in the main CU). */
7389 remove_child_with_prev (c, prev);
7390 add_child_die (parent->new_die, c);
7391 c = prev;
7393 else
7395 /* Clone the existing DIE, move the original to the skeleton
7396 tree (which is in the main CU), and put the clone, with
7397 all the original's children, where the original came from
7398 (which is about to be moved to the type unit). */
7399 dw_die_ref clone = clone_die (c);
7400 move_all_children (c, clone);
7402 /* If the original has a DW_AT_object_pointer attribute,
7403 it would now point to a child DIE just moved to the
7404 cloned tree, so we need to remove that attribute from
7405 the original. */
7406 remove_AT (c, DW_AT_object_pointer);
7408 replace_child (c, clone, prev);
7409 generate_skeleton_ancestor_tree (parent);
7410 add_child_die (parent->new_die, c);
7411 node.new_die = c;
7412 c = clone;
7415 generate_skeleton_bottom_up (&node);
7416 } while (next != NULL);
7419 /* Wrapper function for generate_skeleton_bottom_up. */
7421 static dw_die_ref
7422 generate_skeleton (dw_die_ref die)
7424 skeleton_chain_node node;
7426 node.old_die = die;
7427 node.new_die = NULL;
7428 node.parent = NULL;
7430 /* If this type definition is nested inside another type,
7431 and is not an instantiation of a template, always leave
7432 at least a declaration in its place. */
7433 if (die->die_parent != NULL
7434 && is_type_die (die->die_parent)
7435 && !is_template_instantiation (die))
7436 node.new_die = clone_as_declaration (die);
7438 generate_skeleton_bottom_up (&node);
7439 return node.new_die;
7442 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
7443 declaration. The original DIE is moved to a new compile unit so that
7444 existing references to it follow it to the new location. If any of the
7445 original DIE's descendants is a declaration, we need to replace the
7446 original DIE with a skeleton tree and move the declarations back into the
7447 skeleton tree. */
7449 static dw_die_ref
7450 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
7451 dw_die_ref prev)
7453 dw_die_ref skeleton, orig_parent;
7455 /* Copy the declaration context to the type unit DIE. If the returned
7456 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
7457 that DIE. */
7458 orig_parent = copy_declaration_context (unit, child);
7460 skeleton = generate_skeleton (child);
7461 if (skeleton == NULL)
7462 remove_child_with_prev (child, prev);
7463 else
7465 skeleton->comdat_type_p = true;
7466 skeleton->die_id.die_type_node = child->die_id.die_type_node;
7468 /* If the original DIE was a specification, we need to put
7469 the skeleton under the parent DIE of the declaration.
7470 This leaves the original declaration in the tree, but
7471 it will be pruned later since there are no longer any
7472 references to it. */
7473 if (orig_parent != NULL)
7475 remove_child_with_prev (child, prev);
7476 add_child_die (orig_parent, skeleton);
7478 else
7479 replace_child (child, skeleton, prev);
7482 return skeleton;
7485 /* Traverse the DIE and set up additional .debug_types sections for each
7486 type worthy of being placed in a COMDAT section. */
7488 static void
7489 break_out_comdat_types (dw_die_ref die)
7491 dw_die_ref c;
7492 dw_die_ref first;
7493 dw_die_ref prev = NULL;
7494 dw_die_ref next = NULL;
7495 dw_die_ref unit = NULL;
7497 first = c = die->die_child;
7498 if (c)
7499 next = c->die_sib;
7500 if (c) do {
7501 if (prev == NULL || prev->die_sib == c)
7502 prev = c;
7503 c = next;
7504 next = (c == first ? NULL : c->die_sib);
7505 if (should_move_die_to_comdat (c))
7507 dw_die_ref replacement;
7508 comdat_type_node_ref type_node;
7510 /* Break out nested types into their own type units. */
7511 break_out_comdat_types (c);
7513 /* Create a new type unit DIE as the root for the new tree, and
7514 add it to the list of comdat types. */
7515 unit = new_die (DW_TAG_type_unit, NULL, NULL);
7516 add_AT_unsigned (unit, DW_AT_language,
7517 get_AT_unsigned (comp_unit_die (), DW_AT_language));
7518 type_node = ggc_cleared_alloc<comdat_type_node> ();
7519 type_node->root_die = unit;
7520 type_node->next = comdat_type_list;
7521 comdat_type_list = type_node;
7523 /* Generate the type signature. */
7524 generate_type_signature (c, type_node);
7526 /* Copy the declaration context, attributes, and children of the
7527 declaration into the new type unit DIE, then remove this DIE
7528 from the main CU (or replace it with a skeleton if necessary). */
7529 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
7530 type_node->skeleton_die = replacement;
7532 /* Add the DIE to the new compunit. */
7533 add_child_die (unit, c);
7535 if (replacement != NULL)
7536 c = replacement;
7538 else if (c->die_tag == DW_TAG_namespace
7539 || c->die_tag == DW_TAG_class_type
7540 || c->die_tag == DW_TAG_structure_type
7541 || c->die_tag == DW_TAG_union_type)
7543 /* Look for nested types that can be broken out. */
7544 break_out_comdat_types (c);
7546 } while (next != NULL);
7549 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
7550 Enter all the cloned children into the hash table decl_table. */
7552 static dw_die_ref
7553 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
7555 dw_die_ref c;
7556 dw_die_ref clone;
7557 struct decl_table_entry *entry;
7558 decl_table_entry **slot;
7560 if (die->die_tag == DW_TAG_subprogram)
7561 clone = clone_as_declaration (die);
7562 else
7563 clone = clone_die (die);
7565 slot = decl_table->find_slot_with_hash (die,
7566 htab_hash_pointer (die), INSERT);
7568 /* Assert that DIE isn't in the hash table yet. If it would be there
7569 before, the ancestors would be necessarily there as well, therefore
7570 clone_tree_partial wouldn't be called. */
7571 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
7573 entry = XCNEW (struct decl_table_entry);
7574 entry->orig = die;
7575 entry->copy = clone;
7576 *slot = entry;
7578 if (die->die_tag != DW_TAG_subprogram)
7579 FOR_EACH_CHILD (die, c,
7580 add_child_die (clone, clone_tree_partial (c, decl_table)));
7582 return clone;
7585 /* Walk the DIE and its children, looking for references to incomplete
7586 or trivial types that are unmarked (i.e., that are not in the current
7587 type_unit). */
7589 static void
7590 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
7592 dw_die_ref c;
7593 dw_attr_ref a;
7594 unsigned ix;
7596 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7598 if (AT_class (a) == dw_val_class_die_ref)
7600 dw_die_ref targ = AT_ref (a);
7601 decl_table_entry **slot;
7602 struct decl_table_entry *entry;
7604 if (targ->die_mark != 0 || targ->comdat_type_p)
7605 continue;
7607 slot = decl_table->find_slot_with_hash (targ,
7608 htab_hash_pointer (targ),
7609 INSERT);
7611 if (*slot != HTAB_EMPTY_ENTRY)
7613 /* TARG has already been copied, so we just need to
7614 modify the reference to point to the copy. */
7615 entry = *slot;
7616 a->dw_attr_val.v.val_die_ref.die = entry->copy;
7618 else
7620 dw_die_ref parent = unit;
7621 dw_die_ref copy = clone_die (targ);
7623 /* Record in DECL_TABLE that TARG has been copied.
7624 Need to do this now, before the recursive call,
7625 because DECL_TABLE may be expanded and SLOT
7626 would no longer be a valid pointer. */
7627 entry = XCNEW (struct decl_table_entry);
7628 entry->orig = targ;
7629 entry->copy = copy;
7630 *slot = entry;
7632 /* If TARG is not a declaration DIE, we need to copy its
7633 children. */
7634 if (!is_declaration_die (targ))
7636 FOR_EACH_CHILD (
7637 targ, c,
7638 add_child_die (copy,
7639 clone_tree_partial (c, decl_table)));
7642 /* Make sure the cloned tree is marked as part of the
7643 type unit. */
7644 mark_dies (copy);
7646 /* If TARG has surrounding context, copy its ancestor tree
7647 into the new type unit. */
7648 if (targ->die_parent != NULL
7649 && !is_unit_die (targ->die_parent))
7650 parent = copy_ancestor_tree (unit, targ->die_parent,
7651 decl_table);
7653 add_child_die (parent, copy);
7654 a->dw_attr_val.v.val_die_ref.die = copy;
7656 /* Make sure the newly-copied DIE is walked. If it was
7657 installed in a previously-added context, it won't
7658 get visited otherwise. */
7659 if (parent != unit)
7661 /* Find the highest point of the newly-added tree,
7662 mark each node along the way, and walk from there. */
7663 parent->die_mark = 1;
7664 while (parent->die_parent
7665 && parent->die_parent->die_mark == 0)
7667 parent = parent->die_parent;
7668 parent->die_mark = 1;
7670 copy_decls_walk (unit, parent, decl_table);
7676 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
7679 /* Copy declarations for "unworthy" types into the new comdat section.
7680 Incomplete types, modified types, and certain other types aren't broken
7681 out into comdat sections of their own, so they don't have a signature,
7682 and we need to copy the declaration into the same section so that we
7683 don't have an external reference. */
7685 static void
7686 copy_decls_for_unworthy_types (dw_die_ref unit)
7688 mark_dies (unit);
7689 decl_hash_type decl_table (10);
7690 copy_decls_walk (unit, unit, &decl_table);
7691 unmark_dies (unit);
7694 /* Traverse the DIE and add a sibling attribute if it may have the
7695 effect of speeding up access to siblings. To save some space,
7696 avoid generating sibling attributes for DIE's without children. */
7698 static void
7699 add_sibling_attributes (dw_die_ref die)
7701 dw_die_ref c;
7703 if (! die->die_child)
7704 return;
7706 if (die->die_parent && die != die->die_parent->die_child)
7707 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7709 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7712 /* Output all location lists for the DIE and its children. */
7714 static void
7715 output_location_lists (dw_die_ref die)
7717 dw_die_ref c;
7718 dw_attr_ref a;
7719 unsigned ix;
7721 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7722 if (AT_class (a) == dw_val_class_loc_list)
7723 output_loc_list (AT_loc_list (a));
7725 FOR_EACH_CHILD (die, c, output_location_lists (c));
7728 /* We want to limit the number of external references, because they are
7729 larger than local references: a relocation takes multiple words, and
7730 even a sig8 reference is always eight bytes, whereas a local reference
7731 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
7732 So if we encounter multiple external references to the same type DIE, we
7733 make a local typedef stub for it and redirect all references there.
7735 This is the element of the hash table for keeping track of these
7736 references. */
7738 struct external_ref
7740 dw_die_ref type;
7741 dw_die_ref stub;
7742 unsigned n_refs;
7745 /* Hashtable helpers. */
7747 struct external_ref_hasher : typed_free_remove <external_ref>
7749 typedef external_ref *value_type;
7750 typedef external_ref *compare_type;
7751 static inline hashval_t hash (const external_ref *);
7752 static inline bool equal (const external_ref *, const external_ref *);
7755 inline hashval_t
7756 external_ref_hasher::hash (const external_ref *r)
7758 dw_die_ref die = r->type;
7759 hashval_t h = 0;
7761 /* We can't use the address of the DIE for hashing, because
7762 that will make the order of the stub DIEs non-deterministic. */
7763 if (! die->comdat_type_p)
7764 /* We have a symbol; use it to compute a hash. */
7765 h = htab_hash_string (die->die_id.die_symbol);
7766 else
7768 /* We have a type signature; use a subset of the bits as the hash.
7769 The 8-byte signature is at least as large as hashval_t. */
7770 comdat_type_node_ref type_node = die->die_id.die_type_node;
7771 memcpy (&h, type_node->signature, sizeof (h));
7773 return h;
7776 inline bool
7777 external_ref_hasher::equal (const external_ref *r1, const external_ref *r2)
7779 return r1->type == r2->type;
7782 typedef hash_table<external_ref_hasher> external_ref_hash_type;
7784 /* Return a pointer to the external_ref for references to DIE. */
7786 static struct external_ref *
7787 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
7789 struct external_ref ref, *ref_p;
7790 external_ref **slot;
7792 ref.type = die;
7793 slot = map->find_slot (&ref, INSERT);
7794 if (*slot != HTAB_EMPTY_ENTRY)
7795 return *slot;
7797 ref_p = XCNEW (struct external_ref);
7798 ref_p->type = die;
7799 *slot = ref_p;
7800 return ref_p;
7803 /* Subroutine of optimize_external_refs, below.
7805 If we see a type skeleton, record it as our stub. If we see external
7806 references, remember how many we've seen. */
7808 static void
7809 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
7811 dw_die_ref c;
7812 dw_attr_ref a;
7813 unsigned ix;
7814 struct external_ref *ref_p;
7816 if (is_type_die (die)
7817 && (c = get_AT_ref (die, DW_AT_signature)))
7819 /* This is a local skeleton; use it for local references. */
7820 ref_p = lookup_external_ref (map, c);
7821 ref_p->stub = die;
7824 /* Scan the DIE references, and remember any that refer to DIEs from
7825 other CUs (i.e. those which are not marked). */
7826 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7827 if (AT_class (a) == dw_val_class_die_ref
7828 && (c = AT_ref (a))->die_mark == 0
7829 && is_type_die (c))
7831 ref_p = lookup_external_ref (map, c);
7832 ref_p->n_refs++;
7835 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
7838 /* htab_traverse callback function for optimize_external_refs, below. SLOT
7839 points to an external_ref, DATA is the CU we're processing. If we don't
7840 already have a local stub, and we have multiple refs, build a stub. */
7843 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
7845 struct external_ref *ref_p = *slot;
7847 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
7849 /* We have multiple references to this type, so build a small stub.
7850 Both of these forms are a bit dodgy from the perspective of the
7851 DWARF standard, since technically they should have names. */
7852 dw_die_ref cu = data;
7853 dw_die_ref type = ref_p->type;
7854 dw_die_ref stub = NULL;
7856 if (type->comdat_type_p)
7858 /* If we refer to this type via sig8, use AT_signature. */
7859 stub = new_die (type->die_tag, cu, NULL_TREE);
7860 add_AT_die_ref (stub, DW_AT_signature, type);
7862 else
7864 /* Otherwise, use a typedef with no name. */
7865 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
7866 add_AT_die_ref (stub, DW_AT_type, type);
7869 stub->die_mark++;
7870 ref_p->stub = stub;
7872 return 1;
7875 /* DIE is a unit; look through all the DIE references to see if there are
7876 any external references to types, and if so, create local stubs for
7877 them which will be applied in build_abbrev_table. This is useful because
7878 references to local DIEs are smaller. */
7880 static external_ref_hash_type *
7881 optimize_external_refs (dw_die_ref die)
7883 external_ref_hash_type *map = new external_ref_hash_type (10);
7884 optimize_external_refs_1 (die, map);
7885 map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
7886 return map;
7889 /* The format of each DIE (and its attribute value pairs) is encoded in an
7890 abbreviation table. This routine builds the abbreviation table and assigns
7891 a unique abbreviation id for each abbreviation entry. The children of each
7892 die are visited recursively. */
7894 static void
7895 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
7897 unsigned long abbrev_id;
7898 unsigned int n_alloc;
7899 dw_die_ref c;
7900 dw_attr_ref a;
7901 unsigned ix;
7903 /* Scan the DIE references, and replace any that refer to
7904 DIEs from other CUs (i.e. those which are not marked) with
7905 the local stubs we built in optimize_external_refs. */
7906 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7907 if (AT_class (a) == dw_val_class_die_ref
7908 && (c = AT_ref (a))->die_mark == 0)
7910 struct external_ref *ref_p;
7911 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
7913 ref_p = lookup_external_ref (extern_map, c);
7914 if (ref_p->stub && ref_p->stub != die)
7915 change_AT_die_ref (a, ref_p->stub);
7916 else
7917 /* We aren't changing this reference, so mark it external. */
7918 set_AT_ref_external (a, 1);
7921 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7923 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7924 dw_attr_ref die_a, abbrev_a;
7925 unsigned ix;
7926 bool ok = true;
7928 if (abbrev->die_tag != die->die_tag)
7929 continue;
7930 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7931 continue;
7933 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
7934 continue;
7936 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
7938 abbrev_a = &(*abbrev->die_attr)[ix];
7939 if ((abbrev_a->dw_attr != die_a->dw_attr)
7940 || (value_format (abbrev_a) != value_format (die_a)))
7942 ok = false;
7943 break;
7946 if (ok)
7947 break;
7950 if (abbrev_id >= abbrev_die_table_in_use)
7952 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7954 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
7955 abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7956 n_alloc);
7958 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7959 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
7960 abbrev_die_table_allocated = n_alloc;
7963 ++abbrev_die_table_in_use;
7964 abbrev_die_table[abbrev_id] = die;
7967 die->die_abbrev = abbrev_id;
7968 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
7971 /* Return the power-of-two number of bytes necessary to represent VALUE. */
7973 static int
7974 constant_size (unsigned HOST_WIDE_INT value)
7976 int log;
7978 if (value == 0)
7979 log = 0;
7980 else
7981 log = floor_log2 (value);
7983 log = log / 8;
7984 log = 1 << (floor_log2 (log) + 1);
7986 return log;
7989 /* Return the size of a DIE as it is represented in the
7990 .debug_info section. */
7992 static unsigned long
7993 size_of_die (dw_die_ref die)
7995 unsigned long size = 0;
7996 dw_attr_ref a;
7997 unsigned ix;
7998 enum dwarf_form form;
8000 size += size_of_uleb128 (die->die_abbrev);
8001 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8003 switch (AT_class (a))
8005 case dw_val_class_addr:
8006 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8008 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8009 size += size_of_uleb128 (AT_index (a));
8011 else
8012 size += DWARF2_ADDR_SIZE;
8013 break;
8014 case dw_val_class_offset:
8015 size += DWARF_OFFSET_SIZE;
8016 break;
8017 case dw_val_class_loc:
8019 unsigned long lsize = size_of_locs (AT_loc (a));
8021 /* Block length. */
8022 if (dwarf_version >= 4)
8023 size += size_of_uleb128 (lsize);
8024 else
8025 size += constant_size (lsize);
8026 size += lsize;
8028 break;
8029 case dw_val_class_loc_list:
8030 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8032 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8033 size += size_of_uleb128 (AT_index (a));
8035 else
8036 size += DWARF_OFFSET_SIZE;
8037 break;
8038 case dw_val_class_range_list:
8039 size += DWARF_OFFSET_SIZE;
8040 break;
8041 case dw_val_class_const:
8042 size += size_of_sleb128 (AT_int (a));
8043 break;
8044 case dw_val_class_unsigned_const:
8046 int csize = constant_size (AT_unsigned (a));
8047 if (dwarf_version == 3
8048 && a->dw_attr == DW_AT_data_member_location
8049 && csize >= 4)
8050 size += size_of_uleb128 (AT_unsigned (a));
8051 else
8052 size += csize;
8054 break;
8055 case dw_val_class_const_double:
8056 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
8057 if (HOST_BITS_PER_WIDE_INT >= 64)
8058 size++; /* block */
8059 break;
8060 case dw_val_class_wide_int:
8061 size += (get_full_len (*a->dw_attr_val.v.val_wide)
8062 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
8063 if (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT
8064 > 64)
8065 size++; /* block */
8066 break;
8067 case dw_val_class_vec:
8068 size += constant_size (a->dw_attr_val.v.val_vec.length
8069 * a->dw_attr_val.v.val_vec.elt_size)
8070 + a->dw_attr_val.v.val_vec.length
8071 * a->dw_attr_val.v.val_vec.elt_size; /* block */
8072 break;
8073 case dw_val_class_flag:
8074 if (dwarf_version >= 4)
8075 /* Currently all add_AT_flag calls pass in 1 as last argument,
8076 so DW_FORM_flag_present can be used. If that ever changes,
8077 we'll need to use DW_FORM_flag and have some optimization
8078 in build_abbrev_table that will change those to
8079 DW_FORM_flag_present if it is set to 1 in all DIEs using
8080 the same abbrev entry. */
8081 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8082 else
8083 size += 1;
8084 break;
8085 case dw_val_class_die_ref:
8086 if (AT_ref_external (a))
8088 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
8089 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
8090 is sized by target address length, whereas in DWARF3
8091 it's always sized as an offset. */
8092 if (use_debug_types)
8093 size += DWARF_TYPE_SIGNATURE_SIZE;
8094 else if (dwarf_version == 2)
8095 size += DWARF2_ADDR_SIZE;
8096 else
8097 size += DWARF_OFFSET_SIZE;
8099 else
8100 size += DWARF_OFFSET_SIZE;
8101 break;
8102 case dw_val_class_fde_ref:
8103 size += DWARF_OFFSET_SIZE;
8104 break;
8105 case dw_val_class_lbl_id:
8106 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8108 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8109 size += size_of_uleb128 (AT_index (a));
8111 else
8112 size += DWARF2_ADDR_SIZE;
8113 break;
8114 case dw_val_class_lineptr:
8115 case dw_val_class_macptr:
8116 size += DWARF_OFFSET_SIZE;
8117 break;
8118 case dw_val_class_str:
8119 form = AT_string_form (a);
8120 if (form == DW_FORM_strp)
8121 size += DWARF_OFFSET_SIZE;
8122 else if (form == DW_FORM_GNU_str_index)
8123 size += size_of_uleb128 (AT_index (a));
8124 else
8125 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
8126 break;
8127 case dw_val_class_file:
8128 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
8129 break;
8130 case dw_val_class_data8:
8131 size += 8;
8132 break;
8133 case dw_val_class_vms_delta:
8134 size += DWARF_OFFSET_SIZE;
8135 break;
8136 case dw_val_class_high_pc:
8137 size += DWARF2_ADDR_SIZE;
8138 break;
8139 default:
8140 gcc_unreachable ();
8144 return size;
8147 /* Size the debugging information associated with a given DIE. Visits the
8148 DIE's children recursively. Updates the global variable next_die_offset, on
8149 each time through. Uses the current value of next_die_offset to update the
8150 die_offset field in each DIE. */
8152 static void
8153 calc_die_sizes (dw_die_ref die)
8155 dw_die_ref c;
8157 gcc_assert (die->die_offset == 0
8158 || (unsigned long int) die->die_offset == next_die_offset);
8159 die->die_offset = next_die_offset;
8160 next_die_offset += size_of_die (die);
8162 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
8164 if (die->die_child != NULL)
8165 /* Count the null byte used to terminate sibling lists. */
8166 next_die_offset += 1;
8169 /* Size just the base type children at the start of the CU.
8170 This is needed because build_abbrev needs to size locs
8171 and sizing of type based stack ops needs to know die_offset
8172 values for the base types. */
8174 static void
8175 calc_base_type_die_sizes (void)
8177 unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8178 unsigned int i;
8179 dw_die_ref base_type;
8180 #if ENABLE_ASSERT_CHECKING
8181 dw_die_ref prev = comp_unit_die ()->die_child;
8182 #endif
8184 die_offset += size_of_die (comp_unit_die ());
8185 for (i = 0; base_types.iterate (i, &base_type); i++)
8187 #if ENABLE_ASSERT_CHECKING
8188 gcc_assert (base_type->die_offset == 0
8189 && prev->die_sib == base_type
8190 && base_type->die_child == NULL
8191 && base_type->die_abbrev);
8192 prev = base_type;
8193 #endif
8194 base_type->die_offset = die_offset;
8195 die_offset += size_of_die (base_type);
8199 /* Set the marks for a die and its children. We do this so
8200 that we know whether or not a reference needs to use FORM_ref_addr; only
8201 DIEs in the same CU will be marked. We used to clear out the offset
8202 and use that as the flag, but ran into ordering problems. */
8204 static void
8205 mark_dies (dw_die_ref die)
8207 dw_die_ref c;
8209 gcc_assert (!die->die_mark);
8211 die->die_mark = 1;
8212 FOR_EACH_CHILD (die, c, mark_dies (c));
8215 /* Clear the marks for a die and its children. */
8217 static void
8218 unmark_dies (dw_die_ref die)
8220 dw_die_ref c;
8222 if (! use_debug_types)
8223 gcc_assert (die->die_mark);
8225 die->die_mark = 0;
8226 FOR_EACH_CHILD (die, c, unmark_dies (c));
8229 /* Clear the marks for a die, its children and referred dies. */
8231 static void
8232 unmark_all_dies (dw_die_ref die)
8234 dw_die_ref c;
8235 dw_attr_ref a;
8236 unsigned ix;
8238 if (!die->die_mark)
8239 return;
8240 die->die_mark = 0;
8242 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
8244 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8245 if (AT_class (a) == dw_val_class_die_ref)
8246 unmark_all_dies (AT_ref (a));
8249 /* Calculate if the entry should appear in the final output file. It may be
8250 from a pruned a type. */
8252 static bool
8253 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
8255 /* By limiting gnu pubnames to definitions only, gold can generate a
8256 gdb index without entries for declarations, which don't include
8257 enough information to be useful. */
8258 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
8259 return false;
8261 if (table == pubname_table)
8263 /* Enumerator names are part of the pubname table, but the
8264 parent DW_TAG_enumeration_type die may have been pruned.
8265 Don't output them if that is the case. */
8266 if (p->die->die_tag == DW_TAG_enumerator &&
8267 (p->die->die_parent == NULL
8268 || !p->die->die_parent->die_perennial_p))
8269 return false;
8271 /* Everything else in the pubname table is included. */
8272 return true;
8275 /* The pubtypes table shouldn't include types that have been
8276 pruned. */
8277 return (p->die->die_offset != 0
8278 || !flag_eliminate_unused_debug_types);
8281 /* Return the size of the .debug_pubnames or .debug_pubtypes table
8282 generated for the compilation unit. */
8284 static unsigned long
8285 size_of_pubnames (vec<pubname_entry, va_gc> *names)
8287 unsigned long size;
8288 unsigned i;
8289 pubname_ref p;
8290 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
8292 size = DWARF_PUBNAMES_HEADER_SIZE;
8293 FOR_EACH_VEC_ELT (*names, i, p)
8294 if (include_pubname_in_output (names, p))
8295 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
8297 size += DWARF_OFFSET_SIZE;
8298 return size;
8301 /* Return the size of the information in the .debug_aranges section. */
8303 static unsigned long
8304 size_of_aranges (void)
8306 unsigned long size;
8308 size = DWARF_ARANGES_HEADER_SIZE;
8310 /* Count the address/length pair for this compilation unit. */
8311 if (text_section_used)
8312 size += 2 * DWARF2_ADDR_SIZE;
8313 if (cold_text_section_used)
8314 size += 2 * DWARF2_ADDR_SIZE;
8315 if (have_multiple_function_sections)
8317 unsigned fde_idx;
8318 dw_fde_ref fde;
8320 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
8322 if (DECL_IGNORED_P (fde->decl))
8323 continue;
8324 if (!fde->in_std_section)
8325 size += 2 * DWARF2_ADDR_SIZE;
8326 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
8327 size += 2 * DWARF2_ADDR_SIZE;
8331 /* Count the two zero words used to terminated the address range table. */
8332 size += 2 * DWARF2_ADDR_SIZE;
8333 return size;
8336 /* Select the encoding of an attribute value. */
8338 static enum dwarf_form
8339 value_format (dw_attr_ref a)
8341 switch (AT_class (a))
8343 case dw_val_class_addr:
8344 /* Only very few attributes allow DW_FORM_addr. */
8345 switch (a->dw_attr)
8347 case DW_AT_low_pc:
8348 case DW_AT_high_pc:
8349 case DW_AT_entry_pc:
8350 case DW_AT_trampoline:
8351 return (AT_index (a) == NOT_INDEXED
8352 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8353 default:
8354 break;
8356 switch (DWARF2_ADDR_SIZE)
8358 case 1:
8359 return DW_FORM_data1;
8360 case 2:
8361 return DW_FORM_data2;
8362 case 4:
8363 return DW_FORM_data4;
8364 case 8:
8365 return DW_FORM_data8;
8366 default:
8367 gcc_unreachable ();
8369 case dw_val_class_range_list:
8370 case dw_val_class_loc_list:
8371 if (dwarf_version >= 4)
8372 return DW_FORM_sec_offset;
8373 /* FALLTHRU */
8374 case dw_val_class_vms_delta:
8375 case dw_val_class_offset:
8376 switch (DWARF_OFFSET_SIZE)
8378 case 4:
8379 return DW_FORM_data4;
8380 case 8:
8381 return DW_FORM_data8;
8382 default:
8383 gcc_unreachable ();
8385 case dw_val_class_loc:
8386 if (dwarf_version >= 4)
8387 return DW_FORM_exprloc;
8388 switch (constant_size (size_of_locs (AT_loc (a))))
8390 case 1:
8391 return DW_FORM_block1;
8392 case 2:
8393 return DW_FORM_block2;
8394 case 4:
8395 return DW_FORM_block4;
8396 default:
8397 gcc_unreachable ();
8399 case dw_val_class_const:
8400 return DW_FORM_sdata;
8401 case dw_val_class_unsigned_const:
8402 switch (constant_size (AT_unsigned (a)))
8404 case 1:
8405 return DW_FORM_data1;
8406 case 2:
8407 return DW_FORM_data2;
8408 case 4:
8409 /* In DWARF3 DW_AT_data_member_location with
8410 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
8411 constant, so we need to use DW_FORM_udata if we need
8412 a large constant. */
8413 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8414 return DW_FORM_udata;
8415 return DW_FORM_data4;
8416 case 8:
8417 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8418 return DW_FORM_udata;
8419 return DW_FORM_data8;
8420 default:
8421 gcc_unreachable ();
8423 case dw_val_class_const_double:
8424 switch (HOST_BITS_PER_WIDE_INT)
8426 case 8:
8427 return DW_FORM_data2;
8428 case 16:
8429 return DW_FORM_data4;
8430 case 32:
8431 return DW_FORM_data8;
8432 case 64:
8433 default:
8434 return DW_FORM_block1;
8436 case dw_val_class_wide_int:
8437 switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
8439 case 8:
8440 return DW_FORM_data1;
8441 case 16:
8442 return DW_FORM_data2;
8443 case 32:
8444 return DW_FORM_data4;
8445 case 64:
8446 return DW_FORM_data8;
8447 default:
8448 return DW_FORM_block1;
8450 case dw_val_class_vec:
8451 switch (constant_size (a->dw_attr_val.v.val_vec.length
8452 * a->dw_attr_val.v.val_vec.elt_size))
8454 case 1:
8455 return DW_FORM_block1;
8456 case 2:
8457 return DW_FORM_block2;
8458 case 4:
8459 return DW_FORM_block4;
8460 default:
8461 gcc_unreachable ();
8463 case dw_val_class_flag:
8464 if (dwarf_version >= 4)
8466 /* Currently all add_AT_flag calls pass in 1 as last argument,
8467 so DW_FORM_flag_present can be used. If that ever changes,
8468 we'll need to use DW_FORM_flag and have some optimization
8469 in build_abbrev_table that will change those to
8470 DW_FORM_flag_present if it is set to 1 in all DIEs using
8471 the same abbrev entry. */
8472 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8473 return DW_FORM_flag_present;
8475 return DW_FORM_flag;
8476 case dw_val_class_die_ref:
8477 if (AT_ref_external (a))
8478 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
8479 else
8480 return DW_FORM_ref;
8481 case dw_val_class_fde_ref:
8482 return DW_FORM_data;
8483 case dw_val_class_lbl_id:
8484 return (AT_index (a) == NOT_INDEXED
8485 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8486 case dw_val_class_lineptr:
8487 case dw_val_class_macptr:
8488 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
8489 case dw_val_class_str:
8490 return AT_string_form (a);
8491 case dw_val_class_file:
8492 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8494 case 1:
8495 return DW_FORM_data1;
8496 case 2:
8497 return DW_FORM_data2;
8498 case 4:
8499 return DW_FORM_data4;
8500 default:
8501 gcc_unreachable ();
8504 case dw_val_class_data8:
8505 return DW_FORM_data8;
8507 case dw_val_class_high_pc:
8508 switch (DWARF2_ADDR_SIZE)
8510 case 1:
8511 return DW_FORM_data1;
8512 case 2:
8513 return DW_FORM_data2;
8514 case 4:
8515 return DW_FORM_data4;
8516 case 8:
8517 return DW_FORM_data8;
8518 default:
8519 gcc_unreachable ();
8522 default:
8523 gcc_unreachable ();
8527 /* Output the encoding of an attribute value. */
8529 static void
8530 output_value_format (dw_attr_ref a)
8532 enum dwarf_form form = value_format (a);
8534 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8537 /* Given a die and id, produce the appropriate abbreviations. */
8539 static void
8540 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
8542 unsigned ix;
8543 dw_attr_ref a_attr;
8545 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8546 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8547 dwarf_tag_name (abbrev->die_tag));
8549 if (abbrev->die_child != NULL)
8550 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8551 else
8552 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8554 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
8556 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8557 dwarf_attr_name (a_attr->dw_attr));
8558 output_value_format (a_attr);
8561 dw2_asm_output_data (1, 0, NULL);
8562 dw2_asm_output_data (1, 0, NULL);
8566 /* Output the .debug_abbrev section which defines the DIE abbreviation
8567 table. */
8569 static void
8570 output_abbrev_section (void)
8572 unsigned long abbrev_id;
8574 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8575 output_die_abbrevs (abbrev_id, abbrev_die_table[abbrev_id]);
8577 /* Terminate the table. */
8578 dw2_asm_output_data (1, 0, NULL);
8581 /* Output a symbol we can use to refer to this DIE from another CU. */
8583 static inline void
8584 output_die_symbol (dw_die_ref die)
8586 const char *sym = die->die_id.die_symbol;
8588 gcc_assert (!die->comdat_type_p);
8590 if (sym == 0)
8591 return;
8593 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8594 /* We make these global, not weak; if the target doesn't support
8595 .linkonce, it doesn't support combining the sections, so debugging
8596 will break. */
8597 targetm.asm_out.globalize_label (asm_out_file, sym);
8599 ASM_OUTPUT_LABEL (asm_out_file, sym);
8602 /* Return a new location list, given the begin and end range, and the
8603 expression. */
8605 static inline dw_loc_list_ref
8606 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8607 const char *section)
8609 dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
8611 retlist->begin = begin;
8612 retlist->begin_entry = NULL;
8613 retlist->end = end;
8614 retlist->expr = expr;
8615 retlist->section = section;
8617 return retlist;
8620 /* Generate a new internal symbol for this location list node, if it
8621 hasn't got one yet. */
8623 static inline void
8624 gen_llsym (dw_loc_list_ref list)
8626 gcc_assert (!list->ll_symbol);
8627 list->ll_symbol = gen_internal_sym ("LLST");
8630 /* Output the location list given to us. */
8632 static void
8633 output_loc_list (dw_loc_list_ref list_head)
8635 dw_loc_list_ref curr = list_head;
8637 if (list_head->emitted)
8638 return;
8639 list_head->emitted = true;
8641 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8643 /* Walk the location list, and output each range + expression. */
8644 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8646 unsigned long size;
8647 /* Don't output an entry that starts and ends at the same address. */
8648 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
8649 continue;
8650 size = size_of_locs (curr->expr);
8651 /* If the expression is too large, drop it on the floor. We could
8652 perhaps put it into DW_TAG_dwarf_procedure and refer to that
8653 in the expression, but >= 64KB expressions for a single value
8654 in a single range are unlikely very useful. */
8655 if (size > 0xffff)
8656 continue;
8657 if (dwarf_split_debug_info)
8659 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
8660 "Location list start/length entry (%s)",
8661 list_head->ll_symbol);
8662 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
8663 "Location list range start index (%s)",
8664 curr->begin);
8665 /* The length field is 4 bytes. If we ever need to support
8666 an 8-byte length, we can add a new DW_LLE code or fall back
8667 to DW_LLE_GNU_start_end_entry. */
8668 dw2_asm_output_delta (4, curr->end, curr->begin,
8669 "Location list range length (%s)",
8670 list_head->ll_symbol);
8672 else if (!have_multiple_function_sections)
8674 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8675 "Location list begin address (%s)",
8676 list_head->ll_symbol);
8677 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8678 "Location list end address (%s)",
8679 list_head->ll_symbol);
8681 else
8683 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8684 "Location list begin address (%s)",
8685 list_head->ll_symbol);
8686 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8687 "Location list end address (%s)",
8688 list_head->ll_symbol);
8691 /* Output the block length for this list of location operations. */
8692 gcc_assert (size <= 0xffff);
8693 dw2_asm_output_data (2, size, "%s", "Location expression size");
8695 output_loc_sequence (curr->expr, -1);
8698 if (dwarf_split_debug_info)
8699 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
8700 "Location list terminator (%s)",
8701 list_head->ll_symbol);
8702 else
8704 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8705 "Location list terminator begin (%s)",
8706 list_head->ll_symbol);
8707 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8708 "Location list terminator end (%s)",
8709 list_head->ll_symbol);
8713 /* Output a range_list offset into the debug_range section. Emit a
8714 relocated reference if val_entry is NULL, otherwise, emit an
8715 indirect reference. */
8717 static void
8718 output_range_list_offset (dw_attr_ref a)
8720 const char *name = dwarf_attr_name (a->dw_attr);
8722 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
8724 char *p = strchr (ranges_section_label, '\0');
8725 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX, a->dw_attr_val.v.val_offset);
8726 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8727 debug_ranges_section, "%s", name);
8728 *p = '\0';
8730 else
8731 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8732 "%s (offset from %s)", name, ranges_section_label);
8735 /* Output the offset into the debug_loc section. */
8737 static void
8738 output_loc_list_offset (dw_attr_ref a)
8740 char *sym = AT_loc_list (a)->ll_symbol;
8742 gcc_assert (sym);
8743 if (dwarf_split_debug_info)
8744 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
8745 "%s", dwarf_attr_name (a->dw_attr));
8746 else
8747 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8748 "%s", dwarf_attr_name (a->dw_attr));
8751 /* Output an attribute's index or value appropriately. */
8753 static void
8754 output_attr_index_or_value (dw_attr_ref a)
8756 const char *name = dwarf_attr_name (a->dw_attr);
8758 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8760 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
8761 return;
8763 switch (AT_class (a))
8765 case dw_val_class_addr:
8766 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8767 break;
8768 case dw_val_class_high_pc:
8769 case dw_val_class_lbl_id:
8770 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8771 break;
8772 case dw_val_class_loc_list:
8773 output_loc_list_offset (a);
8774 break;
8775 default:
8776 gcc_unreachable ();
8780 /* Output a type signature. */
8782 static inline void
8783 output_signature (const char *sig, const char *name)
8785 int i;
8787 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8788 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
8791 /* Output the DIE and its attributes. Called recursively to generate
8792 the definitions of each child DIE. */
8794 static void
8795 output_die (dw_die_ref die)
8797 dw_attr_ref a;
8798 dw_die_ref c;
8799 unsigned long size;
8800 unsigned ix;
8802 /* If someone in another CU might refer to us, set up a symbol for
8803 them to point to. */
8804 if (! die->comdat_type_p && die->die_id.die_symbol)
8805 output_die_symbol (die);
8807 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
8808 (unsigned long)die->die_offset,
8809 dwarf_tag_name (die->die_tag));
8811 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8813 const char *name = dwarf_attr_name (a->dw_attr);
8815 switch (AT_class (a))
8817 case dw_val_class_addr:
8818 output_attr_index_or_value (a);
8819 break;
8821 case dw_val_class_offset:
8822 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8823 "%s", name);
8824 break;
8826 case dw_val_class_range_list:
8827 output_range_list_offset (a);
8828 break;
8830 case dw_val_class_loc:
8831 size = size_of_locs (AT_loc (a));
8833 /* Output the block length for this list of location operations. */
8834 if (dwarf_version >= 4)
8835 dw2_asm_output_data_uleb128 (size, "%s", name);
8836 else
8837 dw2_asm_output_data (constant_size (size), size, "%s", name);
8839 output_loc_sequence (AT_loc (a), -1);
8840 break;
8842 case dw_val_class_const:
8843 /* ??? It would be slightly more efficient to use a scheme like is
8844 used for unsigned constants below, but gdb 4.x does not sign
8845 extend. Gdb 5.x does sign extend. */
8846 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8847 break;
8849 case dw_val_class_unsigned_const:
8851 int csize = constant_size (AT_unsigned (a));
8852 if (dwarf_version == 3
8853 && a->dw_attr == DW_AT_data_member_location
8854 && csize >= 4)
8855 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
8856 else
8857 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
8859 break;
8861 case dw_val_class_const_double:
8863 unsigned HOST_WIDE_INT first, second;
8865 if (HOST_BITS_PER_WIDE_INT >= 64)
8866 dw2_asm_output_data (1,
8867 HOST_BITS_PER_DOUBLE_INT
8868 / HOST_BITS_PER_CHAR,
8869 NULL);
8871 if (WORDS_BIG_ENDIAN)
8873 first = a->dw_attr_val.v.val_double.high;
8874 second = a->dw_attr_val.v.val_double.low;
8876 else
8878 first = a->dw_attr_val.v.val_double.low;
8879 second = a->dw_attr_val.v.val_double.high;
8882 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8883 first, "%s", name);
8884 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8885 second, NULL);
8887 break;
8889 case dw_val_class_wide_int:
8891 int i;
8892 int len = get_full_len (*a->dw_attr_val.v.val_wide);
8893 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
8894 if (len * HOST_BITS_PER_WIDE_INT > 64)
8895 dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide) * l,
8896 NULL);
8898 if (WORDS_BIG_ENDIAN)
8899 for (i = len - 1; i >= 0; --i)
8901 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
8902 "%s", name);
8903 name = NULL;
8905 else
8906 for (i = 0; i < len; ++i)
8908 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
8909 "%s", name);
8910 name = NULL;
8913 break;
8915 case dw_val_class_vec:
8917 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8918 unsigned int len = a->dw_attr_val.v.val_vec.length;
8919 unsigned int i;
8920 unsigned char *p;
8922 dw2_asm_output_data (constant_size (len * elt_size),
8923 len * elt_size, "%s", name);
8924 if (elt_size > sizeof (HOST_WIDE_INT))
8926 elt_size /= 2;
8927 len *= 2;
8929 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8930 i < len;
8931 i++, p += elt_size)
8932 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8933 "fp or vector constant word %u", i);
8934 break;
8937 case dw_val_class_flag:
8938 if (dwarf_version >= 4)
8940 /* Currently all add_AT_flag calls pass in 1 as last argument,
8941 so DW_FORM_flag_present can be used. If that ever changes,
8942 we'll need to use DW_FORM_flag and have some optimization
8943 in build_abbrev_table that will change those to
8944 DW_FORM_flag_present if it is set to 1 in all DIEs using
8945 the same abbrev entry. */
8946 gcc_assert (AT_flag (a) == 1);
8947 if (flag_debug_asm)
8948 fprintf (asm_out_file, "\t\t\t%s %s\n",
8949 ASM_COMMENT_START, name);
8950 break;
8952 dw2_asm_output_data (1, AT_flag (a), "%s", name);
8953 break;
8955 case dw_val_class_loc_list:
8956 output_attr_index_or_value (a);
8957 break;
8959 case dw_val_class_die_ref:
8960 if (AT_ref_external (a))
8962 if (AT_ref (a)->comdat_type_p)
8964 comdat_type_node_ref type_node =
8965 AT_ref (a)->die_id.die_type_node;
8967 gcc_assert (type_node);
8968 output_signature (type_node->signature, name);
8970 else
8972 const char *sym = AT_ref (a)->die_id.die_symbol;
8973 int size;
8975 gcc_assert (sym);
8976 /* In DWARF2, DW_FORM_ref_addr is sized by target address
8977 length, whereas in DWARF3 it's always sized as an
8978 offset. */
8979 if (dwarf_version == 2)
8980 size = DWARF2_ADDR_SIZE;
8981 else
8982 size = DWARF_OFFSET_SIZE;
8983 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
8984 name);
8987 else
8989 gcc_assert (AT_ref (a)->die_offset);
8990 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8991 "%s", name);
8993 break;
8995 case dw_val_class_fde_ref:
8997 char l1[20];
8999 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
9000 a->dw_attr_val.v.val_fde_index * 2);
9001 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
9002 "%s", name);
9004 break;
9006 case dw_val_class_vms_delta:
9007 #ifdef ASM_OUTPUT_DWARF_VMS_DELTA
9008 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
9009 AT_vms_delta2 (a), AT_vms_delta1 (a),
9010 "%s", name);
9011 #else
9012 dw2_asm_output_delta (DWARF_OFFSET_SIZE,
9013 AT_vms_delta2 (a), AT_vms_delta1 (a),
9014 "%s", name);
9015 #endif
9016 break;
9018 case dw_val_class_lbl_id:
9019 output_attr_index_or_value (a);
9020 break;
9022 case dw_val_class_lineptr:
9023 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
9024 debug_line_section, "%s", name);
9025 break;
9027 case dw_val_class_macptr:
9028 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
9029 debug_macinfo_section, "%s", name);
9030 break;
9032 case dw_val_class_str:
9033 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
9034 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
9035 a->dw_attr_val.v.val_str->label,
9036 debug_str_section,
9037 "%s: \"%s\"", name, AT_string (a));
9038 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
9039 dw2_asm_output_data_uleb128 (AT_index (a),
9040 "%s: \"%s\"", name, AT_string (a));
9041 else
9042 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
9043 break;
9045 case dw_val_class_file:
9047 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
9049 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
9050 a->dw_attr_val.v.val_file->filename);
9051 break;
9054 case dw_val_class_data8:
9056 int i;
9058 for (i = 0; i < 8; i++)
9059 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
9060 i == 0 ? "%s" : NULL, name);
9061 break;
9064 case dw_val_class_high_pc:
9065 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
9066 get_AT_low_pc (die), "DW_AT_high_pc");
9067 break;
9069 default:
9070 gcc_unreachable ();
9074 FOR_EACH_CHILD (die, c, output_die (c));
9076 /* Add null byte to terminate sibling list. */
9077 if (die->die_child != NULL)
9078 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
9079 (unsigned long) die->die_offset);
9082 /* Output the compilation unit that appears at the beginning of the
9083 .debug_info section, and precedes the DIE descriptions. */
9085 static void
9086 output_compilation_unit_header (void)
9088 /* We don't support actual DWARFv5 units yet, we just use some
9089 DWARFv5 draft DIE tags in DWARFv4 format. */
9090 int ver = dwarf_version < 5 ? dwarf_version : 4;
9092 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9093 dw2_asm_output_data (4, 0xffffffff,
9094 "Initial length escape value indicating 64-bit DWARF extension");
9095 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9096 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
9097 "Length of Compilation Unit Info");
9098 dw2_asm_output_data (2, ver, "DWARF version number");
9099 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
9100 debug_abbrev_section,
9101 "Offset Into Abbrev. Section");
9102 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9105 /* Output the compilation unit DIE and its children. */
9107 static void
9108 output_comp_unit (dw_die_ref die, int output_if_empty)
9110 const char *secname, *oldsym;
9111 char *tmp;
9113 /* Unless we are outputting main CU, we may throw away empty ones. */
9114 if (!output_if_empty && die->die_child == NULL)
9115 return;
9117 /* Even if there are no children of this DIE, we must output the information
9118 about the compilation unit. Otherwise, on an empty translation unit, we
9119 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
9120 will then complain when examining the file. First mark all the DIEs in
9121 this CU so we know which get local refs. */
9122 mark_dies (die);
9124 external_ref_hash_type *extern_map = optimize_external_refs (die);
9126 build_abbrev_table (die, extern_map);
9128 delete extern_map;
9130 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
9131 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
9132 calc_die_sizes (die);
9134 oldsym = die->die_id.die_symbol;
9135 if (oldsym)
9137 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
9139 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
9140 secname = tmp;
9141 die->die_id.die_symbol = NULL;
9142 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9144 else
9146 switch_to_section (debug_info_section);
9147 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
9148 info_section_emitted = true;
9151 /* Output debugging information. */
9152 output_compilation_unit_header ();
9153 output_die (die);
9155 /* Leave the marks on the main CU, so we can check them in
9156 output_pubnames. */
9157 if (oldsym)
9159 unmark_dies (die);
9160 die->die_id.die_symbol = oldsym;
9164 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
9165 and .debug_pubtypes. This is configured per-target, but can be
9166 overridden by the -gpubnames or -gno-pubnames options. */
9168 static inline bool
9169 want_pubnames (void)
9171 if (debug_info_level <= DINFO_LEVEL_TERSE)
9172 return false;
9173 if (debug_generate_pub_sections != -1)
9174 return debug_generate_pub_sections;
9175 return targetm.want_debug_pub_sections;
9178 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
9180 static void
9181 add_AT_pubnames (dw_die_ref die)
9183 if (want_pubnames ())
9184 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
9187 /* Add a string attribute value to a skeleton DIE. */
9189 static inline void
9190 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
9191 const char *str)
9193 dw_attr_node attr;
9194 struct indirect_string_node *node;
9196 if (! skeleton_debug_str_hash)
9197 skeleton_debug_str_hash
9198 = hash_table<indirect_string_hasher>::create_ggc (10);
9200 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
9201 find_string_form (node);
9202 if (node->form == DW_FORM_GNU_str_index)
9203 node->form = DW_FORM_strp;
9205 attr.dw_attr = attr_kind;
9206 attr.dw_attr_val.val_class = dw_val_class_str;
9207 attr.dw_attr_val.val_entry = NULL;
9208 attr.dw_attr_val.v.val_str = node;
9209 add_dwarf_attr (die, &attr);
9212 /* Helper function to generate top-level dies for skeleton debug_info and
9213 debug_types. */
9215 static void
9216 add_top_level_skeleton_die_attrs (dw_die_ref die)
9218 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
9219 const char *comp_dir = comp_dir_string ();
9221 add_skeleton_AT_string (die, DW_AT_GNU_dwo_name, dwo_file_name);
9222 if (comp_dir != NULL)
9223 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
9224 add_AT_pubnames (die);
9225 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
9228 /* Output skeleton debug sections that point to the dwo file. */
9230 static void
9231 output_skeleton_debug_sections (dw_die_ref comp_unit)
9233 /* We don't support actual DWARFv5 units yet, we just use some
9234 DWARFv5 draft DIE tags in DWARFv4 format. */
9235 int ver = dwarf_version < 5 ? dwarf_version : 4;
9237 /* These attributes will be found in the full debug_info section. */
9238 remove_AT (comp_unit, DW_AT_producer);
9239 remove_AT (comp_unit, DW_AT_language);
9241 switch_to_section (debug_skeleton_info_section);
9242 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
9244 /* Produce the skeleton compilation-unit header. This one differs enough from
9245 a normal CU header that it's better not to call output_compilation_unit
9246 header. */
9247 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9248 dw2_asm_output_data (4, 0xffffffff,
9249 "Initial length escape value indicating 64-bit DWARF extension");
9251 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9252 DWARF_COMPILE_UNIT_HEADER_SIZE
9253 - DWARF_INITIAL_LENGTH_SIZE
9254 + size_of_die (comp_unit),
9255 "Length of Compilation Unit Info");
9256 dw2_asm_output_data (2, ver, "DWARF version number");
9257 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
9258 debug_abbrev_section,
9259 "Offset Into Abbrev. Section");
9260 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9262 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
9263 output_die (comp_unit);
9265 /* Build the skeleton debug_abbrev section. */
9266 switch_to_section (debug_skeleton_abbrev_section);
9267 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
9269 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
9271 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
9274 /* Output a comdat type unit DIE and its children. */
9276 static void
9277 output_comdat_type_unit (comdat_type_node *node)
9279 const char *secname;
9280 char *tmp;
9281 int i;
9282 #if defined (OBJECT_FORMAT_ELF)
9283 tree comdat_key;
9284 #endif
9286 /* First mark all the DIEs in this CU so we know which get local refs. */
9287 mark_dies (node->root_die);
9289 external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
9291 build_abbrev_table (node->root_die, extern_map);
9293 delete extern_map;
9294 extern_map = NULL;
9296 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
9297 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
9298 calc_die_sizes (node->root_die);
9300 #if defined (OBJECT_FORMAT_ELF)
9301 if (!dwarf_split_debug_info)
9302 secname = ".debug_types";
9303 else
9304 secname = ".debug_types.dwo";
9306 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9307 sprintf (tmp, "wt.");
9308 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9309 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
9310 comdat_key = get_identifier (tmp);
9311 targetm.asm_out.named_section (secname,
9312 SECTION_DEBUG | SECTION_LINKONCE,
9313 comdat_key);
9314 #else
9315 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9316 sprintf (tmp, ".gnu.linkonce.wt.");
9317 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9318 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
9319 secname = tmp;
9320 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9321 #endif
9323 /* Output debugging information. */
9324 output_compilation_unit_header ();
9325 output_signature (node->signature, "Type Signature");
9326 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
9327 "Offset to Type DIE");
9328 output_die (node->root_die);
9330 unmark_dies (node->root_die);
9333 /* Return the DWARF2/3 pubname associated with a decl. */
9335 static const char *
9336 dwarf2_name (tree decl, int scope)
9338 if (DECL_NAMELESS (decl))
9339 return NULL;
9340 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
9343 /* Add a new entry to .debug_pubnames if appropriate. */
9345 static void
9346 add_pubname_string (const char *str, dw_die_ref die)
9348 pubname_entry e;
9350 e.die = die;
9351 e.name = xstrdup (str);
9352 vec_safe_push (pubname_table, e);
9355 static void
9356 add_pubname (tree decl, dw_die_ref die)
9358 if (!want_pubnames ())
9359 return;
9361 /* Don't add items to the table when we expect that the consumer will have
9362 just read the enclosing die. For example, if the consumer is looking at a
9363 class_member, it will either be inside the class already, or will have just
9364 looked up the class to find the member. Either way, searching the class is
9365 faster than searching the index. */
9366 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
9367 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9369 const char *name = dwarf2_name (decl, 1);
9371 if (name)
9372 add_pubname_string (name, die);
9376 /* Add an enumerator to the pubnames section. */
9378 static void
9379 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
9381 pubname_entry e;
9383 gcc_assert (scope_name);
9384 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
9385 e.die = die;
9386 vec_safe_push (pubname_table, e);
9389 /* Add a new entry to .debug_pubtypes if appropriate. */
9391 static void
9392 add_pubtype (tree decl, dw_die_ref die)
9394 pubname_entry e;
9396 if (!want_pubnames ())
9397 return;
9399 if ((TREE_PUBLIC (decl)
9400 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9401 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
9403 tree scope = NULL;
9404 const char *scope_name = "";
9405 const char *sep = is_cxx () ? "::" : ".";
9406 const char *name;
9408 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
9409 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
9411 scope_name = lang_hooks.dwarf_name (scope, 1);
9412 if (scope_name != NULL && scope_name[0] != '\0')
9413 scope_name = concat (scope_name, sep, NULL);
9414 else
9415 scope_name = "";
9418 if (TYPE_P (decl))
9419 name = type_tag (decl);
9420 else
9421 name = lang_hooks.dwarf_name (decl, 1);
9423 /* If we don't have a name for the type, there's no point in adding
9424 it to the table. */
9425 if (name != NULL && name[0] != '\0')
9427 e.die = die;
9428 e.name = concat (scope_name, name, NULL);
9429 vec_safe_push (pubtype_table, e);
9432 /* Although it might be more consistent to add the pubinfo for the
9433 enumerators as their dies are created, they should only be added if the
9434 enum type meets the criteria above. So rather than re-check the parent
9435 enum type whenever an enumerator die is created, just output them all
9436 here. This isn't protected by the name conditional because anonymous
9437 enums don't have names. */
9438 if (die->die_tag == DW_TAG_enumeration_type)
9440 dw_die_ref c;
9442 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
9447 /* Output a single entry in the pubnames table. */
9449 static void
9450 output_pubname (dw_offset die_offset, pubname_entry *entry)
9452 dw_die_ref die = entry->die;
9453 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
9455 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
9457 if (debug_generate_pub_sections == 2)
9459 /* This logic follows gdb's method for determining the value of the flag
9460 byte. */
9461 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
9462 switch (die->die_tag)
9464 case DW_TAG_typedef:
9465 case DW_TAG_base_type:
9466 case DW_TAG_subrange_type:
9467 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9468 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9469 break;
9470 case DW_TAG_enumerator:
9471 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9472 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9473 if (!is_cxx () && !is_java ())
9474 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9475 break;
9476 case DW_TAG_subprogram:
9477 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9478 GDB_INDEX_SYMBOL_KIND_FUNCTION);
9479 if (!is_ada ())
9480 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9481 break;
9482 case DW_TAG_constant:
9483 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9484 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9485 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9486 break;
9487 case DW_TAG_variable:
9488 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9489 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9490 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9491 break;
9492 case DW_TAG_namespace:
9493 case DW_TAG_imported_declaration:
9494 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9495 break;
9496 case DW_TAG_class_type:
9497 case DW_TAG_interface_type:
9498 case DW_TAG_structure_type:
9499 case DW_TAG_union_type:
9500 case DW_TAG_enumeration_type:
9501 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9502 if (!is_cxx () && !is_java ())
9503 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9504 break;
9505 default:
9506 /* An unusual tag. Leave the flag-byte empty. */
9507 break;
9509 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
9510 "GDB-index flags");
9513 dw2_asm_output_nstring (entry->name, -1, "external name");
9517 /* Output the public names table used to speed up access to externally
9518 visible names; or the public types table used to find type definitions. */
9520 static void
9521 output_pubnames (vec<pubname_entry, va_gc> *names)
9523 unsigned i;
9524 unsigned long pubnames_length = size_of_pubnames (names);
9525 pubname_ref pub;
9527 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9528 dw2_asm_output_data (4, 0xffffffff,
9529 "Initial length escape value indicating 64-bit DWARF extension");
9530 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length, "Pub Info Length");
9532 /* Version number for pubnames/pubtypes is independent of dwarf version. */
9533 dw2_asm_output_data (2, 2, "DWARF Version");
9535 if (dwarf_split_debug_info)
9536 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9537 debug_skeleton_info_section,
9538 "Offset of Compilation Unit Info");
9539 else
9540 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9541 debug_info_section,
9542 "Offset of Compilation Unit Info");
9543 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
9544 "Compilation Unit Length");
9546 FOR_EACH_VEC_ELT (*names, i, pub)
9548 if (include_pubname_in_output (names, pub))
9550 dw_offset die_offset = pub->die->die_offset;
9552 /* We shouldn't see pubnames for DIEs outside of the main CU. */
9553 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
9554 gcc_assert (pub->die->die_mark);
9556 /* If we're putting types in their own .debug_types sections,
9557 the .debug_pubtypes table will still point to the compile
9558 unit (not the type unit), so we want to use the offset of
9559 the skeleton DIE (if there is one). */
9560 if (pub->die->comdat_type_p && names == pubtype_table)
9562 comdat_type_node_ref type_node = pub->die->die_id.die_type_node;
9564 if (type_node != NULL)
9565 die_offset = (type_node->skeleton_die != NULL
9566 ? type_node->skeleton_die->die_offset
9567 : comp_unit_die ()->die_offset);
9570 output_pubname (die_offset, pub);
9574 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
9577 /* Output public names and types tables if necessary. */
9579 static void
9580 output_pubtables (void)
9582 if (!want_pubnames () || !info_section_emitted)
9583 return;
9585 switch_to_section (debug_pubnames_section);
9586 output_pubnames (pubname_table);
9587 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
9588 It shouldn't hurt to emit it always, since pure DWARF2 consumers
9589 simply won't look for the section. */
9590 switch_to_section (debug_pubtypes_section);
9591 output_pubnames (pubtype_table);
9595 /* Output the information that goes into the .debug_aranges table.
9596 Namely, define the beginning and ending address range of the
9597 text section generated for this compilation unit. */
9599 static void
9600 output_aranges (unsigned long aranges_length)
9602 unsigned i;
9604 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9605 dw2_asm_output_data (4, 0xffffffff,
9606 "Initial length escape value indicating 64-bit DWARF extension");
9607 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
9608 "Length of Address Ranges Info");
9609 /* Version number for aranges is still 2, even up to DWARF5. */
9610 dw2_asm_output_data (2, 2, "DWARF Version");
9611 if (dwarf_split_debug_info)
9612 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9613 debug_skeleton_info_section,
9614 "Offset of Compilation Unit Info");
9615 else
9616 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9617 debug_info_section,
9618 "Offset of Compilation Unit Info");
9619 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
9620 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
9622 /* We need to align to twice the pointer size here. */
9623 if (DWARF_ARANGES_PAD_SIZE)
9625 /* Pad using a 2 byte words so that padding is correct for any
9626 pointer size. */
9627 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
9628 2 * DWARF2_ADDR_SIZE);
9629 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
9630 dw2_asm_output_data (2, 0, NULL);
9633 /* It is necessary not to output these entries if the sections were
9634 not used; if the sections were not used, the length will be 0 and
9635 the address may end up as 0 if the section is discarded by ld
9636 --gc-sections, leaving an invalid (0, 0) entry that can be
9637 confused with the terminator. */
9638 if (text_section_used)
9640 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
9641 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
9642 text_section_label, "Length");
9644 if (cold_text_section_used)
9646 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
9647 "Address");
9648 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
9649 cold_text_section_label, "Length");
9652 if (have_multiple_function_sections)
9654 unsigned fde_idx;
9655 dw_fde_ref fde;
9657 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9659 if (DECL_IGNORED_P (fde->decl))
9660 continue;
9661 if (!fde->in_std_section)
9663 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
9664 "Address");
9665 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
9666 fde->dw_fde_begin, "Length");
9668 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9670 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
9671 "Address");
9672 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
9673 fde->dw_fde_second_begin, "Length");
9678 /* Output the terminator words. */
9679 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9680 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9683 /* Add a new entry to .debug_ranges. Return the offset at which it
9684 was placed. */
9686 static unsigned int
9687 add_ranges_num (int num)
9689 unsigned int in_use = ranges_table_in_use;
9691 if (in_use == ranges_table_allocated)
9693 ranges_table_allocated += RANGES_TABLE_INCREMENT;
9694 ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
9695 ranges_table_allocated);
9696 memset (ranges_table + ranges_table_in_use, 0,
9697 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
9700 ranges_table[in_use].num = num;
9701 ranges_table_in_use = in_use + 1;
9703 return in_use * 2 * DWARF2_ADDR_SIZE;
9706 /* Add a new entry to .debug_ranges corresponding to a block, or a
9707 range terminator if BLOCK is NULL. */
9709 static unsigned int
9710 add_ranges (const_tree block)
9712 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
9715 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
9716 When using dwarf_split_debug_info, address attributes in dies destined
9717 for the final executable should be direct references--setting the
9718 parameter force_direct ensures this behavior. */
9720 static void
9721 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
9722 bool *added, bool force_direct)
9724 unsigned int in_use = ranges_by_label_in_use;
9725 unsigned int offset;
9727 if (in_use == ranges_by_label_allocated)
9729 ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
9730 ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
9731 ranges_by_label,
9732 ranges_by_label_allocated);
9733 memset (ranges_by_label + ranges_by_label_in_use, 0,
9734 RANGES_TABLE_INCREMENT
9735 * sizeof (struct dw_ranges_by_label_struct));
9738 ranges_by_label[in_use].begin = begin;
9739 ranges_by_label[in_use].end = end;
9740 ranges_by_label_in_use = in_use + 1;
9742 offset = add_ranges_num (-(int)in_use - 1);
9743 if (!*added)
9745 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
9746 *added = true;
9750 static void
9751 output_ranges (void)
9753 unsigned i;
9754 static const char *const start_fmt = "Offset %#x";
9755 const char *fmt = start_fmt;
9757 for (i = 0; i < ranges_table_in_use; i++)
9759 int block_num = ranges_table[i].num;
9761 if (block_num > 0)
9763 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
9764 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
9766 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
9767 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
9769 /* If all code is in the text section, then the compilation
9770 unit base address defaults to DW_AT_low_pc, which is the
9771 base of the text section. */
9772 if (!have_multiple_function_sections)
9774 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
9775 text_section_label,
9776 fmt, i * 2 * DWARF2_ADDR_SIZE);
9777 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
9778 text_section_label, NULL);
9781 /* Otherwise, the compilation unit base address is zero,
9782 which allows us to use absolute addresses, and not worry
9783 about whether the target supports cross-section
9784 arithmetic. */
9785 else
9787 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
9788 fmt, i * 2 * DWARF2_ADDR_SIZE);
9789 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
9792 fmt = NULL;
9795 /* Negative block_num stands for an index into ranges_by_label. */
9796 else if (block_num < 0)
9798 int lab_idx = - block_num - 1;
9800 if (!have_multiple_function_sections)
9802 gcc_unreachable ();
9803 #if 0
9804 /* If we ever use add_ranges_by_labels () for a single
9805 function section, all we have to do is to take out
9806 the #if 0 above. */
9807 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9808 ranges_by_label[lab_idx].begin,
9809 text_section_label,
9810 fmt, i * 2 * DWARF2_ADDR_SIZE);
9811 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9812 ranges_by_label[lab_idx].end,
9813 text_section_label, NULL);
9814 #endif
9816 else
9818 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9819 ranges_by_label[lab_idx].begin,
9820 fmt, i * 2 * DWARF2_ADDR_SIZE);
9821 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9822 ranges_by_label[lab_idx].end,
9823 NULL);
9826 else
9828 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9829 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9830 fmt = start_fmt;
9835 /* Data structure containing information about input files. */
9836 struct file_info
9838 const char *path; /* Complete file name. */
9839 const char *fname; /* File name part. */
9840 int length; /* Length of entire string. */
9841 struct dwarf_file_data * file_idx; /* Index in input file table. */
9842 int dir_idx; /* Index in directory table. */
9845 /* Data structure containing information about directories with source
9846 files. */
9847 struct dir_info
9849 const char *path; /* Path including directory name. */
9850 int length; /* Path length. */
9851 int prefix; /* Index of directory entry which is a prefix. */
9852 int count; /* Number of files in this directory. */
9853 int dir_idx; /* Index of directory used as base. */
9856 /* Callback function for file_info comparison. We sort by looking at
9857 the directories in the path. */
9859 static int
9860 file_info_cmp (const void *p1, const void *p2)
9862 const struct file_info *const s1 = (const struct file_info *) p1;
9863 const struct file_info *const s2 = (const struct file_info *) p2;
9864 const unsigned char *cp1;
9865 const unsigned char *cp2;
9867 /* Take care of file names without directories. We need to make sure that
9868 we return consistent values to qsort since some will get confused if
9869 we return the same value when identical operands are passed in opposite
9870 orders. So if neither has a directory, return 0 and otherwise return
9871 1 or -1 depending on which one has the directory. */
9872 if ((s1->path == s1->fname || s2->path == s2->fname))
9873 return (s2->path == s2->fname) - (s1->path == s1->fname);
9875 cp1 = (const unsigned char *) s1->path;
9876 cp2 = (const unsigned char *) s2->path;
9878 while (1)
9880 ++cp1;
9881 ++cp2;
9882 /* Reached the end of the first path? If so, handle like above. */
9883 if ((cp1 == (const unsigned char *) s1->fname)
9884 || (cp2 == (const unsigned char *) s2->fname))
9885 return ((cp2 == (const unsigned char *) s2->fname)
9886 - (cp1 == (const unsigned char *) s1->fname));
9888 /* Character of current path component the same? */
9889 else if (*cp1 != *cp2)
9890 return *cp1 - *cp2;
9894 struct file_name_acquire_data
9896 struct file_info *files;
9897 int used_files;
9898 int max_files;
9901 /* Traversal function for the hash table. */
9904 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
9906 struct dwarf_file_data *d = *slot;
9907 struct file_info *fi;
9908 const char *f;
9910 gcc_assert (fnad->max_files >= d->emitted_number);
9912 if (! d->emitted_number)
9913 return 1;
9915 gcc_assert (fnad->max_files != fnad->used_files);
9917 fi = fnad->files + fnad->used_files++;
9919 /* Skip all leading "./". */
9920 f = d->filename;
9921 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
9922 f += 2;
9924 /* Create a new array entry. */
9925 fi->path = f;
9926 fi->length = strlen (f);
9927 fi->file_idx = d;
9929 /* Search for the file name part. */
9930 f = strrchr (f, DIR_SEPARATOR);
9931 #if defined (DIR_SEPARATOR_2)
9933 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
9935 if (g != NULL)
9937 if (f == NULL || f < g)
9938 f = g;
9941 #endif
9943 fi->fname = f == NULL ? fi->path : f + 1;
9944 return 1;
9947 /* Output the directory table and the file name table. We try to minimize
9948 the total amount of memory needed. A heuristic is used to avoid large
9949 slowdowns with many input files. */
9951 static void
9952 output_file_names (void)
9954 struct file_name_acquire_data fnad;
9955 int numfiles;
9956 struct file_info *files;
9957 struct dir_info *dirs;
9958 int *saved;
9959 int *savehere;
9960 int *backmap;
9961 int ndirs;
9962 int idx_offset;
9963 int i;
9965 if (!last_emitted_file)
9967 dw2_asm_output_data (1, 0, "End directory table");
9968 dw2_asm_output_data (1, 0, "End file name table");
9969 return;
9972 numfiles = last_emitted_file->emitted_number;
9974 /* Allocate the various arrays we need. */
9975 files = XALLOCAVEC (struct file_info, numfiles);
9976 dirs = XALLOCAVEC (struct dir_info, numfiles);
9978 fnad.files = files;
9979 fnad.used_files = 0;
9980 fnad.max_files = numfiles;
9981 file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
9982 gcc_assert (fnad.used_files == fnad.max_files);
9984 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
9986 /* Find all the different directories used. */
9987 dirs[0].path = files[0].path;
9988 dirs[0].length = files[0].fname - files[0].path;
9989 dirs[0].prefix = -1;
9990 dirs[0].count = 1;
9991 dirs[0].dir_idx = 0;
9992 files[0].dir_idx = 0;
9993 ndirs = 1;
9995 for (i = 1; i < numfiles; i++)
9996 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
9997 && memcmp (dirs[ndirs - 1].path, files[i].path,
9998 dirs[ndirs - 1].length) == 0)
10000 /* Same directory as last entry. */
10001 files[i].dir_idx = ndirs - 1;
10002 ++dirs[ndirs - 1].count;
10004 else
10006 int j;
10008 /* This is a new directory. */
10009 dirs[ndirs].path = files[i].path;
10010 dirs[ndirs].length = files[i].fname - files[i].path;
10011 dirs[ndirs].count = 1;
10012 dirs[ndirs].dir_idx = ndirs;
10013 files[i].dir_idx = ndirs;
10015 /* Search for a prefix. */
10016 dirs[ndirs].prefix = -1;
10017 for (j = 0; j < ndirs; j++)
10018 if (dirs[j].length < dirs[ndirs].length
10019 && dirs[j].length > 1
10020 && (dirs[ndirs].prefix == -1
10021 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
10022 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
10023 dirs[ndirs].prefix = j;
10025 ++ndirs;
10028 /* Now to the actual work. We have to find a subset of the directories which
10029 allow expressing the file name using references to the directory table
10030 with the least amount of characters. We do not do an exhaustive search
10031 where we would have to check out every combination of every single
10032 possible prefix. Instead we use a heuristic which provides nearly optimal
10033 results in most cases and never is much off. */
10034 saved = XALLOCAVEC (int, ndirs);
10035 savehere = XALLOCAVEC (int, ndirs);
10037 memset (saved, '\0', ndirs * sizeof (saved[0]));
10038 for (i = 0; i < ndirs; i++)
10040 int j;
10041 int total;
10043 /* We can always save some space for the current directory. But this
10044 does not mean it will be enough to justify adding the directory. */
10045 savehere[i] = dirs[i].length;
10046 total = (savehere[i] - saved[i]) * dirs[i].count;
10048 for (j = i + 1; j < ndirs; j++)
10050 savehere[j] = 0;
10051 if (saved[j] < dirs[i].length)
10053 /* Determine whether the dirs[i] path is a prefix of the
10054 dirs[j] path. */
10055 int k;
10057 k = dirs[j].prefix;
10058 while (k != -1 && k != (int) i)
10059 k = dirs[k].prefix;
10061 if (k == (int) i)
10063 /* Yes it is. We can possibly save some memory by
10064 writing the filenames in dirs[j] relative to
10065 dirs[i]. */
10066 savehere[j] = dirs[i].length;
10067 total += (savehere[j] - saved[j]) * dirs[j].count;
10072 /* Check whether we can save enough to justify adding the dirs[i]
10073 directory. */
10074 if (total > dirs[i].length + 1)
10076 /* It's worthwhile adding. */
10077 for (j = i; j < ndirs; j++)
10078 if (savehere[j] > 0)
10080 /* Remember how much we saved for this directory so far. */
10081 saved[j] = savehere[j];
10083 /* Remember the prefix directory. */
10084 dirs[j].dir_idx = i;
10089 /* Emit the directory name table. */
10090 idx_offset = dirs[0].length > 0 ? 1 : 0;
10091 for (i = 1 - idx_offset; i < ndirs; i++)
10092 dw2_asm_output_nstring (dirs[i].path,
10093 dirs[i].length
10094 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
10095 "Directory Entry: %#x", i + idx_offset);
10097 dw2_asm_output_data (1, 0, "End directory table");
10099 /* We have to emit them in the order of emitted_number since that's
10100 used in the debug info generation. To do this efficiently we
10101 generate a back-mapping of the indices first. */
10102 backmap = XALLOCAVEC (int, numfiles);
10103 for (i = 0; i < numfiles; i++)
10104 backmap[files[i].file_idx->emitted_number - 1] = i;
10106 /* Now write all the file names. */
10107 for (i = 0; i < numfiles; i++)
10109 int file_idx = backmap[i];
10110 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
10112 #ifdef VMS_DEBUGGING_INFO
10113 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
10115 /* Setting these fields can lead to debugger miscomparisons,
10116 but VMS Debug requires them to be set correctly. */
10118 int ver;
10119 long long cdt;
10120 long siz;
10121 int maxfilelen = strlen (files[file_idx].path)
10122 + dirs[dir_idx].length
10123 + MAX_VMS_VERSION_LEN + 1;
10124 char *filebuf = XALLOCAVEC (char, maxfilelen);
10126 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
10127 snprintf (filebuf, maxfilelen, "%s;%d",
10128 files[file_idx].path + dirs[dir_idx].length, ver);
10130 dw2_asm_output_nstring
10131 (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
10133 /* Include directory index. */
10134 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10136 /* Modification time. */
10137 dw2_asm_output_data_uleb128
10138 ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
10139 ? cdt : 0,
10140 NULL);
10142 /* File length in bytes. */
10143 dw2_asm_output_data_uleb128
10144 ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
10145 ? siz : 0,
10146 NULL);
10147 #else
10148 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
10149 "File Entry: %#x", (unsigned) i + 1);
10151 /* Include directory index. */
10152 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10154 /* Modification time. */
10155 dw2_asm_output_data_uleb128 (0, NULL);
10157 /* File length in bytes. */
10158 dw2_asm_output_data_uleb128 (0, NULL);
10159 #endif /* VMS_DEBUGGING_INFO */
10162 dw2_asm_output_data (1, 0, "End file name table");
10166 /* Output one line number table into the .debug_line section. */
10168 static void
10169 output_one_line_info_table (dw_line_info_table *table)
10171 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
10172 unsigned int current_line = 1;
10173 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
10174 dw_line_info_entry *ent;
10175 size_t i;
10177 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
10179 switch (ent->opcode)
10181 case LI_set_address:
10182 /* ??? Unfortunately, we have little choice here currently, and
10183 must always use the most general form. GCC does not know the
10184 address delta itself, so we can't use DW_LNS_advance_pc. Many
10185 ports do have length attributes which will give an upper bound
10186 on the address range. We could perhaps use length attributes
10187 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
10188 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
10190 /* This can handle any delta. This takes
10191 4+DWARF2_ADDR_SIZE bytes. */
10192 dw2_asm_output_data (1, 0, "set address %s", line_label);
10193 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10194 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10195 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
10196 break;
10198 case LI_set_line:
10199 if (ent->val == current_line)
10201 /* We still need to start a new row, so output a copy insn. */
10202 dw2_asm_output_data (1, DW_LNS_copy,
10203 "copy line %u", current_line);
10205 else
10207 int line_offset = ent->val - current_line;
10208 int line_delta = line_offset - DWARF_LINE_BASE;
10210 current_line = ent->val;
10211 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
10213 /* This can handle deltas from -10 to 234, using the current
10214 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
10215 This takes 1 byte. */
10216 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
10217 "line %u", current_line);
10219 else
10221 /* This can handle any delta. This takes at least 4 bytes,
10222 depending on the value being encoded. */
10223 dw2_asm_output_data (1, DW_LNS_advance_line,
10224 "advance to line %u", current_line);
10225 dw2_asm_output_data_sleb128 (line_offset, NULL);
10226 dw2_asm_output_data (1, DW_LNS_copy, NULL);
10229 break;
10231 case LI_set_file:
10232 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
10233 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10234 break;
10236 case LI_set_column:
10237 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
10238 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10239 break;
10241 case LI_negate_stmt:
10242 current_is_stmt = !current_is_stmt;
10243 dw2_asm_output_data (1, DW_LNS_negate_stmt,
10244 "is_stmt %d", current_is_stmt);
10245 break;
10247 case LI_set_prologue_end:
10248 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
10249 "set prologue end");
10250 break;
10252 case LI_set_epilogue_begin:
10253 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
10254 "set epilogue begin");
10255 break;
10257 case LI_set_discriminator:
10258 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
10259 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
10260 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
10261 dw2_asm_output_data_uleb128 (ent->val, NULL);
10262 break;
10266 /* Emit debug info for the address of the end of the table. */
10267 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
10268 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10269 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10270 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
10272 dw2_asm_output_data (1, 0, "end sequence");
10273 dw2_asm_output_data_uleb128 (1, NULL);
10274 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
10277 /* Output the source line number correspondence information. This
10278 information goes into the .debug_line section. */
10280 static void
10281 output_line_info (bool prologue_only)
10283 char l1[20], l2[20], p1[20], p2[20];
10284 /* We don't support DWARFv5 line tables yet. */
10285 int ver = dwarf_version < 5 ? dwarf_version : 4;
10286 bool saw_one = false;
10287 int opc;
10289 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
10290 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
10291 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
10292 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
10294 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10295 dw2_asm_output_data (4, 0xffffffff,
10296 "Initial length escape value indicating 64-bit DWARF extension");
10297 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
10298 "Length of Source Line Info");
10299 ASM_OUTPUT_LABEL (asm_out_file, l1);
10301 dw2_asm_output_data (2, ver, "DWARF Version");
10302 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
10303 ASM_OUTPUT_LABEL (asm_out_file, p1);
10305 /* Define the architecture-dependent minimum instruction length (in bytes).
10306 In this implementation of DWARF, this field is used for information
10307 purposes only. Since GCC generates assembly language, we have no
10308 a priori knowledge of how many instruction bytes are generated for each
10309 source line, and therefore can use only the DW_LNE_set_address and
10310 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
10311 this as '1', which is "correct enough" for all architectures,
10312 and don't let the target override. */
10313 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
10315 if (ver >= 4)
10316 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
10317 "Maximum Operations Per Instruction");
10318 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
10319 "Default is_stmt_start flag");
10320 dw2_asm_output_data (1, DWARF_LINE_BASE,
10321 "Line Base Value (Special Opcodes)");
10322 dw2_asm_output_data (1, DWARF_LINE_RANGE,
10323 "Line Range Value (Special Opcodes)");
10324 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
10325 "Special Opcode Base");
10327 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
10329 int n_op_args;
10330 switch (opc)
10332 case DW_LNS_advance_pc:
10333 case DW_LNS_advance_line:
10334 case DW_LNS_set_file:
10335 case DW_LNS_set_column:
10336 case DW_LNS_fixed_advance_pc:
10337 case DW_LNS_set_isa:
10338 n_op_args = 1;
10339 break;
10340 default:
10341 n_op_args = 0;
10342 break;
10345 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
10346 opc, n_op_args);
10349 /* Write out the information about the files we use. */
10350 output_file_names ();
10351 ASM_OUTPUT_LABEL (asm_out_file, p2);
10352 if (prologue_only)
10354 /* Output the marker for the end of the line number info. */
10355 ASM_OUTPUT_LABEL (asm_out_file, l2);
10356 return;
10359 if (separate_line_info)
10361 dw_line_info_table *table;
10362 size_t i;
10364 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
10365 if (table->in_use)
10367 output_one_line_info_table (table);
10368 saw_one = true;
10371 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
10373 output_one_line_info_table (cold_text_section_line_info);
10374 saw_one = true;
10377 /* ??? Some Darwin linkers crash on a .debug_line section with no
10378 sequences. Further, merely a DW_LNE_end_sequence entry is not
10379 sufficient -- the address column must also be initialized.
10380 Make sure to output at least one set_address/end_sequence pair,
10381 choosing .text since that section is always present. */
10382 if (text_section_line_info->in_use || !saw_one)
10383 output_one_line_info_table (text_section_line_info);
10385 /* Output the marker for the end of the line number info. */
10386 ASM_OUTPUT_LABEL (asm_out_file, l2);
10389 /* Given a pointer to a tree node for some base type, return a pointer to
10390 a DIE that describes the given type.
10392 This routine must only be called for GCC type nodes that correspond to
10393 Dwarf base (fundamental) types. */
10395 static dw_die_ref
10396 base_type_die (tree type)
10398 dw_die_ref base_type_result;
10399 enum dwarf_type encoding;
10401 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
10402 return 0;
10404 /* If this is a subtype that should not be emitted as a subrange type,
10405 use the base type. See subrange_type_for_debug_p. */
10406 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
10407 type = TREE_TYPE (type);
10409 switch (TREE_CODE (type))
10411 case INTEGER_TYPE:
10412 if ((dwarf_version >= 4 || !dwarf_strict)
10413 && TYPE_NAME (type)
10414 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10415 && DECL_IS_BUILTIN (TYPE_NAME (type))
10416 && DECL_NAME (TYPE_NAME (type)))
10418 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
10419 if (strcmp (name, "char16_t") == 0
10420 || strcmp (name, "char32_t") == 0)
10422 encoding = DW_ATE_UTF;
10423 break;
10426 if (TYPE_STRING_FLAG (type))
10428 if (TYPE_UNSIGNED (type))
10429 encoding = DW_ATE_unsigned_char;
10430 else
10431 encoding = DW_ATE_signed_char;
10433 else if (TYPE_UNSIGNED (type))
10434 encoding = DW_ATE_unsigned;
10435 else
10436 encoding = DW_ATE_signed;
10437 break;
10439 case REAL_TYPE:
10440 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
10442 if (dwarf_version >= 3 || !dwarf_strict)
10443 encoding = DW_ATE_decimal_float;
10444 else
10445 encoding = DW_ATE_lo_user;
10447 else
10448 encoding = DW_ATE_float;
10449 break;
10451 case FIXED_POINT_TYPE:
10452 if (!(dwarf_version >= 3 || !dwarf_strict))
10453 encoding = DW_ATE_lo_user;
10454 else if (TYPE_UNSIGNED (type))
10455 encoding = DW_ATE_unsigned_fixed;
10456 else
10457 encoding = DW_ATE_signed_fixed;
10458 break;
10460 /* Dwarf2 doesn't know anything about complex ints, so use
10461 a user defined type for it. */
10462 case COMPLEX_TYPE:
10463 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
10464 encoding = DW_ATE_complex_float;
10465 else
10466 encoding = DW_ATE_lo_user;
10467 break;
10469 case BOOLEAN_TYPE:
10470 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
10471 encoding = DW_ATE_boolean;
10472 break;
10474 default:
10475 /* No other TREE_CODEs are Dwarf fundamental types. */
10476 gcc_unreachable ();
10479 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
10481 add_AT_unsigned (base_type_result, DW_AT_byte_size,
10482 int_size_in_bytes (type));
10483 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
10484 add_pubtype (type, base_type_result);
10486 return base_type_result;
10489 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
10490 named 'auto' in its type: return true for it, false otherwise. */
10492 static inline bool
10493 is_cxx_auto (tree type)
10495 if (is_cxx ())
10497 tree name = TYPE_IDENTIFIER (type);
10498 if (name == get_identifier ("auto")
10499 || name == get_identifier ("decltype(auto)"))
10500 return true;
10502 return false;
10505 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
10506 given input type is a Dwarf "fundamental" type. Otherwise return null. */
10508 static inline int
10509 is_base_type (tree type)
10511 switch (TREE_CODE (type))
10513 case ERROR_MARK:
10514 case VOID_TYPE:
10515 case INTEGER_TYPE:
10516 case REAL_TYPE:
10517 case FIXED_POINT_TYPE:
10518 case COMPLEX_TYPE:
10519 case BOOLEAN_TYPE:
10520 case POINTER_BOUNDS_TYPE:
10521 return 1;
10523 case ARRAY_TYPE:
10524 case RECORD_TYPE:
10525 case UNION_TYPE:
10526 case QUAL_UNION_TYPE:
10527 case ENUMERAL_TYPE:
10528 case FUNCTION_TYPE:
10529 case METHOD_TYPE:
10530 case POINTER_TYPE:
10531 case REFERENCE_TYPE:
10532 case NULLPTR_TYPE:
10533 case OFFSET_TYPE:
10534 case LANG_TYPE:
10535 case VECTOR_TYPE:
10536 return 0;
10538 default:
10539 if (is_cxx_auto (type))
10540 return 0;
10541 gcc_unreachable ();
10544 return 0;
10547 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
10548 node, return the size in bits for the type if it is a constant, or else
10549 return the alignment for the type if the type's size is not constant, or
10550 else return BITS_PER_WORD if the type actually turns out to be an
10551 ERROR_MARK node. */
10553 static inline unsigned HOST_WIDE_INT
10554 simple_type_size_in_bits (const_tree type)
10556 if (TREE_CODE (type) == ERROR_MARK)
10557 return BITS_PER_WORD;
10558 else if (TYPE_SIZE (type) == NULL_TREE)
10559 return 0;
10560 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
10561 return tree_to_uhwi (TYPE_SIZE (type));
10562 else
10563 return TYPE_ALIGN (type);
10566 /* Similarly, but return an offset_int instead of UHWI. */
10568 static inline offset_int
10569 offset_int_type_size_in_bits (const_tree type)
10571 if (TREE_CODE (type) == ERROR_MARK)
10572 return BITS_PER_WORD;
10573 else if (TYPE_SIZE (type) == NULL_TREE)
10574 return 0;
10575 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
10576 return wi::to_offset (TYPE_SIZE (type));
10577 else
10578 return TYPE_ALIGN (type);
10581 /* Given a pointer to a tree node for a subrange type, return a pointer
10582 to a DIE that describes the given type. */
10584 static dw_die_ref
10585 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
10587 dw_die_ref subrange_die;
10588 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
10590 if (context_die == NULL)
10591 context_die = comp_unit_die ();
10593 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
10595 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
10597 /* The size of the subrange type and its base type do not match,
10598 so we need to generate a size attribute for the subrange type. */
10599 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
10602 if (low)
10603 add_bound_info (subrange_die, DW_AT_lower_bound, low, NULL);
10604 if (high)
10605 add_bound_info (subrange_die, DW_AT_upper_bound, high, NULL);
10607 return subrange_die;
10610 /* Returns the (const and/or volatile) cv_qualifiers associated with
10611 the decl node. This will normally be augmented with the
10612 cv_qualifiers of the underlying type in add_type_attribute. */
10614 static int
10615 decl_quals (const_tree decl)
10617 return ((TREE_READONLY (decl)
10618 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
10619 | (TREE_THIS_VOLATILE (decl)
10620 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
10623 /* Determine the TYPE whose qualifiers match the largest strict subset
10624 of the given TYPE_QUALS, and return its qualifiers. Ignore all
10625 qualifiers outside QUAL_MASK. */
10627 static int
10628 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
10630 tree t;
10631 int best_rank = 0, best_qual = 0, max_rank;
10633 type_quals &= qual_mask;
10634 max_rank = popcount_hwi (type_quals) - 1;
10636 for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
10637 t = TYPE_NEXT_VARIANT (t))
10639 int q = TYPE_QUALS (t) & qual_mask;
10641 if ((q & type_quals) == q && q != type_quals
10642 && check_base_type (t, type))
10644 int rank = popcount_hwi (q);
10646 if (rank > best_rank)
10648 best_rank = rank;
10649 best_qual = q;
10654 return best_qual;
10657 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
10658 entry that chains various modifiers in front of the given type. */
10660 static dw_die_ref
10661 modified_type_die (tree type, int cv_quals, dw_die_ref context_die)
10663 enum tree_code code = TREE_CODE (type);
10664 dw_die_ref mod_type_die;
10665 dw_die_ref sub_die = NULL;
10666 tree item_type = NULL;
10667 tree qualified_type;
10668 tree name, low, high;
10669 dw_die_ref mod_scope;
10670 /* Only these cv-qualifiers are currently handled. */
10671 const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
10672 | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC);
10674 if (code == ERROR_MARK)
10675 return NULL;
10677 cv_quals &= cv_qual_mask;
10679 /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
10680 tag modifier (and not an attribute) old consumers won't be able
10681 to handle it. */
10682 if (dwarf_version < 3)
10683 cv_quals &= ~TYPE_QUAL_RESTRICT;
10685 /* Likewise for DW_TAG_atomic_type for DWARFv5. */
10686 if (dwarf_version < 5)
10687 cv_quals &= ~TYPE_QUAL_ATOMIC;
10689 /* See if we already have the appropriately qualified variant of
10690 this type. */
10691 qualified_type = get_qualified_type (type, cv_quals);
10693 if (qualified_type == sizetype
10694 && TYPE_NAME (qualified_type)
10695 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
10697 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
10699 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
10700 && TYPE_PRECISION (t)
10701 == TYPE_PRECISION (qualified_type)
10702 && TYPE_UNSIGNED (t)
10703 == TYPE_UNSIGNED (qualified_type));
10704 qualified_type = t;
10707 /* If we do, then we can just use its DIE, if it exists. */
10708 if (qualified_type)
10710 mod_type_die = lookup_type_die (qualified_type);
10711 if (mod_type_die)
10712 return mod_type_die;
10715 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
10717 /* Handle C typedef types. */
10718 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
10719 && !DECL_ARTIFICIAL (name))
10721 tree dtype = TREE_TYPE (name);
10723 if (qualified_type == dtype)
10725 /* For a named type, use the typedef. */
10726 gen_type_die (qualified_type, context_die);
10727 return lookup_type_die (qualified_type);
10729 else
10731 int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
10732 dquals &= cv_qual_mask;
10733 if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
10734 || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
10735 /* cv-unqualified version of named type. Just use
10736 the unnamed type to which it refers. */
10737 return modified_type_die (DECL_ORIGINAL_TYPE (name),
10738 cv_quals, context_die);
10739 /* Else cv-qualified version of named type; fall through. */
10743 mod_scope = scope_die_for (type, context_die);
10745 if (cv_quals)
10747 struct qual_info { int q; enum dwarf_tag t; };
10748 static const struct qual_info qual_info[] =
10750 { TYPE_QUAL_ATOMIC, DW_TAG_atomic_type },
10751 { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
10752 { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
10753 { TYPE_QUAL_CONST, DW_TAG_const_type },
10755 int sub_quals;
10756 unsigned i;
10758 /* Determine a lesser qualified type that most closely matches
10759 this one. Then generate DW_TAG_* entries for the remaining
10760 qualifiers. */
10761 sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
10762 cv_qual_mask);
10763 mod_type_die = modified_type_die (type, sub_quals, context_die);
10765 for (i = 0; i < sizeof (qual_info) / sizeof (qual_info[0]); i++)
10766 if (qual_info[i].q & cv_quals & ~sub_quals)
10768 dw_die_ref d = new_die (qual_info[i].t, mod_scope, type);
10769 if (mod_type_die)
10770 add_AT_die_ref (d, DW_AT_type, mod_type_die);
10771 mod_type_die = d;
10774 else if (code == POINTER_TYPE)
10776 mod_type_die = new_die (DW_TAG_pointer_type, mod_scope, type);
10777 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10778 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10779 item_type = TREE_TYPE (type);
10780 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10781 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10782 TYPE_ADDR_SPACE (item_type));
10784 else if (code == REFERENCE_TYPE)
10786 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
10787 mod_type_die = new_die (DW_TAG_rvalue_reference_type, mod_scope,
10788 type);
10789 else
10790 mod_type_die = new_die (DW_TAG_reference_type, mod_scope, type);
10791 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10792 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10793 item_type = TREE_TYPE (type);
10794 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10795 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10796 TYPE_ADDR_SPACE (item_type));
10798 else if (code == INTEGER_TYPE
10799 && TREE_TYPE (type) != NULL_TREE
10800 && subrange_type_for_debug_p (type, &low, &high))
10802 mod_type_die = subrange_type_die (type, low, high, context_die);
10803 item_type = TREE_TYPE (type);
10805 else if (is_base_type (type))
10806 mod_type_die = base_type_die (type);
10807 else
10809 gen_type_die (type, context_die);
10811 /* We have to get the type_main_variant here (and pass that to the
10812 `lookup_type_die' routine) because the ..._TYPE node we have
10813 might simply be a *copy* of some original type node (where the
10814 copy was created to help us keep track of typedef names) and
10815 that copy might have a different TYPE_UID from the original
10816 ..._TYPE node. */
10817 if (TREE_CODE (type) != VECTOR_TYPE)
10818 return lookup_type_die (type_main_variant (type));
10819 else
10820 /* Vectors have the debugging information in the type,
10821 not the main variant. */
10822 return lookup_type_die (type);
10825 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
10826 don't output a DW_TAG_typedef, since there isn't one in the
10827 user's program; just attach a DW_AT_name to the type.
10828 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
10829 if the base type already has the same name. */
10830 if (name
10831 && ((TREE_CODE (name) != TYPE_DECL
10832 && (qualified_type == TYPE_MAIN_VARIANT (type)
10833 || (cv_quals == TYPE_UNQUALIFIED)))
10834 || (TREE_CODE (name) == TYPE_DECL
10835 && TREE_TYPE (name) == qualified_type
10836 && DECL_NAME (name))))
10838 if (TREE_CODE (name) == TYPE_DECL)
10839 /* Could just call add_name_and_src_coords_attributes here,
10840 but since this is a builtin type it doesn't have any
10841 useful source coordinates anyway. */
10842 name = DECL_NAME (name);
10843 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
10845 /* This probably indicates a bug. */
10846 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
10848 name = TYPE_IDENTIFIER (type);
10849 add_name_attribute (mod_type_die,
10850 name ? IDENTIFIER_POINTER (name) : "__unknown__");
10853 if (qualified_type)
10854 equate_type_number_to_die (qualified_type, mod_type_die);
10856 if (item_type)
10857 /* We must do this after the equate_type_number_to_die call, in case
10858 this is a recursive type. This ensures that the modified_type_die
10859 recursion will terminate even if the type is recursive. Recursive
10860 types are possible in Ada. */
10861 sub_die = modified_type_die (item_type,
10862 TYPE_QUALS_NO_ADDR_SPACE (item_type),
10863 context_die);
10865 if (sub_die != NULL)
10866 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
10868 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
10869 if (TYPE_ARTIFICIAL (type))
10870 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
10872 return mod_type_die;
10875 /* Generate DIEs for the generic parameters of T.
10876 T must be either a generic type or a generic function.
10877 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
10879 static void
10880 gen_generic_params_dies (tree t)
10882 tree parms, args;
10883 int parms_num, i;
10884 dw_die_ref die = NULL;
10885 int non_default;
10887 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
10888 return;
10890 if (TYPE_P (t))
10891 die = lookup_type_die (t);
10892 else if (DECL_P (t))
10893 die = lookup_decl_die (t);
10895 gcc_assert (die);
10897 parms = lang_hooks.get_innermost_generic_parms (t);
10898 if (!parms)
10899 /* T has no generic parameter. It means T is neither a generic type
10900 or function. End of story. */
10901 return;
10903 parms_num = TREE_VEC_LENGTH (parms);
10904 args = lang_hooks.get_innermost_generic_args (t);
10905 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
10906 non_default = int_cst_value (TREE_CHAIN (args));
10907 else
10908 non_default = TREE_VEC_LENGTH (args);
10909 for (i = 0; i < parms_num; i++)
10911 tree parm, arg, arg_pack_elems;
10912 dw_die_ref parm_die;
10914 parm = TREE_VEC_ELT (parms, i);
10915 arg = TREE_VEC_ELT (args, i);
10916 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
10917 gcc_assert (parm && TREE_VALUE (parm) && arg);
10919 if (parm && TREE_VALUE (parm) && arg)
10921 /* If PARM represents a template parameter pack,
10922 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
10923 by DW_TAG_template_*_parameter DIEs for the argument
10924 pack elements of ARG. Note that ARG would then be
10925 an argument pack. */
10926 if (arg_pack_elems)
10927 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
10928 arg_pack_elems,
10929 die);
10930 else
10931 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
10932 true /* emit name */, die);
10933 if (i >= non_default)
10934 add_AT_flag (parm_die, DW_AT_default_value, 1);
10939 /* Create and return a DIE for PARM which should be
10940 the representation of a generic type parameter.
10941 For instance, in the C++ front end, PARM would be a template parameter.
10942 ARG is the argument to PARM.
10943 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
10944 name of the PARM.
10945 PARENT_DIE is the parent DIE which the new created DIE should be added to,
10946 as a child node. */
10948 static dw_die_ref
10949 generic_parameter_die (tree parm, tree arg,
10950 bool emit_name_p,
10951 dw_die_ref parent_die)
10953 dw_die_ref tmpl_die = NULL;
10954 const char *name = NULL;
10956 if (!parm || !DECL_NAME (parm) || !arg)
10957 return NULL;
10959 /* We support non-type generic parameters and arguments,
10960 type generic parameters and arguments, as well as
10961 generic generic parameters (a.k.a. template template parameters in C++)
10962 and arguments. */
10963 if (TREE_CODE (parm) == PARM_DECL)
10964 /* PARM is a nontype generic parameter */
10965 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
10966 else if (TREE_CODE (parm) == TYPE_DECL)
10967 /* PARM is a type generic parameter. */
10968 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
10969 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10970 /* PARM is a generic generic parameter.
10971 Its DIE is a GNU extension. It shall have a
10972 DW_AT_name attribute to represent the name of the template template
10973 parameter, and a DW_AT_GNU_template_name attribute to represent the
10974 name of the template template argument. */
10975 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
10976 parent_die, parm);
10977 else
10978 gcc_unreachable ();
10980 if (tmpl_die)
10982 tree tmpl_type;
10984 /* If PARM is a generic parameter pack, it means we are
10985 emitting debug info for a template argument pack element.
10986 In other terms, ARG is a template argument pack element.
10987 In that case, we don't emit any DW_AT_name attribute for
10988 the die. */
10989 if (emit_name_p)
10991 name = IDENTIFIER_POINTER (DECL_NAME (parm));
10992 gcc_assert (name);
10993 add_AT_string (tmpl_die, DW_AT_name, name);
10996 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10998 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
10999 TMPL_DIE should have a child DW_AT_type attribute that is set
11000 to the type of the argument to PARM, which is ARG.
11001 If PARM is a type generic parameter, TMPL_DIE should have a
11002 child DW_AT_type that is set to ARG. */
11003 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
11004 add_type_attribute (tmpl_die, tmpl_type,
11005 (TREE_THIS_VOLATILE (tmpl_type)
11006 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
11007 parent_die);
11009 else
11011 /* So TMPL_DIE is a DIE representing a
11012 a generic generic template parameter, a.k.a template template
11013 parameter in C++ and arg is a template. */
11015 /* The DW_AT_GNU_template_name attribute of the DIE must be set
11016 to the name of the argument. */
11017 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
11018 if (name)
11019 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
11022 if (TREE_CODE (parm) == PARM_DECL)
11023 /* So PARM is a non-type generic parameter.
11024 DWARF3 5.6.8 says we must set a DW_AT_const_value child
11025 attribute of TMPL_DIE which value represents the value
11026 of ARG.
11027 We must be careful here:
11028 The value of ARG might reference some function decls.
11029 We might currently be emitting debug info for a generic
11030 type and types are emitted before function decls, we don't
11031 know if the function decls referenced by ARG will actually be
11032 emitted after cgraph computations.
11033 So must defer the generation of the DW_AT_const_value to
11034 after cgraph is ready. */
11035 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
11038 return tmpl_die;
11041 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
11042 PARM_PACK must be a template parameter pack. The returned DIE
11043 will be child DIE of PARENT_DIE. */
11045 static dw_die_ref
11046 template_parameter_pack_die (tree parm_pack,
11047 tree parm_pack_args,
11048 dw_die_ref parent_die)
11050 dw_die_ref die;
11051 int j;
11053 gcc_assert (parent_die && parm_pack);
11055 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
11056 add_name_and_src_coords_attributes (die, parm_pack);
11057 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
11058 generic_parameter_die (parm_pack,
11059 TREE_VEC_ELT (parm_pack_args, j),
11060 false /* Don't emit DW_AT_name */,
11061 die);
11062 return die;
11065 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
11066 an enumerated type. */
11068 static inline int
11069 type_is_enum (const_tree type)
11071 return TREE_CODE (type) == ENUMERAL_TYPE;
11074 /* Return the DBX register number described by a given RTL node. */
11076 static unsigned int
11077 dbx_reg_number (const_rtx rtl)
11079 unsigned regno = REGNO (rtl);
11081 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
11083 #ifdef LEAF_REG_REMAP
11084 if (crtl->uses_only_leaf_regs)
11086 int leaf_reg = LEAF_REG_REMAP (regno);
11087 if (leaf_reg != -1)
11088 regno = (unsigned) leaf_reg;
11090 #endif
11092 regno = DBX_REGISTER_NUMBER (regno);
11093 gcc_assert (regno != INVALID_REGNUM);
11094 return regno;
11097 /* Optionally add a DW_OP_piece term to a location description expression.
11098 DW_OP_piece is only added if the location description expression already
11099 doesn't end with DW_OP_piece. */
11101 static void
11102 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
11104 dw_loc_descr_ref loc;
11106 if (*list_head != NULL)
11108 /* Find the end of the chain. */
11109 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
11112 if (loc->dw_loc_opc != DW_OP_piece)
11113 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
11117 /* Return a location descriptor that designates a machine register or
11118 zero if there is none. */
11120 static dw_loc_descr_ref
11121 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
11123 rtx regs;
11125 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
11126 return 0;
11128 /* We only use "frame base" when we're sure we're talking about the
11129 post-prologue local stack frame. We do this by *not* running
11130 register elimination until this point, and recognizing the special
11131 argument pointer and soft frame pointer rtx's.
11132 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
11133 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
11134 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
11136 dw_loc_descr_ref result = NULL;
11138 if (dwarf_version >= 4 || !dwarf_strict)
11140 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
11141 initialized);
11142 if (result)
11143 add_loc_descr (&result,
11144 new_loc_descr (DW_OP_stack_value, 0, 0));
11146 return result;
11149 regs = targetm.dwarf_register_span (rtl);
11151 if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
11152 return multiple_reg_loc_descriptor (rtl, regs, initialized);
11153 else
11155 unsigned int dbx_regnum = dbx_reg_number (rtl);
11156 if (dbx_regnum == IGNORED_DWARF_REGNUM)
11157 return 0;
11158 return one_reg_loc_descriptor (dbx_regnum, initialized);
11162 /* Return a location descriptor that designates a machine register for
11163 a given hard register number. */
11165 static dw_loc_descr_ref
11166 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
11168 dw_loc_descr_ref reg_loc_descr;
11170 if (regno <= 31)
11171 reg_loc_descr
11172 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
11173 else
11174 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
11176 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11177 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11179 return reg_loc_descr;
11182 /* Given an RTL of a register, return a location descriptor that
11183 designates a value that spans more than one register. */
11185 static dw_loc_descr_ref
11186 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
11187 enum var_init_status initialized)
11189 int size, i;
11190 dw_loc_descr_ref loc_result = NULL;
11192 /* Simple, contiguous registers. */
11193 if (regs == NULL_RTX)
11195 unsigned reg = REGNO (rtl);
11196 int nregs;
11198 #ifdef LEAF_REG_REMAP
11199 if (crtl->uses_only_leaf_regs)
11201 int leaf_reg = LEAF_REG_REMAP (reg);
11202 if (leaf_reg != -1)
11203 reg = (unsigned) leaf_reg;
11205 #endif
11207 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
11208 nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
11210 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
11212 loc_result = NULL;
11213 while (nregs--)
11215 dw_loc_descr_ref t;
11217 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
11218 VAR_INIT_STATUS_INITIALIZED);
11219 add_loc_descr (&loc_result, t);
11220 add_loc_descr_op_piece (&loc_result, size);
11221 ++reg;
11223 return loc_result;
11226 /* Now onto stupid register sets in non contiguous locations. */
11228 gcc_assert (GET_CODE (regs) == PARALLEL);
11230 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
11231 loc_result = NULL;
11233 for (i = 0; i < XVECLEN (regs, 0); ++i)
11235 dw_loc_descr_ref t;
11237 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
11238 VAR_INIT_STATUS_INITIALIZED);
11239 add_loc_descr (&loc_result, t);
11240 add_loc_descr_op_piece (&loc_result, size);
11243 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
11244 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11245 return loc_result;
11248 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
11250 /* Return a location descriptor that designates a constant i,
11251 as a compound operation from constant (i >> shift), constant shift
11252 and DW_OP_shl. */
11254 static dw_loc_descr_ref
11255 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11257 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
11258 add_loc_descr (&ret, int_loc_descriptor (shift));
11259 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11260 return ret;
11263 /* Return a location descriptor that designates a constant. */
11265 static dw_loc_descr_ref
11266 int_loc_descriptor (HOST_WIDE_INT i)
11268 enum dwarf_location_atom op;
11270 /* Pick the smallest representation of a constant, rather than just
11271 defaulting to the LEB encoding. */
11272 if (i >= 0)
11274 int clz = clz_hwi (i);
11275 int ctz = ctz_hwi (i);
11276 if (i <= 31)
11277 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
11278 else if (i <= 0xff)
11279 op = DW_OP_const1u;
11280 else if (i <= 0xffff)
11281 op = DW_OP_const2u;
11282 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11283 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11284 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
11285 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
11286 while DW_OP_const4u is 5 bytes. */
11287 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
11288 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11289 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11290 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
11291 while DW_OP_const4u is 5 bytes. */
11292 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11293 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11294 op = DW_OP_const4u;
11295 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11296 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11297 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
11298 while DW_OP_constu of constant >= 0x100000000 takes at least
11299 6 bytes. */
11300 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11301 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11302 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
11303 >= HOST_BITS_PER_WIDE_INT)
11304 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
11305 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
11306 while DW_OP_constu takes in this case at least 6 bytes. */
11307 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
11308 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11309 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11310 && size_of_uleb128 (i) > 6)
11311 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
11312 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
11313 else
11314 op = DW_OP_constu;
11316 else
11318 if (i >= -0x80)
11319 op = DW_OP_const1s;
11320 else if (i >= -0x8000)
11321 op = DW_OP_const2s;
11322 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11324 if (size_of_int_loc_descriptor (i) < 5)
11326 dw_loc_descr_ref ret = int_loc_descriptor (-i);
11327 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11328 return ret;
11330 op = DW_OP_const4s;
11332 else
11334 if (size_of_int_loc_descriptor (i)
11335 < (unsigned long) 1 + size_of_sleb128 (i))
11337 dw_loc_descr_ref ret = int_loc_descriptor (-i);
11338 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11339 return ret;
11341 op = DW_OP_consts;
11345 return new_loc_descr (op, i, 0);
11348 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
11349 without actually allocating it. */
11351 static unsigned long
11352 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11354 return size_of_int_loc_descriptor (i >> shift)
11355 + size_of_int_loc_descriptor (shift)
11356 + 1;
11359 /* Return size_of_locs (int_loc_descriptor (i)) without
11360 actually allocating it. */
11362 static unsigned long
11363 size_of_int_loc_descriptor (HOST_WIDE_INT i)
11365 unsigned long s;
11367 if (i >= 0)
11369 int clz, ctz;
11370 if (i <= 31)
11371 return 1;
11372 else if (i <= 0xff)
11373 return 2;
11374 else if (i <= 0xffff)
11375 return 3;
11376 clz = clz_hwi (i);
11377 ctz = ctz_hwi (i);
11378 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11379 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11380 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11381 - clz - 5);
11382 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11383 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11384 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11385 - clz - 8);
11386 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11387 return 5;
11388 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
11389 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11390 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11391 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11392 - clz - 8);
11393 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11394 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
11395 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11396 - clz - 16);
11397 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11398 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11399 && s > 6)
11400 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11401 - clz - 32);
11402 else
11403 return 1 + s;
11405 else
11407 if (i >= -0x80)
11408 return 2;
11409 else if (i >= -0x8000)
11410 return 3;
11411 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11413 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11415 s = size_of_int_loc_descriptor (-i) + 1;
11416 if (s < 5)
11417 return s;
11419 return 5;
11421 else
11423 unsigned long r = 1 + size_of_sleb128 (i);
11424 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11426 s = size_of_int_loc_descriptor (-i) + 1;
11427 if (s < r)
11428 return s;
11430 return r;
11435 /* Return loc description representing "address" of integer value.
11436 This can appear only as toplevel expression. */
11438 static dw_loc_descr_ref
11439 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
11441 int litsize;
11442 dw_loc_descr_ref loc_result = NULL;
11444 if (!(dwarf_version >= 4 || !dwarf_strict))
11445 return NULL;
11447 litsize = size_of_int_loc_descriptor (i);
11448 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
11449 is more compact. For DW_OP_stack_value we need:
11450 litsize + 1 (DW_OP_stack_value)
11451 and for DW_OP_implicit_value:
11452 1 (DW_OP_implicit_value) + 1 (length) + size. */
11453 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
11455 loc_result = int_loc_descriptor (i);
11456 add_loc_descr (&loc_result,
11457 new_loc_descr (DW_OP_stack_value, 0, 0));
11458 return loc_result;
11461 loc_result = new_loc_descr (DW_OP_implicit_value,
11462 size, 0);
11463 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
11464 loc_result->dw_loc_oprnd2.v.val_int = i;
11465 return loc_result;
11468 /* Return a location descriptor that designates a base+offset location. */
11470 static dw_loc_descr_ref
11471 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
11472 enum var_init_status initialized)
11474 unsigned int regno;
11475 dw_loc_descr_ref result;
11476 dw_fde_ref fde = cfun->fde;
11478 /* We only use "frame base" when we're sure we're talking about the
11479 post-prologue local stack frame. We do this by *not* running
11480 register elimination until this point, and recognizing the special
11481 argument pointer and soft frame pointer rtx's. */
11482 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
11484 rtx elim = (ira_use_lra_p
11485 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
11486 : eliminate_regs (reg, VOIDmode, NULL_RTX));
11488 if (elim != reg)
11490 if (GET_CODE (elim) == PLUS)
11492 offset += INTVAL (XEXP (elim, 1));
11493 elim = XEXP (elim, 0);
11495 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
11496 && (elim == hard_frame_pointer_rtx
11497 || elim == stack_pointer_rtx))
11498 || elim == (frame_pointer_needed
11499 ? hard_frame_pointer_rtx
11500 : stack_pointer_rtx));
11502 /* If drap register is used to align stack, use frame
11503 pointer + offset to access stack variables. If stack
11504 is aligned without drap, use stack pointer + offset to
11505 access stack variables. */
11506 if (crtl->stack_realign_tried
11507 && reg == frame_pointer_rtx)
11509 int base_reg
11510 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
11511 ? HARD_FRAME_POINTER_REGNUM
11512 : REGNO (elim));
11513 return new_reg_loc_descr (base_reg, offset);
11516 gcc_assert (frame_pointer_fb_offset_valid);
11517 offset += frame_pointer_fb_offset;
11518 return new_loc_descr (DW_OP_fbreg, offset, 0);
11522 regno = REGNO (reg);
11523 #ifdef LEAF_REG_REMAP
11524 if (crtl->uses_only_leaf_regs)
11526 int leaf_reg = LEAF_REG_REMAP (regno);
11527 if (leaf_reg != -1)
11528 regno = (unsigned) leaf_reg;
11530 #endif
11531 regno = DWARF_FRAME_REGNUM (regno);
11533 if (!optimize && fde
11534 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
11536 /* Use cfa+offset to represent the location of arguments passed
11537 on the stack when drap is used to align stack.
11538 Only do this when not optimizing, for optimized code var-tracking
11539 is supposed to track where the arguments live and the register
11540 used as vdrap or drap in some spot might be used for something
11541 else in other part of the routine. */
11542 return new_loc_descr (DW_OP_fbreg, offset, 0);
11545 if (regno <= 31)
11546 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
11547 offset, 0);
11548 else
11549 result = new_loc_descr (DW_OP_bregx, regno, offset);
11551 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11552 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11554 return result;
11557 /* Return true if this RTL expression describes a base+offset calculation. */
11559 static inline int
11560 is_based_loc (const_rtx rtl)
11562 return (GET_CODE (rtl) == PLUS
11563 && ((REG_P (XEXP (rtl, 0))
11564 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
11565 && CONST_INT_P (XEXP (rtl, 1)))));
11568 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
11569 failed. */
11571 static dw_loc_descr_ref
11572 tls_mem_loc_descriptor (rtx mem)
11574 tree base;
11575 dw_loc_descr_ref loc_result;
11577 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
11578 return NULL;
11580 base = get_base_address (MEM_EXPR (mem));
11581 if (base == NULL
11582 || TREE_CODE (base) != VAR_DECL
11583 || !DECL_THREAD_LOCAL_P (base))
11584 return NULL;
11586 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
11587 if (loc_result == NULL)
11588 return NULL;
11590 if (MEM_OFFSET (mem))
11591 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
11593 return loc_result;
11596 /* Output debug info about reason why we failed to expand expression as dwarf
11597 expression. */
11599 static void
11600 expansion_failed (tree expr, rtx rtl, char const *reason)
11602 if (dump_file && (dump_flags & TDF_DETAILS))
11604 fprintf (dump_file, "Failed to expand as dwarf: ");
11605 if (expr)
11606 print_generic_expr (dump_file, expr, dump_flags);
11607 if (rtl)
11609 fprintf (dump_file, "\n");
11610 print_rtl (dump_file, rtl);
11612 fprintf (dump_file, "\nReason: %s\n", reason);
11616 /* Helper function for const_ok_for_output. */
11618 static bool
11619 const_ok_for_output_1 (rtx rtl)
11621 if (GET_CODE (rtl) == UNSPEC)
11623 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
11624 we can't express it in the debug info. */
11625 #ifdef ENABLE_CHECKING
11626 /* Don't complain about TLS UNSPECs, those are just too hard to
11627 delegitimize. Note this could be a non-decl SYMBOL_REF such as
11628 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
11629 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
11630 if (XVECLEN (rtl, 0) == 0
11631 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
11632 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE)
11633 inform (current_function_decl
11634 ? DECL_SOURCE_LOCATION (current_function_decl)
11635 : UNKNOWN_LOCATION,
11636 #if NUM_UNSPEC_VALUES > 0
11637 "non-delegitimized UNSPEC %s (%d) found in variable location",
11638 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
11639 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
11640 XINT (rtl, 1));
11641 #else
11642 "non-delegitimized UNSPEC %d found in variable location",
11643 XINT (rtl, 1));
11644 #endif
11645 #endif
11646 expansion_failed (NULL_TREE, rtl,
11647 "UNSPEC hasn't been delegitimized.\n");
11648 return false;
11651 if (targetm.const_not_ok_for_debug_p (rtl))
11653 expansion_failed (NULL_TREE, rtl,
11654 "Expression rejected for debug by the backend.\n");
11655 return false;
11658 /* FIXME: Refer to PR60655. It is possible for simplification
11659 of rtl expressions in var tracking to produce such expressions.
11660 We should really identify / validate expressions
11661 enclosed in CONST that can be handled by assemblers on various
11662 targets and only handle legitimate cases here. */
11663 if (GET_CODE (rtl) != SYMBOL_REF)
11665 if (GET_CODE (rtl) == NOT)
11666 return false;
11667 return true;
11670 if (CONSTANT_POOL_ADDRESS_P (rtl))
11672 bool marked;
11673 get_pool_constant_mark (rtl, &marked);
11674 /* If all references to this pool constant were optimized away,
11675 it was not output and thus we can't represent it. */
11676 if (!marked)
11678 expansion_failed (NULL_TREE, rtl,
11679 "Constant was removed from constant pool.\n");
11680 return false;
11684 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11685 return false;
11687 /* Avoid references to external symbols in debug info, on several targets
11688 the linker might even refuse to link when linking a shared library,
11689 and in many other cases the relocations for .debug_info/.debug_loc are
11690 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
11691 to be defined within the same shared library or executable are fine. */
11692 if (SYMBOL_REF_EXTERNAL_P (rtl))
11694 tree decl = SYMBOL_REF_DECL (rtl);
11696 if (decl == NULL || !targetm.binds_local_p (decl))
11698 expansion_failed (NULL_TREE, rtl,
11699 "Symbol not defined in current TU.\n");
11700 return false;
11704 return true;
11707 /* Return true if constant RTL can be emitted in DW_OP_addr or
11708 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
11709 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
11711 static bool
11712 const_ok_for_output (rtx rtl)
11714 if (GET_CODE (rtl) == SYMBOL_REF)
11715 return const_ok_for_output_1 (rtl);
11717 if (GET_CODE (rtl) == CONST)
11719 subrtx_var_iterator::array_type array;
11720 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
11721 if (!const_ok_for_output_1 (*iter))
11722 return false;
11723 return true;
11726 return true;
11729 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
11730 if possible, NULL otherwise. */
11732 static dw_die_ref
11733 base_type_for_mode (machine_mode mode, bool unsignedp)
11735 dw_die_ref type_die;
11736 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11738 if (type == NULL)
11739 return NULL;
11740 switch (TREE_CODE (type))
11742 case INTEGER_TYPE:
11743 case REAL_TYPE:
11744 break;
11745 default:
11746 return NULL;
11748 type_die = lookup_type_die (type);
11749 if (!type_die)
11750 type_die = modified_type_die (type, TYPE_UNQUALIFIED, comp_unit_die ());
11751 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
11752 return NULL;
11753 return type_die;
11756 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
11757 type matching MODE, or, if MODE is narrower than or as wide as
11758 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
11759 possible. */
11761 static dw_loc_descr_ref
11762 convert_descriptor_to_mode (machine_mode mode, dw_loc_descr_ref op)
11764 machine_mode outer_mode = mode;
11765 dw_die_ref type_die;
11766 dw_loc_descr_ref cvt;
11768 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11770 add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
11771 return op;
11773 type_die = base_type_for_mode (outer_mode, 1);
11774 if (type_die == NULL)
11775 return NULL;
11776 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11777 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11778 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11779 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11780 add_loc_descr (&op, cvt);
11781 return op;
11784 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
11786 static dw_loc_descr_ref
11787 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
11788 dw_loc_descr_ref op1)
11790 dw_loc_descr_ref ret = op0;
11791 add_loc_descr (&ret, op1);
11792 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11793 if (STORE_FLAG_VALUE != 1)
11795 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
11796 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
11798 return ret;
11801 /* Return location descriptor for signed comparison OP RTL. */
11803 static dw_loc_descr_ref
11804 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11805 machine_mode mem_mode)
11807 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11808 dw_loc_descr_ref op0, op1;
11809 int shift;
11811 if (op_mode == VOIDmode)
11812 op_mode = GET_MODE (XEXP (rtl, 1));
11813 if (op_mode == VOIDmode)
11814 return NULL;
11816 if (dwarf_strict
11817 && (GET_MODE_CLASS (op_mode) != MODE_INT
11818 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
11819 return NULL;
11821 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11822 VAR_INIT_STATUS_INITIALIZED);
11823 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11824 VAR_INIT_STATUS_INITIALIZED);
11826 if (op0 == NULL || op1 == NULL)
11827 return NULL;
11829 if (GET_MODE_CLASS (op_mode) != MODE_INT
11830 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11831 return compare_loc_descriptor (op, op0, op1);
11833 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11835 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
11836 dw_loc_descr_ref cvt;
11838 if (type_die == NULL)
11839 return NULL;
11840 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11841 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11842 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11843 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11844 add_loc_descr (&op0, cvt);
11845 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11846 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11847 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11848 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11849 add_loc_descr (&op1, cvt);
11850 return compare_loc_descriptor (op, op0, op1);
11853 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
11854 /* For eq/ne, if the operands are known to be zero-extended,
11855 there is no need to do the fancy shifting up. */
11856 if (op == DW_OP_eq || op == DW_OP_ne)
11858 dw_loc_descr_ref last0, last1;
11859 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11861 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11863 /* deref_size zero extends, and for constants we can check
11864 whether they are zero extended or not. */
11865 if (((last0->dw_loc_opc == DW_OP_deref_size
11866 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11867 || (CONST_INT_P (XEXP (rtl, 0))
11868 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
11869 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
11870 && ((last1->dw_loc_opc == DW_OP_deref_size
11871 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11872 || (CONST_INT_P (XEXP (rtl, 1))
11873 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
11874 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
11875 return compare_loc_descriptor (op, op0, op1);
11877 /* EQ/NE comparison against constant in narrower type than
11878 DWARF2_ADDR_SIZE can be performed either as
11879 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
11880 DW_OP_{eq,ne}
11882 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
11883 DW_OP_{eq,ne}. Pick whatever is shorter. */
11884 if (CONST_INT_P (XEXP (rtl, 1))
11885 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
11886 && (size_of_int_loc_descriptor (shift) + 1
11887 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
11888 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
11889 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11890 & GET_MODE_MASK (op_mode))))
11892 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
11893 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11894 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11895 & GET_MODE_MASK (op_mode));
11896 return compare_loc_descriptor (op, op0, op1);
11899 add_loc_descr (&op0, int_loc_descriptor (shift));
11900 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11901 if (CONST_INT_P (XEXP (rtl, 1)))
11902 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
11903 else
11905 add_loc_descr (&op1, int_loc_descriptor (shift));
11906 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11908 return compare_loc_descriptor (op, op0, op1);
11911 /* Return location descriptor for unsigned comparison OP RTL. */
11913 static dw_loc_descr_ref
11914 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11915 machine_mode mem_mode)
11917 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11918 dw_loc_descr_ref op0, op1;
11920 if (op_mode == VOIDmode)
11921 op_mode = GET_MODE (XEXP (rtl, 1));
11922 if (op_mode == VOIDmode)
11923 return NULL;
11924 if (GET_MODE_CLASS (op_mode) != MODE_INT)
11925 return NULL;
11927 if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11928 return NULL;
11930 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11931 VAR_INIT_STATUS_INITIALIZED);
11932 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11933 VAR_INIT_STATUS_INITIALIZED);
11935 if (op0 == NULL || op1 == NULL)
11936 return NULL;
11938 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
11940 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
11941 dw_loc_descr_ref last0, last1;
11942 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11944 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11946 if (CONST_INT_P (XEXP (rtl, 0)))
11947 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
11948 /* deref_size zero extends, so no need to mask it again. */
11949 else if (last0->dw_loc_opc != DW_OP_deref_size
11950 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11952 add_loc_descr (&op0, int_loc_descriptor (mask));
11953 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11955 if (CONST_INT_P (XEXP (rtl, 1)))
11956 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
11957 /* deref_size zero extends, so no need to mask it again. */
11958 else if (last1->dw_loc_opc != DW_OP_deref_size
11959 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11961 add_loc_descr (&op1, int_loc_descriptor (mask));
11962 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11965 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11967 HOST_WIDE_INT bias = 1;
11968 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11969 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11970 if (CONST_INT_P (XEXP (rtl, 1)))
11971 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
11972 + INTVAL (XEXP (rtl, 1)));
11973 else
11974 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
11975 bias, 0));
11977 return compare_loc_descriptor (op, op0, op1);
11980 /* Return location descriptor for {U,S}{MIN,MAX}. */
11982 static dw_loc_descr_ref
11983 minmax_loc_descriptor (rtx rtl, machine_mode mode,
11984 machine_mode mem_mode)
11986 enum dwarf_location_atom op;
11987 dw_loc_descr_ref op0, op1, ret;
11988 dw_loc_descr_ref bra_node, drop_node;
11990 if (dwarf_strict
11991 && (GET_MODE_CLASS (mode) != MODE_INT
11992 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
11993 return NULL;
11995 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11996 VAR_INIT_STATUS_INITIALIZED);
11997 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11998 VAR_INIT_STATUS_INITIALIZED);
12000 if (op0 == NULL || op1 == NULL)
12001 return NULL;
12003 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
12004 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
12005 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
12006 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
12008 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12010 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
12011 add_loc_descr (&op0, int_loc_descriptor (mask));
12012 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
12013 add_loc_descr (&op1, int_loc_descriptor (mask));
12014 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
12016 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12018 HOST_WIDE_INT bias = 1;
12019 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
12020 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12021 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12024 else if (GET_MODE_CLASS (mode) == MODE_INT
12025 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12027 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
12028 add_loc_descr (&op0, int_loc_descriptor (shift));
12029 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
12030 add_loc_descr (&op1, int_loc_descriptor (shift));
12031 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
12033 else if (GET_MODE_CLASS (mode) == MODE_INT
12034 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12036 dw_die_ref type_die = base_type_for_mode (mode, 0);
12037 dw_loc_descr_ref cvt;
12038 if (type_die == NULL)
12039 return NULL;
12040 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12041 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12042 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12043 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12044 add_loc_descr (&op0, cvt);
12045 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12046 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12047 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12048 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12049 add_loc_descr (&op1, cvt);
12052 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
12053 op = DW_OP_lt;
12054 else
12055 op = DW_OP_gt;
12056 ret = op0;
12057 add_loc_descr (&ret, op1);
12058 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
12059 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12060 add_loc_descr (&ret, bra_node);
12061 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12062 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
12063 add_loc_descr (&ret, drop_node);
12064 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12065 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
12066 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
12067 && GET_MODE_CLASS (mode) == MODE_INT
12068 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12069 ret = convert_descriptor_to_mode (mode, ret);
12070 return ret;
12073 /* Helper function for mem_loc_descriptor. Perform OP binary op,
12074 but after converting arguments to type_die, afterwards
12075 convert back to unsigned. */
12077 static dw_loc_descr_ref
12078 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
12079 machine_mode mode, machine_mode mem_mode)
12081 dw_loc_descr_ref cvt, op0, op1;
12083 if (type_die == NULL)
12084 return NULL;
12085 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12086 VAR_INIT_STATUS_INITIALIZED);
12087 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12088 VAR_INIT_STATUS_INITIALIZED);
12089 if (op0 == NULL || op1 == NULL)
12090 return NULL;
12091 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12092 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12093 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12094 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12095 add_loc_descr (&op0, cvt);
12096 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12097 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12098 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12099 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12100 add_loc_descr (&op1, cvt);
12101 add_loc_descr (&op0, op1);
12102 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
12103 return convert_descriptor_to_mode (mode, op0);
12106 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
12107 const0 is DW_OP_lit0 or corresponding typed constant,
12108 const1 is DW_OP_lit1 or corresponding typed constant
12109 and constMSB is constant with just the MSB bit set
12110 for the mode):
12111 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
12112 L1: const0 DW_OP_swap
12113 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
12114 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12115 L3: DW_OP_drop
12116 L4: DW_OP_nop
12118 CTZ is similar:
12119 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
12120 L1: const0 DW_OP_swap
12121 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
12122 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12123 L3: DW_OP_drop
12124 L4: DW_OP_nop
12126 FFS is similar:
12127 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
12128 L1: const1 DW_OP_swap
12129 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
12130 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12131 L3: DW_OP_drop
12132 L4: DW_OP_nop */
12134 static dw_loc_descr_ref
12135 clz_loc_descriptor (rtx rtl, machine_mode mode,
12136 machine_mode mem_mode)
12138 dw_loc_descr_ref op0, ret, tmp;
12139 HOST_WIDE_INT valv;
12140 dw_loc_descr_ref l1jump, l1label;
12141 dw_loc_descr_ref l2jump, l2label;
12142 dw_loc_descr_ref l3jump, l3label;
12143 dw_loc_descr_ref l4jump, l4label;
12144 rtx msb;
12146 if (GET_MODE_CLASS (mode) != MODE_INT
12147 || GET_MODE (XEXP (rtl, 0)) != mode)
12148 return NULL;
12150 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12151 VAR_INIT_STATUS_INITIALIZED);
12152 if (op0 == NULL)
12153 return NULL;
12154 ret = op0;
12155 if (GET_CODE (rtl) == CLZ)
12157 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12158 valv = GET_MODE_BITSIZE (mode);
12160 else if (GET_CODE (rtl) == FFS)
12161 valv = 0;
12162 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12163 valv = GET_MODE_BITSIZE (mode);
12164 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12165 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
12166 add_loc_descr (&ret, l1jump);
12167 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12168 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
12169 VAR_INIT_STATUS_INITIALIZED);
12170 if (tmp == NULL)
12171 return NULL;
12172 add_loc_descr (&ret, tmp);
12173 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
12174 add_loc_descr (&ret, l4jump);
12175 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
12176 ? const1_rtx : const0_rtx,
12177 mode, mem_mode,
12178 VAR_INIT_STATUS_INITIALIZED);
12179 if (l1label == NULL)
12180 return NULL;
12181 add_loc_descr (&ret, l1label);
12182 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12183 l2label = new_loc_descr (DW_OP_dup, 0, 0);
12184 add_loc_descr (&ret, l2label);
12185 if (GET_CODE (rtl) != CLZ)
12186 msb = const1_rtx;
12187 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
12188 msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
12189 << (GET_MODE_BITSIZE (mode) - 1));
12190 else
12191 msb = immed_wide_int_const
12192 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
12193 GET_MODE_PRECISION (mode)), mode);
12194 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
12195 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12196 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
12197 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
12198 else
12199 tmp = mem_loc_descriptor (msb, mode, mem_mode,
12200 VAR_INIT_STATUS_INITIALIZED);
12201 if (tmp == NULL)
12202 return NULL;
12203 add_loc_descr (&ret, tmp);
12204 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12205 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
12206 add_loc_descr (&ret, l3jump);
12207 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12208 VAR_INIT_STATUS_INITIALIZED);
12209 if (tmp == NULL)
12210 return NULL;
12211 add_loc_descr (&ret, tmp);
12212 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
12213 ? DW_OP_shl : DW_OP_shr, 0, 0));
12214 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12215 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
12216 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12217 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
12218 add_loc_descr (&ret, l2jump);
12219 l3label = new_loc_descr (DW_OP_drop, 0, 0);
12220 add_loc_descr (&ret, l3label);
12221 l4label = new_loc_descr (DW_OP_nop, 0, 0);
12222 add_loc_descr (&ret, l4label);
12223 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12224 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12225 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12226 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12227 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12228 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
12229 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12230 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
12231 return ret;
12234 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
12235 const1 is DW_OP_lit1 or corresponding typed constant):
12236 const0 DW_OP_swap
12237 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
12238 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
12239 L2: DW_OP_drop
12241 PARITY is similar:
12242 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
12243 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
12244 L2: DW_OP_drop */
12246 static dw_loc_descr_ref
12247 popcount_loc_descriptor (rtx rtl, machine_mode mode,
12248 machine_mode mem_mode)
12250 dw_loc_descr_ref op0, ret, tmp;
12251 dw_loc_descr_ref l1jump, l1label;
12252 dw_loc_descr_ref l2jump, l2label;
12254 if (GET_MODE_CLASS (mode) != MODE_INT
12255 || GET_MODE (XEXP (rtl, 0)) != mode)
12256 return NULL;
12258 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12259 VAR_INIT_STATUS_INITIALIZED);
12260 if (op0 == NULL)
12261 return NULL;
12262 ret = op0;
12263 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12264 VAR_INIT_STATUS_INITIALIZED);
12265 if (tmp == NULL)
12266 return NULL;
12267 add_loc_descr (&ret, tmp);
12268 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12269 l1label = new_loc_descr (DW_OP_dup, 0, 0);
12270 add_loc_descr (&ret, l1label);
12271 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12272 add_loc_descr (&ret, l2jump);
12273 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12274 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12275 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12276 VAR_INIT_STATUS_INITIALIZED);
12277 if (tmp == NULL)
12278 return NULL;
12279 add_loc_descr (&ret, tmp);
12280 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12281 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
12282 ? DW_OP_plus : DW_OP_xor, 0, 0));
12283 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12284 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12285 VAR_INIT_STATUS_INITIALIZED);
12286 add_loc_descr (&ret, tmp);
12287 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12288 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12289 add_loc_descr (&ret, l1jump);
12290 l2label = new_loc_descr (DW_OP_drop, 0, 0);
12291 add_loc_descr (&ret, l2label);
12292 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12293 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12294 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12295 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12296 return ret;
12299 /* BSWAP (constS is initial shift count, either 56 or 24):
12300 constS const0
12301 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
12302 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
12303 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
12304 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
12305 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
12307 static dw_loc_descr_ref
12308 bswap_loc_descriptor (rtx rtl, machine_mode mode,
12309 machine_mode mem_mode)
12311 dw_loc_descr_ref op0, ret, tmp;
12312 dw_loc_descr_ref l1jump, l1label;
12313 dw_loc_descr_ref l2jump, l2label;
12315 if (GET_MODE_CLASS (mode) != MODE_INT
12316 || BITS_PER_UNIT != 8
12317 || (GET_MODE_BITSIZE (mode) != 32
12318 && GET_MODE_BITSIZE (mode) != 64))
12319 return NULL;
12321 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12322 VAR_INIT_STATUS_INITIALIZED);
12323 if (op0 == NULL)
12324 return NULL;
12326 ret = op0;
12327 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12328 mode, mem_mode,
12329 VAR_INIT_STATUS_INITIALIZED);
12330 if (tmp == NULL)
12331 return NULL;
12332 add_loc_descr (&ret, tmp);
12333 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12334 VAR_INIT_STATUS_INITIALIZED);
12335 if (tmp == NULL)
12336 return NULL;
12337 add_loc_descr (&ret, tmp);
12338 l1label = new_loc_descr (DW_OP_pick, 2, 0);
12339 add_loc_descr (&ret, l1label);
12340 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12341 mode, mem_mode,
12342 VAR_INIT_STATUS_INITIALIZED);
12343 add_loc_descr (&ret, tmp);
12344 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
12345 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12346 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12347 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
12348 VAR_INIT_STATUS_INITIALIZED);
12349 if (tmp == NULL)
12350 return NULL;
12351 add_loc_descr (&ret, tmp);
12352 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12353 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
12354 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12355 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12356 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12357 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12358 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12359 VAR_INIT_STATUS_INITIALIZED);
12360 add_loc_descr (&ret, tmp);
12361 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
12362 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12363 add_loc_descr (&ret, l2jump);
12364 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
12365 VAR_INIT_STATUS_INITIALIZED);
12366 add_loc_descr (&ret, tmp);
12367 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12368 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12369 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12370 add_loc_descr (&ret, l1jump);
12371 l2label = new_loc_descr (DW_OP_drop, 0, 0);
12372 add_loc_descr (&ret, l2label);
12373 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12374 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12375 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12376 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12377 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12378 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12379 return ret;
12382 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
12383 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12384 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
12385 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
12387 ROTATERT is similar:
12388 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
12389 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12390 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
12392 static dw_loc_descr_ref
12393 rotate_loc_descriptor (rtx rtl, machine_mode mode,
12394 machine_mode mem_mode)
12396 rtx rtlop1 = XEXP (rtl, 1);
12397 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
12398 int i;
12400 if (GET_MODE_CLASS (mode) != MODE_INT)
12401 return NULL;
12403 if (GET_MODE (rtlop1) != VOIDmode
12404 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
12405 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12406 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12407 VAR_INIT_STATUS_INITIALIZED);
12408 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12409 VAR_INIT_STATUS_INITIALIZED);
12410 if (op0 == NULL || op1 == NULL)
12411 return NULL;
12412 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12413 for (i = 0; i < 2; i++)
12415 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
12416 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
12417 mode, mem_mode,
12418 VAR_INIT_STATUS_INITIALIZED);
12419 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12420 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12421 ? DW_OP_const4u
12422 : HOST_BITS_PER_WIDE_INT == 64
12423 ? DW_OP_const8u : DW_OP_constu,
12424 GET_MODE_MASK (mode), 0);
12425 else
12426 mask[i] = NULL;
12427 if (mask[i] == NULL)
12428 return NULL;
12429 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
12431 ret = op0;
12432 add_loc_descr (&ret, op1);
12433 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12434 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12435 if (GET_CODE (rtl) == ROTATERT)
12437 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12438 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12439 GET_MODE_BITSIZE (mode), 0));
12441 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12442 if (mask[0] != NULL)
12443 add_loc_descr (&ret, mask[0]);
12444 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12445 if (mask[1] != NULL)
12447 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12448 add_loc_descr (&ret, mask[1]);
12449 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12451 if (GET_CODE (rtl) == ROTATE)
12453 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12454 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12455 GET_MODE_BITSIZE (mode), 0));
12457 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12458 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12459 return ret;
12462 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
12463 for DEBUG_PARAMETER_REF RTL. */
12465 static dw_loc_descr_ref
12466 parameter_ref_descriptor (rtx rtl)
12468 dw_loc_descr_ref ret;
12469 dw_die_ref ref;
12471 if (dwarf_strict)
12472 return NULL;
12473 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
12474 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
12475 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
12476 if (ref)
12478 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12479 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
12480 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
12482 else
12484 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
12485 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
12487 return ret;
12490 /* The following routine converts the RTL for a variable or parameter
12491 (resident in memory) into an equivalent Dwarf representation of a
12492 mechanism for getting the address of that same variable onto the top of a
12493 hypothetical "address evaluation" stack.
12495 When creating memory location descriptors, we are effectively transforming
12496 the RTL for a memory-resident object into its Dwarf postfix expression
12497 equivalent. This routine recursively descends an RTL tree, turning
12498 it into Dwarf postfix code as it goes.
12500 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
12502 MEM_MODE is the mode of the memory reference, needed to handle some
12503 autoincrement addressing modes.
12505 Return 0 if we can't represent the location. */
12507 dw_loc_descr_ref
12508 mem_loc_descriptor (rtx rtl, machine_mode mode,
12509 machine_mode mem_mode,
12510 enum var_init_status initialized)
12512 dw_loc_descr_ref mem_loc_result = NULL;
12513 enum dwarf_location_atom op;
12514 dw_loc_descr_ref op0, op1;
12515 rtx inner = NULL_RTX;
12517 if (mode == VOIDmode)
12518 mode = GET_MODE (rtl);
12520 /* Note that for a dynamically sized array, the location we will generate a
12521 description of here will be the lowest numbered location which is
12522 actually within the array. That's *not* necessarily the same as the
12523 zeroth element of the array. */
12525 rtl = targetm.delegitimize_address (rtl);
12527 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
12528 return NULL;
12530 switch (GET_CODE (rtl))
12532 case POST_INC:
12533 case POST_DEC:
12534 case POST_MODIFY:
12535 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
12537 case SUBREG:
12538 /* The case of a subreg may arise when we have a local (register)
12539 variable or a formal (register) parameter which doesn't quite fill
12540 up an entire register. For now, just assume that it is
12541 legitimate to make the Dwarf info refer to the whole register which
12542 contains the given subreg. */
12543 if (!subreg_lowpart_p (rtl))
12544 break;
12545 inner = SUBREG_REG (rtl);
12546 case TRUNCATE:
12547 if (inner == NULL_RTX)
12548 inner = XEXP (rtl, 0);
12549 if (GET_MODE_CLASS (mode) == MODE_INT
12550 && GET_MODE_CLASS (GET_MODE (inner)) == MODE_INT
12551 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12552 #ifdef POINTERS_EXTEND_UNSIGNED
12553 || (mode == Pmode && mem_mode != VOIDmode)
12554 #endif
12556 && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
12558 mem_loc_result = mem_loc_descriptor (inner,
12559 GET_MODE (inner),
12560 mem_mode, initialized);
12561 break;
12563 if (dwarf_strict)
12564 break;
12565 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
12566 break;
12567 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
12568 && (GET_MODE_CLASS (mode) != MODE_INT
12569 || GET_MODE_CLASS (GET_MODE (inner)) != MODE_INT))
12570 break;
12571 else
12573 dw_die_ref type_die;
12574 dw_loc_descr_ref cvt;
12576 mem_loc_result = mem_loc_descriptor (inner,
12577 GET_MODE (inner),
12578 mem_mode, initialized);
12579 if (mem_loc_result == NULL)
12580 break;
12581 type_die = base_type_for_mode (mode,
12582 GET_MODE_CLASS (mode) == MODE_INT);
12583 if (type_die == NULL)
12585 mem_loc_result = NULL;
12586 break;
12588 if (GET_MODE_SIZE (mode)
12589 != GET_MODE_SIZE (GET_MODE (inner)))
12590 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12591 else
12592 cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
12593 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12594 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12595 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12596 add_loc_descr (&mem_loc_result, cvt);
12598 break;
12600 case REG:
12601 if (GET_MODE_CLASS (mode) != MODE_INT
12602 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12603 && rtl != arg_pointer_rtx
12604 && rtl != frame_pointer_rtx
12605 #ifdef POINTERS_EXTEND_UNSIGNED
12606 && (mode != Pmode || mem_mode == VOIDmode)
12607 #endif
12610 dw_die_ref type_die;
12611 unsigned int dbx_regnum;
12613 if (dwarf_strict)
12614 break;
12615 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
12616 break;
12617 type_die = base_type_for_mode (mode,
12618 GET_MODE_CLASS (mode) == MODE_INT);
12619 if (type_die == NULL)
12620 break;
12622 dbx_regnum = dbx_reg_number (rtl);
12623 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12624 break;
12625 mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
12626 dbx_regnum, 0);
12627 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12628 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12629 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
12630 break;
12632 /* Whenever a register number forms a part of the description of the
12633 method for calculating the (dynamic) address of a memory resident
12634 object, DWARF rules require the register number be referred to as
12635 a "base register". This distinction is not based in any way upon
12636 what category of register the hardware believes the given register
12637 belongs to. This is strictly DWARF terminology we're dealing with
12638 here. Note that in cases where the location of a memory-resident
12639 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
12640 OP_CONST (0)) the actual DWARF location descriptor that we generate
12641 may just be OP_BASEREG (basereg). This may look deceptively like
12642 the object in question was allocated to a register (rather than in
12643 memory) so DWARF consumers need to be aware of the subtle
12644 distinction between OP_REG and OP_BASEREG. */
12645 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
12646 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
12647 else if (stack_realign_drap
12648 && crtl->drap_reg
12649 && crtl->args.internal_arg_pointer == rtl
12650 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
12652 /* If RTL is internal_arg_pointer, which has been optimized
12653 out, use DRAP instead. */
12654 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
12655 VAR_INIT_STATUS_INITIALIZED);
12657 break;
12659 case SIGN_EXTEND:
12660 case ZERO_EXTEND:
12661 if (GET_MODE_CLASS (mode) != MODE_INT)
12662 break;
12663 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12664 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12665 if (op0 == 0)
12666 break;
12667 else if (GET_CODE (rtl) == ZERO_EXTEND
12668 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12669 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12670 < HOST_BITS_PER_WIDE_INT
12671 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
12672 to expand zero extend as two shifts instead of
12673 masking. */
12674 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
12676 machine_mode imode = GET_MODE (XEXP (rtl, 0));
12677 mem_loc_result = op0;
12678 add_loc_descr (&mem_loc_result,
12679 int_loc_descriptor (GET_MODE_MASK (imode)));
12680 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
12682 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12684 int shift = DWARF2_ADDR_SIZE
12685 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
12686 shift *= BITS_PER_UNIT;
12687 if (GET_CODE (rtl) == SIGN_EXTEND)
12688 op = DW_OP_shra;
12689 else
12690 op = DW_OP_shr;
12691 mem_loc_result = op0;
12692 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12693 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12694 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12695 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12697 else if (!dwarf_strict)
12699 dw_die_ref type_die1, type_die2;
12700 dw_loc_descr_ref cvt;
12702 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12703 GET_CODE (rtl) == ZERO_EXTEND);
12704 if (type_die1 == NULL)
12705 break;
12706 type_die2 = base_type_for_mode (mode, 1);
12707 if (type_die2 == NULL)
12708 break;
12709 mem_loc_result = op0;
12710 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12711 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12712 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
12713 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12714 add_loc_descr (&mem_loc_result, cvt);
12715 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12716 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12717 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
12718 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12719 add_loc_descr (&mem_loc_result, cvt);
12721 break;
12723 case MEM:
12725 rtx new_rtl = avoid_constant_pool_reference (rtl);
12726 if (new_rtl != rtl)
12728 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
12729 initialized);
12730 if (mem_loc_result != NULL)
12731 return mem_loc_result;
12734 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
12735 get_address_mode (rtl), mode,
12736 VAR_INIT_STATUS_INITIALIZED);
12737 if (mem_loc_result == NULL)
12738 mem_loc_result = tls_mem_loc_descriptor (rtl);
12739 if (mem_loc_result != NULL)
12741 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12742 || GET_MODE_CLASS (mode) != MODE_INT)
12744 dw_die_ref type_die;
12745 dw_loc_descr_ref deref;
12747 if (dwarf_strict)
12748 return NULL;
12749 type_die
12750 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
12751 if (type_die == NULL)
12752 return NULL;
12753 deref = new_loc_descr (DW_OP_GNU_deref_type,
12754 GET_MODE_SIZE (mode), 0);
12755 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12756 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12757 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
12758 add_loc_descr (&mem_loc_result, deref);
12760 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12761 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
12762 else
12763 add_loc_descr (&mem_loc_result,
12764 new_loc_descr (DW_OP_deref_size,
12765 GET_MODE_SIZE (mode), 0));
12767 break;
12769 case LO_SUM:
12770 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
12772 case LABEL_REF:
12773 /* Some ports can transform a symbol ref into a label ref, because
12774 the symbol ref is too far away and has to be dumped into a constant
12775 pool. */
12776 case CONST:
12777 case SYMBOL_REF:
12778 if ((GET_MODE_CLASS (mode) != MODE_INT
12779 && GET_MODE_CLASS (mode) != MODE_PARTIAL_INT)
12780 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12781 #ifdef POINTERS_EXTEND_UNSIGNED
12782 && (mode != Pmode || mem_mode == VOIDmode)
12783 #endif
12785 break;
12786 if (GET_CODE (rtl) == SYMBOL_REF
12787 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12789 dw_loc_descr_ref temp;
12791 /* If this is not defined, we have no way to emit the data. */
12792 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
12793 break;
12795 temp = new_addr_loc_descr (rtl, dtprel_true);
12797 mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
12798 add_loc_descr (&mem_loc_result, temp);
12800 break;
12803 if (!const_ok_for_output (rtl))
12805 if (GET_CODE (rtl) == CONST)
12806 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12807 initialized);
12808 break;
12811 symref:
12812 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
12813 vec_safe_push (used_rtx_array, rtl);
12814 break;
12816 case CONCAT:
12817 case CONCATN:
12818 case VAR_LOCATION:
12819 case DEBUG_IMPLICIT_PTR:
12820 expansion_failed (NULL_TREE, rtl,
12821 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
12822 return 0;
12824 case ENTRY_VALUE:
12825 if (dwarf_strict)
12826 return NULL;
12827 if (REG_P (ENTRY_VALUE_EXP (rtl)))
12829 if (GET_MODE_CLASS (mode) != MODE_INT
12830 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12831 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12832 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12833 else
12835 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
12836 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12837 return NULL;
12838 op0 = one_reg_loc_descriptor (dbx_regnum,
12839 VAR_INIT_STATUS_INITIALIZED);
12842 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
12843 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
12845 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12846 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12847 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
12848 return NULL;
12850 else
12851 gcc_unreachable ();
12852 if (op0 == NULL)
12853 return NULL;
12854 mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
12855 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
12856 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
12857 break;
12859 case DEBUG_PARAMETER_REF:
12860 mem_loc_result = parameter_ref_descriptor (rtl);
12861 break;
12863 case PRE_MODIFY:
12864 /* Extract the PLUS expression nested inside and fall into
12865 PLUS code below. */
12866 rtl = XEXP (rtl, 1);
12867 goto plus;
12869 case PRE_INC:
12870 case PRE_DEC:
12871 /* Turn these into a PLUS expression and fall into the PLUS code
12872 below. */
12873 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
12874 gen_int_mode (GET_CODE (rtl) == PRE_INC
12875 ? GET_MODE_UNIT_SIZE (mem_mode)
12876 : -GET_MODE_UNIT_SIZE (mem_mode),
12877 mode));
12879 /* ... fall through ... */
12881 case PLUS:
12882 plus:
12883 if (is_based_loc (rtl)
12884 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12885 || XEXP (rtl, 0) == arg_pointer_rtx
12886 || XEXP (rtl, 0) == frame_pointer_rtx)
12887 && GET_MODE_CLASS (mode) == MODE_INT)
12888 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
12889 INTVAL (XEXP (rtl, 1)),
12890 VAR_INIT_STATUS_INITIALIZED);
12891 else
12893 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12894 VAR_INIT_STATUS_INITIALIZED);
12895 if (mem_loc_result == 0)
12896 break;
12898 if (CONST_INT_P (XEXP (rtl, 1))
12899 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12900 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
12901 else
12903 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12904 VAR_INIT_STATUS_INITIALIZED);
12905 if (op1 == 0)
12906 return NULL;
12907 add_loc_descr (&mem_loc_result, op1);
12908 add_loc_descr (&mem_loc_result,
12909 new_loc_descr (DW_OP_plus, 0, 0));
12912 break;
12914 /* If a pseudo-reg is optimized away, it is possible for it to
12915 be replaced with a MEM containing a multiply or shift. */
12916 case MINUS:
12917 op = DW_OP_minus;
12918 goto do_binop;
12920 case MULT:
12921 op = DW_OP_mul;
12922 goto do_binop;
12924 case DIV:
12925 if (!dwarf_strict
12926 && GET_MODE_CLASS (mode) == MODE_INT
12927 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12929 mem_loc_result = typed_binop (DW_OP_div, rtl,
12930 base_type_for_mode (mode, 0),
12931 mode, mem_mode);
12932 break;
12934 op = DW_OP_div;
12935 goto do_binop;
12937 case UMOD:
12938 op = DW_OP_mod;
12939 goto do_binop;
12941 case ASHIFT:
12942 op = DW_OP_shl;
12943 goto do_shift;
12945 case ASHIFTRT:
12946 op = DW_OP_shra;
12947 goto do_shift;
12949 case LSHIFTRT:
12950 op = DW_OP_shr;
12951 goto do_shift;
12953 do_shift:
12954 if (GET_MODE_CLASS (mode) != MODE_INT)
12955 break;
12956 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12957 VAR_INIT_STATUS_INITIALIZED);
12959 rtx rtlop1 = XEXP (rtl, 1);
12960 if (GET_MODE (rtlop1) != VOIDmode
12961 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
12962 < GET_MODE_BITSIZE (mode))
12963 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12964 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12965 VAR_INIT_STATUS_INITIALIZED);
12968 if (op0 == 0 || op1 == 0)
12969 break;
12971 mem_loc_result = op0;
12972 add_loc_descr (&mem_loc_result, op1);
12973 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12974 break;
12976 case AND:
12977 op = DW_OP_and;
12978 goto do_binop;
12980 case IOR:
12981 op = DW_OP_or;
12982 goto do_binop;
12984 case XOR:
12985 op = DW_OP_xor;
12986 goto do_binop;
12988 do_binop:
12989 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12990 VAR_INIT_STATUS_INITIALIZED);
12991 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12992 VAR_INIT_STATUS_INITIALIZED);
12994 if (op0 == 0 || op1 == 0)
12995 break;
12997 mem_loc_result = op0;
12998 add_loc_descr (&mem_loc_result, op1);
12999 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13000 break;
13002 case MOD:
13003 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
13005 mem_loc_result = typed_binop (DW_OP_mod, rtl,
13006 base_type_for_mode (mode, 0),
13007 mode, mem_mode);
13008 break;
13011 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13012 VAR_INIT_STATUS_INITIALIZED);
13013 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13014 VAR_INIT_STATUS_INITIALIZED);
13016 if (op0 == 0 || op1 == 0)
13017 break;
13019 mem_loc_result = op0;
13020 add_loc_descr (&mem_loc_result, op1);
13021 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13022 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13023 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
13024 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
13025 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
13026 break;
13028 case UDIV:
13029 if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
13031 if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
13033 op = DW_OP_div;
13034 goto do_binop;
13036 mem_loc_result = typed_binop (DW_OP_div, rtl,
13037 base_type_for_mode (mode, 1),
13038 mode, mem_mode);
13040 break;
13042 case NOT:
13043 op = DW_OP_not;
13044 goto do_unop;
13046 case ABS:
13047 op = DW_OP_abs;
13048 goto do_unop;
13050 case NEG:
13051 op = DW_OP_neg;
13052 goto do_unop;
13054 do_unop:
13055 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13056 VAR_INIT_STATUS_INITIALIZED);
13058 if (op0 == 0)
13059 break;
13061 mem_loc_result = op0;
13062 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13063 break;
13065 case CONST_INT:
13066 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13067 #ifdef POINTERS_EXTEND_UNSIGNED
13068 || (mode == Pmode
13069 && mem_mode != VOIDmode
13070 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
13071 #endif
13074 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13075 break;
13077 if (!dwarf_strict
13078 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
13079 || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
13081 dw_die_ref type_die = base_type_for_mode (mode, 1);
13082 machine_mode amode;
13083 if (type_die == NULL)
13084 return NULL;
13085 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
13086 MODE_INT, 0);
13087 if (INTVAL (rtl) >= 0
13088 && amode != BLKmode
13089 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
13090 /* const DW_OP_GNU_convert <XXX> vs.
13091 DW_OP_GNU_const_type <XXX, 1, const>. */
13092 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
13093 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
13095 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13096 op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13097 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13098 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13099 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
13100 add_loc_descr (&mem_loc_result, op0);
13101 return mem_loc_result;
13103 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
13104 INTVAL (rtl));
13105 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13106 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13107 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13108 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
13109 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13110 else
13112 mem_loc_result->dw_loc_oprnd2.val_class
13113 = dw_val_class_const_double;
13114 mem_loc_result->dw_loc_oprnd2.v.val_double
13115 = double_int::from_shwi (INTVAL (rtl));
13118 break;
13120 case CONST_DOUBLE:
13121 if (!dwarf_strict)
13123 dw_die_ref type_die;
13125 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
13126 CONST_DOUBLE rtx could represent either a large integer
13127 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
13128 the value is always a floating point constant.
13130 When it is an integer, a CONST_DOUBLE is used whenever
13131 the constant requires 2 HWIs to be adequately represented.
13132 We output CONST_DOUBLEs as blocks. */
13133 if (mode == VOIDmode
13134 || (GET_MODE (rtl) == VOIDmode
13135 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
13136 break;
13137 type_die = base_type_for_mode (mode,
13138 GET_MODE_CLASS (mode) == MODE_INT);
13139 if (type_die == NULL)
13140 return NULL;
13141 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
13142 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13143 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13144 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13145 #if TARGET_SUPPORTS_WIDE_INT == 0
13146 if (!SCALAR_FLOAT_MODE_P (mode))
13148 mem_loc_result->dw_loc_oprnd2.val_class
13149 = dw_val_class_const_double;
13150 mem_loc_result->dw_loc_oprnd2.v.val_double
13151 = rtx_to_double_int (rtl);
13153 else
13154 #endif
13156 unsigned int length = GET_MODE_SIZE (mode);
13157 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
13159 insert_float (rtl, array);
13160 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13161 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13162 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13163 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13166 break;
13168 case CONST_WIDE_INT:
13169 if (!dwarf_strict)
13171 dw_die_ref type_die;
13173 type_die = base_type_for_mode (mode,
13174 GET_MODE_CLASS (mode) == MODE_INT);
13175 if (type_die == NULL)
13176 return NULL;
13177 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
13178 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13179 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13180 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13181 mem_loc_result->dw_loc_oprnd2.val_class
13182 = dw_val_class_wide_int;
13183 mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
13184 *mem_loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
13186 break;
13188 case EQ:
13189 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
13190 break;
13192 case GE:
13193 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
13194 break;
13196 case GT:
13197 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
13198 break;
13200 case LE:
13201 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
13202 break;
13204 case LT:
13205 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
13206 break;
13208 case NE:
13209 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
13210 break;
13212 case GEU:
13213 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
13214 break;
13216 case GTU:
13217 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
13218 break;
13220 case LEU:
13221 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
13222 break;
13224 case LTU:
13225 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
13226 break;
13228 case UMIN:
13229 case UMAX:
13230 if (GET_MODE_CLASS (mode) != MODE_INT)
13231 break;
13232 /* FALLTHRU */
13233 case SMIN:
13234 case SMAX:
13235 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
13236 break;
13238 case ZERO_EXTRACT:
13239 case SIGN_EXTRACT:
13240 if (CONST_INT_P (XEXP (rtl, 1))
13241 && CONST_INT_P (XEXP (rtl, 2))
13242 && ((unsigned) INTVAL (XEXP (rtl, 1))
13243 + (unsigned) INTVAL (XEXP (rtl, 2))
13244 <= GET_MODE_BITSIZE (mode))
13245 && GET_MODE_CLASS (mode) == MODE_INT
13246 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13247 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
13249 int shift, size;
13250 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13251 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13252 if (op0 == 0)
13253 break;
13254 if (GET_CODE (rtl) == SIGN_EXTRACT)
13255 op = DW_OP_shra;
13256 else
13257 op = DW_OP_shr;
13258 mem_loc_result = op0;
13259 size = INTVAL (XEXP (rtl, 1));
13260 shift = INTVAL (XEXP (rtl, 2));
13261 if (BITS_BIG_ENDIAN)
13262 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
13263 - shift - size;
13264 if (shift + size != (int) DWARF2_ADDR_SIZE)
13266 add_loc_descr (&mem_loc_result,
13267 int_loc_descriptor (DWARF2_ADDR_SIZE
13268 - shift - size));
13269 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13271 if (size != (int) DWARF2_ADDR_SIZE)
13273 add_loc_descr (&mem_loc_result,
13274 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
13275 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13278 break;
13280 case IF_THEN_ELSE:
13282 dw_loc_descr_ref op2, bra_node, drop_node;
13283 op0 = mem_loc_descriptor (XEXP (rtl, 0),
13284 GET_MODE (XEXP (rtl, 0)) == VOIDmode
13285 ? word_mode : GET_MODE (XEXP (rtl, 0)),
13286 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13287 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13288 VAR_INIT_STATUS_INITIALIZED);
13289 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
13290 VAR_INIT_STATUS_INITIALIZED);
13291 if (op0 == NULL || op1 == NULL || op2 == NULL)
13292 break;
13294 mem_loc_result = op1;
13295 add_loc_descr (&mem_loc_result, op2);
13296 add_loc_descr (&mem_loc_result, op0);
13297 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13298 add_loc_descr (&mem_loc_result, bra_node);
13299 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
13300 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
13301 add_loc_descr (&mem_loc_result, drop_node);
13302 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13303 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
13305 break;
13307 case FLOAT_EXTEND:
13308 case FLOAT_TRUNCATE:
13309 case FLOAT:
13310 case UNSIGNED_FLOAT:
13311 case FIX:
13312 case UNSIGNED_FIX:
13313 if (!dwarf_strict)
13315 dw_die_ref type_die;
13316 dw_loc_descr_ref cvt;
13318 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13319 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13320 if (op0 == NULL)
13321 break;
13322 if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
13323 && (GET_CODE (rtl) == FLOAT
13324 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
13325 <= DWARF2_ADDR_SIZE))
13327 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
13328 GET_CODE (rtl) == UNSIGNED_FLOAT);
13329 if (type_die == NULL)
13330 break;
13331 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13332 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13333 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13334 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13335 add_loc_descr (&op0, cvt);
13337 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
13338 if (type_die == NULL)
13339 break;
13340 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13341 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13342 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13343 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13344 add_loc_descr (&op0, cvt);
13345 if (GET_MODE_CLASS (mode) == MODE_INT
13346 && (GET_CODE (rtl) == FIX
13347 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
13349 op0 = convert_descriptor_to_mode (mode, op0);
13350 if (op0 == NULL)
13351 break;
13353 mem_loc_result = op0;
13355 break;
13357 case CLZ:
13358 case CTZ:
13359 case FFS:
13360 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
13361 break;
13363 case POPCOUNT:
13364 case PARITY:
13365 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
13366 break;
13368 case BSWAP:
13369 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
13370 break;
13372 case ROTATE:
13373 case ROTATERT:
13374 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
13375 break;
13377 case COMPARE:
13378 /* In theory, we could implement the above. */
13379 /* DWARF cannot represent the unsigned compare operations
13380 natively. */
13381 case SS_MULT:
13382 case US_MULT:
13383 case SS_DIV:
13384 case US_DIV:
13385 case SS_PLUS:
13386 case US_PLUS:
13387 case SS_MINUS:
13388 case US_MINUS:
13389 case SS_NEG:
13390 case US_NEG:
13391 case SS_ABS:
13392 case SS_ASHIFT:
13393 case US_ASHIFT:
13394 case SS_TRUNCATE:
13395 case US_TRUNCATE:
13396 case UNORDERED:
13397 case ORDERED:
13398 case UNEQ:
13399 case UNGE:
13400 case UNGT:
13401 case UNLE:
13402 case UNLT:
13403 case LTGT:
13404 case FRACT_CONVERT:
13405 case UNSIGNED_FRACT_CONVERT:
13406 case SAT_FRACT:
13407 case UNSIGNED_SAT_FRACT:
13408 case SQRT:
13409 case ASM_OPERANDS:
13410 case VEC_MERGE:
13411 case VEC_SELECT:
13412 case VEC_CONCAT:
13413 case VEC_DUPLICATE:
13414 case UNSPEC:
13415 case HIGH:
13416 case FMA:
13417 case STRICT_LOW_PART:
13418 case CONST_VECTOR:
13419 case CONST_FIXED:
13420 case CLRSB:
13421 case CLOBBER:
13422 /* If delegitimize_address couldn't do anything with the UNSPEC, we
13423 can't express it in the debug info. This can happen e.g. with some
13424 TLS UNSPECs. */
13425 break;
13427 case CONST_STRING:
13428 resolve_one_addr (&rtl);
13429 goto symref;
13431 default:
13432 #ifdef ENABLE_CHECKING
13433 print_rtl (stderr, rtl);
13434 gcc_unreachable ();
13435 #else
13436 break;
13437 #endif
13440 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13441 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13443 return mem_loc_result;
13446 /* Return a descriptor that describes the concatenation of two locations.
13447 This is typically a complex variable. */
13449 static dw_loc_descr_ref
13450 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
13452 dw_loc_descr_ref cc_loc_result = NULL;
13453 dw_loc_descr_ref x0_ref
13454 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13455 dw_loc_descr_ref x1_ref
13456 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13458 if (x0_ref == 0 || x1_ref == 0)
13459 return 0;
13461 cc_loc_result = x0_ref;
13462 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
13464 add_loc_descr (&cc_loc_result, x1_ref);
13465 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
13467 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13468 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13470 return cc_loc_result;
13473 /* Return a descriptor that describes the concatenation of N
13474 locations. */
13476 static dw_loc_descr_ref
13477 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
13479 unsigned int i;
13480 dw_loc_descr_ref cc_loc_result = NULL;
13481 unsigned int n = XVECLEN (concatn, 0);
13483 for (i = 0; i < n; ++i)
13485 dw_loc_descr_ref ref;
13486 rtx x = XVECEXP (concatn, 0, i);
13488 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13489 if (ref == NULL)
13490 return NULL;
13492 add_loc_descr (&cc_loc_result, ref);
13493 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
13496 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13497 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13499 return cc_loc_result;
13502 /* Helper function for loc_descriptor. Return DW_OP_GNU_implicit_pointer
13503 for DEBUG_IMPLICIT_PTR RTL. */
13505 static dw_loc_descr_ref
13506 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
13508 dw_loc_descr_ref ret;
13509 dw_die_ref ref;
13511 if (dwarf_strict)
13512 return NULL;
13513 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
13514 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
13515 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
13516 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
13517 ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
13518 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
13519 if (ref)
13521 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13522 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
13523 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
13525 else
13527 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
13528 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
13530 return ret;
13533 /* Output a proper Dwarf location descriptor for a variable or parameter
13534 which is either allocated in a register or in a memory location. For a
13535 register, we just generate an OP_REG and the register number. For a
13536 memory location we provide a Dwarf postfix expression describing how to
13537 generate the (dynamic) address of the object onto the address stack.
13539 MODE is mode of the decl if this loc_descriptor is going to be used in
13540 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
13541 allowed, VOIDmode otherwise.
13543 If we don't know how to describe it, return 0. */
13545 static dw_loc_descr_ref
13546 loc_descriptor (rtx rtl, machine_mode mode,
13547 enum var_init_status initialized)
13549 dw_loc_descr_ref loc_result = NULL;
13551 switch (GET_CODE (rtl))
13553 case SUBREG:
13554 /* The case of a subreg may arise when we have a local (register)
13555 variable or a formal (register) parameter which doesn't quite fill
13556 up an entire register. For now, just assume that it is
13557 legitimate to make the Dwarf info refer to the whole register which
13558 contains the given subreg. */
13559 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
13560 loc_result = loc_descriptor (SUBREG_REG (rtl),
13561 GET_MODE (SUBREG_REG (rtl)), initialized);
13562 else
13563 goto do_default;
13564 break;
13566 case REG:
13567 loc_result = reg_loc_descriptor (rtl, initialized);
13568 break;
13570 case MEM:
13571 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13572 GET_MODE (rtl), initialized);
13573 if (loc_result == NULL)
13574 loc_result = tls_mem_loc_descriptor (rtl);
13575 if (loc_result == NULL)
13577 rtx new_rtl = avoid_constant_pool_reference (rtl);
13578 if (new_rtl != rtl)
13579 loc_result = loc_descriptor (new_rtl, mode, initialized);
13581 break;
13583 case CONCAT:
13584 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
13585 initialized);
13586 break;
13588 case CONCATN:
13589 loc_result = concatn_loc_descriptor (rtl, initialized);
13590 break;
13592 case VAR_LOCATION:
13593 /* Single part. */
13594 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
13596 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
13597 if (GET_CODE (loc) == EXPR_LIST)
13598 loc = XEXP (loc, 0);
13599 loc_result = loc_descriptor (loc, mode, initialized);
13600 break;
13603 rtl = XEXP (rtl, 1);
13604 /* FALLTHRU */
13606 case PARALLEL:
13608 rtvec par_elems = XVEC (rtl, 0);
13609 int num_elem = GET_NUM_ELEM (par_elems);
13610 machine_mode mode;
13611 int i;
13613 /* Create the first one, so we have something to add to. */
13614 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
13615 VOIDmode, initialized);
13616 if (loc_result == NULL)
13617 return NULL;
13618 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
13619 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13620 for (i = 1; i < num_elem; i++)
13622 dw_loc_descr_ref temp;
13624 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
13625 VOIDmode, initialized);
13626 if (temp == NULL)
13627 return NULL;
13628 add_loc_descr (&loc_result, temp);
13629 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
13630 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13633 break;
13635 case CONST_INT:
13636 if (mode != VOIDmode && mode != BLKmode)
13637 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
13638 INTVAL (rtl));
13639 break;
13641 case CONST_DOUBLE:
13642 if (mode == VOIDmode)
13643 mode = GET_MODE (rtl);
13645 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13647 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13649 /* Note that a CONST_DOUBLE rtx could represent either an integer
13650 or a floating-point constant. A CONST_DOUBLE is used whenever
13651 the constant requires more than one word in order to be
13652 adequately represented. We output CONST_DOUBLEs as blocks. */
13653 loc_result = new_loc_descr (DW_OP_implicit_value,
13654 GET_MODE_SIZE (mode), 0);
13655 #if TARGET_SUPPORTS_WIDE_INT == 0
13656 if (!SCALAR_FLOAT_MODE_P (mode))
13658 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
13659 loc_result->dw_loc_oprnd2.v.val_double
13660 = rtx_to_double_int (rtl);
13662 else
13663 #endif
13665 unsigned int length = GET_MODE_SIZE (mode);
13666 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
13668 insert_float (rtl, array);
13669 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13670 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13671 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13672 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13675 break;
13677 case CONST_WIDE_INT:
13678 if (mode == VOIDmode)
13679 mode = GET_MODE (rtl);
13681 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13683 loc_result = new_loc_descr (DW_OP_implicit_value,
13684 GET_MODE_SIZE (mode), 0);
13685 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
13686 loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
13687 *loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
13689 break;
13691 case CONST_VECTOR:
13692 if (mode == VOIDmode)
13693 mode = GET_MODE (rtl);
13695 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13697 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
13698 unsigned int length = CONST_VECTOR_NUNITS (rtl);
13699 unsigned char *array
13700 = ggc_vec_alloc<unsigned char> (length * elt_size);
13701 unsigned int i;
13702 unsigned char *p;
13703 machine_mode imode = GET_MODE_INNER (mode);
13705 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13706 switch (GET_MODE_CLASS (mode))
13708 case MODE_VECTOR_INT:
13709 for (i = 0, p = array; i < length; i++, p += elt_size)
13711 rtx elt = CONST_VECTOR_ELT (rtl, i);
13712 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
13714 break;
13716 case MODE_VECTOR_FLOAT:
13717 for (i = 0, p = array; i < length; i++, p += elt_size)
13719 rtx elt = CONST_VECTOR_ELT (rtl, i);
13720 insert_float (elt, p);
13722 break;
13724 default:
13725 gcc_unreachable ();
13728 loc_result = new_loc_descr (DW_OP_implicit_value,
13729 length * elt_size, 0);
13730 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13731 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
13732 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
13733 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13735 break;
13737 case CONST:
13738 if (mode == VOIDmode
13739 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
13740 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
13741 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
13743 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
13744 break;
13746 /* FALLTHROUGH */
13747 case SYMBOL_REF:
13748 if (!const_ok_for_output (rtl))
13749 break;
13750 case LABEL_REF:
13751 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
13752 && (dwarf_version >= 4 || !dwarf_strict))
13754 loc_result = new_addr_loc_descr (rtl, dtprel_false);
13755 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
13756 vec_safe_push (used_rtx_array, rtl);
13758 break;
13760 case DEBUG_IMPLICIT_PTR:
13761 loc_result = implicit_ptr_descriptor (rtl, 0);
13762 break;
13764 case PLUS:
13765 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
13766 && CONST_INT_P (XEXP (rtl, 1)))
13768 loc_result
13769 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
13770 break;
13772 /* FALLTHRU */
13773 do_default:
13774 default:
13775 if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
13776 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
13777 && dwarf_version >= 4)
13778 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
13780 /* Value expression. */
13781 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
13782 if (loc_result)
13783 add_loc_descr (&loc_result,
13784 new_loc_descr (DW_OP_stack_value, 0, 0));
13786 break;
13789 return loc_result;
13792 /* We need to figure out what section we should use as the base for the
13793 address ranges where a given location is valid.
13794 1. If this particular DECL has a section associated with it, use that.
13795 2. If this function has a section associated with it, use that.
13796 3. Otherwise, use the text section.
13797 XXX: If you split a variable across multiple sections, we won't notice. */
13799 static const char *
13800 secname_for_decl (const_tree decl)
13802 const char *secname;
13804 if (VAR_OR_FUNCTION_DECL_P (decl)
13805 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
13806 && DECL_SECTION_NAME (decl))
13807 secname = DECL_SECTION_NAME (decl);
13808 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
13809 secname = DECL_SECTION_NAME (current_function_decl);
13810 else if (cfun && in_cold_section_p)
13811 secname = crtl->subsections.cold_section_label;
13812 else
13813 secname = text_section_label;
13815 return secname;
13818 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
13820 static bool
13821 decl_by_reference_p (tree decl)
13823 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
13824 || TREE_CODE (decl) == VAR_DECL)
13825 && DECL_BY_REFERENCE (decl));
13828 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13829 for VARLOC. */
13831 static dw_loc_descr_ref
13832 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
13833 enum var_init_status initialized)
13835 int have_address = 0;
13836 dw_loc_descr_ref descr;
13837 machine_mode mode;
13839 if (want_address != 2)
13841 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
13842 /* Single part. */
13843 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13845 varloc = PAT_VAR_LOCATION_LOC (varloc);
13846 if (GET_CODE (varloc) == EXPR_LIST)
13847 varloc = XEXP (varloc, 0);
13848 mode = GET_MODE (varloc);
13849 if (MEM_P (varloc))
13851 rtx addr = XEXP (varloc, 0);
13852 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
13853 mode, initialized);
13854 if (descr)
13855 have_address = 1;
13856 else
13858 rtx x = avoid_constant_pool_reference (varloc);
13859 if (x != varloc)
13860 descr = mem_loc_descriptor (x, mode, VOIDmode,
13861 initialized);
13864 else
13865 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
13867 else
13868 return 0;
13870 else
13872 if (GET_CODE (varloc) == VAR_LOCATION)
13873 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
13874 else
13875 mode = DECL_MODE (loc);
13876 descr = loc_descriptor (varloc, mode, initialized);
13877 have_address = 1;
13880 if (!descr)
13881 return 0;
13883 if (want_address == 2 && !have_address
13884 && (dwarf_version >= 4 || !dwarf_strict))
13886 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
13888 expansion_failed (loc, NULL_RTX,
13889 "DWARF address size mismatch");
13890 return 0;
13892 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
13893 have_address = 1;
13895 /* Show if we can't fill the request for an address. */
13896 if (want_address && !have_address)
13898 expansion_failed (loc, NULL_RTX,
13899 "Want address and only have value");
13900 return 0;
13903 /* If we've got an address and don't want one, dereference. */
13904 if (!want_address && have_address)
13906 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
13907 enum dwarf_location_atom op;
13909 if (size > DWARF2_ADDR_SIZE || size == -1)
13911 expansion_failed (loc, NULL_RTX,
13912 "DWARF address size mismatch");
13913 return 0;
13915 else if (size == DWARF2_ADDR_SIZE)
13916 op = DW_OP_deref;
13917 else
13918 op = DW_OP_deref_size;
13920 add_loc_descr (&descr, new_loc_descr (op, size, 0));
13923 return descr;
13926 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
13927 if it is not possible. */
13929 static dw_loc_descr_ref
13930 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
13932 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
13933 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
13934 else if (dwarf_version >= 3 || !dwarf_strict)
13935 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
13936 else
13937 return NULL;
13940 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13941 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
13943 static dw_loc_descr_ref
13944 dw_sra_loc_expr (tree decl, rtx loc)
13946 rtx p;
13947 unsigned HOST_WIDE_INT padsize = 0;
13948 dw_loc_descr_ref descr, *descr_tail;
13949 unsigned HOST_WIDE_INT decl_size;
13950 rtx varloc;
13951 enum var_init_status initialized;
13953 if (DECL_SIZE (decl) == NULL
13954 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
13955 return NULL;
13957 decl_size = tree_to_uhwi (DECL_SIZE (decl));
13958 descr = NULL;
13959 descr_tail = &descr;
13961 for (p = loc; p; p = XEXP (p, 1))
13963 unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
13964 rtx loc_note = *decl_piece_varloc_ptr (p);
13965 dw_loc_descr_ref cur_descr;
13966 dw_loc_descr_ref *tail, last = NULL;
13967 unsigned HOST_WIDE_INT opsize = 0;
13969 if (loc_note == NULL_RTX
13970 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
13972 padsize += bitsize;
13973 continue;
13975 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
13976 varloc = NOTE_VAR_LOCATION (loc_note);
13977 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
13978 if (cur_descr == NULL)
13980 padsize += bitsize;
13981 continue;
13984 /* Check that cur_descr either doesn't use
13985 DW_OP_*piece operations, or their sum is equal
13986 to bitsize. Otherwise we can't embed it. */
13987 for (tail = &cur_descr; *tail != NULL;
13988 tail = &(*tail)->dw_loc_next)
13989 if ((*tail)->dw_loc_opc == DW_OP_piece)
13991 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
13992 * BITS_PER_UNIT;
13993 last = *tail;
13995 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
13997 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
13998 last = *tail;
14001 if (last != NULL && opsize != bitsize)
14003 padsize += bitsize;
14004 /* Discard the current piece of the descriptor and release any
14005 addr_table entries it uses. */
14006 remove_loc_list_addr_table_entries (cur_descr);
14007 continue;
14010 /* If there is a hole, add DW_OP_*piece after empty DWARF
14011 expression, which means that those bits are optimized out. */
14012 if (padsize)
14014 if (padsize > decl_size)
14016 remove_loc_list_addr_table_entries (cur_descr);
14017 goto discard_descr;
14019 decl_size -= padsize;
14020 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
14021 if (*descr_tail == NULL)
14023 remove_loc_list_addr_table_entries (cur_descr);
14024 goto discard_descr;
14026 descr_tail = &(*descr_tail)->dw_loc_next;
14027 padsize = 0;
14029 *descr_tail = cur_descr;
14030 descr_tail = tail;
14031 if (bitsize > decl_size)
14032 goto discard_descr;
14033 decl_size -= bitsize;
14034 if (last == NULL)
14036 HOST_WIDE_INT offset = 0;
14037 if (GET_CODE (varloc) == VAR_LOCATION
14038 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14040 varloc = PAT_VAR_LOCATION_LOC (varloc);
14041 if (GET_CODE (varloc) == EXPR_LIST)
14042 varloc = XEXP (varloc, 0);
14046 if (GET_CODE (varloc) == CONST
14047 || GET_CODE (varloc) == SIGN_EXTEND
14048 || GET_CODE (varloc) == ZERO_EXTEND)
14049 varloc = XEXP (varloc, 0);
14050 else if (GET_CODE (varloc) == SUBREG)
14051 varloc = SUBREG_REG (varloc);
14052 else
14053 break;
14055 while (1);
14056 /* DW_OP_bit_size offset should be zero for register
14057 or implicit location descriptions and empty location
14058 descriptions, but for memory addresses needs big endian
14059 adjustment. */
14060 if (MEM_P (varloc))
14062 unsigned HOST_WIDE_INT memsize
14063 = MEM_SIZE (varloc) * BITS_PER_UNIT;
14064 if (memsize != bitsize)
14066 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
14067 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
14068 goto discard_descr;
14069 if (memsize < bitsize)
14070 goto discard_descr;
14071 if (BITS_BIG_ENDIAN)
14072 offset = memsize - bitsize;
14076 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
14077 if (*descr_tail == NULL)
14078 goto discard_descr;
14079 descr_tail = &(*descr_tail)->dw_loc_next;
14083 /* If there were any non-empty expressions, add padding till the end of
14084 the decl. */
14085 if (descr != NULL && decl_size != 0)
14087 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
14088 if (*descr_tail == NULL)
14089 goto discard_descr;
14091 return descr;
14093 discard_descr:
14094 /* Discard the descriptor and release any addr_table entries it uses. */
14095 remove_loc_list_addr_table_entries (descr);
14096 return NULL;
14099 /* Return the dwarf representation of the location list LOC_LIST of
14100 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
14101 function. */
14103 static dw_loc_list_ref
14104 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
14106 const char *endname, *secname;
14107 rtx varloc;
14108 enum var_init_status initialized;
14109 struct var_loc_node *node;
14110 dw_loc_descr_ref descr;
14111 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
14112 dw_loc_list_ref list = NULL;
14113 dw_loc_list_ref *listp = &list;
14115 /* Now that we know what section we are using for a base,
14116 actually construct the list of locations.
14117 The first location information is what is passed to the
14118 function that creates the location list, and the remaining
14119 locations just get added on to that list.
14120 Note that we only know the start address for a location
14121 (IE location changes), so to build the range, we use
14122 the range [current location start, next location start].
14123 This means we have to special case the last node, and generate
14124 a range of [last location start, end of function label]. */
14126 secname = secname_for_decl (decl);
14128 for (node = loc_list->first; node; node = node->next)
14129 if (GET_CODE (node->loc) == EXPR_LIST
14130 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
14132 if (GET_CODE (node->loc) == EXPR_LIST)
14134 /* This requires DW_OP_{,bit_}piece, which is not usable
14135 inside DWARF expressions. */
14136 if (want_address != 2)
14137 continue;
14138 descr = dw_sra_loc_expr (decl, node->loc);
14139 if (descr == NULL)
14140 continue;
14142 else
14144 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14145 varloc = NOTE_VAR_LOCATION (node->loc);
14146 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
14148 if (descr)
14150 bool range_across_switch = false;
14151 /* If section switch happens in between node->label
14152 and node->next->label (or end of function) and
14153 we can't emit it as a single entry list,
14154 emit two ranges, first one ending at the end
14155 of first partition and second one starting at the
14156 beginning of second partition. */
14157 if (node == loc_list->last_before_switch
14158 && (node != loc_list->first || loc_list->first->next)
14159 && current_function_decl)
14161 endname = cfun->fde->dw_fde_end;
14162 range_across_switch = true;
14164 /* The variable has a location between NODE->LABEL and
14165 NODE->NEXT->LABEL. */
14166 else if (node->next)
14167 endname = node->next->label;
14168 /* If the variable has a location at the last label
14169 it keeps its location until the end of function. */
14170 else if (!current_function_decl)
14171 endname = text_end_label;
14172 else
14174 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
14175 current_function_funcdef_no);
14176 endname = ggc_strdup (label_id);
14179 *listp = new_loc_list (descr, node->label, endname, secname);
14180 if (TREE_CODE (decl) == PARM_DECL
14181 && node == loc_list->first
14182 && NOTE_P (node->loc)
14183 && strcmp (node->label, endname) == 0)
14184 (*listp)->force = true;
14185 listp = &(*listp)->dw_loc_next;
14187 if (range_across_switch)
14189 if (GET_CODE (node->loc) == EXPR_LIST)
14190 descr = dw_sra_loc_expr (decl, node->loc);
14191 else
14193 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14194 varloc = NOTE_VAR_LOCATION (node->loc);
14195 descr = dw_loc_list_1 (decl, varloc, want_address,
14196 initialized);
14198 gcc_assert (descr);
14199 /* The variable has a location between NODE->LABEL and
14200 NODE->NEXT->LABEL. */
14201 if (node->next)
14202 endname = node->next->label;
14203 else
14204 endname = cfun->fde->dw_fde_second_end;
14205 *listp = new_loc_list (descr,
14206 cfun->fde->dw_fde_second_begin,
14207 endname, secname);
14208 listp = &(*listp)->dw_loc_next;
14213 /* Try to avoid the overhead of a location list emitting a location
14214 expression instead, but only if we didn't have more than one
14215 location entry in the first place. If some entries were not
14216 representable, we don't want to pretend a single entry that was
14217 applies to the entire scope in which the variable is
14218 available. */
14219 if (list && loc_list->first->next)
14220 gen_llsym (list);
14222 return list;
14225 /* Return if the loc_list has only single element and thus can be represented
14226 as location description. */
14228 static bool
14229 single_element_loc_list_p (dw_loc_list_ref list)
14231 gcc_assert (!list->dw_loc_next || list->ll_symbol);
14232 return !list->ll_symbol;
14235 /* To each location in list LIST add loc descr REF. */
14237 static void
14238 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
14240 dw_loc_descr_ref copy;
14241 add_loc_descr (&list->expr, ref);
14242 list = list->dw_loc_next;
14243 while (list)
14245 copy = ggc_alloc<dw_loc_descr_node> ();
14246 memcpy (copy, ref, sizeof (dw_loc_descr_node));
14247 add_loc_descr (&list->expr, copy);
14248 while (copy->dw_loc_next)
14250 dw_loc_descr_ref new_copy = ggc_alloc<dw_loc_descr_node> ();
14251 memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
14252 copy->dw_loc_next = new_copy;
14253 copy = new_copy;
14255 list = list->dw_loc_next;
14259 /* Given two lists RET and LIST
14260 produce location list that is result of adding expression in LIST
14261 to expression in RET on each position in program.
14262 Might be destructive on both RET and LIST.
14264 TODO: We handle only simple cases of RET or LIST having at most one
14265 element. General case would inolve sorting the lists in program order
14266 and merging them that will need some additional work.
14267 Adding that will improve quality of debug info especially for SRA-ed
14268 structures. */
14270 static void
14271 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
14273 if (!list)
14274 return;
14275 if (!*ret)
14277 *ret = list;
14278 return;
14280 if (!list->dw_loc_next)
14282 add_loc_descr_to_each (*ret, list->expr);
14283 return;
14285 if (!(*ret)->dw_loc_next)
14287 add_loc_descr_to_each (list, (*ret)->expr);
14288 *ret = list;
14289 return;
14291 expansion_failed (NULL_TREE, NULL_RTX,
14292 "Don't know how to merge two non-trivial"
14293 " location lists.\n");
14294 *ret = NULL;
14295 return;
14298 /* LOC is constant expression. Try a luck, look it up in constant
14299 pool and return its loc_descr of its address. */
14301 static dw_loc_descr_ref
14302 cst_pool_loc_descr (tree loc)
14304 /* Get an RTL for this, if something has been emitted. */
14305 rtx rtl = lookup_constant_def (loc);
14307 if (!rtl || !MEM_P (rtl))
14309 gcc_assert (!rtl);
14310 return 0;
14312 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
14314 /* TODO: We might get more coverage if we was actually delaying expansion
14315 of all expressions till end of compilation when constant pools are fully
14316 populated. */
14317 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
14319 expansion_failed (loc, NULL_RTX,
14320 "CST value in contant pool but not marked.");
14321 return 0;
14323 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
14324 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
14327 /* Return dw_loc_list representing address of addr_expr LOC
14328 by looking for inner INDIRECT_REF expression and turning
14329 it into simple arithmetics.
14331 See loc_list_from_tree for the meaning of CONTEXT. */
14333 static dw_loc_list_ref
14334 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
14335 const loc_descr_context *context)
14337 tree obj, offset;
14338 HOST_WIDE_INT bitsize, bitpos, bytepos;
14339 machine_mode mode;
14340 int unsignedp, volatilep = 0;
14341 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14343 obj = get_inner_reference (TREE_OPERAND (loc, 0),
14344 &bitsize, &bitpos, &offset, &mode,
14345 &unsignedp, &volatilep, false);
14346 STRIP_NOPS (obj);
14347 if (bitpos % BITS_PER_UNIT)
14349 expansion_failed (loc, NULL_RTX, "bitfield access");
14350 return 0;
14352 if (!INDIRECT_REF_P (obj))
14354 expansion_failed (obj,
14355 NULL_RTX, "no indirect ref in inner refrence");
14356 return 0;
14358 if (!offset && !bitpos)
14359 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
14360 context);
14361 else if (toplev
14362 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
14363 && (dwarf_version >= 4 || !dwarf_strict))
14365 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
14366 if (!list_ret)
14367 return 0;
14368 if (offset)
14370 /* Variable offset. */
14371 list_ret1 = loc_list_from_tree (offset, 0, context);
14372 if (list_ret1 == 0)
14373 return 0;
14374 add_loc_list (&list_ret, list_ret1);
14375 if (!list_ret)
14376 return 0;
14377 add_loc_descr_to_each (list_ret,
14378 new_loc_descr (DW_OP_plus, 0, 0));
14380 bytepos = bitpos / BITS_PER_UNIT;
14381 if (bytepos > 0)
14382 add_loc_descr_to_each (list_ret,
14383 new_loc_descr (DW_OP_plus_uconst,
14384 bytepos, 0));
14385 else if (bytepos < 0)
14386 loc_list_plus_const (list_ret, bytepos);
14387 add_loc_descr_to_each (list_ret,
14388 new_loc_descr (DW_OP_stack_value, 0, 0));
14390 return list_ret;
14394 /* Helper structure for location descriptions generation. */
14395 struct loc_descr_context
14397 /* The type that is implicitly referenced by DW_OP_push_object_address, or
14398 NULL_TREE if DW_OP_push_object_address in invalid for this location
14399 description. This is used when processing PLACEHOLDER_EXPR nodes. */
14400 tree context_type;
14401 /* The ..._DECL node that should be translated as a
14402 DW_OP_push_object_address operation. */
14403 tree base_decl;
14406 /* Generate Dwarf location list representing LOC.
14407 If WANT_ADDRESS is false, expression computing LOC will be computed
14408 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
14409 if WANT_ADDRESS is 2, expression computing address useable in location
14410 will be returned (i.e. DW_OP_reg can be used
14411 to refer to register values).
14413 CONTEXT provides information to customize the location descriptions
14414 generation. Its context_type field specifies what type is implicitly
14415 referenced by DW_OP_push_object_address. If it is NULL_TREE, this operation
14416 will not be generated.
14418 If CONTEXT is NULL, the behavior is the same as if both context_type and
14419 base_decl fields were NULL_TREE. */
14421 static dw_loc_list_ref
14422 loc_list_from_tree (tree loc, int want_address,
14423 const struct loc_descr_context *context)
14425 dw_loc_descr_ref ret = NULL, ret1 = NULL;
14426 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14427 int have_address = 0;
14428 enum dwarf_location_atom op;
14430 /* ??? Most of the time we do not take proper care for sign/zero
14431 extending the values properly. Hopefully this won't be a real
14432 problem... */
14434 if (context != NULL
14435 && context->base_decl == loc
14436 && want_address == 0)
14438 if (dwarf_version >= 3 || !dwarf_strict)
14439 return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
14440 NULL, NULL, NULL);
14441 else
14442 return NULL;
14445 switch (TREE_CODE (loc))
14447 case ERROR_MARK:
14448 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
14449 return 0;
14451 case PLACEHOLDER_EXPR:
14452 /* This case involves extracting fields from an object to determine the
14453 position of other fields. It is supposed to appear only as the first
14454 operand of COMPONENT_REF nodes and to reference precisely the type
14455 that the context allows. */
14456 if (context != NULL
14457 && TREE_TYPE (loc) == context->context_type
14458 && want_address >= 1)
14460 if (dwarf_version >= 3 || !dwarf_strict)
14462 ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
14463 have_address = 1;
14464 break;
14466 else
14467 return NULL;
14469 else
14470 expansion_failed (loc, NULL_RTX,
14471 "PLACEHOLDER_EXPR for an unexpected type");
14472 break;
14474 case CALL_EXPR:
14475 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
14476 /* There are no opcodes for these operations. */
14477 return 0;
14479 case PREINCREMENT_EXPR:
14480 case PREDECREMENT_EXPR:
14481 case POSTINCREMENT_EXPR:
14482 case POSTDECREMENT_EXPR:
14483 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
14484 /* There are no opcodes for these operations. */
14485 return 0;
14487 case ADDR_EXPR:
14488 /* If we already want an address, see if there is INDIRECT_REF inside
14489 e.g. for &this->field. */
14490 if (want_address)
14492 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
14493 (loc, want_address == 2, context);
14494 if (list_ret)
14495 have_address = 1;
14496 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
14497 && (ret = cst_pool_loc_descr (loc)))
14498 have_address = 1;
14500 /* Otherwise, process the argument and look for the address. */
14501 if (!list_ret && !ret)
14502 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1, context);
14503 else
14505 if (want_address)
14506 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
14507 return NULL;
14509 break;
14511 case VAR_DECL:
14512 if (DECL_THREAD_LOCAL_P (loc))
14514 rtx rtl;
14515 enum dwarf_location_atom tls_op;
14516 enum dtprel_bool dtprel = dtprel_false;
14518 if (targetm.have_tls)
14520 /* If this is not defined, we have no way to emit the
14521 data. */
14522 if (!targetm.asm_out.output_dwarf_dtprel)
14523 return 0;
14525 /* The way DW_OP_GNU_push_tls_address is specified, we
14526 can only look up addresses of objects in the current
14527 module. We used DW_OP_addr as first op, but that's
14528 wrong, because DW_OP_addr is relocated by the debug
14529 info consumer, while DW_OP_GNU_push_tls_address
14530 operand shouldn't be. */
14531 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
14532 return 0;
14533 dtprel = dtprel_true;
14534 tls_op = DW_OP_GNU_push_tls_address;
14536 else
14538 if (!targetm.emutls.debug_form_tls_address
14539 || !(dwarf_version >= 3 || !dwarf_strict))
14540 return 0;
14541 /* We stuffed the control variable into the DECL_VALUE_EXPR
14542 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
14543 no longer appear in gimple code. We used the control
14544 variable in specific so that we could pick it up here. */
14545 loc = DECL_VALUE_EXPR (loc);
14546 tls_op = DW_OP_form_tls_address;
14549 rtl = rtl_for_decl_location (loc);
14550 if (rtl == NULL_RTX)
14551 return 0;
14553 if (!MEM_P (rtl))
14554 return 0;
14555 rtl = XEXP (rtl, 0);
14556 if (! CONSTANT_P (rtl))
14557 return 0;
14559 ret = new_addr_loc_descr (rtl, dtprel);
14560 ret1 = new_loc_descr (tls_op, 0, 0);
14561 add_loc_descr (&ret, ret1);
14563 have_address = 1;
14564 break;
14566 /* FALLTHRU */
14568 case PARM_DECL:
14569 case RESULT_DECL:
14570 if (DECL_HAS_VALUE_EXPR_P (loc))
14571 return loc_list_from_tree (DECL_VALUE_EXPR (loc),
14572 want_address, context);
14573 /* FALLTHRU */
14575 case FUNCTION_DECL:
14577 rtx rtl;
14578 var_loc_list *loc_list = lookup_decl_loc (loc);
14580 if (loc_list && loc_list->first)
14582 list_ret = dw_loc_list (loc_list, loc, want_address);
14583 have_address = want_address != 0;
14584 break;
14586 rtl = rtl_for_decl_location (loc);
14587 if (rtl == NULL_RTX)
14589 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
14590 return 0;
14592 else if (CONST_INT_P (rtl))
14594 HOST_WIDE_INT val = INTVAL (rtl);
14595 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14596 val &= GET_MODE_MASK (DECL_MODE (loc));
14597 ret = int_loc_descriptor (val);
14599 else if (GET_CODE (rtl) == CONST_STRING)
14601 expansion_failed (loc, NULL_RTX, "CONST_STRING");
14602 return 0;
14604 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
14605 ret = new_addr_loc_descr (rtl, dtprel_false);
14606 else
14608 machine_mode mode, mem_mode;
14610 /* Certain constructs can only be represented at top-level. */
14611 if (want_address == 2)
14613 ret = loc_descriptor (rtl, VOIDmode,
14614 VAR_INIT_STATUS_INITIALIZED);
14615 have_address = 1;
14617 else
14619 mode = GET_MODE (rtl);
14620 mem_mode = VOIDmode;
14621 if (MEM_P (rtl))
14623 mem_mode = mode;
14624 mode = get_address_mode (rtl);
14625 rtl = XEXP (rtl, 0);
14626 have_address = 1;
14628 ret = mem_loc_descriptor (rtl, mode, mem_mode,
14629 VAR_INIT_STATUS_INITIALIZED);
14631 if (!ret)
14632 expansion_failed (loc, rtl,
14633 "failed to produce loc descriptor for rtl");
14636 break;
14638 case MEM_REF:
14639 if (!integer_zerop (TREE_OPERAND (loc, 1)))
14641 have_address = 1;
14642 goto do_plus;
14644 /* Fallthru. */
14645 case INDIRECT_REF:
14646 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14647 have_address = 1;
14648 break;
14650 case TARGET_MEM_REF:
14651 case SSA_NAME:
14652 case DEBUG_EXPR_DECL:
14653 return NULL;
14655 case COMPOUND_EXPR:
14656 return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address, context);
14658 CASE_CONVERT:
14659 case VIEW_CONVERT_EXPR:
14660 case SAVE_EXPR:
14661 case MODIFY_EXPR:
14662 return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address, context);
14664 case COMPONENT_REF:
14665 case BIT_FIELD_REF:
14666 case ARRAY_REF:
14667 case ARRAY_RANGE_REF:
14668 case REALPART_EXPR:
14669 case IMAGPART_EXPR:
14671 tree obj, offset;
14672 HOST_WIDE_INT bitsize, bitpos, bytepos;
14673 machine_mode mode;
14674 int unsignedp, volatilep = 0;
14676 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
14677 &unsignedp, &volatilep, false);
14679 gcc_assert (obj != loc);
14681 list_ret = loc_list_from_tree (obj,
14682 want_address == 2
14683 && !bitpos && !offset ? 2 : 1,
14684 context);
14685 /* TODO: We can extract value of the small expression via shifting even
14686 for nonzero bitpos. */
14687 if (list_ret == 0)
14688 return 0;
14689 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
14691 expansion_failed (loc, NULL_RTX,
14692 "bitfield access");
14693 return 0;
14696 if (offset != NULL_TREE)
14698 /* Variable offset. */
14699 list_ret1 = loc_list_from_tree (offset, 0, context);
14700 if (list_ret1 == 0)
14701 return 0;
14702 add_loc_list (&list_ret, list_ret1);
14703 if (!list_ret)
14704 return 0;
14705 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
14708 bytepos = bitpos / BITS_PER_UNIT;
14709 if (bytepos > 0)
14710 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
14711 else if (bytepos < 0)
14712 loc_list_plus_const (list_ret, bytepos);
14714 have_address = 1;
14715 break;
14718 case INTEGER_CST:
14719 if ((want_address || !tree_fits_shwi_p (loc))
14720 && (ret = cst_pool_loc_descr (loc)))
14721 have_address = 1;
14722 else if (want_address == 2
14723 && tree_fits_shwi_p (loc)
14724 && (ret = address_of_int_loc_descriptor
14725 (int_size_in_bytes (TREE_TYPE (loc)),
14726 tree_to_shwi (loc))))
14727 have_address = 1;
14728 else if (tree_fits_shwi_p (loc))
14729 ret = int_loc_descriptor (tree_to_shwi (loc));
14730 else
14732 expansion_failed (loc, NULL_RTX,
14733 "Integer operand is not host integer");
14734 return 0;
14736 break;
14738 case CONSTRUCTOR:
14739 case REAL_CST:
14740 case STRING_CST:
14741 case COMPLEX_CST:
14742 if ((ret = cst_pool_loc_descr (loc)))
14743 have_address = 1;
14744 else
14745 /* We can construct small constants here using int_loc_descriptor. */
14746 expansion_failed (loc, NULL_RTX,
14747 "constructor or constant not in constant pool");
14748 break;
14750 case TRUTH_AND_EXPR:
14751 case TRUTH_ANDIF_EXPR:
14752 case BIT_AND_EXPR:
14753 op = DW_OP_and;
14754 goto do_binop;
14756 case TRUTH_XOR_EXPR:
14757 case BIT_XOR_EXPR:
14758 op = DW_OP_xor;
14759 goto do_binop;
14761 case TRUTH_OR_EXPR:
14762 case TRUTH_ORIF_EXPR:
14763 case BIT_IOR_EXPR:
14764 op = DW_OP_or;
14765 goto do_binop;
14767 case FLOOR_DIV_EXPR:
14768 case CEIL_DIV_EXPR:
14769 case ROUND_DIV_EXPR:
14770 case TRUNC_DIV_EXPR:
14771 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14772 return 0;
14773 op = DW_OP_div;
14774 goto do_binop;
14776 case MINUS_EXPR:
14777 op = DW_OP_minus;
14778 goto do_binop;
14780 case FLOOR_MOD_EXPR:
14781 case CEIL_MOD_EXPR:
14782 case ROUND_MOD_EXPR:
14783 case TRUNC_MOD_EXPR:
14784 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14786 op = DW_OP_mod;
14787 goto do_binop;
14789 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14790 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
14791 if (list_ret == 0 || list_ret1 == 0)
14792 return 0;
14794 add_loc_list (&list_ret, list_ret1);
14795 if (list_ret == 0)
14796 return 0;
14797 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14798 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14799 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
14800 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
14801 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
14802 break;
14804 case MULT_EXPR:
14805 op = DW_OP_mul;
14806 goto do_binop;
14808 case LSHIFT_EXPR:
14809 op = DW_OP_shl;
14810 goto do_binop;
14812 case RSHIFT_EXPR:
14813 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
14814 goto do_binop;
14816 case POINTER_PLUS_EXPR:
14817 case PLUS_EXPR:
14818 do_plus:
14819 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
14821 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14822 if (list_ret == 0)
14823 return 0;
14825 loc_list_plus_const (list_ret, tree_to_shwi (TREE_OPERAND (loc, 1)));
14826 break;
14829 op = DW_OP_plus;
14830 goto do_binop;
14832 case LE_EXPR:
14833 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14834 return 0;
14836 op = DW_OP_le;
14837 goto do_binop;
14839 case GE_EXPR:
14840 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14841 return 0;
14843 op = DW_OP_ge;
14844 goto do_binop;
14846 case LT_EXPR:
14847 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14848 return 0;
14850 op = DW_OP_lt;
14851 goto do_binop;
14853 case GT_EXPR:
14854 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14855 return 0;
14857 op = DW_OP_gt;
14858 goto do_binop;
14860 case EQ_EXPR:
14861 op = DW_OP_eq;
14862 goto do_binop;
14864 case NE_EXPR:
14865 op = DW_OP_ne;
14866 goto do_binop;
14868 do_binop:
14869 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14870 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
14871 if (list_ret == 0 || list_ret1 == 0)
14872 return 0;
14874 add_loc_list (&list_ret, list_ret1);
14875 if (list_ret == 0)
14876 return 0;
14877 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14878 break;
14880 case TRUTH_NOT_EXPR:
14881 case BIT_NOT_EXPR:
14882 op = DW_OP_not;
14883 goto do_unop;
14885 case ABS_EXPR:
14886 op = DW_OP_abs;
14887 goto do_unop;
14889 case NEGATE_EXPR:
14890 op = DW_OP_neg;
14891 goto do_unop;
14893 do_unop:
14894 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14895 if (list_ret == 0)
14896 return 0;
14898 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14899 break;
14901 case MIN_EXPR:
14902 case MAX_EXPR:
14904 const enum tree_code code =
14905 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
14907 loc = build3 (COND_EXPR, TREE_TYPE (loc),
14908 build2 (code, integer_type_node,
14909 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
14910 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
14913 /* ... fall through ... */
14915 case COND_EXPR:
14917 dw_loc_descr_ref lhs
14918 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
14919 dw_loc_list_ref rhs
14920 = loc_list_from_tree (TREE_OPERAND (loc, 2), 0, context);
14921 dw_loc_descr_ref bra_node, jump_node, tmp;
14923 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14924 if (list_ret == 0 || lhs == 0 || rhs == 0)
14925 return 0;
14927 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14928 add_loc_descr_to_each (list_ret, bra_node);
14930 add_loc_list (&list_ret, rhs);
14931 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
14932 add_loc_descr_to_each (list_ret, jump_node);
14934 add_loc_descr_to_each (list_ret, lhs);
14935 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14936 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
14938 /* ??? Need a node to point the skip at. Use a nop. */
14939 tmp = new_loc_descr (DW_OP_nop, 0, 0);
14940 add_loc_descr_to_each (list_ret, tmp);
14941 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14942 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
14944 break;
14946 case FIX_TRUNC_EXPR:
14947 return 0;
14949 default:
14950 /* Leave front-end specific codes as simply unknown. This comes
14951 up, for instance, with the C STMT_EXPR. */
14952 if ((unsigned int) TREE_CODE (loc)
14953 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
14955 expansion_failed (loc, NULL_RTX,
14956 "language specific tree node");
14957 return 0;
14960 #ifdef ENABLE_CHECKING
14961 /* Otherwise this is a generic code; we should just lists all of
14962 these explicitly. We forgot one. */
14963 gcc_unreachable ();
14964 #else
14965 /* In a release build, we want to degrade gracefully: better to
14966 generate incomplete debugging information than to crash. */
14967 return NULL;
14968 #endif
14971 if (!ret && !list_ret)
14972 return 0;
14974 if (want_address == 2 && !have_address
14975 && (dwarf_version >= 4 || !dwarf_strict))
14977 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14979 expansion_failed (loc, NULL_RTX,
14980 "DWARF address size mismatch");
14981 return 0;
14983 if (ret)
14984 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
14985 else
14986 add_loc_descr_to_each (list_ret,
14987 new_loc_descr (DW_OP_stack_value, 0, 0));
14988 have_address = 1;
14990 /* Show if we can't fill the request for an address. */
14991 if (want_address && !have_address)
14993 expansion_failed (loc, NULL_RTX,
14994 "Want address and only have value");
14995 return 0;
14998 gcc_assert (!ret || !list_ret);
15000 /* If we've got an address and don't want one, dereference. */
15001 if (!want_address && have_address)
15003 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
15005 if (size > DWARF2_ADDR_SIZE || size == -1)
15007 expansion_failed (loc, NULL_RTX,
15008 "DWARF address size mismatch");
15009 return 0;
15011 else if (size == DWARF2_ADDR_SIZE)
15012 op = DW_OP_deref;
15013 else
15014 op = DW_OP_deref_size;
15016 if (ret)
15017 add_loc_descr (&ret, new_loc_descr (op, size, 0));
15018 else
15019 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
15021 if (ret)
15022 list_ret = new_loc_list (ret, NULL, NULL, NULL);
15024 return list_ret;
15027 /* Same as above but return only single location expression. */
15028 static dw_loc_descr_ref
15029 loc_descriptor_from_tree (tree loc, int want_address,
15030 const struct loc_descr_context *context)
15032 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
15033 if (!ret)
15034 return NULL;
15035 if (ret->dw_loc_next)
15037 expansion_failed (loc, NULL_RTX,
15038 "Location list where only loc descriptor needed");
15039 return NULL;
15041 return ret->expr;
15044 /* Given a value, round it up to the lowest multiple of `boundary'
15045 which is not less than the value itself. */
15047 static inline HOST_WIDE_INT
15048 ceiling (HOST_WIDE_INT value, unsigned int boundary)
15050 return (((value + boundary - 1) / boundary) * boundary);
15053 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
15054 pointer to the declared type for the relevant field variable, or return
15055 `integer_type_node' if the given node turns out to be an
15056 ERROR_MARK node. */
15058 static inline tree
15059 field_type (const_tree decl)
15061 tree type;
15063 if (TREE_CODE (decl) == ERROR_MARK)
15064 return integer_type_node;
15066 type = DECL_BIT_FIELD_TYPE (decl);
15067 if (type == NULL_TREE)
15068 type = TREE_TYPE (decl);
15070 return type;
15073 /* Given a pointer to a tree node, return the alignment in bits for
15074 it, or else return BITS_PER_WORD if the node actually turns out to
15075 be an ERROR_MARK node. */
15077 static inline unsigned
15078 simple_type_align_in_bits (const_tree type)
15080 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
15083 static inline unsigned
15084 simple_decl_align_in_bits (const_tree decl)
15086 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
15089 /* Return the result of rounding T up to ALIGN. */
15091 static inline offset_int
15092 round_up_to_align (const offset_int &t, unsigned int align)
15094 return wi::udiv_trunc (t + align - 1, align) * align;
15097 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
15098 lowest addressed byte of the "containing object" for the given FIELD_DECL,
15099 or return 0 if we are unable to determine what that offset is, either
15100 because the argument turns out to be a pointer to an ERROR_MARK node, or
15101 because the offset is actually variable. (We can't handle the latter case
15102 just yet). */
15104 static HOST_WIDE_INT
15105 field_byte_offset (const_tree decl)
15107 offset_int object_offset_in_bits;
15108 offset_int object_offset_in_bytes;
15109 offset_int bitpos_int;
15111 if (TREE_CODE (decl) == ERROR_MARK)
15112 return 0;
15114 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
15116 /* We cannot yet cope with fields whose positions are variable, so
15117 for now, when we see such things, we simply return 0. Someday, we may
15118 be able to handle such cases, but it will be damn difficult. */
15119 if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
15120 return 0;
15122 bitpos_int = wi::to_offset (bit_position (decl));
15124 if (PCC_BITFIELD_TYPE_MATTERS)
15126 tree type;
15127 tree field_size_tree;
15128 offset_int deepest_bitpos;
15129 offset_int field_size_in_bits;
15130 unsigned int type_align_in_bits;
15131 unsigned int decl_align_in_bits;
15132 offset_int type_size_in_bits;
15134 type = field_type (decl);
15135 type_size_in_bits = offset_int_type_size_in_bits (type);
15136 type_align_in_bits = simple_type_align_in_bits (type);
15138 field_size_tree = DECL_SIZE (decl);
15140 /* The size could be unspecified if there was an error, or for
15141 a flexible array member. */
15142 if (!field_size_tree)
15143 field_size_tree = bitsize_zero_node;
15145 /* If the size of the field is not constant, use the type size. */
15146 if (TREE_CODE (field_size_tree) == INTEGER_CST)
15147 field_size_in_bits = wi::to_offset (field_size_tree);
15148 else
15149 field_size_in_bits = type_size_in_bits;
15151 decl_align_in_bits = simple_decl_align_in_bits (decl);
15153 /* The GCC front-end doesn't make any attempt to keep track of the
15154 starting bit offset (relative to the start of the containing
15155 structure type) of the hypothetical "containing object" for a
15156 bit-field. Thus, when computing the byte offset value for the
15157 start of the "containing object" of a bit-field, we must deduce
15158 this information on our own. This can be rather tricky to do in
15159 some cases. For example, handling the following structure type
15160 definition when compiling for an i386/i486 target (which only
15161 aligns long long's to 32-bit boundaries) can be very tricky:
15163 struct S { int field1; long long field2:31; };
15165 Fortunately, there is a simple rule-of-thumb which can be used
15166 in such cases. When compiling for an i386/i486, GCC will
15167 allocate 8 bytes for the structure shown above. It decides to
15168 do this based upon one simple rule for bit-field allocation.
15169 GCC allocates each "containing object" for each bit-field at
15170 the first (i.e. lowest addressed) legitimate alignment boundary
15171 (based upon the required minimum alignment for the declared
15172 type of the field) which it can possibly use, subject to the
15173 condition that there is still enough available space remaining
15174 in the containing object (when allocated at the selected point)
15175 to fully accommodate all of the bits of the bit-field itself.
15177 This simple rule makes it obvious why GCC allocates 8 bytes for
15178 each object of the structure type shown above. When looking
15179 for a place to allocate the "containing object" for `field2',
15180 the compiler simply tries to allocate a 64-bit "containing
15181 object" at each successive 32-bit boundary (starting at zero)
15182 until it finds a place to allocate that 64- bit field such that
15183 at least 31 contiguous (and previously unallocated) bits remain
15184 within that selected 64 bit field. (As it turns out, for the
15185 example above, the compiler finds it is OK to allocate the
15186 "containing object" 64-bit field at bit-offset zero within the
15187 structure type.)
15189 Here we attempt to work backwards from the limited set of facts
15190 we're given, and we try to deduce from those facts, where GCC
15191 must have believed that the containing object started (within
15192 the structure type). The value we deduce is then used (by the
15193 callers of this routine) to generate DW_AT_location and
15194 DW_AT_bit_offset attributes for fields (both bit-fields and, in
15195 the case of DW_AT_location, regular fields as well). */
15197 /* Figure out the bit-distance from the start of the structure to
15198 the "deepest" bit of the bit-field. */
15199 deepest_bitpos = bitpos_int + field_size_in_bits;
15201 /* This is the tricky part. Use some fancy footwork to deduce
15202 where the lowest addressed bit of the containing object must
15203 be. */
15204 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
15206 /* Round up to type_align by default. This works best for
15207 bitfields. */
15208 object_offset_in_bits
15209 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
15211 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
15213 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
15215 /* Round up to decl_align instead. */
15216 object_offset_in_bits
15217 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
15220 else
15221 object_offset_in_bits = bitpos_int;
15223 object_offset_in_bytes
15224 = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
15225 return object_offset_in_bytes.to_shwi ();
15228 /* The following routines define various Dwarf attributes and any data
15229 associated with them. */
15231 /* Add a location description attribute value to a DIE.
15233 This emits location attributes suitable for whole variables and
15234 whole parameters. Note that the location attributes for struct fields are
15235 generated by the routine `data_member_location_attribute' below. */
15237 static inline void
15238 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
15239 dw_loc_list_ref descr)
15241 if (descr == 0)
15242 return;
15243 if (single_element_loc_list_p (descr))
15244 add_AT_loc (die, attr_kind, descr->expr);
15245 else
15246 add_AT_loc_list (die, attr_kind, descr);
15249 /* Add DW_AT_accessibility attribute to DIE if needed. */
15251 static void
15252 add_accessibility_attribute (dw_die_ref die, tree decl)
15254 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
15255 children, otherwise the default is DW_ACCESS_public. In DWARF2
15256 the default has always been DW_ACCESS_public. */
15257 if (TREE_PROTECTED (decl))
15258 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
15259 else if (TREE_PRIVATE (decl))
15261 if (dwarf_version == 2
15262 || die->die_parent == NULL
15263 || die->die_parent->die_tag != DW_TAG_class_type)
15264 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
15266 else if (dwarf_version > 2
15267 && die->die_parent
15268 && die->die_parent->die_tag == DW_TAG_class_type)
15269 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
15272 /* Attach the specialized form of location attribute used for data members of
15273 struct and union types. In the special case of a FIELD_DECL node which
15274 represents a bit-field, the "offset" part of this special location
15275 descriptor must indicate the distance in bytes from the lowest-addressed
15276 byte of the containing struct or union type to the lowest-addressed byte of
15277 the "containing object" for the bit-field. (See the `field_byte_offset'
15278 function above).
15280 For any given bit-field, the "containing object" is a hypothetical object
15281 (of some integral or enum type) within which the given bit-field lives. The
15282 type of this hypothetical "containing object" is always the same as the
15283 declared type of the individual bit-field itself (for GCC anyway... the
15284 DWARF spec doesn't actually mandate this). Note that it is the size (in
15285 bytes) of the hypothetical "containing object" which will be given in the
15286 DW_AT_byte_size attribute for this bit-field. (See the
15287 `byte_size_attribute' function below.) It is also used when calculating the
15288 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
15289 function below.) */
15291 static void
15292 add_data_member_location_attribute (dw_die_ref die, tree decl)
15294 HOST_WIDE_INT offset;
15295 dw_loc_descr_ref loc_descr = 0;
15297 if (TREE_CODE (decl) == TREE_BINFO)
15299 /* We're working on the TAG_inheritance for a base class. */
15300 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
15302 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
15303 aren't at a fixed offset from all (sub)objects of the same
15304 type. We need to extract the appropriate offset from our
15305 vtable. The following dwarf expression means
15307 BaseAddr = ObAddr + *((*ObAddr) - Offset)
15309 This is specific to the V3 ABI, of course. */
15311 dw_loc_descr_ref tmp;
15313 /* Make a copy of the object address. */
15314 tmp = new_loc_descr (DW_OP_dup, 0, 0);
15315 add_loc_descr (&loc_descr, tmp);
15317 /* Extract the vtable address. */
15318 tmp = new_loc_descr (DW_OP_deref, 0, 0);
15319 add_loc_descr (&loc_descr, tmp);
15321 /* Calculate the address of the offset. */
15322 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
15323 gcc_assert (offset < 0);
15325 tmp = int_loc_descriptor (-offset);
15326 add_loc_descr (&loc_descr, tmp);
15327 tmp = new_loc_descr (DW_OP_minus, 0, 0);
15328 add_loc_descr (&loc_descr, tmp);
15330 /* Extract the offset. */
15331 tmp = new_loc_descr (DW_OP_deref, 0, 0);
15332 add_loc_descr (&loc_descr, tmp);
15334 /* Add it to the object address. */
15335 tmp = new_loc_descr (DW_OP_plus, 0, 0);
15336 add_loc_descr (&loc_descr, tmp);
15338 else
15339 offset = tree_to_shwi (BINFO_OFFSET (decl));
15341 else
15342 offset = field_byte_offset (decl);
15344 if (! loc_descr)
15346 if (dwarf_version > 2)
15348 /* Don't need to output a location expression, just the constant. */
15349 if (offset < 0)
15350 add_AT_int (die, DW_AT_data_member_location, offset);
15351 else
15352 add_AT_unsigned (die, DW_AT_data_member_location, offset);
15353 return;
15355 else
15357 enum dwarf_location_atom op;
15359 /* The DWARF2 standard says that we should assume that the structure
15360 address is already on the stack, so we can specify a structure
15361 field address by using DW_OP_plus_uconst. */
15362 op = DW_OP_plus_uconst;
15363 loc_descr = new_loc_descr (op, offset, 0);
15367 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
15370 /* Writes integer values to dw_vec_const array. */
15372 static void
15373 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
15375 while (size != 0)
15377 *dest++ = val & 0xff;
15378 val >>= 8;
15379 --size;
15383 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
15385 static HOST_WIDE_INT
15386 extract_int (const unsigned char *src, unsigned int size)
15388 HOST_WIDE_INT val = 0;
15390 src += size;
15391 while (size != 0)
15393 val <<= 8;
15394 val |= *--src & 0xff;
15395 --size;
15397 return val;
15400 /* Writes wide_int values to dw_vec_const array. */
15402 static void
15403 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
15405 int i;
15407 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
15409 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
15410 return;
15413 /* We'd have to extend this code to support odd sizes. */
15414 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
15416 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
15418 if (WORDS_BIG_ENDIAN)
15419 for (i = n - 1; i >= 0; i--)
15421 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
15422 dest += sizeof (HOST_WIDE_INT);
15424 else
15425 for (i = 0; i < n; i++)
15427 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
15428 dest += sizeof (HOST_WIDE_INT);
15432 /* Writes floating point values to dw_vec_const array. */
15434 static void
15435 insert_float (const_rtx rtl, unsigned char *array)
15437 REAL_VALUE_TYPE rv;
15438 long val[4];
15439 int i;
15441 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
15442 real_to_target (val, &rv, GET_MODE (rtl));
15444 /* real_to_target puts 32-bit pieces in each long. Pack them. */
15445 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
15447 insert_int (val[i], 4, array);
15448 array += 4;
15452 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
15453 does not have a "location" either in memory or in a register. These
15454 things can arise in GNU C when a constant is passed as an actual parameter
15455 to an inlined function. They can also arise in C++ where declared
15456 constants do not necessarily get memory "homes". */
15458 static bool
15459 add_const_value_attribute (dw_die_ref die, rtx rtl)
15461 switch (GET_CODE (rtl))
15463 case CONST_INT:
15465 HOST_WIDE_INT val = INTVAL (rtl);
15467 if (val < 0)
15468 add_AT_int (die, DW_AT_const_value, val);
15469 else
15470 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
15472 return true;
15474 case CONST_WIDE_INT:
15475 add_AT_wide (die, DW_AT_const_value,
15476 std::make_pair (rtl, GET_MODE (rtl)));
15477 return true;
15479 case CONST_DOUBLE:
15480 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
15481 floating-point constant. A CONST_DOUBLE is used whenever the
15482 constant requires more than one word in order to be adequately
15483 represented. */
15485 machine_mode mode = GET_MODE (rtl);
15487 if (TARGET_SUPPORTS_WIDE_INT == 0 && !SCALAR_FLOAT_MODE_P (mode))
15488 add_AT_double (die, DW_AT_const_value,
15489 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
15490 else
15492 unsigned int length = GET_MODE_SIZE (mode);
15493 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15495 insert_float (rtl, array);
15496 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
15499 return true;
15501 case CONST_VECTOR:
15503 machine_mode mode = GET_MODE (rtl);
15504 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
15505 unsigned int length = CONST_VECTOR_NUNITS (rtl);
15506 unsigned char *array
15507 = ggc_vec_alloc<unsigned char> (length * elt_size);
15508 unsigned int i;
15509 unsigned char *p;
15510 machine_mode imode = GET_MODE_INNER (mode);
15512 switch (GET_MODE_CLASS (mode))
15514 case MODE_VECTOR_INT:
15515 for (i = 0, p = array; i < length; i++, p += elt_size)
15517 rtx elt = CONST_VECTOR_ELT (rtl, i);
15518 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
15520 break;
15522 case MODE_VECTOR_FLOAT:
15523 for (i = 0, p = array; i < length; i++, p += elt_size)
15525 rtx elt = CONST_VECTOR_ELT (rtl, i);
15526 insert_float (elt, p);
15528 break;
15530 default:
15531 gcc_unreachable ();
15534 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
15536 return true;
15538 case CONST_STRING:
15539 if (dwarf_version >= 4 || !dwarf_strict)
15541 dw_loc_descr_ref loc_result;
15542 resolve_one_addr (&rtl);
15543 rtl_addr:
15544 loc_result = new_addr_loc_descr (rtl, dtprel_false);
15545 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
15546 add_AT_loc (die, DW_AT_location, loc_result);
15547 vec_safe_push (used_rtx_array, rtl);
15548 return true;
15550 return false;
15552 case CONST:
15553 if (CONSTANT_P (XEXP (rtl, 0)))
15554 return add_const_value_attribute (die, XEXP (rtl, 0));
15555 /* FALLTHROUGH */
15556 case SYMBOL_REF:
15557 if (!const_ok_for_output (rtl))
15558 return false;
15559 case LABEL_REF:
15560 if (dwarf_version >= 4 || !dwarf_strict)
15561 goto rtl_addr;
15562 return false;
15564 case PLUS:
15565 /* In cases where an inlined instance of an inline function is passed
15566 the address of an `auto' variable (which is local to the caller) we
15567 can get a situation where the DECL_RTL of the artificial local
15568 variable (for the inlining) which acts as a stand-in for the
15569 corresponding formal parameter (of the inline function) will look
15570 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
15571 exactly a compile-time constant expression, but it isn't the address
15572 of the (artificial) local variable either. Rather, it represents the
15573 *value* which the artificial local variable always has during its
15574 lifetime. We currently have no way to represent such quasi-constant
15575 values in Dwarf, so for now we just punt and generate nothing. */
15576 return false;
15578 case HIGH:
15579 case CONST_FIXED:
15580 return false;
15582 case MEM:
15583 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
15584 && MEM_READONLY_P (rtl)
15585 && GET_MODE (rtl) == BLKmode)
15587 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
15588 return true;
15590 return false;
15592 default:
15593 /* No other kinds of rtx should be possible here. */
15594 gcc_unreachable ();
15596 return false;
15599 /* Determine whether the evaluation of EXPR references any variables
15600 or functions which aren't otherwise used (and therefore may not be
15601 output). */
15602 static tree
15603 reference_to_unused (tree * tp, int * walk_subtrees,
15604 void * data ATTRIBUTE_UNUSED)
15606 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
15607 *walk_subtrees = 0;
15609 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
15610 && ! TREE_ASM_WRITTEN (*tp))
15611 return *tp;
15612 /* ??? The C++ FE emits debug information for using decls, so
15613 putting gcc_unreachable here falls over. See PR31899. For now
15614 be conservative. */
15615 else if (!symtab->global_info_ready
15616 && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
15617 return *tp;
15618 else if (TREE_CODE (*tp) == VAR_DECL)
15620 varpool_node *node = varpool_node::get (*tp);
15621 if (!node || !node->definition)
15622 return *tp;
15624 else if (TREE_CODE (*tp) == FUNCTION_DECL
15625 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
15627 /* The call graph machinery must have finished analyzing,
15628 optimizing and gimplifying the CU by now.
15629 So if *TP has no call graph node associated
15630 to it, it means *TP will not be emitted. */
15631 if (!cgraph_node::get (*tp))
15632 return *tp;
15634 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
15635 return *tp;
15637 return NULL_TREE;
15640 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
15641 for use in a later add_const_value_attribute call. */
15643 static rtx
15644 rtl_for_decl_init (tree init, tree type)
15646 rtx rtl = NULL_RTX;
15648 STRIP_NOPS (init);
15650 /* If a variable is initialized with a string constant without embedded
15651 zeros, build CONST_STRING. */
15652 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
15654 tree enttype = TREE_TYPE (type);
15655 tree domain = TYPE_DOMAIN (type);
15656 machine_mode mode = TYPE_MODE (enttype);
15658 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
15659 && domain
15660 && integer_zerop (TYPE_MIN_VALUE (domain))
15661 && compare_tree_int (TYPE_MAX_VALUE (domain),
15662 TREE_STRING_LENGTH (init) - 1) == 0
15663 && ((size_t) TREE_STRING_LENGTH (init)
15664 == strlen (TREE_STRING_POINTER (init)) + 1))
15666 rtl = gen_rtx_CONST_STRING (VOIDmode,
15667 ggc_strdup (TREE_STRING_POINTER (init)));
15668 rtl = gen_rtx_MEM (BLKmode, rtl);
15669 MEM_READONLY_P (rtl) = 1;
15672 /* Other aggregates, and complex values, could be represented using
15673 CONCAT: FIXME! */
15674 else if (AGGREGATE_TYPE_P (type)
15675 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
15676 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
15677 || TREE_CODE (type) == COMPLEX_TYPE)
15679 /* Vectors only work if their mode is supported by the target.
15680 FIXME: generic vectors ought to work too. */
15681 else if (TREE_CODE (type) == VECTOR_TYPE
15682 && !VECTOR_MODE_P (TYPE_MODE (type)))
15684 /* If the initializer is something that we know will expand into an
15685 immediate RTL constant, expand it now. We must be careful not to
15686 reference variables which won't be output. */
15687 else if (initializer_constant_valid_p (init, type)
15688 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
15690 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
15691 possible. */
15692 if (TREE_CODE (type) == VECTOR_TYPE)
15693 switch (TREE_CODE (init))
15695 case VECTOR_CST:
15696 break;
15697 case CONSTRUCTOR:
15698 if (TREE_CONSTANT (init))
15700 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
15701 bool constant_p = true;
15702 tree value;
15703 unsigned HOST_WIDE_INT ix;
15705 /* Even when ctor is constant, it might contain non-*_CST
15706 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
15707 belong into VECTOR_CST nodes. */
15708 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
15709 if (!CONSTANT_CLASS_P (value))
15711 constant_p = false;
15712 break;
15715 if (constant_p)
15717 init = build_vector_from_ctor (type, elts);
15718 break;
15721 /* FALLTHRU */
15723 default:
15724 return NULL;
15727 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
15729 /* If expand_expr returns a MEM, it wasn't immediate. */
15730 gcc_assert (!rtl || !MEM_P (rtl));
15733 return rtl;
15736 /* Generate RTL for the variable DECL to represent its location. */
15738 static rtx
15739 rtl_for_decl_location (tree decl)
15741 rtx rtl;
15743 /* Here we have to decide where we are going to say the parameter "lives"
15744 (as far as the debugger is concerned). We only have a couple of
15745 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
15747 DECL_RTL normally indicates where the parameter lives during most of the
15748 activation of the function. If optimization is enabled however, this
15749 could be either NULL or else a pseudo-reg. Both of those cases indicate
15750 that the parameter doesn't really live anywhere (as far as the code
15751 generation parts of GCC are concerned) during most of the function's
15752 activation. That will happen (for example) if the parameter is never
15753 referenced within the function.
15755 We could just generate a location descriptor here for all non-NULL
15756 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
15757 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
15758 where DECL_RTL is NULL or is a pseudo-reg.
15760 Note however that we can only get away with using DECL_INCOMING_RTL as
15761 a backup substitute for DECL_RTL in certain limited cases. In cases
15762 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
15763 we can be sure that the parameter was passed using the same type as it is
15764 declared to have within the function, and that its DECL_INCOMING_RTL
15765 points us to a place where a value of that type is passed.
15767 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
15768 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
15769 because in these cases DECL_INCOMING_RTL points us to a value of some
15770 type which is *different* from the type of the parameter itself. Thus,
15771 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
15772 such cases, the debugger would end up (for example) trying to fetch a
15773 `float' from a place which actually contains the first part of a
15774 `double'. That would lead to really incorrect and confusing
15775 output at debug-time.
15777 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
15778 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
15779 are a couple of exceptions however. On little-endian machines we can
15780 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
15781 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
15782 an integral type that is smaller than TREE_TYPE (decl). These cases arise
15783 when (on a little-endian machine) a non-prototyped function has a
15784 parameter declared to be of type `short' or `char'. In such cases,
15785 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
15786 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
15787 passed `int' value. If the debugger then uses that address to fetch
15788 a `short' or a `char' (on a little-endian machine) the result will be
15789 the correct data, so we allow for such exceptional cases below.
15791 Note that our goal here is to describe the place where the given formal
15792 parameter lives during most of the function's activation (i.e. between the
15793 end of the prologue and the start of the epilogue). We'll do that as best
15794 as we can. Note however that if the given formal parameter is modified
15795 sometime during the execution of the function, then a stack backtrace (at
15796 debug-time) will show the function as having been called with the *new*
15797 value rather than the value which was originally passed in. This happens
15798 rarely enough that it is not a major problem, but it *is* a problem, and
15799 I'd like to fix it.
15801 A future version of dwarf2out.c may generate two additional attributes for
15802 any given DW_TAG_formal_parameter DIE which will describe the "passed
15803 type" and the "passed location" for the given formal parameter in addition
15804 to the attributes we now generate to indicate the "declared type" and the
15805 "active location" for each parameter. This additional set of attributes
15806 could be used by debuggers for stack backtraces. Separately, note that
15807 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
15808 This happens (for example) for inlined-instances of inline function formal
15809 parameters which are never referenced. This really shouldn't be
15810 happening. All PARM_DECL nodes should get valid non-NULL
15811 DECL_INCOMING_RTL values. FIXME. */
15813 /* Use DECL_RTL as the "location" unless we find something better. */
15814 rtl = DECL_RTL_IF_SET (decl);
15816 /* When generating abstract instances, ignore everything except
15817 constants, symbols living in memory, and symbols living in
15818 fixed registers. */
15819 if (! reload_completed)
15821 if (rtl
15822 && (CONSTANT_P (rtl)
15823 || (MEM_P (rtl)
15824 && CONSTANT_P (XEXP (rtl, 0)))
15825 || (REG_P (rtl)
15826 && TREE_CODE (decl) == VAR_DECL
15827 && TREE_STATIC (decl))))
15829 rtl = targetm.delegitimize_address (rtl);
15830 return rtl;
15832 rtl = NULL_RTX;
15834 else if (TREE_CODE (decl) == PARM_DECL)
15836 if (rtl == NULL_RTX
15837 || is_pseudo_reg (rtl)
15838 || (MEM_P (rtl)
15839 && is_pseudo_reg (XEXP (rtl, 0))
15840 && DECL_INCOMING_RTL (decl)
15841 && MEM_P (DECL_INCOMING_RTL (decl))
15842 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
15844 tree declared_type = TREE_TYPE (decl);
15845 tree passed_type = DECL_ARG_TYPE (decl);
15846 machine_mode dmode = TYPE_MODE (declared_type);
15847 machine_mode pmode = TYPE_MODE (passed_type);
15849 /* This decl represents a formal parameter which was optimized out.
15850 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
15851 all cases where (rtl == NULL_RTX) just below. */
15852 if (dmode == pmode)
15853 rtl = DECL_INCOMING_RTL (decl);
15854 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
15855 && SCALAR_INT_MODE_P (dmode)
15856 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
15857 && DECL_INCOMING_RTL (decl))
15859 rtx inc = DECL_INCOMING_RTL (decl);
15860 if (REG_P (inc))
15861 rtl = inc;
15862 else if (MEM_P (inc))
15864 if (BYTES_BIG_ENDIAN)
15865 rtl = adjust_address_nv (inc, dmode,
15866 GET_MODE_SIZE (pmode)
15867 - GET_MODE_SIZE (dmode));
15868 else
15869 rtl = inc;
15874 /* If the parm was passed in registers, but lives on the stack, then
15875 make a big endian correction if the mode of the type of the
15876 parameter is not the same as the mode of the rtl. */
15877 /* ??? This is the same series of checks that are made in dbxout.c before
15878 we reach the big endian correction code there. It isn't clear if all
15879 of these checks are necessary here, but keeping them all is the safe
15880 thing to do. */
15881 else if (MEM_P (rtl)
15882 && XEXP (rtl, 0) != const0_rtx
15883 && ! CONSTANT_P (XEXP (rtl, 0))
15884 /* Not passed in memory. */
15885 && !MEM_P (DECL_INCOMING_RTL (decl))
15886 /* Not passed by invisible reference. */
15887 && (!REG_P (XEXP (rtl, 0))
15888 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
15889 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
15890 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
15891 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
15892 #endif
15894 /* Big endian correction check. */
15895 && BYTES_BIG_ENDIAN
15896 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
15897 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
15898 < UNITS_PER_WORD))
15900 machine_mode addr_mode = get_address_mode (rtl);
15901 int offset = (UNITS_PER_WORD
15902 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
15904 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15905 plus_constant (addr_mode, XEXP (rtl, 0), offset));
15908 else if (TREE_CODE (decl) == VAR_DECL
15909 && rtl
15910 && MEM_P (rtl)
15911 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
15912 && BYTES_BIG_ENDIAN)
15914 machine_mode addr_mode = get_address_mode (rtl);
15915 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
15916 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
15918 /* If a variable is declared "register" yet is smaller than
15919 a register, then if we store the variable to memory, it
15920 looks like we're storing a register-sized value, when in
15921 fact we are not. We need to adjust the offset of the
15922 storage location to reflect the actual value's bytes,
15923 else gdb will not be able to display it. */
15924 if (rsize > dsize)
15925 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15926 plus_constant (addr_mode, XEXP (rtl, 0),
15927 rsize - dsize));
15930 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
15931 and will have been substituted directly into all expressions that use it.
15932 C does not have such a concept, but C++ and other languages do. */
15933 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
15934 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
15936 if (rtl)
15937 rtl = targetm.delegitimize_address (rtl);
15939 /* If we don't look past the constant pool, we risk emitting a
15940 reference to a constant pool entry that isn't referenced from
15941 code, and thus is not emitted. */
15942 if (rtl)
15943 rtl = avoid_constant_pool_reference (rtl);
15945 /* Try harder to get a rtl. If this symbol ends up not being emitted
15946 in the current CU, resolve_addr will remove the expression referencing
15947 it. */
15948 if (rtl == NULL_RTX
15949 && TREE_CODE (decl) == VAR_DECL
15950 && !DECL_EXTERNAL (decl)
15951 && TREE_STATIC (decl)
15952 && DECL_NAME (decl)
15953 && !DECL_HARD_REGISTER (decl)
15954 && DECL_MODE (decl) != VOIDmode)
15956 rtl = make_decl_rtl_for_debug (decl);
15957 if (!MEM_P (rtl)
15958 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
15959 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
15960 rtl = NULL_RTX;
15963 return rtl;
15966 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
15967 returned. If so, the decl for the COMMON block is returned, and the
15968 value is the offset into the common block for the symbol. */
15970 static tree
15971 fortran_common (tree decl, HOST_WIDE_INT *value)
15973 tree val_expr, cvar;
15974 machine_mode mode;
15975 HOST_WIDE_INT bitsize, bitpos;
15976 tree offset;
15977 int unsignedp, volatilep = 0;
15979 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
15980 it does not have a value (the offset into the common area), or if it
15981 is thread local (as opposed to global) then it isn't common, and shouldn't
15982 be handled as such. */
15983 if (TREE_CODE (decl) != VAR_DECL
15984 || !TREE_STATIC (decl)
15985 || !DECL_HAS_VALUE_EXPR_P (decl)
15986 || !is_fortran ())
15987 return NULL_TREE;
15989 val_expr = DECL_VALUE_EXPR (decl);
15990 if (TREE_CODE (val_expr) != COMPONENT_REF)
15991 return NULL_TREE;
15993 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
15994 &mode, &unsignedp, &volatilep, true);
15996 if (cvar == NULL_TREE
15997 || TREE_CODE (cvar) != VAR_DECL
15998 || DECL_ARTIFICIAL (cvar)
15999 || !TREE_PUBLIC (cvar))
16000 return NULL_TREE;
16002 *value = 0;
16003 if (offset != NULL)
16005 if (!tree_fits_shwi_p (offset))
16006 return NULL_TREE;
16007 *value = tree_to_shwi (offset);
16009 if (bitpos != 0)
16010 *value += bitpos / BITS_PER_UNIT;
16012 return cvar;
16015 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
16016 data attribute for a variable or a parameter. We generate the
16017 DW_AT_const_value attribute only in those cases where the given variable
16018 or parameter does not have a true "location" either in memory or in a
16019 register. This can happen (for example) when a constant is passed as an
16020 actual argument in a call to an inline function. (It's possible that
16021 these things can crop up in other ways also.) Note that one type of
16022 constant value which can be passed into an inlined function is a constant
16023 pointer. This can happen for example if an actual argument in an inlined
16024 function call evaluates to a compile-time constant address.
16026 CACHE_P is true if it is worth caching the location list for DECL,
16027 so that future calls can reuse it rather than regenerate it from scratch.
16028 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
16029 since we will need to refer to them each time the function is inlined. */
16031 static bool
16032 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
16033 enum dwarf_attribute attr)
16035 rtx rtl;
16036 dw_loc_list_ref list;
16037 var_loc_list *loc_list;
16038 cached_dw_loc_list *cache;
16040 if (TREE_CODE (decl) == ERROR_MARK)
16041 return false;
16043 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
16044 || TREE_CODE (decl) == RESULT_DECL);
16046 /* Try to get some constant RTL for this decl, and use that as the value of
16047 the location. */
16049 rtl = rtl_for_decl_location (decl);
16050 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16051 && add_const_value_attribute (die, rtl))
16052 return true;
16054 /* See if we have single element location list that is equivalent to
16055 a constant value. That way we are better to use add_const_value_attribute
16056 rather than expanding constant value equivalent. */
16057 loc_list = lookup_decl_loc (decl);
16058 if (loc_list
16059 && loc_list->first
16060 && loc_list->first->next == NULL
16061 && NOTE_P (loc_list->first->loc)
16062 && NOTE_VAR_LOCATION (loc_list->first->loc)
16063 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
16065 struct var_loc_node *node;
16067 node = loc_list->first;
16068 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
16069 if (GET_CODE (rtl) == EXPR_LIST)
16070 rtl = XEXP (rtl, 0);
16071 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16072 && add_const_value_attribute (die, rtl))
16073 return true;
16075 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
16076 list several times. See if we've already cached the contents. */
16077 list = NULL;
16078 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
16079 cache_p = false;
16080 if (cache_p)
16082 cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
16083 if (cache)
16084 list = cache->loc_list;
16086 if (list == NULL)
16088 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
16089 NULL);
16090 /* It is usually worth caching this result if the decl is from
16091 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
16092 if (cache_p && list && list->dw_loc_next)
16094 cached_dw_loc_list **slot
16095 = cached_dw_loc_list_table->find_slot_with_hash (decl,
16096 DECL_UID (decl),
16097 INSERT);
16098 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
16099 cache->decl_id = DECL_UID (decl);
16100 cache->loc_list = list;
16101 *slot = cache;
16104 if (list)
16106 add_AT_location_description (die, attr, list);
16107 return true;
16109 /* None of that worked, so it must not really have a location;
16110 try adding a constant value attribute from the DECL_INITIAL. */
16111 return tree_add_const_value_attribute_for_decl (die, decl);
16114 /* Add VARIABLE and DIE into deferred locations list. */
16116 static void
16117 defer_location (tree variable, dw_die_ref die)
16119 deferred_locations entry;
16120 entry.variable = variable;
16121 entry.die = die;
16122 vec_safe_push (deferred_locations_list, entry);
16125 /* Helper function for tree_add_const_value_attribute. Natively encode
16126 initializer INIT into an array. Return true if successful. */
16128 static bool
16129 native_encode_initializer (tree init, unsigned char *array, int size)
16131 tree type;
16133 if (init == NULL_TREE)
16134 return false;
16136 STRIP_NOPS (init);
16137 switch (TREE_CODE (init))
16139 case STRING_CST:
16140 type = TREE_TYPE (init);
16141 if (TREE_CODE (type) == ARRAY_TYPE)
16143 tree enttype = TREE_TYPE (type);
16144 machine_mode mode = TYPE_MODE (enttype);
16146 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
16147 return false;
16148 if (int_size_in_bytes (type) != size)
16149 return false;
16150 if (size > TREE_STRING_LENGTH (init))
16152 memcpy (array, TREE_STRING_POINTER (init),
16153 TREE_STRING_LENGTH (init));
16154 memset (array + TREE_STRING_LENGTH (init),
16155 '\0', size - TREE_STRING_LENGTH (init));
16157 else
16158 memcpy (array, TREE_STRING_POINTER (init), size);
16159 return true;
16161 return false;
16162 case CONSTRUCTOR:
16163 type = TREE_TYPE (init);
16164 if (int_size_in_bytes (type) != size)
16165 return false;
16166 if (TREE_CODE (type) == ARRAY_TYPE)
16168 HOST_WIDE_INT min_index;
16169 unsigned HOST_WIDE_INT cnt;
16170 int curpos = 0, fieldsize;
16171 constructor_elt *ce;
16173 if (TYPE_DOMAIN (type) == NULL_TREE
16174 || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
16175 return false;
16177 fieldsize = int_size_in_bytes (TREE_TYPE (type));
16178 if (fieldsize <= 0)
16179 return false;
16181 min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
16182 memset (array, '\0', size);
16183 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
16185 tree val = ce->value;
16186 tree index = ce->index;
16187 int pos = curpos;
16188 if (index && TREE_CODE (index) == RANGE_EXPR)
16189 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
16190 * fieldsize;
16191 else if (index)
16192 pos = (tree_to_shwi (index) - min_index) * fieldsize;
16194 if (val)
16196 STRIP_NOPS (val);
16197 if (!native_encode_initializer (val, array + pos, fieldsize))
16198 return false;
16200 curpos = pos + fieldsize;
16201 if (index && TREE_CODE (index) == RANGE_EXPR)
16203 int count = tree_to_shwi (TREE_OPERAND (index, 1))
16204 - tree_to_shwi (TREE_OPERAND (index, 0));
16205 while (count-- > 0)
16207 if (val)
16208 memcpy (array + curpos, array + pos, fieldsize);
16209 curpos += fieldsize;
16212 gcc_assert (curpos <= size);
16214 return true;
16216 else if (TREE_CODE (type) == RECORD_TYPE
16217 || TREE_CODE (type) == UNION_TYPE)
16219 tree field = NULL_TREE;
16220 unsigned HOST_WIDE_INT cnt;
16221 constructor_elt *ce;
16223 if (int_size_in_bytes (type) != size)
16224 return false;
16226 if (TREE_CODE (type) == RECORD_TYPE)
16227 field = TYPE_FIELDS (type);
16229 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
16231 tree val = ce->value;
16232 int pos, fieldsize;
16234 if (ce->index != 0)
16235 field = ce->index;
16237 if (val)
16238 STRIP_NOPS (val);
16240 if (field == NULL_TREE || DECL_BIT_FIELD (field))
16241 return false;
16243 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
16244 && TYPE_DOMAIN (TREE_TYPE (field))
16245 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
16246 return false;
16247 else if (DECL_SIZE_UNIT (field) == NULL_TREE
16248 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
16249 return false;
16250 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
16251 pos = int_byte_position (field);
16252 gcc_assert (pos + fieldsize <= size);
16253 if (val
16254 && !native_encode_initializer (val, array + pos, fieldsize))
16255 return false;
16257 return true;
16259 return false;
16260 case VIEW_CONVERT_EXPR:
16261 case NON_LVALUE_EXPR:
16262 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
16263 default:
16264 return native_encode_expr (init, array, size) == size;
16268 /* Attach a DW_AT_const_value attribute to DIE. The value of the
16269 attribute is the const value T. */
16271 static bool
16272 tree_add_const_value_attribute (dw_die_ref die, tree t)
16274 tree init;
16275 tree type = TREE_TYPE (t);
16276 rtx rtl;
16278 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
16279 return false;
16281 init = t;
16282 gcc_assert (!DECL_P (init));
16284 rtl = rtl_for_decl_init (init, type);
16285 if (rtl)
16286 return add_const_value_attribute (die, rtl);
16287 /* If the host and target are sane, try harder. */
16288 else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
16289 && initializer_constant_valid_p (init, type))
16291 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
16292 if (size > 0 && (int) size == size)
16294 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
16296 if (native_encode_initializer (init, array, size))
16298 add_AT_vec (die, DW_AT_const_value, size, 1, array);
16299 return true;
16301 ggc_free (array);
16304 return false;
16307 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
16308 attribute is the const value of T, where T is an integral constant
16309 variable with static storage duration
16310 (so it can't be a PARM_DECL or a RESULT_DECL). */
16312 static bool
16313 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
16316 if (!decl
16317 || (TREE_CODE (decl) != VAR_DECL
16318 && TREE_CODE (decl) != CONST_DECL)
16319 || (TREE_CODE (decl) == VAR_DECL
16320 && !TREE_STATIC (decl)))
16321 return false;
16323 if (TREE_READONLY (decl)
16324 && ! TREE_THIS_VOLATILE (decl)
16325 && DECL_INITIAL (decl))
16326 /* OK */;
16327 else
16328 return false;
16330 /* Don't add DW_AT_const_value if abstract origin already has one. */
16331 if (get_AT (var_die, DW_AT_const_value))
16332 return false;
16334 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
16337 /* Convert the CFI instructions for the current function into a
16338 location list. This is used for DW_AT_frame_base when we targeting
16339 a dwarf2 consumer that does not support the dwarf3
16340 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
16341 expressions. */
16343 static dw_loc_list_ref
16344 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
16346 int ix;
16347 dw_fde_ref fde;
16348 dw_loc_list_ref list, *list_tail;
16349 dw_cfi_ref cfi;
16350 dw_cfa_location last_cfa, next_cfa;
16351 const char *start_label, *last_label, *section;
16352 dw_cfa_location remember;
16354 fde = cfun->fde;
16355 gcc_assert (fde != NULL);
16357 section = secname_for_decl (current_function_decl);
16358 list_tail = &list;
16359 list = NULL;
16361 memset (&next_cfa, 0, sizeof (next_cfa));
16362 next_cfa.reg = INVALID_REGNUM;
16363 remember = next_cfa;
16365 start_label = fde->dw_fde_begin;
16367 /* ??? Bald assumption that the CIE opcode list does not contain
16368 advance opcodes. */
16369 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
16370 lookup_cfa_1 (cfi, &next_cfa, &remember);
16372 last_cfa = next_cfa;
16373 last_label = start_label;
16375 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
16377 /* If the first partition contained no CFI adjustments, the
16378 CIE opcodes apply to the whole first partition. */
16379 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16380 fde->dw_fde_begin, fde->dw_fde_end, section);
16381 list_tail =&(*list_tail)->dw_loc_next;
16382 start_label = last_label = fde->dw_fde_second_begin;
16385 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
16387 switch (cfi->dw_cfi_opc)
16389 case DW_CFA_set_loc:
16390 case DW_CFA_advance_loc1:
16391 case DW_CFA_advance_loc2:
16392 case DW_CFA_advance_loc4:
16393 if (!cfa_equal_p (&last_cfa, &next_cfa))
16395 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16396 start_label, last_label, section);
16398 list_tail = &(*list_tail)->dw_loc_next;
16399 last_cfa = next_cfa;
16400 start_label = last_label;
16402 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
16403 break;
16405 case DW_CFA_advance_loc:
16406 /* The encoding is complex enough that we should never emit this. */
16407 gcc_unreachable ();
16409 default:
16410 lookup_cfa_1 (cfi, &next_cfa, &remember);
16411 break;
16413 if (ix + 1 == fde->dw_fde_switch_cfi_index)
16415 if (!cfa_equal_p (&last_cfa, &next_cfa))
16417 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16418 start_label, last_label, section);
16420 list_tail = &(*list_tail)->dw_loc_next;
16421 last_cfa = next_cfa;
16422 start_label = last_label;
16424 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16425 start_label, fde->dw_fde_end, section);
16426 list_tail = &(*list_tail)->dw_loc_next;
16427 start_label = last_label = fde->dw_fde_second_begin;
16431 if (!cfa_equal_p (&last_cfa, &next_cfa))
16433 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16434 start_label, last_label, section);
16435 list_tail = &(*list_tail)->dw_loc_next;
16436 start_label = last_label;
16439 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
16440 start_label,
16441 fde->dw_fde_second_begin
16442 ? fde->dw_fde_second_end : fde->dw_fde_end,
16443 section);
16445 if (list && list->dw_loc_next)
16446 gen_llsym (list);
16448 return list;
16451 /* Compute a displacement from the "steady-state frame pointer" to the
16452 frame base (often the same as the CFA), and store it in
16453 frame_pointer_fb_offset. OFFSET is added to the displacement
16454 before the latter is negated. */
16456 static void
16457 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
16459 rtx reg, elim;
16461 #ifdef FRAME_POINTER_CFA_OFFSET
16462 reg = frame_pointer_rtx;
16463 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
16464 #else
16465 reg = arg_pointer_rtx;
16466 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
16467 #endif
16469 elim = (ira_use_lra_p
16470 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
16471 : eliminate_regs (reg, VOIDmode, NULL_RTX));
16472 if (GET_CODE (elim) == PLUS)
16474 offset += INTVAL (XEXP (elim, 1));
16475 elim = XEXP (elim, 0);
16478 frame_pointer_fb_offset = -offset;
16480 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
16481 in which to eliminate. This is because it's stack pointer isn't
16482 directly accessible as a register within the ISA. To work around
16483 this, assume that while we cannot provide a proper value for
16484 frame_pointer_fb_offset, we won't need one either. */
16485 frame_pointer_fb_offset_valid
16486 = ((SUPPORTS_STACK_ALIGNMENT
16487 && (elim == hard_frame_pointer_rtx
16488 || elim == stack_pointer_rtx))
16489 || elim == (frame_pointer_needed
16490 ? hard_frame_pointer_rtx
16491 : stack_pointer_rtx));
16494 /* Generate a DW_AT_name attribute given some string value to be included as
16495 the value of the attribute. */
16497 static void
16498 add_name_attribute (dw_die_ref die, const char *name_string)
16500 if (name_string != NULL && *name_string != 0)
16502 if (demangle_name_func)
16503 name_string = (*demangle_name_func) (name_string);
16505 add_AT_string (die, DW_AT_name, name_string);
16509 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
16510 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
16511 of TYPE accordingly.
16513 ??? This is a temporary measure until after we're able to generate
16514 regular DWARF for the complex Ada type system. */
16516 static void
16517 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
16518 dw_die_ref context_die)
16520 tree dtype;
16521 dw_die_ref dtype_die;
16523 if (!lang_hooks.types.descriptive_type)
16524 return;
16526 dtype = lang_hooks.types.descriptive_type (type);
16527 if (!dtype)
16528 return;
16530 dtype_die = lookup_type_die (dtype);
16531 if (!dtype_die)
16533 gen_type_die (dtype, context_die);
16534 dtype_die = lookup_type_die (dtype);
16535 gcc_assert (dtype_die);
16538 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
16541 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
16543 static const char *
16544 comp_dir_string (void)
16546 const char *wd;
16547 char *wd1;
16548 static const char *cached_wd = NULL;
16550 if (cached_wd != NULL)
16551 return cached_wd;
16553 wd = get_src_pwd ();
16554 if (wd == NULL)
16555 return NULL;
16557 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
16559 int wdlen;
16561 wdlen = strlen (wd);
16562 wd1 = ggc_vec_alloc<char> (wdlen + 2);
16563 strcpy (wd1, wd);
16564 wd1 [wdlen] = DIR_SEPARATOR;
16565 wd1 [wdlen + 1] = 0;
16566 wd = wd1;
16569 cached_wd = remap_debug_filename (wd);
16570 return cached_wd;
16573 /* Generate a DW_AT_comp_dir attribute for DIE. */
16575 static void
16576 add_comp_dir_attribute (dw_die_ref die)
16578 const char * wd = comp_dir_string ();
16579 if (wd != NULL)
16580 add_AT_string (die, DW_AT_comp_dir, wd);
16583 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
16584 pointer computation, ...), output a representation for that bound according
16585 to the accepted FORMS (see enum dw_scalar_form) and add it to DIE. See
16586 loc_list_from_tree for the meaning of CONTEXT. */
16588 static void
16589 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
16590 int forms, const struct loc_descr_context *context)
16592 dw_die_ref ctx, decl_die;
16593 dw_loc_list_ref list;
16595 bool strip_conversions = true;
16597 while (strip_conversions)
16598 switch (TREE_CODE (value))
16600 case ERROR_MARK:
16601 case SAVE_EXPR:
16602 return;
16604 CASE_CONVERT:
16605 case VIEW_CONVERT_EXPR:
16606 value = TREE_OPERAND (value, 0);
16607 break;
16609 default:
16610 strip_conversions = false;
16611 break;
16614 /* If possible and permitted, output the attribute as a constant. */
16615 if ((forms & dw_scalar_form_constant) != 0
16616 && TREE_CODE (value) == INTEGER_CST)
16618 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
16620 /* If HOST_WIDE_INT is big enough then represent the bound as
16621 a constant value. We need to choose a form based on
16622 whether the type is signed or unsigned. We cannot just
16623 call add_AT_unsigned if the value itself is positive
16624 (add_AT_unsigned might add the unsigned value encoded as
16625 DW_FORM_data[1248]). Some DWARF consumers will lookup the
16626 bounds type and then sign extend any unsigned values found
16627 for signed types. This is needed only for
16628 DW_AT_{lower,upper}_bound, since for most other attributes,
16629 consumers will treat DW_FORM_data[1248] as unsigned values,
16630 regardless of the underlying type. */
16631 if (prec <= HOST_BITS_PER_WIDE_INT
16632 || tree_fits_uhwi_p (value))
16634 if (TYPE_UNSIGNED (TREE_TYPE (value)))
16635 add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
16636 else
16637 add_AT_int (die, attr, TREE_INT_CST_LOW (value));
16639 else
16640 /* Otherwise represent the bound as an unsigned value with
16641 the precision of its type. The precision and signedness
16642 of the type will be necessary to re-interpret it
16643 unambiguously. */
16644 add_AT_wide (die, attr, value);
16645 return;
16648 /* Otherwise, if it's possible and permitted too, output a reference to
16649 another DIE. */
16650 if ((forms & dw_scalar_form_reference) != 0)
16652 tree decl = NULL_TREE;
16654 /* Some type attributes reference an outer type. For instance, the upper
16655 bound of an array may reference an embedding record (this happens in
16656 Ada). */
16657 if (TREE_CODE (value) == COMPONENT_REF
16658 && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
16659 && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
16660 decl = TREE_OPERAND (value, 1);
16662 else if (TREE_CODE (value) == VAR_DECL
16663 || TREE_CODE (value) == PARM_DECL
16664 || TREE_CODE (value) == RESULT_DECL)
16665 decl = value;
16667 if (decl != NULL_TREE)
16669 dw_die_ref decl_die = lookup_decl_die (decl);
16671 /* ??? Can this happen, or should the variable have been bound
16672 first? Probably it can, since I imagine that we try to create
16673 the types of parameters in the order in which they exist in
16674 the list, and won't have created a forward reference to a
16675 later parameter. */
16676 if (decl_die != NULL)
16678 add_AT_die_ref (die, attr, decl_die);
16679 return;
16684 /* Last chance: try to create a stack operation procedure to evaluate the
16685 value. Do nothing if even that is not possible or permitted. */
16686 if ((forms & dw_scalar_form_exprloc) == 0)
16687 return;
16689 list = loc_list_from_tree (value, 2, context);
16690 if (list == NULL || single_element_loc_list_p (list))
16692 /* If this attribute is not a reference nor constant, it is
16693 a DWARF expression rather than location description. For that
16694 loc_list_from_tree (value, 0, &context) is needed. */
16695 dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
16696 if (list2 && single_element_loc_list_p (list2))
16698 add_AT_loc (die, attr, list2->expr);
16699 return;
16703 /* If that failed to give a single element location list, fall back to
16704 outputting this as a reference... still if permitted. */
16705 if (list == NULL || (forms & dw_scalar_form_reference) == 0)
16706 return;
16708 if (current_function_decl == 0)
16709 ctx = comp_unit_die ();
16710 else
16711 ctx = lookup_decl_die (current_function_decl);
16713 decl_die = new_die (DW_TAG_variable, ctx, value);
16714 add_AT_flag (decl_die, DW_AT_artificial, 1);
16715 add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, ctx);
16716 add_AT_location_description (decl_die, DW_AT_location, list);
16717 add_AT_die_ref (die, attr, decl_die);
16720 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
16721 default. */
16723 static int
16724 lower_bound_default (void)
16726 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
16728 case DW_LANG_C:
16729 case DW_LANG_C89:
16730 case DW_LANG_C99:
16731 case DW_LANG_C11:
16732 case DW_LANG_C_plus_plus:
16733 case DW_LANG_C_plus_plus_11:
16734 case DW_LANG_C_plus_plus_14:
16735 case DW_LANG_ObjC:
16736 case DW_LANG_ObjC_plus_plus:
16737 case DW_LANG_Java:
16738 return 0;
16739 case DW_LANG_Fortran77:
16740 case DW_LANG_Fortran90:
16741 case DW_LANG_Fortran95:
16742 case DW_LANG_Fortran03:
16743 case DW_LANG_Fortran08:
16744 return 1;
16745 case DW_LANG_UPC:
16746 case DW_LANG_D:
16747 case DW_LANG_Python:
16748 return dwarf_version >= 4 ? 0 : -1;
16749 case DW_LANG_Ada95:
16750 case DW_LANG_Ada83:
16751 case DW_LANG_Cobol74:
16752 case DW_LANG_Cobol85:
16753 case DW_LANG_Pascal83:
16754 case DW_LANG_Modula2:
16755 case DW_LANG_PLI:
16756 return dwarf_version >= 4 ? 1 : -1;
16757 default:
16758 return -1;
16762 /* Given a tree node describing an array bound (either lower or upper) output
16763 a representation for that bound. */
16765 static void
16766 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
16767 tree bound, const struct loc_descr_context *context)
16769 int dflt;
16771 while (1)
16772 switch (TREE_CODE (bound))
16774 /* Strip all conversions. */
16775 CASE_CONVERT:
16776 case VIEW_CONVERT_EXPR:
16777 bound = TREE_OPERAND (bound, 0);
16778 break;
16780 /* All fixed-bounds are represented by INTEGER_CST nodes. Lower bounds
16781 are even omitted when they are the default. */
16782 case INTEGER_CST:
16783 /* If the value for this bound is the default one, we can even omit the
16784 attribute. */
16785 if (bound_attr == DW_AT_lower_bound
16786 && tree_fits_shwi_p (bound)
16787 && (dflt = lower_bound_default ()) != -1
16788 && tree_to_shwi (bound) == dflt)
16789 return;
16791 /* FALLTHRU */
16793 default:
16794 add_scalar_info (subrange_die, bound_attr, bound,
16795 dw_scalar_form_constant
16796 | dw_scalar_form_exprloc
16797 | dw_scalar_form_reference,
16798 context);
16799 return;
16803 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
16804 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
16805 Note that the block of subscript information for an array type also
16806 includes information about the element type of the given array type. */
16808 static void
16809 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
16811 unsigned dimension_number;
16812 tree lower, upper;
16813 dw_die_ref subrange_die;
16815 for (dimension_number = 0;
16816 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
16817 type = TREE_TYPE (type), dimension_number++)
16819 tree domain = TYPE_DOMAIN (type);
16821 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
16822 break;
16824 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
16825 and (in GNU C only) variable bounds. Handle all three forms
16826 here. */
16827 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
16828 if (domain)
16830 /* We have an array type with specified bounds. */
16831 lower = TYPE_MIN_VALUE (domain);
16832 upper = TYPE_MAX_VALUE (domain);
16834 /* Define the index type. */
16835 if (TREE_TYPE (domain))
16837 /* ??? This is probably an Ada unnamed subrange type. Ignore the
16838 TREE_TYPE field. We can't emit debug info for this
16839 because it is an unnamed integral type. */
16840 if (TREE_CODE (domain) == INTEGER_TYPE
16841 && TYPE_NAME (domain) == NULL_TREE
16842 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
16843 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
16845 else
16846 add_type_attribute (subrange_die, TREE_TYPE (domain),
16847 TYPE_UNQUALIFIED, type_die);
16850 /* ??? If upper is NULL, the array has unspecified length,
16851 but it does have a lower bound. This happens with Fortran
16852 dimension arr(N:*)
16853 Since the debugger is definitely going to need to know N
16854 to produce useful results, go ahead and output the lower
16855 bound solo, and hope the debugger can cope. */
16857 add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
16858 if (upper)
16859 add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
16862 /* Otherwise we have an array type with an unspecified length. The
16863 DWARF-2 spec does not say how to handle this; let's just leave out the
16864 bounds. */
16868 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
16870 static void
16871 add_byte_size_attribute (dw_die_ref die, tree tree_node)
16873 dw_die_ref decl_die;
16874 HOST_WIDE_INT size;
16876 switch (TREE_CODE (tree_node))
16878 case ERROR_MARK:
16879 size = 0;
16880 break;
16881 case ENUMERAL_TYPE:
16882 case RECORD_TYPE:
16883 case UNION_TYPE:
16884 case QUAL_UNION_TYPE:
16885 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
16886 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
16888 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
16889 return;
16891 size = int_size_in_bytes (tree_node);
16892 break;
16893 case FIELD_DECL:
16894 /* For a data member of a struct or union, the DW_AT_byte_size is
16895 generally given as the number of bytes normally allocated for an
16896 object of the *declared* type of the member itself. This is true
16897 even for bit-fields. */
16898 size = int_size_in_bytes (field_type (tree_node));
16899 break;
16900 default:
16901 gcc_unreachable ();
16904 /* Note that `size' might be -1 when we get to this point. If it is, that
16905 indicates that the byte size of the entity in question is variable. We
16906 have no good way of expressing this fact in Dwarf at the present time,
16907 when location description was not used by the caller code instead. */
16908 if (size >= 0)
16909 add_AT_unsigned (die, DW_AT_byte_size, size);
16912 /* For a FIELD_DECL node which represents a bit-field, output an attribute
16913 which specifies the distance in bits from the highest order bit of the
16914 "containing object" for the bit-field to the highest order bit of the
16915 bit-field itself.
16917 For any given bit-field, the "containing object" is a hypothetical object
16918 (of some integral or enum type) within which the given bit-field lives. The
16919 type of this hypothetical "containing object" is always the same as the
16920 declared type of the individual bit-field itself. The determination of the
16921 exact location of the "containing object" for a bit-field is rather
16922 complicated. It's handled by the `field_byte_offset' function (above).
16924 Note that it is the size (in bytes) of the hypothetical "containing object"
16925 which will be given in the DW_AT_byte_size attribute for this bit-field.
16926 (See `byte_size_attribute' above). */
16928 static inline void
16929 add_bit_offset_attribute (dw_die_ref die, tree decl)
16931 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
16932 tree type = DECL_BIT_FIELD_TYPE (decl);
16933 HOST_WIDE_INT bitpos_int;
16934 HOST_WIDE_INT highest_order_object_bit_offset;
16935 HOST_WIDE_INT highest_order_field_bit_offset;
16936 HOST_WIDE_INT bit_offset;
16938 /* Must be a field and a bit field. */
16939 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
16941 /* We can't yet handle bit-fields whose offsets are variable, so if we
16942 encounter such things, just return without generating any attribute
16943 whatsoever. Likewise for variable or too large size. */
16944 if (! tree_fits_shwi_p (bit_position (decl))
16945 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
16946 return;
16948 bitpos_int = int_bit_position (decl);
16950 /* Note that the bit offset is always the distance (in bits) from the
16951 highest-order bit of the "containing object" to the highest-order bit of
16952 the bit-field itself. Since the "high-order end" of any object or field
16953 is different on big-endian and little-endian machines, the computation
16954 below must take account of these differences. */
16955 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
16956 highest_order_field_bit_offset = bitpos_int;
16958 if (! BYTES_BIG_ENDIAN)
16960 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
16961 highest_order_object_bit_offset += simple_type_size_in_bits (type);
16964 bit_offset
16965 = (! BYTES_BIG_ENDIAN
16966 ? highest_order_object_bit_offset - highest_order_field_bit_offset
16967 : highest_order_field_bit_offset - highest_order_object_bit_offset);
16969 if (bit_offset < 0)
16970 add_AT_int (die, DW_AT_bit_offset, bit_offset);
16971 else
16972 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
16975 /* For a FIELD_DECL node which represents a bit field, output an attribute
16976 which specifies the length in bits of the given field. */
16978 static inline void
16979 add_bit_size_attribute (dw_die_ref die, tree decl)
16981 /* Must be a field and a bit field. */
16982 gcc_assert (TREE_CODE (decl) == FIELD_DECL
16983 && DECL_BIT_FIELD_TYPE (decl));
16985 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
16986 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
16989 /* If the compiled language is ANSI C, then add a 'prototyped'
16990 attribute, if arg types are given for the parameters of a function. */
16992 static inline void
16993 add_prototyped_attribute (dw_die_ref die, tree func_type)
16995 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
16997 case DW_LANG_C:
16998 case DW_LANG_C89:
16999 case DW_LANG_C99:
17000 case DW_LANG_C11:
17001 case DW_LANG_ObjC:
17002 if (prototype_p (func_type))
17003 add_AT_flag (die, DW_AT_prototyped, 1);
17004 break;
17005 default:
17006 break;
17010 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
17011 by looking in either the type declaration or object declaration
17012 equate table. */
17014 static inline dw_die_ref
17015 add_abstract_origin_attribute (dw_die_ref die, tree origin)
17017 dw_die_ref origin_die = NULL;
17019 if (TREE_CODE (origin) != FUNCTION_DECL)
17021 /* We may have gotten separated from the block for the inlined
17022 function, if we're in an exception handler or some such; make
17023 sure that the abstract function has been written out.
17025 Doing this for nested functions is wrong, however; functions are
17026 distinct units, and our context might not even be inline. */
17027 tree fn = origin;
17029 if (TYPE_P (fn))
17030 fn = TYPE_STUB_DECL (fn);
17032 fn = decl_function_context (fn);
17033 if (fn)
17034 dwarf2out_abstract_function (fn);
17037 if (DECL_P (origin))
17038 origin_die = lookup_decl_die (origin);
17039 else if (TYPE_P (origin))
17040 origin_die = lookup_type_die (origin);
17042 /* XXX: Functions that are never lowered don't always have correct block
17043 trees (in the case of java, they simply have no block tree, in some other
17044 languages). For these functions, there is nothing we can really do to
17045 output correct debug info for inlined functions in all cases. Rather
17046 than die, we'll just produce deficient debug info now, in that we will
17047 have variables without a proper abstract origin. In the future, when all
17048 functions are lowered, we should re-add a gcc_assert (origin_die)
17049 here. */
17051 if (origin_die)
17052 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
17053 return origin_die;
17056 /* We do not currently support the pure_virtual attribute. */
17058 static inline void
17059 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
17061 if (DECL_VINDEX (func_decl))
17063 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
17065 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
17066 add_AT_loc (die, DW_AT_vtable_elem_location,
17067 new_loc_descr (DW_OP_constu,
17068 tree_to_shwi (DECL_VINDEX (func_decl)),
17069 0));
17071 /* GNU extension: Record what type this method came from originally. */
17072 if (debug_info_level > DINFO_LEVEL_TERSE
17073 && DECL_CONTEXT (func_decl))
17074 add_AT_die_ref (die, DW_AT_containing_type,
17075 lookup_type_die (DECL_CONTEXT (func_decl)));
17079 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
17080 given decl. This used to be a vendor extension until after DWARF 4
17081 standardized it. */
17083 static void
17084 add_linkage_attr (dw_die_ref die, tree decl)
17086 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
17088 /* Mimic what assemble_name_raw does with a leading '*'. */
17089 if (name[0] == '*')
17090 name = &name[1];
17092 if (dwarf_version >= 4)
17093 add_AT_string (die, DW_AT_linkage_name, name);
17094 else
17095 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
17098 /* Add source coordinate attributes for the given decl. */
17100 static void
17101 add_src_coords_attributes (dw_die_ref die, tree decl)
17103 expanded_location s;
17105 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
17106 return;
17107 s = expand_location (DECL_SOURCE_LOCATION (decl));
17108 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
17109 add_AT_unsigned (die, DW_AT_decl_line, s.line);
17112 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
17114 static void
17115 add_linkage_name (dw_die_ref die, tree decl)
17117 if (debug_info_level > DINFO_LEVEL_NONE
17118 && (TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
17119 && TREE_PUBLIC (decl)
17120 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
17121 && die->die_tag != DW_TAG_member)
17123 /* Defer until we have an assembler name set. */
17124 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
17126 limbo_die_node *asm_name;
17128 asm_name = ggc_cleared_alloc<limbo_die_node> ();
17129 asm_name->die = die;
17130 asm_name->created_for = decl;
17131 asm_name->next = deferred_asm_name;
17132 deferred_asm_name = asm_name;
17134 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
17135 add_linkage_attr (die, decl);
17139 /* Add a DW_AT_name attribute and source coordinate attribute for the
17140 given decl, but only if it actually has a name. */
17142 static void
17143 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
17145 tree decl_name;
17147 decl_name = DECL_NAME (decl);
17148 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
17150 const char *name = dwarf2_name (decl, 0);
17151 if (name)
17152 add_name_attribute (die, name);
17153 if (! DECL_ARTIFICIAL (decl))
17154 add_src_coords_attributes (die, decl);
17156 add_linkage_name (die, decl);
17159 #ifdef VMS_DEBUGGING_INFO
17160 /* Get the function's name, as described by its RTL. This may be different
17161 from the DECL_NAME name used in the source file. */
17162 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
17164 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
17165 XEXP (DECL_RTL (decl), 0), false);
17166 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
17168 #endif /* VMS_DEBUGGING_INFO */
17171 #ifdef VMS_DEBUGGING_INFO
17172 /* Output the debug main pointer die for VMS */
17174 void
17175 dwarf2out_vms_debug_main_pointer (void)
17177 char label[MAX_ARTIFICIAL_LABEL_BYTES];
17178 dw_die_ref die;
17180 /* Allocate the VMS debug main subprogram die. */
17181 die = ggc_cleared_alloc<die_node> ();
17182 die->die_tag = DW_TAG_subprogram;
17183 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
17184 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
17185 current_function_funcdef_no);
17186 add_AT_lbl_id (die, DW_AT_entry_pc, label);
17188 /* Make it the first child of comp_unit_die (). */
17189 die->die_parent = comp_unit_die ();
17190 if (comp_unit_die ()->die_child)
17192 die->die_sib = comp_unit_die ()->die_child->die_sib;
17193 comp_unit_die ()->die_child->die_sib = die;
17195 else
17197 die->die_sib = die;
17198 comp_unit_die ()->die_child = die;
17201 #endif /* VMS_DEBUGGING_INFO */
17203 /* Push a new declaration scope. */
17205 static void
17206 push_decl_scope (tree scope)
17208 vec_safe_push (decl_scope_table, scope);
17211 /* Pop a declaration scope. */
17213 static inline void
17214 pop_decl_scope (void)
17216 decl_scope_table->pop ();
17219 /* walk_tree helper function for uses_local_type, below. */
17221 static tree
17222 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
17224 if (!TYPE_P (*tp))
17225 *walk_subtrees = 0;
17226 else
17228 tree name = TYPE_NAME (*tp);
17229 if (name && DECL_P (name) && decl_function_context (name))
17230 return *tp;
17232 return NULL_TREE;
17235 /* If TYPE involves a function-local type (including a local typedef to a
17236 non-local type), returns that type; otherwise returns NULL_TREE. */
17238 static tree
17239 uses_local_type (tree type)
17241 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
17242 return used;
17245 /* Return the DIE for the scope that immediately contains this type.
17246 Non-named types that do not involve a function-local type get global
17247 scope. Named types nested in namespaces or other types get their
17248 containing scope. All other types (i.e. function-local named types) get
17249 the current active scope. */
17251 static dw_die_ref
17252 scope_die_for (tree t, dw_die_ref context_die)
17254 dw_die_ref scope_die = NULL;
17255 tree containing_scope;
17257 /* Non-types always go in the current scope. */
17258 gcc_assert (TYPE_P (t));
17260 /* Use the scope of the typedef, rather than the scope of the type
17261 it refers to. */
17262 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
17263 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
17264 else
17265 containing_scope = TYPE_CONTEXT (t);
17267 /* Use the containing namespace if there is one. */
17268 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
17270 if (context_die == lookup_decl_die (containing_scope))
17271 /* OK */;
17272 else if (debug_info_level > DINFO_LEVEL_TERSE)
17273 context_die = get_context_die (containing_scope);
17274 else
17275 containing_scope = NULL_TREE;
17278 /* Ignore function type "scopes" from the C frontend. They mean that
17279 a tagged type is local to a parmlist of a function declarator, but
17280 that isn't useful to DWARF. */
17281 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
17282 containing_scope = NULL_TREE;
17284 if (SCOPE_FILE_SCOPE_P (containing_scope))
17286 /* If T uses a local type keep it local as well, to avoid references
17287 to function-local DIEs from outside the function. */
17288 if (current_function_decl && uses_local_type (t))
17289 scope_die = context_die;
17290 else
17291 scope_die = comp_unit_die ();
17293 else if (TYPE_P (containing_scope))
17295 /* For types, we can just look up the appropriate DIE. */
17296 if (debug_info_level > DINFO_LEVEL_TERSE)
17297 scope_die = get_context_die (containing_scope);
17298 else
17300 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
17301 if (scope_die == NULL)
17302 scope_die = comp_unit_die ();
17305 else
17306 scope_die = context_die;
17308 return scope_die;
17311 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
17313 static inline int
17314 local_scope_p (dw_die_ref context_die)
17316 for (; context_die; context_die = context_die->die_parent)
17317 if (context_die->die_tag == DW_TAG_inlined_subroutine
17318 || context_die->die_tag == DW_TAG_subprogram)
17319 return 1;
17321 return 0;
17324 /* Returns nonzero if CONTEXT_DIE is a class. */
17326 static inline int
17327 class_scope_p (dw_die_ref context_die)
17329 return (context_die
17330 && (context_die->die_tag == DW_TAG_structure_type
17331 || context_die->die_tag == DW_TAG_class_type
17332 || context_die->die_tag == DW_TAG_interface_type
17333 || context_die->die_tag == DW_TAG_union_type));
17336 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
17337 whether or not to treat a DIE in this context as a declaration. */
17339 static inline int
17340 class_or_namespace_scope_p (dw_die_ref context_die)
17342 return (class_scope_p (context_die)
17343 || (context_die && context_die->die_tag == DW_TAG_namespace));
17346 /* Many forms of DIEs require a "type description" attribute. This
17347 routine locates the proper "type descriptor" die for the type given
17348 by 'type' plus any additional qualifiers given by 'cv_quals', and
17349 adds a DW_AT_type attribute below the given die. */
17351 static void
17352 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
17353 dw_die_ref context_die)
17355 enum tree_code code = TREE_CODE (type);
17356 dw_die_ref type_die = NULL;
17358 /* ??? If this type is an unnamed subrange type of an integral, floating-point
17359 or fixed-point type, use the inner type. This is because we have no
17360 support for unnamed types in base_type_die. This can happen if this is
17361 an Ada subrange type. Correct solution is emit a subrange type die. */
17362 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
17363 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
17364 type = TREE_TYPE (type), code = TREE_CODE (type);
17366 if (code == ERROR_MARK
17367 /* Handle a special case. For functions whose return type is void, we
17368 generate *no* type attribute. (Note that no object may have type
17369 `void', so this only applies to function return types). */
17370 || code == VOID_TYPE)
17371 return;
17373 type_die = modified_type_die (type,
17374 cv_quals | TYPE_QUALS_NO_ADDR_SPACE (type),
17375 context_die);
17377 if (type_die != NULL)
17378 add_AT_die_ref (object_die, DW_AT_type, type_die);
17381 /* Given an object die, add the calling convention attribute for the
17382 function call type. */
17383 static void
17384 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
17386 enum dwarf_calling_convention value = DW_CC_normal;
17388 value = ((enum dwarf_calling_convention)
17389 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
17391 if (is_fortran ()
17392 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
17394 /* DWARF 2 doesn't provide a way to identify a program's source-level
17395 entry point. DW_AT_calling_convention attributes are only meant
17396 to describe functions' calling conventions. However, lacking a
17397 better way to signal the Fortran main program, we used this for
17398 a long time, following existing custom. Now, DWARF 4 has
17399 DW_AT_main_subprogram, which we add below, but some tools still
17400 rely on the old way, which we thus keep. */
17401 value = DW_CC_program;
17403 if (dwarf_version >= 4 || !dwarf_strict)
17404 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
17407 /* Only add the attribute if the backend requests it, and
17408 is not DW_CC_normal. */
17409 if (value && (value != DW_CC_normal))
17410 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
17413 /* Given a tree pointer to a struct, class, union, or enum type node, return
17414 a pointer to the (string) tag name for the given type, or zero if the type
17415 was declared without a tag. */
17417 static const char *
17418 type_tag (const_tree type)
17420 const char *name = 0;
17422 if (TYPE_NAME (type) != 0)
17424 tree t = 0;
17426 /* Find the IDENTIFIER_NODE for the type name. */
17427 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
17428 && !TYPE_NAMELESS (type))
17429 t = TYPE_NAME (type);
17431 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
17432 a TYPE_DECL node, regardless of whether or not a `typedef' was
17433 involved. */
17434 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
17435 && ! DECL_IGNORED_P (TYPE_NAME (type)))
17437 /* We want to be extra verbose. Don't call dwarf_name if
17438 DECL_NAME isn't set. The default hook for decl_printable_name
17439 doesn't like that, and in this context it's correct to return
17440 0, instead of "<anonymous>" or the like. */
17441 if (DECL_NAME (TYPE_NAME (type))
17442 && !DECL_NAMELESS (TYPE_NAME (type)))
17443 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
17446 /* Now get the name as a string, or invent one. */
17447 if (!name && t != 0)
17448 name = IDENTIFIER_POINTER (t);
17451 return (name == 0 || *name == '\0') ? 0 : name;
17454 /* Return the type associated with a data member, make a special check
17455 for bit field types. */
17457 static inline tree
17458 member_declared_type (const_tree member)
17460 return (DECL_BIT_FIELD_TYPE (member)
17461 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
17464 /* Get the decl's label, as described by its RTL. This may be different
17465 from the DECL_NAME name used in the source file. */
17467 #if 0
17468 static const char *
17469 decl_start_label (tree decl)
17471 rtx x;
17472 const char *fnname;
17474 x = DECL_RTL (decl);
17475 gcc_assert (MEM_P (x));
17477 x = XEXP (x, 0);
17478 gcc_assert (GET_CODE (x) == SYMBOL_REF);
17480 fnname = XSTR (x, 0);
17481 return fnname;
17483 #endif
17485 /* These routines generate the internal representation of the DIE's for
17486 the compilation unit. Debugging information is collected by walking
17487 the declaration trees passed in from dwarf2out_decl(). */
17489 static void
17490 gen_array_type_die (tree type, dw_die_ref context_die)
17492 dw_die_ref scope_die = scope_die_for (type, context_die);
17493 dw_die_ref array_die;
17495 /* GNU compilers represent multidimensional array types as sequences of one
17496 dimensional array types whose element types are themselves array types.
17497 We sometimes squish that down to a single array_type DIE with multiple
17498 subscripts in the Dwarf debugging info. The draft Dwarf specification
17499 say that we are allowed to do this kind of compression in C, because
17500 there is no difference between an array of arrays and a multidimensional
17501 array. We don't do this for Ada to remain as close as possible to the
17502 actual representation, which is especially important against the language
17503 flexibilty wrt arrays of variable size. */
17505 bool collapse_nested_arrays = !is_ada ();
17506 tree element_type;
17508 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
17509 DW_TAG_string_type doesn't have DW_AT_type attribute). */
17510 if (TYPE_STRING_FLAG (type)
17511 && TREE_CODE (type) == ARRAY_TYPE
17512 && is_fortran ()
17513 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
17515 HOST_WIDE_INT size;
17517 array_die = new_die (DW_TAG_string_type, scope_die, type);
17518 add_name_attribute (array_die, type_tag (type));
17519 equate_type_number_to_die (type, array_die);
17520 size = int_size_in_bytes (type);
17521 if (size >= 0)
17522 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17523 else if (TYPE_DOMAIN (type) != NULL_TREE
17524 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
17525 && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
17527 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
17528 dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2, NULL);
17530 size = int_size_in_bytes (TREE_TYPE (szdecl));
17531 if (loc && size > 0)
17533 add_AT_location_description (array_die, DW_AT_string_length, loc);
17534 if (size != DWARF2_ADDR_SIZE)
17535 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17538 return;
17541 array_die = new_die (DW_TAG_array_type, scope_die, type);
17542 add_name_attribute (array_die, type_tag (type));
17543 equate_type_number_to_die (type, array_die);
17545 if (TREE_CODE (type) == VECTOR_TYPE)
17546 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
17548 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
17549 if (is_fortran ()
17550 && TREE_CODE (type) == ARRAY_TYPE
17551 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
17552 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
17553 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17555 #if 0
17556 /* We default the array ordering. SDB will probably do
17557 the right things even if DW_AT_ordering is not present. It's not even
17558 an issue until we start to get into multidimensional arrays anyway. If
17559 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
17560 then we'll have to put the DW_AT_ordering attribute back in. (But if
17561 and when we find out that we need to put these in, we will only do so
17562 for multidimensional arrays. */
17563 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17564 #endif
17566 if (TREE_CODE (type) == VECTOR_TYPE)
17568 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
17569 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
17570 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node, NULL);
17571 add_bound_info (subrange_die, DW_AT_upper_bound,
17572 size_int (TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
17574 else
17575 add_subscript_info (array_die, type, collapse_nested_arrays);
17577 /* Add representation of the type of the elements of this array type and
17578 emit the corresponding DIE if we haven't done it already. */
17579 element_type = TREE_TYPE (type);
17580 if (collapse_nested_arrays)
17581 while (TREE_CODE (element_type) == ARRAY_TYPE)
17583 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
17584 break;
17585 element_type = TREE_TYPE (element_type);
17588 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED, context_die);
17590 add_gnat_descriptive_type_attribute (array_die, type, context_die);
17591 if (TYPE_ARTIFICIAL (type))
17592 add_AT_flag (array_die, DW_AT_artificial, 1);
17594 if (get_AT (array_die, DW_AT_name))
17595 add_pubtype (type, array_die);
17598 /* This routine generates DIE for array with hidden descriptor, details
17599 are filled into *info by a langhook. */
17601 static void
17602 gen_descr_array_type_die (tree type, struct array_descr_info *info,
17603 dw_die_ref context_die)
17605 const dw_die_ref scope_die = scope_die_for (type, context_die);
17606 const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
17607 const struct loc_descr_context context = { type, info->base_decl };
17608 int dim;
17610 add_name_attribute (array_die, type_tag (type));
17611 equate_type_number_to_die (type, array_die);
17613 if (info->ndimensions > 1)
17614 switch (info->ordering)
17616 case array_descr_ordering_row_major:
17617 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17618 break;
17619 case array_descr_ordering_column_major:
17620 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17621 break;
17622 default:
17623 break;
17626 if (dwarf_version >= 3 || !dwarf_strict)
17628 if (info->data_location)
17629 add_scalar_info (array_die, DW_AT_data_location, info->data_location,
17630 dw_scalar_form_exprloc, &context);
17631 if (info->associated)
17632 add_scalar_info (array_die, DW_AT_associated, info->associated,
17633 dw_scalar_form_constant
17634 | dw_scalar_form_exprloc
17635 | dw_scalar_form_reference, &context);
17636 if (info->allocated)
17637 add_scalar_info (array_die, DW_AT_allocated, info->allocated,
17638 dw_scalar_form_constant
17639 | dw_scalar_form_exprloc
17640 | dw_scalar_form_reference, &context);
17643 add_gnat_descriptive_type_attribute (array_die, type, context_die);
17645 for (dim = 0; dim < info->ndimensions; dim++)
17647 dw_die_ref subrange_die
17648 = new_die (DW_TAG_subrange_type, array_die, NULL);
17650 if (info->dimen[dim].bounds_type)
17651 add_type_attribute (subrange_die,
17652 info->dimen[dim].bounds_type, 0,
17653 context_die);
17654 if (info->dimen[dim].lower_bound)
17655 add_bound_info (subrange_die, DW_AT_lower_bound,
17656 info->dimen[dim].lower_bound, &context);
17657 if (info->dimen[dim].upper_bound)
17658 add_bound_info (subrange_die, DW_AT_upper_bound,
17659 info->dimen[dim].upper_bound, &context);
17660 if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
17661 add_scalar_info (subrange_die, DW_AT_byte_stride,
17662 info->dimen[dim].stride,
17663 dw_scalar_form_constant
17664 | dw_scalar_form_exprloc
17665 | dw_scalar_form_reference,
17666 &context);
17669 gen_type_die (info->element_type, context_die);
17670 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
17671 context_die);
17673 if (get_AT (array_die, DW_AT_name))
17674 add_pubtype (type, array_die);
17677 #if 0
17678 static void
17679 gen_entry_point_die (tree decl, dw_die_ref context_die)
17681 tree origin = decl_ultimate_origin (decl);
17682 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
17684 if (origin != NULL)
17685 add_abstract_origin_attribute (decl_die, origin);
17686 else
17688 add_name_and_src_coords_attributes (decl_die, decl);
17689 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
17690 TYPE_UNQUALIFIED, context_die);
17693 if (DECL_ABSTRACT_P (decl))
17694 equate_decl_number_to_die (decl, decl_die);
17695 else
17696 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
17698 #endif
17700 /* Walk through the list of incomplete types again, trying once more to
17701 emit full debugging info for them. */
17703 static void
17704 retry_incomplete_types (void)
17706 int i;
17708 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
17709 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
17710 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
17713 /* Determine what tag to use for a record type. */
17715 static enum dwarf_tag
17716 record_type_tag (tree type)
17718 if (! lang_hooks.types.classify_record)
17719 return DW_TAG_structure_type;
17721 switch (lang_hooks.types.classify_record (type))
17723 case RECORD_IS_STRUCT:
17724 return DW_TAG_structure_type;
17726 case RECORD_IS_CLASS:
17727 return DW_TAG_class_type;
17729 case RECORD_IS_INTERFACE:
17730 if (dwarf_version >= 3 || !dwarf_strict)
17731 return DW_TAG_interface_type;
17732 return DW_TAG_structure_type;
17734 default:
17735 gcc_unreachable ();
17739 /* Generate a DIE to represent an enumeration type. Note that these DIEs
17740 include all of the information about the enumeration values also. Each
17741 enumerated type name/value is listed as a child of the enumerated type
17742 DIE. */
17744 static dw_die_ref
17745 gen_enumeration_type_die (tree type, dw_die_ref context_die)
17747 dw_die_ref type_die = lookup_type_die (type);
17749 if (type_die == NULL)
17751 type_die = new_die (DW_TAG_enumeration_type,
17752 scope_die_for (type, context_die), type);
17753 equate_type_number_to_die (type, type_die);
17754 add_name_attribute (type_die, type_tag (type));
17755 if (dwarf_version >= 4 || !dwarf_strict)
17757 if (ENUM_IS_SCOPED (type))
17758 add_AT_flag (type_die, DW_AT_enum_class, 1);
17759 if (ENUM_IS_OPAQUE (type))
17760 add_AT_flag (type_die, DW_AT_declaration, 1);
17763 else if (! TYPE_SIZE (type))
17764 return type_die;
17765 else
17766 remove_AT (type_die, DW_AT_declaration);
17768 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
17769 given enum type is incomplete, do not generate the DW_AT_byte_size
17770 attribute or the DW_AT_element_list attribute. */
17771 if (TYPE_SIZE (type))
17773 tree link;
17775 TREE_ASM_WRITTEN (type) = 1;
17776 add_byte_size_attribute (type_die, type);
17777 if (dwarf_version >= 3 || !dwarf_strict)
17779 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
17780 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED,
17781 context_die);
17783 if (TYPE_STUB_DECL (type) != NULL_TREE)
17785 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
17786 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
17789 /* If the first reference to this type was as the return type of an
17790 inline function, then it may not have a parent. Fix this now. */
17791 if (type_die->die_parent == NULL)
17792 add_child_die (scope_die_for (type, context_die), type_die);
17794 for (link = TYPE_VALUES (type);
17795 link != NULL; link = TREE_CHAIN (link))
17797 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
17798 tree value = TREE_VALUE (link);
17800 add_name_attribute (enum_die,
17801 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
17803 if (TREE_CODE (value) == CONST_DECL)
17804 value = DECL_INITIAL (value);
17806 if (simple_type_size_in_bits (TREE_TYPE (value))
17807 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
17809 /* For constant forms created by add_AT_unsigned DWARF
17810 consumers (GDB, elfutils, etc.) always zero extend
17811 the value. Only when the actual value is negative
17812 do we need to use add_AT_int to generate a constant
17813 form that can represent negative values. */
17814 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
17815 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
17816 add_AT_unsigned (enum_die, DW_AT_const_value,
17817 (unsigned HOST_WIDE_INT) val);
17818 else
17819 add_AT_int (enum_die, DW_AT_const_value, val);
17821 else
17822 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
17823 that here. TODO: This should be re-worked to use correct
17824 signed/unsigned double tags for all cases. */
17825 add_AT_wide (enum_die, DW_AT_const_value, value);
17828 add_gnat_descriptive_type_attribute (type_die, type, context_die);
17829 if (TYPE_ARTIFICIAL (type))
17830 add_AT_flag (type_die, DW_AT_artificial, 1);
17832 else
17833 add_AT_flag (type_die, DW_AT_declaration, 1);
17835 add_pubtype (type, type_die);
17837 return type_die;
17840 /* Generate a DIE to represent either a real live formal parameter decl or to
17841 represent just the type of some formal parameter position in some function
17842 type.
17844 Note that this routine is a bit unusual because its argument may be a
17845 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
17846 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
17847 node. If it's the former then this function is being called to output a
17848 DIE to represent a formal parameter object (or some inlining thereof). If
17849 it's the latter, then this function is only being called to output a
17850 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
17851 argument type of some subprogram type.
17852 If EMIT_NAME_P is true, name and source coordinate attributes
17853 are emitted. */
17855 static dw_die_ref
17856 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
17857 dw_die_ref context_die)
17859 tree node_or_origin = node ? node : origin;
17860 tree ultimate_origin;
17861 dw_die_ref parm_die
17862 = new_die (DW_TAG_formal_parameter, context_die, node);
17864 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
17866 case tcc_declaration:
17867 ultimate_origin = decl_ultimate_origin (node_or_origin);
17868 if (node || ultimate_origin)
17869 origin = ultimate_origin;
17870 if (origin != NULL)
17871 add_abstract_origin_attribute (parm_die, origin);
17872 else if (emit_name_p)
17873 add_name_and_src_coords_attributes (parm_die, node);
17874 if (origin == NULL
17875 || (! DECL_ABSTRACT_P (node_or_origin)
17876 && variably_modified_type_p (TREE_TYPE (node_or_origin),
17877 decl_function_context
17878 (node_or_origin))))
17880 tree type = TREE_TYPE (node_or_origin);
17881 if (decl_by_reference_p (node_or_origin))
17882 add_type_attribute (parm_die, TREE_TYPE (type),
17883 TYPE_UNQUALIFIED, context_die);
17884 else
17885 add_type_attribute (parm_die, type,
17886 decl_quals (node_or_origin),
17887 context_die);
17889 if (origin == NULL && DECL_ARTIFICIAL (node))
17890 add_AT_flag (parm_die, DW_AT_artificial, 1);
17892 if (node && node != origin)
17893 equate_decl_number_to_die (node, parm_die);
17894 if (! DECL_ABSTRACT_P (node_or_origin))
17895 add_location_or_const_value_attribute (parm_die, node_or_origin,
17896 node == NULL, DW_AT_location);
17898 break;
17900 case tcc_type:
17901 /* We were called with some kind of a ..._TYPE node. */
17902 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED,
17903 context_die);
17904 break;
17906 default:
17907 gcc_unreachable ();
17910 return parm_die;
17913 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
17914 children DW_TAG_formal_parameter DIEs representing the arguments of the
17915 parameter pack.
17917 PARM_PACK must be a function parameter pack.
17918 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
17919 must point to the subsequent arguments of the function PACK_ARG belongs to.
17920 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
17921 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
17922 following the last one for which a DIE was generated. */
17924 static dw_die_ref
17925 gen_formal_parameter_pack_die (tree parm_pack,
17926 tree pack_arg,
17927 dw_die_ref subr_die,
17928 tree *next_arg)
17930 tree arg;
17931 dw_die_ref parm_pack_die;
17933 gcc_assert (parm_pack
17934 && lang_hooks.function_parameter_pack_p (parm_pack)
17935 && subr_die);
17937 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
17938 add_src_coords_attributes (parm_pack_die, parm_pack);
17940 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
17942 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
17943 parm_pack))
17944 break;
17945 gen_formal_parameter_die (arg, NULL,
17946 false /* Don't emit name attribute. */,
17947 parm_pack_die);
17949 if (next_arg)
17950 *next_arg = arg;
17951 return parm_pack_die;
17954 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
17955 at the end of an (ANSI prototyped) formal parameters list. */
17957 static void
17958 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
17960 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
17963 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
17964 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
17965 parameters as specified in some function type specification (except for
17966 those which appear as part of a function *definition*). */
17968 static void
17969 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
17971 tree link;
17972 tree formal_type = NULL;
17973 tree first_parm_type;
17974 tree arg;
17976 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
17978 arg = DECL_ARGUMENTS (function_or_method_type);
17979 function_or_method_type = TREE_TYPE (function_or_method_type);
17981 else
17982 arg = NULL_TREE;
17984 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
17986 /* Make our first pass over the list of formal parameter types and output a
17987 DW_TAG_formal_parameter DIE for each one. */
17988 for (link = first_parm_type; link; )
17990 dw_die_ref parm_die;
17992 formal_type = TREE_VALUE (link);
17993 if (formal_type == void_type_node)
17994 break;
17996 /* Output a (nameless) DIE to represent the formal parameter itself. */
17997 if (!POINTER_BOUNDS_TYPE_P (formal_type))
17999 parm_die = gen_formal_parameter_die (formal_type, NULL,
18000 true /* Emit name attribute. */,
18001 context_die);
18002 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
18003 && link == first_parm_type)
18005 add_AT_flag (parm_die, DW_AT_artificial, 1);
18006 if (dwarf_version >= 3 || !dwarf_strict)
18007 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
18009 else if (arg && DECL_ARTIFICIAL (arg))
18010 add_AT_flag (parm_die, DW_AT_artificial, 1);
18013 link = TREE_CHAIN (link);
18014 if (arg)
18015 arg = DECL_CHAIN (arg);
18018 /* If this function type has an ellipsis, add a
18019 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
18020 if (formal_type != void_type_node)
18021 gen_unspecified_parameters_die (function_or_method_type, context_die);
18023 /* Make our second (and final) pass over the list of formal parameter types
18024 and output DIEs to represent those types (as necessary). */
18025 for (link = TYPE_ARG_TYPES (function_or_method_type);
18026 link && TREE_VALUE (link);
18027 link = TREE_CHAIN (link))
18028 gen_type_die (TREE_VALUE (link), context_die);
18031 /* We want to generate the DIE for TYPE so that we can generate the
18032 die for MEMBER, which has been defined; we will need to refer back
18033 to the member declaration nested within TYPE. If we're trying to
18034 generate minimal debug info for TYPE, processing TYPE won't do the
18035 trick; we need to attach the member declaration by hand. */
18037 static void
18038 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
18040 gen_type_die (type, context_die);
18042 /* If we're trying to avoid duplicate debug info, we may not have
18043 emitted the member decl for this function. Emit it now. */
18044 if (TYPE_STUB_DECL (type)
18045 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
18046 && ! lookup_decl_die (member))
18048 dw_die_ref type_die;
18049 gcc_assert (!decl_ultimate_origin (member));
18051 push_decl_scope (type);
18052 type_die = lookup_type_die_strip_naming_typedef (type);
18053 if (TREE_CODE (member) == FUNCTION_DECL)
18054 gen_subprogram_die (member, type_die);
18055 else if (TREE_CODE (member) == FIELD_DECL)
18057 /* Ignore the nameless fields that are used to skip bits but handle
18058 C++ anonymous unions and structs. */
18059 if (DECL_NAME (member) != NULL_TREE
18060 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
18061 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
18063 gen_type_die (member_declared_type (member), type_die);
18064 gen_field_die (member, type_die);
18067 else
18068 gen_variable_die (member, NULL_TREE, type_die);
18070 pop_decl_scope ();
18074 /* Forward declare these functions, because they are mutually recursive
18075 with their set_block_* pairing functions. */
18076 static void set_decl_origin_self (tree);
18077 static void set_decl_abstract_flags (tree, vec<tree> &);
18079 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
18080 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
18081 that it points to the node itself, thus indicating that the node is its
18082 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
18083 the given node is NULL, recursively descend the decl/block tree which
18084 it is the root of, and for each other ..._DECL or BLOCK node contained
18085 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
18086 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
18087 values to point to themselves. */
18089 static void
18090 set_block_origin_self (tree stmt)
18092 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
18094 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
18097 tree local_decl;
18099 for (local_decl = BLOCK_VARS (stmt);
18100 local_decl != NULL_TREE;
18101 local_decl = DECL_CHAIN (local_decl))
18102 /* Do not recurse on nested functions since the inlining status
18103 of parent and child can be different as per the DWARF spec. */
18104 if (TREE_CODE (local_decl) != FUNCTION_DECL
18105 && !DECL_EXTERNAL (local_decl))
18106 set_decl_origin_self (local_decl);
18110 tree subblock;
18112 for (subblock = BLOCK_SUBBLOCKS (stmt);
18113 subblock != NULL_TREE;
18114 subblock = BLOCK_CHAIN (subblock))
18115 set_block_origin_self (subblock); /* Recurse. */
18120 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
18121 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
18122 node to so that it points to the node itself, thus indicating that the
18123 node represents its own (abstract) origin. Additionally, if the
18124 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
18125 the decl/block tree of which the given node is the root of, and for
18126 each other ..._DECL or BLOCK node contained therein whose
18127 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
18128 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
18129 point to themselves. */
18131 static void
18132 set_decl_origin_self (tree decl)
18134 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
18136 DECL_ABSTRACT_ORIGIN (decl) = decl;
18137 if (TREE_CODE (decl) == FUNCTION_DECL)
18139 tree arg;
18141 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
18142 DECL_ABSTRACT_ORIGIN (arg) = arg;
18143 if (DECL_INITIAL (decl) != NULL_TREE
18144 && DECL_INITIAL (decl) != error_mark_node)
18145 set_block_origin_self (DECL_INITIAL (decl));
18150 /* Given a pointer to some BLOCK node, set the BLOCK_ABSTRACT flag to 1
18151 and if it wasn't 1 before, push it to abstract_vec vector.
18152 For all local decls and all local sub-blocks (recursively) do it
18153 too. */
18155 static void
18156 set_block_abstract_flags (tree stmt, vec<tree> &abstract_vec)
18158 tree local_decl;
18159 tree subblock;
18160 unsigned int i;
18162 if (!BLOCK_ABSTRACT (stmt))
18164 abstract_vec.safe_push (stmt);
18165 BLOCK_ABSTRACT (stmt) = 1;
18168 for (local_decl = BLOCK_VARS (stmt);
18169 local_decl != NULL_TREE;
18170 local_decl = DECL_CHAIN (local_decl))
18171 if (! DECL_EXTERNAL (local_decl))
18172 set_decl_abstract_flags (local_decl, abstract_vec);
18174 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
18176 local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
18177 if ((TREE_CODE (local_decl) == VAR_DECL && !TREE_STATIC (local_decl))
18178 || TREE_CODE (local_decl) == PARM_DECL)
18179 set_decl_abstract_flags (local_decl, abstract_vec);
18182 for (subblock = BLOCK_SUBBLOCKS (stmt);
18183 subblock != NULL_TREE;
18184 subblock = BLOCK_CHAIN (subblock))
18185 set_block_abstract_flags (subblock, abstract_vec);
18188 /* Given a pointer to some ..._DECL node, set DECL_ABSTRACT_P flag on it
18189 to 1 and if it wasn't 1 before, push to abstract_vec vector.
18190 In the case where the decl is a FUNCTION_DECL also set the abstract
18191 flags for all of the parameters, local vars, local
18192 blocks and sub-blocks (recursively). */
18194 static void
18195 set_decl_abstract_flags (tree decl, vec<tree> &abstract_vec)
18197 if (!DECL_ABSTRACT_P (decl))
18199 abstract_vec.safe_push (decl);
18200 DECL_ABSTRACT_P (decl) = 1;
18203 if (TREE_CODE (decl) == FUNCTION_DECL)
18205 tree arg;
18207 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
18208 if (!DECL_ABSTRACT_P (arg))
18210 abstract_vec.safe_push (arg);
18211 DECL_ABSTRACT_P (arg) = 1;
18213 if (DECL_INITIAL (decl) != NULL_TREE
18214 && DECL_INITIAL (decl) != error_mark_node)
18215 set_block_abstract_flags (DECL_INITIAL (decl), abstract_vec);
18219 /* Generate the DWARF2 info for the "abstract" instance of a function which we
18220 may later generate inlined and/or out-of-line instances of. */
18222 static void
18223 dwarf2out_abstract_function (tree decl)
18225 dw_die_ref old_die;
18226 tree save_fn;
18227 tree context;
18228 hash_table<decl_loc_hasher> *old_decl_loc_table;
18229 hash_table<dw_loc_list_hasher> *old_cached_dw_loc_list_table;
18230 int old_call_site_count, old_tail_call_site_count;
18231 struct call_arg_loc_node *old_call_arg_locations;
18233 /* Make sure we have the actual abstract inline, not a clone. */
18234 decl = DECL_ORIGIN (decl);
18236 old_die = lookup_decl_die (decl);
18237 if (old_die && get_AT (old_die, DW_AT_inline))
18238 /* We've already generated the abstract instance. */
18239 return;
18241 /* We can be called while recursively when seeing block defining inlined subroutine
18242 DIE. Be sure to not clobber the outer location table nor use it or we would
18243 get locations in abstract instantces. */
18244 old_decl_loc_table = decl_loc_table;
18245 decl_loc_table = NULL;
18246 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
18247 cached_dw_loc_list_table = NULL;
18248 old_call_arg_locations = call_arg_locations;
18249 call_arg_locations = NULL;
18250 old_call_site_count = call_site_count;
18251 call_site_count = -1;
18252 old_tail_call_site_count = tail_call_site_count;
18253 tail_call_site_count = -1;
18255 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
18256 we don't get confused by DECL_ABSTRACT_P. */
18257 if (debug_info_level > DINFO_LEVEL_TERSE)
18259 context = decl_class_context (decl);
18260 if (context)
18261 gen_type_die_for_member
18262 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
18265 /* Pretend we've just finished compiling this function. */
18266 save_fn = current_function_decl;
18267 current_function_decl = decl;
18269 auto_vec<tree, 64> abstract_vec;
18270 set_decl_abstract_flags (decl, abstract_vec);
18271 dwarf2out_decl (decl);
18272 unsigned int i;
18273 tree t;
18274 FOR_EACH_VEC_ELT (abstract_vec, i, t)
18275 if (TREE_CODE (t) == BLOCK)
18276 BLOCK_ABSTRACT (t) = 0;
18277 else
18278 DECL_ABSTRACT_P (t) = 0;
18280 current_function_decl = save_fn;
18281 decl_loc_table = old_decl_loc_table;
18282 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
18283 call_arg_locations = old_call_arg_locations;
18284 call_site_count = old_call_site_count;
18285 tail_call_site_count = old_tail_call_site_count;
18288 /* Helper function of premark_used_types() which gets called through
18289 htab_traverse.
18291 Marks the DIE of a given type in *SLOT as perennial, so it never gets
18292 marked as unused by prune_unused_types. */
18294 bool
18295 premark_used_types_helper (tree const &type, void *)
18297 dw_die_ref die;
18299 die = lookup_type_die (type);
18300 if (die != NULL)
18301 die->die_perennial_p = 1;
18302 return true;
18305 /* Helper function of premark_types_used_by_global_vars which gets called
18306 through htab_traverse.
18308 Marks the DIE of a given type in *SLOT as perennial, so it never gets
18309 marked as unused by prune_unused_types. The DIE of the type is marked
18310 only if the global variable using the type will actually be emitted. */
18313 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
18314 void *)
18316 struct types_used_by_vars_entry *entry;
18317 dw_die_ref die;
18319 entry = (struct types_used_by_vars_entry *) *slot;
18320 gcc_assert (entry->type != NULL
18321 && entry->var_decl != NULL);
18322 die = lookup_type_die (entry->type);
18323 if (die)
18325 /* Ask cgraph if the global variable really is to be emitted.
18326 If yes, then we'll keep the DIE of ENTRY->TYPE. */
18327 varpool_node *node = varpool_node::get (entry->var_decl);
18328 if (node && node->definition)
18330 die->die_perennial_p = 1;
18331 /* Keep the parent DIEs as well. */
18332 while ((die = die->die_parent) && die->die_perennial_p == 0)
18333 die->die_perennial_p = 1;
18336 return 1;
18339 /* Mark all members of used_types_hash as perennial. */
18341 static void
18342 premark_used_types (struct function *fun)
18344 if (fun && fun->used_types_hash)
18345 fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
18348 /* Mark all members of types_used_by_vars_entry as perennial. */
18350 static void
18351 premark_types_used_by_global_vars (void)
18353 if (types_used_by_vars_hash)
18354 types_used_by_vars_hash
18355 ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
18358 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
18359 for CA_LOC call arg loc node. */
18361 static dw_die_ref
18362 gen_call_site_die (tree decl, dw_die_ref subr_die,
18363 struct call_arg_loc_node *ca_loc)
18365 dw_die_ref stmt_die = NULL, die;
18366 tree block = ca_loc->block;
18368 while (block
18369 && block != DECL_INITIAL (decl)
18370 && TREE_CODE (block) == BLOCK)
18372 if (block_map.length () > BLOCK_NUMBER (block))
18373 stmt_die = block_map[BLOCK_NUMBER (block)];
18374 if (stmt_die)
18375 break;
18376 block = BLOCK_SUPERCONTEXT (block);
18378 if (stmt_die == NULL)
18379 stmt_die = subr_die;
18380 die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
18381 add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
18382 if (ca_loc->tail_call_p)
18383 add_AT_flag (die, DW_AT_GNU_tail_call, 1);
18384 if (ca_loc->symbol_ref)
18386 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
18387 if (tdie)
18388 add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
18389 else
18390 add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref, false);
18392 return die;
18395 /* Generate a DIE to represent a declared function (either file-scope or
18396 block-local). */
18398 static void
18399 gen_subprogram_die (tree decl, dw_die_ref context_die)
18401 tree origin = decl_ultimate_origin (decl);
18402 dw_die_ref subr_die;
18403 tree outer_scope;
18404 dw_die_ref old_die = lookup_decl_die (decl);
18405 int declaration = (current_function_decl != decl
18406 || class_or_namespace_scope_p (context_die));
18408 premark_used_types (DECL_STRUCT_FUNCTION (decl));
18410 /* It is possible to have both DECL_ABSTRACT_P and DECLARATION be true if we
18411 started to generate the abstract instance of an inline, decided to output
18412 its containing class, and proceeded to emit the declaration of the inline
18413 from the member list for the class. If so, DECLARATION takes priority;
18414 we'll get back to the abstract instance when done with the class. */
18416 /* The class-scope declaration DIE must be the primary DIE. */
18417 if (origin && declaration && class_or_namespace_scope_p (context_die))
18419 origin = NULL;
18420 gcc_assert (!old_die);
18423 /* Now that the C++ front end lazily declares artificial member fns, we
18424 might need to retrofit the declaration into its class. */
18425 if (!declaration && !origin && !old_die
18426 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
18427 && !class_or_namespace_scope_p (context_die)
18428 && debug_info_level > DINFO_LEVEL_TERSE)
18429 old_die = force_decl_die (decl);
18431 if (origin != NULL)
18433 gcc_assert (!declaration || local_scope_p (context_die));
18435 /* Fixup die_parent for the abstract instance of a nested
18436 inline function. */
18437 if (old_die && old_die->die_parent == NULL)
18438 add_child_die (context_die, old_die);
18440 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18441 add_abstract_origin_attribute (subr_die, origin);
18442 /* This is where the actual code for a cloned function is.
18443 Let's emit linkage name attribute for it. This helps
18444 debuggers to e.g, set breakpoints into
18445 constructors/destructors when the user asks "break
18446 K::K". */
18447 add_linkage_name (subr_die, decl);
18449 else if (old_die)
18451 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18452 struct dwarf_file_data * file_index = lookup_filename (s.file);
18454 if (!get_AT_flag (old_die, DW_AT_declaration)
18455 /* We can have a normal definition following an inline one in the
18456 case of redefinition of GNU C extern inlines.
18457 It seems reasonable to use AT_specification in this case. */
18458 && !get_AT (old_die, DW_AT_inline))
18460 /* Detect and ignore this case, where we are trying to output
18461 something we have already output. */
18462 return;
18465 /* If the definition comes from the same place as the declaration,
18466 maybe use the old DIE. We always want the DIE for this function
18467 that has the *_pc attributes to be under comp_unit_die so the
18468 debugger can find it. We also need to do this for abstract
18469 instances of inlines, since the spec requires the out-of-line copy
18470 to have the same parent. For local class methods, this doesn't
18471 apply; we just use the old DIE. */
18472 if ((is_cu_die (old_die->die_parent) || context_die == NULL)
18473 && (DECL_ARTIFICIAL (decl)
18474 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
18475 && (get_AT_unsigned (old_die, DW_AT_decl_line)
18476 == (unsigned) s.line))))
18478 subr_die = old_die;
18480 /* Clear out the declaration attribute and the formal parameters.
18481 Do not remove all children, because it is possible that this
18482 declaration die was forced using force_decl_die(). In such
18483 cases die that forced declaration die (e.g. TAG_imported_module)
18484 is one of the children that we do not want to remove. */
18485 remove_AT (subr_die, DW_AT_declaration);
18486 remove_AT (subr_die, DW_AT_object_pointer);
18487 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
18489 else
18491 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18492 add_AT_specification (subr_die, old_die);
18493 add_pubname (decl, subr_die);
18494 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18495 add_AT_file (subr_die, DW_AT_decl_file, file_index);
18496 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18497 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
18499 /* If the prototype had an 'auto' or 'decltype(auto)' return type,
18500 emit the real type on the definition die. */
18501 if (is_cxx() && debug_info_level > DINFO_LEVEL_TERSE)
18503 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
18504 if (die == auto_die || die == decltype_auto_die)
18505 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18506 TYPE_UNQUALIFIED, context_die);
18510 else
18512 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18514 if (TREE_PUBLIC (decl))
18515 add_AT_flag (subr_die, DW_AT_external, 1);
18517 add_name_and_src_coords_attributes (subr_die, decl);
18518 add_pubname (decl, subr_die);
18519 if (debug_info_level > DINFO_LEVEL_TERSE)
18521 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
18522 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18523 TYPE_UNQUALIFIED, context_die);
18526 add_pure_or_virtual_attribute (subr_die, decl);
18527 if (DECL_ARTIFICIAL (decl))
18528 add_AT_flag (subr_die, DW_AT_artificial, 1);
18530 if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
18531 add_AT_flag (subr_die, DW_AT_noreturn, 1);
18533 add_accessibility_attribute (subr_die, decl);
18536 if (declaration)
18538 if (!old_die || !get_AT (old_die, DW_AT_inline))
18540 add_AT_flag (subr_die, DW_AT_declaration, 1);
18542 /* If this is an explicit function declaration then generate
18543 a DW_AT_explicit attribute. */
18544 if (lang_hooks.decls.function_decl_explicit_p (decl)
18545 && (dwarf_version >= 3 || !dwarf_strict))
18546 add_AT_flag (subr_die, DW_AT_explicit, 1);
18548 /* If this is a C++11 deleted special function member then generate
18549 a DW_AT_GNU_deleted attribute. */
18550 if (lang_hooks.decls.function_decl_deleted_p (decl)
18551 && (! dwarf_strict))
18552 add_AT_flag (subr_die, DW_AT_GNU_deleted, 1);
18554 /* The first time we see a member function, it is in the context of
18555 the class to which it belongs. We make sure of this by emitting
18556 the class first. The next time is the definition, which is
18557 handled above. The two may come from the same source text.
18559 Note that force_decl_die() forces function declaration die. It is
18560 later reused to represent definition. */
18561 equate_decl_number_to_die (decl, subr_die);
18564 else if (DECL_ABSTRACT_P (decl))
18566 if (DECL_DECLARED_INLINE_P (decl))
18568 if (cgraph_function_possibly_inlined_p (decl))
18569 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
18570 else
18571 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
18573 else
18575 if (cgraph_function_possibly_inlined_p (decl))
18576 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
18577 else
18578 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
18581 if (DECL_DECLARED_INLINE_P (decl)
18582 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
18583 add_AT_flag (subr_die, DW_AT_artificial, 1);
18585 equate_decl_number_to_die (decl, subr_die);
18587 else if (!DECL_EXTERNAL (decl))
18589 HOST_WIDE_INT cfa_fb_offset;
18590 struct function *fun = DECL_STRUCT_FUNCTION (decl);
18592 if (!old_die || !get_AT (old_die, DW_AT_inline))
18593 equate_decl_number_to_die (decl, subr_die);
18595 gcc_checking_assert (fun);
18596 if (!flag_reorder_blocks_and_partition)
18598 dw_fde_ref fde = fun->fde;
18599 if (fde->dw_fde_begin)
18601 /* We have already generated the labels. */
18602 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18603 fde->dw_fde_end, false);
18605 else
18607 /* Create start/end labels and add the range. */
18608 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
18609 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
18610 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
18611 current_function_funcdef_no);
18612 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
18613 current_function_funcdef_no);
18614 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
18615 false);
18618 #if VMS_DEBUGGING_INFO
18619 /* HP OpenVMS Industry Standard 64: DWARF Extensions
18620 Section 2.3 Prologue and Epilogue Attributes:
18621 When a breakpoint is set on entry to a function, it is generally
18622 desirable for execution to be suspended, not on the very first
18623 instruction of the function, but rather at a point after the
18624 function's frame has been set up, after any language defined local
18625 declaration processing has been completed, and before execution of
18626 the first statement of the function begins. Debuggers generally
18627 cannot properly determine where this point is. Similarly for a
18628 breakpoint set on exit from a function. The prologue and epilogue
18629 attributes allow a compiler to communicate the location(s) to use. */
18632 if (fde->dw_fde_vms_end_prologue)
18633 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
18634 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
18636 if (fde->dw_fde_vms_begin_epilogue)
18637 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
18638 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
18640 #endif
18643 else
18645 /* Generate pubnames entries for the split function code ranges. */
18646 dw_fde_ref fde = fun->fde;
18648 if (fde->dw_fde_second_begin)
18650 if (dwarf_version >= 3 || !dwarf_strict)
18652 /* We should use ranges for non-contiguous code section
18653 addresses. Use the actual code range for the initial
18654 section, since the HOT/COLD labels might precede an
18655 alignment offset. */
18656 bool range_list_added = false;
18657 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
18658 fde->dw_fde_end, &range_list_added,
18659 false);
18660 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
18661 fde->dw_fde_second_end,
18662 &range_list_added, false);
18663 if (range_list_added)
18664 add_ranges (NULL);
18666 else
18668 /* There is no real support in DW2 for this .. so we make
18669 a work-around. First, emit the pub name for the segment
18670 containing the function label. Then make and emit a
18671 simplified subprogram DIE for the second segment with the
18672 name pre-fixed by __hot/cold_sect_of_. We use the same
18673 linkage name for the second die so that gdb will find both
18674 sections when given "b foo". */
18675 const char *name = NULL;
18676 tree decl_name = DECL_NAME (decl);
18677 dw_die_ref seg_die;
18679 /* Do the 'primary' section. */
18680 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18681 fde->dw_fde_end, false);
18683 /* Build a minimal DIE for the secondary section. */
18684 seg_die = new_die (DW_TAG_subprogram,
18685 subr_die->die_parent, decl);
18687 if (TREE_PUBLIC (decl))
18688 add_AT_flag (seg_die, DW_AT_external, 1);
18690 if (decl_name != NULL
18691 && IDENTIFIER_POINTER (decl_name) != NULL)
18693 name = dwarf2_name (decl, 1);
18694 if (! DECL_ARTIFICIAL (decl))
18695 add_src_coords_attributes (seg_die, decl);
18697 add_linkage_name (seg_die, decl);
18699 gcc_assert (name != NULL);
18700 add_pure_or_virtual_attribute (seg_die, decl);
18701 if (DECL_ARTIFICIAL (decl))
18702 add_AT_flag (seg_die, DW_AT_artificial, 1);
18704 name = concat ("__second_sect_of_", name, NULL);
18705 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
18706 fde->dw_fde_second_end, false);
18707 add_name_attribute (seg_die, name);
18708 if (want_pubnames ())
18709 add_pubname_string (name, seg_die);
18712 else
18713 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
18714 false);
18717 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
18719 /* We define the "frame base" as the function's CFA. This is more
18720 convenient for several reasons: (1) It's stable across the prologue
18721 and epilogue, which makes it better than just a frame pointer,
18722 (2) With dwarf3, there exists a one-byte encoding that allows us
18723 to reference the .debug_frame data by proxy, but failing that,
18724 (3) We can at least reuse the code inspection and interpretation
18725 code that determines the CFA position at various points in the
18726 function. */
18727 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
18729 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
18730 add_AT_loc (subr_die, DW_AT_frame_base, op);
18732 else
18734 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
18735 if (list->dw_loc_next)
18736 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
18737 else
18738 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
18741 /* Compute a displacement from the "steady-state frame pointer" to
18742 the CFA. The former is what all stack slots and argument slots
18743 will reference in the rtl; the latter is what we've told the
18744 debugger about. We'll need to adjust all frame_base references
18745 by this displacement. */
18746 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
18748 if (fun->static_chain_decl)
18749 add_AT_location_description (subr_die, DW_AT_static_link,
18750 loc_list_from_tree (fun->static_chain_decl, 2, NULL));
18753 /* Generate child dies for template paramaters. */
18754 if (debug_info_level > DINFO_LEVEL_TERSE)
18755 gen_generic_params_dies (decl);
18757 /* Now output descriptions of the arguments for this function. This gets
18758 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
18759 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
18760 `...' at the end of the formal parameter list. In order to find out if
18761 there was a trailing ellipsis or not, we must instead look at the type
18762 associated with the FUNCTION_DECL. This will be a node of type
18763 FUNCTION_TYPE. If the chain of type nodes hanging off of this
18764 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
18765 an ellipsis at the end. */
18767 /* In the case where we are describing a mere function declaration, all we
18768 need to do here (and all we *can* do here) is to describe the *types* of
18769 its formal parameters. */
18770 if (debug_info_level <= DINFO_LEVEL_TERSE)
18772 else if (declaration)
18773 gen_formal_types_die (decl, subr_die);
18774 else
18776 /* Generate DIEs to represent all known formal parameters. */
18777 tree parm = DECL_ARGUMENTS (decl);
18778 tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
18779 tree generic_decl_parm = generic_decl
18780 ? DECL_ARGUMENTS (generic_decl)
18781 : NULL;
18783 /* Now we want to walk the list of parameters of the function and
18784 emit their relevant DIEs.
18786 We consider the case of DECL being an instance of a generic function
18787 as well as it being a normal function.
18789 If DECL is an instance of a generic function we walk the
18790 parameters of the generic function declaration _and_ the parameters of
18791 DECL itself. This is useful because we want to emit specific DIEs for
18792 function parameter packs and those are declared as part of the
18793 generic function declaration. In that particular case,
18794 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
18795 That DIE has children DIEs representing the set of arguments
18796 of the pack. Note that the set of pack arguments can be empty.
18797 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
18798 children DIE.
18800 Otherwise, we just consider the parameters of DECL. */
18801 while (generic_decl_parm || parm)
18803 if (generic_decl_parm
18804 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
18805 gen_formal_parameter_pack_die (generic_decl_parm,
18806 parm, subr_die,
18807 &parm);
18808 else if (parm && !POINTER_BOUNDS_P (parm))
18810 dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
18812 if (parm == DECL_ARGUMENTS (decl)
18813 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
18814 && parm_die
18815 && (dwarf_version >= 3 || !dwarf_strict))
18816 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
18818 parm = DECL_CHAIN (parm);
18820 else if (parm)
18821 parm = DECL_CHAIN (parm);
18823 if (generic_decl_parm)
18824 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
18827 /* Decide whether we need an unspecified_parameters DIE at the end.
18828 There are 2 more cases to do this for: 1) the ansi ... declaration -
18829 this is detectable when the end of the arg list is not a
18830 void_type_node 2) an unprototyped function declaration (not a
18831 definition). This just means that we have no info about the
18832 parameters at all. */
18833 if (prototype_p (TREE_TYPE (decl)))
18835 /* This is the prototyped case, check for.... */
18836 if (stdarg_p (TREE_TYPE (decl)))
18837 gen_unspecified_parameters_die (decl, subr_die);
18839 else if (DECL_INITIAL (decl) == NULL_TREE)
18840 gen_unspecified_parameters_die (decl, subr_die);
18843 /* Output Dwarf info for all of the stuff within the body of the function
18844 (if it has one - it may be just a declaration). */
18845 outer_scope = DECL_INITIAL (decl);
18847 /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
18848 a function. This BLOCK actually represents the outermost binding contour
18849 for the function, i.e. the contour in which the function's formal
18850 parameters and labels get declared. Curiously, it appears that the front
18851 end doesn't actually put the PARM_DECL nodes for the current function onto
18852 the BLOCK_VARS list for this outer scope, but are strung off of the
18853 DECL_ARGUMENTS list for the function instead.
18855 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
18856 the LABEL_DECL nodes for the function however, and we output DWARF info
18857 for those in decls_for_scope. Just within the `outer_scope' there will be
18858 a BLOCK node representing the function's outermost pair of curly braces,
18859 and any blocks used for the base and member initializers of a C++
18860 constructor function. */
18861 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
18863 int call_site_note_count = 0;
18864 int tail_call_site_note_count = 0;
18866 /* Emit a DW_TAG_variable DIE for a named return value. */
18867 if (DECL_NAME (DECL_RESULT (decl)))
18868 gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
18870 decls_for_scope (outer_scope, subr_die);
18872 if (call_arg_locations && !dwarf_strict)
18874 struct call_arg_loc_node *ca_loc;
18875 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
18877 dw_die_ref die = NULL;
18878 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
18879 rtx arg, next_arg;
18881 for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
18882 arg; arg = next_arg)
18884 dw_loc_descr_ref reg, val;
18885 machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
18886 dw_die_ref cdie, tdie = NULL;
18888 next_arg = XEXP (arg, 1);
18889 if (REG_P (XEXP (XEXP (arg, 0), 0))
18890 && next_arg
18891 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
18892 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
18893 && REGNO (XEXP (XEXP (arg, 0), 0))
18894 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
18895 next_arg = XEXP (next_arg, 1);
18896 if (mode == VOIDmode)
18898 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
18899 if (mode == VOIDmode)
18900 mode = GET_MODE (XEXP (arg, 0));
18902 if (mode == VOIDmode || mode == BLKmode)
18903 continue;
18904 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
18906 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18907 tloc = XEXP (XEXP (arg, 0), 1);
18908 continue;
18910 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
18911 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
18913 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18914 tlocc = XEXP (XEXP (arg, 0), 1);
18915 continue;
18917 reg = NULL;
18918 if (REG_P (XEXP (XEXP (arg, 0), 0)))
18919 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
18920 VAR_INIT_STATUS_INITIALIZED);
18921 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
18923 rtx mem = XEXP (XEXP (arg, 0), 0);
18924 reg = mem_loc_descriptor (XEXP (mem, 0),
18925 get_address_mode (mem),
18926 GET_MODE (mem),
18927 VAR_INIT_STATUS_INITIALIZED);
18929 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
18930 == DEBUG_PARAMETER_REF)
18932 tree tdecl
18933 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
18934 tdie = lookup_decl_die (tdecl);
18935 if (tdie == NULL)
18936 continue;
18938 else
18939 continue;
18940 if (reg == NULL
18941 && GET_CODE (XEXP (XEXP (arg, 0), 0))
18942 != DEBUG_PARAMETER_REF)
18943 continue;
18944 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
18945 VOIDmode,
18946 VAR_INIT_STATUS_INITIALIZED);
18947 if (val == NULL)
18948 continue;
18949 if (die == NULL)
18950 die = gen_call_site_die (decl, subr_die, ca_loc);
18951 cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
18952 NULL_TREE);
18953 if (reg != NULL)
18954 add_AT_loc (cdie, DW_AT_location, reg);
18955 else if (tdie != NULL)
18956 add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
18957 add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
18958 if (next_arg != XEXP (arg, 1))
18960 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
18961 if (mode == VOIDmode)
18962 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
18963 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
18964 0), 1),
18965 mode, VOIDmode,
18966 VAR_INIT_STATUS_INITIALIZED);
18967 if (val != NULL)
18968 add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
18971 if (die == NULL
18972 && (ca_loc->symbol_ref || tloc))
18973 die = gen_call_site_die (decl, subr_die, ca_loc);
18974 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
18976 dw_loc_descr_ref tval = NULL;
18978 if (tloc != NULL_RTX)
18979 tval = mem_loc_descriptor (tloc,
18980 GET_MODE (tloc) == VOIDmode
18981 ? Pmode : GET_MODE (tloc),
18982 VOIDmode,
18983 VAR_INIT_STATUS_INITIALIZED);
18984 if (tval)
18985 add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
18986 else if (tlocc != NULL_RTX)
18988 tval = mem_loc_descriptor (tlocc,
18989 GET_MODE (tlocc) == VOIDmode
18990 ? Pmode : GET_MODE (tlocc),
18991 VOIDmode,
18992 VAR_INIT_STATUS_INITIALIZED);
18993 if (tval)
18994 add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
18995 tval);
18998 if (die != NULL)
19000 call_site_note_count++;
19001 if (ca_loc->tail_call_p)
19002 tail_call_site_note_count++;
19006 call_arg_locations = NULL;
19007 call_arg_loc_last = NULL;
19008 if (tail_call_site_count >= 0
19009 && tail_call_site_count == tail_call_site_note_count
19010 && !dwarf_strict)
19012 if (call_site_count >= 0
19013 && call_site_count == call_site_note_count)
19014 add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
19015 else
19016 add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
19018 call_site_count = -1;
19019 tail_call_site_count = -1;
19022 if (subr_die != old_die)
19023 /* Add the calling convention attribute if requested. */
19024 add_calling_convention_attribute (subr_die, decl);
19027 /* Returns a hash value for X (which really is a die_struct). */
19029 hashval_t
19030 block_die_hasher::hash (die_struct *d)
19032 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
19035 /* Return nonzero if decl_id and die_parent of die_struct X is the same
19036 as decl_id and die_parent of die_struct Y. */
19038 bool
19039 block_die_hasher::equal (die_struct *x, die_struct *y)
19041 return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
19044 /* Generate a DIE to represent a declared data object.
19045 Either DECL or ORIGIN must be non-null. */
19047 static void
19048 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
19050 HOST_WIDE_INT off = 0;
19051 tree com_decl;
19052 tree decl_or_origin = decl ? decl : origin;
19053 tree ultimate_origin;
19054 dw_die_ref var_die;
19055 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
19056 dw_die_ref origin_die;
19057 bool declaration = (DECL_EXTERNAL (decl_or_origin)
19058 || class_or_namespace_scope_p (context_die));
19059 bool specialization_p = false;
19061 ultimate_origin = decl_ultimate_origin (decl_or_origin);
19062 if (decl || ultimate_origin)
19063 origin = ultimate_origin;
19064 com_decl = fortran_common (decl_or_origin, &off);
19066 /* Symbol in common gets emitted as a child of the common block, in the form
19067 of a data member. */
19068 if (com_decl)
19070 dw_die_ref com_die;
19071 dw_loc_list_ref loc;
19072 die_node com_die_arg;
19074 var_die = lookup_decl_die (decl_or_origin);
19075 if (var_die)
19077 if (get_AT (var_die, DW_AT_location) == NULL)
19079 loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
19080 if (loc)
19082 if (off)
19084 /* Optimize the common case. */
19085 if (single_element_loc_list_p (loc)
19086 && loc->expr->dw_loc_opc == DW_OP_addr
19087 && loc->expr->dw_loc_next == NULL
19088 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
19089 == SYMBOL_REF)
19091 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
19092 loc->expr->dw_loc_oprnd1.v.val_addr
19093 = plus_constant (GET_MODE (x), x , off);
19095 else
19096 loc_list_plus_const (loc, off);
19098 add_AT_location_description (var_die, DW_AT_location, loc);
19099 remove_AT (var_die, DW_AT_declaration);
19102 return;
19105 if (common_block_die_table == NULL)
19106 common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
19108 com_die_arg.decl_id = DECL_UID (com_decl);
19109 com_die_arg.die_parent = context_die;
19110 com_die = common_block_die_table->find (&com_die_arg);
19111 loc = loc_list_from_tree (com_decl, 2, NULL);
19112 if (com_die == NULL)
19114 const char *cnam
19115 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
19116 die_node **slot;
19118 com_die = new_die (DW_TAG_common_block, context_die, decl);
19119 add_name_and_src_coords_attributes (com_die, com_decl);
19120 if (loc)
19122 add_AT_location_description (com_die, DW_AT_location, loc);
19123 /* Avoid sharing the same loc descriptor between
19124 DW_TAG_common_block and DW_TAG_variable. */
19125 loc = loc_list_from_tree (com_decl, 2, NULL);
19127 else if (DECL_EXTERNAL (decl))
19128 add_AT_flag (com_die, DW_AT_declaration, 1);
19129 if (want_pubnames ())
19130 add_pubname_string (cnam, com_die); /* ??? needed? */
19131 com_die->decl_id = DECL_UID (com_decl);
19132 slot = common_block_die_table->find_slot (com_die, INSERT);
19133 *slot = com_die;
19135 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
19137 add_AT_location_description (com_die, DW_AT_location, loc);
19138 loc = loc_list_from_tree (com_decl, 2, NULL);
19139 remove_AT (com_die, DW_AT_declaration);
19141 var_die = new_die (DW_TAG_variable, com_die, decl);
19142 add_name_and_src_coords_attributes (var_die, decl);
19143 add_type_attribute (var_die, TREE_TYPE (decl), decl_quals (decl),
19144 context_die);
19145 add_AT_flag (var_die, DW_AT_external, 1);
19146 if (loc)
19148 if (off)
19150 /* Optimize the common case. */
19151 if (single_element_loc_list_p (loc)
19152 && loc->expr->dw_loc_opc == DW_OP_addr
19153 && loc->expr->dw_loc_next == NULL
19154 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
19156 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
19157 loc->expr->dw_loc_oprnd1.v.val_addr
19158 = plus_constant (GET_MODE (x), x, off);
19160 else
19161 loc_list_plus_const (loc, off);
19163 add_AT_location_description (var_die, DW_AT_location, loc);
19165 else if (DECL_EXTERNAL (decl))
19166 add_AT_flag (var_die, DW_AT_declaration, 1);
19167 equate_decl_number_to_die (decl, var_die);
19168 return;
19171 /* If the compiler emitted a definition for the DECL declaration
19172 and if we already emitted a DIE for it, don't emit a second
19173 DIE for it again. Allow re-declarations of DECLs that are
19174 inside functions, though. */
19175 if (old_die && declaration && !local_scope_p (context_die))
19176 return;
19178 /* For static data members, the declaration in the class is supposed
19179 to have DW_TAG_member tag; the specification should still be
19180 DW_TAG_variable referencing the DW_TAG_member DIE. */
19181 if (declaration && class_scope_p (context_die))
19182 var_die = new_die (DW_TAG_member, context_die, decl);
19183 else
19184 var_die = new_die (DW_TAG_variable, context_die, decl);
19186 origin_die = NULL;
19187 if (origin != NULL)
19188 origin_die = add_abstract_origin_attribute (var_die, origin);
19190 /* Loop unrolling can create multiple blocks that refer to the same
19191 static variable, so we must test for the DW_AT_declaration flag.
19193 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
19194 copy decls and set the DECL_ABSTRACT_P flag on them instead of
19195 sharing them.
19197 ??? Duplicated blocks have been rewritten to use .debug_ranges.
19199 ??? The declare_in_namespace support causes us to get two DIEs for one
19200 variable, both of which are declarations. We want to avoid considering
19201 one to be a specification, so we must test that this DIE is not a
19202 declaration. */
19203 else if (old_die && TREE_STATIC (decl) && ! declaration
19204 && get_AT_flag (old_die, DW_AT_declaration) == 1)
19206 /* This is a definition of a C++ class level static. */
19207 add_AT_specification (var_die, old_die);
19208 specialization_p = true;
19209 if (DECL_NAME (decl))
19211 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
19212 struct dwarf_file_data * file_index = lookup_filename (s.file);
19214 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
19215 add_AT_file (var_die, DW_AT_decl_file, file_index);
19217 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
19218 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
19220 if (old_die->die_tag == DW_TAG_member)
19221 add_linkage_name (var_die, decl);
19224 else
19225 add_name_and_src_coords_attributes (var_die, decl);
19227 if ((origin == NULL && !specialization_p)
19228 || (origin != NULL
19229 && !DECL_ABSTRACT_P (decl_or_origin)
19230 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
19231 decl_function_context
19232 (decl_or_origin))))
19234 tree type = TREE_TYPE (decl_or_origin);
19236 if (decl_by_reference_p (decl_or_origin))
19237 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19238 context_die);
19239 else
19240 add_type_attribute (var_die, type, decl_quals (decl_or_origin),
19241 context_die);
19244 if (origin == NULL && !specialization_p)
19246 if (TREE_PUBLIC (decl))
19247 add_AT_flag (var_die, DW_AT_external, 1);
19249 if (DECL_ARTIFICIAL (decl))
19250 add_AT_flag (var_die, DW_AT_artificial, 1);
19252 add_accessibility_attribute (var_die, decl);
19255 if (declaration)
19256 add_AT_flag (var_die, DW_AT_declaration, 1);
19258 if (decl && (DECL_ABSTRACT_P (decl) || declaration || old_die == NULL))
19259 equate_decl_number_to_die (decl, var_die);
19261 if (! declaration
19262 && (! DECL_ABSTRACT_P (decl_or_origin)
19263 /* Local static vars are shared between all clones/inlines,
19264 so emit DW_AT_location on the abstract DIE if DECL_RTL is
19265 already set. */
19266 || (TREE_CODE (decl_or_origin) == VAR_DECL
19267 && TREE_STATIC (decl_or_origin)
19268 && DECL_RTL_SET_P (decl_or_origin)))
19269 /* When abstract origin already has DW_AT_location attribute, no need
19270 to add it again. */
19271 && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
19273 if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
19274 && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
19275 defer_location (decl_or_origin, var_die);
19276 else
19277 add_location_or_const_value_attribute (var_die, decl_or_origin,
19278 decl == NULL, DW_AT_location);
19279 add_pubname (decl_or_origin, var_die);
19281 else
19282 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
19285 /* Generate a DIE to represent a named constant. */
19287 static void
19288 gen_const_die (tree decl, dw_die_ref context_die)
19290 dw_die_ref const_die;
19291 tree type = TREE_TYPE (decl);
19293 const_die = new_die (DW_TAG_constant, context_die, decl);
19294 add_name_and_src_coords_attributes (const_die, decl);
19295 add_type_attribute (const_die, type, TYPE_QUAL_CONST, context_die);
19296 if (TREE_PUBLIC (decl))
19297 add_AT_flag (const_die, DW_AT_external, 1);
19298 if (DECL_ARTIFICIAL (decl))
19299 add_AT_flag (const_die, DW_AT_artificial, 1);
19300 tree_add_const_value_attribute_for_decl (const_die, decl);
19303 /* Generate a DIE to represent a label identifier. */
19305 static void
19306 gen_label_die (tree decl, dw_die_ref context_die)
19308 tree origin = decl_ultimate_origin (decl);
19309 dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
19310 rtx insn;
19311 char label[MAX_ARTIFICIAL_LABEL_BYTES];
19313 if (origin != NULL)
19314 add_abstract_origin_attribute (lbl_die, origin);
19315 else
19316 add_name_and_src_coords_attributes (lbl_die, decl);
19318 if (DECL_ABSTRACT_P (decl))
19319 equate_decl_number_to_die (decl, lbl_die);
19320 else
19322 insn = DECL_RTL_IF_SET (decl);
19324 /* Deleted labels are programmer specified labels which have been
19325 eliminated because of various optimizations. We still emit them
19326 here so that it is possible to put breakpoints on them. */
19327 if (insn
19328 && (LABEL_P (insn)
19329 || ((NOTE_P (insn)
19330 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
19332 /* When optimization is enabled (via -O) some parts of the compiler
19333 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
19334 represent source-level labels which were explicitly declared by
19335 the user. This really shouldn't be happening though, so catch
19336 it if it ever does happen. */
19337 gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
19339 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
19340 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19342 else if (insn
19343 && NOTE_P (insn)
19344 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
19345 && CODE_LABEL_NUMBER (insn) != -1)
19347 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
19348 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19353 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
19354 attributes to the DIE for a block STMT, to describe where the inlined
19355 function was called from. This is similar to add_src_coords_attributes. */
19357 static inline void
19358 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
19360 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
19362 if (dwarf_version >= 3 || !dwarf_strict)
19364 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
19365 add_AT_unsigned (die, DW_AT_call_line, s.line);
19370 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
19371 Add low_pc and high_pc attributes to the DIE for a block STMT. */
19373 static inline void
19374 add_high_low_attributes (tree stmt, dw_die_ref die)
19376 char label[MAX_ARTIFICIAL_LABEL_BYTES];
19378 if (BLOCK_FRAGMENT_CHAIN (stmt)
19379 && (dwarf_version >= 3 || !dwarf_strict))
19381 tree chain, superblock = NULL_TREE;
19382 dw_die_ref pdie;
19383 dw_attr_ref attr = NULL;
19385 if (inlined_function_outer_scope_p (stmt))
19387 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19388 BLOCK_NUMBER (stmt));
19389 add_AT_lbl_id (die, DW_AT_entry_pc, label);
19392 /* Optimize duplicate .debug_ranges lists or even tails of
19393 lists. If this BLOCK has same ranges as its supercontext,
19394 lookup DW_AT_ranges attribute in the supercontext (and
19395 recursively so), verify that the ranges_table contains the
19396 right values and use it instead of adding a new .debug_range. */
19397 for (chain = stmt, pdie = die;
19398 BLOCK_SAME_RANGE (chain);
19399 chain = BLOCK_SUPERCONTEXT (chain))
19401 dw_attr_ref new_attr;
19403 pdie = pdie->die_parent;
19404 if (pdie == NULL)
19405 break;
19406 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
19407 break;
19408 new_attr = get_AT (pdie, DW_AT_ranges);
19409 if (new_attr == NULL
19410 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
19411 break;
19412 attr = new_attr;
19413 superblock = BLOCK_SUPERCONTEXT (chain);
19415 if (attr != NULL
19416 && (ranges_table[attr->dw_attr_val.v.val_offset
19417 / 2 / DWARF2_ADDR_SIZE].num
19418 == BLOCK_NUMBER (superblock))
19419 && BLOCK_FRAGMENT_CHAIN (superblock))
19421 unsigned long off = attr->dw_attr_val.v.val_offset
19422 / 2 / DWARF2_ADDR_SIZE;
19423 unsigned long supercnt = 0, thiscnt = 0;
19424 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
19425 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
19427 ++supercnt;
19428 gcc_checking_assert (ranges_table[off + supercnt].num
19429 == BLOCK_NUMBER (chain));
19431 gcc_checking_assert (ranges_table[off + supercnt + 1].num == 0);
19432 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
19433 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
19434 ++thiscnt;
19435 gcc_assert (supercnt >= thiscnt);
19436 add_AT_range_list (die, DW_AT_ranges,
19437 ((off + supercnt - thiscnt)
19438 * 2 * DWARF2_ADDR_SIZE),
19439 false);
19440 return;
19443 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt), false);
19445 chain = BLOCK_FRAGMENT_CHAIN (stmt);
19448 add_ranges (chain);
19449 chain = BLOCK_FRAGMENT_CHAIN (chain);
19451 while (chain);
19452 add_ranges (NULL);
19454 else
19456 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
19457 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19458 BLOCK_NUMBER (stmt));
19459 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
19460 BLOCK_NUMBER (stmt));
19461 add_AT_low_high_pc (die, label, label_high, false);
19465 /* Generate a DIE for a lexical block. */
19467 static void
19468 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
19470 dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
19472 if (call_arg_locations)
19474 if (block_map.length () <= BLOCK_NUMBER (stmt))
19475 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
19476 block_map[BLOCK_NUMBER (stmt)] = stmt_die;
19479 if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
19480 add_high_low_attributes (stmt, stmt_die);
19482 decls_for_scope (stmt, stmt_die);
19485 /* Generate a DIE for an inlined subprogram. */
19487 static void
19488 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
19490 tree decl;
19492 /* The instance of function that is effectively being inlined shall not
19493 be abstract. */
19494 gcc_assert (! BLOCK_ABSTRACT (stmt));
19496 decl = block_ultimate_origin (stmt);
19498 /* Emit info for the abstract instance first, if we haven't yet. We
19499 must emit this even if the block is abstract, otherwise when we
19500 emit the block below (or elsewhere), we may end up trying to emit
19501 a die whose origin die hasn't been emitted, and crashing. */
19502 dwarf2out_abstract_function (decl);
19504 if (! BLOCK_ABSTRACT (stmt))
19506 dw_die_ref subr_die
19507 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
19509 if (call_arg_locations)
19511 if (block_map.length () <= BLOCK_NUMBER (stmt))
19512 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
19513 block_map[BLOCK_NUMBER (stmt)] = subr_die;
19515 add_abstract_origin_attribute (subr_die, decl);
19516 if (TREE_ASM_WRITTEN (stmt))
19517 add_high_low_attributes (stmt, subr_die);
19518 add_call_src_coords_attributes (stmt, subr_die);
19520 decls_for_scope (stmt, subr_die);
19524 /* Generate a DIE for a field in a record, or structure. */
19526 static void
19527 gen_field_die (tree decl, dw_die_ref context_die)
19529 dw_die_ref decl_die;
19531 if (TREE_TYPE (decl) == error_mark_node)
19532 return;
19534 decl_die = new_die (DW_TAG_member, context_die, decl);
19535 add_name_and_src_coords_attributes (decl_die, decl);
19536 add_type_attribute (decl_die, member_declared_type (decl),
19537 decl_quals (decl), context_die);
19539 if (DECL_BIT_FIELD_TYPE (decl))
19541 add_byte_size_attribute (decl_die, decl);
19542 add_bit_size_attribute (decl_die, decl);
19543 add_bit_offset_attribute (decl_die, decl);
19546 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
19547 add_data_member_location_attribute (decl_die, decl);
19549 if (DECL_ARTIFICIAL (decl))
19550 add_AT_flag (decl_die, DW_AT_artificial, 1);
19552 add_accessibility_attribute (decl_die, decl);
19554 /* Equate decl number to die, so that we can look up this decl later on. */
19555 equate_decl_number_to_die (decl, decl_die);
19558 #if 0
19559 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19560 Use modified_type_die instead.
19561 We keep this code here just in case these types of DIEs may be needed to
19562 represent certain things in other languages (e.g. Pascal) someday. */
19564 static void
19565 gen_pointer_type_die (tree type, dw_die_ref context_die)
19567 dw_die_ref ptr_die
19568 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
19570 equate_type_number_to_die (type, ptr_die);
19571 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19572 context_die);
19573 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19576 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19577 Use modified_type_die instead.
19578 We keep this code here just in case these types of DIEs may be needed to
19579 represent certain things in other languages (e.g. Pascal) someday. */
19581 static void
19582 gen_reference_type_die (tree type, dw_die_ref context_die)
19584 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
19586 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
19587 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
19588 else
19589 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
19591 equate_type_number_to_die (type, ref_die);
19592 add_type_attribute (ref_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19593 context_die);
19594 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19596 #endif
19598 /* Generate a DIE for a pointer to a member type. */
19600 static void
19601 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
19603 dw_die_ref ptr_die
19604 = new_die (DW_TAG_ptr_to_member_type,
19605 scope_die_for (type, context_die), type);
19607 equate_type_number_to_die (type, ptr_die);
19608 add_AT_die_ref (ptr_die, DW_AT_containing_type,
19609 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
19610 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19611 context_die);
19614 typedef const char *dchar_p; /* For DEF_VEC_P. */
19616 static char *producer_string;
19618 /* Return a heap allocated producer string including command line options
19619 if -grecord-gcc-switches. */
19621 static char *
19622 gen_producer_string (void)
19624 size_t j;
19625 auto_vec<dchar_p> switches;
19626 const char *language_string = lang_hooks.name;
19627 char *producer, *tail;
19628 const char *p;
19629 size_t len = dwarf_record_gcc_switches ? 0 : 3;
19630 size_t plen = strlen (language_string) + 1 + strlen (version_string);
19632 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
19633 switch (save_decoded_options[j].opt_index)
19635 case OPT_o:
19636 case OPT_d:
19637 case OPT_dumpbase:
19638 case OPT_dumpdir:
19639 case OPT_auxbase:
19640 case OPT_auxbase_strip:
19641 case OPT_quiet:
19642 case OPT_version:
19643 case OPT_v:
19644 case OPT_w:
19645 case OPT_L:
19646 case OPT_D:
19647 case OPT_I:
19648 case OPT_U:
19649 case OPT_SPECIAL_unknown:
19650 case OPT_SPECIAL_ignore:
19651 case OPT_SPECIAL_program_name:
19652 case OPT_SPECIAL_input_file:
19653 case OPT_grecord_gcc_switches:
19654 case OPT_gno_record_gcc_switches:
19655 case OPT__output_pch_:
19656 case OPT_fdiagnostics_show_location_:
19657 case OPT_fdiagnostics_show_option:
19658 case OPT_fdiagnostics_show_caret:
19659 case OPT_fdiagnostics_color_:
19660 case OPT_fverbose_asm:
19661 case OPT____:
19662 case OPT__sysroot_:
19663 case OPT_nostdinc:
19664 case OPT_nostdinc__:
19665 case OPT_fpreprocessed:
19666 case OPT_fltrans_output_list_:
19667 case OPT_fresolution_:
19668 /* Ignore these. */
19669 continue;
19670 default:
19671 if (cl_options[save_decoded_options[j].opt_index].flags
19672 & CL_NO_DWARF_RECORD)
19673 continue;
19674 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
19675 == '-');
19676 switch (save_decoded_options[j].canonical_option[0][1])
19678 case 'M':
19679 case 'i':
19680 case 'W':
19681 continue;
19682 case 'f':
19683 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
19684 "dump", 4) == 0)
19685 continue;
19686 break;
19687 default:
19688 break;
19690 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
19691 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
19692 break;
19695 producer = XNEWVEC (char, plen + 1 + len + 1);
19696 tail = producer;
19697 sprintf (tail, "%s %s", language_string, version_string);
19698 tail += plen;
19700 FOR_EACH_VEC_ELT (switches, j, p)
19702 len = strlen (p);
19703 *tail = ' ';
19704 memcpy (tail + 1, p, len);
19705 tail += len + 1;
19708 *tail = '\0';
19709 return producer;
19712 /* Given a C and/or C++ language/version string return the "highest".
19713 C++ is assumed to be "higher" than C in this case. Used for merging
19714 LTO translation unit languages. */
19715 static const char *
19716 highest_c_language (const char *lang1, const char *lang2)
19718 if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
19719 return "GNU C++14";
19720 if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
19721 return "GNU C++11";
19722 if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
19723 return "GNU C++98";
19725 if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
19726 return "GNU C11";
19727 if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
19728 return "GNU C99";
19729 if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
19730 return "GNU C89";
19732 gcc_unreachable ();
19736 /* Generate the DIE for the compilation unit. */
19738 static dw_die_ref
19739 gen_compile_unit_die (const char *filename)
19741 dw_die_ref die;
19742 const char *language_string = lang_hooks.name;
19743 int language;
19745 die = new_die (DW_TAG_compile_unit, NULL, NULL);
19747 if (filename)
19749 add_name_attribute (die, filename);
19750 /* Don't add cwd for <built-in>. */
19751 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
19752 add_comp_dir_attribute (die);
19755 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
19757 /* If our producer is LTO try to figure out a common language to use
19758 from the global list of translation units. */
19759 if (strcmp (language_string, "GNU GIMPLE") == 0)
19761 unsigned i;
19762 tree t;
19763 const char *common_lang = NULL;
19765 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
19767 if (!TRANSLATION_UNIT_LANGUAGE (t))
19768 continue;
19769 if (!common_lang)
19770 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
19771 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
19773 else if (strncmp (common_lang, "GNU C", 5) == 0
19774 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
19775 /* Mixing C and C++ is ok, use C++ in that case. */
19776 common_lang = highest_c_language (common_lang,
19777 TRANSLATION_UNIT_LANGUAGE (t));
19778 else
19780 /* Fall back to C. */
19781 common_lang = NULL;
19782 break;
19786 if (common_lang)
19787 language_string = common_lang;
19790 language = DW_LANG_C;
19791 if (strncmp (language_string, "GNU C", 5) == 0
19792 && ISDIGIT (language_string[5]))
19794 language = DW_LANG_C89;
19795 if (dwarf_version >= 3 || !dwarf_strict)
19797 if (strcmp (language_string, "GNU C89") != 0)
19798 language = DW_LANG_C99;
19800 if (dwarf_version >= 5 /* || !dwarf_strict */)
19801 if (strcmp (language_string, "GNU C11") == 0)
19802 language = DW_LANG_C11;
19805 else if (strncmp (language_string, "GNU C++", 7) == 0)
19807 language = DW_LANG_C_plus_plus;
19808 if (dwarf_version >= 5 /* || !dwarf_strict */)
19810 if (strcmp (language_string, "GNU C++11") == 0)
19811 language = DW_LANG_C_plus_plus_11;
19812 else if (strcmp (language_string, "GNU C++14") == 0)
19813 language = DW_LANG_C_plus_plus_14;
19816 else if (strcmp (language_string, "GNU F77") == 0)
19817 language = DW_LANG_Fortran77;
19818 else if (strcmp (language_string, "GNU Pascal") == 0)
19819 language = DW_LANG_Pascal83;
19820 else if (dwarf_version >= 3 || !dwarf_strict)
19822 if (strcmp (language_string, "GNU Ada") == 0)
19823 language = DW_LANG_Ada95;
19824 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
19826 language = DW_LANG_Fortran95;
19827 if (dwarf_version >= 5 /* || !dwarf_strict */)
19829 if (strcmp (language_string, "GNU Fortran2003") == 0)
19830 language = DW_LANG_Fortran03;
19831 else if (strcmp (language_string, "GNU Fortran2008") == 0)
19832 language = DW_LANG_Fortran08;
19835 else if (strcmp (language_string, "GNU Java") == 0)
19836 language = DW_LANG_Java;
19837 else if (strcmp (language_string, "GNU Objective-C") == 0)
19838 language = DW_LANG_ObjC;
19839 else if (strcmp (language_string, "GNU Objective-C++") == 0)
19840 language = DW_LANG_ObjC_plus_plus;
19841 else if (dwarf_version >= 5 || !dwarf_strict)
19843 if (strcmp (language_string, "GNU Go") == 0)
19844 language = DW_LANG_Go;
19847 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
19848 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
19849 language = DW_LANG_Fortran90;
19851 add_AT_unsigned (die, DW_AT_language, language);
19853 switch (language)
19855 case DW_LANG_Fortran77:
19856 case DW_LANG_Fortran90:
19857 case DW_LANG_Fortran95:
19858 case DW_LANG_Fortran03:
19859 case DW_LANG_Fortran08:
19860 /* Fortran has case insensitive identifiers and the front-end
19861 lowercases everything. */
19862 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
19863 break;
19864 default:
19865 /* The default DW_ID_case_sensitive doesn't need to be specified. */
19866 break;
19868 return die;
19871 /* Generate the DIE for a base class. */
19873 static void
19874 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
19876 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
19878 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, context_die);
19879 add_data_member_location_attribute (die, binfo);
19881 if (BINFO_VIRTUAL_P (binfo))
19882 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
19884 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
19885 children, otherwise the default is DW_ACCESS_public. In DWARF2
19886 the default has always been DW_ACCESS_private. */
19887 if (access == access_public_node)
19889 if (dwarf_version == 2
19890 || context_die->die_tag == DW_TAG_class_type)
19891 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
19893 else if (access == access_protected_node)
19894 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
19895 else if (dwarf_version > 2
19896 && context_die->die_tag != DW_TAG_class_type)
19897 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
19900 /* Generate a DIE for a class member. */
19902 static void
19903 gen_member_die (tree type, dw_die_ref context_die)
19905 tree member;
19906 tree binfo = TYPE_BINFO (type);
19907 dw_die_ref child;
19909 /* If this is not an incomplete type, output descriptions of each of its
19910 members. Note that as we output the DIEs necessary to represent the
19911 members of this record or union type, we will also be trying to output
19912 DIEs to represent the *types* of those members. However the `type'
19913 function (above) will specifically avoid generating type DIEs for member
19914 types *within* the list of member DIEs for this (containing) type except
19915 for those types (of members) which are explicitly marked as also being
19916 members of this (containing) type themselves. The g++ front- end can
19917 force any given type to be treated as a member of some other (containing)
19918 type by setting the TYPE_CONTEXT of the given (member) type to point to
19919 the TREE node representing the appropriate (containing) type. */
19921 /* First output info about the base classes. */
19922 if (binfo)
19924 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
19925 int i;
19926 tree base;
19928 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
19929 gen_inheritance_die (base,
19930 (accesses ? (*accesses)[i] : access_public_node),
19931 context_die);
19934 /* Now output info about the data members and type members. */
19935 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
19937 /* If we thought we were generating minimal debug info for TYPE
19938 and then changed our minds, some of the member declarations
19939 may have already been defined. Don't define them again, but
19940 do put them in the right order. */
19942 child = lookup_decl_die (member);
19943 if (child)
19944 splice_child_die (context_die, child);
19945 else
19946 gen_decl_die (member, NULL, context_die);
19949 /* Now output info about the function members (if any). */
19950 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
19952 /* Don't include clones in the member list. */
19953 if (DECL_ABSTRACT_ORIGIN (member))
19954 continue;
19955 /* Nor constructors for anonymous classes. */
19956 if (DECL_ARTIFICIAL (member)
19957 && dwarf2_name (member, 0) == NULL)
19958 continue;
19960 child = lookup_decl_die (member);
19961 if (child)
19962 splice_child_die (context_die, child);
19963 else
19964 gen_decl_die (member, NULL, context_die);
19968 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
19969 is set, we pretend that the type was never defined, so we only get the
19970 member DIEs needed by later specification DIEs. */
19972 static void
19973 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
19974 enum debug_info_usage usage)
19976 dw_die_ref type_die = lookup_type_die (type);
19977 dw_die_ref scope_die = 0;
19978 int nested = 0;
19979 int complete = (TYPE_SIZE (type)
19980 && (! TYPE_STUB_DECL (type)
19981 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
19982 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
19983 complete = complete && should_emit_struct_debug (type, usage);
19985 if (type_die && ! complete)
19986 return;
19988 if (TYPE_CONTEXT (type) != NULL_TREE
19989 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19990 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
19991 nested = 1;
19993 scope_die = scope_die_for (type, context_die);
19995 /* Generate child dies for template paramaters. */
19996 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
19997 schedule_generic_params_dies_gen (type);
19999 if (! type_die || (nested && is_cu_die (scope_die)))
20000 /* First occurrence of type or toplevel definition of nested class. */
20002 dw_die_ref old_die = type_die;
20004 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
20005 ? record_type_tag (type) : DW_TAG_union_type,
20006 scope_die, type);
20007 equate_type_number_to_die (type, type_die);
20008 if (old_die)
20009 add_AT_specification (type_die, old_die);
20010 else
20011 add_name_attribute (type_die, type_tag (type));
20013 else
20014 remove_AT (type_die, DW_AT_declaration);
20016 /* If this type has been completed, then give it a byte_size attribute and
20017 then give a list of members. */
20018 if (complete && !ns_decl)
20020 /* Prevent infinite recursion in cases where the type of some member of
20021 this type is expressed in terms of this type itself. */
20022 TREE_ASM_WRITTEN (type) = 1;
20023 add_byte_size_attribute (type_die, type);
20024 if (TYPE_STUB_DECL (type) != NULL_TREE)
20026 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
20027 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
20030 /* If the first reference to this type was as the return type of an
20031 inline function, then it may not have a parent. Fix this now. */
20032 if (type_die->die_parent == NULL)
20033 add_child_die (scope_die, type_die);
20035 push_decl_scope (type);
20036 gen_member_die (type, type_die);
20037 pop_decl_scope ();
20039 add_gnat_descriptive_type_attribute (type_die, type, context_die);
20040 if (TYPE_ARTIFICIAL (type))
20041 add_AT_flag (type_die, DW_AT_artificial, 1);
20043 /* GNU extension: Record what type our vtable lives in. */
20044 if (TYPE_VFIELD (type))
20046 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
20048 gen_type_die (vtype, context_die);
20049 add_AT_die_ref (type_die, DW_AT_containing_type,
20050 lookup_type_die (vtype));
20053 else
20055 add_AT_flag (type_die, DW_AT_declaration, 1);
20057 /* We don't need to do this for function-local types. */
20058 if (TYPE_STUB_DECL (type)
20059 && ! decl_function_context (TYPE_STUB_DECL (type)))
20060 vec_safe_push (incomplete_types, type);
20063 if (get_AT (type_die, DW_AT_name))
20064 add_pubtype (type, type_die);
20067 /* Generate a DIE for a subroutine _type_. */
20069 static void
20070 gen_subroutine_type_die (tree type, dw_die_ref context_die)
20072 tree return_type = TREE_TYPE (type);
20073 dw_die_ref subr_die
20074 = new_die (DW_TAG_subroutine_type,
20075 scope_die_for (type, context_die), type);
20077 equate_type_number_to_die (type, subr_die);
20078 add_prototyped_attribute (subr_die, type);
20079 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, context_die);
20080 gen_formal_types_die (type, subr_die);
20082 if (get_AT (subr_die, DW_AT_name))
20083 add_pubtype (type, subr_die);
20086 /* Generate a DIE for a type definition. */
20088 static void
20089 gen_typedef_die (tree decl, dw_die_ref context_die)
20091 dw_die_ref type_die;
20092 tree origin;
20094 if (TREE_ASM_WRITTEN (decl))
20095 return;
20097 TREE_ASM_WRITTEN (decl) = 1;
20098 type_die = new_die (DW_TAG_typedef, context_die, decl);
20099 origin = decl_ultimate_origin (decl);
20100 if (origin != NULL)
20101 add_abstract_origin_attribute (type_die, origin);
20102 else
20104 tree type;
20106 add_name_and_src_coords_attributes (type_die, decl);
20107 if (DECL_ORIGINAL_TYPE (decl))
20109 type = DECL_ORIGINAL_TYPE (decl);
20111 gcc_assert (type != TREE_TYPE (decl));
20112 equate_type_number_to_die (TREE_TYPE (decl), type_die);
20114 else
20116 type = TREE_TYPE (decl);
20118 if (is_naming_typedef_decl (TYPE_NAME (type)))
20120 /* Here, we are in the case of decl being a typedef naming
20121 an anonymous type, e.g:
20122 typedef struct {...} foo;
20123 In that case TREE_TYPE (decl) is not a typedef variant
20124 type and TYPE_NAME of the anonymous type is set to the
20125 TYPE_DECL of the typedef. This construct is emitted by
20126 the C++ FE.
20128 TYPE is the anonymous struct named by the typedef
20129 DECL. As we need the DW_AT_type attribute of the
20130 DW_TAG_typedef to point to the DIE of TYPE, let's
20131 generate that DIE right away. add_type_attribute
20132 called below will then pick (via lookup_type_die) that
20133 anonymous struct DIE. */
20134 if (!TREE_ASM_WRITTEN (type))
20135 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
20137 /* This is a GNU Extension. We are adding a
20138 DW_AT_linkage_name attribute to the DIE of the
20139 anonymous struct TYPE. The value of that attribute
20140 is the name of the typedef decl naming the anonymous
20141 struct. This greatly eases the work of consumers of
20142 this debug info. */
20143 add_linkage_attr (lookup_type_die (type), decl);
20147 add_type_attribute (type_die, type, decl_quals (decl), context_die);
20149 if (is_naming_typedef_decl (decl))
20150 /* We want that all subsequent calls to lookup_type_die with
20151 TYPE in argument yield the DW_TAG_typedef we have just
20152 created. */
20153 equate_type_number_to_die (type, type_die);
20155 add_accessibility_attribute (type_die, decl);
20158 if (DECL_ABSTRACT_P (decl))
20159 equate_decl_number_to_die (decl, type_die);
20161 if (get_AT (type_die, DW_AT_name))
20162 add_pubtype (decl, type_die);
20165 /* Generate a DIE for a struct, class, enum or union type. */
20167 static void
20168 gen_tagged_type_die (tree type,
20169 dw_die_ref context_die,
20170 enum debug_info_usage usage)
20172 int need_pop;
20174 if (type == NULL_TREE
20175 || !is_tagged_type (type))
20176 return;
20178 /* If this is a nested type whose containing class hasn't been written
20179 out yet, writing it out will cover this one, too. This does not apply
20180 to instantiations of member class templates; they need to be added to
20181 the containing class as they are generated. FIXME: This hurts the
20182 idea of combining type decls from multiple TUs, since we can't predict
20183 what set of template instantiations we'll get. */
20184 if (TYPE_CONTEXT (type)
20185 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
20186 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
20188 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
20190 if (TREE_ASM_WRITTEN (type))
20191 return;
20193 /* If that failed, attach ourselves to the stub. */
20194 push_decl_scope (TYPE_CONTEXT (type));
20195 context_die = lookup_type_die (TYPE_CONTEXT (type));
20196 need_pop = 1;
20198 else if (TYPE_CONTEXT (type) != NULL_TREE
20199 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
20201 /* If this type is local to a function that hasn't been written
20202 out yet, use a NULL context for now; it will be fixed up in
20203 decls_for_scope. */
20204 context_die = lookup_decl_die (TYPE_CONTEXT (type));
20205 /* A declaration DIE doesn't count; nested types need to go in the
20206 specification. */
20207 if (context_die && is_declaration_die (context_die))
20208 context_die = NULL;
20209 need_pop = 0;
20211 else
20213 context_die = declare_in_namespace (type, context_die);
20214 need_pop = 0;
20217 if (TREE_CODE (type) == ENUMERAL_TYPE)
20219 /* This might have been written out by the call to
20220 declare_in_namespace. */
20221 if (!TREE_ASM_WRITTEN (type))
20222 gen_enumeration_type_die (type, context_die);
20224 else
20225 gen_struct_or_union_type_die (type, context_die, usage);
20227 if (need_pop)
20228 pop_decl_scope ();
20230 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
20231 it up if it is ever completed. gen_*_type_die will set it for us
20232 when appropriate. */
20235 /* Generate a type description DIE. */
20237 static void
20238 gen_type_die_with_usage (tree type, dw_die_ref context_die,
20239 enum debug_info_usage usage)
20241 struct array_descr_info info;
20243 if (type == NULL_TREE || type == error_mark_node)
20244 return;
20246 #ifdef ENABLE_CHECKING
20247 if (type)
20248 verify_type (type);
20249 #endif
20251 if (TYPE_NAME (type) != NULL_TREE
20252 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
20253 && is_redundant_typedef (TYPE_NAME (type))
20254 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
20255 /* The DECL of this type is a typedef we don't want to emit debug
20256 info for but we want debug info for its underlying typedef.
20257 This can happen for e.g, the injected-class-name of a C++
20258 type. */
20259 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
20261 /* If TYPE is a typedef type variant, let's generate debug info
20262 for the parent typedef which TYPE is a type of. */
20263 if (typedef_variant_p (type))
20265 if (TREE_ASM_WRITTEN (type))
20266 return;
20268 /* Prevent broken recursion; we can't hand off to the same type. */
20269 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
20271 /* Give typedefs the right scope. */
20272 context_die = scope_die_for (type, context_die);
20274 TREE_ASM_WRITTEN (type) = 1;
20276 gen_decl_die (TYPE_NAME (type), NULL, context_die);
20277 return;
20280 /* If type is an anonymous tagged type named by a typedef, let's
20281 generate debug info for the typedef. */
20282 if (is_naming_typedef_decl (TYPE_NAME (type)))
20284 /* Use the DIE of the containing namespace as the parent DIE of
20285 the type description DIE we want to generate. */
20286 if (DECL_CONTEXT (TYPE_NAME (type))
20287 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
20288 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
20290 gen_decl_die (TYPE_NAME (type), NULL, context_die);
20291 return;
20294 /* We are going to output a DIE to represent the unqualified version
20295 of this type (i.e. without any const or volatile qualifiers) so
20296 get the main variant (i.e. the unqualified version) of this type
20297 now. (Vectors are special because the debugging info is in the
20298 cloned type itself). */
20299 if (TREE_CODE (type) != VECTOR_TYPE)
20300 type = type_main_variant (type);
20302 /* If this is an array type with hidden descriptor, handle it first. */
20303 if (!TREE_ASM_WRITTEN (type)
20304 && lang_hooks.types.get_array_descr_info)
20306 memset (&info, 0, sizeof (info));
20307 if (lang_hooks.types.get_array_descr_info (type, &info))
20309 gen_descr_array_type_die (type, &info, context_die);
20310 TREE_ASM_WRITTEN (type) = 1;
20311 return;
20315 if (TREE_ASM_WRITTEN (type))
20316 return;
20318 switch (TREE_CODE (type))
20320 case ERROR_MARK:
20321 break;
20323 case POINTER_TYPE:
20324 case REFERENCE_TYPE:
20325 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
20326 ensures that the gen_type_die recursion will terminate even if the
20327 type is recursive. Recursive types are possible in Ada. */
20328 /* ??? We could perhaps do this for all types before the switch
20329 statement. */
20330 TREE_ASM_WRITTEN (type) = 1;
20332 /* For these types, all that is required is that we output a DIE (or a
20333 set of DIEs) to represent the "basis" type. */
20334 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20335 DINFO_USAGE_IND_USE);
20336 break;
20338 case OFFSET_TYPE:
20339 /* This code is used for C++ pointer-to-data-member types.
20340 Output a description of the relevant class type. */
20341 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
20342 DINFO_USAGE_IND_USE);
20344 /* Output a description of the type of the object pointed to. */
20345 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20346 DINFO_USAGE_IND_USE);
20348 /* Now output a DIE to represent this pointer-to-data-member type
20349 itself. */
20350 gen_ptr_to_mbr_type_die (type, context_die);
20351 break;
20353 case FUNCTION_TYPE:
20354 /* Force out return type (in case it wasn't forced out already). */
20355 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20356 DINFO_USAGE_DIR_USE);
20357 gen_subroutine_type_die (type, context_die);
20358 break;
20360 case METHOD_TYPE:
20361 /* Force out return type (in case it wasn't forced out already). */
20362 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20363 DINFO_USAGE_DIR_USE);
20364 gen_subroutine_type_die (type, context_die);
20365 break;
20367 case ARRAY_TYPE:
20368 gen_array_type_die (type, context_die);
20369 break;
20371 case VECTOR_TYPE:
20372 gen_array_type_die (type, context_die);
20373 break;
20375 case ENUMERAL_TYPE:
20376 case RECORD_TYPE:
20377 case UNION_TYPE:
20378 case QUAL_UNION_TYPE:
20379 gen_tagged_type_die (type, context_die, usage);
20380 return;
20382 case VOID_TYPE:
20383 case INTEGER_TYPE:
20384 case REAL_TYPE:
20385 case FIXED_POINT_TYPE:
20386 case COMPLEX_TYPE:
20387 case BOOLEAN_TYPE:
20388 case POINTER_BOUNDS_TYPE:
20389 /* No DIEs needed for fundamental types. */
20390 break;
20392 case NULLPTR_TYPE:
20393 case LANG_TYPE:
20394 /* Just use DW_TAG_unspecified_type. */
20396 dw_die_ref type_die = lookup_type_die (type);
20397 if (type_die == NULL)
20399 tree name = TYPE_IDENTIFIER (type);
20400 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
20401 type);
20402 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
20403 equate_type_number_to_die (type, type_die);
20406 break;
20408 default:
20409 if (is_cxx_auto (type))
20411 tree name = TYPE_IDENTIFIER (type);
20412 dw_die_ref *die = (name == get_identifier ("auto")
20413 ? &auto_die : &decltype_auto_die);
20414 if (!*die)
20416 *die = new_die (DW_TAG_unspecified_type,
20417 comp_unit_die (), NULL_TREE);
20418 add_name_attribute (*die, IDENTIFIER_POINTER (name));
20420 equate_type_number_to_die (type, *die);
20421 break;
20423 gcc_unreachable ();
20426 TREE_ASM_WRITTEN (type) = 1;
20429 static void
20430 gen_type_die (tree type, dw_die_ref context_die)
20432 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
20435 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
20436 things which are local to the given block. */
20438 static void
20439 gen_block_die (tree stmt, dw_die_ref context_die)
20441 int must_output_die = 0;
20442 bool inlined_func;
20444 /* Ignore blocks that are NULL. */
20445 if (stmt == NULL_TREE)
20446 return;
20448 inlined_func = inlined_function_outer_scope_p (stmt);
20450 /* If the block is one fragment of a non-contiguous block, do not
20451 process the variables, since they will have been done by the
20452 origin block. Do process subblocks. */
20453 if (BLOCK_FRAGMENT_ORIGIN (stmt))
20455 tree sub;
20457 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
20458 gen_block_die (sub, context_die);
20460 return;
20463 /* Determine if we need to output any Dwarf DIEs at all to represent this
20464 block. */
20465 if (inlined_func)
20466 /* The outer scopes for inlinings *must* always be represented. We
20467 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
20468 must_output_die = 1;
20469 else
20471 /* Determine if this block directly contains any "significant"
20472 local declarations which we will need to output DIEs for. */
20473 if (debug_info_level > DINFO_LEVEL_TERSE)
20474 /* We are not in terse mode so *any* local declaration counts
20475 as being a "significant" one. */
20476 must_output_die = ((BLOCK_VARS (stmt) != NULL
20477 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
20478 && (TREE_USED (stmt)
20479 || TREE_ASM_WRITTEN (stmt)
20480 || BLOCK_ABSTRACT (stmt)));
20481 else if ((TREE_USED (stmt)
20482 || TREE_ASM_WRITTEN (stmt)
20483 || BLOCK_ABSTRACT (stmt))
20484 && !dwarf2out_ignore_block (stmt))
20485 must_output_die = 1;
20488 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
20489 DIE for any block which contains no significant local declarations at
20490 all. Rather, in such cases we just call `decls_for_scope' so that any
20491 needed Dwarf info for any sub-blocks will get properly generated. Note
20492 that in terse mode, our definition of what constitutes a "significant"
20493 local declaration gets restricted to include only inlined function
20494 instances and local (nested) function definitions. */
20495 if (must_output_die)
20497 if (inlined_func)
20499 /* If STMT block is abstract, that means we have been called
20500 indirectly from dwarf2out_abstract_function.
20501 That function rightfully marks the descendent blocks (of
20502 the abstract function it is dealing with) as being abstract,
20503 precisely to prevent us from emitting any
20504 DW_TAG_inlined_subroutine DIE as a descendent
20505 of an abstract function instance. So in that case, we should
20506 not call gen_inlined_subroutine_die.
20508 Later though, when cgraph asks dwarf2out to emit info
20509 for the concrete instance of the function decl into which
20510 the concrete instance of STMT got inlined, the later will lead
20511 to the generation of a DW_TAG_inlined_subroutine DIE. */
20512 if (! BLOCK_ABSTRACT (stmt))
20513 gen_inlined_subroutine_die (stmt, context_die);
20515 else
20516 gen_lexical_block_die (stmt, context_die);
20518 else
20519 decls_for_scope (stmt, context_die);
20522 /* Process variable DECL (or variable with origin ORIGIN) within
20523 block STMT and add it to CONTEXT_DIE. */
20524 static void
20525 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
20527 dw_die_ref die;
20528 tree decl_or_origin = decl ? decl : origin;
20530 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
20531 die = lookup_decl_die (decl_or_origin);
20532 else if (TREE_CODE (decl_or_origin) == TYPE_DECL
20533 && TYPE_DECL_IS_STUB (decl_or_origin))
20534 die = lookup_type_die (TREE_TYPE (decl_or_origin));
20535 else
20536 die = NULL;
20538 if (die != NULL && die->die_parent == NULL)
20539 add_child_die (context_die, die);
20540 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
20541 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
20542 stmt, context_die);
20543 else
20544 gen_decl_die (decl, origin, context_die);
20547 /* Generate all of the decls declared within a given scope and (recursively)
20548 all of its sub-blocks. */
20550 static void
20551 decls_for_scope (tree stmt, dw_die_ref context_die)
20553 tree decl;
20554 unsigned int i;
20555 tree subblocks;
20557 /* Ignore NULL blocks. */
20558 if (stmt == NULL_TREE)
20559 return;
20561 /* Output the DIEs to represent all of the data objects and typedefs
20562 declared directly within this block but not within any nested
20563 sub-blocks. Also, nested function and tag DIEs have been
20564 generated with a parent of NULL; fix that up now. We don't
20565 have to do this if we're at -g1. */
20566 if (debug_info_level > DINFO_LEVEL_TERSE)
20568 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
20569 process_scope_var (stmt, decl, NULL_TREE, context_die);
20570 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
20571 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
20572 context_die);
20575 /* Even if we're at -g1, we need to process the subblocks in order to get
20576 inlined call information. */
20578 /* Output the DIEs to represent all sub-blocks (and the items declared
20579 therein) of this block. */
20580 for (subblocks = BLOCK_SUBBLOCKS (stmt);
20581 subblocks != NULL;
20582 subblocks = BLOCK_CHAIN (subblocks))
20583 gen_block_die (subblocks, context_die);
20586 /* Is this a typedef we can avoid emitting? */
20588 static inline int
20589 is_redundant_typedef (const_tree decl)
20591 if (TYPE_DECL_IS_STUB (decl))
20592 return 1;
20594 if (DECL_ARTIFICIAL (decl)
20595 && DECL_CONTEXT (decl)
20596 && is_tagged_type (DECL_CONTEXT (decl))
20597 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
20598 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
20599 /* Also ignore the artificial member typedef for the class name. */
20600 return 1;
20602 return 0;
20605 /* Return TRUE if TYPE is a typedef that names a type for linkage
20606 purposes. This kind of typedefs is produced by the C++ FE for
20607 constructs like:
20609 typedef struct {...} foo;
20611 In that case, there is no typedef variant type produced for foo.
20612 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
20613 struct type. */
20615 static bool
20616 is_naming_typedef_decl (const_tree decl)
20618 if (decl == NULL_TREE
20619 || TREE_CODE (decl) != TYPE_DECL
20620 || !is_tagged_type (TREE_TYPE (decl))
20621 || DECL_IS_BUILTIN (decl)
20622 || is_redundant_typedef (decl)
20623 /* It looks like Ada produces TYPE_DECLs that are very similar
20624 to C++ naming typedefs but that have different
20625 semantics. Let's be specific to c++ for now. */
20626 || !is_cxx ())
20627 return FALSE;
20629 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
20630 && TYPE_NAME (TREE_TYPE (decl)) == decl
20631 && (TYPE_STUB_DECL (TREE_TYPE (decl))
20632 != TYPE_NAME (TREE_TYPE (decl))));
20635 /* Returns the DIE for a context. */
20637 static inline dw_die_ref
20638 get_context_die (tree context)
20640 if (context)
20642 /* Find die that represents this context. */
20643 if (TYPE_P (context))
20645 context = TYPE_MAIN_VARIANT (context);
20646 return strip_naming_typedef (context, force_type_die (context));
20648 else
20649 return force_decl_die (context);
20651 return comp_unit_die ();
20654 /* Returns the DIE for decl. A DIE will always be returned. */
20656 static dw_die_ref
20657 force_decl_die (tree decl)
20659 dw_die_ref decl_die;
20660 unsigned saved_external_flag;
20661 tree save_fn = NULL_TREE;
20662 decl_die = lookup_decl_die (decl);
20663 if (!decl_die)
20665 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
20667 decl_die = lookup_decl_die (decl);
20668 if (decl_die)
20669 return decl_die;
20671 switch (TREE_CODE (decl))
20673 case FUNCTION_DECL:
20674 /* Clear current_function_decl, so that gen_subprogram_die thinks
20675 that this is a declaration. At this point, we just want to force
20676 declaration die. */
20677 save_fn = current_function_decl;
20678 current_function_decl = NULL_TREE;
20679 gen_subprogram_die (decl, context_die);
20680 current_function_decl = save_fn;
20681 break;
20683 case VAR_DECL:
20684 /* Set external flag to force declaration die. Restore it after
20685 gen_decl_die() call. */
20686 saved_external_flag = DECL_EXTERNAL (decl);
20687 DECL_EXTERNAL (decl) = 1;
20688 gen_decl_die (decl, NULL, context_die);
20689 DECL_EXTERNAL (decl) = saved_external_flag;
20690 break;
20692 case NAMESPACE_DECL:
20693 if (dwarf_version >= 3 || !dwarf_strict)
20694 dwarf2out_decl (decl);
20695 else
20696 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
20697 decl_die = comp_unit_die ();
20698 break;
20700 case TRANSLATION_UNIT_DECL:
20701 decl_die = comp_unit_die ();
20702 break;
20704 default:
20705 gcc_unreachable ();
20708 /* We should be able to find the DIE now. */
20709 if (!decl_die)
20710 decl_die = lookup_decl_die (decl);
20711 gcc_assert (decl_die);
20714 return decl_die;
20717 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
20718 always returned. */
20720 static dw_die_ref
20721 force_type_die (tree type)
20723 dw_die_ref type_die;
20725 type_die = lookup_type_die (type);
20726 if (!type_die)
20728 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
20730 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
20731 context_die);
20732 gcc_assert (type_die);
20734 return type_die;
20737 /* Force out any required namespaces to be able to output DECL,
20738 and return the new context_die for it, if it's changed. */
20740 static dw_die_ref
20741 setup_namespace_context (tree thing, dw_die_ref context_die)
20743 tree context = (DECL_P (thing)
20744 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
20745 if (context && TREE_CODE (context) == NAMESPACE_DECL)
20746 /* Force out the namespace. */
20747 context_die = force_decl_die (context);
20749 return context_die;
20752 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
20753 type) within its namespace, if appropriate.
20755 For compatibility with older debuggers, namespace DIEs only contain
20756 declarations; all definitions are emitted at CU scope. */
20758 static dw_die_ref
20759 declare_in_namespace (tree thing, dw_die_ref context_die)
20761 dw_die_ref ns_context;
20763 if (debug_info_level <= DINFO_LEVEL_TERSE)
20764 return context_die;
20766 /* External declarations in the local scope only need to be emitted
20767 once, not once in the namespace and once in the scope.
20769 This avoids declaring the `extern' below in the
20770 namespace DIE as well as in the innermost scope:
20772 namespace S
20774 int i=5;
20775 int foo()
20777 int i=8;
20778 extern int i;
20779 return i;
20783 if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
20784 return context_die;
20786 /* If this decl is from an inlined function, then don't try to emit it in its
20787 namespace, as we will get confused. It would have already been emitted
20788 when the abstract instance of the inline function was emitted anyways. */
20789 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
20790 return context_die;
20792 ns_context = setup_namespace_context (thing, context_die);
20794 if (ns_context != context_die)
20796 if (is_fortran ())
20797 return ns_context;
20798 if (DECL_P (thing))
20799 gen_decl_die (thing, NULL, ns_context);
20800 else
20801 gen_type_die (thing, ns_context);
20803 return context_die;
20806 /* Generate a DIE for a namespace or namespace alias. */
20808 static void
20809 gen_namespace_die (tree decl, dw_die_ref context_die)
20811 dw_die_ref namespace_die;
20813 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
20814 they are an alias of. */
20815 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
20817 /* Output a real namespace or module. */
20818 context_die = setup_namespace_context (decl, comp_unit_die ());
20819 namespace_die = new_die (is_fortran ()
20820 ? DW_TAG_module : DW_TAG_namespace,
20821 context_die, decl);
20822 /* For Fortran modules defined in different CU don't add src coords. */
20823 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
20825 const char *name = dwarf2_name (decl, 0);
20826 if (name)
20827 add_name_attribute (namespace_die, name);
20829 else
20830 add_name_and_src_coords_attributes (namespace_die, decl);
20831 if (DECL_EXTERNAL (decl))
20832 add_AT_flag (namespace_die, DW_AT_declaration, 1);
20833 equate_decl_number_to_die (decl, namespace_die);
20835 else
20837 /* Output a namespace alias. */
20839 /* Force out the namespace we are an alias of, if necessary. */
20840 dw_die_ref origin_die
20841 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
20843 if (DECL_FILE_SCOPE_P (decl)
20844 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
20845 context_die = setup_namespace_context (decl, comp_unit_die ());
20846 /* Now create the namespace alias DIE. */
20847 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
20848 add_name_and_src_coords_attributes (namespace_die, decl);
20849 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
20850 equate_decl_number_to_die (decl, namespace_die);
20852 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
20853 if (want_pubnames ())
20854 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
20857 /* Generate Dwarf debug information for a decl described by DECL.
20858 The return value is currently only meaningful for PARM_DECLs,
20859 for all other decls it returns NULL. */
20861 static dw_die_ref
20862 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
20864 tree decl_or_origin = decl ? decl : origin;
20865 tree class_origin = NULL, ultimate_origin;
20867 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
20868 return NULL;
20870 /* Ignore pointer bounds decls. */
20871 if (DECL_P (decl_or_origin)
20872 && TREE_TYPE (decl_or_origin)
20873 && POINTER_BOUNDS_P (decl_or_origin))
20874 return NULL;
20876 switch (TREE_CODE (decl_or_origin))
20878 case ERROR_MARK:
20879 break;
20881 case CONST_DECL:
20882 if (!is_fortran () && !is_ada ())
20884 /* The individual enumerators of an enum type get output when we output
20885 the Dwarf representation of the relevant enum type itself. */
20886 break;
20889 /* Emit its type. */
20890 gen_type_die (TREE_TYPE (decl), context_die);
20892 /* And its containing namespace. */
20893 context_die = declare_in_namespace (decl, context_die);
20895 gen_const_die (decl, context_die);
20896 break;
20898 case FUNCTION_DECL:
20899 /* Don't output any DIEs to represent mere function declarations,
20900 unless they are class members or explicit block externs. */
20901 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
20902 && DECL_FILE_SCOPE_P (decl_or_origin)
20903 && (current_function_decl == NULL_TREE
20904 || DECL_ARTIFICIAL (decl_or_origin)))
20905 break;
20907 #if 0
20908 /* FIXME */
20909 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
20910 on local redeclarations of global functions. That seems broken. */
20911 if (current_function_decl != decl)
20912 /* This is only a declaration. */;
20913 #endif
20915 /* If we're emitting a clone, emit info for the abstract instance. */
20916 if (origin || DECL_ORIGIN (decl) != decl)
20917 dwarf2out_abstract_function (origin
20918 ? DECL_ORIGIN (origin)
20919 : DECL_ABSTRACT_ORIGIN (decl));
20921 /* If we're emitting an out-of-line copy of an inline function,
20922 emit info for the abstract instance and set up to refer to it. */
20923 else if (cgraph_function_possibly_inlined_p (decl)
20924 && ! DECL_ABSTRACT_P (decl)
20925 && ! class_or_namespace_scope_p (context_die)
20926 /* dwarf2out_abstract_function won't emit a die if this is just
20927 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
20928 that case, because that works only if we have a die. */
20929 && DECL_INITIAL (decl) != NULL_TREE)
20931 dwarf2out_abstract_function (decl);
20932 set_decl_origin_self (decl);
20935 /* Otherwise we're emitting the primary DIE for this decl. */
20936 else if (debug_info_level > DINFO_LEVEL_TERSE)
20938 /* Before we describe the FUNCTION_DECL itself, make sure that we
20939 have its containing type. */
20940 if (!origin)
20941 origin = decl_class_context (decl);
20942 if (origin != NULL_TREE)
20943 gen_type_die (origin, context_die);
20945 /* And its return type. */
20946 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
20948 /* And its virtual context. */
20949 if (DECL_VINDEX (decl) != NULL_TREE)
20950 gen_type_die (DECL_CONTEXT (decl), context_die);
20952 /* Make sure we have a member DIE for decl. */
20953 if (origin != NULL_TREE)
20954 gen_type_die_for_member (origin, decl, context_die);
20956 /* And its containing namespace. */
20957 context_die = declare_in_namespace (decl, context_die);
20960 /* Now output a DIE to represent the function itself. */
20961 if (decl)
20962 gen_subprogram_die (decl, context_die);
20963 break;
20965 case TYPE_DECL:
20966 /* If we are in terse mode, don't generate any DIEs to represent any
20967 actual typedefs. */
20968 if (debug_info_level <= DINFO_LEVEL_TERSE)
20969 break;
20971 /* In the special case of a TYPE_DECL node representing the declaration
20972 of some type tag, if the given TYPE_DECL is marked as having been
20973 instantiated from some other (original) TYPE_DECL node (e.g. one which
20974 was generated within the original definition of an inline function) we
20975 used to generate a special (abbreviated) DW_TAG_structure_type,
20976 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
20977 should be actually referencing those DIEs, as variable DIEs with that
20978 type would be emitted already in the abstract origin, so it was always
20979 removed during unused type prunning. Don't add anything in this
20980 case. */
20981 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
20982 break;
20984 if (is_redundant_typedef (decl))
20985 gen_type_die (TREE_TYPE (decl), context_die);
20986 else
20987 /* Output a DIE to represent the typedef itself. */
20988 gen_typedef_die (decl, context_die);
20989 break;
20991 case LABEL_DECL:
20992 if (debug_info_level >= DINFO_LEVEL_NORMAL)
20993 gen_label_die (decl, context_die);
20994 break;
20996 case VAR_DECL:
20997 case RESULT_DECL:
20998 /* If we are in terse mode, don't generate any DIEs to represent any
20999 variable declarations or definitions. */
21000 if (debug_info_level <= DINFO_LEVEL_TERSE)
21001 break;
21003 /* Output any DIEs that are needed to specify the type of this data
21004 object. */
21005 if (decl_by_reference_p (decl_or_origin))
21006 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
21007 else
21008 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
21010 /* And its containing type. */
21011 class_origin = decl_class_context (decl_or_origin);
21012 if (class_origin != NULL_TREE)
21013 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
21015 /* And its containing namespace. */
21016 context_die = declare_in_namespace (decl_or_origin, context_die);
21018 /* Now output the DIE to represent the data object itself. This gets
21019 complicated because of the possibility that the VAR_DECL really
21020 represents an inlined instance of a formal parameter for an inline
21021 function. */
21022 ultimate_origin = decl_ultimate_origin (decl_or_origin);
21023 if (ultimate_origin != NULL_TREE
21024 && TREE_CODE (ultimate_origin) == PARM_DECL)
21025 gen_formal_parameter_die (decl, origin,
21026 true /* Emit name attribute. */,
21027 context_die);
21028 else
21029 gen_variable_die (decl, origin, context_die);
21030 break;
21032 case FIELD_DECL:
21033 /* Ignore the nameless fields that are used to skip bits but handle C++
21034 anonymous unions and structs. */
21035 if (DECL_NAME (decl) != NULL_TREE
21036 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
21037 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
21039 gen_type_die (member_declared_type (decl), context_die);
21040 gen_field_die (decl, context_die);
21042 break;
21044 case PARM_DECL:
21045 if (DECL_BY_REFERENCE (decl_or_origin))
21046 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
21047 else
21048 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
21049 return gen_formal_parameter_die (decl, origin,
21050 true /* Emit name attribute. */,
21051 context_die);
21053 case NAMESPACE_DECL:
21054 case IMPORTED_DECL:
21055 if (dwarf_version >= 3 || !dwarf_strict)
21056 gen_namespace_die (decl, context_die);
21057 break;
21059 case NAMELIST_DECL:
21060 gen_namelist_decl (DECL_NAME (decl), context_die,
21061 NAMELIST_DECL_ASSOCIATED_DECL (decl));
21062 break;
21064 default:
21065 /* Probably some frontend-internal decl. Assume we don't care. */
21066 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
21067 break;
21070 return NULL;
21073 /* Output debug information for global decl DECL. Called from toplev.c after
21074 compilation proper has finished. */
21076 static void
21077 dwarf2out_global_decl (tree decl)
21079 /* Output DWARF2 information for file-scope tentative data object
21080 declarations, file-scope (extern) function declarations (which
21081 had no corresponding body) and file-scope tagged type declarations
21082 and definitions which have not yet been forced out. */
21083 if ((TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
21084 && !POINTER_BOUNDS_P (decl))
21085 dwarf2out_decl (decl);
21088 /* Output debug information for type decl DECL. Called from toplev.c
21089 and from language front ends (to record built-in types). */
21090 static void
21091 dwarf2out_type_decl (tree decl, int local)
21093 if (!local)
21094 dwarf2out_decl (decl);
21097 /* Output debug information for imported module or decl DECL.
21098 NAME is non-NULL name in the lexical block if the decl has been renamed.
21099 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
21100 that DECL belongs to.
21101 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
21102 static void
21103 dwarf2out_imported_module_or_decl_1 (tree decl,
21104 tree name,
21105 tree lexical_block,
21106 dw_die_ref lexical_block_die)
21108 expanded_location xloc;
21109 dw_die_ref imported_die = NULL;
21110 dw_die_ref at_import_die;
21112 if (TREE_CODE (decl) == IMPORTED_DECL)
21114 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
21115 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
21116 gcc_assert (decl);
21118 else
21119 xloc = expand_location (input_location);
21121 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
21123 at_import_die = force_type_die (TREE_TYPE (decl));
21124 /* For namespace N { typedef void T; } using N::T; base_type_die
21125 returns NULL, but DW_TAG_imported_declaration requires
21126 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
21127 if (!at_import_die)
21129 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
21130 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
21131 at_import_die = lookup_type_die (TREE_TYPE (decl));
21132 gcc_assert (at_import_die);
21135 else
21137 at_import_die = lookup_decl_die (decl);
21138 if (!at_import_die)
21140 /* If we're trying to avoid duplicate debug info, we may not have
21141 emitted the member decl for this field. Emit it now. */
21142 if (TREE_CODE (decl) == FIELD_DECL)
21144 tree type = DECL_CONTEXT (decl);
21146 if (TYPE_CONTEXT (type)
21147 && TYPE_P (TYPE_CONTEXT (type))
21148 && !should_emit_struct_debug (TYPE_CONTEXT (type),
21149 DINFO_USAGE_DIR_USE))
21150 return;
21151 gen_type_die_for_member (type, decl,
21152 get_context_die (TYPE_CONTEXT (type)));
21154 if (TREE_CODE (decl) == NAMELIST_DECL)
21155 at_import_die = gen_namelist_decl (DECL_NAME (decl),
21156 get_context_die (DECL_CONTEXT (decl)),
21157 NULL_TREE);
21158 else
21159 at_import_die = force_decl_die (decl);
21163 if (TREE_CODE (decl) == NAMESPACE_DECL)
21165 if (dwarf_version >= 3 || !dwarf_strict)
21166 imported_die = new_die (DW_TAG_imported_module,
21167 lexical_block_die,
21168 lexical_block);
21169 else
21170 return;
21172 else
21173 imported_die = new_die (DW_TAG_imported_declaration,
21174 lexical_block_die,
21175 lexical_block);
21177 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
21178 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
21179 if (name)
21180 add_AT_string (imported_die, DW_AT_name,
21181 IDENTIFIER_POINTER (name));
21182 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
21185 /* Output debug information for imported module or decl DECL.
21186 NAME is non-NULL name in context if the decl has been renamed.
21187 CHILD is true if decl is one of the renamed decls as part of
21188 importing whole module. */
21190 static void
21191 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
21192 bool child)
21194 /* dw_die_ref at_import_die; */
21195 dw_die_ref scope_die;
21197 if (debug_info_level <= DINFO_LEVEL_TERSE)
21198 return;
21200 gcc_assert (decl);
21202 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
21203 We need decl DIE for reference and scope die. First, get DIE for the decl
21204 itself. */
21206 /* Get the scope die for decl context. Use comp_unit_die for global module
21207 or decl. If die is not found for non globals, force new die. */
21208 if (context
21209 && TYPE_P (context)
21210 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
21211 return;
21213 if (!(dwarf_version >= 3 || !dwarf_strict))
21214 return;
21216 scope_die = get_context_die (context);
21218 if (child)
21220 gcc_assert (scope_die->die_child);
21221 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
21222 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
21223 scope_die = scope_die->die_child;
21226 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
21227 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
21231 /* Output debug information for namelists. */
21233 static dw_die_ref
21234 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
21236 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
21237 tree value;
21238 unsigned i;
21240 if (debug_info_level <= DINFO_LEVEL_TERSE)
21241 return NULL;
21243 gcc_assert (scope_die != NULL);
21244 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
21245 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
21247 /* If there are no item_decls, we have a nondefining namelist, e.g.
21248 with USE association; hence, set DW_AT_declaration. */
21249 if (item_decls == NULL_TREE)
21251 add_AT_flag (nml_die, DW_AT_declaration, 1);
21252 return nml_die;
21255 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
21257 nml_item_ref_die = lookup_decl_die (value);
21258 if (!nml_item_ref_die)
21259 nml_item_ref_die = force_decl_die (value);
21261 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
21262 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
21264 return nml_die;
21268 /* Write the debugging output for DECL. */
21270 static void
21271 dwarf2out_decl (tree decl)
21273 dw_die_ref context_die = comp_unit_die ();
21275 switch (TREE_CODE (decl))
21277 case ERROR_MARK:
21278 return;
21280 case FUNCTION_DECL:
21281 /* What we would really like to do here is to filter out all mere
21282 file-scope declarations of file-scope functions which are never
21283 referenced later within this translation unit (and keep all of ones
21284 that *are* referenced later on) but we aren't clairvoyant, so we have
21285 no idea which functions will be referenced in the future (i.e. later
21286 on within the current translation unit). So here we just ignore all
21287 file-scope function declarations which are not also definitions. If
21288 and when the debugger needs to know something about these functions,
21289 it will have to hunt around and find the DWARF information associated
21290 with the definition of the function.
21292 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
21293 nodes represent definitions and which ones represent mere
21294 declarations. We have to check DECL_INITIAL instead. That's because
21295 the C front-end supports some weird semantics for "extern inline"
21296 function definitions. These can get inlined within the current
21297 translation unit (and thus, we need to generate Dwarf info for their
21298 abstract instances so that the Dwarf info for the concrete inlined
21299 instances can have something to refer to) but the compiler never
21300 generates any out-of-lines instances of such things (despite the fact
21301 that they *are* definitions).
21303 The important point is that the C front-end marks these "extern
21304 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
21305 them anyway. Note that the C++ front-end also plays some similar games
21306 for inline function definitions appearing within include files which
21307 also contain `#pragma interface' pragmas.
21309 If we are called from dwarf2out_abstract_function output a DIE
21310 anyway. We can end up here this way with early inlining and LTO
21311 where the inlined function is output in a different LTRANS unit
21312 or not at all. */
21313 if (DECL_INITIAL (decl) == NULL_TREE
21314 && ! DECL_ABSTRACT_P (decl))
21315 return;
21317 /* If we're a nested function, initially use a parent of NULL; if we're
21318 a plain function, this will be fixed up in decls_for_scope. If
21319 we're a method, it will be ignored, since we already have a DIE. */
21320 if (decl_function_context (decl)
21321 /* But if we're in terse mode, we don't care about scope. */
21322 && debug_info_level > DINFO_LEVEL_TERSE)
21323 context_die = NULL;
21324 break;
21326 case VAR_DECL:
21327 /* Ignore this VAR_DECL if it refers to a file-scope extern data object
21328 declaration and if the declaration was never even referenced from
21329 within this entire compilation unit. We suppress these DIEs in
21330 order to save space in the .debug section (by eliminating entries
21331 which are probably useless). Note that we must not suppress
21332 block-local extern declarations (whether used or not) because that
21333 would screw-up the debugger's name lookup mechanism and cause it to
21334 miss things which really ought to be in scope at a given point. */
21335 if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
21336 return;
21338 /* For local statics lookup proper context die. */
21339 if (TREE_STATIC (decl)
21340 && DECL_CONTEXT (decl)
21341 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
21342 context_die = lookup_decl_die (DECL_CONTEXT (decl));
21344 /* If we are in terse mode, don't generate any DIEs to represent any
21345 variable declarations or definitions. */
21346 if (debug_info_level <= DINFO_LEVEL_TERSE)
21347 return;
21348 break;
21350 case CONST_DECL:
21351 if (debug_info_level <= DINFO_LEVEL_TERSE)
21352 return;
21353 if (!is_fortran () && !is_ada ())
21354 return;
21355 if (TREE_STATIC (decl) && decl_function_context (decl))
21356 context_die = lookup_decl_die (DECL_CONTEXT (decl));
21357 break;
21359 case NAMESPACE_DECL:
21360 case IMPORTED_DECL:
21361 if (debug_info_level <= DINFO_LEVEL_TERSE)
21362 return;
21363 if (lookup_decl_die (decl) != NULL)
21364 return;
21365 break;
21367 case TYPE_DECL:
21368 /* Don't emit stubs for types unless they are needed by other DIEs. */
21369 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
21370 return;
21372 /* Don't bother trying to generate any DIEs to represent any of the
21373 normal built-in types for the language we are compiling. */
21374 if (DECL_IS_BUILTIN (decl))
21375 return;
21377 /* If we are in terse mode, don't generate any DIEs for types. */
21378 if (debug_info_level <= DINFO_LEVEL_TERSE)
21379 return;
21381 /* If we're a function-scope tag, initially use a parent of NULL;
21382 this will be fixed up in decls_for_scope. */
21383 if (decl_function_context (decl))
21384 context_die = NULL;
21386 break;
21388 case NAMELIST_DECL:
21389 break;
21391 default:
21392 return;
21395 gen_decl_die (decl, NULL, context_die);
21398 /* Write the debugging output for DECL. */
21400 static void
21401 dwarf2out_function_decl (tree decl)
21403 dwarf2out_decl (decl);
21404 call_arg_locations = NULL;
21405 call_arg_loc_last = NULL;
21406 call_site_count = -1;
21407 tail_call_site_count = -1;
21408 block_map.release ();
21409 decl_loc_table->empty ();
21410 cached_dw_loc_list_table->empty ();
21413 /* Output a marker (i.e. a label) for the beginning of the generated code for
21414 a lexical block. */
21416 static void
21417 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
21418 unsigned int blocknum)
21420 switch_to_section (current_function_section ());
21421 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
21424 /* Output a marker (i.e. a label) for the end of the generated code for a
21425 lexical block. */
21427 static void
21428 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
21430 switch_to_section (current_function_section ());
21431 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
21434 /* Returns nonzero if it is appropriate not to emit any debugging
21435 information for BLOCK, because it doesn't contain any instructions.
21437 Don't allow this for blocks with nested functions or local classes
21438 as we would end up with orphans, and in the presence of scheduling
21439 we may end up calling them anyway. */
21441 static bool
21442 dwarf2out_ignore_block (const_tree block)
21444 tree decl;
21445 unsigned int i;
21447 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
21448 if (TREE_CODE (decl) == FUNCTION_DECL
21449 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21450 return 0;
21451 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
21453 decl = BLOCK_NONLOCALIZED_VAR (block, i);
21454 if (TREE_CODE (decl) == FUNCTION_DECL
21455 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21456 return 0;
21459 return 1;
21462 /* Hash table routines for file_hash. */
21464 bool
21465 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
21467 return filename_cmp (p1->filename, p2) == 0;
21470 hashval_t
21471 dwarf_file_hasher::hash (dwarf_file_data *p)
21473 return htab_hash_string (p->filename);
21476 /* Lookup FILE_NAME (in the list of filenames that we know about here in
21477 dwarf2out.c) and return its "index". The index of each (known) filename is
21478 just a unique number which is associated with only that one filename. We
21479 need such numbers for the sake of generating labels (in the .debug_sfnames
21480 section) and references to those files numbers (in the .debug_srcinfo
21481 and.debug_macinfo sections). If the filename given as an argument is not
21482 found in our current list, add it to the list and assign it the next
21483 available unique index number. In order to speed up searches, we remember
21484 the index of the filename was looked up last. This handles the majority of
21485 all searches. */
21487 static struct dwarf_file_data *
21488 lookup_filename (const char *file_name)
21490 struct dwarf_file_data * created;
21492 /* Check to see if the file name that was searched on the previous
21493 call matches this file name. If so, return the index. */
21494 if (file_table_last_lookup
21495 && (file_name == file_table_last_lookup->filename
21496 || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
21497 return file_table_last_lookup;
21499 /* Didn't match the previous lookup, search the table. */
21500 dwarf_file_data **slot
21501 = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
21502 INSERT);
21503 if (*slot)
21504 return *slot;
21506 created = ggc_alloc<dwarf_file_data> ();
21507 created->filename = file_name;
21508 created->emitted_number = 0;
21509 *slot = created;
21510 return created;
21513 /* If the assembler will construct the file table, then translate the compiler
21514 internal file table number into the assembler file table number, and emit
21515 a .file directive if we haven't already emitted one yet. The file table
21516 numbers are different because we prune debug info for unused variables and
21517 types, which may include filenames. */
21519 static int
21520 maybe_emit_file (struct dwarf_file_data * fd)
21522 if (! fd->emitted_number)
21524 if (last_emitted_file)
21525 fd->emitted_number = last_emitted_file->emitted_number + 1;
21526 else
21527 fd->emitted_number = 1;
21528 last_emitted_file = fd;
21530 if (DWARF2_ASM_LINE_DEBUG_INFO)
21532 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
21533 output_quoted_string (asm_out_file,
21534 remap_debug_filename (fd->filename));
21535 fputc ('\n', asm_out_file);
21539 return fd->emitted_number;
21542 /* Schedule generation of a DW_AT_const_value attribute to DIE.
21543 That generation should happen after function debug info has been
21544 generated. The value of the attribute is the constant value of ARG. */
21546 static void
21547 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
21549 die_arg_entry entry;
21551 if (!die || !arg)
21552 return;
21554 if (!tmpl_value_parm_die_table)
21555 vec_alloc (tmpl_value_parm_die_table, 32);
21557 entry.die = die;
21558 entry.arg = arg;
21559 vec_safe_push (tmpl_value_parm_die_table, entry);
21562 /* Return TRUE if T is an instance of generic type, FALSE
21563 otherwise. */
21565 static bool
21566 generic_type_p (tree t)
21568 if (t == NULL_TREE || !TYPE_P (t))
21569 return false;
21570 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
21573 /* Schedule the generation of the generic parameter dies for the
21574 instance of generic type T. The proper generation itself is later
21575 done by gen_scheduled_generic_parms_dies. */
21577 static void
21578 schedule_generic_params_dies_gen (tree t)
21580 if (!generic_type_p (t))
21581 return;
21583 if (!generic_type_instances)
21584 vec_alloc (generic_type_instances, 256);
21586 vec_safe_push (generic_type_instances, t);
21589 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
21590 by append_entry_to_tmpl_value_parm_die_table. This function must
21591 be called after function DIEs have been generated. */
21593 static void
21594 gen_remaining_tmpl_value_param_die_attribute (void)
21596 if (tmpl_value_parm_die_table)
21598 unsigned i;
21599 die_arg_entry *e;
21601 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
21602 tree_add_const_value_attribute (e->die, e->arg);
21606 /* Generate generic parameters DIEs for instances of generic types
21607 that have been previously scheduled by
21608 schedule_generic_params_dies_gen. This function must be called
21609 after all the types of the CU have been laid out. */
21611 static void
21612 gen_scheduled_generic_parms_dies (void)
21614 unsigned i;
21615 tree t;
21617 if (!generic_type_instances)
21618 return;
21620 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
21621 if (COMPLETE_TYPE_P (t))
21622 gen_generic_params_dies (t);
21626 /* Replace DW_AT_name for the decl with name. */
21628 static void
21629 dwarf2out_set_name (tree decl, tree name)
21631 dw_die_ref die;
21632 dw_attr_ref attr;
21633 const char *dname;
21635 die = TYPE_SYMTAB_DIE (decl);
21636 if (!die)
21637 return;
21639 dname = dwarf2_name (name, 0);
21640 if (!dname)
21641 return;
21643 attr = get_AT (die, DW_AT_name);
21644 if (attr)
21646 struct indirect_string_node *node;
21648 node = find_AT_string (dname);
21649 /* replace the string. */
21650 attr->dw_attr_val.v.val_str = node;
21653 else
21654 add_name_attribute (die, dname);
21657 /* True if before or during processing of the first function being emitted. */
21658 static bool in_first_function_p = true;
21659 /* True if loc_note during dwarf2out_var_location call might still be
21660 before first real instruction at address equal to .Ltext0. */
21661 static bool maybe_at_text_label_p = true;
21662 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
21663 static unsigned int first_loclabel_num_not_at_text_label;
21665 /* Called by the final INSN scan whenever we see a var location. We
21666 use it to drop labels in the right places, and throw the location in
21667 our lookup table. */
21669 static void
21670 dwarf2out_var_location (rtx_insn *loc_note)
21672 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
21673 struct var_loc_node *newloc;
21674 rtx_insn *next_real, *next_note;
21675 static const char *last_label;
21676 static const char *last_postcall_label;
21677 static bool last_in_cold_section_p;
21678 static rtx_insn *expected_next_loc_note;
21679 tree decl;
21680 bool var_loc_p;
21682 if (!NOTE_P (loc_note))
21684 if (CALL_P (loc_note))
21686 call_site_count++;
21687 if (SIBLING_CALL_P (loc_note))
21688 tail_call_site_count++;
21690 return;
21693 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
21694 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
21695 return;
21697 /* Optimize processing a large consecutive sequence of location
21698 notes so we don't spend too much time in next_real_insn. If the
21699 next insn is another location note, remember the next_real_insn
21700 calculation for next time. */
21701 next_real = cached_next_real_insn;
21702 if (next_real)
21704 if (expected_next_loc_note != loc_note)
21705 next_real = NULL;
21708 next_note = NEXT_INSN (loc_note);
21709 if (! next_note
21710 || next_note->deleted ()
21711 || ! NOTE_P (next_note)
21712 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
21713 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
21714 next_note = NULL;
21716 if (! next_real)
21717 next_real = next_real_insn (loc_note);
21719 if (next_note)
21721 expected_next_loc_note = next_note;
21722 cached_next_real_insn = next_real;
21724 else
21725 cached_next_real_insn = NULL;
21727 /* If there are no instructions which would be affected by this note,
21728 don't do anything. */
21729 if (var_loc_p
21730 && next_real == NULL_RTX
21731 && !NOTE_DURING_CALL_P (loc_note))
21732 return;
21734 if (next_real == NULL_RTX)
21735 next_real = get_last_insn ();
21737 /* If there were any real insns between note we processed last time
21738 and this note (or if it is the first note), clear
21739 last_{,postcall_}label so that they are not reused this time. */
21740 if (last_var_location_insn == NULL_RTX
21741 || last_var_location_insn != next_real
21742 || last_in_cold_section_p != in_cold_section_p)
21744 last_label = NULL;
21745 last_postcall_label = NULL;
21748 if (var_loc_p)
21750 decl = NOTE_VAR_LOCATION_DECL (loc_note);
21751 newloc = add_var_loc_to_decl (decl, loc_note,
21752 NOTE_DURING_CALL_P (loc_note)
21753 ? last_postcall_label : last_label);
21754 if (newloc == NULL)
21755 return;
21757 else
21759 decl = NULL_TREE;
21760 newloc = NULL;
21763 /* If there were no real insns between note we processed last time
21764 and this note, use the label we emitted last time. Otherwise
21765 create a new label and emit it. */
21766 if (last_label == NULL)
21768 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
21769 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
21770 loclabel_num++;
21771 last_label = ggc_strdup (loclabel);
21772 /* See if loclabel might be equal to .Ltext0. If yes,
21773 bump first_loclabel_num_not_at_text_label. */
21774 if (!have_multiple_function_sections
21775 && in_first_function_p
21776 && maybe_at_text_label_p)
21778 static rtx_insn *last_start;
21779 rtx_insn *insn;
21780 for (insn = loc_note; insn; insn = previous_insn (insn))
21781 if (insn == last_start)
21782 break;
21783 else if (!NONDEBUG_INSN_P (insn))
21784 continue;
21785 else
21787 rtx body = PATTERN (insn);
21788 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
21789 continue;
21790 /* Inline asm could occupy zero bytes. */
21791 else if (GET_CODE (body) == ASM_INPUT
21792 || asm_noperands (body) >= 0)
21793 continue;
21794 #ifdef HAVE_attr_length
21795 else if (get_attr_min_length (insn) == 0)
21796 continue;
21797 #endif
21798 else
21800 /* Assume insn has non-zero length. */
21801 maybe_at_text_label_p = false;
21802 break;
21805 if (maybe_at_text_label_p)
21807 last_start = loc_note;
21808 first_loclabel_num_not_at_text_label = loclabel_num;
21813 if (!var_loc_p)
21815 struct call_arg_loc_node *ca_loc
21816 = ggc_cleared_alloc<call_arg_loc_node> ();
21817 rtx_insn *prev = prev_real_insn (loc_note);
21818 rtx x;
21819 ca_loc->call_arg_loc_note = loc_note;
21820 ca_loc->next = NULL;
21821 ca_loc->label = last_label;
21822 gcc_assert (prev
21823 && (CALL_P (prev)
21824 || (NONJUMP_INSN_P (prev)
21825 && GET_CODE (PATTERN (prev)) == SEQUENCE
21826 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
21827 if (!CALL_P (prev))
21828 prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
21829 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
21830 x = get_call_rtx_from (PATTERN (prev));
21831 if (x)
21833 x = XEXP (XEXP (x, 0), 0);
21834 if (GET_CODE (x) == SYMBOL_REF
21835 && SYMBOL_REF_DECL (x)
21836 && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
21837 ca_loc->symbol_ref = x;
21839 ca_loc->block = insn_scope (prev);
21840 if (call_arg_locations)
21841 call_arg_loc_last->next = ca_loc;
21842 else
21843 call_arg_locations = ca_loc;
21844 call_arg_loc_last = ca_loc;
21846 else if (!NOTE_DURING_CALL_P (loc_note))
21847 newloc->label = last_label;
21848 else
21850 if (!last_postcall_label)
21852 sprintf (loclabel, "%s-1", last_label);
21853 last_postcall_label = ggc_strdup (loclabel);
21855 newloc->label = last_postcall_label;
21858 last_var_location_insn = next_real;
21859 last_in_cold_section_p = in_cold_section_p;
21862 /* Note in one location list that text section has changed. */
21865 var_location_switch_text_section_1 (var_loc_list **slot, void *)
21867 var_loc_list *list = *slot;
21868 if (list->first)
21869 list->last_before_switch
21870 = list->last->next ? list->last->next : list->last;
21871 return 1;
21874 /* Note in all location lists that text section has changed. */
21876 static void
21877 var_location_switch_text_section (void)
21879 if (decl_loc_table == NULL)
21880 return;
21882 decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
21885 /* Create a new line number table. */
21887 static dw_line_info_table *
21888 new_line_info_table (void)
21890 dw_line_info_table *table;
21892 table = ggc_cleared_alloc<dw_line_info_table_struct> ();
21893 table->file_num = 1;
21894 table->line_num = 1;
21895 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
21897 return table;
21900 /* Lookup the "current" table into which we emit line info, so
21901 that we don't have to do it for every source line. */
21903 static void
21904 set_cur_line_info_table (section *sec)
21906 dw_line_info_table *table;
21908 if (sec == text_section)
21909 table = text_section_line_info;
21910 else if (sec == cold_text_section)
21912 table = cold_text_section_line_info;
21913 if (!table)
21915 cold_text_section_line_info = table = new_line_info_table ();
21916 table->end_label = cold_end_label;
21919 else
21921 const char *end_label;
21923 if (flag_reorder_blocks_and_partition)
21925 if (in_cold_section_p)
21926 end_label = crtl->subsections.cold_section_end_label;
21927 else
21928 end_label = crtl->subsections.hot_section_end_label;
21930 else
21932 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21933 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
21934 current_function_funcdef_no);
21935 end_label = ggc_strdup (label);
21938 table = new_line_info_table ();
21939 table->end_label = end_label;
21941 vec_safe_push (separate_line_info, table);
21944 if (DWARF2_ASM_LINE_DEBUG_INFO)
21945 table->is_stmt = (cur_line_info_table
21946 ? cur_line_info_table->is_stmt
21947 : DWARF_LINE_DEFAULT_IS_STMT_START);
21948 cur_line_info_table = table;
21952 /* We need to reset the locations at the beginning of each
21953 function. We can't do this in the end_function hook, because the
21954 declarations that use the locations won't have been output when
21955 that hook is called. Also compute have_multiple_function_sections here. */
21957 static void
21958 dwarf2out_begin_function (tree fun)
21960 section *sec = function_section (fun);
21962 if (sec != text_section)
21963 have_multiple_function_sections = true;
21965 if (flag_reorder_blocks_and_partition && !cold_text_section)
21967 gcc_assert (current_function_decl == fun);
21968 cold_text_section = unlikely_text_section ();
21969 switch_to_section (cold_text_section);
21970 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
21971 switch_to_section (sec);
21974 dwarf2out_note_section_used ();
21975 call_site_count = 0;
21976 tail_call_site_count = 0;
21978 set_cur_line_info_table (sec);
21981 /* Helper function of dwarf2out_end_function, called only after emitting
21982 the very first function into assembly. Check if some .debug_loc range
21983 might end with a .LVL* label that could be equal to .Ltext0.
21984 In that case we must force using absolute addresses in .debug_loc ranges,
21985 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
21986 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
21987 list terminator.
21988 Set have_multiple_function_sections to true in that case and
21989 terminate htab traversal. */
21992 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
21994 var_loc_list *entry = *slot;
21995 struct var_loc_node *node;
21997 node = entry->first;
21998 if (node && node->next && node->next->label)
22000 unsigned int i;
22001 const char *label = node->next->label;
22002 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
22004 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
22006 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
22007 if (strcmp (label, loclabel) == 0)
22009 have_multiple_function_sections = true;
22010 return 0;
22014 return 1;
22017 /* Hook called after emitting a function into assembly.
22018 This does something only for the very first function emitted. */
22020 static void
22021 dwarf2out_end_function (unsigned int)
22023 if (in_first_function_p
22024 && !have_multiple_function_sections
22025 && first_loclabel_num_not_at_text_label
22026 && decl_loc_table)
22027 decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
22028 in_first_function_p = false;
22029 maybe_at_text_label_p = false;
22032 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
22034 static void
22035 push_dw_line_info_entry (dw_line_info_table *table,
22036 enum dw_line_info_opcode opcode, unsigned int val)
22038 dw_line_info_entry e;
22039 e.opcode = opcode;
22040 e.val = val;
22041 vec_safe_push (table->entries, e);
22044 /* Output a label to mark the beginning of a source code line entry
22045 and record information relating to this source line, in
22046 'line_info_table' for later output of the .debug_line section. */
22047 /* ??? The discriminator parameter ought to be unsigned. */
22049 static void
22050 dwarf2out_source_line (unsigned int line, const char *filename,
22051 int discriminator, bool is_stmt)
22053 unsigned int file_num;
22054 dw_line_info_table *table;
22056 if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
22057 return;
22059 /* The discriminator column was added in dwarf4. Simplify the below
22060 by simply removing it if we're not supposed to output it. */
22061 if (dwarf_version < 4 && dwarf_strict)
22062 discriminator = 0;
22064 table = cur_line_info_table;
22065 file_num = maybe_emit_file (lookup_filename (filename));
22067 /* ??? TODO: Elide duplicate line number entries. Traditionally,
22068 the debugger has used the second (possibly duplicate) line number
22069 at the beginning of the function to mark the end of the prologue.
22070 We could eliminate any other duplicates within the function. For
22071 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
22072 that second line number entry. */
22073 /* Recall that this end-of-prologue indication is *not* the same thing
22074 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
22075 to which the hook corresponds, follows the last insn that was
22076 emitted by gen_prologue. What we need is to precede the first insn
22077 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
22078 insn that corresponds to something the user wrote. These may be
22079 very different locations once scheduling is enabled. */
22081 if (0 && file_num == table->file_num
22082 && line == table->line_num
22083 && discriminator == table->discrim_num
22084 && is_stmt == table->is_stmt)
22085 return;
22087 switch_to_section (current_function_section ());
22089 /* If requested, emit something human-readable. */
22090 if (flag_debug_asm)
22091 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
22093 if (DWARF2_ASM_LINE_DEBUG_INFO)
22095 /* Emit the .loc directive understood by GNU as. */
22096 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
22097 file_num, line, is_stmt, discriminator */
22098 fputs ("\t.loc ", asm_out_file);
22099 fprint_ul (asm_out_file, file_num);
22100 putc (' ', asm_out_file);
22101 fprint_ul (asm_out_file, line);
22102 putc (' ', asm_out_file);
22103 putc ('0', asm_out_file);
22105 if (is_stmt != table->is_stmt)
22107 fputs (" is_stmt ", asm_out_file);
22108 putc (is_stmt ? '1' : '0', asm_out_file);
22110 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
22112 gcc_assert (discriminator > 0);
22113 fputs (" discriminator ", asm_out_file);
22114 fprint_ul (asm_out_file, (unsigned long) discriminator);
22116 putc ('\n', asm_out_file);
22118 else
22120 unsigned int label_num = ++line_info_label_num;
22122 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
22124 push_dw_line_info_entry (table, LI_set_address, label_num);
22125 if (file_num != table->file_num)
22126 push_dw_line_info_entry (table, LI_set_file, file_num);
22127 if (discriminator != table->discrim_num)
22128 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
22129 if (is_stmt != table->is_stmt)
22130 push_dw_line_info_entry (table, LI_negate_stmt, 0);
22131 push_dw_line_info_entry (table, LI_set_line, line);
22134 table->file_num = file_num;
22135 table->line_num = line;
22136 table->discrim_num = discriminator;
22137 table->is_stmt = is_stmt;
22138 table->in_use = true;
22141 /* Record the beginning of a new source file. */
22143 static void
22144 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
22146 if (flag_eliminate_dwarf2_dups)
22148 /* Record the beginning of the file for break_out_includes. */
22149 dw_die_ref bincl_die;
22151 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
22152 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
22155 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22157 macinfo_entry e;
22158 e.code = DW_MACINFO_start_file;
22159 e.lineno = lineno;
22160 e.info = ggc_strdup (filename);
22161 vec_safe_push (macinfo_table, e);
22165 /* Record the end of a source file. */
22167 static void
22168 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
22170 if (flag_eliminate_dwarf2_dups)
22171 /* Record the end of the file for break_out_includes. */
22172 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
22174 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22176 macinfo_entry e;
22177 e.code = DW_MACINFO_end_file;
22178 e.lineno = lineno;
22179 e.info = NULL;
22180 vec_safe_push (macinfo_table, e);
22184 /* Called from debug_define in toplev.c. The `buffer' parameter contains
22185 the tail part of the directive line, i.e. the part which is past the
22186 initial whitespace, #, whitespace, directive-name, whitespace part. */
22188 static void
22189 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
22190 const char *buffer ATTRIBUTE_UNUSED)
22192 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22194 macinfo_entry e;
22195 /* Insert a dummy first entry to be able to optimize the whole
22196 predefined macro block using DW_MACRO_GNU_transparent_include. */
22197 if (macinfo_table->is_empty () && lineno <= 1)
22199 e.code = 0;
22200 e.lineno = 0;
22201 e.info = NULL;
22202 vec_safe_push (macinfo_table, e);
22204 e.code = DW_MACINFO_define;
22205 e.lineno = lineno;
22206 e.info = ggc_strdup (buffer);
22207 vec_safe_push (macinfo_table, e);
22211 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
22212 the tail part of the directive line, i.e. the part which is past the
22213 initial whitespace, #, whitespace, directive-name, whitespace part. */
22215 static void
22216 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
22217 const char *buffer ATTRIBUTE_UNUSED)
22219 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22221 macinfo_entry e;
22222 /* Insert a dummy first entry to be able to optimize the whole
22223 predefined macro block using DW_MACRO_GNU_transparent_include. */
22224 if (macinfo_table->is_empty () && lineno <= 1)
22226 e.code = 0;
22227 e.lineno = 0;
22228 e.info = NULL;
22229 vec_safe_push (macinfo_table, e);
22231 e.code = DW_MACINFO_undef;
22232 e.lineno = lineno;
22233 e.info = ggc_strdup (buffer);
22234 vec_safe_push (macinfo_table, e);
22238 /* Helpers to manipulate hash table of CUs. */
22240 struct macinfo_entry_hasher : typed_noop_remove <macinfo_entry>
22242 typedef macinfo_entry *value_type;
22243 typedef macinfo_entry *compare_type;
22244 static inline hashval_t hash (const macinfo_entry *);
22245 static inline bool equal (const macinfo_entry *, const macinfo_entry *);
22248 inline hashval_t
22249 macinfo_entry_hasher::hash (const macinfo_entry *entry)
22251 return htab_hash_string (entry->info);
22254 inline bool
22255 macinfo_entry_hasher::equal (const macinfo_entry *entry1,
22256 const macinfo_entry *entry2)
22258 return !strcmp (entry1->info, entry2->info);
22261 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
22263 /* Output a single .debug_macinfo entry. */
22265 static void
22266 output_macinfo_op (macinfo_entry *ref)
22268 int file_num;
22269 size_t len;
22270 struct indirect_string_node *node;
22271 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22272 struct dwarf_file_data *fd;
22274 switch (ref->code)
22276 case DW_MACINFO_start_file:
22277 fd = lookup_filename (ref->info);
22278 file_num = maybe_emit_file (fd);
22279 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
22280 dw2_asm_output_data_uleb128 (ref->lineno,
22281 "Included from line number %lu",
22282 (unsigned long) ref->lineno);
22283 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
22284 break;
22285 case DW_MACINFO_end_file:
22286 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
22287 break;
22288 case DW_MACINFO_define:
22289 case DW_MACINFO_undef:
22290 len = strlen (ref->info) + 1;
22291 if (!dwarf_strict
22292 && len > DWARF_OFFSET_SIZE
22293 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
22294 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
22296 ref->code = ref->code == DW_MACINFO_define
22297 ? DW_MACRO_GNU_define_indirect
22298 : DW_MACRO_GNU_undef_indirect;
22299 output_macinfo_op (ref);
22300 return;
22302 dw2_asm_output_data (1, ref->code,
22303 ref->code == DW_MACINFO_define
22304 ? "Define macro" : "Undefine macro");
22305 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
22306 (unsigned long) ref->lineno);
22307 dw2_asm_output_nstring (ref->info, -1, "The macro");
22308 break;
22309 case DW_MACRO_GNU_define_indirect:
22310 case DW_MACRO_GNU_undef_indirect:
22311 node = find_AT_string (ref->info);
22312 gcc_assert (node
22313 && ((node->form == DW_FORM_strp)
22314 || (node->form == DW_FORM_GNU_str_index)));
22315 dw2_asm_output_data (1, ref->code,
22316 ref->code == DW_MACRO_GNU_define_indirect
22317 ? "Define macro indirect"
22318 : "Undefine macro indirect");
22319 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
22320 (unsigned long) ref->lineno);
22321 if (node->form == DW_FORM_strp)
22322 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
22323 debug_str_section, "The macro: \"%s\"",
22324 ref->info);
22325 else
22326 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
22327 ref->info);
22328 break;
22329 case DW_MACRO_GNU_transparent_include:
22330 dw2_asm_output_data (1, ref->code, "Transparent include");
22331 ASM_GENERATE_INTERNAL_LABEL (label,
22332 DEBUG_MACRO_SECTION_LABEL, ref->lineno);
22333 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
22334 break;
22335 default:
22336 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
22337 ASM_COMMENT_START, (unsigned long) ref->code);
22338 break;
22342 /* Attempt to make a sequence of define/undef macinfo ops shareable with
22343 other compilation unit .debug_macinfo sections. IDX is the first
22344 index of a define/undef, return the number of ops that should be
22345 emitted in a comdat .debug_macinfo section and emit
22346 a DW_MACRO_GNU_transparent_include entry referencing it.
22347 If the define/undef entry should be emitted normally, return 0. */
22349 static unsigned
22350 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
22351 macinfo_hash_type **macinfo_htab)
22353 macinfo_entry *first, *second, *cur, *inc;
22354 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
22355 unsigned char checksum[16];
22356 struct md5_ctx ctx;
22357 char *grp_name, *tail;
22358 const char *base;
22359 unsigned int i, count, encoded_filename_len, linebuf_len;
22360 macinfo_entry **slot;
22362 first = &(*macinfo_table)[idx];
22363 second = &(*macinfo_table)[idx + 1];
22365 /* Optimize only if there are at least two consecutive define/undef ops,
22366 and either all of them are before first DW_MACINFO_start_file
22367 with lineno {0,1} (i.e. predefined macro block), or all of them are
22368 in some included header file. */
22369 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
22370 return 0;
22371 if (vec_safe_is_empty (files))
22373 if (first->lineno > 1 || second->lineno > 1)
22374 return 0;
22376 else if (first->lineno == 0)
22377 return 0;
22379 /* Find the last define/undef entry that can be grouped together
22380 with first and at the same time compute md5 checksum of their
22381 codes, linenumbers and strings. */
22382 md5_init_ctx (&ctx);
22383 for (i = idx; macinfo_table->iterate (i, &cur); i++)
22384 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
22385 break;
22386 else if (vec_safe_is_empty (files) && cur->lineno > 1)
22387 break;
22388 else
22390 unsigned char code = cur->code;
22391 md5_process_bytes (&code, 1, &ctx);
22392 checksum_uleb128 (cur->lineno, &ctx);
22393 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
22395 md5_finish_ctx (&ctx, checksum);
22396 count = i - idx;
22398 /* From the containing include filename (if any) pick up just
22399 usable characters from its basename. */
22400 if (vec_safe_is_empty (files))
22401 base = "";
22402 else
22403 base = lbasename (files->last ().info);
22404 for (encoded_filename_len = 0, i = 0; base[i]; i++)
22405 if (ISIDNUM (base[i]) || base[i] == '.')
22406 encoded_filename_len++;
22407 /* Count . at the end. */
22408 if (encoded_filename_len)
22409 encoded_filename_len++;
22411 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
22412 linebuf_len = strlen (linebuf);
22414 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
22415 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
22416 + 16 * 2 + 1);
22417 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
22418 tail = grp_name + 4;
22419 if (encoded_filename_len)
22421 for (i = 0; base[i]; i++)
22422 if (ISIDNUM (base[i]) || base[i] == '.')
22423 *tail++ = base[i];
22424 *tail++ = '.';
22426 memcpy (tail, linebuf, linebuf_len);
22427 tail += linebuf_len;
22428 *tail++ = '.';
22429 for (i = 0; i < 16; i++)
22430 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
22432 /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
22433 in the empty vector entry before the first define/undef. */
22434 inc = &(*macinfo_table)[idx - 1];
22435 inc->code = DW_MACRO_GNU_transparent_include;
22436 inc->lineno = 0;
22437 inc->info = ggc_strdup (grp_name);
22438 if (!*macinfo_htab)
22439 *macinfo_htab = new macinfo_hash_type (10);
22440 /* Avoid emitting duplicates. */
22441 slot = (*macinfo_htab)->find_slot (inc, INSERT);
22442 if (*slot != NULL)
22444 inc->code = 0;
22445 inc->info = NULL;
22446 /* If such an entry has been used before, just emit
22447 a DW_MACRO_GNU_transparent_include op. */
22448 inc = *slot;
22449 output_macinfo_op (inc);
22450 /* And clear all macinfo_entry in the range to avoid emitting them
22451 in the second pass. */
22452 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
22454 cur->code = 0;
22455 cur->info = NULL;
22458 else
22460 *slot = inc;
22461 inc->lineno = (*macinfo_htab)->elements ();
22462 output_macinfo_op (inc);
22464 return count;
22467 /* Save any strings needed by the macinfo table in the debug str
22468 table. All strings must be collected into the table by the time
22469 index_string is called. */
22471 static void
22472 save_macinfo_strings (void)
22474 unsigned len;
22475 unsigned i;
22476 macinfo_entry *ref;
22478 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
22480 switch (ref->code)
22482 /* Match the logic in output_macinfo_op to decide on
22483 indirect strings. */
22484 case DW_MACINFO_define:
22485 case DW_MACINFO_undef:
22486 len = strlen (ref->info) + 1;
22487 if (!dwarf_strict
22488 && len > DWARF_OFFSET_SIZE
22489 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
22490 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
22491 set_indirect_string (find_AT_string (ref->info));
22492 break;
22493 case DW_MACRO_GNU_define_indirect:
22494 case DW_MACRO_GNU_undef_indirect:
22495 set_indirect_string (find_AT_string (ref->info));
22496 break;
22497 default:
22498 break;
22503 /* Output macinfo section(s). */
22505 static void
22506 output_macinfo (void)
22508 unsigned i;
22509 unsigned long length = vec_safe_length (macinfo_table);
22510 macinfo_entry *ref;
22511 vec<macinfo_entry, va_gc> *files = NULL;
22512 macinfo_hash_type *macinfo_htab = NULL;
22514 if (! length)
22515 return;
22517 /* output_macinfo* uses these interchangeably. */
22518 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
22519 && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
22520 && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
22521 && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
22523 /* For .debug_macro emit the section header. */
22524 if (!dwarf_strict)
22526 dw2_asm_output_data (2, 4, "DWARF macro version number");
22527 if (DWARF_OFFSET_SIZE == 8)
22528 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
22529 else
22530 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
22531 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
22532 (!dwarf_split_debug_info ? debug_line_section_label
22533 : debug_skeleton_line_section_label),
22534 debug_line_section, NULL);
22537 /* In the first loop, it emits the primary .debug_macinfo section
22538 and after each emitted op the macinfo_entry is cleared.
22539 If a longer range of define/undef ops can be optimized using
22540 DW_MACRO_GNU_transparent_include, the
22541 DW_MACRO_GNU_transparent_include op is emitted and kept in
22542 the vector before the first define/undef in the range and the
22543 whole range of define/undef ops is not emitted and kept. */
22544 for (i = 0; macinfo_table->iterate (i, &ref); i++)
22546 switch (ref->code)
22548 case DW_MACINFO_start_file:
22549 vec_safe_push (files, *ref);
22550 break;
22551 case DW_MACINFO_end_file:
22552 if (!vec_safe_is_empty (files))
22553 files->pop ();
22554 break;
22555 case DW_MACINFO_define:
22556 case DW_MACINFO_undef:
22557 if (!dwarf_strict
22558 && HAVE_COMDAT_GROUP
22559 && vec_safe_length (files) != 1
22560 && i > 0
22561 && i + 1 < length
22562 && (*macinfo_table)[i - 1].code == 0)
22564 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
22565 if (count)
22567 i += count - 1;
22568 continue;
22571 break;
22572 case 0:
22573 /* A dummy entry may be inserted at the beginning to be able
22574 to optimize the whole block of predefined macros. */
22575 if (i == 0)
22576 continue;
22577 default:
22578 break;
22580 output_macinfo_op (ref);
22581 ref->info = NULL;
22582 ref->code = 0;
22585 if (!macinfo_htab)
22586 return;
22588 delete macinfo_htab;
22589 macinfo_htab = NULL;
22591 /* If any DW_MACRO_GNU_transparent_include were used, on those
22592 DW_MACRO_GNU_transparent_include entries terminate the
22593 current chain and switch to a new comdat .debug_macinfo
22594 section and emit the define/undef entries within it. */
22595 for (i = 0; macinfo_table->iterate (i, &ref); i++)
22596 switch (ref->code)
22598 case 0:
22599 continue;
22600 case DW_MACRO_GNU_transparent_include:
22602 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22603 tree comdat_key = get_identifier (ref->info);
22604 /* Terminate the previous .debug_macinfo section. */
22605 dw2_asm_output_data (1, 0, "End compilation unit");
22606 targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
22607 SECTION_DEBUG
22608 | SECTION_LINKONCE,
22609 comdat_key);
22610 ASM_GENERATE_INTERNAL_LABEL (label,
22611 DEBUG_MACRO_SECTION_LABEL,
22612 ref->lineno);
22613 ASM_OUTPUT_LABEL (asm_out_file, label);
22614 ref->code = 0;
22615 ref->info = NULL;
22616 dw2_asm_output_data (2, 4, "DWARF macro version number");
22617 if (DWARF_OFFSET_SIZE == 8)
22618 dw2_asm_output_data (1, 1, "Flags: 64-bit");
22619 else
22620 dw2_asm_output_data (1, 0, "Flags: 32-bit");
22622 break;
22623 case DW_MACINFO_define:
22624 case DW_MACINFO_undef:
22625 output_macinfo_op (ref);
22626 ref->code = 0;
22627 ref->info = NULL;
22628 break;
22629 default:
22630 gcc_unreachable ();
22634 /* Set up for Dwarf output at the start of compilation. */
22636 static void
22637 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
22639 /* This option is currently broken, see (PR53118 and PR46102). */
22640 if (flag_eliminate_dwarf2_dups
22641 && strstr (lang_hooks.name, "C++"))
22643 warning (0, "-feliminate-dwarf2-dups is broken for C++, ignoring");
22644 flag_eliminate_dwarf2_dups = 0;
22647 /* Allocate the file_table. */
22648 file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
22650 /* Allocate the decl_die_table. */
22651 decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
22653 /* Allocate the decl_loc_table. */
22654 decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
22656 /* Allocate the cached_dw_loc_list_table. */
22657 cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
22659 /* Allocate the initial hunk of the decl_scope_table. */
22660 vec_alloc (decl_scope_table, 256);
22662 /* Allocate the initial hunk of the abbrev_die_table. */
22663 abbrev_die_table = ggc_cleared_vec_alloc<dw_die_ref>
22664 (ABBREV_DIE_TABLE_INCREMENT);
22665 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
22666 /* Zero-th entry is allocated, but unused. */
22667 abbrev_die_table_in_use = 1;
22669 /* Allocate the pubtypes and pubnames vectors. */
22670 vec_alloc (pubname_table, 32);
22671 vec_alloc (pubtype_table, 32);
22673 vec_alloc (incomplete_types, 64);
22675 vec_alloc (used_rtx_array, 32);
22677 if (!dwarf_split_debug_info)
22679 debug_info_section = get_section (DEBUG_INFO_SECTION,
22680 SECTION_DEBUG, NULL);
22681 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22682 SECTION_DEBUG, NULL);
22683 debug_loc_section = get_section (DEBUG_LOC_SECTION,
22684 SECTION_DEBUG, NULL);
22686 else
22688 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
22689 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22690 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
22691 SECTION_DEBUG | SECTION_EXCLUDE,
22692 NULL);
22693 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
22694 SECTION_DEBUG, NULL);
22695 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
22696 SECTION_DEBUG, NULL);
22697 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22698 SECTION_DEBUG, NULL);
22699 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
22700 DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
22702 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
22703 the main .o, but the skeleton_line goes into the split off dwo. */
22704 debug_skeleton_line_section
22705 = get_section (DEBUG_DWO_LINE_SECTION,
22706 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22707 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
22708 DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
22709 debug_str_offsets_section = get_section (DEBUG_STR_OFFSETS_SECTION,
22710 SECTION_DEBUG | SECTION_EXCLUDE,
22711 NULL);
22712 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
22713 DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
22714 debug_loc_section = get_section (DEBUG_DWO_LOC_SECTION,
22715 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22716 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
22717 DEBUG_STR_DWO_SECTION_FLAGS, NULL);
22719 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
22720 SECTION_DEBUG, NULL);
22721 debug_macinfo_section = get_section (dwarf_strict
22722 ? DEBUG_MACINFO_SECTION
22723 : DEBUG_MACRO_SECTION,
22724 DEBUG_MACRO_SECTION_FLAGS, NULL);
22725 debug_line_section = get_section (DEBUG_LINE_SECTION,
22726 SECTION_DEBUG, NULL);
22727 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
22728 SECTION_DEBUG, NULL);
22729 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
22730 SECTION_DEBUG, NULL);
22731 debug_str_section = get_section (DEBUG_STR_SECTION,
22732 DEBUG_STR_SECTION_FLAGS, NULL);
22733 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
22734 SECTION_DEBUG, NULL);
22735 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
22736 SECTION_DEBUG, NULL);
22738 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
22739 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
22740 DEBUG_ABBREV_SECTION_LABEL, 0);
22741 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
22742 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
22743 COLD_TEXT_SECTION_LABEL, 0);
22744 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
22746 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
22747 DEBUG_INFO_SECTION_LABEL, 0);
22748 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
22749 DEBUG_LINE_SECTION_LABEL, 0);
22750 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
22751 DEBUG_RANGES_SECTION_LABEL, 0);
22752 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
22753 DEBUG_ADDR_SECTION_LABEL, 0);
22754 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
22755 dwarf_strict
22756 ? DEBUG_MACINFO_SECTION_LABEL
22757 : DEBUG_MACRO_SECTION_LABEL, 0);
22758 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
22760 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22761 vec_alloc (macinfo_table, 64);
22763 switch_to_section (text_section);
22764 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
22766 /* Make sure the line number table for .text always exists. */
22767 text_section_line_info = new_line_info_table ();
22768 text_section_line_info->end_label = text_end_label;
22771 /* Called before compile () starts outputtting functions, variables
22772 and toplevel asms into assembly. */
22774 static void
22775 dwarf2out_assembly_start (void)
22777 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
22778 && dwarf2out_do_cfi_asm ()
22779 && (!(flag_unwind_tables || flag_exceptions)
22780 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
22781 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
22784 /* A helper function for dwarf2out_finish called through
22785 htab_traverse. Assign a string its index. All strings must be
22786 collected into the table by the time index_string is called,
22787 because the indexing code relies on htab_traverse to traverse nodes
22788 in the same order for each run. */
22791 index_string (indirect_string_node **h, unsigned int *index)
22793 indirect_string_node *node = *h;
22795 find_string_form (node);
22796 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22798 gcc_assert (node->index == NO_INDEX_ASSIGNED);
22799 node->index = *index;
22800 *index += 1;
22802 return 1;
22805 /* A helper function for output_indirect_strings called through
22806 htab_traverse. Output the offset to a string and update the
22807 current offset. */
22810 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
22812 indirect_string_node *node = *h;
22814 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22816 /* Assert that this node has been assigned an index. */
22817 gcc_assert (node->index != NO_INDEX_ASSIGNED
22818 && node->index != NOT_INDEXED);
22819 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
22820 "indexed string 0x%x: %s", node->index, node->str);
22821 *offset += strlen (node->str) + 1;
22823 return 1;
22826 /* A helper function for dwarf2out_finish called through
22827 htab_traverse. Output the indexed string. */
22830 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
22832 struct indirect_string_node *node = *h;
22834 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22836 /* Assert that the strings are output in the same order as their
22837 indexes were assigned. */
22838 gcc_assert (*cur_idx == node->index);
22839 assemble_string (node->str, strlen (node->str) + 1);
22840 *cur_idx += 1;
22842 return 1;
22845 /* A helper function for dwarf2out_finish called through
22846 htab_traverse. Emit one queued .debug_str string. */
22849 output_indirect_string (indirect_string_node **h, void *)
22851 struct indirect_string_node *node = *h;
22853 node->form = find_string_form (node);
22854 if (node->form == DW_FORM_strp && node->refcount > 0)
22856 ASM_OUTPUT_LABEL (asm_out_file, node->label);
22857 assemble_string (node->str, strlen (node->str) + 1);
22860 return 1;
22863 /* Output the indexed string table. */
22865 static void
22866 output_indirect_strings (void)
22868 switch_to_section (debug_str_section);
22869 if (!dwarf_split_debug_info)
22870 debug_str_hash->traverse<void *, output_indirect_string> (NULL);
22871 else
22873 unsigned int offset = 0;
22874 unsigned int cur_idx = 0;
22876 skeleton_debug_str_hash->traverse<void *, output_indirect_string> (NULL);
22878 switch_to_section (debug_str_offsets_section);
22879 debug_str_hash->traverse_noresize
22880 <unsigned int *, output_index_string_offset> (&offset);
22881 switch_to_section (debug_str_dwo_section);
22882 debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
22883 (&cur_idx);
22887 /* Callback for htab_traverse to assign an index to an entry in the
22888 table, and to write that entry to the .debug_addr section. */
22891 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
22893 addr_table_entry *entry = *slot;
22895 if (entry->refcount == 0)
22897 gcc_assert (entry->index == NO_INDEX_ASSIGNED
22898 || entry->index == NOT_INDEXED);
22899 return 1;
22902 gcc_assert (entry->index == *cur_index);
22903 (*cur_index)++;
22905 switch (entry->kind)
22907 case ate_kind_rtx:
22908 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
22909 "0x%x", entry->index);
22910 break;
22911 case ate_kind_rtx_dtprel:
22912 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
22913 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
22914 DWARF2_ADDR_SIZE,
22915 entry->addr.rtl);
22916 fputc ('\n', asm_out_file);
22917 break;
22918 case ate_kind_label:
22919 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
22920 "0x%x", entry->index);
22921 break;
22922 default:
22923 gcc_unreachable ();
22925 return 1;
22928 /* Produce the .debug_addr section. */
22930 static void
22931 output_addr_table (void)
22933 unsigned int index = 0;
22934 if (addr_index_table == NULL || addr_index_table->size () == 0)
22935 return;
22937 switch_to_section (debug_addr_section);
22938 addr_index_table
22939 ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
22942 #if ENABLE_ASSERT_CHECKING
22943 /* Verify that all marks are clear. */
22945 static void
22946 verify_marks_clear (dw_die_ref die)
22948 dw_die_ref c;
22950 gcc_assert (! die->die_mark);
22951 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
22953 #endif /* ENABLE_ASSERT_CHECKING */
22955 /* Clear the marks for a die and its children.
22956 Be cool if the mark isn't set. */
22958 static void
22959 prune_unmark_dies (dw_die_ref die)
22961 dw_die_ref c;
22963 if (die->die_mark)
22964 die->die_mark = 0;
22965 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
22968 /* Given DIE that we're marking as used, find any other dies
22969 it references as attributes and mark them as used. */
22971 static void
22972 prune_unused_types_walk_attribs (dw_die_ref die)
22974 dw_attr_ref a;
22975 unsigned ix;
22977 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22979 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
22981 /* A reference to another DIE.
22982 Make sure that it will get emitted.
22983 If it was broken out into a comdat group, don't follow it. */
22984 if (! AT_ref (a)->comdat_type_p
22985 || a->dw_attr == DW_AT_specification)
22986 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
22988 /* Set the string's refcount to 0 so that prune_unused_types_mark
22989 accounts properly for it. */
22990 if (AT_class (a) == dw_val_class_str)
22991 a->dw_attr_val.v.val_str->refcount = 0;
22995 /* Mark the generic parameters and arguments children DIEs of DIE. */
22997 static void
22998 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
23000 dw_die_ref c;
23002 if (die == NULL || die->die_child == NULL)
23003 return;
23004 c = die->die_child;
23007 if (is_template_parameter (c))
23008 prune_unused_types_mark (c, 1);
23009 c = c->die_sib;
23010 } while (c && c != die->die_child);
23013 /* Mark DIE as being used. If DOKIDS is true, then walk down
23014 to DIE's children. */
23016 static void
23017 prune_unused_types_mark (dw_die_ref die, int dokids)
23019 dw_die_ref c;
23021 if (die->die_mark == 0)
23023 /* We haven't done this node yet. Mark it as used. */
23024 die->die_mark = 1;
23025 /* If this is the DIE of a generic type instantiation,
23026 mark the children DIEs that describe its generic parms and
23027 args. */
23028 prune_unused_types_mark_generic_parms_dies (die);
23030 /* We also have to mark its parents as used.
23031 (But we don't want to mark our parent's kids due to this,
23032 unless it is a class.) */
23033 if (die->die_parent)
23034 prune_unused_types_mark (die->die_parent,
23035 class_scope_p (die->die_parent));
23037 /* Mark any referenced nodes. */
23038 prune_unused_types_walk_attribs (die);
23040 /* If this node is a specification,
23041 also mark the definition, if it exists. */
23042 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
23043 prune_unused_types_mark (die->die_definition, 1);
23046 if (dokids && die->die_mark != 2)
23048 /* We need to walk the children, but haven't done so yet.
23049 Remember that we've walked the kids. */
23050 die->die_mark = 2;
23052 /* If this is an array type, we need to make sure our
23053 kids get marked, even if they're types. If we're
23054 breaking out types into comdat sections, do this
23055 for all type definitions. */
23056 if (die->die_tag == DW_TAG_array_type
23057 || (use_debug_types
23058 && is_type_die (die) && ! is_declaration_die (die)))
23059 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
23060 else
23061 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
23065 /* For local classes, look if any static member functions were emitted
23066 and if so, mark them. */
23068 static void
23069 prune_unused_types_walk_local_classes (dw_die_ref die)
23071 dw_die_ref c;
23073 if (die->die_mark == 2)
23074 return;
23076 switch (die->die_tag)
23078 case DW_TAG_structure_type:
23079 case DW_TAG_union_type:
23080 case DW_TAG_class_type:
23081 break;
23083 case DW_TAG_subprogram:
23084 if (!get_AT_flag (die, DW_AT_declaration)
23085 || die->die_definition != NULL)
23086 prune_unused_types_mark (die, 1);
23087 return;
23089 default:
23090 return;
23093 /* Mark children. */
23094 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
23097 /* Walk the tree DIE and mark types that we actually use. */
23099 static void
23100 prune_unused_types_walk (dw_die_ref die)
23102 dw_die_ref c;
23104 /* Don't do anything if this node is already marked and
23105 children have been marked as well. */
23106 if (die->die_mark == 2)
23107 return;
23109 switch (die->die_tag)
23111 case DW_TAG_structure_type:
23112 case DW_TAG_union_type:
23113 case DW_TAG_class_type:
23114 if (die->die_perennial_p)
23115 break;
23117 for (c = die->die_parent; c; c = c->die_parent)
23118 if (c->die_tag == DW_TAG_subprogram)
23119 break;
23121 /* Finding used static member functions inside of classes
23122 is needed just for local classes, because for other classes
23123 static member function DIEs with DW_AT_specification
23124 are emitted outside of the DW_TAG_*_type. If we ever change
23125 it, we'd need to call this even for non-local classes. */
23126 if (c)
23127 prune_unused_types_walk_local_classes (die);
23129 /* It's a type node --- don't mark it. */
23130 return;
23132 case DW_TAG_const_type:
23133 case DW_TAG_packed_type:
23134 case DW_TAG_pointer_type:
23135 case DW_TAG_reference_type:
23136 case DW_TAG_rvalue_reference_type:
23137 case DW_TAG_volatile_type:
23138 case DW_TAG_typedef:
23139 case DW_TAG_array_type:
23140 case DW_TAG_interface_type:
23141 case DW_TAG_friend:
23142 case DW_TAG_variant_part:
23143 case DW_TAG_enumeration_type:
23144 case DW_TAG_subroutine_type:
23145 case DW_TAG_string_type:
23146 case DW_TAG_set_type:
23147 case DW_TAG_subrange_type:
23148 case DW_TAG_ptr_to_member_type:
23149 case DW_TAG_file_type:
23150 if (die->die_perennial_p)
23151 break;
23153 /* It's a type node --- don't mark it. */
23154 return;
23156 default:
23157 /* Mark everything else. */
23158 break;
23161 if (die->die_mark == 0)
23163 die->die_mark = 1;
23165 /* Now, mark any dies referenced from here. */
23166 prune_unused_types_walk_attribs (die);
23169 die->die_mark = 2;
23171 /* Mark children. */
23172 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
23175 /* Increment the string counts on strings referred to from DIE's
23176 attributes. */
23178 static void
23179 prune_unused_types_update_strings (dw_die_ref die)
23181 dw_attr_ref a;
23182 unsigned ix;
23184 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23185 if (AT_class (a) == dw_val_class_str)
23187 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
23188 s->refcount++;
23189 /* Avoid unnecessarily putting strings that are used less than
23190 twice in the hash table. */
23191 if (s->refcount
23192 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
23194 indirect_string_node **slot
23195 = debug_str_hash->find_slot_with_hash (s->str,
23196 htab_hash_string (s->str),
23197 INSERT);
23198 gcc_assert (*slot == NULL);
23199 *slot = s;
23204 /* Remove from the tree DIE any dies that aren't marked. */
23206 static void
23207 prune_unused_types_prune (dw_die_ref die)
23209 dw_die_ref c;
23211 gcc_assert (die->die_mark);
23212 prune_unused_types_update_strings (die);
23214 if (! die->die_child)
23215 return;
23217 c = die->die_child;
23218 do {
23219 dw_die_ref prev = c;
23220 for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
23221 if (c == die->die_child)
23223 /* No marked children between 'prev' and the end of the list. */
23224 if (prev == c)
23225 /* No marked children at all. */
23226 die->die_child = NULL;
23227 else
23229 prev->die_sib = c->die_sib;
23230 die->die_child = prev;
23232 return;
23235 if (c != prev->die_sib)
23236 prev->die_sib = c;
23237 prune_unused_types_prune (c);
23238 } while (c != die->die_child);
23241 /* Remove dies representing declarations that we never use. */
23243 static void
23244 prune_unused_types (void)
23246 unsigned int i;
23247 limbo_die_node *node;
23248 comdat_type_node *ctnode;
23249 pubname_ref pub;
23250 dw_die_ref base_type;
23252 #if ENABLE_ASSERT_CHECKING
23253 /* All the marks should already be clear. */
23254 verify_marks_clear (comp_unit_die ());
23255 for (node = limbo_die_list; node; node = node->next)
23256 verify_marks_clear (node->die);
23257 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23258 verify_marks_clear (ctnode->root_die);
23259 #endif /* ENABLE_ASSERT_CHECKING */
23261 /* Mark types that are used in global variables. */
23262 premark_types_used_by_global_vars ();
23264 /* Set the mark on nodes that are actually used. */
23265 prune_unused_types_walk (comp_unit_die ());
23266 for (node = limbo_die_list; node; node = node->next)
23267 prune_unused_types_walk (node->die);
23268 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23270 prune_unused_types_walk (ctnode->root_die);
23271 prune_unused_types_mark (ctnode->type_die, 1);
23274 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
23275 are unusual in that they are pubnames that are the children of pubtypes.
23276 They should only be marked via their parent DW_TAG_enumeration_type die,
23277 not as roots in themselves. */
23278 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
23279 if (pub->die->die_tag != DW_TAG_enumerator)
23280 prune_unused_types_mark (pub->die, 1);
23281 for (i = 0; base_types.iterate (i, &base_type); i++)
23282 prune_unused_types_mark (base_type, 1);
23284 if (debug_str_hash)
23285 debug_str_hash->empty ();
23286 if (skeleton_debug_str_hash)
23287 skeleton_debug_str_hash->empty ();
23288 prune_unused_types_prune (comp_unit_die ());
23289 for (node = limbo_die_list; node; node = node->next)
23290 prune_unused_types_prune (node->die);
23291 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23292 prune_unused_types_prune (ctnode->root_die);
23294 /* Leave the marks clear. */
23295 prune_unmark_dies (comp_unit_die ());
23296 for (node = limbo_die_list; node; node = node->next)
23297 prune_unmark_dies (node->die);
23298 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23299 prune_unmark_dies (ctnode->root_die);
23302 /* Set the parameter to true if there are any relative pathnames in
23303 the file table. */
23305 file_table_relative_p (dwarf_file_data **slot, bool *p)
23307 struct dwarf_file_data *d = *slot;
23308 if (!IS_ABSOLUTE_PATH (d->filename))
23310 *p = true;
23311 return 0;
23313 return 1;
23316 /* Helpers to manipulate hash table of comdat type units. */
23318 struct comdat_type_hasher : typed_noop_remove <comdat_type_node>
23320 typedef comdat_type_node *value_type;
23321 typedef comdat_type_node *compare_type;
23322 static inline hashval_t hash (const comdat_type_node *);
23323 static inline bool equal (const comdat_type_node *, const comdat_type_node *);
23326 inline hashval_t
23327 comdat_type_hasher::hash (const comdat_type_node *type_node)
23329 hashval_t h;
23330 memcpy (&h, type_node->signature, sizeof (h));
23331 return h;
23334 inline bool
23335 comdat_type_hasher::equal (const comdat_type_node *type_node_1,
23336 const comdat_type_node *type_node_2)
23338 return (! memcmp (type_node_1->signature, type_node_2->signature,
23339 DWARF_TYPE_SIGNATURE_SIZE));
23342 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
23343 to the location it would have been added, should we know its
23344 DECL_ASSEMBLER_NAME when we added other attributes. This will
23345 probably improve compactness of debug info, removing equivalent
23346 abbrevs, and hide any differences caused by deferring the
23347 computation of the assembler name, triggered by e.g. PCH. */
23349 static inline void
23350 move_linkage_attr (dw_die_ref die)
23352 unsigned ix = vec_safe_length (die->die_attr);
23353 dw_attr_node linkage = (*die->die_attr)[ix - 1];
23355 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
23356 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
23358 while (--ix > 0)
23360 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
23362 if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
23363 break;
23366 if (ix != vec_safe_length (die->die_attr) - 1)
23368 die->die_attr->pop ();
23369 die->die_attr->quick_insert (ix, linkage);
23373 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
23374 referenced from typed stack ops and count how often they are used. */
23376 static void
23377 mark_base_types (dw_loc_descr_ref loc)
23379 dw_die_ref base_type = NULL;
23381 for (; loc; loc = loc->dw_loc_next)
23383 switch (loc->dw_loc_opc)
23385 case DW_OP_GNU_regval_type:
23386 case DW_OP_GNU_deref_type:
23387 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
23388 break;
23389 case DW_OP_GNU_convert:
23390 case DW_OP_GNU_reinterpret:
23391 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
23392 continue;
23393 /* FALLTHRU */
23394 case DW_OP_GNU_const_type:
23395 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
23396 break;
23397 case DW_OP_GNU_entry_value:
23398 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
23399 continue;
23400 default:
23401 continue;
23403 gcc_assert (base_type->die_parent == comp_unit_die ());
23404 if (base_type->die_mark)
23405 base_type->die_mark++;
23406 else
23408 base_types.safe_push (base_type);
23409 base_type->die_mark = 1;
23414 /* Comparison function for sorting marked base types. */
23416 static int
23417 base_type_cmp (const void *x, const void *y)
23419 dw_die_ref dx = *(const dw_die_ref *) x;
23420 dw_die_ref dy = *(const dw_die_ref *) y;
23421 unsigned int byte_size1, byte_size2;
23422 unsigned int encoding1, encoding2;
23423 if (dx->die_mark > dy->die_mark)
23424 return -1;
23425 if (dx->die_mark < dy->die_mark)
23426 return 1;
23427 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
23428 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
23429 if (byte_size1 < byte_size2)
23430 return 1;
23431 if (byte_size1 > byte_size2)
23432 return -1;
23433 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
23434 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
23435 if (encoding1 < encoding2)
23436 return 1;
23437 if (encoding1 > encoding2)
23438 return -1;
23439 return 0;
23442 /* Move base types marked by mark_base_types as early as possible
23443 in the CU, sorted by decreasing usage count both to make the
23444 uleb128 references as small as possible and to make sure they
23445 will have die_offset already computed by calc_die_sizes when
23446 sizes of typed stack loc ops is computed. */
23448 static void
23449 move_marked_base_types (void)
23451 unsigned int i;
23452 dw_die_ref base_type, die, c;
23454 if (base_types.is_empty ())
23455 return;
23457 /* Sort by decreasing usage count, they will be added again in that
23458 order later on. */
23459 base_types.qsort (base_type_cmp);
23460 die = comp_unit_die ();
23461 c = die->die_child;
23464 dw_die_ref prev = c;
23465 c = c->die_sib;
23466 while (c->die_mark)
23468 remove_child_with_prev (c, prev);
23469 /* As base types got marked, there must be at least
23470 one node other than DW_TAG_base_type. */
23471 gcc_assert (c != c->die_sib);
23472 c = c->die_sib;
23475 while (c != die->die_child);
23476 gcc_assert (die->die_child);
23477 c = die->die_child;
23478 for (i = 0; base_types.iterate (i, &base_type); i++)
23480 base_type->die_mark = 0;
23481 base_type->die_sib = c->die_sib;
23482 c->die_sib = base_type;
23483 c = base_type;
23487 /* Helper function for resolve_addr, attempt to resolve
23488 one CONST_STRING, return true if successful. Similarly verify that
23489 SYMBOL_REFs refer to variables emitted in the current CU. */
23491 static bool
23492 resolve_one_addr (rtx *addr)
23494 rtx rtl = *addr;
23496 if (GET_CODE (rtl) == CONST_STRING)
23498 size_t len = strlen (XSTR (rtl, 0)) + 1;
23499 tree t = build_string (len, XSTR (rtl, 0));
23500 tree tlen = size_int (len - 1);
23501 TREE_TYPE (t)
23502 = build_array_type (char_type_node, build_index_type (tlen));
23503 rtl = lookup_constant_def (t);
23504 if (!rtl || !MEM_P (rtl))
23505 return false;
23506 rtl = XEXP (rtl, 0);
23507 if (GET_CODE (rtl) == SYMBOL_REF
23508 && SYMBOL_REF_DECL (rtl)
23509 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
23510 return false;
23511 vec_safe_push (used_rtx_array, rtl);
23512 *addr = rtl;
23513 return true;
23516 if (GET_CODE (rtl) == SYMBOL_REF
23517 && SYMBOL_REF_DECL (rtl))
23519 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
23521 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
23522 return false;
23524 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
23525 return false;
23528 if (GET_CODE (rtl) == CONST)
23530 subrtx_ptr_iterator::array_type array;
23531 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
23532 if (!resolve_one_addr (*iter))
23533 return false;
23536 return true;
23539 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
23540 if possible, and create DW_TAG_dwarf_procedure that can be referenced
23541 from DW_OP_GNU_implicit_pointer if the string hasn't been seen yet. */
23543 static rtx
23544 string_cst_pool_decl (tree t)
23546 rtx rtl = output_constant_def (t, 1);
23547 unsigned char *array;
23548 dw_loc_descr_ref l;
23549 tree decl;
23550 size_t len;
23551 dw_die_ref ref;
23553 if (!rtl || !MEM_P (rtl))
23554 return NULL_RTX;
23555 rtl = XEXP (rtl, 0);
23556 if (GET_CODE (rtl) != SYMBOL_REF
23557 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
23558 return NULL_RTX;
23560 decl = SYMBOL_REF_DECL (rtl);
23561 if (!lookup_decl_die (decl))
23563 len = TREE_STRING_LENGTH (t);
23564 vec_safe_push (used_rtx_array, rtl);
23565 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
23566 array = ggc_vec_alloc<unsigned char> (len);
23567 memcpy (array, TREE_STRING_POINTER (t), len);
23568 l = new_loc_descr (DW_OP_implicit_value, len, 0);
23569 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
23570 l->dw_loc_oprnd2.v.val_vec.length = len;
23571 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
23572 l->dw_loc_oprnd2.v.val_vec.array = array;
23573 add_AT_loc (ref, DW_AT_location, l);
23574 equate_decl_number_to_die (decl, ref);
23576 return rtl;
23579 /* Helper function of resolve_addr_in_expr. LOC is
23580 a DW_OP_addr followed by DW_OP_stack_value, either at the start
23581 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
23582 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
23583 with DW_OP_GNU_implicit_pointer if possible
23584 and return true, if unsuccessful, return false. */
23586 static bool
23587 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
23589 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
23590 HOST_WIDE_INT offset = 0;
23591 dw_die_ref ref = NULL;
23592 tree decl;
23594 if (GET_CODE (rtl) == CONST
23595 && GET_CODE (XEXP (rtl, 0)) == PLUS
23596 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
23598 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
23599 rtl = XEXP (XEXP (rtl, 0), 0);
23601 if (GET_CODE (rtl) == CONST_STRING)
23603 size_t len = strlen (XSTR (rtl, 0)) + 1;
23604 tree t = build_string (len, XSTR (rtl, 0));
23605 tree tlen = size_int (len - 1);
23607 TREE_TYPE (t)
23608 = build_array_type (char_type_node, build_index_type (tlen));
23609 rtl = string_cst_pool_decl (t);
23610 if (!rtl)
23611 return false;
23613 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
23615 decl = SYMBOL_REF_DECL (rtl);
23616 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
23618 ref = lookup_decl_die (decl);
23619 if (ref && (get_AT (ref, DW_AT_location)
23620 || get_AT (ref, DW_AT_const_value)))
23622 loc->dw_loc_opc = DW_OP_GNU_implicit_pointer;
23623 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23624 loc->dw_loc_oprnd1.val_entry = NULL;
23625 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
23626 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
23627 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
23628 loc->dw_loc_oprnd2.v.val_int = offset;
23629 return true;
23633 return false;
23636 /* Helper function for resolve_addr, handle one location
23637 expression, return false if at least one CONST_STRING or SYMBOL_REF in
23638 the location list couldn't be resolved. */
23640 static bool
23641 resolve_addr_in_expr (dw_loc_descr_ref loc)
23643 dw_loc_descr_ref keep = NULL;
23644 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
23645 switch (loc->dw_loc_opc)
23647 case DW_OP_addr:
23648 if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
23650 if ((prev == NULL
23651 || prev->dw_loc_opc == DW_OP_piece
23652 || prev->dw_loc_opc == DW_OP_bit_piece)
23653 && loc->dw_loc_next
23654 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
23655 && !dwarf_strict
23656 && optimize_one_addr_into_implicit_ptr (loc))
23657 break;
23658 return false;
23660 break;
23661 case DW_OP_GNU_addr_index:
23662 case DW_OP_GNU_const_index:
23663 if (loc->dw_loc_opc == DW_OP_GNU_addr_index
23664 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
23666 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
23667 if (!resolve_one_addr (&rtl))
23668 return false;
23669 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
23670 loc->dw_loc_oprnd1.val_entry =
23671 add_addr_table_entry (rtl, ate_kind_rtx);
23673 break;
23674 case DW_OP_const4u:
23675 case DW_OP_const8u:
23676 if (loc->dtprel
23677 && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
23678 return false;
23679 break;
23680 case DW_OP_plus_uconst:
23681 if (size_of_loc_descr (loc)
23682 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
23684 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
23686 dw_loc_descr_ref repl
23687 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
23688 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
23689 add_loc_descr (&repl, loc->dw_loc_next);
23690 *loc = *repl;
23692 break;
23693 case DW_OP_implicit_value:
23694 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
23695 && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
23696 return false;
23697 break;
23698 case DW_OP_GNU_implicit_pointer:
23699 case DW_OP_GNU_parameter_ref:
23700 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
23702 dw_die_ref ref
23703 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
23704 if (ref == NULL)
23705 return false;
23706 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23707 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
23708 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
23710 break;
23711 case DW_OP_GNU_const_type:
23712 case DW_OP_GNU_regval_type:
23713 case DW_OP_GNU_deref_type:
23714 case DW_OP_GNU_convert:
23715 case DW_OP_GNU_reinterpret:
23716 while (loc->dw_loc_next
23717 && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
23719 dw_die_ref base1, base2;
23720 unsigned enc1, enc2, size1, size2;
23721 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
23722 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
23723 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
23724 else if (loc->dw_loc_oprnd1.val_class
23725 == dw_val_class_unsigned_const)
23726 break;
23727 else
23728 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
23729 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
23730 == dw_val_class_unsigned_const)
23731 break;
23732 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
23733 gcc_assert (base1->die_tag == DW_TAG_base_type
23734 && base2->die_tag == DW_TAG_base_type);
23735 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
23736 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
23737 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
23738 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
23739 if (size1 == size2
23740 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
23741 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
23742 && loc != keep)
23743 || enc1 == enc2))
23745 /* Optimize away next DW_OP_GNU_convert after
23746 adjusting LOC's base type die reference. */
23747 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
23748 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
23749 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
23750 else
23751 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
23752 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
23753 continue;
23755 /* Don't change integer DW_OP_GNU_convert after e.g. floating
23756 point typed stack entry. */
23757 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
23758 keep = loc->dw_loc_next;
23759 break;
23761 break;
23762 default:
23763 break;
23765 return true;
23768 /* Helper function of resolve_addr. DIE had DW_AT_location of
23769 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
23770 and DW_OP_addr couldn't be resolved. resolve_addr has already
23771 removed the DW_AT_location attribute. This function attempts to
23772 add a new DW_AT_location attribute with DW_OP_GNU_implicit_pointer
23773 to it or DW_AT_const_value attribute, if possible. */
23775 static void
23776 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
23778 if (TREE_CODE (decl) != VAR_DECL
23779 || lookup_decl_die (decl) != die
23780 || DECL_EXTERNAL (decl)
23781 || !TREE_STATIC (decl)
23782 || DECL_INITIAL (decl) == NULL_TREE
23783 || DECL_P (DECL_INITIAL (decl))
23784 || get_AT (die, DW_AT_const_value))
23785 return;
23787 tree init = DECL_INITIAL (decl);
23788 HOST_WIDE_INT offset = 0;
23789 /* For variables that have been optimized away and thus
23790 don't have a memory location, see if we can emit
23791 DW_AT_const_value instead. */
23792 if (tree_add_const_value_attribute (die, init))
23793 return;
23794 if (dwarf_strict)
23795 return;
23796 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
23797 and ADDR_EXPR refers to a decl that has DW_AT_location or
23798 DW_AT_const_value (but isn't addressable, otherwise
23799 resolving the original DW_OP_addr wouldn't fail), see if
23800 we can add DW_OP_GNU_implicit_pointer. */
23801 STRIP_NOPS (init);
23802 if (TREE_CODE (init) == POINTER_PLUS_EXPR
23803 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
23805 offset = tree_to_shwi (TREE_OPERAND (init, 1));
23806 init = TREE_OPERAND (init, 0);
23807 STRIP_NOPS (init);
23809 if (TREE_CODE (init) != ADDR_EXPR)
23810 return;
23811 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
23812 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
23813 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
23814 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
23815 && TREE_OPERAND (init, 0) != decl))
23817 dw_die_ref ref;
23818 dw_loc_descr_ref l;
23820 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
23822 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
23823 if (!rtl)
23824 return;
23825 decl = SYMBOL_REF_DECL (rtl);
23827 else
23828 decl = TREE_OPERAND (init, 0);
23829 ref = lookup_decl_die (decl);
23830 if (ref == NULL
23831 || (!get_AT (ref, DW_AT_location)
23832 && !get_AT (ref, DW_AT_const_value)))
23833 return;
23834 l = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
23835 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23836 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
23837 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
23838 add_AT_loc (die, DW_AT_location, l);
23842 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
23843 an address in .rodata section if the string literal is emitted there,
23844 or remove the containing location list or replace DW_AT_const_value
23845 with DW_AT_location and empty location expression, if it isn't found
23846 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
23847 to something that has been emitted in the current CU. */
23849 static void
23850 resolve_addr (dw_die_ref die)
23852 dw_die_ref c;
23853 dw_attr_ref a;
23854 dw_loc_list_ref *curr, *start, loc;
23855 unsigned ix;
23857 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23858 switch (AT_class (a))
23860 case dw_val_class_loc_list:
23861 start = curr = AT_loc_list_ptr (a);
23862 loc = *curr;
23863 gcc_assert (loc);
23864 /* The same list can be referenced more than once. See if we have
23865 already recorded the result from a previous pass. */
23866 if (loc->replaced)
23867 *curr = loc->dw_loc_next;
23868 else if (!loc->resolved_addr)
23870 /* As things stand, we do not expect or allow one die to
23871 reference a suffix of another die's location list chain.
23872 References must be identical or completely separate.
23873 There is therefore no need to cache the result of this
23874 pass on any list other than the first; doing so
23875 would lead to unnecessary writes. */
23876 while (*curr)
23878 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
23879 if (!resolve_addr_in_expr ((*curr)->expr))
23881 dw_loc_list_ref next = (*curr)->dw_loc_next;
23882 dw_loc_descr_ref l = (*curr)->expr;
23884 if (next && (*curr)->ll_symbol)
23886 gcc_assert (!next->ll_symbol);
23887 next->ll_symbol = (*curr)->ll_symbol;
23889 if (dwarf_split_debug_info)
23890 remove_loc_list_addr_table_entries (l);
23891 *curr = next;
23893 else
23895 mark_base_types ((*curr)->expr);
23896 curr = &(*curr)->dw_loc_next;
23899 if (loc == *start)
23900 loc->resolved_addr = 1;
23901 else
23903 loc->replaced = 1;
23904 loc->dw_loc_next = *start;
23907 if (!*start)
23909 remove_AT (die, a->dw_attr);
23910 ix--;
23912 break;
23913 case dw_val_class_loc:
23915 dw_loc_descr_ref l = AT_loc (a);
23916 /* For -gdwarf-2 don't attempt to optimize
23917 DW_AT_data_member_location containing
23918 DW_OP_plus_uconst - older consumers might
23919 rely on it being that op instead of a more complex,
23920 but shorter, location description. */
23921 if ((dwarf_version > 2
23922 || a->dw_attr != DW_AT_data_member_location
23923 || l == NULL
23924 || l->dw_loc_opc != DW_OP_plus_uconst
23925 || l->dw_loc_next != NULL)
23926 && !resolve_addr_in_expr (l))
23928 if (dwarf_split_debug_info)
23929 remove_loc_list_addr_table_entries (l);
23930 if (l != NULL
23931 && l->dw_loc_next == NULL
23932 && l->dw_loc_opc == DW_OP_addr
23933 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
23934 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
23935 && a->dw_attr == DW_AT_location)
23937 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
23938 remove_AT (die, a->dw_attr);
23939 ix--;
23940 optimize_location_into_implicit_ptr (die, decl);
23941 break;
23943 remove_AT (die, a->dw_attr);
23944 ix--;
23946 else
23947 mark_base_types (l);
23949 break;
23950 case dw_val_class_addr:
23951 if (a->dw_attr == DW_AT_const_value
23952 && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
23954 if (AT_index (a) != NOT_INDEXED)
23955 remove_addr_table_entry (a->dw_attr_val.val_entry);
23956 remove_AT (die, a->dw_attr);
23957 ix--;
23959 if (die->die_tag == DW_TAG_GNU_call_site
23960 && a->dw_attr == DW_AT_abstract_origin)
23962 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
23963 dw_die_ref tdie = lookup_decl_die (tdecl);
23964 if (tdie == NULL
23965 && DECL_EXTERNAL (tdecl)
23966 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE)
23968 force_decl_die (tdecl);
23969 tdie = lookup_decl_die (tdecl);
23971 if (tdie)
23973 a->dw_attr_val.val_class = dw_val_class_die_ref;
23974 a->dw_attr_val.v.val_die_ref.die = tdie;
23975 a->dw_attr_val.v.val_die_ref.external = 0;
23977 else
23979 if (AT_index (a) != NOT_INDEXED)
23980 remove_addr_table_entry (a->dw_attr_val.val_entry);
23981 remove_AT (die, a->dw_attr);
23982 ix--;
23985 break;
23986 default:
23987 break;
23990 FOR_EACH_CHILD (die, c, resolve_addr (c));
23993 /* Helper routines for optimize_location_lists.
23994 This pass tries to share identical local lists in .debug_loc
23995 section. */
23997 /* Iteratively hash operands of LOC opcode into HSTATE. */
23999 static void
24000 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
24002 dw_val_ref val1 = &loc->dw_loc_oprnd1;
24003 dw_val_ref val2 = &loc->dw_loc_oprnd2;
24005 switch (loc->dw_loc_opc)
24007 case DW_OP_const4u:
24008 case DW_OP_const8u:
24009 if (loc->dtprel)
24010 goto hash_addr;
24011 /* FALLTHRU */
24012 case DW_OP_const1u:
24013 case DW_OP_const1s:
24014 case DW_OP_const2u:
24015 case DW_OP_const2s:
24016 case DW_OP_const4s:
24017 case DW_OP_const8s:
24018 case DW_OP_constu:
24019 case DW_OP_consts:
24020 case DW_OP_pick:
24021 case DW_OP_plus_uconst:
24022 case DW_OP_breg0:
24023 case DW_OP_breg1:
24024 case DW_OP_breg2:
24025 case DW_OP_breg3:
24026 case DW_OP_breg4:
24027 case DW_OP_breg5:
24028 case DW_OP_breg6:
24029 case DW_OP_breg7:
24030 case DW_OP_breg8:
24031 case DW_OP_breg9:
24032 case DW_OP_breg10:
24033 case DW_OP_breg11:
24034 case DW_OP_breg12:
24035 case DW_OP_breg13:
24036 case DW_OP_breg14:
24037 case DW_OP_breg15:
24038 case DW_OP_breg16:
24039 case DW_OP_breg17:
24040 case DW_OP_breg18:
24041 case DW_OP_breg19:
24042 case DW_OP_breg20:
24043 case DW_OP_breg21:
24044 case DW_OP_breg22:
24045 case DW_OP_breg23:
24046 case DW_OP_breg24:
24047 case DW_OP_breg25:
24048 case DW_OP_breg26:
24049 case DW_OP_breg27:
24050 case DW_OP_breg28:
24051 case DW_OP_breg29:
24052 case DW_OP_breg30:
24053 case DW_OP_breg31:
24054 case DW_OP_regx:
24055 case DW_OP_fbreg:
24056 case DW_OP_piece:
24057 case DW_OP_deref_size:
24058 case DW_OP_xderef_size:
24059 hstate.add_object (val1->v.val_int);
24060 break;
24061 case DW_OP_skip:
24062 case DW_OP_bra:
24064 int offset;
24066 gcc_assert (val1->val_class == dw_val_class_loc);
24067 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
24068 hstate.add_object (offset);
24070 break;
24071 case DW_OP_implicit_value:
24072 hstate.add_object (val1->v.val_unsigned);
24073 switch (val2->val_class)
24075 case dw_val_class_const:
24076 hstate.add_object (val2->v.val_int);
24077 break;
24078 case dw_val_class_vec:
24080 unsigned int elt_size = val2->v.val_vec.elt_size;
24081 unsigned int len = val2->v.val_vec.length;
24083 hstate.add_int (elt_size);
24084 hstate.add_int (len);
24085 hstate.add (val2->v.val_vec.array, len * elt_size);
24087 break;
24088 case dw_val_class_const_double:
24089 hstate.add_object (val2->v.val_double.low);
24090 hstate.add_object (val2->v.val_double.high);
24091 break;
24092 case dw_val_class_wide_int:
24093 hstate.add (val2->v.val_wide->get_val (),
24094 get_full_len (*val2->v.val_wide)
24095 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
24096 break;
24097 case dw_val_class_addr:
24098 inchash::add_rtx (val2->v.val_addr, hstate);
24099 break;
24100 default:
24101 gcc_unreachable ();
24103 break;
24104 case DW_OP_bregx:
24105 case DW_OP_bit_piece:
24106 hstate.add_object (val1->v.val_int);
24107 hstate.add_object (val2->v.val_int);
24108 break;
24109 case DW_OP_addr:
24110 hash_addr:
24111 if (loc->dtprel)
24113 unsigned char dtprel = 0xd1;
24114 hstate.add_object (dtprel);
24116 inchash::add_rtx (val1->v.val_addr, hstate);
24117 break;
24118 case DW_OP_GNU_addr_index:
24119 case DW_OP_GNU_const_index:
24121 if (loc->dtprel)
24123 unsigned char dtprel = 0xd1;
24124 hstate.add_object (dtprel);
24126 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
24128 break;
24129 case DW_OP_GNU_implicit_pointer:
24130 hstate.add_int (val2->v.val_int);
24131 break;
24132 case DW_OP_GNU_entry_value:
24133 hstate.add_object (val1->v.val_loc);
24134 break;
24135 case DW_OP_GNU_regval_type:
24136 case DW_OP_GNU_deref_type:
24138 unsigned int byte_size
24139 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
24140 unsigned int encoding
24141 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
24142 hstate.add_object (val1->v.val_int);
24143 hstate.add_object (byte_size);
24144 hstate.add_object (encoding);
24146 break;
24147 case DW_OP_GNU_convert:
24148 case DW_OP_GNU_reinterpret:
24149 if (val1->val_class == dw_val_class_unsigned_const)
24151 hstate.add_object (val1->v.val_unsigned);
24152 break;
24154 /* FALLTHRU */
24155 case DW_OP_GNU_const_type:
24157 unsigned int byte_size
24158 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
24159 unsigned int encoding
24160 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
24161 hstate.add_object (byte_size);
24162 hstate.add_object (encoding);
24163 if (loc->dw_loc_opc != DW_OP_GNU_const_type)
24164 break;
24165 hstate.add_object (val2->val_class);
24166 switch (val2->val_class)
24168 case dw_val_class_const:
24169 hstate.add_object (val2->v.val_int);
24170 break;
24171 case dw_val_class_vec:
24173 unsigned int elt_size = val2->v.val_vec.elt_size;
24174 unsigned int len = val2->v.val_vec.length;
24176 hstate.add_object (elt_size);
24177 hstate.add_object (len);
24178 hstate.add (val2->v.val_vec.array, len * elt_size);
24180 break;
24181 case dw_val_class_const_double:
24182 hstate.add_object (val2->v.val_double.low);
24183 hstate.add_object (val2->v.val_double.high);
24184 break;
24185 case dw_val_class_wide_int:
24186 hstate.add (val2->v.val_wide->get_val (),
24187 get_full_len (*val2->v.val_wide)
24188 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
24189 break;
24190 default:
24191 gcc_unreachable ();
24194 break;
24196 default:
24197 /* Other codes have no operands. */
24198 break;
24202 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
24204 static inline void
24205 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
24207 dw_loc_descr_ref l;
24208 bool sizes_computed = false;
24209 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
24210 size_of_locs (loc);
24212 for (l = loc; l != NULL; l = l->dw_loc_next)
24214 enum dwarf_location_atom opc = l->dw_loc_opc;
24215 hstate.add_object (opc);
24216 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
24218 size_of_locs (loc);
24219 sizes_computed = true;
24221 hash_loc_operands (l, hstate);
24225 /* Compute hash of the whole location list LIST_HEAD. */
24227 static inline void
24228 hash_loc_list (dw_loc_list_ref list_head)
24230 dw_loc_list_ref curr = list_head;
24231 inchash::hash hstate;
24233 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
24235 hstate.add (curr->begin, strlen (curr->begin) + 1);
24236 hstate.add (curr->end, strlen (curr->end) + 1);
24237 if (curr->section)
24238 hstate.add (curr->section, strlen (curr->section) + 1);
24239 hash_locs (curr->expr, hstate);
24241 list_head->hash = hstate.end ();
24244 /* Return true if X and Y opcodes have the same operands. */
24246 static inline bool
24247 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
24249 dw_val_ref valx1 = &x->dw_loc_oprnd1;
24250 dw_val_ref valx2 = &x->dw_loc_oprnd2;
24251 dw_val_ref valy1 = &y->dw_loc_oprnd1;
24252 dw_val_ref valy2 = &y->dw_loc_oprnd2;
24254 switch (x->dw_loc_opc)
24256 case DW_OP_const4u:
24257 case DW_OP_const8u:
24258 if (x->dtprel)
24259 goto hash_addr;
24260 /* FALLTHRU */
24261 case DW_OP_const1u:
24262 case DW_OP_const1s:
24263 case DW_OP_const2u:
24264 case DW_OP_const2s:
24265 case DW_OP_const4s:
24266 case DW_OP_const8s:
24267 case DW_OP_constu:
24268 case DW_OP_consts:
24269 case DW_OP_pick:
24270 case DW_OP_plus_uconst:
24271 case DW_OP_breg0:
24272 case DW_OP_breg1:
24273 case DW_OP_breg2:
24274 case DW_OP_breg3:
24275 case DW_OP_breg4:
24276 case DW_OP_breg5:
24277 case DW_OP_breg6:
24278 case DW_OP_breg7:
24279 case DW_OP_breg8:
24280 case DW_OP_breg9:
24281 case DW_OP_breg10:
24282 case DW_OP_breg11:
24283 case DW_OP_breg12:
24284 case DW_OP_breg13:
24285 case DW_OP_breg14:
24286 case DW_OP_breg15:
24287 case DW_OP_breg16:
24288 case DW_OP_breg17:
24289 case DW_OP_breg18:
24290 case DW_OP_breg19:
24291 case DW_OP_breg20:
24292 case DW_OP_breg21:
24293 case DW_OP_breg22:
24294 case DW_OP_breg23:
24295 case DW_OP_breg24:
24296 case DW_OP_breg25:
24297 case DW_OP_breg26:
24298 case DW_OP_breg27:
24299 case DW_OP_breg28:
24300 case DW_OP_breg29:
24301 case DW_OP_breg30:
24302 case DW_OP_breg31:
24303 case DW_OP_regx:
24304 case DW_OP_fbreg:
24305 case DW_OP_piece:
24306 case DW_OP_deref_size:
24307 case DW_OP_xderef_size:
24308 return valx1->v.val_int == valy1->v.val_int;
24309 case DW_OP_skip:
24310 case DW_OP_bra:
24311 /* If splitting debug info, the use of DW_OP_GNU_addr_index
24312 can cause irrelevant differences in dw_loc_addr. */
24313 gcc_assert (valx1->val_class == dw_val_class_loc
24314 && valy1->val_class == dw_val_class_loc
24315 && (dwarf_split_debug_info
24316 || x->dw_loc_addr == y->dw_loc_addr));
24317 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
24318 case DW_OP_implicit_value:
24319 if (valx1->v.val_unsigned != valy1->v.val_unsigned
24320 || valx2->val_class != valy2->val_class)
24321 return false;
24322 switch (valx2->val_class)
24324 case dw_val_class_const:
24325 return valx2->v.val_int == valy2->v.val_int;
24326 case dw_val_class_vec:
24327 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
24328 && valx2->v.val_vec.length == valy2->v.val_vec.length
24329 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
24330 valx2->v.val_vec.elt_size
24331 * valx2->v.val_vec.length) == 0;
24332 case dw_val_class_const_double:
24333 return valx2->v.val_double.low == valy2->v.val_double.low
24334 && valx2->v.val_double.high == valy2->v.val_double.high;
24335 case dw_val_class_wide_int:
24336 return *valx2->v.val_wide == *valy2->v.val_wide;
24337 case dw_val_class_addr:
24338 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
24339 default:
24340 gcc_unreachable ();
24342 case DW_OP_bregx:
24343 case DW_OP_bit_piece:
24344 return valx1->v.val_int == valy1->v.val_int
24345 && valx2->v.val_int == valy2->v.val_int;
24346 case DW_OP_addr:
24347 hash_addr:
24348 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
24349 case DW_OP_GNU_addr_index:
24350 case DW_OP_GNU_const_index:
24352 rtx ax1 = valx1->val_entry->addr.rtl;
24353 rtx ay1 = valy1->val_entry->addr.rtl;
24354 return rtx_equal_p (ax1, ay1);
24356 case DW_OP_GNU_implicit_pointer:
24357 return valx1->val_class == dw_val_class_die_ref
24358 && valx1->val_class == valy1->val_class
24359 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
24360 && valx2->v.val_int == valy2->v.val_int;
24361 case DW_OP_GNU_entry_value:
24362 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
24363 case DW_OP_GNU_const_type:
24364 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
24365 || valx2->val_class != valy2->val_class)
24366 return false;
24367 switch (valx2->val_class)
24369 case dw_val_class_const:
24370 return valx2->v.val_int == valy2->v.val_int;
24371 case dw_val_class_vec:
24372 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
24373 && valx2->v.val_vec.length == valy2->v.val_vec.length
24374 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
24375 valx2->v.val_vec.elt_size
24376 * valx2->v.val_vec.length) == 0;
24377 case dw_val_class_const_double:
24378 return valx2->v.val_double.low == valy2->v.val_double.low
24379 && valx2->v.val_double.high == valy2->v.val_double.high;
24380 case dw_val_class_wide_int:
24381 return *valx2->v.val_wide == *valy2->v.val_wide;
24382 default:
24383 gcc_unreachable ();
24385 case DW_OP_GNU_regval_type:
24386 case DW_OP_GNU_deref_type:
24387 return valx1->v.val_int == valy1->v.val_int
24388 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
24389 case DW_OP_GNU_convert:
24390 case DW_OP_GNU_reinterpret:
24391 if (valx1->val_class != valy1->val_class)
24392 return false;
24393 if (valx1->val_class == dw_val_class_unsigned_const)
24394 return valx1->v.val_unsigned == valy1->v.val_unsigned;
24395 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
24396 case DW_OP_GNU_parameter_ref:
24397 return valx1->val_class == dw_val_class_die_ref
24398 && valx1->val_class == valy1->val_class
24399 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
24400 default:
24401 /* Other codes have no operands. */
24402 return true;
24406 /* Return true if DWARF location expressions X and Y are the same. */
24408 static inline bool
24409 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
24411 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
24412 if (x->dw_loc_opc != y->dw_loc_opc
24413 || x->dtprel != y->dtprel
24414 || !compare_loc_operands (x, y))
24415 break;
24416 return x == NULL && y == NULL;
24419 /* Hashtable helpers. */
24421 struct loc_list_hasher : typed_noop_remove <dw_loc_list_struct>
24423 typedef dw_loc_list_struct *value_type;
24424 typedef dw_loc_list_struct *compare_type;
24425 static inline hashval_t hash (const dw_loc_list_struct *);
24426 static inline bool equal (const dw_loc_list_struct *,
24427 const dw_loc_list_struct *);
24430 /* Return precomputed hash of location list X. */
24432 inline hashval_t
24433 loc_list_hasher::hash (const dw_loc_list_struct *x)
24435 return x->hash;
24438 /* Return true if location lists A and B are the same. */
24440 inline bool
24441 loc_list_hasher::equal (const dw_loc_list_struct *a,
24442 const dw_loc_list_struct *b)
24444 if (a == b)
24445 return 1;
24446 if (a->hash != b->hash)
24447 return 0;
24448 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
24449 if (strcmp (a->begin, b->begin) != 0
24450 || strcmp (a->end, b->end) != 0
24451 || (a->section == NULL) != (b->section == NULL)
24452 || (a->section && strcmp (a->section, b->section) != 0)
24453 || !compare_locs (a->expr, b->expr))
24454 break;
24455 return a == NULL && b == NULL;
24458 typedef hash_table<loc_list_hasher> loc_list_hash_type;
24461 /* Recursively optimize location lists referenced from DIE
24462 children and share them whenever possible. */
24464 static void
24465 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
24467 dw_die_ref c;
24468 dw_attr_ref a;
24469 unsigned ix;
24470 dw_loc_list_struct **slot;
24472 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
24473 if (AT_class (a) == dw_val_class_loc_list)
24475 dw_loc_list_ref list = AT_loc_list (a);
24476 /* TODO: perform some optimizations here, before hashing
24477 it and storing into the hash table. */
24478 hash_loc_list (list);
24479 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
24480 if (*slot == NULL)
24481 *slot = list;
24482 else
24483 a->dw_attr_val.v.val_loc_list = *slot;
24486 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
24490 /* Recursively assign each location list a unique index into the debug_addr
24491 section. */
24493 static void
24494 index_location_lists (dw_die_ref die)
24496 dw_die_ref c;
24497 dw_attr_ref a;
24498 unsigned ix;
24500 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
24501 if (AT_class (a) == dw_val_class_loc_list)
24503 dw_loc_list_ref list = AT_loc_list (a);
24504 dw_loc_list_ref curr;
24505 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
24507 /* Don't index an entry that has already been indexed
24508 or won't be output. */
24509 if (curr->begin_entry != NULL
24510 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
24511 continue;
24513 curr->begin_entry
24514 = add_addr_table_entry (xstrdup (curr->begin),
24515 ate_kind_label);
24519 FOR_EACH_CHILD (die, c, index_location_lists (c));
24522 /* Optimize location lists referenced from DIE
24523 children and share them whenever possible. */
24525 static void
24526 optimize_location_lists (dw_die_ref die)
24528 loc_list_hash_type htab (500);
24529 optimize_location_lists_1 (die, &htab);
24532 /* Output stuff that dwarf requires at the end of every file,
24533 and generate the DWARF-2 debugging info. */
24535 static void
24536 dwarf2out_finish (const char *filename)
24538 limbo_die_node *node, *next_node;
24539 comdat_type_node *ctnode;
24540 unsigned int i;
24541 dw_die_ref main_comp_unit_die;
24543 /* PCH might result in DW_AT_producer string being restored from the
24544 header compilation, so always fill it with empty string initially
24545 and overwrite only here. */
24546 dw_attr_ref producer = get_AT (comp_unit_die (), DW_AT_producer);
24547 producer_string = gen_producer_string ();
24548 producer->dw_attr_val.v.val_str->refcount--;
24549 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
24551 gen_scheduled_generic_parms_dies ();
24552 gen_remaining_tmpl_value_param_die_attribute ();
24554 /* Add the name for the main input file now. We delayed this from
24555 dwarf2out_init to avoid complications with PCH.
24556 For LTO produced units use a fixed artificial name to avoid
24557 leaking tempfile names into the dwarf. */
24558 if (!in_lto_p)
24559 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
24560 else
24561 add_name_attribute (comp_unit_die (), "<artificial>");
24562 if (!IS_ABSOLUTE_PATH (filename) || targetm.force_at_comp_dir)
24563 add_comp_dir_attribute (comp_unit_die ());
24564 else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
24566 bool p = false;
24567 file_table->traverse<bool *, file_table_relative_p> (&p);
24568 if (p)
24569 add_comp_dir_attribute (comp_unit_die ());
24572 if (deferred_locations_list)
24573 for (i = 0; i < deferred_locations_list->length (); i++)
24575 add_location_or_const_value_attribute (
24576 (*deferred_locations_list)[i].die,
24577 (*deferred_locations_list)[i].variable,
24578 false,
24579 DW_AT_location);
24582 /* Traverse the limbo die list, and add parent/child links. The only
24583 dies without parents that should be here are concrete instances of
24584 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
24585 For concrete instances, we can get the parent die from the abstract
24586 instance. */
24587 for (node = limbo_die_list; node; node = next_node)
24589 dw_die_ref die = node->die;
24590 next_node = node->next;
24592 if (die->die_parent == NULL)
24594 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
24596 if (origin && origin->die_parent)
24597 add_child_die (origin->die_parent, die);
24598 else if (is_cu_die (die))
24600 else if (seen_error ())
24601 /* It's OK to be confused by errors in the input. */
24602 add_child_die (comp_unit_die (), die);
24603 else
24605 /* In certain situations, the lexical block containing a
24606 nested function can be optimized away, which results
24607 in the nested function die being orphaned. Likewise
24608 with the return type of that nested function. Force
24609 this to be a child of the containing function.
24611 It may happen that even the containing function got fully
24612 inlined and optimized out. In that case we are lost and
24613 assign the empty child. This should not be big issue as
24614 the function is likely unreachable too. */
24615 gcc_assert (node->created_for);
24617 if (DECL_P (node->created_for))
24618 origin = get_context_die (DECL_CONTEXT (node->created_for));
24619 else if (TYPE_P (node->created_for))
24620 origin = scope_die_for (node->created_for, comp_unit_die ());
24621 else
24622 origin = comp_unit_die ();
24624 add_child_die (origin, die);
24629 limbo_die_list = NULL;
24631 #if ENABLE_ASSERT_CHECKING
24633 dw_die_ref die = comp_unit_die (), c;
24634 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
24636 #endif
24637 resolve_addr (comp_unit_die ());
24638 move_marked_base_types ();
24640 for (node = deferred_asm_name; node; node = node->next)
24642 tree decl = node->created_for;
24643 /* When generating LTO bytecode we can not generate new assembler
24644 names at this point and all important decls got theirs via
24645 free-lang-data. */
24646 if (((!flag_generate_lto && !flag_generate_offload)
24647 || DECL_ASSEMBLER_NAME_SET_P (decl))
24648 && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
24650 add_linkage_attr (node->die, decl);
24651 move_linkage_attr (node->die);
24655 deferred_asm_name = NULL;
24657 /* Walk through the list of incomplete types again, trying once more to
24658 emit full debugging info for them. */
24659 retry_incomplete_types ();
24661 if (flag_eliminate_unused_debug_types)
24662 prune_unused_types ();
24664 /* Generate separate COMDAT sections for type DIEs. */
24665 if (use_debug_types)
24667 break_out_comdat_types (comp_unit_die ());
24669 /* Each new type_unit DIE was added to the limbo die list when created.
24670 Since these have all been added to comdat_type_list, clear the
24671 limbo die list. */
24672 limbo_die_list = NULL;
24674 /* For each new comdat type unit, copy declarations for incomplete
24675 types to make the new unit self-contained (i.e., no direct
24676 references to the main compile unit). */
24677 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24678 copy_decls_for_unworthy_types (ctnode->root_die);
24679 copy_decls_for_unworthy_types (comp_unit_die ());
24681 /* In the process of copying declarations from one unit to another,
24682 we may have left some declarations behind that are no longer
24683 referenced. Prune them. */
24684 prune_unused_types ();
24687 /* Generate separate CUs for each of the include files we've seen.
24688 They will go into limbo_die_list. */
24689 if (flag_eliminate_dwarf2_dups)
24690 break_out_includes (comp_unit_die ());
24692 /* Traverse the DIE's and add add sibling attributes to those DIE's
24693 that have children. */
24694 add_sibling_attributes (comp_unit_die ());
24695 for (node = limbo_die_list; node; node = node->next)
24696 add_sibling_attributes (node->die);
24697 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24698 add_sibling_attributes (ctnode->root_die);
24700 /* When splitting DWARF info, we put some attributes in the
24701 skeleton compile_unit DIE that remains in the .o, while
24702 most attributes go in the DWO compile_unit_die. */
24703 if (dwarf_split_debug_info)
24704 main_comp_unit_die = gen_compile_unit_die (NULL);
24705 else
24706 main_comp_unit_die = comp_unit_die ();
24708 /* Output a terminator label for the .text section. */
24709 switch_to_section (text_section);
24710 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
24711 if (cold_text_section)
24713 switch_to_section (cold_text_section);
24714 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
24717 /* We can only use the low/high_pc attributes if all of the code was
24718 in .text. */
24719 if (!have_multiple_function_sections
24720 || (dwarf_version < 3 && dwarf_strict))
24722 /* Don't add if the CU has no associated code. */
24723 if (text_section_used)
24724 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
24725 text_end_label, true);
24727 else
24729 unsigned fde_idx;
24730 dw_fde_ref fde;
24731 bool range_list_added = false;
24733 if (text_section_used)
24734 add_ranges_by_labels (main_comp_unit_die, text_section_label,
24735 text_end_label, &range_list_added, true);
24736 if (cold_text_section_used)
24737 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
24738 cold_end_label, &range_list_added, true);
24740 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
24742 if (DECL_IGNORED_P (fde->decl))
24743 continue;
24744 if (!fde->in_std_section)
24745 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
24746 fde->dw_fde_end, &range_list_added,
24747 true);
24748 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
24749 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
24750 fde->dw_fde_second_end, &range_list_added,
24751 true);
24754 if (range_list_added)
24756 /* We need to give .debug_loc and .debug_ranges an appropriate
24757 "base address". Use zero so that these addresses become
24758 absolute. Historically, we've emitted the unexpected
24759 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
24760 Emit both to give time for other tools to adapt. */
24761 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
24762 if (! dwarf_strict && dwarf_version < 4)
24763 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
24765 add_ranges (NULL);
24769 if (debug_info_level >= DINFO_LEVEL_TERSE)
24770 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
24771 debug_line_section_label);
24773 if (have_macinfo)
24774 add_AT_macptr (comp_unit_die (),
24775 dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
24776 macinfo_section_label);
24778 if (dwarf_split_debug_info)
24780 /* optimize_location_lists calculates the size of the lists,
24781 so index them first, and assign indices to the entries.
24782 Although optimize_location_lists will remove entries from
24783 the table, it only does so for duplicates, and therefore
24784 only reduces ref_counts to 1. */
24785 index_location_lists (comp_unit_die ());
24787 if (addr_index_table != NULL)
24789 unsigned int index = 0;
24790 addr_index_table
24791 ->traverse_noresize<unsigned int *, index_addr_table_entry>
24792 (&index);
24796 if (have_location_lists)
24797 optimize_location_lists (comp_unit_die ());
24799 save_macinfo_strings ();
24801 if (dwarf_split_debug_info)
24803 unsigned int index = 0;
24805 /* Add attributes common to skeleton compile_units and
24806 type_units. Because these attributes include strings, it
24807 must be done before freezing the string table. Top-level
24808 skeleton die attrs are added when the skeleton type unit is
24809 created, so ensure it is created by this point. */
24810 add_top_level_skeleton_die_attrs (main_comp_unit_die);
24811 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
24814 /* Output all of the compilation units. We put the main one last so that
24815 the offsets are available to output_pubnames. */
24816 for (node = limbo_die_list; node; node = node->next)
24817 output_comp_unit (node->die, 0);
24819 hash_table<comdat_type_hasher> comdat_type_table (100);
24820 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24822 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
24824 /* Don't output duplicate types. */
24825 if (*slot != HTAB_EMPTY_ENTRY)
24826 continue;
24828 /* Add a pointer to the line table for the main compilation unit
24829 so that the debugger can make sense of DW_AT_decl_file
24830 attributes. */
24831 if (debug_info_level >= DINFO_LEVEL_TERSE)
24832 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
24833 (!dwarf_split_debug_info
24834 ? debug_line_section_label
24835 : debug_skeleton_line_section_label));
24837 output_comdat_type_unit (ctnode);
24838 *slot = ctnode;
24841 /* The AT_pubnames attribute needs to go in all skeleton dies, including
24842 both the main_cu and all skeleton TUs. Making this call unconditional
24843 would end up either adding a second copy of the AT_pubnames attribute, or
24844 requiring a special case in add_top_level_skeleton_die_attrs. */
24845 if (!dwarf_split_debug_info)
24846 add_AT_pubnames (comp_unit_die ());
24848 if (dwarf_split_debug_info)
24850 int mark;
24851 unsigned char checksum[16];
24852 struct md5_ctx ctx;
24854 /* Compute a checksum of the comp_unit to use as the dwo_id. */
24855 md5_init_ctx (&ctx);
24856 mark = 0;
24857 die_checksum (comp_unit_die (), &ctx, &mark);
24858 unmark_all_dies (comp_unit_die ());
24859 md5_finish_ctx (&ctx, checksum);
24861 /* Use the first 8 bytes of the checksum as the dwo_id,
24862 and add it to both comp-unit DIEs. */
24863 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
24864 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
24866 /* Add the base offset of the ranges table to the skeleton
24867 comp-unit DIE. */
24868 if (ranges_table_in_use)
24869 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
24870 ranges_section_label);
24872 switch_to_section (debug_addr_section);
24873 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
24874 output_addr_table ();
24877 /* Output the main compilation unit if non-empty or if .debug_macinfo
24878 or .debug_macro will be emitted. */
24879 output_comp_unit (comp_unit_die (), have_macinfo);
24881 if (dwarf_split_debug_info && info_section_emitted)
24882 output_skeleton_debug_sections (main_comp_unit_die);
24884 /* Output the abbreviation table. */
24885 if (abbrev_die_table_in_use != 1)
24887 switch_to_section (debug_abbrev_section);
24888 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
24889 output_abbrev_section ();
24892 /* Output location list section if necessary. */
24893 if (have_location_lists)
24895 /* Output the location lists info. */
24896 switch_to_section (debug_loc_section);
24897 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
24898 output_location_lists (comp_unit_die ());
24901 output_pubtables ();
24903 /* Output the address range information if a CU (.debug_info section)
24904 was emitted. We output an empty table even if we had no functions
24905 to put in it. This because the consumer has no way to tell the
24906 difference between an empty table that we omitted and failure to
24907 generate a table that would have contained data. */
24908 if (info_section_emitted)
24910 unsigned long aranges_length = size_of_aranges ();
24912 switch_to_section (debug_aranges_section);
24913 output_aranges (aranges_length);
24916 /* Output ranges section if necessary. */
24917 if (ranges_table_in_use)
24919 switch_to_section (debug_ranges_section);
24920 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
24921 output_ranges ();
24924 /* Have to end the macro section. */
24925 if (have_macinfo)
24927 switch_to_section (debug_macinfo_section);
24928 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
24929 output_macinfo ();
24930 dw2_asm_output_data (1, 0, "End compilation unit");
24933 /* Output the source line correspondence table. We must do this
24934 even if there is no line information. Otherwise, on an empty
24935 translation unit, we will generate a present, but empty,
24936 .debug_info section. IRIX 6.5 `nm' will then complain when
24937 examining the file. This is done late so that any filenames
24938 used by the debug_info section are marked as 'used'. */
24939 switch_to_section (debug_line_section);
24940 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
24941 if (! DWARF2_ASM_LINE_DEBUG_INFO)
24942 output_line_info (false);
24944 if (dwarf_split_debug_info && info_section_emitted)
24946 switch_to_section (debug_skeleton_line_section);
24947 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
24948 output_line_info (true);
24951 /* If we emitted any indirect strings, output the string table too. */
24952 if (debug_str_hash || skeleton_debug_str_hash)
24953 output_indirect_strings ();
24956 /* Reset all state within dwarf2out.c so that we can rerun the compiler
24957 within the same process. For use by toplev::finalize. */
24959 void
24960 dwarf2out_c_finalize (void)
24962 last_var_location_insn = NULL;
24963 cached_next_real_insn = NULL;
24964 used_rtx_array = NULL;
24965 incomplete_types = NULL;
24966 decl_scope_table = NULL;
24967 debug_info_section = NULL;
24968 debug_skeleton_info_section = NULL;
24969 debug_abbrev_section = NULL;
24970 debug_skeleton_abbrev_section = NULL;
24971 debug_aranges_section = NULL;
24972 debug_addr_section = NULL;
24973 debug_macinfo_section = NULL;
24974 debug_line_section = NULL;
24975 debug_skeleton_line_section = NULL;
24976 debug_loc_section = NULL;
24977 debug_pubnames_section = NULL;
24978 debug_pubtypes_section = NULL;
24979 debug_str_section = NULL;
24980 debug_str_dwo_section = NULL;
24981 debug_str_offsets_section = NULL;
24982 debug_ranges_section = NULL;
24983 debug_frame_section = NULL;
24984 fde_vec = NULL;
24985 debug_str_hash = NULL;
24986 skeleton_debug_str_hash = NULL;
24987 dw2_string_counter = 0;
24988 have_multiple_function_sections = false;
24989 text_section_used = false;
24990 cold_text_section_used = false;
24991 cold_text_section = NULL;
24992 current_unit_personality = NULL;
24994 deferred_locations_list = NULL;
24996 next_die_offset = 0;
24997 single_comp_unit_die = NULL;
24998 comdat_type_list = NULL;
24999 limbo_die_list = NULL;
25000 deferred_asm_name = NULL;
25001 file_table = NULL;
25002 decl_die_table = NULL;
25003 common_block_die_table = NULL;
25004 decl_loc_table = NULL;
25005 call_arg_locations = NULL;
25006 call_arg_loc_last = NULL;
25007 call_site_count = -1;
25008 tail_call_site_count = -1;
25009 //block_map = NULL;
25010 cached_dw_loc_list_table = NULL;
25011 abbrev_die_table = NULL;
25012 abbrev_die_table_allocated = 0;
25013 abbrev_die_table_in_use = 0;
25014 line_info_label_num = 0;
25015 cur_line_info_table = NULL;
25016 text_section_line_info = NULL;
25017 cold_text_section_line_info = NULL;
25018 separate_line_info = NULL;
25019 info_section_emitted = false;
25020 pubname_table = NULL;
25021 pubtype_table = NULL;
25022 macinfo_table = NULL;
25023 ranges_table = NULL;
25024 ranges_table_allocated = 0;
25025 ranges_table_in_use = 0;
25026 ranges_by_label = 0;
25027 ranges_by_label_allocated = 0;
25028 ranges_by_label_in_use = 0;
25029 have_location_lists = false;
25030 loclabel_num = 0;
25031 poc_label_num = 0;
25032 last_emitted_file = NULL;
25033 label_num = 0;
25034 file_table_last_lookup = NULL;
25035 tmpl_value_parm_die_table = NULL;
25036 generic_type_instances = NULL;
25037 frame_pointer_fb_offset = 0;
25038 frame_pointer_fb_offset_valid = false;
25039 base_types.release ();
25040 XDELETEVEC (producer_string);
25041 producer_string = NULL;
25044 #include "gt-dwarf2out.h"