2015-02-05 Robert Dewar <dewar@adacore.com>
[official-gcc.git] / gcc / dwarf2out.c
blobdb82710ecfb368534db8969a1293fe060c602e1f
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 == DWARF2_ADDR_SIZE);
1892 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
1893 break;
1894 default:
1895 gcc_unreachable ();
1897 break;
1898 #else
1899 case DW_OP_const2u:
1900 case DW_OP_const2s:
1901 case DW_OP_const4u:
1902 case DW_OP_const4s:
1903 case DW_OP_const8u:
1904 case DW_OP_const8s:
1905 case DW_OP_skip:
1906 case DW_OP_bra:
1907 case DW_OP_implicit_value:
1908 /* We currently don't make any attempt to make sure these are
1909 aligned properly like we do for the main unwind info, so
1910 don't support emitting things larger than a byte if we're
1911 only doing unwinding. */
1912 gcc_unreachable ();
1913 #endif
1914 case DW_OP_const1u:
1915 case DW_OP_const1s:
1916 dw2_asm_output_data (1, val1->v.val_int, NULL);
1917 break;
1918 case DW_OP_constu:
1919 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1920 break;
1921 case DW_OP_consts:
1922 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1923 break;
1924 case DW_OP_pick:
1925 dw2_asm_output_data (1, val1->v.val_int, NULL);
1926 break;
1927 case DW_OP_plus_uconst:
1928 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1929 break;
1930 case DW_OP_breg0:
1931 case DW_OP_breg1:
1932 case DW_OP_breg2:
1933 case DW_OP_breg3:
1934 case DW_OP_breg4:
1935 case DW_OP_breg5:
1936 case DW_OP_breg6:
1937 case DW_OP_breg7:
1938 case DW_OP_breg8:
1939 case DW_OP_breg9:
1940 case DW_OP_breg10:
1941 case DW_OP_breg11:
1942 case DW_OP_breg12:
1943 case DW_OP_breg13:
1944 case DW_OP_breg14:
1945 case DW_OP_breg15:
1946 case DW_OP_breg16:
1947 case DW_OP_breg17:
1948 case DW_OP_breg18:
1949 case DW_OP_breg19:
1950 case DW_OP_breg20:
1951 case DW_OP_breg21:
1952 case DW_OP_breg22:
1953 case DW_OP_breg23:
1954 case DW_OP_breg24:
1955 case DW_OP_breg25:
1956 case DW_OP_breg26:
1957 case DW_OP_breg27:
1958 case DW_OP_breg28:
1959 case DW_OP_breg29:
1960 case DW_OP_breg30:
1961 case DW_OP_breg31:
1962 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1963 break;
1964 case DW_OP_regx:
1966 unsigned r = val1->v.val_unsigned;
1967 if (for_eh_or_skip >= 0)
1968 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1969 gcc_assert (size_of_uleb128 (r)
1970 == size_of_uleb128 (val1->v.val_unsigned));
1971 dw2_asm_output_data_uleb128 (r, NULL);
1973 break;
1974 case DW_OP_fbreg:
1975 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1976 break;
1977 case DW_OP_bregx:
1979 unsigned r = val1->v.val_unsigned;
1980 if (for_eh_or_skip >= 0)
1981 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1982 gcc_assert (size_of_uleb128 (r)
1983 == size_of_uleb128 (val1->v.val_unsigned));
1984 dw2_asm_output_data_uleb128 (r, NULL);
1985 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
1987 break;
1988 case DW_OP_piece:
1989 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1990 break;
1991 case DW_OP_bit_piece:
1992 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1993 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
1994 break;
1995 case DW_OP_deref_size:
1996 case DW_OP_xderef_size:
1997 dw2_asm_output_data (1, val1->v.val_int, NULL);
1998 break;
2000 case DW_OP_addr:
2001 if (loc->dtprel)
2003 if (targetm.asm_out.output_dwarf_dtprel)
2005 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
2006 DWARF2_ADDR_SIZE,
2007 val1->v.val_addr);
2008 fputc ('\n', asm_out_file);
2010 else
2011 gcc_unreachable ();
2013 else
2015 #ifdef DWARF2_DEBUGGING_INFO
2016 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
2017 #else
2018 gcc_unreachable ();
2019 #endif
2021 break;
2023 case DW_OP_GNU_addr_index:
2024 case DW_OP_GNU_const_index:
2025 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
2026 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
2027 "(index into .debug_addr)");
2028 break;
2030 case DW_OP_GNU_implicit_pointer:
2032 char label[MAX_ARTIFICIAL_LABEL_BYTES
2033 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2034 gcc_assert (val1->val_class == dw_val_class_die_ref);
2035 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2036 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2037 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2039 break;
2041 case DW_OP_GNU_entry_value:
2042 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2043 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2044 break;
2046 case DW_OP_GNU_const_type:
2048 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2049 gcc_assert (o);
2050 dw2_asm_output_data_uleb128 (o, NULL);
2051 switch (val2->val_class)
2053 case dw_val_class_const:
2054 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2055 dw2_asm_output_data (1, l, NULL);
2056 dw2_asm_output_data (l, val2->v.val_int, NULL);
2057 break;
2058 case dw_val_class_vec:
2060 unsigned int elt_size = val2->v.val_vec.elt_size;
2061 unsigned int len = val2->v.val_vec.length;
2062 unsigned int i;
2063 unsigned char *p;
2065 l = len * elt_size;
2066 dw2_asm_output_data (1, l, NULL);
2067 if (elt_size > sizeof (HOST_WIDE_INT))
2069 elt_size /= 2;
2070 len *= 2;
2072 for (i = 0, p = val2->v.val_vec.array;
2073 i < len;
2074 i++, p += elt_size)
2075 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2076 "fp or vector constant word %u", i);
2078 break;
2079 case dw_val_class_const_double:
2081 unsigned HOST_WIDE_INT first, second;
2082 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2084 dw2_asm_output_data (1, 2 * l, NULL);
2085 if (WORDS_BIG_ENDIAN)
2087 first = val2->v.val_double.high;
2088 second = val2->v.val_double.low;
2090 else
2092 first = val2->v.val_double.low;
2093 second = val2->v.val_double.high;
2095 dw2_asm_output_data (l, first, NULL);
2096 dw2_asm_output_data (l, second, NULL);
2098 break;
2099 case dw_val_class_wide_int:
2101 int i;
2102 int len = get_full_len (*val2->v.val_wide);
2103 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2105 dw2_asm_output_data (1, len * l, NULL);
2106 if (WORDS_BIG_ENDIAN)
2107 for (i = len - 1; i >= 0; --i)
2108 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2109 else
2110 for (i = 0; i < len; ++i)
2111 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2113 break;
2114 default:
2115 gcc_unreachable ();
2118 break;
2119 case DW_OP_GNU_regval_type:
2121 unsigned r = val1->v.val_unsigned;
2122 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2123 gcc_assert (o);
2124 if (for_eh_or_skip >= 0)
2126 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2127 gcc_assert (size_of_uleb128 (r)
2128 == size_of_uleb128 (val1->v.val_unsigned));
2130 dw2_asm_output_data_uleb128 (r, NULL);
2131 dw2_asm_output_data_uleb128 (o, NULL);
2133 break;
2134 case DW_OP_GNU_deref_type:
2136 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2137 gcc_assert (o);
2138 dw2_asm_output_data (1, val1->v.val_int, NULL);
2139 dw2_asm_output_data_uleb128 (o, NULL);
2141 break;
2142 case DW_OP_GNU_convert:
2143 case DW_OP_GNU_reinterpret:
2144 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2145 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2146 else
2148 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2149 gcc_assert (o);
2150 dw2_asm_output_data_uleb128 (o, NULL);
2152 break;
2154 case DW_OP_GNU_parameter_ref:
2156 unsigned long o;
2157 gcc_assert (val1->val_class == dw_val_class_die_ref);
2158 o = get_ref_die_offset (val1->v.val_die_ref.die);
2159 dw2_asm_output_data (4, o, NULL);
2161 break;
2163 default:
2164 /* Other codes have no operands. */
2165 break;
2169 /* Output a sequence of location operations.
2170 The for_eh_or_skip parameter controls whether register numbers are
2171 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2172 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2173 info). This should be suppressed for the cases that have not been converted
2174 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2176 void
2177 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2179 for (; loc != NULL; loc = loc->dw_loc_next)
2181 enum dwarf_location_atom opc = loc->dw_loc_opc;
2182 /* Output the opcode. */
2183 if (for_eh_or_skip >= 0
2184 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2186 unsigned r = (opc - DW_OP_breg0);
2187 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2188 gcc_assert (r <= 31);
2189 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2191 else if (for_eh_or_skip >= 0
2192 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2194 unsigned r = (opc - DW_OP_reg0);
2195 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2196 gcc_assert (r <= 31);
2197 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2200 dw2_asm_output_data (1, opc,
2201 "%s", dwarf_stack_op_name (opc));
2203 /* Output the operand(s) (if any). */
2204 output_loc_operands (loc, for_eh_or_skip);
2208 /* Output location description stack opcode's operands (if any).
2209 The output is single bytes on a line, suitable for .cfi_escape. */
2211 static void
2212 output_loc_operands_raw (dw_loc_descr_ref loc)
2214 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2215 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2217 switch (loc->dw_loc_opc)
2219 case DW_OP_addr:
2220 case DW_OP_GNU_addr_index:
2221 case DW_OP_GNU_const_index:
2222 case DW_OP_implicit_value:
2223 /* We cannot output addresses in .cfi_escape, only bytes. */
2224 gcc_unreachable ();
2226 case DW_OP_const1u:
2227 case DW_OP_const1s:
2228 case DW_OP_pick:
2229 case DW_OP_deref_size:
2230 case DW_OP_xderef_size:
2231 fputc (',', asm_out_file);
2232 dw2_asm_output_data_raw (1, val1->v.val_int);
2233 break;
2235 case DW_OP_const2u:
2236 case DW_OP_const2s:
2237 fputc (',', asm_out_file);
2238 dw2_asm_output_data_raw (2, val1->v.val_int);
2239 break;
2241 case DW_OP_const4u:
2242 case DW_OP_const4s:
2243 fputc (',', asm_out_file);
2244 dw2_asm_output_data_raw (4, val1->v.val_int);
2245 break;
2247 case DW_OP_const8u:
2248 case DW_OP_const8s:
2249 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2250 fputc (',', asm_out_file);
2251 dw2_asm_output_data_raw (8, val1->v.val_int);
2252 break;
2254 case DW_OP_skip:
2255 case DW_OP_bra:
2257 int offset;
2259 gcc_assert (val1->val_class == dw_val_class_loc);
2260 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2262 fputc (',', asm_out_file);
2263 dw2_asm_output_data_raw (2, offset);
2265 break;
2267 case DW_OP_regx:
2269 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2270 gcc_assert (size_of_uleb128 (r)
2271 == size_of_uleb128 (val1->v.val_unsigned));
2272 fputc (',', asm_out_file);
2273 dw2_asm_output_data_uleb128_raw (r);
2275 break;
2277 case DW_OP_constu:
2278 case DW_OP_plus_uconst:
2279 case DW_OP_piece:
2280 fputc (',', asm_out_file);
2281 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2282 break;
2284 case DW_OP_bit_piece:
2285 fputc (',', asm_out_file);
2286 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2287 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2288 break;
2290 case DW_OP_consts:
2291 case DW_OP_breg0:
2292 case DW_OP_breg1:
2293 case DW_OP_breg2:
2294 case DW_OP_breg3:
2295 case DW_OP_breg4:
2296 case DW_OP_breg5:
2297 case DW_OP_breg6:
2298 case DW_OP_breg7:
2299 case DW_OP_breg8:
2300 case DW_OP_breg9:
2301 case DW_OP_breg10:
2302 case DW_OP_breg11:
2303 case DW_OP_breg12:
2304 case DW_OP_breg13:
2305 case DW_OP_breg14:
2306 case DW_OP_breg15:
2307 case DW_OP_breg16:
2308 case DW_OP_breg17:
2309 case DW_OP_breg18:
2310 case DW_OP_breg19:
2311 case DW_OP_breg20:
2312 case DW_OP_breg21:
2313 case DW_OP_breg22:
2314 case DW_OP_breg23:
2315 case DW_OP_breg24:
2316 case DW_OP_breg25:
2317 case DW_OP_breg26:
2318 case DW_OP_breg27:
2319 case DW_OP_breg28:
2320 case DW_OP_breg29:
2321 case DW_OP_breg30:
2322 case DW_OP_breg31:
2323 case DW_OP_fbreg:
2324 fputc (',', asm_out_file);
2325 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2326 break;
2328 case DW_OP_bregx:
2330 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2331 gcc_assert (size_of_uleb128 (r)
2332 == size_of_uleb128 (val1->v.val_unsigned));
2333 fputc (',', asm_out_file);
2334 dw2_asm_output_data_uleb128_raw (r);
2335 fputc (',', asm_out_file);
2336 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2338 break;
2340 case DW_OP_GNU_implicit_pointer:
2341 case DW_OP_GNU_entry_value:
2342 case DW_OP_GNU_const_type:
2343 case DW_OP_GNU_regval_type:
2344 case DW_OP_GNU_deref_type:
2345 case DW_OP_GNU_convert:
2346 case DW_OP_GNU_reinterpret:
2347 case DW_OP_GNU_parameter_ref:
2348 gcc_unreachable ();
2349 break;
2351 default:
2352 /* Other codes have no operands. */
2353 break;
2357 void
2358 output_loc_sequence_raw (dw_loc_descr_ref loc)
2360 while (1)
2362 enum dwarf_location_atom opc = loc->dw_loc_opc;
2363 /* Output the opcode. */
2364 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2366 unsigned r = (opc - DW_OP_breg0);
2367 r = DWARF2_FRAME_REG_OUT (r, 1);
2368 gcc_assert (r <= 31);
2369 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2371 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2373 unsigned r = (opc - DW_OP_reg0);
2374 r = DWARF2_FRAME_REG_OUT (r, 1);
2375 gcc_assert (r <= 31);
2376 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2378 /* Output the opcode. */
2379 fprintf (asm_out_file, "%#x", opc);
2380 output_loc_operands_raw (loc);
2382 if (!loc->dw_loc_next)
2383 break;
2384 loc = loc->dw_loc_next;
2386 fputc (',', asm_out_file);
2390 /* This function builds a dwarf location descriptor sequence from a
2391 dw_cfa_location, adding the given OFFSET to the result of the
2392 expression. */
2394 struct dw_loc_descr_node *
2395 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2397 struct dw_loc_descr_node *head, *tmp;
2399 offset += cfa->offset;
2401 if (cfa->indirect)
2403 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2404 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2405 head->dw_loc_oprnd1.val_entry = NULL;
2406 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2407 add_loc_descr (&head, tmp);
2408 if (offset != 0)
2410 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2411 add_loc_descr (&head, tmp);
2414 else
2415 head = new_reg_loc_descr (cfa->reg, offset);
2417 return head;
2420 /* This function builds a dwarf location descriptor sequence for
2421 the address at OFFSET from the CFA when stack is aligned to
2422 ALIGNMENT byte. */
2424 struct dw_loc_descr_node *
2425 build_cfa_aligned_loc (dw_cfa_location *cfa,
2426 HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2428 struct dw_loc_descr_node *head;
2429 unsigned int dwarf_fp
2430 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2432 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2433 if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2435 head = new_reg_loc_descr (dwarf_fp, 0);
2436 add_loc_descr (&head, int_loc_descriptor (alignment));
2437 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2438 loc_descr_plus_const (&head, offset);
2440 else
2441 head = new_reg_loc_descr (dwarf_fp, offset);
2442 return head;
2445 /* And now, the support for symbolic debugging information. */
2447 /* .debug_str support. */
2449 static void dwarf2out_init (const char *);
2450 static void dwarf2out_finish (const char *);
2451 static void dwarf2out_assembly_start (void);
2452 static void dwarf2out_define (unsigned int, const char *);
2453 static void dwarf2out_undef (unsigned int, const char *);
2454 static void dwarf2out_start_source_file (unsigned, const char *);
2455 static void dwarf2out_end_source_file (unsigned);
2456 static void dwarf2out_function_decl (tree);
2457 static void dwarf2out_begin_block (unsigned, unsigned);
2458 static void dwarf2out_end_block (unsigned, unsigned);
2459 static bool dwarf2out_ignore_block (const_tree);
2460 static void dwarf2out_global_decl (tree);
2461 static void dwarf2out_type_decl (tree, int);
2462 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
2463 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2464 dw_die_ref);
2465 static void dwarf2out_abstract_function (tree);
2466 static void dwarf2out_var_location (rtx_insn *);
2467 static void dwarf2out_begin_function (tree);
2468 static void dwarf2out_end_function (unsigned int);
2469 static void dwarf2out_set_name (tree, tree);
2471 /* The debug hooks structure. */
2473 const struct gcc_debug_hooks dwarf2_debug_hooks =
2475 dwarf2out_init,
2476 dwarf2out_finish,
2477 dwarf2out_assembly_start,
2478 dwarf2out_define,
2479 dwarf2out_undef,
2480 dwarf2out_start_source_file,
2481 dwarf2out_end_source_file,
2482 dwarf2out_begin_block,
2483 dwarf2out_end_block,
2484 dwarf2out_ignore_block,
2485 dwarf2out_source_line,
2486 dwarf2out_begin_prologue,
2487 #if VMS_DEBUGGING_INFO
2488 dwarf2out_vms_end_prologue,
2489 dwarf2out_vms_begin_epilogue,
2490 #else
2491 debug_nothing_int_charstar,
2492 debug_nothing_int_charstar,
2493 #endif
2494 dwarf2out_end_epilogue,
2495 dwarf2out_begin_function,
2496 dwarf2out_end_function, /* end_function */
2497 dwarf2out_function_decl, /* function_decl */
2498 dwarf2out_global_decl,
2499 dwarf2out_type_decl, /* type_decl */
2500 dwarf2out_imported_module_or_decl,
2501 debug_nothing_tree, /* deferred_inline_function */
2502 /* The DWARF 2 backend tries to reduce debugging bloat by not
2503 emitting the abstract description of inline functions until
2504 something tries to reference them. */
2505 dwarf2out_abstract_function, /* outlining_inline_function */
2506 debug_nothing_rtx_code_label, /* label */
2507 debug_nothing_int, /* handle_pch */
2508 dwarf2out_var_location,
2509 dwarf2out_switch_text_section,
2510 dwarf2out_set_name,
2511 1, /* start_end_main_source_file */
2512 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2515 /* NOTE: In the comments in this file, many references are made to
2516 "Debugging Information Entries". This term is abbreviated as `DIE'
2517 throughout the remainder of this file. */
2519 /* An internal representation of the DWARF output is built, and then
2520 walked to generate the DWARF debugging info. The walk of the internal
2521 representation is done after the entire program has been compiled.
2522 The types below are used to describe the internal representation. */
2524 /* Whether to put type DIEs into their own section .debug_types instead
2525 of making them part of the .debug_info section. Only supported for
2526 Dwarf V4 or higher and the user didn't disable them through
2527 -fno-debug-types-section. It is more efficient to put them in a
2528 separate comdat sections since the linker will then be able to
2529 remove duplicates. But not all tools support .debug_types sections
2530 yet. */
2532 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2534 /* Various DIE's use offsets relative to the beginning of the
2535 .debug_info section to refer to each other. */
2537 typedef long int dw_offset;
2539 /* Define typedefs here to avoid circular dependencies. */
2541 typedef struct dw_attr_struct *dw_attr_ref;
2542 typedef struct dw_line_info_struct *dw_line_info_ref;
2543 typedef struct pubname_struct *pubname_ref;
2544 typedef struct dw_ranges_struct *dw_ranges_ref;
2545 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
2546 typedef struct comdat_type_struct *comdat_type_node_ref;
2548 /* The entries in the line_info table more-or-less mirror the opcodes
2549 that are used in the real dwarf line table. Arrays of these entries
2550 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2551 supported. */
2553 enum dw_line_info_opcode {
2554 /* Emit DW_LNE_set_address; the operand is the label index. */
2555 LI_set_address,
2557 /* Emit a row to the matrix with the given line. This may be done
2558 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2559 special opcodes. */
2560 LI_set_line,
2562 /* Emit a DW_LNS_set_file. */
2563 LI_set_file,
2565 /* Emit a DW_LNS_set_column. */
2566 LI_set_column,
2568 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
2569 LI_negate_stmt,
2571 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
2572 LI_set_prologue_end,
2573 LI_set_epilogue_begin,
2575 /* Emit a DW_LNE_set_discriminator. */
2576 LI_set_discriminator
2579 typedef struct GTY(()) dw_line_info_struct {
2580 enum dw_line_info_opcode opcode;
2581 unsigned int val;
2582 } dw_line_info_entry;
2585 typedef struct GTY(()) dw_line_info_table_struct {
2586 /* The label that marks the end of this section. */
2587 const char *end_label;
2589 /* The values for the last row of the matrix, as collected in the table.
2590 These are used to minimize the changes to the next row. */
2591 unsigned int file_num;
2592 unsigned int line_num;
2593 unsigned int column_num;
2594 int discrim_num;
2595 bool is_stmt;
2596 bool in_use;
2598 vec<dw_line_info_entry, va_gc> *entries;
2599 } dw_line_info_table;
2601 typedef dw_line_info_table *dw_line_info_table_p;
2604 /* Each DIE attribute has a field specifying the attribute kind,
2605 a link to the next attribute in the chain, and an attribute value.
2606 Attributes are typically linked below the DIE they modify. */
2608 typedef struct GTY(()) dw_attr_struct {
2609 enum dwarf_attribute dw_attr;
2610 dw_val_node dw_attr_val;
2612 dw_attr_node;
2615 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
2616 The children of each node form a circular list linked by
2617 die_sib. die_child points to the node *before* the "first" child node. */
2619 typedef struct GTY((chain_circular ("%h.die_sib"), for_user)) die_struct {
2620 union die_symbol_or_type_node
2622 const char * GTY ((tag ("0"))) die_symbol;
2623 comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
2625 GTY ((desc ("%0.comdat_type_p"))) die_id;
2626 vec<dw_attr_node, va_gc> *die_attr;
2627 dw_die_ref die_parent;
2628 dw_die_ref die_child;
2629 dw_die_ref die_sib;
2630 dw_die_ref die_definition; /* ref from a specification to its definition */
2631 dw_offset die_offset;
2632 unsigned long die_abbrev;
2633 int die_mark;
2634 unsigned int decl_id;
2635 enum dwarf_tag die_tag;
2636 /* Die is used and must not be pruned as unused. */
2637 BOOL_BITFIELD die_perennial_p : 1;
2638 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2639 /* Lots of spare bits. */
2641 die_node;
2643 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
2644 #define FOR_EACH_CHILD(die, c, expr) do { \
2645 c = die->die_child; \
2646 if (c) do { \
2647 c = c->die_sib; \
2648 expr; \
2649 } while (c != die->die_child); \
2650 } while (0)
2652 /* The pubname structure */
2654 typedef struct GTY(()) pubname_struct {
2655 dw_die_ref die;
2656 const char *name;
2658 pubname_entry;
2661 struct GTY(()) dw_ranges_struct {
2662 /* If this is positive, it's a block number, otherwise it's a
2663 bitwise-negated index into dw_ranges_by_label. */
2664 int num;
2667 /* A structure to hold a macinfo entry. */
2669 typedef struct GTY(()) macinfo_struct {
2670 unsigned char code;
2671 unsigned HOST_WIDE_INT lineno;
2672 const char *info;
2674 macinfo_entry;
2677 struct GTY(()) dw_ranges_by_label_struct {
2678 const char *begin;
2679 const char *end;
2682 /* The comdat type node structure. */
2683 typedef struct GTY(()) comdat_type_struct
2685 dw_die_ref root_die;
2686 dw_die_ref type_die;
2687 dw_die_ref skeleton_die;
2688 char signature[DWARF_TYPE_SIGNATURE_SIZE];
2689 struct comdat_type_struct *next;
2691 comdat_type_node;
2693 /* The limbo die list structure. */
2694 typedef struct GTY(()) limbo_die_struct {
2695 dw_die_ref die;
2696 tree created_for;
2697 struct limbo_die_struct *next;
2699 limbo_die_node;
2701 typedef struct skeleton_chain_struct
2703 dw_die_ref old_die;
2704 dw_die_ref new_die;
2705 struct skeleton_chain_struct *parent;
2707 skeleton_chain_node;
2709 /* Define a macro which returns nonzero for a TYPE_DECL which was
2710 implicitly generated for a type.
2712 Note that, unlike the C front-end (which generates a NULL named
2713 TYPE_DECL node for each complete tagged type, each array type,
2714 and each function type node created) the C++ front-end generates
2715 a _named_ TYPE_DECL node for each tagged type node created.
2716 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
2717 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
2718 front-end, but for each type, tagged or not. */
2720 #define TYPE_DECL_IS_STUB(decl) \
2721 (DECL_NAME (decl) == NULL_TREE \
2722 || (DECL_ARTIFICIAL (decl) \
2723 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
2724 /* This is necessary for stub decls that \
2725 appear in nested inline functions. */ \
2726 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
2727 && (decl_ultimate_origin (decl) \
2728 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
2730 /* Information concerning the compilation unit's programming
2731 language, and compiler version. */
2733 /* Fixed size portion of the DWARF compilation unit header. */
2734 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
2735 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
2737 /* Fixed size portion of the DWARF comdat type unit header. */
2738 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
2739 (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
2740 + DWARF_OFFSET_SIZE)
2742 /* Fixed size portion of public names info. */
2743 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
2745 /* Fixed size portion of the address range info. */
2746 #define DWARF_ARANGES_HEADER_SIZE \
2747 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2748 DWARF2_ADDR_SIZE * 2) \
2749 - DWARF_INITIAL_LENGTH_SIZE)
2751 /* Size of padding portion in the address range info. It must be
2752 aligned to twice the pointer size. */
2753 #define DWARF_ARANGES_PAD_SIZE \
2754 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2755 DWARF2_ADDR_SIZE * 2) \
2756 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
2758 /* Use assembler line directives if available. */
2759 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
2760 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
2761 #define DWARF2_ASM_LINE_DEBUG_INFO 1
2762 #else
2763 #define DWARF2_ASM_LINE_DEBUG_INFO 0
2764 #endif
2765 #endif
2767 /* Minimum line offset in a special line info. opcode.
2768 This value was chosen to give a reasonable range of values. */
2769 #define DWARF_LINE_BASE -10
2771 /* First special line opcode - leave room for the standard opcodes. */
2772 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
2774 /* Range of line offsets in a special line info. opcode. */
2775 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
2777 /* Flag that indicates the initial value of the is_stmt_start flag.
2778 In the present implementation, we do not mark any lines as
2779 the beginning of a source statement, because that information
2780 is not made available by the GCC front-end. */
2781 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
2783 /* Maximum number of operations per instruction bundle. */
2784 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
2785 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
2786 #endif
2788 /* This location is used by calc_die_sizes() to keep track
2789 the offset of each DIE within the .debug_info section. */
2790 static unsigned long next_die_offset;
2792 /* Record the root of the DIE's built for the current compilation unit. */
2793 static GTY(()) dw_die_ref single_comp_unit_die;
2795 /* A list of type DIEs that have been separated into comdat sections. */
2796 static GTY(()) comdat_type_node *comdat_type_list;
2798 /* A list of DIEs with a NULL parent waiting to be relocated. */
2799 static GTY(()) limbo_die_node *limbo_die_list;
2801 /* A list of DIEs for which we may have to generate
2802 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
2803 static GTY(()) limbo_die_node *deferred_asm_name;
2805 struct dwarf_file_hasher : ggc_hasher<dwarf_file_data *>
2807 typedef const char *compare_type;
2809 static hashval_t hash (dwarf_file_data *);
2810 static bool equal (dwarf_file_data *, const char *);
2813 /* Filenames referenced by this compilation unit. */
2814 static GTY(()) hash_table<dwarf_file_hasher> *file_table;
2816 struct decl_die_hasher : ggc_hasher<die_node *>
2818 typedef tree compare_type;
2820 static hashval_t hash (die_node *);
2821 static bool equal (die_node *, tree);
2823 /* A hash table of references to DIE's that describe declarations.
2824 The key is a DECL_UID() which is a unique number identifying each decl. */
2825 static GTY (()) hash_table<decl_die_hasher> *decl_die_table;
2827 struct block_die_hasher : ggc_hasher<die_struct *>
2829 static hashval_t hash (die_struct *);
2830 static bool equal (die_struct *, die_struct *);
2833 /* A hash table of references to DIE's that describe COMMON blocks.
2834 The key is DECL_UID() ^ die_parent. */
2835 static GTY (()) hash_table<block_die_hasher> *common_block_die_table;
2837 typedef struct GTY(()) die_arg_entry_struct {
2838 dw_die_ref die;
2839 tree arg;
2840 } die_arg_entry;
2843 /* Node of the variable location list. */
2844 struct GTY ((chain_next ("%h.next"))) var_loc_node {
2845 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
2846 EXPR_LIST chain. For small bitsizes, bitsize is encoded
2847 in mode of the EXPR_LIST node and first EXPR_LIST operand
2848 is either NOTE_INSN_VAR_LOCATION for a piece with a known
2849 location or NULL for padding. For larger bitsizes,
2850 mode is 0 and first operand is a CONCAT with bitsize
2851 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
2852 NULL as second operand. */
2853 rtx GTY (()) loc;
2854 const char * GTY (()) label;
2855 struct var_loc_node * GTY (()) next;
2858 /* Variable location list. */
2859 struct GTY ((for_user)) var_loc_list_def {
2860 struct var_loc_node * GTY (()) first;
2862 /* Pointer to the last but one or last element of the
2863 chained list. If the list is empty, both first and
2864 last are NULL, if the list contains just one node
2865 or the last node certainly is not redundant, it points
2866 to the last node, otherwise points to the last but one.
2867 Do not mark it for GC because it is marked through the chain. */
2868 struct var_loc_node * GTY ((skip ("%h"))) last;
2870 /* Pointer to the last element before section switch,
2871 if NULL, either sections weren't switched or first
2872 is after section switch. */
2873 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
2875 /* DECL_UID of the variable decl. */
2876 unsigned int decl_id;
2878 typedef struct var_loc_list_def var_loc_list;
2880 /* Call argument location list. */
2881 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
2882 rtx GTY (()) call_arg_loc_note;
2883 const char * GTY (()) label;
2884 tree GTY (()) block;
2885 bool tail_call_p;
2886 rtx GTY (()) symbol_ref;
2887 struct call_arg_loc_node * GTY (()) next;
2891 struct decl_loc_hasher : ggc_hasher<var_loc_list *>
2893 typedef const_tree compare_type;
2895 static hashval_t hash (var_loc_list *);
2896 static bool equal (var_loc_list *, const_tree);
2899 /* Table of decl location linked lists. */
2900 static GTY (()) hash_table<decl_loc_hasher> *decl_loc_table;
2902 /* Head and tail of call_arg_loc chain. */
2903 static GTY (()) struct call_arg_loc_node *call_arg_locations;
2904 static struct call_arg_loc_node *call_arg_loc_last;
2906 /* Number of call sites in the current function. */
2907 static int call_site_count = -1;
2908 /* Number of tail call sites in the current function. */
2909 static int tail_call_site_count = -1;
2911 /* Vector mapping block numbers to DW_TAG_{lexical_block,inlined_subroutine}
2912 DIEs. */
2913 static vec<dw_die_ref> block_map;
2915 /* A cached location list. */
2916 struct GTY ((for_user)) cached_dw_loc_list_def {
2917 /* The DECL_UID of the decl that this entry describes. */
2918 unsigned int decl_id;
2920 /* The cached location list. */
2921 dw_loc_list_ref loc_list;
2923 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
2925 struct dw_loc_list_hasher : ggc_hasher<cached_dw_loc_list *>
2928 typedef const_tree compare_type;
2930 static hashval_t hash (cached_dw_loc_list *);
2931 static bool equal (cached_dw_loc_list *, const_tree);
2934 /* Table of cached location lists. */
2935 static GTY (()) hash_table<dw_loc_list_hasher> *cached_dw_loc_list_table;
2937 /* A pointer to the base of a list of references to DIE's that
2938 are uniquely identified by their tag, presence/absence of
2939 children DIE's, and list of attribute/value pairs. */
2940 static GTY((length ("abbrev_die_table_allocated")))
2941 dw_die_ref *abbrev_die_table;
2943 /* Number of elements currently allocated for abbrev_die_table. */
2944 static GTY(()) unsigned abbrev_die_table_allocated;
2946 /* Number of elements in type_die_table currently in use. */
2947 static GTY(()) unsigned abbrev_die_table_in_use;
2949 /* Size (in elements) of increments by which we may expand the
2950 abbrev_die_table. */
2951 #define ABBREV_DIE_TABLE_INCREMENT 256
2953 /* A global counter for generating labels for line number data. */
2954 static unsigned int line_info_label_num;
2956 /* The current table to which we should emit line number information
2957 for the current function. This will be set up at the beginning of
2958 assembly for the function. */
2959 static dw_line_info_table *cur_line_info_table;
2961 /* The two default tables of line number info. */
2962 static GTY(()) dw_line_info_table *text_section_line_info;
2963 static GTY(()) dw_line_info_table *cold_text_section_line_info;
2965 /* The set of all non-default tables of line number info. */
2966 static GTY(()) vec<dw_line_info_table_p, va_gc> *separate_line_info;
2968 /* A flag to tell pubnames/types export if there is an info section to
2969 refer to. */
2970 static bool info_section_emitted;
2972 /* A pointer to the base of a table that contains a list of publicly
2973 accessible names. */
2974 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
2976 /* A pointer to the base of a table that contains a list of publicly
2977 accessible types. */
2978 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
2980 /* A pointer to the base of a table that contains a list of macro
2981 defines/undefines (and file start/end markers). */
2982 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
2984 /* True if .debug_macinfo or .debug_macros section is going to be
2985 emitted. */
2986 #define have_macinfo \
2987 (debug_info_level >= DINFO_LEVEL_VERBOSE \
2988 && !macinfo_table->is_empty ())
2990 /* Array of dies for which we should generate .debug_ranges info. */
2991 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
2993 /* Number of elements currently allocated for ranges_table. */
2994 static GTY(()) unsigned ranges_table_allocated;
2996 /* Number of elements in ranges_table currently in use. */
2997 static GTY(()) unsigned ranges_table_in_use;
2999 /* Array of pairs of labels referenced in ranges_table. */
3000 static GTY ((length ("ranges_by_label_allocated")))
3001 dw_ranges_by_label_ref ranges_by_label;
3003 /* Number of elements currently allocated for ranges_by_label. */
3004 static GTY(()) unsigned ranges_by_label_allocated;
3006 /* Number of elements in ranges_by_label currently in use. */
3007 static GTY(()) unsigned ranges_by_label_in_use;
3009 /* Size (in elements) of increments by which we may expand the
3010 ranges_table. */
3011 #define RANGES_TABLE_INCREMENT 64
3013 /* Whether we have location lists that need outputting */
3014 static GTY(()) bool have_location_lists;
3016 /* Unique label counter. */
3017 static GTY(()) unsigned int loclabel_num;
3019 /* Unique label counter for point-of-call tables. */
3020 static GTY(()) unsigned int poc_label_num;
3022 /* The last file entry emitted by maybe_emit_file(). */
3023 static GTY(()) struct dwarf_file_data * last_emitted_file;
3025 /* Number of internal labels generated by gen_internal_sym(). */
3026 static GTY(()) int label_num;
3028 /* Cached result of previous call to lookup_filename. */
3029 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
3031 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
3033 /* Instances of generic types for which we need to generate debug
3034 info that describe their generic parameters and arguments. That
3035 generation needs to happen once all types are properly laid out so
3036 we do it at the end of compilation. */
3037 static GTY(()) vec<tree, va_gc> *generic_type_instances;
3039 /* Offset from the "steady-state frame pointer" to the frame base,
3040 within the current function. */
3041 static HOST_WIDE_INT frame_pointer_fb_offset;
3042 static bool frame_pointer_fb_offset_valid;
3044 static vec<dw_die_ref> base_types;
3046 /* Flags to represent a set of attribute classes for attributes that represent
3047 a scalar value (bounds, pointers, ...). */
3048 enum dw_scalar_form
3050 dw_scalar_form_constant = 0x01,
3051 dw_scalar_form_exprloc = 0x02,
3052 dw_scalar_form_reference = 0x04
3055 /* Forward declarations for functions defined in this file. */
3057 static int is_pseudo_reg (const_rtx);
3058 static tree type_main_variant (tree);
3059 static int is_tagged_type (const_tree);
3060 static const char *dwarf_tag_name (unsigned);
3061 static const char *dwarf_attr_name (unsigned);
3062 static const char *dwarf_form_name (unsigned);
3063 static tree decl_ultimate_origin (const_tree);
3064 static tree decl_class_context (tree);
3065 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
3066 static inline enum dw_val_class AT_class (dw_attr_ref);
3067 static inline unsigned int AT_index (dw_attr_ref);
3068 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3069 static inline unsigned AT_flag (dw_attr_ref);
3070 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3071 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
3072 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3073 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
3074 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3075 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3076 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3077 unsigned int, unsigned char *);
3078 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3079 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3080 static inline const char *AT_string (dw_attr_ref);
3081 static enum dwarf_form AT_string_form (dw_attr_ref);
3082 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3083 static void add_AT_specification (dw_die_ref, dw_die_ref);
3084 static inline dw_die_ref AT_ref (dw_attr_ref);
3085 static inline int AT_ref_external (dw_attr_ref);
3086 static inline void set_AT_ref_external (dw_attr_ref, int);
3087 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3088 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3089 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
3090 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3091 dw_loc_list_ref);
3092 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
3093 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3094 static void remove_addr_table_entry (addr_table_entry *);
3095 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3096 static inline rtx AT_addr (dw_attr_ref);
3097 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3098 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3099 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3100 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3101 unsigned HOST_WIDE_INT);
3102 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3103 unsigned long, bool);
3104 static inline const char *AT_lbl (dw_attr_ref);
3105 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
3106 static const char *get_AT_low_pc (dw_die_ref);
3107 static const char *get_AT_hi_pc (dw_die_ref);
3108 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3109 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3110 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3111 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3112 static bool is_cxx (void);
3113 static bool is_fortran (void);
3114 static bool is_ada (void);
3115 static void remove_AT (dw_die_ref, enum dwarf_attribute);
3116 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3117 static void add_child_die (dw_die_ref, dw_die_ref);
3118 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3119 static dw_die_ref lookup_type_die (tree);
3120 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3121 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3122 static void equate_type_number_to_die (tree, dw_die_ref);
3123 static dw_die_ref lookup_decl_die (tree);
3124 static var_loc_list *lookup_decl_loc (const_tree);
3125 static void equate_decl_number_to_die (tree, dw_die_ref);
3126 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3127 static void print_spaces (FILE *);
3128 static void print_die (dw_die_ref, FILE *);
3129 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3130 static dw_die_ref pop_compile_unit (dw_die_ref);
3131 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3132 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
3133 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3134 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3135 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3136 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3137 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
3138 struct md5_ctx *, int *);
3139 struct checksum_attributes;
3140 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3141 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3142 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3143 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3144 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3145 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3146 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
3147 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3148 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3149 static void compute_section_prefix (dw_die_ref);
3150 static int is_type_die (dw_die_ref);
3151 static int is_comdat_die (dw_die_ref);
3152 static int is_symbol_die (dw_die_ref);
3153 static inline bool is_template_instantiation (dw_die_ref);
3154 static void assign_symbol_names (dw_die_ref);
3155 static void break_out_includes (dw_die_ref);
3156 static int is_declaration_die (dw_die_ref);
3157 static int should_move_die_to_comdat (dw_die_ref);
3158 static dw_die_ref clone_as_declaration (dw_die_ref);
3159 static dw_die_ref clone_die (dw_die_ref);
3160 static dw_die_ref clone_tree (dw_die_ref);
3161 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3162 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3163 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3164 static dw_die_ref generate_skeleton (dw_die_ref);
3165 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3166 dw_die_ref,
3167 dw_die_ref);
3168 static void break_out_comdat_types (dw_die_ref);
3169 static void copy_decls_for_unworthy_types (dw_die_ref);
3171 static void add_sibling_attributes (dw_die_ref);
3172 static void output_location_lists (dw_die_ref);
3173 static int constant_size (unsigned HOST_WIDE_INT);
3174 static unsigned long size_of_die (dw_die_ref);
3175 static void calc_die_sizes (dw_die_ref);
3176 static void calc_base_type_die_sizes (void);
3177 static void mark_dies (dw_die_ref);
3178 static void unmark_dies (dw_die_ref);
3179 static void unmark_all_dies (dw_die_ref);
3180 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3181 static unsigned long size_of_aranges (void);
3182 static enum dwarf_form value_format (dw_attr_ref);
3183 static void output_value_format (dw_attr_ref);
3184 static void output_abbrev_section (void);
3185 static void output_die_abbrevs (unsigned long, dw_die_ref);
3186 static void output_die_symbol (dw_die_ref);
3187 static void output_die (dw_die_ref);
3188 static void output_compilation_unit_header (void);
3189 static void output_comp_unit (dw_die_ref, int);
3190 static void output_comdat_type_unit (comdat_type_node *);
3191 static const char *dwarf2_name (tree, int);
3192 static void add_pubname (tree, dw_die_ref);
3193 static void add_enumerator_pubname (const char *, dw_die_ref);
3194 static void add_pubname_string (const char *, dw_die_ref);
3195 static void add_pubtype (tree, dw_die_ref);
3196 static void output_pubnames (vec<pubname_entry, va_gc> *);
3197 static void output_aranges (unsigned long);
3198 static unsigned int add_ranges_num (int);
3199 static unsigned int add_ranges (const_tree);
3200 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3201 bool *, bool);
3202 static void output_ranges (void);
3203 static dw_line_info_table *new_line_info_table (void);
3204 static void output_line_info (bool);
3205 static void output_file_names (void);
3206 static dw_die_ref base_type_die (tree);
3207 static int is_base_type (tree);
3208 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
3209 static int decl_quals (const_tree);
3210 static dw_die_ref modified_type_die (tree, int, dw_die_ref);
3211 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3212 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3213 static int type_is_enum (const_tree);
3214 static unsigned int dbx_reg_number (const_rtx);
3215 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3216 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3217 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3218 enum var_init_status);
3219 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3220 enum var_init_status);
3221 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3222 enum var_init_status);
3223 static int is_based_loc (const_rtx);
3224 static bool resolve_one_addr (rtx *);
3225 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3226 enum var_init_status);
3227 static dw_loc_descr_ref loc_descriptor (rtx, machine_mode mode,
3228 enum var_init_status);
3229 struct loc_descr_context;
3230 static dw_loc_list_ref loc_list_from_tree (tree, int,
3231 const struct loc_descr_context *);
3232 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int,
3233 const struct loc_descr_context *);
3234 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3235 static tree field_type (const_tree);
3236 static unsigned int simple_type_align_in_bits (const_tree);
3237 static unsigned int simple_decl_align_in_bits (const_tree);
3238 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3239 static HOST_WIDE_INT field_byte_offset (const_tree);
3240 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3241 dw_loc_list_ref);
3242 static void add_data_member_location_attribute (dw_die_ref, tree);
3243 static bool add_const_value_attribute (dw_die_ref, rtx);
3244 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3245 static void insert_wide_int (const wide_int &, unsigned char *, int);
3246 static void insert_float (const_rtx, unsigned char *);
3247 static rtx rtl_for_decl_location (tree);
3248 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool,
3249 enum dwarf_attribute);
3250 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3251 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3252 static void add_name_attribute (dw_die_ref, const char *);
3253 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3254 static void add_comp_dir_attribute (dw_die_ref);
3255 static void add_scalar_info (dw_die_ref, enum dwarf_attribute, tree, int,
3256 const struct loc_descr_context *);
3257 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree,
3258 const struct loc_descr_context *);
3259 static void add_subscript_info (dw_die_ref, tree, bool);
3260 static void add_byte_size_attribute (dw_die_ref, tree);
3261 static void add_bit_offset_attribute (dw_die_ref, tree);
3262 static void add_bit_size_attribute (dw_die_ref, tree);
3263 static void add_prototyped_attribute (dw_die_ref, tree);
3264 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3265 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3266 static void add_src_coords_attributes (dw_die_ref, tree);
3267 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3268 static void push_decl_scope (tree);
3269 static void pop_decl_scope (void);
3270 static dw_die_ref scope_die_for (tree, dw_die_ref);
3271 static inline int local_scope_p (dw_die_ref);
3272 static inline int class_scope_p (dw_die_ref);
3273 static inline int class_or_namespace_scope_p (dw_die_ref);
3274 static void add_type_attribute (dw_die_ref, tree, int, dw_die_ref);
3275 static void add_calling_convention_attribute (dw_die_ref, tree);
3276 static const char *type_tag (const_tree);
3277 static tree member_declared_type (const_tree);
3278 #if 0
3279 static const char *decl_start_label (tree);
3280 #endif
3281 static void gen_array_type_die (tree, dw_die_ref);
3282 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3283 #if 0
3284 static void gen_entry_point_die (tree, dw_die_ref);
3285 #endif
3286 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3287 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3288 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3289 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3290 static void gen_formal_types_die (tree, dw_die_ref);
3291 static void gen_subprogram_die (tree, dw_die_ref);
3292 static void gen_variable_die (tree, tree, dw_die_ref);
3293 static void gen_const_die (tree, dw_die_ref);
3294 static void gen_label_die (tree, dw_die_ref);
3295 static void gen_lexical_block_die (tree, dw_die_ref);
3296 static void gen_inlined_subroutine_die (tree, dw_die_ref);
3297 static void gen_field_die (tree, dw_die_ref);
3298 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3299 static dw_die_ref gen_compile_unit_die (const char *);
3300 static void gen_inheritance_die (tree, tree, dw_die_ref);
3301 static void gen_member_die (tree, dw_die_ref);
3302 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3303 enum debug_info_usage);
3304 static void gen_subroutine_type_die (tree, dw_die_ref);
3305 static void gen_typedef_die (tree, dw_die_ref);
3306 static void gen_type_die (tree, dw_die_ref);
3307 static void gen_block_die (tree, dw_die_ref);
3308 static void decls_for_scope (tree, dw_die_ref);
3309 static inline int is_redundant_typedef (const_tree);
3310 static bool is_naming_typedef_decl (const_tree);
3311 static inline dw_die_ref get_context_die (tree);
3312 static void gen_namespace_die (tree, dw_die_ref);
3313 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3314 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
3315 static dw_die_ref force_decl_die (tree);
3316 static dw_die_ref force_type_die (tree);
3317 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3318 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3319 static struct dwarf_file_data * lookup_filename (const char *);
3320 static void retry_incomplete_types (void);
3321 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3322 static void gen_generic_params_dies (tree);
3323 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3324 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3325 static void splice_child_die (dw_die_ref, dw_die_ref);
3326 static int file_info_cmp (const void *, const void *);
3327 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3328 const char *, const char *);
3329 static void output_loc_list (dw_loc_list_ref);
3330 static char *gen_internal_sym (const char *);
3331 static bool want_pubnames (void);
3333 static void prune_unmark_dies (dw_die_ref);
3334 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3335 static void prune_unused_types_mark (dw_die_ref, int);
3336 static void prune_unused_types_walk (dw_die_ref);
3337 static void prune_unused_types_walk_attribs (dw_die_ref);
3338 static void prune_unused_types_prune (dw_die_ref);
3339 static void prune_unused_types (void);
3340 static int maybe_emit_file (struct dwarf_file_data *fd);
3341 static inline const char *AT_vms_delta1 (dw_attr_ref);
3342 static inline const char *AT_vms_delta2 (dw_attr_ref);
3343 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3344 const char *, const char *);
3345 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3346 static void gen_remaining_tmpl_value_param_die_attribute (void);
3347 static bool generic_type_p (tree);
3348 static void schedule_generic_params_dies_gen (tree t);
3349 static void gen_scheduled_generic_parms_dies (void);
3351 static const char *comp_dir_string (void);
3353 static void hash_loc_operands (dw_loc_descr_ref, inchash::hash &);
3355 /* enum for tracking thread-local variables whose address is really an offset
3356 relative to the TLS pointer, which will need link-time relocation, but will
3357 not need relocation by the DWARF consumer. */
3359 enum dtprel_bool
3361 dtprel_false = 0,
3362 dtprel_true = 1
3365 /* Return the operator to use for an address of a variable. For dtprel_true, we
3366 use DW_OP_const*. For regular variables, which need both link-time
3367 relocation and consumer-level relocation (e.g., to account for shared objects
3368 loaded at a random address), we use DW_OP_addr*. */
3370 static inline enum dwarf_location_atom
3371 dw_addr_op (enum dtprel_bool dtprel)
3373 if (dtprel == dtprel_true)
3374 return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3375 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3376 else
3377 return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3380 /* Return a pointer to a newly allocated address location description. If
3381 dwarf_split_debug_info is true, then record the address with the appropriate
3382 relocation. */
3383 static inline dw_loc_descr_ref
3384 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3386 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3388 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3389 ref->dw_loc_oprnd1.v.val_addr = addr;
3390 ref->dtprel = dtprel;
3391 if (dwarf_split_debug_info)
3392 ref->dw_loc_oprnd1.val_entry
3393 = add_addr_table_entry (addr,
3394 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3395 else
3396 ref->dw_loc_oprnd1.val_entry = NULL;
3398 return ref;
3401 /* Section names used to hold DWARF debugging information. */
3403 #ifndef DEBUG_INFO_SECTION
3404 #define DEBUG_INFO_SECTION ".debug_info"
3405 #endif
3406 #ifndef DEBUG_DWO_INFO_SECTION
3407 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3408 #endif
3409 #ifndef DEBUG_ABBREV_SECTION
3410 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3411 #endif
3412 #ifndef DEBUG_DWO_ABBREV_SECTION
3413 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3414 #endif
3415 #ifndef DEBUG_ARANGES_SECTION
3416 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3417 #endif
3418 #ifndef DEBUG_ADDR_SECTION
3419 #define DEBUG_ADDR_SECTION ".debug_addr"
3420 #endif
3421 #ifndef DEBUG_NORM_MACINFO_SECTION
3422 #define DEBUG_NORM_MACINFO_SECTION ".debug_macinfo"
3423 #endif
3424 #ifndef DEBUG_DWO_MACINFO_SECTION
3425 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
3426 #endif
3427 #ifndef DEBUG_MACINFO_SECTION
3428 #define DEBUG_MACINFO_SECTION \
3429 (!dwarf_split_debug_info \
3430 ? (DEBUG_NORM_MACINFO_SECTION) : (DEBUG_DWO_MACINFO_SECTION))
3431 #endif
3432 #ifndef DEBUG_NORM_MACRO_SECTION
3433 #define DEBUG_NORM_MACRO_SECTION ".debug_macro"
3434 #endif
3435 #ifndef DEBUG_DWO_MACRO_SECTION
3436 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
3437 #endif
3438 #ifndef DEBUG_MACRO_SECTION
3439 #define DEBUG_MACRO_SECTION \
3440 (!dwarf_split_debug_info \
3441 ? (DEBUG_NORM_MACRO_SECTION) : (DEBUG_DWO_MACRO_SECTION))
3442 #endif
3443 #ifndef DEBUG_LINE_SECTION
3444 #define DEBUG_LINE_SECTION ".debug_line"
3445 #endif
3446 #ifndef DEBUG_DWO_LINE_SECTION
3447 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3448 #endif
3449 #ifndef DEBUG_LOC_SECTION
3450 #define DEBUG_LOC_SECTION ".debug_loc"
3451 #endif
3452 #ifndef DEBUG_DWO_LOC_SECTION
3453 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
3454 #endif
3455 #ifndef DEBUG_PUBNAMES_SECTION
3456 #define DEBUG_PUBNAMES_SECTION \
3457 ((debug_generate_pub_sections == 2) \
3458 ? ".debug_gnu_pubnames" : ".debug_pubnames")
3459 #endif
3460 #ifndef DEBUG_PUBTYPES_SECTION
3461 #define DEBUG_PUBTYPES_SECTION \
3462 ((debug_generate_pub_sections == 2) \
3463 ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
3464 #endif
3465 #define DEBUG_NORM_STR_OFFSETS_SECTION ".debug_str_offsets"
3466 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3467 #ifndef DEBUG_STR_OFFSETS_SECTION
3468 #define DEBUG_STR_OFFSETS_SECTION \
3469 (!dwarf_split_debug_info \
3470 ? (DEBUG_NORM_STR_OFFSETS_SECTION) : (DEBUG_DWO_STR_OFFSETS_SECTION))
3471 #endif
3472 #ifndef DEBUG_STR_DWO_SECTION
3473 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
3474 #endif
3475 #ifndef DEBUG_STR_SECTION
3476 #define DEBUG_STR_SECTION ".debug_str"
3477 #endif
3478 #ifndef DEBUG_RANGES_SECTION
3479 #define DEBUG_RANGES_SECTION ".debug_ranges"
3480 #endif
3482 /* Standard ELF section names for compiled code and data. */
3483 #ifndef TEXT_SECTION_NAME
3484 #define TEXT_SECTION_NAME ".text"
3485 #endif
3487 /* Section flags for .debug_macinfo/.debug_macro section. */
3488 #define DEBUG_MACRO_SECTION_FLAGS \
3489 (dwarf_split_debug_info ? SECTION_DEBUG | SECTION_EXCLUDE : SECTION_DEBUG)
3491 /* Section flags for .debug_str section. */
3492 #define DEBUG_STR_SECTION_FLAGS \
3493 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
3494 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3495 : SECTION_DEBUG)
3497 /* Section flags for .debug_str.dwo section. */
3498 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
3500 /* Labels we insert at beginning sections we can reference instead of
3501 the section names themselves. */
3503 #ifndef TEXT_SECTION_LABEL
3504 #define TEXT_SECTION_LABEL "Ltext"
3505 #endif
3506 #ifndef COLD_TEXT_SECTION_LABEL
3507 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
3508 #endif
3509 #ifndef DEBUG_LINE_SECTION_LABEL
3510 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3511 #endif
3512 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3513 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
3514 #endif
3515 #ifndef DEBUG_INFO_SECTION_LABEL
3516 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3517 #endif
3518 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3519 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
3520 #endif
3521 #ifndef DEBUG_ABBREV_SECTION_LABEL
3522 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3523 #endif
3524 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3525 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3526 #endif
3527 #ifndef DEBUG_ADDR_SECTION_LABEL
3528 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
3529 #endif
3530 #ifndef DEBUG_LOC_SECTION_LABEL
3531 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3532 #endif
3533 #ifndef DEBUG_RANGES_SECTION_LABEL
3534 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3535 #endif
3536 #ifndef DEBUG_MACINFO_SECTION_LABEL
3537 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3538 #endif
3539 #ifndef DEBUG_MACRO_SECTION_LABEL
3540 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
3541 #endif
3542 #define SKELETON_COMP_DIE_ABBREV 1
3543 #define SKELETON_TYPE_DIE_ABBREV 2
3545 /* Definitions of defaults for formats and names of various special
3546 (artificial) labels which may be generated within this file (when the -g
3547 options is used and DWARF2_DEBUGGING_INFO is in effect.
3548 If necessary, these may be overridden from within the tm.h file, but
3549 typically, overriding these defaults is unnecessary. */
3551 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3552 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3553 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3554 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3555 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3556 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3557 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3558 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3559 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3560 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3561 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3562 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3563 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3564 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3566 #ifndef TEXT_END_LABEL
3567 #define TEXT_END_LABEL "Letext"
3568 #endif
3569 #ifndef COLD_END_LABEL
3570 #define COLD_END_LABEL "Letext_cold"
3571 #endif
3572 #ifndef BLOCK_BEGIN_LABEL
3573 #define BLOCK_BEGIN_LABEL "LBB"
3574 #endif
3575 #ifndef BLOCK_END_LABEL
3576 #define BLOCK_END_LABEL "LBE"
3577 #endif
3578 #ifndef LINE_CODE_LABEL
3579 #define LINE_CODE_LABEL "LM"
3580 #endif
3583 /* Return the root of the DIE's built for the current compilation unit. */
3584 static dw_die_ref
3585 comp_unit_die (void)
3587 if (!single_comp_unit_die)
3588 single_comp_unit_die = gen_compile_unit_die (NULL);
3589 return single_comp_unit_die;
3592 /* We allow a language front-end to designate a function that is to be
3593 called to "demangle" any name before it is put into a DIE. */
3595 static const char *(*demangle_name_func) (const char *);
3597 void
3598 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3600 demangle_name_func = func;
3603 /* Test if rtl node points to a pseudo register. */
3605 static inline int
3606 is_pseudo_reg (const_rtx rtl)
3608 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3609 || (GET_CODE (rtl) == SUBREG
3610 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3613 /* Return a reference to a type, with its const and volatile qualifiers
3614 removed. */
3616 static inline tree
3617 type_main_variant (tree type)
3619 type = TYPE_MAIN_VARIANT (type);
3621 /* ??? There really should be only one main variant among any group of
3622 variants of a given type (and all of the MAIN_VARIANT values for all
3623 members of the group should point to that one type) but sometimes the C
3624 front-end messes this up for array types, so we work around that bug
3625 here. */
3626 if (TREE_CODE (type) == ARRAY_TYPE)
3627 while (type != TYPE_MAIN_VARIANT (type))
3628 type = TYPE_MAIN_VARIANT (type);
3630 return type;
3633 /* Return nonzero if the given type node represents a tagged type. */
3635 static inline int
3636 is_tagged_type (const_tree type)
3638 enum tree_code code = TREE_CODE (type);
3640 return (code == RECORD_TYPE || code == UNION_TYPE
3641 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3644 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
3646 static void
3647 get_ref_die_offset_label (char *label, dw_die_ref ref)
3649 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3652 /* Return die_offset of a DIE reference to a base type. */
3654 static unsigned long int
3655 get_base_type_offset (dw_die_ref ref)
3657 if (ref->die_offset)
3658 return ref->die_offset;
3659 if (comp_unit_die ()->die_abbrev)
3661 calc_base_type_die_sizes ();
3662 gcc_assert (ref->die_offset);
3664 return ref->die_offset;
3667 /* Return die_offset of a DIE reference other than base type. */
3669 static unsigned long int
3670 get_ref_die_offset (dw_die_ref ref)
3672 gcc_assert (ref->die_offset);
3673 return ref->die_offset;
3676 /* Convert a DIE tag into its string name. */
3678 static const char *
3679 dwarf_tag_name (unsigned int tag)
3681 const char *name = get_DW_TAG_name (tag);
3683 if (name != NULL)
3684 return name;
3686 return "DW_TAG_<unknown>";
3689 /* Convert a DWARF attribute code into its string name. */
3691 static const char *
3692 dwarf_attr_name (unsigned int attr)
3694 const char *name;
3696 switch (attr)
3698 #if VMS_DEBUGGING_INFO
3699 case DW_AT_HP_prologue:
3700 return "DW_AT_HP_prologue";
3701 #else
3702 case DW_AT_MIPS_loop_unroll_factor:
3703 return "DW_AT_MIPS_loop_unroll_factor";
3704 #endif
3706 #if VMS_DEBUGGING_INFO
3707 case DW_AT_HP_epilogue:
3708 return "DW_AT_HP_epilogue";
3709 #else
3710 case DW_AT_MIPS_stride:
3711 return "DW_AT_MIPS_stride";
3712 #endif
3715 name = get_DW_AT_name (attr);
3717 if (name != NULL)
3718 return name;
3720 return "DW_AT_<unknown>";
3723 /* Convert a DWARF value form code into its string name. */
3725 static const char *
3726 dwarf_form_name (unsigned int form)
3728 const char *name = get_DW_FORM_name (form);
3730 if (name != NULL)
3731 return name;
3733 return "DW_FORM_<unknown>";
3736 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
3737 instance of an inlined instance of a decl which is local to an inline
3738 function, so we have to trace all of the way back through the origin chain
3739 to find out what sort of node actually served as the original seed for the
3740 given block. */
3742 static tree
3743 decl_ultimate_origin (const_tree decl)
3745 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
3746 return NULL_TREE;
3748 /* DECL_ABSTRACT_ORIGIN can point to itself; ignore that if
3749 we're trying to output the abstract instance of this function. */
3750 if (DECL_ABSTRACT_P (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
3751 return NULL_TREE;
3753 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
3754 most distant ancestor, this should never happen. */
3755 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
3757 return DECL_ABSTRACT_ORIGIN (decl);
3760 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
3761 of a virtual function may refer to a base class, so we check the 'this'
3762 parameter. */
3764 static tree
3765 decl_class_context (tree decl)
3767 tree context = NULL_TREE;
3769 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
3770 context = DECL_CONTEXT (decl);
3771 else
3772 context = TYPE_MAIN_VARIANT
3773 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
3775 if (context && !TYPE_P (context))
3776 context = NULL_TREE;
3778 return context;
3781 /* Add an attribute/value pair to a DIE. */
3783 static inline void
3784 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
3786 /* Maybe this should be an assert? */
3787 if (die == NULL)
3788 return;
3790 vec_safe_reserve (die->die_attr, 1);
3791 vec_safe_push (die->die_attr, *attr);
3794 static inline enum dw_val_class
3795 AT_class (dw_attr_ref a)
3797 return a->dw_attr_val.val_class;
3800 /* Return the index for any attribute that will be referenced with a
3801 DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index. String indices
3802 are stored in dw_attr_val.v.val_str for reference counting
3803 pruning. */
3805 static inline unsigned int
3806 AT_index (dw_attr_ref a)
3808 if (AT_class (a) == dw_val_class_str)
3809 return a->dw_attr_val.v.val_str->index;
3810 else if (a->dw_attr_val.val_entry != NULL)
3811 return a->dw_attr_val.val_entry->index;
3812 return NOT_INDEXED;
3815 /* Add a flag value attribute to a DIE. */
3817 static inline void
3818 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
3820 dw_attr_node attr;
3822 attr.dw_attr = attr_kind;
3823 attr.dw_attr_val.val_class = dw_val_class_flag;
3824 attr.dw_attr_val.val_entry = NULL;
3825 attr.dw_attr_val.v.val_flag = flag;
3826 add_dwarf_attr (die, &attr);
3829 static inline unsigned
3830 AT_flag (dw_attr_ref a)
3832 gcc_assert (a && AT_class (a) == dw_val_class_flag);
3833 return a->dw_attr_val.v.val_flag;
3836 /* Add a signed integer attribute value to a DIE. */
3838 static inline void
3839 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
3841 dw_attr_node attr;
3843 attr.dw_attr = attr_kind;
3844 attr.dw_attr_val.val_class = dw_val_class_const;
3845 attr.dw_attr_val.val_entry = NULL;
3846 attr.dw_attr_val.v.val_int = int_val;
3847 add_dwarf_attr (die, &attr);
3850 static inline HOST_WIDE_INT
3851 AT_int (dw_attr_ref a)
3853 gcc_assert (a && AT_class (a) == dw_val_class_const);
3854 return a->dw_attr_val.v.val_int;
3857 /* Add an unsigned integer attribute value to a DIE. */
3859 static inline void
3860 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
3861 unsigned HOST_WIDE_INT unsigned_val)
3863 dw_attr_node attr;
3865 attr.dw_attr = attr_kind;
3866 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
3867 attr.dw_attr_val.val_entry = NULL;
3868 attr.dw_attr_val.v.val_unsigned = unsigned_val;
3869 add_dwarf_attr (die, &attr);
3872 static inline unsigned HOST_WIDE_INT
3873 AT_unsigned (dw_attr_ref a)
3875 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
3876 return a->dw_attr_val.v.val_unsigned;
3879 /* Add an unsigned wide integer attribute value to a DIE. */
3881 static inline void
3882 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
3883 const wide_int& w)
3885 dw_attr_node attr;
3887 attr.dw_attr = attr_kind;
3888 attr.dw_attr_val.val_class = dw_val_class_wide_int;
3889 attr.dw_attr_val.v.val_wide = ggc_alloc<wide_int> ();
3890 *attr.dw_attr_val.v.val_wide = w;
3891 add_dwarf_attr (die, &attr);
3894 /* Add an unsigned double integer attribute value to a DIE. */
3896 static inline void
3897 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
3898 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
3900 dw_attr_node attr;
3902 attr.dw_attr = attr_kind;
3903 attr.dw_attr_val.val_class = dw_val_class_const_double;
3904 attr.dw_attr_val.val_entry = NULL;
3905 attr.dw_attr_val.v.val_double.high = high;
3906 attr.dw_attr_val.v.val_double.low = low;
3907 add_dwarf_attr (die, &attr);
3910 /* Add a floating point attribute value to a DIE and return it. */
3912 static inline void
3913 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
3914 unsigned int length, unsigned int elt_size, unsigned char *array)
3916 dw_attr_node attr;
3918 attr.dw_attr = attr_kind;
3919 attr.dw_attr_val.val_class = dw_val_class_vec;
3920 attr.dw_attr_val.val_entry = NULL;
3921 attr.dw_attr_val.v.val_vec.length = length;
3922 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
3923 attr.dw_attr_val.v.val_vec.array = array;
3924 add_dwarf_attr (die, &attr);
3927 /* Add an 8-byte data attribute value to a DIE. */
3929 static inline void
3930 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
3931 unsigned char data8[8])
3933 dw_attr_node attr;
3935 attr.dw_attr = attr_kind;
3936 attr.dw_attr_val.val_class = dw_val_class_data8;
3937 attr.dw_attr_val.val_entry = NULL;
3938 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
3939 add_dwarf_attr (die, &attr);
3942 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
3943 dwarf_split_debug_info, address attributes in dies destined for the
3944 final executable have force_direct set to avoid using indexed
3945 references. */
3947 static inline void
3948 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
3949 bool force_direct)
3951 dw_attr_node attr;
3952 char * lbl_id;
3954 lbl_id = xstrdup (lbl_low);
3955 attr.dw_attr = DW_AT_low_pc;
3956 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3957 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3958 if (dwarf_split_debug_info && !force_direct)
3959 attr.dw_attr_val.val_entry
3960 = add_addr_table_entry (lbl_id, ate_kind_label);
3961 else
3962 attr.dw_attr_val.val_entry = NULL;
3963 add_dwarf_attr (die, &attr);
3965 attr.dw_attr = DW_AT_high_pc;
3966 if (dwarf_version < 4)
3967 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3968 else
3969 attr.dw_attr_val.val_class = dw_val_class_high_pc;
3970 lbl_id = xstrdup (lbl_high);
3971 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3972 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
3973 && dwarf_split_debug_info && !force_direct)
3974 attr.dw_attr_val.val_entry
3975 = add_addr_table_entry (lbl_id, ate_kind_label);
3976 else
3977 attr.dw_attr_val.val_entry = NULL;
3978 add_dwarf_attr (die, &attr);
3981 /* Hash and equality functions for debug_str_hash. */
3983 hashval_t
3984 indirect_string_hasher::hash (indirect_string_node *x)
3986 return htab_hash_string (x->str);
3989 bool
3990 indirect_string_hasher::equal (indirect_string_node *x1, const char *x2)
3992 return strcmp (x1->str, x2) == 0;
3995 /* Add STR to the given string hash table. */
3997 static struct indirect_string_node *
3998 find_AT_string_in_table (const char *str,
3999 hash_table<indirect_string_hasher> *table)
4001 struct indirect_string_node *node;
4003 indirect_string_node **slot
4004 = table->find_slot_with_hash (str, htab_hash_string (str), INSERT);
4005 if (*slot == NULL)
4007 node = ggc_cleared_alloc<indirect_string_node> ();
4008 node->str = ggc_strdup (str);
4009 *slot = node;
4011 else
4012 node = *slot;
4014 node->refcount++;
4015 return node;
4018 /* Add STR to the indirect string hash table. */
4020 static struct indirect_string_node *
4021 find_AT_string (const char *str)
4023 if (! debug_str_hash)
4024 debug_str_hash = hash_table<indirect_string_hasher>::create_ggc (10);
4026 return find_AT_string_in_table (str, debug_str_hash);
4029 /* Add a string attribute value to a DIE. */
4031 static inline void
4032 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4034 dw_attr_node attr;
4035 struct indirect_string_node *node;
4037 node = find_AT_string (str);
4039 attr.dw_attr = attr_kind;
4040 attr.dw_attr_val.val_class = dw_val_class_str;
4041 attr.dw_attr_val.val_entry = NULL;
4042 attr.dw_attr_val.v.val_str = node;
4043 add_dwarf_attr (die, &attr);
4046 static inline const char *
4047 AT_string (dw_attr_ref a)
4049 gcc_assert (a && AT_class (a) == dw_val_class_str);
4050 return a->dw_attr_val.v.val_str->str;
4053 /* Call this function directly to bypass AT_string_form's logic to put
4054 the string inline in the die. */
4056 static void
4057 set_indirect_string (struct indirect_string_node *node)
4059 char label[32];
4060 /* Already indirect is a no op. */
4061 if (node->form == DW_FORM_strp || node->form == DW_FORM_GNU_str_index)
4063 gcc_assert (node->label);
4064 return;
4066 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4067 ++dw2_string_counter;
4068 node->label = xstrdup (label);
4070 if (!dwarf_split_debug_info)
4072 node->form = DW_FORM_strp;
4073 node->index = NOT_INDEXED;
4075 else
4077 node->form = DW_FORM_GNU_str_index;
4078 node->index = NO_INDEX_ASSIGNED;
4082 /* Find out whether a string should be output inline in DIE
4083 or out-of-line in .debug_str section. */
4085 static enum dwarf_form
4086 find_string_form (struct indirect_string_node *node)
4088 unsigned int len;
4090 if (node->form)
4091 return node->form;
4093 len = strlen (node->str) + 1;
4095 /* If the string is shorter or equal to the size of the reference, it is
4096 always better to put it inline. */
4097 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4098 return node->form = DW_FORM_string;
4100 /* If we cannot expect the linker to merge strings in .debug_str
4101 section, only put it into .debug_str if it is worth even in this
4102 single module. */
4103 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4104 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4105 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
4106 return node->form = DW_FORM_string;
4108 set_indirect_string (node);
4110 return node->form;
4113 /* Find out whether the string referenced from the attribute should be
4114 output inline in DIE or out-of-line in .debug_str section. */
4116 static enum dwarf_form
4117 AT_string_form (dw_attr_ref a)
4119 gcc_assert (a && AT_class (a) == dw_val_class_str);
4120 return find_string_form (a->dw_attr_val.v.val_str);
4123 /* Add a DIE reference attribute value to a DIE. */
4125 static inline void
4126 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4128 dw_attr_node attr;
4130 #ifdef ENABLE_CHECKING
4131 gcc_assert (targ_die != NULL);
4132 #else
4133 /* With LTO we can end up trying to reference something we didn't create
4134 a DIE for. Avoid crashing later on a NULL referenced DIE. */
4135 if (targ_die == NULL)
4136 return;
4137 #endif
4139 attr.dw_attr = attr_kind;
4140 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4141 attr.dw_attr_val.val_entry = NULL;
4142 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4143 attr.dw_attr_val.v.val_die_ref.external = 0;
4144 add_dwarf_attr (die, &attr);
4147 /* Change DIE reference REF to point to NEW_DIE instead. */
4149 static inline void
4150 change_AT_die_ref (dw_attr_ref ref, dw_die_ref new_die)
4152 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4153 ref->dw_attr_val.v.val_die_ref.die = new_die;
4154 ref->dw_attr_val.v.val_die_ref.external = 0;
4157 /* Add an AT_specification attribute to a DIE, and also make the back
4158 pointer from the specification to the definition. */
4160 static inline void
4161 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4163 add_AT_die_ref (die, DW_AT_specification, targ_die);
4164 gcc_assert (!targ_die->die_definition);
4165 targ_die->die_definition = die;
4168 static inline dw_die_ref
4169 AT_ref (dw_attr_ref a)
4171 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4172 return a->dw_attr_val.v.val_die_ref.die;
4175 static inline int
4176 AT_ref_external (dw_attr_ref a)
4178 if (a && AT_class (a) == dw_val_class_die_ref)
4179 return a->dw_attr_val.v.val_die_ref.external;
4181 return 0;
4184 static inline void
4185 set_AT_ref_external (dw_attr_ref a, int i)
4187 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4188 a->dw_attr_val.v.val_die_ref.external = i;
4191 /* Add an FDE reference attribute value to a DIE. */
4193 static inline void
4194 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4196 dw_attr_node attr;
4198 attr.dw_attr = attr_kind;
4199 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4200 attr.dw_attr_val.val_entry = NULL;
4201 attr.dw_attr_val.v.val_fde_index = targ_fde;
4202 add_dwarf_attr (die, &attr);
4205 /* Add a location description attribute value to a DIE. */
4207 static inline void
4208 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4210 dw_attr_node attr;
4212 attr.dw_attr = attr_kind;
4213 attr.dw_attr_val.val_class = dw_val_class_loc;
4214 attr.dw_attr_val.val_entry = NULL;
4215 attr.dw_attr_val.v.val_loc = loc;
4216 add_dwarf_attr (die, &attr);
4219 static inline dw_loc_descr_ref
4220 AT_loc (dw_attr_ref a)
4222 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4223 return a->dw_attr_val.v.val_loc;
4226 static inline void
4227 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4229 dw_attr_node attr;
4231 attr.dw_attr = attr_kind;
4232 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4233 attr.dw_attr_val.val_entry = NULL;
4234 attr.dw_attr_val.v.val_loc_list = loc_list;
4235 add_dwarf_attr (die, &attr);
4236 have_location_lists = true;
4239 static inline dw_loc_list_ref
4240 AT_loc_list (dw_attr_ref a)
4242 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4243 return a->dw_attr_val.v.val_loc_list;
4246 static inline dw_loc_list_ref *
4247 AT_loc_list_ptr (dw_attr_ref a)
4249 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4250 return &a->dw_attr_val.v.val_loc_list;
4253 struct addr_hasher : ggc_hasher<addr_table_entry *>
4255 static hashval_t hash (addr_table_entry *);
4256 static bool equal (addr_table_entry *, addr_table_entry *);
4259 /* Table of entries into the .debug_addr section. */
4261 static GTY (()) hash_table<addr_hasher> *addr_index_table;
4263 /* Hash an address_table_entry. */
4265 hashval_t
4266 addr_hasher::hash (addr_table_entry *a)
4268 inchash::hash hstate;
4269 switch (a->kind)
4271 case ate_kind_rtx:
4272 hstate.add_int (0);
4273 break;
4274 case ate_kind_rtx_dtprel:
4275 hstate.add_int (1);
4276 break;
4277 case ate_kind_label:
4278 return htab_hash_string (a->addr.label);
4279 default:
4280 gcc_unreachable ();
4282 inchash::add_rtx (a->addr.rtl, hstate);
4283 return hstate.end ();
4286 /* Determine equality for two address_table_entries. */
4288 bool
4289 addr_hasher::equal (addr_table_entry *a1, addr_table_entry *a2)
4291 if (a1->kind != a2->kind)
4292 return 0;
4293 switch (a1->kind)
4295 case ate_kind_rtx:
4296 case ate_kind_rtx_dtprel:
4297 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4298 case ate_kind_label:
4299 return strcmp (a1->addr.label, a2->addr.label) == 0;
4300 default:
4301 gcc_unreachable ();
4305 /* Initialize an addr_table_entry. */
4307 void
4308 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4310 e->kind = kind;
4311 switch (kind)
4313 case ate_kind_rtx:
4314 case ate_kind_rtx_dtprel:
4315 e->addr.rtl = (rtx) addr;
4316 break;
4317 case ate_kind_label:
4318 e->addr.label = (char *) addr;
4319 break;
4321 e->refcount = 0;
4322 e->index = NO_INDEX_ASSIGNED;
4325 /* Add attr to the address table entry to the table. Defer setting an
4326 index until output time. */
4328 static addr_table_entry *
4329 add_addr_table_entry (void *addr, enum ate_kind kind)
4331 addr_table_entry *node;
4332 addr_table_entry finder;
4334 gcc_assert (dwarf_split_debug_info);
4335 if (! addr_index_table)
4336 addr_index_table = hash_table<addr_hasher>::create_ggc (10);
4337 init_addr_table_entry (&finder, kind, addr);
4338 addr_table_entry **slot = addr_index_table->find_slot (&finder, INSERT);
4340 if (*slot == HTAB_EMPTY_ENTRY)
4342 node = ggc_cleared_alloc<addr_table_entry> ();
4343 init_addr_table_entry (node, kind, addr);
4344 *slot = node;
4346 else
4347 node = *slot;
4349 node->refcount++;
4350 return node;
4353 /* Remove an entry from the addr table by decrementing its refcount.
4354 Strictly, decrementing the refcount would be enough, but the
4355 assertion that the entry is actually in the table has found
4356 bugs. */
4358 static void
4359 remove_addr_table_entry (addr_table_entry *entry)
4361 gcc_assert (dwarf_split_debug_info && addr_index_table);
4362 /* After an index is assigned, the table is frozen. */
4363 gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
4364 entry->refcount--;
4367 /* Given a location list, remove all addresses it refers to from the
4368 address_table. */
4370 static void
4371 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4373 for (; descr; descr = descr->dw_loc_next)
4374 if (descr->dw_loc_oprnd1.val_entry != NULL)
4376 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4377 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4381 /* A helper function for dwarf2out_finish called through
4382 htab_traverse. Assign an addr_table_entry its index. All entries
4383 must be collected into the table when this function is called,
4384 because the indexing code relies on htab_traverse to traverse nodes
4385 in the same order for each run. */
4388 index_addr_table_entry (addr_table_entry **h, unsigned int *index)
4390 addr_table_entry *node = *h;
4392 /* Don't index unreferenced nodes. */
4393 if (node->refcount == 0)
4394 return 1;
4396 gcc_assert (node->index == NO_INDEX_ASSIGNED);
4397 node->index = *index;
4398 *index += 1;
4400 return 1;
4403 /* Add an address constant attribute value to a DIE. When using
4404 dwarf_split_debug_info, address attributes in dies destined for the
4405 final executable should be direct references--setting the parameter
4406 force_direct ensures this behavior. */
4408 static inline void
4409 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4410 bool force_direct)
4412 dw_attr_node attr;
4414 attr.dw_attr = attr_kind;
4415 attr.dw_attr_val.val_class = dw_val_class_addr;
4416 attr.dw_attr_val.v.val_addr = addr;
4417 if (dwarf_split_debug_info && !force_direct)
4418 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4419 else
4420 attr.dw_attr_val.val_entry = NULL;
4421 add_dwarf_attr (die, &attr);
4424 /* Get the RTX from to an address DIE attribute. */
4426 static inline rtx
4427 AT_addr (dw_attr_ref a)
4429 gcc_assert (a && AT_class (a) == dw_val_class_addr);
4430 return a->dw_attr_val.v.val_addr;
4433 /* Add a file attribute value to a DIE. */
4435 static inline void
4436 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4437 struct dwarf_file_data *fd)
4439 dw_attr_node attr;
4441 attr.dw_attr = attr_kind;
4442 attr.dw_attr_val.val_class = dw_val_class_file;
4443 attr.dw_attr_val.val_entry = NULL;
4444 attr.dw_attr_val.v.val_file = fd;
4445 add_dwarf_attr (die, &attr);
4448 /* Get the dwarf_file_data from a file DIE attribute. */
4450 static inline struct dwarf_file_data *
4451 AT_file (dw_attr_ref a)
4453 gcc_assert (a && AT_class (a) == dw_val_class_file);
4454 return a->dw_attr_val.v.val_file;
4457 /* Add a vms delta attribute value to a DIE. */
4459 static inline void
4460 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4461 const char *lbl1, const char *lbl2)
4463 dw_attr_node attr;
4465 attr.dw_attr = attr_kind;
4466 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4467 attr.dw_attr_val.val_entry = NULL;
4468 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4469 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4470 add_dwarf_attr (die, &attr);
4473 /* Add a label identifier attribute value to a DIE. */
4475 static inline void
4476 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4477 const char *lbl_id)
4479 dw_attr_node attr;
4481 attr.dw_attr = attr_kind;
4482 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4483 attr.dw_attr_val.val_entry = NULL;
4484 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4485 if (dwarf_split_debug_info)
4486 attr.dw_attr_val.val_entry
4487 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4488 ate_kind_label);
4489 add_dwarf_attr (die, &attr);
4492 /* Add a section offset attribute value to a DIE, an offset into the
4493 debug_line section. */
4495 static inline void
4496 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4497 const char *label)
4499 dw_attr_node attr;
4501 attr.dw_attr = attr_kind;
4502 attr.dw_attr_val.val_class = dw_val_class_lineptr;
4503 attr.dw_attr_val.val_entry = NULL;
4504 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4505 add_dwarf_attr (die, &attr);
4508 /* Add a section offset attribute value to a DIE, an offset into the
4509 debug_macinfo section. */
4511 static inline void
4512 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4513 const char *label)
4515 dw_attr_node attr;
4517 attr.dw_attr = attr_kind;
4518 attr.dw_attr_val.val_class = dw_val_class_macptr;
4519 attr.dw_attr_val.val_entry = NULL;
4520 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4521 add_dwarf_attr (die, &attr);
4524 /* Add an offset attribute value to a DIE. */
4526 static inline void
4527 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4528 unsigned HOST_WIDE_INT offset)
4530 dw_attr_node attr;
4532 attr.dw_attr = attr_kind;
4533 attr.dw_attr_val.val_class = dw_val_class_offset;
4534 attr.dw_attr_val.val_entry = NULL;
4535 attr.dw_attr_val.v.val_offset = offset;
4536 add_dwarf_attr (die, &attr);
4539 /* Add a range_list attribute value to a DIE. When using
4540 dwarf_split_debug_info, address attributes in dies destined for the
4541 final executable should be direct references--setting the parameter
4542 force_direct ensures this behavior. */
4544 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4545 #define RELOCATED_OFFSET (NULL)
4547 static void
4548 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4549 long unsigned int offset, bool force_direct)
4551 dw_attr_node attr;
4553 attr.dw_attr = attr_kind;
4554 attr.dw_attr_val.val_class = dw_val_class_range_list;
4555 /* For the range_list attribute, use val_entry to store whether the
4556 offset should follow split-debug-info or normal semantics. This
4557 value is read in output_range_list_offset. */
4558 if (dwarf_split_debug_info && !force_direct)
4559 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4560 else
4561 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4562 attr.dw_attr_val.v.val_offset = offset;
4563 add_dwarf_attr (die, &attr);
4566 /* Return the start label of a delta attribute. */
4568 static inline const char *
4569 AT_vms_delta1 (dw_attr_ref a)
4571 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4572 return a->dw_attr_val.v.val_vms_delta.lbl1;
4575 /* Return the end label of a delta attribute. */
4577 static inline const char *
4578 AT_vms_delta2 (dw_attr_ref a)
4580 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4581 return a->dw_attr_val.v.val_vms_delta.lbl2;
4584 static inline const char *
4585 AT_lbl (dw_attr_ref a)
4587 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4588 || AT_class (a) == dw_val_class_lineptr
4589 || AT_class (a) == dw_val_class_macptr
4590 || AT_class (a) == dw_val_class_high_pc));
4591 return a->dw_attr_val.v.val_lbl_id;
4594 /* Get the attribute of type attr_kind. */
4596 static dw_attr_ref
4597 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4599 dw_attr_ref a;
4600 unsigned ix;
4601 dw_die_ref spec = NULL;
4603 if (! die)
4604 return NULL;
4606 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4607 if (a->dw_attr == attr_kind)
4608 return a;
4609 else if (a->dw_attr == DW_AT_specification
4610 || a->dw_attr == DW_AT_abstract_origin)
4611 spec = AT_ref (a);
4613 if (spec)
4614 return get_AT (spec, attr_kind);
4616 return NULL;
4619 /* Returns the parent of the declaration of DIE. */
4621 static dw_die_ref
4622 get_die_parent (dw_die_ref die)
4624 dw_die_ref t;
4626 if (!die)
4627 return NULL;
4629 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4630 || (t = get_AT_ref (die, DW_AT_specification)))
4631 die = t;
4633 return die->die_parent;
4636 /* Return the "low pc" attribute value, typically associated with a subprogram
4637 DIE. Return null if the "low pc" attribute is either not present, or if it
4638 cannot be represented as an assembler label identifier. */
4640 static inline const char *
4641 get_AT_low_pc (dw_die_ref die)
4643 dw_attr_ref a = get_AT (die, DW_AT_low_pc);
4645 return a ? AT_lbl (a) : NULL;
4648 /* Return the "high pc" attribute value, typically associated with a subprogram
4649 DIE. Return null if the "high pc" attribute is either not present, or if it
4650 cannot be represented as an assembler label identifier. */
4652 static inline const char *
4653 get_AT_hi_pc (dw_die_ref die)
4655 dw_attr_ref a = get_AT (die, DW_AT_high_pc);
4657 return a ? AT_lbl (a) : NULL;
4660 /* Return the value of the string attribute designated by ATTR_KIND, or
4661 NULL if it is not present. */
4663 static inline const char *
4664 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4666 dw_attr_ref a = get_AT (die, attr_kind);
4668 return a ? AT_string (a) : NULL;
4671 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4672 if it is not present. */
4674 static inline int
4675 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4677 dw_attr_ref a = get_AT (die, attr_kind);
4679 return a ? AT_flag (a) : 0;
4682 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4683 if it is not present. */
4685 static inline unsigned
4686 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4688 dw_attr_ref a = get_AT (die, attr_kind);
4690 return a ? AT_unsigned (a) : 0;
4693 static inline dw_die_ref
4694 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4696 dw_attr_ref a = get_AT (die, attr_kind);
4698 return a ? AT_ref (a) : NULL;
4701 static inline struct dwarf_file_data *
4702 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4704 dw_attr_ref a = get_AT (die, attr_kind);
4706 return a ? AT_file (a) : NULL;
4709 /* Return TRUE if the language is C++. */
4711 static inline bool
4712 is_cxx (void)
4714 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4716 return (lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus
4717 || lang == DW_LANG_C_plus_plus_11 || lang == DW_LANG_C_plus_plus_14);
4720 /* Return TRUE if the language is Java. */
4722 static inline bool
4723 is_java (void)
4725 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4727 return lang == DW_LANG_Java;
4730 /* Return TRUE if the language is Fortran. */
4732 static inline bool
4733 is_fortran (void)
4735 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4737 return (lang == DW_LANG_Fortran77
4738 || lang == DW_LANG_Fortran90
4739 || lang == DW_LANG_Fortran95
4740 || lang == DW_LANG_Fortran03
4741 || lang == DW_LANG_Fortran08);
4744 /* Return TRUE if the language is Ada. */
4746 static inline bool
4747 is_ada (void)
4749 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4751 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4754 /* Remove the specified attribute if present. */
4756 static void
4757 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4759 dw_attr_ref a;
4760 unsigned ix;
4762 if (! die)
4763 return;
4765 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4766 if (a->dw_attr == attr_kind)
4768 if (AT_class (a) == dw_val_class_str)
4769 if (a->dw_attr_val.v.val_str->refcount)
4770 a->dw_attr_val.v.val_str->refcount--;
4772 /* vec::ordered_remove should help reduce the number of abbrevs
4773 that are needed. */
4774 die->die_attr->ordered_remove (ix);
4775 return;
4779 /* Remove CHILD from its parent. PREV must have the property that
4780 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
4782 static void
4783 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4785 gcc_assert (child->die_parent == prev->die_parent);
4786 gcc_assert (prev->die_sib == child);
4787 if (prev == child)
4789 gcc_assert (child->die_parent->die_child == child);
4790 prev = NULL;
4792 else
4793 prev->die_sib = child->die_sib;
4794 if (child->die_parent->die_child == child)
4795 child->die_parent->die_child = prev;
4798 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
4799 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
4801 static void
4802 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4804 dw_die_ref parent = old_child->die_parent;
4806 gcc_assert (parent == prev->die_parent);
4807 gcc_assert (prev->die_sib == old_child);
4809 new_child->die_parent = parent;
4810 if (prev == old_child)
4812 gcc_assert (parent->die_child == old_child);
4813 new_child->die_sib = new_child;
4815 else
4817 prev->die_sib = new_child;
4818 new_child->die_sib = old_child->die_sib;
4820 if (old_child->die_parent->die_child == old_child)
4821 old_child->die_parent->die_child = new_child;
4824 /* Move all children from OLD_PARENT to NEW_PARENT. */
4826 static void
4827 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4829 dw_die_ref c;
4830 new_parent->die_child = old_parent->die_child;
4831 old_parent->die_child = NULL;
4832 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4835 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
4836 matches TAG. */
4838 static void
4839 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4841 dw_die_ref c;
4843 c = die->die_child;
4844 if (c) do {
4845 dw_die_ref prev = c;
4846 c = c->die_sib;
4847 while (c->die_tag == tag)
4849 remove_child_with_prev (c, prev);
4850 /* Might have removed every child. */
4851 if (c == c->die_sib)
4852 return;
4853 c = c->die_sib;
4855 } while (c != die->die_child);
4858 /* Add a CHILD_DIE as the last child of DIE. */
4860 static void
4861 add_child_die (dw_die_ref die, dw_die_ref child_die)
4863 /* FIXME this should probably be an assert. */
4864 if (! die || ! child_die)
4865 return;
4866 gcc_assert (die != child_die);
4868 child_die->die_parent = die;
4869 if (die->die_child)
4871 child_die->die_sib = die->die_child->die_sib;
4872 die->die_child->die_sib = child_die;
4874 else
4875 child_die->die_sib = child_die;
4876 die->die_child = child_die;
4879 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
4880 is the specification, to the end of PARENT's list of children.
4881 This is done by removing and re-adding it. */
4883 static void
4884 splice_child_die (dw_die_ref parent, dw_die_ref child)
4886 dw_die_ref p;
4888 /* We want the declaration DIE from inside the class, not the
4889 specification DIE at toplevel. */
4890 if (child->die_parent != parent)
4892 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
4894 if (tmp)
4895 child = tmp;
4898 gcc_assert (child->die_parent == parent
4899 || (child->die_parent
4900 == get_AT_ref (parent, DW_AT_specification)));
4902 for (p = child->die_parent->die_child; ; p = p->die_sib)
4903 if (p->die_sib == child)
4905 remove_child_with_prev (child, p);
4906 break;
4909 add_child_die (parent, child);
4912 /* Return a pointer to a newly created DIE node. */
4914 static inline dw_die_ref
4915 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
4917 dw_die_ref die = ggc_cleared_alloc<die_node> ();
4919 die->die_tag = tag_value;
4921 if (parent_die != NULL)
4922 add_child_die (parent_die, die);
4923 else
4925 limbo_die_node *limbo_node;
4927 limbo_node = ggc_cleared_alloc<limbo_die_node> ();
4928 limbo_node->die = die;
4929 limbo_node->created_for = t;
4930 limbo_node->next = limbo_die_list;
4931 limbo_die_list = limbo_node;
4934 return die;
4937 /* Return the DIE associated with the given type specifier. */
4939 static inline dw_die_ref
4940 lookup_type_die (tree type)
4942 return TYPE_SYMTAB_DIE (type);
4945 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
4946 anonymous type named by the typedef TYPE_DIE, return the DIE of the
4947 anonymous type instead the one of the naming typedef. */
4949 static inline dw_die_ref
4950 strip_naming_typedef (tree type, dw_die_ref type_die)
4952 if (type
4953 && TREE_CODE (type) == RECORD_TYPE
4954 && type_die
4955 && type_die->die_tag == DW_TAG_typedef
4956 && is_naming_typedef_decl (TYPE_NAME (type)))
4957 type_die = get_AT_ref (type_die, DW_AT_type);
4958 return type_die;
4961 /* Like lookup_type_die, but if type is an anonymous type named by a
4962 typedef[1], return the DIE of the anonymous type instead the one of
4963 the naming typedef. This is because in gen_typedef_die, we did
4964 equate the anonymous struct named by the typedef with the DIE of
4965 the naming typedef. So by default, lookup_type_die on an anonymous
4966 struct yields the DIE of the naming typedef.
4968 [1]: Read the comment of is_naming_typedef_decl to learn about what
4969 a naming typedef is. */
4971 static inline dw_die_ref
4972 lookup_type_die_strip_naming_typedef (tree type)
4974 dw_die_ref die = lookup_type_die (type);
4975 return strip_naming_typedef (type, die);
4978 /* Equate a DIE to a given type specifier. */
4980 static inline void
4981 equate_type_number_to_die (tree type, dw_die_ref type_die)
4983 TYPE_SYMTAB_DIE (type) = type_die;
4986 /* Returns a hash value for X (which really is a die_struct). */
4988 inline hashval_t
4989 decl_die_hasher::hash (die_node *x)
4991 return (hashval_t) x->decl_id;
4994 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
4996 inline bool
4997 decl_die_hasher::equal (die_node *x, tree y)
4999 return (x->decl_id == DECL_UID (y));
5002 /* Return the DIE associated with a given declaration. */
5004 static inline dw_die_ref
5005 lookup_decl_die (tree decl)
5007 return decl_die_table->find_with_hash (decl, DECL_UID (decl));
5010 /* Returns a hash value for X (which really is a var_loc_list). */
5012 inline hashval_t
5013 decl_loc_hasher::hash (var_loc_list *x)
5015 return (hashval_t) x->decl_id;
5018 /* Return nonzero if decl_id of var_loc_list X is the same as
5019 UID of decl *Y. */
5021 inline bool
5022 decl_loc_hasher::equal (var_loc_list *x, const_tree y)
5024 return (x->decl_id == DECL_UID (y));
5027 /* Return the var_loc list associated with a given declaration. */
5029 static inline var_loc_list *
5030 lookup_decl_loc (const_tree decl)
5032 if (!decl_loc_table)
5033 return NULL;
5034 return decl_loc_table->find_with_hash (decl, DECL_UID (decl));
5037 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
5039 inline hashval_t
5040 dw_loc_list_hasher::hash (cached_dw_loc_list *x)
5042 return (hashval_t) x->decl_id;
5045 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
5046 UID of decl *Y. */
5048 inline bool
5049 dw_loc_list_hasher::equal (cached_dw_loc_list *x, const_tree y)
5051 return (x->decl_id == DECL_UID (y));
5054 /* Equate a DIE to a particular declaration. */
5056 static void
5057 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5059 unsigned int decl_id = DECL_UID (decl);
5061 *decl_die_table->find_slot_with_hash (decl, decl_id, INSERT) = decl_die;
5062 decl_die->decl_id = decl_id;
5065 /* Return how many bits covers PIECE EXPR_LIST. */
5067 static HOST_WIDE_INT
5068 decl_piece_bitsize (rtx piece)
5070 int ret = (int) GET_MODE (piece);
5071 if (ret)
5072 return ret;
5073 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
5074 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
5075 return INTVAL (XEXP (XEXP (piece, 0), 0));
5078 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
5080 static rtx *
5081 decl_piece_varloc_ptr (rtx piece)
5083 if ((int) GET_MODE (piece))
5084 return &XEXP (piece, 0);
5085 else
5086 return &XEXP (XEXP (piece, 0), 1);
5089 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
5090 Next is the chain of following piece nodes. */
5092 static rtx_expr_list *
5093 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
5095 if (bitsize > 0 && bitsize <= (int) MAX_MACHINE_MODE)
5096 return alloc_EXPR_LIST (bitsize, loc_note, next);
5097 else
5098 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
5099 GEN_INT (bitsize),
5100 loc_note), next);
5103 /* Return rtx that should be stored into loc field for
5104 LOC_NOTE and BITPOS/BITSIZE. */
5106 static rtx
5107 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
5108 HOST_WIDE_INT bitsize)
5110 if (bitsize != -1)
5112 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
5113 if (bitpos != 0)
5114 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
5116 return loc_note;
5119 /* This function either modifies location piece list *DEST in
5120 place (if SRC and INNER is NULL), or copies location piece list
5121 *SRC to *DEST while modifying it. Location BITPOS is modified
5122 to contain LOC_NOTE, any pieces overlapping it are removed resp.
5123 not copied and if needed some padding around it is added.
5124 When modifying in place, DEST should point to EXPR_LIST where
5125 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
5126 to the start of the whole list and INNER points to the EXPR_LIST
5127 where earlier pieces cover PIECE_BITPOS bits. */
5129 static void
5130 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5131 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5132 HOST_WIDE_INT bitsize, rtx loc_note)
5134 HOST_WIDE_INT diff;
5135 bool copy = inner != NULL;
5137 if (copy)
5139 /* First copy all nodes preceding the current bitpos. */
5140 while (src != inner)
5142 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5143 decl_piece_bitsize (*src), NULL_RTX);
5144 dest = &XEXP (*dest, 1);
5145 src = &XEXP (*src, 1);
5148 /* Add padding if needed. */
5149 if (bitpos != piece_bitpos)
5151 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5152 copy ? NULL_RTX : *dest);
5153 dest = &XEXP (*dest, 1);
5155 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5157 gcc_assert (!copy);
5158 /* A piece with correct bitpos and bitsize already exist,
5159 just update the location for it and return. */
5160 *decl_piece_varloc_ptr (*dest) = loc_note;
5161 return;
5163 /* Add the piece that changed. */
5164 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5165 dest = &XEXP (*dest, 1);
5166 /* Skip over pieces that overlap it. */
5167 diff = bitpos - piece_bitpos + bitsize;
5168 if (!copy)
5169 src = dest;
5170 while (diff > 0 && *src)
5172 rtx piece = *src;
5173 diff -= decl_piece_bitsize (piece);
5174 if (copy)
5175 src = &XEXP (piece, 1);
5176 else
5178 *src = XEXP (piece, 1);
5179 free_EXPR_LIST_node (piece);
5182 /* Add padding if needed. */
5183 if (diff < 0 && *src)
5185 if (!copy)
5186 dest = src;
5187 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5188 dest = &XEXP (*dest, 1);
5190 if (!copy)
5191 return;
5192 /* Finally copy all nodes following it. */
5193 while (*src)
5195 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5196 decl_piece_bitsize (*src), NULL_RTX);
5197 dest = &XEXP (*dest, 1);
5198 src = &XEXP (*src, 1);
5202 /* Add a variable location node to the linked list for DECL. */
5204 static struct var_loc_node *
5205 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5207 unsigned int decl_id;
5208 var_loc_list *temp;
5209 struct var_loc_node *loc = NULL;
5210 HOST_WIDE_INT bitsize = -1, bitpos = -1;
5212 if (TREE_CODE (decl) == VAR_DECL
5213 && DECL_HAS_DEBUG_EXPR_P (decl))
5215 tree realdecl = DECL_DEBUG_EXPR (decl);
5216 if (handled_component_p (realdecl)
5217 || (TREE_CODE (realdecl) == MEM_REF
5218 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5220 HOST_WIDE_INT maxsize;
5221 tree innerdecl;
5222 innerdecl
5223 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
5224 if (!DECL_P (innerdecl)
5225 || DECL_IGNORED_P (innerdecl)
5226 || TREE_STATIC (innerdecl)
5227 || bitsize <= 0
5228 || bitpos + bitsize > 256
5229 || bitsize != maxsize)
5230 return NULL;
5231 decl = innerdecl;
5235 decl_id = DECL_UID (decl);
5236 var_loc_list **slot
5237 = decl_loc_table->find_slot_with_hash (decl, decl_id, INSERT);
5238 if (*slot == NULL)
5240 temp = ggc_cleared_alloc<var_loc_list> ();
5241 temp->decl_id = decl_id;
5242 *slot = temp;
5244 else
5245 temp = *slot;
5247 /* For PARM_DECLs try to keep around the original incoming value,
5248 even if that means we'll emit a zero-range .debug_loc entry. */
5249 if (temp->last
5250 && temp->first == temp->last
5251 && TREE_CODE (decl) == PARM_DECL
5252 && NOTE_P (temp->first->loc)
5253 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5254 && DECL_INCOMING_RTL (decl)
5255 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5256 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5257 == GET_CODE (DECL_INCOMING_RTL (decl))
5258 && prev_real_insn (temp->first->loc) == NULL_RTX
5259 && (bitsize != -1
5260 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5261 NOTE_VAR_LOCATION_LOC (loc_note))
5262 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5263 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5265 loc = ggc_cleared_alloc<var_loc_node> ();
5266 temp->first->next = loc;
5267 temp->last = loc;
5268 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5270 else if (temp->last)
5272 struct var_loc_node *last = temp->last, *unused = NULL;
5273 rtx *piece_loc = NULL, last_loc_note;
5274 HOST_WIDE_INT piece_bitpos = 0;
5275 if (last->next)
5277 last = last->next;
5278 gcc_assert (last->next == NULL);
5280 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5282 piece_loc = &last->loc;
5285 HOST_WIDE_INT cur_bitsize = decl_piece_bitsize (*piece_loc);
5286 if (piece_bitpos + cur_bitsize > bitpos)
5287 break;
5288 piece_bitpos += cur_bitsize;
5289 piece_loc = &XEXP (*piece_loc, 1);
5291 while (*piece_loc);
5293 /* TEMP->LAST here is either pointer to the last but one or
5294 last element in the chained list, LAST is pointer to the
5295 last element. */
5296 if (label && strcmp (last->label, label) == 0)
5298 /* For SRA optimized variables if there weren't any real
5299 insns since last note, just modify the last node. */
5300 if (piece_loc != NULL)
5302 adjust_piece_list (piece_loc, NULL, NULL,
5303 bitpos, piece_bitpos, bitsize, loc_note);
5304 return NULL;
5306 /* If the last note doesn't cover any instructions, remove it. */
5307 if (temp->last != last)
5309 temp->last->next = NULL;
5310 unused = last;
5311 last = temp->last;
5312 gcc_assert (strcmp (last->label, label) != 0);
5314 else
5316 gcc_assert (temp->first == temp->last
5317 || (temp->first->next == temp->last
5318 && TREE_CODE (decl) == PARM_DECL));
5319 memset (temp->last, '\0', sizeof (*temp->last));
5320 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5321 return temp->last;
5324 if (bitsize == -1 && NOTE_P (last->loc))
5325 last_loc_note = last->loc;
5326 else if (piece_loc != NULL
5327 && *piece_loc != NULL_RTX
5328 && piece_bitpos == bitpos
5329 && decl_piece_bitsize (*piece_loc) == bitsize)
5330 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5331 else
5332 last_loc_note = NULL_RTX;
5333 /* If the current location is the same as the end of the list,
5334 and either both or neither of the locations is uninitialized,
5335 we have nothing to do. */
5336 if (last_loc_note == NULL_RTX
5337 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5338 NOTE_VAR_LOCATION_LOC (loc_note)))
5339 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5340 != NOTE_VAR_LOCATION_STATUS (loc_note))
5341 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5342 == VAR_INIT_STATUS_UNINITIALIZED)
5343 || (NOTE_VAR_LOCATION_STATUS (loc_note)
5344 == VAR_INIT_STATUS_UNINITIALIZED))))
5346 /* Add LOC to the end of list and update LAST. If the last
5347 element of the list has been removed above, reuse its
5348 memory for the new node, otherwise allocate a new one. */
5349 if (unused)
5351 loc = unused;
5352 memset (loc, '\0', sizeof (*loc));
5354 else
5355 loc = ggc_cleared_alloc<var_loc_node> ();
5356 if (bitsize == -1 || piece_loc == NULL)
5357 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5358 else
5359 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5360 bitpos, piece_bitpos, bitsize, loc_note);
5361 last->next = loc;
5362 /* Ensure TEMP->LAST will point either to the new last but one
5363 element of the chain, or to the last element in it. */
5364 if (last != temp->last)
5365 temp->last = last;
5367 else if (unused)
5368 ggc_free (unused);
5370 else
5372 loc = ggc_cleared_alloc<var_loc_node> ();
5373 temp->first = loc;
5374 temp->last = loc;
5375 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5377 return loc;
5380 /* Keep track of the number of spaces used to indent the
5381 output of the debugging routines that print the structure of
5382 the DIE internal representation. */
5383 static int print_indent;
5385 /* Indent the line the number of spaces given by print_indent. */
5387 static inline void
5388 print_spaces (FILE *outfile)
5390 fprintf (outfile, "%*s", print_indent, "");
5393 /* Print a type signature in hex. */
5395 static inline void
5396 print_signature (FILE *outfile, char *sig)
5398 int i;
5400 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5401 fprintf (outfile, "%02x", sig[i] & 0xff);
5404 static void print_loc_descr (dw_loc_descr_ref, FILE *);
5406 /* Print the value associated to the VAL DWARF value node to OUTFILE. If
5407 RECURSE, output location descriptor operations. */
5409 static void
5410 print_dw_val (dw_val_node *val, bool recurse, FILE *outfile)
5412 switch (val->val_class)
5414 case dw_val_class_addr:
5415 fprintf (outfile, "address");
5416 break;
5417 case dw_val_class_offset:
5418 fprintf (outfile, "offset");
5419 break;
5420 case dw_val_class_loc:
5421 fprintf (outfile, "location descriptor");
5422 if (val->v.val_loc == NULL)
5423 fprintf (outfile, " -> <null>\n");
5424 else if (recurse)
5426 fprintf (outfile, ":\n");
5427 print_indent += 4;
5428 print_loc_descr (val->v.val_loc, outfile);
5429 print_indent -= 4;
5431 else
5432 fprintf (outfile, " (%p)\n", (void *) val->v.val_loc);
5433 break;
5434 case dw_val_class_loc_list:
5435 fprintf (outfile, "location list -> label:%s",
5436 val->v.val_loc_list->ll_symbol);
5437 break;
5438 case dw_val_class_range_list:
5439 fprintf (outfile, "range list");
5440 break;
5441 case dw_val_class_const:
5442 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, val->v.val_int);
5443 break;
5444 case dw_val_class_unsigned_const:
5445 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, val->v.val_unsigned);
5446 break;
5447 case dw_val_class_const_double:
5448 fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
5449 HOST_WIDE_INT_PRINT_UNSIGNED")",
5450 val->v.val_double.high,
5451 val->v.val_double.low);
5452 break;
5453 case dw_val_class_wide_int:
5455 int i = val->v.val_wide->get_len ();
5456 fprintf (outfile, "constant (");
5457 gcc_assert (i > 0);
5458 if (val->v.val_wide->elt (i - 1) == 0)
5459 fprintf (outfile, "0x");
5460 fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
5461 val->v.val_wide->elt (--i));
5462 while (--i >= 0)
5463 fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
5464 val->v.val_wide->elt (i));
5465 fprintf (outfile, ")");
5466 break;
5468 case dw_val_class_vec:
5469 fprintf (outfile, "floating-point or vector constant");
5470 break;
5471 case dw_val_class_flag:
5472 fprintf (outfile, "%u", val->v.val_flag);
5473 break;
5474 case dw_val_class_die_ref:
5475 if (val->v.val_die_ref.die != NULL)
5477 dw_die_ref die = val->v.val_die_ref.die;
5479 if (die->comdat_type_p)
5481 fprintf (outfile, "die -> signature: ");
5482 print_signature (outfile,
5483 die->die_id.die_type_node->signature);
5485 else if (die->die_id.die_symbol)
5486 fprintf (outfile, "die -> label: %s", die->die_id.die_symbol);
5487 else
5488 fprintf (outfile, "die -> %ld", die->die_offset);
5489 fprintf (outfile, " (%p)", (void *) die);
5491 else
5492 fprintf (outfile, "die -> <null>");
5493 break;
5494 case dw_val_class_vms_delta:
5495 fprintf (outfile, "delta: @slotcount(%s-%s)",
5496 val->v.val_vms_delta.lbl2, val->v.val_vms_delta.lbl1);
5497 break;
5498 case dw_val_class_lbl_id:
5499 case dw_val_class_lineptr:
5500 case dw_val_class_macptr:
5501 case dw_val_class_high_pc:
5502 fprintf (outfile, "label: %s", val->v.val_lbl_id);
5503 break;
5504 case dw_val_class_str:
5505 if (val->v.val_str->str != NULL)
5506 fprintf (outfile, "\"%s\"", val->v.val_str->str);
5507 else
5508 fprintf (outfile, "<null>");
5509 break;
5510 case dw_val_class_file:
5511 fprintf (outfile, "\"%s\" (%d)", val->v.val_file->filename,
5512 val->v.val_file->emitted_number);
5513 break;
5514 case dw_val_class_data8:
5516 int i;
5518 for (i = 0; i < 8; i++)
5519 fprintf (outfile, "%02x", val->v.val_data8[i]);
5520 break;
5522 default:
5523 break;
5527 /* Likewise, for a DIE attribute. */
5529 static void
5530 print_attribute (dw_attr_ref a, bool recurse, FILE *outfile)
5532 print_dw_val (&a->dw_attr_val, recurse, outfile);
5536 /* Print the list of operands in the LOC location description to OUTFILE. This
5537 routine is a debugging aid only. */
5539 static void
5540 print_loc_descr (dw_loc_descr_ref loc, FILE *outfile)
5542 dw_loc_descr_ref l = loc;
5544 if (loc == NULL)
5546 print_spaces (outfile);
5547 fprintf (outfile, "<null>\n");
5548 return;
5551 for (l = loc; l != NULL; l = l->dw_loc_next)
5553 print_spaces (outfile);
5554 fprintf (outfile, "(%p) %s",
5555 (void *) l,
5556 dwarf_stack_op_name (l->dw_loc_opc));
5557 if (l->dw_loc_oprnd1.val_class != dw_val_class_none)
5559 fprintf (outfile, " ");
5560 print_dw_val (&l->dw_loc_oprnd1, false, outfile);
5562 if (l->dw_loc_oprnd2.val_class != dw_val_class_none)
5564 fprintf (outfile, ", ");
5565 print_dw_val (&l->dw_loc_oprnd2, false, outfile);
5567 fprintf (outfile, "\n");
5571 /* Print the information associated with a given DIE, and its children.
5572 This routine is a debugging aid only. */
5574 static void
5575 print_die (dw_die_ref die, FILE *outfile)
5577 dw_attr_ref a;
5578 dw_die_ref c;
5579 unsigned ix;
5581 print_spaces (outfile);
5582 fprintf (outfile, "DIE %4ld: %s (%p)\n",
5583 die->die_offset, dwarf_tag_name (die->die_tag),
5584 (void*) die);
5585 print_spaces (outfile);
5586 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5587 fprintf (outfile, " offset: %ld", die->die_offset);
5588 fprintf (outfile, " mark: %d\n", die->die_mark);
5590 if (die->comdat_type_p)
5592 print_spaces (outfile);
5593 fprintf (outfile, " signature: ");
5594 print_signature (outfile, die->die_id.die_type_node->signature);
5595 fprintf (outfile, "\n");
5598 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5600 print_spaces (outfile);
5601 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5603 print_attribute (a, true, outfile);
5604 fprintf (outfile, "\n");
5607 if (die->die_child != NULL)
5609 print_indent += 4;
5610 FOR_EACH_CHILD (die, c, print_die (c, outfile));
5611 print_indent -= 4;
5613 if (print_indent == 0)
5614 fprintf (outfile, "\n");
5617 /* Print the list of operations in the LOC location description. */
5619 DEBUG_FUNCTION void
5620 debug_dwarf_loc_descr (dw_loc_descr_ref loc)
5622 print_loc_descr (loc, stderr);
5625 /* Print the information collected for a given DIE. */
5627 DEBUG_FUNCTION void
5628 debug_dwarf_die (dw_die_ref die)
5630 print_die (die, stderr);
5633 DEBUG_FUNCTION void
5634 debug (die_struct &ref)
5636 print_die (&ref, stderr);
5639 DEBUG_FUNCTION void
5640 debug (die_struct *ptr)
5642 if (ptr)
5643 debug (*ptr);
5644 else
5645 fprintf (stderr, "<nil>\n");
5649 /* Print all DWARF information collected for the compilation unit.
5650 This routine is a debugging aid only. */
5652 DEBUG_FUNCTION void
5653 debug_dwarf (void)
5655 print_indent = 0;
5656 print_die (comp_unit_die (), stderr);
5659 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
5660 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
5661 DIE that marks the start of the DIEs for this include file. */
5663 static dw_die_ref
5664 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5666 const char *filename = get_AT_string (bincl_die, DW_AT_name);
5667 dw_die_ref new_unit = gen_compile_unit_die (filename);
5669 new_unit->die_sib = old_unit;
5670 return new_unit;
5673 /* Close an include-file CU and reopen the enclosing one. */
5675 static dw_die_ref
5676 pop_compile_unit (dw_die_ref old_unit)
5678 dw_die_ref new_unit = old_unit->die_sib;
5680 old_unit->die_sib = NULL;
5681 return new_unit;
5684 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5685 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5686 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5688 /* Calculate the checksum of a location expression. */
5690 static inline void
5691 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5693 int tem;
5694 inchash::hash hstate;
5695 hashval_t hash;
5697 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
5698 CHECKSUM (tem);
5699 hash_loc_operands (loc, hstate);
5700 hash = hstate.end();
5701 CHECKSUM (hash);
5704 /* Calculate the checksum of an attribute. */
5706 static void
5707 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5709 dw_loc_descr_ref loc;
5710 rtx r;
5712 CHECKSUM (at->dw_attr);
5714 /* We don't care that this was compiled with a different compiler
5715 snapshot; if the output is the same, that's what matters. */
5716 if (at->dw_attr == DW_AT_producer)
5717 return;
5719 switch (AT_class (at))
5721 case dw_val_class_const:
5722 CHECKSUM (at->dw_attr_val.v.val_int);
5723 break;
5724 case dw_val_class_unsigned_const:
5725 CHECKSUM (at->dw_attr_val.v.val_unsigned);
5726 break;
5727 case dw_val_class_const_double:
5728 CHECKSUM (at->dw_attr_val.v.val_double);
5729 break;
5730 case dw_val_class_wide_int:
5731 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
5732 get_full_len (*at->dw_attr_val.v.val_wide)
5733 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
5734 break;
5735 case dw_val_class_vec:
5736 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5737 (at->dw_attr_val.v.val_vec.length
5738 * at->dw_attr_val.v.val_vec.elt_size));
5739 break;
5740 case dw_val_class_flag:
5741 CHECKSUM (at->dw_attr_val.v.val_flag);
5742 break;
5743 case dw_val_class_str:
5744 CHECKSUM_STRING (AT_string (at));
5745 break;
5747 case dw_val_class_addr:
5748 r = AT_addr (at);
5749 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5750 CHECKSUM_STRING (XSTR (r, 0));
5751 break;
5753 case dw_val_class_offset:
5754 CHECKSUM (at->dw_attr_val.v.val_offset);
5755 break;
5757 case dw_val_class_loc:
5758 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5759 loc_checksum (loc, ctx);
5760 break;
5762 case dw_val_class_die_ref:
5763 die_checksum (AT_ref (at), ctx, mark);
5764 break;
5766 case dw_val_class_fde_ref:
5767 case dw_val_class_vms_delta:
5768 case dw_val_class_lbl_id:
5769 case dw_val_class_lineptr:
5770 case dw_val_class_macptr:
5771 case dw_val_class_high_pc:
5772 break;
5774 case dw_val_class_file:
5775 CHECKSUM_STRING (AT_file (at)->filename);
5776 break;
5778 case dw_val_class_data8:
5779 CHECKSUM (at->dw_attr_val.v.val_data8);
5780 break;
5782 default:
5783 break;
5787 /* Calculate the checksum of a DIE. */
5789 static void
5790 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5792 dw_die_ref c;
5793 dw_attr_ref a;
5794 unsigned ix;
5796 /* To avoid infinite recursion. */
5797 if (die->die_mark)
5799 CHECKSUM (die->die_mark);
5800 return;
5802 die->die_mark = ++(*mark);
5804 CHECKSUM (die->die_tag);
5806 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5807 attr_checksum (a, ctx, mark);
5809 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
5812 #undef CHECKSUM
5813 #undef CHECKSUM_BLOCK
5814 #undef CHECKSUM_STRING
5816 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
5817 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5818 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5819 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
5820 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
5821 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
5822 #define CHECKSUM_ATTR(FOO) \
5823 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
5825 /* Calculate the checksum of a number in signed LEB128 format. */
5827 static void
5828 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
5830 unsigned char byte;
5831 bool more;
5833 while (1)
5835 byte = (value & 0x7f);
5836 value >>= 7;
5837 more = !((value == 0 && (byte & 0x40) == 0)
5838 || (value == -1 && (byte & 0x40) != 0));
5839 if (more)
5840 byte |= 0x80;
5841 CHECKSUM (byte);
5842 if (!more)
5843 break;
5847 /* Calculate the checksum of a number in unsigned LEB128 format. */
5849 static void
5850 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
5852 while (1)
5854 unsigned char byte = (value & 0x7f);
5855 value >>= 7;
5856 if (value != 0)
5857 /* More bytes to follow. */
5858 byte |= 0x80;
5859 CHECKSUM (byte);
5860 if (value == 0)
5861 break;
5865 /* Checksum the context of the DIE. This adds the names of any
5866 surrounding namespaces or structures to the checksum. */
5868 static void
5869 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
5871 const char *name;
5872 dw_die_ref spec;
5873 int tag = die->die_tag;
5875 if (tag != DW_TAG_namespace
5876 && tag != DW_TAG_structure_type
5877 && tag != DW_TAG_class_type)
5878 return;
5880 name = get_AT_string (die, DW_AT_name);
5882 spec = get_AT_ref (die, DW_AT_specification);
5883 if (spec != NULL)
5884 die = spec;
5886 if (die->die_parent != NULL)
5887 checksum_die_context (die->die_parent, ctx);
5889 CHECKSUM_ULEB128 ('C');
5890 CHECKSUM_ULEB128 (tag);
5891 if (name != NULL)
5892 CHECKSUM_STRING (name);
5895 /* Calculate the checksum of a location expression. */
5897 static inline void
5898 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5900 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
5901 were emitted as a DW_FORM_sdata instead of a location expression. */
5902 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
5904 CHECKSUM_ULEB128 (DW_FORM_sdata);
5905 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
5906 return;
5909 /* Otherwise, just checksum the raw location expression. */
5910 while (loc != NULL)
5912 inchash::hash hstate;
5913 hashval_t hash;
5915 CHECKSUM_ULEB128 (loc->dtprel);
5916 CHECKSUM_ULEB128 (loc->dw_loc_opc);
5917 hash_loc_operands (loc, hstate);
5918 hash = hstate.end ();
5919 CHECKSUM (hash);
5920 loc = loc->dw_loc_next;
5924 /* Calculate the checksum of an attribute. */
5926 static void
5927 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
5928 struct md5_ctx *ctx, int *mark)
5930 dw_loc_descr_ref loc;
5931 rtx r;
5933 if (AT_class (at) == dw_val_class_die_ref)
5935 dw_die_ref target_die = AT_ref (at);
5937 /* For pointer and reference types, we checksum only the (qualified)
5938 name of the target type (if there is a name). For friend entries,
5939 we checksum only the (qualified) name of the target type or function.
5940 This allows the checksum to remain the same whether the target type
5941 is complete or not. */
5942 if ((at->dw_attr == DW_AT_type
5943 && (tag == DW_TAG_pointer_type
5944 || tag == DW_TAG_reference_type
5945 || tag == DW_TAG_rvalue_reference_type
5946 || tag == DW_TAG_ptr_to_member_type))
5947 || (at->dw_attr == DW_AT_friend
5948 && tag == DW_TAG_friend))
5950 dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
5952 if (name_attr != NULL)
5954 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5956 if (decl == NULL)
5957 decl = target_die;
5958 CHECKSUM_ULEB128 ('N');
5959 CHECKSUM_ULEB128 (at->dw_attr);
5960 if (decl->die_parent != NULL)
5961 checksum_die_context (decl->die_parent, ctx);
5962 CHECKSUM_ULEB128 ('E');
5963 CHECKSUM_STRING (AT_string (name_attr));
5964 return;
5968 /* For all other references to another DIE, we check to see if the
5969 target DIE has already been visited. If it has, we emit a
5970 backward reference; if not, we descend recursively. */
5971 if (target_die->die_mark > 0)
5973 CHECKSUM_ULEB128 ('R');
5974 CHECKSUM_ULEB128 (at->dw_attr);
5975 CHECKSUM_ULEB128 (target_die->die_mark);
5977 else
5979 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5981 if (decl == NULL)
5982 decl = target_die;
5983 target_die->die_mark = ++(*mark);
5984 CHECKSUM_ULEB128 ('T');
5985 CHECKSUM_ULEB128 (at->dw_attr);
5986 if (decl->die_parent != NULL)
5987 checksum_die_context (decl->die_parent, ctx);
5988 die_checksum_ordered (target_die, ctx, mark);
5990 return;
5993 CHECKSUM_ULEB128 ('A');
5994 CHECKSUM_ULEB128 (at->dw_attr);
5996 switch (AT_class (at))
5998 case dw_val_class_const:
5999 CHECKSUM_ULEB128 (DW_FORM_sdata);
6000 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
6001 break;
6003 case dw_val_class_unsigned_const:
6004 CHECKSUM_ULEB128 (DW_FORM_sdata);
6005 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
6006 break;
6008 case dw_val_class_const_double:
6009 CHECKSUM_ULEB128 (DW_FORM_block);
6010 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
6011 CHECKSUM (at->dw_attr_val.v.val_double);
6012 break;
6014 case dw_val_class_wide_int:
6015 CHECKSUM_ULEB128 (DW_FORM_block);
6016 CHECKSUM_ULEB128 (get_full_len (*at->dw_attr_val.v.val_wide)
6017 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
6018 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6019 get_full_len (*at->dw_attr_val.v.val_wide)
6020 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6021 break;
6023 case dw_val_class_vec:
6024 CHECKSUM_ULEB128 (DW_FORM_block);
6025 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
6026 * at->dw_attr_val.v.val_vec.elt_size);
6027 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6028 (at->dw_attr_val.v.val_vec.length
6029 * at->dw_attr_val.v.val_vec.elt_size));
6030 break;
6032 case dw_val_class_flag:
6033 CHECKSUM_ULEB128 (DW_FORM_flag);
6034 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
6035 break;
6037 case dw_val_class_str:
6038 CHECKSUM_ULEB128 (DW_FORM_string);
6039 CHECKSUM_STRING (AT_string (at));
6040 break;
6042 case dw_val_class_addr:
6043 r = AT_addr (at);
6044 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6045 CHECKSUM_ULEB128 (DW_FORM_string);
6046 CHECKSUM_STRING (XSTR (r, 0));
6047 break;
6049 case dw_val_class_offset:
6050 CHECKSUM_ULEB128 (DW_FORM_sdata);
6051 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
6052 break;
6054 case dw_val_class_loc:
6055 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6056 loc_checksum_ordered (loc, ctx);
6057 break;
6059 case dw_val_class_fde_ref:
6060 case dw_val_class_lbl_id:
6061 case dw_val_class_lineptr:
6062 case dw_val_class_macptr:
6063 case dw_val_class_high_pc:
6064 break;
6066 case dw_val_class_file:
6067 CHECKSUM_ULEB128 (DW_FORM_string);
6068 CHECKSUM_STRING (AT_file (at)->filename);
6069 break;
6071 case dw_val_class_data8:
6072 CHECKSUM (at->dw_attr_val.v.val_data8);
6073 break;
6075 default:
6076 break;
6080 struct checksum_attributes
6082 dw_attr_ref at_name;
6083 dw_attr_ref at_type;
6084 dw_attr_ref at_friend;
6085 dw_attr_ref at_accessibility;
6086 dw_attr_ref at_address_class;
6087 dw_attr_ref at_allocated;
6088 dw_attr_ref at_artificial;
6089 dw_attr_ref at_associated;
6090 dw_attr_ref at_binary_scale;
6091 dw_attr_ref at_bit_offset;
6092 dw_attr_ref at_bit_size;
6093 dw_attr_ref at_bit_stride;
6094 dw_attr_ref at_byte_size;
6095 dw_attr_ref at_byte_stride;
6096 dw_attr_ref at_const_value;
6097 dw_attr_ref at_containing_type;
6098 dw_attr_ref at_count;
6099 dw_attr_ref at_data_location;
6100 dw_attr_ref at_data_member_location;
6101 dw_attr_ref at_decimal_scale;
6102 dw_attr_ref at_decimal_sign;
6103 dw_attr_ref at_default_value;
6104 dw_attr_ref at_digit_count;
6105 dw_attr_ref at_discr;
6106 dw_attr_ref at_discr_list;
6107 dw_attr_ref at_discr_value;
6108 dw_attr_ref at_encoding;
6109 dw_attr_ref at_endianity;
6110 dw_attr_ref at_explicit;
6111 dw_attr_ref at_is_optional;
6112 dw_attr_ref at_location;
6113 dw_attr_ref at_lower_bound;
6114 dw_attr_ref at_mutable;
6115 dw_attr_ref at_ordering;
6116 dw_attr_ref at_picture_string;
6117 dw_attr_ref at_prototyped;
6118 dw_attr_ref at_small;
6119 dw_attr_ref at_segment;
6120 dw_attr_ref at_string_length;
6121 dw_attr_ref at_threads_scaled;
6122 dw_attr_ref at_upper_bound;
6123 dw_attr_ref at_use_location;
6124 dw_attr_ref at_use_UTF8;
6125 dw_attr_ref at_variable_parameter;
6126 dw_attr_ref at_virtuality;
6127 dw_attr_ref at_visibility;
6128 dw_attr_ref at_vtable_elem_location;
6131 /* Collect the attributes that we will want to use for the checksum. */
6133 static void
6134 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
6136 dw_attr_ref a;
6137 unsigned ix;
6139 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6141 switch (a->dw_attr)
6143 case DW_AT_name:
6144 attrs->at_name = a;
6145 break;
6146 case DW_AT_type:
6147 attrs->at_type = a;
6148 break;
6149 case DW_AT_friend:
6150 attrs->at_friend = a;
6151 break;
6152 case DW_AT_accessibility:
6153 attrs->at_accessibility = a;
6154 break;
6155 case DW_AT_address_class:
6156 attrs->at_address_class = a;
6157 break;
6158 case DW_AT_allocated:
6159 attrs->at_allocated = a;
6160 break;
6161 case DW_AT_artificial:
6162 attrs->at_artificial = a;
6163 break;
6164 case DW_AT_associated:
6165 attrs->at_associated = a;
6166 break;
6167 case DW_AT_binary_scale:
6168 attrs->at_binary_scale = a;
6169 break;
6170 case DW_AT_bit_offset:
6171 attrs->at_bit_offset = a;
6172 break;
6173 case DW_AT_bit_size:
6174 attrs->at_bit_size = a;
6175 break;
6176 case DW_AT_bit_stride:
6177 attrs->at_bit_stride = a;
6178 break;
6179 case DW_AT_byte_size:
6180 attrs->at_byte_size = a;
6181 break;
6182 case DW_AT_byte_stride:
6183 attrs->at_byte_stride = a;
6184 break;
6185 case DW_AT_const_value:
6186 attrs->at_const_value = a;
6187 break;
6188 case DW_AT_containing_type:
6189 attrs->at_containing_type = a;
6190 break;
6191 case DW_AT_count:
6192 attrs->at_count = a;
6193 break;
6194 case DW_AT_data_location:
6195 attrs->at_data_location = a;
6196 break;
6197 case DW_AT_data_member_location:
6198 attrs->at_data_member_location = a;
6199 break;
6200 case DW_AT_decimal_scale:
6201 attrs->at_decimal_scale = a;
6202 break;
6203 case DW_AT_decimal_sign:
6204 attrs->at_decimal_sign = a;
6205 break;
6206 case DW_AT_default_value:
6207 attrs->at_default_value = a;
6208 break;
6209 case DW_AT_digit_count:
6210 attrs->at_digit_count = a;
6211 break;
6212 case DW_AT_discr:
6213 attrs->at_discr = a;
6214 break;
6215 case DW_AT_discr_list:
6216 attrs->at_discr_list = a;
6217 break;
6218 case DW_AT_discr_value:
6219 attrs->at_discr_value = a;
6220 break;
6221 case DW_AT_encoding:
6222 attrs->at_encoding = a;
6223 break;
6224 case DW_AT_endianity:
6225 attrs->at_endianity = a;
6226 break;
6227 case DW_AT_explicit:
6228 attrs->at_explicit = a;
6229 break;
6230 case DW_AT_is_optional:
6231 attrs->at_is_optional = a;
6232 break;
6233 case DW_AT_location:
6234 attrs->at_location = a;
6235 break;
6236 case DW_AT_lower_bound:
6237 attrs->at_lower_bound = a;
6238 break;
6239 case DW_AT_mutable:
6240 attrs->at_mutable = a;
6241 break;
6242 case DW_AT_ordering:
6243 attrs->at_ordering = a;
6244 break;
6245 case DW_AT_picture_string:
6246 attrs->at_picture_string = a;
6247 break;
6248 case DW_AT_prototyped:
6249 attrs->at_prototyped = a;
6250 break;
6251 case DW_AT_small:
6252 attrs->at_small = a;
6253 break;
6254 case DW_AT_segment:
6255 attrs->at_segment = a;
6256 break;
6257 case DW_AT_string_length:
6258 attrs->at_string_length = a;
6259 break;
6260 case DW_AT_threads_scaled:
6261 attrs->at_threads_scaled = a;
6262 break;
6263 case DW_AT_upper_bound:
6264 attrs->at_upper_bound = a;
6265 break;
6266 case DW_AT_use_location:
6267 attrs->at_use_location = a;
6268 break;
6269 case DW_AT_use_UTF8:
6270 attrs->at_use_UTF8 = a;
6271 break;
6272 case DW_AT_variable_parameter:
6273 attrs->at_variable_parameter = a;
6274 break;
6275 case DW_AT_virtuality:
6276 attrs->at_virtuality = a;
6277 break;
6278 case DW_AT_visibility:
6279 attrs->at_visibility = a;
6280 break;
6281 case DW_AT_vtable_elem_location:
6282 attrs->at_vtable_elem_location = a;
6283 break;
6284 default:
6285 break;
6290 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
6292 static void
6293 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6295 dw_die_ref c;
6296 dw_die_ref decl;
6297 struct checksum_attributes attrs;
6299 CHECKSUM_ULEB128 ('D');
6300 CHECKSUM_ULEB128 (die->die_tag);
6302 memset (&attrs, 0, sizeof (attrs));
6304 decl = get_AT_ref (die, DW_AT_specification);
6305 if (decl != NULL)
6306 collect_checksum_attributes (&attrs, decl);
6307 collect_checksum_attributes (&attrs, die);
6309 CHECKSUM_ATTR (attrs.at_name);
6310 CHECKSUM_ATTR (attrs.at_accessibility);
6311 CHECKSUM_ATTR (attrs.at_address_class);
6312 CHECKSUM_ATTR (attrs.at_allocated);
6313 CHECKSUM_ATTR (attrs.at_artificial);
6314 CHECKSUM_ATTR (attrs.at_associated);
6315 CHECKSUM_ATTR (attrs.at_binary_scale);
6316 CHECKSUM_ATTR (attrs.at_bit_offset);
6317 CHECKSUM_ATTR (attrs.at_bit_size);
6318 CHECKSUM_ATTR (attrs.at_bit_stride);
6319 CHECKSUM_ATTR (attrs.at_byte_size);
6320 CHECKSUM_ATTR (attrs.at_byte_stride);
6321 CHECKSUM_ATTR (attrs.at_const_value);
6322 CHECKSUM_ATTR (attrs.at_containing_type);
6323 CHECKSUM_ATTR (attrs.at_count);
6324 CHECKSUM_ATTR (attrs.at_data_location);
6325 CHECKSUM_ATTR (attrs.at_data_member_location);
6326 CHECKSUM_ATTR (attrs.at_decimal_scale);
6327 CHECKSUM_ATTR (attrs.at_decimal_sign);
6328 CHECKSUM_ATTR (attrs.at_default_value);
6329 CHECKSUM_ATTR (attrs.at_digit_count);
6330 CHECKSUM_ATTR (attrs.at_discr);
6331 CHECKSUM_ATTR (attrs.at_discr_list);
6332 CHECKSUM_ATTR (attrs.at_discr_value);
6333 CHECKSUM_ATTR (attrs.at_encoding);
6334 CHECKSUM_ATTR (attrs.at_endianity);
6335 CHECKSUM_ATTR (attrs.at_explicit);
6336 CHECKSUM_ATTR (attrs.at_is_optional);
6337 CHECKSUM_ATTR (attrs.at_location);
6338 CHECKSUM_ATTR (attrs.at_lower_bound);
6339 CHECKSUM_ATTR (attrs.at_mutable);
6340 CHECKSUM_ATTR (attrs.at_ordering);
6341 CHECKSUM_ATTR (attrs.at_picture_string);
6342 CHECKSUM_ATTR (attrs.at_prototyped);
6343 CHECKSUM_ATTR (attrs.at_small);
6344 CHECKSUM_ATTR (attrs.at_segment);
6345 CHECKSUM_ATTR (attrs.at_string_length);
6346 CHECKSUM_ATTR (attrs.at_threads_scaled);
6347 CHECKSUM_ATTR (attrs.at_upper_bound);
6348 CHECKSUM_ATTR (attrs.at_use_location);
6349 CHECKSUM_ATTR (attrs.at_use_UTF8);
6350 CHECKSUM_ATTR (attrs.at_variable_parameter);
6351 CHECKSUM_ATTR (attrs.at_virtuality);
6352 CHECKSUM_ATTR (attrs.at_visibility);
6353 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6354 CHECKSUM_ATTR (attrs.at_type);
6355 CHECKSUM_ATTR (attrs.at_friend);
6357 /* Checksum the child DIEs. */
6358 c = die->die_child;
6359 if (c) do {
6360 dw_attr_ref name_attr;
6362 c = c->die_sib;
6363 name_attr = get_AT (c, DW_AT_name);
6364 if (is_template_instantiation (c))
6366 /* Ignore instantiations of member type and function templates. */
6368 else if (name_attr != NULL
6369 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
6371 /* Use a shallow checksum for named nested types and member
6372 functions. */
6373 CHECKSUM_ULEB128 ('S');
6374 CHECKSUM_ULEB128 (c->die_tag);
6375 CHECKSUM_STRING (AT_string (name_attr));
6377 else
6379 /* Use a deep checksum for other children. */
6380 /* Mark this DIE so it gets processed when unmarking. */
6381 if (c->die_mark == 0)
6382 c->die_mark = -1;
6383 die_checksum_ordered (c, ctx, mark);
6385 } while (c != die->die_child);
6387 CHECKSUM_ULEB128 (0);
6390 /* Add a type name and tag to a hash. */
6391 static void
6392 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
6394 CHECKSUM_ULEB128 (tag);
6395 CHECKSUM_STRING (name);
6398 #undef CHECKSUM
6399 #undef CHECKSUM_STRING
6400 #undef CHECKSUM_ATTR
6401 #undef CHECKSUM_LEB128
6402 #undef CHECKSUM_ULEB128
6404 /* Generate the type signature for DIE. This is computed by generating an
6405 MD5 checksum over the DIE's tag, its relevant attributes, and its
6406 children. Attributes that are references to other DIEs are processed
6407 by recursion, using the MARK field to prevent infinite recursion.
6408 If the DIE is nested inside a namespace or another type, we also
6409 need to include that context in the signature. The lower 64 bits
6410 of the resulting MD5 checksum comprise the signature. */
6412 static void
6413 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6415 int mark;
6416 const char *name;
6417 unsigned char checksum[16];
6418 struct md5_ctx ctx;
6419 dw_die_ref decl;
6420 dw_die_ref parent;
6422 name = get_AT_string (die, DW_AT_name);
6423 decl = get_AT_ref (die, DW_AT_specification);
6424 parent = get_die_parent (die);
6426 /* First, compute a signature for just the type name (and its surrounding
6427 context, if any. This is stored in the type unit DIE for link-time
6428 ODR (one-definition rule) checking. */
6430 if (is_cxx () && name != NULL)
6432 md5_init_ctx (&ctx);
6434 /* Checksum the names of surrounding namespaces and structures. */
6435 if (parent != NULL)
6436 checksum_die_context (parent, &ctx);
6438 /* Checksum the current DIE. */
6439 die_odr_checksum (die->die_tag, name, &ctx);
6440 md5_finish_ctx (&ctx, checksum);
6442 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6445 /* Next, compute the complete type signature. */
6447 md5_init_ctx (&ctx);
6448 mark = 1;
6449 die->die_mark = mark;
6451 /* Checksum the names of surrounding namespaces and structures. */
6452 if (parent != NULL)
6453 checksum_die_context (parent, &ctx);
6455 /* Checksum the DIE and its children. */
6456 die_checksum_ordered (die, &ctx, &mark);
6457 unmark_all_dies (die);
6458 md5_finish_ctx (&ctx, checksum);
6460 /* Store the signature in the type node and link the type DIE and the
6461 type node together. */
6462 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6463 DWARF_TYPE_SIGNATURE_SIZE);
6464 die->comdat_type_p = true;
6465 die->die_id.die_type_node = type_node;
6466 type_node->type_die = die;
6468 /* If the DIE is a specification, link its declaration to the type node
6469 as well. */
6470 if (decl != NULL)
6472 decl->comdat_type_p = true;
6473 decl->die_id.die_type_node = type_node;
6477 /* Do the location expressions look same? */
6478 static inline int
6479 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6481 return loc1->dw_loc_opc == loc2->dw_loc_opc
6482 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6483 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6486 /* Do the values look the same? */
6487 static int
6488 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6490 dw_loc_descr_ref loc1, loc2;
6491 rtx r1, r2;
6493 if (v1->val_class != v2->val_class)
6494 return 0;
6496 switch (v1->val_class)
6498 case dw_val_class_const:
6499 return v1->v.val_int == v2->v.val_int;
6500 case dw_val_class_unsigned_const:
6501 return v1->v.val_unsigned == v2->v.val_unsigned;
6502 case dw_val_class_const_double:
6503 return v1->v.val_double.high == v2->v.val_double.high
6504 && v1->v.val_double.low == v2->v.val_double.low;
6505 case dw_val_class_wide_int:
6506 return *v1->v.val_wide == *v2->v.val_wide;
6507 case dw_val_class_vec:
6508 if (v1->v.val_vec.length != v2->v.val_vec.length
6509 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6510 return 0;
6511 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6512 v1->v.val_vec.length * v1->v.val_vec.elt_size))
6513 return 0;
6514 return 1;
6515 case dw_val_class_flag:
6516 return v1->v.val_flag == v2->v.val_flag;
6517 case dw_val_class_str:
6518 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
6520 case dw_val_class_addr:
6521 r1 = v1->v.val_addr;
6522 r2 = v2->v.val_addr;
6523 if (GET_CODE (r1) != GET_CODE (r2))
6524 return 0;
6525 return !rtx_equal_p (r1, r2);
6527 case dw_val_class_offset:
6528 return v1->v.val_offset == v2->v.val_offset;
6530 case dw_val_class_loc:
6531 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6532 loc1 && loc2;
6533 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6534 if (!same_loc_p (loc1, loc2, mark))
6535 return 0;
6536 return !loc1 && !loc2;
6538 case dw_val_class_die_ref:
6539 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6541 case dw_val_class_fde_ref:
6542 case dw_val_class_vms_delta:
6543 case dw_val_class_lbl_id:
6544 case dw_val_class_lineptr:
6545 case dw_val_class_macptr:
6546 case dw_val_class_high_pc:
6547 return 1;
6549 case dw_val_class_file:
6550 return v1->v.val_file == v2->v.val_file;
6552 case dw_val_class_data8:
6553 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
6555 default:
6556 return 1;
6560 /* Do the attributes look the same? */
6562 static int
6563 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6565 if (at1->dw_attr != at2->dw_attr)
6566 return 0;
6568 /* We don't care that this was compiled with a different compiler
6569 snapshot; if the output is the same, that's what matters. */
6570 if (at1->dw_attr == DW_AT_producer)
6571 return 1;
6573 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6576 /* Do the dies look the same? */
6578 static int
6579 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6581 dw_die_ref c1, c2;
6582 dw_attr_ref a1;
6583 unsigned ix;
6585 /* To avoid infinite recursion. */
6586 if (die1->die_mark)
6587 return die1->die_mark == die2->die_mark;
6588 die1->die_mark = die2->die_mark = ++(*mark);
6590 if (die1->die_tag != die2->die_tag)
6591 return 0;
6593 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
6594 return 0;
6596 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
6597 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
6598 return 0;
6600 c1 = die1->die_child;
6601 c2 = die2->die_child;
6602 if (! c1)
6604 if (c2)
6605 return 0;
6607 else
6608 for (;;)
6610 if (!same_die_p (c1, c2, mark))
6611 return 0;
6612 c1 = c1->die_sib;
6613 c2 = c2->die_sib;
6614 if (c1 == die1->die_child)
6616 if (c2 == die2->die_child)
6617 break;
6618 else
6619 return 0;
6623 return 1;
6626 /* Do the dies look the same? Wrapper around same_die_p. */
6628 static int
6629 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6631 int mark = 0;
6632 int ret = same_die_p (die1, die2, &mark);
6634 unmark_all_dies (die1);
6635 unmark_all_dies (die2);
6637 return ret;
6640 /* The prefix to attach to symbols on DIEs in the current comdat debug
6641 info section. */
6642 static const char *comdat_symbol_id;
6644 /* The index of the current symbol within the current comdat CU. */
6645 static unsigned int comdat_symbol_number;
6647 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6648 children, and set comdat_symbol_id accordingly. */
6650 static void
6651 compute_section_prefix (dw_die_ref unit_die)
6653 const char *die_name = get_AT_string (unit_die, DW_AT_name);
6654 const char *base = die_name ? lbasename (die_name) : "anonymous";
6655 char *name = XALLOCAVEC (char, strlen (base) + 64);
6656 char *p;
6657 int i, mark;
6658 unsigned char checksum[16];
6659 struct md5_ctx ctx;
6661 /* Compute the checksum of the DIE, then append part of it as hex digits to
6662 the name filename of the unit. */
6664 md5_init_ctx (&ctx);
6665 mark = 0;
6666 die_checksum (unit_die, &ctx, &mark);
6667 unmark_all_dies (unit_die);
6668 md5_finish_ctx (&ctx, checksum);
6670 sprintf (name, "%s.", base);
6671 clean_symbol_name (name);
6673 p = name + strlen (name);
6674 for (i = 0; i < 4; i++)
6676 sprintf (p, "%.2x", checksum[i]);
6677 p += 2;
6680 comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
6681 comdat_symbol_number = 0;
6684 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
6686 static int
6687 is_type_die (dw_die_ref die)
6689 switch (die->die_tag)
6691 case DW_TAG_array_type:
6692 case DW_TAG_class_type:
6693 case DW_TAG_interface_type:
6694 case DW_TAG_enumeration_type:
6695 case DW_TAG_pointer_type:
6696 case DW_TAG_reference_type:
6697 case DW_TAG_rvalue_reference_type:
6698 case DW_TAG_string_type:
6699 case DW_TAG_structure_type:
6700 case DW_TAG_subroutine_type:
6701 case DW_TAG_union_type:
6702 case DW_TAG_ptr_to_member_type:
6703 case DW_TAG_set_type:
6704 case DW_TAG_subrange_type:
6705 case DW_TAG_base_type:
6706 case DW_TAG_const_type:
6707 case DW_TAG_file_type:
6708 case DW_TAG_packed_type:
6709 case DW_TAG_volatile_type:
6710 case DW_TAG_typedef:
6711 return 1;
6712 default:
6713 return 0;
6717 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6718 Basically, we want to choose the bits that are likely to be shared between
6719 compilations (types) and leave out the bits that are specific to individual
6720 compilations (functions). */
6722 static int
6723 is_comdat_die (dw_die_ref c)
6725 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6726 we do for stabs. The advantage is a greater likelihood of sharing between
6727 objects that don't include headers in the same order (and therefore would
6728 put the base types in a different comdat). jason 8/28/00 */
6730 if (c->die_tag == DW_TAG_base_type)
6731 return 0;
6733 if (c->die_tag == DW_TAG_pointer_type
6734 || c->die_tag == DW_TAG_reference_type
6735 || c->die_tag == DW_TAG_rvalue_reference_type
6736 || c->die_tag == DW_TAG_const_type
6737 || c->die_tag == DW_TAG_volatile_type)
6739 dw_die_ref t = get_AT_ref (c, DW_AT_type);
6741 return t ? is_comdat_die (t) : 0;
6744 return is_type_die (c);
6747 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6748 compilation unit. */
6750 static int
6751 is_symbol_die (dw_die_ref c)
6753 return (is_type_die (c)
6754 || is_declaration_die (c)
6755 || c->die_tag == DW_TAG_namespace
6756 || c->die_tag == DW_TAG_module);
6759 /* Returns true iff C is a compile-unit DIE. */
6761 static inline bool
6762 is_cu_die (dw_die_ref c)
6764 return c && c->die_tag == DW_TAG_compile_unit;
6767 /* Returns true iff C is a unit DIE of some sort. */
6769 static inline bool
6770 is_unit_die (dw_die_ref c)
6772 return c && (c->die_tag == DW_TAG_compile_unit
6773 || c->die_tag == DW_TAG_partial_unit
6774 || c->die_tag == DW_TAG_type_unit);
6777 /* Returns true iff C is a namespace DIE. */
6779 static inline bool
6780 is_namespace_die (dw_die_ref c)
6782 return c && c->die_tag == DW_TAG_namespace;
6785 /* Returns true iff C is a class or structure DIE. */
6787 static inline bool
6788 is_class_die (dw_die_ref c)
6790 return c && (c->die_tag == DW_TAG_class_type
6791 || c->die_tag == DW_TAG_structure_type);
6794 /* Return non-zero if this DIE is a template parameter. */
6796 static inline bool
6797 is_template_parameter (dw_die_ref die)
6799 switch (die->die_tag)
6801 case DW_TAG_template_type_param:
6802 case DW_TAG_template_value_param:
6803 case DW_TAG_GNU_template_template_param:
6804 case DW_TAG_GNU_template_parameter_pack:
6805 return true;
6806 default:
6807 return false;
6811 /* Return non-zero if this DIE represents a template instantiation. */
6813 static inline bool
6814 is_template_instantiation (dw_die_ref die)
6816 dw_die_ref c;
6818 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
6819 return false;
6820 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
6821 return false;
6824 static char *
6825 gen_internal_sym (const char *prefix)
6827 char buf[256];
6829 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6830 return xstrdup (buf);
6833 /* Assign symbols to all worthy DIEs under DIE. */
6835 static void
6836 assign_symbol_names (dw_die_ref die)
6838 dw_die_ref c;
6840 if (is_symbol_die (die) && !die->comdat_type_p)
6842 if (comdat_symbol_id)
6844 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
6846 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6847 comdat_symbol_id, comdat_symbol_number++);
6848 die->die_id.die_symbol = xstrdup (p);
6850 else
6851 die->die_id.die_symbol = gen_internal_sym ("LDIE");
6854 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6857 struct cu_hash_table_entry
6859 dw_die_ref cu;
6860 unsigned min_comdat_num, max_comdat_num;
6861 struct cu_hash_table_entry *next;
6864 /* Helpers to manipulate hash table of CUs. */
6866 struct cu_hash_table_entry_hasher
6868 typedef cu_hash_table_entry value_type;
6869 typedef die_struct compare_type;
6870 static inline hashval_t hash (const value_type *);
6871 static inline bool equal (const value_type *, const compare_type *);
6872 static inline void remove (value_type *);
6875 inline hashval_t
6876 cu_hash_table_entry_hasher::hash (const value_type *entry)
6878 return htab_hash_string (entry->cu->die_id.die_symbol);
6881 inline bool
6882 cu_hash_table_entry_hasher::equal (const value_type *entry1,
6883 const compare_type *entry2)
6885 return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
6888 inline void
6889 cu_hash_table_entry_hasher::remove (value_type *entry)
6891 struct cu_hash_table_entry *next;
6893 while (entry)
6895 next = entry->next;
6896 free (entry);
6897 entry = next;
6901 typedef hash_table<cu_hash_table_entry_hasher> cu_hash_type;
6903 /* Check whether we have already seen this CU and set up SYM_NUM
6904 accordingly. */
6905 static int
6906 check_duplicate_cu (dw_die_ref cu, cu_hash_type *htable, unsigned int *sym_num)
6908 struct cu_hash_table_entry dummy;
6909 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6911 dummy.max_comdat_num = 0;
6913 slot = htable->find_slot_with_hash (cu,
6914 htab_hash_string (cu->die_id.die_symbol),
6915 INSERT);
6916 entry = *slot;
6918 for (; entry; last = entry, entry = entry->next)
6920 if (same_die_p_wrap (cu, entry->cu))
6921 break;
6924 if (entry)
6926 *sym_num = entry->min_comdat_num;
6927 return 1;
6930 entry = XCNEW (struct cu_hash_table_entry);
6931 entry->cu = cu;
6932 entry->min_comdat_num = *sym_num = last->max_comdat_num;
6933 entry->next = *slot;
6934 *slot = entry;
6936 return 0;
6939 /* Record SYM_NUM to record of CU in HTABLE. */
6940 static void
6941 record_comdat_symbol_number (dw_die_ref cu, cu_hash_type *htable,
6942 unsigned int sym_num)
6944 struct cu_hash_table_entry **slot, *entry;
6946 slot = htable->find_slot_with_hash (cu,
6947 htab_hash_string (cu->die_id.die_symbol),
6948 NO_INSERT);
6949 entry = *slot;
6951 entry->max_comdat_num = sym_num;
6954 /* Traverse the DIE (which is always comp_unit_die), and set up
6955 additional compilation units for each of the include files we see
6956 bracketed by BINCL/EINCL. */
6958 static void
6959 break_out_includes (dw_die_ref die)
6961 dw_die_ref c;
6962 dw_die_ref unit = NULL;
6963 limbo_die_node *node, **pnode;
6965 c = die->die_child;
6966 if (c) do {
6967 dw_die_ref prev = c;
6968 c = c->die_sib;
6969 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6970 || (unit && is_comdat_die (c)))
6972 dw_die_ref next = c->die_sib;
6974 /* This DIE is for a secondary CU; remove it from the main one. */
6975 remove_child_with_prev (c, prev);
6977 if (c->die_tag == DW_TAG_GNU_BINCL)
6978 unit = push_new_compile_unit (unit, c);
6979 else if (c->die_tag == DW_TAG_GNU_EINCL)
6980 unit = pop_compile_unit (unit);
6981 else
6982 add_child_die (unit, c);
6983 c = next;
6984 if (c == die->die_child)
6985 break;
6987 } while (c != die->die_child);
6989 #if 0
6990 /* We can only use this in debugging, since the frontend doesn't check
6991 to make sure that we leave every include file we enter. */
6992 gcc_assert (!unit);
6993 #endif
6995 assign_symbol_names (die);
6996 cu_hash_type cu_hash_table (10);
6997 for (node = limbo_die_list, pnode = &limbo_die_list;
6998 node;
6999 node = node->next)
7001 int is_dupl;
7003 compute_section_prefix (node->die);
7004 is_dupl = check_duplicate_cu (node->die, &cu_hash_table,
7005 &comdat_symbol_number);
7006 assign_symbol_names (node->die);
7007 if (is_dupl)
7008 *pnode = node->next;
7009 else
7011 pnode = &node->next;
7012 record_comdat_symbol_number (node->die, &cu_hash_table,
7013 comdat_symbol_number);
7018 /* Return non-zero if this DIE is a declaration. */
7020 static int
7021 is_declaration_die (dw_die_ref die)
7023 dw_attr_ref a;
7024 unsigned ix;
7026 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7027 if (a->dw_attr == DW_AT_declaration)
7028 return 1;
7030 return 0;
7033 /* Return non-zero if this DIE is nested inside a subprogram. */
7035 static int
7036 is_nested_in_subprogram (dw_die_ref die)
7038 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
7040 if (decl == NULL)
7041 decl = die;
7042 return local_scope_p (decl);
7045 /* Return non-zero if this DIE contains a defining declaration of a
7046 subprogram. */
7048 static int
7049 contains_subprogram_definition (dw_die_ref die)
7051 dw_die_ref c;
7053 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
7054 return 1;
7055 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
7056 return 0;
7059 /* Return non-zero if this is a type DIE that should be moved to a
7060 COMDAT .debug_types section. */
7062 static int
7063 should_move_die_to_comdat (dw_die_ref die)
7065 switch (die->die_tag)
7067 case DW_TAG_class_type:
7068 case DW_TAG_structure_type:
7069 case DW_TAG_enumeration_type:
7070 case DW_TAG_union_type:
7071 /* Don't move declarations, inlined instances, types nested in a
7072 subprogram, or types that contain subprogram definitions. */
7073 if (is_declaration_die (die)
7074 || get_AT (die, DW_AT_abstract_origin)
7075 || is_nested_in_subprogram (die)
7076 || contains_subprogram_definition (die))
7077 return 0;
7078 return 1;
7079 case DW_TAG_array_type:
7080 case DW_TAG_interface_type:
7081 case DW_TAG_pointer_type:
7082 case DW_TAG_reference_type:
7083 case DW_TAG_rvalue_reference_type:
7084 case DW_TAG_string_type:
7085 case DW_TAG_subroutine_type:
7086 case DW_TAG_ptr_to_member_type:
7087 case DW_TAG_set_type:
7088 case DW_TAG_subrange_type:
7089 case DW_TAG_base_type:
7090 case DW_TAG_const_type:
7091 case DW_TAG_file_type:
7092 case DW_TAG_packed_type:
7093 case DW_TAG_volatile_type:
7094 case DW_TAG_typedef:
7095 default:
7096 return 0;
7100 /* Make a clone of DIE. */
7102 static dw_die_ref
7103 clone_die (dw_die_ref die)
7105 dw_die_ref clone;
7106 dw_attr_ref a;
7107 unsigned ix;
7109 clone = ggc_cleared_alloc<die_node> ();
7110 clone->die_tag = die->die_tag;
7112 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7113 add_dwarf_attr (clone, a);
7115 return clone;
7118 /* Make a clone of the tree rooted at DIE. */
7120 static dw_die_ref
7121 clone_tree (dw_die_ref die)
7123 dw_die_ref c;
7124 dw_die_ref clone = clone_die (die);
7126 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
7128 return clone;
7131 /* Make a clone of DIE as a declaration. */
7133 static dw_die_ref
7134 clone_as_declaration (dw_die_ref die)
7136 dw_die_ref clone;
7137 dw_die_ref decl;
7138 dw_attr_ref a;
7139 unsigned ix;
7141 /* If the DIE is already a declaration, just clone it. */
7142 if (is_declaration_die (die))
7143 return clone_die (die);
7145 /* If the DIE is a specification, just clone its declaration DIE. */
7146 decl = get_AT_ref (die, DW_AT_specification);
7147 if (decl != NULL)
7149 clone = clone_die (decl);
7150 if (die->comdat_type_p)
7151 add_AT_die_ref (clone, DW_AT_signature, die);
7152 return clone;
7155 clone = ggc_cleared_alloc<die_node> ();
7156 clone->die_tag = die->die_tag;
7158 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7160 /* We don't want to copy over all attributes.
7161 For example we don't want DW_AT_byte_size because otherwise we will no
7162 longer have a declaration and GDB will treat it as a definition. */
7164 switch (a->dw_attr)
7166 case DW_AT_abstract_origin:
7167 case DW_AT_artificial:
7168 case DW_AT_containing_type:
7169 case DW_AT_external:
7170 case DW_AT_name:
7171 case DW_AT_type:
7172 case DW_AT_virtuality:
7173 case DW_AT_linkage_name:
7174 case DW_AT_MIPS_linkage_name:
7175 add_dwarf_attr (clone, a);
7176 break;
7177 case DW_AT_byte_size:
7178 default:
7179 break;
7183 if (die->comdat_type_p)
7184 add_AT_die_ref (clone, DW_AT_signature, die);
7186 add_AT_flag (clone, DW_AT_declaration, 1);
7187 return clone;
7191 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
7193 struct decl_table_entry
7195 dw_die_ref orig;
7196 dw_die_ref copy;
7199 /* Helpers to manipulate hash table of copied declarations. */
7201 /* Hashtable helpers. */
7203 struct decl_table_entry_hasher : typed_free_remove <decl_table_entry>
7205 typedef decl_table_entry value_type;
7206 typedef die_struct compare_type;
7207 static inline hashval_t hash (const value_type *);
7208 static inline bool equal (const value_type *, const compare_type *);
7211 inline hashval_t
7212 decl_table_entry_hasher::hash (const value_type *entry)
7214 return htab_hash_pointer (entry->orig);
7217 inline bool
7218 decl_table_entry_hasher::equal (const value_type *entry1,
7219 const compare_type *entry2)
7221 return entry1->orig == entry2;
7224 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
7226 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7227 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
7228 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
7229 to check if the ancestor has already been copied into UNIT. */
7231 static dw_die_ref
7232 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
7233 decl_hash_type *decl_table)
7235 dw_die_ref parent = die->die_parent;
7236 dw_die_ref new_parent = unit;
7237 dw_die_ref copy;
7238 decl_table_entry **slot = NULL;
7239 struct decl_table_entry *entry = NULL;
7241 if (decl_table)
7243 /* Check if the entry has already been copied to UNIT. */
7244 slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
7245 INSERT);
7246 if (*slot != HTAB_EMPTY_ENTRY)
7248 entry = *slot;
7249 return entry->copy;
7252 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
7253 entry = XCNEW (struct decl_table_entry);
7254 entry->orig = die;
7255 entry->copy = NULL;
7256 *slot = entry;
7259 if (parent != NULL)
7261 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7262 if (spec != NULL)
7263 parent = spec;
7264 if (!is_unit_die (parent))
7265 new_parent = copy_ancestor_tree (unit, parent, decl_table);
7268 copy = clone_as_declaration (die);
7269 add_child_die (new_parent, copy);
7271 if (decl_table)
7273 /* Record the pointer to the copy. */
7274 entry->copy = copy;
7277 return copy;
7279 /* Copy the declaration context to the new type unit DIE. This includes
7280 any surrounding namespace or type declarations. If the DIE has an
7281 AT_specification attribute, it also includes attributes and children
7282 attached to the specification, and returns a pointer to the original
7283 parent of the declaration DIE. Returns NULL otherwise. */
7285 static dw_die_ref
7286 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7288 dw_die_ref decl;
7289 dw_die_ref new_decl;
7290 dw_die_ref orig_parent = NULL;
7292 decl = get_AT_ref (die, DW_AT_specification);
7293 if (decl == NULL)
7294 decl = die;
7295 else
7297 unsigned ix;
7298 dw_die_ref c;
7299 dw_attr_ref a;
7301 /* The original DIE will be changed to a declaration, and must
7302 be moved to be a child of the original declaration DIE. */
7303 orig_parent = decl->die_parent;
7305 /* Copy the type node pointer from the new DIE to the original
7306 declaration DIE so we can forward references later. */
7307 decl->comdat_type_p = true;
7308 decl->die_id.die_type_node = die->die_id.die_type_node;
7310 remove_AT (die, DW_AT_specification);
7312 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7314 if (a->dw_attr != DW_AT_name
7315 && a->dw_attr != DW_AT_declaration
7316 && a->dw_attr != DW_AT_external)
7317 add_dwarf_attr (die, a);
7320 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7323 if (decl->die_parent != NULL
7324 && !is_unit_die (decl->die_parent))
7326 new_decl = copy_ancestor_tree (unit, decl, NULL);
7327 if (new_decl != NULL)
7329 remove_AT (new_decl, DW_AT_signature);
7330 add_AT_specification (die, new_decl);
7334 return orig_parent;
7337 /* Generate the skeleton ancestor tree for the given NODE, then clone
7338 the DIE and add the clone into the tree. */
7340 static void
7341 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7343 if (node->new_die != NULL)
7344 return;
7346 node->new_die = clone_as_declaration (node->old_die);
7348 if (node->parent != NULL)
7350 generate_skeleton_ancestor_tree (node->parent);
7351 add_child_die (node->parent->new_die, node->new_die);
7355 /* Generate a skeleton tree of DIEs containing any declarations that are
7356 found in the original tree. We traverse the tree looking for declaration
7357 DIEs, and construct the skeleton from the bottom up whenever we find one. */
7359 static void
7360 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7362 skeleton_chain_node node;
7363 dw_die_ref c;
7364 dw_die_ref first;
7365 dw_die_ref prev = NULL;
7366 dw_die_ref next = NULL;
7368 node.parent = parent;
7370 first = c = parent->old_die->die_child;
7371 if (c)
7372 next = c->die_sib;
7373 if (c) do {
7374 if (prev == NULL || prev->die_sib == c)
7375 prev = c;
7376 c = next;
7377 next = (c == first ? NULL : c->die_sib);
7378 node.old_die = c;
7379 node.new_die = NULL;
7380 if (is_declaration_die (c))
7382 if (is_template_instantiation (c))
7384 /* Instantiated templates do not need to be cloned into the
7385 type unit. Just move the DIE and its children back to
7386 the skeleton tree (in the main CU). */
7387 remove_child_with_prev (c, prev);
7388 add_child_die (parent->new_die, c);
7389 c = prev;
7391 else
7393 /* Clone the existing DIE, move the original to the skeleton
7394 tree (which is in the main CU), and put the clone, with
7395 all the original's children, where the original came from
7396 (which is about to be moved to the type unit). */
7397 dw_die_ref clone = clone_die (c);
7398 move_all_children (c, clone);
7400 /* If the original has a DW_AT_object_pointer attribute,
7401 it would now point to a child DIE just moved to the
7402 cloned tree, so we need to remove that attribute from
7403 the original. */
7404 remove_AT (c, DW_AT_object_pointer);
7406 replace_child (c, clone, prev);
7407 generate_skeleton_ancestor_tree (parent);
7408 add_child_die (parent->new_die, c);
7409 node.new_die = c;
7410 c = clone;
7413 generate_skeleton_bottom_up (&node);
7414 } while (next != NULL);
7417 /* Wrapper function for generate_skeleton_bottom_up. */
7419 static dw_die_ref
7420 generate_skeleton (dw_die_ref die)
7422 skeleton_chain_node node;
7424 node.old_die = die;
7425 node.new_die = NULL;
7426 node.parent = NULL;
7428 /* If this type definition is nested inside another type,
7429 and is not an instantiation of a template, always leave
7430 at least a declaration in its place. */
7431 if (die->die_parent != NULL
7432 && is_type_die (die->die_parent)
7433 && !is_template_instantiation (die))
7434 node.new_die = clone_as_declaration (die);
7436 generate_skeleton_bottom_up (&node);
7437 return node.new_die;
7440 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
7441 declaration. The original DIE is moved to a new compile unit so that
7442 existing references to it follow it to the new location. If any of the
7443 original DIE's descendants is a declaration, we need to replace the
7444 original DIE with a skeleton tree and move the declarations back into the
7445 skeleton tree. */
7447 static dw_die_ref
7448 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
7449 dw_die_ref prev)
7451 dw_die_ref skeleton, orig_parent;
7453 /* Copy the declaration context to the type unit DIE. If the returned
7454 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
7455 that DIE. */
7456 orig_parent = copy_declaration_context (unit, child);
7458 skeleton = generate_skeleton (child);
7459 if (skeleton == NULL)
7460 remove_child_with_prev (child, prev);
7461 else
7463 skeleton->comdat_type_p = true;
7464 skeleton->die_id.die_type_node = child->die_id.die_type_node;
7466 /* If the original DIE was a specification, we need to put
7467 the skeleton under the parent DIE of the declaration.
7468 This leaves the original declaration in the tree, but
7469 it will be pruned later since there are no longer any
7470 references to it. */
7471 if (orig_parent != NULL)
7473 remove_child_with_prev (child, prev);
7474 add_child_die (orig_parent, skeleton);
7476 else
7477 replace_child (child, skeleton, prev);
7480 return skeleton;
7483 /* Traverse the DIE and set up additional .debug_types sections for each
7484 type worthy of being placed in a COMDAT section. */
7486 static void
7487 break_out_comdat_types (dw_die_ref die)
7489 dw_die_ref c;
7490 dw_die_ref first;
7491 dw_die_ref prev = NULL;
7492 dw_die_ref next = NULL;
7493 dw_die_ref unit = NULL;
7495 first = c = die->die_child;
7496 if (c)
7497 next = c->die_sib;
7498 if (c) do {
7499 if (prev == NULL || prev->die_sib == c)
7500 prev = c;
7501 c = next;
7502 next = (c == first ? NULL : c->die_sib);
7503 if (should_move_die_to_comdat (c))
7505 dw_die_ref replacement;
7506 comdat_type_node_ref type_node;
7508 /* Break out nested types into their own type units. */
7509 break_out_comdat_types (c);
7511 /* Create a new type unit DIE as the root for the new tree, and
7512 add it to the list of comdat types. */
7513 unit = new_die (DW_TAG_type_unit, NULL, NULL);
7514 add_AT_unsigned (unit, DW_AT_language,
7515 get_AT_unsigned (comp_unit_die (), DW_AT_language));
7516 type_node = ggc_cleared_alloc<comdat_type_node> ();
7517 type_node->root_die = unit;
7518 type_node->next = comdat_type_list;
7519 comdat_type_list = type_node;
7521 /* Generate the type signature. */
7522 generate_type_signature (c, type_node);
7524 /* Copy the declaration context, attributes, and children of the
7525 declaration into the new type unit DIE, then remove this DIE
7526 from the main CU (or replace it with a skeleton if necessary). */
7527 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
7528 type_node->skeleton_die = replacement;
7530 /* Add the DIE to the new compunit. */
7531 add_child_die (unit, c);
7533 if (replacement != NULL)
7534 c = replacement;
7536 else if (c->die_tag == DW_TAG_namespace
7537 || c->die_tag == DW_TAG_class_type
7538 || c->die_tag == DW_TAG_structure_type
7539 || c->die_tag == DW_TAG_union_type)
7541 /* Look for nested types that can be broken out. */
7542 break_out_comdat_types (c);
7544 } while (next != NULL);
7547 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
7548 Enter all the cloned children into the hash table decl_table. */
7550 static dw_die_ref
7551 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
7553 dw_die_ref c;
7554 dw_die_ref clone;
7555 struct decl_table_entry *entry;
7556 decl_table_entry **slot;
7558 if (die->die_tag == DW_TAG_subprogram)
7559 clone = clone_as_declaration (die);
7560 else
7561 clone = clone_die (die);
7563 slot = decl_table->find_slot_with_hash (die,
7564 htab_hash_pointer (die), INSERT);
7566 /* Assert that DIE isn't in the hash table yet. If it would be there
7567 before, the ancestors would be necessarily there as well, therefore
7568 clone_tree_partial wouldn't be called. */
7569 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
7571 entry = XCNEW (struct decl_table_entry);
7572 entry->orig = die;
7573 entry->copy = clone;
7574 *slot = entry;
7576 if (die->die_tag != DW_TAG_subprogram)
7577 FOR_EACH_CHILD (die, c,
7578 add_child_die (clone, clone_tree_partial (c, decl_table)));
7580 return clone;
7583 /* Walk the DIE and its children, looking for references to incomplete
7584 or trivial types that are unmarked (i.e., that are not in the current
7585 type_unit). */
7587 static void
7588 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
7590 dw_die_ref c;
7591 dw_attr_ref a;
7592 unsigned ix;
7594 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7596 if (AT_class (a) == dw_val_class_die_ref)
7598 dw_die_ref targ = AT_ref (a);
7599 decl_table_entry **slot;
7600 struct decl_table_entry *entry;
7602 if (targ->die_mark != 0 || targ->comdat_type_p)
7603 continue;
7605 slot = decl_table->find_slot_with_hash (targ,
7606 htab_hash_pointer (targ),
7607 INSERT);
7609 if (*slot != HTAB_EMPTY_ENTRY)
7611 /* TARG has already been copied, so we just need to
7612 modify the reference to point to the copy. */
7613 entry = *slot;
7614 a->dw_attr_val.v.val_die_ref.die = entry->copy;
7616 else
7618 dw_die_ref parent = unit;
7619 dw_die_ref copy = clone_die (targ);
7621 /* Record in DECL_TABLE that TARG has been copied.
7622 Need to do this now, before the recursive call,
7623 because DECL_TABLE may be expanded and SLOT
7624 would no longer be a valid pointer. */
7625 entry = XCNEW (struct decl_table_entry);
7626 entry->orig = targ;
7627 entry->copy = copy;
7628 *slot = entry;
7630 /* If TARG is not a declaration DIE, we need to copy its
7631 children. */
7632 if (!is_declaration_die (targ))
7634 FOR_EACH_CHILD (
7635 targ, c,
7636 add_child_die (copy,
7637 clone_tree_partial (c, decl_table)));
7640 /* Make sure the cloned tree is marked as part of the
7641 type unit. */
7642 mark_dies (copy);
7644 /* If TARG has surrounding context, copy its ancestor tree
7645 into the new type unit. */
7646 if (targ->die_parent != NULL
7647 && !is_unit_die (targ->die_parent))
7648 parent = copy_ancestor_tree (unit, targ->die_parent,
7649 decl_table);
7651 add_child_die (parent, copy);
7652 a->dw_attr_val.v.val_die_ref.die = copy;
7654 /* Make sure the newly-copied DIE is walked. If it was
7655 installed in a previously-added context, it won't
7656 get visited otherwise. */
7657 if (parent != unit)
7659 /* Find the highest point of the newly-added tree,
7660 mark each node along the way, and walk from there. */
7661 parent->die_mark = 1;
7662 while (parent->die_parent
7663 && parent->die_parent->die_mark == 0)
7665 parent = parent->die_parent;
7666 parent->die_mark = 1;
7668 copy_decls_walk (unit, parent, decl_table);
7674 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
7677 /* Copy declarations for "unworthy" types into the new comdat section.
7678 Incomplete types, modified types, and certain other types aren't broken
7679 out into comdat sections of their own, so they don't have a signature,
7680 and we need to copy the declaration into the same section so that we
7681 don't have an external reference. */
7683 static void
7684 copy_decls_for_unworthy_types (dw_die_ref unit)
7686 mark_dies (unit);
7687 decl_hash_type decl_table (10);
7688 copy_decls_walk (unit, unit, &decl_table);
7689 unmark_dies (unit);
7692 /* Traverse the DIE and add a sibling attribute if it may have the
7693 effect of speeding up access to siblings. To save some space,
7694 avoid generating sibling attributes for DIE's without children. */
7696 static void
7697 add_sibling_attributes (dw_die_ref die)
7699 dw_die_ref c;
7701 if (! die->die_child)
7702 return;
7704 if (die->die_parent && die != die->die_parent->die_child)
7705 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7707 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7710 /* Output all location lists for the DIE and its children. */
7712 static void
7713 output_location_lists (dw_die_ref die)
7715 dw_die_ref c;
7716 dw_attr_ref a;
7717 unsigned ix;
7719 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7720 if (AT_class (a) == dw_val_class_loc_list)
7721 output_loc_list (AT_loc_list (a));
7723 FOR_EACH_CHILD (die, c, output_location_lists (c));
7726 /* We want to limit the number of external references, because they are
7727 larger than local references: a relocation takes multiple words, and
7728 even a sig8 reference is always eight bytes, whereas a local reference
7729 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
7730 So if we encounter multiple external references to the same type DIE, we
7731 make a local typedef stub for it and redirect all references there.
7733 This is the element of the hash table for keeping track of these
7734 references. */
7736 struct external_ref
7738 dw_die_ref type;
7739 dw_die_ref stub;
7740 unsigned n_refs;
7743 /* Hashtable helpers. */
7745 struct external_ref_hasher : typed_free_remove <external_ref>
7747 typedef external_ref value_type;
7748 typedef external_ref compare_type;
7749 static inline hashval_t hash (const value_type *);
7750 static inline bool equal (const value_type *, const compare_type *);
7753 inline hashval_t
7754 external_ref_hasher::hash (const value_type *r)
7756 dw_die_ref die = r->type;
7757 hashval_t h = 0;
7759 /* We can't use the address of the DIE for hashing, because
7760 that will make the order of the stub DIEs non-deterministic. */
7761 if (! die->comdat_type_p)
7762 /* We have a symbol; use it to compute a hash. */
7763 h = htab_hash_string (die->die_id.die_symbol);
7764 else
7766 /* We have a type signature; use a subset of the bits as the hash.
7767 The 8-byte signature is at least as large as hashval_t. */
7768 comdat_type_node_ref type_node = die->die_id.die_type_node;
7769 memcpy (&h, type_node->signature, sizeof (h));
7771 return h;
7774 inline bool
7775 external_ref_hasher::equal (const value_type *r1, const compare_type *r2)
7777 return r1->type == r2->type;
7780 typedef hash_table<external_ref_hasher> external_ref_hash_type;
7782 /* Return a pointer to the external_ref for references to DIE. */
7784 static struct external_ref *
7785 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
7787 struct external_ref ref, *ref_p;
7788 external_ref **slot;
7790 ref.type = die;
7791 slot = map->find_slot (&ref, INSERT);
7792 if (*slot != HTAB_EMPTY_ENTRY)
7793 return *slot;
7795 ref_p = XCNEW (struct external_ref);
7796 ref_p->type = die;
7797 *slot = ref_p;
7798 return ref_p;
7801 /* Subroutine of optimize_external_refs, below.
7803 If we see a type skeleton, record it as our stub. If we see external
7804 references, remember how many we've seen. */
7806 static void
7807 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
7809 dw_die_ref c;
7810 dw_attr_ref a;
7811 unsigned ix;
7812 struct external_ref *ref_p;
7814 if (is_type_die (die)
7815 && (c = get_AT_ref (die, DW_AT_signature)))
7817 /* This is a local skeleton; use it for local references. */
7818 ref_p = lookup_external_ref (map, c);
7819 ref_p->stub = die;
7822 /* Scan the DIE references, and remember any that refer to DIEs from
7823 other CUs (i.e. those which are not marked). */
7824 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7825 if (AT_class (a) == dw_val_class_die_ref
7826 && (c = AT_ref (a))->die_mark == 0
7827 && is_type_die (c))
7829 ref_p = lookup_external_ref (map, c);
7830 ref_p->n_refs++;
7833 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
7836 /* htab_traverse callback function for optimize_external_refs, below. SLOT
7837 points to an external_ref, DATA is the CU we're processing. If we don't
7838 already have a local stub, and we have multiple refs, build a stub. */
7841 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
7843 struct external_ref *ref_p = *slot;
7845 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
7847 /* We have multiple references to this type, so build a small stub.
7848 Both of these forms are a bit dodgy from the perspective of the
7849 DWARF standard, since technically they should have names. */
7850 dw_die_ref cu = data;
7851 dw_die_ref type = ref_p->type;
7852 dw_die_ref stub = NULL;
7854 if (type->comdat_type_p)
7856 /* If we refer to this type via sig8, use AT_signature. */
7857 stub = new_die (type->die_tag, cu, NULL_TREE);
7858 add_AT_die_ref (stub, DW_AT_signature, type);
7860 else
7862 /* Otherwise, use a typedef with no name. */
7863 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
7864 add_AT_die_ref (stub, DW_AT_type, type);
7867 stub->die_mark++;
7868 ref_p->stub = stub;
7870 return 1;
7873 /* DIE is a unit; look through all the DIE references to see if there are
7874 any external references to types, and if so, create local stubs for
7875 them which will be applied in build_abbrev_table. This is useful because
7876 references to local DIEs are smaller. */
7878 static external_ref_hash_type *
7879 optimize_external_refs (dw_die_ref die)
7881 external_ref_hash_type *map = new external_ref_hash_type (10);
7882 optimize_external_refs_1 (die, map);
7883 map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
7884 return map;
7887 /* The format of each DIE (and its attribute value pairs) is encoded in an
7888 abbreviation table. This routine builds the abbreviation table and assigns
7889 a unique abbreviation id for each abbreviation entry. The children of each
7890 die are visited recursively. */
7892 static void
7893 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
7895 unsigned long abbrev_id;
7896 unsigned int n_alloc;
7897 dw_die_ref c;
7898 dw_attr_ref a;
7899 unsigned ix;
7901 /* Scan the DIE references, and replace any that refer to
7902 DIEs from other CUs (i.e. those which are not marked) with
7903 the local stubs we built in optimize_external_refs. */
7904 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7905 if (AT_class (a) == dw_val_class_die_ref
7906 && (c = AT_ref (a))->die_mark == 0)
7908 struct external_ref *ref_p;
7909 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
7911 ref_p = lookup_external_ref (extern_map, c);
7912 if (ref_p->stub && ref_p->stub != die)
7913 change_AT_die_ref (a, ref_p->stub);
7914 else
7915 /* We aren't changing this reference, so mark it external. */
7916 set_AT_ref_external (a, 1);
7919 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7921 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7922 dw_attr_ref die_a, abbrev_a;
7923 unsigned ix;
7924 bool ok = true;
7926 if (abbrev->die_tag != die->die_tag)
7927 continue;
7928 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7929 continue;
7931 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
7932 continue;
7934 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
7936 abbrev_a = &(*abbrev->die_attr)[ix];
7937 if ((abbrev_a->dw_attr != die_a->dw_attr)
7938 || (value_format (abbrev_a) != value_format (die_a)))
7940 ok = false;
7941 break;
7944 if (ok)
7945 break;
7948 if (abbrev_id >= abbrev_die_table_in_use)
7950 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7952 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
7953 abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7954 n_alloc);
7956 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7957 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
7958 abbrev_die_table_allocated = n_alloc;
7961 ++abbrev_die_table_in_use;
7962 abbrev_die_table[abbrev_id] = die;
7965 die->die_abbrev = abbrev_id;
7966 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
7969 /* Return the power-of-two number of bytes necessary to represent VALUE. */
7971 static int
7972 constant_size (unsigned HOST_WIDE_INT value)
7974 int log;
7976 if (value == 0)
7977 log = 0;
7978 else
7979 log = floor_log2 (value);
7981 log = log / 8;
7982 log = 1 << (floor_log2 (log) + 1);
7984 return log;
7987 /* Return the size of a DIE as it is represented in the
7988 .debug_info section. */
7990 static unsigned long
7991 size_of_die (dw_die_ref die)
7993 unsigned long size = 0;
7994 dw_attr_ref a;
7995 unsigned ix;
7996 enum dwarf_form form;
7998 size += size_of_uleb128 (die->die_abbrev);
7999 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8001 switch (AT_class (a))
8003 case dw_val_class_addr:
8004 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8006 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8007 size += size_of_uleb128 (AT_index (a));
8009 else
8010 size += DWARF2_ADDR_SIZE;
8011 break;
8012 case dw_val_class_offset:
8013 size += DWARF_OFFSET_SIZE;
8014 break;
8015 case dw_val_class_loc:
8017 unsigned long lsize = size_of_locs (AT_loc (a));
8019 /* Block length. */
8020 if (dwarf_version >= 4)
8021 size += size_of_uleb128 (lsize);
8022 else
8023 size += constant_size (lsize);
8024 size += lsize;
8026 break;
8027 case dw_val_class_loc_list:
8028 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8030 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8031 size += size_of_uleb128 (AT_index (a));
8033 else
8034 size += DWARF_OFFSET_SIZE;
8035 break;
8036 case dw_val_class_range_list:
8037 size += DWARF_OFFSET_SIZE;
8038 break;
8039 case dw_val_class_const:
8040 size += size_of_sleb128 (AT_int (a));
8041 break;
8042 case dw_val_class_unsigned_const:
8044 int csize = constant_size (AT_unsigned (a));
8045 if (dwarf_version == 3
8046 && a->dw_attr == DW_AT_data_member_location
8047 && csize >= 4)
8048 size += size_of_uleb128 (AT_unsigned (a));
8049 else
8050 size += csize;
8052 break;
8053 case dw_val_class_const_double:
8054 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
8055 if (HOST_BITS_PER_WIDE_INT >= 64)
8056 size++; /* block */
8057 break;
8058 case dw_val_class_wide_int:
8059 size += (get_full_len (*a->dw_attr_val.v.val_wide)
8060 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
8061 if (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT
8062 > 64)
8063 size++; /* block */
8064 break;
8065 case dw_val_class_vec:
8066 size += constant_size (a->dw_attr_val.v.val_vec.length
8067 * a->dw_attr_val.v.val_vec.elt_size)
8068 + a->dw_attr_val.v.val_vec.length
8069 * a->dw_attr_val.v.val_vec.elt_size; /* block */
8070 break;
8071 case dw_val_class_flag:
8072 if (dwarf_version >= 4)
8073 /* Currently all add_AT_flag calls pass in 1 as last argument,
8074 so DW_FORM_flag_present can be used. If that ever changes,
8075 we'll need to use DW_FORM_flag and have some optimization
8076 in build_abbrev_table that will change those to
8077 DW_FORM_flag_present if it is set to 1 in all DIEs using
8078 the same abbrev entry. */
8079 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8080 else
8081 size += 1;
8082 break;
8083 case dw_val_class_die_ref:
8084 if (AT_ref_external (a))
8086 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
8087 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
8088 is sized by target address length, whereas in DWARF3
8089 it's always sized as an offset. */
8090 if (use_debug_types)
8091 size += DWARF_TYPE_SIGNATURE_SIZE;
8092 else if (dwarf_version == 2)
8093 size += DWARF2_ADDR_SIZE;
8094 else
8095 size += DWARF_OFFSET_SIZE;
8097 else
8098 size += DWARF_OFFSET_SIZE;
8099 break;
8100 case dw_val_class_fde_ref:
8101 size += DWARF_OFFSET_SIZE;
8102 break;
8103 case dw_val_class_lbl_id:
8104 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8106 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8107 size += size_of_uleb128 (AT_index (a));
8109 else
8110 size += DWARF2_ADDR_SIZE;
8111 break;
8112 case dw_val_class_lineptr:
8113 case dw_val_class_macptr:
8114 size += DWARF_OFFSET_SIZE;
8115 break;
8116 case dw_val_class_str:
8117 form = AT_string_form (a);
8118 if (form == DW_FORM_strp)
8119 size += DWARF_OFFSET_SIZE;
8120 else if (form == DW_FORM_GNU_str_index)
8121 size += size_of_uleb128 (AT_index (a));
8122 else
8123 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
8124 break;
8125 case dw_val_class_file:
8126 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
8127 break;
8128 case dw_val_class_data8:
8129 size += 8;
8130 break;
8131 case dw_val_class_vms_delta:
8132 size += DWARF_OFFSET_SIZE;
8133 break;
8134 case dw_val_class_high_pc:
8135 size += DWARF2_ADDR_SIZE;
8136 break;
8137 default:
8138 gcc_unreachable ();
8142 return size;
8145 /* Size the debugging information associated with a given DIE. Visits the
8146 DIE's children recursively. Updates the global variable next_die_offset, on
8147 each time through. Uses the current value of next_die_offset to update the
8148 die_offset field in each DIE. */
8150 static void
8151 calc_die_sizes (dw_die_ref die)
8153 dw_die_ref c;
8155 gcc_assert (die->die_offset == 0
8156 || (unsigned long int) die->die_offset == next_die_offset);
8157 die->die_offset = next_die_offset;
8158 next_die_offset += size_of_die (die);
8160 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
8162 if (die->die_child != NULL)
8163 /* Count the null byte used to terminate sibling lists. */
8164 next_die_offset += 1;
8167 /* Size just the base type children at the start of the CU.
8168 This is needed because build_abbrev needs to size locs
8169 and sizing of type based stack ops needs to know die_offset
8170 values for the base types. */
8172 static void
8173 calc_base_type_die_sizes (void)
8175 unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8176 unsigned int i;
8177 dw_die_ref base_type;
8178 #if ENABLE_ASSERT_CHECKING
8179 dw_die_ref prev = comp_unit_die ()->die_child;
8180 #endif
8182 die_offset += size_of_die (comp_unit_die ());
8183 for (i = 0; base_types.iterate (i, &base_type); i++)
8185 #if ENABLE_ASSERT_CHECKING
8186 gcc_assert (base_type->die_offset == 0
8187 && prev->die_sib == base_type
8188 && base_type->die_child == NULL
8189 && base_type->die_abbrev);
8190 prev = base_type;
8191 #endif
8192 base_type->die_offset = die_offset;
8193 die_offset += size_of_die (base_type);
8197 /* Set the marks for a die and its children. We do this so
8198 that we know whether or not a reference needs to use FORM_ref_addr; only
8199 DIEs in the same CU will be marked. We used to clear out the offset
8200 and use that as the flag, but ran into ordering problems. */
8202 static void
8203 mark_dies (dw_die_ref die)
8205 dw_die_ref c;
8207 gcc_assert (!die->die_mark);
8209 die->die_mark = 1;
8210 FOR_EACH_CHILD (die, c, mark_dies (c));
8213 /* Clear the marks for a die and its children. */
8215 static void
8216 unmark_dies (dw_die_ref die)
8218 dw_die_ref c;
8220 if (! use_debug_types)
8221 gcc_assert (die->die_mark);
8223 die->die_mark = 0;
8224 FOR_EACH_CHILD (die, c, unmark_dies (c));
8227 /* Clear the marks for a die, its children and referred dies. */
8229 static void
8230 unmark_all_dies (dw_die_ref die)
8232 dw_die_ref c;
8233 dw_attr_ref a;
8234 unsigned ix;
8236 if (!die->die_mark)
8237 return;
8238 die->die_mark = 0;
8240 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
8242 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8243 if (AT_class (a) == dw_val_class_die_ref)
8244 unmark_all_dies (AT_ref (a));
8247 /* Calculate if the entry should appear in the final output file. It may be
8248 from a pruned a type. */
8250 static bool
8251 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
8253 /* By limiting gnu pubnames to definitions only, gold can generate a
8254 gdb index without entries for declarations, which don't include
8255 enough information to be useful. */
8256 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
8257 return false;
8259 if (table == pubname_table)
8261 /* Enumerator names are part of the pubname table, but the
8262 parent DW_TAG_enumeration_type die may have been pruned.
8263 Don't output them if that is the case. */
8264 if (p->die->die_tag == DW_TAG_enumerator &&
8265 (p->die->die_parent == NULL
8266 || !p->die->die_parent->die_perennial_p))
8267 return false;
8269 /* Everything else in the pubname table is included. */
8270 return true;
8273 /* The pubtypes table shouldn't include types that have been
8274 pruned. */
8275 return (p->die->die_offset != 0
8276 || !flag_eliminate_unused_debug_types);
8279 /* Return the size of the .debug_pubnames or .debug_pubtypes table
8280 generated for the compilation unit. */
8282 static unsigned long
8283 size_of_pubnames (vec<pubname_entry, va_gc> *names)
8285 unsigned long size;
8286 unsigned i;
8287 pubname_ref p;
8288 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
8290 size = DWARF_PUBNAMES_HEADER_SIZE;
8291 FOR_EACH_VEC_ELT (*names, i, p)
8292 if (include_pubname_in_output (names, p))
8293 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
8295 size += DWARF_OFFSET_SIZE;
8296 return size;
8299 /* Return the size of the information in the .debug_aranges section. */
8301 static unsigned long
8302 size_of_aranges (void)
8304 unsigned long size;
8306 size = DWARF_ARANGES_HEADER_SIZE;
8308 /* Count the address/length pair for this compilation unit. */
8309 if (text_section_used)
8310 size += 2 * DWARF2_ADDR_SIZE;
8311 if (cold_text_section_used)
8312 size += 2 * DWARF2_ADDR_SIZE;
8313 if (have_multiple_function_sections)
8315 unsigned fde_idx;
8316 dw_fde_ref fde;
8318 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
8320 if (DECL_IGNORED_P (fde->decl))
8321 continue;
8322 if (!fde->in_std_section)
8323 size += 2 * DWARF2_ADDR_SIZE;
8324 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
8325 size += 2 * DWARF2_ADDR_SIZE;
8329 /* Count the two zero words used to terminated the address range table. */
8330 size += 2 * DWARF2_ADDR_SIZE;
8331 return size;
8334 /* Select the encoding of an attribute value. */
8336 static enum dwarf_form
8337 value_format (dw_attr_ref a)
8339 switch (AT_class (a))
8341 case dw_val_class_addr:
8342 /* Only very few attributes allow DW_FORM_addr. */
8343 switch (a->dw_attr)
8345 case DW_AT_low_pc:
8346 case DW_AT_high_pc:
8347 case DW_AT_entry_pc:
8348 case DW_AT_trampoline:
8349 return (AT_index (a) == NOT_INDEXED
8350 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8351 default:
8352 break;
8354 switch (DWARF2_ADDR_SIZE)
8356 case 1:
8357 return DW_FORM_data1;
8358 case 2:
8359 return DW_FORM_data2;
8360 case 4:
8361 return DW_FORM_data4;
8362 case 8:
8363 return DW_FORM_data8;
8364 default:
8365 gcc_unreachable ();
8367 case dw_val_class_range_list:
8368 case dw_val_class_loc_list:
8369 if (dwarf_version >= 4)
8370 return DW_FORM_sec_offset;
8371 /* FALLTHRU */
8372 case dw_val_class_vms_delta:
8373 case dw_val_class_offset:
8374 switch (DWARF_OFFSET_SIZE)
8376 case 4:
8377 return DW_FORM_data4;
8378 case 8:
8379 return DW_FORM_data8;
8380 default:
8381 gcc_unreachable ();
8383 case dw_val_class_loc:
8384 if (dwarf_version >= 4)
8385 return DW_FORM_exprloc;
8386 switch (constant_size (size_of_locs (AT_loc (a))))
8388 case 1:
8389 return DW_FORM_block1;
8390 case 2:
8391 return DW_FORM_block2;
8392 case 4:
8393 return DW_FORM_block4;
8394 default:
8395 gcc_unreachable ();
8397 case dw_val_class_const:
8398 return DW_FORM_sdata;
8399 case dw_val_class_unsigned_const:
8400 switch (constant_size (AT_unsigned (a)))
8402 case 1:
8403 return DW_FORM_data1;
8404 case 2:
8405 return DW_FORM_data2;
8406 case 4:
8407 /* In DWARF3 DW_AT_data_member_location with
8408 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
8409 constant, so we need to use DW_FORM_udata if we need
8410 a large constant. */
8411 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8412 return DW_FORM_udata;
8413 return DW_FORM_data4;
8414 case 8:
8415 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8416 return DW_FORM_udata;
8417 return DW_FORM_data8;
8418 default:
8419 gcc_unreachable ();
8421 case dw_val_class_const_double:
8422 switch (HOST_BITS_PER_WIDE_INT)
8424 case 8:
8425 return DW_FORM_data2;
8426 case 16:
8427 return DW_FORM_data4;
8428 case 32:
8429 return DW_FORM_data8;
8430 case 64:
8431 default:
8432 return DW_FORM_block1;
8434 case dw_val_class_wide_int:
8435 switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
8437 case 8:
8438 return DW_FORM_data1;
8439 case 16:
8440 return DW_FORM_data2;
8441 case 32:
8442 return DW_FORM_data4;
8443 case 64:
8444 return DW_FORM_data8;
8445 default:
8446 return DW_FORM_block1;
8448 case dw_val_class_vec:
8449 switch (constant_size (a->dw_attr_val.v.val_vec.length
8450 * a->dw_attr_val.v.val_vec.elt_size))
8452 case 1:
8453 return DW_FORM_block1;
8454 case 2:
8455 return DW_FORM_block2;
8456 case 4:
8457 return DW_FORM_block4;
8458 default:
8459 gcc_unreachable ();
8461 case dw_val_class_flag:
8462 if (dwarf_version >= 4)
8464 /* Currently all add_AT_flag calls pass in 1 as last argument,
8465 so DW_FORM_flag_present can be used. If that ever changes,
8466 we'll need to use DW_FORM_flag and have some optimization
8467 in build_abbrev_table that will change those to
8468 DW_FORM_flag_present if it is set to 1 in all DIEs using
8469 the same abbrev entry. */
8470 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8471 return DW_FORM_flag_present;
8473 return DW_FORM_flag;
8474 case dw_val_class_die_ref:
8475 if (AT_ref_external (a))
8476 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
8477 else
8478 return DW_FORM_ref;
8479 case dw_val_class_fde_ref:
8480 return DW_FORM_data;
8481 case dw_val_class_lbl_id:
8482 return (AT_index (a) == NOT_INDEXED
8483 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8484 case dw_val_class_lineptr:
8485 case dw_val_class_macptr:
8486 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
8487 case dw_val_class_str:
8488 return AT_string_form (a);
8489 case dw_val_class_file:
8490 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8492 case 1:
8493 return DW_FORM_data1;
8494 case 2:
8495 return DW_FORM_data2;
8496 case 4:
8497 return DW_FORM_data4;
8498 default:
8499 gcc_unreachable ();
8502 case dw_val_class_data8:
8503 return DW_FORM_data8;
8505 case dw_val_class_high_pc:
8506 switch (DWARF2_ADDR_SIZE)
8508 case 1:
8509 return DW_FORM_data1;
8510 case 2:
8511 return DW_FORM_data2;
8512 case 4:
8513 return DW_FORM_data4;
8514 case 8:
8515 return DW_FORM_data8;
8516 default:
8517 gcc_unreachable ();
8520 default:
8521 gcc_unreachable ();
8525 /* Output the encoding of an attribute value. */
8527 static void
8528 output_value_format (dw_attr_ref a)
8530 enum dwarf_form form = value_format (a);
8532 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8535 /* Given a die and id, produce the appropriate abbreviations. */
8537 static void
8538 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
8540 unsigned ix;
8541 dw_attr_ref a_attr;
8543 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8544 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8545 dwarf_tag_name (abbrev->die_tag));
8547 if (abbrev->die_child != NULL)
8548 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8549 else
8550 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8552 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
8554 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8555 dwarf_attr_name (a_attr->dw_attr));
8556 output_value_format (a_attr);
8559 dw2_asm_output_data (1, 0, NULL);
8560 dw2_asm_output_data (1, 0, NULL);
8564 /* Output the .debug_abbrev section which defines the DIE abbreviation
8565 table. */
8567 static void
8568 output_abbrev_section (void)
8570 unsigned long abbrev_id;
8572 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8573 output_die_abbrevs (abbrev_id, abbrev_die_table[abbrev_id]);
8575 /* Terminate the table. */
8576 dw2_asm_output_data (1, 0, NULL);
8579 /* Output a symbol we can use to refer to this DIE from another CU. */
8581 static inline void
8582 output_die_symbol (dw_die_ref die)
8584 const char *sym = die->die_id.die_symbol;
8586 gcc_assert (!die->comdat_type_p);
8588 if (sym == 0)
8589 return;
8591 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8592 /* We make these global, not weak; if the target doesn't support
8593 .linkonce, it doesn't support combining the sections, so debugging
8594 will break. */
8595 targetm.asm_out.globalize_label (asm_out_file, sym);
8597 ASM_OUTPUT_LABEL (asm_out_file, sym);
8600 /* Return a new location list, given the begin and end range, and the
8601 expression. */
8603 static inline dw_loc_list_ref
8604 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8605 const char *section)
8607 dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
8609 retlist->begin = begin;
8610 retlist->begin_entry = NULL;
8611 retlist->end = end;
8612 retlist->expr = expr;
8613 retlist->section = section;
8615 return retlist;
8618 /* Generate a new internal symbol for this location list node, if it
8619 hasn't got one yet. */
8621 static inline void
8622 gen_llsym (dw_loc_list_ref list)
8624 gcc_assert (!list->ll_symbol);
8625 list->ll_symbol = gen_internal_sym ("LLST");
8628 /* Output the location list given to us. */
8630 static void
8631 output_loc_list (dw_loc_list_ref list_head)
8633 dw_loc_list_ref curr = list_head;
8635 if (list_head->emitted)
8636 return;
8637 list_head->emitted = true;
8639 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8641 /* Walk the location list, and output each range + expression. */
8642 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8644 unsigned long size;
8645 /* Don't output an entry that starts and ends at the same address. */
8646 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
8647 continue;
8648 size = size_of_locs (curr->expr);
8649 /* If the expression is too large, drop it on the floor. We could
8650 perhaps put it into DW_TAG_dwarf_procedure and refer to that
8651 in the expression, but >= 64KB expressions for a single value
8652 in a single range are unlikely very useful. */
8653 if (size > 0xffff)
8654 continue;
8655 if (dwarf_split_debug_info)
8657 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
8658 "Location list start/length entry (%s)",
8659 list_head->ll_symbol);
8660 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
8661 "Location list range start index (%s)",
8662 curr->begin);
8663 /* The length field is 4 bytes. If we ever need to support
8664 an 8-byte length, we can add a new DW_LLE code or fall back
8665 to DW_LLE_GNU_start_end_entry. */
8666 dw2_asm_output_delta (4, curr->end, curr->begin,
8667 "Location list range length (%s)",
8668 list_head->ll_symbol);
8670 else if (!have_multiple_function_sections)
8672 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8673 "Location list begin address (%s)",
8674 list_head->ll_symbol);
8675 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8676 "Location list end address (%s)",
8677 list_head->ll_symbol);
8679 else
8681 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8682 "Location list begin address (%s)",
8683 list_head->ll_symbol);
8684 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8685 "Location list end address (%s)",
8686 list_head->ll_symbol);
8689 /* Output the block length for this list of location operations. */
8690 gcc_assert (size <= 0xffff);
8691 dw2_asm_output_data (2, size, "%s", "Location expression size");
8693 output_loc_sequence (curr->expr, -1);
8696 if (dwarf_split_debug_info)
8697 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
8698 "Location list terminator (%s)",
8699 list_head->ll_symbol);
8700 else
8702 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8703 "Location list terminator begin (%s)",
8704 list_head->ll_symbol);
8705 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8706 "Location list terminator end (%s)",
8707 list_head->ll_symbol);
8711 /* Output a range_list offset into the debug_range section. Emit a
8712 relocated reference if val_entry is NULL, otherwise, emit an
8713 indirect reference. */
8715 static void
8716 output_range_list_offset (dw_attr_ref a)
8718 const char *name = dwarf_attr_name (a->dw_attr);
8720 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
8722 char *p = strchr (ranges_section_label, '\0');
8723 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX, a->dw_attr_val.v.val_offset);
8724 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8725 debug_ranges_section, "%s", name);
8726 *p = '\0';
8728 else
8729 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8730 "%s (offset from %s)", name, ranges_section_label);
8733 /* Output the offset into the debug_loc section. */
8735 static void
8736 output_loc_list_offset (dw_attr_ref a)
8738 char *sym = AT_loc_list (a)->ll_symbol;
8740 gcc_assert (sym);
8741 if (dwarf_split_debug_info)
8742 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
8743 "%s", dwarf_attr_name (a->dw_attr));
8744 else
8745 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8746 "%s", dwarf_attr_name (a->dw_attr));
8749 /* Output an attribute's index or value appropriately. */
8751 static void
8752 output_attr_index_or_value (dw_attr_ref a)
8754 const char *name = dwarf_attr_name (a->dw_attr);
8756 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8758 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
8759 return;
8761 switch (AT_class (a))
8763 case dw_val_class_addr:
8764 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8765 break;
8766 case dw_val_class_high_pc:
8767 case dw_val_class_lbl_id:
8768 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8769 break;
8770 case dw_val_class_loc_list:
8771 output_loc_list_offset (a);
8772 break;
8773 default:
8774 gcc_unreachable ();
8778 /* Output a type signature. */
8780 static inline void
8781 output_signature (const char *sig, const char *name)
8783 int i;
8785 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8786 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
8789 /* Output the DIE and its attributes. Called recursively to generate
8790 the definitions of each child DIE. */
8792 static void
8793 output_die (dw_die_ref die)
8795 dw_attr_ref a;
8796 dw_die_ref c;
8797 unsigned long size;
8798 unsigned ix;
8800 /* If someone in another CU might refer to us, set up a symbol for
8801 them to point to. */
8802 if (! die->comdat_type_p && die->die_id.die_symbol)
8803 output_die_symbol (die);
8805 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
8806 (unsigned long)die->die_offset,
8807 dwarf_tag_name (die->die_tag));
8809 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8811 const char *name = dwarf_attr_name (a->dw_attr);
8813 switch (AT_class (a))
8815 case dw_val_class_addr:
8816 output_attr_index_or_value (a);
8817 break;
8819 case dw_val_class_offset:
8820 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8821 "%s", name);
8822 break;
8824 case dw_val_class_range_list:
8825 output_range_list_offset (a);
8826 break;
8828 case dw_val_class_loc:
8829 size = size_of_locs (AT_loc (a));
8831 /* Output the block length for this list of location operations. */
8832 if (dwarf_version >= 4)
8833 dw2_asm_output_data_uleb128 (size, "%s", name);
8834 else
8835 dw2_asm_output_data (constant_size (size), size, "%s", name);
8837 output_loc_sequence (AT_loc (a), -1);
8838 break;
8840 case dw_val_class_const:
8841 /* ??? It would be slightly more efficient to use a scheme like is
8842 used for unsigned constants below, but gdb 4.x does not sign
8843 extend. Gdb 5.x does sign extend. */
8844 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8845 break;
8847 case dw_val_class_unsigned_const:
8849 int csize = constant_size (AT_unsigned (a));
8850 if (dwarf_version == 3
8851 && a->dw_attr == DW_AT_data_member_location
8852 && csize >= 4)
8853 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
8854 else
8855 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
8857 break;
8859 case dw_val_class_const_double:
8861 unsigned HOST_WIDE_INT first, second;
8863 if (HOST_BITS_PER_WIDE_INT >= 64)
8864 dw2_asm_output_data (1,
8865 HOST_BITS_PER_DOUBLE_INT
8866 / HOST_BITS_PER_CHAR,
8867 NULL);
8869 if (WORDS_BIG_ENDIAN)
8871 first = a->dw_attr_val.v.val_double.high;
8872 second = a->dw_attr_val.v.val_double.low;
8874 else
8876 first = a->dw_attr_val.v.val_double.low;
8877 second = a->dw_attr_val.v.val_double.high;
8880 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8881 first, "%s", name);
8882 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8883 second, NULL);
8885 break;
8887 case dw_val_class_wide_int:
8889 int i;
8890 int len = get_full_len (*a->dw_attr_val.v.val_wide);
8891 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
8892 if (len * HOST_BITS_PER_WIDE_INT > 64)
8893 dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide) * l,
8894 NULL);
8896 if (WORDS_BIG_ENDIAN)
8897 for (i = len - 1; i >= 0; --i)
8899 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
8900 name);
8901 name = NULL;
8903 else
8904 for (i = 0; i < len; ++i)
8906 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
8907 name);
8908 name = NULL;
8911 break;
8913 case dw_val_class_vec:
8915 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8916 unsigned int len = a->dw_attr_val.v.val_vec.length;
8917 unsigned int i;
8918 unsigned char *p;
8920 dw2_asm_output_data (constant_size (len * elt_size),
8921 len * elt_size, "%s", name);
8922 if (elt_size > sizeof (HOST_WIDE_INT))
8924 elt_size /= 2;
8925 len *= 2;
8927 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8928 i < len;
8929 i++, p += elt_size)
8930 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8931 "fp or vector constant word %u", i);
8932 break;
8935 case dw_val_class_flag:
8936 if (dwarf_version >= 4)
8938 /* Currently all add_AT_flag calls pass in 1 as last argument,
8939 so DW_FORM_flag_present can be used. If that ever changes,
8940 we'll need to use DW_FORM_flag and have some optimization
8941 in build_abbrev_table that will change those to
8942 DW_FORM_flag_present if it is set to 1 in all DIEs using
8943 the same abbrev entry. */
8944 gcc_assert (AT_flag (a) == 1);
8945 if (flag_debug_asm)
8946 fprintf (asm_out_file, "\t\t\t%s %s\n",
8947 ASM_COMMENT_START, name);
8948 break;
8950 dw2_asm_output_data (1, AT_flag (a), "%s", name);
8951 break;
8953 case dw_val_class_loc_list:
8954 output_attr_index_or_value (a);
8955 break;
8957 case dw_val_class_die_ref:
8958 if (AT_ref_external (a))
8960 if (AT_ref (a)->comdat_type_p)
8962 comdat_type_node_ref type_node =
8963 AT_ref (a)->die_id.die_type_node;
8965 gcc_assert (type_node);
8966 output_signature (type_node->signature, name);
8968 else
8970 const char *sym = AT_ref (a)->die_id.die_symbol;
8971 int size;
8973 gcc_assert (sym);
8974 /* In DWARF2, DW_FORM_ref_addr is sized by target address
8975 length, whereas in DWARF3 it's always sized as an
8976 offset. */
8977 if (dwarf_version == 2)
8978 size = DWARF2_ADDR_SIZE;
8979 else
8980 size = DWARF_OFFSET_SIZE;
8981 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
8982 name);
8985 else
8987 gcc_assert (AT_ref (a)->die_offset);
8988 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8989 "%s", name);
8991 break;
8993 case dw_val_class_fde_ref:
8995 char l1[20];
8997 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
8998 a->dw_attr_val.v.val_fde_index * 2);
8999 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
9000 "%s", name);
9002 break;
9004 case dw_val_class_vms_delta:
9005 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
9006 AT_vms_delta2 (a), AT_vms_delta1 (a),
9007 "%s", name);
9008 break;
9010 case dw_val_class_lbl_id:
9011 output_attr_index_or_value (a);
9012 break;
9014 case dw_val_class_lineptr:
9015 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
9016 debug_line_section, "%s", name);
9017 break;
9019 case dw_val_class_macptr:
9020 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
9021 debug_macinfo_section, "%s", name);
9022 break;
9024 case dw_val_class_str:
9025 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
9026 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
9027 a->dw_attr_val.v.val_str->label,
9028 debug_str_section,
9029 "%s: \"%s\"", name, AT_string (a));
9030 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
9031 dw2_asm_output_data_uleb128 (AT_index (a),
9032 "%s: \"%s\"", name, AT_string (a));
9033 else
9034 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
9035 break;
9037 case dw_val_class_file:
9039 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
9041 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
9042 a->dw_attr_val.v.val_file->filename);
9043 break;
9046 case dw_val_class_data8:
9048 int i;
9050 for (i = 0; i < 8; i++)
9051 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
9052 i == 0 ? "%s" : NULL, name);
9053 break;
9056 case dw_val_class_high_pc:
9057 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
9058 get_AT_low_pc (die), "DW_AT_high_pc");
9059 break;
9061 default:
9062 gcc_unreachable ();
9066 FOR_EACH_CHILD (die, c, output_die (c));
9068 /* Add null byte to terminate sibling list. */
9069 if (die->die_child != NULL)
9070 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
9071 (unsigned long) die->die_offset);
9074 /* Output the compilation unit that appears at the beginning of the
9075 .debug_info section, and precedes the DIE descriptions. */
9077 static void
9078 output_compilation_unit_header (void)
9080 /* We don't support actual DWARFv5 units yet, we just use some
9081 DWARFv5 draft DIE tags in DWARFv4 format. */
9082 int ver = dwarf_version < 5 ? dwarf_version : 4;
9084 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9085 dw2_asm_output_data (4, 0xffffffff,
9086 "Initial length escape value indicating 64-bit DWARF extension");
9087 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9088 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
9089 "Length of Compilation Unit Info");
9090 dw2_asm_output_data (2, ver, "DWARF version number");
9091 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
9092 debug_abbrev_section,
9093 "Offset Into Abbrev. Section");
9094 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9097 /* Output the compilation unit DIE and its children. */
9099 static void
9100 output_comp_unit (dw_die_ref die, int output_if_empty)
9102 const char *secname, *oldsym;
9103 char *tmp;
9105 /* Unless we are outputting main CU, we may throw away empty ones. */
9106 if (!output_if_empty && die->die_child == NULL)
9107 return;
9109 /* Even if there are no children of this DIE, we must output the information
9110 about the compilation unit. Otherwise, on an empty translation unit, we
9111 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
9112 will then complain when examining the file. First mark all the DIEs in
9113 this CU so we know which get local refs. */
9114 mark_dies (die);
9116 external_ref_hash_type *extern_map = optimize_external_refs (die);
9118 build_abbrev_table (die, extern_map);
9120 delete extern_map;
9122 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
9123 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
9124 calc_die_sizes (die);
9126 oldsym = die->die_id.die_symbol;
9127 if (oldsym)
9129 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
9131 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
9132 secname = tmp;
9133 die->die_id.die_symbol = NULL;
9134 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9136 else
9138 switch_to_section (debug_info_section);
9139 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
9140 info_section_emitted = true;
9143 /* Output debugging information. */
9144 output_compilation_unit_header ();
9145 output_die (die);
9147 /* Leave the marks on the main CU, so we can check them in
9148 output_pubnames. */
9149 if (oldsym)
9151 unmark_dies (die);
9152 die->die_id.die_symbol = oldsym;
9156 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
9157 and .debug_pubtypes. This is configured per-target, but can be
9158 overridden by the -gpubnames or -gno-pubnames options. */
9160 static inline bool
9161 want_pubnames (void)
9163 if (debug_info_level <= DINFO_LEVEL_TERSE)
9164 return false;
9165 if (debug_generate_pub_sections != -1)
9166 return debug_generate_pub_sections;
9167 return targetm.want_debug_pub_sections;
9170 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
9172 static void
9173 add_AT_pubnames (dw_die_ref die)
9175 if (want_pubnames ())
9176 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
9179 /* Add a string attribute value to a skeleton DIE. */
9181 static inline void
9182 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
9183 const char *str)
9185 dw_attr_node attr;
9186 struct indirect_string_node *node;
9188 if (! skeleton_debug_str_hash)
9189 skeleton_debug_str_hash
9190 = hash_table<indirect_string_hasher>::create_ggc (10);
9192 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
9193 find_string_form (node);
9194 if (node->form == DW_FORM_GNU_str_index)
9195 node->form = DW_FORM_strp;
9197 attr.dw_attr = attr_kind;
9198 attr.dw_attr_val.val_class = dw_val_class_str;
9199 attr.dw_attr_val.val_entry = NULL;
9200 attr.dw_attr_val.v.val_str = node;
9201 add_dwarf_attr (die, &attr);
9204 /* Helper function to generate top-level dies for skeleton debug_info and
9205 debug_types. */
9207 static void
9208 add_top_level_skeleton_die_attrs (dw_die_ref die)
9210 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
9211 const char *comp_dir = comp_dir_string ();
9213 add_skeleton_AT_string (die, DW_AT_GNU_dwo_name, dwo_file_name);
9214 if (comp_dir != NULL)
9215 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
9216 add_AT_pubnames (die);
9217 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
9220 /* Output skeleton debug sections that point to the dwo file. */
9222 static void
9223 output_skeleton_debug_sections (dw_die_ref comp_unit)
9225 /* We don't support actual DWARFv5 units yet, we just use some
9226 DWARFv5 draft DIE tags in DWARFv4 format. */
9227 int ver = dwarf_version < 5 ? dwarf_version : 4;
9229 /* These attributes will be found in the full debug_info section. */
9230 remove_AT (comp_unit, DW_AT_producer);
9231 remove_AT (comp_unit, DW_AT_language);
9233 switch_to_section (debug_skeleton_info_section);
9234 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
9236 /* Produce the skeleton compilation-unit header. This one differs enough from
9237 a normal CU header that it's better not to call output_compilation_unit
9238 header. */
9239 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9240 dw2_asm_output_data (4, 0xffffffff,
9241 "Initial length escape value indicating 64-bit DWARF extension");
9243 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9244 DWARF_COMPILE_UNIT_HEADER_SIZE
9245 - DWARF_INITIAL_LENGTH_SIZE
9246 + size_of_die (comp_unit),
9247 "Length of Compilation Unit Info");
9248 dw2_asm_output_data (2, ver, "DWARF version number");
9249 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
9250 debug_abbrev_section,
9251 "Offset Into Abbrev. Section");
9252 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9254 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
9255 output_die (comp_unit);
9257 /* Build the skeleton debug_abbrev section. */
9258 switch_to_section (debug_skeleton_abbrev_section);
9259 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
9261 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
9263 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
9266 /* Output a comdat type unit DIE and its children. */
9268 static void
9269 output_comdat_type_unit (comdat_type_node *node)
9271 const char *secname;
9272 char *tmp;
9273 int i;
9274 #if defined (OBJECT_FORMAT_ELF)
9275 tree comdat_key;
9276 #endif
9278 /* First mark all the DIEs in this CU so we know which get local refs. */
9279 mark_dies (node->root_die);
9281 external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
9283 build_abbrev_table (node->root_die, extern_map);
9285 delete extern_map;
9286 extern_map = NULL;
9288 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
9289 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
9290 calc_die_sizes (node->root_die);
9292 #if defined (OBJECT_FORMAT_ELF)
9293 if (!dwarf_split_debug_info)
9294 secname = ".debug_types";
9295 else
9296 secname = ".debug_types.dwo";
9298 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9299 sprintf (tmp, "wt.");
9300 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9301 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
9302 comdat_key = get_identifier (tmp);
9303 targetm.asm_out.named_section (secname,
9304 SECTION_DEBUG | SECTION_LINKONCE,
9305 comdat_key);
9306 #else
9307 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9308 sprintf (tmp, ".gnu.linkonce.wt.");
9309 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9310 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
9311 secname = tmp;
9312 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9313 #endif
9315 /* Output debugging information. */
9316 output_compilation_unit_header ();
9317 output_signature (node->signature, "Type Signature");
9318 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
9319 "Offset to Type DIE");
9320 output_die (node->root_die);
9322 unmark_dies (node->root_die);
9325 /* Return the DWARF2/3 pubname associated with a decl. */
9327 static const char *
9328 dwarf2_name (tree decl, int scope)
9330 if (DECL_NAMELESS (decl))
9331 return NULL;
9332 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
9335 /* Add a new entry to .debug_pubnames if appropriate. */
9337 static void
9338 add_pubname_string (const char *str, dw_die_ref die)
9340 pubname_entry e;
9342 e.die = die;
9343 e.name = xstrdup (str);
9344 vec_safe_push (pubname_table, e);
9347 static void
9348 add_pubname (tree decl, dw_die_ref die)
9350 if (!want_pubnames ())
9351 return;
9353 /* Don't add items to the table when we expect that the consumer will have
9354 just read the enclosing die. For example, if the consumer is looking at a
9355 class_member, it will either be inside the class already, or will have just
9356 looked up the class to find the member. Either way, searching the class is
9357 faster than searching the index. */
9358 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
9359 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9361 const char *name = dwarf2_name (decl, 1);
9363 if (name)
9364 add_pubname_string (name, die);
9368 /* Add an enumerator to the pubnames section. */
9370 static void
9371 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
9373 pubname_entry e;
9375 gcc_assert (scope_name);
9376 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
9377 e.die = die;
9378 vec_safe_push (pubname_table, e);
9381 /* Add a new entry to .debug_pubtypes if appropriate. */
9383 static void
9384 add_pubtype (tree decl, dw_die_ref die)
9386 pubname_entry e;
9388 if (!want_pubnames ())
9389 return;
9391 if ((TREE_PUBLIC (decl)
9392 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9393 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
9395 tree scope = NULL;
9396 const char *scope_name = "";
9397 const char *sep = is_cxx () ? "::" : ".";
9398 const char *name;
9400 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
9401 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
9403 scope_name = lang_hooks.dwarf_name (scope, 1);
9404 if (scope_name != NULL && scope_name[0] != '\0')
9405 scope_name = concat (scope_name, sep, NULL);
9406 else
9407 scope_name = "";
9410 if (TYPE_P (decl))
9411 name = type_tag (decl);
9412 else
9413 name = lang_hooks.dwarf_name (decl, 1);
9415 /* If we don't have a name for the type, there's no point in adding
9416 it to the table. */
9417 if (name != NULL && name[0] != '\0')
9419 e.die = die;
9420 e.name = concat (scope_name, name, NULL);
9421 vec_safe_push (pubtype_table, e);
9424 /* Although it might be more consistent to add the pubinfo for the
9425 enumerators as their dies are created, they should only be added if the
9426 enum type meets the criteria above. So rather than re-check the parent
9427 enum type whenever an enumerator die is created, just output them all
9428 here. This isn't protected by the name conditional because anonymous
9429 enums don't have names. */
9430 if (die->die_tag == DW_TAG_enumeration_type)
9432 dw_die_ref c;
9434 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
9439 /* Output a single entry in the pubnames table. */
9441 static void
9442 output_pubname (dw_offset die_offset, pubname_entry *entry)
9444 dw_die_ref die = entry->die;
9445 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
9447 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
9449 if (debug_generate_pub_sections == 2)
9451 /* This logic follows gdb's method for determining the value of the flag
9452 byte. */
9453 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
9454 switch (die->die_tag)
9456 case DW_TAG_typedef:
9457 case DW_TAG_base_type:
9458 case DW_TAG_subrange_type:
9459 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9460 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9461 break;
9462 case DW_TAG_enumerator:
9463 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9464 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9465 if (!is_cxx () && !is_java ())
9466 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9467 break;
9468 case DW_TAG_subprogram:
9469 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9470 GDB_INDEX_SYMBOL_KIND_FUNCTION);
9471 if (!is_ada ())
9472 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9473 break;
9474 case DW_TAG_constant:
9475 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9476 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9477 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9478 break;
9479 case DW_TAG_variable:
9480 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9481 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9482 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9483 break;
9484 case DW_TAG_namespace:
9485 case DW_TAG_imported_declaration:
9486 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9487 break;
9488 case DW_TAG_class_type:
9489 case DW_TAG_interface_type:
9490 case DW_TAG_structure_type:
9491 case DW_TAG_union_type:
9492 case DW_TAG_enumeration_type:
9493 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9494 if (!is_cxx () && !is_java ())
9495 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9496 break;
9497 default:
9498 /* An unusual tag. Leave the flag-byte empty. */
9499 break;
9501 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
9502 "GDB-index flags");
9505 dw2_asm_output_nstring (entry->name, -1, "external name");
9509 /* Output the public names table used to speed up access to externally
9510 visible names; or the public types table used to find type definitions. */
9512 static void
9513 output_pubnames (vec<pubname_entry, va_gc> *names)
9515 unsigned i;
9516 unsigned long pubnames_length = size_of_pubnames (names);
9517 pubname_ref pub;
9519 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9520 dw2_asm_output_data (4, 0xffffffff,
9521 "Initial length escape value indicating 64-bit DWARF extension");
9522 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length, "Pub Info Length");
9524 /* Version number for pubnames/pubtypes is independent of dwarf version. */
9525 dw2_asm_output_data (2, 2, "DWARF Version");
9527 if (dwarf_split_debug_info)
9528 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9529 debug_skeleton_info_section,
9530 "Offset of Compilation Unit Info");
9531 else
9532 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9533 debug_info_section,
9534 "Offset of Compilation Unit Info");
9535 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
9536 "Compilation Unit Length");
9538 FOR_EACH_VEC_ELT (*names, i, pub)
9540 if (include_pubname_in_output (names, pub))
9542 dw_offset die_offset = pub->die->die_offset;
9544 /* We shouldn't see pubnames for DIEs outside of the main CU. */
9545 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
9546 gcc_assert (pub->die->die_mark);
9548 /* If we're putting types in their own .debug_types sections,
9549 the .debug_pubtypes table will still point to the compile
9550 unit (not the type unit), so we want to use the offset of
9551 the skeleton DIE (if there is one). */
9552 if (pub->die->comdat_type_p && names == pubtype_table)
9554 comdat_type_node_ref type_node = pub->die->die_id.die_type_node;
9556 if (type_node != NULL)
9557 die_offset = (type_node->skeleton_die != NULL
9558 ? type_node->skeleton_die->die_offset
9559 : comp_unit_die ()->die_offset);
9562 output_pubname (die_offset, pub);
9566 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
9569 /* Output public names and types tables if necessary. */
9571 static void
9572 output_pubtables (void)
9574 if (!want_pubnames () || !info_section_emitted)
9575 return;
9577 switch_to_section (debug_pubnames_section);
9578 output_pubnames (pubname_table);
9579 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
9580 It shouldn't hurt to emit it always, since pure DWARF2 consumers
9581 simply won't look for the section. */
9582 switch_to_section (debug_pubtypes_section);
9583 output_pubnames (pubtype_table);
9587 /* Output the information that goes into the .debug_aranges table.
9588 Namely, define the beginning and ending address range of the
9589 text section generated for this compilation unit. */
9591 static void
9592 output_aranges (unsigned long aranges_length)
9594 unsigned i;
9596 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9597 dw2_asm_output_data (4, 0xffffffff,
9598 "Initial length escape value indicating 64-bit DWARF extension");
9599 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
9600 "Length of Address Ranges Info");
9601 /* Version number for aranges is still 2, even up to DWARF5. */
9602 dw2_asm_output_data (2, 2, "DWARF Version");
9603 if (dwarf_split_debug_info)
9604 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9605 debug_skeleton_info_section,
9606 "Offset of Compilation Unit Info");
9607 else
9608 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9609 debug_info_section,
9610 "Offset of Compilation Unit Info");
9611 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
9612 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
9614 /* We need to align to twice the pointer size here. */
9615 if (DWARF_ARANGES_PAD_SIZE)
9617 /* Pad using a 2 byte words so that padding is correct for any
9618 pointer size. */
9619 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
9620 2 * DWARF2_ADDR_SIZE);
9621 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
9622 dw2_asm_output_data (2, 0, NULL);
9625 /* It is necessary not to output these entries if the sections were
9626 not used; if the sections were not used, the length will be 0 and
9627 the address may end up as 0 if the section is discarded by ld
9628 --gc-sections, leaving an invalid (0, 0) entry that can be
9629 confused with the terminator. */
9630 if (text_section_used)
9632 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
9633 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
9634 text_section_label, "Length");
9636 if (cold_text_section_used)
9638 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
9639 "Address");
9640 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
9641 cold_text_section_label, "Length");
9644 if (have_multiple_function_sections)
9646 unsigned fde_idx;
9647 dw_fde_ref fde;
9649 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9651 if (DECL_IGNORED_P (fde->decl))
9652 continue;
9653 if (!fde->in_std_section)
9655 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
9656 "Address");
9657 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
9658 fde->dw_fde_begin, "Length");
9660 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9662 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
9663 "Address");
9664 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
9665 fde->dw_fde_second_begin, "Length");
9670 /* Output the terminator words. */
9671 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9672 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9675 /* Add a new entry to .debug_ranges. Return the offset at which it
9676 was placed. */
9678 static unsigned int
9679 add_ranges_num (int num)
9681 unsigned int in_use = ranges_table_in_use;
9683 if (in_use == ranges_table_allocated)
9685 ranges_table_allocated += RANGES_TABLE_INCREMENT;
9686 ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
9687 ranges_table_allocated);
9688 memset (ranges_table + ranges_table_in_use, 0,
9689 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
9692 ranges_table[in_use].num = num;
9693 ranges_table_in_use = in_use + 1;
9695 return in_use * 2 * DWARF2_ADDR_SIZE;
9698 /* Add a new entry to .debug_ranges corresponding to a block, or a
9699 range terminator if BLOCK is NULL. */
9701 static unsigned int
9702 add_ranges (const_tree block)
9704 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
9707 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
9708 When using dwarf_split_debug_info, address attributes in dies destined
9709 for the final executable should be direct references--setting the
9710 parameter force_direct ensures this behavior. */
9712 static void
9713 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
9714 bool *added, bool force_direct)
9716 unsigned int in_use = ranges_by_label_in_use;
9717 unsigned int offset;
9719 if (in_use == ranges_by_label_allocated)
9721 ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
9722 ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
9723 ranges_by_label,
9724 ranges_by_label_allocated);
9725 memset (ranges_by_label + ranges_by_label_in_use, 0,
9726 RANGES_TABLE_INCREMENT
9727 * sizeof (struct dw_ranges_by_label_struct));
9730 ranges_by_label[in_use].begin = begin;
9731 ranges_by_label[in_use].end = end;
9732 ranges_by_label_in_use = in_use + 1;
9734 offset = add_ranges_num (-(int)in_use - 1);
9735 if (!*added)
9737 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
9738 *added = true;
9742 static void
9743 output_ranges (void)
9745 unsigned i;
9746 static const char *const start_fmt = "Offset %#x";
9747 const char *fmt = start_fmt;
9749 for (i = 0; i < ranges_table_in_use; i++)
9751 int block_num = ranges_table[i].num;
9753 if (block_num > 0)
9755 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
9756 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
9758 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
9759 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
9761 /* If all code is in the text section, then the compilation
9762 unit base address defaults to DW_AT_low_pc, which is the
9763 base of the text section. */
9764 if (!have_multiple_function_sections)
9766 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
9767 text_section_label,
9768 fmt, i * 2 * DWARF2_ADDR_SIZE);
9769 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
9770 text_section_label, NULL);
9773 /* Otherwise, the compilation unit base address is zero,
9774 which allows us to use absolute addresses, and not worry
9775 about whether the target supports cross-section
9776 arithmetic. */
9777 else
9779 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
9780 fmt, i * 2 * DWARF2_ADDR_SIZE);
9781 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
9784 fmt = NULL;
9787 /* Negative block_num stands for an index into ranges_by_label. */
9788 else if (block_num < 0)
9790 int lab_idx = - block_num - 1;
9792 if (!have_multiple_function_sections)
9794 gcc_unreachable ();
9795 #if 0
9796 /* If we ever use add_ranges_by_labels () for a single
9797 function section, all we have to do is to take out
9798 the #if 0 above. */
9799 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9800 ranges_by_label[lab_idx].begin,
9801 text_section_label,
9802 fmt, i * 2 * DWARF2_ADDR_SIZE);
9803 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9804 ranges_by_label[lab_idx].end,
9805 text_section_label, NULL);
9806 #endif
9808 else
9810 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9811 ranges_by_label[lab_idx].begin,
9812 fmt, i * 2 * DWARF2_ADDR_SIZE);
9813 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9814 ranges_by_label[lab_idx].end,
9815 NULL);
9818 else
9820 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9821 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9822 fmt = start_fmt;
9827 /* Data structure containing information about input files. */
9828 struct file_info
9830 const char *path; /* Complete file name. */
9831 const char *fname; /* File name part. */
9832 int length; /* Length of entire string. */
9833 struct dwarf_file_data * file_idx; /* Index in input file table. */
9834 int dir_idx; /* Index in directory table. */
9837 /* Data structure containing information about directories with source
9838 files. */
9839 struct dir_info
9841 const char *path; /* Path including directory name. */
9842 int length; /* Path length. */
9843 int prefix; /* Index of directory entry which is a prefix. */
9844 int count; /* Number of files in this directory. */
9845 int dir_idx; /* Index of directory used as base. */
9848 /* Callback function for file_info comparison. We sort by looking at
9849 the directories in the path. */
9851 static int
9852 file_info_cmp (const void *p1, const void *p2)
9854 const struct file_info *const s1 = (const struct file_info *) p1;
9855 const struct file_info *const s2 = (const struct file_info *) p2;
9856 const unsigned char *cp1;
9857 const unsigned char *cp2;
9859 /* Take care of file names without directories. We need to make sure that
9860 we return consistent values to qsort since some will get confused if
9861 we return the same value when identical operands are passed in opposite
9862 orders. So if neither has a directory, return 0 and otherwise return
9863 1 or -1 depending on which one has the directory. */
9864 if ((s1->path == s1->fname || s2->path == s2->fname))
9865 return (s2->path == s2->fname) - (s1->path == s1->fname);
9867 cp1 = (const unsigned char *) s1->path;
9868 cp2 = (const unsigned char *) s2->path;
9870 while (1)
9872 ++cp1;
9873 ++cp2;
9874 /* Reached the end of the first path? If so, handle like above. */
9875 if ((cp1 == (const unsigned char *) s1->fname)
9876 || (cp2 == (const unsigned char *) s2->fname))
9877 return ((cp2 == (const unsigned char *) s2->fname)
9878 - (cp1 == (const unsigned char *) s1->fname));
9880 /* Character of current path component the same? */
9881 else if (*cp1 != *cp2)
9882 return *cp1 - *cp2;
9886 struct file_name_acquire_data
9888 struct file_info *files;
9889 int used_files;
9890 int max_files;
9893 /* Traversal function for the hash table. */
9896 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
9898 struct dwarf_file_data *d = *slot;
9899 struct file_info *fi;
9900 const char *f;
9902 gcc_assert (fnad->max_files >= d->emitted_number);
9904 if (! d->emitted_number)
9905 return 1;
9907 gcc_assert (fnad->max_files != fnad->used_files);
9909 fi = fnad->files + fnad->used_files++;
9911 /* Skip all leading "./". */
9912 f = d->filename;
9913 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
9914 f += 2;
9916 /* Create a new array entry. */
9917 fi->path = f;
9918 fi->length = strlen (f);
9919 fi->file_idx = d;
9921 /* Search for the file name part. */
9922 f = strrchr (f, DIR_SEPARATOR);
9923 #if defined (DIR_SEPARATOR_2)
9925 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
9927 if (g != NULL)
9929 if (f == NULL || f < g)
9930 f = g;
9933 #endif
9935 fi->fname = f == NULL ? fi->path : f + 1;
9936 return 1;
9939 /* Output the directory table and the file name table. We try to minimize
9940 the total amount of memory needed. A heuristic is used to avoid large
9941 slowdowns with many input files. */
9943 static void
9944 output_file_names (void)
9946 struct file_name_acquire_data fnad;
9947 int numfiles;
9948 struct file_info *files;
9949 struct dir_info *dirs;
9950 int *saved;
9951 int *savehere;
9952 int *backmap;
9953 int ndirs;
9954 int idx_offset;
9955 int i;
9957 if (!last_emitted_file)
9959 dw2_asm_output_data (1, 0, "End directory table");
9960 dw2_asm_output_data (1, 0, "End file name table");
9961 return;
9964 numfiles = last_emitted_file->emitted_number;
9966 /* Allocate the various arrays we need. */
9967 files = XALLOCAVEC (struct file_info, numfiles);
9968 dirs = XALLOCAVEC (struct dir_info, numfiles);
9970 fnad.files = files;
9971 fnad.used_files = 0;
9972 fnad.max_files = numfiles;
9973 file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
9974 gcc_assert (fnad.used_files == fnad.max_files);
9976 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
9978 /* Find all the different directories used. */
9979 dirs[0].path = files[0].path;
9980 dirs[0].length = files[0].fname - files[0].path;
9981 dirs[0].prefix = -1;
9982 dirs[0].count = 1;
9983 dirs[0].dir_idx = 0;
9984 files[0].dir_idx = 0;
9985 ndirs = 1;
9987 for (i = 1; i < numfiles; i++)
9988 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
9989 && memcmp (dirs[ndirs - 1].path, files[i].path,
9990 dirs[ndirs - 1].length) == 0)
9992 /* Same directory as last entry. */
9993 files[i].dir_idx = ndirs - 1;
9994 ++dirs[ndirs - 1].count;
9996 else
9998 int j;
10000 /* This is a new directory. */
10001 dirs[ndirs].path = files[i].path;
10002 dirs[ndirs].length = files[i].fname - files[i].path;
10003 dirs[ndirs].count = 1;
10004 dirs[ndirs].dir_idx = ndirs;
10005 files[i].dir_idx = ndirs;
10007 /* Search for a prefix. */
10008 dirs[ndirs].prefix = -1;
10009 for (j = 0; j < ndirs; j++)
10010 if (dirs[j].length < dirs[ndirs].length
10011 && dirs[j].length > 1
10012 && (dirs[ndirs].prefix == -1
10013 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
10014 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
10015 dirs[ndirs].prefix = j;
10017 ++ndirs;
10020 /* Now to the actual work. We have to find a subset of the directories which
10021 allow expressing the file name using references to the directory table
10022 with the least amount of characters. We do not do an exhaustive search
10023 where we would have to check out every combination of every single
10024 possible prefix. Instead we use a heuristic which provides nearly optimal
10025 results in most cases and never is much off. */
10026 saved = XALLOCAVEC (int, ndirs);
10027 savehere = XALLOCAVEC (int, ndirs);
10029 memset (saved, '\0', ndirs * sizeof (saved[0]));
10030 for (i = 0; i < ndirs; i++)
10032 int j;
10033 int total;
10035 /* We can always save some space for the current directory. But this
10036 does not mean it will be enough to justify adding the directory. */
10037 savehere[i] = dirs[i].length;
10038 total = (savehere[i] - saved[i]) * dirs[i].count;
10040 for (j = i + 1; j < ndirs; j++)
10042 savehere[j] = 0;
10043 if (saved[j] < dirs[i].length)
10045 /* Determine whether the dirs[i] path is a prefix of the
10046 dirs[j] path. */
10047 int k;
10049 k = dirs[j].prefix;
10050 while (k != -1 && k != (int) i)
10051 k = dirs[k].prefix;
10053 if (k == (int) i)
10055 /* Yes it is. We can possibly save some memory by
10056 writing the filenames in dirs[j] relative to
10057 dirs[i]. */
10058 savehere[j] = dirs[i].length;
10059 total += (savehere[j] - saved[j]) * dirs[j].count;
10064 /* Check whether we can save enough to justify adding the dirs[i]
10065 directory. */
10066 if (total > dirs[i].length + 1)
10068 /* It's worthwhile adding. */
10069 for (j = i; j < ndirs; j++)
10070 if (savehere[j] > 0)
10072 /* Remember how much we saved for this directory so far. */
10073 saved[j] = savehere[j];
10075 /* Remember the prefix directory. */
10076 dirs[j].dir_idx = i;
10081 /* Emit the directory name table. */
10082 idx_offset = dirs[0].length > 0 ? 1 : 0;
10083 for (i = 1 - idx_offset; i < ndirs; i++)
10084 dw2_asm_output_nstring (dirs[i].path,
10085 dirs[i].length
10086 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
10087 "Directory Entry: %#x", i + idx_offset);
10089 dw2_asm_output_data (1, 0, "End directory table");
10091 /* We have to emit them in the order of emitted_number since that's
10092 used in the debug info generation. To do this efficiently we
10093 generate a back-mapping of the indices first. */
10094 backmap = XALLOCAVEC (int, numfiles);
10095 for (i = 0; i < numfiles; i++)
10096 backmap[files[i].file_idx->emitted_number - 1] = i;
10098 /* Now write all the file names. */
10099 for (i = 0; i < numfiles; i++)
10101 int file_idx = backmap[i];
10102 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
10104 #ifdef VMS_DEBUGGING_INFO
10105 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
10107 /* Setting these fields can lead to debugger miscomparisons,
10108 but VMS Debug requires them to be set correctly. */
10110 int ver;
10111 long long cdt;
10112 long siz;
10113 int maxfilelen = strlen (files[file_idx].path)
10114 + dirs[dir_idx].length
10115 + MAX_VMS_VERSION_LEN + 1;
10116 char *filebuf = XALLOCAVEC (char, maxfilelen);
10118 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
10119 snprintf (filebuf, maxfilelen, "%s;%d",
10120 files[file_idx].path + dirs[dir_idx].length, ver);
10122 dw2_asm_output_nstring
10123 (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
10125 /* Include directory index. */
10126 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10128 /* Modification time. */
10129 dw2_asm_output_data_uleb128
10130 ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
10131 ? cdt : 0,
10132 NULL);
10134 /* File length in bytes. */
10135 dw2_asm_output_data_uleb128
10136 ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
10137 ? siz : 0,
10138 NULL);
10139 #else
10140 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
10141 "File Entry: %#x", (unsigned) i + 1);
10143 /* Include directory index. */
10144 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10146 /* Modification time. */
10147 dw2_asm_output_data_uleb128 (0, NULL);
10149 /* File length in bytes. */
10150 dw2_asm_output_data_uleb128 (0, NULL);
10151 #endif /* VMS_DEBUGGING_INFO */
10154 dw2_asm_output_data (1, 0, "End file name table");
10158 /* Output one line number table into the .debug_line section. */
10160 static void
10161 output_one_line_info_table (dw_line_info_table *table)
10163 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
10164 unsigned int current_line = 1;
10165 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
10166 dw_line_info_entry *ent;
10167 size_t i;
10169 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
10171 switch (ent->opcode)
10173 case LI_set_address:
10174 /* ??? Unfortunately, we have little choice here currently, and
10175 must always use the most general form. GCC does not know the
10176 address delta itself, so we can't use DW_LNS_advance_pc. Many
10177 ports do have length attributes which will give an upper bound
10178 on the address range. We could perhaps use length attributes
10179 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
10180 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
10182 /* This can handle any delta. This takes
10183 4+DWARF2_ADDR_SIZE bytes. */
10184 dw2_asm_output_data (1, 0, "set address %s", line_label);
10185 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10186 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10187 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
10188 break;
10190 case LI_set_line:
10191 if (ent->val == current_line)
10193 /* We still need to start a new row, so output a copy insn. */
10194 dw2_asm_output_data (1, DW_LNS_copy,
10195 "copy line %u", current_line);
10197 else
10199 int line_offset = ent->val - current_line;
10200 int line_delta = line_offset - DWARF_LINE_BASE;
10202 current_line = ent->val;
10203 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
10205 /* This can handle deltas from -10 to 234, using the current
10206 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
10207 This takes 1 byte. */
10208 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
10209 "line %u", current_line);
10211 else
10213 /* This can handle any delta. This takes at least 4 bytes,
10214 depending on the value being encoded. */
10215 dw2_asm_output_data (1, DW_LNS_advance_line,
10216 "advance to line %u", current_line);
10217 dw2_asm_output_data_sleb128 (line_offset, NULL);
10218 dw2_asm_output_data (1, DW_LNS_copy, NULL);
10221 break;
10223 case LI_set_file:
10224 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
10225 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10226 break;
10228 case LI_set_column:
10229 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
10230 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10231 break;
10233 case LI_negate_stmt:
10234 current_is_stmt = !current_is_stmt;
10235 dw2_asm_output_data (1, DW_LNS_negate_stmt,
10236 "is_stmt %d", current_is_stmt);
10237 break;
10239 case LI_set_prologue_end:
10240 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
10241 "set prologue end");
10242 break;
10244 case LI_set_epilogue_begin:
10245 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
10246 "set epilogue begin");
10247 break;
10249 case LI_set_discriminator:
10250 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
10251 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
10252 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
10253 dw2_asm_output_data_uleb128 (ent->val, NULL);
10254 break;
10258 /* Emit debug info for the address of the end of the table. */
10259 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
10260 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10261 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10262 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
10264 dw2_asm_output_data (1, 0, "end sequence");
10265 dw2_asm_output_data_uleb128 (1, NULL);
10266 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
10269 /* Output the source line number correspondence information. This
10270 information goes into the .debug_line section. */
10272 static void
10273 output_line_info (bool prologue_only)
10275 char l1[20], l2[20], p1[20], p2[20];
10276 /* We don't support DWARFv5 line tables yet. */
10277 int ver = dwarf_version < 5 ? dwarf_version : 4;
10278 bool saw_one = false;
10279 int opc;
10281 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
10282 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
10283 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
10284 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
10286 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10287 dw2_asm_output_data (4, 0xffffffff,
10288 "Initial length escape value indicating 64-bit DWARF extension");
10289 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
10290 "Length of Source Line Info");
10291 ASM_OUTPUT_LABEL (asm_out_file, l1);
10293 dw2_asm_output_data (2, ver, "DWARF Version");
10294 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
10295 ASM_OUTPUT_LABEL (asm_out_file, p1);
10297 /* Define the architecture-dependent minimum instruction length (in bytes).
10298 In this implementation of DWARF, this field is used for information
10299 purposes only. Since GCC generates assembly language, we have no
10300 a priori knowledge of how many instruction bytes are generated for each
10301 source line, and therefore can use only the DW_LNE_set_address and
10302 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
10303 this as '1', which is "correct enough" for all architectures,
10304 and don't let the target override. */
10305 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
10307 if (ver >= 4)
10308 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
10309 "Maximum Operations Per Instruction");
10310 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
10311 "Default is_stmt_start flag");
10312 dw2_asm_output_data (1, DWARF_LINE_BASE,
10313 "Line Base Value (Special Opcodes)");
10314 dw2_asm_output_data (1, DWARF_LINE_RANGE,
10315 "Line Range Value (Special Opcodes)");
10316 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
10317 "Special Opcode Base");
10319 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
10321 int n_op_args;
10322 switch (opc)
10324 case DW_LNS_advance_pc:
10325 case DW_LNS_advance_line:
10326 case DW_LNS_set_file:
10327 case DW_LNS_set_column:
10328 case DW_LNS_fixed_advance_pc:
10329 case DW_LNS_set_isa:
10330 n_op_args = 1;
10331 break;
10332 default:
10333 n_op_args = 0;
10334 break;
10337 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
10338 opc, n_op_args);
10341 /* Write out the information about the files we use. */
10342 output_file_names ();
10343 ASM_OUTPUT_LABEL (asm_out_file, p2);
10344 if (prologue_only)
10346 /* Output the marker for the end of the line number info. */
10347 ASM_OUTPUT_LABEL (asm_out_file, l2);
10348 return;
10351 if (separate_line_info)
10353 dw_line_info_table *table;
10354 size_t i;
10356 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
10357 if (table->in_use)
10359 output_one_line_info_table (table);
10360 saw_one = true;
10363 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
10365 output_one_line_info_table (cold_text_section_line_info);
10366 saw_one = true;
10369 /* ??? Some Darwin linkers crash on a .debug_line section with no
10370 sequences. Further, merely a DW_LNE_end_sequence entry is not
10371 sufficient -- the address column must also be initialized.
10372 Make sure to output at least one set_address/end_sequence pair,
10373 choosing .text since that section is always present. */
10374 if (text_section_line_info->in_use || !saw_one)
10375 output_one_line_info_table (text_section_line_info);
10377 /* Output the marker for the end of the line number info. */
10378 ASM_OUTPUT_LABEL (asm_out_file, l2);
10381 /* Given a pointer to a tree node for some base type, return a pointer to
10382 a DIE that describes the given type.
10384 This routine must only be called for GCC type nodes that correspond to
10385 Dwarf base (fundamental) types. */
10387 static dw_die_ref
10388 base_type_die (tree type)
10390 dw_die_ref base_type_result;
10391 enum dwarf_type encoding;
10393 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
10394 return 0;
10396 /* If this is a subtype that should not be emitted as a subrange type,
10397 use the base type. See subrange_type_for_debug_p. */
10398 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
10399 type = TREE_TYPE (type);
10401 switch (TREE_CODE (type))
10403 case INTEGER_TYPE:
10404 if ((dwarf_version >= 4 || !dwarf_strict)
10405 && TYPE_NAME (type)
10406 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10407 && DECL_IS_BUILTIN (TYPE_NAME (type))
10408 && DECL_NAME (TYPE_NAME (type)))
10410 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
10411 if (strcmp (name, "char16_t") == 0
10412 || strcmp (name, "char32_t") == 0)
10414 encoding = DW_ATE_UTF;
10415 break;
10418 if (TYPE_STRING_FLAG (type))
10420 if (TYPE_UNSIGNED (type))
10421 encoding = DW_ATE_unsigned_char;
10422 else
10423 encoding = DW_ATE_signed_char;
10425 else if (TYPE_UNSIGNED (type))
10426 encoding = DW_ATE_unsigned;
10427 else
10428 encoding = DW_ATE_signed;
10429 break;
10431 case REAL_TYPE:
10432 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
10434 if (dwarf_version >= 3 || !dwarf_strict)
10435 encoding = DW_ATE_decimal_float;
10436 else
10437 encoding = DW_ATE_lo_user;
10439 else
10440 encoding = DW_ATE_float;
10441 break;
10443 case FIXED_POINT_TYPE:
10444 if (!(dwarf_version >= 3 || !dwarf_strict))
10445 encoding = DW_ATE_lo_user;
10446 else if (TYPE_UNSIGNED (type))
10447 encoding = DW_ATE_unsigned_fixed;
10448 else
10449 encoding = DW_ATE_signed_fixed;
10450 break;
10452 /* Dwarf2 doesn't know anything about complex ints, so use
10453 a user defined type for it. */
10454 case COMPLEX_TYPE:
10455 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
10456 encoding = DW_ATE_complex_float;
10457 else
10458 encoding = DW_ATE_lo_user;
10459 break;
10461 case BOOLEAN_TYPE:
10462 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
10463 encoding = DW_ATE_boolean;
10464 break;
10466 default:
10467 /* No other TREE_CODEs are Dwarf fundamental types. */
10468 gcc_unreachable ();
10471 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
10473 add_AT_unsigned (base_type_result, DW_AT_byte_size,
10474 int_size_in_bytes (type));
10475 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
10476 add_pubtype (type, base_type_result);
10478 return base_type_result;
10481 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
10482 named 'auto' in its type: return true for it, false otherwise. */
10484 static inline bool
10485 is_cxx_auto (tree type)
10487 if (is_cxx ())
10489 tree name = TYPE_IDENTIFIER (type);
10490 if (name == get_identifier ("auto")
10491 || name == get_identifier ("decltype(auto)"))
10492 return true;
10494 return false;
10497 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
10498 given input type is a Dwarf "fundamental" type. Otherwise return null. */
10500 static inline int
10501 is_base_type (tree type)
10503 switch (TREE_CODE (type))
10505 case ERROR_MARK:
10506 case VOID_TYPE:
10507 case INTEGER_TYPE:
10508 case REAL_TYPE:
10509 case FIXED_POINT_TYPE:
10510 case COMPLEX_TYPE:
10511 case BOOLEAN_TYPE:
10512 case POINTER_BOUNDS_TYPE:
10513 return 1;
10515 case ARRAY_TYPE:
10516 case RECORD_TYPE:
10517 case UNION_TYPE:
10518 case QUAL_UNION_TYPE:
10519 case ENUMERAL_TYPE:
10520 case FUNCTION_TYPE:
10521 case METHOD_TYPE:
10522 case POINTER_TYPE:
10523 case REFERENCE_TYPE:
10524 case NULLPTR_TYPE:
10525 case OFFSET_TYPE:
10526 case LANG_TYPE:
10527 case VECTOR_TYPE:
10528 return 0;
10530 default:
10531 if (is_cxx_auto (type))
10532 return 0;
10533 gcc_unreachable ();
10536 return 0;
10539 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
10540 node, return the size in bits for the type if it is a constant, or else
10541 return the alignment for the type if the type's size is not constant, or
10542 else return BITS_PER_WORD if the type actually turns out to be an
10543 ERROR_MARK node. */
10545 static inline unsigned HOST_WIDE_INT
10546 simple_type_size_in_bits (const_tree type)
10548 if (TREE_CODE (type) == ERROR_MARK)
10549 return BITS_PER_WORD;
10550 else if (TYPE_SIZE (type) == NULL_TREE)
10551 return 0;
10552 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
10553 return tree_to_uhwi (TYPE_SIZE (type));
10554 else
10555 return TYPE_ALIGN (type);
10558 /* Similarly, but return an offset_int instead of UHWI. */
10560 static inline offset_int
10561 offset_int_type_size_in_bits (const_tree type)
10563 if (TREE_CODE (type) == ERROR_MARK)
10564 return BITS_PER_WORD;
10565 else if (TYPE_SIZE (type) == NULL_TREE)
10566 return 0;
10567 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
10568 return wi::to_offset (TYPE_SIZE (type));
10569 else
10570 return TYPE_ALIGN (type);
10573 /* Given a pointer to a tree node for a subrange type, return a pointer
10574 to a DIE that describes the given type. */
10576 static dw_die_ref
10577 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
10579 dw_die_ref subrange_die;
10580 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
10582 if (context_die == NULL)
10583 context_die = comp_unit_die ();
10585 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
10587 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
10589 /* The size of the subrange type and its base type do not match,
10590 so we need to generate a size attribute for the subrange type. */
10591 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
10594 if (low)
10595 add_bound_info (subrange_die, DW_AT_lower_bound, low, NULL);
10596 if (high)
10597 add_bound_info (subrange_die, DW_AT_upper_bound, high, NULL);
10599 return subrange_die;
10602 /* Returns the (const and/or volatile) cv_qualifiers associated with
10603 the decl node. This will normally be augmented with the
10604 cv_qualifiers of the underlying type in add_type_attribute. */
10606 static int
10607 decl_quals (const_tree decl)
10609 return ((TREE_READONLY (decl)
10610 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
10611 | (TREE_THIS_VOLATILE (decl)
10612 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
10615 /* Determine the TYPE whose qualifiers match the largest strict subset
10616 of the given TYPE_QUALS, and return its qualifiers. Ignore all
10617 qualifiers outside QUAL_MASK. */
10619 static int
10620 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
10622 tree t;
10623 int best_rank = 0, best_qual = 0, max_rank;
10625 type_quals &= qual_mask;
10626 max_rank = popcount_hwi (type_quals) - 1;
10628 for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
10629 t = TYPE_NEXT_VARIANT (t))
10631 int q = TYPE_QUALS (t) & qual_mask;
10633 if ((q & type_quals) == q && q != type_quals
10634 && check_base_type (t, type))
10636 int rank = popcount_hwi (q);
10638 if (rank > best_rank)
10640 best_rank = rank;
10641 best_qual = q;
10646 return best_qual;
10649 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
10650 entry that chains various modifiers in front of the given type. */
10652 static dw_die_ref
10653 modified_type_die (tree type, int cv_quals, dw_die_ref context_die)
10655 enum tree_code code = TREE_CODE (type);
10656 dw_die_ref mod_type_die;
10657 dw_die_ref sub_die = NULL;
10658 tree item_type = NULL;
10659 tree qualified_type;
10660 tree name, low, high;
10661 dw_die_ref mod_scope;
10662 /* Only these cv-qualifiers are currently handled. */
10663 const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
10664 | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC);
10666 if (code == ERROR_MARK)
10667 return NULL;
10669 cv_quals &= cv_qual_mask;
10671 /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
10672 tag modifier (and not an attribute) old consumers won't be able
10673 to handle it. */
10674 if (dwarf_version < 3)
10675 cv_quals &= ~TYPE_QUAL_RESTRICT;
10677 /* Likewise for DW_TAG_atomic_type for DWARFv5. */
10678 if (dwarf_version < 5)
10679 cv_quals &= ~TYPE_QUAL_ATOMIC;
10681 /* See if we already have the appropriately qualified variant of
10682 this type. */
10683 qualified_type = get_qualified_type (type, cv_quals);
10685 if (qualified_type == sizetype
10686 && TYPE_NAME (qualified_type)
10687 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
10689 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
10691 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
10692 && TYPE_PRECISION (t)
10693 == TYPE_PRECISION (qualified_type)
10694 && TYPE_UNSIGNED (t)
10695 == TYPE_UNSIGNED (qualified_type));
10696 qualified_type = t;
10699 /* If we do, then we can just use its DIE, if it exists. */
10700 if (qualified_type)
10702 mod_type_die = lookup_type_die (qualified_type);
10703 if (mod_type_die)
10704 return mod_type_die;
10707 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
10709 /* Handle C typedef types. */
10710 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
10711 && !DECL_ARTIFICIAL (name))
10713 tree dtype = TREE_TYPE (name);
10715 if (qualified_type == dtype)
10717 /* For a named type, use the typedef. */
10718 gen_type_die (qualified_type, context_die);
10719 return lookup_type_die (qualified_type);
10721 else
10723 int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
10724 dquals &= cv_qual_mask;
10725 if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
10726 || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
10727 /* cv-unqualified version of named type. Just use
10728 the unnamed type to which it refers. */
10729 return modified_type_die (DECL_ORIGINAL_TYPE (name),
10730 cv_quals, context_die);
10731 /* Else cv-qualified version of named type; fall through. */
10735 mod_scope = scope_die_for (type, context_die);
10737 if (cv_quals)
10739 struct qual_info { int q; enum dwarf_tag t; };
10740 static const struct qual_info qual_info[] =
10742 { TYPE_QUAL_ATOMIC, DW_TAG_atomic_type },
10743 { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
10744 { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
10745 { TYPE_QUAL_CONST, DW_TAG_const_type },
10747 int sub_quals;
10748 unsigned i;
10750 /* Determine a lesser qualified type that most closely matches
10751 this one. Then generate DW_TAG_* entries for the remaining
10752 qualifiers. */
10753 sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
10754 cv_qual_mask);
10755 mod_type_die = modified_type_die (type, sub_quals, context_die);
10757 for (i = 0; i < sizeof (qual_info) / sizeof (qual_info[0]); i++)
10758 if (qual_info[i].q & cv_quals & ~sub_quals)
10760 dw_die_ref d = new_die (qual_info[i].t, mod_scope, type);
10761 if (mod_type_die)
10762 add_AT_die_ref (d, DW_AT_type, mod_type_die);
10763 mod_type_die = d;
10766 else if (code == POINTER_TYPE)
10768 mod_type_die = new_die (DW_TAG_pointer_type, mod_scope, type);
10769 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10770 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10771 item_type = TREE_TYPE (type);
10772 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10773 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10774 TYPE_ADDR_SPACE (item_type));
10776 else if (code == REFERENCE_TYPE)
10778 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
10779 mod_type_die = new_die (DW_TAG_rvalue_reference_type, mod_scope,
10780 type);
10781 else
10782 mod_type_die = new_die (DW_TAG_reference_type, mod_scope, type);
10783 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10784 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10785 item_type = TREE_TYPE (type);
10786 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10787 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10788 TYPE_ADDR_SPACE (item_type));
10790 else if (code == INTEGER_TYPE
10791 && TREE_TYPE (type) != NULL_TREE
10792 && subrange_type_for_debug_p (type, &low, &high))
10794 mod_type_die = subrange_type_die (type, low, high, context_die);
10795 item_type = TREE_TYPE (type);
10797 else if (is_base_type (type))
10798 mod_type_die = base_type_die (type);
10799 else
10801 gen_type_die (type, context_die);
10803 /* We have to get the type_main_variant here (and pass that to the
10804 `lookup_type_die' routine) because the ..._TYPE node we have
10805 might simply be a *copy* of some original type node (where the
10806 copy was created to help us keep track of typedef names) and
10807 that copy might have a different TYPE_UID from the original
10808 ..._TYPE node. */
10809 if (TREE_CODE (type) != VECTOR_TYPE)
10810 return lookup_type_die (type_main_variant (type));
10811 else
10812 /* Vectors have the debugging information in the type,
10813 not the main variant. */
10814 return lookup_type_die (type);
10817 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
10818 don't output a DW_TAG_typedef, since there isn't one in the
10819 user's program; just attach a DW_AT_name to the type.
10820 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
10821 if the base type already has the same name. */
10822 if (name
10823 && ((TREE_CODE (name) != TYPE_DECL
10824 && (qualified_type == TYPE_MAIN_VARIANT (type)
10825 || (cv_quals == TYPE_UNQUALIFIED)))
10826 || (TREE_CODE (name) == TYPE_DECL
10827 && TREE_TYPE (name) == qualified_type
10828 && DECL_NAME (name))))
10830 if (TREE_CODE (name) == TYPE_DECL)
10831 /* Could just call add_name_and_src_coords_attributes here,
10832 but since this is a builtin type it doesn't have any
10833 useful source coordinates anyway. */
10834 name = DECL_NAME (name);
10835 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
10837 /* This probably indicates a bug. */
10838 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
10840 name = TYPE_IDENTIFIER (type);
10841 add_name_attribute (mod_type_die,
10842 name ? IDENTIFIER_POINTER (name) : "__unknown__");
10845 if (qualified_type)
10846 equate_type_number_to_die (qualified_type, mod_type_die);
10848 if (item_type)
10849 /* We must do this after the equate_type_number_to_die call, in case
10850 this is a recursive type. This ensures that the modified_type_die
10851 recursion will terminate even if the type is recursive. Recursive
10852 types are possible in Ada. */
10853 sub_die = modified_type_die (item_type,
10854 TYPE_QUALS_NO_ADDR_SPACE (item_type),
10855 context_die);
10857 if (sub_die != NULL)
10858 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
10860 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
10861 if (TYPE_ARTIFICIAL (type))
10862 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
10864 return mod_type_die;
10867 /* Generate DIEs for the generic parameters of T.
10868 T must be either a generic type or a generic function.
10869 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
10871 static void
10872 gen_generic_params_dies (tree t)
10874 tree parms, args;
10875 int parms_num, i;
10876 dw_die_ref die = NULL;
10877 int non_default;
10879 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
10880 return;
10882 if (TYPE_P (t))
10883 die = lookup_type_die (t);
10884 else if (DECL_P (t))
10885 die = lookup_decl_die (t);
10887 gcc_assert (die);
10889 parms = lang_hooks.get_innermost_generic_parms (t);
10890 if (!parms)
10891 /* T has no generic parameter. It means T is neither a generic type
10892 or function. End of story. */
10893 return;
10895 parms_num = TREE_VEC_LENGTH (parms);
10896 args = lang_hooks.get_innermost_generic_args (t);
10897 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
10898 non_default = int_cst_value (TREE_CHAIN (args));
10899 else
10900 non_default = TREE_VEC_LENGTH (args);
10901 for (i = 0; i < parms_num; i++)
10903 tree parm, arg, arg_pack_elems;
10904 dw_die_ref parm_die;
10906 parm = TREE_VEC_ELT (parms, i);
10907 arg = TREE_VEC_ELT (args, i);
10908 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
10909 gcc_assert (parm && TREE_VALUE (parm) && arg);
10911 if (parm && TREE_VALUE (parm) && arg)
10913 /* If PARM represents a template parameter pack,
10914 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
10915 by DW_TAG_template_*_parameter DIEs for the argument
10916 pack elements of ARG. Note that ARG would then be
10917 an argument pack. */
10918 if (arg_pack_elems)
10919 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
10920 arg_pack_elems,
10921 die);
10922 else
10923 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
10924 true /* emit name */, die);
10925 if (i >= non_default)
10926 add_AT_flag (parm_die, DW_AT_default_value, 1);
10931 /* Create and return a DIE for PARM which should be
10932 the representation of a generic type parameter.
10933 For instance, in the C++ front end, PARM would be a template parameter.
10934 ARG is the argument to PARM.
10935 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
10936 name of the PARM.
10937 PARENT_DIE is the parent DIE which the new created DIE should be added to,
10938 as a child node. */
10940 static dw_die_ref
10941 generic_parameter_die (tree parm, tree arg,
10942 bool emit_name_p,
10943 dw_die_ref parent_die)
10945 dw_die_ref tmpl_die = NULL;
10946 const char *name = NULL;
10948 if (!parm || !DECL_NAME (parm) || !arg)
10949 return NULL;
10951 /* We support non-type generic parameters and arguments,
10952 type generic parameters and arguments, as well as
10953 generic generic parameters (a.k.a. template template parameters in C++)
10954 and arguments. */
10955 if (TREE_CODE (parm) == PARM_DECL)
10956 /* PARM is a nontype generic parameter */
10957 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
10958 else if (TREE_CODE (parm) == TYPE_DECL)
10959 /* PARM is a type generic parameter. */
10960 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
10961 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10962 /* PARM is a generic generic parameter.
10963 Its DIE is a GNU extension. It shall have a
10964 DW_AT_name attribute to represent the name of the template template
10965 parameter, and a DW_AT_GNU_template_name attribute to represent the
10966 name of the template template argument. */
10967 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
10968 parent_die, parm);
10969 else
10970 gcc_unreachable ();
10972 if (tmpl_die)
10974 tree tmpl_type;
10976 /* If PARM is a generic parameter pack, it means we are
10977 emitting debug info for a template argument pack element.
10978 In other terms, ARG is a template argument pack element.
10979 In that case, we don't emit any DW_AT_name attribute for
10980 the die. */
10981 if (emit_name_p)
10983 name = IDENTIFIER_POINTER (DECL_NAME (parm));
10984 gcc_assert (name);
10985 add_AT_string (tmpl_die, DW_AT_name, name);
10988 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10990 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
10991 TMPL_DIE should have a child DW_AT_type attribute that is set
10992 to the type of the argument to PARM, which is ARG.
10993 If PARM is a type generic parameter, TMPL_DIE should have a
10994 child DW_AT_type that is set to ARG. */
10995 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
10996 add_type_attribute (tmpl_die, tmpl_type,
10997 (TREE_THIS_VOLATILE (tmpl_type)
10998 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
10999 parent_die);
11001 else
11003 /* So TMPL_DIE is a DIE representing a
11004 a generic generic template parameter, a.k.a template template
11005 parameter in C++ and arg is a template. */
11007 /* The DW_AT_GNU_template_name attribute of the DIE must be set
11008 to the name of the argument. */
11009 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
11010 if (name)
11011 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
11014 if (TREE_CODE (parm) == PARM_DECL)
11015 /* So PARM is a non-type generic parameter.
11016 DWARF3 5.6.8 says we must set a DW_AT_const_value child
11017 attribute of TMPL_DIE which value represents the value
11018 of ARG.
11019 We must be careful here:
11020 The value of ARG might reference some function decls.
11021 We might currently be emitting debug info for a generic
11022 type and types are emitted before function decls, we don't
11023 know if the function decls referenced by ARG will actually be
11024 emitted after cgraph computations.
11025 So must defer the generation of the DW_AT_const_value to
11026 after cgraph is ready. */
11027 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
11030 return tmpl_die;
11033 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
11034 PARM_PACK must be a template parameter pack. The returned DIE
11035 will be child DIE of PARENT_DIE. */
11037 static dw_die_ref
11038 template_parameter_pack_die (tree parm_pack,
11039 tree parm_pack_args,
11040 dw_die_ref parent_die)
11042 dw_die_ref die;
11043 int j;
11045 gcc_assert (parent_die && parm_pack);
11047 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
11048 add_name_and_src_coords_attributes (die, parm_pack);
11049 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
11050 generic_parameter_die (parm_pack,
11051 TREE_VEC_ELT (parm_pack_args, j),
11052 false /* Don't emit DW_AT_name */,
11053 die);
11054 return die;
11057 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
11058 an enumerated type. */
11060 static inline int
11061 type_is_enum (const_tree type)
11063 return TREE_CODE (type) == ENUMERAL_TYPE;
11066 /* Return the DBX register number described by a given RTL node. */
11068 static unsigned int
11069 dbx_reg_number (const_rtx rtl)
11071 unsigned regno = REGNO (rtl);
11073 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
11075 #ifdef LEAF_REG_REMAP
11076 if (crtl->uses_only_leaf_regs)
11078 int leaf_reg = LEAF_REG_REMAP (regno);
11079 if (leaf_reg != -1)
11080 regno = (unsigned) leaf_reg;
11082 #endif
11084 regno = DBX_REGISTER_NUMBER (regno);
11085 gcc_assert (regno != INVALID_REGNUM);
11086 return regno;
11089 /* Optionally add a DW_OP_piece term to a location description expression.
11090 DW_OP_piece is only added if the location description expression already
11091 doesn't end with DW_OP_piece. */
11093 static void
11094 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
11096 dw_loc_descr_ref loc;
11098 if (*list_head != NULL)
11100 /* Find the end of the chain. */
11101 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
11104 if (loc->dw_loc_opc != DW_OP_piece)
11105 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
11109 /* Return a location descriptor that designates a machine register or
11110 zero if there is none. */
11112 static dw_loc_descr_ref
11113 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
11115 rtx regs;
11117 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
11118 return 0;
11120 /* We only use "frame base" when we're sure we're talking about the
11121 post-prologue local stack frame. We do this by *not* running
11122 register elimination until this point, and recognizing the special
11123 argument pointer and soft frame pointer rtx's.
11124 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
11125 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
11126 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
11128 dw_loc_descr_ref result = NULL;
11130 if (dwarf_version >= 4 || !dwarf_strict)
11132 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
11133 initialized);
11134 if (result)
11135 add_loc_descr (&result,
11136 new_loc_descr (DW_OP_stack_value, 0, 0));
11138 return result;
11141 regs = targetm.dwarf_register_span (rtl);
11143 if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
11144 return multiple_reg_loc_descriptor (rtl, regs, initialized);
11145 else
11147 unsigned int dbx_regnum = dbx_reg_number (rtl);
11148 if (dbx_regnum == IGNORED_DWARF_REGNUM)
11149 return 0;
11150 return one_reg_loc_descriptor (dbx_regnum, initialized);
11154 /* Return a location descriptor that designates a machine register for
11155 a given hard register number. */
11157 static dw_loc_descr_ref
11158 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
11160 dw_loc_descr_ref reg_loc_descr;
11162 if (regno <= 31)
11163 reg_loc_descr
11164 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
11165 else
11166 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
11168 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11169 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11171 return reg_loc_descr;
11174 /* Given an RTL of a register, return a location descriptor that
11175 designates a value that spans more than one register. */
11177 static dw_loc_descr_ref
11178 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
11179 enum var_init_status initialized)
11181 int size, i;
11182 dw_loc_descr_ref loc_result = NULL;
11184 /* Simple, contiguous registers. */
11185 if (regs == NULL_RTX)
11187 unsigned reg = REGNO (rtl);
11188 int nregs;
11190 #ifdef LEAF_REG_REMAP
11191 if (crtl->uses_only_leaf_regs)
11193 int leaf_reg = LEAF_REG_REMAP (reg);
11194 if (leaf_reg != -1)
11195 reg = (unsigned) leaf_reg;
11197 #endif
11199 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
11200 nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
11202 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
11204 loc_result = NULL;
11205 while (nregs--)
11207 dw_loc_descr_ref t;
11209 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
11210 VAR_INIT_STATUS_INITIALIZED);
11211 add_loc_descr (&loc_result, t);
11212 add_loc_descr_op_piece (&loc_result, size);
11213 ++reg;
11215 return loc_result;
11218 /* Now onto stupid register sets in non contiguous locations. */
11220 gcc_assert (GET_CODE (regs) == PARALLEL);
11222 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
11223 loc_result = NULL;
11225 for (i = 0; i < XVECLEN (regs, 0); ++i)
11227 dw_loc_descr_ref t;
11229 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
11230 VAR_INIT_STATUS_INITIALIZED);
11231 add_loc_descr (&loc_result, t);
11232 add_loc_descr_op_piece (&loc_result, size);
11235 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
11236 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11237 return loc_result;
11240 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
11242 /* Return a location descriptor that designates a constant i,
11243 as a compound operation from constant (i >> shift), constant shift
11244 and DW_OP_shl. */
11246 static dw_loc_descr_ref
11247 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11249 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
11250 add_loc_descr (&ret, int_loc_descriptor (shift));
11251 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11252 return ret;
11255 /* Return a location descriptor that designates a constant. */
11257 static dw_loc_descr_ref
11258 int_loc_descriptor (HOST_WIDE_INT i)
11260 enum dwarf_location_atom op;
11262 /* Pick the smallest representation of a constant, rather than just
11263 defaulting to the LEB encoding. */
11264 if (i >= 0)
11266 int clz = clz_hwi (i);
11267 int ctz = ctz_hwi (i);
11268 if (i <= 31)
11269 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
11270 else if (i <= 0xff)
11271 op = DW_OP_const1u;
11272 else if (i <= 0xffff)
11273 op = DW_OP_const2u;
11274 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11275 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11276 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
11277 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
11278 while DW_OP_const4u is 5 bytes. */
11279 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
11280 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11281 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11282 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
11283 while DW_OP_const4u is 5 bytes. */
11284 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11285 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11286 op = DW_OP_const4u;
11287 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11288 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11289 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
11290 while DW_OP_constu of constant >= 0x100000000 takes at least
11291 6 bytes. */
11292 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11293 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11294 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
11295 >= HOST_BITS_PER_WIDE_INT)
11296 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
11297 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
11298 while DW_OP_constu takes in this case at least 6 bytes. */
11299 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
11300 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11301 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11302 && size_of_uleb128 (i) > 6)
11303 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
11304 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
11305 else
11306 op = DW_OP_constu;
11308 else
11310 if (i >= -0x80)
11311 op = DW_OP_const1s;
11312 else if (i >= -0x8000)
11313 op = DW_OP_const2s;
11314 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11316 if (size_of_int_loc_descriptor (i) < 5)
11318 dw_loc_descr_ref ret = int_loc_descriptor (-i);
11319 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11320 return ret;
11322 op = DW_OP_const4s;
11324 else
11326 if (size_of_int_loc_descriptor (i)
11327 < (unsigned long) 1 + size_of_sleb128 (i))
11329 dw_loc_descr_ref ret = int_loc_descriptor (-i);
11330 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11331 return ret;
11333 op = DW_OP_consts;
11337 return new_loc_descr (op, i, 0);
11340 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
11341 without actually allocating it. */
11343 static unsigned long
11344 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11346 return size_of_int_loc_descriptor (i >> shift)
11347 + size_of_int_loc_descriptor (shift)
11348 + 1;
11351 /* Return size_of_locs (int_loc_descriptor (i)) without
11352 actually allocating it. */
11354 static unsigned long
11355 size_of_int_loc_descriptor (HOST_WIDE_INT i)
11357 unsigned long s;
11359 if (i >= 0)
11361 int clz, ctz;
11362 if (i <= 31)
11363 return 1;
11364 else if (i <= 0xff)
11365 return 2;
11366 else if (i <= 0xffff)
11367 return 3;
11368 clz = clz_hwi (i);
11369 ctz = ctz_hwi (i);
11370 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11371 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11372 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11373 - clz - 5);
11374 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11375 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11376 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11377 - clz - 8);
11378 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11379 return 5;
11380 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
11381 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11382 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11383 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11384 - clz - 8);
11385 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11386 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
11387 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11388 - clz - 16);
11389 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11390 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11391 && s > 6)
11392 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11393 - clz - 32);
11394 else
11395 return 1 + s;
11397 else
11399 if (i >= -0x80)
11400 return 2;
11401 else if (i >= -0x8000)
11402 return 3;
11403 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11405 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11407 s = size_of_int_loc_descriptor (-i) + 1;
11408 if (s < 5)
11409 return s;
11411 return 5;
11413 else
11415 unsigned long r = 1 + size_of_sleb128 (i);
11416 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11418 s = size_of_int_loc_descriptor (-i) + 1;
11419 if (s < r)
11420 return s;
11422 return r;
11427 /* Return loc description representing "address" of integer value.
11428 This can appear only as toplevel expression. */
11430 static dw_loc_descr_ref
11431 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
11433 int litsize;
11434 dw_loc_descr_ref loc_result = NULL;
11436 if (!(dwarf_version >= 4 || !dwarf_strict))
11437 return NULL;
11439 litsize = size_of_int_loc_descriptor (i);
11440 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
11441 is more compact. For DW_OP_stack_value we need:
11442 litsize + 1 (DW_OP_stack_value)
11443 and for DW_OP_implicit_value:
11444 1 (DW_OP_implicit_value) + 1 (length) + size. */
11445 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
11447 loc_result = int_loc_descriptor (i);
11448 add_loc_descr (&loc_result,
11449 new_loc_descr (DW_OP_stack_value, 0, 0));
11450 return loc_result;
11453 loc_result = new_loc_descr (DW_OP_implicit_value,
11454 size, 0);
11455 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
11456 loc_result->dw_loc_oprnd2.v.val_int = i;
11457 return loc_result;
11460 /* Return a location descriptor that designates a base+offset location. */
11462 static dw_loc_descr_ref
11463 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
11464 enum var_init_status initialized)
11466 unsigned int regno;
11467 dw_loc_descr_ref result;
11468 dw_fde_ref fde = cfun->fde;
11470 /* We only use "frame base" when we're sure we're talking about the
11471 post-prologue local stack frame. We do this by *not* running
11472 register elimination until this point, and recognizing the special
11473 argument pointer and soft frame pointer rtx's. */
11474 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
11476 rtx elim = (ira_use_lra_p
11477 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
11478 : eliminate_regs (reg, VOIDmode, NULL_RTX));
11480 if (elim != reg)
11482 if (GET_CODE (elim) == PLUS)
11484 offset += INTVAL (XEXP (elim, 1));
11485 elim = XEXP (elim, 0);
11487 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
11488 && (elim == hard_frame_pointer_rtx
11489 || elim == stack_pointer_rtx))
11490 || elim == (frame_pointer_needed
11491 ? hard_frame_pointer_rtx
11492 : stack_pointer_rtx));
11494 /* If drap register is used to align stack, use frame
11495 pointer + offset to access stack variables. If stack
11496 is aligned without drap, use stack pointer + offset to
11497 access stack variables. */
11498 if (crtl->stack_realign_tried
11499 && reg == frame_pointer_rtx)
11501 int base_reg
11502 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
11503 ? HARD_FRAME_POINTER_REGNUM
11504 : REGNO (elim));
11505 return new_reg_loc_descr (base_reg, offset);
11508 gcc_assert (frame_pointer_fb_offset_valid);
11509 offset += frame_pointer_fb_offset;
11510 return new_loc_descr (DW_OP_fbreg, offset, 0);
11514 regno = REGNO (reg);
11515 #ifdef LEAF_REG_REMAP
11516 if (crtl->uses_only_leaf_regs)
11518 int leaf_reg = LEAF_REG_REMAP (regno);
11519 if (leaf_reg != -1)
11520 regno = (unsigned) leaf_reg;
11522 #endif
11523 regno = DWARF_FRAME_REGNUM (regno);
11525 if (!optimize && fde
11526 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
11528 /* Use cfa+offset to represent the location of arguments passed
11529 on the stack when drap is used to align stack.
11530 Only do this when not optimizing, for optimized code var-tracking
11531 is supposed to track where the arguments live and the register
11532 used as vdrap or drap in some spot might be used for something
11533 else in other part of the routine. */
11534 return new_loc_descr (DW_OP_fbreg, offset, 0);
11537 if (regno <= 31)
11538 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
11539 offset, 0);
11540 else
11541 result = new_loc_descr (DW_OP_bregx, regno, offset);
11543 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11544 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11546 return result;
11549 /* Return true if this RTL expression describes a base+offset calculation. */
11551 static inline int
11552 is_based_loc (const_rtx rtl)
11554 return (GET_CODE (rtl) == PLUS
11555 && ((REG_P (XEXP (rtl, 0))
11556 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
11557 && CONST_INT_P (XEXP (rtl, 1)))));
11560 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
11561 failed. */
11563 static dw_loc_descr_ref
11564 tls_mem_loc_descriptor (rtx mem)
11566 tree base;
11567 dw_loc_descr_ref loc_result;
11569 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
11570 return NULL;
11572 base = get_base_address (MEM_EXPR (mem));
11573 if (base == NULL
11574 || TREE_CODE (base) != VAR_DECL
11575 || !DECL_THREAD_LOCAL_P (base))
11576 return NULL;
11578 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
11579 if (loc_result == NULL)
11580 return NULL;
11582 if (MEM_OFFSET (mem))
11583 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
11585 return loc_result;
11588 /* Output debug info about reason why we failed to expand expression as dwarf
11589 expression. */
11591 static void
11592 expansion_failed (tree expr, rtx rtl, char const *reason)
11594 if (dump_file && (dump_flags & TDF_DETAILS))
11596 fprintf (dump_file, "Failed to expand as dwarf: ");
11597 if (expr)
11598 print_generic_expr (dump_file, expr, dump_flags);
11599 if (rtl)
11601 fprintf (dump_file, "\n");
11602 print_rtl (dump_file, rtl);
11604 fprintf (dump_file, "\nReason: %s\n", reason);
11608 /* Helper function for const_ok_for_output. */
11610 static bool
11611 const_ok_for_output_1 (rtx rtl)
11613 if (GET_CODE (rtl) == UNSPEC)
11615 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
11616 we can't express it in the debug info. */
11617 #ifdef ENABLE_CHECKING
11618 /* Don't complain about TLS UNSPECs, those are just too hard to
11619 delegitimize. Note this could be a non-decl SYMBOL_REF such as
11620 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
11621 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
11622 if (XVECLEN (rtl, 0) == 0
11623 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
11624 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE)
11625 inform (current_function_decl
11626 ? DECL_SOURCE_LOCATION (current_function_decl)
11627 : UNKNOWN_LOCATION,
11628 #if NUM_UNSPEC_VALUES > 0
11629 "non-delegitimized UNSPEC %s (%d) found in variable location",
11630 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
11631 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
11632 XINT (rtl, 1));
11633 #else
11634 "non-delegitimized UNSPEC %d found in variable location",
11635 XINT (rtl, 1));
11636 #endif
11637 #endif
11638 expansion_failed (NULL_TREE, rtl,
11639 "UNSPEC hasn't been delegitimized.\n");
11640 return false;
11643 if (targetm.const_not_ok_for_debug_p (rtl))
11645 expansion_failed (NULL_TREE, rtl,
11646 "Expression rejected for debug by the backend.\n");
11647 return false;
11650 /* FIXME: Refer to PR60655. It is possible for simplification
11651 of rtl expressions in var tracking to produce such expressions.
11652 We should really identify / validate expressions
11653 enclosed in CONST that can be handled by assemblers on various
11654 targets and only handle legitimate cases here. */
11655 if (GET_CODE (rtl) != SYMBOL_REF)
11657 if (GET_CODE (rtl) == NOT)
11658 return false;
11659 return true;
11662 if (CONSTANT_POOL_ADDRESS_P (rtl))
11664 bool marked;
11665 get_pool_constant_mark (rtl, &marked);
11666 /* If all references to this pool constant were optimized away,
11667 it was not output and thus we can't represent it. */
11668 if (!marked)
11670 expansion_failed (NULL_TREE, rtl,
11671 "Constant was removed from constant pool.\n");
11672 return false;
11676 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11677 return false;
11679 /* Avoid references to external symbols in debug info, on several targets
11680 the linker might even refuse to link when linking a shared library,
11681 and in many other cases the relocations for .debug_info/.debug_loc are
11682 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
11683 to be defined within the same shared library or executable are fine. */
11684 if (SYMBOL_REF_EXTERNAL_P (rtl))
11686 tree decl = SYMBOL_REF_DECL (rtl);
11688 if (decl == NULL || !targetm.binds_local_p (decl))
11690 expansion_failed (NULL_TREE, rtl,
11691 "Symbol not defined in current TU.\n");
11692 return false;
11696 return true;
11699 /* Return true if constant RTL can be emitted in DW_OP_addr or
11700 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
11701 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
11703 static bool
11704 const_ok_for_output (rtx rtl)
11706 if (GET_CODE (rtl) == SYMBOL_REF)
11707 return const_ok_for_output_1 (rtl);
11709 if (GET_CODE (rtl) == CONST)
11711 subrtx_var_iterator::array_type array;
11712 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
11713 if (!const_ok_for_output_1 (*iter))
11714 return false;
11715 return true;
11718 return true;
11721 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
11722 if possible, NULL otherwise. */
11724 static dw_die_ref
11725 base_type_for_mode (machine_mode mode, bool unsignedp)
11727 dw_die_ref type_die;
11728 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11730 if (type == NULL)
11731 return NULL;
11732 switch (TREE_CODE (type))
11734 case INTEGER_TYPE:
11735 case REAL_TYPE:
11736 break;
11737 default:
11738 return NULL;
11740 type_die = lookup_type_die (type);
11741 if (!type_die)
11742 type_die = modified_type_die (type, TYPE_UNQUALIFIED, comp_unit_die ());
11743 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
11744 return NULL;
11745 return type_die;
11748 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
11749 type matching MODE, or, if MODE is narrower than or as wide as
11750 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
11751 possible. */
11753 static dw_loc_descr_ref
11754 convert_descriptor_to_mode (machine_mode mode, dw_loc_descr_ref op)
11756 machine_mode outer_mode = mode;
11757 dw_die_ref type_die;
11758 dw_loc_descr_ref cvt;
11760 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11762 add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
11763 return op;
11765 type_die = base_type_for_mode (outer_mode, 1);
11766 if (type_die == NULL)
11767 return NULL;
11768 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11769 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11770 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11771 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11772 add_loc_descr (&op, cvt);
11773 return op;
11776 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
11778 static dw_loc_descr_ref
11779 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
11780 dw_loc_descr_ref op1)
11782 dw_loc_descr_ref ret = op0;
11783 add_loc_descr (&ret, op1);
11784 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11785 if (STORE_FLAG_VALUE != 1)
11787 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
11788 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
11790 return ret;
11793 /* Return location descriptor for signed comparison OP RTL. */
11795 static dw_loc_descr_ref
11796 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11797 machine_mode mem_mode)
11799 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11800 dw_loc_descr_ref op0, op1;
11801 int shift;
11803 if (op_mode == VOIDmode)
11804 op_mode = GET_MODE (XEXP (rtl, 1));
11805 if (op_mode == VOIDmode)
11806 return NULL;
11808 if (dwarf_strict
11809 && (GET_MODE_CLASS (op_mode) != MODE_INT
11810 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
11811 return NULL;
11813 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11814 VAR_INIT_STATUS_INITIALIZED);
11815 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11816 VAR_INIT_STATUS_INITIALIZED);
11818 if (op0 == NULL || op1 == NULL)
11819 return NULL;
11821 if (GET_MODE_CLASS (op_mode) != MODE_INT
11822 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11823 return compare_loc_descriptor (op, op0, op1);
11825 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11827 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
11828 dw_loc_descr_ref cvt;
11830 if (type_die == NULL)
11831 return NULL;
11832 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11833 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11834 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11835 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11836 add_loc_descr (&op0, cvt);
11837 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11838 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11839 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11840 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11841 add_loc_descr (&op1, cvt);
11842 return compare_loc_descriptor (op, op0, op1);
11845 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
11846 /* For eq/ne, if the operands are known to be zero-extended,
11847 there is no need to do the fancy shifting up. */
11848 if (op == DW_OP_eq || op == DW_OP_ne)
11850 dw_loc_descr_ref last0, last1;
11851 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11853 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11855 /* deref_size zero extends, and for constants we can check
11856 whether they are zero extended or not. */
11857 if (((last0->dw_loc_opc == DW_OP_deref_size
11858 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11859 || (CONST_INT_P (XEXP (rtl, 0))
11860 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
11861 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
11862 && ((last1->dw_loc_opc == DW_OP_deref_size
11863 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11864 || (CONST_INT_P (XEXP (rtl, 1))
11865 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
11866 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
11867 return compare_loc_descriptor (op, op0, op1);
11869 /* EQ/NE comparison against constant in narrower type than
11870 DWARF2_ADDR_SIZE can be performed either as
11871 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
11872 DW_OP_{eq,ne}
11874 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
11875 DW_OP_{eq,ne}. Pick whatever is shorter. */
11876 if (CONST_INT_P (XEXP (rtl, 1))
11877 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
11878 && (size_of_int_loc_descriptor (shift) + 1
11879 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
11880 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
11881 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11882 & GET_MODE_MASK (op_mode))))
11884 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
11885 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11886 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11887 & GET_MODE_MASK (op_mode));
11888 return compare_loc_descriptor (op, op0, op1);
11891 add_loc_descr (&op0, int_loc_descriptor (shift));
11892 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11893 if (CONST_INT_P (XEXP (rtl, 1)))
11894 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
11895 else
11897 add_loc_descr (&op1, int_loc_descriptor (shift));
11898 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11900 return compare_loc_descriptor (op, op0, op1);
11903 /* Return location descriptor for unsigned comparison OP RTL. */
11905 static dw_loc_descr_ref
11906 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11907 machine_mode mem_mode)
11909 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11910 dw_loc_descr_ref op0, op1;
11912 if (op_mode == VOIDmode)
11913 op_mode = GET_MODE (XEXP (rtl, 1));
11914 if (op_mode == VOIDmode)
11915 return NULL;
11916 if (GET_MODE_CLASS (op_mode) != MODE_INT)
11917 return NULL;
11919 if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11920 return NULL;
11922 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11923 VAR_INIT_STATUS_INITIALIZED);
11924 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11925 VAR_INIT_STATUS_INITIALIZED);
11927 if (op0 == NULL || op1 == NULL)
11928 return NULL;
11930 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
11932 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
11933 dw_loc_descr_ref last0, last1;
11934 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11936 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11938 if (CONST_INT_P (XEXP (rtl, 0)))
11939 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
11940 /* deref_size zero extends, so no need to mask it again. */
11941 else if (last0->dw_loc_opc != DW_OP_deref_size
11942 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11944 add_loc_descr (&op0, int_loc_descriptor (mask));
11945 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11947 if (CONST_INT_P (XEXP (rtl, 1)))
11948 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
11949 /* deref_size zero extends, so no need to mask it again. */
11950 else if (last1->dw_loc_opc != DW_OP_deref_size
11951 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11953 add_loc_descr (&op1, int_loc_descriptor (mask));
11954 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11957 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11959 HOST_WIDE_INT bias = 1;
11960 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11961 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11962 if (CONST_INT_P (XEXP (rtl, 1)))
11963 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
11964 + INTVAL (XEXP (rtl, 1)));
11965 else
11966 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
11967 bias, 0));
11969 return compare_loc_descriptor (op, op0, op1);
11972 /* Return location descriptor for {U,S}{MIN,MAX}. */
11974 static dw_loc_descr_ref
11975 minmax_loc_descriptor (rtx rtl, machine_mode mode,
11976 machine_mode mem_mode)
11978 enum dwarf_location_atom op;
11979 dw_loc_descr_ref op0, op1, ret;
11980 dw_loc_descr_ref bra_node, drop_node;
11982 if (dwarf_strict
11983 && (GET_MODE_CLASS (mode) != MODE_INT
11984 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
11985 return NULL;
11987 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11988 VAR_INIT_STATUS_INITIALIZED);
11989 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11990 VAR_INIT_STATUS_INITIALIZED);
11992 if (op0 == NULL || op1 == NULL)
11993 return NULL;
11995 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
11996 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
11997 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
11998 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
12000 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12002 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
12003 add_loc_descr (&op0, int_loc_descriptor (mask));
12004 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
12005 add_loc_descr (&op1, int_loc_descriptor (mask));
12006 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
12008 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12010 HOST_WIDE_INT bias = 1;
12011 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
12012 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12013 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12016 else if (GET_MODE_CLASS (mode) == MODE_INT
12017 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12019 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
12020 add_loc_descr (&op0, int_loc_descriptor (shift));
12021 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
12022 add_loc_descr (&op1, int_loc_descriptor (shift));
12023 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
12025 else if (GET_MODE_CLASS (mode) == MODE_INT
12026 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12028 dw_die_ref type_die = base_type_for_mode (mode, 0);
12029 dw_loc_descr_ref cvt;
12030 if (type_die == NULL)
12031 return NULL;
12032 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12033 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12034 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12035 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12036 add_loc_descr (&op0, cvt);
12037 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12038 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12039 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12040 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12041 add_loc_descr (&op1, cvt);
12044 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
12045 op = DW_OP_lt;
12046 else
12047 op = DW_OP_gt;
12048 ret = op0;
12049 add_loc_descr (&ret, op1);
12050 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
12051 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12052 add_loc_descr (&ret, bra_node);
12053 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12054 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
12055 add_loc_descr (&ret, drop_node);
12056 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12057 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
12058 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
12059 && GET_MODE_CLASS (mode) == MODE_INT
12060 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12061 ret = convert_descriptor_to_mode (mode, ret);
12062 return ret;
12065 /* Helper function for mem_loc_descriptor. Perform OP binary op,
12066 but after converting arguments to type_die, afterwards
12067 convert back to unsigned. */
12069 static dw_loc_descr_ref
12070 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
12071 machine_mode mode, machine_mode mem_mode)
12073 dw_loc_descr_ref cvt, op0, op1;
12075 if (type_die == NULL)
12076 return NULL;
12077 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12078 VAR_INIT_STATUS_INITIALIZED);
12079 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12080 VAR_INIT_STATUS_INITIALIZED);
12081 if (op0 == NULL || op1 == NULL)
12082 return NULL;
12083 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12084 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12085 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12086 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12087 add_loc_descr (&op0, cvt);
12088 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12089 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12090 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12091 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12092 add_loc_descr (&op1, cvt);
12093 add_loc_descr (&op0, op1);
12094 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
12095 return convert_descriptor_to_mode (mode, op0);
12098 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
12099 const0 is DW_OP_lit0 or corresponding typed constant,
12100 const1 is DW_OP_lit1 or corresponding typed constant
12101 and constMSB is constant with just the MSB bit set
12102 for the mode):
12103 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
12104 L1: const0 DW_OP_swap
12105 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
12106 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12107 L3: DW_OP_drop
12108 L4: DW_OP_nop
12110 CTZ is similar:
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 const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
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 FFS is similar:
12119 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
12120 L1: const1 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 static dw_loc_descr_ref
12127 clz_loc_descriptor (rtx rtl, machine_mode mode,
12128 machine_mode mem_mode)
12130 dw_loc_descr_ref op0, ret, tmp;
12131 HOST_WIDE_INT valv;
12132 dw_loc_descr_ref l1jump, l1label;
12133 dw_loc_descr_ref l2jump, l2label;
12134 dw_loc_descr_ref l3jump, l3label;
12135 dw_loc_descr_ref l4jump, l4label;
12136 rtx msb;
12138 if (GET_MODE_CLASS (mode) != MODE_INT
12139 || GET_MODE (XEXP (rtl, 0)) != mode)
12140 return NULL;
12142 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12143 VAR_INIT_STATUS_INITIALIZED);
12144 if (op0 == NULL)
12145 return NULL;
12146 ret = op0;
12147 if (GET_CODE (rtl) == CLZ)
12149 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12150 valv = GET_MODE_BITSIZE (mode);
12152 else if (GET_CODE (rtl) == FFS)
12153 valv = 0;
12154 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12155 valv = GET_MODE_BITSIZE (mode);
12156 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12157 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
12158 add_loc_descr (&ret, l1jump);
12159 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12160 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
12161 VAR_INIT_STATUS_INITIALIZED);
12162 if (tmp == NULL)
12163 return NULL;
12164 add_loc_descr (&ret, tmp);
12165 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
12166 add_loc_descr (&ret, l4jump);
12167 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
12168 ? const1_rtx : const0_rtx,
12169 mode, mem_mode,
12170 VAR_INIT_STATUS_INITIALIZED);
12171 if (l1label == NULL)
12172 return NULL;
12173 add_loc_descr (&ret, l1label);
12174 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12175 l2label = new_loc_descr (DW_OP_dup, 0, 0);
12176 add_loc_descr (&ret, l2label);
12177 if (GET_CODE (rtl) != CLZ)
12178 msb = const1_rtx;
12179 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
12180 msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
12181 << (GET_MODE_BITSIZE (mode) - 1));
12182 else
12183 msb = immed_wide_int_const
12184 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
12185 GET_MODE_PRECISION (mode)), mode);
12186 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
12187 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12188 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
12189 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
12190 else
12191 tmp = mem_loc_descriptor (msb, mode, mem_mode,
12192 VAR_INIT_STATUS_INITIALIZED);
12193 if (tmp == NULL)
12194 return NULL;
12195 add_loc_descr (&ret, tmp);
12196 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12197 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
12198 add_loc_descr (&ret, l3jump);
12199 tmp = mem_loc_descriptor (const1_rtx, 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 (GET_CODE (rtl) == CLZ
12205 ? DW_OP_shl : DW_OP_shr, 0, 0));
12206 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12207 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
12208 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12209 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
12210 add_loc_descr (&ret, l2jump);
12211 l3label = new_loc_descr (DW_OP_drop, 0, 0);
12212 add_loc_descr (&ret, l3label);
12213 l4label = new_loc_descr (DW_OP_nop, 0, 0);
12214 add_loc_descr (&ret, l4label);
12215 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12216 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12217 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12218 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12219 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12220 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
12221 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12222 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
12223 return ret;
12226 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
12227 const1 is DW_OP_lit1 or corresponding typed constant):
12228 const0 DW_OP_swap
12229 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
12230 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
12231 L2: DW_OP_drop
12233 PARITY is similar:
12234 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
12235 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
12236 L2: DW_OP_drop */
12238 static dw_loc_descr_ref
12239 popcount_loc_descriptor (rtx rtl, machine_mode mode,
12240 machine_mode mem_mode)
12242 dw_loc_descr_ref op0, ret, tmp;
12243 dw_loc_descr_ref l1jump, l1label;
12244 dw_loc_descr_ref l2jump, l2label;
12246 if (GET_MODE_CLASS (mode) != MODE_INT
12247 || GET_MODE (XEXP (rtl, 0)) != mode)
12248 return NULL;
12250 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12251 VAR_INIT_STATUS_INITIALIZED);
12252 if (op0 == NULL)
12253 return NULL;
12254 ret = op0;
12255 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12256 VAR_INIT_STATUS_INITIALIZED);
12257 if (tmp == NULL)
12258 return NULL;
12259 add_loc_descr (&ret, tmp);
12260 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12261 l1label = new_loc_descr (DW_OP_dup, 0, 0);
12262 add_loc_descr (&ret, l1label);
12263 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12264 add_loc_descr (&ret, l2jump);
12265 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12266 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12267 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12268 VAR_INIT_STATUS_INITIALIZED);
12269 if (tmp == NULL)
12270 return NULL;
12271 add_loc_descr (&ret, tmp);
12272 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12273 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
12274 ? DW_OP_plus : DW_OP_xor, 0, 0));
12275 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12276 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12277 VAR_INIT_STATUS_INITIALIZED);
12278 add_loc_descr (&ret, tmp);
12279 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12280 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12281 add_loc_descr (&ret, l1jump);
12282 l2label = new_loc_descr (DW_OP_drop, 0, 0);
12283 add_loc_descr (&ret, l2label);
12284 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12285 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12286 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12287 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12288 return ret;
12291 /* BSWAP (constS is initial shift count, either 56 or 24):
12292 constS const0
12293 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
12294 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
12295 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
12296 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
12297 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
12299 static dw_loc_descr_ref
12300 bswap_loc_descriptor (rtx rtl, machine_mode mode,
12301 machine_mode mem_mode)
12303 dw_loc_descr_ref op0, ret, tmp;
12304 dw_loc_descr_ref l1jump, l1label;
12305 dw_loc_descr_ref l2jump, l2label;
12307 if (GET_MODE_CLASS (mode) != MODE_INT
12308 || BITS_PER_UNIT != 8
12309 || (GET_MODE_BITSIZE (mode) != 32
12310 && GET_MODE_BITSIZE (mode) != 64))
12311 return NULL;
12313 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12314 VAR_INIT_STATUS_INITIALIZED);
12315 if (op0 == NULL)
12316 return NULL;
12318 ret = op0;
12319 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12320 mode, mem_mode,
12321 VAR_INIT_STATUS_INITIALIZED);
12322 if (tmp == NULL)
12323 return NULL;
12324 add_loc_descr (&ret, tmp);
12325 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12326 VAR_INIT_STATUS_INITIALIZED);
12327 if (tmp == NULL)
12328 return NULL;
12329 add_loc_descr (&ret, tmp);
12330 l1label = new_loc_descr (DW_OP_pick, 2, 0);
12331 add_loc_descr (&ret, l1label);
12332 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12333 mode, mem_mode,
12334 VAR_INIT_STATUS_INITIALIZED);
12335 add_loc_descr (&ret, tmp);
12336 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
12337 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12338 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12339 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
12340 VAR_INIT_STATUS_INITIALIZED);
12341 if (tmp == NULL)
12342 return NULL;
12343 add_loc_descr (&ret, tmp);
12344 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12345 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
12346 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12347 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12348 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12349 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12350 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12351 VAR_INIT_STATUS_INITIALIZED);
12352 add_loc_descr (&ret, tmp);
12353 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
12354 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12355 add_loc_descr (&ret, l2jump);
12356 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
12357 VAR_INIT_STATUS_INITIALIZED);
12358 add_loc_descr (&ret, tmp);
12359 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12360 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12361 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12362 add_loc_descr (&ret, l1jump);
12363 l2label = new_loc_descr (DW_OP_drop, 0, 0);
12364 add_loc_descr (&ret, l2label);
12365 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12366 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12367 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12368 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12369 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12370 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12371 return ret;
12374 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
12375 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12376 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
12377 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
12379 ROTATERT is similar:
12380 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
12381 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12382 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
12384 static dw_loc_descr_ref
12385 rotate_loc_descriptor (rtx rtl, machine_mode mode,
12386 machine_mode mem_mode)
12388 rtx rtlop1 = XEXP (rtl, 1);
12389 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
12390 int i;
12392 if (GET_MODE_CLASS (mode) != MODE_INT)
12393 return NULL;
12395 if (GET_MODE (rtlop1) != VOIDmode
12396 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
12397 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12398 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12399 VAR_INIT_STATUS_INITIALIZED);
12400 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12401 VAR_INIT_STATUS_INITIALIZED);
12402 if (op0 == NULL || op1 == NULL)
12403 return NULL;
12404 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12405 for (i = 0; i < 2; i++)
12407 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
12408 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
12409 mode, mem_mode,
12410 VAR_INIT_STATUS_INITIALIZED);
12411 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12412 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12413 ? DW_OP_const4u
12414 : HOST_BITS_PER_WIDE_INT == 64
12415 ? DW_OP_const8u : DW_OP_constu,
12416 GET_MODE_MASK (mode), 0);
12417 else
12418 mask[i] = NULL;
12419 if (mask[i] == NULL)
12420 return NULL;
12421 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
12423 ret = op0;
12424 add_loc_descr (&ret, op1);
12425 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12426 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12427 if (GET_CODE (rtl) == ROTATERT)
12429 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12430 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12431 GET_MODE_BITSIZE (mode), 0));
12433 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12434 if (mask[0] != NULL)
12435 add_loc_descr (&ret, mask[0]);
12436 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12437 if (mask[1] != NULL)
12439 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12440 add_loc_descr (&ret, mask[1]);
12441 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12443 if (GET_CODE (rtl) == ROTATE)
12445 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12446 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12447 GET_MODE_BITSIZE (mode), 0));
12449 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12450 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12451 return ret;
12454 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
12455 for DEBUG_PARAMETER_REF RTL. */
12457 static dw_loc_descr_ref
12458 parameter_ref_descriptor (rtx rtl)
12460 dw_loc_descr_ref ret;
12461 dw_die_ref ref;
12463 if (dwarf_strict)
12464 return NULL;
12465 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
12466 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
12467 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
12468 if (ref)
12470 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12471 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
12472 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
12474 else
12476 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
12477 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
12479 return ret;
12482 /* The following routine converts the RTL for a variable or parameter
12483 (resident in memory) into an equivalent Dwarf representation of a
12484 mechanism for getting the address of that same variable onto the top of a
12485 hypothetical "address evaluation" stack.
12487 When creating memory location descriptors, we are effectively transforming
12488 the RTL for a memory-resident object into its Dwarf postfix expression
12489 equivalent. This routine recursively descends an RTL tree, turning
12490 it into Dwarf postfix code as it goes.
12492 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
12494 MEM_MODE is the mode of the memory reference, needed to handle some
12495 autoincrement addressing modes.
12497 Return 0 if we can't represent the location. */
12499 dw_loc_descr_ref
12500 mem_loc_descriptor (rtx rtl, machine_mode mode,
12501 machine_mode mem_mode,
12502 enum var_init_status initialized)
12504 dw_loc_descr_ref mem_loc_result = NULL;
12505 enum dwarf_location_atom op;
12506 dw_loc_descr_ref op0, op1;
12507 rtx inner = NULL_RTX;
12509 if (mode == VOIDmode)
12510 mode = GET_MODE (rtl);
12512 /* Note that for a dynamically sized array, the location we will generate a
12513 description of here will be the lowest numbered location which is
12514 actually within the array. That's *not* necessarily the same as the
12515 zeroth element of the array. */
12517 rtl = targetm.delegitimize_address (rtl);
12519 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
12520 return NULL;
12522 switch (GET_CODE (rtl))
12524 case POST_INC:
12525 case POST_DEC:
12526 case POST_MODIFY:
12527 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
12529 case SUBREG:
12530 /* The case of a subreg may arise when we have a local (register)
12531 variable or a formal (register) parameter which doesn't quite fill
12532 up an entire register. For now, just assume that it is
12533 legitimate to make the Dwarf info refer to the whole register which
12534 contains the given subreg. */
12535 if (!subreg_lowpart_p (rtl))
12536 break;
12537 inner = SUBREG_REG (rtl);
12538 case TRUNCATE:
12539 if (inner == NULL_RTX)
12540 inner = XEXP (rtl, 0);
12541 if (GET_MODE_CLASS (mode) == MODE_INT
12542 && GET_MODE_CLASS (GET_MODE (inner)) == MODE_INT
12543 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12544 #ifdef POINTERS_EXTEND_UNSIGNED
12545 || (mode == Pmode && mem_mode != VOIDmode)
12546 #endif
12548 && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
12550 mem_loc_result = mem_loc_descriptor (inner,
12551 GET_MODE (inner),
12552 mem_mode, initialized);
12553 break;
12555 if (dwarf_strict)
12556 break;
12557 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
12558 break;
12559 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
12560 && (GET_MODE_CLASS (mode) != MODE_INT
12561 || GET_MODE_CLASS (GET_MODE (inner)) != MODE_INT))
12562 break;
12563 else
12565 dw_die_ref type_die;
12566 dw_loc_descr_ref cvt;
12568 mem_loc_result = mem_loc_descriptor (inner,
12569 GET_MODE (inner),
12570 mem_mode, initialized);
12571 if (mem_loc_result == NULL)
12572 break;
12573 type_die = base_type_for_mode (mode,
12574 GET_MODE_CLASS (mode) == MODE_INT);
12575 if (type_die == NULL)
12577 mem_loc_result = NULL;
12578 break;
12580 if (GET_MODE_SIZE (mode)
12581 != GET_MODE_SIZE (GET_MODE (inner)))
12582 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12583 else
12584 cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
12585 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12586 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12587 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12588 add_loc_descr (&mem_loc_result, cvt);
12590 break;
12592 case REG:
12593 if (GET_MODE_CLASS (mode) != MODE_INT
12594 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12595 && rtl != arg_pointer_rtx
12596 && rtl != frame_pointer_rtx
12597 #ifdef POINTERS_EXTEND_UNSIGNED
12598 && (mode != Pmode || mem_mode == VOIDmode)
12599 #endif
12602 dw_die_ref type_die;
12603 unsigned int dbx_regnum;
12605 if (dwarf_strict)
12606 break;
12607 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
12608 break;
12609 type_die = base_type_for_mode (mode,
12610 GET_MODE_CLASS (mode) == MODE_INT);
12611 if (type_die == NULL)
12612 break;
12614 dbx_regnum = dbx_reg_number (rtl);
12615 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12616 break;
12617 mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
12618 dbx_regnum, 0);
12619 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12620 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12621 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
12622 break;
12624 /* Whenever a register number forms a part of the description of the
12625 method for calculating the (dynamic) address of a memory resident
12626 object, DWARF rules require the register number be referred to as
12627 a "base register". This distinction is not based in any way upon
12628 what category of register the hardware believes the given register
12629 belongs to. This is strictly DWARF terminology we're dealing with
12630 here. Note that in cases where the location of a memory-resident
12631 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
12632 OP_CONST (0)) the actual DWARF location descriptor that we generate
12633 may just be OP_BASEREG (basereg). This may look deceptively like
12634 the object in question was allocated to a register (rather than in
12635 memory) so DWARF consumers need to be aware of the subtle
12636 distinction between OP_REG and OP_BASEREG. */
12637 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
12638 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
12639 else if (stack_realign_drap
12640 && crtl->drap_reg
12641 && crtl->args.internal_arg_pointer == rtl
12642 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
12644 /* If RTL is internal_arg_pointer, which has been optimized
12645 out, use DRAP instead. */
12646 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
12647 VAR_INIT_STATUS_INITIALIZED);
12649 break;
12651 case SIGN_EXTEND:
12652 case ZERO_EXTEND:
12653 if (GET_MODE_CLASS (mode) != MODE_INT)
12654 break;
12655 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12656 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12657 if (op0 == 0)
12658 break;
12659 else if (GET_CODE (rtl) == ZERO_EXTEND
12660 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12661 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12662 < HOST_BITS_PER_WIDE_INT
12663 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
12664 to expand zero extend as two shifts instead of
12665 masking. */
12666 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
12668 machine_mode imode = GET_MODE (XEXP (rtl, 0));
12669 mem_loc_result = op0;
12670 add_loc_descr (&mem_loc_result,
12671 int_loc_descriptor (GET_MODE_MASK (imode)));
12672 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
12674 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12676 int shift = DWARF2_ADDR_SIZE
12677 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
12678 shift *= BITS_PER_UNIT;
12679 if (GET_CODE (rtl) == SIGN_EXTEND)
12680 op = DW_OP_shra;
12681 else
12682 op = DW_OP_shr;
12683 mem_loc_result = op0;
12684 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12685 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12686 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12687 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12689 else if (!dwarf_strict)
12691 dw_die_ref type_die1, type_die2;
12692 dw_loc_descr_ref cvt;
12694 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12695 GET_CODE (rtl) == ZERO_EXTEND);
12696 if (type_die1 == NULL)
12697 break;
12698 type_die2 = base_type_for_mode (mode, 1);
12699 if (type_die2 == NULL)
12700 break;
12701 mem_loc_result = op0;
12702 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12703 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12704 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
12705 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12706 add_loc_descr (&mem_loc_result, cvt);
12707 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12708 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12709 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
12710 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12711 add_loc_descr (&mem_loc_result, cvt);
12713 break;
12715 case MEM:
12717 rtx new_rtl = avoid_constant_pool_reference (rtl);
12718 if (new_rtl != rtl)
12720 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
12721 initialized);
12722 if (mem_loc_result != NULL)
12723 return mem_loc_result;
12726 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
12727 get_address_mode (rtl), mode,
12728 VAR_INIT_STATUS_INITIALIZED);
12729 if (mem_loc_result == NULL)
12730 mem_loc_result = tls_mem_loc_descriptor (rtl);
12731 if (mem_loc_result != NULL)
12733 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12734 || GET_MODE_CLASS (mode) != MODE_INT)
12736 dw_die_ref type_die;
12737 dw_loc_descr_ref deref;
12739 if (dwarf_strict)
12740 return NULL;
12741 type_die
12742 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
12743 if (type_die == NULL)
12744 return NULL;
12745 deref = new_loc_descr (DW_OP_GNU_deref_type,
12746 GET_MODE_SIZE (mode), 0);
12747 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12748 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12749 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
12750 add_loc_descr (&mem_loc_result, deref);
12752 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12753 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
12754 else
12755 add_loc_descr (&mem_loc_result,
12756 new_loc_descr (DW_OP_deref_size,
12757 GET_MODE_SIZE (mode), 0));
12759 break;
12761 case LO_SUM:
12762 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
12764 case LABEL_REF:
12765 /* Some ports can transform a symbol ref into a label ref, because
12766 the symbol ref is too far away and has to be dumped into a constant
12767 pool. */
12768 case CONST:
12769 case SYMBOL_REF:
12770 if ((GET_MODE_CLASS (mode) != MODE_INT
12771 && GET_MODE_CLASS (mode) != MODE_PARTIAL_INT)
12772 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12773 #ifdef POINTERS_EXTEND_UNSIGNED
12774 && (mode != Pmode || mem_mode == VOIDmode)
12775 #endif
12777 break;
12778 if (GET_CODE (rtl) == SYMBOL_REF
12779 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12781 dw_loc_descr_ref temp;
12783 /* If this is not defined, we have no way to emit the data. */
12784 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
12785 break;
12787 temp = new_addr_loc_descr (rtl, dtprel_true);
12789 mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
12790 add_loc_descr (&mem_loc_result, temp);
12792 break;
12795 if (!const_ok_for_output (rtl))
12796 break;
12798 symref:
12799 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
12800 vec_safe_push (used_rtx_array, rtl);
12801 break;
12803 case CONCAT:
12804 case CONCATN:
12805 case VAR_LOCATION:
12806 case DEBUG_IMPLICIT_PTR:
12807 expansion_failed (NULL_TREE, rtl,
12808 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
12809 return 0;
12811 case ENTRY_VALUE:
12812 if (dwarf_strict)
12813 return NULL;
12814 if (REG_P (ENTRY_VALUE_EXP (rtl)))
12816 if (GET_MODE_CLASS (mode) != MODE_INT
12817 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12818 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12819 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12820 else
12822 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
12823 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12824 return NULL;
12825 op0 = one_reg_loc_descriptor (dbx_regnum,
12826 VAR_INIT_STATUS_INITIALIZED);
12829 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
12830 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
12832 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12833 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12834 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
12835 return NULL;
12837 else
12838 gcc_unreachable ();
12839 if (op0 == NULL)
12840 return NULL;
12841 mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
12842 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
12843 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
12844 break;
12846 case DEBUG_PARAMETER_REF:
12847 mem_loc_result = parameter_ref_descriptor (rtl);
12848 break;
12850 case PRE_MODIFY:
12851 /* Extract the PLUS expression nested inside and fall into
12852 PLUS code below. */
12853 rtl = XEXP (rtl, 1);
12854 goto plus;
12856 case PRE_INC:
12857 case PRE_DEC:
12858 /* Turn these into a PLUS expression and fall into the PLUS code
12859 below. */
12860 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
12861 gen_int_mode (GET_CODE (rtl) == PRE_INC
12862 ? GET_MODE_UNIT_SIZE (mem_mode)
12863 : -GET_MODE_UNIT_SIZE (mem_mode),
12864 mode));
12866 /* ... fall through ... */
12868 case PLUS:
12869 plus:
12870 if (is_based_loc (rtl)
12871 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12872 || XEXP (rtl, 0) == arg_pointer_rtx
12873 || XEXP (rtl, 0) == frame_pointer_rtx)
12874 && GET_MODE_CLASS (mode) == MODE_INT)
12875 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
12876 INTVAL (XEXP (rtl, 1)),
12877 VAR_INIT_STATUS_INITIALIZED);
12878 else
12880 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12881 VAR_INIT_STATUS_INITIALIZED);
12882 if (mem_loc_result == 0)
12883 break;
12885 if (CONST_INT_P (XEXP (rtl, 1))
12886 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12887 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
12888 else
12890 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12891 VAR_INIT_STATUS_INITIALIZED);
12892 if (op1 == 0)
12893 return NULL;
12894 add_loc_descr (&mem_loc_result, op1);
12895 add_loc_descr (&mem_loc_result,
12896 new_loc_descr (DW_OP_plus, 0, 0));
12899 break;
12901 /* If a pseudo-reg is optimized away, it is possible for it to
12902 be replaced with a MEM containing a multiply or shift. */
12903 case MINUS:
12904 op = DW_OP_minus;
12905 goto do_binop;
12907 case MULT:
12908 op = DW_OP_mul;
12909 goto do_binop;
12911 case DIV:
12912 if (!dwarf_strict
12913 && GET_MODE_CLASS (mode) == MODE_INT
12914 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12916 mem_loc_result = typed_binop (DW_OP_div, rtl,
12917 base_type_for_mode (mode, 0),
12918 mode, mem_mode);
12919 break;
12921 op = DW_OP_div;
12922 goto do_binop;
12924 case UMOD:
12925 op = DW_OP_mod;
12926 goto do_binop;
12928 case ASHIFT:
12929 op = DW_OP_shl;
12930 goto do_shift;
12932 case ASHIFTRT:
12933 op = DW_OP_shra;
12934 goto do_shift;
12936 case LSHIFTRT:
12937 op = DW_OP_shr;
12938 goto do_shift;
12940 do_shift:
12941 if (GET_MODE_CLASS (mode) != MODE_INT)
12942 break;
12943 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12944 VAR_INIT_STATUS_INITIALIZED);
12946 rtx rtlop1 = XEXP (rtl, 1);
12947 if (GET_MODE (rtlop1) != VOIDmode
12948 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
12949 < GET_MODE_BITSIZE (mode))
12950 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12951 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12952 VAR_INIT_STATUS_INITIALIZED);
12955 if (op0 == 0 || op1 == 0)
12956 break;
12958 mem_loc_result = op0;
12959 add_loc_descr (&mem_loc_result, op1);
12960 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12961 break;
12963 case AND:
12964 op = DW_OP_and;
12965 goto do_binop;
12967 case IOR:
12968 op = DW_OP_or;
12969 goto do_binop;
12971 case XOR:
12972 op = DW_OP_xor;
12973 goto do_binop;
12975 do_binop:
12976 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12977 VAR_INIT_STATUS_INITIALIZED);
12978 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12979 VAR_INIT_STATUS_INITIALIZED);
12981 if (op0 == 0 || op1 == 0)
12982 break;
12984 mem_loc_result = op0;
12985 add_loc_descr (&mem_loc_result, op1);
12986 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12987 break;
12989 case MOD:
12990 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
12992 mem_loc_result = typed_binop (DW_OP_mod, rtl,
12993 base_type_for_mode (mode, 0),
12994 mode, mem_mode);
12995 break;
12998 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12999 VAR_INIT_STATUS_INITIALIZED);
13000 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13001 VAR_INIT_STATUS_INITIALIZED);
13003 if (op0 == 0 || op1 == 0)
13004 break;
13006 mem_loc_result = op0;
13007 add_loc_descr (&mem_loc_result, op1);
13008 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13009 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13010 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
13011 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
13012 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
13013 break;
13015 case UDIV:
13016 if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
13018 if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
13020 op = DW_OP_div;
13021 goto do_binop;
13023 mem_loc_result = typed_binop (DW_OP_div, rtl,
13024 base_type_for_mode (mode, 1),
13025 mode, mem_mode);
13027 break;
13029 case NOT:
13030 op = DW_OP_not;
13031 goto do_unop;
13033 case ABS:
13034 op = DW_OP_abs;
13035 goto do_unop;
13037 case NEG:
13038 op = DW_OP_neg;
13039 goto do_unop;
13041 do_unop:
13042 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13043 VAR_INIT_STATUS_INITIALIZED);
13045 if (op0 == 0)
13046 break;
13048 mem_loc_result = op0;
13049 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13050 break;
13052 case CONST_INT:
13053 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13054 #ifdef POINTERS_EXTEND_UNSIGNED
13055 || (mode == Pmode
13056 && mem_mode != VOIDmode
13057 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
13058 #endif
13061 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13062 break;
13064 if (!dwarf_strict
13065 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
13066 || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
13068 dw_die_ref type_die = base_type_for_mode (mode, 1);
13069 machine_mode amode;
13070 if (type_die == NULL)
13071 return NULL;
13072 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
13073 MODE_INT, 0);
13074 if (INTVAL (rtl) >= 0
13075 && amode != BLKmode
13076 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
13077 /* const DW_OP_GNU_convert <XXX> vs.
13078 DW_OP_GNU_const_type <XXX, 1, const>. */
13079 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
13080 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
13082 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13083 op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13084 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13085 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13086 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
13087 add_loc_descr (&mem_loc_result, op0);
13088 return mem_loc_result;
13090 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
13091 INTVAL (rtl));
13092 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13093 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13094 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13095 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
13096 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13097 else
13099 mem_loc_result->dw_loc_oprnd2.val_class
13100 = dw_val_class_const_double;
13101 mem_loc_result->dw_loc_oprnd2.v.val_double
13102 = double_int::from_shwi (INTVAL (rtl));
13105 break;
13107 case CONST_DOUBLE:
13108 if (!dwarf_strict)
13110 dw_die_ref type_die;
13112 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
13113 CONST_DOUBLE rtx could represent either a large integer
13114 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
13115 the value is always a floating point constant.
13117 When it is an integer, a CONST_DOUBLE is used whenever
13118 the constant requires 2 HWIs to be adequately represented.
13119 We output CONST_DOUBLEs as blocks. */
13120 if (mode == VOIDmode
13121 || (GET_MODE (rtl) == VOIDmode
13122 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
13123 break;
13124 type_die = base_type_for_mode (mode,
13125 GET_MODE_CLASS (mode) == MODE_INT);
13126 if (type_die == NULL)
13127 return NULL;
13128 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
13129 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13130 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13131 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13132 #if TARGET_SUPPORTS_WIDE_INT == 0
13133 if (!SCALAR_FLOAT_MODE_P (mode))
13135 mem_loc_result->dw_loc_oprnd2.val_class
13136 = dw_val_class_const_double;
13137 mem_loc_result->dw_loc_oprnd2.v.val_double
13138 = rtx_to_double_int (rtl);
13140 else
13141 #endif
13143 unsigned int length = GET_MODE_SIZE (mode);
13144 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
13146 insert_float (rtl, array);
13147 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13148 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13149 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13150 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13153 break;
13155 case CONST_WIDE_INT:
13156 if (!dwarf_strict)
13158 dw_die_ref type_die;
13160 type_die = base_type_for_mode (mode,
13161 GET_MODE_CLASS (mode) == MODE_INT);
13162 if (type_die == NULL)
13163 return NULL;
13164 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
13165 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13166 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13167 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13168 mem_loc_result->dw_loc_oprnd2.val_class
13169 = dw_val_class_wide_int;
13170 mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
13171 *mem_loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
13173 break;
13175 case EQ:
13176 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
13177 break;
13179 case GE:
13180 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
13181 break;
13183 case GT:
13184 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
13185 break;
13187 case LE:
13188 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
13189 break;
13191 case LT:
13192 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
13193 break;
13195 case NE:
13196 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
13197 break;
13199 case GEU:
13200 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
13201 break;
13203 case GTU:
13204 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
13205 break;
13207 case LEU:
13208 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
13209 break;
13211 case LTU:
13212 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
13213 break;
13215 case UMIN:
13216 case UMAX:
13217 if (GET_MODE_CLASS (mode) != MODE_INT)
13218 break;
13219 /* FALLTHRU */
13220 case SMIN:
13221 case SMAX:
13222 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
13223 break;
13225 case ZERO_EXTRACT:
13226 case SIGN_EXTRACT:
13227 if (CONST_INT_P (XEXP (rtl, 1))
13228 && CONST_INT_P (XEXP (rtl, 2))
13229 && ((unsigned) INTVAL (XEXP (rtl, 1))
13230 + (unsigned) INTVAL (XEXP (rtl, 2))
13231 <= GET_MODE_BITSIZE (mode))
13232 && GET_MODE_CLASS (mode) == MODE_INT
13233 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13234 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
13236 int shift, size;
13237 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13238 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13239 if (op0 == 0)
13240 break;
13241 if (GET_CODE (rtl) == SIGN_EXTRACT)
13242 op = DW_OP_shra;
13243 else
13244 op = DW_OP_shr;
13245 mem_loc_result = op0;
13246 size = INTVAL (XEXP (rtl, 1));
13247 shift = INTVAL (XEXP (rtl, 2));
13248 if (BITS_BIG_ENDIAN)
13249 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
13250 - shift - size;
13251 if (shift + size != (int) DWARF2_ADDR_SIZE)
13253 add_loc_descr (&mem_loc_result,
13254 int_loc_descriptor (DWARF2_ADDR_SIZE
13255 - shift - size));
13256 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13258 if (size != (int) DWARF2_ADDR_SIZE)
13260 add_loc_descr (&mem_loc_result,
13261 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
13262 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13265 break;
13267 case IF_THEN_ELSE:
13269 dw_loc_descr_ref op2, bra_node, drop_node;
13270 op0 = mem_loc_descriptor (XEXP (rtl, 0),
13271 GET_MODE (XEXP (rtl, 0)) == VOIDmode
13272 ? word_mode : GET_MODE (XEXP (rtl, 0)),
13273 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13274 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13275 VAR_INIT_STATUS_INITIALIZED);
13276 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
13277 VAR_INIT_STATUS_INITIALIZED);
13278 if (op0 == NULL || op1 == NULL || op2 == NULL)
13279 break;
13281 mem_loc_result = op1;
13282 add_loc_descr (&mem_loc_result, op2);
13283 add_loc_descr (&mem_loc_result, op0);
13284 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13285 add_loc_descr (&mem_loc_result, bra_node);
13286 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
13287 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
13288 add_loc_descr (&mem_loc_result, drop_node);
13289 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13290 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
13292 break;
13294 case FLOAT_EXTEND:
13295 case FLOAT_TRUNCATE:
13296 case FLOAT:
13297 case UNSIGNED_FLOAT:
13298 case FIX:
13299 case UNSIGNED_FIX:
13300 if (!dwarf_strict)
13302 dw_die_ref type_die;
13303 dw_loc_descr_ref cvt;
13305 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13306 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13307 if (op0 == NULL)
13308 break;
13309 if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
13310 && (GET_CODE (rtl) == FLOAT
13311 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
13312 <= DWARF2_ADDR_SIZE))
13314 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
13315 GET_CODE (rtl) == UNSIGNED_FLOAT);
13316 if (type_die == NULL)
13317 break;
13318 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13319 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13320 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13321 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13322 add_loc_descr (&op0, cvt);
13324 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
13325 if (type_die == NULL)
13326 break;
13327 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13328 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13329 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13330 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13331 add_loc_descr (&op0, cvt);
13332 if (GET_MODE_CLASS (mode) == MODE_INT
13333 && (GET_CODE (rtl) == FIX
13334 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
13336 op0 = convert_descriptor_to_mode (mode, op0);
13337 if (op0 == NULL)
13338 break;
13340 mem_loc_result = op0;
13342 break;
13344 case CLZ:
13345 case CTZ:
13346 case FFS:
13347 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
13348 break;
13350 case POPCOUNT:
13351 case PARITY:
13352 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
13353 break;
13355 case BSWAP:
13356 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
13357 break;
13359 case ROTATE:
13360 case ROTATERT:
13361 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
13362 break;
13364 case COMPARE:
13365 /* In theory, we could implement the above. */
13366 /* DWARF cannot represent the unsigned compare operations
13367 natively. */
13368 case SS_MULT:
13369 case US_MULT:
13370 case SS_DIV:
13371 case US_DIV:
13372 case SS_PLUS:
13373 case US_PLUS:
13374 case SS_MINUS:
13375 case US_MINUS:
13376 case SS_NEG:
13377 case US_NEG:
13378 case SS_ABS:
13379 case SS_ASHIFT:
13380 case US_ASHIFT:
13381 case SS_TRUNCATE:
13382 case US_TRUNCATE:
13383 case UNORDERED:
13384 case ORDERED:
13385 case UNEQ:
13386 case UNGE:
13387 case UNGT:
13388 case UNLE:
13389 case UNLT:
13390 case LTGT:
13391 case FRACT_CONVERT:
13392 case UNSIGNED_FRACT_CONVERT:
13393 case SAT_FRACT:
13394 case UNSIGNED_SAT_FRACT:
13395 case SQRT:
13396 case ASM_OPERANDS:
13397 case VEC_MERGE:
13398 case VEC_SELECT:
13399 case VEC_CONCAT:
13400 case VEC_DUPLICATE:
13401 case UNSPEC:
13402 case HIGH:
13403 case FMA:
13404 case STRICT_LOW_PART:
13405 case CONST_VECTOR:
13406 case CONST_FIXED:
13407 case CLRSB:
13408 case CLOBBER:
13409 /* If delegitimize_address couldn't do anything with the UNSPEC, we
13410 can't express it in the debug info. This can happen e.g. with some
13411 TLS UNSPECs. */
13412 break;
13414 case CONST_STRING:
13415 resolve_one_addr (&rtl);
13416 goto symref;
13418 default:
13419 #ifdef ENABLE_CHECKING
13420 print_rtl (stderr, rtl);
13421 gcc_unreachable ();
13422 #else
13423 break;
13424 #endif
13427 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13428 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13430 return mem_loc_result;
13433 /* Return a descriptor that describes the concatenation of two locations.
13434 This is typically a complex variable. */
13436 static dw_loc_descr_ref
13437 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
13439 dw_loc_descr_ref cc_loc_result = NULL;
13440 dw_loc_descr_ref x0_ref
13441 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13442 dw_loc_descr_ref x1_ref
13443 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13445 if (x0_ref == 0 || x1_ref == 0)
13446 return 0;
13448 cc_loc_result = x0_ref;
13449 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
13451 add_loc_descr (&cc_loc_result, x1_ref);
13452 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
13454 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13455 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13457 return cc_loc_result;
13460 /* Return a descriptor that describes the concatenation of N
13461 locations. */
13463 static dw_loc_descr_ref
13464 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
13466 unsigned int i;
13467 dw_loc_descr_ref cc_loc_result = NULL;
13468 unsigned int n = XVECLEN (concatn, 0);
13470 for (i = 0; i < n; ++i)
13472 dw_loc_descr_ref ref;
13473 rtx x = XVECEXP (concatn, 0, i);
13475 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13476 if (ref == NULL)
13477 return NULL;
13479 add_loc_descr (&cc_loc_result, ref);
13480 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
13483 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13484 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13486 return cc_loc_result;
13489 /* Helper function for loc_descriptor. Return DW_OP_GNU_implicit_pointer
13490 for DEBUG_IMPLICIT_PTR RTL. */
13492 static dw_loc_descr_ref
13493 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
13495 dw_loc_descr_ref ret;
13496 dw_die_ref ref;
13498 if (dwarf_strict)
13499 return NULL;
13500 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
13501 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
13502 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
13503 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
13504 ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
13505 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
13506 if (ref)
13508 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13509 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
13510 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
13512 else
13514 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
13515 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
13517 return ret;
13520 /* Output a proper Dwarf location descriptor for a variable or parameter
13521 which is either allocated in a register or in a memory location. For a
13522 register, we just generate an OP_REG and the register number. For a
13523 memory location we provide a Dwarf postfix expression describing how to
13524 generate the (dynamic) address of the object onto the address stack.
13526 MODE is mode of the decl if this loc_descriptor is going to be used in
13527 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
13528 allowed, VOIDmode otherwise.
13530 If we don't know how to describe it, return 0. */
13532 static dw_loc_descr_ref
13533 loc_descriptor (rtx rtl, machine_mode mode,
13534 enum var_init_status initialized)
13536 dw_loc_descr_ref loc_result = NULL;
13538 switch (GET_CODE (rtl))
13540 case SUBREG:
13541 /* The case of a subreg may arise when we have a local (register)
13542 variable or a formal (register) parameter which doesn't quite fill
13543 up an entire register. For now, just assume that it is
13544 legitimate to make the Dwarf info refer to the whole register which
13545 contains the given subreg. */
13546 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
13547 loc_result = loc_descriptor (SUBREG_REG (rtl),
13548 GET_MODE (SUBREG_REG (rtl)), initialized);
13549 else
13550 goto do_default;
13551 break;
13553 case REG:
13554 loc_result = reg_loc_descriptor (rtl, initialized);
13555 break;
13557 case MEM:
13558 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13559 GET_MODE (rtl), initialized);
13560 if (loc_result == NULL)
13561 loc_result = tls_mem_loc_descriptor (rtl);
13562 if (loc_result == NULL)
13564 rtx new_rtl = avoid_constant_pool_reference (rtl);
13565 if (new_rtl != rtl)
13566 loc_result = loc_descriptor (new_rtl, mode, initialized);
13568 break;
13570 case CONCAT:
13571 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
13572 initialized);
13573 break;
13575 case CONCATN:
13576 loc_result = concatn_loc_descriptor (rtl, initialized);
13577 break;
13579 case VAR_LOCATION:
13580 /* Single part. */
13581 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
13583 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
13584 if (GET_CODE (loc) == EXPR_LIST)
13585 loc = XEXP (loc, 0);
13586 loc_result = loc_descriptor (loc, mode, initialized);
13587 break;
13590 rtl = XEXP (rtl, 1);
13591 /* FALLTHRU */
13593 case PARALLEL:
13595 rtvec par_elems = XVEC (rtl, 0);
13596 int num_elem = GET_NUM_ELEM (par_elems);
13597 machine_mode mode;
13598 int i;
13600 /* Create the first one, so we have something to add to. */
13601 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
13602 VOIDmode, initialized);
13603 if (loc_result == NULL)
13604 return NULL;
13605 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
13606 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13607 for (i = 1; i < num_elem; i++)
13609 dw_loc_descr_ref temp;
13611 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
13612 VOIDmode, initialized);
13613 if (temp == NULL)
13614 return NULL;
13615 add_loc_descr (&loc_result, temp);
13616 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
13617 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13620 break;
13622 case CONST_INT:
13623 if (mode != VOIDmode && mode != BLKmode)
13624 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
13625 INTVAL (rtl));
13626 break;
13628 case CONST_DOUBLE:
13629 if (mode == VOIDmode)
13630 mode = GET_MODE (rtl);
13632 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13634 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13636 /* Note that a CONST_DOUBLE rtx could represent either an integer
13637 or a floating-point constant. A CONST_DOUBLE is used whenever
13638 the constant requires more than one word in order to be
13639 adequately represented. We output CONST_DOUBLEs as blocks. */
13640 loc_result = new_loc_descr (DW_OP_implicit_value,
13641 GET_MODE_SIZE (mode), 0);
13642 #if TARGET_SUPPORTS_WIDE_INT == 0
13643 if (!SCALAR_FLOAT_MODE_P (mode))
13645 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
13646 loc_result->dw_loc_oprnd2.v.val_double
13647 = rtx_to_double_int (rtl);
13649 else
13650 #endif
13652 unsigned int length = GET_MODE_SIZE (mode);
13653 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
13655 insert_float (rtl, array);
13656 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13657 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13658 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13659 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13662 break;
13664 case CONST_WIDE_INT:
13665 if (mode == VOIDmode)
13666 mode = GET_MODE (rtl);
13668 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13670 loc_result = new_loc_descr (DW_OP_implicit_value,
13671 GET_MODE_SIZE (mode), 0);
13672 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
13673 loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
13674 *loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
13676 break;
13678 case CONST_VECTOR:
13679 if (mode == VOIDmode)
13680 mode = GET_MODE (rtl);
13682 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13684 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
13685 unsigned int length = CONST_VECTOR_NUNITS (rtl);
13686 unsigned char *array
13687 = ggc_vec_alloc<unsigned char> (length * elt_size);
13688 unsigned int i;
13689 unsigned char *p;
13690 machine_mode imode = GET_MODE_INNER (mode);
13692 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13693 switch (GET_MODE_CLASS (mode))
13695 case MODE_VECTOR_INT:
13696 for (i = 0, p = array; i < length; i++, p += elt_size)
13698 rtx elt = CONST_VECTOR_ELT (rtl, i);
13699 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
13701 break;
13703 case MODE_VECTOR_FLOAT:
13704 for (i = 0, p = array; i < length; i++, p += elt_size)
13706 rtx elt = CONST_VECTOR_ELT (rtl, i);
13707 insert_float (elt, p);
13709 break;
13711 default:
13712 gcc_unreachable ();
13715 loc_result = new_loc_descr (DW_OP_implicit_value,
13716 length * elt_size, 0);
13717 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13718 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
13719 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
13720 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13722 break;
13724 case CONST:
13725 if (mode == VOIDmode
13726 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
13727 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
13728 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
13730 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
13731 break;
13733 /* FALLTHROUGH */
13734 case SYMBOL_REF:
13735 if (!const_ok_for_output (rtl))
13736 break;
13737 case LABEL_REF:
13738 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
13739 && (dwarf_version >= 4 || !dwarf_strict))
13741 loc_result = new_addr_loc_descr (rtl, dtprel_false);
13742 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
13743 vec_safe_push (used_rtx_array, rtl);
13745 break;
13747 case DEBUG_IMPLICIT_PTR:
13748 loc_result = implicit_ptr_descriptor (rtl, 0);
13749 break;
13751 case PLUS:
13752 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
13753 && CONST_INT_P (XEXP (rtl, 1)))
13755 loc_result
13756 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
13757 break;
13759 /* FALLTHRU */
13760 do_default:
13761 default:
13762 if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
13763 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
13764 && dwarf_version >= 4)
13765 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
13767 /* Value expression. */
13768 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
13769 if (loc_result)
13770 add_loc_descr (&loc_result,
13771 new_loc_descr (DW_OP_stack_value, 0, 0));
13773 break;
13776 return loc_result;
13779 /* We need to figure out what section we should use as the base for the
13780 address ranges where a given location is valid.
13781 1. If this particular DECL has a section associated with it, use that.
13782 2. If this function has a section associated with it, use that.
13783 3. Otherwise, use the text section.
13784 XXX: If you split a variable across multiple sections, we won't notice. */
13786 static const char *
13787 secname_for_decl (const_tree decl)
13789 const char *secname;
13791 if (VAR_OR_FUNCTION_DECL_P (decl)
13792 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
13793 && DECL_SECTION_NAME (decl))
13794 secname = DECL_SECTION_NAME (decl);
13795 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
13796 secname = DECL_SECTION_NAME (current_function_decl);
13797 else if (cfun && in_cold_section_p)
13798 secname = crtl->subsections.cold_section_label;
13799 else
13800 secname = text_section_label;
13802 return secname;
13805 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
13807 static bool
13808 decl_by_reference_p (tree decl)
13810 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
13811 || TREE_CODE (decl) == VAR_DECL)
13812 && DECL_BY_REFERENCE (decl));
13815 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13816 for VARLOC. */
13818 static dw_loc_descr_ref
13819 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
13820 enum var_init_status initialized)
13822 int have_address = 0;
13823 dw_loc_descr_ref descr;
13824 machine_mode mode;
13826 if (want_address != 2)
13828 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
13829 /* Single part. */
13830 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13832 varloc = PAT_VAR_LOCATION_LOC (varloc);
13833 if (GET_CODE (varloc) == EXPR_LIST)
13834 varloc = XEXP (varloc, 0);
13835 mode = GET_MODE (varloc);
13836 if (MEM_P (varloc))
13838 rtx addr = XEXP (varloc, 0);
13839 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
13840 mode, initialized);
13841 if (descr)
13842 have_address = 1;
13843 else
13845 rtx x = avoid_constant_pool_reference (varloc);
13846 if (x != varloc)
13847 descr = mem_loc_descriptor (x, mode, VOIDmode,
13848 initialized);
13851 else
13852 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
13854 else
13855 return 0;
13857 else
13859 if (GET_CODE (varloc) == VAR_LOCATION)
13860 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
13861 else
13862 mode = DECL_MODE (loc);
13863 descr = loc_descriptor (varloc, mode, initialized);
13864 have_address = 1;
13867 if (!descr)
13868 return 0;
13870 if (want_address == 2 && !have_address
13871 && (dwarf_version >= 4 || !dwarf_strict))
13873 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
13875 expansion_failed (loc, NULL_RTX,
13876 "DWARF address size mismatch");
13877 return 0;
13879 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
13880 have_address = 1;
13882 /* Show if we can't fill the request for an address. */
13883 if (want_address && !have_address)
13885 expansion_failed (loc, NULL_RTX,
13886 "Want address and only have value");
13887 return 0;
13890 /* If we've got an address and don't want one, dereference. */
13891 if (!want_address && have_address)
13893 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
13894 enum dwarf_location_atom op;
13896 if (size > DWARF2_ADDR_SIZE || size == -1)
13898 expansion_failed (loc, NULL_RTX,
13899 "DWARF address size mismatch");
13900 return 0;
13902 else if (size == DWARF2_ADDR_SIZE)
13903 op = DW_OP_deref;
13904 else
13905 op = DW_OP_deref_size;
13907 add_loc_descr (&descr, new_loc_descr (op, size, 0));
13910 return descr;
13913 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
13914 if it is not possible. */
13916 static dw_loc_descr_ref
13917 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
13919 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
13920 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
13921 else if (dwarf_version >= 3 || !dwarf_strict)
13922 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
13923 else
13924 return NULL;
13927 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13928 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
13930 static dw_loc_descr_ref
13931 dw_sra_loc_expr (tree decl, rtx loc)
13933 rtx p;
13934 unsigned HOST_WIDE_INT padsize = 0;
13935 dw_loc_descr_ref descr, *descr_tail;
13936 unsigned HOST_WIDE_INT decl_size;
13937 rtx varloc;
13938 enum var_init_status initialized;
13940 if (DECL_SIZE (decl) == NULL
13941 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
13942 return NULL;
13944 decl_size = tree_to_uhwi (DECL_SIZE (decl));
13945 descr = NULL;
13946 descr_tail = &descr;
13948 for (p = loc; p; p = XEXP (p, 1))
13950 unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
13951 rtx loc_note = *decl_piece_varloc_ptr (p);
13952 dw_loc_descr_ref cur_descr;
13953 dw_loc_descr_ref *tail, last = NULL;
13954 unsigned HOST_WIDE_INT opsize = 0;
13956 if (loc_note == NULL_RTX
13957 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
13959 padsize += bitsize;
13960 continue;
13962 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
13963 varloc = NOTE_VAR_LOCATION (loc_note);
13964 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
13965 if (cur_descr == NULL)
13967 padsize += bitsize;
13968 continue;
13971 /* Check that cur_descr either doesn't use
13972 DW_OP_*piece operations, or their sum is equal
13973 to bitsize. Otherwise we can't embed it. */
13974 for (tail = &cur_descr; *tail != NULL;
13975 tail = &(*tail)->dw_loc_next)
13976 if ((*tail)->dw_loc_opc == DW_OP_piece)
13978 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
13979 * BITS_PER_UNIT;
13980 last = *tail;
13982 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
13984 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
13985 last = *tail;
13988 if (last != NULL && opsize != bitsize)
13990 padsize += bitsize;
13991 /* Discard the current piece of the descriptor and release any
13992 addr_table entries it uses. */
13993 remove_loc_list_addr_table_entries (cur_descr);
13994 continue;
13997 /* If there is a hole, add DW_OP_*piece after empty DWARF
13998 expression, which means that those bits are optimized out. */
13999 if (padsize)
14001 if (padsize > decl_size)
14003 remove_loc_list_addr_table_entries (cur_descr);
14004 goto discard_descr;
14006 decl_size -= padsize;
14007 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
14008 if (*descr_tail == NULL)
14010 remove_loc_list_addr_table_entries (cur_descr);
14011 goto discard_descr;
14013 descr_tail = &(*descr_tail)->dw_loc_next;
14014 padsize = 0;
14016 *descr_tail = cur_descr;
14017 descr_tail = tail;
14018 if (bitsize > decl_size)
14019 goto discard_descr;
14020 decl_size -= bitsize;
14021 if (last == NULL)
14023 HOST_WIDE_INT offset = 0;
14024 if (GET_CODE (varloc) == VAR_LOCATION
14025 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14027 varloc = PAT_VAR_LOCATION_LOC (varloc);
14028 if (GET_CODE (varloc) == EXPR_LIST)
14029 varloc = XEXP (varloc, 0);
14033 if (GET_CODE (varloc) == CONST
14034 || GET_CODE (varloc) == SIGN_EXTEND
14035 || GET_CODE (varloc) == ZERO_EXTEND)
14036 varloc = XEXP (varloc, 0);
14037 else if (GET_CODE (varloc) == SUBREG)
14038 varloc = SUBREG_REG (varloc);
14039 else
14040 break;
14042 while (1);
14043 /* DW_OP_bit_size offset should be zero for register
14044 or implicit location descriptions and empty location
14045 descriptions, but for memory addresses needs big endian
14046 adjustment. */
14047 if (MEM_P (varloc))
14049 unsigned HOST_WIDE_INT memsize
14050 = MEM_SIZE (varloc) * BITS_PER_UNIT;
14051 if (memsize != bitsize)
14053 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
14054 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
14055 goto discard_descr;
14056 if (memsize < bitsize)
14057 goto discard_descr;
14058 if (BITS_BIG_ENDIAN)
14059 offset = memsize - bitsize;
14063 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
14064 if (*descr_tail == NULL)
14065 goto discard_descr;
14066 descr_tail = &(*descr_tail)->dw_loc_next;
14070 /* If there were any non-empty expressions, add padding till the end of
14071 the decl. */
14072 if (descr != NULL && decl_size != 0)
14074 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
14075 if (*descr_tail == NULL)
14076 goto discard_descr;
14078 return descr;
14080 discard_descr:
14081 /* Discard the descriptor and release any addr_table entries it uses. */
14082 remove_loc_list_addr_table_entries (descr);
14083 return NULL;
14086 /* Return the dwarf representation of the location list LOC_LIST of
14087 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
14088 function. */
14090 static dw_loc_list_ref
14091 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
14093 const char *endname, *secname;
14094 rtx varloc;
14095 enum var_init_status initialized;
14096 struct var_loc_node *node;
14097 dw_loc_descr_ref descr;
14098 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
14099 dw_loc_list_ref list = NULL;
14100 dw_loc_list_ref *listp = &list;
14102 /* Now that we know what section we are using for a base,
14103 actually construct the list of locations.
14104 The first location information is what is passed to the
14105 function that creates the location list, and the remaining
14106 locations just get added on to that list.
14107 Note that we only know the start address for a location
14108 (IE location changes), so to build the range, we use
14109 the range [current location start, next location start].
14110 This means we have to special case the last node, and generate
14111 a range of [last location start, end of function label]. */
14113 secname = secname_for_decl (decl);
14115 for (node = loc_list->first; node; node = node->next)
14116 if (GET_CODE (node->loc) == EXPR_LIST
14117 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
14119 if (GET_CODE (node->loc) == EXPR_LIST)
14121 /* This requires DW_OP_{,bit_}piece, which is not usable
14122 inside DWARF expressions. */
14123 if (want_address != 2)
14124 continue;
14125 descr = dw_sra_loc_expr (decl, node->loc);
14126 if (descr == NULL)
14127 continue;
14129 else
14131 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14132 varloc = NOTE_VAR_LOCATION (node->loc);
14133 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
14135 if (descr)
14137 bool range_across_switch = false;
14138 /* If section switch happens in between node->label
14139 and node->next->label (or end of function) and
14140 we can't emit it as a single entry list,
14141 emit two ranges, first one ending at the end
14142 of first partition and second one starting at the
14143 beginning of second partition. */
14144 if (node == loc_list->last_before_switch
14145 && (node != loc_list->first || loc_list->first->next)
14146 && current_function_decl)
14148 endname = cfun->fde->dw_fde_end;
14149 range_across_switch = true;
14151 /* The variable has a location between NODE->LABEL and
14152 NODE->NEXT->LABEL. */
14153 else if (node->next)
14154 endname = node->next->label;
14155 /* If the variable has a location at the last label
14156 it keeps its location until the end of function. */
14157 else if (!current_function_decl)
14158 endname = text_end_label;
14159 else
14161 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
14162 current_function_funcdef_no);
14163 endname = ggc_strdup (label_id);
14166 *listp = new_loc_list (descr, node->label, endname, secname);
14167 if (TREE_CODE (decl) == PARM_DECL
14168 && node == loc_list->first
14169 && NOTE_P (node->loc)
14170 && strcmp (node->label, endname) == 0)
14171 (*listp)->force = true;
14172 listp = &(*listp)->dw_loc_next;
14174 if (range_across_switch)
14176 if (GET_CODE (node->loc) == EXPR_LIST)
14177 descr = dw_sra_loc_expr (decl, node->loc);
14178 else
14180 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14181 varloc = NOTE_VAR_LOCATION (node->loc);
14182 descr = dw_loc_list_1 (decl, varloc, want_address,
14183 initialized);
14185 gcc_assert (descr);
14186 /* The variable has a location between NODE->LABEL and
14187 NODE->NEXT->LABEL. */
14188 if (node->next)
14189 endname = node->next->label;
14190 else
14191 endname = cfun->fde->dw_fde_second_end;
14192 *listp = new_loc_list (descr,
14193 cfun->fde->dw_fde_second_begin,
14194 endname, secname);
14195 listp = &(*listp)->dw_loc_next;
14200 /* Try to avoid the overhead of a location list emitting a location
14201 expression instead, but only if we didn't have more than one
14202 location entry in the first place. If some entries were not
14203 representable, we don't want to pretend a single entry that was
14204 applies to the entire scope in which the variable is
14205 available. */
14206 if (list && loc_list->first->next)
14207 gen_llsym (list);
14209 return list;
14212 /* Return if the loc_list has only single element and thus can be represented
14213 as location description. */
14215 static bool
14216 single_element_loc_list_p (dw_loc_list_ref list)
14218 gcc_assert (!list->dw_loc_next || list->ll_symbol);
14219 return !list->ll_symbol;
14222 /* To each location in list LIST add loc descr REF. */
14224 static void
14225 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
14227 dw_loc_descr_ref copy;
14228 add_loc_descr (&list->expr, ref);
14229 list = list->dw_loc_next;
14230 while (list)
14232 copy = ggc_alloc<dw_loc_descr_node> ();
14233 memcpy (copy, ref, sizeof (dw_loc_descr_node));
14234 add_loc_descr (&list->expr, copy);
14235 while (copy->dw_loc_next)
14237 dw_loc_descr_ref new_copy = ggc_alloc<dw_loc_descr_node> ();
14238 memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
14239 copy->dw_loc_next = new_copy;
14240 copy = new_copy;
14242 list = list->dw_loc_next;
14246 /* Given two lists RET and LIST
14247 produce location list that is result of adding expression in LIST
14248 to expression in RET on each position in program.
14249 Might be destructive on both RET and LIST.
14251 TODO: We handle only simple cases of RET or LIST having at most one
14252 element. General case would inolve sorting the lists in program order
14253 and merging them that will need some additional work.
14254 Adding that will improve quality of debug info especially for SRA-ed
14255 structures. */
14257 static void
14258 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
14260 if (!list)
14261 return;
14262 if (!*ret)
14264 *ret = list;
14265 return;
14267 if (!list->dw_loc_next)
14269 add_loc_descr_to_each (*ret, list->expr);
14270 return;
14272 if (!(*ret)->dw_loc_next)
14274 add_loc_descr_to_each (list, (*ret)->expr);
14275 *ret = list;
14276 return;
14278 expansion_failed (NULL_TREE, NULL_RTX,
14279 "Don't know how to merge two non-trivial"
14280 " location lists.\n");
14281 *ret = NULL;
14282 return;
14285 /* LOC is constant expression. Try a luck, look it up in constant
14286 pool and return its loc_descr of its address. */
14288 static dw_loc_descr_ref
14289 cst_pool_loc_descr (tree loc)
14291 /* Get an RTL for this, if something has been emitted. */
14292 rtx rtl = lookup_constant_def (loc);
14294 if (!rtl || !MEM_P (rtl))
14296 gcc_assert (!rtl);
14297 return 0;
14299 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
14301 /* TODO: We might get more coverage if we was actually delaying expansion
14302 of all expressions till end of compilation when constant pools are fully
14303 populated. */
14304 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
14306 expansion_failed (loc, NULL_RTX,
14307 "CST value in contant pool but not marked.");
14308 return 0;
14310 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
14311 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
14314 /* Return dw_loc_list representing address of addr_expr LOC
14315 by looking for inner INDIRECT_REF expression and turning
14316 it into simple arithmetics.
14318 See loc_list_from_tree for the meaning of CONTEXT. */
14320 static dw_loc_list_ref
14321 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
14322 const loc_descr_context *context)
14324 tree obj, offset;
14325 HOST_WIDE_INT bitsize, bitpos, bytepos;
14326 machine_mode mode;
14327 int unsignedp, volatilep = 0;
14328 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14330 obj = get_inner_reference (TREE_OPERAND (loc, 0),
14331 &bitsize, &bitpos, &offset, &mode,
14332 &unsignedp, &volatilep, false);
14333 STRIP_NOPS (obj);
14334 if (bitpos % BITS_PER_UNIT)
14336 expansion_failed (loc, NULL_RTX, "bitfield access");
14337 return 0;
14339 if (!INDIRECT_REF_P (obj))
14341 expansion_failed (obj,
14342 NULL_RTX, "no indirect ref in inner refrence");
14343 return 0;
14345 if (!offset && !bitpos)
14346 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
14347 context);
14348 else if (toplev
14349 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
14350 && (dwarf_version >= 4 || !dwarf_strict))
14352 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
14353 if (!list_ret)
14354 return 0;
14355 if (offset)
14357 /* Variable offset. */
14358 list_ret1 = loc_list_from_tree (offset, 0, context);
14359 if (list_ret1 == 0)
14360 return 0;
14361 add_loc_list (&list_ret, list_ret1);
14362 if (!list_ret)
14363 return 0;
14364 add_loc_descr_to_each (list_ret,
14365 new_loc_descr (DW_OP_plus, 0, 0));
14367 bytepos = bitpos / BITS_PER_UNIT;
14368 if (bytepos > 0)
14369 add_loc_descr_to_each (list_ret,
14370 new_loc_descr (DW_OP_plus_uconst,
14371 bytepos, 0));
14372 else if (bytepos < 0)
14373 loc_list_plus_const (list_ret, bytepos);
14374 add_loc_descr_to_each (list_ret,
14375 new_loc_descr (DW_OP_stack_value, 0, 0));
14377 return list_ret;
14381 /* Helper structure for location descriptions generation. */
14382 struct loc_descr_context
14384 /* The type that is implicitly referenced by DW_OP_push_object_address, or
14385 NULL_TREE if DW_OP_push_object_address in invalid for this location
14386 description. This is used when processing PLACEHOLDER_EXPR nodes. */
14387 tree context_type;
14388 /* The ..._DECL node that should be translated as a
14389 DW_OP_push_object_address operation. */
14390 tree base_decl;
14393 /* Generate Dwarf location list representing LOC.
14394 If WANT_ADDRESS is false, expression computing LOC will be computed
14395 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
14396 if WANT_ADDRESS is 2, expression computing address useable in location
14397 will be returned (i.e. DW_OP_reg can be used
14398 to refer to register values).
14400 CONTEXT provides information to customize the location descriptions
14401 generation. Its context_type field specifies what type is implicitly
14402 referenced by DW_OP_push_object_address. If it is NULL_TREE, this operation
14403 will not be generated.
14405 If CONTEXT is NULL, the behavior is the same as if both context_type and
14406 base_decl fields were NULL_TREE. */
14408 static dw_loc_list_ref
14409 loc_list_from_tree (tree loc, int want_address,
14410 const struct loc_descr_context *context)
14412 dw_loc_descr_ref ret = NULL, ret1 = NULL;
14413 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14414 int have_address = 0;
14415 enum dwarf_location_atom op;
14417 /* ??? Most of the time we do not take proper care for sign/zero
14418 extending the values properly. Hopefully this won't be a real
14419 problem... */
14421 if (context != NULL
14422 && context->base_decl == loc
14423 && want_address == 0)
14425 if (dwarf_version >= 3 || !dwarf_strict)
14426 return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
14427 NULL, NULL, NULL);
14428 else
14429 return NULL;
14432 switch (TREE_CODE (loc))
14434 case ERROR_MARK:
14435 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
14436 return 0;
14438 case PLACEHOLDER_EXPR:
14439 /* This case involves extracting fields from an object to determine the
14440 position of other fields. It is supposed to appear only as the first
14441 operand of COMPONENT_REF nodes and to reference precisely the type
14442 that the context allows. */
14443 if (context != NULL
14444 && TREE_TYPE (loc) == context->context_type
14445 && want_address >= 1)
14447 if (dwarf_version >= 3 || !dwarf_strict)
14449 ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
14450 have_address = 1;
14451 break;
14453 else
14454 return NULL;
14456 else
14457 expansion_failed (loc, NULL_RTX,
14458 "PLACEHOLDER_EXPR for an unexpected type");
14459 break;
14461 case CALL_EXPR:
14462 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
14463 /* There are no opcodes for these operations. */
14464 return 0;
14466 case PREINCREMENT_EXPR:
14467 case PREDECREMENT_EXPR:
14468 case POSTINCREMENT_EXPR:
14469 case POSTDECREMENT_EXPR:
14470 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
14471 /* There are no opcodes for these operations. */
14472 return 0;
14474 case ADDR_EXPR:
14475 /* If we already want an address, see if there is INDIRECT_REF inside
14476 e.g. for &this->field. */
14477 if (want_address)
14479 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
14480 (loc, want_address == 2, context);
14481 if (list_ret)
14482 have_address = 1;
14483 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
14484 && (ret = cst_pool_loc_descr (loc)))
14485 have_address = 1;
14487 /* Otherwise, process the argument and look for the address. */
14488 if (!list_ret && !ret)
14489 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1, context);
14490 else
14492 if (want_address)
14493 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
14494 return NULL;
14496 break;
14498 case VAR_DECL:
14499 if (DECL_THREAD_LOCAL_P (loc))
14501 rtx rtl;
14502 enum dwarf_location_atom tls_op;
14503 enum dtprel_bool dtprel = dtprel_false;
14505 if (targetm.have_tls)
14507 /* If this is not defined, we have no way to emit the
14508 data. */
14509 if (!targetm.asm_out.output_dwarf_dtprel)
14510 return 0;
14512 /* The way DW_OP_GNU_push_tls_address is specified, we
14513 can only look up addresses of objects in the current
14514 module. We used DW_OP_addr as first op, but that's
14515 wrong, because DW_OP_addr is relocated by the debug
14516 info consumer, while DW_OP_GNU_push_tls_address
14517 operand shouldn't be. */
14518 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
14519 return 0;
14520 dtprel = dtprel_true;
14521 tls_op = DW_OP_GNU_push_tls_address;
14523 else
14525 if (!targetm.emutls.debug_form_tls_address
14526 || !(dwarf_version >= 3 || !dwarf_strict))
14527 return 0;
14528 /* We stuffed the control variable into the DECL_VALUE_EXPR
14529 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
14530 no longer appear in gimple code. We used the control
14531 variable in specific so that we could pick it up here. */
14532 loc = DECL_VALUE_EXPR (loc);
14533 tls_op = DW_OP_form_tls_address;
14536 rtl = rtl_for_decl_location (loc);
14537 if (rtl == NULL_RTX)
14538 return 0;
14540 if (!MEM_P (rtl))
14541 return 0;
14542 rtl = XEXP (rtl, 0);
14543 if (! CONSTANT_P (rtl))
14544 return 0;
14546 ret = new_addr_loc_descr (rtl, dtprel);
14547 ret1 = new_loc_descr (tls_op, 0, 0);
14548 add_loc_descr (&ret, ret1);
14550 have_address = 1;
14551 break;
14553 /* FALLTHRU */
14555 case PARM_DECL:
14556 case RESULT_DECL:
14557 if (DECL_HAS_VALUE_EXPR_P (loc))
14558 return loc_list_from_tree (DECL_VALUE_EXPR (loc),
14559 want_address, context);
14560 /* FALLTHRU */
14562 case FUNCTION_DECL:
14564 rtx rtl;
14565 var_loc_list *loc_list = lookup_decl_loc (loc);
14567 if (loc_list && loc_list->first)
14569 list_ret = dw_loc_list (loc_list, loc, want_address);
14570 have_address = want_address != 0;
14571 break;
14573 rtl = rtl_for_decl_location (loc);
14574 if (rtl == NULL_RTX)
14576 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
14577 return 0;
14579 else if (CONST_INT_P (rtl))
14581 HOST_WIDE_INT val = INTVAL (rtl);
14582 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14583 val &= GET_MODE_MASK (DECL_MODE (loc));
14584 ret = int_loc_descriptor (val);
14586 else if (GET_CODE (rtl) == CONST_STRING)
14588 expansion_failed (loc, NULL_RTX, "CONST_STRING");
14589 return 0;
14591 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
14592 ret = new_addr_loc_descr (rtl, dtprel_false);
14593 else
14595 machine_mode mode, mem_mode;
14597 /* Certain constructs can only be represented at top-level. */
14598 if (want_address == 2)
14600 ret = loc_descriptor (rtl, VOIDmode,
14601 VAR_INIT_STATUS_INITIALIZED);
14602 have_address = 1;
14604 else
14606 mode = GET_MODE (rtl);
14607 mem_mode = VOIDmode;
14608 if (MEM_P (rtl))
14610 mem_mode = mode;
14611 mode = get_address_mode (rtl);
14612 rtl = XEXP (rtl, 0);
14613 have_address = 1;
14615 ret = mem_loc_descriptor (rtl, mode, mem_mode,
14616 VAR_INIT_STATUS_INITIALIZED);
14618 if (!ret)
14619 expansion_failed (loc, rtl,
14620 "failed to produce loc descriptor for rtl");
14623 break;
14625 case MEM_REF:
14626 if (!integer_zerop (TREE_OPERAND (loc, 1)))
14628 have_address = 1;
14629 goto do_plus;
14631 /* Fallthru. */
14632 case INDIRECT_REF:
14633 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14634 have_address = 1;
14635 break;
14637 case TARGET_MEM_REF:
14638 case SSA_NAME:
14639 return NULL;
14641 case COMPOUND_EXPR:
14642 return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address, context);
14644 CASE_CONVERT:
14645 case VIEW_CONVERT_EXPR:
14646 case SAVE_EXPR:
14647 case MODIFY_EXPR:
14648 return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address, context);
14650 case COMPONENT_REF:
14651 case BIT_FIELD_REF:
14652 case ARRAY_REF:
14653 case ARRAY_RANGE_REF:
14654 case REALPART_EXPR:
14655 case IMAGPART_EXPR:
14657 tree obj, offset;
14658 HOST_WIDE_INT bitsize, bitpos, bytepos;
14659 machine_mode mode;
14660 int unsignedp, volatilep = 0;
14662 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
14663 &unsignedp, &volatilep, false);
14665 gcc_assert (obj != loc);
14667 list_ret = loc_list_from_tree (obj,
14668 want_address == 2
14669 && !bitpos && !offset ? 2 : 1,
14670 context);
14671 /* TODO: We can extract value of the small expression via shifting even
14672 for nonzero bitpos. */
14673 if (list_ret == 0)
14674 return 0;
14675 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
14677 expansion_failed (loc, NULL_RTX,
14678 "bitfield access");
14679 return 0;
14682 if (offset != NULL_TREE)
14684 /* Variable offset. */
14685 list_ret1 = loc_list_from_tree (offset, 0, context);
14686 if (list_ret1 == 0)
14687 return 0;
14688 add_loc_list (&list_ret, list_ret1);
14689 if (!list_ret)
14690 return 0;
14691 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
14694 bytepos = bitpos / BITS_PER_UNIT;
14695 if (bytepos > 0)
14696 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
14697 else if (bytepos < 0)
14698 loc_list_plus_const (list_ret, bytepos);
14700 have_address = 1;
14701 break;
14704 case INTEGER_CST:
14705 if ((want_address || !tree_fits_shwi_p (loc))
14706 && (ret = cst_pool_loc_descr (loc)))
14707 have_address = 1;
14708 else if (want_address == 2
14709 && tree_fits_shwi_p (loc)
14710 && (ret = address_of_int_loc_descriptor
14711 (int_size_in_bytes (TREE_TYPE (loc)),
14712 tree_to_shwi (loc))))
14713 have_address = 1;
14714 else if (tree_fits_shwi_p (loc))
14715 ret = int_loc_descriptor (tree_to_shwi (loc));
14716 else
14718 expansion_failed (loc, NULL_RTX,
14719 "Integer operand is not host integer");
14720 return 0;
14722 break;
14724 case CONSTRUCTOR:
14725 case REAL_CST:
14726 case STRING_CST:
14727 case COMPLEX_CST:
14728 if ((ret = cst_pool_loc_descr (loc)))
14729 have_address = 1;
14730 else
14731 /* We can construct small constants here using int_loc_descriptor. */
14732 expansion_failed (loc, NULL_RTX,
14733 "constructor or constant not in constant pool");
14734 break;
14736 case TRUTH_AND_EXPR:
14737 case TRUTH_ANDIF_EXPR:
14738 case BIT_AND_EXPR:
14739 op = DW_OP_and;
14740 goto do_binop;
14742 case TRUTH_XOR_EXPR:
14743 case BIT_XOR_EXPR:
14744 op = DW_OP_xor;
14745 goto do_binop;
14747 case TRUTH_OR_EXPR:
14748 case TRUTH_ORIF_EXPR:
14749 case BIT_IOR_EXPR:
14750 op = DW_OP_or;
14751 goto do_binop;
14753 case FLOOR_DIV_EXPR:
14754 case CEIL_DIV_EXPR:
14755 case ROUND_DIV_EXPR:
14756 case TRUNC_DIV_EXPR:
14757 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14758 return 0;
14759 op = DW_OP_div;
14760 goto do_binop;
14762 case MINUS_EXPR:
14763 op = DW_OP_minus;
14764 goto do_binop;
14766 case FLOOR_MOD_EXPR:
14767 case CEIL_MOD_EXPR:
14768 case ROUND_MOD_EXPR:
14769 case TRUNC_MOD_EXPR:
14770 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14772 op = DW_OP_mod;
14773 goto do_binop;
14775 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14776 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
14777 if (list_ret == 0 || list_ret1 == 0)
14778 return 0;
14780 add_loc_list (&list_ret, list_ret1);
14781 if (list_ret == 0)
14782 return 0;
14783 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14784 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14785 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
14786 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
14787 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
14788 break;
14790 case MULT_EXPR:
14791 op = DW_OP_mul;
14792 goto do_binop;
14794 case LSHIFT_EXPR:
14795 op = DW_OP_shl;
14796 goto do_binop;
14798 case RSHIFT_EXPR:
14799 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
14800 goto do_binop;
14802 case POINTER_PLUS_EXPR:
14803 case PLUS_EXPR:
14804 do_plus:
14805 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
14807 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14808 if (list_ret == 0)
14809 return 0;
14811 loc_list_plus_const (list_ret, tree_to_shwi (TREE_OPERAND (loc, 1)));
14812 break;
14815 op = DW_OP_plus;
14816 goto do_binop;
14818 case LE_EXPR:
14819 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14820 return 0;
14822 op = DW_OP_le;
14823 goto do_binop;
14825 case GE_EXPR:
14826 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14827 return 0;
14829 op = DW_OP_ge;
14830 goto do_binop;
14832 case LT_EXPR:
14833 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14834 return 0;
14836 op = DW_OP_lt;
14837 goto do_binop;
14839 case GT_EXPR:
14840 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14841 return 0;
14843 op = DW_OP_gt;
14844 goto do_binop;
14846 case EQ_EXPR:
14847 op = DW_OP_eq;
14848 goto do_binop;
14850 case NE_EXPR:
14851 op = DW_OP_ne;
14852 goto do_binop;
14854 do_binop:
14855 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14856 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
14857 if (list_ret == 0 || list_ret1 == 0)
14858 return 0;
14860 add_loc_list (&list_ret, list_ret1);
14861 if (list_ret == 0)
14862 return 0;
14863 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14864 break;
14866 case TRUTH_NOT_EXPR:
14867 case BIT_NOT_EXPR:
14868 op = DW_OP_not;
14869 goto do_unop;
14871 case ABS_EXPR:
14872 op = DW_OP_abs;
14873 goto do_unop;
14875 case NEGATE_EXPR:
14876 op = DW_OP_neg;
14877 goto do_unop;
14879 do_unop:
14880 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14881 if (list_ret == 0)
14882 return 0;
14884 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14885 break;
14887 case MIN_EXPR:
14888 case MAX_EXPR:
14890 const enum tree_code code =
14891 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
14893 loc = build3 (COND_EXPR, TREE_TYPE (loc),
14894 build2 (code, integer_type_node,
14895 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
14896 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
14899 /* ... fall through ... */
14901 case COND_EXPR:
14903 dw_loc_descr_ref lhs
14904 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
14905 dw_loc_list_ref rhs
14906 = loc_list_from_tree (TREE_OPERAND (loc, 2), 0, context);
14907 dw_loc_descr_ref bra_node, jump_node, tmp;
14909 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14910 if (list_ret == 0 || lhs == 0 || rhs == 0)
14911 return 0;
14913 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14914 add_loc_descr_to_each (list_ret, bra_node);
14916 add_loc_list (&list_ret, rhs);
14917 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
14918 add_loc_descr_to_each (list_ret, jump_node);
14920 add_loc_descr_to_each (list_ret, lhs);
14921 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14922 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
14924 /* ??? Need a node to point the skip at. Use a nop. */
14925 tmp = new_loc_descr (DW_OP_nop, 0, 0);
14926 add_loc_descr_to_each (list_ret, tmp);
14927 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14928 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
14930 break;
14932 case FIX_TRUNC_EXPR:
14933 return 0;
14935 default:
14936 /* Leave front-end specific codes as simply unknown. This comes
14937 up, for instance, with the C STMT_EXPR. */
14938 if ((unsigned int) TREE_CODE (loc)
14939 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
14941 expansion_failed (loc, NULL_RTX,
14942 "language specific tree node");
14943 return 0;
14946 #ifdef ENABLE_CHECKING
14947 /* Otherwise this is a generic code; we should just lists all of
14948 these explicitly. We forgot one. */
14949 gcc_unreachable ();
14950 #else
14951 /* In a release build, we want to degrade gracefully: better to
14952 generate incomplete debugging information than to crash. */
14953 return NULL;
14954 #endif
14957 if (!ret && !list_ret)
14958 return 0;
14960 if (want_address == 2 && !have_address
14961 && (dwarf_version >= 4 || !dwarf_strict))
14963 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14965 expansion_failed (loc, NULL_RTX,
14966 "DWARF address size mismatch");
14967 return 0;
14969 if (ret)
14970 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
14971 else
14972 add_loc_descr_to_each (list_ret,
14973 new_loc_descr (DW_OP_stack_value, 0, 0));
14974 have_address = 1;
14976 /* Show if we can't fill the request for an address. */
14977 if (want_address && !have_address)
14979 expansion_failed (loc, NULL_RTX,
14980 "Want address and only have value");
14981 return 0;
14984 gcc_assert (!ret || !list_ret);
14986 /* If we've got an address and don't want one, dereference. */
14987 if (!want_address && have_address)
14989 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14991 if (size > DWARF2_ADDR_SIZE || size == -1)
14993 expansion_failed (loc, NULL_RTX,
14994 "DWARF address size mismatch");
14995 return 0;
14997 else if (size == DWARF2_ADDR_SIZE)
14998 op = DW_OP_deref;
14999 else
15000 op = DW_OP_deref_size;
15002 if (ret)
15003 add_loc_descr (&ret, new_loc_descr (op, size, 0));
15004 else
15005 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
15007 if (ret)
15008 list_ret = new_loc_list (ret, NULL, NULL, NULL);
15010 return list_ret;
15013 /* Same as above but return only single location expression. */
15014 static dw_loc_descr_ref
15015 loc_descriptor_from_tree (tree loc, int want_address,
15016 const struct loc_descr_context *context)
15018 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
15019 if (!ret)
15020 return NULL;
15021 if (ret->dw_loc_next)
15023 expansion_failed (loc, NULL_RTX,
15024 "Location list where only loc descriptor needed");
15025 return NULL;
15027 return ret->expr;
15030 /* Given a value, round it up to the lowest multiple of `boundary'
15031 which is not less than the value itself. */
15033 static inline HOST_WIDE_INT
15034 ceiling (HOST_WIDE_INT value, unsigned int boundary)
15036 return (((value + boundary - 1) / boundary) * boundary);
15039 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
15040 pointer to the declared type for the relevant field variable, or return
15041 `integer_type_node' if the given node turns out to be an
15042 ERROR_MARK node. */
15044 static inline tree
15045 field_type (const_tree decl)
15047 tree type;
15049 if (TREE_CODE (decl) == ERROR_MARK)
15050 return integer_type_node;
15052 type = DECL_BIT_FIELD_TYPE (decl);
15053 if (type == NULL_TREE)
15054 type = TREE_TYPE (decl);
15056 return type;
15059 /* Given a pointer to a tree node, return the alignment in bits for
15060 it, or else return BITS_PER_WORD if the node actually turns out to
15061 be an ERROR_MARK node. */
15063 static inline unsigned
15064 simple_type_align_in_bits (const_tree type)
15066 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
15069 static inline unsigned
15070 simple_decl_align_in_bits (const_tree decl)
15072 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
15075 /* Return the result of rounding T up to ALIGN. */
15077 static inline offset_int
15078 round_up_to_align (const offset_int &t, unsigned int align)
15080 return wi::udiv_trunc (t + align - 1, align) * align;
15083 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
15084 lowest addressed byte of the "containing object" for the given FIELD_DECL,
15085 or return 0 if we are unable to determine what that offset is, either
15086 because the argument turns out to be a pointer to an ERROR_MARK node, or
15087 because the offset is actually variable. (We can't handle the latter case
15088 just yet). */
15090 static HOST_WIDE_INT
15091 field_byte_offset (const_tree decl)
15093 offset_int object_offset_in_bits;
15094 offset_int object_offset_in_bytes;
15095 offset_int bitpos_int;
15097 if (TREE_CODE (decl) == ERROR_MARK)
15098 return 0;
15100 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
15102 /* We cannot yet cope with fields whose positions are variable, so
15103 for now, when we see such things, we simply return 0. Someday, we may
15104 be able to handle such cases, but it will be damn difficult. */
15105 if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
15106 return 0;
15108 bitpos_int = wi::to_offset (bit_position (decl));
15110 #ifdef PCC_BITFIELD_TYPE_MATTERS
15111 if (PCC_BITFIELD_TYPE_MATTERS)
15113 tree type;
15114 tree field_size_tree;
15115 offset_int deepest_bitpos;
15116 offset_int field_size_in_bits;
15117 unsigned int type_align_in_bits;
15118 unsigned int decl_align_in_bits;
15119 offset_int type_size_in_bits;
15121 type = field_type (decl);
15122 type_size_in_bits = offset_int_type_size_in_bits (type);
15123 type_align_in_bits = simple_type_align_in_bits (type);
15125 field_size_tree = DECL_SIZE (decl);
15127 /* The size could be unspecified if there was an error, or for
15128 a flexible array member. */
15129 if (!field_size_tree)
15130 field_size_tree = bitsize_zero_node;
15132 /* If the size of the field is not constant, use the type size. */
15133 if (TREE_CODE (field_size_tree) == INTEGER_CST)
15134 field_size_in_bits = wi::to_offset (field_size_tree);
15135 else
15136 field_size_in_bits = type_size_in_bits;
15138 decl_align_in_bits = simple_decl_align_in_bits (decl);
15140 /* The GCC front-end doesn't make any attempt to keep track of the
15141 starting bit offset (relative to the start of the containing
15142 structure type) of the hypothetical "containing object" for a
15143 bit-field. Thus, when computing the byte offset value for the
15144 start of the "containing object" of a bit-field, we must deduce
15145 this information on our own. This can be rather tricky to do in
15146 some cases. For example, handling the following structure type
15147 definition when compiling for an i386/i486 target (which only
15148 aligns long long's to 32-bit boundaries) can be very tricky:
15150 struct S { int field1; long long field2:31; };
15152 Fortunately, there is a simple rule-of-thumb which can be used
15153 in such cases. When compiling for an i386/i486, GCC will
15154 allocate 8 bytes for the structure shown above. It decides to
15155 do this based upon one simple rule for bit-field allocation.
15156 GCC allocates each "containing object" for each bit-field at
15157 the first (i.e. lowest addressed) legitimate alignment boundary
15158 (based upon the required minimum alignment for the declared
15159 type of the field) which it can possibly use, subject to the
15160 condition that there is still enough available space remaining
15161 in the containing object (when allocated at the selected point)
15162 to fully accommodate all of the bits of the bit-field itself.
15164 This simple rule makes it obvious why GCC allocates 8 bytes for
15165 each object of the structure type shown above. When looking
15166 for a place to allocate the "containing object" for `field2',
15167 the compiler simply tries to allocate a 64-bit "containing
15168 object" at each successive 32-bit boundary (starting at zero)
15169 until it finds a place to allocate that 64- bit field such that
15170 at least 31 contiguous (and previously unallocated) bits remain
15171 within that selected 64 bit field. (As it turns out, for the
15172 example above, the compiler finds it is OK to allocate the
15173 "containing object" 64-bit field at bit-offset zero within the
15174 structure type.)
15176 Here we attempt to work backwards from the limited set of facts
15177 we're given, and we try to deduce from those facts, where GCC
15178 must have believed that the containing object started (within
15179 the structure type). The value we deduce is then used (by the
15180 callers of this routine) to generate DW_AT_location and
15181 DW_AT_bit_offset attributes for fields (both bit-fields and, in
15182 the case of DW_AT_location, regular fields as well). */
15184 /* Figure out the bit-distance from the start of the structure to
15185 the "deepest" bit of the bit-field. */
15186 deepest_bitpos = bitpos_int + field_size_in_bits;
15188 /* This is the tricky part. Use some fancy footwork to deduce
15189 where the lowest addressed bit of the containing object must
15190 be. */
15191 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
15193 /* Round up to type_align by default. This works best for
15194 bitfields. */
15195 object_offset_in_bits
15196 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
15198 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
15200 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
15202 /* Round up to decl_align instead. */
15203 object_offset_in_bits
15204 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
15207 else
15208 #endif /* PCC_BITFIELD_TYPE_MATTERS */
15209 object_offset_in_bits = bitpos_int;
15211 object_offset_in_bytes
15212 = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
15213 return object_offset_in_bytes.to_shwi ();
15216 /* The following routines define various Dwarf attributes and any data
15217 associated with them. */
15219 /* Add a location description attribute value to a DIE.
15221 This emits location attributes suitable for whole variables and
15222 whole parameters. Note that the location attributes for struct fields are
15223 generated by the routine `data_member_location_attribute' below. */
15225 static inline void
15226 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
15227 dw_loc_list_ref descr)
15229 if (descr == 0)
15230 return;
15231 if (single_element_loc_list_p (descr))
15232 add_AT_loc (die, attr_kind, descr->expr);
15233 else
15234 add_AT_loc_list (die, attr_kind, descr);
15237 /* Add DW_AT_accessibility attribute to DIE if needed. */
15239 static void
15240 add_accessibility_attribute (dw_die_ref die, tree decl)
15242 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
15243 children, otherwise the default is DW_ACCESS_public. In DWARF2
15244 the default has always been DW_ACCESS_public. */
15245 if (TREE_PROTECTED (decl))
15246 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
15247 else if (TREE_PRIVATE (decl))
15249 if (dwarf_version == 2
15250 || die->die_parent == NULL
15251 || die->die_parent->die_tag != DW_TAG_class_type)
15252 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
15254 else if (dwarf_version > 2
15255 && die->die_parent
15256 && die->die_parent->die_tag == DW_TAG_class_type)
15257 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
15260 /* Attach the specialized form of location attribute used for data members of
15261 struct and union types. In the special case of a FIELD_DECL node which
15262 represents a bit-field, the "offset" part of this special location
15263 descriptor must indicate the distance in bytes from the lowest-addressed
15264 byte of the containing struct or union type to the lowest-addressed byte of
15265 the "containing object" for the bit-field. (See the `field_byte_offset'
15266 function above).
15268 For any given bit-field, the "containing object" is a hypothetical object
15269 (of some integral or enum type) within which the given bit-field lives. The
15270 type of this hypothetical "containing object" is always the same as the
15271 declared type of the individual bit-field itself (for GCC anyway... the
15272 DWARF spec doesn't actually mandate this). Note that it is the size (in
15273 bytes) of the hypothetical "containing object" which will be given in the
15274 DW_AT_byte_size attribute for this bit-field. (See the
15275 `byte_size_attribute' function below.) It is also used when calculating the
15276 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
15277 function below.) */
15279 static void
15280 add_data_member_location_attribute (dw_die_ref die, tree decl)
15282 HOST_WIDE_INT offset;
15283 dw_loc_descr_ref loc_descr = 0;
15285 if (TREE_CODE (decl) == TREE_BINFO)
15287 /* We're working on the TAG_inheritance for a base class. */
15288 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
15290 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
15291 aren't at a fixed offset from all (sub)objects of the same
15292 type. We need to extract the appropriate offset from our
15293 vtable. The following dwarf expression means
15295 BaseAddr = ObAddr + *((*ObAddr) - Offset)
15297 This is specific to the V3 ABI, of course. */
15299 dw_loc_descr_ref tmp;
15301 /* Make a copy of the object address. */
15302 tmp = new_loc_descr (DW_OP_dup, 0, 0);
15303 add_loc_descr (&loc_descr, tmp);
15305 /* Extract the vtable address. */
15306 tmp = new_loc_descr (DW_OP_deref, 0, 0);
15307 add_loc_descr (&loc_descr, tmp);
15309 /* Calculate the address of the offset. */
15310 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
15311 gcc_assert (offset < 0);
15313 tmp = int_loc_descriptor (-offset);
15314 add_loc_descr (&loc_descr, tmp);
15315 tmp = new_loc_descr (DW_OP_minus, 0, 0);
15316 add_loc_descr (&loc_descr, tmp);
15318 /* Extract the offset. */
15319 tmp = new_loc_descr (DW_OP_deref, 0, 0);
15320 add_loc_descr (&loc_descr, tmp);
15322 /* Add it to the object address. */
15323 tmp = new_loc_descr (DW_OP_plus, 0, 0);
15324 add_loc_descr (&loc_descr, tmp);
15326 else
15327 offset = tree_to_shwi (BINFO_OFFSET (decl));
15329 else
15330 offset = field_byte_offset (decl);
15332 if (! loc_descr)
15334 if (dwarf_version > 2)
15336 /* Don't need to output a location expression, just the constant. */
15337 if (offset < 0)
15338 add_AT_int (die, DW_AT_data_member_location, offset);
15339 else
15340 add_AT_unsigned (die, DW_AT_data_member_location, offset);
15341 return;
15343 else
15345 enum dwarf_location_atom op;
15347 /* The DWARF2 standard says that we should assume that the structure
15348 address is already on the stack, so we can specify a structure
15349 field address by using DW_OP_plus_uconst. */
15350 op = DW_OP_plus_uconst;
15351 loc_descr = new_loc_descr (op, offset, 0);
15355 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
15358 /* Writes integer values to dw_vec_const array. */
15360 static void
15361 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
15363 while (size != 0)
15365 *dest++ = val & 0xff;
15366 val >>= 8;
15367 --size;
15371 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
15373 static HOST_WIDE_INT
15374 extract_int (const unsigned char *src, unsigned int size)
15376 HOST_WIDE_INT val = 0;
15378 src += size;
15379 while (size != 0)
15381 val <<= 8;
15382 val |= *--src & 0xff;
15383 --size;
15385 return val;
15388 /* Writes wide_int values to dw_vec_const array. */
15390 static void
15391 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
15393 int i;
15395 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
15397 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
15398 return;
15401 /* We'd have to extend this code to support odd sizes. */
15402 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
15404 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
15406 if (WORDS_BIG_ENDIAN)
15407 for (i = n - 1; i >= 0; i--)
15409 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
15410 dest += sizeof (HOST_WIDE_INT);
15412 else
15413 for (i = 0; i < n; i++)
15415 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
15416 dest += sizeof (HOST_WIDE_INT);
15420 /* Writes floating point values to dw_vec_const array. */
15422 static void
15423 insert_float (const_rtx rtl, unsigned char *array)
15425 REAL_VALUE_TYPE rv;
15426 long val[4];
15427 int i;
15429 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
15430 real_to_target (val, &rv, GET_MODE (rtl));
15432 /* real_to_target puts 32-bit pieces in each long. Pack them. */
15433 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
15435 insert_int (val[i], 4, array);
15436 array += 4;
15440 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
15441 does not have a "location" either in memory or in a register. These
15442 things can arise in GNU C when a constant is passed as an actual parameter
15443 to an inlined function. They can also arise in C++ where declared
15444 constants do not necessarily get memory "homes". */
15446 static bool
15447 add_const_value_attribute (dw_die_ref die, rtx rtl)
15449 switch (GET_CODE (rtl))
15451 case CONST_INT:
15453 HOST_WIDE_INT val = INTVAL (rtl);
15455 if (val < 0)
15456 add_AT_int (die, DW_AT_const_value, val);
15457 else
15458 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
15460 return true;
15462 case CONST_WIDE_INT:
15463 add_AT_wide (die, DW_AT_const_value,
15464 std::make_pair (rtl, GET_MODE (rtl)));
15465 return true;
15467 case CONST_DOUBLE:
15468 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
15469 floating-point constant. A CONST_DOUBLE is used whenever the
15470 constant requires more than one word in order to be adequately
15471 represented. */
15473 machine_mode mode = GET_MODE (rtl);
15475 if (TARGET_SUPPORTS_WIDE_INT == 0 && !SCALAR_FLOAT_MODE_P (mode))
15476 add_AT_double (die, DW_AT_const_value,
15477 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
15478 else
15480 unsigned int length = GET_MODE_SIZE (mode);
15481 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15483 insert_float (rtl, array);
15484 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
15487 return true;
15489 case CONST_VECTOR:
15491 machine_mode mode = GET_MODE (rtl);
15492 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
15493 unsigned int length = CONST_VECTOR_NUNITS (rtl);
15494 unsigned char *array
15495 = ggc_vec_alloc<unsigned char> (length * elt_size);
15496 unsigned int i;
15497 unsigned char *p;
15498 machine_mode imode = GET_MODE_INNER (mode);
15500 switch (GET_MODE_CLASS (mode))
15502 case MODE_VECTOR_INT:
15503 for (i = 0, p = array; i < length; i++, p += elt_size)
15505 rtx elt = CONST_VECTOR_ELT (rtl, i);
15506 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
15508 break;
15510 case MODE_VECTOR_FLOAT:
15511 for (i = 0, p = array; i < length; i++, p += elt_size)
15513 rtx elt = CONST_VECTOR_ELT (rtl, i);
15514 insert_float (elt, p);
15516 break;
15518 default:
15519 gcc_unreachable ();
15522 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
15524 return true;
15526 case CONST_STRING:
15527 if (dwarf_version >= 4 || !dwarf_strict)
15529 dw_loc_descr_ref loc_result;
15530 resolve_one_addr (&rtl);
15531 rtl_addr:
15532 loc_result = new_addr_loc_descr (rtl, dtprel_false);
15533 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
15534 add_AT_loc (die, DW_AT_location, loc_result);
15535 vec_safe_push (used_rtx_array, rtl);
15536 return true;
15538 return false;
15540 case CONST:
15541 if (CONSTANT_P (XEXP (rtl, 0)))
15542 return add_const_value_attribute (die, XEXP (rtl, 0));
15543 /* FALLTHROUGH */
15544 case SYMBOL_REF:
15545 if (!const_ok_for_output (rtl))
15546 return false;
15547 case LABEL_REF:
15548 if (dwarf_version >= 4 || !dwarf_strict)
15549 goto rtl_addr;
15550 return false;
15552 case PLUS:
15553 /* In cases where an inlined instance of an inline function is passed
15554 the address of an `auto' variable (which is local to the caller) we
15555 can get a situation where the DECL_RTL of the artificial local
15556 variable (for the inlining) which acts as a stand-in for the
15557 corresponding formal parameter (of the inline function) will look
15558 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
15559 exactly a compile-time constant expression, but it isn't the address
15560 of the (artificial) local variable either. Rather, it represents the
15561 *value* which the artificial local variable always has during its
15562 lifetime. We currently have no way to represent such quasi-constant
15563 values in Dwarf, so for now we just punt and generate nothing. */
15564 return false;
15566 case HIGH:
15567 case CONST_FIXED:
15568 return false;
15570 case MEM:
15571 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
15572 && MEM_READONLY_P (rtl)
15573 && GET_MODE (rtl) == BLKmode)
15575 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
15576 return true;
15578 return false;
15580 default:
15581 /* No other kinds of rtx should be possible here. */
15582 gcc_unreachable ();
15584 return false;
15587 /* Determine whether the evaluation of EXPR references any variables
15588 or functions which aren't otherwise used (and therefore may not be
15589 output). */
15590 static tree
15591 reference_to_unused (tree * tp, int * walk_subtrees,
15592 void * data ATTRIBUTE_UNUSED)
15594 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
15595 *walk_subtrees = 0;
15597 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
15598 && ! TREE_ASM_WRITTEN (*tp))
15599 return *tp;
15600 /* ??? The C++ FE emits debug information for using decls, so
15601 putting gcc_unreachable here falls over. See PR31899. For now
15602 be conservative. */
15603 else if (!symtab->global_info_ready
15604 && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
15605 return *tp;
15606 else if (TREE_CODE (*tp) == VAR_DECL)
15608 varpool_node *node = varpool_node::get (*tp);
15609 if (!node || !node->definition)
15610 return *tp;
15612 else if (TREE_CODE (*tp) == FUNCTION_DECL
15613 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
15615 /* The call graph machinery must have finished analyzing,
15616 optimizing and gimplifying the CU by now.
15617 So if *TP has no call graph node associated
15618 to it, it means *TP will not be emitted. */
15619 if (!cgraph_node::get (*tp))
15620 return *tp;
15622 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
15623 return *tp;
15625 return NULL_TREE;
15628 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
15629 for use in a later add_const_value_attribute call. */
15631 static rtx
15632 rtl_for_decl_init (tree init, tree type)
15634 rtx rtl = NULL_RTX;
15636 STRIP_NOPS (init);
15638 /* If a variable is initialized with a string constant without embedded
15639 zeros, build CONST_STRING. */
15640 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
15642 tree enttype = TREE_TYPE (type);
15643 tree domain = TYPE_DOMAIN (type);
15644 machine_mode mode = TYPE_MODE (enttype);
15646 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
15647 && domain
15648 && integer_zerop (TYPE_MIN_VALUE (domain))
15649 && compare_tree_int (TYPE_MAX_VALUE (domain),
15650 TREE_STRING_LENGTH (init) - 1) == 0
15651 && ((size_t) TREE_STRING_LENGTH (init)
15652 == strlen (TREE_STRING_POINTER (init)) + 1))
15654 rtl = gen_rtx_CONST_STRING (VOIDmode,
15655 ggc_strdup (TREE_STRING_POINTER (init)));
15656 rtl = gen_rtx_MEM (BLKmode, rtl);
15657 MEM_READONLY_P (rtl) = 1;
15660 /* Other aggregates, and complex values, could be represented using
15661 CONCAT: FIXME! */
15662 else if (AGGREGATE_TYPE_P (type)
15663 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
15664 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
15665 || TREE_CODE (type) == COMPLEX_TYPE)
15667 /* Vectors only work if their mode is supported by the target.
15668 FIXME: generic vectors ought to work too. */
15669 else if (TREE_CODE (type) == VECTOR_TYPE
15670 && !VECTOR_MODE_P (TYPE_MODE (type)))
15672 /* If the initializer is something that we know will expand into an
15673 immediate RTL constant, expand it now. We must be careful not to
15674 reference variables which won't be output. */
15675 else if (initializer_constant_valid_p (init, type)
15676 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
15678 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
15679 possible. */
15680 if (TREE_CODE (type) == VECTOR_TYPE)
15681 switch (TREE_CODE (init))
15683 case VECTOR_CST:
15684 break;
15685 case CONSTRUCTOR:
15686 if (TREE_CONSTANT (init))
15688 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
15689 bool constant_p = true;
15690 tree value;
15691 unsigned HOST_WIDE_INT ix;
15693 /* Even when ctor is constant, it might contain non-*_CST
15694 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
15695 belong into VECTOR_CST nodes. */
15696 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
15697 if (!CONSTANT_CLASS_P (value))
15699 constant_p = false;
15700 break;
15703 if (constant_p)
15705 init = build_vector_from_ctor (type, elts);
15706 break;
15709 /* FALLTHRU */
15711 default:
15712 return NULL;
15715 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
15717 /* If expand_expr returns a MEM, it wasn't immediate. */
15718 gcc_assert (!rtl || !MEM_P (rtl));
15721 return rtl;
15724 /* Generate RTL for the variable DECL to represent its location. */
15726 static rtx
15727 rtl_for_decl_location (tree decl)
15729 rtx rtl;
15731 /* Here we have to decide where we are going to say the parameter "lives"
15732 (as far as the debugger is concerned). We only have a couple of
15733 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
15735 DECL_RTL normally indicates where the parameter lives during most of the
15736 activation of the function. If optimization is enabled however, this
15737 could be either NULL or else a pseudo-reg. Both of those cases indicate
15738 that the parameter doesn't really live anywhere (as far as the code
15739 generation parts of GCC are concerned) during most of the function's
15740 activation. That will happen (for example) if the parameter is never
15741 referenced within the function.
15743 We could just generate a location descriptor here for all non-NULL
15744 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
15745 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
15746 where DECL_RTL is NULL or is a pseudo-reg.
15748 Note however that we can only get away with using DECL_INCOMING_RTL as
15749 a backup substitute for DECL_RTL in certain limited cases. In cases
15750 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
15751 we can be sure that the parameter was passed using the same type as it is
15752 declared to have within the function, and that its DECL_INCOMING_RTL
15753 points us to a place where a value of that type is passed.
15755 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
15756 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
15757 because in these cases DECL_INCOMING_RTL points us to a value of some
15758 type which is *different* from the type of the parameter itself. Thus,
15759 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
15760 such cases, the debugger would end up (for example) trying to fetch a
15761 `float' from a place which actually contains the first part of a
15762 `double'. That would lead to really incorrect and confusing
15763 output at debug-time.
15765 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
15766 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
15767 are a couple of exceptions however. On little-endian machines we can
15768 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
15769 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
15770 an integral type that is smaller than TREE_TYPE (decl). These cases arise
15771 when (on a little-endian machine) a non-prototyped function has a
15772 parameter declared to be of type `short' or `char'. In such cases,
15773 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
15774 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
15775 passed `int' value. If the debugger then uses that address to fetch
15776 a `short' or a `char' (on a little-endian machine) the result will be
15777 the correct data, so we allow for such exceptional cases below.
15779 Note that our goal here is to describe the place where the given formal
15780 parameter lives during most of the function's activation (i.e. between the
15781 end of the prologue and the start of the epilogue). We'll do that as best
15782 as we can. Note however that if the given formal parameter is modified
15783 sometime during the execution of the function, then a stack backtrace (at
15784 debug-time) will show the function as having been called with the *new*
15785 value rather than the value which was originally passed in. This happens
15786 rarely enough that it is not a major problem, but it *is* a problem, and
15787 I'd like to fix it.
15789 A future version of dwarf2out.c may generate two additional attributes for
15790 any given DW_TAG_formal_parameter DIE which will describe the "passed
15791 type" and the "passed location" for the given formal parameter in addition
15792 to the attributes we now generate to indicate the "declared type" and the
15793 "active location" for each parameter. This additional set of attributes
15794 could be used by debuggers for stack backtraces. Separately, note that
15795 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
15796 This happens (for example) for inlined-instances of inline function formal
15797 parameters which are never referenced. This really shouldn't be
15798 happening. All PARM_DECL nodes should get valid non-NULL
15799 DECL_INCOMING_RTL values. FIXME. */
15801 /* Use DECL_RTL as the "location" unless we find something better. */
15802 rtl = DECL_RTL_IF_SET (decl);
15804 /* When generating abstract instances, ignore everything except
15805 constants, symbols living in memory, and symbols living in
15806 fixed registers. */
15807 if (! reload_completed)
15809 if (rtl
15810 && (CONSTANT_P (rtl)
15811 || (MEM_P (rtl)
15812 && CONSTANT_P (XEXP (rtl, 0)))
15813 || (REG_P (rtl)
15814 && TREE_CODE (decl) == VAR_DECL
15815 && TREE_STATIC (decl))))
15817 rtl = targetm.delegitimize_address (rtl);
15818 return rtl;
15820 rtl = NULL_RTX;
15822 else if (TREE_CODE (decl) == PARM_DECL)
15824 if (rtl == NULL_RTX
15825 || is_pseudo_reg (rtl)
15826 || (MEM_P (rtl)
15827 && is_pseudo_reg (XEXP (rtl, 0))
15828 && DECL_INCOMING_RTL (decl)
15829 && MEM_P (DECL_INCOMING_RTL (decl))
15830 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
15832 tree declared_type = TREE_TYPE (decl);
15833 tree passed_type = DECL_ARG_TYPE (decl);
15834 machine_mode dmode = TYPE_MODE (declared_type);
15835 machine_mode pmode = TYPE_MODE (passed_type);
15837 /* This decl represents a formal parameter which was optimized out.
15838 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
15839 all cases where (rtl == NULL_RTX) just below. */
15840 if (dmode == pmode)
15841 rtl = DECL_INCOMING_RTL (decl);
15842 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
15843 && SCALAR_INT_MODE_P (dmode)
15844 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
15845 && DECL_INCOMING_RTL (decl))
15847 rtx inc = DECL_INCOMING_RTL (decl);
15848 if (REG_P (inc))
15849 rtl = inc;
15850 else if (MEM_P (inc))
15852 if (BYTES_BIG_ENDIAN)
15853 rtl = adjust_address_nv (inc, dmode,
15854 GET_MODE_SIZE (pmode)
15855 - GET_MODE_SIZE (dmode));
15856 else
15857 rtl = inc;
15862 /* If the parm was passed in registers, but lives on the stack, then
15863 make a big endian correction if the mode of the type of the
15864 parameter is not the same as the mode of the rtl. */
15865 /* ??? This is the same series of checks that are made in dbxout.c before
15866 we reach the big endian correction code there. It isn't clear if all
15867 of these checks are necessary here, but keeping them all is the safe
15868 thing to do. */
15869 else if (MEM_P (rtl)
15870 && XEXP (rtl, 0) != const0_rtx
15871 && ! CONSTANT_P (XEXP (rtl, 0))
15872 /* Not passed in memory. */
15873 && !MEM_P (DECL_INCOMING_RTL (decl))
15874 /* Not passed by invisible reference. */
15875 && (!REG_P (XEXP (rtl, 0))
15876 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
15877 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
15878 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
15879 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
15880 #endif
15882 /* Big endian correction check. */
15883 && BYTES_BIG_ENDIAN
15884 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
15885 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
15886 < UNITS_PER_WORD))
15888 machine_mode addr_mode = get_address_mode (rtl);
15889 int offset = (UNITS_PER_WORD
15890 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
15892 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15893 plus_constant (addr_mode, XEXP (rtl, 0), offset));
15896 else if (TREE_CODE (decl) == VAR_DECL
15897 && rtl
15898 && MEM_P (rtl)
15899 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
15900 && BYTES_BIG_ENDIAN)
15902 machine_mode addr_mode = get_address_mode (rtl);
15903 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
15904 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
15906 /* If a variable is declared "register" yet is smaller than
15907 a register, then if we store the variable to memory, it
15908 looks like we're storing a register-sized value, when in
15909 fact we are not. We need to adjust the offset of the
15910 storage location to reflect the actual value's bytes,
15911 else gdb will not be able to display it. */
15912 if (rsize > dsize)
15913 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15914 plus_constant (addr_mode, XEXP (rtl, 0),
15915 rsize - dsize));
15918 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
15919 and will have been substituted directly into all expressions that use it.
15920 C does not have such a concept, but C++ and other languages do. */
15921 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
15922 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
15924 if (rtl)
15925 rtl = targetm.delegitimize_address (rtl);
15927 /* If we don't look past the constant pool, we risk emitting a
15928 reference to a constant pool entry that isn't referenced from
15929 code, and thus is not emitted. */
15930 if (rtl)
15931 rtl = avoid_constant_pool_reference (rtl);
15933 /* Try harder to get a rtl. If this symbol ends up not being emitted
15934 in the current CU, resolve_addr will remove the expression referencing
15935 it. */
15936 if (rtl == NULL_RTX
15937 && TREE_CODE (decl) == VAR_DECL
15938 && !DECL_EXTERNAL (decl)
15939 && TREE_STATIC (decl)
15940 && DECL_NAME (decl)
15941 && !DECL_HARD_REGISTER (decl)
15942 && DECL_MODE (decl) != VOIDmode)
15944 rtl = make_decl_rtl_for_debug (decl);
15945 if (!MEM_P (rtl)
15946 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
15947 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
15948 rtl = NULL_RTX;
15951 return rtl;
15954 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
15955 returned. If so, the decl for the COMMON block is returned, and the
15956 value is the offset into the common block for the symbol. */
15958 static tree
15959 fortran_common (tree decl, HOST_WIDE_INT *value)
15961 tree val_expr, cvar;
15962 machine_mode mode;
15963 HOST_WIDE_INT bitsize, bitpos;
15964 tree offset;
15965 int unsignedp, volatilep = 0;
15967 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
15968 it does not have a value (the offset into the common area), or if it
15969 is thread local (as opposed to global) then it isn't common, and shouldn't
15970 be handled as such. */
15971 if (TREE_CODE (decl) != VAR_DECL
15972 || !TREE_STATIC (decl)
15973 || !DECL_HAS_VALUE_EXPR_P (decl)
15974 || !is_fortran ())
15975 return NULL_TREE;
15977 val_expr = DECL_VALUE_EXPR (decl);
15978 if (TREE_CODE (val_expr) != COMPONENT_REF)
15979 return NULL_TREE;
15981 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
15982 &mode, &unsignedp, &volatilep, true);
15984 if (cvar == NULL_TREE
15985 || TREE_CODE (cvar) != VAR_DECL
15986 || DECL_ARTIFICIAL (cvar)
15987 || !TREE_PUBLIC (cvar))
15988 return NULL_TREE;
15990 *value = 0;
15991 if (offset != NULL)
15993 if (!tree_fits_shwi_p (offset))
15994 return NULL_TREE;
15995 *value = tree_to_shwi (offset);
15997 if (bitpos != 0)
15998 *value += bitpos / BITS_PER_UNIT;
16000 return cvar;
16003 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
16004 data attribute for a variable or a parameter. We generate the
16005 DW_AT_const_value attribute only in those cases where the given variable
16006 or parameter does not have a true "location" either in memory or in a
16007 register. This can happen (for example) when a constant is passed as an
16008 actual argument in a call to an inline function. (It's possible that
16009 these things can crop up in other ways also.) Note that one type of
16010 constant value which can be passed into an inlined function is a constant
16011 pointer. This can happen for example if an actual argument in an inlined
16012 function call evaluates to a compile-time constant address.
16014 CACHE_P is true if it is worth caching the location list for DECL,
16015 so that future calls can reuse it rather than regenerate it from scratch.
16016 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
16017 since we will need to refer to them each time the function is inlined. */
16019 static bool
16020 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
16021 enum dwarf_attribute attr)
16023 rtx rtl;
16024 dw_loc_list_ref list;
16025 var_loc_list *loc_list;
16026 cached_dw_loc_list *cache;
16028 if (TREE_CODE (decl) == ERROR_MARK)
16029 return false;
16031 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
16032 || TREE_CODE (decl) == RESULT_DECL);
16034 /* Try to get some constant RTL for this decl, and use that as the value of
16035 the location. */
16037 rtl = rtl_for_decl_location (decl);
16038 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16039 && add_const_value_attribute (die, rtl))
16040 return true;
16042 /* See if we have single element location list that is equivalent to
16043 a constant value. That way we are better to use add_const_value_attribute
16044 rather than expanding constant value equivalent. */
16045 loc_list = lookup_decl_loc (decl);
16046 if (loc_list
16047 && loc_list->first
16048 && loc_list->first->next == NULL
16049 && NOTE_P (loc_list->first->loc)
16050 && NOTE_VAR_LOCATION (loc_list->first->loc)
16051 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
16053 struct var_loc_node *node;
16055 node = loc_list->first;
16056 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
16057 if (GET_CODE (rtl) == EXPR_LIST)
16058 rtl = XEXP (rtl, 0);
16059 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16060 && add_const_value_attribute (die, rtl))
16061 return true;
16063 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
16064 list several times. See if we've already cached the contents. */
16065 list = NULL;
16066 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
16067 cache_p = false;
16068 if (cache_p)
16070 cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
16071 if (cache)
16072 list = cache->loc_list;
16074 if (list == NULL)
16076 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
16077 NULL);
16078 /* It is usually worth caching this result if the decl is from
16079 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
16080 if (cache_p && list && list->dw_loc_next)
16082 cached_dw_loc_list **slot
16083 = cached_dw_loc_list_table->find_slot_with_hash (decl,
16084 DECL_UID (decl),
16085 INSERT);
16086 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
16087 cache->decl_id = DECL_UID (decl);
16088 cache->loc_list = list;
16089 *slot = cache;
16092 if (list)
16094 add_AT_location_description (die, attr, list);
16095 return true;
16097 /* None of that worked, so it must not really have a location;
16098 try adding a constant value attribute from the DECL_INITIAL. */
16099 return tree_add_const_value_attribute_for_decl (die, decl);
16102 /* Add VARIABLE and DIE into deferred locations list. */
16104 static void
16105 defer_location (tree variable, dw_die_ref die)
16107 deferred_locations entry;
16108 entry.variable = variable;
16109 entry.die = die;
16110 vec_safe_push (deferred_locations_list, entry);
16113 /* Helper function for tree_add_const_value_attribute. Natively encode
16114 initializer INIT into an array. Return true if successful. */
16116 static bool
16117 native_encode_initializer (tree init, unsigned char *array, int size)
16119 tree type;
16121 if (init == NULL_TREE)
16122 return false;
16124 STRIP_NOPS (init);
16125 switch (TREE_CODE (init))
16127 case STRING_CST:
16128 type = TREE_TYPE (init);
16129 if (TREE_CODE (type) == ARRAY_TYPE)
16131 tree enttype = TREE_TYPE (type);
16132 machine_mode mode = TYPE_MODE (enttype);
16134 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
16135 return false;
16136 if (int_size_in_bytes (type) != size)
16137 return false;
16138 if (size > TREE_STRING_LENGTH (init))
16140 memcpy (array, TREE_STRING_POINTER (init),
16141 TREE_STRING_LENGTH (init));
16142 memset (array + TREE_STRING_LENGTH (init),
16143 '\0', size - TREE_STRING_LENGTH (init));
16145 else
16146 memcpy (array, TREE_STRING_POINTER (init), size);
16147 return true;
16149 return false;
16150 case CONSTRUCTOR:
16151 type = TREE_TYPE (init);
16152 if (int_size_in_bytes (type) != size)
16153 return false;
16154 if (TREE_CODE (type) == ARRAY_TYPE)
16156 HOST_WIDE_INT min_index;
16157 unsigned HOST_WIDE_INT cnt;
16158 int curpos = 0, fieldsize;
16159 constructor_elt *ce;
16161 if (TYPE_DOMAIN (type) == NULL_TREE
16162 || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
16163 return false;
16165 fieldsize = int_size_in_bytes (TREE_TYPE (type));
16166 if (fieldsize <= 0)
16167 return false;
16169 min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
16170 memset (array, '\0', size);
16171 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
16173 tree val = ce->value;
16174 tree index = ce->index;
16175 int pos = curpos;
16176 if (index && TREE_CODE (index) == RANGE_EXPR)
16177 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
16178 * fieldsize;
16179 else if (index)
16180 pos = (tree_to_shwi (index) - min_index) * fieldsize;
16182 if (val)
16184 STRIP_NOPS (val);
16185 if (!native_encode_initializer (val, array + pos, fieldsize))
16186 return false;
16188 curpos = pos + fieldsize;
16189 if (index && TREE_CODE (index) == RANGE_EXPR)
16191 int count = tree_to_shwi (TREE_OPERAND (index, 1))
16192 - tree_to_shwi (TREE_OPERAND (index, 0));
16193 while (count-- > 0)
16195 if (val)
16196 memcpy (array + curpos, array + pos, fieldsize);
16197 curpos += fieldsize;
16200 gcc_assert (curpos <= size);
16202 return true;
16204 else if (TREE_CODE (type) == RECORD_TYPE
16205 || TREE_CODE (type) == UNION_TYPE)
16207 tree field = NULL_TREE;
16208 unsigned HOST_WIDE_INT cnt;
16209 constructor_elt *ce;
16211 if (int_size_in_bytes (type) != size)
16212 return false;
16214 if (TREE_CODE (type) == RECORD_TYPE)
16215 field = TYPE_FIELDS (type);
16217 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
16219 tree val = ce->value;
16220 int pos, fieldsize;
16222 if (ce->index != 0)
16223 field = ce->index;
16225 if (val)
16226 STRIP_NOPS (val);
16228 if (field == NULL_TREE || DECL_BIT_FIELD (field))
16229 return false;
16231 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
16232 && TYPE_DOMAIN (TREE_TYPE (field))
16233 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
16234 return false;
16235 else if (DECL_SIZE_UNIT (field) == NULL_TREE
16236 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
16237 return false;
16238 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
16239 pos = int_byte_position (field);
16240 gcc_assert (pos + fieldsize <= size);
16241 if (val
16242 && !native_encode_initializer (val, array + pos, fieldsize))
16243 return false;
16245 return true;
16247 return false;
16248 case VIEW_CONVERT_EXPR:
16249 case NON_LVALUE_EXPR:
16250 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
16251 default:
16252 return native_encode_expr (init, array, size) == size;
16256 /* Attach a DW_AT_const_value attribute to DIE. The value of the
16257 attribute is the const value T. */
16259 static bool
16260 tree_add_const_value_attribute (dw_die_ref die, tree t)
16262 tree init;
16263 tree type = TREE_TYPE (t);
16264 rtx rtl;
16266 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
16267 return false;
16269 init = t;
16270 gcc_assert (!DECL_P (init));
16272 rtl = rtl_for_decl_init (init, type);
16273 if (rtl)
16274 return add_const_value_attribute (die, rtl);
16275 /* If the host and target are sane, try harder. */
16276 else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
16277 && initializer_constant_valid_p (init, type))
16279 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
16280 if (size > 0 && (int) size == size)
16282 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
16284 if (native_encode_initializer (init, array, size))
16286 add_AT_vec (die, DW_AT_const_value, size, 1, array);
16287 return true;
16289 ggc_free (array);
16292 return false;
16295 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
16296 attribute is the const value of T, where T is an integral constant
16297 variable with static storage duration
16298 (so it can't be a PARM_DECL or a RESULT_DECL). */
16300 static bool
16301 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
16304 if (!decl
16305 || (TREE_CODE (decl) != VAR_DECL
16306 && TREE_CODE (decl) != CONST_DECL)
16307 || (TREE_CODE (decl) == VAR_DECL
16308 && !TREE_STATIC (decl)))
16309 return false;
16311 if (TREE_READONLY (decl)
16312 && ! TREE_THIS_VOLATILE (decl)
16313 && DECL_INITIAL (decl))
16314 /* OK */;
16315 else
16316 return false;
16318 /* Don't add DW_AT_const_value if abstract origin already has one. */
16319 if (get_AT (var_die, DW_AT_const_value))
16320 return false;
16322 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
16325 /* Convert the CFI instructions for the current function into a
16326 location list. This is used for DW_AT_frame_base when we targeting
16327 a dwarf2 consumer that does not support the dwarf3
16328 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
16329 expressions. */
16331 static dw_loc_list_ref
16332 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
16334 int ix;
16335 dw_fde_ref fde;
16336 dw_loc_list_ref list, *list_tail;
16337 dw_cfi_ref cfi;
16338 dw_cfa_location last_cfa, next_cfa;
16339 const char *start_label, *last_label, *section;
16340 dw_cfa_location remember;
16342 fde = cfun->fde;
16343 gcc_assert (fde != NULL);
16345 section = secname_for_decl (current_function_decl);
16346 list_tail = &list;
16347 list = NULL;
16349 memset (&next_cfa, 0, sizeof (next_cfa));
16350 next_cfa.reg = INVALID_REGNUM;
16351 remember = next_cfa;
16353 start_label = fde->dw_fde_begin;
16355 /* ??? Bald assumption that the CIE opcode list does not contain
16356 advance opcodes. */
16357 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
16358 lookup_cfa_1 (cfi, &next_cfa, &remember);
16360 last_cfa = next_cfa;
16361 last_label = start_label;
16363 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
16365 /* If the first partition contained no CFI adjustments, the
16366 CIE opcodes apply to the whole first partition. */
16367 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16368 fde->dw_fde_begin, fde->dw_fde_end, section);
16369 list_tail =&(*list_tail)->dw_loc_next;
16370 start_label = last_label = fde->dw_fde_second_begin;
16373 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
16375 switch (cfi->dw_cfi_opc)
16377 case DW_CFA_set_loc:
16378 case DW_CFA_advance_loc1:
16379 case DW_CFA_advance_loc2:
16380 case DW_CFA_advance_loc4:
16381 if (!cfa_equal_p (&last_cfa, &next_cfa))
16383 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16384 start_label, last_label, section);
16386 list_tail = &(*list_tail)->dw_loc_next;
16387 last_cfa = next_cfa;
16388 start_label = last_label;
16390 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
16391 break;
16393 case DW_CFA_advance_loc:
16394 /* The encoding is complex enough that we should never emit this. */
16395 gcc_unreachable ();
16397 default:
16398 lookup_cfa_1 (cfi, &next_cfa, &remember);
16399 break;
16401 if (ix + 1 == fde->dw_fde_switch_cfi_index)
16403 if (!cfa_equal_p (&last_cfa, &next_cfa))
16405 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16406 start_label, last_label, section);
16408 list_tail = &(*list_tail)->dw_loc_next;
16409 last_cfa = next_cfa;
16410 start_label = last_label;
16412 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16413 start_label, fde->dw_fde_end, section);
16414 list_tail = &(*list_tail)->dw_loc_next;
16415 start_label = last_label = fde->dw_fde_second_begin;
16419 if (!cfa_equal_p (&last_cfa, &next_cfa))
16421 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16422 start_label, last_label, section);
16423 list_tail = &(*list_tail)->dw_loc_next;
16424 start_label = last_label;
16427 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
16428 start_label,
16429 fde->dw_fde_second_begin
16430 ? fde->dw_fde_second_end : fde->dw_fde_end,
16431 section);
16433 if (list && list->dw_loc_next)
16434 gen_llsym (list);
16436 return list;
16439 /* Compute a displacement from the "steady-state frame pointer" to the
16440 frame base (often the same as the CFA), and store it in
16441 frame_pointer_fb_offset. OFFSET is added to the displacement
16442 before the latter is negated. */
16444 static void
16445 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
16447 rtx reg, elim;
16449 #ifdef FRAME_POINTER_CFA_OFFSET
16450 reg = frame_pointer_rtx;
16451 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
16452 #else
16453 reg = arg_pointer_rtx;
16454 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
16455 #endif
16457 elim = (ira_use_lra_p
16458 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
16459 : eliminate_regs (reg, VOIDmode, NULL_RTX));
16460 if (GET_CODE (elim) == PLUS)
16462 offset += INTVAL (XEXP (elim, 1));
16463 elim = XEXP (elim, 0);
16466 frame_pointer_fb_offset = -offset;
16468 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
16469 in which to eliminate. This is because it's stack pointer isn't
16470 directly accessible as a register within the ISA. To work around
16471 this, assume that while we cannot provide a proper value for
16472 frame_pointer_fb_offset, we won't need one either. */
16473 frame_pointer_fb_offset_valid
16474 = ((SUPPORTS_STACK_ALIGNMENT
16475 && (elim == hard_frame_pointer_rtx
16476 || elim == stack_pointer_rtx))
16477 || elim == (frame_pointer_needed
16478 ? hard_frame_pointer_rtx
16479 : stack_pointer_rtx));
16482 /* Generate a DW_AT_name attribute given some string value to be included as
16483 the value of the attribute. */
16485 static void
16486 add_name_attribute (dw_die_ref die, const char *name_string)
16488 if (name_string != NULL && *name_string != 0)
16490 if (demangle_name_func)
16491 name_string = (*demangle_name_func) (name_string);
16493 add_AT_string (die, DW_AT_name, name_string);
16497 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
16498 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
16499 of TYPE accordingly.
16501 ??? This is a temporary measure until after we're able to generate
16502 regular DWARF for the complex Ada type system. */
16504 static void
16505 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
16506 dw_die_ref context_die)
16508 tree dtype;
16509 dw_die_ref dtype_die;
16511 if (!lang_hooks.types.descriptive_type)
16512 return;
16514 dtype = lang_hooks.types.descriptive_type (type);
16515 if (!dtype)
16516 return;
16518 dtype_die = lookup_type_die (dtype);
16519 if (!dtype_die)
16521 gen_type_die (dtype, context_die);
16522 dtype_die = lookup_type_die (dtype);
16523 gcc_assert (dtype_die);
16526 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
16529 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
16531 static const char *
16532 comp_dir_string (void)
16534 const char *wd;
16535 char *wd1;
16536 static const char *cached_wd = NULL;
16538 if (cached_wd != NULL)
16539 return cached_wd;
16541 wd = get_src_pwd ();
16542 if (wd == NULL)
16543 return NULL;
16545 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
16547 int wdlen;
16549 wdlen = strlen (wd);
16550 wd1 = ggc_vec_alloc<char> (wdlen + 2);
16551 strcpy (wd1, wd);
16552 wd1 [wdlen] = DIR_SEPARATOR;
16553 wd1 [wdlen + 1] = 0;
16554 wd = wd1;
16557 cached_wd = remap_debug_filename (wd);
16558 return cached_wd;
16561 /* Generate a DW_AT_comp_dir attribute for DIE. */
16563 static void
16564 add_comp_dir_attribute (dw_die_ref die)
16566 const char * wd = comp_dir_string ();
16567 if (wd != NULL)
16568 add_AT_string (die, DW_AT_comp_dir, wd);
16571 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
16572 pointer computation, ...), output a representation for that bound according
16573 to the accepted FORMS (see enum dw_scalar_form) and add it to DIE. See
16574 loc_list_from_tree for the meaning of CONTEXT. */
16576 static void
16577 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
16578 int forms, const struct loc_descr_context *context)
16580 dw_die_ref ctx, decl_die;
16581 dw_loc_list_ref list;
16583 bool strip_conversions = true;
16585 while (strip_conversions)
16586 switch (TREE_CODE (value))
16588 case ERROR_MARK:
16589 case SAVE_EXPR:
16590 return;
16592 CASE_CONVERT:
16593 case VIEW_CONVERT_EXPR:
16594 value = TREE_OPERAND (value, 0);
16595 break;
16597 default:
16598 strip_conversions = false;
16599 break;
16602 /* If possible and permitted, output the attribute as a constant. */
16603 if ((forms & dw_scalar_form_constant) != 0
16604 && TREE_CODE (value) == INTEGER_CST)
16606 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
16608 /* If HOST_WIDE_INT is big enough then represent the bound as
16609 a constant value. We need to choose a form based on
16610 whether the type is signed or unsigned. We cannot just
16611 call add_AT_unsigned if the value itself is positive
16612 (add_AT_unsigned might add the unsigned value encoded as
16613 DW_FORM_data[1248]). Some DWARF consumers will lookup the
16614 bounds type and then sign extend any unsigned values found
16615 for signed types. This is needed only for
16616 DW_AT_{lower,upper}_bound, since for most other attributes,
16617 consumers will treat DW_FORM_data[1248] as unsigned values,
16618 regardless of the underlying type. */
16619 if (prec <= HOST_BITS_PER_WIDE_INT
16620 || tree_fits_uhwi_p (value))
16622 if (TYPE_UNSIGNED (TREE_TYPE (value)))
16623 add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
16624 else
16625 add_AT_int (die, attr, TREE_INT_CST_LOW (value));
16627 else
16628 /* Otherwise represent the bound as an unsigned value with
16629 the precision of its type. The precision and signedness
16630 of the type will be necessary to re-interpret it
16631 unambiguously. */
16632 add_AT_wide (die, attr, value);
16633 return;
16636 /* Otherwise, if it's possible and permitted too, output a reference to
16637 another DIE. */
16638 if ((forms & dw_scalar_form_reference) != 0)
16640 tree decl = NULL_TREE;
16642 /* Some type attributes reference an outer type. For instance, the upper
16643 bound of an array may reference an embedding record (this happens in
16644 Ada). */
16645 if (TREE_CODE (value) == COMPONENT_REF
16646 && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
16647 && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
16648 decl = TREE_OPERAND (value, 1);
16650 else if (TREE_CODE (value) == VAR_DECL
16651 || TREE_CODE (value) == PARM_DECL
16652 || TREE_CODE (value) == RESULT_DECL)
16653 decl = value;
16655 if (decl != NULL_TREE)
16657 dw_die_ref decl_die = lookup_decl_die (decl);
16659 /* ??? Can this happen, or should the variable have been bound
16660 first? Probably it can, since I imagine that we try to create
16661 the types of parameters in the order in which they exist in
16662 the list, and won't have created a forward reference to a
16663 later parameter. */
16664 if (decl_die != NULL)
16666 add_AT_die_ref (die, attr, decl_die);
16667 return;
16672 /* Last chance: try to create a stack operation procedure to evaluate the
16673 value. Do nothing if even that is not possible or permitted. */
16674 if ((forms & dw_scalar_form_exprloc) == 0)
16675 return;
16677 list = loc_list_from_tree (value, 2, context);
16678 if (list == NULL || single_element_loc_list_p (list))
16680 /* If this attribute is not a reference nor constant, it is
16681 a DWARF expression rather than location description. For that
16682 loc_list_from_tree (value, 0, &context) is needed. */
16683 dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
16684 if (list2 && single_element_loc_list_p (list2))
16686 add_AT_loc (die, attr, list2->expr);
16687 return;
16691 /* If that failed to give a single element location list, fall back to
16692 outputting this as a reference... still if permitted. */
16693 if (list == NULL || (forms & dw_scalar_form_reference) == 0)
16694 return;
16696 if (current_function_decl == 0)
16697 ctx = comp_unit_die ();
16698 else
16699 ctx = lookup_decl_die (current_function_decl);
16701 decl_die = new_die (DW_TAG_variable, ctx, value);
16702 add_AT_flag (decl_die, DW_AT_artificial, 1);
16703 add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, ctx);
16704 add_AT_location_description (decl_die, DW_AT_location, list);
16705 add_AT_die_ref (die, attr, decl_die);
16708 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
16709 default. */
16711 static int
16712 lower_bound_default (void)
16714 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
16716 case DW_LANG_C:
16717 case DW_LANG_C89:
16718 case DW_LANG_C99:
16719 case DW_LANG_C11:
16720 case DW_LANG_C_plus_plus:
16721 case DW_LANG_C_plus_plus_11:
16722 case DW_LANG_C_plus_plus_14:
16723 case DW_LANG_ObjC:
16724 case DW_LANG_ObjC_plus_plus:
16725 case DW_LANG_Java:
16726 return 0;
16727 case DW_LANG_Fortran77:
16728 case DW_LANG_Fortran90:
16729 case DW_LANG_Fortran95:
16730 case DW_LANG_Fortran03:
16731 case DW_LANG_Fortran08:
16732 return 1;
16733 case DW_LANG_UPC:
16734 case DW_LANG_D:
16735 case DW_LANG_Python:
16736 return dwarf_version >= 4 ? 0 : -1;
16737 case DW_LANG_Ada95:
16738 case DW_LANG_Ada83:
16739 case DW_LANG_Cobol74:
16740 case DW_LANG_Cobol85:
16741 case DW_LANG_Pascal83:
16742 case DW_LANG_Modula2:
16743 case DW_LANG_PLI:
16744 return dwarf_version >= 4 ? 1 : -1;
16745 default:
16746 return -1;
16750 /* Given a tree node describing an array bound (either lower or upper) output
16751 a representation for that bound. */
16753 static void
16754 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
16755 tree bound, const struct loc_descr_context *context)
16757 int dflt;
16759 while (1)
16760 switch (TREE_CODE (bound))
16762 /* Strip all conversions. */
16763 CASE_CONVERT:
16764 case VIEW_CONVERT_EXPR:
16765 bound = TREE_OPERAND (bound, 0);
16766 break;
16768 /* All fixed-bounds are represented by INTEGER_CST nodes. Lower bounds
16769 are even omitted when they are the default. */
16770 case INTEGER_CST:
16771 /* If the value for this bound is the default one, we can even omit the
16772 attribute. */
16773 if (bound_attr == DW_AT_lower_bound
16774 && tree_fits_shwi_p (bound)
16775 && (dflt = lower_bound_default ()) != -1
16776 && tree_to_shwi (bound) == dflt)
16777 return;
16779 /* FALLTHRU */
16781 default:
16782 add_scalar_info (subrange_die, bound_attr, bound,
16783 dw_scalar_form_constant
16784 | dw_scalar_form_exprloc
16785 | dw_scalar_form_reference,
16786 context);
16787 return;
16791 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
16792 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
16793 Note that the block of subscript information for an array type also
16794 includes information about the element type of the given array type. */
16796 static void
16797 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
16799 unsigned dimension_number;
16800 tree lower, upper;
16801 dw_die_ref subrange_die;
16803 for (dimension_number = 0;
16804 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
16805 type = TREE_TYPE (type), dimension_number++)
16807 tree domain = TYPE_DOMAIN (type);
16809 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
16810 break;
16812 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
16813 and (in GNU C only) variable bounds. Handle all three forms
16814 here. */
16815 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
16816 if (domain)
16818 /* We have an array type with specified bounds. */
16819 lower = TYPE_MIN_VALUE (domain);
16820 upper = TYPE_MAX_VALUE (domain);
16822 /* Define the index type. */
16823 if (TREE_TYPE (domain))
16825 /* ??? This is probably an Ada unnamed subrange type. Ignore the
16826 TREE_TYPE field. We can't emit debug info for this
16827 because it is an unnamed integral type. */
16828 if (TREE_CODE (domain) == INTEGER_TYPE
16829 && TYPE_NAME (domain) == NULL_TREE
16830 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
16831 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
16833 else
16834 add_type_attribute (subrange_die, TREE_TYPE (domain),
16835 TYPE_UNQUALIFIED, type_die);
16838 /* ??? If upper is NULL, the array has unspecified length,
16839 but it does have a lower bound. This happens with Fortran
16840 dimension arr(N:*)
16841 Since the debugger is definitely going to need to know N
16842 to produce useful results, go ahead and output the lower
16843 bound solo, and hope the debugger can cope. */
16845 add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
16846 if (upper)
16847 add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
16850 /* Otherwise we have an array type with an unspecified length. The
16851 DWARF-2 spec does not say how to handle this; let's just leave out the
16852 bounds. */
16856 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
16858 static void
16859 add_byte_size_attribute (dw_die_ref die, tree tree_node)
16861 dw_die_ref decl_die;
16862 HOST_WIDE_INT size;
16864 switch (TREE_CODE (tree_node))
16866 case ERROR_MARK:
16867 size = 0;
16868 break;
16869 case ENUMERAL_TYPE:
16870 case RECORD_TYPE:
16871 case UNION_TYPE:
16872 case QUAL_UNION_TYPE:
16873 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
16874 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
16876 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
16877 return;
16879 size = int_size_in_bytes (tree_node);
16880 break;
16881 case FIELD_DECL:
16882 /* For a data member of a struct or union, the DW_AT_byte_size is
16883 generally given as the number of bytes normally allocated for an
16884 object of the *declared* type of the member itself. This is true
16885 even for bit-fields. */
16886 size = int_size_in_bytes (field_type (tree_node));
16887 break;
16888 default:
16889 gcc_unreachable ();
16892 /* Note that `size' might be -1 when we get to this point. If it is, that
16893 indicates that the byte size of the entity in question is variable. We
16894 have no good way of expressing this fact in Dwarf at the present time,
16895 when location description was not used by the caller code instead. */
16896 if (size >= 0)
16897 add_AT_unsigned (die, DW_AT_byte_size, size);
16900 /* For a FIELD_DECL node which represents a bit-field, output an attribute
16901 which specifies the distance in bits from the highest order bit of the
16902 "containing object" for the bit-field to the highest order bit of the
16903 bit-field itself.
16905 For any given bit-field, the "containing object" is a hypothetical object
16906 (of some integral or enum type) within which the given bit-field lives. The
16907 type of this hypothetical "containing object" is always the same as the
16908 declared type of the individual bit-field itself. The determination of the
16909 exact location of the "containing object" for a bit-field is rather
16910 complicated. It's handled by the `field_byte_offset' function (above).
16912 Note that it is the size (in bytes) of the hypothetical "containing object"
16913 which will be given in the DW_AT_byte_size attribute for this bit-field.
16914 (See `byte_size_attribute' above). */
16916 static inline void
16917 add_bit_offset_attribute (dw_die_ref die, tree decl)
16919 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
16920 tree type = DECL_BIT_FIELD_TYPE (decl);
16921 HOST_WIDE_INT bitpos_int;
16922 HOST_WIDE_INT highest_order_object_bit_offset;
16923 HOST_WIDE_INT highest_order_field_bit_offset;
16924 HOST_WIDE_INT bit_offset;
16926 /* Must be a field and a bit field. */
16927 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
16929 /* We can't yet handle bit-fields whose offsets are variable, so if we
16930 encounter such things, just return without generating any attribute
16931 whatsoever. Likewise for variable or too large size. */
16932 if (! tree_fits_shwi_p (bit_position (decl))
16933 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
16934 return;
16936 bitpos_int = int_bit_position (decl);
16938 /* Note that the bit offset is always the distance (in bits) from the
16939 highest-order bit of the "containing object" to the highest-order bit of
16940 the bit-field itself. Since the "high-order end" of any object or field
16941 is different on big-endian and little-endian machines, the computation
16942 below must take account of these differences. */
16943 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
16944 highest_order_field_bit_offset = bitpos_int;
16946 if (! BYTES_BIG_ENDIAN)
16948 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
16949 highest_order_object_bit_offset += simple_type_size_in_bits (type);
16952 bit_offset
16953 = (! BYTES_BIG_ENDIAN
16954 ? highest_order_object_bit_offset - highest_order_field_bit_offset
16955 : highest_order_field_bit_offset - highest_order_object_bit_offset);
16957 if (bit_offset < 0)
16958 add_AT_int (die, DW_AT_bit_offset, bit_offset);
16959 else
16960 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
16963 /* For a FIELD_DECL node which represents a bit field, output an attribute
16964 which specifies the length in bits of the given field. */
16966 static inline void
16967 add_bit_size_attribute (dw_die_ref die, tree decl)
16969 /* Must be a field and a bit field. */
16970 gcc_assert (TREE_CODE (decl) == FIELD_DECL
16971 && DECL_BIT_FIELD_TYPE (decl));
16973 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
16974 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
16977 /* If the compiled language is ANSI C, then add a 'prototyped'
16978 attribute, if arg types are given for the parameters of a function. */
16980 static inline void
16981 add_prototyped_attribute (dw_die_ref die, tree func_type)
16983 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
16985 case DW_LANG_C:
16986 case DW_LANG_C89:
16987 case DW_LANG_C99:
16988 case DW_LANG_C11:
16989 case DW_LANG_ObjC:
16990 if (prototype_p (func_type))
16991 add_AT_flag (die, DW_AT_prototyped, 1);
16992 break;
16993 default:
16994 break;
16998 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
16999 by looking in either the type declaration or object declaration
17000 equate table. */
17002 static inline dw_die_ref
17003 add_abstract_origin_attribute (dw_die_ref die, tree origin)
17005 dw_die_ref origin_die = NULL;
17007 if (TREE_CODE (origin) != FUNCTION_DECL)
17009 /* We may have gotten separated from the block for the inlined
17010 function, if we're in an exception handler or some such; make
17011 sure that the abstract function has been written out.
17013 Doing this for nested functions is wrong, however; functions are
17014 distinct units, and our context might not even be inline. */
17015 tree fn = origin;
17017 if (TYPE_P (fn))
17018 fn = TYPE_STUB_DECL (fn);
17020 fn = decl_function_context (fn);
17021 if (fn)
17022 dwarf2out_abstract_function (fn);
17025 if (DECL_P (origin))
17026 origin_die = lookup_decl_die (origin);
17027 else if (TYPE_P (origin))
17028 origin_die = lookup_type_die (origin);
17030 /* XXX: Functions that are never lowered don't always have correct block
17031 trees (in the case of java, they simply have no block tree, in some other
17032 languages). For these functions, there is nothing we can really do to
17033 output correct debug info for inlined functions in all cases. Rather
17034 than die, we'll just produce deficient debug info now, in that we will
17035 have variables without a proper abstract origin. In the future, when all
17036 functions are lowered, we should re-add a gcc_assert (origin_die)
17037 here. */
17039 if (origin_die)
17040 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
17041 return origin_die;
17044 /* We do not currently support the pure_virtual attribute. */
17046 static inline void
17047 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
17049 if (DECL_VINDEX (func_decl))
17051 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
17053 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
17054 add_AT_loc (die, DW_AT_vtable_elem_location,
17055 new_loc_descr (DW_OP_constu,
17056 tree_to_shwi (DECL_VINDEX (func_decl)),
17057 0));
17059 /* GNU extension: Record what type this method came from originally. */
17060 if (debug_info_level > DINFO_LEVEL_TERSE
17061 && DECL_CONTEXT (func_decl))
17062 add_AT_die_ref (die, DW_AT_containing_type,
17063 lookup_type_die (DECL_CONTEXT (func_decl)));
17067 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
17068 given decl. This used to be a vendor extension until after DWARF 4
17069 standardized it. */
17071 static void
17072 add_linkage_attr (dw_die_ref die, tree decl)
17074 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
17076 /* Mimic what assemble_name_raw does with a leading '*'. */
17077 if (name[0] == '*')
17078 name = &name[1];
17080 if (dwarf_version >= 4)
17081 add_AT_string (die, DW_AT_linkage_name, name);
17082 else
17083 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
17086 /* Add source coordinate attributes for the given decl. */
17088 static void
17089 add_src_coords_attributes (dw_die_ref die, tree decl)
17091 expanded_location s;
17093 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
17094 return;
17095 s = expand_location (DECL_SOURCE_LOCATION (decl));
17096 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
17097 add_AT_unsigned (die, DW_AT_decl_line, s.line);
17100 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
17102 static void
17103 add_linkage_name (dw_die_ref die, tree decl)
17105 if (debug_info_level > DINFO_LEVEL_NONE
17106 && (TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
17107 && TREE_PUBLIC (decl)
17108 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
17109 && die->die_tag != DW_TAG_member)
17111 /* Defer until we have an assembler name set. */
17112 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
17114 limbo_die_node *asm_name;
17116 asm_name = ggc_cleared_alloc<limbo_die_node> ();
17117 asm_name->die = die;
17118 asm_name->created_for = decl;
17119 asm_name->next = deferred_asm_name;
17120 deferred_asm_name = asm_name;
17122 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
17123 add_linkage_attr (die, decl);
17127 /* Add a DW_AT_name attribute and source coordinate attribute for the
17128 given decl, but only if it actually has a name. */
17130 static void
17131 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
17133 tree decl_name;
17135 decl_name = DECL_NAME (decl);
17136 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
17138 const char *name = dwarf2_name (decl, 0);
17139 if (name)
17140 add_name_attribute (die, name);
17141 if (! DECL_ARTIFICIAL (decl))
17142 add_src_coords_attributes (die, decl);
17144 add_linkage_name (die, decl);
17147 #ifdef VMS_DEBUGGING_INFO
17148 /* Get the function's name, as described by its RTL. This may be different
17149 from the DECL_NAME name used in the source file. */
17150 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
17152 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
17153 XEXP (DECL_RTL (decl), 0), false);
17154 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
17156 #endif /* VMS_DEBUGGING_INFO */
17159 #ifdef VMS_DEBUGGING_INFO
17160 /* Output the debug main pointer die for VMS */
17162 void
17163 dwarf2out_vms_debug_main_pointer (void)
17165 char label[MAX_ARTIFICIAL_LABEL_BYTES];
17166 dw_die_ref die;
17168 /* Allocate the VMS debug main subprogram die. */
17169 die = ggc_cleared_alloc<die_node> ();
17170 die->die_tag = DW_TAG_subprogram;
17171 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
17172 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
17173 current_function_funcdef_no);
17174 add_AT_lbl_id (die, DW_AT_entry_pc, label);
17176 /* Make it the first child of comp_unit_die (). */
17177 die->die_parent = comp_unit_die ();
17178 if (comp_unit_die ()->die_child)
17180 die->die_sib = comp_unit_die ()->die_child->die_sib;
17181 comp_unit_die ()->die_child->die_sib = die;
17183 else
17185 die->die_sib = die;
17186 comp_unit_die ()->die_child = die;
17189 #endif /* VMS_DEBUGGING_INFO */
17191 /* Push a new declaration scope. */
17193 static void
17194 push_decl_scope (tree scope)
17196 vec_safe_push (decl_scope_table, scope);
17199 /* Pop a declaration scope. */
17201 static inline void
17202 pop_decl_scope (void)
17204 decl_scope_table->pop ();
17207 /* walk_tree helper function for uses_local_type, below. */
17209 static tree
17210 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
17212 if (!TYPE_P (*tp))
17213 *walk_subtrees = 0;
17214 else
17216 tree name = TYPE_NAME (*tp);
17217 if (name && DECL_P (name) && decl_function_context (name))
17218 return *tp;
17220 return NULL_TREE;
17223 /* If TYPE involves a function-local type (including a local typedef to a
17224 non-local type), returns that type; otherwise returns NULL_TREE. */
17226 static tree
17227 uses_local_type (tree type)
17229 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
17230 return used;
17233 /* Return the DIE for the scope that immediately contains this type.
17234 Non-named types that do not involve a function-local type get global
17235 scope. Named types nested in namespaces or other types get their
17236 containing scope. All other types (i.e. function-local named types) get
17237 the current active scope. */
17239 static dw_die_ref
17240 scope_die_for (tree t, dw_die_ref context_die)
17242 dw_die_ref scope_die = NULL;
17243 tree containing_scope;
17245 /* Non-types always go in the current scope. */
17246 gcc_assert (TYPE_P (t));
17248 /* Use the scope of the typedef, rather than the scope of the type
17249 it refers to. */
17250 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
17251 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
17252 else
17253 containing_scope = TYPE_CONTEXT (t);
17255 /* Use the containing namespace if there is one. */
17256 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
17258 if (context_die == lookup_decl_die (containing_scope))
17259 /* OK */;
17260 else if (debug_info_level > DINFO_LEVEL_TERSE)
17261 context_die = get_context_die (containing_scope);
17262 else
17263 containing_scope = NULL_TREE;
17266 /* Ignore function type "scopes" from the C frontend. They mean that
17267 a tagged type is local to a parmlist of a function declarator, but
17268 that isn't useful to DWARF. */
17269 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
17270 containing_scope = NULL_TREE;
17272 if (SCOPE_FILE_SCOPE_P (containing_scope))
17274 /* If T uses a local type keep it local as well, to avoid references
17275 to function-local DIEs from outside the function. */
17276 if (current_function_decl && uses_local_type (t))
17277 scope_die = context_die;
17278 else
17279 scope_die = comp_unit_die ();
17281 else if (TYPE_P (containing_scope))
17283 /* For types, we can just look up the appropriate DIE. */
17284 if (debug_info_level > DINFO_LEVEL_TERSE)
17285 scope_die = get_context_die (containing_scope);
17286 else
17288 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
17289 if (scope_die == NULL)
17290 scope_die = comp_unit_die ();
17293 else
17294 scope_die = context_die;
17296 return scope_die;
17299 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
17301 static inline int
17302 local_scope_p (dw_die_ref context_die)
17304 for (; context_die; context_die = context_die->die_parent)
17305 if (context_die->die_tag == DW_TAG_inlined_subroutine
17306 || context_die->die_tag == DW_TAG_subprogram)
17307 return 1;
17309 return 0;
17312 /* Returns nonzero if CONTEXT_DIE is a class. */
17314 static inline int
17315 class_scope_p (dw_die_ref context_die)
17317 return (context_die
17318 && (context_die->die_tag == DW_TAG_structure_type
17319 || context_die->die_tag == DW_TAG_class_type
17320 || context_die->die_tag == DW_TAG_interface_type
17321 || context_die->die_tag == DW_TAG_union_type));
17324 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
17325 whether or not to treat a DIE in this context as a declaration. */
17327 static inline int
17328 class_or_namespace_scope_p (dw_die_ref context_die)
17330 return (class_scope_p (context_die)
17331 || (context_die && context_die->die_tag == DW_TAG_namespace));
17334 /* Many forms of DIEs require a "type description" attribute. This
17335 routine locates the proper "type descriptor" die for the type given
17336 by 'type' plus any additional qualifiers given by 'cv_quals', and
17337 adds a DW_AT_type attribute below the given die. */
17339 static void
17340 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
17341 dw_die_ref context_die)
17343 enum tree_code code = TREE_CODE (type);
17344 dw_die_ref type_die = NULL;
17346 /* ??? If this type is an unnamed subrange type of an integral, floating-point
17347 or fixed-point type, use the inner type. This is because we have no
17348 support for unnamed types in base_type_die. This can happen if this is
17349 an Ada subrange type. Correct solution is emit a subrange type die. */
17350 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
17351 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
17352 type = TREE_TYPE (type), code = TREE_CODE (type);
17354 if (code == ERROR_MARK
17355 /* Handle a special case. For functions whose return type is void, we
17356 generate *no* type attribute. (Note that no object may have type
17357 `void', so this only applies to function return types). */
17358 || code == VOID_TYPE)
17359 return;
17361 type_die = modified_type_die (type,
17362 cv_quals | TYPE_QUALS_NO_ADDR_SPACE (type),
17363 context_die);
17365 if (type_die != NULL)
17366 add_AT_die_ref (object_die, DW_AT_type, type_die);
17369 /* Given an object die, add the calling convention attribute for the
17370 function call type. */
17371 static void
17372 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
17374 enum dwarf_calling_convention value = DW_CC_normal;
17376 value = ((enum dwarf_calling_convention)
17377 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
17379 if (is_fortran ()
17380 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
17382 /* DWARF 2 doesn't provide a way to identify a program's source-level
17383 entry point. DW_AT_calling_convention attributes are only meant
17384 to describe functions' calling conventions. However, lacking a
17385 better way to signal the Fortran main program, we used this for
17386 a long time, following existing custom. Now, DWARF 4 has
17387 DW_AT_main_subprogram, which we add below, but some tools still
17388 rely on the old way, which we thus keep. */
17389 value = DW_CC_program;
17391 if (dwarf_version >= 4 || !dwarf_strict)
17392 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
17395 /* Only add the attribute if the backend requests it, and
17396 is not DW_CC_normal. */
17397 if (value && (value != DW_CC_normal))
17398 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
17401 /* Given a tree pointer to a struct, class, union, or enum type node, return
17402 a pointer to the (string) tag name for the given type, or zero if the type
17403 was declared without a tag. */
17405 static const char *
17406 type_tag (const_tree type)
17408 const char *name = 0;
17410 if (TYPE_NAME (type) != 0)
17412 tree t = 0;
17414 /* Find the IDENTIFIER_NODE for the type name. */
17415 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
17416 && !TYPE_NAMELESS (type))
17417 t = TYPE_NAME (type);
17419 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
17420 a TYPE_DECL node, regardless of whether or not a `typedef' was
17421 involved. */
17422 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
17423 && ! DECL_IGNORED_P (TYPE_NAME (type)))
17425 /* We want to be extra verbose. Don't call dwarf_name if
17426 DECL_NAME isn't set. The default hook for decl_printable_name
17427 doesn't like that, and in this context it's correct to return
17428 0, instead of "<anonymous>" or the like. */
17429 if (DECL_NAME (TYPE_NAME (type))
17430 && !DECL_NAMELESS (TYPE_NAME (type)))
17431 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
17434 /* Now get the name as a string, or invent one. */
17435 if (!name && t != 0)
17436 name = IDENTIFIER_POINTER (t);
17439 return (name == 0 || *name == '\0') ? 0 : name;
17442 /* Return the type associated with a data member, make a special check
17443 for bit field types. */
17445 static inline tree
17446 member_declared_type (const_tree member)
17448 return (DECL_BIT_FIELD_TYPE (member)
17449 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
17452 /* Get the decl's label, as described by its RTL. This may be different
17453 from the DECL_NAME name used in the source file. */
17455 #if 0
17456 static const char *
17457 decl_start_label (tree decl)
17459 rtx x;
17460 const char *fnname;
17462 x = DECL_RTL (decl);
17463 gcc_assert (MEM_P (x));
17465 x = XEXP (x, 0);
17466 gcc_assert (GET_CODE (x) == SYMBOL_REF);
17468 fnname = XSTR (x, 0);
17469 return fnname;
17471 #endif
17473 /* These routines generate the internal representation of the DIE's for
17474 the compilation unit. Debugging information is collected by walking
17475 the declaration trees passed in from dwarf2out_decl(). */
17477 static void
17478 gen_array_type_die (tree type, dw_die_ref context_die)
17480 dw_die_ref scope_die = scope_die_for (type, context_die);
17481 dw_die_ref array_die;
17483 /* GNU compilers represent multidimensional array types as sequences of one
17484 dimensional array types whose element types are themselves array types.
17485 We sometimes squish that down to a single array_type DIE with multiple
17486 subscripts in the Dwarf debugging info. The draft Dwarf specification
17487 say that we are allowed to do this kind of compression in C, because
17488 there is no difference between an array of arrays and a multidimensional
17489 array. We don't do this for Ada to remain as close as possible to the
17490 actual representation, which is especially important against the language
17491 flexibilty wrt arrays of variable size. */
17493 bool collapse_nested_arrays = !is_ada ();
17494 tree element_type;
17496 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
17497 DW_TAG_string_type doesn't have DW_AT_type attribute). */
17498 if (TYPE_STRING_FLAG (type)
17499 && TREE_CODE (type) == ARRAY_TYPE
17500 && is_fortran ()
17501 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
17503 HOST_WIDE_INT size;
17505 array_die = new_die (DW_TAG_string_type, scope_die, type);
17506 add_name_attribute (array_die, type_tag (type));
17507 equate_type_number_to_die (type, array_die);
17508 size = int_size_in_bytes (type);
17509 if (size >= 0)
17510 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17511 else if (TYPE_DOMAIN (type) != NULL_TREE
17512 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
17513 && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
17515 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
17516 dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2, NULL);
17518 size = int_size_in_bytes (TREE_TYPE (szdecl));
17519 if (loc && size > 0)
17521 add_AT_location_description (array_die, DW_AT_string_length, loc);
17522 if (size != DWARF2_ADDR_SIZE)
17523 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17526 return;
17529 array_die = new_die (DW_TAG_array_type, scope_die, type);
17530 add_name_attribute (array_die, type_tag (type));
17531 equate_type_number_to_die (type, array_die);
17533 if (TREE_CODE (type) == VECTOR_TYPE)
17534 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
17536 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
17537 if (is_fortran ()
17538 && TREE_CODE (type) == ARRAY_TYPE
17539 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
17540 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
17541 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17543 #if 0
17544 /* We default the array ordering. SDB will probably do
17545 the right things even if DW_AT_ordering is not present. It's not even
17546 an issue until we start to get into multidimensional arrays anyway. If
17547 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
17548 then we'll have to put the DW_AT_ordering attribute back in. (But if
17549 and when we find out that we need to put these in, we will only do so
17550 for multidimensional arrays. */
17551 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17552 #endif
17554 if (TREE_CODE (type) == VECTOR_TYPE)
17556 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
17557 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
17558 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node, NULL);
17559 add_bound_info (subrange_die, DW_AT_upper_bound,
17560 size_int (TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
17562 else
17563 add_subscript_info (array_die, type, collapse_nested_arrays);
17565 /* Add representation of the type of the elements of this array type and
17566 emit the corresponding DIE if we haven't done it already. */
17567 element_type = TREE_TYPE (type);
17568 if (collapse_nested_arrays)
17569 while (TREE_CODE (element_type) == ARRAY_TYPE)
17571 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
17572 break;
17573 element_type = TREE_TYPE (element_type);
17576 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED, context_die);
17578 add_gnat_descriptive_type_attribute (array_die, type, context_die);
17579 if (TYPE_ARTIFICIAL (type))
17580 add_AT_flag (array_die, DW_AT_artificial, 1);
17582 if (get_AT (array_die, DW_AT_name))
17583 add_pubtype (type, array_die);
17586 /* This routine generates DIE for array with hidden descriptor, details
17587 are filled into *info by a langhook. */
17589 static void
17590 gen_descr_array_type_die (tree type, struct array_descr_info *info,
17591 dw_die_ref context_die)
17593 const dw_die_ref scope_die = scope_die_for (type, context_die);
17594 const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
17595 const struct loc_descr_context context = { type, info->base_decl };
17596 int dim;
17598 add_name_attribute (array_die, type_tag (type));
17599 equate_type_number_to_die (type, array_die);
17601 if (info->ndimensions > 1)
17602 switch (info->ordering)
17604 case array_descr_ordering_row_major:
17605 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17606 break;
17607 case array_descr_ordering_column_major:
17608 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17609 break;
17610 default:
17611 break;
17614 if (dwarf_version >= 3 || !dwarf_strict)
17616 if (info->data_location)
17617 add_scalar_info (array_die, DW_AT_data_location, info->data_location,
17618 dw_scalar_form_exprloc, &context);
17619 if (info->associated)
17620 add_scalar_info (array_die, DW_AT_associated, info->associated,
17621 dw_scalar_form_constant
17622 | dw_scalar_form_exprloc
17623 | dw_scalar_form_reference, &context);
17624 if (info->allocated)
17625 add_scalar_info (array_die, DW_AT_allocated, info->allocated,
17626 dw_scalar_form_constant
17627 | dw_scalar_form_exprloc
17628 | dw_scalar_form_reference, &context);
17631 add_gnat_descriptive_type_attribute (array_die, type, context_die);
17633 for (dim = 0; dim < info->ndimensions; dim++)
17635 dw_die_ref subrange_die
17636 = new_die (DW_TAG_subrange_type, array_die, NULL);
17638 if (info->dimen[dim].bounds_type)
17639 add_type_attribute (subrange_die,
17640 info->dimen[dim].bounds_type, 0,
17641 context_die);
17642 if (info->dimen[dim].lower_bound)
17643 add_bound_info (subrange_die, DW_AT_lower_bound,
17644 info->dimen[dim].lower_bound, &context);
17645 if (info->dimen[dim].upper_bound)
17646 add_bound_info (subrange_die, DW_AT_upper_bound,
17647 info->dimen[dim].upper_bound, &context);
17648 if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
17649 add_scalar_info (subrange_die, DW_AT_byte_stride,
17650 info->dimen[dim].stride,
17651 dw_scalar_form_constant
17652 | dw_scalar_form_exprloc
17653 | dw_scalar_form_reference,
17654 &context);
17657 gen_type_die (info->element_type, context_die);
17658 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
17659 context_die);
17661 if (get_AT (array_die, DW_AT_name))
17662 add_pubtype (type, array_die);
17665 #if 0
17666 static void
17667 gen_entry_point_die (tree decl, dw_die_ref context_die)
17669 tree origin = decl_ultimate_origin (decl);
17670 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
17672 if (origin != NULL)
17673 add_abstract_origin_attribute (decl_die, origin);
17674 else
17676 add_name_and_src_coords_attributes (decl_die, decl);
17677 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
17678 TYPE_UNQUALIFIED, context_die);
17681 if (DECL_ABSTRACT_P (decl))
17682 equate_decl_number_to_die (decl, decl_die);
17683 else
17684 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
17686 #endif
17688 /* Walk through the list of incomplete types again, trying once more to
17689 emit full debugging info for them. */
17691 static void
17692 retry_incomplete_types (void)
17694 int i;
17696 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
17697 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
17698 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
17701 /* Determine what tag to use for a record type. */
17703 static enum dwarf_tag
17704 record_type_tag (tree type)
17706 if (! lang_hooks.types.classify_record)
17707 return DW_TAG_structure_type;
17709 switch (lang_hooks.types.classify_record (type))
17711 case RECORD_IS_STRUCT:
17712 return DW_TAG_structure_type;
17714 case RECORD_IS_CLASS:
17715 return DW_TAG_class_type;
17717 case RECORD_IS_INTERFACE:
17718 if (dwarf_version >= 3 || !dwarf_strict)
17719 return DW_TAG_interface_type;
17720 return DW_TAG_structure_type;
17722 default:
17723 gcc_unreachable ();
17727 /* Generate a DIE to represent an enumeration type. Note that these DIEs
17728 include all of the information about the enumeration values also. Each
17729 enumerated type name/value is listed as a child of the enumerated type
17730 DIE. */
17732 static dw_die_ref
17733 gen_enumeration_type_die (tree type, dw_die_ref context_die)
17735 dw_die_ref type_die = lookup_type_die (type);
17737 if (type_die == NULL)
17739 type_die = new_die (DW_TAG_enumeration_type,
17740 scope_die_for (type, context_die), type);
17741 equate_type_number_to_die (type, type_die);
17742 add_name_attribute (type_die, type_tag (type));
17743 if (dwarf_version >= 4 || !dwarf_strict)
17745 if (ENUM_IS_SCOPED (type))
17746 add_AT_flag (type_die, DW_AT_enum_class, 1);
17747 if (ENUM_IS_OPAQUE (type))
17748 add_AT_flag (type_die, DW_AT_declaration, 1);
17751 else if (! TYPE_SIZE (type))
17752 return type_die;
17753 else
17754 remove_AT (type_die, DW_AT_declaration);
17756 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
17757 given enum type is incomplete, do not generate the DW_AT_byte_size
17758 attribute or the DW_AT_element_list attribute. */
17759 if (TYPE_SIZE (type))
17761 tree link;
17763 TREE_ASM_WRITTEN (type) = 1;
17764 add_byte_size_attribute (type_die, type);
17765 if (dwarf_version >= 3 || !dwarf_strict)
17767 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
17768 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED,
17769 context_die);
17771 if (TYPE_STUB_DECL (type) != NULL_TREE)
17773 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
17774 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
17777 /* If the first reference to this type was as the return type of an
17778 inline function, then it may not have a parent. Fix this now. */
17779 if (type_die->die_parent == NULL)
17780 add_child_die (scope_die_for (type, context_die), type_die);
17782 for (link = TYPE_VALUES (type);
17783 link != NULL; link = TREE_CHAIN (link))
17785 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
17786 tree value = TREE_VALUE (link);
17788 add_name_attribute (enum_die,
17789 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
17791 if (TREE_CODE (value) == CONST_DECL)
17792 value = DECL_INITIAL (value);
17794 if (simple_type_size_in_bits (TREE_TYPE (value))
17795 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
17797 /* For constant forms created by add_AT_unsigned DWARF
17798 consumers (GDB, elfutils, etc.) always zero extend
17799 the value. Only when the actual value is negative
17800 do we need to use add_AT_int to generate a constant
17801 form that can represent negative values. */
17802 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
17803 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
17804 add_AT_unsigned (enum_die, DW_AT_const_value,
17805 (unsigned HOST_WIDE_INT) val);
17806 else
17807 add_AT_int (enum_die, DW_AT_const_value, val);
17809 else
17810 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
17811 that here. TODO: This should be re-worked to use correct
17812 signed/unsigned double tags for all cases. */
17813 add_AT_wide (enum_die, DW_AT_const_value, value);
17816 add_gnat_descriptive_type_attribute (type_die, type, context_die);
17817 if (TYPE_ARTIFICIAL (type))
17818 add_AT_flag (type_die, DW_AT_artificial, 1);
17820 else
17821 add_AT_flag (type_die, DW_AT_declaration, 1);
17823 add_pubtype (type, type_die);
17825 return type_die;
17828 /* Generate a DIE to represent either a real live formal parameter decl or to
17829 represent just the type of some formal parameter position in some function
17830 type.
17832 Note that this routine is a bit unusual because its argument may be a
17833 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
17834 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
17835 node. If it's the former then this function is being called to output a
17836 DIE to represent a formal parameter object (or some inlining thereof). If
17837 it's the latter, then this function is only being called to output a
17838 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
17839 argument type of some subprogram type.
17840 If EMIT_NAME_P is true, name and source coordinate attributes
17841 are emitted. */
17843 static dw_die_ref
17844 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
17845 dw_die_ref context_die)
17847 tree node_or_origin = node ? node : origin;
17848 tree ultimate_origin;
17849 dw_die_ref parm_die
17850 = new_die (DW_TAG_formal_parameter, context_die, node);
17852 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
17854 case tcc_declaration:
17855 ultimate_origin = decl_ultimate_origin (node_or_origin);
17856 if (node || ultimate_origin)
17857 origin = ultimate_origin;
17858 if (origin != NULL)
17859 add_abstract_origin_attribute (parm_die, origin);
17860 else if (emit_name_p)
17861 add_name_and_src_coords_attributes (parm_die, node);
17862 if (origin == NULL
17863 || (! DECL_ABSTRACT_P (node_or_origin)
17864 && variably_modified_type_p (TREE_TYPE (node_or_origin),
17865 decl_function_context
17866 (node_or_origin))))
17868 tree type = TREE_TYPE (node_or_origin);
17869 if (decl_by_reference_p (node_or_origin))
17870 add_type_attribute (parm_die, TREE_TYPE (type),
17871 TYPE_UNQUALIFIED, context_die);
17872 else
17873 add_type_attribute (parm_die, type,
17874 decl_quals (node_or_origin),
17875 context_die);
17877 if (origin == NULL && DECL_ARTIFICIAL (node))
17878 add_AT_flag (parm_die, DW_AT_artificial, 1);
17880 if (node && node != origin)
17881 equate_decl_number_to_die (node, parm_die);
17882 if (! DECL_ABSTRACT_P (node_or_origin))
17883 add_location_or_const_value_attribute (parm_die, node_or_origin,
17884 node == NULL, DW_AT_location);
17886 break;
17888 case tcc_type:
17889 /* We were called with some kind of a ..._TYPE node. */
17890 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED,
17891 context_die);
17892 break;
17894 default:
17895 gcc_unreachable ();
17898 return parm_die;
17901 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
17902 children DW_TAG_formal_parameter DIEs representing the arguments of the
17903 parameter pack.
17905 PARM_PACK must be a function parameter pack.
17906 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
17907 must point to the subsequent arguments of the function PACK_ARG belongs to.
17908 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
17909 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
17910 following the last one for which a DIE was generated. */
17912 static dw_die_ref
17913 gen_formal_parameter_pack_die (tree parm_pack,
17914 tree pack_arg,
17915 dw_die_ref subr_die,
17916 tree *next_arg)
17918 tree arg;
17919 dw_die_ref parm_pack_die;
17921 gcc_assert (parm_pack
17922 && lang_hooks.function_parameter_pack_p (parm_pack)
17923 && subr_die);
17925 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
17926 add_src_coords_attributes (parm_pack_die, parm_pack);
17928 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
17930 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
17931 parm_pack))
17932 break;
17933 gen_formal_parameter_die (arg, NULL,
17934 false /* Don't emit name attribute. */,
17935 parm_pack_die);
17937 if (next_arg)
17938 *next_arg = arg;
17939 return parm_pack_die;
17942 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
17943 at the end of an (ANSI prototyped) formal parameters list. */
17945 static void
17946 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
17948 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
17951 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
17952 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
17953 parameters as specified in some function type specification (except for
17954 those which appear as part of a function *definition*). */
17956 static void
17957 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
17959 tree link;
17960 tree formal_type = NULL;
17961 tree first_parm_type;
17962 tree arg;
17964 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
17966 arg = DECL_ARGUMENTS (function_or_method_type);
17967 function_or_method_type = TREE_TYPE (function_or_method_type);
17969 else
17970 arg = NULL_TREE;
17972 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
17974 /* Make our first pass over the list of formal parameter types and output a
17975 DW_TAG_formal_parameter DIE for each one. */
17976 for (link = first_parm_type; link; )
17978 dw_die_ref parm_die;
17980 formal_type = TREE_VALUE (link);
17981 if (formal_type == void_type_node)
17982 break;
17984 /* Output a (nameless) DIE to represent the formal parameter itself. */
17985 if (!POINTER_BOUNDS_TYPE_P (formal_type))
17987 parm_die = gen_formal_parameter_die (formal_type, NULL,
17988 true /* Emit name attribute. */,
17989 context_die);
17990 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
17991 && link == first_parm_type)
17993 add_AT_flag (parm_die, DW_AT_artificial, 1);
17994 if (dwarf_version >= 3 || !dwarf_strict)
17995 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
17997 else if (arg && DECL_ARTIFICIAL (arg))
17998 add_AT_flag (parm_die, DW_AT_artificial, 1);
18001 link = TREE_CHAIN (link);
18002 if (arg)
18003 arg = DECL_CHAIN (arg);
18006 /* If this function type has an ellipsis, add a
18007 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
18008 if (formal_type != void_type_node)
18009 gen_unspecified_parameters_die (function_or_method_type, context_die);
18011 /* Make our second (and final) pass over the list of formal parameter types
18012 and output DIEs to represent those types (as necessary). */
18013 for (link = TYPE_ARG_TYPES (function_or_method_type);
18014 link && TREE_VALUE (link);
18015 link = TREE_CHAIN (link))
18016 gen_type_die (TREE_VALUE (link), context_die);
18019 /* We want to generate the DIE for TYPE so that we can generate the
18020 die for MEMBER, which has been defined; we will need to refer back
18021 to the member declaration nested within TYPE. If we're trying to
18022 generate minimal debug info for TYPE, processing TYPE won't do the
18023 trick; we need to attach the member declaration by hand. */
18025 static void
18026 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
18028 gen_type_die (type, context_die);
18030 /* If we're trying to avoid duplicate debug info, we may not have
18031 emitted the member decl for this function. Emit it now. */
18032 if (TYPE_STUB_DECL (type)
18033 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
18034 && ! lookup_decl_die (member))
18036 dw_die_ref type_die;
18037 gcc_assert (!decl_ultimate_origin (member));
18039 push_decl_scope (type);
18040 type_die = lookup_type_die_strip_naming_typedef (type);
18041 if (TREE_CODE (member) == FUNCTION_DECL)
18042 gen_subprogram_die (member, type_die);
18043 else if (TREE_CODE (member) == FIELD_DECL)
18045 /* Ignore the nameless fields that are used to skip bits but handle
18046 C++ anonymous unions and structs. */
18047 if (DECL_NAME (member) != NULL_TREE
18048 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
18049 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
18051 gen_type_die (member_declared_type (member), type_die);
18052 gen_field_die (member, type_die);
18055 else
18056 gen_variable_die (member, NULL_TREE, type_die);
18058 pop_decl_scope ();
18062 /* Forward declare these functions, because they are mutually recursive
18063 with their set_block_* pairing functions. */
18064 static void set_decl_origin_self (tree);
18065 static void set_decl_abstract_flags (tree, int);
18067 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
18068 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
18069 that it points to the node itself, thus indicating that the node is its
18070 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
18071 the given node is NULL, recursively descend the decl/block tree which
18072 it is the root of, and for each other ..._DECL or BLOCK node contained
18073 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
18074 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
18075 values to point to themselves. */
18077 static void
18078 set_block_origin_self (tree stmt)
18080 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
18082 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
18085 tree local_decl;
18087 for (local_decl = BLOCK_VARS (stmt);
18088 local_decl != NULL_TREE;
18089 local_decl = DECL_CHAIN (local_decl))
18090 /* Do not recurse on nested functions since the inlining status
18091 of parent and child can be different as per the DWARF spec. */
18092 if (TREE_CODE (local_decl) != FUNCTION_DECL
18093 && !DECL_EXTERNAL (local_decl))
18094 set_decl_origin_self (local_decl);
18098 tree subblock;
18100 for (subblock = BLOCK_SUBBLOCKS (stmt);
18101 subblock != NULL_TREE;
18102 subblock = BLOCK_CHAIN (subblock))
18103 set_block_origin_self (subblock); /* Recurse. */
18108 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
18109 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
18110 node to so that it points to the node itself, thus indicating that the
18111 node represents its own (abstract) origin. Additionally, if the
18112 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
18113 the decl/block tree of which the given node is the root of, and for
18114 each other ..._DECL or BLOCK node contained therein whose
18115 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
18116 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
18117 point to themselves. */
18119 static void
18120 set_decl_origin_self (tree decl)
18122 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
18124 DECL_ABSTRACT_ORIGIN (decl) = decl;
18125 if (TREE_CODE (decl) == FUNCTION_DECL)
18127 tree arg;
18129 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
18130 DECL_ABSTRACT_ORIGIN (arg) = arg;
18131 if (DECL_INITIAL (decl) != NULL_TREE
18132 && DECL_INITIAL (decl) != error_mark_node)
18133 set_block_origin_self (DECL_INITIAL (decl));
18138 /* Given a pointer to some BLOCK node, and a boolean value to set the
18139 "abstract" flags to, set that value into the BLOCK_ABSTRACT flag for
18140 the given block, and for all local decls and all local sub-blocks
18141 (recursively) which are contained therein. */
18143 static void
18144 set_block_abstract_flags (tree stmt, int setting)
18146 tree local_decl;
18147 tree subblock;
18148 unsigned int i;
18150 BLOCK_ABSTRACT (stmt) = setting;
18152 for (local_decl = BLOCK_VARS (stmt);
18153 local_decl != NULL_TREE;
18154 local_decl = DECL_CHAIN (local_decl))
18155 if (! DECL_EXTERNAL (local_decl))
18156 set_decl_abstract_flags (local_decl, setting);
18158 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
18160 local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
18161 if ((TREE_CODE (local_decl) == VAR_DECL && !TREE_STATIC (local_decl))
18162 || TREE_CODE (local_decl) == PARM_DECL)
18163 set_decl_abstract_flags (local_decl, setting);
18166 for (subblock = BLOCK_SUBBLOCKS (stmt);
18167 subblock != NULL_TREE;
18168 subblock = BLOCK_CHAIN (subblock))
18169 set_block_abstract_flags (subblock, setting);
18172 /* Given a pointer to some ..._DECL node, and a boolean value to set the
18173 "abstract" flags to, set that value into the DECL_ABSTRACT_P flag for the
18174 given decl, and (in the case where the decl is a FUNCTION_DECL) also
18175 set the abstract flags for all of the parameters, local vars, local
18176 blocks and sub-blocks (recursively) to the same setting. */
18178 static void
18179 set_decl_abstract_flags (tree decl, int setting)
18181 DECL_ABSTRACT_P (decl) = setting;
18182 if (TREE_CODE (decl) == FUNCTION_DECL)
18184 tree arg;
18186 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
18187 DECL_ABSTRACT_P (arg) = setting;
18188 if (DECL_INITIAL (decl) != NULL_TREE
18189 && DECL_INITIAL (decl) != error_mark_node)
18190 set_block_abstract_flags (DECL_INITIAL (decl), setting);
18194 /* Generate the DWARF2 info for the "abstract" instance of a function which we
18195 may later generate inlined and/or out-of-line instances of. */
18197 static void
18198 dwarf2out_abstract_function (tree decl)
18200 dw_die_ref old_die;
18201 tree save_fn;
18202 tree context;
18203 int was_abstract;
18204 hash_table<decl_loc_hasher> *old_decl_loc_table;
18205 hash_table<dw_loc_list_hasher> *old_cached_dw_loc_list_table;
18206 int old_call_site_count, old_tail_call_site_count;
18207 struct call_arg_loc_node *old_call_arg_locations;
18209 /* Make sure we have the actual abstract inline, not a clone. */
18210 decl = DECL_ORIGIN (decl);
18212 old_die = lookup_decl_die (decl);
18213 if (old_die && get_AT (old_die, DW_AT_inline))
18214 /* We've already generated the abstract instance. */
18215 return;
18217 /* We can be called while recursively when seeing block defining inlined subroutine
18218 DIE. Be sure to not clobber the outer location table nor use it or we would
18219 get locations in abstract instantces. */
18220 old_decl_loc_table = decl_loc_table;
18221 decl_loc_table = NULL;
18222 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
18223 cached_dw_loc_list_table = NULL;
18224 old_call_arg_locations = call_arg_locations;
18225 call_arg_locations = NULL;
18226 old_call_site_count = call_site_count;
18227 call_site_count = -1;
18228 old_tail_call_site_count = tail_call_site_count;
18229 tail_call_site_count = -1;
18231 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
18232 we don't get confused by DECL_ABSTRACT_P. */
18233 if (debug_info_level > DINFO_LEVEL_TERSE)
18235 context = decl_class_context (decl);
18236 if (context)
18237 gen_type_die_for_member
18238 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
18241 /* Pretend we've just finished compiling this function. */
18242 save_fn = current_function_decl;
18243 current_function_decl = decl;
18245 was_abstract = DECL_ABSTRACT_P (decl);
18246 set_decl_abstract_flags (decl, 1);
18247 dwarf2out_decl (decl);
18248 if (! was_abstract)
18249 set_decl_abstract_flags (decl, 0);
18251 current_function_decl = save_fn;
18252 decl_loc_table = old_decl_loc_table;
18253 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
18254 call_arg_locations = old_call_arg_locations;
18255 call_site_count = old_call_site_count;
18256 tail_call_site_count = old_tail_call_site_count;
18259 /* Helper function of premark_used_types() which gets called through
18260 htab_traverse.
18262 Marks the DIE of a given type in *SLOT as perennial, so it never gets
18263 marked as unused by prune_unused_types. */
18265 bool
18266 premark_used_types_helper (tree const &type, void *)
18268 dw_die_ref die;
18270 die = lookup_type_die (type);
18271 if (die != NULL)
18272 die->die_perennial_p = 1;
18273 return true;
18276 /* Helper function of premark_types_used_by_global_vars which gets called
18277 through htab_traverse.
18279 Marks the DIE of a given type in *SLOT as perennial, so it never gets
18280 marked as unused by prune_unused_types. The DIE of the type is marked
18281 only if the global variable using the type will actually be emitted. */
18284 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
18285 void *)
18287 struct types_used_by_vars_entry *entry;
18288 dw_die_ref die;
18290 entry = (struct types_used_by_vars_entry *) *slot;
18291 gcc_assert (entry->type != NULL
18292 && entry->var_decl != NULL);
18293 die = lookup_type_die (entry->type);
18294 if (die)
18296 /* Ask cgraph if the global variable really is to be emitted.
18297 If yes, then we'll keep the DIE of ENTRY->TYPE. */
18298 varpool_node *node = varpool_node::get (entry->var_decl);
18299 if (node && node->definition)
18301 die->die_perennial_p = 1;
18302 /* Keep the parent DIEs as well. */
18303 while ((die = die->die_parent) && die->die_perennial_p == 0)
18304 die->die_perennial_p = 1;
18307 return 1;
18310 /* Mark all members of used_types_hash as perennial. */
18312 static void
18313 premark_used_types (struct function *fun)
18315 if (fun && fun->used_types_hash)
18316 fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
18319 /* Mark all members of types_used_by_vars_entry as perennial. */
18321 static void
18322 premark_types_used_by_global_vars (void)
18324 if (types_used_by_vars_hash)
18325 types_used_by_vars_hash
18326 ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
18329 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
18330 for CA_LOC call arg loc node. */
18332 static dw_die_ref
18333 gen_call_site_die (tree decl, dw_die_ref subr_die,
18334 struct call_arg_loc_node *ca_loc)
18336 dw_die_ref stmt_die = NULL, die;
18337 tree block = ca_loc->block;
18339 while (block
18340 && block != DECL_INITIAL (decl)
18341 && TREE_CODE (block) == BLOCK)
18343 if (block_map.length () > BLOCK_NUMBER (block))
18344 stmt_die = block_map[BLOCK_NUMBER (block)];
18345 if (stmt_die)
18346 break;
18347 block = BLOCK_SUPERCONTEXT (block);
18349 if (stmt_die == NULL)
18350 stmt_die = subr_die;
18351 die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
18352 add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
18353 if (ca_loc->tail_call_p)
18354 add_AT_flag (die, DW_AT_GNU_tail_call, 1);
18355 if (ca_loc->symbol_ref)
18357 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
18358 if (tdie)
18359 add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
18360 else
18361 add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref, false);
18363 return die;
18366 /* Generate a DIE to represent a declared function (either file-scope or
18367 block-local). */
18369 static void
18370 gen_subprogram_die (tree decl, dw_die_ref context_die)
18372 tree origin = decl_ultimate_origin (decl);
18373 dw_die_ref subr_die;
18374 tree outer_scope;
18375 dw_die_ref old_die = lookup_decl_die (decl);
18376 int declaration = (current_function_decl != decl
18377 || class_or_namespace_scope_p (context_die));
18379 premark_used_types (DECL_STRUCT_FUNCTION (decl));
18381 /* It is possible to have both DECL_ABSTRACT_P and DECLARATION be true if we
18382 started to generate the abstract instance of an inline, decided to output
18383 its containing class, and proceeded to emit the declaration of the inline
18384 from the member list for the class. If so, DECLARATION takes priority;
18385 we'll get back to the abstract instance when done with the class. */
18387 /* The class-scope declaration DIE must be the primary DIE. */
18388 if (origin && declaration && class_or_namespace_scope_p (context_die))
18390 origin = NULL;
18391 gcc_assert (!old_die);
18394 /* Now that the C++ front end lazily declares artificial member fns, we
18395 might need to retrofit the declaration into its class. */
18396 if (!declaration && !origin && !old_die
18397 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
18398 && !class_or_namespace_scope_p (context_die)
18399 && debug_info_level > DINFO_LEVEL_TERSE)
18400 old_die = force_decl_die (decl);
18402 if (origin != NULL)
18404 gcc_assert (!declaration || local_scope_p (context_die));
18406 /* Fixup die_parent for the abstract instance of a nested
18407 inline function. */
18408 if (old_die && old_die->die_parent == NULL)
18409 add_child_die (context_die, old_die);
18411 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18412 add_abstract_origin_attribute (subr_die, origin);
18413 /* This is where the actual code for a cloned function is.
18414 Let's emit linkage name attribute for it. This helps
18415 debuggers to e.g, set breakpoints into
18416 constructors/destructors when the user asks "break
18417 K::K". */
18418 add_linkage_name (subr_die, decl);
18420 else if (old_die)
18422 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18423 struct dwarf_file_data * file_index = lookup_filename (s.file);
18425 if (!get_AT_flag (old_die, DW_AT_declaration)
18426 /* We can have a normal definition following an inline one in the
18427 case of redefinition of GNU C extern inlines.
18428 It seems reasonable to use AT_specification in this case. */
18429 && !get_AT (old_die, DW_AT_inline))
18431 /* Detect and ignore this case, where we are trying to output
18432 something we have already output. */
18433 return;
18436 /* If the definition comes from the same place as the declaration,
18437 maybe use the old DIE. We always want the DIE for this function
18438 that has the *_pc attributes to be under comp_unit_die so the
18439 debugger can find it. We also need to do this for abstract
18440 instances of inlines, since the spec requires the out-of-line copy
18441 to have the same parent. For local class methods, this doesn't
18442 apply; we just use the old DIE. */
18443 if ((is_cu_die (old_die->die_parent) || context_die == NULL)
18444 && (DECL_ARTIFICIAL (decl)
18445 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
18446 && (get_AT_unsigned (old_die, DW_AT_decl_line)
18447 == (unsigned) s.line))))
18449 subr_die = old_die;
18451 /* Clear out the declaration attribute and the formal parameters.
18452 Do not remove all children, because it is possible that this
18453 declaration die was forced using force_decl_die(). In such
18454 cases die that forced declaration die (e.g. TAG_imported_module)
18455 is one of the children that we do not want to remove. */
18456 remove_AT (subr_die, DW_AT_declaration);
18457 remove_AT (subr_die, DW_AT_object_pointer);
18458 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
18460 else
18462 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18463 add_AT_specification (subr_die, old_die);
18464 add_pubname (decl, subr_die);
18465 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18466 add_AT_file (subr_die, DW_AT_decl_file, file_index);
18467 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18468 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
18470 /* If the prototype had an 'auto' or 'decltype(auto)' return type,
18471 emit the real type on the definition die. */
18472 if (is_cxx() && debug_info_level > DINFO_LEVEL_TERSE)
18474 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
18475 if (die == auto_die || die == decltype_auto_die)
18476 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18477 TYPE_UNQUALIFIED, context_die);
18481 else
18483 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18485 if (TREE_PUBLIC (decl))
18486 add_AT_flag (subr_die, DW_AT_external, 1);
18488 add_name_and_src_coords_attributes (subr_die, decl);
18489 add_pubname (decl, subr_die);
18490 if (debug_info_level > DINFO_LEVEL_TERSE)
18492 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
18493 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18494 TYPE_UNQUALIFIED, context_die);
18497 add_pure_or_virtual_attribute (subr_die, decl);
18498 if (DECL_ARTIFICIAL (decl))
18499 add_AT_flag (subr_die, DW_AT_artificial, 1);
18501 if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
18502 add_AT_flag (subr_die, DW_AT_noreturn, 1);
18504 add_accessibility_attribute (subr_die, decl);
18507 if (declaration)
18509 if (!old_die || !get_AT (old_die, DW_AT_inline))
18511 add_AT_flag (subr_die, DW_AT_declaration, 1);
18513 /* If this is an explicit function declaration then generate
18514 a DW_AT_explicit attribute. */
18515 if (lang_hooks.decls.function_decl_explicit_p (decl)
18516 && (dwarf_version >= 3 || !dwarf_strict))
18517 add_AT_flag (subr_die, DW_AT_explicit, 1);
18519 /* If this is a C++11 deleted special function member then generate
18520 a DW_AT_GNU_deleted attribute. */
18521 if (lang_hooks.decls.function_decl_deleted_p (decl)
18522 && (! dwarf_strict))
18523 add_AT_flag (subr_die, DW_AT_GNU_deleted, 1);
18525 /* The first time we see a member function, it is in the context of
18526 the class to which it belongs. We make sure of this by emitting
18527 the class first. The next time is the definition, which is
18528 handled above. The two may come from the same source text.
18530 Note that force_decl_die() forces function declaration die. It is
18531 later reused to represent definition. */
18532 equate_decl_number_to_die (decl, subr_die);
18535 else if (DECL_ABSTRACT_P (decl))
18537 if (DECL_DECLARED_INLINE_P (decl))
18539 if (cgraph_function_possibly_inlined_p (decl))
18540 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
18541 else
18542 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
18544 else
18546 if (cgraph_function_possibly_inlined_p (decl))
18547 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
18548 else
18549 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
18552 if (DECL_DECLARED_INLINE_P (decl)
18553 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
18554 add_AT_flag (subr_die, DW_AT_artificial, 1);
18556 equate_decl_number_to_die (decl, subr_die);
18558 else if (!DECL_EXTERNAL (decl))
18560 HOST_WIDE_INT cfa_fb_offset;
18561 struct function *fun = DECL_STRUCT_FUNCTION (decl);
18563 if (!old_die || !get_AT (old_die, DW_AT_inline))
18564 equate_decl_number_to_die (decl, subr_die);
18566 gcc_checking_assert (fun);
18567 if (!flag_reorder_blocks_and_partition)
18569 dw_fde_ref fde = fun->fde;
18570 if (fde->dw_fde_begin)
18572 /* We have already generated the labels. */
18573 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18574 fde->dw_fde_end, false);
18576 else
18578 /* Create start/end labels and add the range. */
18579 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
18580 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
18581 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
18582 current_function_funcdef_no);
18583 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
18584 current_function_funcdef_no);
18585 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
18586 false);
18589 #if VMS_DEBUGGING_INFO
18590 /* HP OpenVMS Industry Standard 64: DWARF Extensions
18591 Section 2.3 Prologue and Epilogue Attributes:
18592 When a breakpoint is set on entry to a function, it is generally
18593 desirable for execution to be suspended, not on the very first
18594 instruction of the function, but rather at a point after the
18595 function's frame has been set up, after any language defined local
18596 declaration processing has been completed, and before execution of
18597 the first statement of the function begins. Debuggers generally
18598 cannot properly determine where this point is. Similarly for a
18599 breakpoint set on exit from a function. The prologue and epilogue
18600 attributes allow a compiler to communicate the location(s) to use. */
18603 if (fde->dw_fde_vms_end_prologue)
18604 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
18605 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
18607 if (fde->dw_fde_vms_begin_epilogue)
18608 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
18609 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
18611 #endif
18614 else
18616 /* Generate pubnames entries for the split function code ranges. */
18617 dw_fde_ref fde = fun->fde;
18619 if (fde->dw_fde_second_begin)
18621 if (dwarf_version >= 3 || !dwarf_strict)
18623 /* We should use ranges for non-contiguous code section
18624 addresses. Use the actual code range for the initial
18625 section, since the HOT/COLD labels might precede an
18626 alignment offset. */
18627 bool range_list_added = false;
18628 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
18629 fde->dw_fde_end, &range_list_added,
18630 false);
18631 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
18632 fde->dw_fde_second_end,
18633 &range_list_added, false);
18634 if (range_list_added)
18635 add_ranges (NULL);
18637 else
18639 /* There is no real support in DW2 for this .. so we make
18640 a work-around. First, emit the pub name for the segment
18641 containing the function label. Then make and emit a
18642 simplified subprogram DIE for the second segment with the
18643 name pre-fixed by __hot/cold_sect_of_. We use the same
18644 linkage name for the second die so that gdb will find both
18645 sections when given "b foo". */
18646 const char *name = NULL;
18647 tree decl_name = DECL_NAME (decl);
18648 dw_die_ref seg_die;
18650 /* Do the 'primary' section. */
18651 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18652 fde->dw_fde_end, false);
18654 /* Build a minimal DIE for the secondary section. */
18655 seg_die = new_die (DW_TAG_subprogram,
18656 subr_die->die_parent, decl);
18658 if (TREE_PUBLIC (decl))
18659 add_AT_flag (seg_die, DW_AT_external, 1);
18661 if (decl_name != NULL
18662 && IDENTIFIER_POINTER (decl_name) != NULL)
18664 name = dwarf2_name (decl, 1);
18665 if (! DECL_ARTIFICIAL (decl))
18666 add_src_coords_attributes (seg_die, decl);
18668 add_linkage_name (seg_die, decl);
18670 gcc_assert (name != NULL);
18671 add_pure_or_virtual_attribute (seg_die, decl);
18672 if (DECL_ARTIFICIAL (decl))
18673 add_AT_flag (seg_die, DW_AT_artificial, 1);
18675 name = concat ("__second_sect_of_", name, NULL);
18676 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
18677 fde->dw_fde_second_end, false);
18678 add_name_attribute (seg_die, name);
18679 if (want_pubnames ())
18680 add_pubname_string (name, seg_die);
18683 else
18684 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
18685 false);
18688 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
18690 /* We define the "frame base" as the function's CFA. This is more
18691 convenient for several reasons: (1) It's stable across the prologue
18692 and epilogue, which makes it better than just a frame pointer,
18693 (2) With dwarf3, there exists a one-byte encoding that allows us
18694 to reference the .debug_frame data by proxy, but failing that,
18695 (3) We can at least reuse the code inspection and interpretation
18696 code that determines the CFA position at various points in the
18697 function. */
18698 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
18700 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
18701 add_AT_loc (subr_die, DW_AT_frame_base, op);
18703 else
18705 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
18706 if (list->dw_loc_next)
18707 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
18708 else
18709 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
18712 /* Compute a displacement from the "steady-state frame pointer" to
18713 the CFA. The former is what all stack slots and argument slots
18714 will reference in the rtl; the latter is what we've told the
18715 debugger about. We'll need to adjust all frame_base references
18716 by this displacement. */
18717 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
18719 if (fun->static_chain_decl)
18720 add_AT_location_description (subr_die, DW_AT_static_link,
18721 loc_list_from_tree (fun->static_chain_decl, 2, NULL));
18724 /* Generate child dies for template paramaters. */
18725 if (debug_info_level > DINFO_LEVEL_TERSE)
18726 gen_generic_params_dies (decl);
18728 /* Now output descriptions of the arguments for this function. This gets
18729 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
18730 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
18731 `...' at the end of the formal parameter list. In order to find out if
18732 there was a trailing ellipsis or not, we must instead look at the type
18733 associated with the FUNCTION_DECL. This will be a node of type
18734 FUNCTION_TYPE. If the chain of type nodes hanging off of this
18735 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
18736 an ellipsis at the end. */
18738 /* In the case where we are describing a mere function declaration, all we
18739 need to do here (and all we *can* do here) is to describe the *types* of
18740 its formal parameters. */
18741 if (debug_info_level <= DINFO_LEVEL_TERSE)
18743 else if (declaration)
18744 gen_formal_types_die (decl, subr_die);
18745 else
18747 /* Generate DIEs to represent all known formal parameters. */
18748 tree parm = DECL_ARGUMENTS (decl);
18749 tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
18750 tree generic_decl_parm = generic_decl
18751 ? DECL_ARGUMENTS (generic_decl)
18752 : NULL;
18754 /* Now we want to walk the list of parameters of the function and
18755 emit their relevant DIEs.
18757 We consider the case of DECL being an instance of a generic function
18758 as well as it being a normal function.
18760 If DECL is an instance of a generic function we walk the
18761 parameters of the generic function declaration _and_ the parameters of
18762 DECL itself. This is useful because we want to emit specific DIEs for
18763 function parameter packs and those are declared as part of the
18764 generic function declaration. In that particular case,
18765 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
18766 That DIE has children DIEs representing the set of arguments
18767 of the pack. Note that the set of pack arguments can be empty.
18768 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
18769 children DIE.
18771 Otherwise, we just consider the parameters of DECL. */
18772 while (generic_decl_parm || parm)
18774 if (generic_decl_parm
18775 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
18776 gen_formal_parameter_pack_die (generic_decl_parm,
18777 parm, subr_die,
18778 &parm);
18779 else if (parm && !POINTER_BOUNDS_P (parm))
18781 dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
18783 if (parm == DECL_ARGUMENTS (decl)
18784 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
18785 && parm_die
18786 && (dwarf_version >= 3 || !dwarf_strict))
18787 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
18789 parm = DECL_CHAIN (parm);
18791 else if (parm)
18792 parm = DECL_CHAIN (parm);
18794 if (generic_decl_parm)
18795 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
18798 /* Decide whether we need an unspecified_parameters DIE at the end.
18799 There are 2 more cases to do this for: 1) the ansi ... declaration -
18800 this is detectable when the end of the arg list is not a
18801 void_type_node 2) an unprototyped function declaration (not a
18802 definition). This just means that we have no info about the
18803 parameters at all. */
18804 if (prototype_p (TREE_TYPE (decl)))
18806 /* This is the prototyped case, check for.... */
18807 if (stdarg_p (TREE_TYPE (decl)))
18808 gen_unspecified_parameters_die (decl, subr_die);
18810 else if (DECL_INITIAL (decl) == NULL_TREE)
18811 gen_unspecified_parameters_die (decl, subr_die);
18814 /* Output Dwarf info for all of the stuff within the body of the function
18815 (if it has one - it may be just a declaration). */
18816 outer_scope = DECL_INITIAL (decl);
18818 /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
18819 a function. This BLOCK actually represents the outermost binding contour
18820 for the function, i.e. the contour in which the function's formal
18821 parameters and labels get declared. Curiously, it appears that the front
18822 end doesn't actually put the PARM_DECL nodes for the current function onto
18823 the BLOCK_VARS list for this outer scope, but are strung off of the
18824 DECL_ARGUMENTS list for the function instead.
18826 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
18827 the LABEL_DECL nodes for the function however, and we output DWARF info
18828 for those in decls_for_scope. Just within the `outer_scope' there will be
18829 a BLOCK node representing the function's outermost pair of curly braces,
18830 and any blocks used for the base and member initializers of a C++
18831 constructor function. */
18832 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
18834 int call_site_note_count = 0;
18835 int tail_call_site_note_count = 0;
18837 /* Emit a DW_TAG_variable DIE for a named return value. */
18838 if (DECL_NAME (DECL_RESULT (decl)))
18839 gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
18841 decls_for_scope (outer_scope, subr_die);
18843 if (call_arg_locations && !dwarf_strict)
18845 struct call_arg_loc_node *ca_loc;
18846 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
18848 dw_die_ref die = NULL;
18849 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
18850 rtx arg, next_arg;
18852 for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
18853 arg; arg = next_arg)
18855 dw_loc_descr_ref reg, val;
18856 machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
18857 dw_die_ref cdie, tdie = NULL;
18859 next_arg = XEXP (arg, 1);
18860 if (REG_P (XEXP (XEXP (arg, 0), 0))
18861 && next_arg
18862 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
18863 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
18864 && REGNO (XEXP (XEXP (arg, 0), 0))
18865 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
18866 next_arg = XEXP (next_arg, 1);
18867 if (mode == VOIDmode)
18869 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
18870 if (mode == VOIDmode)
18871 mode = GET_MODE (XEXP (arg, 0));
18873 if (mode == VOIDmode || mode == BLKmode)
18874 continue;
18875 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
18877 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18878 tloc = XEXP (XEXP (arg, 0), 1);
18879 continue;
18881 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
18882 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
18884 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18885 tlocc = XEXP (XEXP (arg, 0), 1);
18886 continue;
18888 reg = NULL;
18889 if (REG_P (XEXP (XEXP (arg, 0), 0)))
18890 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
18891 VAR_INIT_STATUS_INITIALIZED);
18892 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
18894 rtx mem = XEXP (XEXP (arg, 0), 0);
18895 reg = mem_loc_descriptor (XEXP (mem, 0),
18896 get_address_mode (mem),
18897 GET_MODE (mem),
18898 VAR_INIT_STATUS_INITIALIZED);
18900 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
18901 == DEBUG_PARAMETER_REF)
18903 tree tdecl
18904 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
18905 tdie = lookup_decl_die (tdecl);
18906 if (tdie == NULL)
18907 continue;
18909 else
18910 continue;
18911 if (reg == NULL
18912 && GET_CODE (XEXP (XEXP (arg, 0), 0))
18913 != DEBUG_PARAMETER_REF)
18914 continue;
18915 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
18916 VOIDmode,
18917 VAR_INIT_STATUS_INITIALIZED);
18918 if (val == NULL)
18919 continue;
18920 if (die == NULL)
18921 die = gen_call_site_die (decl, subr_die, ca_loc);
18922 cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
18923 NULL_TREE);
18924 if (reg != NULL)
18925 add_AT_loc (cdie, DW_AT_location, reg);
18926 else if (tdie != NULL)
18927 add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
18928 add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
18929 if (next_arg != XEXP (arg, 1))
18931 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
18932 if (mode == VOIDmode)
18933 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
18934 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
18935 0), 1),
18936 mode, VOIDmode,
18937 VAR_INIT_STATUS_INITIALIZED);
18938 if (val != NULL)
18939 add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
18942 if (die == NULL
18943 && (ca_loc->symbol_ref || tloc))
18944 die = gen_call_site_die (decl, subr_die, ca_loc);
18945 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
18947 dw_loc_descr_ref tval = NULL;
18949 if (tloc != NULL_RTX)
18950 tval = mem_loc_descriptor (tloc,
18951 GET_MODE (tloc) == VOIDmode
18952 ? Pmode : GET_MODE (tloc),
18953 VOIDmode,
18954 VAR_INIT_STATUS_INITIALIZED);
18955 if (tval)
18956 add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
18957 else if (tlocc != NULL_RTX)
18959 tval = mem_loc_descriptor (tlocc,
18960 GET_MODE (tlocc) == VOIDmode
18961 ? Pmode : GET_MODE (tlocc),
18962 VOIDmode,
18963 VAR_INIT_STATUS_INITIALIZED);
18964 if (tval)
18965 add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
18966 tval);
18969 if (die != NULL)
18971 call_site_note_count++;
18972 if (ca_loc->tail_call_p)
18973 tail_call_site_note_count++;
18977 call_arg_locations = NULL;
18978 call_arg_loc_last = NULL;
18979 if (tail_call_site_count >= 0
18980 && tail_call_site_count == tail_call_site_note_count
18981 && !dwarf_strict)
18983 if (call_site_count >= 0
18984 && call_site_count == call_site_note_count)
18985 add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
18986 else
18987 add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
18989 call_site_count = -1;
18990 tail_call_site_count = -1;
18993 if (subr_die != old_die)
18994 /* Add the calling convention attribute if requested. */
18995 add_calling_convention_attribute (subr_die, decl);
18998 /* Returns a hash value for X (which really is a die_struct). */
19000 hashval_t
19001 block_die_hasher::hash (die_struct *d)
19003 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
19006 /* Return nonzero if decl_id and die_parent of die_struct X is the same
19007 as decl_id and die_parent of die_struct Y. */
19009 bool
19010 block_die_hasher::equal (die_struct *x, die_struct *y)
19012 return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
19015 /* Generate a DIE to represent a declared data object.
19016 Either DECL or ORIGIN must be non-null. */
19018 static void
19019 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
19021 HOST_WIDE_INT off = 0;
19022 tree com_decl;
19023 tree decl_or_origin = decl ? decl : origin;
19024 tree ultimate_origin;
19025 dw_die_ref var_die;
19026 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
19027 dw_die_ref origin_die;
19028 bool declaration = (DECL_EXTERNAL (decl_or_origin)
19029 || class_or_namespace_scope_p (context_die));
19030 bool specialization_p = false;
19032 ultimate_origin = decl_ultimate_origin (decl_or_origin);
19033 if (decl || ultimate_origin)
19034 origin = ultimate_origin;
19035 com_decl = fortran_common (decl_or_origin, &off);
19037 /* Symbol in common gets emitted as a child of the common block, in the form
19038 of a data member. */
19039 if (com_decl)
19041 dw_die_ref com_die;
19042 dw_loc_list_ref loc;
19043 die_node com_die_arg;
19045 var_die = lookup_decl_die (decl_or_origin);
19046 if (var_die)
19048 if (get_AT (var_die, DW_AT_location) == NULL)
19050 loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
19051 if (loc)
19053 if (off)
19055 /* Optimize the common case. */
19056 if (single_element_loc_list_p (loc)
19057 && loc->expr->dw_loc_opc == DW_OP_addr
19058 && loc->expr->dw_loc_next == NULL
19059 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
19060 == SYMBOL_REF)
19062 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
19063 loc->expr->dw_loc_oprnd1.v.val_addr
19064 = plus_constant (GET_MODE (x), x , off);
19066 else
19067 loc_list_plus_const (loc, off);
19069 add_AT_location_description (var_die, DW_AT_location, loc);
19070 remove_AT (var_die, DW_AT_declaration);
19073 return;
19076 if (common_block_die_table == NULL)
19077 common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
19079 com_die_arg.decl_id = DECL_UID (com_decl);
19080 com_die_arg.die_parent = context_die;
19081 com_die = common_block_die_table->find (&com_die_arg);
19082 loc = loc_list_from_tree (com_decl, 2, NULL);
19083 if (com_die == NULL)
19085 const char *cnam
19086 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
19087 die_node **slot;
19089 com_die = new_die (DW_TAG_common_block, context_die, decl);
19090 add_name_and_src_coords_attributes (com_die, com_decl);
19091 if (loc)
19093 add_AT_location_description (com_die, DW_AT_location, loc);
19094 /* Avoid sharing the same loc descriptor between
19095 DW_TAG_common_block and DW_TAG_variable. */
19096 loc = loc_list_from_tree (com_decl, 2, NULL);
19098 else if (DECL_EXTERNAL (decl))
19099 add_AT_flag (com_die, DW_AT_declaration, 1);
19100 if (want_pubnames ())
19101 add_pubname_string (cnam, com_die); /* ??? needed? */
19102 com_die->decl_id = DECL_UID (com_decl);
19103 slot = common_block_die_table->find_slot (com_die, INSERT);
19104 *slot = com_die;
19106 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
19108 add_AT_location_description (com_die, DW_AT_location, loc);
19109 loc = loc_list_from_tree (com_decl, 2, NULL);
19110 remove_AT (com_die, DW_AT_declaration);
19112 var_die = new_die (DW_TAG_variable, com_die, decl);
19113 add_name_and_src_coords_attributes (var_die, decl);
19114 add_type_attribute (var_die, TREE_TYPE (decl), decl_quals (decl),
19115 context_die);
19116 add_AT_flag (var_die, DW_AT_external, 1);
19117 if (loc)
19119 if (off)
19121 /* Optimize the common case. */
19122 if (single_element_loc_list_p (loc)
19123 && loc->expr->dw_loc_opc == DW_OP_addr
19124 && loc->expr->dw_loc_next == NULL
19125 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
19127 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
19128 loc->expr->dw_loc_oprnd1.v.val_addr
19129 = plus_constant (GET_MODE (x), x, off);
19131 else
19132 loc_list_plus_const (loc, off);
19134 add_AT_location_description (var_die, DW_AT_location, loc);
19136 else if (DECL_EXTERNAL (decl))
19137 add_AT_flag (var_die, DW_AT_declaration, 1);
19138 equate_decl_number_to_die (decl, var_die);
19139 return;
19142 /* If the compiler emitted a definition for the DECL declaration
19143 and if we already emitted a DIE for it, don't emit a second
19144 DIE for it again. Allow re-declarations of DECLs that are
19145 inside functions, though. */
19146 if (old_die && declaration && !local_scope_p (context_die))
19147 return;
19149 /* For static data members, the declaration in the class is supposed
19150 to have DW_TAG_member tag; the specification should still be
19151 DW_TAG_variable referencing the DW_TAG_member DIE. */
19152 if (declaration && class_scope_p (context_die))
19153 var_die = new_die (DW_TAG_member, context_die, decl);
19154 else
19155 var_die = new_die (DW_TAG_variable, context_die, decl);
19157 origin_die = NULL;
19158 if (origin != NULL)
19159 origin_die = add_abstract_origin_attribute (var_die, origin);
19161 /* Loop unrolling can create multiple blocks that refer to the same
19162 static variable, so we must test for the DW_AT_declaration flag.
19164 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
19165 copy decls and set the DECL_ABSTRACT_P flag on them instead of
19166 sharing them.
19168 ??? Duplicated blocks have been rewritten to use .debug_ranges.
19170 ??? The declare_in_namespace support causes us to get two DIEs for one
19171 variable, both of which are declarations. We want to avoid considering
19172 one to be a specification, so we must test that this DIE is not a
19173 declaration. */
19174 else if (old_die && TREE_STATIC (decl) && ! declaration
19175 && get_AT_flag (old_die, DW_AT_declaration) == 1)
19177 /* This is a definition of a C++ class level static. */
19178 add_AT_specification (var_die, old_die);
19179 specialization_p = true;
19180 if (DECL_NAME (decl))
19182 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
19183 struct dwarf_file_data * file_index = lookup_filename (s.file);
19185 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
19186 add_AT_file (var_die, DW_AT_decl_file, file_index);
19188 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
19189 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
19191 if (old_die->die_tag == DW_TAG_member)
19192 add_linkage_name (var_die, decl);
19195 else
19196 add_name_and_src_coords_attributes (var_die, decl);
19198 if ((origin == NULL && !specialization_p)
19199 || (origin != NULL
19200 && !DECL_ABSTRACT_P (decl_or_origin)
19201 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
19202 decl_function_context
19203 (decl_or_origin))))
19205 tree type = TREE_TYPE (decl_or_origin);
19207 if (decl_by_reference_p (decl_or_origin))
19208 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19209 context_die);
19210 else
19211 add_type_attribute (var_die, type, decl_quals (decl_or_origin),
19212 context_die);
19215 if (origin == NULL && !specialization_p)
19217 if (TREE_PUBLIC (decl))
19218 add_AT_flag (var_die, DW_AT_external, 1);
19220 if (DECL_ARTIFICIAL (decl))
19221 add_AT_flag (var_die, DW_AT_artificial, 1);
19223 add_accessibility_attribute (var_die, decl);
19226 if (declaration)
19227 add_AT_flag (var_die, DW_AT_declaration, 1);
19229 if (decl && (DECL_ABSTRACT_P (decl) || declaration || old_die == NULL))
19230 equate_decl_number_to_die (decl, var_die);
19232 if (! declaration
19233 && (! DECL_ABSTRACT_P (decl_or_origin)
19234 /* Local static vars are shared between all clones/inlines,
19235 so emit DW_AT_location on the abstract DIE if DECL_RTL is
19236 already set. */
19237 || (TREE_CODE (decl_or_origin) == VAR_DECL
19238 && TREE_STATIC (decl_or_origin)
19239 && DECL_RTL_SET_P (decl_or_origin)))
19240 /* When abstract origin already has DW_AT_location attribute, no need
19241 to add it again. */
19242 && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
19244 if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
19245 && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
19246 defer_location (decl_or_origin, var_die);
19247 else
19248 add_location_or_const_value_attribute (var_die, decl_or_origin,
19249 decl == NULL, DW_AT_location);
19250 add_pubname (decl_or_origin, var_die);
19252 else
19253 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
19256 /* Generate a DIE to represent a named constant. */
19258 static void
19259 gen_const_die (tree decl, dw_die_ref context_die)
19261 dw_die_ref const_die;
19262 tree type = TREE_TYPE (decl);
19264 const_die = new_die (DW_TAG_constant, context_die, decl);
19265 add_name_and_src_coords_attributes (const_die, decl);
19266 add_type_attribute (const_die, type, TYPE_QUAL_CONST, context_die);
19267 if (TREE_PUBLIC (decl))
19268 add_AT_flag (const_die, DW_AT_external, 1);
19269 if (DECL_ARTIFICIAL (decl))
19270 add_AT_flag (const_die, DW_AT_artificial, 1);
19271 tree_add_const_value_attribute_for_decl (const_die, decl);
19274 /* Generate a DIE to represent a label identifier. */
19276 static void
19277 gen_label_die (tree decl, dw_die_ref context_die)
19279 tree origin = decl_ultimate_origin (decl);
19280 dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
19281 rtx insn;
19282 char label[MAX_ARTIFICIAL_LABEL_BYTES];
19284 if (origin != NULL)
19285 add_abstract_origin_attribute (lbl_die, origin);
19286 else
19287 add_name_and_src_coords_attributes (lbl_die, decl);
19289 if (DECL_ABSTRACT_P (decl))
19290 equate_decl_number_to_die (decl, lbl_die);
19291 else
19293 insn = DECL_RTL_IF_SET (decl);
19295 /* Deleted labels are programmer specified labels which have been
19296 eliminated because of various optimizations. We still emit them
19297 here so that it is possible to put breakpoints on them. */
19298 if (insn
19299 && (LABEL_P (insn)
19300 || ((NOTE_P (insn)
19301 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
19303 /* When optimization is enabled (via -O) some parts of the compiler
19304 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
19305 represent source-level labels which were explicitly declared by
19306 the user. This really shouldn't be happening though, so catch
19307 it if it ever does happen. */
19308 gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
19310 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
19311 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19313 else if (insn
19314 && NOTE_P (insn)
19315 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
19316 && CODE_LABEL_NUMBER (insn) != -1)
19318 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
19319 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19324 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
19325 attributes to the DIE for a block STMT, to describe where the inlined
19326 function was called from. This is similar to add_src_coords_attributes. */
19328 static inline void
19329 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
19331 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
19333 if (dwarf_version >= 3 || !dwarf_strict)
19335 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
19336 add_AT_unsigned (die, DW_AT_call_line, s.line);
19341 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
19342 Add low_pc and high_pc attributes to the DIE for a block STMT. */
19344 static inline void
19345 add_high_low_attributes (tree stmt, dw_die_ref die)
19347 char label[MAX_ARTIFICIAL_LABEL_BYTES];
19349 if (BLOCK_FRAGMENT_CHAIN (stmt)
19350 && (dwarf_version >= 3 || !dwarf_strict))
19352 tree chain, superblock = NULL_TREE;
19353 dw_die_ref pdie;
19354 dw_attr_ref attr = NULL;
19356 if (inlined_function_outer_scope_p (stmt))
19358 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19359 BLOCK_NUMBER (stmt));
19360 add_AT_lbl_id (die, DW_AT_entry_pc, label);
19363 /* Optimize duplicate .debug_ranges lists or even tails of
19364 lists. If this BLOCK has same ranges as its supercontext,
19365 lookup DW_AT_ranges attribute in the supercontext (and
19366 recursively so), verify that the ranges_table contains the
19367 right values and use it instead of adding a new .debug_range. */
19368 for (chain = stmt, pdie = die;
19369 BLOCK_SAME_RANGE (chain);
19370 chain = BLOCK_SUPERCONTEXT (chain))
19372 dw_attr_ref new_attr;
19374 pdie = pdie->die_parent;
19375 if (pdie == NULL)
19376 break;
19377 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
19378 break;
19379 new_attr = get_AT (pdie, DW_AT_ranges);
19380 if (new_attr == NULL
19381 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
19382 break;
19383 attr = new_attr;
19384 superblock = BLOCK_SUPERCONTEXT (chain);
19386 if (attr != NULL
19387 && (ranges_table[attr->dw_attr_val.v.val_offset
19388 / 2 / DWARF2_ADDR_SIZE].num
19389 == BLOCK_NUMBER (superblock))
19390 && BLOCK_FRAGMENT_CHAIN (superblock))
19392 unsigned long off = attr->dw_attr_val.v.val_offset
19393 / 2 / DWARF2_ADDR_SIZE;
19394 unsigned long supercnt = 0, thiscnt = 0;
19395 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
19396 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
19398 ++supercnt;
19399 gcc_checking_assert (ranges_table[off + supercnt].num
19400 == BLOCK_NUMBER (chain));
19402 gcc_checking_assert (ranges_table[off + supercnt + 1].num == 0);
19403 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
19404 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
19405 ++thiscnt;
19406 gcc_assert (supercnt >= thiscnt);
19407 add_AT_range_list (die, DW_AT_ranges,
19408 ((off + supercnt - thiscnt)
19409 * 2 * DWARF2_ADDR_SIZE),
19410 false);
19411 return;
19414 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt), false);
19416 chain = BLOCK_FRAGMENT_CHAIN (stmt);
19419 add_ranges (chain);
19420 chain = BLOCK_FRAGMENT_CHAIN (chain);
19422 while (chain);
19423 add_ranges (NULL);
19425 else
19427 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
19428 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19429 BLOCK_NUMBER (stmt));
19430 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
19431 BLOCK_NUMBER (stmt));
19432 add_AT_low_high_pc (die, label, label_high, false);
19436 /* Generate a DIE for a lexical block. */
19438 static void
19439 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
19441 dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
19443 if (call_arg_locations)
19445 if (block_map.length () <= BLOCK_NUMBER (stmt))
19446 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
19447 block_map[BLOCK_NUMBER (stmt)] = stmt_die;
19450 if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
19451 add_high_low_attributes (stmt, stmt_die);
19453 decls_for_scope (stmt, stmt_die);
19456 /* Generate a DIE for an inlined subprogram. */
19458 static void
19459 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
19461 tree decl;
19463 /* The instance of function that is effectively being inlined shall not
19464 be abstract. */
19465 gcc_assert (! BLOCK_ABSTRACT (stmt));
19467 decl = block_ultimate_origin (stmt);
19469 /* Emit info for the abstract instance first, if we haven't yet. We
19470 must emit this even if the block is abstract, otherwise when we
19471 emit the block below (or elsewhere), we may end up trying to emit
19472 a die whose origin die hasn't been emitted, and crashing. */
19473 dwarf2out_abstract_function (decl);
19475 if (! BLOCK_ABSTRACT (stmt))
19477 dw_die_ref subr_die
19478 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
19480 if (call_arg_locations)
19482 if (block_map.length () <= BLOCK_NUMBER (stmt))
19483 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
19484 block_map[BLOCK_NUMBER (stmt)] = subr_die;
19486 add_abstract_origin_attribute (subr_die, decl);
19487 if (TREE_ASM_WRITTEN (stmt))
19488 add_high_low_attributes (stmt, subr_die);
19489 add_call_src_coords_attributes (stmt, subr_die);
19491 decls_for_scope (stmt, subr_die);
19495 /* Generate a DIE for a field in a record, or structure. */
19497 static void
19498 gen_field_die (tree decl, dw_die_ref context_die)
19500 dw_die_ref decl_die;
19502 if (TREE_TYPE (decl) == error_mark_node)
19503 return;
19505 decl_die = new_die (DW_TAG_member, context_die, decl);
19506 add_name_and_src_coords_attributes (decl_die, decl);
19507 add_type_attribute (decl_die, member_declared_type (decl),
19508 decl_quals (decl), context_die);
19510 if (DECL_BIT_FIELD_TYPE (decl))
19512 add_byte_size_attribute (decl_die, decl);
19513 add_bit_size_attribute (decl_die, decl);
19514 add_bit_offset_attribute (decl_die, decl);
19517 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
19518 add_data_member_location_attribute (decl_die, decl);
19520 if (DECL_ARTIFICIAL (decl))
19521 add_AT_flag (decl_die, DW_AT_artificial, 1);
19523 add_accessibility_attribute (decl_die, decl);
19525 /* Equate decl number to die, so that we can look up this decl later on. */
19526 equate_decl_number_to_die (decl, decl_die);
19529 #if 0
19530 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19531 Use modified_type_die instead.
19532 We keep this code here just in case these types of DIEs may be needed to
19533 represent certain things in other languages (e.g. Pascal) someday. */
19535 static void
19536 gen_pointer_type_die (tree type, dw_die_ref context_die)
19538 dw_die_ref ptr_die
19539 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
19541 equate_type_number_to_die (type, ptr_die);
19542 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19543 context_die);
19544 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19547 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19548 Use modified_type_die instead.
19549 We keep this code here just in case these types of DIEs may be needed to
19550 represent certain things in other languages (e.g. Pascal) someday. */
19552 static void
19553 gen_reference_type_die (tree type, dw_die_ref context_die)
19555 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
19557 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
19558 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
19559 else
19560 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
19562 equate_type_number_to_die (type, ref_die);
19563 add_type_attribute (ref_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19564 context_die);
19565 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19567 #endif
19569 /* Generate a DIE for a pointer to a member type. */
19571 static void
19572 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
19574 dw_die_ref ptr_die
19575 = new_die (DW_TAG_ptr_to_member_type,
19576 scope_die_for (type, context_die), type);
19578 equate_type_number_to_die (type, ptr_die);
19579 add_AT_die_ref (ptr_die, DW_AT_containing_type,
19580 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
19581 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19582 context_die);
19585 typedef const char *dchar_p; /* For DEF_VEC_P. */
19587 static char *producer_string;
19589 /* Return a heap allocated producer string including command line options
19590 if -grecord-gcc-switches. */
19592 static char *
19593 gen_producer_string (void)
19595 size_t j;
19596 auto_vec<dchar_p> switches;
19597 const char *language_string = lang_hooks.name;
19598 char *producer, *tail;
19599 const char *p;
19600 size_t len = dwarf_record_gcc_switches ? 0 : 3;
19601 size_t plen = strlen (language_string) + 1 + strlen (version_string);
19603 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
19604 switch (save_decoded_options[j].opt_index)
19606 case OPT_o:
19607 case OPT_d:
19608 case OPT_dumpbase:
19609 case OPT_dumpdir:
19610 case OPT_auxbase:
19611 case OPT_auxbase_strip:
19612 case OPT_quiet:
19613 case OPT_version:
19614 case OPT_v:
19615 case OPT_w:
19616 case OPT_L:
19617 case OPT_D:
19618 case OPT_I:
19619 case OPT_U:
19620 case OPT_SPECIAL_unknown:
19621 case OPT_SPECIAL_ignore:
19622 case OPT_SPECIAL_program_name:
19623 case OPT_SPECIAL_input_file:
19624 case OPT_grecord_gcc_switches:
19625 case OPT_gno_record_gcc_switches:
19626 case OPT__output_pch_:
19627 case OPT_fdiagnostics_show_location_:
19628 case OPT_fdiagnostics_show_option:
19629 case OPT_fdiagnostics_show_caret:
19630 case OPT_fdiagnostics_color_:
19631 case OPT_fverbose_asm:
19632 case OPT____:
19633 case OPT__sysroot_:
19634 case OPT_nostdinc:
19635 case OPT_nostdinc__:
19636 case OPT_fpreprocessed:
19637 /* Ignore these. */
19638 continue;
19639 default:
19640 if (cl_options[save_decoded_options[j].opt_index].flags
19641 & CL_NO_DWARF_RECORD)
19642 continue;
19643 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
19644 == '-');
19645 switch (save_decoded_options[j].canonical_option[0][1])
19647 case 'M':
19648 case 'i':
19649 case 'W':
19650 continue;
19651 case 'f':
19652 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
19653 "dump", 4) == 0)
19654 continue;
19655 break;
19656 default:
19657 break;
19659 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
19660 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
19661 break;
19664 producer = XNEWVEC (char, plen + 1 + len + 1);
19665 tail = producer;
19666 sprintf (tail, "%s %s", language_string, version_string);
19667 tail += plen;
19669 FOR_EACH_VEC_ELT (switches, j, p)
19671 len = strlen (p);
19672 *tail = ' ';
19673 memcpy (tail + 1, p, len);
19674 tail += len + 1;
19677 *tail = '\0';
19678 return producer;
19681 /* Given a C and/or C++ language/version string return the "highest".
19682 C++ is assumed to be "higher" than C in this case. Used for merging
19683 LTO translation unit languages. */
19684 static const char *
19685 highest_c_language (const char *lang1, const char *lang2)
19687 if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
19688 return "GNU C++14";
19689 if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
19690 return "GNU C++11";
19691 if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
19692 return "GNU C++98";
19694 if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
19695 return "GNU C11";
19696 if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
19697 return "GNU C99";
19698 if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
19699 return "GNU C89";
19701 gcc_unreachable ();
19705 /* Generate the DIE for the compilation unit. */
19707 static dw_die_ref
19708 gen_compile_unit_die (const char *filename)
19710 dw_die_ref die;
19711 const char *language_string = lang_hooks.name;
19712 int language;
19714 die = new_die (DW_TAG_compile_unit, NULL, NULL);
19716 if (filename)
19718 add_name_attribute (die, filename);
19719 /* Don't add cwd for <built-in>. */
19720 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
19721 add_comp_dir_attribute (die);
19724 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
19726 /* If our producer is LTO try to figure out a common language to use
19727 from the global list of translation units. */
19728 if (strcmp (language_string, "GNU GIMPLE") == 0)
19730 unsigned i;
19731 tree t;
19732 const char *common_lang = NULL;
19734 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
19736 if (!TRANSLATION_UNIT_LANGUAGE (t))
19737 continue;
19738 if (!common_lang)
19739 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
19740 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
19742 else if (strncmp (common_lang, "GNU C", 5) == 0
19743 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
19744 /* Mixing C and C++ is ok, use C++ in that case. */
19745 common_lang = highest_c_language (common_lang,
19746 TRANSLATION_UNIT_LANGUAGE (t));
19747 else
19749 /* Fall back to C. */
19750 common_lang = NULL;
19751 break;
19755 if (common_lang)
19756 language_string = common_lang;
19759 language = DW_LANG_C;
19760 if (strncmp (language_string, "GNU C", 5) == 0
19761 && ISDIGIT (language_string[5]))
19763 language = DW_LANG_C89;
19764 if (dwarf_version >= 3 || !dwarf_strict)
19766 if (strcmp (language_string, "GNU C89") != 0)
19767 language = DW_LANG_C99;
19769 if (dwarf_version >= 5 /* || !dwarf_strict */)
19770 if (strcmp (language_string, "GNU C11") == 0)
19771 language = DW_LANG_C11;
19774 else if (strncmp (language_string, "GNU C++", 7) == 0)
19776 language = DW_LANG_C_plus_plus;
19777 if (dwarf_version >= 5 /* || !dwarf_strict */)
19779 if (strcmp (language_string, "GNU C++11") == 0)
19780 language = DW_LANG_C_plus_plus_11;
19781 else if (strcmp (language_string, "GNU C++14") == 0)
19782 language = DW_LANG_C_plus_plus_14;
19785 else if (strcmp (language_string, "GNU F77") == 0)
19786 language = DW_LANG_Fortran77;
19787 else if (strcmp (language_string, "GNU Pascal") == 0)
19788 language = DW_LANG_Pascal83;
19789 else if (dwarf_version >= 3 || !dwarf_strict)
19791 if (strcmp (language_string, "GNU Ada") == 0)
19792 language = DW_LANG_Ada95;
19793 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
19795 language = DW_LANG_Fortran95;
19796 if (dwarf_version >= 5 /* || !dwarf_strict */)
19798 if (strcmp (language_string, "GNU Fortran2003") == 0)
19799 language = DW_LANG_Fortran03;
19800 else if (strcmp (language_string, "GNU Fortran2008") == 0)
19801 language = DW_LANG_Fortran08;
19804 else if (strcmp (language_string, "GNU Java") == 0)
19805 language = DW_LANG_Java;
19806 else if (strcmp (language_string, "GNU Objective-C") == 0)
19807 language = DW_LANG_ObjC;
19808 else if (strcmp (language_string, "GNU Objective-C++") == 0)
19809 language = DW_LANG_ObjC_plus_plus;
19810 else if (dwarf_version >= 5 || !dwarf_strict)
19812 if (strcmp (language_string, "GNU Go") == 0)
19813 language = DW_LANG_Go;
19816 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
19817 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
19818 language = DW_LANG_Fortran90;
19820 add_AT_unsigned (die, DW_AT_language, language);
19822 switch (language)
19824 case DW_LANG_Fortran77:
19825 case DW_LANG_Fortran90:
19826 case DW_LANG_Fortran95:
19827 case DW_LANG_Fortran03:
19828 case DW_LANG_Fortran08:
19829 /* Fortran has case insensitive identifiers and the front-end
19830 lowercases everything. */
19831 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
19832 break;
19833 default:
19834 /* The default DW_ID_case_sensitive doesn't need to be specified. */
19835 break;
19837 return die;
19840 /* Generate the DIE for a base class. */
19842 static void
19843 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
19845 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
19847 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, context_die);
19848 add_data_member_location_attribute (die, binfo);
19850 if (BINFO_VIRTUAL_P (binfo))
19851 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
19853 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
19854 children, otherwise the default is DW_ACCESS_public. In DWARF2
19855 the default has always been DW_ACCESS_private. */
19856 if (access == access_public_node)
19858 if (dwarf_version == 2
19859 || context_die->die_tag == DW_TAG_class_type)
19860 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
19862 else if (access == access_protected_node)
19863 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
19864 else if (dwarf_version > 2
19865 && context_die->die_tag != DW_TAG_class_type)
19866 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
19869 /* Generate a DIE for a class member. */
19871 static void
19872 gen_member_die (tree type, dw_die_ref context_die)
19874 tree member;
19875 tree binfo = TYPE_BINFO (type);
19876 dw_die_ref child;
19878 /* If this is not an incomplete type, output descriptions of each of its
19879 members. Note that as we output the DIEs necessary to represent the
19880 members of this record or union type, we will also be trying to output
19881 DIEs to represent the *types* of those members. However the `type'
19882 function (above) will specifically avoid generating type DIEs for member
19883 types *within* the list of member DIEs for this (containing) type except
19884 for those types (of members) which are explicitly marked as also being
19885 members of this (containing) type themselves. The g++ front- end can
19886 force any given type to be treated as a member of some other (containing)
19887 type by setting the TYPE_CONTEXT of the given (member) type to point to
19888 the TREE node representing the appropriate (containing) type. */
19890 /* First output info about the base classes. */
19891 if (binfo)
19893 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
19894 int i;
19895 tree base;
19897 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
19898 gen_inheritance_die (base,
19899 (accesses ? (*accesses)[i] : access_public_node),
19900 context_die);
19903 /* Now output info about the data members and type members. */
19904 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
19906 /* If we thought we were generating minimal debug info for TYPE
19907 and then changed our minds, some of the member declarations
19908 may have already been defined. Don't define them again, but
19909 do put them in the right order. */
19911 child = lookup_decl_die (member);
19912 if (child)
19913 splice_child_die (context_die, child);
19914 else
19915 gen_decl_die (member, NULL, context_die);
19918 /* Now output info about the function members (if any). */
19919 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
19921 /* Don't include clones in the member list. */
19922 if (DECL_ABSTRACT_ORIGIN (member))
19923 continue;
19925 child = lookup_decl_die (member);
19926 if (child)
19927 splice_child_die (context_die, child);
19928 else
19929 gen_decl_die (member, NULL, context_die);
19933 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
19934 is set, we pretend that the type was never defined, so we only get the
19935 member DIEs needed by later specification DIEs. */
19937 static void
19938 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
19939 enum debug_info_usage usage)
19941 dw_die_ref type_die = lookup_type_die (type);
19942 dw_die_ref scope_die = 0;
19943 int nested = 0;
19944 int complete = (TYPE_SIZE (type)
19945 && (! TYPE_STUB_DECL (type)
19946 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
19947 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
19948 complete = complete && should_emit_struct_debug (type, usage);
19950 if (type_die && ! complete)
19951 return;
19953 if (TYPE_CONTEXT (type) != NULL_TREE
19954 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19955 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
19956 nested = 1;
19958 scope_die = scope_die_for (type, context_die);
19960 /* Generate child dies for template paramaters. */
19961 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
19962 schedule_generic_params_dies_gen (type);
19964 if (! type_die || (nested && is_cu_die (scope_die)))
19965 /* First occurrence of type or toplevel definition of nested class. */
19967 dw_die_ref old_die = type_die;
19969 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
19970 ? record_type_tag (type) : DW_TAG_union_type,
19971 scope_die, type);
19972 equate_type_number_to_die (type, type_die);
19973 if (old_die)
19974 add_AT_specification (type_die, old_die);
19975 else
19976 add_name_attribute (type_die, type_tag (type));
19978 else
19979 remove_AT (type_die, DW_AT_declaration);
19981 /* If this type has been completed, then give it a byte_size attribute and
19982 then give a list of members. */
19983 if (complete && !ns_decl)
19985 /* Prevent infinite recursion in cases where the type of some member of
19986 this type is expressed in terms of this type itself. */
19987 TREE_ASM_WRITTEN (type) = 1;
19988 add_byte_size_attribute (type_die, type);
19989 if (TYPE_STUB_DECL (type) != NULL_TREE)
19991 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
19992 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
19995 /* If the first reference to this type was as the return type of an
19996 inline function, then it may not have a parent. Fix this now. */
19997 if (type_die->die_parent == NULL)
19998 add_child_die (scope_die, type_die);
20000 push_decl_scope (type);
20001 gen_member_die (type, type_die);
20002 pop_decl_scope ();
20004 add_gnat_descriptive_type_attribute (type_die, type, context_die);
20005 if (TYPE_ARTIFICIAL (type))
20006 add_AT_flag (type_die, DW_AT_artificial, 1);
20008 /* GNU extension: Record what type our vtable lives in. */
20009 if (TYPE_VFIELD (type))
20011 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
20013 gen_type_die (vtype, context_die);
20014 add_AT_die_ref (type_die, DW_AT_containing_type,
20015 lookup_type_die (vtype));
20018 else
20020 add_AT_flag (type_die, DW_AT_declaration, 1);
20022 /* We don't need to do this for function-local types. */
20023 if (TYPE_STUB_DECL (type)
20024 && ! decl_function_context (TYPE_STUB_DECL (type)))
20025 vec_safe_push (incomplete_types, type);
20028 if (get_AT (type_die, DW_AT_name))
20029 add_pubtype (type, type_die);
20032 /* Generate a DIE for a subroutine _type_. */
20034 static void
20035 gen_subroutine_type_die (tree type, dw_die_ref context_die)
20037 tree return_type = TREE_TYPE (type);
20038 dw_die_ref subr_die
20039 = new_die (DW_TAG_subroutine_type,
20040 scope_die_for (type, context_die), type);
20042 equate_type_number_to_die (type, subr_die);
20043 add_prototyped_attribute (subr_die, type);
20044 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, context_die);
20045 gen_formal_types_die (type, subr_die);
20047 if (get_AT (subr_die, DW_AT_name))
20048 add_pubtype (type, subr_die);
20051 /* Generate a DIE for a type definition. */
20053 static void
20054 gen_typedef_die (tree decl, dw_die_ref context_die)
20056 dw_die_ref type_die;
20057 tree origin;
20059 if (TREE_ASM_WRITTEN (decl))
20060 return;
20062 TREE_ASM_WRITTEN (decl) = 1;
20063 type_die = new_die (DW_TAG_typedef, context_die, decl);
20064 origin = decl_ultimate_origin (decl);
20065 if (origin != NULL)
20066 add_abstract_origin_attribute (type_die, origin);
20067 else
20069 tree type;
20071 add_name_and_src_coords_attributes (type_die, decl);
20072 if (DECL_ORIGINAL_TYPE (decl))
20074 type = DECL_ORIGINAL_TYPE (decl);
20076 gcc_assert (type != TREE_TYPE (decl));
20077 equate_type_number_to_die (TREE_TYPE (decl), type_die);
20079 else
20081 type = TREE_TYPE (decl);
20083 if (is_naming_typedef_decl (TYPE_NAME (type)))
20085 /* Here, we are in the case of decl being a typedef naming
20086 an anonymous type, e.g:
20087 typedef struct {...} foo;
20088 In that case TREE_TYPE (decl) is not a typedef variant
20089 type and TYPE_NAME of the anonymous type is set to the
20090 TYPE_DECL of the typedef. This construct is emitted by
20091 the C++ FE.
20093 TYPE is the anonymous struct named by the typedef
20094 DECL. As we need the DW_AT_type attribute of the
20095 DW_TAG_typedef to point to the DIE of TYPE, let's
20096 generate that DIE right away. add_type_attribute
20097 called below will then pick (via lookup_type_die) that
20098 anonymous struct DIE. */
20099 if (!TREE_ASM_WRITTEN (type))
20100 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
20102 /* This is a GNU Extension. We are adding a
20103 DW_AT_linkage_name attribute to the DIE of the
20104 anonymous struct TYPE. The value of that attribute
20105 is the name of the typedef decl naming the anonymous
20106 struct. This greatly eases the work of consumers of
20107 this debug info. */
20108 add_linkage_attr (lookup_type_die (type), decl);
20112 add_type_attribute (type_die, type, decl_quals (decl), context_die);
20114 if (is_naming_typedef_decl (decl))
20115 /* We want that all subsequent calls to lookup_type_die with
20116 TYPE in argument yield the DW_TAG_typedef we have just
20117 created. */
20118 equate_type_number_to_die (type, type_die);
20120 add_accessibility_attribute (type_die, decl);
20123 if (DECL_ABSTRACT_P (decl))
20124 equate_decl_number_to_die (decl, type_die);
20126 if (get_AT (type_die, DW_AT_name))
20127 add_pubtype (decl, type_die);
20130 /* Generate a DIE for a struct, class, enum or union type. */
20132 static void
20133 gen_tagged_type_die (tree type,
20134 dw_die_ref context_die,
20135 enum debug_info_usage usage)
20137 int need_pop;
20139 if (type == NULL_TREE
20140 || !is_tagged_type (type))
20141 return;
20143 /* If this is a nested type whose containing class hasn't been written
20144 out yet, writing it out will cover this one, too. This does not apply
20145 to instantiations of member class templates; they need to be added to
20146 the containing class as they are generated. FIXME: This hurts the
20147 idea of combining type decls from multiple TUs, since we can't predict
20148 what set of template instantiations we'll get. */
20149 if (TYPE_CONTEXT (type)
20150 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
20151 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
20153 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
20155 if (TREE_ASM_WRITTEN (type))
20156 return;
20158 /* If that failed, attach ourselves to the stub. */
20159 push_decl_scope (TYPE_CONTEXT (type));
20160 context_die = lookup_type_die (TYPE_CONTEXT (type));
20161 need_pop = 1;
20163 else if (TYPE_CONTEXT (type) != NULL_TREE
20164 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
20166 /* If this type is local to a function that hasn't been written
20167 out yet, use a NULL context for now; it will be fixed up in
20168 decls_for_scope. */
20169 context_die = lookup_decl_die (TYPE_CONTEXT (type));
20170 /* A declaration DIE doesn't count; nested types need to go in the
20171 specification. */
20172 if (context_die && is_declaration_die (context_die))
20173 context_die = NULL;
20174 need_pop = 0;
20176 else
20178 context_die = declare_in_namespace (type, context_die);
20179 need_pop = 0;
20182 if (TREE_CODE (type) == ENUMERAL_TYPE)
20184 /* This might have been written out by the call to
20185 declare_in_namespace. */
20186 if (!TREE_ASM_WRITTEN (type))
20187 gen_enumeration_type_die (type, context_die);
20189 else
20190 gen_struct_or_union_type_die (type, context_die, usage);
20192 if (need_pop)
20193 pop_decl_scope ();
20195 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
20196 it up if it is ever completed. gen_*_type_die will set it for us
20197 when appropriate. */
20200 /* Generate a type description DIE. */
20202 static void
20203 gen_type_die_with_usage (tree type, dw_die_ref context_die,
20204 enum debug_info_usage usage)
20206 struct array_descr_info info;
20208 if (type == NULL_TREE || type == error_mark_node)
20209 return;
20211 if (TYPE_NAME (type) != NULL_TREE
20212 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
20213 && is_redundant_typedef (TYPE_NAME (type))
20214 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
20215 /* The DECL of this type is a typedef we don't want to emit debug
20216 info for but we want debug info for its underlying typedef.
20217 This can happen for e.g, the injected-class-name of a C++
20218 type. */
20219 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
20221 /* If TYPE is a typedef type variant, let's generate debug info
20222 for the parent typedef which TYPE is a type of. */
20223 if (typedef_variant_p (type))
20225 if (TREE_ASM_WRITTEN (type))
20226 return;
20228 /* Prevent broken recursion; we can't hand off to the same type. */
20229 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
20231 /* Give typedefs the right scope. */
20232 context_die = scope_die_for (type, context_die);
20234 TREE_ASM_WRITTEN (type) = 1;
20236 gen_decl_die (TYPE_NAME (type), NULL, context_die);
20237 return;
20240 /* If type is an anonymous tagged type named by a typedef, let's
20241 generate debug info for the typedef. */
20242 if (is_naming_typedef_decl (TYPE_NAME (type)))
20244 /* Use the DIE of the containing namespace as the parent DIE of
20245 the type description DIE we want to generate. */
20246 if (DECL_CONTEXT (TYPE_NAME (type))
20247 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
20248 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
20250 gen_decl_die (TYPE_NAME (type), NULL, context_die);
20251 return;
20254 /* We are going to output a DIE to represent the unqualified version
20255 of this type (i.e. without any const or volatile qualifiers) so
20256 get the main variant (i.e. the unqualified version) of this type
20257 now. (Vectors are special because the debugging info is in the
20258 cloned type itself). */
20259 if (TREE_CODE (type) != VECTOR_TYPE)
20260 type = type_main_variant (type);
20262 /* If this is an array type with hidden descriptor, handle it first. */
20263 if (!TREE_ASM_WRITTEN (type)
20264 && lang_hooks.types.get_array_descr_info)
20266 memset (&info, 0, sizeof (info));
20267 if (lang_hooks.types.get_array_descr_info (type, &info))
20269 gen_descr_array_type_die (type, &info, context_die);
20270 TREE_ASM_WRITTEN (type) = 1;
20271 return;
20275 if (TREE_ASM_WRITTEN (type))
20276 return;
20278 switch (TREE_CODE (type))
20280 case ERROR_MARK:
20281 break;
20283 case POINTER_TYPE:
20284 case REFERENCE_TYPE:
20285 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
20286 ensures that the gen_type_die recursion will terminate even if the
20287 type is recursive. Recursive types are possible in Ada. */
20288 /* ??? We could perhaps do this for all types before the switch
20289 statement. */
20290 TREE_ASM_WRITTEN (type) = 1;
20292 /* For these types, all that is required is that we output a DIE (or a
20293 set of DIEs) to represent the "basis" type. */
20294 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20295 DINFO_USAGE_IND_USE);
20296 break;
20298 case OFFSET_TYPE:
20299 /* This code is used for C++ pointer-to-data-member types.
20300 Output a description of the relevant class type. */
20301 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
20302 DINFO_USAGE_IND_USE);
20304 /* Output a description of the type of the object pointed to. */
20305 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20306 DINFO_USAGE_IND_USE);
20308 /* Now output a DIE to represent this pointer-to-data-member type
20309 itself. */
20310 gen_ptr_to_mbr_type_die (type, context_die);
20311 break;
20313 case FUNCTION_TYPE:
20314 /* Force out return type (in case it wasn't forced out already). */
20315 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20316 DINFO_USAGE_DIR_USE);
20317 gen_subroutine_type_die (type, context_die);
20318 break;
20320 case METHOD_TYPE:
20321 /* Force out return type (in case it wasn't forced out already). */
20322 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20323 DINFO_USAGE_DIR_USE);
20324 gen_subroutine_type_die (type, context_die);
20325 break;
20327 case ARRAY_TYPE:
20328 gen_array_type_die (type, context_die);
20329 break;
20331 case VECTOR_TYPE:
20332 gen_array_type_die (type, context_die);
20333 break;
20335 case ENUMERAL_TYPE:
20336 case RECORD_TYPE:
20337 case UNION_TYPE:
20338 case QUAL_UNION_TYPE:
20339 gen_tagged_type_die (type, context_die, usage);
20340 return;
20342 case VOID_TYPE:
20343 case INTEGER_TYPE:
20344 case REAL_TYPE:
20345 case FIXED_POINT_TYPE:
20346 case COMPLEX_TYPE:
20347 case BOOLEAN_TYPE:
20348 case POINTER_BOUNDS_TYPE:
20349 /* No DIEs needed for fundamental types. */
20350 break;
20352 case NULLPTR_TYPE:
20353 case LANG_TYPE:
20354 /* Just use DW_TAG_unspecified_type. */
20356 dw_die_ref type_die = lookup_type_die (type);
20357 if (type_die == NULL)
20359 tree name = TYPE_IDENTIFIER (type);
20360 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
20361 type);
20362 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
20363 equate_type_number_to_die (type, type_die);
20366 break;
20368 default:
20369 if (is_cxx_auto (type))
20371 tree name = TYPE_IDENTIFIER (type);
20372 dw_die_ref *die = (name == get_identifier ("auto")
20373 ? &auto_die : &decltype_auto_die);
20374 if (!*die)
20376 *die = new_die (DW_TAG_unspecified_type,
20377 comp_unit_die (), NULL_TREE);
20378 add_name_attribute (*die, IDENTIFIER_POINTER (name));
20380 equate_type_number_to_die (type, *die);
20381 break;
20383 gcc_unreachable ();
20386 TREE_ASM_WRITTEN (type) = 1;
20389 static void
20390 gen_type_die (tree type, dw_die_ref context_die)
20392 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
20395 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
20396 things which are local to the given block. */
20398 static void
20399 gen_block_die (tree stmt, dw_die_ref context_die)
20401 int must_output_die = 0;
20402 bool inlined_func;
20404 /* Ignore blocks that are NULL. */
20405 if (stmt == NULL_TREE)
20406 return;
20408 inlined_func = inlined_function_outer_scope_p (stmt);
20410 /* If the block is one fragment of a non-contiguous block, do not
20411 process the variables, since they will have been done by the
20412 origin block. Do process subblocks. */
20413 if (BLOCK_FRAGMENT_ORIGIN (stmt))
20415 tree sub;
20417 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
20418 gen_block_die (sub, context_die);
20420 return;
20423 /* Determine if we need to output any Dwarf DIEs at all to represent this
20424 block. */
20425 if (inlined_func)
20426 /* The outer scopes for inlinings *must* always be represented. We
20427 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
20428 must_output_die = 1;
20429 else
20431 /* Determine if this block directly contains any "significant"
20432 local declarations which we will need to output DIEs for. */
20433 if (debug_info_level > DINFO_LEVEL_TERSE)
20434 /* We are not in terse mode so *any* local declaration counts
20435 as being a "significant" one. */
20436 must_output_die = ((BLOCK_VARS (stmt) != NULL
20437 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
20438 && (TREE_USED (stmt)
20439 || TREE_ASM_WRITTEN (stmt)
20440 || BLOCK_ABSTRACT (stmt)));
20441 else if ((TREE_USED (stmt)
20442 || TREE_ASM_WRITTEN (stmt)
20443 || BLOCK_ABSTRACT (stmt))
20444 && !dwarf2out_ignore_block (stmt))
20445 must_output_die = 1;
20448 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
20449 DIE for any block which contains no significant local declarations at
20450 all. Rather, in such cases we just call `decls_for_scope' so that any
20451 needed Dwarf info for any sub-blocks will get properly generated. Note
20452 that in terse mode, our definition of what constitutes a "significant"
20453 local declaration gets restricted to include only inlined function
20454 instances and local (nested) function definitions. */
20455 if (must_output_die)
20457 if (inlined_func)
20459 /* If STMT block is abstract, that means we have been called
20460 indirectly from dwarf2out_abstract_function.
20461 That function rightfully marks the descendent blocks (of
20462 the abstract function it is dealing with) as being abstract,
20463 precisely to prevent us from emitting any
20464 DW_TAG_inlined_subroutine DIE as a descendent
20465 of an abstract function instance. So in that case, we should
20466 not call gen_inlined_subroutine_die.
20468 Later though, when cgraph asks dwarf2out to emit info
20469 for the concrete instance of the function decl into which
20470 the concrete instance of STMT got inlined, the later will lead
20471 to the generation of a DW_TAG_inlined_subroutine DIE. */
20472 if (! BLOCK_ABSTRACT (stmt))
20473 gen_inlined_subroutine_die (stmt, context_die);
20475 else
20476 gen_lexical_block_die (stmt, context_die);
20478 else
20479 decls_for_scope (stmt, context_die);
20482 /* Process variable DECL (or variable with origin ORIGIN) within
20483 block STMT and add it to CONTEXT_DIE. */
20484 static void
20485 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
20487 dw_die_ref die;
20488 tree decl_or_origin = decl ? decl : origin;
20490 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
20491 die = lookup_decl_die (decl_or_origin);
20492 else if (TREE_CODE (decl_or_origin) == TYPE_DECL
20493 && TYPE_DECL_IS_STUB (decl_or_origin))
20494 die = lookup_type_die (TREE_TYPE (decl_or_origin));
20495 else
20496 die = NULL;
20498 if (die != NULL && die->die_parent == NULL)
20499 add_child_die (context_die, die);
20500 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
20501 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
20502 stmt, context_die);
20503 else
20504 gen_decl_die (decl, origin, context_die);
20507 /* Generate all of the decls declared within a given scope and (recursively)
20508 all of its sub-blocks. */
20510 static void
20511 decls_for_scope (tree stmt, dw_die_ref context_die)
20513 tree decl;
20514 unsigned int i;
20515 tree subblocks;
20517 /* Ignore NULL blocks. */
20518 if (stmt == NULL_TREE)
20519 return;
20521 /* Output the DIEs to represent all of the data objects and typedefs
20522 declared directly within this block but not within any nested
20523 sub-blocks. Also, nested function and tag DIEs have been
20524 generated with a parent of NULL; fix that up now. We don't
20525 have to do this if we're at -g1. */
20526 if (debug_info_level > DINFO_LEVEL_TERSE)
20528 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
20529 process_scope_var (stmt, decl, NULL_TREE, context_die);
20530 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
20531 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
20532 context_die);
20535 /* Even if we're at -g1, we need to process the subblocks in order to get
20536 inlined call information. */
20538 /* Output the DIEs to represent all sub-blocks (and the items declared
20539 therein) of this block. */
20540 for (subblocks = BLOCK_SUBBLOCKS (stmt);
20541 subblocks != NULL;
20542 subblocks = BLOCK_CHAIN (subblocks))
20543 gen_block_die (subblocks, context_die);
20546 /* Is this a typedef we can avoid emitting? */
20548 static inline int
20549 is_redundant_typedef (const_tree decl)
20551 if (TYPE_DECL_IS_STUB (decl))
20552 return 1;
20554 if (DECL_ARTIFICIAL (decl)
20555 && DECL_CONTEXT (decl)
20556 && is_tagged_type (DECL_CONTEXT (decl))
20557 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
20558 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
20559 /* Also ignore the artificial member typedef for the class name. */
20560 return 1;
20562 return 0;
20565 /* Return TRUE if TYPE is a typedef that names a type for linkage
20566 purposes. This kind of typedefs is produced by the C++ FE for
20567 constructs like:
20569 typedef struct {...} foo;
20571 In that case, there is no typedef variant type produced for foo.
20572 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
20573 struct type. */
20575 static bool
20576 is_naming_typedef_decl (const_tree decl)
20578 if (decl == NULL_TREE
20579 || TREE_CODE (decl) != TYPE_DECL
20580 || !is_tagged_type (TREE_TYPE (decl))
20581 || DECL_IS_BUILTIN (decl)
20582 || is_redundant_typedef (decl)
20583 /* It looks like Ada produces TYPE_DECLs that are very similar
20584 to C++ naming typedefs but that have different
20585 semantics. Let's be specific to c++ for now. */
20586 || !is_cxx ())
20587 return FALSE;
20589 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
20590 && TYPE_NAME (TREE_TYPE (decl)) == decl
20591 && (TYPE_STUB_DECL (TREE_TYPE (decl))
20592 != TYPE_NAME (TREE_TYPE (decl))));
20595 /* Returns the DIE for a context. */
20597 static inline dw_die_ref
20598 get_context_die (tree context)
20600 if (context)
20602 /* Find die that represents this context. */
20603 if (TYPE_P (context))
20605 context = TYPE_MAIN_VARIANT (context);
20606 return strip_naming_typedef (context, force_type_die (context));
20608 else
20609 return force_decl_die (context);
20611 return comp_unit_die ();
20614 /* Returns the DIE for decl. A DIE will always be returned. */
20616 static dw_die_ref
20617 force_decl_die (tree decl)
20619 dw_die_ref decl_die;
20620 unsigned saved_external_flag;
20621 tree save_fn = NULL_TREE;
20622 decl_die = lookup_decl_die (decl);
20623 if (!decl_die)
20625 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
20627 decl_die = lookup_decl_die (decl);
20628 if (decl_die)
20629 return decl_die;
20631 switch (TREE_CODE (decl))
20633 case FUNCTION_DECL:
20634 /* Clear current_function_decl, so that gen_subprogram_die thinks
20635 that this is a declaration. At this point, we just want to force
20636 declaration die. */
20637 save_fn = current_function_decl;
20638 current_function_decl = NULL_TREE;
20639 gen_subprogram_die (decl, context_die);
20640 current_function_decl = save_fn;
20641 break;
20643 case VAR_DECL:
20644 /* Set external flag to force declaration die. Restore it after
20645 gen_decl_die() call. */
20646 saved_external_flag = DECL_EXTERNAL (decl);
20647 DECL_EXTERNAL (decl) = 1;
20648 gen_decl_die (decl, NULL, context_die);
20649 DECL_EXTERNAL (decl) = saved_external_flag;
20650 break;
20652 case NAMESPACE_DECL:
20653 if (dwarf_version >= 3 || !dwarf_strict)
20654 dwarf2out_decl (decl);
20655 else
20656 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
20657 decl_die = comp_unit_die ();
20658 break;
20660 case TRANSLATION_UNIT_DECL:
20661 decl_die = comp_unit_die ();
20662 break;
20664 default:
20665 gcc_unreachable ();
20668 /* We should be able to find the DIE now. */
20669 if (!decl_die)
20670 decl_die = lookup_decl_die (decl);
20671 gcc_assert (decl_die);
20674 return decl_die;
20677 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
20678 always returned. */
20680 static dw_die_ref
20681 force_type_die (tree type)
20683 dw_die_ref type_die;
20685 type_die = lookup_type_die (type);
20686 if (!type_die)
20688 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
20690 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
20691 context_die);
20692 gcc_assert (type_die);
20694 return type_die;
20697 /* Force out any required namespaces to be able to output DECL,
20698 and return the new context_die for it, if it's changed. */
20700 static dw_die_ref
20701 setup_namespace_context (tree thing, dw_die_ref context_die)
20703 tree context = (DECL_P (thing)
20704 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
20705 if (context && TREE_CODE (context) == NAMESPACE_DECL)
20706 /* Force out the namespace. */
20707 context_die = force_decl_die (context);
20709 return context_die;
20712 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
20713 type) within its namespace, if appropriate.
20715 For compatibility with older debuggers, namespace DIEs only contain
20716 declarations; all definitions are emitted at CU scope. */
20718 static dw_die_ref
20719 declare_in_namespace (tree thing, dw_die_ref context_die)
20721 dw_die_ref ns_context;
20723 if (debug_info_level <= DINFO_LEVEL_TERSE)
20724 return context_die;
20726 /* External declarations in the local scope only need to be emitted
20727 once, not once in the namespace and once in the scope.
20729 This avoids declaring the `extern' below in the
20730 namespace DIE as well as in the innermost scope:
20732 namespace S
20734 int i=5;
20735 int foo()
20737 int i=8;
20738 extern int i;
20739 return i;
20743 if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
20744 return context_die;
20746 /* If this decl is from an inlined function, then don't try to emit it in its
20747 namespace, as we will get confused. It would have already been emitted
20748 when the abstract instance of the inline function was emitted anyways. */
20749 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
20750 return context_die;
20752 ns_context = setup_namespace_context (thing, context_die);
20754 if (ns_context != context_die)
20756 if (is_fortran ())
20757 return ns_context;
20758 if (DECL_P (thing))
20759 gen_decl_die (thing, NULL, ns_context);
20760 else
20761 gen_type_die (thing, ns_context);
20763 return context_die;
20766 /* Generate a DIE for a namespace or namespace alias. */
20768 static void
20769 gen_namespace_die (tree decl, dw_die_ref context_die)
20771 dw_die_ref namespace_die;
20773 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
20774 they are an alias of. */
20775 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
20777 /* Output a real namespace or module. */
20778 context_die = setup_namespace_context (decl, comp_unit_die ());
20779 namespace_die = new_die (is_fortran ()
20780 ? DW_TAG_module : DW_TAG_namespace,
20781 context_die, decl);
20782 /* For Fortran modules defined in different CU don't add src coords. */
20783 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
20785 const char *name = dwarf2_name (decl, 0);
20786 if (name)
20787 add_name_attribute (namespace_die, name);
20789 else
20790 add_name_and_src_coords_attributes (namespace_die, decl);
20791 if (DECL_EXTERNAL (decl))
20792 add_AT_flag (namespace_die, DW_AT_declaration, 1);
20793 equate_decl_number_to_die (decl, namespace_die);
20795 else
20797 /* Output a namespace alias. */
20799 /* Force out the namespace we are an alias of, if necessary. */
20800 dw_die_ref origin_die
20801 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
20803 if (DECL_FILE_SCOPE_P (decl)
20804 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
20805 context_die = setup_namespace_context (decl, comp_unit_die ());
20806 /* Now create the namespace alias DIE. */
20807 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
20808 add_name_and_src_coords_attributes (namespace_die, decl);
20809 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
20810 equate_decl_number_to_die (decl, namespace_die);
20812 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
20813 if (want_pubnames ())
20814 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
20817 /* Generate Dwarf debug information for a decl described by DECL.
20818 The return value is currently only meaningful for PARM_DECLs,
20819 for all other decls it returns NULL. */
20821 static dw_die_ref
20822 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
20824 tree decl_or_origin = decl ? decl : origin;
20825 tree class_origin = NULL, ultimate_origin;
20827 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
20828 return NULL;
20830 /* Ignore pointer bounds decls. */
20831 if (DECL_P (decl_or_origin)
20832 && TREE_TYPE (decl_or_origin)
20833 && POINTER_BOUNDS_P (decl_or_origin))
20834 return NULL;
20836 switch (TREE_CODE (decl_or_origin))
20838 case ERROR_MARK:
20839 break;
20841 case CONST_DECL:
20842 if (!is_fortran () && !is_ada ())
20844 /* The individual enumerators of an enum type get output when we output
20845 the Dwarf representation of the relevant enum type itself. */
20846 break;
20849 /* Emit its type. */
20850 gen_type_die (TREE_TYPE (decl), context_die);
20852 /* And its containing namespace. */
20853 context_die = declare_in_namespace (decl, context_die);
20855 gen_const_die (decl, context_die);
20856 break;
20858 case FUNCTION_DECL:
20859 /* Don't output any DIEs to represent mere function declarations,
20860 unless they are class members or explicit block externs. */
20861 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
20862 && DECL_FILE_SCOPE_P (decl_or_origin)
20863 && (current_function_decl == NULL_TREE
20864 || DECL_ARTIFICIAL (decl_or_origin)))
20865 break;
20867 #if 0
20868 /* FIXME */
20869 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
20870 on local redeclarations of global functions. That seems broken. */
20871 if (current_function_decl != decl)
20872 /* This is only a declaration. */;
20873 #endif
20875 /* If we're emitting a clone, emit info for the abstract instance. */
20876 if (origin || DECL_ORIGIN (decl) != decl)
20877 dwarf2out_abstract_function (origin
20878 ? DECL_ORIGIN (origin)
20879 : DECL_ABSTRACT_ORIGIN (decl));
20881 /* If we're emitting an out-of-line copy of an inline function,
20882 emit info for the abstract instance and set up to refer to it. */
20883 else if (cgraph_function_possibly_inlined_p (decl)
20884 && ! DECL_ABSTRACT_P (decl)
20885 && ! class_or_namespace_scope_p (context_die)
20886 /* dwarf2out_abstract_function won't emit a die if this is just
20887 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
20888 that case, because that works only if we have a die. */
20889 && DECL_INITIAL (decl) != NULL_TREE)
20891 dwarf2out_abstract_function (decl);
20892 set_decl_origin_self (decl);
20895 /* Otherwise we're emitting the primary DIE for this decl. */
20896 else if (debug_info_level > DINFO_LEVEL_TERSE)
20898 /* Before we describe the FUNCTION_DECL itself, make sure that we
20899 have its containing type. */
20900 if (!origin)
20901 origin = decl_class_context (decl);
20902 if (origin != NULL_TREE)
20903 gen_type_die (origin, context_die);
20905 /* And its return type. */
20906 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
20908 /* And its virtual context. */
20909 if (DECL_VINDEX (decl) != NULL_TREE)
20910 gen_type_die (DECL_CONTEXT (decl), context_die);
20912 /* Make sure we have a member DIE for decl. */
20913 if (origin != NULL_TREE)
20914 gen_type_die_for_member (origin, decl, context_die);
20916 /* And its containing namespace. */
20917 context_die = declare_in_namespace (decl, context_die);
20920 /* Now output a DIE to represent the function itself. */
20921 if (decl)
20922 gen_subprogram_die (decl, context_die);
20923 break;
20925 case TYPE_DECL:
20926 /* If we are in terse mode, don't generate any DIEs to represent any
20927 actual typedefs. */
20928 if (debug_info_level <= DINFO_LEVEL_TERSE)
20929 break;
20931 /* In the special case of a TYPE_DECL node representing the declaration
20932 of some type tag, if the given TYPE_DECL is marked as having been
20933 instantiated from some other (original) TYPE_DECL node (e.g. one which
20934 was generated within the original definition of an inline function) we
20935 used to generate a special (abbreviated) DW_TAG_structure_type,
20936 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
20937 should be actually referencing those DIEs, as variable DIEs with that
20938 type would be emitted already in the abstract origin, so it was always
20939 removed during unused type prunning. Don't add anything in this
20940 case. */
20941 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
20942 break;
20944 if (is_redundant_typedef (decl))
20945 gen_type_die (TREE_TYPE (decl), context_die);
20946 else
20947 /* Output a DIE to represent the typedef itself. */
20948 gen_typedef_die (decl, context_die);
20949 break;
20951 case LABEL_DECL:
20952 if (debug_info_level >= DINFO_LEVEL_NORMAL)
20953 gen_label_die (decl, context_die);
20954 break;
20956 case VAR_DECL:
20957 case RESULT_DECL:
20958 /* If we are in terse mode, don't generate any DIEs to represent any
20959 variable declarations or definitions. */
20960 if (debug_info_level <= DINFO_LEVEL_TERSE)
20961 break;
20963 /* Output any DIEs that are needed to specify the type of this data
20964 object. */
20965 if (decl_by_reference_p (decl_or_origin))
20966 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20967 else
20968 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20970 /* And its containing type. */
20971 class_origin = decl_class_context (decl_or_origin);
20972 if (class_origin != NULL_TREE)
20973 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
20975 /* And its containing namespace. */
20976 context_die = declare_in_namespace (decl_or_origin, context_die);
20978 /* Now output the DIE to represent the data object itself. This gets
20979 complicated because of the possibility that the VAR_DECL really
20980 represents an inlined instance of a formal parameter for an inline
20981 function. */
20982 ultimate_origin = decl_ultimate_origin (decl_or_origin);
20983 if (ultimate_origin != NULL_TREE
20984 && TREE_CODE (ultimate_origin) == PARM_DECL)
20985 gen_formal_parameter_die (decl, origin,
20986 true /* Emit name attribute. */,
20987 context_die);
20988 else
20989 gen_variable_die (decl, origin, context_die);
20990 break;
20992 case FIELD_DECL:
20993 /* Ignore the nameless fields that are used to skip bits but handle C++
20994 anonymous unions and structs. */
20995 if (DECL_NAME (decl) != NULL_TREE
20996 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
20997 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
20999 gen_type_die (member_declared_type (decl), context_die);
21000 gen_field_die (decl, context_die);
21002 break;
21004 case PARM_DECL:
21005 if (DECL_BY_REFERENCE (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);
21009 return gen_formal_parameter_die (decl, origin,
21010 true /* Emit name attribute. */,
21011 context_die);
21013 case NAMESPACE_DECL:
21014 case IMPORTED_DECL:
21015 if (dwarf_version >= 3 || !dwarf_strict)
21016 gen_namespace_die (decl, context_die);
21017 break;
21019 case NAMELIST_DECL:
21020 gen_namelist_decl (DECL_NAME (decl), context_die,
21021 NAMELIST_DECL_ASSOCIATED_DECL (decl));
21022 break;
21024 default:
21025 /* Probably some frontend-internal decl. Assume we don't care. */
21026 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
21027 break;
21030 return NULL;
21033 /* Output debug information for global decl DECL. Called from toplev.c after
21034 compilation proper has finished. */
21036 static void
21037 dwarf2out_global_decl (tree decl)
21039 /* Output DWARF2 information for file-scope tentative data object
21040 declarations, file-scope (extern) function declarations (which
21041 had no corresponding body) and file-scope tagged type declarations
21042 and definitions which have not yet been forced out. */
21043 if ((TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
21044 && !POINTER_BOUNDS_P (decl))
21045 dwarf2out_decl (decl);
21048 /* Output debug information for type decl DECL. Called from toplev.c
21049 and from language front ends (to record built-in types). */
21050 static void
21051 dwarf2out_type_decl (tree decl, int local)
21053 if (!local)
21054 dwarf2out_decl (decl);
21057 /* Output debug information for imported module or decl DECL.
21058 NAME is non-NULL name in the lexical block if the decl has been renamed.
21059 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
21060 that DECL belongs to.
21061 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
21062 static void
21063 dwarf2out_imported_module_or_decl_1 (tree decl,
21064 tree name,
21065 tree lexical_block,
21066 dw_die_ref lexical_block_die)
21068 expanded_location xloc;
21069 dw_die_ref imported_die = NULL;
21070 dw_die_ref at_import_die;
21072 if (TREE_CODE (decl) == IMPORTED_DECL)
21074 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
21075 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
21076 gcc_assert (decl);
21078 else
21079 xloc = expand_location (input_location);
21081 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
21083 at_import_die = force_type_die (TREE_TYPE (decl));
21084 /* For namespace N { typedef void T; } using N::T; base_type_die
21085 returns NULL, but DW_TAG_imported_declaration requires
21086 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
21087 if (!at_import_die)
21089 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
21090 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
21091 at_import_die = lookup_type_die (TREE_TYPE (decl));
21092 gcc_assert (at_import_die);
21095 else
21097 at_import_die = lookup_decl_die (decl);
21098 if (!at_import_die)
21100 /* If we're trying to avoid duplicate debug info, we may not have
21101 emitted the member decl for this field. Emit it now. */
21102 if (TREE_CODE (decl) == FIELD_DECL)
21104 tree type = DECL_CONTEXT (decl);
21106 if (TYPE_CONTEXT (type)
21107 && TYPE_P (TYPE_CONTEXT (type))
21108 && !should_emit_struct_debug (TYPE_CONTEXT (type),
21109 DINFO_USAGE_DIR_USE))
21110 return;
21111 gen_type_die_for_member (type, decl,
21112 get_context_die (TYPE_CONTEXT (type)));
21114 if (TREE_CODE (decl) == NAMELIST_DECL)
21115 at_import_die = gen_namelist_decl (DECL_NAME (decl),
21116 get_context_die (DECL_CONTEXT (decl)),
21117 NULL_TREE);
21118 else
21119 at_import_die = force_decl_die (decl);
21123 if (TREE_CODE (decl) == NAMESPACE_DECL)
21125 if (dwarf_version >= 3 || !dwarf_strict)
21126 imported_die = new_die (DW_TAG_imported_module,
21127 lexical_block_die,
21128 lexical_block);
21129 else
21130 return;
21132 else
21133 imported_die = new_die (DW_TAG_imported_declaration,
21134 lexical_block_die,
21135 lexical_block);
21137 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
21138 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
21139 if (name)
21140 add_AT_string (imported_die, DW_AT_name,
21141 IDENTIFIER_POINTER (name));
21142 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
21145 /* Output debug information for imported module or decl DECL.
21146 NAME is non-NULL name in context if the decl has been renamed.
21147 CHILD is true if decl is one of the renamed decls as part of
21148 importing whole module. */
21150 static void
21151 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
21152 bool child)
21154 /* dw_die_ref at_import_die; */
21155 dw_die_ref scope_die;
21157 if (debug_info_level <= DINFO_LEVEL_TERSE)
21158 return;
21160 gcc_assert (decl);
21162 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
21163 We need decl DIE for reference and scope die. First, get DIE for the decl
21164 itself. */
21166 /* Get the scope die for decl context. Use comp_unit_die for global module
21167 or decl. If die is not found for non globals, force new die. */
21168 if (context
21169 && TYPE_P (context)
21170 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
21171 return;
21173 if (!(dwarf_version >= 3 || !dwarf_strict))
21174 return;
21176 scope_die = get_context_die (context);
21178 if (child)
21180 gcc_assert (scope_die->die_child);
21181 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
21182 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
21183 scope_die = scope_die->die_child;
21186 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
21187 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
21191 /* Output debug information for namelists. */
21193 static dw_die_ref
21194 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
21196 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
21197 tree value;
21198 unsigned i;
21200 if (debug_info_level <= DINFO_LEVEL_TERSE)
21201 return NULL;
21203 gcc_assert (scope_die != NULL);
21204 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
21205 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
21207 /* If there are no item_decls, we have a nondefining namelist, e.g.
21208 with USE association; hence, set DW_AT_declaration. */
21209 if (item_decls == NULL_TREE)
21211 add_AT_flag (nml_die, DW_AT_declaration, 1);
21212 return nml_die;
21215 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
21217 nml_item_ref_die = lookup_decl_die (value);
21218 if (!nml_item_ref_die)
21219 nml_item_ref_die = force_decl_die (value);
21221 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
21222 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
21224 return nml_die;
21228 /* Write the debugging output for DECL. */
21230 static void
21231 dwarf2out_decl (tree decl)
21233 dw_die_ref context_die = comp_unit_die ();
21235 switch (TREE_CODE (decl))
21237 case ERROR_MARK:
21238 return;
21240 case FUNCTION_DECL:
21241 /* What we would really like to do here is to filter out all mere
21242 file-scope declarations of file-scope functions which are never
21243 referenced later within this translation unit (and keep all of ones
21244 that *are* referenced later on) but we aren't clairvoyant, so we have
21245 no idea which functions will be referenced in the future (i.e. later
21246 on within the current translation unit). So here we just ignore all
21247 file-scope function declarations which are not also definitions. If
21248 and when the debugger needs to know something about these functions,
21249 it will have to hunt around and find the DWARF information associated
21250 with the definition of the function.
21252 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
21253 nodes represent definitions and which ones represent mere
21254 declarations. We have to check DECL_INITIAL instead. That's because
21255 the C front-end supports some weird semantics for "extern inline"
21256 function definitions. These can get inlined within the current
21257 translation unit (and thus, we need to generate Dwarf info for their
21258 abstract instances so that the Dwarf info for the concrete inlined
21259 instances can have something to refer to) but the compiler never
21260 generates any out-of-lines instances of such things (despite the fact
21261 that they *are* definitions).
21263 The important point is that the C front-end marks these "extern
21264 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
21265 them anyway. Note that the C++ front-end also plays some similar games
21266 for inline function definitions appearing within include files which
21267 also contain `#pragma interface' pragmas.
21269 If we are called from dwarf2out_abstract_function output a DIE
21270 anyway. We can end up here this way with early inlining and LTO
21271 where the inlined function is output in a different LTRANS unit
21272 or not at all. */
21273 if (DECL_INITIAL (decl) == NULL_TREE
21274 && ! DECL_ABSTRACT_P (decl))
21275 return;
21277 /* If we're a nested function, initially use a parent of NULL; if we're
21278 a plain function, this will be fixed up in decls_for_scope. If
21279 we're a method, it will be ignored, since we already have a DIE. */
21280 if (decl_function_context (decl)
21281 /* But if we're in terse mode, we don't care about scope. */
21282 && debug_info_level > DINFO_LEVEL_TERSE)
21283 context_die = NULL;
21284 break;
21286 case VAR_DECL:
21287 /* Ignore this VAR_DECL if it refers to a file-scope extern data object
21288 declaration and if the declaration was never even referenced from
21289 within this entire compilation unit. We suppress these DIEs in
21290 order to save space in the .debug section (by eliminating entries
21291 which are probably useless). Note that we must not suppress
21292 block-local extern declarations (whether used or not) because that
21293 would screw-up the debugger's name lookup mechanism and cause it to
21294 miss things which really ought to be in scope at a given point. */
21295 if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
21296 return;
21298 /* For local statics lookup proper context die. */
21299 if (TREE_STATIC (decl)
21300 && DECL_CONTEXT (decl)
21301 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
21302 context_die = lookup_decl_die (DECL_CONTEXT (decl));
21304 /* If we are in terse mode, don't generate any DIEs to represent any
21305 variable declarations or definitions. */
21306 if (debug_info_level <= DINFO_LEVEL_TERSE)
21307 return;
21308 break;
21310 case CONST_DECL:
21311 if (debug_info_level <= DINFO_LEVEL_TERSE)
21312 return;
21313 if (!is_fortran () && !is_ada ())
21314 return;
21315 if (TREE_STATIC (decl) && decl_function_context (decl))
21316 context_die = lookup_decl_die (DECL_CONTEXT (decl));
21317 break;
21319 case NAMESPACE_DECL:
21320 case IMPORTED_DECL:
21321 if (debug_info_level <= DINFO_LEVEL_TERSE)
21322 return;
21323 if (lookup_decl_die (decl) != NULL)
21324 return;
21325 break;
21327 case TYPE_DECL:
21328 /* Don't emit stubs for types unless they are needed by other DIEs. */
21329 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
21330 return;
21332 /* Don't bother trying to generate any DIEs to represent any of the
21333 normal built-in types for the language we are compiling. */
21334 if (DECL_IS_BUILTIN (decl))
21335 return;
21337 /* If we are in terse mode, don't generate any DIEs for types. */
21338 if (debug_info_level <= DINFO_LEVEL_TERSE)
21339 return;
21341 /* If we're a function-scope tag, initially use a parent of NULL;
21342 this will be fixed up in decls_for_scope. */
21343 if (decl_function_context (decl))
21344 context_die = NULL;
21346 break;
21348 case NAMELIST_DECL:
21349 break;
21351 default:
21352 return;
21355 gen_decl_die (decl, NULL, context_die);
21358 /* Write the debugging output for DECL. */
21360 static void
21361 dwarf2out_function_decl (tree decl)
21363 dwarf2out_decl (decl);
21364 call_arg_locations = NULL;
21365 call_arg_loc_last = NULL;
21366 call_site_count = -1;
21367 tail_call_site_count = -1;
21368 block_map.release ();
21369 decl_loc_table->empty ();
21370 cached_dw_loc_list_table->empty ();
21373 /* Output a marker (i.e. a label) for the beginning of the generated code for
21374 a lexical block. */
21376 static void
21377 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
21378 unsigned int blocknum)
21380 switch_to_section (current_function_section ());
21381 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
21384 /* Output a marker (i.e. a label) for the end of the generated code for a
21385 lexical block. */
21387 static void
21388 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
21390 switch_to_section (current_function_section ());
21391 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
21394 /* Returns nonzero if it is appropriate not to emit any debugging
21395 information for BLOCK, because it doesn't contain any instructions.
21397 Don't allow this for blocks with nested functions or local classes
21398 as we would end up with orphans, and in the presence of scheduling
21399 we may end up calling them anyway. */
21401 static bool
21402 dwarf2out_ignore_block (const_tree block)
21404 tree decl;
21405 unsigned int i;
21407 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
21408 if (TREE_CODE (decl) == FUNCTION_DECL
21409 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21410 return 0;
21411 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
21413 decl = BLOCK_NONLOCALIZED_VAR (block, i);
21414 if (TREE_CODE (decl) == FUNCTION_DECL
21415 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21416 return 0;
21419 return 1;
21422 /* Hash table routines for file_hash. */
21424 bool
21425 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
21427 return filename_cmp (p1->filename, p2) == 0;
21430 hashval_t
21431 dwarf_file_hasher::hash (dwarf_file_data *p)
21433 return htab_hash_string (p->filename);
21436 /* Lookup FILE_NAME (in the list of filenames that we know about here in
21437 dwarf2out.c) and return its "index". The index of each (known) filename is
21438 just a unique number which is associated with only that one filename. We
21439 need such numbers for the sake of generating labels (in the .debug_sfnames
21440 section) and references to those files numbers (in the .debug_srcinfo
21441 and.debug_macinfo sections). If the filename given as an argument is not
21442 found in our current list, add it to the list and assign it the next
21443 available unique index number. In order to speed up searches, we remember
21444 the index of the filename was looked up last. This handles the majority of
21445 all searches. */
21447 static struct dwarf_file_data *
21448 lookup_filename (const char *file_name)
21450 struct dwarf_file_data * created;
21452 /* Check to see if the file name that was searched on the previous
21453 call matches this file name. If so, return the index. */
21454 if (file_table_last_lookup
21455 && (file_name == file_table_last_lookup->filename
21456 || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
21457 return file_table_last_lookup;
21459 /* Didn't match the previous lookup, search the table. */
21460 dwarf_file_data **slot
21461 = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
21462 INSERT);
21463 if (*slot)
21464 return *slot;
21466 created = ggc_alloc<dwarf_file_data> ();
21467 created->filename = file_name;
21468 created->emitted_number = 0;
21469 *slot = created;
21470 return created;
21473 /* If the assembler will construct the file table, then translate the compiler
21474 internal file table number into the assembler file table number, and emit
21475 a .file directive if we haven't already emitted one yet. The file table
21476 numbers are different because we prune debug info for unused variables and
21477 types, which may include filenames. */
21479 static int
21480 maybe_emit_file (struct dwarf_file_data * fd)
21482 if (! fd->emitted_number)
21484 if (last_emitted_file)
21485 fd->emitted_number = last_emitted_file->emitted_number + 1;
21486 else
21487 fd->emitted_number = 1;
21488 last_emitted_file = fd;
21490 if (DWARF2_ASM_LINE_DEBUG_INFO)
21492 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
21493 output_quoted_string (asm_out_file,
21494 remap_debug_filename (fd->filename));
21495 fputc ('\n', asm_out_file);
21499 return fd->emitted_number;
21502 /* Schedule generation of a DW_AT_const_value attribute to DIE.
21503 That generation should happen after function debug info has been
21504 generated. The value of the attribute is the constant value of ARG. */
21506 static void
21507 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
21509 die_arg_entry entry;
21511 if (!die || !arg)
21512 return;
21514 if (!tmpl_value_parm_die_table)
21515 vec_alloc (tmpl_value_parm_die_table, 32);
21517 entry.die = die;
21518 entry.arg = arg;
21519 vec_safe_push (tmpl_value_parm_die_table, entry);
21522 /* Return TRUE if T is an instance of generic type, FALSE
21523 otherwise. */
21525 static bool
21526 generic_type_p (tree t)
21528 if (t == NULL_TREE || !TYPE_P (t))
21529 return false;
21530 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
21533 /* Schedule the generation of the generic parameter dies for the
21534 instance of generic type T. The proper generation itself is later
21535 done by gen_scheduled_generic_parms_dies. */
21537 static void
21538 schedule_generic_params_dies_gen (tree t)
21540 if (!generic_type_p (t))
21541 return;
21543 if (!generic_type_instances)
21544 vec_alloc (generic_type_instances, 256);
21546 vec_safe_push (generic_type_instances, t);
21549 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
21550 by append_entry_to_tmpl_value_parm_die_table. This function must
21551 be called after function DIEs have been generated. */
21553 static void
21554 gen_remaining_tmpl_value_param_die_attribute (void)
21556 if (tmpl_value_parm_die_table)
21558 unsigned i;
21559 die_arg_entry *e;
21561 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
21562 tree_add_const_value_attribute (e->die, e->arg);
21566 /* Generate generic parameters DIEs for instances of generic types
21567 that have been previously scheduled by
21568 schedule_generic_params_dies_gen. This function must be called
21569 after all the types of the CU have been laid out. */
21571 static void
21572 gen_scheduled_generic_parms_dies (void)
21574 unsigned i;
21575 tree t;
21577 if (!generic_type_instances)
21578 return;
21580 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
21581 if (COMPLETE_TYPE_P (t))
21582 gen_generic_params_dies (t);
21586 /* Replace DW_AT_name for the decl with name. */
21588 static void
21589 dwarf2out_set_name (tree decl, tree name)
21591 dw_die_ref die;
21592 dw_attr_ref attr;
21593 const char *dname;
21595 die = TYPE_SYMTAB_DIE (decl);
21596 if (!die)
21597 return;
21599 dname = dwarf2_name (name, 0);
21600 if (!dname)
21601 return;
21603 attr = get_AT (die, DW_AT_name);
21604 if (attr)
21606 struct indirect_string_node *node;
21608 node = find_AT_string (dname);
21609 /* replace the string. */
21610 attr->dw_attr_val.v.val_str = node;
21613 else
21614 add_name_attribute (die, dname);
21617 /* True if before or during processing of the first function being emitted. */
21618 static bool in_first_function_p = true;
21619 /* True if loc_note during dwarf2out_var_location call might still be
21620 before first real instruction at address equal to .Ltext0. */
21621 static bool maybe_at_text_label_p = true;
21622 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
21623 static unsigned int first_loclabel_num_not_at_text_label;
21625 /* Called by the final INSN scan whenever we see a var location. We
21626 use it to drop labels in the right places, and throw the location in
21627 our lookup table. */
21629 static void
21630 dwarf2out_var_location (rtx_insn *loc_note)
21632 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
21633 struct var_loc_node *newloc;
21634 rtx_insn *next_real, *next_note;
21635 static const char *last_label;
21636 static const char *last_postcall_label;
21637 static bool last_in_cold_section_p;
21638 static rtx_insn *expected_next_loc_note;
21639 tree decl;
21640 bool var_loc_p;
21642 if (!NOTE_P (loc_note))
21644 if (CALL_P (loc_note))
21646 call_site_count++;
21647 if (SIBLING_CALL_P (loc_note))
21648 tail_call_site_count++;
21650 return;
21653 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
21654 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
21655 return;
21657 /* Optimize processing a large consecutive sequence of location
21658 notes so we don't spend too much time in next_real_insn. If the
21659 next insn is another location note, remember the next_real_insn
21660 calculation for next time. */
21661 next_real = cached_next_real_insn;
21662 if (next_real)
21664 if (expected_next_loc_note != loc_note)
21665 next_real = NULL;
21668 next_note = NEXT_INSN (loc_note);
21669 if (! next_note
21670 || next_note->deleted ()
21671 || ! NOTE_P (next_note)
21672 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
21673 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
21674 next_note = NULL;
21676 if (! next_real)
21677 next_real = next_real_insn (loc_note);
21679 if (next_note)
21681 expected_next_loc_note = next_note;
21682 cached_next_real_insn = next_real;
21684 else
21685 cached_next_real_insn = NULL;
21687 /* If there are no instructions which would be affected by this note,
21688 don't do anything. */
21689 if (var_loc_p
21690 && next_real == NULL_RTX
21691 && !NOTE_DURING_CALL_P (loc_note))
21692 return;
21694 if (next_real == NULL_RTX)
21695 next_real = get_last_insn ();
21697 /* If there were any real insns between note we processed last time
21698 and this note (or if it is the first note), clear
21699 last_{,postcall_}label so that they are not reused this time. */
21700 if (last_var_location_insn == NULL_RTX
21701 || last_var_location_insn != next_real
21702 || last_in_cold_section_p != in_cold_section_p)
21704 last_label = NULL;
21705 last_postcall_label = NULL;
21708 if (var_loc_p)
21710 decl = NOTE_VAR_LOCATION_DECL (loc_note);
21711 newloc = add_var_loc_to_decl (decl, loc_note,
21712 NOTE_DURING_CALL_P (loc_note)
21713 ? last_postcall_label : last_label);
21714 if (newloc == NULL)
21715 return;
21717 else
21719 decl = NULL_TREE;
21720 newloc = NULL;
21723 /* If there were no real insns between note we processed last time
21724 and this note, use the label we emitted last time. Otherwise
21725 create a new label and emit it. */
21726 if (last_label == NULL)
21728 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
21729 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
21730 loclabel_num++;
21731 last_label = ggc_strdup (loclabel);
21732 /* See if loclabel might be equal to .Ltext0. If yes,
21733 bump first_loclabel_num_not_at_text_label. */
21734 if (!have_multiple_function_sections
21735 && in_first_function_p
21736 && maybe_at_text_label_p)
21738 static rtx_insn *last_start;
21739 rtx_insn *insn;
21740 for (insn = loc_note; insn; insn = previous_insn (insn))
21741 if (insn == last_start)
21742 break;
21743 else if (!NONDEBUG_INSN_P (insn))
21744 continue;
21745 else
21747 rtx body = PATTERN (insn);
21748 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
21749 continue;
21750 /* Inline asm could occupy zero bytes. */
21751 else if (GET_CODE (body) == ASM_INPUT
21752 || asm_noperands (body) >= 0)
21753 continue;
21754 #ifdef HAVE_attr_length
21755 else if (get_attr_min_length (insn) == 0)
21756 continue;
21757 #endif
21758 else
21760 /* Assume insn has non-zero length. */
21761 maybe_at_text_label_p = false;
21762 break;
21765 if (maybe_at_text_label_p)
21767 last_start = loc_note;
21768 first_loclabel_num_not_at_text_label = loclabel_num;
21773 if (!var_loc_p)
21775 struct call_arg_loc_node *ca_loc
21776 = ggc_cleared_alloc<call_arg_loc_node> ();
21777 rtx_insn *prev = prev_real_insn (loc_note);
21778 rtx x;
21779 ca_loc->call_arg_loc_note = loc_note;
21780 ca_loc->next = NULL;
21781 ca_loc->label = last_label;
21782 gcc_assert (prev
21783 && (CALL_P (prev)
21784 || (NONJUMP_INSN_P (prev)
21785 && GET_CODE (PATTERN (prev)) == SEQUENCE
21786 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
21787 if (!CALL_P (prev))
21788 prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
21789 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
21790 x = get_call_rtx_from (PATTERN (prev));
21791 if (x)
21793 x = XEXP (XEXP (x, 0), 0);
21794 if (GET_CODE (x) == SYMBOL_REF
21795 && SYMBOL_REF_DECL (x)
21796 && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
21797 ca_loc->symbol_ref = x;
21799 ca_loc->block = insn_scope (prev);
21800 if (call_arg_locations)
21801 call_arg_loc_last->next = ca_loc;
21802 else
21803 call_arg_locations = ca_loc;
21804 call_arg_loc_last = ca_loc;
21806 else if (!NOTE_DURING_CALL_P (loc_note))
21807 newloc->label = last_label;
21808 else
21810 if (!last_postcall_label)
21812 sprintf (loclabel, "%s-1", last_label);
21813 last_postcall_label = ggc_strdup (loclabel);
21815 newloc->label = last_postcall_label;
21818 last_var_location_insn = next_real;
21819 last_in_cold_section_p = in_cold_section_p;
21822 /* Note in one location list that text section has changed. */
21825 var_location_switch_text_section_1 (var_loc_list **slot, void *)
21827 var_loc_list *list = *slot;
21828 if (list->first)
21829 list->last_before_switch
21830 = list->last->next ? list->last->next : list->last;
21831 return 1;
21834 /* Note in all location lists that text section has changed. */
21836 static void
21837 var_location_switch_text_section (void)
21839 if (decl_loc_table == NULL)
21840 return;
21842 decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
21845 /* Create a new line number table. */
21847 static dw_line_info_table *
21848 new_line_info_table (void)
21850 dw_line_info_table *table;
21852 table = ggc_cleared_alloc<dw_line_info_table_struct> ();
21853 table->file_num = 1;
21854 table->line_num = 1;
21855 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
21857 return table;
21860 /* Lookup the "current" table into which we emit line info, so
21861 that we don't have to do it for every source line. */
21863 static void
21864 set_cur_line_info_table (section *sec)
21866 dw_line_info_table *table;
21868 if (sec == text_section)
21869 table = text_section_line_info;
21870 else if (sec == cold_text_section)
21872 table = cold_text_section_line_info;
21873 if (!table)
21875 cold_text_section_line_info = table = new_line_info_table ();
21876 table->end_label = cold_end_label;
21879 else
21881 const char *end_label;
21883 if (flag_reorder_blocks_and_partition)
21885 if (in_cold_section_p)
21886 end_label = crtl->subsections.cold_section_end_label;
21887 else
21888 end_label = crtl->subsections.hot_section_end_label;
21890 else
21892 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21893 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
21894 current_function_funcdef_no);
21895 end_label = ggc_strdup (label);
21898 table = new_line_info_table ();
21899 table->end_label = end_label;
21901 vec_safe_push (separate_line_info, table);
21904 if (DWARF2_ASM_LINE_DEBUG_INFO)
21905 table->is_stmt = (cur_line_info_table
21906 ? cur_line_info_table->is_stmt
21907 : DWARF_LINE_DEFAULT_IS_STMT_START);
21908 cur_line_info_table = table;
21912 /* We need to reset the locations at the beginning of each
21913 function. We can't do this in the end_function hook, because the
21914 declarations that use the locations won't have been output when
21915 that hook is called. Also compute have_multiple_function_sections here. */
21917 static void
21918 dwarf2out_begin_function (tree fun)
21920 section *sec = function_section (fun);
21922 if (sec != text_section)
21923 have_multiple_function_sections = true;
21925 if (flag_reorder_blocks_and_partition && !cold_text_section)
21927 gcc_assert (current_function_decl == fun);
21928 cold_text_section = unlikely_text_section ();
21929 switch_to_section (cold_text_section);
21930 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
21931 switch_to_section (sec);
21934 dwarf2out_note_section_used ();
21935 call_site_count = 0;
21936 tail_call_site_count = 0;
21938 set_cur_line_info_table (sec);
21941 /* Helper function of dwarf2out_end_function, called only after emitting
21942 the very first function into assembly. Check if some .debug_loc range
21943 might end with a .LVL* label that could be equal to .Ltext0.
21944 In that case we must force using absolute addresses in .debug_loc ranges,
21945 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
21946 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
21947 list terminator.
21948 Set have_multiple_function_sections to true in that case and
21949 terminate htab traversal. */
21952 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
21954 var_loc_list *entry = *slot;
21955 struct var_loc_node *node;
21957 node = entry->first;
21958 if (node && node->next && node->next->label)
21960 unsigned int i;
21961 const char *label = node->next->label;
21962 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
21964 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
21966 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
21967 if (strcmp (label, loclabel) == 0)
21969 have_multiple_function_sections = true;
21970 return 0;
21974 return 1;
21977 /* Hook called after emitting a function into assembly.
21978 This does something only for the very first function emitted. */
21980 static void
21981 dwarf2out_end_function (unsigned int)
21983 if (in_first_function_p
21984 && !have_multiple_function_sections
21985 && first_loclabel_num_not_at_text_label
21986 && decl_loc_table)
21987 decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
21988 in_first_function_p = false;
21989 maybe_at_text_label_p = false;
21992 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
21994 static void
21995 push_dw_line_info_entry (dw_line_info_table *table,
21996 enum dw_line_info_opcode opcode, unsigned int val)
21998 dw_line_info_entry e;
21999 e.opcode = opcode;
22000 e.val = val;
22001 vec_safe_push (table->entries, e);
22004 /* Output a label to mark the beginning of a source code line entry
22005 and record information relating to this source line, in
22006 'line_info_table' for later output of the .debug_line section. */
22007 /* ??? The discriminator parameter ought to be unsigned. */
22009 static void
22010 dwarf2out_source_line (unsigned int line, const char *filename,
22011 int discriminator, bool is_stmt)
22013 unsigned int file_num;
22014 dw_line_info_table *table;
22016 if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
22017 return;
22019 /* The discriminator column was added in dwarf4. Simplify the below
22020 by simply removing it if we're not supposed to output it. */
22021 if (dwarf_version < 4 && dwarf_strict)
22022 discriminator = 0;
22024 table = cur_line_info_table;
22025 file_num = maybe_emit_file (lookup_filename (filename));
22027 /* ??? TODO: Elide duplicate line number entries. Traditionally,
22028 the debugger has used the second (possibly duplicate) line number
22029 at the beginning of the function to mark the end of the prologue.
22030 We could eliminate any other duplicates within the function. For
22031 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
22032 that second line number entry. */
22033 /* Recall that this end-of-prologue indication is *not* the same thing
22034 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
22035 to which the hook corresponds, follows the last insn that was
22036 emitted by gen_prologue. What we need is to precede the first insn
22037 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
22038 insn that corresponds to something the user wrote. These may be
22039 very different locations once scheduling is enabled. */
22041 if (0 && file_num == table->file_num
22042 && line == table->line_num
22043 && discriminator == table->discrim_num
22044 && is_stmt == table->is_stmt)
22045 return;
22047 switch_to_section (current_function_section ());
22049 /* If requested, emit something human-readable. */
22050 if (flag_debug_asm)
22051 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
22053 if (DWARF2_ASM_LINE_DEBUG_INFO)
22055 /* Emit the .loc directive understood by GNU as. */
22056 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
22057 file_num, line, is_stmt, discriminator */
22058 fputs ("\t.loc ", asm_out_file);
22059 fprint_ul (asm_out_file, file_num);
22060 putc (' ', asm_out_file);
22061 fprint_ul (asm_out_file, line);
22062 putc (' ', asm_out_file);
22063 putc ('0', asm_out_file);
22065 if (is_stmt != table->is_stmt)
22067 fputs (" is_stmt ", asm_out_file);
22068 putc (is_stmt ? '1' : '0', asm_out_file);
22070 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
22072 gcc_assert (discriminator > 0);
22073 fputs (" discriminator ", asm_out_file);
22074 fprint_ul (asm_out_file, (unsigned long) discriminator);
22076 putc ('\n', asm_out_file);
22078 else
22080 unsigned int label_num = ++line_info_label_num;
22082 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
22084 push_dw_line_info_entry (table, LI_set_address, label_num);
22085 if (file_num != table->file_num)
22086 push_dw_line_info_entry (table, LI_set_file, file_num);
22087 if (discriminator != table->discrim_num)
22088 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
22089 if (is_stmt != table->is_stmt)
22090 push_dw_line_info_entry (table, LI_negate_stmt, 0);
22091 push_dw_line_info_entry (table, LI_set_line, line);
22094 table->file_num = file_num;
22095 table->line_num = line;
22096 table->discrim_num = discriminator;
22097 table->is_stmt = is_stmt;
22098 table->in_use = true;
22101 /* Record the beginning of a new source file. */
22103 static void
22104 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
22106 if (flag_eliminate_dwarf2_dups)
22108 /* Record the beginning of the file for break_out_includes. */
22109 dw_die_ref bincl_die;
22111 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
22112 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
22115 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22117 macinfo_entry e;
22118 e.code = DW_MACINFO_start_file;
22119 e.lineno = lineno;
22120 e.info = ggc_strdup (filename);
22121 vec_safe_push (macinfo_table, e);
22125 /* Record the end of a source file. */
22127 static void
22128 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
22130 if (flag_eliminate_dwarf2_dups)
22131 /* Record the end of the file for break_out_includes. */
22132 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
22134 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22136 macinfo_entry e;
22137 e.code = DW_MACINFO_end_file;
22138 e.lineno = lineno;
22139 e.info = NULL;
22140 vec_safe_push (macinfo_table, e);
22144 /* Called from debug_define in toplev.c. The `buffer' parameter contains
22145 the tail part of the directive line, i.e. the part which is past the
22146 initial whitespace, #, whitespace, directive-name, whitespace part. */
22148 static void
22149 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
22150 const char *buffer ATTRIBUTE_UNUSED)
22152 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22154 macinfo_entry e;
22155 /* Insert a dummy first entry to be able to optimize the whole
22156 predefined macro block using DW_MACRO_GNU_transparent_include. */
22157 if (macinfo_table->is_empty () && lineno <= 1)
22159 e.code = 0;
22160 e.lineno = 0;
22161 e.info = NULL;
22162 vec_safe_push (macinfo_table, e);
22164 e.code = DW_MACINFO_define;
22165 e.lineno = lineno;
22166 e.info = ggc_strdup (buffer);
22167 vec_safe_push (macinfo_table, e);
22171 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
22172 the tail part of the directive line, i.e. the part which is past the
22173 initial whitespace, #, whitespace, directive-name, whitespace part. */
22175 static void
22176 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
22177 const char *buffer ATTRIBUTE_UNUSED)
22179 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22181 macinfo_entry e;
22182 /* Insert a dummy first entry to be able to optimize the whole
22183 predefined macro block using DW_MACRO_GNU_transparent_include. */
22184 if (macinfo_table->is_empty () && lineno <= 1)
22186 e.code = 0;
22187 e.lineno = 0;
22188 e.info = NULL;
22189 vec_safe_push (macinfo_table, e);
22191 e.code = DW_MACINFO_undef;
22192 e.lineno = lineno;
22193 e.info = ggc_strdup (buffer);
22194 vec_safe_push (macinfo_table, e);
22198 /* Helpers to manipulate hash table of CUs. */
22200 struct macinfo_entry_hasher : typed_noop_remove <macinfo_entry>
22202 typedef macinfo_entry value_type;
22203 typedef macinfo_entry compare_type;
22204 static inline hashval_t hash (const value_type *);
22205 static inline bool equal (const value_type *, const compare_type *);
22208 inline hashval_t
22209 macinfo_entry_hasher::hash (const value_type *entry)
22211 return htab_hash_string (entry->info);
22214 inline bool
22215 macinfo_entry_hasher::equal (const value_type *entry1,
22216 const compare_type *entry2)
22218 return !strcmp (entry1->info, entry2->info);
22221 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
22223 /* Output a single .debug_macinfo entry. */
22225 static void
22226 output_macinfo_op (macinfo_entry *ref)
22228 int file_num;
22229 size_t len;
22230 struct indirect_string_node *node;
22231 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22232 struct dwarf_file_data *fd;
22234 switch (ref->code)
22236 case DW_MACINFO_start_file:
22237 fd = lookup_filename (ref->info);
22238 file_num = maybe_emit_file (fd);
22239 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
22240 dw2_asm_output_data_uleb128 (ref->lineno,
22241 "Included from line number %lu",
22242 (unsigned long) ref->lineno);
22243 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
22244 break;
22245 case DW_MACINFO_end_file:
22246 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
22247 break;
22248 case DW_MACINFO_define:
22249 case DW_MACINFO_undef:
22250 len = strlen (ref->info) + 1;
22251 if (!dwarf_strict
22252 && len > DWARF_OFFSET_SIZE
22253 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
22254 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
22256 ref->code = ref->code == DW_MACINFO_define
22257 ? DW_MACRO_GNU_define_indirect
22258 : DW_MACRO_GNU_undef_indirect;
22259 output_macinfo_op (ref);
22260 return;
22262 dw2_asm_output_data (1, ref->code,
22263 ref->code == DW_MACINFO_define
22264 ? "Define macro" : "Undefine macro");
22265 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
22266 (unsigned long) ref->lineno);
22267 dw2_asm_output_nstring (ref->info, -1, "The macro");
22268 break;
22269 case DW_MACRO_GNU_define_indirect:
22270 case DW_MACRO_GNU_undef_indirect:
22271 node = find_AT_string (ref->info);
22272 gcc_assert (node
22273 && ((node->form == DW_FORM_strp)
22274 || (node->form == DW_FORM_GNU_str_index)));
22275 dw2_asm_output_data (1, ref->code,
22276 ref->code == DW_MACRO_GNU_define_indirect
22277 ? "Define macro indirect"
22278 : "Undefine macro indirect");
22279 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
22280 (unsigned long) ref->lineno);
22281 if (node->form == DW_FORM_strp)
22282 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
22283 debug_str_section, "The macro: \"%s\"",
22284 ref->info);
22285 else
22286 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
22287 ref->info);
22288 break;
22289 case DW_MACRO_GNU_transparent_include:
22290 dw2_asm_output_data (1, ref->code, "Transparent include");
22291 ASM_GENERATE_INTERNAL_LABEL (label,
22292 DEBUG_MACRO_SECTION_LABEL, ref->lineno);
22293 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
22294 break;
22295 default:
22296 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
22297 ASM_COMMENT_START, (unsigned long) ref->code);
22298 break;
22302 /* Attempt to make a sequence of define/undef macinfo ops shareable with
22303 other compilation unit .debug_macinfo sections. IDX is the first
22304 index of a define/undef, return the number of ops that should be
22305 emitted in a comdat .debug_macinfo section and emit
22306 a DW_MACRO_GNU_transparent_include entry referencing it.
22307 If the define/undef entry should be emitted normally, return 0. */
22309 static unsigned
22310 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
22311 macinfo_hash_type **macinfo_htab)
22313 macinfo_entry *first, *second, *cur, *inc;
22314 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
22315 unsigned char checksum[16];
22316 struct md5_ctx ctx;
22317 char *grp_name, *tail;
22318 const char *base;
22319 unsigned int i, count, encoded_filename_len, linebuf_len;
22320 macinfo_entry **slot;
22322 first = &(*macinfo_table)[idx];
22323 second = &(*macinfo_table)[idx + 1];
22325 /* Optimize only if there are at least two consecutive define/undef ops,
22326 and either all of them are before first DW_MACINFO_start_file
22327 with lineno {0,1} (i.e. predefined macro block), or all of them are
22328 in some included header file. */
22329 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
22330 return 0;
22331 if (vec_safe_is_empty (files))
22333 if (first->lineno > 1 || second->lineno > 1)
22334 return 0;
22336 else if (first->lineno == 0)
22337 return 0;
22339 /* Find the last define/undef entry that can be grouped together
22340 with first and at the same time compute md5 checksum of their
22341 codes, linenumbers and strings. */
22342 md5_init_ctx (&ctx);
22343 for (i = idx; macinfo_table->iterate (i, &cur); i++)
22344 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
22345 break;
22346 else if (vec_safe_is_empty (files) && cur->lineno > 1)
22347 break;
22348 else
22350 unsigned char code = cur->code;
22351 md5_process_bytes (&code, 1, &ctx);
22352 checksum_uleb128 (cur->lineno, &ctx);
22353 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
22355 md5_finish_ctx (&ctx, checksum);
22356 count = i - idx;
22358 /* From the containing include filename (if any) pick up just
22359 usable characters from its basename. */
22360 if (vec_safe_is_empty (files))
22361 base = "";
22362 else
22363 base = lbasename (files->last ().info);
22364 for (encoded_filename_len = 0, i = 0; base[i]; i++)
22365 if (ISIDNUM (base[i]) || base[i] == '.')
22366 encoded_filename_len++;
22367 /* Count . at the end. */
22368 if (encoded_filename_len)
22369 encoded_filename_len++;
22371 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
22372 linebuf_len = strlen (linebuf);
22374 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
22375 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
22376 + 16 * 2 + 1);
22377 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
22378 tail = grp_name + 4;
22379 if (encoded_filename_len)
22381 for (i = 0; base[i]; i++)
22382 if (ISIDNUM (base[i]) || base[i] == '.')
22383 *tail++ = base[i];
22384 *tail++ = '.';
22386 memcpy (tail, linebuf, linebuf_len);
22387 tail += linebuf_len;
22388 *tail++ = '.';
22389 for (i = 0; i < 16; i++)
22390 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
22392 /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
22393 in the empty vector entry before the first define/undef. */
22394 inc = &(*macinfo_table)[idx - 1];
22395 inc->code = DW_MACRO_GNU_transparent_include;
22396 inc->lineno = 0;
22397 inc->info = ggc_strdup (grp_name);
22398 if (!*macinfo_htab)
22399 *macinfo_htab = new macinfo_hash_type (10);
22400 /* Avoid emitting duplicates. */
22401 slot = (*macinfo_htab)->find_slot (inc, INSERT);
22402 if (*slot != NULL)
22404 inc->code = 0;
22405 inc->info = NULL;
22406 /* If such an entry has been used before, just emit
22407 a DW_MACRO_GNU_transparent_include op. */
22408 inc = *slot;
22409 output_macinfo_op (inc);
22410 /* And clear all macinfo_entry in the range to avoid emitting them
22411 in the second pass. */
22412 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
22414 cur->code = 0;
22415 cur->info = NULL;
22418 else
22420 *slot = inc;
22421 inc->lineno = (*macinfo_htab)->elements ();
22422 output_macinfo_op (inc);
22424 return count;
22427 /* Save any strings needed by the macinfo table in the debug str
22428 table. All strings must be collected into the table by the time
22429 index_string is called. */
22431 static void
22432 save_macinfo_strings (void)
22434 unsigned len;
22435 unsigned i;
22436 macinfo_entry *ref;
22438 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
22440 switch (ref->code)
22442 /* Match the logic in output_macinfo_op to decide on
22443 indirect strings. */
22444 case DW_MACINFO_define:
22445 case DW_MACINFO_undef:
22446 len = strlen (ref->info) + 1;
22447 if (!dwarf_strict
22448 && len > DWARF_OFFSET_SIZE
22449 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
22450 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
22451 set_indirect_string (find_AT_string (ref->info));
22452 break;
22453 case DW_MACRO_GNU_define_indirect:
22454 case DW_MACRO_GNU_undef_indirect:
22455 set_indirect_string (find_AT_string (ref->info));
22456 break;
22457 default:
22458 break;
22463 /* Output macinfo section(s). */
22465 static void
22466 output_macinfo (void)
22468 unsigned i;
22469 unsigned long length = vec_safe_length (macinfo_table);
22470 macinfo_entry *ref;
22471 vec<macinfo_entry, va_gc> *files = NULL;
22472 macinfo_hash_type *macinfo_htab = NULL;
22474 if (! length)
22475 return;
22477 /* output_macinfo* uses these interchangeably. */
22478 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
22479 && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
22480 && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
22481 && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
22483 /* For .debug_macro emit the section header. */
22484 if (!dwarf_strict)
22486 dw2_asm_output_data (2, 4, "DWARF macro version number");
22487 if (DWARF_OFFSET_SIZE == 8)
22488 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
22489 else
22490 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
22491 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
22492 (!dwarf_split_debug_info ? debug_line_section_label
22493 : debug_skeleton_line_section_label),
22494 debug_line_section, NULL);
22497 /* In the first loop, it emits the primary .debug_macinfo section
22498 and after each emitted op the macinfo_entry is cleared.
22499 If a longer range of define/undef ops can be optimized using
22500 DW_MACRO_GNU_transparent_include, the
22501 DW_MACRO_GNU_transparent_include op is emitted and kept in
22502 the vector before the first define/undef in the range and the
22503 whole range of define/undef ops is not emitted and kept. */
22504 for (i = 0; macinfo_table->iterate (i, &ref); i++)
22506 switch (ref->code)
22508 case DW_MACINFO_start_file:
22509 vec_safe_push (files, *ref);
22510 break;
22511 case DW_MACINFO_end_file:
22512 if (!vec_safe_is_empty (files))
22513 files->pop ();
22514 break;
22515 case DW_MACINFO_define:
22516 case DW_MACINFO_undef:
22517 if (!dwarf_strict
22518 && HAVE_COMDAT_GROUP
22519 && vec_safe_length (files) != 1
22520 && i > 0
22521 && i + 1 < length
22522 && (*macinfo_table)[i - 1].code == 0)
22524 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
22525 if (count)
22527 i += count - 1;
22528 continue;
22531 break;
22532 case 0:
22533 /* A dummy entry may be inserted at the beginning to be able
22534 to optimize the whole block of predefined macros. */
22535 if (i == 0)
22536 continue;
22537 default:
22538 break;
22540 output_macinfo_op (ref);
22541 ref->info = NULL;
22542 ref->code = 0;
22545 if (!macinfo_htab)
22546 return;
22548 delete macinfo_htab;
22549 macinfo_htab = NULL;
22551 /* If any DW_MACRO_GNU_transparent_include were used, on those
22552 DW_MACRO_GNU_transparent_include entries terminate the
22553 current chain and switch to a new comdat .debug_macinfo
22554 section and emit the define/undef entries within it. */
22555 for (i = 0; macinfo_table->iterate (i, &ref); i++)
22556 switch (ref->code)
22558 case 0:
22559 continue;
22560 case DW_MACRO_GNU_transparent_include:
22562 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22563 tree comdat_key = get_identifier (ref->info);
22564 /* Terminate the previous .debug_macinfo section. */
22565 dw2_asm_output_data (1, 0, "End compilation unit");
22566 targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
22567 SECTION_DEBUG
22568 | SECTION_LINKONCE,
22569 comdat_key);
22570 ASM_GENERATE_INTERNAL_LABEL (label,
22571 DEBUG_MACRO_SECTION_LABEL,
22572 ref->lineno);
22573 ASM_OUTPUT_LABEL (asm_out_file, label);
22574 ref->code = 0;
22575 ref->info = NULL;
22576 dw2_asm_output_data (2, 4, "DWARF macro version number");
22577 if (DWARF_OFFSET_SIZE == 8)
22578 dw2_asm_output_data (1, 1, "Flags: 64-bit");
22579 else
22580 dw2_asm_output_data (1, 0, "Flags: 32-bit");
22582 break;
22583 case DW_MACINFO_define:
22584 case DW_MACINFO_undef:
22585 output_macinfo_op (ref);
22586 ref->code = 0;
22587 ref->info = NULL;
22588 break;
22589 default:
22590 gcc_unreachable ();
22594 /* Set up for Dwarf output at the start of compilation. */
22596 static void
22597 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
22599 /* Allocate the file_table. */
22600 file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
22602 /* Allocate the decl_die_table. */
22603 decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
22605 /* Allocate the decl_loc_table. */
22606 decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
22608 /* Allocate the cached_dw_loc_list_table. */
22609 cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
22611 /* Allocate the initial hunk of the decl_scope_table. */
22612 vec_alloc (decl_scope_table, 256);
22614 /* Allocate the initial hunk of the abbrev_die_table. */
22615 abbrev_die_table = ggc_cleared_vec_alloc<dw_die_ref>
22616 (ABBREV_DIE_TABLE_INCREMENT);
22617 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
22618 /* Zero-th entry is allocated, but unused. */
22619 abbrev_die_table_in_use = 1;
22621 /* Allocate the pubtypes and pubnames vectors. */
22622 vec_alloc (pubname_table, 32);
22623 vec_alloc (pubtype_table, 32);
22625 vec_alloc (incomplete_types, 64);
22627 vec_alloc (used_rtx_array, 32);
22629 if (!dwarf_split_debug_info)
22631 debug_info_section = get_section (DEBUG_INFO_SECTION,
22632 SECTION_DEBUG, NULL);
22633 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22634 SECTION_DEBUG, NULL);
22635 debug_loc_section = get_section (DEBUG_LOC_SECTION,
22636 SECTION_DEBUG, NULL);
22638 else
22640 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
22641 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22642 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
22643 SECTION_DEBUG | SECTION_EXCLUDE,
22644 NULL);
22645 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
22646 SECTION_DEBUG, NULL);
22647 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
22648 SECTION_DEBUG, NULL);
22649 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22650 SECTION_DEBUG, NULL);
22651 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
22652 DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
22654 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
22655 the main .o, but the skeleton_line goes into the split off dwo. */
22656 debug_skeleton_line_section
22657 = get_section (DEBUG_DWO_LINE_SECTION,
22658 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22659 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
22660 DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
22661 debug_str_offsets_section = get_section (DEBUG_STR_OFFSETS_SECTION,
22662 SECTION_DEBUG | SECTION_EXCLUDE,
22663 NULL);
22664 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
22665 DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
22666 debug_loc_section = get_section (DEBUG_DWO_LOC_SECTION,
22667 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22668 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
22669 DEBUG_STR_DWO_SECTION_FLAGS, NULL);
22671 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
22672 SECTION_DEBUG, NULL);
22673 debug_macinfo_section = get_section (dwarf_strict
22674 ? DEBUG_MACINFO_SECTION
22675 : DEBUG_MACRO_SECTION,
22676 DEBUG_MACRO_SECTION_FLAGS, NULL);
22677 debug_line_section = get_section (DEBUG_LINE_SECTION,
22678 SECTION_DEBUG, NULL);
22679 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
22680 SECTION_DEBUG, NULL);
22681 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
22682 SECTION_DEBUG, NULL);
22683 debug_str_section = get_section (DEBUG_STR_SECTION,
22684 DEBUG_STR_SECTION_FLAGS, NULL);
22685 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
22686 SECTION_DEBUG, NULL);
22687 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
22688 SECTION_DEBUG, NULL);
22690 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
22691 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
22692 DEBUG_ABBREV_SECTION_LABEL, 0);
22693 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
22694 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
22695 COLD_TEXT_SECTION_LABEL, 0);
22696 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
22698 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
22699 DEBUG_INFO_SECTION_LABEL, 0);
22700 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
22701 DEBUG_LINE_SECTION_LABEL, 0);
22702 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
22703 DEBUG_RANGES_SECTION_LABEL, 0);
22704 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
22705 DEBUG_ADDR_SECTION_LABEL, 0);
22706 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
22707 dwarf_strict
22708 ? DEBUG_MACINFO_SECTION_LABEL
22709 : DEBUG_MACRO_SECTION_LABEL, 0);
22710 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
22712 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22713 vec_alloc (macinfo_table, 64);
22715 switch_to_section (text_section);
22716 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
22718 /* Make sure the line number table for .text always exists. */
22719 text_section_line_info = new_line_info_table ();
22720 text_section_line_info->end_label = text_end_label;
22723 /* Called before compile () starts outputtting functions, variables
22724 and toplevel asms into assembly. */
22726 static void
22727 dwarf2out_assembly_start (void)
22729 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
22730 && dwarf2out_do_cfi_asm ()
22731 && (!(flag_unwind_tables || flag_exceptions)
22732 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
22733 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
22736 /* A helper function for dwarf2out_finish called through
22737 htab_traverse. Assign a string its index. All strings must be
22738 collected into the table by the time index_string is called,
22739 because the indexing code relies on htab_traverse to traverse nodes
22740 in the same order for each run. */
22743 index_string (indirect_string_node **h, unsigned int *index)
22745 indirect_string_node *node = *h;
22747 find_string_form (node);
22748 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22750 gcc_assert (node->index == NO_INDEX_ASSIGNED);
22751 node->index = *index;
22752 *index += 1;
22754 return 1;
22757 /* A helper function for output_indirect_strings called through
22758 htab_traverse. Output the offset to a string and update the
22759 current offset. */
22762 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
22764 indirect_string_node *node = *h;
22766 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22768 /* Assert that this node has been assigned an index. */
22769 gcc_assert (node->index != NO_INDEX_ASSIGNED
22770 && node->index != NOT_INDEXED);
22771 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
22772 "indexed string 0x%x: %s", node->index, node->str);
22773 *offset += strlen (node->str) + 1;
22775 return 1;
22778 /* A helper function for dwarf2out_finish called through
22779 htab_traverse. Output the indexed string. */
22782 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
22784 struct indirect_string_node *node = *h;
22786 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22788 /* Assert that the strings are output in the same order as their
22789 indexes were assigned. */
22790 gcc_assert (*cur_idx == node->index);
22791 assemble_string (node->str, strlen (node->str) + 1);
22792 *cur_idx += 1;
22794 return 1;
22797 /* A helper function for dwarf2out_finish called through
22798 htab_traverse. Emit one queued .debug_str string. */
22801 output_indirect_string (indirect_string_node **h, void *)
22803 struct indirect_string_node *node = *h;
22805 node->form = find_string_form (node);
22806 if (node->form == DW_FORM_strp && node->refcount > 0)
22808 ASM_OUTPUT_LABEL (asm_out_file, node->label);
22809 assemble_string (node->str, strlen (node->str) + 1);
22812 return 1;
22815 /* Output the indexed string table. */
22817 static void
22818 output_indirect_strings (void)
22820 switch_to_section (debug_str_section);
22821 if (!dwarf_split_debug_info)
22822 debug_str_hash->traverse<void *, output_indirect_string> (NULL);
22823 else
22825 unsigned int offset = 0;
22826 unsigned int cur_idx = 0;
22828 skeleton_debug_str_hash->traverse<void *, output_indirect_string> (NULL);
22830 switch_to_section (debug_str_offsets_section);
22831 debug_str_hash->traverse_noresize
22832 <unsigned int *, output_index_string_offset> (&offset);
22833 switch_to_section (debug_str_dwo_section);
22834 debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
22835 (&cur_idx);
22839 /* Callback for htab_traverse to assign an index to an entry in the
22840 table, and to write that entry to the .debug_addr section. */
22843 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
22845 addr_table_entry *entry = *slot;
22847 if (entry->refcount == 0)
22849 gcc_assert (entry->index == NO_INDEX_ASSIGNED
22850 || entry->index == NOT_INDEXED);
22851 return 1;
22854 gcc_assert (entry->index == *cur_index);
22855 (*cur_index)++;
22857 switch (entry->kind)
22859 case ate_kind_rtx:
22860 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
22861 "0x%x", entry->index);
22862 break;
22863 case ate_kind_rtx_dtprel:
22864 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
22865 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
22866 DWARF2_ADDR_SIZE,
22867 entry->addr.rtl);
22868 fputc ('\n', asm_out_file);
22869 break;
22870 case ate_kind_label:
22871 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
22872 "0x%x", entry->index);
22873 break;
22874 default:
22875 gcc_unreachable ();
22877 return 1;
22880 /* Produce the .debug_addr section. */
22882 static void
22883 output_addr_table (void)
22885 unsigned int index = 0;
22886 if (addr_index_table == NULL || addr_index_table->size () == 0)
22887 return;
22889 switch_to_section (debug_addr_section);
22890 addr_index_table
22891 ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
22894 #if ENABLE_ASSERT_CHECKING
22895 /* Verify that all marks are clear. */
22897 static void
22898 verify_marks_clear (dw_die_ref die)
22900 dw_die_ref c;
22902 gcc_assert (! die->die_mark);
22903 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
22905 #endif /* ENABLE_ASSERT_CHECKING */
22907 /* Clear the marks for a die and its children.
22908 Be cool if the mark isn't set. */
22910 static void
22911 prune_unmark_dies (dw_die_ref die)
22913 dw_die_ref c;
22915 if (die->die_mark)
22916 die->die_mark = 0;
22917 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
22920 /* Given DIE that we're marking as used, find any other dies
22921 it references as attributes and mark them as used. */
22923 static void
22924 prune_unused_types_walk_attribs (dw_die_ref die)
22926 dw_attr_ref a;
22927 unsigned ix;
22929 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22931 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
22933 /* A reference to another DIE.
22934 Make sure that it will get emitted.
22935 If it was broken out into a comdat group, don't follow it. */
22936 if (! AT_ref (a)->comdat_type_p
22937 || a->dw_attr == DW_AT_specification)
22938 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
22940 /* Set the string's refcount to 0 so that prune_unused_types_mark
22941 accounts properly for it. */
22942 if (AT_class (a) == dw_val_class_str)
22943 a->dw_attr_val.v.val_str->refcount = 0;
22947 /* Mark the generic parameters and arguments children DIEs of DIE. */
22949 static void
22950 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
22952 dw_die_ref c;
22954 if (die == NULL || die->die_child == NULL)
22955 return;
22956 c = die->die_child;
22959 if (is_template_parameter (c))
22960 prune_unused_types_mark (c, 1);
22961 c = c->die_sib;
22962 } while (c && c != die->die_child);
22965 /* Mark DIE as being used. If DOKIDS is true, then walk down
22966 to DIE's children. */
22968 static void
22969 prune_unused_types_mark (dw_die_ref die, int dokids)
22971 dw_die_ref c;
22973 if (die->die_mark == 0)
22975 /* We haven't done this node yet. Mark it as used. */
22976 die->die_mark = 1;
22977 /* If this is the DIE of a generic type instantiation,
22978 mark the children DIEs that describe its generic parms and
22979 args. */
22980 prune_unused_types_mark_generic_parms_dies (die);
22982 /* We also have to mark its parents as used.
22983 (But we don't want to mark our parent's kids due to this,
22984 unless it is a class.) */
22985 if (die->die_parent)
22986 prune_unused_types_mark (die->die_parent,
22987 class_scope_p (die->die_parent));
22989 /* Mark any referenced nodes. */
22990 prune_unused_types_walk_attribs (die);
22992 /* If this node is a specification,
22993 also mark the definition, if it exists. */
22994 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
22995 prune_unused_types_mark (die->die_definition, 1);
22998 if (dokids && die->die_mark != 2)
23000 /* We need to walk the children, but haven't done so yet.
23001 Remember that we've walked the kids. */
23002 die->die_mark = 2;
23004 /* If this is an array type, we need to make sure our
23005 kids get marked, even if they're types. If we're
23006 breaking out types into comdat sections, do this
23007 for all type definitions. */
23008 if (die->die_tag == DW_TAG_array_type
23009 || (use_debug_types
23010 && is_type_die (die) && ! is_declaration_die (die)))
23011 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
23012 else
23013 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
23017 /* For local classes, look if any static member functions were emitted
23018 and if so, mark them. */
23020 static void
23021 prune_unused_types_walk_local_classes (dw_die_ref die)
23023 dw_die_ref c;
23025 if (die->die_mark == 2)
23026 return;
23028 switch (die->die_tag)
23030 case DW_TAG_structure_type:
23031 case DW_TAG_union_type:
23032 case DW_TAG_class_type:
23033 break;
23035 case DW_TAG_subprogram:
23036 if (!get_AT_flag (die, DW_AT_declaration)
23037 || die->die_definition != NULL)
23038 prune_unused_types_mark (die, 1);
23039 return;
23041 default:
23042 return;
23045 /* Mark children. */
23046 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
23049 /* Walk the tree DIE and mark types that we actually use. */
23051 static void
23052 prune_unused_types_walk (dw_die_ref die)
23054 dw_die_ref c;
23056 /* Don't do anything if this node is already marked and
23057 children have been marked as well. */
23058 if (die->die_mark == 2)
23059 return;
23061 switch (die->die_tag)
23063 case DW_TAG_structure_type:
23064 case DW_TAG_union_type:
23065 case DW_TAG_class_type:
23066 if (die->die_perennial_p)
23067 break;
23069 for (c = die->die_parent; c; c = c->die_parent)
23070 if (c->die_tag == DW_TAG_subprogram)
23071 break;
23073 /* Finding used static member functions inside of classes
23074 is needed just for local classes, because for other classes
23075 static member function DIEs with DW_AT_specification
23076 are emitted outside of the DW_TAG_*_type. If we ever change
23077 it, we'd need to call this even for non-local classes. */
23078 if (c)
23079 prune_unused_types_walk_local_classes (die);
23081 /* It's a type node --- don't mark it. */
23082 return;
23084 case DW_TAG_const_type:
23085 case DW_TAG_packed_type:
23086 case DW_TAG_pointer_type:
23087 case DW_TAG_reference_type:
23088 case DW_TAG_rvalue_reference_type:
23089 case DW_TAG_volatile_type:
23090 case DW_TAG_typedef:
23091 case DW_TAG_array_type:
23092 case DW_TAG_interface_type:
23093 case DW_TAG_friend:
23094 case DW_TAG_variant_part:
23095 case DW_TAG_enumeration_type:
23096 case DW_TAG_subroutine_type:
23097 case DW_TAG_string_type:
23098 case DW_TAG_set_type:
23099 case DW_TAG_subrange_type:
23100 case DW_TAG_ptr_to_member_type:
23101 case DW_TAG_file_type:
23102 if (die->die_perennial_p)
23103 break;
23105 /* It's a type node --- don't mark it. */
23106 return;
23108 default:
23109 /* Mark everything else. */
23110 break;
23113 if (die->die_mark == 0)
23115 die->die_mark = 1;
23117 /* Now, mark any dies referenced from here. */
23118 prune_unused_types_walk_attribs (die);
23121 die->die_mark = 2;
23123 /* Mark children. */
23124 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
23127 /* Increment the string counts on strings referred to from DIE's
23128 attributes. */
23130 static void
23131 prune_unused_types_update_strings (dw_die_ref die)
23133 dw_attr_ref a;
23134 unsigned ix;
23136 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23137 if (AT_class (a) == dw_val_class_str)
23139 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
23140 s->refcount++;
23141 /* Avoid unnecessarily putting strings that are used less than
23142 twice in the hash table. */
23143 if (s->refcount
23144 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
23146 indirect_string_node **slot
23147 = debug_str_hash->find_slot_with_hash (s->str,
23148 htab_hash_string (s->str),
23149 INSERT);
23150 gcc_assert (*slot == NULL);
23151 *slot = s;
23156 /* Remove from the tree DIE any dies that aren't marked. */
23158 static void
23159 prune_unused_types_prune (dw_die_ref die)
23161 dw_die_ref c;
23163 gcc_assert (die->die_mark);
23164 prune_unused_types_update_strings (die);
23166 if (! die->die_child)
23167 return;
23169 c = die->die_child;
23170 do {
23171 dw_die_ref prev = c;
23172 for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
23173 if (c == die->die_child)
23175 /* No marked children between 'prev' and the end of the list. */
23176 if (prev == c)
23177 /* No marked children at all. */
23178 die->die_child = NULL;
23179 else
23181 prev->die_sib = c->die_sib;
23182 die->die_child = prev;
23184 return;
23187 if (c != prev->die_sib)
23188 prev->die_sib = c;
23189 prune_unused_types_prune (c);
23190 } while (c != die->die_child);
23193 /* Remove dies representing declarations that we never use. */
23195 static void
23196 prune_unused_types (void)
23198 unsigned int i;
23199 limbo_die_node *node;
23200 comdat_type_node *ctnode;
23201 pubname_ref pub;
23202 dw_die_ref base_type;
23204 #if ENABLE_ASSERT_CHECKING
23205 /* All the marks should already be clear. */
23206 verify_marks_clear (comp_unit_die ());
23207 for (node = limbo_die_list; node; node = node->next)
23208 verify_marks_clear (node->die);
23209 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23210 verify_marks_clear (ctnode->root_die);
23211 #endif /* ENABLE_ASSERT_CHECKING */
23213 /* Mark types that are used in global variables. */
23214 premark_types_used_by_global_vars ();
23216 /* Set the mark on nodes that are actually used. */
23217 prune_unused_types_walk (comp_unit_die ());
23218 for (node = limbo_die_list; node; node = node->next)
23219 prune_unused_types_walk (node->die);
23220 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23222 prune_unused_types_walk (ctnode->root_die);
23223 prune_unused_types_mark (ctnode->type_die, 1);
23226 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
23227 are unusual in that they are pubnames that are the children of pubtypes.
23228 They should only be marked via their parent DW_TAG_enumeration_type die,
23229 not as roots in themselves. */
23230 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
23231 if (pub->die->die_tag != DW_TAG_enumerator)
23232 prune_unused_types_mark (pub->die, 1);
23233 for (i = 0; base_types.iterate (i, &base_type); i++)
23234 prune_unused_types_mark (base_type, 1);
23236 if (debug_str_hash)
23237 debug_str_hash->empty ();
23238 if (skeleton_debug_str_hash)
23239 skeleton_debug_str_hash->empty ();
23240 prune_unused_types_prune (comp_unit_die ());
23241 for (node = limbo_die_list; node; node = node->next)
23242 prune_unused_types_prune (node->die);
23243 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23244 prune_unused_types_prune (ctnode->root_die);
23246 /* Leave the marks clear. */
23247 prune_unmark_dies (comp_unit_die ());
23248 for (node = limbo_die_list; node; node = node->next)
23249 prune_unmark_dies (node->die);
23250 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23251 prune_unmark_dies (ctnode->root_die);
23254 /* Set the parameter to true if there are any relative pathnames in
23255 the file table. */
23257 file_table_relative_p (dwarf_file_data **slot, bool *p)
23259 struct dwarf_file_data *d = *slot;
23260 if (!IS_ABSOLUTE_PATH (d->filename))
23262 *p = true;
23263 return 0;
23265 return 1;
23268 /* Helpers to manipulate hash table of comdat type units. */
23270 struct comdat_type_hasher : typed_noop_remove <comdat_type_node>
23272 typedef comdat_type_node value_type;
23273 typedef comdat_type_node compare_type;
23274 static inline hashval_t hash (const value_type *);
23275 static inline bool equal (const value_type *, const compare_type *);
23278 inline hashval_t
23279 comdat_type_hasher::hash (const value_type *type_node)
23281 hashval_t h;
23282 memcpy (&h, type_node->signature, sizeof (h));
23283 return h;
23286 inline bool
23287 comdat_type_hasher::equal (const value_type *type_node_1,
23288 const compare_type *type_node_2)
23290 return (! memcmp (type_node_1->signature, type_node_2->signature,
23291 DWARF_TYPE_SIGNATURE_SIZE));
23294 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
23295 to the location it would have been added, should we know its
23296 DECL_ASSEMBLER_NAME when we added other attributes. This will
23297 probably improve compactness of debug info, removing equivalent
23298 abbrevs, and hide any differences caused by deferring the
23299 computation of the assembler name, triggered by e.g. PCH. */
23301 static inline void
23302 move_linkage_attr (dw_die_ref die)
23304 unsigned ix = vec_safe_length (die->die_attr);
23305 dw_attr_node linkage = (*die->die_attr)[ix - 1];
23307 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
23308 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
23310 while (--ix > 0)
23312 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
23314 if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
23315 break;
23318 if (ix != vec_safe_length (die->die_attr) - 1)
23320 die->die_attr->pop ();
23321 die->die_attr->quick_insert (ix, linkage);
23325 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
23326 referenced from typed stack ops and count how often they are used. */
23328 static void
23329 mark_base_types (dw_loc_descr_ref loc)
23331 dw_die_ref base_type = NULL;
23333 for (; loc; loc = loc->dw_loc_next)
23335 switch (loc->dw_loc_opc)
23337 case DW_OP_GNU_regval_type:
23338 case DW_OP_GNU_deref_type:
23339 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
23340 break;
23341 case DW_OP_GNU_convert:
23342 case DW_OP_GNU_reinterpret:
23343 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
23344 continue;
23345 /* FALLTHRU */
23346 case DW_OP_GNU_const_type:
23347 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
23348 break;
23349 case DW_OP_GNU_entry_value:
23350 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
23351 continue;
23352 default:
23353 continue;
23355 gcc_assert (base_type->die_parent == comp_unit_die ());
23356 if (base_type->die_mark)
23357 base_type->die_mark++;
23358 else
23360 base_types.safe_push (base_type);
23361 base_type->die_mark = 1;
23366 /* Comparison function for sorting marked base types. */
23368 static int
23369 base_type_cmp (const void *x, const void *y)
23371 dw_die_ref dx = *(const dw_die_ref *) x;
23372 dw_die_ref dy = *(const dw_die_ref *) y;
23373 unsigned int byte_size1, byte_size2;
23374 unsigned int encoding1, encoding2;
23375 if (dx->die_mark > dy->die_mark)
23376 return -1;
23377 if (dx->die_mark < dy->die_mark)
23378 return 1;
23379 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
23380 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
23381 if (byte_size1 < byte_size2)
23382 return 1;
23383 if (byte_size1 > byte_size2)
23384 return -1;
23385 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
23386 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
23387 if (encoding1 < encoding2)
23388 return 1;
23389 if (encoding1 > encoding2)
23390 return -1;
23391 return 0;
23394 /* Move base types marked by mark_base_types as early as possible
23395 in the CU, sorted by decreasing usage count both to make the
23396 uleb128 references as small as possible and to make sure they
23397 will have die_offset already computed by calc_die_sizes when
23398 sizes of typed stack loc ops is computed. */
23400 static void
23401 move_marked_base_types (void)
23403 unsigned int i;
23404 dw_die_ref base_type, die, c;
23406 if (base_types.is_empty ())
23407 return;
23409 /* Sort by decreasing usage count, they will be added again in that
23410 order later on. */
23411 base_types.qsort (base_type_cmp);
23412 die = comp_unit_die ();
23413 c = die->die_child;
23416 dw_die_ref prev = c;
23417 c = c->die_sib;
23418 while (c->die_mark)
23420 remove_child_with_prev (c, prev);
23421 /* As base types got marked, there must be at least
23422 one node other than DW_TAG_base_type. */
23423 gcc_assert (c != c->die_sib);
23424 c = c->die_sib;
23427 while (c != die->die_child);
23428 gcc_assert (die->die_child);
23429 c = die->die_child;
23430 for (i = 0; base_types.iterate (i, &base_type); i++)
23432 base_type->die_mark = 0;
23433 base_type->die_sib = c->die_sib;
23434 c->die_sib = base_type;
23435 c = base_type;
23439 /* Helper function for resolve_addr, attempt to resolve
23440 one CONST_STRING, return true if successful. Similarly verify that
23441 SYMBOL_REFs refer to variables emitted in the current CU. */
23443 static bool
23444 resolve_one_addr (rtx *addr)
23446 rtx rtl = *addr;
23448 if (GET_CODE (rtl) == CONST_STRING)
23450 size_t len = strlen (XSTR (rtl, 0)) + 1;
23451 tree t = build_string (len, XSTR (rtl, 0));
23452 tree tlen = size_int (len - 1);
23453 TREE_TYPE (t)
23454 = build_array_type (char_type_node, build_index_type (tlen));
23455 rtl = lookup_constant_def (t);
23456 if (!rtl || !MEM_P (rtl))
23457 return false;
23458 rtl = XEXP (rtl, 0);
23459 if (GET_CODE (rtl) == SYMBOL_REF
23460 && SYMBOL_REF_DECL (rtl)
23461 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
23462 return false;
23463 vec_safe_push (used_rtx_array, rtl);
23464 *addr = rtl;
23465 return true;
23468 if (GET_CODE (rtl) == SYMBOL_REF
23469 && SYMBOL_REF_DECL (rtl))
23471 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
23473 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
23474 return false;
23476 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
23477 return false;
23480 if (GET_CODE (rtl) == CONST)
23482 subrtx_ptr_iterator::array_type array;
23483 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
23484 if (!resolve_one_addr (*iter))
23485 return false;
23488 return true;
23491 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
23492 if possible, and create DW_TAG_dwarf_procedure that can be referenced
23493 from DW_OP_GNU_implicit_pointer if the string hasn't been seen yet. */
23495 static rtx
23496 string_cst_pool_decl (tree t)
23498 rtx rtl = output_constant_def (t, 1);
23499 unsigned char *array;
23500 dw_loc_descr_ref l;
23501 tree decl;
23502 size_t len;
23503 dw_die_ref ref;
23505 if (!rtl || !MEM_P (rtl))
23506 return NULL_RTX;
23507 rtl = XEXP (rtl, 0);
23508 if (GET_CODE (rtl) != SYMBOL_REF
23509 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
23510 return NULL_RTX;
23512 decl = SYMBOL_REF_DECL (rtl);
23513 if (!lookup_decl_die (decl))
23515 len = TREE_STRING_LENGTH (t);
23516 vec_safe_push (used_rtx_array, rtl);
23517 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
23518 array = ggc_vec_alloc<unsigned char> (len);
23519 memcpy (array, TREE_STRING_POINTER (t), len);
23520 l = new_loc_descr (DW_OP_implicit_value, len, 0);
23521 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
23522 l->dw_loc_oprnd2.v.val_vec.length = len;
23523 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
23524 l->dw_loc_oprnd2.v.val_vec.array = array;
23525 add_AT_loc (ref, DW_AT_location, l);
23526 equate_decl_number_to_die (decl, ref);
23528 return rtl;
23531 /* Helper function of resolve_addr_in_expr. LOC is
23532 a DW_OP_addr followed by DW_OP_stack_value, either at the start
23533 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
23534 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
23535 with DW_OP_GNU_implicit_pointer if possible
23536 and return true, if unsuccessful, return false. */
23538 static bool
23539 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
23541 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
23542 HOST_WIDE_INT offset = 0;
23543 dw_die_ref ref = NULL;
23544 tree decl;
23546 if (GET_CODE (rtl) == CONST
23547 && GET_CODE (XEXP (rtl, 0)) == PLUS
23548 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
23550 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
23551 rtl = XEXP (XEXP (rtl, 0), 0);
23553 if (GET_CODE (rtl) == CONST_STRING)
23555 size_t len = strlen (XSTR (rtl, 0)) + 1;
23556 tree t = build_string (len, XSTR (rtl, 0));
23557 tree tlen = size_int (len - 1);
23559 TREE_TYPE (t)
23560 = build_array_type (char_type_node, build_index_type (tlen));
23561 rtl = string_cst_pool_decl (t);
23562 if (!rtl)
23563 return false;
23565 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
23567 decl = SYMBOL_REF_DECL (rtl);
23568 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
23570 ref = lookup_decl_die (decl);
23571 if (ref && (get_AT (ref, DW_AT_location)
23572 || get_AT (ref, DW_AT_const_value)))
23574 loc->dw_loc_opc = DW_OP_GNU_implicit_pointer;
23575 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23576 loc->dw_loc_oprnd1.val_entry = NULL;
23577 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
23578 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
23579 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
23580 loc->dw_loc_oprnd2.v.val_int = offset;
23581 return true;
23585 return false;
23588 /* Helper function for resolve_addr, handle one location
23589 expression, return false if at least one CONST_STRING or SYMBOL_REF in
23590 the location list couldn't be resolved. */
23592 static bool
23593 resolve_addr_in_expr (dw_loc_descr_ref loc)
23595 dw_loc_descr_ref keep = NULL;
23596 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
23597 switch (loc->dw_loc_opc)
23599 case DW_OP_addr:
23600 if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
23602 if ((prev == NULL
23603 || prev->dw_loc_opc == DW_OP_piece
23604 || prev->dw_loc_opc == DW_OP_bit_piece)
23605 && loc->dw_loc_next
23606 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
23607 && !dwarf_strict
23608 && optimize_one_addr_into_implicit_ptr (loc))
23609 break;
23610 return false;
23612 break;
23613 case DW_OP_GNU_addr_index:
23614 case DW_OP_GNU_const_index:
23615 if (loc->dw_loc_opc == DW_OP_GNU_addr_index
23616 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
23618 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
23619 if (!resolve_one_addr (&rtl))
23620 return false;
23621 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
23622 loc->dw_loc_oprnd1.val_entry =
23623 add_addr_table_entry (rtl, ate_kind_rtx);
23625 break;
23626 case DW_OP_const4u:
23627 case DW_OP_const8u:
23628 if (loc->dtprel
23629 && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
23630 return false;
23631 break;
23632 case DW_OP_plus_uconst:
23633 if (size_of_loc_descr (loc)
23634 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
23636 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
23638 dw_loc_descr_ref repl
23639 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
23640 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
23641 add_loc_descr (&repl, loc->dw_loc_next);
23642 *loc = *repl;
23644 break;
23645 case DW_OP_implicit_value:
23646 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
23647 && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
23648 return false;
23649 break;
23650 case DW_OP_GNU_implicit_pointer:
23651 case DW_OP_GNU_parameter_ref:
23652 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
23654 dw_die_ref ref
23655 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
23656 if (ref == NULL)
23657 return false;
23658 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23659 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
23660 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
23662 break;
23663 case DW_OP_GNU_const_type:
23664 case DW_OP_GNU_regval_type:
23665 case DW_OP_GNU_deref_type:
23666 case DW_OP_GNU_convert:
23667 case DW_OP_GNU_reinterpret:
23668 while (loc->dw_loc_next
23669 && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
23671 dw_die_ref base1, base2;
23672 unsigned enc1, enc2, size1, size2;
23673 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
23674 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
23675 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
23676 else if (loc->dw_loc_oprnd1.val_class
23677 == dw_val_class_unsigned_const)
23678 break;
23679 else
23680 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
23681 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
23682 == dw_val_class_unsigned_const)
23683 break;
23684 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
23685 gcc_assert (base1->die_tag == DW_TAG_base_type
23686 && base2->die_tag == DW_TAG_base_type);
23687 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
23688 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
23689 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
23690 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
23691 if (size1 == size2
23692 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
23693 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
23694 && loc != keep)
23695 || enc1 == enc2))
23697 /* Optimize away next DW_OP_GNU_convert after
23698 adjusting LOC's base type die reference. */
23699 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
23700 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
23701 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
23702 else
23703 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
23704 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
23705 continue;
23707 /* Don't change integer DW_OP_GNU_convert after e.g. floating
23708 point typed stack entry. */
23709 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
23710 keep = loc->dw_loc_next;
23711 break;
23713 break;
23714 default:
23715 break;
23717 return true;
23720 /* Helper function of resolve_addr. DIE had DW_AT_location of
23721 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
23722 and DW_OP_addr couldn't be resolved. resolve_addr has already
23723 removed the DW_AT_location attribute. This function attempts to
23724 add a new DW_AT_location attribute with DW_OP_GNU_implicit_pointer
23725 to it or DW_AT_const_value attribute, if possible. */
23727 static void
23728 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
23730 if (TREE_CODE (decl) != VAR_DECL
23731 || lookup_decl_die (decl) != die
23732 || DECL_EXTERNAL (decl)
23733 || !TREE_STATIC (decl)
23734 || DECL_INITIAL (decl) == NULL_TREE
23735 || DECL_P (DECL_INITIAL (decl))
23736 || get_AT (die, DW_AT_const_value))
23737 return;
23739 tree init = DECL_INITIAL (decl);
23740 HOST_WIDE_INT offset = 0;
23741 /* For variables that have been optimized away and thus
23742 don't have a memory location, see if we can emit
23743 DW_AT_const_value instead. */
23744 if (tree_add_const_value_attribute (die, init))
23745 return;
23746 if (dwarf_strict)
23747 return;
23748 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
23749 and ADDR_EXPR refers to a decl that has DW_AT_location or
23750 DW_AT_const_value (but isn't addressable, otherwise
23751 resolving the original DW_OP_addr wouldn't fail), see if
23752 we can add DW_OP_GNU_implicit_pointer. */
23753 STRIP_NOPS (init);
23754 if (TREE_CODE (init) == POINTER_PLUS_EXPR
23755 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
23757 offset = tree_to_shwi (TREE_OPERAND (init, 1));
23758 init = TREE_OPERAND (init, 0);
23759 STRIP_NOPS (init);
23761 if (TREE_CODE (init) != ADDR_EXPR)
23762 return;
23763 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
23764 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
23765 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
23766 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
23767 && TREE_OPERAND (init, 0) != decl))
23769 dw_die_ref ref;
23770 dw_loc_descr_ref l;
23772 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
23774 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
23775 if (!rtl)
23776 return;
23777 decl = SYMBOL_REF_DECL (rtl);
23779 else
23780 decl = TREE_OPERAND (init, 0);
23781 ref = lookup_decl_die (decl);
23782 if (ref == NULL
23783 || (!get_AT (ref, DW_AT_location)
23784 && !get_AT (ref, DW_AT_const_value)))
23785 return;
23786 l = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
23787 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23788 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
23789 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
23790 add_AT_loc (die, DW_AT_location, l);
23794 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
23795 an address in .rodata section if the string literal is emitted there,
23796 or remove the containing location list or replace DW_AT_const_value
23797 with DW_AT_location and empty location expression, if it isn't found
23798 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
23799 to something that has been emitted in the current CU. */
23801 static void
23802 resolve_addr (dw_die_ref die)
23804 dw_die_ref c;
23805 dw_attr_ref a;
23806 dw_loc_list_ref *curr, *start, loc;
23807 unsigned ix;
23809 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23810 switch (AT_class (a))
23812 case dw_val_class_loc_list:
23813 start = curr = AT_loc_list_ptr (a);
23814 loc = *curr;
23815 gcc_assert (loc);
23816 /* The same list can be referenced more than once. See if we have
23817 already recorded the result from a previous pass. */
23818 if (loc->replaced)
23819 *curr = loc->dw_loc_next;
23820 else if (!loc->resolved_addr)
23822 /* As things stand, we do not expect or allow one die to
23823 reference a suffix of another die's location list chain.
23824 References must be identical or completely separate.
23825 There is therefore no need to cache the result of this
23826 pass on any list other than the first; doing so
23827 would lead to unnecessary writes. */
23828 while (*curr)
23830 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
23831 if (!resolve_addr_in_expr ((*curr)->expr))
23833 dw_loc_list_ref next = (*curr)->dw_loc_next;
23834 dw_loc_descr_ref l = (*curr)->expr;
23836 if (next && (*curr)->ll_symbol)
23838 gcc_assert (!next->ll_symbol);
23839 next->ll_symbol = (*curr)->ll_symbol;
23841 if (dwarf_split_debug_info)
23842 remove_loc_list_addr_table_entries (l);
23843 *curr = next;
23845 else
23847 mark_base_types ((*curr)->expr);
23848 curr = &(*curr)->dw_loc_next;
23851 if (loc == *start)
23852 loc->resolved_addr = 1;
23853 else
23855 loc->replaced = 1;
23856 loc->dw_loc_next = *start;
23859 if (!*start)
23861 remove_AT (die, a->dw_attr);
23862 ix--;
23864 break;
23865 case dw_val_class_loc:
23867 dw_loc_descr_ref l = AT_loc (a);
23868 /* For -gdwarf-2 don't attempt to optimize
23869 DW_AT_data_member_location containing
23870 DW_OP_plus_uconst - older consumers might
23871 rely on it being that op instead of a more complex,
23872 but shorter, location description. */
23873 if ((dwarf_version > 2
23874 || a->dw_attr != DW_AT_data_member_location
23875 || l == NULL
23876 || l->dw_loc_opc != DW_OP_plus_uconst
23877 || l->dw_loc_next != NULL)
23878 && !resolve_addr_in_expr (l))
23880 if (dwarf_split_debug_info)
23881 remove_loc_list_addr_table_entries (l);
23882 if (l != NULL
23883 && l->dw_loc_next == NULL
23884 && l->dw_loc_opc == DW_OP_addr
23885 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
23886 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
23887 && a->dw_attr == DW_AT_location)
23889 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
23890 remove_AT (die, a->dw_attr);
23891 ix--;
23892 optimize_location_into_implicit_ptr (die, decl);
23893 break;
23895 remove_AT (die, a->dw_attr);
23896 ix--;
23898 else
23899 mark_base_types (l);
23901 break;
23902 case dw_val_class_addr:
23903 if (a->dw_attr == DW_AT_const_value
23904 && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
23906 if (AT_index (a) != NOT_INDEXED)
23907 remove_addr_table_entry (a->dw_attr_val.val_entry);
23908 remove_AT (die, a->dw_attr);
23909 ix--;
23911 if (die->die_tag == DW_TAG_GNU_call_site
23912 && a->dw_attr == DW_AT_abstract_origin)
23914 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
23915 dw_die_ref tdie = lookup_decl_die (tdecl);
23916 if (tdie == NULL
23917 && DECL_EXTERNAL (tdecl)
23918 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE)
23920 force_decl_die (tdecl);
23921 tdie = lookup_decl_die (tdecl);
23923 if (tdie)
23925 a->dw_attr_val.val_class = dw_val_class_die_ref;
23926 a->dw_attr_val.v.val_die_ref.die = tdie;
23927 a->dw_attr_val.v.val_die_ref.external = 0;
23929 else
23931 if (AT_index (a) != NOT_INDEXED)
23932 remove_addr_table_entry (a->dw_attr_val.val_entry);
23933 remove_AT (die, a->dw_attr);
23934 ix--;
23937 break;
23938 default:
23939 break;
23942 FOR_EACH_CHILD (die, c, resolve_addr (c));
23945 /* Helper routines for optimize_location_lists.
23946 This pass tries to share identical local lists in .debug_loc
23947 section. */
23949 /* Iteratively hash operands of LOC opcode into HSTATE. */
23951 static void
23952 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
23954 dw_val_ref val1 = &loc->dw_loc_oprnd1;
23955 dw_val_ref val2 = &loc->dw_loc_oprnd2;
23957 switch (loc->dw_loc_opc)
23959 case DW_OP_const4u:
23960 case DW_OP_const8u:
23961 if (loc->dtprel)
23962 goto hash_addr;
23963 /* FALLTHRU */
23964 case DW_OP_const1u:
23965 case DW_OP_const1s:
23966 case DW_OP_const2u:
23967 case DW_OP_const2s:
23968 case DW_OP_const4s:
23969 case DW_OP_const8s:
23970 case DW_OP_constu:
23971 case DW_OP_consts:
23972 case DW_OP_pick:
23973 case DW_OP_plus_uconst:
23974 case DW_OP_breg0:
23975 case DW_OP_breg1:
23976 case DW_OP_breg2:
23977 case DW_OP_breg3:
23978 case DW_OP_breg4:
23979 case DW_OP_breg5:
23980 case DW_OP_breg6:
23981 case DW_OP_breg7:
23982 case DW_OP_breg8:
23983 case DW_OP_breg9:
23984 case DW_OP_breg10:
23985 case DW_OP_breg11:
23986 case DW_OP_breg12:
23987 case DW_OP_breg13:
23988 case DW_OP_breg14:
23989 case DW_OP_breg15:
23990 case DW_OP_breg16:
23991 case DW_OP_breg17:
23992 case DW_OP_breg18:
23993 case DW_OP_breg19:
23994 case DW_OP_breg20:
23995 case DW_OP_breg21:
23996 case DW_OP_breg22:
23997 case DW_OP_breg23:
23998 case DW_OP_breg24:
23999 case DW_OP_breg25:
24000 case DW_OP_breg26:
24001 case DW_OP_breg27:
24002 case DW_OP_breg28:
24003 case DW_OP_breg29:
24004 case DW_OP_breg30:
24005 case DW_OP_breg31:
24006 case DW_OP_regx:
24007 case DW_OP_fbreg:
24008 case DW_OP_piece:
24009 case DW_OP_deref_size:
24010 case DW_OP_xderef_size:
24011 hstate.add_object (val1->v.val_int);
24012 break;
24013 case DW_OP_skip:
24014 case DW_OP_bra:
24016 int offset;
24018 gcc_assert (val1->val_class == dw_val_class_loc);
24019 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
24020 hstate.add_object (offset);
24022 break;
24023 case DW_OP_implicit_value:
24024 hstate.add_object (val1->v.val_unsigned);
24025 switch (val2->val_class)
24027 case dw_val_class_const:
24028 hstate.add_object (val2->v.val_int);
24029 break;
24030 case dw_val_class_vec:
24032 unsigned int elt_size = val2->v.val_vec.elt_size;
24033 unsigned int len = val2->v.val_vec.length;
24035 hstate.add_int (elt_size);
24036 hstate.add_int (len);
24037 hstate.add (val2->v.val_vec.array, len * elt_size);
24039 break;
24040 case dw_val_class_const_double:
24041 hstate.add_object (val2->v.val_double.low);
24042 hstate.add_object (val2->v.val_double.high);
24043 break;
24044 case dw_val_class_wide_int:
24045 hstate.add (val2->v.val_wide->get_val (),
24046 get_full_len (*val2->v.val_wide)
24047 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
24048 break;
24049 case dw_val_class_addr:
24050 inchash::add_rtx (val2->v.val_addr, hstate);
24051 break;
24052 default:
24053 gcc_unreachable ();
24055 break;
24056 case DW_OP_bregx:
24057 case DW_OP_bit_piece:
24058 hstate.add_object (val1->v.val_int);
24059 hstate.add_object (val2->v.val_int);
24060 break;
24061 case DW_OP_addr:
24062 hash_addr:
24063 if (loc->dtprel)
24065 unsigned char dtprel = 0xd1;
24066 hstate.add_object (dtprel);
24068 inchash::add_rtx (val1->v.val_addr, hstate);
24069 break;
24070 case DW_OP_GNU_addr_index:
24071 case DW_OP_GNU_const_index:
24073 if (loc->dtprel)
24075 unsigned char dtprel = 0xd1;
24076 hstate.add_object (dtprel);
24078 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
24080 break;
24081 case DW_OP_GNU_implicit_pointer:
24082 hstate.add_int (val2->v.val_int);
24083 break;
24084 case DW_OP_GNU_entry_value:
24085 hstate.add_object (val1->v.val_loc);
24086 break;
24087 case DW_OP_GNU_regval_type:
24088 case DW_OP_GNU_deref_type:
24090 unsigned int byte_size
24091 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
24092 unsigned int encoding
24093 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
24094 hstate.add_object (val1->v.val_int);
24095 hstate.add_object (byte_size);
24096 hstate.add_object (encoding);
24098 break;
24099 case DW_OP_GNU_convert:
24100 case DW_OP_GNU_reinterpret:
24101 if (val1->val_class == dw_val_class_unsigned_const)
24103 hstate.add_object (val1->v.val_unsigned);
24104 break;
24106 /* FALLTHRU */
24107 case DW_OP_GNU_const_type:
24109 unsigned int byte_size
24110 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
24111 unsigned int encoding
24112 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
24113 hstate.add_object (byte_size);
24114 hstate.add_object (encoding);
24115 if (loc->dw_loc_opc != DW_OP_GNU_const_type)
24116 break;
24117 hstate.add_object (val2->val_class);
24118 switch (val2->val_class)
24120 case dw_val_class_const:
24121 hstate.add_object (val2->v.val_int);
24122 break;
24123 case dw_val_class_vec:
24125 unsigned int elt_size = val2->v.val_vec.elt_size;
24126 unsigned int len = val2->v.val_vec.length;
24128 hstate.add_object (elt_size);
24129 hstate.add_object (len);
24130 hstate.add (val2->v.val_vec.array, len * elt_size);
24132 break;
24133 case dw_val_class_const_double:
24134 hstate.add_object (val2->v.val_double.low);
24135 hstate.add_object (val2->v.val_double.high);
24136 break;
24137 case dw_val_class_wide_int:
24138 hstate.add (val2->v.val_wide->get_val (),
24139 get_full_len (*val2->v.val_wide)
24140 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
24141 break;
24142 default:
24143 gcc_unreachable ();
24146 break;
24148 default:
24149 /* Other codes have no operands. */
24150 break;
24154 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
24156 static inline void
24157 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
24159 dw_loc_descr_ref l;
24160 bool sizes_computed = false;
24161 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
24162 size_of_locs (loc);
24164 for (l = loc; l != NULL; l = l->dw_loc_next)
24166 enum dwarf_location_atom opc = l->dw_loc_opc;
24167 hstate.add_object (opc);
24168 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
24170 size_of_locs (loc);
24171 sizes_computed = true;
24173 hash_loc_operands (l, hstate);
24177 /* Compute hash of the whole location list LIST_HEAD. */
24179 static inline void
24180 hash_loc_list (dw_loc_list_ref list_head)
24182 dw_loc_list_ref curr = list_head;
24183 inchash::hash hstate;
24185 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
24187 hstate.add (curr->begin, strlen (curr->begin) + 1);
24188 hstate.add (curr->end, strlen (curr->end) + 1);
24189 if (curr->section)
24190 hstate.add (curr->section, strlen (curr->section) + 1);
24191 hash_locs (curr->expr, hstate);
24193 list_head->hash = hstate.end ();
24196 /* Return true if X and Y opcodes have the same operands. */
24198 static inline bool
24199 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
24201 dw_val_ref valx1 = &x->dw_loc_oprnd1;
24202 dw_val_ref valx2 = &x->dw_loc_oprnd2;
24203 dw_val_ref valy1 = &y->dw_loc_oprnd1;
24204 dw_val_ref valy2 = &y->dw_loc_oprnd2;
24206 switch (x->dw_loc_opc)
24208 case DW_OP_const4u:
24209 case DW_OP_const8u:
24210 if (x->dtprel)
24211 goto hash_addr;
24212 /* FALLTHRU */
24213 case DW_OP_const1u:
24214 case DW_OP_const1s:
24215 case DW_OP_const2u:
24216 case DW_OP_const2s:
24217 case DW_OP_const4s:
24218 case DW_OP_const8s:
24219 case DW_OP_constu:
24220 case DW_OP_consts:
24221 case DW_OP_pick:
24222 case DW_OP_plus_uconst:
24223 case DW_OP_breg0:
24224 case DW_OP_breg1:
24225 case DW_OP_breg2:
24226 case DW_OP_breg3:
24227 case DW_OP_breg4:
24228 case DW_OP_breg5:
24229 case DW_OP_breg6:
24230 case DW_OP_breg7:
24231 case DW_OP_breg8:
24232 case DW_OP_breg9:
24233 case DW_OP_breg10:
24234 case DW_OP_breg11:
24235 case DW_OP_breg12:
24236 case DW_OP_breg13:
24237 case DW_OP_breg14:
24238 case DW_OP_breg15:
24239 case DW_OP_breg16:
24240 case DW_OP_breg17:
24241 case DW_OP_breg18:
24242 case DW_OP_breg19:
24243 case DW_OP_breg20:
24244 case DW_OP_breg21:
24245 case DW_OP_breg22:
24246 case DW_OP_breg23:
24247 case DW_OP_breg24:
24248 case DW_OP_breg25:
24249 case DW_OP_breg26:
24250 case DW_OP_breg27:
24251 case DW_OP_breg28:
24252 case DW_OP_breg29:
24253 case DW_OP_breg30:
24254 case DW_OP_breg31:
24255 case DW_OP_regx:
24256 case DW_OP_fbreg:
24257 case DW_OP_piece:
24258 case DW_OP_deref_size:
24259 case DW_OP_xderef_size:
24260 return valx1->v.val_int == valy1->v.val_int;
24261 case DW_OP_skip:
24262 case DW_OP_bra:
24263 /* If splitting debug info, the use of DW_OP_GNU_addr_index
24264 can cause irrelevant differences in dw_loc_addr. */
24265 gcc_assert (valx1->val_class == dw_val_class_loc
24266 && valy1->val_class == dw_val_class_loc
24267 && (dwarf_split_debug_info
24268 || x->dw_loc_addr == y->dw_loc_addr));
24269 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
24270 case DW_OP_implicit_value:
24271 if (valx1->v.val_unsigned != valy1->v.val_unsigned
24272 || valx2->val_class != valy2->val_class)
24273 return false;
24274 switch (valx2->val_class)
24276 case dw_val_class_const:
24277 return valx2->v.val_int == valy2->v.val_int;
24278 case dw_val_class_vec:
24279 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
24280 && valx2->v.val_vec.length == valy2->v.val_vec.length
24281 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
24282 valx2->v.val_vec.elt_size
24283 * valx2->v.val_vec.length) == 0;
24284 case dw_val_class_const_double:
24285 return valx2->v.val_double.low == valy2->v.val_double.low
24286 && valx2->v.val_double.high == valy2->v.val_double.high;
24287 case dw_val_class_wide_int:
24288 return *valx2->v.val_wide == *valy2->v.val_wide;
24289 case dw_val_class_addr:
24290 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
24291 default:
24292 gcc_unreachable ();
24294 case DW_OP_bregx:
24295 case DW_OP_bit_piece:
24296 return valx1->v.val_int == valy1->v.val_int
24297 && valx2->v.val_int == valy2->v.val_int;
24298 case DW_OP_addr:
24299 hash_addr:
24300 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
24301 case DW_OP_GNU_addr_index:
24302 case DW_OP_GNU_const_index:
24304 rtx ax1 = valx1->val_entry->addr.rtl;
24305 rtx ay1 = valy1->val_entry->addr.rtl;
24306 return rtx_equal_p (ax1, ay1);
24308 case DW_OP_GNU_implicit_pointer:
24309 return valx1->val_class == dw_val_class_die_ref
24310 && valx1->val_class == valy1->val_class
24311 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
24312 && valx2->v.val_int == valy2->v.val_int;
24313 case DW_OP_GNU_entry_value:
24314 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
24315 case DW_OP_GNU_const_type:
24316 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
24317 || valx2->val_class != valy2->val_class)
24318 return false;
24319 switch (valx2->val_class)
24321 case dw_val_class_const:
24322 return valx2->v.val_int == valy2->v.val_int;
24323 case dw_val_class_vec:
24324 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
24325 && valx2->v.val_vec.length == valy2->v.val_vec.length
24326 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
24327 valx2->v.val_vec.elt_size
24328 * valx2->v.val_vec.length) == 0;
24329 case dw_val_class_const_double:
24330 return valx2->v.val_double.low == valy2->v.val_double.low
24331 && valx2->v.val_double.high == valy2->v.val_double.high;
24332 case dw_val_class_wide_int:
24333 return *valx2->v.val_wide == *valy2->v.val_wide;
24334 default:
24335 gcc_unreachable ();
24337 case DW_OP_GNU_regval_type:
24338 case DW_OP_GNU_deref_type:
24339 return valx1->v.val_int == valy1->v.val_int
24340 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
24341 case DW_OP_GNU_convert:
24342 case DW_OP_GNU_reinterpret:
24343 if (valx1->val_class != valy1->val_class)
24344 return false;
24345 if (valx1->val_class == dw_val_class_unsigned_const)
24346 return valx1->v.val_unsigned == valy1->v.val_unsigned;
24347 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
24348 case DW_OP_GNU_parameter_ref:
24349 return valx1->val_class == dw_val_class_die_ref
24350 && valx1->val_class == valy1->val_class
24351 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
24352 default:
24353 /* Other codes have no operands. */
24354 return true;
24358 /* Return true if DWARF location expressions X and Y are the same. */
24360 static inline bool
24361 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
24363 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
24364 if (x->dw_loc_opc != y->dw_loc_opc
24365 || x->dtprel != y->dtprel
24366 || !compare_loc_operands (x, y))
24367 break;
24368 return x == NULL && y == NULL;
24371 /* Hashtable helpers. */
24373 struct loc_list_hasher : typed_noop_remove <dw_loc_list_struct>
24375 typedef dw_loc_list_struct value_type;
24376 typedef dw_loc_list_struct compare_type;
24377 static inline hashval_t hash (const value_type *);
24378 static inline bool equal (const value_type *, const compare_type *);
24381 /* Return precomputed hash of location list X. */
24383 inline hashval_t
24384 loc_list_hasher::hash (const value_type *x)
24386 return x->hash;
24389 /* Return true if location lists A and B are the same. */
24391 inline bool
24392 loc_list_hasher::equal (const value_type *a, const compare_type *b)
24394 if (a == b)
24395 return 1;
24396 if (a->hash != b->hash)
24397 return 0;
24398 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
24399 if (strcmp (a->begin, b->begin) != 0
24400 || strcmp (a->end, b->end) != 0
24401 || (a->section == NULL) != (b->section == NULL)
24402 || (a->section && strcmp (a->section, b->section) != 0)
24403 || !compare_locs (a->expr, b->expr))
24404 break;
24405 return a == NULL && b == NULL;
24408 typedef hash_table<loc_list_hasher> loc_list_hash_type;
24411 /* Recursively optimize location lists referenced from DIE
24412 children and share them whenever possible. */
24414 static void
24415 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
24417 dw_die_ref c;
24418 dw_attr_ref a;
24419 unsigned ix;
24420 dw_loc_list_struct **slot;
24422 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
24423 if (AT_class (a) == dw_val_class_loc_list)
24425 dw_loc_list_ref list = AT_loc_list (a);
24426 /* TODO: perform some optimizations here, before hashing
24427 it and storing into the hash table. */
24428 hash_loc_list (list);
24429 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
24430 if (*slot == NULL)
24431 *slot = list;
24432 else
24433 a->dw_attr_val.v.val_loc_list = *slot;
24436 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
24440 /* Recursively assign each location list a unique index into the debug_addr
24441 section. */
24443 static void
24444 index_location_lists (dw_die_ref die)
24446 dw_die_ref c;
24447 dw_attr_ref a;
24448 unsigned ix;
24450 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
24451 if (AT_class (a) == dw_val_class_loc_list)
24453 dw_loc_list_ref list = AT_loc_list (a);
24454 dw_loc_list_ref curr;
24455 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
24457 /* Don't index an entry that has already been indexed
24458 or won't be output. */
24459 if (curr->begin_entry != NULL
24460 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
24461 continue;
24463 curr->begin_entry
24464 = add_addr_table_entry (xstrdup (curr->begin),
24465 ate_kind_label);
24469 FOR_EACH_CHILD (die, c, index_location_lists (c));
24472 /* Optimize location lists referenced from DIE
24473 children and share them whenever possible. */
24475 static void
24476 optimize_location_lists (dw_die_ref die)
24478 loc_list_hash_type htab (500);
24479 optimize_location_lists_1 (die, &htab);
24482 /* Output stuff that dwarf requires at the end of every file,
24483 and generate the DWARF-2 debugging info. */
24485 static void
24486 dwarf2out_finish (const char *filename)
24488 limbo_die_node *node, *next_node;
24489 comdat_type_node *ctnode;
24490 unsigned int i;
24491 dw_die_ref main_comp_unit_die;
24493 /* PCH might result in DW_AT_producer string being restored from the
24494 header compilation, so always fill it with empty string initially
24495 and overwrite only here. */
24496 dw_attr_ref producer = get_AT (comp_unit_die (), DW_AT_producer);
24497 producer_string = gen_producer_string ();
24498 producer->dw_attr_val.v.val_str->refcount--;
24499 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
24501 gen_scheduled_generic_parms_dies ();
24502 gen_remaining_tmpl_value_param_die_attribute ();
24504 /* Add the name for the main input file now. We delayed this from
24505 dwarf2out_init to avoid complications with PCH. */
24506 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
24507 if (!IS_ABSOLUTE_PATH (filename) || targetm.force_at_comp_dir)
24508 add_comp_dir_attribute (comp_unit_die ());
24509 else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
24511 bool p = false;
24512 file_table->traverse<bool *, file_table_relative_p> (&p);
24513 if (p)
24514 add_comp_dir_attribute (comp_unit_die ());
24517 if (deferred_locations_list)
24518 for (i = 0; i < deferred_locations_list->length (); i++)
24520 add_location_or_const_value_attribute (
24521 (*deferred_locations_list)[i].die,
24522 (*deferred_locations_list)[i].variable,
24523 false,
24524 DW_AT_location);
24527 /* Traverse the limbo die list, and add parent/child links. The only
24528 dies without parents that should be here are concrete instances of
24529 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
24530 For concrete instances, we can get the parent die from the abstract
24531 instance. */
24532 for (node = limbo_die_list; node; node = next_node)
24534 dw_die_ref die = node->die;
24535 next_node = node->next;
24537 if (die->die_parent == NULL)
24539 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
24541 if (origin && origin->die_parent)
24542 add_child_die (origin->die_parent, die);
24543 else if (is_cu_die (die))
24545 else if (seen_error ())
24546 /* It's OK to be confused by errors in the input. */
24547 add_child_die (comp_unit_die (), die);
24548 else
24550 /* In certain situations, the lexical block containing a
24551 nested function can be optimized away, which results
24552 in the nested function die being orphaned. Likewise
24553 with the return type of that nested function. Force
24554 this to be a child of the containing function.
24556 It may happen that even the containing function got fully
24557 inlined and optimized out. In that case we are lost and
24558 assign the empty child. This should not be big issue as
24559 the function is likely unreachable too. */
24560 gcc_assert (node->created_for);
24562 if (DECL_P (node->created_for))
24563 origin = get_context_die (DECL_CONTEXT (node->created_for));
24564 else if (TYPE_P (node->created_for))
24565 origin = scope_die_for (node->created_for, comp_unit_die ());
24566 else
24567 origin = comp_unit_die ();
24569 add_child_die (origin, die);
24574 limbo_die_list = NULL;
24576 #if ENABLE_ASSERT_CHECKING
24578 dw_die_ref die = comp_unit_die (), c;
24579 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
24581 #endif
24582 resolve_addr (comp_unit_die ());
24583 move_marked_base_types ();
24585 for (node = deferred_asm_name; node; node = node->next)
24587 tree decl = node->created_for;
24588 /* When generating LTO bytecode we can not generate new assembler
24589 names at this point and all important decls got theirs via
24590 free-lang-data. */
24591 if (((!flag_generate_lto && !flag_generate_offload)
24592 || DECL_ASSEMBLER_NAME_SET_P (decl))
24593 && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
24595 add_linkage_attr (node->die, decl);
24596 move_linkage_attr (node->die);
24600 deferred_asm_name = NULL;
24602 /* Walk through the list of incomplete types again, trying once more to
24603 emit full debugging info for them. */
24604 retry_incomplete_types ();
24606 if (flag_eliminate_unused_debug_types)
24607 prune_unused_types ();
24609 /* Generate separate COMDAT sections for type DIEs. */
24610 if (use_debug_types)
24612 break_out_comdat_types (comp_unit_die ());
24614 /* Each new type_unit DIE was added to the limbo die list when created.
24615 Since these have all been added to comdat_type_list, clear the
24616 limbo die list. */
24617 limbo_die_list = NULL;
24619 /* For each new comdat type unit, copy declarations for incomplete
24620 types to make the new unit self-contained (i.e., no direct
24621 references to the main compile unit). */
24622 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24623 copy_decls_for_unworthy_types (ctnode->root_die);
24624 copy_decls_for_unworthy_types (comp_unit_die ());
24626 /* In the process of copying declarations from one unit to another,
24627 we may have left some declarations behind that are no longer
24628 referenced. Prune them. */
24629 prune_unused_types ();
24632 /* Generate separate CUs for each of the include files we've seen.
24633 They will go into limbo_die_list. */
24634 if (flag_eliminate_dwarf2_dups)
24635 break_out_includes (comp_unit_die ());
24637 /* Traverse the DIE's and add add sibling attributes to those DIE's
24638 that have children. */
24639 add_sibling_attributes (comp_unit_die ());
24640 for (node = limbo_die_list; node; node = node->next)
24641 add_sibling_attributes (node->die);
24642 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24643 add_sibling_attributes (ctnode->root_die);
24645 /* When splitting DWARF info, we put some attributes in the
24646 skeleton compile_unit DIE that remains in the .o, while
24647 most attributes go in the DWO compile_unit_die. */
24648 if (dwarf_split_debug_info)
24649 main_comp_unit_die = gen_compile_unit_die (NULL);
24650 else
24651 main_comp_unit_die = comp_unit_die ();
24653 /* Output a terminator label for the .text section. */
24654 switch_to_section (text_section);
24655 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
24656 if (cold_text_section)
24658 switch_to_section (cold_text_section);
24659 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
24662 /* We can only use the low/high_pc attributes if all of the code was
24663 in .text. */
24664 if (!have_multiple_function_sections
24665 || (dwarf_version < 3 && dwarf_strict))
24667 /* Don't add if the CU has no associated code. */
24668 if (text_section_used)
24669 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
24670 text_end_label, true);
24672 else
24674 unsigned fde_idx;
24675 dw_fde_ref fde;
24676 bool range_list_added = false;
24678 if (text_section_used)
24679 add_ranges_by_labels (main_comp_unit_die, text_section_label,
24680 text_end_label, &range_list_added, true);
24681 if (cold_text_section_used)
24682 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
24683 cold_end_label, &range_list_added, true);
24685 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
24687 if (DECL_IGNORED_P (fde->decl))
24688 continue;
24689 if (!fde->in_std_section)
24690 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
24691 fde->dw_fde_end, &range_list_added,
24692 true);
24693 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
24694 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
24695 fde->dw_fde_second_end, &range_list_added,
24696 true);
24699 if (range_list_added)
24701 /* We need to give .debug_loc and .debug_ranges an appropriate
24702 "base address". Use zero so that these addresses become
24703 absolute. Historically, we've emitted the unexpected
24704 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
24705 Emit both to give time for other tools to adapt. */
24706 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
24707 if (! dwarf_strict && dwarf_version < 4)
24708 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
24710 add_ranges (NULL);
24714 if (debug_info_level >= DINFO_LEVEL_TERSE)
24715 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
24716 debug_line_section_label);
24718 if (have_macinfo)
24719 add_AT_macptr (comp_unit_die (),
24720 dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
24721 macinfo_section_label);
24723 if (dwarf_split_debug_info)
24725 /* optimize_location_lists calculates the size of the lists,
24726 so index them first, and assign indices to the entries.
24727 Although optimize_location_lists will remove entries from
24728 the table, it only does so for duplicates, and therefore
24729 only reduces ref_counts to 1. */
24730 index_location_lists (comp_unit_die ());
24732 if (addr_index_table != NULL)
24734 unsigned int index = 0;
24735 addr_index_table
24736 ->traverse_noresize<unsigned int *, index_addr_table_entry>
24737 (&index);
24741 if (have_location_lists)
24742 optimize_location_lists (comp_unit_die ());
24744 save_macinfo_strings ();
24746 if (dwarf_split_debug_info)
24748 unsigned int index = 0;
24750 /* Add attributes common to skeleton compile_units and
24751 type_units. Because these attributes include strings, it
24752 must be done before freezing the string table. Top-level
24753 skeleton die attrs are added when the skeleton type unit is
24754 created, so ensure it is created by this point. */
24755 add_top_level_skeleton_die_attrs (main_comp_unit_die);
24756 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
24759 /* Output all of the compilation units. We put the main one last so that
24760 the offsets are available to output_pubnames. */
24761 for (node = limbo_die_list; node; node = node->next)
24762 output_comp_unit (node->die, 0);
24764 hash_table<comdat_type_hasher> comdat_type_table (100);
24765 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24767 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
24769 /* Don't output duplicate types. */
24770 if (*slot != HTAB_EMPTY_ENTRY)
24771 continue;
24773 /* Add a pointer to the line table for the main compilation unit
24774 so that the debugger can make sense of DW_AT_decl_file
24775 attributes. */
24776 if (debug_info_level >= DINFO_LEVEL_TERSE)
24777 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
24778 (!dwarf_split_debug_info
24779 ? debug_line_section_label
24780 : debug_skeleton_line_section_label));
24782 output_comdat_type_unit (ctnode);
24783 *slot = ctnode;
24786 /* The AT_pubnames attribute needs to go in all skeleton dies, including
24787 both the main_cu and all skeleton TUs. Making this call unconditional
24788 would end up either adding a second copy of the AT_pubnames attribute, or
24789 requiring a special case in add_top_level_skeleton_die_attrs. */
24790 if (!dwarf_split_debug_info)
24791 add_AT_pubnames (comp_unit_die ());
24793 if (dwarf_split_debug_info)
24795 int mark;
24796 unsigned char checksum[16];
24797 struct md5_ctx ctx;
24799 /* Compute a checksum of the comp_unit to use as the dwo_id. */
24800 md5_init_ctx (&ctx);
24801 mark = 0;
24802 die_checksum (comp_unit_die (), &ctx, &mark);
24803 unmark_all_dies (comp_unit_die ());
24804 md5_finish_ctx (&ctx, checksum);
24806 /* Use the first 8 bytes of the checksum as the dwo_id,
24807 and add it to both comp-unit DIEs. */
24808 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
24809 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
24811 /* Add the base offset of the ranges table to the skeleton
24812 comp-unit DIE. */
24813 if (ranges_table_in_use)
24814 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
24815 ranges_section_label);
24817 switch_to_section (debug_addr_section);
24818 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
24819 output_addr_table ();
24822 /* Output the main compilation unit if non-empty or if .debug_macinfo
24823 or .debug_macro will be emitted. */
24824 output_comp_unit (comp_unit_die (), have_macinfo);
24826 if (dwarf_split_debug_info && info_section_emitted)
24827 output_skeleton_debug_sections (main_comp_unit_die);
24829 /* Output the abbreviation table. */
24830 if (abbrev_die_table_in_use != 1)
24832 switch_to_section (debug_abbrev_section);
24833 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
24834 output_abbrev_section ();
24837 /* Output location list section if necessary. */
24838 if (have_location_lists)
24840 /* Output the location lists info. */
24841 switch_to_section (debug_loc_section);
24842 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
24843 output_location_lists (comp_unit_die ());
24846 output_pubtables ();
24848 /* Output the address range information if a CU (.debug_info section)
24849 was emitted. We output an empty table even if we had no functions
24850 to put in it. This because the consumer has no way to tell the
24851 difference between an empty table that we omitted and failure to
24852 generate a table that would have contained data. */
24853 if (info_section_emitted)
24855 unsigned long aranges_length = size_of_aranges ();
24857 switch_to_section (debug_aranges_section);
24858 output_aranges (aranges_length);
24861 /* Output ranges section if necessary. */
24862 if (ranges_table_in_use)
24864 switch_to_section (debug_ranges_section);
24865 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
24866 output_ranges ();
24869 /* Have to end the macro section. */
24870 if (have_macinfo)
24872 switch_to_section (debug_macinfo_section);
24873 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
24874 output_macinfo ();
24875 dw2_asm_output_data (1, 0, "End compilation unit");
24878 /* Output the source line correspondence table. We must do this
24879 even if there is no line information. Otherwise, on an empty
24880 translation unit, we will generate a present, but empty,
24881 .debug_info section. IRIX 6.5 `nm' will then complain when
24882 examining the file. This is done late so that any filenames
24883 used by the debug_info section are marked as 'used'. */
24884 switch_to_section (debug_line_section);
24885 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
24886 if (! DWARF2_ASM_LINE_DEBUG_INFO)
24887 output_line_info (false);
24889 if (dwarf_split_debug_info && info_section_emitted)
24891 switch_to_section (debug_skeleton_line_section);
24892 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
24893 output_line_info (true);
24896 /* If we emitted any indirect strings, output the string table too. */
24897 if (debug_str_hash || skeleton_debug_str_hash)
24898 output_indirect_strings ();
24901 /* Reset all state within dwarf2out.c so that we can rerun the compiler
24902 within the same process. For use by toplev::finalize. */
24904 void
24905 dwarf2out_c_finalize (void)
24907 last_var_location_insn = NULL;
24908 cached_next_real_insn = NULL;
24909 used_rtx_array = NULL;
24910 incomplete_types = NULL;
24911 decl_scope_table = NULL;
24912 debug_info_section = NULL;
24913 debug_skeleton_info_section = NULL;
24914 debug_abbrev_section = NULL;
24915 debug_skeleton_abbrev_section = NULL;
24916 debug_aranges_section = NULL;
24917 debug_addr_section = NULL;
24918 debug_macinfo_section = NULL;
24919 debug_line_section = NULL;
24920 debug_skeleton_line_section = NULL;
24921 debug_loc_section = NULL;
24922 debug_pubnames_section = NULL;
24923 debug_pubtypes_section = NULL;
24924 debug_str_section = NULL;
24925 debug_str_dwo_section = NULL;
24926 debug_str_offsets_section = NULL;
24927 debug_ranges_section = NULL;
24928 debug_frame_section = NULL;
24929 fde_vec = NULL;
24930 debug_str_hash = NULL;
24931 skeleton_debug_str_hash = NULL;
24932 dw2_string_counter = 0;
24933 have_multiple_function_sections = false;
24934 text_section_used = false;
24935 cold_text_section_used = false;
24936 cold_text_section = NULL;
24937 current_unit_personality = NULL;
24939 deferred_locations_list = NULL;
24941 next_die_offset = 0;
24942 single_comp_unit_die = NULL;
24943 comdat_type_list = NULL;
24944 limbo_die_list = NULL;
24945 deferred_asm_name = NULL;
24946 file_table = NULL;
24947 decl_die_table = NULL;
24948 common_block_die_table = NULL;
24949 decl_loc_table = NULL;
24950 call_arg_locations = NULL;
24951 call_arg_loc_last = NULL;
24952 call_site_count = -1;
24953 tail_call_site_count = -1;
24954 //block_map = NULL;
24955 cached_dw_loc_list_table = NULL;
24956 abbrev_die_table = NULL;
24957 abbrev_die_table_allocated = 0;
24958 abbrev_die_table_in_use = 0;
24959 line_info_label_num = 0;
24960 cur_line_info_table = NULL;
24961 text_section_line_info = NULL;
24962 cold_text_section_line_info = NULL;
24963 separate_line_info = NULL;
24964 info_section_emitted = false;
24965 pubname_table = NULL;
24966 pubtype_table = NULL;
24967 macinfo_table = NULL;
24968 ranges_table = NULL;
24969 ranges_table_allocated = 0;
24970 ranges_table_in_use = 0;
24971 ranges_by_label = 0;
24972 ranges_by_label_allocated = 0;
24973 ranges_by_label_in_use = 0;
24974 have_location_lists = false;
24975 loclabel_num = 0;
24976 poc_label_num = 0;
24977 last_emitted_file = NULL;
24978 label_num = 0;
24979 file_table_last_lookup = NULL;
24980 tmpl_value_parm_die_table = NULL;
24981 generic_type_instances = NULL;
24982 frame_pointer_fb_offset = 0;
24983 frame_pointer_fb_offset_valid = false;
24984 base_types.release ();
24985 XDELETEVEC (producer_string);
24986 producer_string = NULL;
24989 #include "gt-dwarf2out.h"