* xvasprintf.c: New file.
[official-gcc.git] / gcc / dwarf2out.c
blob34b327e7514610a70450cdbbb909cc16a17ef3bf
1 /* Output Dwarf2 format symbol table information from GCC.
2 Copyright (C) 1992-2014 Free Software Foundation, Inc.
3 Contributed by Gary Funck (gary@intrepid.com).
4 Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
5 Extensively modified by Jason Merrill (jason@cygnus.com).
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 /* TODO: Emit .debug_line header even when there are no functions, since
24 the file numbers are used by .debug_info. Alternately, leave
25 out locations for types and decls.
26 Avoid talking about ctors and op= for PODs.
27 Factor out common prologue sequences into multiple CIEs. */
29 /* The first part of this file deals with the DWARF 2 frame unwind
30 information, which is also used by the GCC efficient exception handling
31 mechanism. The second part, controlled only by an #ifdef
32 DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
33 information. */
35 /* DWARF2 Abbreviation Glossary:
37 CFA = Canonical Frame Address
38 a fixed address on the stack which identifies a call frame.
39 We define it to be the value of SP just before the call insn.
40 The CFA register and offset, which may change during the course
41 of the function, are used to calculate its value at runtime.
43 CFI = Call Frame Instruction
44 an instruction for the DWARF2 abstract machine
46 CIE = Common Information Entry
47 information describing information common to one or more FDEs
49 DIE = Debugging Information Entry
51 FDE = Frame Description Entry
52 information describing the stack call frame, in particular,
53 how to restore registers
55 DW_CFA_... = DWARF2 CFA call frame instruction
56 DW_TAG_... = DWARF2 DIE tag */
58 #include "config.h"
59 #include "system.h"
60 #include "coretypes.h"
61 #include "tm.h"
62 #include "rtl.h"
63 #include "tree.h"
64 #include "stringpool.h"
65 #include "stor-layout.h"
66 #include "varasm.h"
67 #include "hashtab.h"
68 #include "hash-set.h"
69 #include "vec.h"
70 #include "machmode.h"
71 #include "hard-reg-set.h"
72 #include "input.h"
73 #include "function.h"
74 #include "emit-rtl.h"
75 #include "hash-table.h"
76 #include "version.h"
77 #include "flags.h"
78 #include "regs.h"
79 #include "rtlhash.h"
80 #include "insn-config.h"
81 #include "reload.h"
82 #include "output.h"
83 #include "expr.h"
84 #include "except.h"
85 #include "dwarf2.h"
86 #include "dwarf2out.h"
87 #include "dwarf2asm.h"
88 #include "toplev.h"
89 #include "md5.h"
90 #include "tm_p.h"
91 #include "diagnostic.h"
92 #include "tree-pretty-print.h"
93 #include "debug.h"
94 #include "target.h"
95 #include "common/common-target.h"
96 #include "langhooks.h"
97 #include "hash-map.h"
98 #include "is-a.h"
99 #include "plugin-api.h"
100 #include "ipa-ref.h"
101 #include "cgraph.h"
102 #include "ira.h"
103 #include "lra.h"
104 #include "dumpfile.h"
105 #include "opts.h"
106 #include "tree-dfa.h"
107 #include "gdb/gdb-index.h"
108 #include "rtl-iter.h"
110 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
111 static rtx_insn *last_var_location_insn;
112 static rtx_insn *cached_next_real_insn;
113 static void dwarf2out_decl (tree);
115 #ifdef VMS_DEBUGGING_INFO
116 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
118 /* Define this macro to be a nonzero value if the directory specifications
119 which are output in the debug info should end with a separator. */
120 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
121 /* Define this macro to evaluate to a nonzero value if GCC should refrain
122 from generating indirect strings in DWARF2 debug information, for instance
123 if your target is stuck with an old version of GDB that is unable to
124 process them properly or uses VMS Debug. */
125 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
126 #else
127 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
128 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
129 #endif
131 /* ??? Poison these here until it can be done generically. They've been
132 totally replaced in this file; make sure it stays that way. */
133 #undef DWARF2_UNWIND_INFO
134 #undef DWARF2_FRAME_INFO
135 #if (GCC_VERSION >= 3000)
136 #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
137 #endif
139 /* The size of the target's pointer type. */
140 #ifndef PTR_SIZE
141 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
142 #endif
144 /* Array of RTXes referenced by the debugging information, which therefore
145 must be kept around forever. */
146 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
148 /* A pointer to the base of a list of incomplete types which might be
149 completed at some later time. incomplete_types_list needs to be a
150 vec<tree, va_gc> *because we want to tell the garbage collector about
151 it. */
152 static GTY(()) vec<tree, va_gc> *incomplete_types;
154 /* A pointer to the base of a table of references to declaration
155 scopes. This table is a display which tracks the nesting
156 of declaration scopes at the current scope and containing
157 scopes. This table is used to find the proper place to
158 define type declaration DIE's. */
159 static GTY(()) vec<tree, va_gc> *decl_scope_table;
161 /* Pointers to various DWARF2 sections. */
162 static GTY(()) section *debug_info_section;
163 static GTY(()) section *debug_skeleton_info_section;
164 static GTY(()) section *debug_abbrev_section;
165 static GTY(()) section *debug_skeleton_abbrev_section;
166 static GTY(()) section *debug_aranges_section;
167 static GTY(()) section *debug_addr_section;
168 static GTY(()) section *debug_macinfo_section;
169 static GTY(()) section *debug_line_section;
170 static GTY(()) section *debug_skeleton_line_section;
171 static GTY(()) section *debug_loc_section;
172 static GTY(()) section *debug_pubnames_section;
173 static GTY(()) section *debug_pubtypes_section;
174 static GTY(()) section *debug_str_section;
175 static GTY(()) section *debug_str_dwo_section;
176 static GTY(()) section *debug_str_offsets_section;
177 static GTY(()) section *debug_ranges_section;
178 static GTY(()) section *debug_frame_section;
180 /* Maximum size (in bytes) of an artificially generated label. */
181 #define MAX_ARTIFICIAL_LABEL_BYTES 30
183 /* According to the (draft) DWARF 3 specification, the initial length
184 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
185 bytes are 0xffffffff, followed by the length stored in the next 8
186 bytes.
188 However, the SGI/MIPS ABI uses an initial length which is equal to
189 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
191 #ifndef DWARF_INITIAL_LENGTH_SIZE
192 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
193 #endif
195 /* Round SIZE up to the nearest BOUNDARY. */
196 #define DWARF_ROUND(SIZE,BOUNDARY) \
197 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
199 /* CIE identifier. */
200 #if HOST_BITS_PER_WIDE_INT >= 64
201 #define DWARF_CIE_ID \
202 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
203 #else
204 #define DWARF_CIE_ID DW_CIE_ID
205 #endif
208 /* A vector for a table that contains frame description
209 information for each routine. */
210 #define NOT_INDEXED (-1U)
211 #define NO_INDEX_ASSIGNED (-2U)
213 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
215 struct GTY((for_user)) indirect_string_node {
216 const char *str;
217 unsigned int refcount;
218 enum dwarf_form form;
219 char *label;
220 unsigned int index;
223 struct indirect_string_hasher : ggc_hasher<indirect_string_node *>
225 typedef const char *compare_type;
227 static hashval_t hash (indirect_string_node *);
228 static bool equal (indirect_string_node *, const char *);
231 static GTY (()) hash_table<indirect_string_hasher> *debug_str_hash;
233 /* With split_debug_info, both the comp_dir and dwo_name go in the
234 main object file, rather than the dwo, similar to the force_direct
235 parameter elsewhere but with additional complications:
237 1) The string is needed in both the main object file and the dwo.
238 That is, the comp_dir and dwo_name will appear in both places.
240 2) Strings can use three forms: DW_FORM_string, DW_FORM_strp or
241 DW_FORM_GNU_str_index.
243 3) GCC chooses the form to use late, depending on the size and
244 reference count.
246 Rather than forcing the all debug string handling functions and
247 callers to deal with these complications, simply use a separate,
248 special-cased string table for any attribute that should go in the
249 main object file. This limits the complexity to just the places
250 that need it. */
252 static GTY (()) hash_table<indirect_string_hasher> *skeleton_debug_str_hash;
254 static GTY(()) int dw2_string_counter;
256 /* True if the compilation unit places functions in more than one section. */
257 static GTY(()) bool have_multiple_function_sections = false;
259 /* Whether the default text and cold text sections have been used at all. */
261 static GTY(()) bool text_section_used = false;
262 static GTY(()) bool cold_text_section_used = false;
264 /* The default cold text section. */
265 static GTY(()) section *cold_text_section;
267 /* The DIE for C++14 'auto' in a function return type. */
268 static GTY(()) dw_die_ref auto_die;
270 /* The DIE for C++14 'decltype(auto)' in a function return type. */
271 static GTY(()) dw_die_ref decltype_auto_die;
273 /* Forward declarations for functions defined in this file. */
275 static char *stripattributes (const char *);
276 static void output_call_frame_info (int);
277 static void dwarf2out_note_section_used (void);
279 /* Personality decl of current unit. Used only when assembler does not support
280 personality CFI. */
281 static GTY(()) rtx current_unit_personality;
283 /* Data and reference forms for relocatable data. */
284 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
285 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
287 #ifndef DEBUG_FRAME_SECTION
288 #define DEBUG_FRAME_SECTION ".debug_frame"
289 #endif
291 #ifndef FUNC_BEGIN_LABEL
292 #define FUNC_BEGIN_LABEL "LFB"
293 #endif
295 #ifndef FUNC_END_LABEL
296 #define FUNC_END_LABEL "LFE"
297 #endif
299 #ifndef PROLOGUE_END_LABEL
300 #define PROLOGUE_END_LABEL "LPE"
301 #endif
303 #ifndef EPILOGUE_BEGIN_LABEL
304 #define EPILOGUE_BEGIN_LABEL "LEB"
305 #endif
307 #ifndef FRAME_BEGIN_LABEL
308 #define FRAME_BEGIN_LABEL "Lframe"
309 #endif
310 #define CIE_AFTER_SIZE_LABEL "LSCIE"
311 #define CIE_END_LABEL "LECIE"
312 #define FDE_LABEL "LSFDE"
313 #define FDE_AFTER_SIZE_LABEL "LASFDE"
314 #define FDE_END_LABEL "LEFDE"
315 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
316 #define LINE_NUMBER_END_LABEL "LELT"
317 #define LN_PROLOG_AS_LABEL "LASLTP"
318 #define LN_PROLOG_END_LABEL "LELTP"
319 #define DIE_LABEL_PREFIX "DW"
321 /* Match the base name of a file to the base name of a compilation unit. */
323 static int
324 matches_main_base (const char *path)
326 /* Cache the last query. */
327 static const char *last_path = NULL;
328 static int last_match = 0;
329 if (path != last_path)
331 const char *base;
332 int length = base_of_path (path, &base);
333 last_path = path;
334 last_match = (length == main_input_baselength
335 && memcmp (base, main_input_basename, length) == 0);
337 return last_match;
340 #ifdef DEBUG_DEBUG_STRUCT
342 static int
343 dump_struct_debug (tree type, enum debug_info_usage usage,
344 enum debug_struct_file criterion, int generic,
345 int matches, int result)
347 /* Find the type name. */
348 tree type_decl = TYPE_STUB_DECL (type);
349 tree t = type_decl;
350 const char *name = 0;
351 if (TREE_CODE (t) == TYPE_DECL)
352 t = DECL_NAME (t);
353 if (t)
354 name = IDENTIFIER_POINTER (t);
356 fprintf (stderr, " struct %d %s %s %s %s %d %p %s\n",
357 criterion,
358 DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
359 matches ? "bas" : "hdr",
360 generic ? "gen" : "ord",
361 usage == DINFO_USAGE_DFN ? ";" :
362 usage == DINFO_USAGE_DIR_USE ? "." : "*",
363 result,
364 (void*) type_decl, name);
365 return result;
367 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
368 dump_struct_debug (type, usage, criterion, generic, matches, result)
370 #else
372 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
373 (result)
375 #endif
377 /* Get the number of HOST_WIDE_INTs needed to represent the precision
378 of the number. */
380 static unsigned int
381 get_full_len (const wide_int &op)
383 return ((op.get_precision () + HOST_BITS_PER_WIDE_INT - 1)
384 / HOST_BITS_PER_WIDE_INT);
387 static bool
388 should_emit_struct_debug (tree type, enum debug_info_usage usage)
390 enum debug_struct_file criterion;
391 tree type_decl;
392 bool generic = lang_hooks.types.generic_p (type);
394 if (generic)
395 criterion = debug_struct_generic[usage];
396 else
397 criterion = debug_struct_ordinary[usage];
399 if (criterion == DINFO_STRUCT_FILE_NONE)
400 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
401 if (criterion == DINFO_STRUCT_FILE_ANY)
402 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
404 type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
406 if (type_decl != NULL)
408 if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
409 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
411 if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
412 return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
415 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
418 /* Return a pointer to a copy of the section string name S with all
419 attributes stripped off, and an asterisk prepended (for assemble_name). */
421 static inline char *
422 stripattributes (const char *s)
424 char *stripped = XNEWVEC (char, strlen (s) + 2);
425 char *p = stripped;
427 *p++ = '*';
429 while (*s && *s != ',')
430 *p++ = *s++;
432 *p = '\0';
433 return stripped;
436 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
437 switch to the data section instead, and write out a synthetic start label
438 for collect2 the first time around. */
440 static void
441 switch_to_eh_frame_section (bool back)
443 tree label;
445 #ifdef EH_FRAME_SECTION_NAME
446 if (eh_frame_section == 0)
448 int flags;
450 if (EH_TABLES_CAN_BE_READ_ONLY)
452 int fde_encoding;
453 int per_encoding;
454 int lsda_encoding;
456 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
457 /*global=*/0);
458 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
459 /*global=*/1);
460 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
461 /*global=*/0);
462 flags = ((! flag_pic
463 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
464 && (fde_encoding & 0x70) != DW_EH_PE_aligned
465 && (per_encoding & 0x70) != DW_EH_PE_absptr
466 && (per_encoding & 0x70) != DW_EH_PE_aligned
467 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
468 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
469 ? 0 : SECTION_WRITE);
471 else
472 flags = SECTION_WRITE;
473 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
475 #endif /* EH_FRAME_SECTION_NAME */
477 if (eh_frame_section)
478 switch_to_section (eh_frame_section);
479 else
481 /* We have no special eh_frame section. Put the information in
482 the data section and emit special labels to guide collect2. */
483 switch_to_section (data_section);
485 if (!back)
487 label = get_file_function_name ("F");
488 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
489 targetm.asm_out.globalize_label (asm_out_file,
490 IDENTIFIER_POINTER (label));
491 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
496 /* Switch [BACK] to the eh or debug frame table section, depending on
497 FOR_EH. */
499 static void
500 switch_to_frame_table_section (int for_eh, bool back)
502 if (for_eh)
503 switch_to_eh_frame_section (back);
504 else
506 if (!debug_frame_section)
507 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
508 SECTION_DEBUG, NULL);
509 switch_to_section (debug_frame_section);
513 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
515 enum dw_cfi_oprnd_type
516 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
518 switch (cfi)
520 case DW_CFA_nop:
521 case DW_CFA_GNU_window_save:
522 case DW_CFA_remember_state:
523 case DW_CFA_restore_state:
524 return dw_cfi_oprnd_unused;
526 case DW_CFA_set_loc:
527 case DW_CFA_advance_loc1:
528 case DW_CFA_advance_loc2:
529 case DW_CFA_advance_loc4:
530 case DW_CFA_MIPS_advance_loc8:
531 return dw_cfi_oprnd_addr;
533 case DW_CFA_offset:
534 case DW_CFA_offset_extended:
535 case DW_CFA_def_cfa:
536 case DW_CFA_offset_extended_sf:
537 case DW_CFA_def_cfa_sf:
538 case DW_CFA_restore:
539 case DW_CFA_restore_extended:
540 case DW_CFA_undefined:
541 case DW_CFA_same_value:
542 case DW_CFA_def_cfa_register:
543 case DW_CFA_register:
544 case DW_CFA_expression:
545 return dw_cfi_oprnd_reg_num;
547 case DW_CFA_def_cfa_offset:
548 case DW_CFA_GNU_args_size:
549 case DW_CFA_def_cfa_offset_sf:
550 return dw_cfi_oprnd_offset;
552 case DW_CFA_def_cfa_expression:
553 return dw_cfi_oprnd_loc;
555 default:
556 gcc_unreachable ();
560 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
562 enum dw_cfi_oprnd_type
563 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
565 switch (cfi)
567 case DW_CFA_def_cfa:
568 case DW_CFA_def_cfa_sf:
569 case DW_CFA_offset:
570 case DW_CFA_offset_extended_sf:
571 case DW_CFA_offset_extended:
572 return dw_cfi_oprnd_offset;
574 case DW_CFA_register:
575 return dw_cfi_oprnd_reg_num;
577 case DW_CFA_expression:
578 return dw_cfi_oprnd_loc;
580 default:
581 return dw_cfi_oprnd_unused;
585 /* Output one FDE. */
587 static void
588 output_fde (dw_fde_ref fde, bool for_eh, bool second,
589 char *section_start_label, int fde_encoding, char *augmentation,
590 bool any_lsda_needed, int lsda_encoding)
592 const char *begin, *end;
593 static unsigned int j;
594 char l1[20], l2[20];
596 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
597 /* empty */ 0);
598 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
599 for_eh + j);
600 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
601 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
602 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
603 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
604 " indicating 64-bit DWARF extension");
605 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
606 "FDE Length");
607 ASM_OUTPUT_LABEL (asm_out_file, l1);
609 if (for_eh)
610 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
611 else
612 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
613 debug_frame_section, "FDE CIE offset");
615 begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
616 end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
618 if (for_eh)
620 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
621 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
622 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
623 "FDE initial location");
624 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
625 end, begin, "FDE address range");
627 else
629 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
630 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
633 if (augmentation[0])
635 if (any_lsda_needed)
637 int size = size_of_encoded_value (lsda_encoding);
639 if (lsda_encoding == DW_EH_PE_aligned)
641 int offset = ( 4 /* Length */
642 + 4 /* CIE offset */
643 + 2 * size_of_encoded_value (fde_encoding)
644 + 1 /* Augmentation size */ );
645 int pad = -offset & (PTR_SIZE - 1);
647 size += pad;
648 gcc_assert (size_of_uleb128 (size) == 1);
651 dw2_asm_output_data_uleb128 (size, "Augmentation size");
653 if (fde->uses_eh_lsda)
655 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
656 fde->funcdef_number);
657 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
658 gen_rtx_SYMBOL_REF (Pmode, l1),
659 false,
660 "Language Specific Data Area");
662 else
664 if (lsda_encoding == DW_EH_PE_aligned)
665 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
666 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
667 "Language Specific Data Area (none)");
670 else
671 dw2_asm_output_data_uleb128 (0, "Augmentation size");
674 /* Loop through the Call Frame Instructions associated with this FDE. */
675 fde->dw_fde_current_label = begin;
677 size_t from, until, i;
679 from = 0;
680 until = vec_safe_length (fde->dw_fde_cfi);
682 if (fde->dw_fde_second_begin == NULL)
684 else if (!second)
685 until = fde->dw_fde_switch_cfi_index;
686 else
687 from = fde->dw_fde_switch_cfi_index;
689 for (i = from; i < until; i++)
690 output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
693 /* If we are to emit a ref/link from function bodies to their frame tables,
694 do it now. This is typically performed to make sure that tables
695 associated with functions are dragged with them and not discarded in
696 garbage collecting links. We need to do this on a per function basis to
697 cope with -ffunction-sections. */
699 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
700 /* Switch to the function section, emit the ref to the tables, and
701 switch *back* into the table section. */
702 switch_to_section (function_section (fde->decl));
703 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
704 switch_to_frame_table_section (for_eh, true);
705 #endif
707 /* Pad the FDE out to an address sized boundary. */
708 ASM_OUTPUT_ALIGN (asm_out_file,
709 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
710 ASM_OUTPUT_LABEL (asm_out_file, l2);
712 j += 2;
715 /* Return true if frame description entry FDE is needed for EH. */
717 static bool
718 fde_needed_for_eh_p (dw_fde_ref fde)
720 if (flag_asynchronous_unwind_tables)
721 return true;
723 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
724 return true;
726 if (fde->uses_eh_lsda)
727 return true;
729 /* If exceptions are enabled, we have collected nothrow info. */
730 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
731 return false;
733 return true;
736 /* Output the call frame information used to record information
737 that relates to calculating the frame pointer, and records the
738 location of saved registers. */
740 static void
741 output_call_frame_info (int for_eh)
743 unsigned int i;
744 dw_fde_ref fde;
745 dw_cfi_ref cfi;
746 char l1[20], l2[20], section_start_label[20];
747 bool any_lsda_needed = false;
748 char augmentation[6];
749 int augmentation_size;
750 int fde_encoding = DW_EH_PE_absptr;
751 int per_encoding = DW_EH_PE_absptr;
752 int lsda_encoding = DW_EH_PE_absptr;
753 int return_reg;
754 rtx personality = NULL;
755 int dw_cie_version;
757 /* Don't emit a CIE if there won't be any FDEs. */
758 if (!fde_vec)
759 return;
761 /* Nothing to do if the assembler's doing it all. */
762 if (dwarf2out_do_cfi_asm ())
763 return;
765 /* If we don't have any functions we'll want to unwind out of, don't emit
766 any EH unwind information. If we make FDEs linkonce, we may have to
767 emit an empty label for an FDE that wouldn't otherwise be emitted. We
768 want to avoid having an FDE kept around when the function it refers to
769 is discarded. Example where this matters: a primary function template
770 in C++ requires EH information, an explicit specialization doesn't. */
771 if (for_eh)
773 bool any_eh_needed = false;
775 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
777 if (fde->uses_eh_lsda)
778 any_eh_needed = any_lsda_needed = true;
779 else if (fde_needed_for_eh_p (fde))
780 any_eh_needed = true;
781 else if (TARGET_USES_WEAK_UNWIND_INFO)
782 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
785 if (!any_eh_needed)
786 return;
789 /* We're going to be generating comments, so turn on app. */
790 if (flag_debug_asm)
791 app_enable ();
793 /* Switch to the proper frame section, first time. */
794 switch_to_frame_table_section (for_eh, false);
796 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
797 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
799 /* Output the CIE. */
800 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
801 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
802 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
803 dw2_asm_output_data (4, 0xffffffff,
804 "Initial length escape value indicating 64-bit DWARF extension");
805 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
806 "Length of Common Information Entry");
807 ASM_OUTPUT_LABEL (asm_out_file, l1);
809 /* Now that the CIE pointer is PC-relative for EH,
810 use 0 to identify the CIE. */
811 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
812 (for_eh ? 0 : DWARF_CIE_ID),
813 "CIE Identifier Tag");
815 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
816 use CIE version 1, unless that would produce incorrect results
817 due to overflowing the return register column. */
818 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
819 dw_cie_version = 1;
820 if (return_reg >= 256 || dwarf_version > 2)
821 dw_cie_version = 3;
822 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
824 augmentation[0] = 0;
825 augmentation_size = 0;
827 personality = current_unit_personality;
828 if (for_eh)
830 char *p;
832 /* Augmentation:
833 z Indicates that a uleb128 is present to size the
834 augmentation section.
835 L Indicates the encoding (and thus presence) of
836 an LSDA pointer in the FDE augmentation.
837 R Indicates a non-default pointer encoding for
838 FDE code pointers.
839 P Indicates the presence of an encoding + language
840 personality routine in the CIE augmentation. */
842 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
843 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
844 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
846 p = augmentation + 1;
847 if (personality)
849 *p++ = 'P';
850 augmentation_size += 1 + size_of_encoded_value (per_encoding);
851 assemble_external_libcall (personality);
853 if (any_lsda_needed)
855 *p++ = 'L';
856 augmentation_size += 1;
858 if (fde_encoding != DW_EH_PE_absptr)
860 *p++ = 'R';
861 augmentation_size += 1;
863 if (p > augmentation + 1)
865 augmentation[0] = 'z';
866 *p = '\0';
869 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
870 if (personality && per_encoding == DW_EH_PE_aligned)
872 int offset = ( 4 /* Length */
873 + 4 /* CIE Id */
874 + 1 /* CIE version */
875 + strlen (augmentation) + 1 /* Augmentation */
876 + size_of_uleb128 (1) /* Code alignment */
877 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
878 + 1 /* RA column */
879 + 1 /* Augmentation size */
880 + 1 /* Personality encoding */ );
881 int pad = -offset & (PTR_SIZE - 1);
883 augmentation_size += pad;
885 /* Augmentations should be small, so there's scarce need to
886 iterate for a solution. Die if we exceed one uleb128 byte. */
887 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
891 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
892 if (dw_cie_version >= 4)
894 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
895 dw2_asm_output_data (1, 0, "CIE Segment Size");
897 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
898 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
899 "CIE Data Alignment Factor");
901 if (dw_cie_version == 1)
902 dw2_asm_output_data (1, return_reg, "CIE RA Column");
903 else
904 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
906 if (augmentation[0])
908 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
909 if (personality)
911 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
912 eh_data_format_name (per_encoding));
913 dw2_asm_output_encoded_addr_rtx (per_encoding,
914 personality,
915 true, NULL);
918 if (any_lsda_needed)
919 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
920 eh_data_format_name (lsda_encoding));
922 if (fde_encoding != DW_EH_PE_absptr)
923 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
924 eh_data_format_name (fde_encoding));
927 FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
928 output_cfi (cfi, NULL, for_eh);
930 /* Pad the CIE out to an address sized boundary. */
931 ASM_OUTPUT_ALIGN (asm_out_file,
932 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
933 ASM_OUTPUT_LABEL (asm_out_file, l2);
935 /* Loop through all of the FDE's. */
936 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
938 unsigned int k;
940 /* Don't emit EH unwind info for leaf functions that don't need it. */
941 if (for_eh && !fde_needed_for_eh_p (fde))
942 continue;
944 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
945 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
946 augmentation, any_lsda_needed, lsda_encoding);
949 if (for_eh && targetm.terminate_dw2_eh_frame_info)
950 dw2_asm_output_data (4, 0, "End of Table");
952 /* Turn off app to make assembly quicker. */
953 if (flag_debug_asm)
954 app_disable ();
957 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
959 static void
960 dwarf2out_do_cfi_startproc (bool second)
962 int enc;
963 rtx ref;
964 rtx personality = get_personality_function (current_function_decl);
966 fprintf (asm_out_file, "\t.cfi_startproc\n");
968 if (personality)
970 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
971 ref = personality;
973 /* ??? The GAS support isn't entirely consistent. We have to
974 handle indirect support ourselves, but PC-relative is done
975 in the assembler. Further, the assembler can't handle any
976 of the weirder relocation types. */
977 if (enc & DW_EH_PE_indirect)
978 ref = dw2_force_const_mem (ref, true);
980 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
981 output_addr_const (asm_out_file, ref);
982 fputc ('\n', asm_out_file);
985 if (crtl->uses_eh_lsda)
987 char lab[20];
989 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
990 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
991 current_function_funcdef_no);
992 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
993 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
995 if (enc & DW_EH_PE_indirect)
996 ref = dw2_force_const_mem (ref, true);
998 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
999 output_addr_const (asm_out_file, ref);
1000 fputc ('\n', asm_out_file);
1004 /* Allocate CURRENT_FDE. Immediately initialize all we can, noting that
1005 this allocation may be done before pass_final. */
1007 dw_fde_ref
1008 dwarf2out_alloc_current_fde (void)
1010 dw_fde_ref fde;
1012 fde = ggc_cleared_alloc<dw_fde_node> ();
1013 fde->decl = current_function_decl;
1014 fde->funcdef_number = current_function_funcdef_no;
1015 fde->fde_index = vec_safe_length (fde_vec);
1016 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
1017 fde->uses_eh_lsda = crtl->uses_eh_lsda;
1018 fde->nothrow = crtl->nothrow;
1019 fde->drap_reg = INVALID_REGNUM;
1020 fde->vdrap_reg = INVALID_REGNUM;
1022 /* Record the FDE associated with this function. */
1023 cfun->fde = fde;
1024 vec_safe_push (fde_vec, fde);
1026 return fde;
1029 /* Output a marker (i.e. a label) for the beginning of a function, before
1030 the prologue. */
1032 void
1033 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1034 const char *file ATTRIBUTE_UNUSED)
1036 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1037 char * dup_label;
1038 dw_fde_ref fde;
1039 section *fnsec;
1040 bool do_frame;
1042 current_function_func_begin_label = NULL;
1044 do_frame = dwarf2out_do_frame ();
1046 /* ??? current_function_func_begin_label is also used by except.c for
1047 call-site information. We must emit this label if it might be used. */
1048 if (!do_frame
1049 && (!flag_exceptions
1050 || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1051 return;
1053 fnsec = function_section (current_function_decl);
1054 switch_to_section (fnsec);
1055 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1056 current_function_funcdef_no);
1057 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1058 current_function_funcdef_no);
1059 dup_label = xstrdup (label);
1060 current_function_func_begin_label = dup_label;
1062 /* We can elide the fde allocation if we're not emitting debug info. */
1063 if (!do_frame)
1064 return;
1066 /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1067 emit insns as rtx but bypass the bulk of rest_of_compilation, which
1068 would include pass_dwarf2_frame. If we've not created the FDE yet,
1069 do so now. */
1070 fde = cfun->fde;
1071 if (fde == NULL)
1072 fde = dwarf2out_alloc_current_fde ();
1074 /* Initialize the bits of CURRENT_FDE that were not available earlier. */
1075 fde->dw_fde_begin = dup_label;
1076 fde->dw_fde_current_label = dup_label;
1077 fde->in_std_section = (fnsec == text_section
1078 || (cold_text_section && fnsec == cold_text_section));
1080 /* We only want to output line number information for the genuine dwarf2
1081 prologue case, not the eh frame case. */
1082 #ifdef DWARF2_DEBUGGING_INFO
1083 if (file)
1084 dwarf2out_source_line (line, file, 0, true);
1085 #endif
1087 if (dwarf2out_do_cfi_asm ())
1088 dwarf2out_do_cfi_startproc (false);
1089 else
1091 rtx personality = get_personality_function (current_function_decl);
1092 if (!current_unit_personality)
1093 current_unit_personality = personality;
1095 /* We cannot keep a current personality per function as without CFI
1096 asm, at the point where we emit the CFI data, there is no current
1097 function anymore. */
1098 if (personality && current_unit_personality != personality)
1099 sorry ("multiple EH personalities are supported only with assemblers "
1100 "supporting .cfi_personality directive");
1104 /* Output a marker (i.e. a label) for the end of the generated code
1105 for a function prologue. This gets called *after* the prologue code has
1106 been generated. */
1108 void
1109 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1110 const char *file ATTRIBUTE_UNUSED)
1112 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1114 /* Output a label to mark the endpoint of the code generated for this
1115 function. */
1116 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1117 current_function_funcdef_no);
1118 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1119 current_function_funcdef_no);
1120 cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1123 /* Output a marker (i.e. a label) for the beginning of the generated code
1124 for a function epilogue. This gets called *before* the prologue code has
1125 been generated. */
1127 void
1128 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1129 const char *file ATTRIBUTE_UNUSED)
1131 dw_fde_ref fde = cfun->fde;
1132 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1134 if (fde->dw_fde_vms_begin_epilogue)
1135 return;
1137 /* Output a label to mark the endpoint of the code generated for this
1138 function. */
1139 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1140 current_function_funcdef_no);
1141 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1142 current_function_funcdef_no);
1143 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1146 /* Output a marker (i.e. a label) for the absolute end of the generated code
1147 for a function definition. This gets called *after* the epilogue code has
1148 been generated. */
1150 void
1151 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1152 const char *file ATTRIBUTE_UNUSED)
1154 dw_fde_ref fde;
1155 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1157 last_var_location_insn = NULL;
1158 cached_next_real_insn = NULL;
1160 if (dwarf2out_do_cfi_asm ())
1161 fprintf (asm_out_file, "\t.cfi_endproc\n");
1163 /* Output a label to mark the endpoint of the code generated for this
1164 function. */
1165 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1166 current_function_funcdef_no);
1167 ASM_OUTPUT_LABEL (asm_out_file, label);
1168 fde = cfun->fde;
1169 gcc_assert (fde != NULL);
1170 if (fde->dw_fde_second_begin == NULL)
1171 fde->dw_fde_end = xstrdup (label);
1174 void
1175 dwarf2out_frame_finish (void)
1177 /* Output call frame information. */
1178 if (targetm.debug_unwind_info () == UI_DWARF2)
1179 output_call_frame_info (0);
1181 /* Output another copy for the unwinder. */
1182 if ((flag_unwind_tables || flag_exceptions)
1183 && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1184 output_call_frame_info (1);
1187 /* Note that the current function section is being used for code. */
1189 static void
1190 dwarf2out_note_section_used (void)
1192 section *sec = current_function_section ();
1193 if (sec == text_section)
1194 text_section_used = true;
1195 else if (sec == cold_text_section)
1196 cold_text_section_used = true;
1199 static void var_location_switch_text_section (void);
1200 static void set_cur_line_info_table (section *);
1202 void
1203 dwarf2out_switch_text_section (void)
1205 section *sect;
1206 dw_fde_ref fde = cfun->fde;
1208 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1210 if (!in_cold_section_p)
1212 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1213 fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1214 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1216 else
1218 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1219 fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1220 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1222 have_multiple_function_sections = true;
1224 /* There is no need to mark used sections when not debugging. */
1225 if (cold_text_section != NULL)
1226 dwarf2out_note_section_used ();
1228 if (dwarf2out_do_cfi_asm ())
1229 fprintf (asm_out_file, "\t.cfi_endproc\n");
1231 /* Now do the real section switch. */
1232 sect = current_function_section ();
1233 switch_to_section (sect);
1235 fde->second_in_std_section
1236 = (sect == text_section
1237 || (cold_text_section && sect == cold_text_section));
1239 if (dwarf2out_do_cfi_asm ())
1240 dwarf2out_do_cfi_startproc (true);
1242 var_location_switch_text_section ();
1244 if (cold_text_section != NULL)
1245 set_cur_line_info_table (sect);
1248 /* And now, the subset of the debugging information support code necessary
1249 for emitting location expressions. */
1251 /* Data about a single source file. */
1252 struct GTY((for_user)) dwarf_file_data {
1253 const char * filename;
1254 int emitted_number;
1257 typedef struct GTY(()) deferred_locations_struct
1259 tree variable;
1260 dw_die_ref die;
1261 } deferred_locations;
1264 static GTY(()) vec<deferred_locations, va_gc> *deferred_locations_list;
1267 /* Describe an entry into the .debug_addr section. */
1269 enum ate_kind {
1270 ate_kind_rtx,
1271 ate_kind_rtx_dtprel,
1272 ate_kind_label
1275 typedef struct GTY((for_user)) addr_table_entry_struct {
1276 enum ate_kind kind;
1277 unsigned int refcount;
1278 unsigned int index;
1279 union addr_table_entry_struct_union
1281 rtx GTY ((tag ("0"))) rtl;
1282 char * GTY ((tag ("1"))) label;
1284 GTY ((desc ("%1.kind"))) addr;
1286 addr_table_entry;
1288 /* Location lists are ranges + location descriptions for that range,
1289 so you can track variables that are in different places over
1290 their entire life. */
1291 typedef struct GTY(()) dw_loc_list_struct {
1292 dw_loc_list_ref dw_loc_next;
1293 const char *begin; /* Label and addr_entry for start of range */
1294 addr_table_entry *begin_entry;
1295 const char *end; /* Label for end of range */
1296 char *ll_symbol; /* Label for beginning of location list.
1297 Only on head of list */
1298 const char *section; /* Section this loclist is relative to */
1299 dw_loc_descr_ref expr;
1300 hashval_t hash;
1301 /* True if all addresses in this and subsequent lists are known to be
1302 resolved. */
1303 bool resolved_addr;
1304 /* True if this list has been replaced by dw_loc_next. */
1305 bool replaced;
1306 bool emitted;
1307 /* True if the range should be emitted even if begin and end
1308 are the same. */
1309 bool force;
1310 } dw_loc_list_node;
1312 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1314 /* Convert a DWARF stack opcode into its string name. */
1316 static const char *
1317 dwarf_stack_op_name (unsigned int op)
1319 const char *name = get_DW_OP_name (op);
1321 if (name != NULL)
1322 return name;
1324 return "OP_<unknown>";
1327 /* Return a pointer to a newly allocated location description. Location
1328 descriptions are simple expression terms that can be strung
1329 together to form more complicated location (address) descriptions. */
1331 static inline dw_loc_descr_ref
1332 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1333 unsigned HOST_WIDE_INT oprnd2)
1335 dw_loc_descr_ref descr = ggc_cleared_alloc<dw_loc_descr_node> ();
1337 descr->dw_loc_opc = op;
1338 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1339 descr->dw_loc_oprnd1.val_entry = NULL;
1340 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1341 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1342 descr->dw_loc_oprnd2.val_entry = NULL;
1343 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1345 return descr;
1348 /* Return a pointer to a newly allocated location description for
1349 REG and OFFSET. */
1351 static inline dw_loc_descr_ref
1352 new_reg_loc_descr (unsigned int reg, unsigned HOST_WIDE_INT offset)
1354 if (reg <= 31)
1355 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1356 offset, 0);
1357 else
1358 return new_loc_descr (DW_OP_bregx, reg, offset);
1361 /* Add a location description term to a location description expression. */
1363 static inline void
1364 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1366 dw_loc_descr_ref *d;
1368 /* Find the end of the chain. */
1369 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1372 *d = descr;
1375 /* Compare two location operands for exact equality. */
1377 static bool
1378 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1380 if (a->val_class != b->val_class)
1381 return false;
1382 switch (a->val_class)
1384 case dw_val_class_none:
1385 return true;
1386 case dw_val_class_addr:
1387 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1389 case dw_val_class_offset:
1390 case dw_val_class_unsigned_const:
1391 case dw_val_class_const:
1392 case dw_val_class_range_list:
1393 case dw_val_class_lineptr:
1394 case dw_val_class_macptr:
1395 /* These are all HOST_WIDE_INT, signed or unsigned. */
1396 return a->v.val_unsigned == b->v.val_unsigned;
1398 case dw_val_class_loc:
1399 return a->v.val_loc == b->v.val_loc;
1400 case dw_val_class_loc_list:
1401 return a->v.val_loc_list == b->v.val_loc_list;
1402 case dw_val_class_die_ref:
1403 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1404 case dw_val_class_fde_ref:
1405 return a->v.val_fde_index == b->v.val_fde_index;
1406 case dw_val_class_lbl_id:
1407 case dw_val_class_high_pc:
1408 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1409 case dw_val_class_str:
1410 return a->v.val_str == b->v.val_str;
1411 case dw_val_class_flag:
1412 return a->v.val_flag == b->v.val_flag;
1413 case dw_val_class_file:
1414 return a->v.val_file == b->v.val_file;
1415 case dw_val_class_decl_ref:
1416 return a->v.val_decl_ref == b->v.val_decl_ref;
1418 case dw_val_class_const_double:
1419 return (a->v.val_double.high == b->v.val_double.high
1420 && a->v.val_double.low == b->v.val_double.low);
1422 case dw_val_class_wide_int:
1423 return *a->v.val_wide == *b->v.val_wide;
1425 case dw_val_class_vec:
1427 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1428 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1430 return (a_len == b_len
1431 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1434 case dw_val_class_data8:
1435 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1437 case dw_val_class_vms_delta:
1438 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1439 && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1441 gcc_unreachable ();
1444 /* Compare two location atoms for exact equality. */
1446 static bool
1447 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1449 if (a->dw_loc_opc != b->dw_loc_opc)
1450 return false;
1452 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1453 address size, but since we always allocate cleared storage it
1454 should be zero for other types of locations. */
1455 if (a->dtprel != b->dtprel)
1456 return false;
1458 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1459 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1462 /* Compare two complete location expressions for exact equality. */
1464 bool
1465 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1467 while (1)
1469 if (a == b)
1470 return true;
1471 if (a == NULL || b == NULL)
1472 return false;
1473 if (!loc_descr_equal_p_1 (a, b))
1474 return false;
1476 a = a->dw_loc_next;
1477 b = b->dw_loc_next;
1482 /* Add a constant OFFSET to a location expression. */
1484 static void
1485 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1487 dw_loc_descr_ref loc;
1488 HOST_WIDE_INT *p;
1490 gcc_assert (*list_head != NULL);
1492 if (!offset)
1493 return;
1495 /* Find the end of the chain. */
1496 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1499 p = NULL;
1500 if (loc->dw_loc_opc == DW_OP_fbreg
1501 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1502 p = &loc->dw_loc_oprnd1.v.val_int;
1503 else if (loc->dw_loc_opc == DW_OP_bregx)
1504 p = &loc->dw_loc_oprnd2.v.val_int;
1506 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1507 offset. Don't optimize if an signed integer overflow would happen. */
1508 if (p != NULL
1509 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1510 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1511 *p += offset;
1513 else if (offset > 0)
1514 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1516 else
1518 loc->dw_loc_next = int_loc_descriptor (-offset);
1519 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1523 /* Add a constant OFFSET to a location list. */
1525 static void
1526 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1528 dw_loc_list_ref d;
1529 for (d = list_head; d != NULL; d = d->dw_loc_next)
1530 loc_descr_plus_const (&d->expr, offset);
1533 #define DWARF_REF_SIZE \
1534 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1536 static unsigned long int get_base_type_offset (dw_die_ref);
1538 /* Return the size of a location descriptor. */
1540 static unsigned long
1541 size_of_loc_descr (dw_loc_descr_ref loc)
1543 unsigned long size = 1;
1545 switch (loc->dw_loc_opc)
1547 case DW_OP_addr:
1548 size += DWARF2_ADDR_SIZE;
1549 break;
1550 case DW_OP_GNU_addr_index:
1551 case DW_OP_GNU_const_index:
1552 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1553 size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1554 break;
1555 case DW_OP_const1u:
1556 case DW_OP_const1s:
1557 size += 1;
1558 break;
1559 case DW_OP_const2u:
1560 case DW_OP_const2s:
1561 size += 2;
1562 break;
1563 case DW_OP_const4u:
1564 case DW_OP_const4s:
1565 size += 4;
1566 break;
1567 case DW_OP_const8u:
1568 case DW_OP_const8s:
1569 size += 8;
1570 break;
1571 case DW_OP_constu:
1572 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1573 break;
1574 case DW_OP_consts:
1575 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1576 break;
1577 case DW_OP_pick:
1578 size += 1;
1579 break;
1580 case DW_OP_plus_uconst:
1581 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1582 break;
1583 case DW_OP_skip:
1584 case DW_OP_bra:
1585 size += 2;
1586 break;
1587 case DW_OP_breg0:
1588 case DW_OP_breg1:
1589 case DW_OP_breg2:
1590 case DW_OP_breg3:
1591 case DW_OP_breg4:
1592 case DW_OP_breg5:
1593 case DW_OP_breg6:
1594 case DW_OP_breg7:
1595 case DW_OP_breg8:
1596 case DW_OP_breg9:
1597 case DW_OP_breg10:
1598 case DW_OP_breg11:
1599 case DW_OP_breg12:
1600 case DW_OP_breg13:
1601 case DW_OP_breg14:
1602 case DW_OP_breg15:
1603 case DW_OP_breg16:
1604 case DW_OP_breg17:
1605 case DW_OP_breg18:
1606 case DW_OP_breg19:
1607 case DW_OP_breg20:
1608 case DW_OP_breg21:
1609 case DW_OP_breg22:
1610 case DW_OP_breg23:
1611 case DW_OP_breg24:
1612 case DW_OP_breg25:
1613 case DW_OP_breg26:
1614 case DW_OP_breg27:
1615 case DW_OP_breg28:
1616 case DW_OP_breg29:
1617 case DW_OP_breg30:
1618 case DW_OP_breg31:
1619 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1620 break;
1621 case DW_OP_regx:
1622 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1623 break;
1624 case DW_OP_fbreg:
1625 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1626 break;
1627 case DW_OP_bregx:
1628 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1629 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1630 break;
1631 case DW_OP_piece:
1632 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1633 break;
1634 case DW_OP_bit_piece:
1635 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1636 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1637 break;
1638 case DW_OP_deref_size:
1639 case DW_OP_xderef_size:
1640 size += 1;
1641 break;
1642 case DW_OP_call2:
1643 size += 2;
1644 break;
1645 case DW_OP_call4:
1646 size += 4;
1647 break;
1648 case DW_OP_call_ref:
1649 size += DWARF_REF_SIZE;
1650 break;
1651 case DW_OP_implicit_value:
1652 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1653 + loc->dw_loc_oprnd1.v.val_unsigned;
1654 break;
1655 case DW_OP_GNU_implicit_pointer:
1656 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1657 break;
1658 case DW_OP_GNU_entry_value:
1660 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1661 size += size_of_uleb128 (op_size) + op_size;
1662 break;
1664 case DW_OP_GNU_const_type:
1666 unsigned long o
1667 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1668 size += size_of_uleb128 (o) + 1;
1669 switch (loc->dw_loc_oprnd2.val_class)
1671 case dw_val_class_vec:
1672 size += loc->dw_loc_oprnd2.v.val_vec.length
1673 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1674 break;
1675 case dw_val_class_const:
1676 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1677 break;
1678 case dw_val_class_const_double:
1679 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1680 break;
1681 case dw_val_class_wide_int:
1682 size += (get_full_len (*loc->dw_loc_oprnd2.v.val_wide)
1683 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
1684 break;
1685 default:
1686 gcc_unreachable ();
1688 break;
1690 case DW_OP_GNU_regval_type:
1692 unsigned long o
1693 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1694 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1695 + size_of_uleb128 (o);
1697 break;
1698 case DW_OP_GNU_deref_type:
1700 unsigned long o
1701 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1702 size += 1 + size_of_uleb128 (o);
1704 break;
1705 case DW_OP_GNU_convert:
1706 case DW_OP_GNU_reinterpret:
1707 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1708 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1709 else
1711 unsigned long o
1712 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1713 size += size_of_uleb128 (o);
1715 break;
1716 case DW_OP_GNU_parameter_ref:
1717 size += 4;
1718 break;
1719 default:
1720 break;
1723 return size;
1726 /* Return the size of a series of location descriptors. */
1728 unsigned long
1729 size_of_locs (dw_loc_descr_ref loc)
1731 dw_loc_descr_ref l;
1732 unsigned long size;
1734 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1735 field, to avoid writing to a PCH file. */
1736 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1738 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1739 break;
1740 size += size_of_loc_descr (l);
1742 if (! l)
1743 return size;
1745 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1747 l->dw_loc_addr = size;
1748 size += size_of_loc_descr (l);
1751 return size;
1754 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1755 static void get_ref_die_offset_label (char *, dw_die_ref);
1756 static unsigned long int get_ref_die_offset (dw_die_ref);
1758 /* Output location description stack opcode's operands (if any).
1759 The for_eh_or_skip parameter controls whether register numbers are
1760 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1761 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1762 info). This should be suppressed for the cases that have not been converted
1763 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
1765 static void
1766 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1768 dw_val_ref val1 = &loc->dw_loc_oprnd1;
1769 dw_val_ref val2 = &loc->dw_loc_oprnd2;
1771 switch (loc->dw_loc_opc)
1773 #ifdef DWARF2_DEBUGGING_INFO
1774 case DW_OP_const2u:
1775 case DW_OP_const2s:
1776 dw2_asm_output_data (2, val1->v.val_int, NULL);
1777 break;
1778 case DW_OP_const4u:
1779 if (loc->dtprel)
1781 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1782 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1783 val1->v.val_addr);
1784 fputc ('\n', asm_out_file);
1785 break;
1787 /* FALLTHRU */
1788 case DW_OP_const4s:
1789 dw2_asm_output_data (4, val1->v.val_int, NULL);
1790 break;
1791 case DW_OP_const8u:
1792 if (loc->dtprel)
1794 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1795 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1796 val1->v.val_addr);
1797 fputc ('\n', asm_out_file);
1798 break;
1800 /* FALLTHRU */
1801 case DW_OP_const8s:
1802 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
1803 dw2_asm_output_data (8, val1->v.val_int, NULL);
1804 break;
1805 case DW_OP_skip:
1806 case DW_OP_bra:
1808 int offset;
1810 gcc_assert (val1->val_class == dw_val_class_loc);
1811 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
1813 dw2_asm_output_data (2, offset, NULL);
1815 break;
1816 case DW_OP_implicit_value:
1817 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1818 switch (val2->val_class)
1820 case dw_val_class_const:
1821 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
1822 break;
1823 case dw_val_class_vec:
1825 unsigned int elt_size = val2->v.val_vec.elt_size;
1826 unsigned int len = val2->v.val_vec.length;
1827 unsigned int i;
1828 unsigned char *p;
1830 if (elt_size > sizeof (HOST_WIDE_INT))
1832 elt_size /= 2;
1833 len *= 2;
1835 for (i = 0, p = val2->v.val_vec.array;
1836 i < len;
1837 i++, p += elt_size)
1838 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
1839 "fp or vector constant word %u", i);
1841 break;
1842 case dw_val_class_const_double:
1844 unsigned HOST_WIDE_INT first, second;
1846 if (WORDS_BIG_ENDIAN)
1848 first = val2->v.val_double.high;
1849 second = val2->v.val_double.low;
1851 else
1853 first = val2->v.val_double.low;
1854 second = val2->v.val_double.high;
1856 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1857 first, NULL);
1858 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1859 second, NULL);
1861 break;
1862 case dw_val_class_wide_int:
1864 int i;
1865 int len = get_full_len (*val2->v.val_wide);
1866 if (WORDS_BIG_ENDIAN)
1867 for (i = len - 1; i >= 0; --i)
1868 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1869 val2->v.val_wide->elt (i), NULL);
1870 else
1871 for (i = 0; i < len; ++i)
1872 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1873 val2->v.val_wide->elt (i), NULL);
1875 break;
1876 case dw_val_class_addr:
1877 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
1878 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
1879 break;
1880 default:
1881 gcc_unreachable ();
1883 break;
1884 #else
1885 case DW_OP_const2u:
1886 case DW_OP_const2s:
1887 case DW_OP_const4u:
1888 case DW_OP_const4s:
1889 case DW_OP_const8u:
1890 case DW_OP_const8s:
1891 case DW_OP_skip:
1892 case DW_OP_bra:
1893 case DW_OP_implicit_value:
1894 /* We currently don't make any attempt to make sure these are
1895 aligned properly like we do for the main unwind info, so
1896 don't support emitting things larger than a byte if we're
1897 only doing unwinding. */
1898 gcc_unreachable ();
1899 #endif
1900 case DW_OP_const1u:
1901 case DW_OP_const1s:
1902 dw2_asm_output_data (1, val1->v.val_int, NULL);
1903 break;
1904 case DW_OP_constu:
1905 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1906 break;
1907 case DW_OP_consts:
1908 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1909 break;
1910 case DW_OP_pick:
1911 dw2_asm_output_data (1, val1->v.val_int, NULL);
1912 break;
1913 case DW_OP_plus_uconst:
1914 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1915 break;
1916 case DW_OP_breg0:
1917 case DW_OP_breg1:
1918 case DW_OP_breg2:
1919 case DW_OP_breg3:
1920 case DW_OP_breg4:
1921 case DW_OP_breg5:
1922 case DW_OP_breg6:
1923 case DW_OP_breg7:
1924 case DW_OP_breg8:
1925 case DW_OP_breg9:
1926 case DW_OP_breg10:
1927 case DW_OP_breg11:
1928 case DW_OP_breg12:
1929 case DW_OP_breg13:
1930 case DW_OP_breg14:
1931 case DW_OP_breg15:
1932 case DW_OP_breg16:
1933 case DW_OP_breg17:
1934 case DW_OP_breg18:
1935 case DW_OP_breg19:
1936 case DW_OP_breg20:
1937 case DW_OP_breg21:
1938 case DW_OP_breg22:
1939 case DW_OP_breg23:
1940 case DW_OP_breg24:
1941 case DW_OP_breg25:
1942 case DW_OP_breg26:
1943 case DW_OP_breg27:
1944 case DW_OP_breg28:
1945 case DW_OP_breg29:
1946 case DW_OP_breg30:
1947 case DW_OP_breg31:
1948 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1949 break;
1950 case DW_OP_regx:
1952 unsigned r = val1->v.val_unsigned;
1953 if (for_eh_or_skip >= 0)
1954 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1955 gcc_assert (size_of_uleb128 (r)
1956 == size_of_uleb128 (val1->v.val_unsigned));
1957 dw2_asm_output_data_uleb128 (r, NULL);
1959 break;
1960 case DW_OP_fbreg:
1961 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1962 break;
1963 case DW_OP_bregx:
1965 unsigned r = val1->v.val_unsigned;
1966 if (for_eh_or_skip >= 0)
1967 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1968 gcc_assert (size_of_uleb128 (r)
1969 == size_of_uleb128 (val1->v.val_unsigned));
1970 dw2_asm_output_data_uleb128 (r, NULL);
1971 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
1973 break;
1974 case DW_OP_piece:
1975 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1976 break;
1977 case DW_OP_bit_piece:
1978 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1979 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
1980 break;
1981 case DW_OP_deref_size:
1982 case DW_OP_xderef_size:
1983 dw2_asm_output_data (1, val1->v.val_int, NULL);
1984 break;
1986 case DW_OP_addr:
1987 if (loc->dtprel)
1989 if (targetm.asm_out.output_dwarf_dtprel)
1991 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
1992 DWARF2_ADDR_SIZE,
1993 val1->v.val_addr);
1994 fputc ('\n', asm_out_file);
1996 else
1997 gcc_unreachable ();
1999 else
2001 #ifdef DWARF2_DEBUGGING_INFO
2002 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
2003 #else
2004 gcc_unreachable ();
2005 #endif
2007 break;
2009 case DW_OP_GNU_addr_index:
2010 case DW_OP_GNU_const_index:
2011 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
2012 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
2013 "(index into .debug_addr)");
2014 break;
2016 case DW_OP_GNU_implicit_pointer:
2018 char label[MAX_ARTIFICIAL_LABEL_BYTES
2019 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2020 gcc_assert (val1->val_class == dw_val_class_die_ref);
2021 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2022 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2023 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2025 break;
2027 case DW_OP_GNU_entry_value:
2028 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2029 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2030 break;
2032 case DW_OP_GNU_const_type:
2034 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2035 gcc_assert (o);
2036 dw2_asm_output_data_uleb128 (o, NULL);
2037 switch (val2->val_class)
2039 case dw_val_class_const:
2040 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2041 dw2_asm_output_data (1, l, NULL);
2042 dw2_asm_output_data (l, val2->v.val_int, NULL);
2043 break;
2044 case dw_val_class_vec:
2046 unsigned int elt_size = val2->v.val_vec.elt_size;
2047 unsigned int len = val2->v.val_vec.length;
2048 unsigned int i;
2049 unsigned char *p;
2051 l = len * elt_size;
2052 dw2_asm_output_data (1, l, NULL);
2053 if (elt_size > sizeof (HOST_WIDE_INT))
2055 elt_size /= 2;
2056 len *= 2;
2058 for (i = 0, p = val2->v.val_vec.array;
2059 i < len;
2060 i++, p += elt_size)
2061 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2062 "fp or vector constant word %u", i);
2064 break;
2065 case dw_val_class_const_double:
2067 unsigned HOST_WIDE_INT first, second;
2068 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2070 dw2_asm_output_data (1, 2 * l, NULL);
2071 if (WORDS_BIG_ENDIAN)
2073 first = val2->v.val_double.high;
2074 second = val2->v.val_double.low;
2076 else
2078 first = val2->v.val_double.low;
2079 second = val2->v.val_double.high;
2081 dw2_asm_output_data (l, first, NULL);
2082 dw2_asm_output_data (l, second, NULL);
2084 break;
2085 case dw_val_class_wide_int:
2087 int i;
2088 int len = get_full_len (*val2->v.val_wide);
2089 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2091 dw2_asm_output_data (1, len * l, NULL);
2092 if (WORDS_BIG_ENDIAN)
2093 for (i = len - 1; i >= 0; --i)
2094 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2095 else
2096 for (i = 0; i < len; ++i)
2097 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2099 break;
2100 default:
2101 gcc_unreachable ();
2104 break;
2105 case DW_OP_GNU_regval_type:
2107 unsigned r = val1->v.val_unsigned;
2108 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2109 gcc_assert (o);
2110 if (for_eh_or_skip >= 0)
2112 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2113 gcc_assert (size_of_uleb128 (r)
2114 == size_of_uleb128 (val1->v.val_unsigned));
2116 dw2_asm_output_data_uleb128 (r, NULL);
2117 dw2_asm_output_data_uleb128 (o, NULL);
2119 break;
2120 case DW_OP_GNU_deref_type:
2122 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2123 gcc_assert (o);
2124 dw2_asm_output_data (1, val1->v.val_int, NULL);
2125 dw2_asm_output_data_uleb128 (o, NULL);
2127 break;
2128 case DW_OP_GNU_convert:
2129 case DW_OP_GNU_reinterpret:
2130 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2131 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2132 else
2134 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2135 gcc_assert (o);
2136 dw2_asm_output_data_uleb128 (o, NULL);
2138 break;
2140 case DW_OP_GNU_parameter_ref:
2142 unsigned long o;
2143 gcc_assert (val1->val_class == dw_val_class_die_ref);
2144 o = get_ref_die_offset (val1->v.val_die_ref.die);
2145 dw2_asm_output_data (4, o, NULL);
2147 break;
2149 default:
2150 /* Other codes have no operands. */
2151 break;
2155 /* Output a sequence of location operations.
2156 The for_eh_or_skip parameter controls whether register numbers are
2157 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2158 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2159 info). This should be suppressed for the cases that have not been converted
2160 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2162 void
2163 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2165 for (; loc != NULL; loc = loc->dw_loc_next)
2167 enum dwarf_location_atom opc = loc->dw_loc_opc;
2168 /* Output the opcode. */
2169 if (for_eh_or_skip >= 0
2170 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2172 unsigned r = (opc - DW_OP_breg0);
2173 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2174 gcc_assert (r <= 31);
2175 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2177 else if (for_eh_or_skip >= 0
2178 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2180 unsigned r = (opc - DW_OP_reg0);
2181 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2182 gcc_assert (r <= 31);
2183 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2186 dw2_asm_output_data (1, opc,
2187 "%s", dwarf_stack_op_name (opc));
2189 /* Output the operand(s) (if any). */
2190 output_loc_operands (loc, for_eh_or_skip);
2194 /* Output location description stack opcode's operands (if any).
2195 The output is single bytes on a line, suitable for .cfi_escape. */
2197 static void
2198 output_loc_operands_raw (dw_loc_descr_ref loc)
2200 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2201 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2203 switch (loc->dw_loc_opc)
2205 case DW_OP_addr:
2206 case DW_OP_GNU_addr_index:
2207 case DW_OP_GNU_const_index:
2208 case DW_OP_implicit_value:
2209 /* We cannot output addresses in .cfi_escape, only bytes. */
2210 gcc_unreachable ();
2212 case DW_OP_const1u:
2213 case DW_OP_const1s:
2214 case DW_OP_pick:
2215 case DW_OP_deref_size:
2216 case DW_OP_xderef_size:
2217 fputc (',', asm_out_file);
2218 dw2_asm_output_data_raw (1, val1->v.val_int);
2219 break;
2221 case DW_OP_const2u:
2222 case DW_OP_const2s:
2223 fputc (',', asm_out_file);
2224 dw2_asm_output_data_raw (2, val1->v.val_int);
2225 break;
2227 case DW_OP_const4u:
2228 case DW_OP_const4s:
2229 fputc (',', asm_out_file);
2230 dw2_asm_output_data_raw (4, val1->v.val_int);
2231 break;
2233 case DW_OP_const8u:
2234 case DW_OP_const8s:
2235 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2236 fputc (',', asm_out_file);
2237 dw2_asm_output_data_raw (8, val1->v.val_int);
2238 break;
2240 case DW_OP_skip:
2241 case DW_OP_bra:
2243 int offset;
2245 gcc_assert (val1->val_class == dw_val_class_loc);
2246 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2248 fputc (',', asm_out_file);
2249 dw2_asm_output_data_raw (2, offset);
2251 break;
2253 case DW_OP_regx:
2255 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2256 gcc_assert (size_of_uleb128 (r)
2257 == size_of_uleb128 (val1->v.val_unsigned));
2258 fputc (',', asm_out_file);
2259 dw2_asm_output_data_uleb128_raw (r);
2261 break;
2263 case DW_OP_constu:
2264 case DW_OP_plus_uconst:
2265 case DW_OP_piece:
2266 fputc (',', asm_out_file);
2267 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2268 break;
2270 case DW_OP_bit_piece:
2271 fputc (',', asm_out_file);
2272 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2273 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2274 break;
2276 case DW_OP_consts:
2277 case DW_OP_breg0:
2278 case DW_OP_breg1:
2279 case DW_OP_breg2:
2280 case DW_OP_breg3:
2281 case DW_OP_breg4:
2282 case DW_OP_breg5:
2283 case DW_OP_breg6:
2284 case DW_OP_breg7:
2285 case DW_OP_breg8:
2286 case DW_OP_breg9:
2287 case DW_OP_breg10:
2288 case DW_OP_breg11:
2289 case DW_OP_breg12:
2290 case DW_OP_breg13:
2291 case DW_OP_breg14:
2292 case DW_OP_breg15:
2293 case DW_OP_breg16:
2294 case DW_OP_breg17:
2295 case DW_OP_breg18:
2296 case DW_OP_breg19:
2297 case DW_OP_breg20:
2298 case DW_OP_breg21:
2299 case DW_OP_breg22:
2300 case DW_OP_breg23:
2301 case DW_OP_breg24:
2302 case DW_OP_breg25:
2303 case DW_OP_breg26:
2304 case DW_OP_breg27:
2305 case DW_OP_breg28:
2306 case DW_OP_breg29:
2307 case DW_OP_breg30:
2308 case DW_OP_breg31:
2309 case DW_OP_fbreg:
2310 fputc (',', asm_out_file);
2311 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2312 break;
2314 case DW_OP_bregx:
2316 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2317 gcc_assert (size_of_uleb128 (r)
2318 == size_of_uleb128 (val1->v.val_unsigned));
2319 fputc (',', asm_out_file);
2320 dw2_asm_output_data_uleb128_raw (r);
2321 fputc (',', asm_out_file);
2322 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2324 break;
2326 case DW_OP_GNU_implicit_pointer:
2327 case DW_OP_GNU_entry_value:
2328 case DW_OP_GNU_const_type:
2329 case DW_OP_GNU_regval_type:
2330 case DW_OP_GNU_deref_type:
2331 case DW_OP_GNU_convert:
2332 case DW_OP_GNU_reinterpret:
2333 case DW_OP_GNU_parameter_ref:
2334 gcc_unreachable ();
2335 break;
2337 default:
2338 /* Other codes have no operands. */
2339 break;
2343 void
2344 output_loc_sequence_raw (dw_loc_descr_ref loc)
2346 while (1)
2348 enum dwarf_location_atom opc = loc->dw_loc_opc;
2349 /* Output the opcode. */
2350 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2352 unsigned r = (opc - DW_OP_breg0);
2353 r = DWARF2_FRAME_REG_OUT (r, 1);
2354 gcc_assert (r <= 31);
2355 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2357 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2359 unsigned r = (opc - DW_OP_reg0);
2360 r = DWARF2_FRAME_REG_OUT (r, 1);
2361 gcc_assert (r <= 31);
2362 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2364 /* Output the opcode. */
2365 fprintf (asm_out_file, "%#x", opc);
2366 output_loc_operands_raw (loc);
2368 if (!loc->dw_loc_next)
2369 break;
2370 loc = loc->dw_loc_next;
2372 fputc (',', asm_out_file);
2376 /* This function builds a dwarf location descriptor sequence from a
2377 dw_cfa_location, adding the given OFFSET to the result of the
2378 expression. */
2380 struct dw_loc_descr_node *
2381 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2383 struct dw_loc_descr_node *head, *tmp;
2385 offset += cfa->offset;
2387 if (cfa->indirect)
2389 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2390 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2391 head->dw_loc_oprnd1.val_entry = NULL;
2392 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2393 add_loc_descr (&head, tmp);
2394 if (offset != 0)
2396 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2397 add_loc_descr (&head, tmp);
2400 else
2401 head = new_reg_loc_descr (cfa->reg, offset);
2403 return head;
2406 /* This function builds a dwarf location descriptor sequence for
2407 the address at OFFSET from the CFA when stack is aligned to
2408 ALIGNMENT byte. */
2410 struct dw_loc_descr_node *
2411 build_cfa_aligned_loc (dw_cfa_location *cfa,
2412 HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2414 struct dw_loc_descr_node *head;
2415 unsigned int dwarf_fp
2416 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2418 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2419 if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2421 head = new_reg_loc_descr (dwarf_fp, 0);
2422 add_loc_descr (&head, int_loc_descriptor (alignment));
2423 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2424 loc_descr_plus_const (&head, offset);
2426 else
2427 head = new_reg_loc_descr (dwarf_fp, offset);
2428 return head;
2431 /* And now, the support for symbolic debugging information. */
2433 /* .debug_str support. */
2435 static void dwarf2out_init (const char *);
2436 static void dwarf2out_finish (const char *);
2437 static void dwarf2out_assembly_start (void);
2438 static void dwarf2out_define (unsigned int, const char *);
2439 static void dwarf2out_undef (unsigned int, const char *);
2440 static void dwarf2out_start_source_file (unsigned, const char *);
2441 static void dwarf2out_end_source_file (unsigned);
2442 static void dwarf2out_function_decl (tree);
2443 static void dwarf2out_begin_block (unsigned, unsigned);
2444 static void dwarf2out_end_block (unsigned, unsigned);
2445 static bool dwarf2out_ignore_block (const_tree);
2446 static void dwarf2out_global_decl (tree);
2447 static void dwarf2out_type_decl (tree, int);
2448 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
2449 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2450 dw_die_ref);
2451 static void dwarf2out_abstract_function (tree);
2452 static void dwarf2out_var_location (rtx_insn *);
2453 static void dwarf2out_begin_function (tree);
2454 static void dwarf2out_end_function (unsigned int);
2455 static void dwarf2out_set_name (tree, tree);
2457 /* The debug hooks structure. */
2459 const struct gcc_debug_hooks dwarf2_debug_hooks =
2461 dwarf2out_init,
2462 dwarf2out_finish,
2463 dwarf2out_assembly_start,
2464 dwarf2out_define,
2465 dwarf2out_undef,
2466 dwarf2out_start_source_file,
2467 dwarf2out_end_source_file,
2468 dwarf2out_begin_block,
2469 dwarf2out_end_block,
2470 dwarf2out_ignore_block,
2471 dwarf2out_source_line,
2472 dwarf2out_begin_prologue,
2473 #if VMS_DEBUGGING_INFO
2474 dwarf2out_vms_end_prologue,
2475 dwarf2out_vms_begin_epilogue,
2476 #else
2477 debug_nothing_int_charstar,
2478 debug_nothing_int_charstar,
2479 #endif
2480 dwarf2out_end_epilogue,
2481 dwarf2out_begin_function,
2482 dwarf2out_end_function, /* end_function */
2483 dwarf2out_function_decl, /* function_decl */
2484 dwarf2out_global_decl,
2485 dwarf2out_type_decl, /* type_decl */
2486 dwarf2out_imported_module_or_decl,
2487 debug_nothing_tree, /* deferred_inline_function */
2488 /* The DWARF 2 backend tries to reduce debugging bloat by not
2489 emitting the abstract description of inline functions until
2490 something tries to reference them. */
2491 dwarf2out_abstract_function, /* outlining_inline_function */
2492 debug_nothing_rtx_code_label, /* label */
2493 debug_nothing_int, /* handle_pch */
2494 dwarf2out_var_location,
2495 dwarf2out_switch_text_section,
2496 dwarf2out_set_name,
2497 1, /* start_end_main_source_file */
2498 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2501 /* NOTE: In the comments in this file, many references are made to
2502 "Debugging Information Entries". This term is abbreviated as `DIE'
2503 throughout the remainder of this file. */
2505 /* An internal representation of the DWARF output is built, and then
2506 walked to generate the DWARF debugging info. The walk of the internal
2507 representation is done after the entire program has been compiled.
2508 The types below are used to describe the internal representation. */
2510 /* Whether to put type DIEs into their own section .debug_types instead
2511 of making them part of the .debug_info section. Only supported for
2512 Dwarf V4 or higher and the user didn't disable them through
2513 -fno-debug-types-section. It is more efficient to put them in a
2514 separate comdat sections since the linker will then be able to
2515 remove duplicates. But not all tools support .debug_types sections
2516 yet. */
2518 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2520 /* Various DIE's use offsets relative to the beginning of the
2521 .debug_info section to refer to each other. */
2523 typedef long int dw_offset;
2525 /* Define typedefs here to avoid circular dependencies. */
2527 typedef struct dw_attr_struct *dw_attr_ref;
2528 typedef struct dw_line_info_struct *dw_line_info_ref;
2529 typedef struct pubname_struct *pubname_ref;
2530 typedef struct dw_ranges_struct *dw_ranges_ref;
2531 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
2532 typedef struct comdat_type_struct *comdat_type_node_ref;
2534 /* The entries in the line_info table more-or-less mirror the opcodes
2535 that are used in the real dwarf line table. Arrays of these entries
2536 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2537 supported. */
2539 enum dw_line_info_opcode {
2540 /* Emit DW_LNE_set_address; the operand is the label index. */
2541 LI_set_address,
2543 /* Emit a row to the matrix with the given line. This may be done
2544 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2545 special opcodes. */
2546 LI_set_line,
2548 /* Emit a DW_LNS_set_file. */
2549 LI_set_file,
2551 /* Emit a DW_LNS_set_column. */
2552 LI_set_column,
2554 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
2555 LI_negate_stmt,
2557 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
2558 LI_set_prologue_end,
2559 LI_set_epilogue_begin,
2561 /* Emit a DW_LNE_set_discriminator. */
2562 LI_set_discriminator
2565 typedef struct GTY(()) dw_line_info_struct {
2566 enum dw_line_info_opcode opcode;
2567 unsigned int val;
2568 } dw_line_info_entry;
2571 typedef struct GTY(()) dw_line_info_table_struct {
2572 /* The label that marks the end of this section. */
2573 const char *end_label;
2575 /* The values for the last row of the matrix, as collected in the table.
2576 These are used to minimize the changes to the next row. */
2577 unsigned int file_num;
2578 unsigned int line_num;
2579 unsigned int column_num;
2580 int discrim_num;
2581 bool is_stmt;
2582 bool in_use;
2584 vec<dw_line_info_entry, va_gc> *entries;
2585 } dw_line_info_table;
2587 typedef dw_line_info_table *dw_line_info_table_p;
2590 /* Each DIE attribute has a field specifying the attribute kind,
2591 a link to the next attribute in the chain, and an attribute value.
2592 Attributes are typically linked below the DIE they modify. */
2594 typedef struct GTY(()) dw_attr_struct {
2595 enum dwarf_attribute dw_attr;
2596 dw_val_node dw_attr_val;
2598 dw_attr_node;
2601 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
2602 The children of each node form a circular list linked by
2603 die_sib. die_child points to the node *before* the "first" child node. */
2605 typedef struct GTY((chain_circular ("%h.die_sib"), for_user)) die_struct {
2606 union die_symbol_or_type_node
2608 const char * GTY ((tag ("0"))) die_symbol;
2609 comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
2611 GTY ((desc ("%0.comdat_type_p"))) die_id;
2612 vec<dw_attr_node, va_gc> *die_attr;
2613 dw_die_ref die_parent;
2614 dw_die_ref die_child;
2615 dw_die_ref die_sib;
2616 dw_die_ref die_definition; /* ref from a specification to its definition */
2617 dw_offset die_offset;
2618 unsigned long die_abbrev;
2619 int die_mark;
2620 unsigned int decl_id;
2621 enum dwarf_tag die_tag;
2622 /* Die is used and must not be pruned as unused. */
2623 BOOL_BITFIELD die_perennial_p : 1;
2624 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2625 /* Lots of spare bits. */
2627 die_node;
2629 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
2630 #define FOR_EACH_CHILD(die, c, expr) do { \
2631 c = die->die_child; \
2632 if (c) do { \
2633 c = c->die_sib; \
2634 expr; \
2635 } while (c != die->die_child); \
2636 } while (0)
2638 /* The pubname structure */
2640 typedef struct GTY(()) pubname_struct {
2641 dw_die_ref die;
2642 const char *name;
2644 pubname_entry;
2647 struct GTY(()) dw_ranges_struct {
2648 /* If this is positive, it's a block number, otherwise it's a
2649 bitwise-negated index into dw_ranges_by_label. */
2650 int num;
2653 /* A structure to hold a macinfo entry. */
2655 typedef struct GTY(()) macinfo_struct {
2656 unsigned char code;
2657 unsigned HOST_WIDE_INT lineno;
2658 const char *info;
2660 macinfo_entry;
2663 struct GTY(()) dw_ranges_by_label_struct {
2664 const char *begin;
2665 const char *end;
2668 /* The comdat type node structure. */
2669 typedef struct GTY(()) comdat_type_struct
2671 dw_die_ref root_die;
2672 dw_die_ref type_die;
2673 dw_die_ref skeleton_die;
2674 char signature[DWARF_TYPE_SIGNATURE_SIZE];
2675 struct comdat_type_struct *next;
2677 comdat_type_node;
2679 /* The limbo die list structure. */
2680 typedef struct GTY(()) limbo_die_struct {
2681 dw_die_ref die;
2682 tree created_for;
2683 struct limbo_die_struct *next;
2685 limbo_die_node;
2687 typedef struct skeleton_chain_struct
2689 dw_die_ref old_die;
2690 dw_die_ref new_die;
2691 struct skeleton_chain_struct *parent;
2693 skeleton_chain_node;
2695 /* Define a macro which returns nonzero for a TYPE_DECL which was
2696 implicitly generated for a type.
2698 Note that, unlike the C front-end (which generates a NULL named
2699 TYPE_DECL node for each complete tagged type, each array type,
2700 and each function type node created) the C++ front-end generates
2701 a _named_ TYPE_DECL node for each tagged type node created.
2702 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
2703 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
2704 front-end, but for each type, tagged or not. */
2706 #define TYPE_DECL_IS_STUB(decl) \
2707 (DECL_NAME (decl) == NULL_TREE \
2708 || (DECL_ARTIFICIAL (decl) \
2709 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
2710 /* This is necessary for stub decls that \
2711 appear in nested inline functions. */ \
2712 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
2713 && (decl_ultimate_origin (decl) \
2714 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
2716 /* Information concerning the compilation unit's programming
2717 language, and compiler version. */
2719 /* Fixed size portion of the DWARF compilation unit header. */
2720 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
2721 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
2723 /* Fixed size portion of the DWARF comdat type unit header. */
2724 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
2725 (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
2726 + DWARF_OFFSET_SIZE)
2728 /* Fixed size portion of public names info. */
2729 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
2731 /* Fixed size portion of the address range info. */
2732 #define DWARF_ARANGES_HEADER_SIZE \
2733 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2734 DWARF2_ADDR_SIZE * 2) \
2735 - DWARF_INITIAL_LENGTH_SIZE)
2737 /* Size of padding portion in the address range info. It must be
2738 aligned to twice the pointer size. */
2739 #define DWARF_ARANGES_PAD_SIZE \
2740 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2741 DWARF2_ADDR_SIZE * 2) \
2742 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
2744 /* Use assembler line directives if available. */
2745 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
2746 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
2747 #define DWARF2_ASM_LINE_DEBUG_INFO 1
2748 #else
2749 #define DWARF2_ASM_LINE_DEBUG_INFO 0
2750 #endif
2751 #endif
2753 /* Minimum line offset in a special line info. opcode.
2754 This value was chosen to give a reasonable range of values. */
2755 #define DWARF_LINE_BASE -10
2757 /* First special line opcode - leave room for the standard opcodes. */
2758 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
2760 /* Range of line offsets in a special line info. opcode. */
2761 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
2763 /* Flag that indicates the initial value of the is_stmt_start flag.
2764 In the present implementation, we do not mark any lines as
2765 the beginning of a source statement, because that information
2766 is not made available by the GCC front-end. */
2767 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
2769 /* Maximum number of operations per instruction bundle. */
2770 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
2771 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
2772 #endif
2774 /* This location is used by calc_die_sizes() to keep track
2775 the offset of each DIE within the .debug_info section. */
2776 static unsigned long next_die_offset;
2778 /* Record the root of the DIE's built for the current compilation unit. */
2779 static GTY(()) dw_die_ref single_comp_unit_die;
2781 /* A list of type DIEs that have been separated into comdat sections. */
2782 static GTY(()) comdat_type_node *comdat_type_list;
2784 /* A list of DIEs with a NULL parent waiting to be relocated. */
2785 static GTY(()) limbo_die_node *limbo_die_list;
2787 /* A list of DIEs for which we may have to generate
2788 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
2789 static GTY(()) limbo_die_node *deferred_asm_name;
2791 struct dwarf_file_hasher : ggc_hasher<dwarf_file_data *>
2793 typedef const char *compare_type;
2795 static hashval_t hash (dwarf_file_data *);
2796 static bool equal (dwarf_file_data *, const char *);
2799 /* Filenames referenced by this compilation unit. */
2800 static GTY(()) hash_table<dwarf_file_hasher> *file_table;
2802 struct decl_die_hasher : ggc_hasher<die_node *>
2804 typedef tree compare_type;
2806 static hashval_t hash (die_node *);
2807 static bool equal (die_node *, tree);
2809 /* A hash table of references to DIE's that describe declarations.
2810 The key is a DECL_UID() which is a unique number identifying each decl. */
2811 static GTY (()) hash_table<decl_die_hasher> *decl_die_table;
2813 struct block_die_hasher : ggc_hasher<die_struct *>
2815 static hashval_t hash (die_struct *);
2816 static bool equal (die_struct *, die_struct *);
2819 /* A hash table of references to DIE's that describe COMMON blocks.
2820 The key is DECL_UID() ^ die_parent. */
2821 static GTY (()) hash_table<block_die_hasher> *common_block_die_table;
2823 typedef struct GTY(()) die_arg_entry_struct {
2824 dw_die_ref die;
2825 tree arg;
2826 } die_arg_entry;
2829 /* Node of the variable location list. */
2830 struct GTY ((chain_next ("%h.next"))) var_loc_node {
2831 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
2832 EXPR_LIST chain. For small bitsizes, bitsize is encoded
2833 in mode of the EXPR_LIST node and first EXPR_LIST operand
2834 is either NOTE_INSN_VAR_LOCATION for a piece with a known
2835 location or NULL for padding. For larger bitsizes,
2836 mode is 0 and first operand is a CONCAT with bitsize
2837 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
2838 NULL as second operand. */
2839 rtx GTY (()) loc;
2840 const char * GTY (()) label;
2841 struct var_loc_node * GTY (()) next;
2844 /* Variable location list. */
2845 struct GTY ((for_user)) var_loc_list_def {
2846 struct var_loc_node * GTY (()) first;
2848 /* Pointer to the last but one or last element of the
2849 chained list. If the list is empty, both first and
2850 last are NULL, if the list contains just one node
2851 or the last node certainly is not redundant, it points
2852 to the last node, otherwise points to the last but one.
2853 Do not mark it for GC because it is marked through the chain. */
2854 struct var_loc_node * GTY ((skip ("%h"))) last;
2856 /* Pointer to the last element before section switch,
2857 if NULL, either sections weren't switched or first
2858 is after section switch. */
2859 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
2861 /* DECL_UID of the variable decl. */
2862 unsigned int decl_id;
2864 typedef struct var_loc_list_def var_loc_list;
2866 /* Call argument location list. */
2867 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
2868 rtx GTY (()) call_arg_loc_note;
2869 const char * GTY (()) label;
2870 tree GTY (()) block;
2871 bool tail_call_p;
2872 rtx GTY (()) symbol_ref;
2873 struct call_arg_loc_node * GTY (()) next;
2877 struct decl_loc_hasher : ggc_hasher<var_loc_list *>
2879 typedef const_tree compare_type;
2881 static hashval_t hash (var_loc_list *);
2882 static bool equal (var_loc_list *, const_tree);
2885 /* Table of decl location linked lists. */
2886 static GTY (()) hash_table<decl_loc_hasher> *decl_loc_table;
2888 /* Head and tail of call_arg_loc chain. */
2889 static GTY (()) struct call_arg_loc_node *call_arg_locations;
2890 static struct call_arg_loc_node *call_arg_loc_last;
2892 /* Number of call sites in the current function. */
2893 static int call_site_count = -1;
2894 /* Number of tail call sites in the current function. */
2895 static int tail_call_site_count = -1;
2897 /* Vector mapping block numbers to DW_TAG_{lexical_block,inlined_subroutine}
2898 DIEs. */
2899 static vec<dw_die_ref> block_map;
2901 /* A cached location list. */
2902 struct GTY ((for_user)) cached_dw_loc_list_def {
2903 /* The DECL_UID of the decl that this entry describes. */
2904 unsigned int decl_id;
2906 /* The cached location list. */
2907 dw_loc_list_ref loc_list;
2909 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
2911 struct dw_loc_list_hasher : ggc_hasher<cached_dw_loc_list *>
2914 typedef const_tree compare_type;
2916 static hashval_t hash (cached_dw_loc_list *);
2917 static bool equal (cached_dw_loc_list *, const_tree);
2920 /* Table of cached location lists. */
2921 static GTY (()) hash_table<dw_loc_list_hasher> *cached_dw_loc_list_table;
2923 /* A pointer to the base of a list of references to DIE's that
2924 are uniquely identified by their tag, presence/absence of
2925 children DIE's, and list of attribute/value pairs. */
2926 static GTY((length ("abbrev_die_table_allocated")))
2927 dw_die_ref *abbrev_die_table;
2929 /* Number of elements currently allocated for abbrev_die_table. */
2930 static GTY(()) unsigned abbrev_die_table_allocated;
2932 /* Number of elements in type_die_table currently in use. */
2933 static GTY(()) unsigned abbrev_die_table_in_use;
2935 /* Size (in elements) of increments by which we may expand the
2936 abbrev_die_table. */
2937 #define ABBREV_DIE_TABLE_INCREMENT 256
2939 /* A global counter for generating labels for line number data. */
2940 static unsigned int line_info_label_num;
2942 /* The current table to which we should emit line number information
2943 for the current function. This will be set up at the beginning of
2944 assembly for the function. */
2945 static dw_line_info_table *cur_line_info_table;
2947 /* The two default tables of line number info. */
2948 static GTY(()) dw_line_info_table *text_section_line_info;
2949 static GTY(()) dw_line_info_table *cold_text_section_line_info;
2951 /* The set of all non-default tables of line number info. */
2952 static GTY(()) vec<dw_line_info_table_p, va_gc> *separate_line_info;
2954 /* A flag to tell pubnames/types export if there is an info section to
2955 refer to. */
2956 static bool info_section_emitted;
2958 /* A pointer to the base of a table that contains a list of publicly
2959 accessible names. */
2960 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
2962 /* A pointer to the base of a table that contains a list of publicly
2963 accessible types. */
2964 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
2966 /* A pointer to the base of a table that contains a list of macro
2967 defines/undefines (and file start/end markers). */
2968 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
2970 /* True if .debug_macinfo or .debug_macros section is going to be
2971 emitted. */
2972 #define have_macinfo \
2973 (debug_info_level >= DINFO_LEVEL_VERBOSE \
2974 && !macinfo_table->is_empty ())
2976 /* Array of dies for which we should generate .debug_ranges info. */
2977 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
2979 /* Number of elements currently allocated for ranges_table. */
2980 static GTY(()) unsigned ranges_table_allocated;
2982 /* Number of elements in ranges_table currently in use. */
2983 static GTY(()) unsigned ranges_table_in_use;
2985 /* Array of pairs of labels referenced in ranges_table. */
2986 static GTY ((length ("ranges_by_label_allocated")))
2987 dw_ranges_by_label_ref ranges_by_label;
2989 /* Number of elements currently allocated for ranges_by_label. */
2990 static GTY(()) unsigned ranges_by_label_allocated;
2992 /* Number of elements in ranges_by_label currently in use. */
2993 static GTY(()) unsigned ranges_by_label_in_use;
2995 /* Size (in elements) of increments by which we may expand the
2996 ranges_table. */
2997 #define RANGES_TABLE_INCREMENT 64
2999 /* Whether we have location lists that need outputting */
3000 static GTY(()) bool have_location_lists;
3002 /* Unique label counter. */
3003 static GTY(()) unsigned int loclabel_num;
3005 /* Unique label counter for point-of-call tables. */
3006 static GTY(()) unsigned int poc_label_num;
3008 /* The last file entry emitted by maybe_emit_file(). */
3009 static GTY(()) struct dwarf_file_data * last_emitted_file;
3011 /* Number of internal labels generated by gen_internal_sym(). */
3012 static GTY(()) int label_num;
3014 /* Cached result of previous call to lookup_filename. */
3015 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
3017 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
3019 /* Instances of generic types for which we need to generate debug
3020 info that describe their generic parameters and arguments. That
3021 generation needs to happen once all types are properly laid out so
3022 we do it at the end of compilation. */
3023 static GTY(()) vec<tree, va_gc> *generic_type_instances;
3025 /* Offset from the "steady-state frame pointer" to the frame base,
3026 within the current function. */
3027 static HOST_WIDE_INT frame_pointer_fb_offset;
3028 static bool frame_pointer_fb_offset_valid;
3030 static vec<dw_die_ref> base_types;
3032 /* Forward declarations for functions defined in this file. */
3034 static int is_pseudo_reg (const_rtx);
3035 static tree type_main_variant (tree);
3036 static int is_tagged_type (const_tree);
3037 static const char *dwarf_tag_name (unsigned);
3038 static const char *dwarf_attr_name (unsigned);
3039 static const char *dwarf_form_name (unsigned);
3040 static tree decl_ultimate_origin (const_tree);
3041 static tree decl_class_context (tree);
3042 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
3043 static inline enum dw_val_class AT_class (dw_attr_ref);
3044 static inline unsigned int AT_index (dw_attr_ref);
3045 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3046 static inline unsigned AT_flag (dw_attr_ref);
3047 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3048 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
3049 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3050 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
3051 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3052 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3053 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3054 unsigned int, unsigned char *);
3055 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3056 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3057 static inline const char *AT_string (dw_attr_ref);
3058 static enum dwarf_form AT_string_form (dw_attr_ref);
3059 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3060 static void add_AT_specification (dw_die_ref, dw_die_ref);
3061 static inline dw_die_ref AT_ref (dw_attr_ref);
3062 static inline int AT_ref_external (dw_attr_ref);
3063 static inline void set_AT_ref_external (dw_attr_ref, int);
3064 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3065 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3066 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
3067 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3068 dw_loc_list_ref);
3069 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
3070 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3071 static void remove_addr_table_entry (addr_table_entry *);
3072 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3073 static inline rtx AT_addr (dw_attr_ref);
3074 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3075 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3076 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3077 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3078 unsigned HOST_WIDE_INT);
3079 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3080 unsigned long, bool);
3081 static inline const char *AT_lbl (dw_attr_ref);
3082 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
3083 static const char *get_AT_low_pc (dw_die_ref);
3084 static const char *get_AT_hi_pc (dw_die_ref);
3085 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3086 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3087 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3088 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3089 static bool is_cxx (void);
3090 static bool is_fortran (void);
3091 static bool is_ada (void);
3092 static void remove_AT (dw_die_ref, enum dwarf_attribute);
3093 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3094 static void add_child_die (dw_die_ref, dw_die_ref);
3095 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3096 static dw_die_ref lookup_type_die (tree);
3097 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3098 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3099 static void equate_type_number_to_die (tree, dw_die_ref);
3100 static dw_die_ref lookup_decl_die (tree);
3101 static var_loc_list *lookup_decl_loc (const_tree);
3102 static void equate_decl_number_to_die (tree, dw_die_ref);
3103 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3104 static void print_spaces (FILE *);
3105 static void print_die (dw_die_ref, FILE *);
3106 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3107 static dw_die_ref pop_compile_unit (dw_die_ref);
3108 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3109 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
3110 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3111 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3112 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3113 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3114 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
3115 struct md5_ctx *, int *);
3116 struct checksum_attributes;
3117 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3118 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3119 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3120 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3121 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3122 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3123 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
3124 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3125 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3126 static void compute_section_prefix (dw_die_ref);
3127 static int is_type_die (dw_die_ref);
3128 static int is_comdat_die (dw_die_ref);
3129 static int is_symbol_die (dw_die_ref);
3130 static inline bool is_template_instantiation (dw_die_ref);
3131 static void assign_symbol_names (dw_die_ref);
3132 static void break_out_includes (dw_die_ref);
3133 static int is_declaration_die (dw_die_ref);
3134 static int should_move_die_to_comdat (dw_die_ref);
3135 static dw_die_ref clone_as_declaration (dw_die_ref);
3136 static dw_die_ref clone_die (dw_die_ref);
3137 static dw_die_ref clone_tree (dw_die_ref);
3138 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3139 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3140 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3141 static dw_die_ref generate_skeleton (dw_die_ref);
3142 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3143 dw_die_ref,
3144 dw_die_ref);
3145 static void break_out_comdat_types (dw_die_ref);
3146 static void copy_decls_for_unworthy_types (dw_die_ref);
3148 static void add_sibling_attributes (dw_die_ref);
3149 static void output_location_lists (dw_die_ref);
3150 static int constant_size (unsigned HOST_WIDE_INT);
3151 static unsigned long size_of_die (dw_die_ref);
3152 static void calc_die_sizes (dw_die_ref);
3153 static void calc_base_type_die_sizes (void);
3154 static void mark_dies (dw_die_ref);
3155 static void unmark_dies (dw_die_ref);
3156 static void unmark_all_dies (dw_die_ref);
3157 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3158 static unsigned long size_of_aranges (void);
3159 static enum dwarf_form value_format (dw_attr_ref);
3160 static void output_value_format (dw_attr_ref);
3161 static void output_abbrev_section (void);
3162 static void output_die_abbrevs (unsigned long, dw_die_ref);
3163 static void output_die_symbol (dw_die_ref);
3164 static void output_die (dw_die_ref);
3165 static void output_compilation_unit_header (void);
3166 static void output_comp_unit (dw_die_ref, int);
3167 static void output_comdat_type_unit (comdat_type_node *);
3168 static const char *dwarf2_name (tree, int);
3169 static void add_pubname (tree, dw_die_ref);
3170 static void add_enumerator_pubname (const char *, dw_die_ref);
3171 static void add_pubname_string (const char *, dw_die_ref);
3172 static void add_pubtype (tree, dw_die_ref);
3173 static void output_pubnames (vec<pubname_entry, va_gc> *);
3174 static void output_aranges (unsigned long);
3175 static unsigned int add_ranges_num (int);
3176 static unsigned int add_ranges (const_tree);
3177 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3178 bool *, bool);
3179 static void output_ranges (void);
3180 static dw_line_info_table *new_line_info_table (void);
3181 static void output_line_info (bool);
3182 static void output_file_names (void);
3183 static dw_die_ref base_type_die (tree);
3184 static int is_base_type (tree);
3185 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
3186 static int decl_quals (const_tree);
3187 static dw_die_ref modified_type_die (tree, int, dw_die_ref);
3188 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3189 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3190 static int type_is_enum (const_tree);
3191 static unsigned int dbx_reg_number (const_rtx);
3192 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3193 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3194 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3195 enum var_init_status);
3196 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3197 enum var_init_status);
3198 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3199 enum var_init_status);
3200 static int is_based_loc (const_rtx);
3201 static bool resolve_one_addr (rtx *);
3202 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3203 enum var_init_status);
3204 static dw_loc_descr_ref loc_descriptor (rtx, machine_mode mode,
3205 enum var_init_status);
3206 static dw_loc_list_ref loc_list_from_tree (tree, int);
3207 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
3208 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3209 static tree field_type (const_tree);
3210 static unsigned int simple_type_align_in_bits (const_tree);
3211 static unsigned int simple_decl_align_in_bits (const_tree);
3212 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3213 static HOST_WIDE_INT field_byte_offset (const_tree);
3214 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3215 dw_loc_list_ref);
3216 static void add_data_member_location_attribute (dw_die_ref, tree);
3217 static bool add_const_value_attribute (dw_die_ref, rtx);
3218 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3219 static void insert_wide_int (const wide_int &, unsigned char *, int);
3220 static void insert_float (const_rtx, unsigned char *);
3221 static rtx rtl_for_decl_location (tree);
3222 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool,
3223 enum dwarf_attribute);
3224 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3225 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3226 static void add_name_attribute (dw_die_ref, const char *);
3227 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3228 static void add_comp_dir_attribute (dw_die_ref);
3229 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
3230 static void add_subscript_info (dw_die_ref, tree, bool);
3231 static void add_byte_size_attribute (dw_die_ref, tree);
3232 static void add_bit_offset_attribute (dw_die_ref, tree);
3233 static void add_bit_size_attribute (dw_die_ref, tree);
3234 static void add_prototyped_attribute (dw_die_ref, tree);
3235 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3236 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3237 static void add_src_coords_attributes (dw_die_ref, tree);
3238 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3239 static void push_decl_scope (tree);
3240 static void pop_decl_scope (void);
3241 static dw_die_ref scope_die_for (tree, dw_die_ref);
3242 static inline int local_scope_p (dw_die_ref);
3243 static inline int class_scope_p (dw_die_ref);
3244 static inline int class_or_namespace_scope_p (dw_die_ref);
3245 static void add_type_attribute (dw_die_ref, tree, int, dw_die_ref);
3246 static void add_calling_convention_attribute (dw_die_ref, tree);
3247 static const char *type_tag (const_tree);
3248 static tree member_declared_type (const_tree);
3249 #if 0
3250 static const char *decl_start_label (tree);
3251 #endif
3252 static void gen_array_type_die (tree, dw_die_ref);
3253 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3254 #if 0
3255 static void gen_entry_point_die (tree, dw_die_ref);
3256 #endif
3257 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3258 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3259 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3260 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3261 static void gen_formal_types_die (tree, dw_die_ref);
3262 static void gen_subprogram_die (tree, dw_die_ref);
3263 static void gen_variable_die (tree, tree, dw_die_ref);
3264 static void gen_const_die (tree, dw_die_ref);
3265 static void gen_label_die (tree, dw_die_ref);
3266 static void gen_lexical_block_die (tree, dw_die_ref, int);
3267 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
3268 static void gen_field_die (tree, dw_die_ref);
3269 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3270 static dw_die_ref gen_compile_unit_die (const char *);
3271 static void gen_inheritance_die (tree, tree, dw_die_ref);
3272 static void gen_member_die (tree, dw_die_ref);
3273 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3274 enum debug_info_usage);
3275 static void gen_subroutine_type_die (tree, dw_die_ref);
3276 static void gen_typedef_die (tree, dw_die_ref);
3277 static void gen_type_die (tree, dw_die_ref);
3278 static void gen_block_die (tree, dw_die_ref, int);
3279 static void decls_for_scope (tree, dw_die_ref, int);
3280 static inline int is_redundant_typedef (const_tree);
3281 static bool is_naming_typedef_decl (const_tree);
3282 static inline dw_die_ref get_context_die (tree);
3283 static void gen_namespace_die (tree, dw_die_ref);
3284 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3285 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
3286 static dw_die_ref force_decl_die (tree);
3287 static dw_die_ref force_type_die (tree);
3288 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3289 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3290 static struct dwarf_file_data * lookup_filename (const char *);
3291 static void retry_incomplete_types (void);
3292 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3293 static void gen_generic_params_dies (tree);
3294 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3295 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3296 static void splice_child_die (dw_die_ref, dw_die_ref);
3297 static int file_info_cmp (const void *, const void *);
3298 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3299 const char *, const char *);
3300 static void output_loc_list (dw_loc_list_ref);
3301 static char *gen_internal_sym (const char *);
3302 static bool want_pubnames (void);
3304 static void prune_unmark_dies (dw_die_ref);
3305 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3306 static void prune_unused_types_mark (dw_die_ref, int);
3307 static void prune_unused_types_walk (dw_die_ref);
3308 static void prune_unused_types_walk_attribs (dw_die_ref);
3309 static void prune_unused_types_prune (dw_die_ref);
3310 static void prune_unused_types (void);
3311 static int maybe_emit_file (struct dwarf_file_data *fd);
3312 static inline const char *AT_vms_delta1 (dw_attr_ref);
3313 static inline const char *AT_vms_delta2 (dw_attr_ref);
3314 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3315 const char *, const char *);
3316 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3317 static void gen_remaining_tmpl_value_param_die_attribute (void);
3318 static bool generic_type_p (tree);
3319 static void schedule_generic_params_dies_gen (tree t);
3320 static void gen_scheduled_generic_parms_dies (void);
3322 static const char *comp_dir_string (void);
3324 static void hash_loc_operands (dw_loc_descr_ref, inchash::hash &);
3326 /* enum for tracking thread-local variables whose address is really an offset
3327 relative to the TLS pointer, which will need link-time relocation, but will
3328 not need relocation by the DWARF consumer. */
3330 enum dtprel_bool
3332 dtprel_false = 0,
3333 dtprel_true = 1
3336 /* Return the operator to use for an address of a variable. For dtprel_true, we
3337 use DW_OP_const*. For regular variables, which need both link-time
3338 relocation and consumer-level relocation (e.g., to account for shared objects
3339 loaded at a random address), we use DW_OP_addr*. */
3341 static inline enum dwarf_location_atom
3342 dw_addr_op (enum dtprel_bool dtprel)
3344 if (dtprel == dtprel_true)
3345 return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3346 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3347 else
3348 return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3351 /* Return a pointer to a newly allocated address location description. If
3352 dwarf_split_debug_info is true, then record the address with the appropriate
3353 relocation. */
3354 static inline dw_loc_descr_ref
3355 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3357 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3359 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3360 ref->dw_loc_oprnd1.v.val_addr = addr;
3361 ref->dtprel = dtprel;
3362 if (dwarf_split_debug_info)
3363 ref->dw_loc_oprnd1.val_entry
3364 = add_addr_table_entry (addr,
3365 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3366 else
3367 ref->dw_loc_oprnd1.val_entry = NULL;
3369 return ref;
3372 /* Section names used to hold DWARF debugging information. */
3374 #ifndef DEBUG_INFO_SECTION
3375 #define DEBUG_INFO_SECTION ".debug_info"
3376 #endif
3377 #ifndef DEBUG_DWO_INFO_SECTION
3378 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3379 #endif
3380 #ifndef DEBUG_ABBREV_SECTION
3381 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3382 #endif
3383 #ifndef DEBUG_DWO_ABBREV_SECTION
3384 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3385 #endif
3386 #ifndef DEBUG_ARANGES_SECTION
3387 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3388 #endif
3389 #ifndef DEBUG_ADDR_SECTION
3390 #define DEBUG_ADDR_SECTION ".debug_addr"
3391 #endif
3392 #ifndef DEBUG_NORM_MACINFO_SECTION
3393 #define DEBUG_NORM_MACINFO_SECTION ".debug_macinfo"
3394 #endif
3395 #ifndef DEBUG_DWO_MACINFO_SECTION
3396 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
3397 #endif
3398 #ifndef DEBUG_MACINFO_SECTION
3399 #define DEBUG_MACINFO_SECTION \
3400 (!dwarf_split_debug_info \
3401 ? (DEBUG_NORM_MACINFO_SECTION) : (DEBUG_DWO_MACINFO_SECTION))
3402 #endif
3403 #ifndef DEBUG_NORM_MACRO_SECTION
3404 #define DEBUG_NORM_MACRO_SECTION ".debug_macro"
3405 #endif
3406 #ifndef DEBUG_DWO_MACRO_SECTION
3407 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
3408 #endif
3409 #ifndef DEBUG_MACRO_SECTION
3410 #define DEBUG_MACRO_SECTION \
3411 (!dwarf_split_debug_info \
3412 ? (DEBUG_NORM_MACRO_SECTION) : (DEBUG_DWO_MACRO_SECTION))
3413 #endif
3414 #ifndef DEBUG_LINE_SECTION
3415 #define DEBUG_LINE_SECTION ".debug_line"
3416 #endif
3417 #ifndef DEBUG_DWO_LINE_SECTION
3418 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3419 #endif
3420 #ifndef DEBUG_LOC_SECTION
3421 #define DEBUG_LOC_SECTION ".debug_loc"
3422 #endif
3423 #ifndef DEBUG_DWO_LOC_SECTION
3424 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
3425 #endif
3426 #ifndef DEBUG_PUBNAMES_SECTION
3427 #define DEBUG_PUBNAMES_SECTION \
3428 ((debug_generate_pub_sections == 2) \
3429 ? ".debug_gnu_pubnames" : ".debug_pubnames")
3430 #endif
3431 #ifndef DEBUG_PUBTYPES_SECTION
3432 #define DEBUG_PUBTYPES_SECTION \
3433 ((debug_generate_pub_sections == 2) \
3434 ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
3435 #endif
3436 #define DEBUG_NORM_STR_OFFSETS_SECTION ".debug_str_offsets"
3437 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3438 #ifndef DEBUG_STR_OFFSETS_SECTION
3439 #define DEBUG_STR_OFFSETS_SECTION \
3440 (!dwarf_split_debug_info \
3441 ? (DEBUG_NORM_STR_OFFSETS_SECTION) : (DEBUG_DWO_STR_OFFSETS_SECTION))
3442 #endif
3443 #ifndef DEBUG_STR_DWO_SECTION
3444 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
3445 #endif
3446 #ifndef DEBUG_STR_SECTION
3447 #define DEBUG_STR_SECTION ".debug_str"
3448 #endif
3449 #ifndef DEBUG_RANGES_SECTION
3450 #define DEBUG_RANGES_SECTION ".debug_ranges"
3451 #endif
3453 /* Standard ELF section names for compiled code and data. */
3454 #ifndef TEXT_SECTION_NAME
3455 #define TEXT_SECTION_NAME ".text"
3456 #endif
3458 /* Section flags for .debug_macinfo/.debug_macro section. */
3459 #define DEBUG_MACRO_SECTION_FLAGS \
3460 (dwarf_split_debug_info ? SECTION_DEBUG | SECTION_EXCLUDE : SECTION_DEBUG)
3462 /* Section flags for .debug_str section. */
3463 #define DEBUG_STR_SECTION_FLAGS \
3464 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
3465 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3466 : SECTION_DEBUG)
3468 /* Section flags for .debug_str.dwo section. */
3469 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
3471 /* Labels we insert at beginning sections we can reference instead of
3472 the section names themselves. */
3474 #ifndef TEXT_SECTION_LABEL
3475 #define TEXT_SECTION_LABEL "Ltext"
3476 #endif
3477 #ifndef COLD_TEXT_SECTION_LABEL
3478 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
3479 #endif
3480 #ifndef DEBUG_LINE_SECTION_LABEL
3481 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3482 #endif
3483 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3484 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
3485 #endif
3486 #ifndef DEBUG_INFO_SECTION_LABEL
3487 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3488 #endif
3489 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3490 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
3491 #endif
3492 #ifndef DEBUG_ABBREV_SECTION_LABEL
3493 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3494 #endif
3495 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3496 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3497 #endif
3498 #ifndef DEBUG_ADDR_SECTION_LABEL
3499 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
3500 #endif
3501 #ifndef DEBUG_LOC_SECTION_LABEL
3502 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3503 #endif
3504 #ifndef DEBUG_RANGES_SECTION_LABEL
3505 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3506 #endif
3507 #ifndef DEBUG_MACINFO_SECTION_LABEL
3508 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3509 #endif
3510 #ifndef DEBUG_MACRO_SECTION_LABEL
3511 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
3512 #endif
3513 #define SKELETON_COMP_DIE_ABBREV 1
3514 #define SKELETON_TYPE_DIE_ABBREV 2
3516 /* Definitions of defaults for formats and names of various special
3517 (artificial) labels which may be generated within this file (when the -g
3518 options is used and DWARF2_DEBUGGING_INFO is in effect.
3519 If necessary, these may be overridden from within the tm.h file, but
3520 typically, overriding these defaults is unnecessary. */
3522 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3523 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3524 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3525 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3526 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3527 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3528 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3529 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3530 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3531 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3532 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3533 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3534 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3535 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3537 #ifndef TEXT_END_LABEL
3538 #define TEXT_END_LABEL "Letext"
3539 #endif
3540 #ifndef COLD_END_LABEL
3541 #define COLD_END_LABEL "Letext_cold"
3542 #endif
3543 #ifndef BLOCK_BEGIN_LABEL
3544 #define BLOCK_BEGIN_LABEL "LBB"
3545 #endif
3546 #ifndef BLOCK_END_LABEL
3547 #define BLOCK_END_LABEL "LBE"
3548 #endif
3549 #ifndef LINE_CODE_LABEL
3550 #define LINE_CODE_LABEL "LM"
3551 #endif
3554 /* Return the root of the DIE's built for the current compilation unit. */
3555 static dw_die_ref
3556 comp_unit_die (void)
3558 if (!single_comp_unit_die)
3559 single_comp_unit_die = gen_compile_unit_die (NULL);
3560 return single_comp_unit_die;
3563 /* We allow a language front-end to designate a function that is to be
3564 called to "demangle" any name before it is put into a DIE. */
3566 static const char *(*demangle_name_func) (const char *);
3568 void
3569 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3571 demangle_name_func = func;
3574 /* Test if rtl node points to a pseudo register. */
3576 static inline int
3577 is_pseudo_reg (const_rtx rtl)
3579 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3580 || (GET_CODE (rtl) == SUBREG
3581 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3584 /* Return a reference to a type, with its const and volatile qualifiers
3585 removed. */
3587 static inline tree
3588 type_main_variant (tree type)
3590 type = TYPE_MAIN_VARIANT (type);
3592 /* ??? There really should be only one main variant among any group of
3593 variants of a given type (and all of the MAIN_VARIANT values for all
3594 members of the group should point to that one type) but sometimes the C
3595 front-end messes this up for array types, so we work around that bug
3596 here. */
3597 if (TREE_CODE (type) == ARRAY_TYPE)
3598 while (type != TYPE_MAIN_VARIANT (type))
3599 type = TYPE_MAIN_VARIANT (type);
3601 return type;
3604 /* Return nonzero if the given type node represents a tagged type. */
3606 static inline int
3607 is_tagged_type (const_tree type)
3609 enum tree_code code = TREE_CODE (type);
3611 return (code == RECORD_TYPE || code == UNION_TYPE
3612 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3615 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
3617 static void
3618 get_ref_die_offset_label (char *label, dw_die_ref ref)
3620 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3623 /* Return die_offset of a DIE reference to a base type. */
3625 static unsigned long int
3626 get_base_type_offset (dw_die_ref ref)
3628 if (ref->die_offset)
3629 return ref->die_offset;
3630 if (comp_unit_die ()->die_abbrev)
3632 calc_base_type_die_sizes ();
3633 gcc_assert (ref->die_offset);
3635 return ref->die_offset;
3638 /* Return die_offset of a DIE reference other than base type. */
3640 static unsigned long int
3641 get_ref_die_offset (dw_die_ref ref)
3643 gcc_assert (ref->die_offset);
3644 return ref->die_offset;
3647 /* Convert a DIE tag into its string name. */
3649 static const char *
3650 dwarf_tag_name (unsigned int tag)
3652 const char *name = get_DW_TAG_name (tag);
3654 if (name != NULL)
3655 return name;
3657 return "DW_TAG_<unknown>";
3660 /* Convert a DWARF attribute code into its string name. */
3662 static const char *
3663 dwarf_attr_name (unsigned int attr)
3665 const char *name;
3667 switch (attr)
3669 #if VMS_DEBUGGING_INFO
3670 case DW_AT_HP_prologue:
3671 return "DW_AT_HP_prologue";
3672 #else
3673 case DW_AT_MIPS_loop_unroll_factor:
3674 return "DW_AT_MIPS_loop_unroll_factor";
3675 #endif
3677 #if VMS_DEBUGGING_INFO
3678 case DW_AT_HP_epilogue:
3679 return "DW_AT_HP_epilogue";
3680 #else
3681 case DW_AT_MIPS_stride:
3682 return "DW_AT_MIPS_stride";
3683 #endif
3686 name = get_DW_AT_name (attr);
3688 if (name != NULL)
3689 return name;
3691 return "DW_AT_<unknown>";
3694 /* Convert a DWARF value form code into its string name. */
3696 static const char *
3697 dwarf_form_name (unsigned int form)
3699 const char *name = get_DW_FORM_name (form);
3701 if (name != NULL)
3702 return name;
3704 return "DW_FORM_<unknown>";
3707 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
3708 instance of an inlined instance of a decl which is local to an inline
3709 function, so we have to trace all of the way back through the origin chain
3710 to find out what sort of node actually served as the original seed for the
3711 given block. */
3713 static tree
3714 decl_ultimate_origin (const_tree decl)
3716 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
3717 return NULL_TREE;
3719 /* DECL_ABSTRACT_ORIGIN can point to itself; ignore that if
3720 we're trying to output the abstract instance of this function. */
3721 if (DECL_ABSTRACT_P (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
3722 return NULL_TREE;
3724 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
3725 most distant ancestor, this should never happen. */
3726 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
3728 return DECL_ABSTRACT_ORIGIN (decl);
3731 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
3732 of a virtual function may refer to a base class, so we check the 'this'
3733 parameter. */
3735 static tree
3736 decl_class_context (tree decl)
3738 tree context = NULL_TREE;
3740 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
3741 context = DECL_CONTEXT (decl);
3742 else
3743 context = TYPE_MAIN_VARIANT
3744 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
3746 if (context && !TYPE_P (context))
3747 context = NULL_TREE;
3749 return context;
3752 /* Add an attribute/value pair to a DIE. */
3754 static inline void
3755 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
3757 /* Maybe this should be an assert? */
3758 if (die == NULL)
3759 return;
3761 vec_safe_reserve (die->die_attr, 1);
3762 vec_safe_push (die->die_attr, *attr);
3765 static inline enum dw_val_class
3766 AT_class (dw_attr_ref a)
3768 return a->dw_attr_val.val_class;
3771 /* Return the index for any attribute that will be referenced with a
3772 DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index. String indices
3773 are stored in dw_attr_val.v.val_str for reference counting
3774 pruning. */
3776 static inline unsigned int
3777 AT_index (dw_attr_ref a)
3779 if (AT_class (a) == dw_val_class_str)
3780 return a->dw_attr_val.v.val_str->index;
3781 else if (a->dw_attr_val.val_entry != NULL)
3782 return a->dw_attr_val.val_entry->index;
3783 return NOT_INDEXED;
3786 /* Add a flag value attribute to a DIE. */
3788 static inline void
3789 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
3791 dw_attr_node attr;
3793 attr.dw_attr = attr_kind;
3794 attr.dw_attr_val.val_class = dw_val_class_flag;
3795 attr.dw_attr_val.val_entry = NULL;
3796 attr.dw_attr_val.v.val_flag = flag;
3797 add_dwarf_attr (die, &attr);
3800 static inline unsigned
3801 AT_flag (dw_attr_ref a)
3803 gcc_assert (a && AT_class (a) == dw_val_class_flag);
3804 return a->dw_attr_val.v.val_flag;
3807 /* Add a signed integer attribute value to a DIE. */
3809 static inline void
3810 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
3812 dw_attr_node attr;
3814 attr.dw_attr = attr_kind;
3815 attr.dw_attr_val.val_class = dw_val_class_const;
3816 attr.dw_attr_val.val_entry = NULL;
3817 attr.dw_attr_val.v.val_int = int_val;
3818 add_dwarf_attr (die, &attr);
3821 static inline HOST_WIDE_INT
3822 AT_int (dw_attr_ref a)
3824 gcc_assert (a && AT_class (a) == dw_val_class_const);
3825 return a->dw_attr_val.v.val_int;
3828 /* Add an unsigned integer attribute value to a DIE. */
3830 static inline void
3831 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
3832 unsigned HOST_WIDE_INT unsigned_val)
3834 dw_attr_node attr;
3836 attr.dw_attr = attr_kind;
3837 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
3838 attr.dw_attr_val.val_entry = NULL;
3839 attr.dw_attr_val.v.val_unsigned = unsigned_val;
3840 add_dwarf_attr (die, &attr);
3843 static inline unsigned HOST_WIDE_INT
3844 AT_unsigned (dw_attr_ref a)
3846 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
3847 return a->dw_attr_val.v.val_unsigned;
3850 /* Add an unsigned wide integer attribute value to a DIE. */
3852 static inline void
3853 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
3854 const wide_int& w)
3856 dw_attr_node attr;
3858 attr.dw_attr = attr_kind;
3859 attr.dw_attr_val.val_class = dw_val_class_wide_int;
3860 attr.dw_attr_val.v.val_wide = ggc_cleared_alloc<wide_int> ();
3861 *attr.dw_attr_val.v.val_wide = w;
3862 add_dwarf_attr (die, &attr);
3865 /* Add an unsigned double integer attribute value to a DIE. */
3867 static inline void
3868 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
3869 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
3871 dw_attr_node attr;
3873 attr.dw_attr = attr_kind;
3874 attr.dw_attr_val.val_class = dw_val_class_const_double;
3875 attr.dw_attr_val.val_entry = NULL;
3876 attr.dw_attr_val.v.val_double.high = high;
3877 attr.dw_attr_val.v.val_double.low = low;
3878 add_dwarf_attr (die, &attr);
3881 /* Add a floating point attribute value to a DIE and return it. */
3883 static inline void
3884 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
3885 unsigned int length, unsigned int elt_size, unsigned char *array)
3887 dw_attr_node attr;
3889 attr.dw_attr = attr_kind;
3890 attr.dw_attr_val.val_class = dw_val_class_vec;
3891 attr.dw_attr_val.val_entry = NULL;
3892 attr.dw_attr_val.v.val_vec.length = length;
3893 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
3894 attr.dw_attr_val.v.val_vec.array = array;
3895 add_dwarf_attr (die, &attr);
3898 /* Add an 8-byte data attribute value to a DIE. */
3900 static inline void
3901 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
3902 unsigned char data8[8])
3904 dw_attr_node attr;
3906 attr.dw_attr = attr_kind;
3907 attr.dw_attr_val.val_class = dw_val_class_data8;
3908 attr.dw_attr_val.val_entry = NULL;
3909 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
3910 add_dwarf_attr (die, &attr);
3913 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
3914 dwarf_split_debug_info, address attributes in dies destined for the
3915 final executable have force_direct set to avoid using indexed
3916 references. */
3918 static inline void
3919 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
3920 bool force_direct)
3922 dw_attr_node attr;
3923 char * lbl_id;
3925 lbl_id = xstrdup (lbl_low);
3926 attr.dw_attr = DW_AT_low_pc;
3927 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3928 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3929 if (dwarf_split_debug_info && !force_direct)
3930 attr.dw_attr_val.val_entry
3931 = add_addr_table_entry (lbl_id, ate_kind_label);
3932 else
3933 attr.dw_attr_val.val_entry = NULL;
3934 add_dwarf_attr (die, &attr);
3936 attr.dw_attr = DW_AT_high_pc;
3937 if (dwarf_version < 4)
3938 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3939 else
3940 attr.dw_attr_val.val_class = dw_val_class_high_pc;
3941 lbl_id = xstrdup (lbl_high);
3942 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3943 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
3944 && dwarf_split_debug_info && !force_direct)
3945 attr.dw_attr_val.val_entry
3946 = add_addr_table_entry (lbl_id, ate_kind_label);
3947 else
3948 attr.dw_attr_val.val_entry = NULL;
3949 add_dwarf_attr (die, &attr);
3952 /* Hash and equality functions for debug_str_hash. */
3954 hashval_t
3955 indirect_string_hasher::hash (indirect_string_node *x)
3957 return htab_hash_string (x->str);
3960 bool
3961 indirect_string_hasher::equal (indirect_string_node *x1, const char *x2)
3963 return strcmp (x1->str, x2) == 0;
3966 /* Add STR to the given string hash table. */
3968 static struct indirect_string_node *
3969 find_AT_string_in_table (const char *str,
3970 hash_table<indirect_string_hasher> *table)
3972 struct indirect_string_node *node;
3974 indirect_string_node **slot
3975 = table->find_slot_with_hash (str, htab_hash_string (str), INSERT);
3976 if (*slot == NULL)
3978 node = ggc_cleared_alloc<indirect_string_node> ();
3979 node->str = ggc_strdup (str);
3980 *slot = node;
3982 else
3983 node = *slot;
3985 node->refcount++;
3986 return node;
3989 /* Add STR to the indirect string hash table. */
3991 static struct indirect_string_node *
3992 find_AT_string (const char *str)
3994 if (! debug_str_hash)
3995 debug_str_hash = hash_table<indirect_string_hasher>::create_ggc (10);
3997 return find_AT_string_in_table (str, debug_str_hash);
4000 /* Add a string attribute value to a DIE. */
4002 static inline void
4003 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4005 dw_attr_node attr;
4006 struct indirect_string_node *node;
4008 node = find_AT_string (str);
4010 attr.dw_attr = attr_kind;
4011 attr.dw_attr_val.val_class = dw_val_class_str;
4012 attr.dw_attr_val.val_entry = NULL;
4013 attr.dw_attr_val.v.val_str = node;
4014 add_dwarf_attr (die, &attr);
4017 static inline const char *
4018 AT_string (dw_attr_ref a)
4020 gcc_assert (a && AT_class (a) == dw_val_class_str);
4021 return a->dw_attr_val.v.val_str->str;
4024 /* Call this function directly to bypass AT_string_form's logic to put
4025 the string inline in the die. */
4027 static void
4028 set_indirect_string (struct indirect_string_node *node)
4030 char label[32];
4031 /* Already indirect is a no op. */
4032 if (node->form == DW_FORM_strp || node->form == DW_FORM_GNU_str_index)
4034 gcc_assert (node->label);
4035 return;
4037 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4038 ++dw2_string_counter;
4039 node->label = xstrdup (label);
4041 if (!dwarf_split_debug_info)
4043 node->form = DW_FORM_strp;
4044 node->index = NOT_INDEXED;
4046 else
4048 node->form = DW_FORM_GNU_str_index;
4049 node->index = NO_INDEX_ASSIGNED;
4053 /* Find out whether a string should be output inline in DIE
4054 or out-of-line in .debug_str section. */
4056 static enum dwarf_form
4057 find_string_form (struct indirect_string_node *node)
4059 unsigned int len;
4061 if (node->form)
4062 return node->form;
4064 len = strlen (node->str) + 1;
4066 /* If the string is shorter or equal to the size of the reference, it is
4067 always better to put it inline. */
4068 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4069 return node->form = DW_FORM_string;
4071 /* If we cannot expect the linker to merge strings in .debug_str
4072 section, only put it into .debug_str if it is worth even in this
4073 single module. */
4074 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4075 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4076 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
4077 return node->form = DW_FORM_string;
4079 set_indirect_string (node);
4081 return node->form;
4084 /* Find out whether the string referenced from the attribute should be
4085 output inline in DIE or out-of-line in .debug_str section. */
4087 static enum dwarf_form
4088 AT_string_form (dw_attr_ref a)
4090 gcc_assert (a && AT_class (a) == dw_val_class_str);
4091 return find_string_form (a->dw_attr_val.v.val_str);
4094 /* Add a DIE reference attribute value to a DIE. */
4096 static inline void
4097 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4099 dw_attr_node attr;
4101 #ifdef ENABLE_CHECKING
4102 gcc_assert (targ_die != NULL);
4103 #else
4104 /* With LTO we can end up trying to reference something we didn't create
4105 a DIE for. Avoid crashing later on a NULL referenced DIE. */
4106 if (targ_die == NULL)
4107 return;
4108 #endif
4110 attr.dw_attr = attr_kind;
4111 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4112 attr.dw_attr_val.val_entry = NULL;
4113 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4114 attr.dw_attr_val.v.val_die_ref.external = 0;
4115 add_dwarf_attr (die, &attr);
4118 /* Change DIE reference REF to point to NEW_DIE instead. */
4120 static inline void
4121 change_AT_die_ref (dw_attr_ref ref, dw_die_ref new_die)
4123 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4124 ref->dw_attr_val.v.val_die_ref.die = new_die;
4125 ref->dw_attr_val.v.val_die_ref.external = 0;
4128 /* Add an AT_specification attribute to a DIE, and also make the back
4129 pointer from the specification to the definition. */
4131 static inline void
4132 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4134 add_AT_die_ref (die, DW_AT_specification, targ_die);
4135 gcc_assert (!targ_die->die_definition);
4136 targ_die->die_definition = die;
4139 static inline dw_die_ref
4140 AT_ref (dw_attr_ref a)
4142 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4143 return a->dw_attr_val.v.val_die_ref.die;
4146 static inline int
4147 AT_ref_external (dw_attr_ref a)
4149 if (a && AT_class (a) == dw_val_class_die_ref)
4150 return a->dw_attr_val.v.val_die_ref.external;
4152 return 0;
4155 static inline void
4156 set_AT_ref_external (dw_attr_ref a, int i)
4158 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4159 a->dw_attr_val.v.val_die_ref.external = i;
4162 /* Add an FDE reference attribute value to a DIE. */
4164 static inline void
4165 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4167 dw_attr_node attr;
4169 attr.dw_attr = attr_kind;
4170 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4171 attr.dw_attr_val.val_entry = NULL;
4172 attr.dw_attr_val.v.val_fde_index = targ_fde;
4173 add_dwarf_attr (die, &attr);
4176 /* Add a location description attribute value to a DIE. */
4178 static inline void
4179 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4181 dw_attr_node attr;
4183 attr.dw_attr = attr_kind;
4184 attr.dw_attr_val.val_class = dw_val_class_loc;
4185 attr.dw_attr_val.val_entry = NULL;
4186 attr.dw_attr_val.v.val_loc = loc;
4187 add_dwarf_attr (die, &attr);
4190 static inline dw_loc_descr_ref
4191 AT_loc (dw_attr_ref a)
4193 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4194 return a->dw_attr_val.v.val_loc;
4197 static inline void
4198 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4200 dw_attr_node attr;
4202 attr.dw_attr = attr_kind;
4203 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4204 attr.dw_attr_val.val_entry = NULL;
4205 attr.dw_attr_val.v.val_loc_list = loc_list;
4206 add_dwarf_attr (die, &attr);
4207 have_location_lists = true;
4210 static inline dw_loc_list_ref
4211 AT_loc_list (dw_attr_ref a)
4213 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4214 return a->dw_attr_val.v.val_loc_list;
4217 static inline dw_loc_list_ref *
4218 AT_loc_list_ptr (dw_attr_ref a)
4220 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4221 return &a->dw_attr_val.v.val_loc_list;
4224 struct addr_hasher : ggc_hasher<addr_table_entry *>
4226 static hashval_t hash (addr_table_entry *);
4227 static bool equal (addr_table_entry *, addr_table_entry *);
4230 /* Table of entries into the .debug_addr section. */
4232 static GTY (()) hash_table<addr_hasher> *addr_index_table;
4234 /* Hash an address_table_entry. */
4236 hashval_t
4237 addr_hasher::hash (addr_table_entry *a)
4239 inchash::hash hstate;
4240 switch (a->kind)
4242 case ate_kind_rtx:
4243 hstate.add_int (0);
4244 break;
4245 case ate_kind_rtx_dtprel:
4246 hstate.add_int (1);
4247 break;
4248 case ate_kind_label:
4249 return htab_hash_string (a->addr.label);
4250 default:
4251 gcc_unreachable ();
4253 inchash::add_rtx (a->addr.rtl, hstate);
4254 return hstate.end ();
4257 /* Determine equality for two address_table_entries. */
4259 bool
4260 addr_hasher::equal (addr_table_entry *a1, addr_table_entry *a2)
4262 if (a1->kind != a2->kind)
4263 return 0;
4264 switch (a1->kind)
4266 case ate_kind_rtx:
4267 case ate_kind_rtx_dtprel:
4268 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4269 case ate_kind_label:
4270 return strcmp (a1->addr.label, a2->addr.label) == 0;
4271 default:
4272 gcc_unreachable ();
4276 /* Initialize an addr_table_entry. */
4278 void
4279 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4281 e->kind = kind;
4282 switch (kind)
4284 case ate_kind_rtx:
4285 case ate_kind_rtx_dtprel:
4286 e->addr.rtl = (rtx) addr;
4287 break;
4288 case ate_kind_label:
4289 e->addr.label = (char *) addr;
4290 break;
4292 e->refcount = 0;
4293 e->index = NO_INDEX_ASSIGNED;
4296 /* Add attr to the address table entry to the table. Defer setting an
4297 index until output time. */
4299 static addr_table_entry *
4300 add_addr_table_entry (void *addr, enum ate_kind kind)
4302 addr_table_entry *node;
4303 addr_table_entry finder;
4305 gcc_assert (dwarf_split_debug_info);
4306 if (! addr_index_table)
4307 addr_index_table = hash_table<addr_hasher>::create_ggc (10);
4308 init_addr_table_entry (&finder, kind, addr);
4309 addr_table_entry **slot = addr_index_table->find_slot (&finder, INSERT);
4311 if (*slot == HTAB_EMPTY_ENTRY)
4313 node = ggc_cleared_alloc<addr_table_entry> ();
4314 init_addr_table_entry (node, kind, addr);
4315 *slot = node;
4317 else
4318 node = *slot;
4320 node->refcount++;
4321 return node;
4324 /* Remove an entry from the addr table by decrementing its refcount.
4325 Strictly, decrementing the refcount would be enough, but the
4326 assertion that the entry is actually in the table has found
4327 bugs. */
4329 static void
4330 remove_addr_table_entry (addr_table_entry *entry)
4332 gcc_assert (dwarf_split_debug_info && addr_index_table);
4333 /* After an index is assigned, the table is frozen. */
4334 gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
4335 entry->refcount--;
4338 /* Given a location list, remove all addresses it refers to from the
4339 address_table. */
4341 static void
4342 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4344 for (; descr; descr = descr->dw_loc_next)
4345 if (descr->dw_loc_oprnd1.val_entry != NULL)
4347 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4348 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4352 /* A helper function for dwarf2out_finish called through
4353 htab_traverse. Assign an addr_table_entry its index. All entries
4354 must be collected into the table when this function is called,
4355 because the indexing code relies on htab_traverse to traverse nodes
4356 in the same order for each run. */
4359 index_addr_table_entry (addr_table_entry **h, unsigned int *index)
4361 addr_table_entry *node = *h;
4363 /* Don't index unreferenced nodes. */
4364 if (node->refcount == 0)
4365 return 1;
4367 gcc_assert (node->index == NO_INDEX_ASSIGNED);
4368 node->index = *index;
4369 *index += 1;
4371 return 1;
4374 /* Add an address constant attribute value to a DIE. When using
4375 dwarf_split_debug_info, address attributes in dies destined for the
4376 final executable should be direct references--setting the parameter
4377 force_direct ensures this behavior. */
4379 static inline void
4380 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4381 bool force_direct)
4383 dw_attr_node attr;
4385 attr.dw_attr = attr_kind;
4386 attr.dw_attr_val.val_class = dw_val_class_addr;
4387 attr.dw_attr_val.v.val_addr = addr;
4388 if (dwarf_split_debug_info && !force_direct)
4389 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4390 else
4391 attr.dw_attr_val.val_entry = NULL;
4392 add_dwarf_attr (die, &attr);
4395 /* Get the RTX from to an address DIE attribute. */
4397 static inline rtx
4398 AT_addr (dw_attr_ref a)
4400 gcc_assert (a && AT_class (a) == dw_val_class_addr);
4401 return a->dw_attr_val.v.val_addr;
4404 /* Add a file attribute value to a DIE. */
4406 static inline void
4407 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4408 struct dwarf_file_data *fd)
4410 dw_attr_node attr;
4412 attr.dw_attr = attr_kind;
4413 attr.dw_attr_val.val_class = dw_val_class_file;
4414 attr.dw_attr_val.val_entry = NULL;
4415 attr.dw_attr_val.v.val_file = fd;
4416 add_dwarf_attr (die, &attr);
4419 /* Get the dwarf_file_data from a file DIE attribute. */
4421 static inline struct dwarf_file_data *
4422 AT_file (dw_attr_ref a)
4424 gcc_assert (a && AT_class (a) == dw_val_class_file);
4425 return a->dw_attr_val.v.val_file;
4428 /* Add a vms delta attribute value to a DIE. */
4430 static inline void
4431 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4432 const char *lbl1, const char *lbl2)
4434 dw_attr_node attr;
4436 attr.dw_attr = attr_kind;
4437 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4438 attr.dw_attr_val.val_entry = NULL;
4439 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4440 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4441 add_dwarf_attr (die, &attr);
4444 /* Add a label identifier attribute value to a DIE. */
4446 static inline void
4447 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4448 const char *lbl_id)
4450 dw_attr_node attr;
4452 attr.dw_attr = attr_kind;
4453 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4454 attr.dw_attr_val.val_entry = NULL;
4455 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4456 if (dwarf_split_debug_info)
4457 attr.dw_attr_val.val_entry
4458 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4459 ate_kind_label);
4460 add_dwarf_attr (die, &attr);
4463 /* Add a section offset attribute value to a DIE, an offset into the
4464 debug_line section. */
4466 static inline void
4467 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4468 const char *label)
4470 dw_attr_node attr;
4472 attr.dw_attr = attr_kind;
4473 attr.dw_attr_val.val_class = dw_val_class_lineptr;
4474 attr.dw_attr_val.val_entry = NULL;
4475 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4476 add_dwarf_attr (die, &attr);
4479 /* Add a section offset attribute value to a DIE, an offset into the
4480 debug_macinfo section. */
4482 static inline void
4483 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4484 const char *label)
4486 dw_attr_node attr;
4488 attr.dw_attr = attr_kind;
4489 attr.dw_attr_val.val_class = dw_val_class_macptr;
4490 attr.dw_attr_val.val_entry = NULL;
4491 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4492 add_dwarf_attr (die, &attr);
4495 /* Add an offset attribute value to a DIE. */
4497 static inline void
4498 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4499 unsigned HOST_WIDE_INT offset)
4501 dw_attr_node attr;
4503 attr.dw_attr = attr_kind;
4504 attr.dw_attr_val.val_class = dw_val_class_offset;
4505 attr.dw_attr_val.val_entry = NULL;
4506 attr.dw_attr_val.v.val_offset = offset;
4507 add_dwarf_attr (die, &attr);
4510 /* Add a range_list attribute value to a DIE. When using
4511 dwarf_split_debug_info, address attributes in dies destined for the
4512 final executable should be direct references--setting the parameter
4513 force_direct ensures this behavior. */
4515 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4516 #define RELOCATED_OFFSET (NULL)
4518 static void
4519 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4520 long unsigned int offset, bool force_direct)
4522 dw_attr_node attr;
4524 attr.dw_attr = attr_kind;
4525 attr.dw_attr_val.val_class = dw_val_class_range_list;
4526 /* For the range_list attribute, use val_entry to store whether the
4527 offset should follow split-debug-info or normal semantics. This
4528 value is read in output_range_list_offset. */
4529 if (dwarf_split_debug_info && !force_direct)
4530 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4531 else
4532 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4533 attr.dw_attr_val.v.val_offset = offset;
4534 add_dwarf_attr (die, &attr);
4537 /* Return the start label of a delta attribute. */
4539 static inline const char *
4540 AT_vms_delta1 (dw_attr_ref a)
4542 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4543 return a->dw_attr_val.v.val_vms_delta.lbl1;
4546 /* Return the end label of a delta attribute. */
4548 static inline const char *
4549 AT_vms_delta2 (dw_attr_ref a)
4551 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4552 return a->dw_attr_val.v.val_vms_delta.lbl2;
4555 static inline const char *
4556 AT_lbl (dw_attr_ref a)
4558 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4559 || AT_class (a) == dw_val_class_lineptr
4560 || AT_class (a) == dw_val_class_macptr
4561 || AT_class (a) == dw_val_class_high_pc));
4562 return a->dw_attr_val.v.val_lbl_id;
4565 /* Get the attribute of type attr_kind. */
4567 static dw_attr_ref
4568 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4570 dw_attr_ref a;
4571 unsigned ix;
4572 dw_die_ref spec = NULL;
4574 if (! die)
4575 return NULL;
4577 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4578 if (a->dw_attr == attr_kind)
4579 return a;
4580 else if (a->dw_attr == DW_AT_specification
4581 || a->dw_attr == DW_AT_abstract_origin)
4582 spec = AT_ref (a);
4584 if (spec)
4585 return get_AT (spec, attr_kind);
4587 return NULL;
4590 /* Returns the parent of the declaration of DIE. */
4592 static dw_die_ref
4593 get_die_parent (dw_die_ref die)
4595 dw_die_ref t;
4597 if (!die)
4598 return NULL;
4600 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4601 || (t = get_AT_ref (die, DW_AT_specification)))
4602 die = t;
4604 return die->die_parent;
4607 /* Return the "low pc" attribute value, typically associated with a subprogram
4608 DIE. Return null if the "low pc" attribute is either not present, or if it
4609 cannot be represented as an assembler label identifier. */
4611 static inline const char *
4612 get_AT_low_pc (dw_die_ref die)
4614 dw_attr_ref a = get_AT (die, DW_AT_low_pc);
4616 return a ? AT_lbl (a) : NULL;
4619 /* Return the "high pc" attribute value, typically associated with a subprogram
4620 DIE. Return null if the "high pc" attribute is either not present, or if it
4621 cannot be represented as an assembler label identifier. */
4623 static inline const char *
4624 get_AT_hi_pc (dw_die_ref die)
4626 dw_attr_ref a = get_AT (die, DW_AT_high_pc);
4628 return a ? AT_lbl (a) : NULL;
4631 /* Return the value of the string attribute designated by ATTR_KIND, or
4632 NULL if it is not present. */
4634 static inline const char *
4635 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4637 dw_attr_ref a = get_AT (die, attr_kind);
4639 return a ? AT_string (a) : NULL;
4642 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4643 if it is not present. */
4645 static inline int
4646 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4648 dw_attr_ref a = get_AT (die, attr_kind);
4650 return a ? AT_flag (a) : 0;
4653 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4654 if it is not present. */
4656 static inline unsigned
4657 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4659 dw_attr_ref a = get_AT (die, attr_kind);
4661 return a ? AT_unsigned (a) : 0;
4664 static inline dw_die_ref
4665 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4667 dw_attr_ref a = get_AT (die, attr_kind);
4669 return a ? AT_ref (a) : NULL;
4672 static inline struct dwarf_file_data *
4673 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4675 dw_attr_ref a = get_AT (die, attr_kind);
4677 return a ? AT_file (a) : NULL;
4680 /* Return TRUE if the language is C++. */
4682 static inline bool
4683 is_cxx (void)
4685 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4687 return (lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus
4688 || lang == DW_LANG_C_plus_plus_11 || lang == DW_LANG_C_plus_plus_14);
4691 /* Return TRUE if the language is Java. */
4693 static inline bool
4694 is_java (void)
4696 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4698 return lang == DW_LANG_Java;
4701 /* Return TRUE if the language is Fortran. */
4703 static inline bool
4704 is_fortran (void)
4706 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4708 return (lang == DW_LANG_Fortran77
4709 || lang == DW_LANG_Fortran90
4710 || lang == DW_LANG_Fortran95);
4713 /* Return TRUE if the language is Ada. */
4715 static inline bool
4716 is_ada (void)
4718 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4720 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4723 /* Remove the specified attribute if present. */
4725 static void
4726 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4728 dw_attr_ref a;
4729 unsigned ix;
4731 if (! die)
4732 return;
4734 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4735 if (a->dw_attr == attr_kind)
4737 if (AT_class (a) == dw_val_class_str)
4738 if (a->dw_attr_val.v.val_str->refcount)
4739 a->dw_attr_val.v.val_str->refcount--;
4741 /* vec::ordered_remove should help reduce the number of abbrevs
4742 that are needed. */
4743 die->die_attr->ordered_remove (ix);
4744 return;
4748 /* Remove CHILD from its parent. PREV must have the property that
4749 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
4751 static void
4752 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4754 gcc_assert (child->die_parent == prev->die_parent);
4755 gcc_assert (prev->die_sib == child);
4756 if (prev == child)
4758 gcc_assert (child->die_parent->die_child == child);
4759 prev = NULL;
4761 else
4762 prev->die_sib = child->die_sib;
4763 if (child->die_parent->die_child == child)
4764 child->die_parent->die_child = prev;
4767 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
4768 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
4770 static void
4771 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4773 dw_die_ref parent = old_child->die_parent;
4775 gcc_assert (parent == prev->die_parent);
4776 gcc_assert (prev->die_sib == old_child);
4778 new_child->die_parent = parent;
4779 if (prev == old_child)
4781 gcc_assert (parent->die_child == old_child);
4782 new_child->die_sib = new_child;
4784 else
4786 prev->die_sib = new_child;
4787 new_child->die_sib = old_child->die_sib;
4789 if (old_child->die_parent->die_child == old_child)
4790 old_child->die_parent->die_child = new_child;
4793 /* Move all children from OLD_PARENT to NEW_PARENT. */
4795 static void
4796 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4798 dw_die_ref c;
4799 new_parent->die_child = old_parent->die_child;
4800 old_parent->die_child = NULL;
4801 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4804 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
4805 matches TAG. */
4807 static void
4808 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4810 dw_die_ref c;
4812 c = die->die_child;
4813 if (c) do {
4814 dw_die_ref prev = c;
4815 c = c->die_sib;
4816 while (c->die_tag == tag)
4818 remove_child_with_prev (c, prev);
4819 /* Might have removed every child. */
4820 if (c == c->die_sib)
4821 return;
4822 c = c->die_sib;
4824 } while (c != die->die_child);
4827 /* Add a CHILD_DIE as the last child of DIE. */
4829 static void
4830 add_child_die (dw_die_ref die, dw_die_ref child_die)
4832 /* FIXME this should probably be an assert. */
4833 if (! die || ! child_die)
4834 return;
4835 gcc_assert (die != child_die);
4837 child_die->die_parent = die;
4838 if (die->die_child)
4840 child_die->die_sib = die->die_child->die_sib;
4841 die->die_child->die_sib = child_die;
4843 else
4844 child_die->die_sib = child_die;
4845 die->die_child = child_die;
4848 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
4849 is the specification, to the end of PARENT's list of children.
4850 This is done by removing and re-adding it. */
4852 static void
4853 splice_child_die (dw_die_ref parent, dw_die_ref child)
4855 dw_die_ref p;
4857 /* We want the declaration DIE from inside the class, not the
4858 specification DIE at toplevel. */
4859 if (child->die_parent != parent)
4861 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
4863 if (tmp)
4864 child = tmp;
4867 gcc_assert (child->die_parent == parent
4868 || (child->die_parent
4869 == get_AT_ref (parent, DW_AT_specification)));
4871 for (p = child->die_parent->die_child; ; p = p->die_sib)
4872 if (p->die_sib == child)
4874 remove_child_with_prev (child, p);
4875 break;
4878 add_child_die (parent, child);
4881 /* Return a pointer to a newly created DIE node. */
4883 static inline dw_die_ref
4884 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
4886 dw_die_ref die = ggc_cleared_alloc<die_node> ();
4888 die->die_tag = tag_value;
4890 if (parent_die != NULL)
4891 add_child_die (parent_die, die);
4892 else
4894 limbo_die_node *limbo_node;
4896 limbo_node = ggc_cleared_alloc<limbo_die_node> ();
4897 limbo_node->die = die;
4898 limbo_node->created_for = t;
4899 limbo_node->next = limbo_die_list;
4900 limbo_die_list = limbo_node;
4903 return die;
4906 /* Return the DIE associated with the given type specifier. */
4908 static inline dw_die_ref
4909 lookup_type_die (tree type)
4911 return TYPE_SYMTAB_DIE (type);
4914 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
4915 anonymous type named by the typedef TYPE_DIE, return the DIE of the
4916 anonymous type instead the one of the naming typedef. */
4918 static inline dw_die_ref
4919 strip_naming_typedef (tree type, dw_die_ref type_die)
4921 if (type
4922 && TREE_CODE (type) == RECORD_TYPE
4923 && type_die
4924 && type_die->die_tag == DW_TAG_typedef
4925 && is_naming_typedef_decl (TYPE_NAME (type)))
4926 type_die = get_AT_ref (type_die, DW_AT_type);
4927 return type_die;
4930 /* Like lookup_type_die, but if type is an anonymous type named by a
4931 typedef[1], return the DIE of the anonymous type instead the one of
4932 the naming typedef. This is because in gen_typedef_die, we did
4933 equate the anonymous struct named by the typedef with the DIE of
4934 the naming typedef. So by default, lookup_type_die on an anonymous
4935 struct yields the DIE of the naming typedef.
4937 [1]: Read the comment of is_naming_typedef_decl to learn about what
4938 a naming typedef is. */
4940 static inline dw_die_ref
4941 lookup_type_die_strip_naming_typedef (tree type)
4943 dw_die_ref die = lookup_type_die (type);
4944 return strip_naming_typedef (type, die);
4947 /* Equate a DIE to a given type specifier. */
4949 static inline void
4950 equate_type_number_to_die (tree type, dw_die_ref type_die)
4952 TYPE_SYMTAB_DIE (type) = type_die;
4955 /* Returns a hash value for X (which really is a die_struct). */
4957 inline hashval_t
4958 decl_die_hasher::hash (die_node *x)
4960 return (hashval_t) x->decl_id;
4963 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
4965 inline bool
4966 decl_die_hasher::equal (die_node *x, tree y)
4968 return (x->decl_id == DECL_UID (y));
4971 /* Return the DIE associated with a given declaration. */
4973 static inline dw_die_ref
4974 lookup_decl_die (tree decl)
4976 return decl_die_table->find_with_hash (decl, DECL_UID (decl));
4979 /* Returns a hash value for X (which really is a var_loc_list). */
4981 inline hashval_t
4982 decl_loc_hasher::hash (var_loc_list *x)
4984 return (hashval_t) x->decl_id;
4987 /* Return nonzero if decl_id of var_loc_list X is the same as
4988 UID of decl *Y. */
4990 inline bool
4991 decl_loc_hasher::equal (var_loc_list *x, const_tree y)
4993 return (x->decl_id == DECL_UID (y));
4996 /* Return the var_loc list associated with a given declaration. */
4998 static inline var_loc_list *
4999 lookup_decl_loc (const_tree decl)
5001 if (!decl_loc_table)
5002 return NULL;
5003 return decl_loc_table->find_with_hash (decl, DECL_UID (decl));
5006 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
5008 inline hashval_t
5009 dw_loc_list_hasher::hash (cached_dw_loc_list *x)
5011 return (hashval_t) x->decl_id;
5014 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
5015 UID of decl *Y. */
5017 inline bool
5018 dw_loc_list_hasher::equal (cached_dw_loc_list *x, const_tree y)
5020 return (x->decl_id == DECL_UID (y));
5023 /* Equate a DIE to a particular declaration. */
5025 static void
5026 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5028 unsigned int decl_id = DECL_UID (decl);
5030 *decl_die_table->find_slot_with_hash (decl, decl_id, INSERT) = decl_die;
5031 decl_die->decl_id = decl_id;
5034 /* Return how many bits covers PIECE EXPR_LIST. */
5036 static int
5037 decl_piece_bitsize (rtx piece)
5039 int ret = (int) GET_MODE (piece);
5040 if (ret)
5041 return ret;
5042 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
5043 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
5044 return INTVAL (XEXP (XEXP (piece, 0), 0));
5047 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
5049 static rtx *
5050 decl_piece_varloc_ptr (rtx piece)
5052 if ((int) GET_MODE (piece))
5053 return &XEXP (piece, 0);
5054 else
5055 return &XEXP (XEXP (piece, 0), 1);
5058 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
5059 Next is the chain of following piece nodes. */
5061 static rtx_expr_list *
5062 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
5064 if (bitsize <= (int) MAX_MACHINE_MODE)
5065 return alloc_EXPR_LIST (bitsize, loc_note, next);
5066 else
5067 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
5068 GEN_INT (bitsize),
5069 loc_note), next);
5072 /* Return rtx that should be stored into loc field for
5073 LOC_NOTE and BITPOS/BITSIZE. */
5075 static rtx
5076 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
5077 HOST_WIDE_INT bitsize)
5079 if (bitsize != -1)
5081 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
5082 if (bitpos != 0)
5083 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
5085 return loc_note;
5088 /* This function either modifies location piece list *DEST in
5089 place (if SRC and INNER is NULL), or copies location piece list
5090 *SRC to *DEST while modifying it. Location BITPOS is modified
5091 to contain LOC_NOTE, any pieces overlapping it are removed resp.
5092 not copied and if needed some padding around it is added.
5093 When modifying in place, DEST should point to EXPR_LIST where
5094 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
5095 to the start of the whole list and INNER points to the EXPR_LIST
5096 where earlier pieces cover PIECE_BITPOS bits. */
5098 static void
5099 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5100 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5101 HOST_WIDE_INT bitsize, rtx loc_note)
5103 int diff;
5104 bool copy = inner != NULL;
5106 if (copy)
5108 /* First copy all nodes preceding the current bitpos. */
5109 while (src != inner)
5111 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5112 decl_piece_bitsize (*src), NULL_RTX);
5113 dest = &XEXP (*dest, 1);
5114 src = &XEXP (*src, 1);
5117 /* Add padding if needed. */
5118 if (bitpos != piece_bitpos)
5120 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5121 copy ? NULL_RTX : *dest);
5122 dest = &XEXP (*dest, 1);
5124 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5126 gcc_assert (!copy);
5127 /* A piece with correct bitpos and bitsize already exist,
5128 just update the location for it and return. */
5129 *decl_piece_varloc_ptr (*dest) = loc_note;
5130 return;
5132 /* Add the piece that changed. */
5133 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5134 dest = &XEXP (*dest, 1);
5135 /* Skip over pieces that overlap it. */
5136 diff = bitpos - piece_bitpos + bitsize;
5137 if (!copy)
5138 src = dest;
5139 while (diff > 0 && *src)
5141 rtx piece = *src;
5142 diff -= decl_piece_bitsize (piece);
5143 if (copy)
5144 src = &XEXP (piece, 1);
5145 else
5147 *src = XEXP (piece, 1);
5148 free_EXPR_LIST_node (piece);
5151 /* Add padding if needed. */
5152 if (diff < 0 && *src)
5154 if (!copy)
5155 dest = src;
5156 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5157 dest = &XEXP (*dest, 1);
5159 if (!copy)
5160 return;
5161 /* Finally copy all nodes following it. */
5162 while (*src)
5164 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5165 decl_piece_bitsize (*src), NULL_RTX);
5166 dest = &XEXP (*dest, 1);
5167 src = &XEXP (*src, 1);
5171 /* Add a variable location node to the linked list for DECL. */
5173 static struct var_loc_node *
5174 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5176 unsigned int decl_id;
5177 var_loc_list *temp;
5178 struct var_loc_node *loc = NULL;
5179 HOST_WIDE_INT bitsize = -1, bitpos = -1;
5181 if (TREE_CODE (decl) == VAR_DECL
5182 && DECL_HAS_DEBUG_EXPR_P (decl))
5184 tree realdecl = DECL_DEBUG_EXPR (decl);
5185 if (handled_component_p (realdecl)
5186 || (TREE_CODE (realdecl) == MEM_REF
5187 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5189 HOST_WIDE_INT maxsize;
5190 tree innerdecl;
5191 innerdecl
5192 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
5193 if (!DECL_P (innerdecl)
5194 || DECL_IGNORED_P (innerdecl)
5195 || TREE_STATIC (innerdecl)
5196 || bitsize <= 0
5197 || bitpos + bitsize > 256
5198 || bitsize != maxsize)
5199 return NULL;
5200 decl = innerdecl;
5204 decl_id = DECL_UID (decl);
5205 var_loc_list **slot
5206 = decl_loc_table->find_slot_with_hash (decl, decl_id, INSERT);
5207 if (*slot == NULL)
5209 temp = ggc_cleared_alloc<var_loc_list> ();
5210 temp->decl_id = decl_id;
5211 *slot = temp;
5213 else
5214 temp = *slot;
5216 /* For PARM_DECLs try to keep around the original incoming value,
5217 even if that means we'll emit a zero-range .debug_loc entry. */
5218 if (temp->last
5219 && temp->first == temp->last
5220 && TREE_CODE (decl) == PARM_DECL
5221 && NOTE_P (temp->first->loc)
5222 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5223 && DECL_INCOMING_RTL (decl)
5224 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5225 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5226 == GET_CODE (DECL_INCOMING_RTL (decl))
5227 && prev_real_insn (temp->first->loc) == NULL_RTX
5228 && (bitsize != -1
5229 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5230 NOTE_VAR_LOCATION_LOC (loc_note))
5231 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5232 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5234 loc = ggc_cleared_alloc<var_loc_node> ();
5235 temp->first->next = loc;
5236 temp->last = loc;
5237 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5239 else if (temp->last)
5241 struct var_loc_node *last = temp->last, *unused = NULL;
5242 rtx *piece_loc = NULL, last_loc_note;
5243 int piece_bitpos = 0;
5244 if (last->next)
5246 last = last->next;
5247 gcc_assert (last->next == NULL);
5249 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5251 piece_loc = &last->loc;
5254 int cur_bitsize = decl_piece_bitsize (*piece_loc);
5255 if (piece_bitpos + cur_bitsize > bitpos)
5256 break;
5257 piece_bitpos += cur_bitsize;
5258 piece_loc = &XEXP (*piece_loc, 1);
5260 while (*piece_loc);
5262 /* TEMP->LAST here is either pointer to the last but one or
5263 last element in the chained list, LAST is pointer to the
5264 last element. */
5265 if (label && strcmp (last->label, label) == 0)
5267 /* For SRA optimized variables if there weren't any real
5268 insns since last note, just modify the last node. */
5269 if (piece_loc != NULL)
5271 adjust_piece_list (piece_loc, NULL, NULL,
5272 bitpos, piece_bitpos, bitsize, loc_note);
5273 return NULL;
5275 /* If the last note doesn't cover any instructions, remove it. */
5276 if (temp->last != last)
5278 temp->last->next = NULL;
5279 unused = last;
5280 last = temp->last;
5281 gcc_assert (strcmp (last->label, label) != 0);
5283 else
5285 gcc_assert (temp->first == temp->last
5286 || (temp->first->next == temp->last
5287 && TREE_CODE (decl) == PARM_DECL));
5288 memset (temp->last, '\0', sizeof (*temp->last));
5289 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5290 return temp->last;
5293 if (bitsize == -1 && NOTE_P (last->loc))
5294 last_loc_note = last->loc;
5295 else if (piece_loc != NULL
5296 && *piece_loc != NULL_RTX
5297 && piece_bitpos == bitpos
5298 && decl_piece_bitsize (*piece_loc) == bitsize)
5299 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5300 else
5301 last_loc_note = NULL_RTX;
5302 /* If the current location is the same as the end of the list,
5303 and either both or neither of the locations is uninitialized,
5304 we have nothing to do. */
5305 if (last_loc_note == NULL_RTX
5306 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5307 NOTE_VAR_LOCATION_LOC (loc_note)))
5308 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5309 != NOTE_VAR_LOCATION_STATUS (loc_note))
5310 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5311 == VAR_INIT_STATUS_UNINITIALIZED)
5312 || (NOTE_VAR_LOCATION_STATUS (loc_note)
5313 == VAR_INIT_STATUS_UNINITIALIZED))))
5315 /* Add LOC to the end of list and update LAST. If the last
5316 element of the list has been removed above, reuse its
5317 memory for the new node, otherwise allocate a new one. */
5318 if (unused)
5320 loc = unused;
5321 memset (loc, '\0', sizeof (*loc));
5323 else
5324 loc = ggc_cleared_alloc<var_loc_node> ();
5325 if (bitsize == -1 || piece_loc == NULL)
5326 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5327 else
5328 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5329 bitpos, piece_bitpos, bitsize, loc_note);
5330 last->next = loc;
5331 /* Ensure TEMP->LAST will point either to the new last but one
5332 element of the chain, or to the last element in it. */
5333 if (last != temp->last)
5334 temp->last = last;
5336 else if (unused)
5337 ggc_free (unused);
5339 else
5341 loc = ggc_cleared_alloc<var_loc_node> ();
5342 temp->first = loc;
5343 temp->last = loc;
5344 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5346 return loc;
5349 /* Keep track of the number of spaces used to indent the
5350 output of the debugging routines that print the structure of
5351 the DIE internal representation. */
5352 static int print_indent;
5354 /* Indent the line the number of spaces given by print_indent. */
5356 static inline void
5357 print_spaces (FILE *outfile)
5359 fprintf (outfile, "%*s", print_indent, "");
5362 /* Print a type signature in hex. */
5364 static inline void
5365 print_signature (FILE *outfile, char *sig)
5367 int i;
5369 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5370 fprintf (outfile, "%02x", sig[i] & 0xff);
5373 /* Print the information associated with a given DIE, and its children.
5374 This routine is a debugging aid only. */
5376 static void
5377 print_die (dw_die_ref die, FILE *outfile)
5379 dw_attr_ref a;
5380 dw_die_ref c;
5381 unsigned ix;
5383 print_spaces (outfile);
5384 fprintf (outfile, "DIE %4ld: %s (%p)\n",
5385 die->die_offset, dwarf_tag_name (die->die_tag),
5386 (void*) die);
5387 print_spaces (outfile);
5388 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5389 fprintf (outfile, " offset: %ld", die->die_offset);
5390 fprintf (outfile, " mark: %d\n", die->die_mark);
5392 if (die->comdat_type_p)
5394 print_spaces (outfile);
5395 fprintf (outfile, " signature: ");
5396 print_signature (outfile, die->die_id.die_type_node->signature);
5397 fprintf (outfile, "\n");
5400 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5402 print_spaces (outfile);
5403 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5405 switch (AT_class (a))
5407 case dw_val_class_addr:
5408 fprintf (outfile, "address");
5409 break;
5410 case dw_val_class_offset:
5411 fprintf (outfile, "offset");
5412 break;
5413 case dw_val_class_loc:
5414 fprintf (outfile, "location descriptor");
5415 break;
5416 case dw_val_class_loc_list:
5417 fprintf (outfile, "location list -> label:%s",
5418 AT_loc_list (a)->ll_symbol);
5419 break;
5420 case dw_val_class_range_list:
5421 fprintf (outfile, "range list");
5422 break;
5423 case dw_val_class_const:
5424 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
5425 break;
5426 case dw_val_class_unsigned_const:
5427 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
5428 break;
5429 case dw_val_class_const_double:
5430 fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
5431 HOST_WIDE_INT_PRINT_UNSIGNED")",
5432 a->dw_attr_val.v.val_double.high,
5433 a->dw_attr_val.v.val_double.low);
5434 break;
5435 case dw_val_class_wide_int:
5437 int i = a->dw_attr_val.v.val_wide->get_len ();
5438 fprintf (outfile, "constant (");
5439 gcc_assert (i > 0);
5440 if (a->dw_attr_val.v.val_wide->elt (i - 1) == 0)
5441 fprintf (outfile, "0x");
5442 fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
5443 a->dw_attr_val.v.val_wide->elt (--i));
5444 while (--i >= 0)
5445 fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
5446 a->dw_attr_val.v.val_wide->elt (i));
5447 fprintf (outfile, ")");
5448 break;
5450 case dw_val_class_vec:
5451 fprintf (outfile, "floating-point or vector constant");
5452 break;
5453 case dw_val_class_flag:
5454 fprintf (outfile, "%u", AT_flag (a));
5455 break;
5456 case dw_val_class_die_ref:
5457 if (AT_ref (a) != NULL)
5459 if (AT_ref (a)->comdat_type_p)
5461 fprintf (outfile, "die -> signature: ");
5462 print_signature (outfile,
5463 AT_ref (a)->die_id.die_type_node->signature);
5465 else if (AT_ref (a)->die_id.die_symbol)
5466 fprintf (outfile, "die -> label: %s",
5467 AT_ref (a)->die_id.die_symbol);
5468 else
5469 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
5470 fprintf (outfile, " (%p)", (void *) AT_ref (a));
5472 else
5473 fprintf (outfile, "die -> <null>");
5474 break;
5475 case dw_val_class_vms_delta:
5476 fprintf (outfile, "delta: @slotcount(%s-%s)",
5477 AT_vms_delta2 (a), AT_vms_delta1 (a));
5478 break;
5479 case dw_val_class_lbl_id:
5480 case dw_val_class_lineptr:
5481 case dw_val_class_macptr:
5482 case dw_val_class_high_pc:
5483 fprintf (outfile, "label: %s", AT_lbl (a));
5484 break;
5485 case dw_val_class_str:
5486 if (AT_string (a) != NULL)
5487 fprintf (outfile, "\"%s\"", AT_string (a));
5488 else
5489 fprintf (outfile, "<null>");
5490 break;
5491 case dw_val_class_file:
5492 fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
5493 AT_file (a)->emitted_number);
5494 break;
5495 case dw_val_class_data8:
5497 int i;
5499 for (i = 0; i < 8; i++)
5500 fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
5501 break;
5503 default:
5504 break;
5507 fprintf (outfile, "\n");
5510 if (die->die_child != NULL)
5512 print_indent += 4;
5513 FOR_EACH_CHILD (die, c, print_die (c, outfile));
5514 print_indent -= 4;
5516 if (print_indent == 0)
5517 fprintf (outfile, "\n");
5520 /* Print the information collected for a given DIE. */
5522 DEBUG_FUNCTION void
5523 debug_dwarf_die (dw_die_ref die)
5525 print_die (die, stderr);
5528 DEBUG_FUNCTION void
5529 debug (die_struct &ref)
5531 print_die (&ref, stderr);
5534 DEBUG_FUNCTION void
5535 debug (die_struct *ptr)
5537 if (ptr)
5538 debug (*ptr);
5539 else
5540 fprintf (stderr, "<nil>\n");
5544 /* Print all DWARF information collected for the compilation unit.
5545 This routine is a debugging aid only. */
5547 DEBUG_FUNCTION void
5548 debug_dwarf (void)
5550 print_indent = 0;
5551 print_die (comp_unit_die (), stderr);
5554 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
5555 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
5556 DIE that marks the start of the DIEs for this include file. */
5558 static dw_die_ref
5559 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5561 const char *filename = get_AT_string (bincl_die, DW_AT_name);
5562 dw_die_ref new_unit = gen_compile_unit_die (filename);
5564 new_unit->die_sib = old_unit;
5565 return new_unit;
5568 /* Close an include-file CU and reopen the enclosing one. */
5570 static dw_die_ref
5571 pop_compile_unit (dw_die_ref old_unit)
5573 dw_die_ref new_unit = old_unit->die_sib;
5575 old_unit->die_sib = NULL;
5576 return new_unit;
5579 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5580 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5581 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5583 /* Calculate the checksum of a location expression. */
5585 static inline void
5586 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5588 int tem;
5589 inchash::hash hstate;
5590 hashval_t hash;
5592 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
5593 CHECKSUM (tem);
5594 hash_loc_operands (loc, hstate);
5595 hash = hstate.end();
5596 CHECKSUM (hash);
5599 /* Calculate the checksum of an attribute. */
5601 static void
5602 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5604 dw_loc_descr_ref loc;
5605 rtx r;
5607 CHECKSUM (at->dw_attr);
5609 /* We don't care that this was compiled with a different compiler
5610 snapshot; if the output is the same, that's what matters. */
5611 if (at->dw_attr == DW_AT_producer)
5612 return;
5614 switch (AT_class (at))
5616 case dw_val_class_const:
5617 CHECKSUM (at->dw_attr_val.v.val_int);
5618 break;
5619 case dw_val_class_unsigned_const:
5620 CHECKSUM (at->dw_attr_val.v.val_unsigned);
5621 break;
5622 case dw_val_class_const_double:
5623 CHECKSUM (at->dw_attr_val.v.val_double);
5624 break;
5625 case dw_val_class_wide_int:
5626 CHECKSUM (*at->dw_attr_val.v.val_wide);
5627 break;
5628 case dw_val_class_vec:
5629 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5630 (at->dw_attr_val.v.val_vec.length
5631 * at->dw_attr_val.v.val_vec.elt_size));
5632 break;
5633 case dw_val_class_flag:
5634 CHECKSUM (at->dw_attr_val.v.val_flag);
5635 break;
5636 case dw_val_class_str:
5637 CHECKSUM_STRING (AT_string (at));
5638 break;
5640 case dw_val_class_addr:
5641 r = AT_addr (at);
5642 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5643 CHECKSUM_STRING (XSTR (r, 0));
5644 break;
5646 case dw_val_class_offset:
5647 CHECKSUM (at->dw_attr_val.v.val_offset);
5648 break;
5650 case dw_val_class_loc:
5651 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5652 loc_checksum (loc, ctx);
5653 break;
5655 case dw_val_class_die_ref:
5656 die_checksum (AT_ref (at), ctx, mark);
5657 break;
5659 case dw_val_class_fde_ref:
5660 case dw_val_class_vms_delta:
5661 case dw_val_class_lbl_id:
5662 case dw_val_class_lineptr:
5663 case dw_val_class_macptr:
5664 case dw_val_class_high_pc:
5665 break;
5667 case dw_val_class_file:
5668 CHECKSUM_STRING (AT_file (at)->filename);
5669 break;
5671 case dw_val_class_data8:
5672 CHECKSUM (at->dw_attr_val.v.val_data8);
5673 break;
5675 default:
5676 break;
5680 /* Calculate the checksum of a DIE. */
5682 static void
5683 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5685 dw_die_ref c;
5686 dw_attr_ref a;
5687 unsigned ix;
5689 /* To avoid infinite recursion. */
5690 if (die->die_mark)
5692 CHECKSUM (die->die_mark);
5693 return;
5695 die->die_mark = ++(*mark);
5697 CHECKSUM (die->die_tag);
5699 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5700 attr_checksum (a, ctx, mark);
5702 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
5705 #undef CHECKSUM
5706 #undef CHECKSUM_BLOCK
5707 #undef CHECKSUM_STRING
5709 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
5710 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5711 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5712 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
5713 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
5714 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
5715 #define CHECKSUM_ATTR(FOO) \
5716 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
5718 /* Calculate the checksum of a number in signed LEB128 format. */
5720 static void
5721 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
5723 unsigned char byte;
5724 bool more;
5726 while (1)
5728 byte = (value & 0x7f);
5729 value >>= 7;
5730 more = !((value == 0 && (byte & 0x40) == 0)
5731 || (value == -1 && (byte & 0x40) != 0));
5732 if (more)
5733 byte |= 0x80;
5734 CHECKSUM (byte);
5735 if (!more)
5736 break;
5740 /* Calculate the checksum of a number in unsigned LEB128 format. */
5742 static void
5743 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
5745 while (1)
5747 unsigned char byte = (value & 0x7f);
5748 value >>= 7;
5749 if (value != 0)
5750 /* More bytes to follow. */
5751 byte |= 0x80;
5752 CHECKSUM (byte);
5753 if (value == 0)
5754 break;
5758 /* Checksum the context of the DIE. This adds the names of any
5759 surrounding namespaces or structures to the checksum. */
5761 static void
5762 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
5764 const char *name;
5765 dw_die_ref spec;
5766 int tag = die->die_tag;
5768 if (tag != DW_TAG_namespace
5769 && tag != DW_TAG_structure_type
5770 && tag != DW_TAG_class_type)
5771 return;
5773 name = get_AT_string (die, DW_AT_name);
5775 spec = get_AT_ref (die, DW_AT_specification);
5776 if (spec != NULL)
5777 die = spec;
5779 if (die->die_parent != NULL)
5780 checksum_die_context (die->die_parent, ctx);
5782 CHECKSUM_ULEB128 ('C');
5783 CHECKSUM_ULEB128 (tag);
5784 if (name != NULL)
5785 CHECKSUM_STRING (name);
5788 /* Calculate the checksum of a location expression. */
5790 static inline void
5791 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5793 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
5794 were emitted as a DW_FORM_sdata instead of a location expression. */
5795 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
5797 CHECKSUM_ULEB128 (DW_FORM_sdata);
5798 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
5799 return;
5802 /* Otherwise, just checksum the raw location expression. */
5803 while (loc != NULL)
5805 inchash::hash hstate;
5806 hashval_t hash;
5808 CHECKSUM_ULEB128 (loc->dtprel);
5809 CHECKSUM_ULEB128 (loc->dw_loc_opc);
5810 hash_loc_operands (loc, hstate);
5811 hash = hstate.end ();
5812 CHECKSUM (hash);
5813 loc = loc->dw_loc_next;
5817 /* Calculate the checksum of an attribute. */
5819 static void
5820 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
5821 struct md5_ctx *ctx, int *mark)
5823 dw_loc_descr_ref loc;
5824 rtx r;
5826 if (AT_class (at) == dw_val_class_die_ref)
5828 dw_die_ref target_die = AT_ref (at);
5830 /* For pointer and reference types, we checksum only the (qualified)
5831 name of the target type (if there is a name). For friend entries,
5832 we checksum only the (qualified) name of the target type or function.
5833 This allows the checksum to remain the same whether the target type
5834 is complete or not. */
5835 if ((at->dw_attr == DW_AT_type
5836 && (tag == DW_TAG_pointer_type
5837 || tag == DW_TAG_reference_type
5838 || tag == DW_TAG_rvalue_reference_type
5839 || tag == DW_TAG_ptr_to_member_type))
5840 || (at->dw_attr == DW_AT_friend
5841 && tag == DW_TAG_friend))
5843 dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
5845 if (name_attr != NULL)
5847 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5849 if (decl == NULL)
5850 decl = target_die;
5851 CHECKSUM_ULEB128 ('N');
5852 CHECKSUM_ULEB128 (at->dw_attr);
5853 if (decl->die_parent != NULL)
5854 checksum_die_context (decl->die_parent, ctx);
5855 CHECKSUM_ULEB128 ('E');
5856 CHECKSUM_STRING (AT_string (name_attr));
5857 return;
5861 /* For all other references to another DIE, we check to see if the
5862 target DIE has already been visited. If it has, we emit a
5863 backward reference; if not, we descend recursively. */
5864 if (target_die->die_mark > 0)
5866 CHECKSUM_ULEB128 ('R');
5867 CHECKSUM_ULEB128 (at->dw_attr);
5868 CHECKSUM_ULEB128 (target_die->die_mark);
5870 else
5872 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5874 if (decl == NULL)
5875 decl = target_die;
5876 target_die->die_mark = ++(*mark);
5877 CHECKSUM_ULEB128 ('T');
5878 CHECKSUM_ULEB128 (at->dw_attr);
5879 if (decl->die_parent != NULL)
5880 checksum_die_context (decl->die_parent, ctx);
5881 die_checksum_ordered (target_die, ctx, mark);
5883 return;
5886 CHECKSUM_ULEB128 ('A');
5887 CHECKSUM_ULEB128 (at->dw_attr);
5889 switch (AT_class (at))
5891 case dw_val_class_const:
5892 CHECKSUM_ULEB128 (DW_FORM_sdata);
5893 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
5894 break;
5896 case dw_val_class_unsigned_const:
5897 CHECKSUM_ULEB128 (DW_FORM_sdata);
5898 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
5899 break;
5901 case dw_val_class_const_double:
5902 CHECKSUM_ULEB128 (DW_FORM_block);
5903 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
5904 CHECKSUM (at->dw_attr_val.v.val_double);
5905 break;
5907 case dw_val_class_wide_int:
5908 CHECKSUM_ULEB128 (DW_FORM_block);
5909 CHECKSUM_ULEB128 (sizeof (*at->dw_attr_val.v.val_wide));
5910 CHECKSUM (*at->dw_attr_val.v.val_wide);
5911 break;
5913 case dw_val_class_vec:
5914 CHECKSUM_ULEB128 (DW_FORM_block);
5915 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
5916 * at->dw_attr_val.v.val_vec.elt_size);
5917 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5918 (at->dw_attr_val.v.val_vec.length
5919 * at->dw_attr_val.v.val_vec.elt_size));
5920 break;
5922 case dw_val_class_flag:
5923 CHECKSUM_ULEB128 (DW_FORM_flag);
5924 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
5925 break;
5927 case dw_val_class_str:
5928 CHECKSUM_ULEB128 (DW_FORM_string);
5929 CHECKSUM_STRING (AT_string (at));
5930 break;
5932 case dw_val_class_addr:
5933 r = AT_addr (at);
5934 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5935 CHECKSUM_ULEB128 (DW_FORM_string);
5936 CHECKSUM_STRING (XSTR (r, 0));
5937 break;
5939 case dw_val_class_offset:
5940 CHECKSUM_ULEB128 (DW_FORM_sdata);
5941 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
5942 break;
5944 case dw_val_class_loc:
5945 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5946 loc_checksum_ordered (loc, ctx);
5947 break;
5949 case dw_val_class_fde_ref:
5950 case dw_val_class_lbl_id:
5951 case dw_val_class_lineptr:
5952 case dw_val_class_macptr:
5953 case dw_val_class_high_pc:
5954 break;
5956 case dw_val_class_file:
5957 CHECKSUM_ULEB128 (DW_FORM_string);
5958 CHECKSUM_STRING (AT_file (at)->filename);
5959 break;
5961 case dw_val_class_data8:
5962 CHECKSUM (at->dw_attr_val.v.val_data8);
5963 break;
5965 default:
5966 break;
5970 struct checksum_attributes
5972 dw_attr_ref at_name;
5973 dw_attr_ref at_type;
5974 dw_attr_ref at_friend;
5975 dw_attr_ref at_accessibility;
5976 dw_attr_ref at_address_class;
5977 dw_attr_ref at_allocated;
5978 dw_attr_ref at_artificial;
5979 dw_attr_ref at_associated;
5980 dw_attr_ref at_binary_scale;
5981 dw_attr_ref at_bit_offset;
5982 dw_attr_ref at_bit_size;
5983 dw_attr_ref at_bit_stride;
5984 dw_attr_ref at_byte_size;
5985 dw_attr_ref at_byte_stride;
5986 dw_attr_ref at_const_value;
5987 dw_attr_ref at_containing_type;
5988 dw_attr_ref at_count;
5989 dw_attr_ref at_data_location;
5990 dw_attr_ref at_data_member_location;
5991 dw_attr_ref at_decimal_scale;
5992 dw_attr_ref at_decimal_sign;
5993 dw_attr_ref at_default_value;
5994 dw_attr_ref at_digit_count;
5995 dw_attr_ref at_discr;
5996 dw_attr_ref at_discr_list;
5997 dw_attr_ref at_discr_value;
5998 dw_attr_ref at_encoding;
5999 dw_attr_ref at_endianity;
6000 dw_attr_ref at_explicit;
6001 dw_attr_ref at_is_optional;
6002 dw_attr_ref at_location;
6003 dw_attr_ref at_lower_bound;
6004 dw_attr_ref at_mutable;
6005 dw_attr_ref at_ordering;
6006 dw_attr_ref at_picture_string;
6007 dw_attr_ref at_prototyped;
6008 dw_attr_ref at_small;
6009 dw_attr_ref at_segment;
6010 dw_attr_ref at_string_length;
6011 dw_attr_ref at_threads_scaled;
6012 dw_attr_ref at_upper_bound;
6013 dw_attr_ref at_use_location;
6014 dw_attr_ref at_use_UTF8;
6015 dw_attr_ref at_variable_parameter;
6016 dw_attr_ref at_virtuality;
6017 dw_attr_ref at_visibility;
6018 dw_attr_ref at_vtable_elem_location;
6021 /* Collect the attributes that we will want to use for the checksum. */
6023 static void
6024 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
6026 dw_attr_ref a;
6027 unsigned ix;
6029 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6031 switch (a->dw_attr)
6033 case DW_AT_name:
6034 attrs->at_name = a;
6035 break;
6036 case DW_AT_type:
6037 attrs->at_type = a;
6038 break;
6039 case DW_AT_friend:
6040 attrs->at_friend = a;
6041 break;
6042 case DW_AT_accessibility:
6043 attrs->at_accessibility = a;
6044 break;
6045 case DW_AT_address_class:
6046 attrs->at_address_class = a;
6047 break;
6048 case DW_AT_allocated:
6049 attrs->at_allocated = a;
6050 break;
6051 case DW_AT_artificial:
6052 attrs->at_artificial = a;
6053 break;
6054 case DW_AT_associated:
6055 attrs->at_associated = a;
6056 break;
6057 case DW_AT_binary_scale:
6058 attrs->at_binary_scale = a;
6059 break;
6060 case DW_AT_bit_offset:
6061 attrs->at_bit_offset = a;
6062 break;
6063 case DW_AT_bit_size:
6064 attrs->at_bit_size = a;
6065 break;
6066 case DW_AT_bit_stride:
6067 attrs->at_bit_stride = a;
6068 break;
6069 case DW_AT_byte_size:
6070 attrs->at_byte_size = a;
6071 break;
6072 case DW_AT_byte_stride:
6073 attrs->at_byte_stride = a;
6074 break;
6075 case DW_AT_const_value:
6076 attrs->at_const_value = a;
6077 break;
6078 case DW_AT_containing_type:
6079 attrs->at_containing_type = a;
6080 break;
6081 case DW_AT_count:
6082 attrs->at_count = a;
6083 break;
6084 case DW_AT_data_location:
6085 attrs->at_data_location = a;
6086 break;
6087 case DW_AT_data_member_location:
6088 attrs->at_data_member_location = a;
6089 break;
6090 case DW_AT_decimal_scale:
6091 attrs->at_decimal_scale = a;
6092 break;
6093 case DW_AT_decimal_sign:
6094 attrs->at_decimal_sign = a;
6095 break;
6096 case DW_AT_default_value:
6097 attrs->at_default_value = a;
6098 break;
6099 case DW_AT_digit_count:
6100 attrs->at_digit_count = a;
6101 break;
6102 case DW_AT_discr:
6103 attrs->at_discr = a;
6104 break;
6105 case DW_AT_discr_list:
6106 attrs->at_discr_list = a;
6107 break;
6108 case DW_AT_discr_value:
6109 attrs->at_discr_value = a;
6110 break;
6111 case DW_AT_encoding:
6112 attrs->at_encoding = a;
6113 break;
6114 case DW_AT_endianity:
6115 attrs->at_endianity = a;
6116 break;
6117 case DW_AT_explicit:
6118 attrs->at_explicit = a;
6119 break;
6120 case DW_AT_is_optional:
6121 attrs->at_is_optional = a;
6122 break;
6123 case DW_AT_location:
6124 attrs->at_location = a;
6125 break;
6126 case DW_AT_lower_bound:
6127 attrs->at_lower_bound = a;
6128 break;
6129 case DW_AT_mutable:
6130 attrs->at_mutable = a;
6131 break;
6132 case DW_AT_ordering:
6133 attrs->at_ordering = a;
6134 break;
6135 case DW_AT_picture_string:
6136 attrs->at_picture_string = a;
6137 break;
6138 case DW_AT_prototyped:
6139 attrs->at_prototyped = a;
6140 break;
6141 case DW_AT_small:
6142 attrs->at_small = a;
6143 break;
6144 case DW_AT_segment:
6145 attrs->at_segment = a;
6146 break;
6147 case DW_AT_string_length:
6148 attrs->at_string_length = a;
6149 break;
6150 case DW_AT_threads_scaled:
6151 attrs->at_threads_scaled = a;
6152 break;
6153 case DW_AT_upper_bound:
6154 attrs->at_upper_bound = a;
6155 break;
6156 case DW_AT_use_location:
6157 attrs->at_use_location = a;
6158 break;
6159 case DW_AT_use_UTF8:
6160 attrs->at_use_UTF8 = a;
6161 break;
6162 case DW_AT_variable_parameter:
6163 attrs->at_variable_parameter = a;
6164 break;
6165 case DW_AT_virtuality:
6166 attrs->at_virtuality = a;
6167 break;
6168 case DW_AT_visibility:
6169 attrs->at_visibility = a;
6170 break;
6171 case DW_AT_vtable_elem_location:
6172 attrs->at_vtable_elem_location = a;
6173 break;
6174 default:
6175 break;
6180 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
6182 static void
6183 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6185 dw_die_ref c;
6186 dw_die_ref decl;
6187 struct checksum_attributes attrs;
6189 CHECKSUM_ULEB128 ('D');
6190 CHECKSUM_ULEB128 (die->die_tag);
6192 memset (&attrs, 0, sizeof (attrs));
6194 decl = get_AT_ref (die, DW_AT_specification);
6195 if (decl != NULL)
6196 collect_checksum_attributes (&attrs, decl);
6197 collect_checksum_attributes (&attrs, die);
6199 CHECKSUM_ATTR (attrs.at_name);
6200 CHECKSUM_ATTR (attrs.at_accessibility);
6201 CHECKSUM_ATTR (attrs.at_address_class);
6202 CHECKSUM_ATTR (attrs.at_allocated);
6203 CHECKSUM_ATTR (attrs.at_artificial);
6204 CHECKSUM_ATTR (attrs.at_associated);
6205 CHECKSUM_ATTR (attrs.at_binary_scale);
6206 CHECKSUM_ATTR (attrs.at_bit_offset);
6207 CHECKSUM_ATTR (attrs.at_bit_size);
6208 CHECKSUM_ATTR (attrs.at_bit_stride);
6209 CHECKSUM_ATTR (attrs.at_byte_size);
6210 CHECKSUM_ATTR (attrs.at_byte_stride);
6211 CHECKSUM_ATTR (attrs.at_const_value);
6212 CHECKSUM_ATTR (attrs.at_containing_type);
6213 CHECKSUM_ATTR (attrs.at_count);
6214 CHECKSUM_ATTR (attrs.at_data_location);
6215 CHECKSUM_ATTR (attrs.at_data_member_location);
6216 CHECKSUM_ATTR (attrs.at_decimal_scale);
6217 CHECKSUM_ATTR (attrs.at_decimal_sign);
6218 CHECKSUM_ATTR (attrs.at_default_value);
6219 CHECKSUM_ATTR (attrs.at_digit_count);
6220 CHECKSUM_ATTR (attrs.at_discr);
6221 CHECKSUM_ATTR (attrs.at_discr_list);
6222 CHECKSUM_ATTR (attrs.at_discr_value);
6223 CHECKSUM_ATTR (attrs.at_encoding);
6224 CHECKSUM_ATTR (attrs.at_endianity);
6225 CHECKSUM_ATTR (attrs.at_explicit);
6226 CHECKSUM_ATTR (attrs.at_is_optional);
6227 CHECKSUM_ATTR (attrs.at_location);
6228 CHECKSUM_ATTR (attrs.at_lower_bound);
6229 CHECKSUM_ATTR (attrs.at_mutable);
6230 CHECKSUM_ATTR (attrs.at_ordering);
6231 CHECKSUM_ATTR (attrs.at_picture_string);
6232 CHECKSUM_ATTR (attrs.at_prototyped);
6233 CHECKSUM_ATTR (attrs.at_small);
6234 CHECKSUM_ATTR (attrs.at_segment);
6235 CHECKSUM_ATTR (attrs.at_string_length);
6236 CHECKSUM_ATTR (attrs.at_threads_scaled);
6237 CHECKSUM_ATTR (attrs.at_upper_bound);
6238 CHECKSUM_ATTR (attrs.at_use_location);
6239 CHECKSUM_ATTR (attrs.at_use_UTF8);
6240 CHECKSUM_ATTR (attrs.at_variable_parameter);
6241 CHECKSUM_ATTR (attrs.at_virtuality);
6242 CHECKSUM_ATTR (attrs.at_visibility);
6243 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6244 CHECKSUM_ATTR (attrs.at_type);
6245 CHECKSUM_ATTR (attrs.at_friend);
6247 /* Checksum the child DIEs. */
6248 c = die->die_child;
6249 if (c) do {
6250 dw_attr_ref name_attr;
6252 c = c->die_sib;
6253 name_attr = get_AT (c, DW_AT_name);
6254 if (is_template_instantiation (c))
6256 /* Ignore instantiations of member type and function templates. */
6258 else if (name_attr != NULL
6259 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
6261 /* Use a shallow checksum for named nested types and member
6262 functions. */
6263 CHECKSUM_ULEB128 ('S');
6264 CHECKSUM_ULEB128 (c->die_tag);
6265 CHECKSUM_STRING (AT_string (name_attr));
6267 else
6269 /* Use a deep checksum for other children. */
6270 /* Mark this DIE so it gets processed when unmarking. */
6271 if (c->die_mark == 0)
6272 c->die_mark = -1;
6273 die_checksum_ordered (c, ctx, mark);
6275 } while (c != die->die_child);
6277 CHECKSUM_ULEB128 (0);
6280 /* Add a type name and tag to a hash. */
6281 static void
6282 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
6284 CHECKSUM_ULEB128 (tag);
6285 CHECKSUM_STRING (name);
6288 #undef CHECKSUM
6289 #undef CHECKSUM_STRING
6290 #undef CHECKSUM_ATTR
6291 #undef CHECKSUM_LEB128
6292 #undef CHECKSUM_ULEB128
6294 /* Generate the type signature for DIE. This is computed by generating an
6295 MD5 checksum over the DIE's tag, its relevant attributes, and its
6296 children. Attributes that are references to other DIEs are processed
6297 by recursion, using the MARK field to prevent infinite recursion.
6298 If the DIE is nested inside a namespace or another type, we also
6299 need to include that context in the signature. The lower 64 bits
6300 of the resulting MD5 checksum comprise the signature. */
6302 static void
6303 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6305 int mark;
6306 const char *name;
6307 unsigned char checksum[16];
6308 struct md5_ctx ctx;
6309 dw_die_ref decl;
6310 dw_die_ref parent;
6312 name = get_AT_string (die, DW_AT_name);
6313 decl = get_AT_ref (die, DW_AT_specification);
6314 parent = get_die_parent (die);
6316 /* First, compute a signature for just the type name (and its surrounding
6317 context, if any. This is stored in the type unit DIE for link-time
6318 ODR (one-definition rule) checking. */
6320 if (is_cxx () && name != NULL)
6322 md5_init_ctx (&ctx);
6324 /* Checksum the names of surrounding namespaces and structures. */
6325 if (parent != NULL)
6326 checksum_die_context (parent, &ctx);
6328 /* Checksum the current DIE. */
6329 die_odr_checksum (die->die_tag, name, &ctx);
6330 md5_finish_ctx (&ctx, checksum);
6332 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6335 /* Next, compute the complete type signature. */
6337 md5_init_ctx (&ctx);
6338 mark = 1;
6339 die->die_mark = mark;
6341 /* Checksum the names of surrounding namespaces and structures. */
6342 if (parent != NULL)
6343 checksum_die_context (parent, &ctx);
6345 /* Checksum the DIE and its children. */
6346 die_checksum_ordered (die, &ctx, &mark);
6347 unmark_all_dies (die);
6348 md5_finish_ctx (&ctx, checksum);
6350 /* Store the signature in the type node and link the type DIE and the
6351 type node together. */
6352 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6353 DWARF_TYPE_SIGNATURE_SIZE);
6354 die->comdat_type_p = true;
6355 die->die_id.die_type_node = type_node;
6356 type_node->type_die = die;
6358 /* If the DIE is a specification, link its declaration to the type node
6359 as well. */
6360 if (decl != NULL)
6362 decl->comdat_type_p = true;
6363 decl->die_id.die_type_node = type_node;
6367 /* Do the location expressions look same? */
6368 static inline int
6369 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6371 return loc1->dw_loc_opc == loc2->dw_loc_opc
6372 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6373 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6376 /* Do the values look the same? */
6377 static int
6378 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6380 dw_loc_descr_ref loc1, loc2;
6381 rtx r1, r2;
6383 if (v1->val_class != v2->val_class)
6384 return 0;
6386 switch (v1->val_class)
6388 case dw_val_class_const:
6389 return v1->v.val_int == v2->v.val_int;
6390 case dw_val_class_unsigned_const:
6391 return v1->v.val_unsigned == v2->v.val_unsigned;
6392 case dw_val_class_const_double:
6393 return v1->v.val_double.high == v2->v.val_double.high
6394 && v1->v.val_double.low == v2->v.val_double.low;
6395 case dw_val_class_wide_int:
6396 return *v1->v.val_wide == *v2->v.val_wide;
6397 case dw_val_class_vec:
6398 if (v1->v.val_vec.length != v2->v.val_vec.length
6399 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6400 return 0;
6401 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6402 v1->v.val_vec.length * v1->v.val_vec.elt_size))
6403 return 0;
6404 return 1;
6405 case dw_val_class_flag:
6406 return v1->v.val_flag == v2->v.val_flag;
6407 case dw_val_class_str:
6408 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
6410 case dw_val_class_addr:
6411 r1 = v1->v.val_addr;
6412 r2 = v2->v.val_addr;
6413 if (GET_CODE (r1) != GET_CODE (r2))
6414 return 0;
6415 return !rtx_equal_p (r1, r2);
6417 case dw_val_class_offset:
6418 return v1->v.val_offset == v2->v.val_offset;
6420 case dw_val_class_loc:
6421 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6422 loc1 && loc2;
6423 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6424 if (!same_loc_p (loc1, loc2, mark))
6425 return 0;
6426 return !loc1 && !loc2;
6428 case dw_val_class_die_ref:
6429 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6431 case dw_val_class_fde_ref:
6432 case dw_val_class_vms_delta:
6433 case dw_val_class_lbl_id:
6434 case dw_val_class_lineptr:
6435 case dw_val_class_macptr:
6436 case dw_val_class_high_pc:
6437 return 1;
6439 case dw_val_class_file:
6440 return v1->v.val_file == v2->v.val_file;
6442 case dw_val_class_data8:
6443 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
6445 default:
6446 return 1;
6450 /* Do the attributes look the same? */
6452 static int
6453 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6455 if (at1->dw_attr != at2->dw_attr)
6456 return 0;
6458 /* We don't care that this was compiled with a different compiler
6459 snapshot; if the output is the same, that's what matters. */
6460 if (at1->dw_attr == DW_AT_producer)
6461 return 1;
6463 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6466 /* Do the dies look the same? */
6468 static int
6469 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6471 dw_die_ref c1, c2;
6472 dw_attr_ref a1;
6473 unsigned ix;
6475 /* To avoid infinite recursion. */
6476 if (die1->die_mark)
6477 return die1->die_mark == die2->die_mark;
6478 die1->die_mark = die2->die_mark = ++(*mark);
6480 if (die1->die_tag != die2->die_tag)
6481 return 0;
6483 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
6484 return 0;
6486 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
6487 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
6488 return 0;
6490 c1 = die1->die_child;
6491 c2 = die2->die_child;
6492 if (! c1)
6494 if (c2)
6495 return 0;
6497 else
6498 for (;;)
6500 if (!same_die_p (c1, c2, mark))
6501 return 0;
6502 c1 = c1->die_sib;
6503 c2 = c2->die_sib;
6504 if (c1 == die1->die_child)
6506 if (c2 == die2->die_child)
6507 break;
6508 else
6509 return 0;
6513 return 1;
6516 /* Do the dies look the same? Wrapper around same_die_p. */
6518 static int
6519 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6521 int mark = 0;
6522 int ret = same_die_p (die1, die2, &mark);
6524 unmark_all_dies (die1);
6525 unmark_all_dies (die2);
6527 return ret;
6530 /* The prefix to attach to symbols on DIEs in the current comdat debug
6531 info section. */
6532 static const char *comdat_symbol_id;
6534 /* The index of the current symbol within the current comdat CU. */
6535 static unsigned int comdat_symbol_number;
6537 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6538 children, and set comdat_symbol_id accordingly. */
6540 static void
6541 compute_section_prefix (dw_die_ref unit_die)
6543 const char *die_name = get_AT_string (unit_die, DW_AT_name);
6544 const char *base = die_name ? lbasename (die_name) : "anonymous";
6545 char *name = XALLOCAVEC (char, strlen (base) + 64);
6546 char *p;
6547 int i, mark;
6548 unsigned char checksum[16];
6549 struct md5_ctx ctx;
6551 /* Compute the checksum of the DIE, then append part of it as hex digits to
6552 the name filename of the unit. */
6554 md5_init_ctx (&ctx);
6555 mark = 0;
6556 die_checksum (unit_die, &ctx, &mark);
6557 unmark_all_dies (unit_die);
6558 md5_finish_ctx (&ctx, checksum);
6560 sprintf (name, "%s.", base);
6561 clean_symbol_name (name);
6563 p = name + strlen (name);
6564 for (i = 0; i < 4; i++)
6566 sprintf (p, "%.2x", checksum[i]);
6567 p += 2;
6570 comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
6571 comdat_symbol_number = 0;
6574 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
6576 static int
6577 is_type_die (dw_die_ref die)
6579 switch (die->die_tag)
6581 case DW_TAG_array_type:
6582 case DW_TAG_class_type:
6583 case DW_TAG_interface_type:
6584 case DW_TAG_enumeration_type:
6585 case DW_TAG_pointer_type:
6586 case DW_TAG_reference_type:
6587 case DW_TAG_rvalue_reference_type:
6588 case DW_TAG_string_type:
6589 case DW_TAG_structure_type:
6590 case DW_TAG_subroutine_type:
6591 case DW_TAG_union_type:
6592 case DW_TAG_ptr_to_member_type:
6593 case DW_TAG_set_type:
6594 case DW_TAG_subrange_type:
6595 case DW_TAG_base_type:
6596 case DW_TAG_const_type:
6597 case DW_TAG_file_type:
6598 case DW_TAG_packed_type:
6599 case DW_TAG_volatile_type:
6600 case DW_TAG_typedef:
6601 return 1;
6602 default:
6603 return 0;
6607 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6608 Basically, we want to choose the bits that are likely to be shared between
6609 compilations (types) and leave out the bits that are specific to individual
6610 compilations (functions). */
6612 static int
6613 is_comdat_die (dw_die_ref c)
6615 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6616 we do for stabs. The advantage is a greater likelihood of sharing between
6617 objects that don't include headers in the same order (and therefore would
6618 put the base types in a different comdat). jason 8/28/00 */
6620 if (c->die_tag == DW_TAG_base_type)
6621 return 0;
6623 if (c->die_tag == DW_TAG_pointer_type
6624 || c->die_tag == DW_TAG_reference_type
6625 || c->die_tag == DW_TAG_rvalue_reference_type
6626 || c->die_tag == DW_TAG_const_type
6627 || c->die_tag == DW_TAG_volatile_type)
6629 dw_die_ref t = get_AT_ref (c, DW_AT_type);
6631 return t ? is_comdat_die (t) : 0;
6634 return is_type_die (c);
6637 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6638 compilation unit. */
6640 static int
6641 is_symbol_die (dw_die_ref c)
6643 return (is_type_die (c)
6644 || is_declaration_die (c)
6645 || c->die_tag == DW_TAG_namespace
6646 || c->die_tag == DW_TAG_module);
6649 /* Returns true iff C is a compile-unit DIE. */
6651 static inline bool
6652 is_cu_die (dw_die_ref c)
6654 return c && c->die_tag == DW_TAG_compile_unit;
6657 /* Returns true iff C is a unit DIE of some sort. */
6659 static inline bool
6660 is_unit_die (dw_die_ref c)
6662 return c && (c->die_tag == DW_TAG_compile_unit
6663 || c->die_tag == DW_TAG_partial_unit
6664 || c->die_tag == DW_TAG_type_unit);
6667 /* Returns true iff C is a namespace DIE. */
6669 static inline bool
6670 is_namespace_die (dw_die_ref c)
6672 return c && c->die_tag == DW_TAG_namespace;
6675 /* Returns true iff C is a class or structure DIE. */
6677 static inline bool
6678 is_class_die (dw_die_ref c)
6680 return c && (c->die_tag == DW_TAG_class_type
6681 || c->die_tag == DW_TAG_structure_type);
6684 /* Return non-zero if this DIE is a template parameter. */
6686 static inline bool
6687 is_template_parameter (dw_die_ref die)
6689 switch (die->die_tag)
6691 case DW_TAG_template_type_param:
6692 case DW_TAG_template_value_param:
6693 case DW_TAG_GNU_template_template_param:
6694 case DW_TAG_GNU_template_parameter_pack:
6695 return true;
6696 default:
6697 return false;
6701 /* Return non-zero if this DIE represents a template instantiation. */
6703 static inline bool
6704 is_template_instantiation (dw_die_ref die)
6706 dw_die_ref c;
6708 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
6709 return false;
6710 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
6711 return false;
6714 static char *
6715 gen_internal_sym (const char *prefix)
6717 char buf[256];
6719 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6720 return xstrdup (buf);
6723 /* Assign symbols to all worthy DIEs under DIE. */
6725 static void
6726 assign_symbol_names (dw_die_ref die)
6728 dw_die_ref c;
6730 if (is_symbol_die (die) && !die->comdat_type_p)
6732 if (comdat_symbol_id)
6734 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
6736 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6737 comdat_symbol_id, comdat_symbol_number++);
6738 die->die_id.die_symbol = xstrdup (p);
6740 else
6741 die->die_id.die_symbol = gen_internal_sym ("LDIE");
6744 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6747 struct cu_hash_table_entry
6749 dw_die_ref cu;
6750 unsigned min_comdat_num, max_comdat_num;
6751 struct cu_hash_table_entry *next;
6754 /* Helpers to manipulate hash table of CUs. */
6756 struct cu_hash_table_entry_hasher
6758 typedef cu_hash_table_entry value_type;
6759 typedef die_struct compare_type;
6760 static inline hashval_t hash (const value_type *);
6761 static inline bool equal (const value_type *, const compare_type *);
6762 static inline void remove (value_type *);
6765 inline hashval_t
6766 cu_hash_table_entry_hasher::hash (const value_type *entry)
6768 return htab_hash_string (entry->cu->die_id.die_symbol);
6771 inline bool
6772 cu_hash_table_entry_hasher::equal (const value_type *entry1,
6773 const compare_type *entry2)
6775 return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
6778 inline void
6779 cu_hash_table_entry_hasher::remove (value_type *entry)
6781 struct cu_hash_table_entry *next;
6783 while (entry)
6785 next = entry->next;
6786 free (entry);
6787 entry = next;
6791 typedef hash_table<cu_hash_table_entry_hasher> cu_hash_type;
6793 /* Check whether we have already seen this CU and set up SYM_NUM
6794 accordingly. */
6795 static int
6796 check_duplicate_cu (dw_die_ref cu, cu_hash_type *htable, unsigned int *sym_num)
6798 struct cu_hash_table_entry dummy;
6799 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6801 dummy.max_comdat_num = 0;
6803 slot = htable->find_slot_with_hash (cu,
6804 htab_hash_string (cu->die_id.die_symbol),
6805 INSERT);
6806 entry = *slot;
6808 for (; entry; last = entry, entry = entry->next)
6810 if (same_die_p_wrap (cu, entry->cu))
6811 break;
6814 if (entry)
6816 *sym_num = entry->min_comdat_num;
6817 return 1;
6820 entry = XCNEW (struct cu_hash_table_entry);
6821 entry->cu = cu;
6822 entry->min_comdat_num = *sym_num = last->max_comdat_num;
6823 entry->next = *slot;
6824 *slot = entry;
6826 return 0;
6829 /* Record SYM_NUM to record of CU in HTABLE. */
6830 static void
6831 record_comdat_symbol_number (dw_die_ref cu, cu_hash_type *htable,
6832 unsigned int sym_num)
6834 struct cu_hash_table_entry **slot, *entry;
6836 slot = htable->find_slot_with_hash (cu,
6837 htab_hash_string (cu->die_id.die_symbol),
6838 NO_INSERT);
6839 entry = *slot;
6841 entry->max_comdat_num = sym_num;
6844 /* Traverse the DIE (which is always comp_unit_die), and set up
6845 additional compilation units for each of the include files we see
6846 bracketed by BINCL/EINCL. */
6848 static void
6849 break_out_includes (dw_die_ref die)
6851 dw_die_ref c;
6852 dw_die_ref unit = NULL;
6853 limbo_die_node *node, **pnode;
6855 c = die->die_child;
6856 if (c) do {
6857 dw_die_ref prev = c;
6858 c = c->die_sib;
6859 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6860 || (unit && is_comdat_die (c)))
6862 dw_die_ref next = c->die_sib;
6864 /* This DIE is for a secondary CU; remove it from the main one. */
6865 remove_child_with_prev (c, prev);
6867 if (c->die_tag == DW_TAG_GNU_BINCL)
6868 unit = push_new_compile_unit (unit, c);
6869 else if (c->die_tag == DW_TAG_GNU_EINCL)
6870 unit = pop_compile_unit (unit);
6871 else
6872 add_child_die (unit, c);
6873 c = next;
6874 if (c == die->die_child)
6875 break;
6877 } while (c != die->die_child);
6879 #if 0
6880 /* We can only use this in debugging, since the frontend doesn't check
6881 to make sure that we leave every include file we enter. */
6882 gcc_assert (!unit);
6883 #endif
6885 assign_symbol_names (die);
6886 cu_hash_type cu_hash_table (10);
6887 for (node = limbo_die_list, pnode = &limbo_die_list;
6888 node;
6889 node = node->next)
6891 int is_dupl;
6893 compute_section_prefix (node->die);
6894 is_dupl = check_duplicate_cu (node->die, &cu_hash_table,
6895 &comdat_symbol_number);
6896 assign_symbol_names (node->die);
6897 if (is_dupl)
6898 *pnode = node->next;
6899 else
6901 pnode = &node->next;
6902 record_comdat_symbol_number (node->die, &cu_hash_table,
6903 comdat_symbol_number);
6908 /* Return non-zero if this DIE is a declaration. */
6910 static int
6911 is_declaration_die (dw_die_ref die)
6913 dw_attr_ref a;
6914 unsigned ix;
6916 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6917 if (a->dw_attr == DW_AT_declaration)
6918 return 1;
6920 return 0;
6923 /* Return non-zero if this DIE is nested inside a subprogram. */
6925 static int
6926 is_nested_in_subprogram (dw_die_ref die)
6928 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
6930 if (decl == NULL)
6931 decl = die;
6932 return local_scope_p (decl);
6935 /* Return non-zero if this DIE contains a defining declaration of a
6936 subprogram. */
6938 static int
6939 contains_subprogram_definition (dw_die_ref die)
6941 dw_die_ref c;
6943 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
6944 return 1;
6945 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
6946 return 0;
6949 /* Return non-zero if this is a type DIE that should be moved to a
6950 COMDAT .debug_types section. */
6952 static int
6953 should_move_die_to_comdat (dw_die_ref die)
6955 switch (die->die_tag)
6957 case DW_TAG_class_type:
6958 case DW_TAG_structure_type:
6959 case DW_TAG_enumeration_type:
6960 case DW_TAG_union_type:
6961 /* Don't move declarations, inlined instances, types nested in a
6962 subprogram, or types that contain subprogram definitions. */
6963 if (is_declaration_die (die)
6964 || get_AT (die, DW_AT_abstract_origin)
6965 || is_nested_in_subprogram (die)
6966 || contains_subprogram_definition (die))
6967 return 0;
6968 return 1;
6969 case DW_TAG_array_type:
6970 case DW_TAG_interface_type:
6971 case DW_TAG_pointer_type:
6972 case DW_TAG_reference_type:
6973 case DW_TAG_rvalue_reference_type:
6974 case DW_TAG_string_type:
6975 case DW_TAG_subroutine_type:
6976 case DW_TAG_ptr_to_member_type:
6977 case DW_TAG_set_type:
6978 case DW_TAG_subrange_type:
6979 case DW_TAG_base_type:
6980 case DW_TAG_const_type:
6981 case DW_TAG_file_type:
6982 case DW_TAG_packed_type:
6983 case DW_TAG_volatile_type:
6984 case DW_TAG_typedef:
6985 default:
6986 return 0;
6990 /* Make a clone of DIE. */
6992 static dw_die_ref
6993 clone_die (dw_die_ref die)
6995 dw_die_ref clone;
6996 dw_attr_ref a;
6997 unsigned ix;
6999 clone = ggc_cleared_alloc<die_node> ();
7000 clone->die_tag = die->die_tag;
7002 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7003 add_dwarf_attr (clone, a);
7005 return clone;
7008 /* Make a clone of the tree rooted at DIE. */
7010 static dw_die_ref
7011 clone_tree (dw_die_ref die)
7013 dw_die_ref c;
7014 dw_die_ref clone = clone_die (die);
7016 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
7018 return clone;
7021 /* Make a clone of DIE as a declaration. */
7023 static dw_die_ref
7024 clone_as_declaration (dw_die_ref die)
7026 dw_die_ref clone;
7027 dw_die_ref decl;
7028 dw_attr_ref a;
7029 unsigned ix;
7031 /* If the DIE is already a declaration, just clone it. */
7032 if (is_declaration_die (die))
7033 return clone_die (die);
7035 /* If the DIE is a specification, just clone its declaration DIE. */
7036 decl = get_AT_ref (die, DW_AT_specification);
7037 if (decl != NULL)
7039 clone = clone_die (decl);
7040 if (die->comdat_type_p)
7041 add_AT_die_ref (clone, DW_AT_signature, die);
7042 return clone;
7045 clone = ggc_cleared_alloc<die_node> ();
7046 clone->die_tag = die->die_tag;
7048 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7050 /* We don't want to copy over all attributes.
7051 For example we don't want DW_AT_byte_size because otherwise we will no
7052 longer have a declaration and GDB will treat it as a definition. */
7054 switch (a->dw_attr)
7056 case DW_AT_abstract_origin:
7057 case DW_AT_artificial:
7058 case DW_AT_containing_type:
7059 case DW_AT_external:
7060 case DW_AT_name:
7061 case DW_AT_type:
7062 case DW_AT_virtuality:
7063 case DW_AT_linkage_name:
7064 case DW_AT_MIPS_linkage_name:
7065 add_dwarf_attr (clone, a);
7066 break;
7067 case DW_AT_byte_size:
7068 default:
7069 break;
7073 if (die->comdat_type_p)
7074 add_AT_die_ref (clone, DW_AT_signature, die);
7076 add_AT_flag (clone, DW_AT_declaration, 1);
7077 return clone;
7081 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
7083 struct decl_table_entry
7085 dw_die_ref orig;
7086 dw_die_ref copy;
7089 /* Helpers to manipulate hash table of copied declarations. */
7091 /* Hashtable helpers. */
7093 struct decl_table_entry_hasher : typed_free_remove <decl_table_entry>
7095 typedef decl_table_entry value_type;
7096 typedef die_struct compare_type;
7097 static inline hashval_t hash (const value_type *);
7098 static inline bool equal (const value_type *, const compare_type *);
7101 inline hashval_t
7102 decl_table_entry_hasher::hash (const value_type *entry)
7104 return htab_hash_pointer (entry->orig);
7107 inline bool
7108 decl_table_entry_hasher::equal (const value_type *entry1,
7109 const compare_type *entry2)
7111 return entry1->orig == entry2;
7114 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
7116 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7117 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
7118 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
7119 to check if the ancestor has already been copied into UNIT. */
7121 static dw_die_ref
7122 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
7123 decl_hash_type *decl_table)
7125 dw_die_ref parent = die->die_parent;
7126 dw_die_ref new_parent = unit;
7127 dw_die_ref copy;
7128 decl_table_entry **slot = NULL;
7129 struct decl_table_entry *entry = NULL;
7131 if (decl_table)
7133 /* Check if the entry has already been copied to UNIT. */
7134 slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
7135 INSERT);
7136 if (*slot != HTAB_EMPTY_ENTRY)
7138 entry = *slot;
7139 return entry->copy;
7142 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
7143 entry = XCNEW (struct decl_table_entry);
7144 entry->orig = die;
7145 entry->copy = NULL;
7146 *slot = entry;
7149 if (parent != NULL)
7151 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7152 if (spec != NULL)
7153 parent = spec;
7154 if (!is_unit_die (parent))
7155 new_parent = copy_ancestor_tree (unit, parent, decl_table);
7158 copy = clone_as_declaration (die);
7159 add_child_die (new_parent, copy);
7161 if (decl_table)
7163 /* Record the pointer to the copy. */
7164 entry->copy = copy;
7167 return copy;
7169 /* Copy the declaration context to the new type unit DIE. This includes
7170 any surrounding namespace or type declarations. If the DIE has an
7171 AT_specification attribute, it also includes attributes and children
7172 attached to the specification, and returns a pointer to the original
7173 parent of the declaration DIE. Returns NULL otherwise. */
7175 static dw_die_ref
7176 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7178 dw_die_ref decl;
7179 dw_die_ref new_decl;
7180 dw_die_ref orig_parent = NULL;
7182 decl = get_AT_ref (die, DW_AT_specification);
7183 if (decl == NULL)
7184 decl = die;
7185 else
7187 unsigned ix;
7188 dw_die_ref c;
7189 dw_attr_ref a;
7191 /* The original DIE will be changed to a declaration, and must
7192 be moved to be a child of the original declaration DIE. */
7193 orig_parent = decl->die_parent;
7195 /* Copy the type node pointer from the new DIE to the original
7196 declaration DIE so we can forward references later. */
7197 decl->comdat_type_p = true;
7198 decl->die_id.die_type_node = die->die_id.die_type_node;
7200 remove_AT (die, DW_AT_specification);
7202 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7204 if (a->dw_attr != DW_AT_name
7205 && a->dw_attr != DW_AT_declaration
7206 && a->dw_attr != DW_AT_external)
7207 add_dwarf_attr (die, a);
7210 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7213 if (decl->die_parent != NULL
7214 && !is_unit_die (decl->die_parent))
7216 new_decl = copy_ancestor_tree (unit, decl, NULL);
7217 if (new_decl != NULL)
7219 remove_AT (new_decl, DW_AT_signature);
7220 add_AT_specification (die, new_decl);
7224 return orig_parent;
7227 /* Generate the skeleton ancestor tree for the given NODE, then clone
7228 the DIE and add the clone into the tree. */
7230 static void
7231 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7233 if (node->new_die != NULL)
7234 return;
7236 node->new_die = clone_as_declaration (node->old_die);
7238 if (node->parent != NULL)
7240 generate_skeleton_ancestor_tree (node->parent);
7241 add_child_die (node->parent->new_die, node->new_die);
7245 /* Generate a skeleton tree of DIEs containing any declarations that are
7246 found in the original tree. We traverse the tree looking for declaration
7247 DIEs, and construct the skeleton from the bottom up whenever we find one. */
7249 static void
7250 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7252 skeleton_chain_node node;
7253 dw_die_ref c;
7254 dw_die_ref first;
7255 dw_die_ref prev = NULL;
7256 dw_die_ref next = NULL;
7258 node.parent = parent;
7260 first = c = parent->old_die->die_child;
7261 if (c)
7262 next = c->die_sib;
7263 if (c) do {
7264 if (prev == NULL || prev->die_sib == c)
7265 prev = c;
7266 c = next;
7267 next = (c == first ? NULL : c->die_sib);
7268 node.old_die = c;
7269 node.new_die = NULL;
7270 if (is_declaration_die (c))
7272 if (is_template_instantiation (c))
7274 /* Instantiated templates do not need to be cloned into the
7275 type unit. Just move the DIE and its children back to
7276 the skeleton tree (in the main CU). */
7277 remove_child_with_prev (c, prev);
7278 add_child_die (parent->new_die, c);
7279 c = prev;
7281 else
7283 /* Clone the existing DIE, move the original to the skeleton
7284 tree (which is in the main CU), and put the clone, with
7285 all the original's children, where the original came from
7286 (which is about to be moved to the type unit). */
7287 dw_die_ref clone = clone_die (c);
7288 move_all_children (c, clone);
7290 /* If the original has a DW_AT_object_pointer attribute,
7291 it would now point to a child DIE just moved to the
7292 cloned tree, so we need to remove that attribute from
7293 the original. */
7294 remove_AT (c, DW_AT_object_pointer);
7296 replace_child (c, clone, prev);
7297 generate_skeleton_ancestor_tree (parent);
7298 add_child_die (parent->new_die, c);
7299 node.new_die = c;
7300 c = clone;
7303 generate_skeleton_bottom_up (&node);
7304 } while (next != NULL);
7307 /* Wrapper function for generate_skeleton_bottom_up. */
7309 static dw_die_ref
7310 generate_skeleton (dw_die_ref die)
7312 skeleton_chain_node node;
7314 node.old_die = die;
7315 node.new_die = NULL;
7316 node.parent = NULL;
7318 /* If this type definition is nested inside another type,
7319 and is not an instantiation of a template, always leave
7320 at least a declaration in its place. */
7321 if (die->die_parent != NULL
7322 && is_type_die (die->die_parent)
7323 && !is_template_instantiation (die))
7324 node.new_die = clone_as_declaration (die);
7326 generate_skeleton_bottom_up (&node);
7327 return node.new_die;
7330 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
7331 declaration. The original DIE is moved to a new compile unit so that
7332 existing references to it follow it to the new location. If any of the
7333 original DIE's descendants is a declaration, we need to replace the
7334 original DIE with a skeleton tree and move the declarations back into the
7335 skeleton tree. */
7337 static dw_die_ref
7338 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
7339 dw_die_ref prev)
7341 dw_die_ref skeleton, orig_parent;
7343 /* Copy the declaration context to the type unit DIE. If the returned
7344 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
7345 that DIE. */
7346 orig_parent = copy_declaration_context (unit, child);
7348 skeleton = generate_skeleton (child);
7349 if (skeleton == NULL)
7350 remove_child_with_prev (child, prev);
7351 else
7353 skeleton->comdat_type_p = true;
7354 skeleton->die_id.die_type_node = child->die_id.die_type_node;
7356 /* If the original DIE was a specification, we need to put
7357 the skeleton under the parent DIE of the declaration.
7358 This leaves the original declaration in the tree, but
7359 it will be pruned later since there are no longer any
7360 references to it. */
7361 if (orig_parent != NULL)
7363 remove_child_with_prev (child, prev);
7364 add_child_die (orig_parent, skeleton);
7366 else
7367 replace_child (child, skeleton, prev);
7370 return skeleton;
7373 /* Traverse the DIE and set up additional .debug_types sections for each
7374 type worthy of being placed in a COMDAT section. */
7376 static void
7377 break_out_comdat_types (dw_die_ref die)
7379 dw_die_ref c;
7380 dw_die_ref first;
7381 dw_die_ref prev = NULL;
7382 dw_die_ref next = NULL;
7383 dw_die_ref unit = NULL;
7385 first = c = die->die_child;
7386 if (c)
7387 next = c->die_sib;
7388 if (c) do {
7389 if (prev == NULL || prev->die_sib == c)
7390 prev = c;
7391 c = next;
7392 next = (c == first ? NULL : c->die_sib);
7393 if (should_move_die_to_comdat (c))
7395 dw_die_ref replacement;
7396 comdat_type_node_ref type_node;
7398 /* Break out nested types into their own type units. */
7399 break_out_comdat_types (c);
7401 /* Create a new type unit DIE as the root for the new tree, and
7402 add it to the list of comdat types. */
7403 unit = new_die (DW_TAG_type_unit, NULL, NULL);
7404 add_AT_unsigned (unit, DW_AT_language,
7405 get_AT_unsigned (comp_unit_die (), DW_AT_language));
7406 type_node = ggc_cleared_alloc<comdat_type_node> ();
7407 type_node->root_die = unit;
7408 type_node->next = comdat_type_list;
7409 comdat_type_list = type_node;
7411 /* Generate the type signature. */
7412 generate_type_signature (c, type_node);
7414 /* Copy the declaration context, attributes, and children of the
7415 declaration into the new type unit DIE, then remove this DIE
7416 from the main CU (or replace it with a skeleton if necessary). */
7417 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
7418 type_node->skeleton_die = replacement;
7420 /* Add the DIE to the new compunit. */
7421 add_child_die (unit, c);
7423 if (replacement != NULL)
7424 c = replacement;
7426 else if (c->die_tag == DW_TAG_namespace
7427 || c->die_tag == DW_TAG_class_type
7428 || c->die_tag == DW_TAG_structure_type
7429 || c->die_tag == DW_TAG_union_type)
7431 /* Look for nested types that can be broken out. */
7432 break_out_comdat_types (c);
7434 } while (next != NULL);
7437 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
7438 Enter all the cloned children into the hash table decl_table. */
7440 static dw_die_ref
7441 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
7443 dw_die_ref c;
7444 dw_die_ref clone;
7445 struct decl_table_entry *entry;
7446 decl_table_entry **slot;
7448 if (die->die_tag == DW_TAG_subprogram)
7449 clone = clone_as_declaration (die);
7450 else
7451 clone = clone_die (die);
7453 slot = decl_table->find_slot_with_hash (die,
7454 htab_hash_pointer (die), INSERT);
7456 /* Assert that DIE isn't in the hash table yet. If it would be there
7457 before, the ancestors would be necessarily there as well, therefore
7458 clone_tree_partial wouldn't be called. */
7459 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
7461 entry = XCNEW (struct decl_table_entry);
7462 entry->orig = die;
7463 entry->copy = clone;
7464 *slot = entry;
7466 if (die->die_tag != DW_TAG_subprogram)
7467 FOR_EACH_CHILD (die, c,
7468 add_child_die (clone, clone_tree_partial (c, decl_table)));
7470 return clone;
7473 /* Walk the DIE and its children, looking for references to incomplete
7474 or trivial types that are unmarked (i.e., that are not in the current
7475 type_unit). */
7477 static void
7478 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
7480 dw_die_ref c;
7481 dw_attr_ref a;
7482 unsigned ix;
7484 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7486 if (AT_class (a) == dw_val_class_die_ref)
7488 dw_die_ref targ = AT_ref (a);
7489 decl_table_entry **slot;
7490 struct decl_table_entry *entry;
7492 if (targ->die_mark != 0 || targ->comdat_type_p)
7493 continue;
7495 slot = decl_table->find_slot_with_hash (targ,
7496 htab_hash_pointer (targ),
7497 INSERT);
7499 if (*slot != HTAB_EMPTY_ENTRY)
7501 /* TARG has already been copied, so we just need to
7502 modify the reference to point to the copy. */
7503 entry = *slot;
7504 a->dw_attr_val.v.val_die_ref.die = entry->copy;
7506 else
7508 dw_die_ref parent = unit;
7509 dw_die_ref copy = clone_die (targ);
7511 /* Record in DECL_TABLE that TARG has been copied.
7512 Need to do this now, before the recursive call,
7513 because DECL_TABLE may be expanded and SLOT
7514 would no longer be a valid pointer. */
7515 entry = XCNEW (struct decl_table_entry);
7516 entry->orig = targ;
7517 entry->copy = copy;
7518 *slot = entry;
7520 /* If TARG is not a declaration DIE, we need to copy its
7521 children. */
7522 if (!is_declaration_die (targ))
7524 FOR_EACH_CHILD (
7525 targ, c,
7526 add_child_die (copy,
7527 clone_tree_partial (c, decl_table)));
7530 /* Make sure the cloned tree is marked as part of the
7531 type unit. */
7532 mark_dies (copy);
7534 /* If TARG has surrounding context, copy its ancestor tree
7535 into the new type unit. */
7536 if (targ->die_parent != NULL
7537 && !is_unit_die (targ->die_parent))
7538 parent = copy_ancestor_tree (unit, targ->die_parent,
7539 decl_table);
7541 add_child_die (parent, copy);
7542 a->dw_attr_val.v.val_die_ref.die = copy;
7544 /* Make sure the newly-copied DIE is walked. If it was
7545 installed in a previously-added context, it won't
7546 get visited otherwise. */
7547 if (parent != unit)
7549 /* Find the highest point of the newly-added tree,
7550 mark each node along the way, and walk from there. */
7551 parent->die_mark = 1;
7552 while (parent->die_parent
7553 && parent->die_parent->die_mark == 0)
7555 parent = parent->die_parent;
7556 parent->die_mark = 1;
7558 copy_decls_walk (unit, parent, decl_table);
7564 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
7567 /* Copy declarations for "unworthy" types into the new comdat section.
7568 Incomplete types, modified types, and certain other types aren't broken
7569 out into comdat sections of their own, so they don't have a signature,
7570 and we need to copy the declaration into the same section so that we
7571 don't have an external reference. */
7573 static void
7574 copy_decls_for_unworthy_types (dw_die_ref unit)
7576 mark_dies (unit);
7577 decl_hash_type decl_table (10);
7578 copy_decls_walk (unit, unit, &decl_table);
7579 unmark_dies (unit);
7582 /* Traverse the DIE and add a sibling attribute if it may have the
7583 effect of speeding up access to siblings. To save some space,
7584 avoid generating sibling attributes for DIE's without children. */
7586 static void
7587 add_sibling_attributes (dw_die_ref die)
7589 dw_die_ref c;
7591 if (! die->die_child)
7592 return;
7594 if (die->die_parent && die != die->die_parent->die_child)
7595 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7597 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7600 /* Output all location lists for the DIE and its children. */
7602 static void
7603 output_location_lists (dw_die_ref die)
7605 dw_die_ref c;
7606 dw_attr_ref a;
7607 unsigned ix;
7609 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7610 if (AT_class (a) == dw_val_class_loc_list)
7611 output_loc_list (AT_loc_list (a));
7613 FOR_EACH_CHILD (die, c, output_location_lists (c));
7616 /* We want to limit the number of external references, because they are
7617 larger than local references: a relocation takes multiple words, and
7618 even a sig8 reference is always eight bytes, whereas a local reference
7619 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
7620 So if we encounter multiple external references to the same type DIE, we
7621 make a local typedef stub for it and redirect all references there.
7623 This is the element of the hash table for keeping track of these
7624 references. */
7626 struct external_ref
7628 dw_die_ref type;
7629 dw_die_ref stub;
7630 unsigned n_refs;
7633 /* Hashtable helpers. */
7635 struct external_ref_hasher : typed_free_remove <external_ref>
7637 typedef external_ref value_type;
7638 typedef external_ref compare_type;
7639 static inline hashval_t hash (const value_type *);
7640 static inline bool equal (const value_type *, const compare_type *);
7643 inline hashval_t
7644 external_ref_hasher::hash (const value_type *r)
7646 dw_die_ref die = r->type;
7647 hashval_t h = 0;
7649 /* We can't use the address of the DIE for hashing, because
7650 that will make the order of the stub DIEs non-deterministic. */
7651 if (! die->comdat_type_p)
7652 /* We have a symbol; use it to compute a hash. */
7653 h = htab_hash_string (die->die_id.die_symbol);
7654 else
7656 /* We have a type signature; use a subset of the bits as the hash.
7657 The 8-byte signature is at least as large as hashval_t. */
7658 comdat_type_node_ref type_node = die->die_id.die_type_node;
7659 memcpy (&h, type_node->signature, sizeof (h));
7661 return h;
7664 inline bool
7665 external_ref_hasher::equal (const value_type *r1, const compare_type *r2)
7667 return r1->type == r2->type;
7670 typedef hash_table<external_ref_hasher> external_ref_hash_type;
7672 /* Return a pointer to the external_ref for references to DIE. */
7674 static struct external_ref *
7675 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
7677 struct external_ref ref, *ref_p;
7678 external_ref **slot;
7680 ref.type = die;
7681 slot = map->find_slot (&ref, INSERT);
7682 if (*slot != HTAB_EMPTY_ENTRY)
7683 return *slot;
7685 ref_p = XCNEW (struct external_ref);
7686 ref_p->type = die;
7687 *slot = ref_p;
7688 return ref_p;
7691 /* Subroutine of optimize_external_refs, below.
7693 If we see a type skeleton, record it as our stub. If we see external
7694 references, remember how many we've seen. */
7696 static void
7697 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
7699 dw_die_ref c;
7700 dw_attr_ref a;
7701 unsigned ix;
7702 struct external_ref *ref_p;
7704 if (is_type_die (die)
7705 && (c = get_AT_ref (die, DW_AT_signature)))
7707 /* This is a local skeleton; use it for local references. */
7708 ref_p = lookup_external_ref (map, c);
7709 ref_p->stub = die;
7712 /* Scan the DIE references, and remember any that refer to DIEs from
7713 other CUs (i.e. those which are not marked). */
7714 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7715 if (AT_class (a) == dw_val_class_die_ref
7716 && (c = AT_ref (a))->die_mark == 0
7717 && is_type_die (c))
7719 ref_p = lookup_external_ref (map, c);
7720 ref_p->n_refs++;
7723 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
7726 /* htab_traverse callback function for optimize_external_refs, below. SLOT
7727 points to an external_ref, DATA is the CU we're processing. If we don't
7728 already have a local stub, and we have multiple refs, build a stub. */
7731 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
7733 struct external_ref *ref_p = *slot;
7735 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
7737 /* We have multiple references to this type, so build a small stub.
7738 Both of these forms are a bit dodgy from the perspective of the
7739 DWARF standard, since technically they should have names. */
7740 dw_die_ref cu = data;
7741 dw_die_ref type = ref_p->type;
7742 dw_die_ref stub = NULL;
7744 if (type->comdat_type_p)
7746 /* If we refer to this type via sig8, use AT_signature. */
7747 stub = new_die (type->die_tag, cu, NULL_TREE);
7748 add_AT_die_ref (stub, DW_AT_signature, type);
7750 else
7752 /* Otherwise, use a typedef with no name. */
7753 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
7754 add_AT_die_ref (stub, DW_AT_type, type);
7757 stub->die_mark++;
7758 ref_p->stub = stub;
7760 return 1;
7763 /* DIE is a unit; look through all the DIE references to see if there are
7764 any external references to types, and if so, create local stubs for
7765 them which will be applied in build_abbrev_table. This is useful because
7766 references to local DIEs are smaller. */
7768 static external_ref_hash_type *
7769 optimize_external_refs (dw_die_ref die)
7771 external_ref_hash_type *map = new external_ref_hash_type (10);
7772 optimize_external_refs_1 (die, map);
7773 map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
7774 return map;
7777 /* The format of each DIE (and its attribute value pairs) is encoded in an
7778 abbreviation table. This routine builds the abbreviation table and assigns
7779 a unique abbreviation id for each abbreviation entry. The children of each
7780 die are visited recursively. */
7782 static void
7783 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
7785 unsigned long abbrev_id;
7786 unsigned int n_alloc;
7787 dw_die_ref c;
7788 dw_attr_ref a;
7789 unsigned ix;
7791 /* Scan the DIE references, and replace any that refer to
7792 DIEs from other CUs (i.e. those which are not marked) with
7793 the local stubs we built in optimize_external_refs. */
7794 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7795 if (AT_class (a) == dw_val_class_die_ref
7796 && (c = AT_ref (a))->die_mark == 0)
7798 struct external_ref *ref_p;
7799 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
7801 ref_p = lookup_external_ref (extern_map, c);
7802 if (ref_p->stub && ref_p->stub != die)
7803 change_AT_die_ref (a, ref_p->stub);
7804 else
7805 /* We aren't changing this reference, so mark it external. */
7806 set_AT_ref_external (a, 1);
7809 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7811 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7812 dw_attr_ref die_a, abbrev_a;
7813 unsigned ix;
7814 bool ok = true;
7816 if (abbrev->die_tag != die->die_tag)
7817 continue;
7818 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7819 continue;
7821 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
7822 continue;
7824 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
7826 abbrev_a = &(*abbrev->die_attr)[ix];
7827 if ((abbrev_a->dw_attr != die_a->dw_attr)
7828 || (value_format (abbrev_a) != value_format (die_a)))
7830 ok = false;
7831 break;
7834 if (ok)
7835 break;
7838 if (abbrev_id >= abbrev_die_table_in_use)
7840 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7842 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
7843 abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7844 n_alloc);
7846 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7847 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
7848 abbrev_die_table_allocated = n_alloc;
7851 ++abbrev_die_table_in_use;
7852 abbrev_die_table[abbrev_id] = die;
7855 die->die_abbrev = abbrev_id;
7856 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
7859 /* Return the power-of-two number of bytes necessary to represent VALUE. */
7861 static int
7862 constant_size (unsigned HOST_WIDE_INT value)
7864 int log;
7866 if (value == 0)
7867 log = 0;
7868 else
7869 log = floor_log2 (value);
7871 log = log / 8;
7872 log = 1 << (floor_log2 (log) + 1);
7874 return log;
7877 /* Return the size of a DIE as it is represented in the
7878 .debug_info section. */
7880 static unsigned long
7881 size_of_die (dw_die_ref die)
7883 unsigned long size = 0;
7884 dw_attr_ref a;
7885 unsigned ix;
7886 enum dwarf_form form;
7888 size += size_of_uleb128 (die->die_abbrev);
7889 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7891 switch (AT_class (a))
7893 case dw_val_class_addr:
7894 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7896 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7897 size += size_of_uleb128 (AT_index (a));
7899 else
7900 size += DWARF2_ADDR_SIZE;
7901 break;
7902 case dw_val_class_offset:
7903 size += DWARF_OFFSET_SIZE;
7904 break;
7905 case dw_val_class_loc:
7907 unsigned long lsize = size_of_locs (AT_loc (a));
7909 /* Block length. */
7910 if (dwarf_version >= 4)
7911 size += size_of_uleb128 (lsize);
7912 else
7913 size += constant_size (lsize);
7914 size += lsize;
7916 break;
7917 case dw_val_class_loc_list:
7918 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7920 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7921 size += size_of_uleb128 (AT_index (a));
7923 else
7924 size += DWARF_OFFSET_SIZE;
7925 break;
7926 case dw_val_class_range_list:
7927 size += DWARF_OFFSET_SIZE;
7928 break;
7929 case dw_val_class_const:
7930 size += size_of_sleb128 (AT_int (a));
7931 break;
7932 case dw_val_class_unsigned_const:
7934 int csize = constant_size (AT_unsigned (a));
7935 if (dwarf_version == 3
7936 && a->dw_attr == DW_AT_data_member_location
7937 && csize >= 4)
7938 size += size_of_uleb128 (AT_unsigned (a));
7939 else
7940 size += csize;
7942 break;
7943 case dw_val_class_const_double:
7944 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
7945 if (HOST_BITS_PER_WIDE_INT >= 64)
7946 size++; /* block */
7947 break;
7948 case dw_val_class_wide_int:
7949 size += (get_full_len (*a->dw_attr_val.v.val_wide)
7950 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
7951 if (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT
7952 > 64)
7953 size++; /* block */
7954 break;
7955 case dw_val_class_vec:
7956 size += constant_size (a->dw_attr_val.v.val_vec.length
7957 * a->dw_attr_val.v.val_vec.elt_size)
7958 + a->dw_attr_val.v.val_vec.length
7959 * a->dw_attr_val.v.val_vec.elt_size; /* block */
7960 break;
7961 case dw_val_class_flag:
7962 if (dwarf_version >= 4)
7963 /* Currently all add_AT_flag calls pass in 1 as last argument,
7964 so DW_FORM_flag_present can be used. If that ever changes,
7965 we'll need to use DW_FORM_flag and have some optimization
7966 in build_abbrev_table that will change those to
7967 DW_FORM_flag_present if it is set to 1 in all DIEs using
7968 the same abbrev entry. */
7969 gcc_assert (a->dw_attr_val.v.val_flag == 1);
7970 else
7971 size += 1;
7972 break;
7973 case dw_val_class_die_ref:
7974 if (AT_ref_external (a))
7976 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
7977 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
7978 is sized by target address length, whereas in DWARF3
7979 it's always sized as an offset. */
7980 if (use_debug_types)
7981 size += DWARF_TYPE_SIGNATURE_SIZE;
7982 else if (dwarf_version == 2)
7983 size += DWARF2_ADDR_SIZE;
7984 else
7985 size += DWARF_OFFSET_SIZE;
7987 else
7988 size += DWARF_OFFSET_SIZE;
7989 break;
7990 case dw_val_class_fde_ref:
7991 size += DWARF_OFFSET_SIZE;
7992 break;
7993 case dw_val_class_lbl_id:
7994 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7996 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7997 size += size_of_uleb128 (AT_index (a));
7999 else
8000 size += DWARF2_ADDR_SIZE;
8001 break;
8002 case dw_val_class_lineptr:
8003 case dw_val_class_macptr:
8004 size += DWARF_OFFSET_SIZE;
8005 break;
8006 case dw_val_class_str:
8007 form = AT_string_form (a);
8008 if (form == DW_FORM_strp)
8009 size += DWARF_OFFSET_SIZE;
8010 else if (form == DW_FORM_GNU_str_index)
8011 size += size_of_uleb128 (AT_index (a));
8012 else
8013 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
8014 break;
8015 case dw_val_class_file:
8016 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
8017 break;
8018 case dw_val_class_data8:
8019 size += 8;
8020 break;
8021 case dw_val_class_vms_delta:
8022 size += DWARF_OFFSET_SIZE;
8023 break;
8024 case dw_val_class_high_pc:
8025 size += DWARF2_ADDR_SIZE;
8026 break;
8027 default:
8028 gcc_unreachable ();
8032 return size;
8035 /* Size the debugging information associated with a given DIE. Visits the
8036 DIE's children recursively. Updates the global variable next_die_offset, on
8037 each time through. Uses the current value of next_die_offset to update the
8038 die_offset field in each DIE. */
8040 static void
8041 calc_die_sizes (dw_die_ref die)
8043 dw_die_ref c;
8045 gcc_assert (die->die_offset == 0
8046 || (unsigned long int) die->die_offset == next_die_offset);
8047 die->die_offset = next_die_offset;
8048 next_die_offset += size_of_die (die);
8050 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
8052 if (die->die_child != NULL)
8053 /* Count the null byte used to terminate sibling lists. */
8054 next_die_offset += 1;
8057 /* Size just the base type children at the start of the CU.
8058 This is needed because build_abbrev needs to size locs
8059 and sizing of type based stack ops needs to know die_offset
8060 values for the base types. */
8062 static void
8063 calc_base_type_die_sizes (void)
8065 unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8066 unsigned int i;
8067 dw_die_ref base_type;
8068 #if ENABLE_ASSERT_CHECKING
8069 dw_die_ref prev = comp_unit_die ()->die_child;
8070 #endif
8072 die_offset += size_of_die (comp_unit_die ());
8073 for (i = 0; base_types.iterate (i, &base_type); i++)
8075 #if ENABLE_ASSERT_CHECKING
8076 gcc_assert (base_type->die_offset == 0
8077 && prev->die_sib == base_type
8078 && base_type->die_child == NULL
8079 && base_type->die_abbrev);
8080 prev = base_type;
8081 #endif
8082 base_type->die_offset = die_offset;
8083 die_offset += size_of_die (base_type);
8087 /* Set the marks for a die and its children. We do this so
8088 that we know whether or not a reference needs to use FORM_ref_addr; only
8089 DIEs in the same CU will be marked. We used to clear out the offset
8090 and use that as the flag, but ran into ordering problems. */
8092 static void
8093 mark_dies (dw_die_ref die)
8095 dw_die_ref c;
8097 gcc_assert (!die->die_mark);
8099 die->die_mark = 1;
8100 FOR_EACH_CHILD (die, c, mark_dies (c));
8103 /* Clear the marks for a die and its children. */
8105 static void
8106 unmark_dies (dw_die_ref die)
8108 dw_die_ref c;
8110 if (! use_debug_types)
8111 gcc_assert (die->die_mark);
8113 die->die_mark = 0;
8114 FOR_EACH_CHILD (die, c, unmark_dies (c));
8117 /* Clear the marks for a die, its children and referred dies. */
8119 static void
8120 unmark_all_dies (dw_die_ref die)
8122 dw_die_ref c;
8123 dw_attr_ref a;
8124 unsigned ix;
8126 if (!die->die_mark)
8127 return;
8128 die->die_mark = 0;
8130 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
8132 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8133 if (AT_class (a) == dw_val_class_die_ref)
8134 unmark_all_dies (AT_ref (a));
8137 /* Calculate if the entry should appear in the final output file. It may be
8138 from a pruned a type. */
8140 static bool
8141 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
8143 /* By limiting gnu pubnames to definitions only, gold can generate a
8144 gdb index without entries for declarations, which don't include
8145 enough information to be useful. */
8146 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
8147 return false;
8149 if (table == pubname_table)
8151 /* Enumerator names are part of the pubname table, but the
8152 parent DW_TAG_enumeration_type die may have been pruned.
8153 Don't output them if that is the case. */
8154 if (p->die->die_tag == DW_TAG_enumerator &&
8155 (p->die->die_parent == NULL
8156 || !p->die->die_parent->die_perennial_p))
8157 return false;
8159 /* Everything else in the pubname table is included. */
8160 return true;
8163 /* The pubtypes table shouldn't include types that have been
8164 pruned. */
8165 return (p->die->die_offset != 0
8166 || !flag_eliminate_unused_debug_types);
8169 /* Return the size of the .debug_pubnames or .debug_pubtypes table
8170 generated for the compilation unit. */
8172 static unsigned long
8173 size_of_pubnames (vec<pubname_entry, va_gc> *names)
8175 unsigned long size;
8176 unsigned i;
8177 pubname_ref p;
8178 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
8180 size = DWARF_PUBNAMES_HEADER_SIZE;
8181 FOR_EACH_VEC_ELT (*names, i, p)
8182 if (include_pubname_in_output (names, p))
8183 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
8185 size += DWARF_OFFSET_SIZE;
8186 return size;
8189 /* Return the size of the information in the .debug_aranges section. */
8191 static unsigned long
8192 size_of_aranges (void)
8194 unsigned long size;
8196 size = DWARF_ARANGES_HEADER_SIZE;
8198 /* Count the address/length pair for this compilation unit. */
8199 if (text_section_used)
8200 size += 2 * DWARF2_ADDR_SIZE;
8201 if (cold_text_section_used)
8202 size += 2 * DWARF2_ADDR_SIZE;
8203 if (have_multiple_function_sections)
8205 unsigned fde_idx;
8206 dw_fde_ref fde;
8208 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
8210 if (DECL_IGNORED_P (fde->decl))
8211 continue;
8212 if (!fde->in_std_section)
8213 size += 2 * DWARF2_ADDR_SIZE;
8214 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
8215 size += 2 * DWARF2_ADDR_SIZE;
8219 /* Count the two zero words used to terminated the address range table. */
8220 size += 2 * DWARF2_ADDR_SIZE;
8221 return size;
8224 /* Select the encoding of an attribute value. */
8226 static enum dwarf_form
8227 value_format (dw_attr_ref a)
8229 switch (AT_class (a))
8231 case dw_val_class_addr:
8232 /* Only very few attributes allow DW_FORM_addr. */
8233 switch (a->dw_attr)
8235 case DW_AT_low_pc:
8236 case DW_AT_high_pc:
8237 case DW_AT_entry_pc:
8238 case DW_AT_trampoline:
8239 return (AT_index (a) == NOT_INDEXED
8240 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8241 default:
8242 break;
8244 switch (DWARF2_ADDR_SIZE)
8246 case 1:
8247 return DW_FORM_data1;
8248 case 2:
8249 return DW_FORM_data2;
8250 case 4:
8251 return DW_FORM_data4;
8252 case 8:
8253 return DW_FORM_data8;
8254 default:
8255 gcc_unreachable ();
8257 case dw_val_class_range_list:
8258 case dw_val_class_loc_list:
8259 if (dwarf_version >= 4)
8260 return DW_FORM_sec_offset;
8261 /* FALLTHRU */
8262 case dw_val_class_vms_delta:
8263 case dw_val_class_offset:
8264 switch (DWARF_OFFSET_SIZE)
8266 case 4:
8267 return DW_FORM_data4;
8268 case 8:
8269 return DW_FORM_data8;
8270 default:
8271 gcc_unreachable ();
8273 case dw_val_class_loc:
8274 if (dwarf_version >= 4)
8275 return DW_FORM_exprloc;
8276 switch (constant_size (size_of_locs (AT_loc (a))))
8278 case 1:
8279 return DW_FORM_block1;
8280 case 2:
8281 return DW_FORM_block2;
8282 case 4:
8283 return DW_FORM_block4;
8284 default:
8285 gcc_unreachable ();
8287 case dw_val_class_const:
8288 return DW_FORM_sdata;
8289 case dw_val_class_unsigned_const:
8290 switch (constant_size (AT_unsigned (a)))
8292 case 1:
8293 return DW_FORM_data1;
8294 case 2:
8295 return DW_FORM_data2;
8296 case 4:
8297 /* In DWARF3 DW_AT_data_member_location with
8298 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
8299 constant, so we need to use DW_FORM_udata if we need
8300 a large constant. */
8301 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8302 return DW_FORM_udata;
8303 return DW_FORM_data4;
8304 case 8:
8305 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8306 return DW_FORM_udata;
8307 return DW_FORM_data8;
8308 default:
8309 gcc_unreachable ();
8311 case dw_val_class_const_double:
8312 switch (HOST_BITS_PER_WIDE_INT)
8314 case 8:
8315 return DW_FORM_data2;
8316 case 16:
8317 return DW_FORM_data4;
8318 case 32:
8319 return DW_FORM_data8;
8320 case 64:
8321 default:
8322 return DW_FORM_block1;
8324 case dw_val_class_wide_int:
8325 switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
8327 case 8:
8328 return DW_FORM_data1;
8329 case 16:
8330 return DW_FORM_data2;
8331 case 32:
8332 return DW_FORM_data4;
8333 case 64:
8334 return DW_FORM_data8;
8335 default:
8336 return DW_FORM_block1;
8338 case dw_val_class_vec:
8339 switch (constant_size (a->dw_attr_val.v.val_vec.length
8340 * a->dw_attr_val.v.val_vec.elt_size))
8342 case 1:
8343 return DW_FORM_block1;
8344 case 2:
8345 return DW_FORM_block2;
8346 case 4:
8347 return DW_FORM_block4;
8348 default:
8349 gcc_unreachable ();
8351 case dw_val_class_flag:
8352 if (dwarf_version >= 4)
8354 /* Currently all add_AT_flag calls pass in 1 as last argument,
8355 so DW_FORM_flag_present can be used. If that ever changes,
8356 we'll need to use DW_FORM_flag and have some optimization
8357 in build_abbrev_table that will change those to
8358 DW_FORM_flag_present if it is set to 1 in all DIEs using
8359 the same abbrev entry. */
8360 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8361 return DW_FORM_flag_present;
8363 return DW_FORM_flag;
8364 case dw_val_class_die_ref:
8365 if (AT_ref_external (a))
8366 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
8367 else
8368 return DW_FORM_ref;
8369 case dw_val_class_fde_ref:
8370 return DW_FORM_data;
8371 case dw_val_class_lbl_id:
8372 return (AT_index (a) == NOT_INDEXED
8373 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8374 case dw_val_class_lineptr:
8375 case dw_val_class_macptr:
8376 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
8377 case dw_val_class_str:
8378 return AT_string_form (a);
8379 case dw_val_class_file:
8380 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8382 case 1:
8383 return DW_FORM_data1;
8384 case 2:
8385 return DW_FORM_data2;
8386 case 4:
8387 return DW_FORM_data4;
8388 default:
8389 gcc_unreachable ();
8392 case dw_val_class_data8:
8393 return DW_FORM_data8;
8395 case dw_val_class_high_pc:
8396 switch (DWARF2_ADDR_SIZE)
8398 case 1:
8399 return DW_FORM_data1;
8400 case 2:
8401 return DW_FORM_data2;
8402 case 4:
8403 return DW_FORM_data4;
8404 case 8:
8405 return DW_FORM_data8;
8406 default:
8407 gcc_unreachable ();
8410 default:
8411 gcc_unreachable ();
8415 /* Output the encoding of an attribute value. */
8417 static void
8418 output_value_format (dw_attr_ref a)
8420 enum dwarf_form form = value_format (a);
8422 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8425 /* Given a die and id, produce the appropriate abbreviations. */
8427 static void
8428 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
8430 unsigned ix;
8431 dw_attr_ref a_attr;
8433 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8434 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8435 dwarf_tag_name (abbrev->die_tag));
8437 if (abbrev->die_child != NULL)
8438 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8439 else
8440 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8442 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
8444 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8445 dwarf_attr_name (a_attr->dw_attr));
8446 output_value_format (a_attr);
8449 dw2_asm_output_data (1, 0, NULL);
8450 dw2_asm_output_data (1, 0, NULL);
8454 /* Output the .debug_abbrev section which defines the DIE abbreviation
8455 table. */
8457 static void
8458 output_abbrev_section (void)
8460 unsigned long abbrev_id;
8462 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8463 output_die_abbrevs (abbrev_id, abbrev_die_table[abbrev_id]);
8465 /* Terminate the table. */
8466 dw2_asm_output_data (1, 0, NULL);
8469 /* Output a symbol we can use to refer to this DIE from another CU. */
8471 static inline void
8472 output_die_symbol (dw_die_ref die)
8474 const char *sym = die->die_id.die_symbol;
8476 gcc_assert (!die->comdat_type_p);
8478 if (sym == 0)
8479 return;
8481 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8482 /* We make these global, not weak; if the target doesn't support
8483 .linkonce, it doesn't support combining the sections, so debugging
8484 will break. */
8485 targetm.asm_out.globalize_label (asm_out_file, sym);
8487 ASM_OUTPUT_LABEL (asm_out_file, sym);
8490 /* Return a new location list, given the begin and end range, and the
8491 expression. */
8493 static inline dw_loc_list_ref
8494 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8495 const char *section)
8497 dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
8499 retlist->begin = begin;
8500 retlist->begin_entry = NULL;
8501 retlist->end = end;
8502 retlist->expr = expr;
8503 retlist->section = section;
8505 return retlist;
8508 /* Generate a new internal symbol for this location list node, if it
8509 hasn't got one yet. */
8511 static inline void
8512 gen_llsym (dw_loc_list_ref list)
8514 gcc_assert (!list->ll_symbol);
8515 list->ll_symbol = gen_internal_sym ("LLST");
8518 /* Output the location list given to us. */
8520 static void
8521 output_loc_list (dw_loc_list_ref list_head)
8523 dw_loc_list_ref curr = list_head;
8525 if (list_head->emitted)
8526 return;
8527 list_head->emitted = true;
8529 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8531 /* Walk the location list, and output each range + expression. */
8532 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8534 unsigned long size;
8535 /* Don't output an entry that starts and ends at the same address. */
8536 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
8537 continue;
8538 size = size_of_locs (curr->expr);
8539 /* If the expression is too large, drop it on the floor. We could
8540 perhaps put it into DW_TAG_dwarf_procedure and refer to that
8541 in the expression, but >= 64KB expressions for a single value
8542 in a single range are unlikely very useful. */
8543 if (size > 0xffff)
8544 continue;
8545 if (dwarf_split_debug_info)
8547 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
8548 "Location list start/length entry (%s)",
8549 list_head->ll_symbol);
8550 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
8551 "Location list range start index (%s)",
8552 curr->begin);
8553 /* The length field is 4 bytes. If we ever need to support
8554 an 8-byte length, we can add a new DW_LLE code or fall back
8555 to DW_LLE_GNU_start_end_entry. */
8556 dw2_asm_output_delta (4, curr->end, curr->begin,
8557 "Location list range length (%s)",
8558 list_head->ll_symbol);
8560 else if (!have_multiple_function_sections)
8562 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8563 "Location list begin address (%s)",
8564 list_head->ll_symbol);
8565 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8566 "Location list end address (%s)",
8567 list_head->ll_symbol);
8569 else
8571 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8572 "Location list begin address (%s)",
8573 list_head->ll_symbol);
8574 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8575 "Location list end address (%s)",
8576 list_head->ll_symbol);
8579 /* Output the block length for this list of location operations. */
8580 gcc_assert (size <= 0xffff);
8581 dw2_asm_output_data (2, size, "%s", "Location expression size");
8583 output_loc_sequence (curr->expr, -1);
8586 if (dwarf_split_debug_info)
8587 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
8588 "Location list terminator (%s)",
8589 list_head->ll_symbol);
8590 else
8592 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8593 "Location list terminator begin (%s)",
8594 list_head->ll_symbol);
8595 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8596 "Location list terminator end (%s)",
8597 list_head->ll_symbol);
8601 /* Output a range_list offset into the debug_range section. Emit a
8602 relocated reference if val_entry is NULL, otherwise, emit an
8603 indirect reference. */
8605 static void
8606 output_range_list_offset (dw_attr_ref a)
8608 const char *name = dwarf_attr_name (a->dw_attr);
8610 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
8612 char *p = strchr (ranges_section_label, '\0');
8613 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX, a->dw_attr_val.v.val_offset);
8614 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8615 debug_ranges_section, "%s", name);
8616 *p = '\0';
8618 else
8619 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8620 "%s (offset from %s)", name, ranges_section_label);
8623 /* Output the offset into the debug_loc section. */
8625 static void
8626 output_loc_list_offset (dw_attr_ref a)
8628 char *sym = AT_loc_list (a)->ll_symbol;
8630 gcc_assert (sym);
8631 if (dwarf_split_debug_info)
8632 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
8633 "%s", dwarf_attr_name (a->dw_attr));
8634 else
8635 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8636 "%s", dwarf_attr_name (a->dw_attr));
8639 /* Output an attribute's index or value appropriately. */
8641 static void
8642 output_attr_index_or_value (dw_attr_ref a)
8644 const char *name = dwarf_attr_name (a->dw_attr);
8646 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8648 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
8649 return;
8651 switch (AT_class (a))
8653 case dw_val_class_addr:
8654 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8655 break;
8656 case dw_val_class_high_pc:
8657 case dw_val_class_lbl_id:
8658 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8659 break;
8660 case dw_val_class_loc_list:
8661 output_loc_list_offset (a);
8662 break;
8663 default:
8664 gcc_unreachable ();
8668 /* Output a type signature. */
8670 static inline void
8671 output_signature (const char *sig, const char *name)
8673 int i;
8675 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8676 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
8679 /* Output the DIE and its attributes. Called recursively to generate
8680 the definitions of each child DIE. */
8682 static void
8683 output_die (dw_die_ref die)
8685 dw_attr_ref a;
8686 dw_die_ref c;
8687 unsigned long size;
8688 unsigned ix;
8690 /* If someone in another CU might refer to us, set up a symbol for
8691 them to point to. */
8692 if (! die->comdat_type_p && die->die_id.die_symbol)
8693 output_die_symbol (die);
8695 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
8696 (unsigned long)die->die_offset,
8697 dwarf_tag_name (die->die_tag));
8699 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8701 const char *name = dwarf_attr_name (a->dw_attr);
8703 switch (AT_class (a))
8705 case dw_val_class_addr:
8706 output_attr_index_or_value (a);
8707 break;
8709 case dw_val_class_offset:
8710 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8711 "%s", name);
8712 break;
8714 case dw_val_class_range_list:
8715 output_range_list_offset (a);
8716 break;
8718 case dw_val_class_loc:
8719 size = size_of_locs (AT_loc (a));
8721 /* Output the block length for this list of location operations. */
8722 if (dwarf_version >= 4)
8723 dw2_asm_output_data_uleb128 (size, "%s", name);
8724 else
8725 dw2_asm_output_data (constant_size (size), size, "%s", name);
8727 output_loc_sequence (AT_loc (a), -1);
8728 break;
8730 case dw_val_class_const:
8731 /* ??? It would be slightly more efficient to use a scheme like is
8732 used for unsigned constants below, but gdb 4.x does not sign
8733 extend. Gdb 5.x does sign extend. */
8734 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8735 break;
8737 case dw_val_class_unsigned_const:
8739 int csize = constant_size (AT_unsigned (a));
8740 if (dwarf_version == 3
8741 && a->dw_attr == DW_AT_data_member_location
8742 && csize >= 4)
8743 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
8744 else
8745 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
8747 break;
8749 case dw_val_class_const_double:
8751 unsigned HOST_WIDE_INT first, second;
8753 if (HOST_BITS_PER_WIDE_INT >= 64)
8754 dw2_asm_output_data (1,
8755 HOST_BITS_PER_DOUBLE_INT
8756 / HOST_BITS_PER_CHAR,
8757 NULL);
8759 if (WORDS_BIG_ENDIAN)
8761 first = a->dw_attr_val.v.val_double.high;
8762 second = a->dw_attr_val.v.val_double.low;
8764 else
8766 first = a->dw_attr_val.v.val_double.low;
8767 second = a->dw_attr_val.v.val_double.high;
8770 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8771 first, "%s", name);
8772 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8773 second, NULL);
8775 break;
8777 case dw_val_class_wide_int:
8779 int i;
8780 int len = get_full_len (*a->dw_attr_val.v.val_wide);
8781 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
8782 if (len * HOST_BITS_PER_WIDE_INT > 64)
8783 dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide) * l,
8784 NULL);
8786 if (WORDS_BIG_ENDIAN)
8787 for (i = len - 1; i >= 0; --i)
8789 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
8790 name);
8791 name = NULL;
8793 else
8794 for (i = 0; i < len; ++i)
8796 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
8797 name);
8798 name = NULL;
8801 break;
8803 case dw_val_class_vec:
8805 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8806 unsigned int len = a->dw_attr_val.v.val_vec.length;
8807 unsigned int i;
8808 unsigned char *p;
8810 dw2_asm_output_data (constant_size (len * elt_size),
8811 len * elt_size, "%s", name);
8812 if (elt_size > sizeof (HOST_WIDE_INT))
8814 elt_size /= 2;
8815 len *= 2;
8817 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8818 i < len;
8819 i++, p += elt_size)
8820 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8821 "fp or vector constant word %u", i);
8822 break;
8825 case dw_val_class_flag:
8826 if (dwarf_version >= 4)
8828 /* Currently all add_AT_flag calls pass in 1 as last argument,
8829 so DW_FORM_flag_present can be used. If that ever changes,
8830 we'll need to use DW_FORM_flag and have some optimization
8831 in build_abbrev_table that will change those to
8832 DW_FORM_flag_present if it is set to 1 in all DIEs using
8833 the same abbrev entry. */
8834 gcc_assert (AT_flag (a) == 1);
8835 if (flag_debug_asm)
8836 fprintf (asm_out_file, "\t\t\t%s %s\n",
8837 ASM_COMMENT_START, name);
8838 break;
8840 dw2_asm_output_data (1, AT_flag (a), "%s", name);
8841 break;
8843 case dw_val_class_loc_list:
8844 output_attr_index_or_value (a);
8845 break;
8847 case dw_val_class_die_ref:
8848 if (AT_ref_external (a))
8850 if (AT_ref (a)->comdat_type_p)
8852 comdat_type_node_ref type_node =
8853 AT_ref (a)->die_id.die_type_node;
8855 gcc_assert (type_node);
8856 output_signature (type_node->signature, name);
8858 else
8860 const char *sym = AT_ref (a)->die_id.die_symbol;
8861 int size;
8863 gcc_assert (sym);
8864 /* In DWARF2, DW_FORM_ref_addr is sized by target address
8865 length, whereas in DWARF3 it's always sized as an
8866 offset. */
8867 if (dwarf_version == 2)
8868 size = DWARF2_ADDR_SIZE;
8869 else
8870 size = DWARF_OFFSET_SIZE;
8871 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
8872 name);
8875 else
8877 gcc_assert (AT_ref (a)->die_offset);
8878 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8879 "%s", name);
8881 break;
8883 case dw_val_class_fde_ref:
8885 char l1[20];
8887 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
8888 a->dw_attr_val.v.val_fde_index * 2);
8889 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
8890 "%s", name);
8892 break;
8894 case dw_val_class_vms_delta:
8895 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
8896 AT_vms_delta2 (a), AT_vms_delta1 (a),
8897 "%s", name);
8898 break;
8900 case dw_val_class_lbl_id:
8901 output_attr_index_or_value (a);
8902 break;
8904 case dw_val_class_lineptr:
8905 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8906 debug_line_section, "%s", name);
8907 break;
8909 case dw_val_class_macptr:
8910 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8911 debug_macinfo_section, "%s", name);
8912 break;
8914 case dw_val_class_str:
8915 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
8916 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8917 a->dw_attr_val.v.val_str->label,
8918 debug_str_section,
8919 "%s: \"%s\"", name, AT_string (a));
8920 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
8921 dw2_asm_output_data_uleb128 (AT_index (a),
8922 "%s: \"%s\"", name, AT_string (a));
8923 else
8924 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
8925 break;
8927 case dw_val_class_file:
8929 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
8931 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
8932 a->dw_attr_val.v.val_file->filename);
8933 break;
8936 case dw_val_class_data8:
8938 int i;
8940 for (i = 0; i < 8; i++)
8941 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
8942 i == 0 ? "%s" : NULL, name);
8943 break;
8946 case dw_val_class_high_pc:
8947 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
8948 get_AT_low_pc (die), "DW_AT_high_pc");
8949 break;
8951 default:
8952 gcc_unreachable ();
8956 FOR_EACH_CHILD (die, c, output_die (c));
8958 /* Add null byte to terminate sibling list. */
8959 if (die->die_child != NULL)
8960 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
8961 (unsigned long) die->die_offset);
8964 /* Output the compilation unit that appears at the beginning of the
8965 .debug_info section, and precedes the DIE descriptions. */
8967 static void
8968 output_compilation_unit_header (void)
8970 /* We don't support actual DWARFv5 units yet, we just use some
8971 DWARFv5 draft DIE tags in DWARFv4 format. */
8972 int ver = dwarf_version < 5 ? dwarf_version : 4;
8974 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8975 dw2_asm_output_data (4, 0xffffffff,
8976 "Initial length escape value indicating 64-bit DWARF extension");
8977 dw2_asm_output_data (DWARF_OFFSET_SIZE,
8978 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
8979 "Length of Compilation Unit Info");
8980 dw2_asm_output_data (2, ver, "DWARF version number");
8981 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
8982 debug_abbrev_section,
8983 "Offset Into Abbrev. Section");
8984 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8987 /* Output the compilation unit DIE and its children. */
8989 static void
8990 output_comp_unit (dw_die_ref die, int output_if_empty)
8992 const char *secname, *oldsym;
8993 char *tmp;
8995 /* Unless we are outputting main CU, we may throw away empty ones. */
8996 if (!output_if_empty && die->die_child == NULL)
8997 return;
8999 /* Even if there are no children of this DIE, we must output the information
9000 about the compilation unit. Otherwise, on an empty translation unit, we
9001 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
9002 will then complain when examining the file. First mark all the DIEs in
9003 this CU so we know which get local refs. */
9004 mark_dies (die);
9006 external_ref_hash_type *extern_map = optimize_external_refs (die);
9008 build_abbrev_table (die, extern_map);
9010 delete extern_map;
9012 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
9013 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
9014 calc_die_sizes (die);
9016 oldsym = die->die_id.die_symbol;
9017 if (oldsym)
9019 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
9021 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
9022 secname = tmp;
9023 die->die_id.die_symbol = NULL;
9024 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9026 else
9028 switch_to_section (debug_info_section);
9029 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
9030 info_section_emitted = true;
9033 /* Output debugging information. */
9034 output_compilation_unit_header ();
9035 output_die (die);
9037 /* Leave the marks on the main CU, so we can check them in
9038 output_pubnames. */
9039 if (oldsym)
9041 unmark_dies (die);
9042 die->die_id.die_symbol = oldsym;
9046 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
9047 and .debug_pubtypes. This is configured per-target, but can be
9048 overridden by the -gpubnames or -gno-pubnames options. */
9050 static inline bool
9051 want_pubnames (void)
9053 if (debug_info_level <= DINFO_LEVEL_TERSE)
9054 return false;
9055 if (debug_generate_pub_sections != -1)
9056 return debug_generate_pub_sections;
9057 return targetm.want_debug_pub_sections;
9060 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
9062 static void
9063 add_AT_pubnames (dw_die_ref die)
9065 if (want_pubnames ())
9066 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
9069 /* Add a string attribute value to a skeleton DIE. */
9071 static inline void
9072 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
9073 const char *str)
9075 dw_attr_node attr;
9076 struct indirect_string_node *node;
9078 if (! skeleton_debug_str_hash)
9079 skeleton_debug_str_hash
9080 = hash_table<indirect_string_hasher>::create_ggc (10);
9082 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
9083 find_string_form (node);
9084 if (node->form == DW_FORM_GNU_str_index)
9085 node->form = DW_FORM_strp;
9087 attr.dw_attr = attr_kind;
9088 attr.dw_attr_val.val_class = dw_val_class_str;
9089 attr.dw_attr_val.val_entry = NULL;
9090 attr.dw_attr_val.v.val_str = node;
9091 add_dwarf_attr (die, &attr);
9094 /* Helper function to generate top-level dies for skeleton debug_info and
9095 debug_types. */
9097 static void
9098 add_top_level_skeleton_die_attrs (dw_die_ref die)
9100 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
9101 const char *comp_dir = comp_dir_string ();
9103 add_skeleton_AT_string (die, DW_AT_GNU_dwo_name, dwo_file_name);
9104 if (comp_dir != NULL)
9105 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
9106 add_AT_pubnames (die);
9107 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
9110 /* Output skeleton debug sections that point to the dwo file. */
9112 static void
9113 output_skeleton_debug_sections (dw_die_ref comp_unit)
9115 /* We don't support actual DWARFv5 units yet, we just use some
9116 DWARFv5 draft DIE tags in DWARFv4 format. */
9117 int ver = dwarf_version < 5 ? dwarf_version : 4;
9119 /* These attributes will be found in the full debug_info section. */
9120 remove_AT (comp_unit, DW_AT_producer);
9121 remove_AT (comp_unit, DW_AT_language);
9123 switch_to_section (debug_skeleton_info_section);
9124 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
9126 /* Produce the skeleton compilation-unit header. This one differs enough from
9127 a normal CU header that it's better not to call output_compilation_unit
9128 header. */
9129 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9130 dw2_asm_output_data (4, 0xffffffff,
9131 "Initial length escape value indicating 64-bit DWARF extension");
9133 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9134 DWARF_COMPILE_UNIT_HEADER_SIZE
9135 - DWARF_INITIAL_LENGTH_SIZE
9136 + size_of_die (comp_unit),
9137 "Length of Compilation Unit Info");
9138 dw2_asm_output_data (2, ver, "DWARF version number");
9139 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
9140 debug_abbrev_section,
9141 "Offset Into Abbrev. Section");
9142 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9144 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
9145 output_die (comp_unit);
9147 /* Build the skeleton debug_abbrev section. */
9148 switch_to_section (debug_skeleton_abbrev_section);
9149 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
9151 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
9153 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
9156 /* Output a comdat type unit DIE and its children. */
9158 static void
9159 output_comdat_type_unit (comdat_type_node *node)
9161 const char *secname;
9162 char *tmp;
9163 int i;
9164 #if defined (OBJECT_FORMAT_ELF)
9165 tree comdat_key;
9166 #endif
9168 /* First mark all the DIEs in this CU so we know which get local refs. */
9169 mark_dies (node->root_die);
9171 external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
9173 build_abbrev_table (node->root_die, extern_map);
9175 delete extern_map;
9176 extern_map = NULL;
9178 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
9179 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
9180 calc_die_sizes (node->root_die);
9182 #if defined (OBJECT_FORMAT_ELF)
9183 if (!dwarf_split_debug_info)
9184 secname = ".debug_types";
9185 else
9186 secname = ".debug_types.dwo";
9188 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9189 sprintf (tmp, "wt.");
9190 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9191 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
9192 comdat_key = get_identifier (tmp);
9193 targetm.asm_out.named_section (secname,
9194 SECTION_DEBUG | SECTION_LINKONCE,
9195 comdat_key);
9196 #else
9197 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9198 sprintf (tmp, ".gnu.linkonce.wt.");
9199 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9200 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
9201 secname = tmp;
9202 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9203 #endif
9205 /* Output debugging information. */
9206 output_compilation_unit_header ();
9207 output_signature (node->signature, "Type Signature");
9208 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
9209 "Offset to Type DIE");
9210 output_die (node->root_die);
9212 unmark_dies (node->root_die);
9215 /* Return the DWARF2/3 pubname associated with a decl. */
9217 static const char *
9218 dwarf2_name (tree decl, int scope)
9220 if (DECL_NAMELESS (decl))
9221 return NULL;
9222 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
9225 /* Add a new entry to .debug_pubnames if appropriate. */
9227 static void
9228 add_pubname_string (const char *str, dw_die_ref die)
9230 pubname_entry e;
9232 e.die = die;
9233 e.name = xstrdup (str);
9234 vec_safe_push (pubname_table, e);
9237 static void
9238 add_pubname (tree decl, dw_die_ref die)
9240 if (!want_pubnames ())
9241 return;
9243 /* Don't add items to the table when we expect that the consumer will have
9244 just read the enclosing die. For example, if the consumer is looking at a
9245 class_member, it will either be inside the class already, or will have just
9246 looked up the class to find the member. Either way, searching the class is
9247 faster than searching the index. */
9248 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
9249 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9251 const char *name = dwarf2_name (decl, 1);
9253 if (name)
9254 add_pubname_string (name, die);
9258 /* Add an enumerator to the pubnames section. */
9260 static void
9261 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
9263 pubname_entry e;
9265 gcc_assert (scope_name);
9266 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
9267 e.die = die;
9268 vec_safe_push (pubname_table, e);
9271 /* Add a new entry to .debug_pubtypes if appropriate. */
9273 static void
9274 add_pubtype (tree decl, dw_die_ref die)
9276 pubname_entry e;
9278 if (!want_pubnames ())
9279 return;
9281 if ((TREE_PUBLIC (decl)
9282 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9283 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
9285 tree scope = NULL;
9286 const char *scope_name = "";
9287 const char *sep = is_cxx () ? "::" : ".";
9288 const char *name;
9290 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
9291 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
9293 scope_name = lang_hooks.dwarf_name (scope, 1);
9294 if (scope_name != NULL && scope_name[0] != '\0')
9295 scope_name = concat (scope_name, sep, NULL);
9296 else
9297 scope_name = "";
9300 if (TYPE_P (decl))
9301 name = type_tag (decl);
9302 else
9303 name = lang_hooks.dwarf_name (decl, 1);
9305 /* If we don't have a name for the type, there's no point in adding
9306 it to the table. */
9307 if (name != NULL && name[0] != '\0')
9309 e.die = die;
9310 e.name = concat (scope_name, name, NULL);
9311 vec_safe_push (pubtype_table, e);
9314 /* Although it might be more consistent to add the pubinfo for the
9315 enumerators as their dies are created, they should only be added if the
9316 enum type meets the criteria above. So rather than re-check the parent
9317 enum type whenever an enumerator die is created, just output them all
9318 here. This isn't protected by the name conditional because anonymous
9319 enums don't have names. */
9320 if (die->die_tag == DW_TAG_enumeration_type)
9322 dw_die_ref c;
9324 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
9329 /* Output a single entry in the pubnames table. */
9331 static void
9332 output_pubname (dw_offset die_offset, pubname_entry *entry)
9334 dw_die_ref die = entry->die;
9335 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
9337 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
9339 if (debug_generate_pub_sections == 2)
9341 /* This logic follows gdb's method for determining the value of the flag
9342 byte. */
9343 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
9344 switch (die->die_tag)
9346 case DW_TAG_typedef:
9347 case DW_TAG_base_type:
9348 case DW_TAG_subrange_type:
9349 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9350 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9351 break;
9352 case DW_TAG_enumerator:
9353 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9354 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9355 if (!is_cxx () && !is_java ())
9356 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9357 break;
9358 case DW_TAG_subprogram:
9359 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9360 GDB_INDEX_SYMBOL_KIND_FUNCTION);
9361 if (!is_ada ())
9362 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9363 break;
9364 case DW_TAG_constant:
9365 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9366 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9367 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9368 break;
9369 case DW_TAG_variable:
9370 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9371 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9372 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9373 break;
9374 case DW_TAG_namespace:
9375 case DW_TAG_imported_declaration:
9376 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9377 break;
9378 case DW_TAG_class_type:
9379 case DW_TAG_interface_type:
9380 case DW_TAG_structure_type:
9381 case DW_TAG_union_type:
9382 case DW_TAG_enumeration_type:
9383 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9384 if (!is_cxx () && !is_java ())
9385 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9386 break;
9387 default:
9388 /* An unusual tag. Leave the flag-byte empty. */
9389 break;
9391 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
9392 "GDB-index flags");
9395 dw2_asm_output_nstring (entry->name, -1, "external name");
9399 /* Output the public names table used to speed up access to externally
9400 visible names; or the public types table used to find type definitions. */
9402 static void
9403 output_pubnames (vec<pubname_entry, va_gc> *names)
9405 unsigned i;
9406 unsigned long pubnames_length = size_of_pubnames (names);
9407 pubname_ref pub;
9409 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9410 dw2_asm_output_data (4, 0xffffffff,
9411 "Initial length escape value indicating 64-bit DWARF extension");
9412 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length, "Pub Info Length");
9414 /* Version number for pubnames/pubtypes is independent of dwarf version. */
9415 dw2_asm_output_data (2, 2, "DWARF Version");
9417 if (dwarf_split_debug_info)
9418 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9419 debug_skeleton_info_section,
9420 "Offset of Compilation Unit Info");
9421 else
9422 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9423 debug_info_section,
9424 "Offset of Compilation Unit Info");
9425 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
9426 "Compilation Unit Length");
9428 FOR_EACH_VEC_ELT (*names, i, pub)
9430 if (include_pubname_in_output (names, pub))
9432 dw_offset die_offset = pub->die->die_offset;
9434 /* We shouldn't see pubnames for DIEs outside of the main CU. */
9435 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
9436 gcc_assert (pub->die->die_mark);
9438 /* If we're putting types in their own .debug_types sections,
9439 the .debug_pubtypes table will still point to the compile
9440 unit (not the type unit), so we want to use the offset of
9441 the skeleton DIE (if there is one). */
9442 if (pub->die->comdat_type_p && names == pubtype_table)
9444 comdat_type_node_ref type_node = pub->die->die_id.die_type_node;
9446 if (type_node != NULL)
9447 die_offset = (type_node->skeleton_die != NULL
9448 ? type_node->skeleton_die->die_offset
9449 : comp_unit_die ()->die_offset);
9452 output_pubname (die_offset, pub);
9456 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
9459 /* Output public names and types tables if necessary. */
9461 static void
9462 output_pubtables (void)
9464 if (!want_pubnames () || !info_section_emitted)
9465 return;
9467 switch_to_section (debug_pubnames_section);
9468 output_pubnames (pubname_table);
9469 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
9470 It shouldn't hurt to emit it always, since pure DWARF2 consumers
9471 simply won't look for the section. */
9472 switch_to_section (debug_pubtypes_section);
9473 output_pubnames (pubtype_table);
9477 /* Output the information that goes into the .debug_aranges table.
9478 Namely, define the beginning and ending address range of the
9479 text section generated for this compilation unit. */
9481 static void
9482 output_aranges (unsigned long aranges_length)
9484 unsigned i;
9486 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9487 dw2_asm_output_data (4, 0xffffffff,
9488 "Initial length escape value indicating 64-bit DWARF extension");
9489 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
9490 "Length of Address Ranges Info");
9491 /* Version number for aranges is still 2, even up to DWARF5. */
9492 dw2_asm_output_data (2, 2, "DWARF Version");
9493 if (dwarf_split_debug_info)
9494 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9495 debug_skeleton_info_section,
9496 "Offset of Compilation Unit Info");
9497 else
9498 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9499 debug_info_section,
9500 "Offset of Compilation Unit Info");
9501 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
9502 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
9504 /* We need to align to twice the pointer size here. */
9505 if (DWARF_ARANGES_PAD_SIZE)
9507 /* Pad using a 2 byte words so that padding is correct for any
9508 pointer size. */
9509 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
9510 2 * DWARF2_ADDR_SIZE);
9511 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
9512 dw2_asm_output_data (2, 0, NULL);
9515 /* It is necessary not to output these entries if the sections were
9516 not used; if the sections were not used, the length will be 0 and
9517 the address may end up as 0 if the section is discarded by ld
9518 --gc-sections, leaving an invalid (0, 0) entry that can be
9519 confused with the terminator. */
9520 if (text_section_used)
9522 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
9523 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
9524 text_section_label, "Length");
9526 if (cold_text_section_used)
9528 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
9529 "Address");
9530 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
9531 cold_text_section_label, "Length");
9534 if (have_multiple_function_sections)
9536 unsigned fde_idx;
9537 dw_fde_ref fde;
9539 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9541 if (DECL_IGNORED_P (fde->decl))
9542 continue;
9543 if (!fde->in_std_section)
9545 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
9546 "Address");
9547 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
9548 fde->dw_fde_begin, "Length");
9550 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9552 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
9553 "Address");
9554 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
9555 fde->dw_fde_second_begin, "Length");
9560 /* Output the terminator words. */
9561 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9562 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9565 /* Add a new entry to .debug_ranges. Return the offset at which it
9566 was placed. */
9568 static unsigned int
9569 add_ranges_num (int num)
9571 unsigned int in_use = ranges_table_in_use;
9573 if (in_use == ranges_table_allocated)
9575 ranges_table_allocated += RANGES_TABLE_INCREMENT;
9576 ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
9577 ranges_table_allocated);
9578 memset (ranges_table + ranges_table_in_use, 0,
9579 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
9582 ranges_table[in_use].num = num;
9583 ranges_table_in_use = in_use + 1;
9585 return in_use * 2 * DWARF2_ADDR_SIZE;
9588 /* Add a new entry to .debug_ranges corresponding to a block, or a
9589 range terminator if BLOCK is NULL. */
9591 static unsigned int
9592 add_ranges (const_tree block)
9594 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
9597 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
9598 When using dwarf_split_debug_info, address attributes in dies destined
9599 for the final executable should be direct references--setting the
9600 parameter force_direct ensures this behavior. */
9602 static void
9603 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
9604 bool *added, bool force_direct)
9606 unsigned int in_use = ranges_by_label_in_use;
9607 unsigned int offset;
9609 if (in_use == ranges_by_label_allocated)
9611 ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
9612 ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
9613 ranges_by_label,
9614 ranges_by_label_allocated);
9615 memset (ranges_by_label + ranges_by_label_in_use, 0,
9616 RANGES_TABLE_INCREMENT
9617 * sizeof (struct dw_ranges_by_label_struct));
9620 ranges_by_label[in_use].begin = begin;
9621 ranges_by_label[in_use].end = end;
9622 ranges_by_label_in_use = in_use + 1;
9624 offset = add_ranges_num (-(int)in_use - 1);
9625 if (!*added)
9627 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
9628 *added = true;
9632 static void
9633 output_ranges (void)
9635 unsigned i;
9636 static const char *const start_fmt = "Offset %#x";
9637 const char *fmt = start_fmt;
9639 for (i = 0; i < ranges_table_in_use; i++)
9641 int block_num = ranges_table[i].num;
9643 if (block_num > 0)
9645 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
9646 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
9648 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
9649 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
9651 /* If all code is in the text section, then the compilation
9652 unit base address defaults to DW_AT_low_pc, which is the
9653 base of the text section. */
9654 if (!have_multiple_function_sections)
9656 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
9657 text_section_label,
9658 fmt, i * 2 * DWARF2_ADDR_SIZE);
9659 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
9660 text_section_label, NULL);
9663 /* Otherwise, the compilation unit base address is zero,
9664 which allows us to use absolute addresses, and not worry
9665 about whether the target supports cross-section
9666 arithmetic. */
9667 else
9669 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
9670 fmt, i * 2 * DWARF2_ADDR_SIZE);
9671 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
9674 fmt = NULL;
9677 /* Negative block_num stands for an index into ranges_by_label. */
9678 else if (block_num < 0)
9680 int lab_idx = - block_num - 1;
9682 if (!have_multiple_function_sections)
9684 gcc_unreachable ();
9685 #if 0
9686 /* If we ever use add_ranges_by_labels () for a single
9687 function section, all we have to do is to take out
9688 the #if 0 above. */
9689 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9690 ranges_by_label[lab_idx].begin,
9691 text_section_label,
9692 fmt, i * 2 * DWARF2_ADDR_SIZE);
9693 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9694 ranges_by_label[lab_idx].end,
9695 text_section_label, NULL);
9696 #endif
9698 else
9700 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9701 ranges_by_label[lab_idx].begin,
9702 fmt, i * 2 * DWARF2_ADDR_SIZE);
9703 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9704 ranges_by_label[lab_idx].end,
9705 NULL);
9708 else
9710 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9711 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9712 fmt = start_fmt;
9717 /* Data structure containing information about input files. */
9718 struct file_info
9720 const char *path; /* Complete file name. */
9721 const char *fname; /* File name part. */
9722 int length; /* Length of entire string. */
9723 struct dwarf_file_data * file_idx; /* Index in input file table. */
9724 int dir_idx; /* Index in directory table. */
9727 /* Data structure containing information about directories with source
9728 files. */
9729 struct dir_info
9731 const char *path; /* Path including directory name. */
9732 int length; /* Path length. */
9733 int prefix; /* Index of directory entry which is a prefix. */
9734 int count; /* Number of files in this directory. */
9735 int dir_idx; /* Index of directory used as base. */
9738 /* Callback function for file_info comparison. We sort by looking at
9739 the directories in the path. */
9741 static int
9742 file_info_cmp (const void *p1, const void *p2)
9744 const struct file_info *const s1 = (const struct file_info *) p1;
9745 const struct file_info *const s2 = (const struct file_info *) p2;
9746 const unsigned char *cp1;
9747 const unsigned char *cp2;
9749 /* Take care of file names without directories. We need to make sure that
9750 we return consistent values to qsort since some will get confused if
9751 we return the same value when identical operands are passed in opposite
9752 orders. So if neither has a directory, return 0 and otherwise return
9753 1 or -1 depending on which one has the directory. */
9754 if ((s1->path == s1->fname || s2->path == s2->fname))
9755 return (s2->path == s2->fname) - (s1->path == s1->fname);
9757 cp1 = (const unsigned char *) s1->path;
9758 cp2 = (const unsigned char *) s2->path;
9760 while (1)
9762 ++cp1;
9763 ++cp2;
9764 /* Reached the end of the first path? If so, handle like above. */
9765 if ((cp1 == (const unsigned char *) s1->fname)
9766 || (cp2 == (const unsigned char *) s2->fname))
9767 return ((cp2 == (const unsigned char *) s2->fname)
9768 - (cp1 == (const unsigned char *) s1->fname));
9770 /* Character of current path component the same? */
9771 else if (*cp1 != *cp2)
9772 return *cp1 - *cp2;
9776 struct file_name_acquire_data
9778 struct file_info *files;
9779 int used_files;
9780 int max_files;
9783 /* Traversal function for the hash table. */
9786 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
9788 struct dwarf_file_data *d = *slot;
9789 struct file_info *fi;
9790 const char *f;
9792 gcc_assert (fnad->max_files >= d->emitted_number);
9794 if (! d->emitted_number)
9795 return 1;
9797 gcc_assert (fnad->max_files != fnad->used_files);
9799 fi = fnad->files + fnad->used_files++;
9801 /* Skip all leading "./". */
9802 f = d->filename;
9803 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
9804 f += 2;
9806 /* Create a new array entry. */
9807 fi->path = f;
9808 fi->length = strlen (f);
9809 fi->file_idx = d;
9811 /* Search for the file name part. */
9812 f = strrchr (f, DIR_SEPARATOR);
9813 #if defined (DIR_SEPARATOR_2)
9815 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
9817 if (g != NULL)
9819 if (f == NULL || f < g)
9820 f = g;
9823 #endif
9825 fi->fname = f == NULL ? fi->path : f + 1;
9826 return 1;
9829 /* Output the directory table and the file name table. We try to minimize
9830 the total amount of memory needed. A heuristic is used to avoid large
9831 slowdowns with many input files. */
9833 static void
9834 output_file_names (void)
9836 struct file_name_acquire_data fnad;
9837 int numfiles;
9838 struct file_info *files;
9839 struct dir_info *dirs;
9840 int *saved;
9841 int *savehere;
9842 int *backmap;
9843 int ndirs;
9844 int idx_offset;
9845 int i;
9847 if (!last_emitted_file)
9849 dw2_asm_output_data (1, 0, "End directory table");
9850 dw2_asm_output_data (1, 0, "End file name table");
9851 return;
9854 numfiles = last_emitted_file->emitted_number;
9856 /* Allocate the various arrays we need. */
9857 files = XALLOCAVEC (struct file_info, numfiles);
9858 dirs = XALLOCAVEC (struct dir_info, numfiles);
9860 fnad.files = files;
9861 fnad.used_files = 0;
9862 fnad.max_files = numfiles;
9863 file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
9864 gcc_assert (fnad.used_files == fnad.max_files);
9866 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
9868 /* Find all the different directories used. */
9869 dirs[0].path = files[0].path;
9870 dirs[0].length = files[0].fname - files[0].path;
9871 dirs[0].prefix = -1;
9872 dirs[0].count = 1;
9873 dirs[0].dir_idx = 0;
9874 files[0].dir_idx = 0;
9875 ndirs = 1;
9877 for (i = 1; i < numfiles; i++)
9878 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
9879 && memcmp (dirs[ndirs - 1].path, files[i].path,
9880 dirs[ndirs - 1].length) == 0)
9882 /* Same directory as last entry. */
9883 files[i].dir_idx = ndirs - 1;
9884 ++dirs[ndirs - 1].count;
9886 else
9888 int j;
9890 /* This is a new directory. */
9891 dirs[ndirs].path = files[i].path;
9892 dirs[ndirs].length = files[i].fname - files[i].path;
9893 dirs[ndirs].count = 1;
9894 dirs[ndirs].dir_idx = ndirs;
9895 files[i].dir_idx = ndirs;
9897 /* Search for a prefix. */
9898 dirs[ndirs].prefix = -1;
9899 for (j = 0; j < ndirs; j++)
9900 if (dirs[j].length < dirs[ndirs].length
9901 && dirs[j].length > 1
9902 && (dirs[ndirs].prefix == -1
9903 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
9904 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
9905 dirs[ndirs].prefix = j;
9907 ++ndirs;
9910 /* Now to the actual work. We have to find a subset of the directories which
9911 allow expressing the file name using references to the directory table
9912 with the least amount of characters. We do not do an exhaustive search
9913 where we would have to check out every combination of every single
9914 possible prefix. Instead we use a heuristic which provides nearly optimal
9915 results in most cases and never is much off. */
9916 saved = XALLOCAVEC (int, ndirs);
9917 savehere = XALLOCAVEC (int, ndirs);
9919 memset (saved, '\0', ndirs * sizeof (saved[0]));
9920 for (i = 0; i < ndirs; i++)
9922 int j;
9923 int total;
9925 /* We can always save some space for the current directory. But this
9926 does not mean it will be enough to justify adding the directory. */
9927 savehere[i] = dirs[i].length;
9928 total = (savehere[i] - saved[i]) * dirs[i].count;
9930 for (j = i + 1; j < ndirs; j++)
9932 savehere[j] = 0;
9933 if (saved[j] < dirs[i].length)
9935 /* Determine whether the dirs[i] path is a prefix of the
9936 dirs[j] path. */
9937 int k;
9939 k = dirs[j].prefix;
9940 while (k != -1 && k != (int) i)
9941 k = dirs[k].prefix;
9943 if (k == (int) i)
9945 /* Yes it is. We can possibly save some memory by
9946 writing the filenames in dirs[j] relative to
9947 dirs[i]. */
9948 savehere[j] = dirs[i].length;
9949 total += (savehere[j] - saved[j]) * dirs[j].count;
9954 /* Check whether we can save enough to justify adding the dirs[i]
9955 directory. */
9956 if (total > dirs[i].length + 1)
9958 /* It's worthwhile adding. */
9959 for (j = i; j < ndirs; j++)
9960 if (savehere[j] > 0)
9962 /* Remember how much we saved for this directory so far. */
9963 saved[j] = savehere[j];
9965 /* Remember the prefix directory. */
9966 dirs[j].dir_idx = i;
9971 /* Emit the directory name table. */
9972 idx_offset = dirs[0].length > 0 ? 1 : 0;
9973 for (i = 1 - idx_offset; i < ndirs; i++)
9974 dw2_asm_output_nstring (dirs[i].path,
9975 dirs[i].length
9976 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
9977 "Directory Entry: %#x", i + idx_offset);
9979 dw2_asm_output_data (1, 0, "End directory table");
9981 /* We have to emit them in the order of emitted_number since that's
9982 used in the debug info generation. To do this efficiently we
9983 generate a back-mapping of the indices first. */
9984 backmap = XALLOCAVEC (int, numfiles);
9985 for (i = 0; i < numfiles; i++)
9986 backmap[files[i].file_idx->emitted_number - 1] = i;
9988 /* Now write all the file names. */
9989 for (i = 0; i < numfiles; i++)
9991 int file_idx = backmap[i];
9992 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
9994 #ifdef VMS_DEBUGGING_INFO
9995 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
9997 /* Setting these fields can lead to debugger miscomparisons,
9998 but VMS Debug requires them to be set correctly. */
10000 int ver;
10001 long long cdt;
10002 long siz;
10003 int maxfilelen = strlen (files[file_idx].path)
10004 + dirs[dir_idx].length
10005 + MAX_VMS_VERSION_LEN + 1;
10006 char *filebuf = XALLOCAVEC (char, maxfilelen);
10008 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
10009 snprintf (filebuf, maxfilelen, "%s;%d",
10010 files[file_idx].path + dirs[dir_idx].length, ver);
10012 dw2_asm_output_nstring
10013 (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
10015 /* Include directory index. */
10016 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10018 /* Modification time. */
10019 dw2_asm_output_data_uleb128
10020 ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
10021 ? cdt : 0,
10022 NULL);
10024 /* File length in bytes. */
10025 dw2_asm_output_data_uleb128
10026 ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
10027 ? siz : 0,
10028 NULL);
10029 #else
10030 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
10031 "File Entry: %#x", (unsigned) i + 1);
10033 /* Include directory index. */
10034 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10036 /* Modification time. */
10037 dw2_asm_output_data_uleb128 (0, NULL);
10039 /* File length in bytes. */
10040 dw2_asm_output_data_uleb128 (0, NULL);
10041 #endif /* VMS_DEBUGGING_INFO */
10044 dw2_asm_output_data (1, 0, "End file name table");
10048 /* Output one line number table into the .debug_line section. */
10050 static void
10051 output_one_line_info_table (dw_line_info_table *table)
10053 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
10054 unsigned int current_line = 1;
10055 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
10056 dw_line_info_entry *ent;
10057 size_t i;
10059 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
10061 switch (ent->opcode)
10063 case LI_set_address:
10064 /* ??? Unfortunately, we have little choice here currently, and
10065 must always use the most general form. GCC does not know the
10066 address delta itself, so we can't use DW_LNS_advance_pc. Many
10067 ports do have length attributes which will give an upper bound
10068 on the address range. We could perhaps use length attributes
10069 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
10070 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
10072 /* This can handle any delta. This takes
10073 4+DWARF2_ADDR_SIZE bytes. */
10074 dw2_asm_output_data (1, 0, "set address %s", line_label);
10075 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10076 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10077 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
10078 break;
10080 case LI_set_line:
10081 if (ent->val == current_line)
10083 /* We still need to start a new row, so output a copy insn. */
10084 dw2_asm_output_data (1, DW_LNS_copy,
10085 "copy line %u", current_line);
10087 else
10089 int line_offset = ent->val - current_line;
10090 int line_delta = line_offset - DWARF_LINE_BASE;
10092 current_line = ent->val;
10093 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
10095 /* This can handle deltas from -10 to 234, using the current
10096 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
10097 This takes 1 byte. */
10098 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
10099 "line %u", current_line);
10101 else
10103 /* This can handle any delta. This takes at least 4 bytes,
10104 depending on the value being encoded. */
10105 dw2_asm_output_data (1, DW_LNS_advance_line,
10106 "advance to line %u", current_line);
10107 dw2_asm_output_data_sleb128 (line_offset, NULL);
10108 dw2_asm_output_data (1, DW_LNS_copy, NULL);
10111 break;
10113 case LI_set_file:
10114 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
10115 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10116 break;
10118 case LI_set_column:
10119 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
10120 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10121 break;
10123 case LI_negate_stmt:
10124 current_is_stmt = !current_is_stmt;
10125 dw2_asm_output_data (1, DW_LNS_negate_stmt,
10126 "is_stmt %d", current_is_stmt);
10127 break;
10129 case LI_set_prologue_end:
10130 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
10131 "set prologue end");
10132 break;
10134 case LI_set_epilogue_begin:
10135 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
10136 "set epilogue begin");
10137 break;
10139 case LI_set_discriminator:
10140 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
10141 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
10142 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
10143 dw2_asm_output_data_uleb128 (ent->val, NULL);
10144 break;
10148 /* Emit debug info for the address of the end of the table. */
10149 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
10150 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10151 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10152 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
10154 dw2_asm_output_data (1, 0, "end sequence");
10155 dw2_asm_output_data_uleb128 (1, NULL);
10156 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
10159 /* Output the source line number correspondence information. This
10160 information goes into the .debug_line section. */
10162 static void
10163 output_line_info (bool prologue_only)
10165 char l1[20], l2[20], p1[20], p2[20];
10166 /* We don't support DWARFv5 line tables yet. */
10167 int ver = dwarf_version < 5 ? dwarf_version : 4;
10168 bool saw_one = false;
10169 int opc;
10171 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
10172 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
10173 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
10174 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
10176 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10177 dw2_asm_output_data (4, 0xffffffff,
10178 "Initial length escape value indicating 64-bit DWARF extension");
10179 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
10180 "Length of Source Line Info");
10181 ASM_OUTPUT_LABEL (asm_out_file, l1);
10183 dw2_asm_output_data (2, ver, "DWARF Version");
10184 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
10185 ASM_OUTPUT_LABEL (asm_out_file, p1);
10187 /* Define the architecture-dependent minimum instruction length (in bytes).
10188 In this implementation of DWARF, this field is used for information
10189 purposes only. Since GCC generates assembly language, we have no
10190 a priori knowledge of how many instruction bytes are generated for each
10191 source line, and therefore can use only the DW_LNE_set_address and
10192 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
10193 this as '1', which is "correct enough" for all architectures,
10194 and don't let the target override. */
10195 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
10197 if (ver >= 4)
10198 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
10199 "Maximum Operations Per Instruction");
10200 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
10201 "Default is_stmt_start flag");
10202 dw2_asm_output_data (1, DWARF_LINE_BASE,
10203 "Line Base Value (Special Opcodes)");
10204 dw2_asm_output_data (1, DWARF_LINE_RANGE,
10205 "Line Range Value (Special Opcodes)");
10206 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
10207 "Special Opcode Base");
10209 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
10211 int n_op_args;
10212 switch (opc)
10214 case DW_LNS_advance_pc:
10215 case DW_LNS_advance_line:
10216 case DW_LNS_set_file:
10217 case DW_LNS_set_column:
10218 case DW_LNS_fixed_advance_pc:
10219 case DW_LNS_set_isa:
10220 n_op_args = 1;
10221 break;
10222 default:
10223 n_op_args = 0;
10224 break;
10227 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
10228 opc, n_op_args);
10231 /* Write out the information about the files we use. */
10232 output_file_names ();
10233 ASM_OUTPUT_LABEL (asm_out_file, p2);
10234 if (prologue_only)
10236 /* Output the marker for the end of the line number info. */
10237 ASM_OUTPUT_LABEL (asm_out_file, l2);
10238 return;
10241 if (separate_line_info)
10243 dw_line_info_table *table;
10244 size_t i;
10246 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
10247 if (table->in_use)
10249 output_one_line_info_table (table);
10250 saw_one = true;
10253 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
10255 output_one_line_info_table (cold_text_section_line_info);
10256 saw_one = true;
10259 /* ??? Some Darwin linkers crash on a .debug_line section with no
10260 sequences. Further, merely a DW_LNE_end_sequence entry is not
10261 sufficient -- the address column must also be initialized.
10262 Make sure to output at least one set_address/end_sequence pair,
10263 choosing .text since that section is always present. */
10264 if (text_section_line_info->in_use || !saw_one)
10265 output_one_line_info_table (text_section_line_info);
10267 /* Output the marker for the end of the line number info. */
10268 ASM_OUTPUT_LABEL (asm_out_file, l2);
10271 /* Given a pointer to a tree node for some base type, return a pointer to
10272 a DIE that describes the given type.
10274 This routine must only be called for GCC type nodes that correspond to
10275 Dwarf base (fundamental) types. */
10277 static dw_die_ref
10278 base_type_die (tree type)
10280 dw_die_ref base_type_result;
10281 enum dwarf_type encoding;
10283 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
10284 return 0;
10286 /* If this is a subtype that should not be emitted as a subrange type,
10287 use the base type. See subrange_type_for_debug_p. */
10288 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
10289 type = TREE_TYPE (type);
10291 switch (TREE_CODE (type))
10293 case INTEGER_TYPE:
10294 if ((dwarf_version >= 4 || !dwarf_strict)
10295 && TYPE_NAME (type)
10296 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10297 && DECL_IS_BUILTIN (TYPE_NAME (type))
10298 && DECL_NAME (TYPE_NAME (type)))
10300 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
10301 if (strcmp (name, "char16_t") == 0
10302 || strcmp (name, "char32_t") == 0)
10304 encoding = DW_ATE_UTF;
10305 break;
10308 if (TYPE_STRING_FLAG (type))
10310 if (TYPE_UNSIGNED (type))
10311 encoding = DW_ATE_unsigned_char;
10312 else
10313 encoding = DW_ATE_signed_char;
10315 else if (TYPE_UNSIGNED (type))
10316 encoding = DW_ATE_unsigned;
10317 else
10318 encoding = DW_ATE_signed;
10319 break;
10321 case REAL_TYPE:
10322 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
10324 if (dwarf_version >= 3 || !dwarf_strict)
10325 encoding = DW_ATE_decimal_float;
10326 else
10327 encoding = DW_ATE_lo_user;
10329 else
10330 encoding = DW_ATE_float;
10331 break;
10333 case FIXED_POINT_TYPE:
10334 if (!(dwarf_version >= 3 || !dwarf_strict))
10335 encoding = DW_ATE_lo_user;
10336 else if (TYPE_UNSIGNED (type))
10337 encoding = DW_ATE_unsigned_fixed;
10338 else
10339 encoding = DW_ATE_signed_fixed;
10340 break;
10342 /* Dwarf2 doesn't know anything about complex ints, so use
10343 a user defined type for it. */
10344 case COMPLEX_TYPE:
10345 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
10346 encoding = DW_ATE_complex_float;
10347 else
10348 encoding = DW_ATE_lo_user;
10349 break;
10351 case BOOLEAN_TYPE:
10352 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
10353 encoding = DW_ATE_boolean;
10354 break;
10356 default:
10357 /* No other TREE_CODEs are Dwarf fundamental types. */
10358 gcc_unreachable ();
10361 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
10363 add_AT_unsigned (base_type_result, DW_AT_byte_size,
10364 int_size_in_bytes (type));
10365 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
10366 add_pubtype (type, base_type_result);
10368 return base_type_result;
10371 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
10372 named 'auto' in its type: return true for it, false otherwise. */
10374 static inline bool
10375 is_cxx_auto (tree type)
10377 if (is_cxx ())
10379 tree name = TYPE_IDENTIFIER (type);
10380 if (name == get_identifier ("auto")
10381 || name == get_identifier ("decltype(auto)"))
10382 return true;
10384 return false;
10387 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
10388 given input type is a Dwarf "fundamental" type. Otherwise return null. */
10390 static inline int
10391 is_base_type (tree type)
10393 switch (TREE_CODE (type))
10395 case ERROR_MARK:
10396 case VOID_TYPE:
10397 case INTEGER_TYPE:
10398 case REAL_TYPE:
10399 case FIXED_POINT_TYPE:
10400 case COMPLEX_TYPE:
10401 case BOOLEAN_TYPE:
10402 case POINTER_BOUNDS_TYPE:
10403 return 1;
10405 case ARRAY_TYPE:
10406 case RECORD_TYPE:
10407 case UNION_TYPE:
10408 case QUAL_UNION_TYPE:
10409 case ENUMERAL_TYPE:
10410 case FUNCTION_TYPE:
10411 case METHOD_TYPE:
10412 case POINTER_TYPE:
10413 case REFERENCE_TYPE:
10414 case NULLPTR_TYPE:
10415 case OFFSET_TYPE:
10416 case LANG_TYPE:
10417 case VECTOR_TYPE:
10418 return 0;
10420 default:
10421 if (is_cxx_auto (type))
10422 return 0;
10423 gcc_unreachable ();
10426 return 0;
10429 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
10430 node, return the size in bits for the type if it is a constant, or else
10431 return the alignment for the type if the type's size is not constant, or
10432 else return BITS_PER_WORD if the type actually turns out to be an
10433 ERROR_MARK node. */
10435 static inline unsigned HOST_WIDE_INT
10436 simple_type_size_in_bits (const_tree type)
10438 if (TREE_CODE (type) == ERROR_MARK)
10439 return BITS_PER_WORD;
10440 else if (TYPE_SIZE (type) == NULL_TREE)
10441 return 0;
10442 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
10443 return tree_to_uhwi (TYPE_SIZE (type));
10444 else
10445 return TYPE_ALIGN (type);
10448 /* Similarly, but return an offset_int instead of UHWI. */
10450 static inline offset_int
10451 offset_int_type_size_in_bits (const_tree type)
10453 if (TREE_CODE (type) == ERROR_MARK)
10454 return BITS_PER_WORD;
10455 else if (TYPE_SIZE (type) == NULL_TREE)
10456 return 0;
10457 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
10458 return wi::to_offset (TYPE_SIZE (type));
10459 else
10460 return TYPE_ALIGN (type);
10463 /* Given a pointer to a tree node for a subrange type, return a pointer
10464 to a DIE that describes the given type. */
10466 static dw_die_ref
10467 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
10469 dw_die_ref subrange_die;
10470 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
10472 if (context_die == NULL)
10473 context_die = comp_unit_die ();
10475 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
10477 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
10479 /* The size of the subrange type and its base type do not match,
10480 so we need to generate a size attribute for the subrange type. */
10481 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
10484 if (low)
10485 add_bound_info (subrange_die, DW_AT_lower_bound, low);
10486 if (high)
10487 add_bound_info (subrange_die, DW_AT_upper_bound, high);
10489 return subrange_die;
10492 /* Returns the (const and/or volatile) cv_qualifiers associated with
10493 the decl node. This will normally be augmented with the
10494 cv_qualifiers of the underlying type in add_type_attribute. */
10496 static int
10497 decl_quals (const_tree decl)
10499 return ((TREE_READONLY (decl)
10500 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
10501 | (TREE_THIS_VOLATILE (decl)
10502 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
10505 /* Determine the TYPE whose qualifiers match the largest strict subset
10506 of the given TYPE_QUALS, and return its qualifiers. Ignore all
10507 qualifiers outside QUAL_MASK. */
10509 static int
10510 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
10512 tree t;
10513 int best_rank = 0, best_qual = 0, max_rank;
10515 type_quals &= qual_mask;
10516 max_rank = popcount_hwi (type_quals) - 1;
10518 for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
10519 t = TYPE_NEXT_VARIANT (t))
10521 int q = TYPE_QUALS (t) & qual_mask;
10523 if ((q & type_quals) == q && q != type_quals
10524 && check_base_type (t, type))
10526 int rank = popcount_hwi (q);
10528 if (rank > best_rank)
10530 best_rank = rank;
10531 best_qual = q;
10536 return best_qual;
10539 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
10540 entry that chains various modifiers in front of the given type. */
10542 static dw_die_ref
10543 modified_type_die (tree type, int cv_quals, dw_die_ref context_die)
10545 enum tree_code code = TREE_CODE (type);
10546 dw_die_ref mod_type_die;
10547 dw_die_ref sub_die = NULL;
10548 tree item_type = NULL;
10549 tree qualified_type;
10550 tree name, low, high;
10551 dw_die_ref mod_scope;
10552 /* Only these cv-qualifiers are currently handled. */
10553 const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
10554 | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC);
10556 if (code == ERROR_MARK)
10557 return NULL;
10559 cv_quals &= cv_qual_mask;
10561 /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
10562 tag modifier (and not an attribute) old consumers won't be able
10563 to handle it. */
10564 if (dwarf_version < 3)
10565 cv_quals &= ~TYPE_QUAL_RESTRICT;
10567 /* Likewise for DW_TAG_atomic_type for DWARFv5. */
10568 if (dwarf_version < 5)
10569 cv_quals &= ~TYPE_QUAL_ATOMIC;
10571 /* See if we already have the appropriately qualified variant of
10572 this type. */
10573 qualified_type = get_qualified_type (type, cv_quals);
10575 if (qualified_type == sizetype
10576 && TYPE_NAME (qualified_type)
10577 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
10579 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
10581 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
10582 && TYPE_PRECISION (t)
10583 == TYPE_PRECISION (qualified_type)
10584 && TYPE_UNSIGNED (t)
10585 == TYPE_UNSIGNED (qualified_type));
10586 qualified_type = t;
10589 /* If we do, then we can just use its DIE, if it exists. */
10590 if (qualified_type)
10592 mod_type_die = lookup_type_die (qualified_type);
10593 if (mod_type_die)
10594 return mod_type_die;
10597 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
10599 /* Handle C typedef types. */
10600 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
10601 && !DECL_ARTIFICIAL (name))
10603 tree dtype = TREE_TYPE (name);
10605 if (qualified_type == dtype)
10607 /* For a named type, use the typedef. */
10608 gen_type_die (qualified_type, context_die);
10609 return lookup_type_die (qualified_type);
10611 else
10613 int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
10614 dquals &= cv_qual_mask;
10615 if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
10616 || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
10617 /* cv-unqualified version of named type. Just use
10618 the unnamed type to which it refers. */
10619 return modified_type_die (DECL_ORIGINAL_TYPE (name),
10620 cv_quals, context_die);
10621 /* Else cv-qualified version of named type; fall through. */
10625 mod_scope = scope_die_for (type, context_die);
10627 if (cv_quals)
10629 struct qual_info { int q; enum dwarf_tag t; };
10630 static const struct qual_info qual_info[] =
10632 { TYPE_QUAL_ATOMIC, DW_TAG_atomic_type },
10633 { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
10634 { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
10635 { TYPE_QUAL_CONST, DW_TAG_const_type },
10637 int sub_quals;
10638 unsigned i;
10640 /* Determine a lesser qualified type that most closely matches
10641 this one. Then generate DW_TAG_* entries for the remaining
10642 qualifiers. */
10643 sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
10644 cv_qual_mask);
10645 mod_type_die = modified_type_die (type, sub_quals, context_die);
10647 for (i = 0; i < sizeof (qual_info) / sizeof (qual_info[0]); i++)
10648 if (qual_info[i].q & cv_quals & ~sub_quals)
10650 dw_die_ref d = new_die (qual_info[i].t, mod_scope, type);
10651 if (mod_type_die)
10652 add_AT_die_ref (d, DW_AT_type, mod_type_die);
10653 mod_type_die = d;
10656 else if (code == POINTER_TYPE)
10658 mod_type_die = new_die (DW_TAG_pointer_type, mod_scope, type);
10659 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10660 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10661 item_type = TREE_TYPE (type);
10662 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10663 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10664 TYPE_ADDR_SPACE (item_type));
10666 else if (code == REFERENCE_TYPE)
10668 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
10669 mod_type_die = new_die (DW_TAG_rvalue_reference_type, mod_scope,
10670 type);
10671 else
10672 mod_type_die = new_die (DW_TAG_reference_type, mod_scope, type);
10673 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10674 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10675 item_type = TREE_TYPE (type);
10676 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10677 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10678 TYPE_ADDR_SPACE (item_type));
10680 else if (code == INTEGER_TYPE
10681 && TREE_TYPE (type) != NULL_TREE
10682 && subrange_type_for_debug_p (type, &low, &high))
10684 mod_type_die = subrange_type_die (type, low, high, context_die);
10685 item_type = TREE_TYPE (type);
10687 else if (is_base_type (type))
10688 mod_type_die = base_type_die (type);
10689 else
10691 gen_type_die (type, context_die);
10693 /* We have to get the type_main_variant here (and pass that to the
10694 `lookup_type_die' routine) because the ..._TYPE node we have
10695 might simply be a *copy* of some original type node (where the
10696 copy was created to help us keep track of typedef names) and
10697 that copy might have a different TYPE_UID from the original
10698 ..._TYPE node. */
10699 if (TREE_CODE (type) != VECTOR_TYPE)
10700 return lookup_type_die (type_main_variant (type));
10701 else
10702 /* Vectors have the debugging information in the type,
10703 not the main variant. */
10704 return lookup_type_die (type);
10707 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
10708 don't output a DW_TAG_typedef, since there isn't one in the
10709 user's program; just attach a DW_AT_name to the type.
10710 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
10711 if the base type already has the same name. */
10712 if (name
10713 && ((TREE_CODE (name) != TYPE_DECL
10714 && (qualified_type == TYPE_MAIN_VARIANT (type)
10715 || (cv_quals == TYPE_UNQUALIFIED)))
10716 || (TREE_CODE (name) == TYPE_DECL
10717 && TREE_TYPE (name) == qualified_type
10718 && DECL_NAME (name))))
10720 if (TREE_CODE (name) == TYPE_DECL)
10721 /* Could just call add_name_and_src_coords_attributes here,
10722 but since this is a builtin type it doesn't have any
10723 useful source coordinates anyway. */
10724 name = DECL_NAME (name);
10725 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
10727 /* This probably indicates a bug. */
10728 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
10730 name = TYPE_IDENTIFIER (type);
10731 add_name_attribute (mod_type_die,
10732 name ? IDENTIFIER_POINTER (name) : "__unknown__");
10735 if (qualified_type)
10736 equate_type_number_to_die (qualified_type, mod_type_die);
10738 if (item_type)
10739 /* We must do this after the equate_type_number_to_die call, in case
10740 this is a recursive type. This ensures that the modified_type_die
10741 recursion will terminate even if the type is recursive. Recursive
10742 types are possible in Ada. */
10743 sub_die = modified_type_die (item_type,
10744 TYPE_QUALS_NO_ADDR_SPACE (item_type),
10745 context_die);
10747 if (sub_die != NULL)
10748 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
10750 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
10751 if (TYPE_ARTIFICIAL (type))
10752 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
10754 return mod_type_die;
10757 /* Generate DIEs for the generic parameters of T.
10758 T must be either a generic type or a generic function.
10759 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
10761 static void
10762 gen_generic_params_dies (tree t)
10764 tree parms, args;
10765 int parms_num, i;
10766 dw_die_ref die = NULL;
10767 int non_default;
10769 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
10770 return;
10772 if (TYPE_P (t))
10773 die = lookup_type_die (t);
10774 else if (DECL_P (t))
10775 die = lookup_decl_die (t);
10777 gcc_assert (die);
10779 parms = lang_hooks.get_innermost_generic_parms (t);
10780 if (!parms)
10781 /* T has no generic parameter. It means T is neither a generic type
10782 or function. End of story. */
10783 return;
10785 parms_num = TREE_VEC_LENGTH (parms);
10786 args = lang_hooks.get_innermost_generic_args (t);
10787 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
10788 non_default = int_cst_value (TREE_CHAIN (args));
10789 else
10790 non_default = TREE_VEC_LENGTH (args);
10791 for (i = 0; i < parms_num; i++)
10793 tree parm, arg, arg_pack_elems;
10794 dw_die_ref parm_die;
10796 parm = TREE_VEC_ELT (parms, i);
10797 arg = TREE_VEC_ELT (args, i);
10798 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
10799 gcc_assert (parm && TREE_VALUE (parm) && arg);
10801 if (parm && TREE_VALUE (parm) && arg)
10803 /* If PARM represents a template parameter pack,
10804 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
10805 by DW_TAG_template_*_parameter DIEs for the argument
10806 pack elements of ARG. Note that ARG would then be
10807 an argument pack. */
10808 if (arg_pack_elems)
10809 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
10810 arg_pack_elems,
10811 die);
10812 else
10813 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
10814 true /* emit name */, die);
10815 if (i >= non_default)
10816 add_AT_flag (parm_die, DW_AT_default_value, 1);
10821 /* Create and return a DIE for PARM which should be
10822 the representation of a generic type parameter.
10823 For instance, in the C++ front end, PARM would be a template parameter.
10824 ARG is the argument to PARM.
10825 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
10826 name of the PARM.
10827 PARENT_DIE is the parent DIE which the new created DIE should be added to,
10828 as a child node. */
10830 static dw_die_ref
10831 generic_parameter_die (tree parm, tree arg,
10832 bool emit_name_p,
10833 dw_die_ref parent_die)
10835 dw_die_ref tmpl_die = NULL;
10836 const char *name = NULL;
10838 if (!parm || !DECL_NAME (parm) || !arg)
10839 return NULL;
10841 /* We support non-type generic parameters and arguments,
10842 type generic parameters and arguments, as well as
10843 generic generic parameters (a.k.a. template template parameters in C++)
10844 and arguments. */
10845 if (TREE_CODE (parm) == PARM_DECL)
10846 /* PARM is a nontype generic parameter */
10847 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
10848 else if (TREE_CODE (parm) == TYPE_DECL)
10849 /* PARM is a type generic parameter. */
10850 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
10851 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10852 /* PARM is a generic generic parameter.
10853 Its DIE is a GNU extension. It shall have a
10854 DW_AT_name attribute to represent the name of the template template
10855 parameter, and a DW_AT_GNU_template_name attribute to represent the
10856 name of the template template argument. */
10857 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
10858 parent_die, parm);
10859 else
10860 gcc_unreachable ();
10862 if (tmpl_die)
10864 tree tmpl_type;
10866 /* If PARM is a generic parameter pack, it means we are
10867 emitting debug info for a template argument pack element.
10868 In other terms, ARG is a template argument pack element.
10869 In that case, we don't emit any DW_AT_name attribute for
10870 the die. */
10871 if (emit_name_p)
10873 name = IDENTIFIER_POINTER (DECL_NAME (parm));
10874 gcc_assert (name);
10875 add_AT_string (tmpl_die, DW_AT_name, name);
10878 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10880 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
10881 TMPL_DIE should have a child DW_AT_type attribute that is set
10882 to the type of the argument to PARM, which is ARG.
10883 If PARM is a type generic parameter, TMPL_DIE should have a
10884 child DW_AT_type that is set to ARG. */
10885 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
10886 add_type_attribute (tmpl_die, tmpl_type,
10887 (TREE_THIS_VOLATILE (tmpl_type)
10888 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
10889 parent_die);
10891 else
10893 /* So TMPL_DIE is a DIE representing a
10894 a generic generic template parameter, a.k.a template template
10895 parameter in C++ and arg is a template. */
10897 /* The DW_AT_GNU_template_name attribute of the DIE must be set
10898 to the name of the argument. */
10899 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
10900 if (name)
10901 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
10904 if (TREE_CODE (parm) == PARM_DECL)
10905 /* So PARM is a non-type generic parameter.
10906 DWARF3 5.6.8 says we must set a DW_AT_const_value child
10907 attribute of TMPL_DIE which value represents the value
10908 of ARG.
10909 We must be careful here:
10910 The value of ARG might reference some function decls.
10911 We might currently be emitting debug info for a generic
10912 type and types are emitted before function decls, we don't
10913 know if the function decls referenced by ARG will actually be
10914 emitted after cgraph computations.
10915 So must defer the generation of the DW_AT_const_value to
10916 after cgraph is ready. */
10917 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
10920 return tmpl_die;
10923 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
10924 PARM_PACK must be a template parameter pack. The returned DIE
10925 will be child DIE of PARENT_DIE. */
10927 static dw_die_ref
10928 template_parameter_pack_die (tree parm_pack,
10929 tree parm_pack_args,
10930 dw_die_ref parent_die)
10932 dw_die_ref die;
10933 int j;
10935 gcc_assert (parent_die && parm_pack);
10937 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
10938 add_name_and_src_coords_attributes (die, parm_pack);
10939 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
10940 generic_parameter_die (parm_pack,
10941 TREE_VEC_ELT (parm_pack_args, j),
10942 false /* Don't emit DW_AT_name */,
10943 die);
10944 return die;
10947 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
10948 an enumerated type. */
10950 static inline int
10951 type_is_enum (const_tree type)
10953 return TREE_CODE (type) == ENUMERAL_TYPE;
10956 /* Return the DBX register number described by a given RTL node. */
10958 static unsigned int
10959 dbx_reg_number (const_rtx rtl)
10961 unsigned regno = REGNO (rtl);
10963 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
10965 #ifdef LEAF_REG_REMAP
10966 if (crtl->uses_only_leaf_regs)
10968 int leaf_reg = LEAF_REG_REMAP (regno);
10969 if (leaf_reg != -1)
10970 regno = (unsigned) leaf_reg;
10972 #endif
10974 regno = DBX_REGISTER_NUMBER (regno);
10975 gcc_assert (regno != INVALID_REGNUM);
10976 return regno;
10979 /* Optionally add a DW_OP_piece term to a location description expression.
10980 DW_OP_piece is only added if the location description expression already
10981 doesn't end with DW_OP_piece. */
10983 static void
10984 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
10986 dw_loc_descr_ref loc;
10988 if (*list_head != NULL)
10990 /* Find the end of the chain. */
10991 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
10994 if (loc->dw_loc_opc != DW_OP_piece)
10995 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
10999 /* Return a location descriptor that designates a machine register or
11000 zero if there is none. */
11002 static dw_loc_descr_ref
11003 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
11005 rtx regs;
11007 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
11008 return 0;
11010 /* We only use "frame base" when we're sure we're talking about the
11011 post-prologue local stack frame. We do this by *not* running
11012 register elimination until this point, and recognizing the special
11013 argument pointer and soft frame pointer rtx's.
11014 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
11015 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
11016 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
11018 dw_loc_descr_ref result = NULL;
11020 if (dwarf_version >= 4 || !dwarf_strict)
11022 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
11023 initialized);
11024 if (result)
11025 add_loc_descr (&result,
11026 new_loc_descr (DW_OP_stack_value, 0, 0));
11028 return result;
11031 regs = targetm.dwarf_register_span (rtl);
11033 if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
11034 return multiple_reg_loc_descriptor (rtl, regs, initialized);
11035 else
11037 unsigned int dbx_regnum = dbx_reg_number (rtl);
11038 if (dbx_regnum == IGNORED_DWARF_REGNUM)
11039 return 0;
11040 return one_reg_loc_descriptor (dbx_regnum, initialized);
11044 /* Return a location descriptor that designates a machine register for
11045 a given hard register number. */
11047 static dw_loc_descr_ref
11048 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
11050 dw_loc_descr_ref reg_loc_descr;
11052 if (regno <= 31)
11053 reg_loc_descr
11054 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
11055 else
11056 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
11058 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11059 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11061 return reg_loc_descr;
11064 /* Given an RTL of a register, return a location descriptor that
11065 designates a value that spans more than one register. */
11067 static dw_loc_descr_ref
11068 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
11069 enum var_init_status initialized)
11071 int size, i;
11072 dw_loc_descr_ref loc_result = NULL;
11074 /* Simple, contiguous registers. */
11075 if (regs == NULL_RTX)
11077 unsigned reg = REGNO (rtl);
11078 int nregs;
11080 #ifdef LEAF_REG_REMAP
11081 if (crtl->uses_only_leaf_regs)
11083 int leaf_reg = LEAF_REG_REMAP (reg);
11084 if (leaf_reg != -1)
11085 reg = (unsigned) leaf_reg;
11087 #endif
11089 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
11090 nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
11092 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
11094 loc_result = NULL;
11095 while (nregs--)
11097 dw_loc_descr_ref t;
11099 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
11100 VAR_INIT_STATUS_INITIALIZED);
11101 add_loc_descr (&loc_result, t);
11102 add_loc_descr_op_piece (&loc_result, size);
11103 ++reg;
11105 return loc_result;
11108 /* Now onto stupid register sets in non contiguous locations. */
11110 gcc_assert (GET_CODE (regs) == PARALLEL);
11112 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
11113 loc_result = NULL;
11115 for (i = 0; i < XVECLEN (regs, 0); ++i)
11117 dw_loc_descr_ref t;
11119 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
11120 VAR_INIT_STATUS_INITIALIZED);
11121 add_loc_descr (&loc_result, t);
11122 add_loc_descr_op_piece (&loc_result, size);
11125 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
11126 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11127 return loc_result;
11130 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
11132 /* Return a location descriptor that designates a constant i,
11133 as a compound operation from constant (i >> shift), constant shift
11134 and DW_OP_shl. */
11136 static dw_loc_descr_ref
11137 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11139 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
11140 add_loc_descr (&ret, int_loc_descriptor (shift));
11141 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11142 return ret;
11145 /* Return a location descriptor that designates a constant. */
11147 static dw_loc_descr_ref
11148 int_loc_descriptor (HOST_WIDE_INT i)
11150 enum dwarf_location_atom op;
11152 /* Pick the smallest representation of a constant, rather than just
11153 defaulting to the LEB encoding. */
11154 if (i >= 0)
11156 int clz = clz_hwi (i);
11157 int ctz = ctz_hwi (i);
11158 if (i <= 31)
11159 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
11160 else if (i <= 0xff)
11161 op = DW_OP_const1u;
11162 else if (i <= 0xffff)
11163 op = DW_OP_const2u;
11164 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11165 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11166 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
11167 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
11168 while DW_OP_const4u is 5 bytes. */
11169 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
11170 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11171 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11172 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
11173 while DW_OP_const4u is 5 bytes. */
11174 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11175 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11176 op = DW_OP_const4u;
11177 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11178 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11179 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
11180 while DW_OP_constu of constant >= 0x100000000 takes at least
11181 6 bytes. */
11182 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11183 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11184 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
11185 >= HOST_BITS_PER_WIDE_INT)
11186 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
11187 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
11188 while DW_OP_constu takes in this case at least 6 bytes. */
11189 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
11190 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11191 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11192 && size_of_uleb128 (i) > 6)
11193 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
11194 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
11195 else
11196 op = DW_OP_constu;
11198 else
11200 if (i >= -0x80)
11201 op = DW_OP_const1s;
11202 else if (i >= -0x8000)
11203 op = DW_OP_const2s;
11204 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11206 if (size_of_int_loc_descriptor (i) < 5)
11208 dw_loc_descr_ref ret = int_loc_descriptor (-i);
11209 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11210 return ret;
11212 op = DW_OP_const4s;
11214 else
11216 if (size_of_int_loc_descriptor (i)
11217 < (unsigned long) 1 + size_of_sleb128 (i))
11219 dw_loc_descr_ref ret = int_loc_descriptor (-i);
11220 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11221 return ret;
11223 op = DW_OP_consts;
11227 return new_loc_descr (op, i, 0);
11230 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
11231 without actually allocating it. */
11233 static unsigned long
11234 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11236 return size_of_int_loc_descriptor (i >> shift)
11237 + size_of_int_loc_descriptor (shift)
11238 + 1;
11241 /* Return size_of_locs (int_loc_descriptor (i)) without
11242 actually allocating it. */
11244 static unsigned long
11245 size_of_int_loc_descriptor (HOST_WIDE_INT i)
11247 unsigned long s;
11249 if (i >= 0)
11251 int clz, ctz;
11252 if (i <= 31)
11253 return 1;
11254 else if (i <= 0xff)
11255 return 2;
11256 else if (i <= 0xffff)
11257 return 3;
11258 clz = clz_hwi (i);
11259 ctz = ctz_hwi (i);
11260 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11261 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11262 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11263 - clz - 5);
11264 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11265 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11266 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11267 - clz - 8);
11268 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11269 return 5;
11270 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
11271 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11272 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11273 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11274 - clz - 8);
11275 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11276 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
11277 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11278 - clz - 16);
11279 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11280 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11281 && s > 6)
11282 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11283 - clz - 32);
11284 else
11285 return 1 + s;
11287 else
11289 if (i >= -0x80)
11290 return 2;
11291 else if (i >= -0x8000)
11292 return 3;
11293 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11295 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11297 s = size_of_int_loc_descriptor (-i) + 1;
11298 if (s < 5)
11299 return s;
11301 return 5;
11303 else
11305 unsigned long r = 1 + size_of_sleb128 (i);
11306 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11308 s = size_of_int_loc_descriptor (-i) + 1;
11309 if (s < r)
11310 return s;
11312 return r;
11317 /* Return loc description representing "address" of integer value.
11318 This can appear only as toplevel expression. */
11320 static dw_loc_descr_ref
11321 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
11323 int litsize;
11324 dw_loc_descr_ref loc_result = NULL;
11326 if (!(dwarf_version >= 4 || !dwarf_strict))
11327 return NULL;
11329 litsize = size_of_int_loc_descriptor (i);
11330 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
11331 is more compact. For DW_OP_stack_value we need:
11332 litsize + 1 (DW_OP_stack_value)
11333 and for DW_OP_implicit_value:
11334 1 (DW_OP_implicit_value) + 1 (length) + size. */
11335 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
11337 loc_result = int_loc_descriptor (i);
11338 add_loc_descr (&loc_result,
11339 new_loc_descr (DW_OP_stack_value, 0, 0));
11340 return loc_result;
11343 loc_result = new_loc_descr (DW_OP_implicit_value,
11344 size, 0);
11345 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
11346 loc_result->dw_loc_oprnd2.v.val_int = i;
11347 return loc_result;
11350 /* Return a location descriptor that designates a base+offset location. */
11352 static dw_loc_descr_ref
11353 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
11354 enum var_init_status initialized)
11356 unsigned int regno;
11357 dw_loc_descr_ref result;
11358 dw_fde_ref fde = cfun->fde;
11360 /* We only use "frame base" when we're sure we're talking about the
11361 post-prologue local stack frame. We do this by *not* running
11362 register elimination until this point, and recognizing the special
11363 argument pointer and soft frame pointer rtx's. */
11364 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
11366 rtx elim = (ira_use_lra_p
11367 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
11368 : eliminate_regs (reg, VOIDmode, NULL_RTX));
11370 if (elim != reg)
11372 if (GET_CODE (elim) == PLUS)
11374 offset += INTVAL (XEXP (elim, 1));
11375 elim = XEXP (elim, 0);
11377 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
11378 && (elim == hard_frame_pointer_rtx
11379 || elim == stack_pointer_rtx))
11380 || elim == (frame_pointer_needed
11381 ? hard_frame_pointer_rtx
11382 : stack_pointer_rtx));
11384 /* If drap register is used to align stack, use frame
11385 pointer + offset to access stack variables. If stack
11386 is aligned without drap, use stack pointer + offset to
11387 access stack variables. */
11388 if (crtl->stack_realign_tried
11389 && reg == frame_pointer_rtx)
11391 int base_reg
11392 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
11393 ? HARD_FRAME_POINTER_REGNUM
11394 : REGNO (elim));
11395 return new_reg_loc_descr (base_reg, offset);
11398 gcc_assert (frame_pointer_fb_offset_valid);
11399 offset += frame_pointer_fb_offset;
11400 return new_loc_descr (DW_OP_fbreg, offset, 0);
11404 regno = REGNO (reg);
11405 #ifdef LEAF_REG_REMAP
11406 if (crtl->uses_only_leaf_regs)
11408 int leaf_reg = LEAF_REG_REMAP (regno);
11409 if (leaf_reg != -1)
11410 regno = (unsigned) leaf_reg;
11412 #endif
11413 regno = DWARF_FRAME_REGNUM (regno);
11415 if (!optimize && fde
11416 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
11418 /* Use cfa+offset to represent the location of arguments passed
11419 on the stack when drap is used to align stack.
11420 Only do this when not optimizing, for optimized code var-tracking
11421 is supposed to track where the arguments live and the register
11422 used as vdrap or drap in some spot might be used for something
11423 else in other part of the routine. */
11424 return new_loc_descr (DW_OP_fbreg, offset, 0);
11427 if (regno <= 31)
11428 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
11429 offset, 0);
11430 else
11431 result = new_loc_descr (DW_OP_bregx, regno, offset);
11433 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11434 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11436 return result;
11439 /* Return true if this RTL expression describes a base+offset calculation. */
11441 static inline int
11442 is_based_loc (const_rtx rtl)
11444 return (GET_CODE (rtl) == PLUS
11445 && ((REG_P (XEXP (rtl, 0))
11446 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
11447 && CONST_INT_P (XEXP (rtl, 1)))));
11450 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
11451 failed. */
11453 static dw_loc_descr_ref
11454 tls_mem_loc_descriptor (rtx mem)
11456 tree base;
11457 dw_loc_descr_ref loc_result;
11459 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
11460 return NULL;
11462 base = get_base_address (MEM_EXPR (mem));
11463 if (base == NULL
11464 || TREE_CODE (base) != VAR_DECL
11465 || !DECL_THREAD_LOCAL_P (base))
11466 return NULL;
11468 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
11469 if (loc_result == NULL)
11470 return NULL;
11472 if (MEM_OFFSET (mem))
11473 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
11475 return loc_result;
11478 /* Output debug info about reason why we failed to expand expression as dwarf
11479 expression. */
11481 static void
11482 expansion_failed (tree expr, rtx rtl, char const *reason)
11484 if (dump_file && (dump_flags & TDF_DETAILS))
11486 fprintf (dump_file, "Failed to expand as dwarf: ");
11487 if (expr)
11488 print_generic_expr (dump_file, expr, dump_flags);
11489 if (rtl)
11491 fprintf (dump_file, "\n");
11492 print_rtl (dump_file, rtl);
11494 fprintf (dump_file, "\nReason: %s\n", reason);
11498 /* Helper function for const_ok_for_output. */
11500 static bool
11501 const_ok_for_output_1 (rtx rtl)
11503 if (GET_CODE (rtl) == UNSPEC)
11505 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
11506 we can't express it in the debug info. */
11507 #ifdef ENABLE_CHECKING
11508 /* Don't complain about TLS UNSPECs, those are just too hard to
11509 delegitimize. Note this could be a non-decl SYMBOL_REF such as
11510 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
11511 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
11512 if (XVECLEN (rtl, 0) == 0
11513 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
11514 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE)
11515 inform (current_function_decl
11516 ? DECL_SOURCE_LOCATION (current_function_decl)
11517 : UNKNOWN_LOCATION,
11518 #if NUM_UNSPEC_VALUES > 0
11519 "non-delegitimized UNSPEC %s (%d) found in variable location",
11520 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
11521 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
11522 XINT (rtl, 1));
11523 #else
11524 "non-delegitimized UNSPEC %d found in variable location",
11525 XINT (rtl, 1));
11526 #endif
11527 #endif
11528 expansion_failed (NULL_TREE, rtl,
11529 "UNSPEC hasn't been delegitimized.\n");
11530 return false;
11533 if (targetm.const_not_ok_for_debug_p (rtl))
11535 expansion_failed (NULL_TREE, rtl,
11536 "Expression rejected for debug by the backend.\n");
11537 return false;
11540 /* FIXME: Refer to PR60655. It is possible for simplification
11541 of rtl expressions in var tracking to produce such expressions.
11542 We should really identify / validate expressions
11543 enclosed in CONST that can be handled by assemblers on various
11544 targets and only handle legitimate cases here. */
11545 if (GET_CODE (rtl) != SYMBOL_REF)
11547 if (GET_CODE (rtl) == NOT)
11548 return false;
11549 return true;
11552 if (CONSTANT_POOL_ADDRESS_P (rtl))
11554 bool marked;
11555 get_pool_constant_mark (rtl, &marked);
11556 /* If all references to this pool constant were optimized away,
11557 it was not output and thus we can't represent it. */
11558 if (!marked)
11560 expansion_failed (NULL_TREE, rtl,
11561 "Constant was removed from constant pool.\n");
11562 return false;
11566 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11567 return false;
11569 /* Avoid references to external symbols in debug info, on several targets
11570 the linker might even refuse to link when linking a shared library,
11571 and in many other cases the relocations for .debug_info/.debug_loc are
11572 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
11573 to be defined within the same shared library or executable are fine. */
11574 if (SYMBOL_REF_EXTERNAL_P (rtl))
11576 tree decl = SYMBOL_REF_DECL (rtl);
11578 if (decl == NULL || !targetm.binds_local_p (decl))
11580 expansion_failed (NULL_TREE, rtl,
11581 "Symbol not defined in current TU.\n");
11582 return false;
11586 return true;
11589 /* Return true if constant RTL can be emitted in DW_OP_addr or
11590 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
11591 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
11593 static bool
11594 const_ok_for_output (rtx rtl)
11596 if (GET_CODE (rtl) == SYMBOL_REF)
11597 return const_ok_for_output_1 (rtl);
11599 if (GET_CODE (rtl) == CONST)
11601 subrtx_var_iterator::array_type array;
11602 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
11603 if (!const_ok_for_output_1 (*iter))
11604 return false;
11605 return true;
11608 return true;
11611 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
11612 if possible, NULL otherwise. */
11614 static dw_die_ref
11615 base_type_for_mode (machine_mode mode, bool unsignedp)
11617 dw_die_ref type_die;
11618 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11620 if (type == NULL)
11621 return NULL;
11622 switch (TREE_CODE (type))
11624 case INTEGER_TYPE:
11625 case REAL_TYPE:
11626 break;
11627 default:
11628 return NULL;
11630 type_die = lookup_type_die (type);
11631 if (!type_die)
11632 type_die = modified_type_die (type, TYPE_UNQUALIFIED, comp_unit_die ());
11633 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
11634 return NULL;
11635 return type_die;
11638 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
11639 type matching MODE, or, if MODE is narrower than or as wide as
11640 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
11641 possible. */
11643 static dw_loc_descr_ref
11644 convert_descriptor_to_mode (machine_mode mode, dw_loc_descr_ref op)
11646 machine_mode outer_mode = mode;
11647 dw_die_ref type_die;
11648 dw_loc_descr_ref cvt;
11650 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11652 add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
11653 return op;
11655 type_die = base_type_for_mode (outer_mode, 1);
11656 if (type_die == NULL)
11657 return NULL;
11658 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11659 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11660 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11661 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11662 add_loc_descr (&op, cvt);
11663 return op;
11666 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
11668 static dw_loc_descr_ref
11669 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
11670 dw_loc_descr_ref op1)
11672 dw_loc_descr_ref ret = op0;
11673 add_loc_descr (&ret, op1);
11674 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11675 if (STORE_FLAG_VALUE != 1)
11677 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
11678 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
11680 return ret;
11683 /* Return location descriptor for signed comparison OP RTL. */
11685 static dw_loc_descr_ref
11686 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11687 machine_mode mem_mode)
11689 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11690 dw_loc_descr_ref op0, op1;
11691 int shift;
11693 if (op_mode == VOIDmode)
11694 op_mode = GET_MODE (XEXP (rtl, 1));
11695 if (op_mode == VOIDmode)
11696 return NULL;
11698 if (dwarf_strict
11699 && (GET_MODE_CLASS (op_mode) != MODE_INT
11700 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
11701 return NULL;
11703 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11704 VAR_INIT_STATUS_INITIALIZED);
11705 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11706 VAR_INIT_STATUS_INITIALIZED);
11708 if (op0 == NULL || op1 == NULL)
11709 return NULL;
11711 if (GET_MODE_CLASS (op_mode) != MODE_INT
11712 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11713 return compare_loc_descriptor (op, op0, op1);
11715 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11717 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
11718 dw_loc_descr_ref cvt;
11720 if (type_die == NULL)
11721 return NULL;
11722 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11723 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11724 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11725 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11726 add_loc_descr (&op0, cvt);
11727 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11728 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11729 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11730 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11731 add_loc_descr (&op1, cvt);
11732 return compare_loc_descriptor (op, op0, op1);
11735 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
11736 /* For eq/ne, if the operands are known to be zero-extended,
11737 there is no need to do the fancy shifting up. */
11738 if (op == DW_OP_eq || op == DW_OP_ne)
11740 dw_loc_descr_ref last0, last1;
11741 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11743 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11745 /* deref_size zero extends, and for constants we can check
11746 whether they are zero extended or not. */
11747 if (((last0->dw_loc_opc == DW_OP_deref_size
11748 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11749 || (CONST_INT_P (XEXP (rtl, 0))
11750 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
11751 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
11752 && ((last1->dw_loc_opc == DW_OP_deref_size
11753 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11754 || (CONST_INT_P (XEXP (rtl, 1))
11755 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
11756 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
11757 return compare_loc_descriptor (op, op0, op1);
11759 /* EQ/NE comparison against constant in narrower type than
11760 DWARF2_ADDR_SIZE can be performed either as
11761 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
11762 DW_OP_{eq,ne}
11764 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
11765 DW_OP_{eq,ne}. Pick whatever is shorter. */
11766 if (CONST_INT_P (XEXP (rtl, 1))
11767 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
11768 && (size_of_int_loc_descriptor (shift) + 1
11769 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
11770 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
11771 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11772 & GET_MODE_MASK (op_mode))))
11774 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
11775 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11776 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11777 & GET_MODE_MASK (op_mode));
11778 return compare_loc_descriptor (op, op0, op1);
11781 add_loc_descr (&op0, int_loc_descriptor (shift));
11782 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11783 if (CONST_INT_P (XEXP (rtl, 1)))
11784 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
11785 else
11787 add_loc_descr (&op1, int_loc_descriptor (shift));
11788 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11790 return compare_loc_descriptor (op, op0, op1);
11793 /* Return location descriptor for unsigned comparison OP RTL. */
11795 static dw_loc_descr_ref
11796 ucompare_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;
11802 if (op_mode == VOIDmode)
11803 op_mode = GET_MODE (XEXP (rtl, 1));
11804 if (op_mode == VOIDmode)
11805 return NULL;
11806 if (GET_MODE_CLASS (op_mode) != MODE_INT)
11807 return NULL;
11809 if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11810 return NULL;
11812 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11813 VAR_INIT_STATUS_INITIALIZED);
11814 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11815 VAR_INIT_STATUS_INITIALIZED);
11817 if (op0 == NULL || op1 == NULL)
11818 return NULL;
11820 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
11822 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
11823 dw_loc_descr_ref last0, last1;
11824 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11826 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11828 if (CONST_INT_P (XEXP (rtl, 0)))
11829 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
11830 /* deref_size zero extends, so no need to mask it again. */
11831 else if (last0->dw_loc_opc != DW_OP_deref_size
11832 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11834 add_loc_descr (&op0, int_loc_descriptor (mask));
11835 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11837 if (CONST_INT_P (XEXP (rtl, 1)))
11838 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
11839 /* deref_size zero extends, so no need to mask it again. */
11840 else if (last1->dw_loc_opc != DW_OP_deref_size
11841 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11843 add_loc_descr (&op1, int_loc_descriptor (mask));
11844 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11847 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11849 HOST_WIDE_INT bias = 1;
11850 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11851 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11852 if (CONST_INT_P (XEXP (rtl, 1)))
11853 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
11854 + INTVAL (XEXP (rtl, 1)));
11855 else
11856 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
11857 bias, 0));
11859 return compare_loc_descriptor (op, op0, op1);
11862 /* Return location descriptor for {U,S}{MIN,MAX}. */
11864 static dw_loc_descr_ref
11865 minmax_loc_descriptor (rtx rtl, machine_mode mode,
11866 machine_mode mem_mode)
11868 enum dwarf_location_atom op;
11869 dw_loc_descr_ref op0, op1, ret;
11870 dw_loc_descr_ref bra_node, drop_node;
11872 if (dwarf_strict
11873 && (GET_MODE_CLASS (mode) != MODE_INT
11874 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
11875 return NULL;
11877 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11878 VAR_INIT_STATUS_INITIALIZED);
11879 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11880 VAR_INIT_STATUS_INITIALIZED);
11882 if (op0 == NULL || op1 == NULL)
11883 return NULL;
11885 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
11886 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
11887 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
11888 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
11890 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11892 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11893 add_loc_descr (&op0, int_loc_descriptor (mask));
11894 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11895 add_loc_descr (&op1, int_loc_descriptor (mask));
11896 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11898 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
11900 HOST_WIDE_INT bias = 1;
11901 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11902 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11903 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11906 else if (GET_MODE_CLASS (mode) == MODE_INT
11907 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11909 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
11910 add_loc_descr (&op0, int_loc_descriptor (shift));
11911 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11912 add_loc_descr (&op1, int_loc_descriptor (shift));
11913 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11915 else if (GET_MODE_CLASS (mode) == MODE_INT
11916 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11918 dw_die_ref type_die = base_type_for_mode (mode, 0);
11919 dw_loc_descr_ref cvt;
11920 if (type_die == NULL)
11921 return NULL;
11922 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11923 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11924 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11925 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11926 add_loc_descr (&op0, cvt);
11927 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11928 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11929 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11930 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11931 add_loc_descr (&op1, cvt);
11934 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
11935 op = DW_OP_lt;
11936 else
11937 op = DW_OP_gt;
11938 ret = op0;
11939 add_loc_descr (&ret, op1);
11940 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11941 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
11942 add_loc_descr (&ret, bra_node);
11943 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11944 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
11945 add_loc_descr (&ret, drop_node);
11946 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
11947 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
11948 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
11949 && GET_MODE_CLASS (mode) == MODE_INT
11950 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11951 ret = convert_descriptor_to_mode (mode, ret);
11952 return ret;
11955 /* Helper function for mem_loc_descriptor. Perform OP binary op,
11956 but after converting arguments to type_die, afterwards
11957 convert back to unsigned. */
11959 static dw_loc_descr_ref
11960 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
11961 machine_mode mode, machine_mode mem_mode)
11963 dw_loc_descr_ref cvt, op0, op1;
11965 if (type_die == NULL)
11966 return NULL;
11967 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11968 VAR_INIT_STATUS_INITIALIZED);
11969 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11970 VAR_INIT_STATUS_INITIALIZED);
11971 if (op0 == NULL || op1 == NULL)
11972 return NULL;
11973 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11974 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11975 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11976 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11977 add_loc_descr (&op0, cvt);
11978 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11979 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11980 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11981 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11982 add_loc_descr (&op1, cvt);
11983 add_loc_descr (&op0, op1);
11984 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
11985 return convert_descriptor_to_mode (mode, op0);
11988 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
11989 const0 is DW_OP_lit0 or corresponding typed constant,
11990 const1 is DW_OP_lit1 or corresponding typed constant
11991 and constMSB is constant with just the MSB bit set
11992 for the mode):
11993 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11994 L1: const0 DW_OP_swap
11995 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
11996 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11997 L3: DW_OP_drop
11998 L4: DW_OP_nop
12000 CTZ is similar:
12001 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
12002 L1: const0 DW_OP_swap
12003 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
12004 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12005 L3: DW_OP_drop
12006 L4: DW_OP_nop
12008 FFS is similar:
12009 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
12010 L1: const1 DW_OP_swap
12011 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
12012 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12013 L3: DW_OP_drop
12014 L4: DW_OP_nop */
12016 static dw_loc_descr_ref
12017 clz_loc_descriptor (rtx rtl, machine_mode mode,
12018 machine_mode mem_mode)
12020 dw_loc_descr_ref op0, ret, tmp;
12021 HOST_WIDE_INT valv;
12022 dw_loc_descr_ref l1jump, l1label;
12023 dw_loc_descr_ref l2jump, l2label;
12024 dw_loc_descr_ref l3jump, l3label;
12025 dw_loc_descr_ref l4jump, l4label;
12026 rtx msb;
12028 if (GET_MODE_CLASS (mode) != MODE_INT
12029 || GET_MODE (XEXP (rtl, 0)) != mode)
12030 return NULL;
12032 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12033 VAR_INIT_STATUS_INITIALIZED);
12034 if (op0 == NULL)
12035 return NULL;
12036 ret = op0;
12037 if (GET_CODE (rtl) == CLZ)
12039 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12040 valv = GET_MODE_BITSIZE (mode);
12042 else if (GET_CODE (rtl) == FFS)
12043 valv = 0;
12044 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12045 valv = GET_MODE_BITSIZE (mode);
12046 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12047 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
12048 add_loc_descr (&ret, l1jump);
12049 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12050 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
12051 VAR_INIT_STATUS_INITIALIZED);
12052 if (tmp == NULL)
12053 return NULL;
12054 add_loc_descr (&ret, tmp);
12055 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
12056 add_loc_descr (&ret, l4jump);
12057 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
12058 ? const1_rtx : const0_rtx,
12059 mode, mem_mode,
12060 VAR_INIT_STATUS_INITIALIZED);
12061 if (l1label == NULL)
12062 return NULL;
12063 add_loc_descr (&ret, l1label);
12064 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12065 l2label = new_loc_descr (DW_OP_dup, 0, 0);
12066 add_loc_descr (&ret, l2label);
12067 if (GET_CODE (rtl) != CLZ)
12068 msb = const1_rtx;
12069 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
12070 msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
12071 << (GET_MODE_BITSIZE (mode) - 1));
12072 else
12073 msb = immed_wide_int_const
12074 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
12075 GET_MODE_PRECISION (mode)), mode);
12076 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
12077 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12078 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
12079 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
12080 else
12081 tmp = mem_loc_descriptor (msb, mode, mem_mode,
12082 VAR_INIT_STATUS_INITIALIZED);
12083 if (tmp == NULL)
12084 return NULL;
12085 add_loc_descr (&ret, tmp);
12086 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12087 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
12088 add_loc_descr (&ret, l3jump);
12089 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12090 VAR_INIT_STATUS_INITIALIZED);
12091 if (tmp == NULL)
12092 return NULL;
12093 add_loc_descr (&ret, tmp);
12094 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
12095 ? DW_OP_shl : DW_OP_shr, 0, 0));
12096 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12097 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
12098 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12099 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
12100 add_loc_descr (&ret, l2jump);
12101 l3label = new_loc_descr (DW_OP_drop, 0, 0);
12102 add_loc_descr (&ret, l3label);
12103 l4label = new_loc_descr (DW_OP_nop, 0, 0);
12104 add_loc_descr (&ret, l4label);
12105 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12106 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12107 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12108 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12109 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12110 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
12111 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12112 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
12113 return ret;
12116 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
12117 const1 is DW_OP_lit1 or corresponding typed constant):
12118 const0 DW_OP_swap
12119 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
12120 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
12121 L2: DW_OP_drop
12123 PARITY is similar:
12124 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
12125 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
12126 L2: DW_OP_drop */
12128 static dw_loc_descr_ref
12129 popcount_loc_descriptor (rtx rtl, machine_mode mode,
12130 machine_mode mem_mode)
12132 dw_loc_descr_ref op0, ret, tmp;
12133 dw_loc_descr_ref l1jump, l1label;
12134 dw_loc_descr_ref l2jump, l2label;
12136 if (GET_MODE_CLASS (mode) != MODE_INT
12137 || GET_MODE (XEXP (rtl, 0)) != mode)
12138 return NULL;
12140 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12141 VAR_INIT_STATUS_INITIALIZED);
12142 if (op0 == NULL)
12143 return NULL;
12144 ret = op0;
12145 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12146 VAR_INIT_STATUS_INITIALIZED);
12147 if (tmp == NULL)
12148 return NULL;
12149 add_loc_descr (&ret, tmp);
12150 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12151 l1label = new_loc_descr (DW_OP_dup, 0, 0);
12152 add_loc_descr (&ret, l1label);
12153 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12154 add_loc_descr (&ret, l2jump);
12155 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12156 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12157 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12158 VAR_INIT_STATUS_INITIALIZED);
12159 if (tmp == NULL)
12160 return NULL;
12161 add_loc_descr (&ret, tmp);
12162 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12163 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
12164 ? DW_OP_plus : DW_OP_xor, 0, 0));
12165 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12166 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12167 VAR_INIT_STATUS_INITIALIZED);
12168 add_loc_descr (&ret, tmp);
12169 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12170 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12171 add_loc_descr (&ret, l1jump);
12172 l2label = new_loc_descr (DW_OP_drop, 0, 0);
12173 add_loc_descr (&ret, l2label);
12174 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12175 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12176 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12177 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12178 return ret;
12181 /* BSWAP (constS is initial shift count, either 56 or 24):
12182 constS const0
12183 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
12184 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
12185 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
12186 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
12187 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
12189 static dw_loc_descr_ref
12190 bswap_loc_descriptor (rtx rtl, machine_mode mode,
12191 machine_mode mem_mode)
12193 dw_loc_descr_ref op0, ret, tmp;
12194 dw_loc_descr_ref l1jump, l1label;
12195 dw_loc_descr_ref l2jump, l2label;
12197 if (GET_MODE_CLASS (mode) != MODE_INT
12198 || BITS_PER_UNIT != 8
12199 || (GET_MODE_BITSIZE (mode) != 32
12200 && GET_MODE_BITSIZE (mode) != 64))
12201 return NULL;
12203 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12204 VAR_INIT_STATUS_INITIALIZED);
12205 if (op0 == NULL)
12206 return NULL;
12208 ret = op0;
12209 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12210 mode, mem_mode,
12211 VAR_INIT_STATUS_INITIALIZED);
12212 if (tmp == NULL)
12213 return NULL;
12214 add_loc_descr (&ret, tmp);
12215 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12216 VAR_INIT_STATUS_INITIALIZED);
12217 if (tmp == NULL)
12218 return NULL;
12219 add_loc_descr (&ret, tmp);
12220 l1label = new_loc_descr (DW_OP_pick, 2, 0);
12221 add_loc_descr (&ret, l1label);
12222 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12223 mode, mem_mode,
12224 VAR_INIT_STATUS_INITIALIZED);
12225 add_loc_descr (&ret, tmp);
12226 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
12227 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12228 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12229 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
12230 VAR_INIT_STATUS_INITIALIZED);
12231 if (tmp == NULL)
12232 return NULL;
12233 add_loc_descr (&ret, tmp);
12234 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12235 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
12236 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12237 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12238 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12239 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12240 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12241 VAR_INIT_STATUS_INITIALIZED);
12242 add_loc_descr (&ret, tmp);
12243 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
12244 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12245 add_loc_descr (&ret, l2jump);
12246 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
12247 VAR_INIT_STATUS_INITIALIZED);
12248 add_loc_descr (&ret, tmp);
12249 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12250 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12251 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12252 add_loc_descr (&ret, l1jump);
12253 l2label = new_loc_descr (DW_OP_drop, 0, 0);
12254 add_loc_descr (&ret, l2label);
12255 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12256 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12257 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12258 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12259 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12260 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12261 return ret;
12264 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
12265 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12266 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
12267 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
12269 ROTATERT is similar:
12270 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
12271 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12272 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
12274 static dw_loc_descr_ref
12275 rotate_loc_descriptor (rtx rtl, machine_mode mode,
12276 machine_mode mem_mode)
12278 rtx rtlop1 = XEXP (rtl, 1);
12279 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
12280 int i;
12282 if (GET_MODE_CLASS (mode) != MODE_INT)
12283 return NULL;
12285 if (GET_MODE (rtlop1) != VOIDmode
12286 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
12287 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12288 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12289 VAR_INIT_STATUS_INITIALIZED);
12290 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12291 VAR_INIT_STATUS_INITIALIZED);
12292 if (op0 == NULL || op1 == NULL)
12293 return NULL;
12294 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12295 for (i = 0; i < 2; i++)
12297 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
12298 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
12299 mode, mem_mode,
12300 VAR_INIT_STATUS_INITIALIZED);
12301 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12302 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12303 ? DW_OP_const4u
12304 : HOST_BITS_PER_WIDE_INT == 64
12305 ? DW_OP_const8u : DW_OP_constu,
12306 GET_MODE_MASK (mode), 0);
12307 else
12308 mask[i] = NULL;
12309 if (mask[i] == NULL)
12310 return NULL;
12311 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
12313 ret = op0;
12314 add_loc_descr (&ret, op1);
12315 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12316 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12317 if (GET_CODE (rtl) == ROTATERT)
12319 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12320 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12321 GET_MODE_BITSIZE (mode), 0));
12323 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12324 if (mask[0] != NULL)
12325 add_loc_descr (&ret, mask[0]);
12326 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12327 if (mask[1] != NULL)
12329 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12330 add_loc_descr (&ret, mask[1]);
12331 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12333 if (GET_CODE (rtl) == ROTATE)
12335 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12336 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12337 GET_MODE_BITSIZE (mode), 0));
12339 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12340 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12341 return ret;
12344 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
12345 for DEBUG_PARAMETER_REF RTL. */
12347 static dw_loc_descr_ref
12348 parameter_ref_descriptor (rtx rtl)
12350 dw_loc_descr_ref ret;
12351 dw_die_ref ref;
12353 if (dwarf_strict)
12354 return NULL;
12355 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
12356 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
12357 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
12358 if (ref)
12360 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12361 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
12362 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
12364 else
12366 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
12367 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
12369 return ret;
12372 /* The following routine converts the RTL for a variable or parameter
12373 (resident in memory) into an equivalent Dwarf representation of a
12374 mechanism for getting the address of that same variable onto the top of a
12375 hypothetical "address evaluation" stack.
12377 When creating memory location descriptors, we are effectively transforming
12378 the RTL for a memory-resident object into its Dwarf postfix expression
12379 equivalent. This routine recursively descends an RTL tree, turning
12380 it into Dwarf postfix code as it goes.
12382 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
12384 MEM_MODE is the mode of the memory reference, needed to handle some
12385 autoincrement addressing modes.
12387 Return 0 if we can't represent the location. */
12389 dw_loc_descr_ref
12390 mem_loc_descriptor (rtx rtl, machine_mode mode,
12391 machine_mode mem_mode,
12392 enum var_init_status initialized)
12394 dw_loc_descr_ref mem_loc_result = NULL;
12395 enum dwarf_location_atom op;
12396 dw_loc_descr_ref op0, op1;
12397 rtx inner = NULL_RTX;
12399 if (mode == VOIDmode)
12400 mode = GET_MODE (rtl);
12402 /* Note that for a dynamically sized array, the location we will generate a
12403 description of here will be the lowest numbered location which is
12404 actually within the array. That's *not* necessarily the same as the
12405 zeroth element of the array. */
12407 rtl = targetm.delegitimize_address (rtl);
12409 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
12410 return NULL;
12412 switch (GET_CODE (rtl))
12414 case POST_INC:
12415 case POST_DEC:
12416 case POST_MODIFY:
12417 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
12419 case SUBREG:
12420 /* The case of a subreg may arise when we have a local (register)
12421 variable or a formal (register) parameter which doesn't quite fill
12422 up an entire register. For now, just assume that it is
12423 legitimate to make the Dwarf info refer to the whole register which
12424 contains the given subreg. */
12425 if (!subreg_lowpart_p (rtl))
12426 break;
12427 inner = SUBREG_REG (rtl);
12428 case TRUNCATE:
12429 if (inner == NULL_RTX)
12430 inner = XEXP (rtl, 0);
12431 if (GET_MODE_CLASS (mode) == MODE_INT
12432 && GET_MODE_CLASS (GET_MODE (inner)) == MODE_INT
12433 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12434 #ifdef POINTERS_EXTEND_UNSIGNED
12435 || (mode == Pmode && mem_mode != VOIDmode)
12436 #endif
12438 && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
12440 mem_loc_result = mem_loc_descriptor (inner,
12441 GET_MODE (inner),
12442 mem_mode, initialized);
12443 break;
12445 if (dwarf_strict)
12446 break;
12447 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
12448 break;
12449 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
12450 && (GET_MODE_CLASS (mode) != MODE_INT
12451 || GET_MODE_CLASS (GET_MODE (inner)) != MODE_INT))
12452 break;
12453 else
12455 dw_die_ref type_die;
12456 dw_loc_descr_ref cvt;
12458 mem_loc_result = mem_loc_descriptor (inner,
12459 GET_MODE (inner),
12460 mem_mode, initialized);
12461 if (mem_loc_result == NULL)
12462 break;
12463 type_die = base_type_for_mode (mode,
12464 GET_MODE_CLASS (mode) == MODE_INT);
12465 if (type_die == NULL)
12467 mem_loc_result = NULL;
12468 break;
12470 if (GET_MODE_SIZE (mode)
12471 != GET_MODE_SIZE (GET_MODE (inner)))
12472 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12473 else
12474 cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
12475 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12476 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12477 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12478 add_loc_descr (&mem_loc_result, cvt);
12480 break;
12482 case REG:
12483 if (GET_MODE_CLASS (mode) != MODE_INT
12484 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12485 && rtl != arg_pointer_rtx
12486 && rtl != frame_pointer_rtx
12487 #ifdef POINTERS_EXTEND_UNSIGNED
12488 && (mode != Pmode || mem_mode == VOIDmode)
12489 #endif
12492 dw_die_ref type_die;
12493 unsigned int dbx_regnum;
12495 if (dwarf_strict)
12496 break;
12497 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
12498 break;
12499 type_die = base_type_for_mode (mode,
12500 GET_MODE_CLASS (mode) == MODE_INT);
12501 if (type_die == NULL)
12502 break;
12504 dbx_regnum = dbx_reg_number (rtl);
12505 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12506 break;
12507 mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
12508 dbx_regnum, 0);
12509 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12510 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12511 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
12512 break;
12514 /* Whenever a register number forms a part of the description of the
12515 method for calculating the (dynamic) address of a memory resident
12516 object, DWARF rules require the register number be referred to as
12517 a "base register". This distinction is not based in any way upon
12518 what category of register the hardware believes the given register
12519 belongs to. This is strictly DWARF terminology we're dealing with
12520 here. Note that in cases where the location of a memory-resident
12521 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
12522 OP_CONST (0)) the actual DWARF location descriptor that we generate
12523 may just be OP_BASEREG (basereg). This may look deceptively like
12524 the object in question was allocated to a register (rather than in
12525 memory) so DWARF consumers need to be aware of the subtle
12526 distinction between OP_REG and OP_BASEREG. */
12527 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
12528 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
12529 else if (stack_realign_drap
12530 && crtl->drap_reg
12531 && crtl->args.internal_arg_pointer == rtl
12532 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
12534 /* If RTL is internal_arg_pointer, which has been optimized
12535 out, use DRAP instead. */
12536 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
12537 VAR_INIT_STATUS_INITIALIZED);
12539 break;
12541 case SIGN_EXTEND:
12542 case ZERO_EXTEND:
12543 if (GET_MODE_CLASS (mode) != MODE_INT)
12544 break;
12545 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12546 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12547 if (op0 == 0)
12548 break;
12549 else if (GET_CODE (rtl) == ZERO_EXTEND
12550 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12551 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12552 < HOST_BITS_PER_WIDE_INT
12553 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
12554 to expand zero extend as two shifts instead of
12555 masking. */
12556 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
12558 machine_mode imode = GET_MODE (XEXP (rtl, 0));
12559 mem_loc_result = op0;
12560 add_loc_descr (&mem_loc_result,
12561 int_loc_descriptor (GET_MODE_MASK (imode)));
12562 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
12564 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12566 int shift = DWARF2_ADDR_SIZE
12567 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
12568 shift *= BITS_PER_UNIT;
12569 if (GET_CODE (rtl) == SIGN_EXTEND)
12570 op = DW_OP_shra;
12571 else
12572 op = DW_OP_shr;
12573 mem_loc_result = op0;
12574 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12575 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12576 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12577 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12579 else if (!dwarf_strict)
12581 dw_die_ref type_die1, type_die2;
12582 dw_loc_descr_ref cvt;
12584 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12585 GET_CODE (rtl) == ZERO_EXTEND);
12586 if (type_die1 == NULL)
12587 break;
12588 type_die2 = base_type_for_mode (mode, 1);
12589 if (type_die2 == NULL)
12590 break;
12591 mem_loc_result = op0;
12592 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12593 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12594 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
12595 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12596 add_loc_descr (&mem_loc_result, cvt);
12597 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12598 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12599 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
12600 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12601 add_loc_descr (&mem_loc_result, cvt);
12603 break;
12605 case MEM:
12607 rtx new_rtl = avoid_constant_pool_reference (rtl);
12608 if (new_rtl != rtl)
12610 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
12611 initialized);
12612 if (mem_loc_result != NULL)
12613 return mem_loc_result;
12616 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
12617 get_address_mode (rtl), mode,
12618 VAR_INIT_STATUS_INITIALIZED);
12619 if (mem_loc_result == NULL)
12620 mem_loc_result = tls_mem_loc_descriptor (rtl);
12621 if (mem_loc_result != NULL)
12623 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12624 || GET_MODE_CLASS (mode) != MODE_INT)
12626 dw_die_ref type_die;
12627 dw_loc_descr_ref deref;
12629 if (dwarf_strict)
12630 return NULL;
12631 type_die
12632 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
12633 if (type_die == NULL)
12634 return NULL;
12635 deref = new_loc_descr (DW_OP_GNU_deref_type,
12636 GET_MODE_SIZE (mode), 0);
12637 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12638 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12639 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
12640 add_loc_descr (&mem_loc_result, deref);
12642 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12643 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
12644 else
12645 add_loc_descr (&mem_loc_result,
12646 new_loc_descr (DW_OP_deref_size,
12647 GET_MODE_SIZE (mode), 0));
12649 break;
12651 case LO_SUM:
12652 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
12654 case LABEL_REF:
12655 /* Some ports can transform a symbol ref into a label ref, because
12656 the symbol ref is too far away and has to be dumped into a constant
12657 pool. */
12658 case CONST:
12659 case SYMBOL_REF:
12660 if ((GET_MODE_CLASS (mode) != MODE_INT
12661 && GET_MODE_CLASS (mode) != MODE_PARTIAL_INT)
12662 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12663 #ifdef POINTERS_EXTEND_UNSIGNED
12664 && (mode != Pmode || mem_mode == VOIDmode)
12665 #endif
12667 break;
12668 if (GET_CODE (rtl) == SYMBOL_REF
12669 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12671 dw_loc_descr_ref temp;
12673 /* If this is not defined, we have no way to emit the data. */
12674 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
12675 break;
12677 temp = new_addr_loc_descr (rtl, dtprel_true);
12679 mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
12680 add_loc_descr (&mem_loc_result, temp);
12682 break;
12685 if (!const_ok_for_output (rtl))
12686 break;
12688 symref:
12689 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
12690 vec_safe_push (used_rtx_array, rtl);
12691 break;
12693 case CONCAT:
12694 case CONCATN:
12695 case VAR_LOCATION:
12696 case DEBUG_IMPLICIT_PTR:
12697 expansion_failed (NULL_TREE, rtl,
12698 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
12699 return 0;
12701 case ENTRY_VALUE:
12702 if (dwarf_strict)
12703 return NULL;
12704 if (REG_P (ENTRY_VALUE_EXP (rtl)))
12706 if (GET_MODE_CLASS (mode) != MODE_INT
12707 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12708 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12709 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12710 else
12712 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
12713 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12714 return NULL;
12715 op0 = one_reg_loc_descriptor (dbx_regnum,
12716 VAR_INIT_STATUS_INITIALIZED);
12719 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
12720 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
12722 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12723 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12724 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
12725 return NULL;
12727 else
12728 gcc_unreachable ();
12729 if (op0 == NULL)
12730 return NULL;
12731 mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
12732 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
12733 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
12734 break;
12736 case DEBUG_PARAMETER_REF:
12737 mem_loc_result = parameter_ref_descriptor (rtl);
12738 break;
12740 case PRE_MODIFY:
12741 /* Extract the PLUS expression nested inside and fall into
12742 PLUS code below. */
12743 rtl = XEXP (rtl, 1);
12744 goto plus;
12746 case PRE_INC:
12747 case PRE_DEC:
12748 /* Turn these into a PLUS expression and fall into the PLUS code
12749 below. */
12750 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
12751 gen_int_mode (GET_CODE (rtl) == PRE_INC
12752 ? GET_MODE_UNIT_SIZE (mem_mode)
12753 : -GET_MODE_UNIT_SIZE (mem_mode),
12754 mode));
12756 /* ... fall through ... */
12758 case PLUS:
12759 plus:
12760 if (is_based_loc (rtl)
12761 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12762 || XEXP (rtl, 0) == arg_pointer_rtx
12763 || XEXP (rtl, 0) == frame_pointer_rtx)
12764 && GET_MODE_CLASS (mode) == MODE_INT)
12765 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
12766 INTVAL (XEXP (rtl, 1)),
12767 VAR_INIT_STATUS_INITIALIZED);
12768 else
12770 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12771 VAR_INIT_STATUS_INITIALIZED);
12772 if (mem_loc_result == 0)
12773 break;
12775 if (CONST_INT_P (XEXP (rtl, 1))
12776 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12777 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
12778 else
12780 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12781 VAR_INIT_STATUS_INITIALIZED);
12782 if (op1 == 0)
12783 return NULL;
12784 add_loc_descr (&mem_loc_result, op1);
12785 add_loc_descr (&mem_loc_result,
12786 new_loc_descr (DW_OP_plus, 0, 0));
12789 break;
12791 /* If a pseudo-reg is optimized away, it is possible for it to
12792 be replaced with a MEM containing a multiply or shift. */
12793 case MINUS:
12794 op = DW_OP_minus;
12795 goto do_binop;
12797 case MULT:
12798 op = DW_OP_mul;
12799 goto do_binop;
12801 case DIV:
12802 if (!dwarf_strict
12803 && GET_MODE_CLASS (mode) == MODE_INT
12804 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12806 mem_loc_result = typed_binop (DW_OP_div, rtl,
12807 base_type_for_mode (mode, 0),
12808 mode, mem_mode);
12809 break;
12811 op = DW_OP_div;
12812 goto do_binop;
12814 case UMOD:
12815 op = DW_OP_mod;
12816 goto do_binop;
12818 case ASHIFT:
12819 op = DW_OP_shl;
12820 goto do_shift;
12822 case ASHIFTRT:
12823 op = DW_OP_shra;
12824 goto do_shift;
12826 case LSHIFTRT:
12827 op = DW_OP_shr;
12828 goto do_shift;
12830 do_shift:
12831 if (GET_MODE_CLASS (mode) != MODE_INT)
12832 break;
12833 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12834 VAR_INIT_STATUS_INITIALIZED);
12836 rtx rtlop1 = XEXP (rtl, 1);
12837 if (GET_MODE (rtlop1) != VOIDmode
12838 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
12839 < GET_MODE_BITSIZE (mode))
12840 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12841 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12842 VAR_INIT_STATUS_INITIALIZED);
12845 if (op0 == 0 || op1 == 0)
12846 break;
12848 mem_loc_result = op0;
12849 add_loc_descr (&mem_loc_result, op1);
12850 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12851 break;
12853 case AND:
12854 op = DW_OP_and;
12855 goto do_binop;
12857 case IOR:
12858 op = DW_OP_or;
12859 goto do_binop;
12861 case XOR:
12862 op = DW_OP_xor;
12863 goto do_binop;
12865 do_binop:
12866 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12867 VAR_INIT_STATUS_INITIALIZED);
12868 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12869 VAR_INIT_STATUS_INITIALIZED);
12871 if (op0 == 0 || op1 == 0)
12872 break;
12874 mem_loc_result = op0;
12875 add_loc_descr (&mem_loc_result, op1);
12876 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12877 break;
12879 case MOD:
12880 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
12882 mem_loc_result = typed_binop (DW_OP_mod, rtl,
12883 base_type_for_mode (mode, 0),
12884 mode, mem_mode);
12885 break;
12888 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12889 VAR_INIT_STATUS_INITIALIZED);
12890 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12891 VAR_INIT_STATUS_INITIALIZED);
12893 if (op0 == 0 || op1 == 0)
12894 break;
12896 mem_loc_result = op0;
12897 add_loc_descr (&mem_loc_result, op1);
12898 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12899 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12900 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
12901 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
12902 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
12903 break;
12905 case UDIV:
12906 if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
12908 if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
12910 op = DW_OP_div;
12911 goto do_binop;
12913 mem_loc_result = typed_binop (DW_OP_div, rtl,
12914 base_type_for_mode (mode, 1),
12915 mode, mem_mode);
12917 break;
12919 case NOT:
12920 op = DW_OP_not;
12921 goto do_unop;
12923 case ABS:
12924 op = DW_OP_abs;
12925 goto do_unop;
12927 case NEG:
12928 op = DW_OP_neg;
12929 goto do_unop;
12931 do_unop:
12932 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12933 VAR_INIT_STATUS_INITIALIZED);
12935 if (op0 == 0)
12936 break;
12938 mem_loc_result = op0;
12939 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12940 break;
12942 case CONST_INT:
12943 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12944 #ifdef POINTERS_EXTEND_UNSIGNED
12945 || (mode == Pmode
12946 && mem_mode != VOIDmode
12947 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
12948 #endif
12951 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12952 break;
12954 if (!dwarf_strict
12955 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
12956 || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
12958 dw_die_ref type_die = base_type_for_mode (mode, 1);
12959 machine_mode amode;
12960 if (type_die == NULL)
12961 return NULL;
12962 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
12963 MODE_INT, 0);
12964 if (INTVAL (rtl) >= 0
12965 && amode != BLKmode
12966 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
12967 /* const DW_OP_GNU_convert <XXX> vs.
12968 DW_OP_GNU_const_type <XXX, 1, const>. */
12969 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
12970 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
12972 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12973 op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12974 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12975 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12976 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
12977 add_loc_descr (&mem_loc_result, op0);
12978 return mem_loc_result;
12980 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
12981 INTVAL (rtl));
12982 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12983 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12984 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12985 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12986 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
12987 else
12989 mem_loc_result->dw_loc_oprnd2.val_class
12990 = dw_val_class_const_double;
12991 mem_loc_result->dw_loc_oprnd2.v.val_double
12992 = double_int::from_shwi (INTVAL (rtl));
12995 break;
12997 case CONST_DOUBLE:
12998 if (!dwarf_strict)
13000 dw_die_ref type_die;
13002 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
13003 CONST_DOUBLE rtx could represent either a large integer
13004 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
13005 the value is always a floating point constant.
13007 When it is an integer, a CONST_DOUBLE is used whenever
13008 the constant requires 2 HWIs to be adequately represented.
13009 We output CONST_DOUBLEs as blocks. */
13010 if (mode == VOIDmode
13011 || (GET_MODE (rtl) == VOIDmode
13012 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
13013 break;
13014 type_die = base_type_for_mode (mode,
13015 GET_MODE_CLASS (mode) == MODE_INT);
13016 if (type_die == NULL)
13017 return NULL;
13018 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
13019 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13020 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13021 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13022 #if TARGET_SUPPORTS_WIDE_INT == 0
13023 if (!SCALAR_FLOAT_MODE_P (mode))
13025 mem_loc_result->dw_loc_oprnd2.val_class
13026 = dw_val_class_const_double;
13027 mem_loc_result->dw_loc_oprnd2.v.val_double
13028 = rtx_to_double_int (rtl);
13030 else
13031 #endif
13033 unsigned int length = GET_MODE_SIZE (mode);
13034 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
13036 insert_float (rtl, array);
13037 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13038 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13039 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13040 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13043 break;
13045 case CONST_WIDE_INT:
13046 if (!dwarf_strict)
13048 dw_die_ref type_die;
13050 type_die = base_type_for_mode (mode,
13051 GET_MODE_CLASS (mode) == MODE_INT);
13052 if (type_die == NULL)
13053 return NULL;
13054 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
13055 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13056 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13057 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13058 mem_loc_result->dw_loc_oprnd2.val_class
13059 = dw_val_class_wide_int;
13060 mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_cleared_alloc<wide_int> ();
13061 *mem_loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
13063 break;
13065 case EQ:
13066 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
13067 break;
13069 case GE:
13070 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
13071 break;
13073 case GT:
13074 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
13075 break;
13077 case LE:
13078 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
13079 break;
13081 case LT:
13082 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
13083 break;
13085 case NE:
13086 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
13087 break;
13089 case GEU:
13090 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
13091 break;
13093 case GTU:
13094 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
13095 break;
13097 case LEU:
13098 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
13099 break;
13101 case LTU:
13102 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
13103 break;
13105 case UMIN:
13106 case UMAX:
13107 if (GET_MODE_CLASS (mode) != MODE_INT)
13108 break;
13109 /* FALLTHRU */
13110 case SMIN:
13111 case SMAX:
13112 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
13113 break;
13115 case ZERO_EXTRACT:
13116 case SIGN_EXTRACT:
13117 if (CONST_INT_P (XEXP (rtl, 1))
13118 && CONST_INT_P (XEXP (rtl, 2))
13119 && ((unsigned) INTVAL (XEXP (rtl, 1))
13120 + (unsigned) INTVAL (XEXP (rtl, 2))
13121 <= GET_MODE_BITSIZE (mode))
13122 && GET_MODE_CLASS (mode) == MODE_INT
13123 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13124 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
13126 int shift, size;
13127 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13128 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13129 if (op0 == 0)
13130 break;
13131 if (GET_CODE (rtl) == SIGN_EXTRACT)
13132 op = DW_OP_shra;
13133 else
13134 op = DW_OP_shr;
13135 mem_loc_result = op0;
13136 size = INTVAL (XEXP (rtl, 1));
13137 shift = INTVAL (XEXP (rtl, 2));
13138 if (BITS_BIG_ENDIAN)
13139 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
13140 - shift - size;
13141 if (shift + size != (int) DWARF2_ADDR_SIZE)
13143 add_loc_descr (&mem_loc_result,
13144 int_loc_descriptor (DWARF2_ADDR_SIZE
13145 - shift - size));
13146 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13148 if (size != (int) DWARF2_ADDR_SIZE)
13150 add_loc_descr (&mem_loc_result,
13151 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
13152 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13155 break;
13157 case IF_THEN_ELSE:
13159 dw_loc_descr_ref op2, bra_node, drop_node;
13160 op0 = mem_loc_descriptor (XEXP (rtl, 0),
13161 GET_MODE (XEXP (rtl, 0)) == VOIDmode
13162 ? word_mode : GET_MODE (XEXP (rtl, 0)),
13163 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13164 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13165 VAR_INIT_STATUS_INITIALIZED);
13166 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
13167 VAR_INIT_STATUS_INITIALIZED);
13168 if (op0 == NULL || op1 == NULL || op2 == NULL)
13169 break;
13171 mem_loc_result = op1;
13172 add_loc_descr (&mem_loc_result, op2);
13173 add_loc_descr (&mem_loc_result, op0);
13174 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13175 add_loc_descr (&mem_loc_result, bra_node);
13176 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
13177 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
13178 add_loc_descr (&mem_loc_result, drop_node);
13179 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13180 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
13182 break;
13184 case FLOAT_EXTEND:
13185 case FLOAT_TRUNCATE:
13186 case FLOAT:
13187 case UNSIGNED_FLOAT:
13188 case FIX:
13189 case UNSIGNED_FIX:
13190 if (!dwarf_strict)
13192 dw_die_ref type_die;
13193 dw_loc_descr_ref cvt;
13195 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13196 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13197 if (op0 == NULL)
13198 break;
13199 if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
13200 && (GET_CODE (rtl) == FLOAT
13201 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
13202 <= DWARF2_ADDR_SIZE))
13204 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
13205 GET_CODE (rtl) == UNSIGNED_FLOAT);
13206 if (type_die == NULL)
13207 break;
13208 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13209 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13210 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13211 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13212 add_loc_descr (&op0, cvt);
13214 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
13215 if (type_die == NULL)
13216 break;
13217 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13218 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13219 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13220 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13221 add_loc_descr (&op0, cvt);
13222 if (GET_MODE_CLASS (mode) == MODE_INT
13223 && (GET_CODE (rtl) == FIX
13224 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
13226 op0 = convert_descriptor_to_mode (mode, op0);
13227 if (op0 == NULL)
13228 break;
13230 mem_loc_result = op0;
13232 break;
13234 case CLZ:
13235 case CTZ:
13236 case FFS:
13237 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
13238 break;
13240 case POPCOUNT:
13241 case PARITY:
13242 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
13243 break;
13245 case BSWAP:
13246 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
13247 break;
13249 case ROTATE:
13250 case ROTATERT:
13251 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
13252 break;
13254 case COMPARE:
13255 /* In theory, we could implement the above. */
13256 /* DWARF cannot represent the unsigned compare operations
13257 natively. */
13258 case SS_MULT:
13259 case US_MULT:
13260 case SS_DIV:
13261 case US_DIV:
13262 case SS_PLUS:
13263 case US_PLUS:
13264 case SS_MINUS:
13265 case US_MINUS:
13266 case SS_NEG:
13267 case US_NEG:
13268 case SS_ABS:
13269 case SS_ASHIFT:
13270 case US_ASHIFT:
13271 case SS_TRUNCATE:
13272 case US_TRUNCATE:
13273 case UNORDERED:
13274 case ORDERED:
13275 case UNEQ:
13276 case UNGE:
13277 case UNGT:
13278 case UNLE:
13279 case UNLT:
13280 case LTGT:
13281 case FRACT_CONVERT:
13282 case UNSIGNED_FRACT_CONVERT:
13283 case SAT_FRACT:
13284 case UNSIGNED_SAT_FRACT:
13285 case SQRT:
13286 case ASM_OPERANDS:
13287 case VEC_MERGE:
13288 case VEC_SELECT:
13289 case VEC_CONCAT:
13290 case VEC_DUPLICATE:
13291 case UNSPEC:
13292 case HIGH:
13293 case FMA:
13294 case STRICT_LOW_PART:
13295 case CONST_VECTOR:
13296 case CONST_FIXED:
13297 case CLRSB:
13298 case CLOBBER:
13299 /* If delegitimize_address couldn't do anything with the UNSPEC, we
13300 can't express it in the debug info. This can happen e.g. with some
13301 TLS UNSPECs. */
13302 break;
13304 case CONST_STRING:
13305 resolve_one_addr (&rtl);
13306 goto symref;
13308 default:
13309 #ifdef ENABLE_CHECKING
13310 print_rtl (stderr, rtl);
13311 gcc_unreachable ();
13312 #else
13313 break;
13314 #endif
13317 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13318 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13320 return mem_loc_result;
13323 /* Return a descriptor that describes the concatenation of two locations.
13324 This is typically a complex variable. */
13326 static dw_loc_descr_ref
13327 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
13329 dw_loc_descr_ref cc_loc_result = NULL;
13330 dw_loc_descr_ref x0_ref
13331 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13332 dw_loc_descr_ref x1_ref
13333 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13335 if (x0_ref == 0 || x1_ref == 0)
13336 return 0;
13338 cc_loc_result = x0_ref;
13339 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
13341 add_loc_descr (&cc_loc_result, x1_ref);
13342 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
13344 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13345 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13347 return cc_loc_result;
13350 /* Return a descriptor that describes the concatenation of N
13351 locations. */
13353 static dw_loc_descr_ref
13354 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
13356 unsigned int i;
13357 dw_loc_descr_ref cc_loc_result = NULL;
13358 unsigned int n = XVECLEN (concatn, 0);
13360 for (i = 0; i < n; ++i)
13362 dw_loc_descr_ref ref;
13363 rtx x = XVECEXP (concatn, 0, i);
13365 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13366 if (ref == NULL)
13367 return NULL;
13369 add_loc_descr (&cc_loc_result, ref);
13370 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
13373 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13374 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13376 return cc_loc_result;
13379 /* Helper function for loc_descriptor. Return DW_OP_GNU_implicit_pointer
13380 for DEBUG_IMPLICIT_PTR RTL. */
13382 static dw_loc_descr_ref
13383 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
13385 dw_loc_descr_ref ret;
13386 dw_die_ref ref;
13388 if (dwarf_strict)
13389 return NULL;
13390 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
13391 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
13392 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
13393 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
13394 ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
13395 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
13396 if (ref)
13398 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13399 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
13400 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
13402 else
13404 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
13405 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
13407 return ret;
13410 /* Output a proper Dwarf location descriptor for a variable or parameter
13411 which is either allocated in a register or in a memory location. For a
13412 register, we just generate an OP_REG and the register number. For a
13413 memory location we provide a Dwarf postfix expression describing how to
13414 generate the (dynamic) address of the object onto the address stack.
13416 MODE is mode of the decl if this loc_descriptor is going to be used in
13417 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
13418 allowed, VOIDmode otherwise.
13420 If we don't know how to describe it, return 0. */
13422 static dw_loc_descr_ref
13423 loc_descriptor (rtx rtl, machine_mode mode,
13424 enum var_init_status initialized)
13426 dw_loc_descr_ref loc_result = NULL;
13428 switch (GET_CODE (rtl))
13430 case SUBREG:
13431 /* The case of a subreg may arise when we have a local (register)
13432 variable or a formal (register) parameter which doesn't quite fill
13433 up an entire register. For now, just assume that it is
13434 legitimate to make the Dwarf info refer to the whole register which
13435 contains the given subreg. */
13436 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
13437 loc_result = loc_descriptor (SUBREG_REG (rtl),
13438 GET_MODE (SUBREG_REG (rtl)), initialized);
13439 else
13440 goto do_default;
13441 break;
13443 case REG:
13444 loc_result = reg_loc_descriptor (rtl, initialized);
13445 break;
13447 case MEM:
13448 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13449 GET_MODE (rtl), initialized);
13450 if (loc_result == NULL)
13451 loc_result = tls_mem_loc_descriptor (rtl);
13452 if (loc_result == NULL)
13454 rtx new_rtl = avoid_constant_pool_reference (rtl);
13455 if (new_rtl != rtl)
13456 loc_result = loc_descriptor (new_rtl, mode, initialized);
13458 break;
13460 case CONCAT:
13461 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
13462 initialized);
13463 break;
13465 case CONCATN:
13466 loc_result = concatn_loc_descriptor (rtl, initialized);
13467 break;
13469 case VAR_LOCATION:
13470 /* Single part. */
13471 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
13473 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
13474 if (GET_CODE (loc) == EXPR_LIST)
13475 loc = XEXP (loc, 0);
13476 loc_result = loc_descriptor (loc, mode, initialized);
13477 break;
13480 rtl = XEXP (rtl, 1);
13481 /* FALLTHRU */
13483 case PARALLEL:
13485 rtvec par_elems = XVEC (rtl, 0);
13486 int num_elem = GET_NUM_ELEM (par_elems);
13487 machine_mode mode;
13488 int i;
13490 /* Create the first one, so we have something to add to. */
13491 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
13492 VOIDmode, initialized);
13493 if (loc_result == NULL)
13494 return NULL;
13495 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
13496 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13497 for (i = 1; i < num_elem; i++)
13499 dw_loc_descr_ref temp;
13501 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
13502 VOIDmode, initialized);
13503 if (temp == NULL)
13504 return NULL;
13505 add_loc_descr (&loc_result, temp);
13506 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
13507 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13510 break;
13512 case CONST_INT:
13513 if (mode != VOIDmode && mode != BLKmode)
13514 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
13515 INTVAL (rtl));
13516 break;
13518 case CONST_DOUBLE:
13519 if (mode == VOIDmode)
13520 mode = GET_MODE (rtl);
13522 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13524 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13526 /* Note that a CONST_DOUBLE rtx could represent either an integer
13527 or a floating-point constant. A CONST_DOUBLE is used whenever
13528 the constant requires more than one word in order to be
13529 adequately represented. We output CONST_DOUBLEs as blocks. */
13530 loc_result = new_loc_descr (DW_OP_implicit_value,
13531 GET_MODE_SIZE (mode), 0);
13532 #if TARGET_SUPPORTS_WIDE_INT == 0
13533 if (!SCALAR_FLOAT_MODE_P (mode))
13535 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
13536 loc_result->dw_loc_oprnd2.v.val_double
13537 = rtx_to_double_int (rtl);
13539 else
13540 #endif
13542 unsigned int length = GET_MODE_SIZE (mode);
13543 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
13545 insert_float (rtl, array);
13546 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13547 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13548 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13549 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13552 break;
13554 case CONST_WIDE_INT:
13555 if (mode == VOIDmode)
13556 mode = GET_MODE (rtl);
13558 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13560 loc_result = new_loc_descr (DW_OP_implicit_value,
13561 GET_MODE_SIZE (mode), 0);
13562 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
13563 loc_result->dw_loc_oprnd2.v.val_wide = ggc_cleared_alloc<wide_int> ();
13564 *loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
13566 break;
13568 case CONST_VECTOR:
13569 if (mode == VOIDmode)
13570 mode = GET_MODE (rtl);
13572 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13574 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
13575 unsigned int length = CONST_VECTOR_NUNITS (rtl);
13576 unsigned char *array
13577 = ggc_vec_alloc<unsigned char> (length * elt_size);
13578 unsigned int i;
13579 unsigned char *p;
13580 machine_mode imode = GET_MODE_INNER (mode);
13582 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13583 switch (GET_MODE_CLASS (mode))
13585 case MODE_VECTOR_INT:
13586 for (i = 0, p = array; i < length; i++, p += elt_size)
13588 rtx elt = CONST_VECTOR_ELT (rtl, i);
13589 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
13591 break;
13593 case MODE_VECTOR_FLOAT:
13594 for (i = 0, p = array; i < length; i++, p += elt_size)
13596 rtx elt = CONST_VECTOR_ELT (rtl, i);
13597 insert_float (elt, p);
13599 break;
13601 default:
13602 gcc_unreachable ();
13605 loc_result = new_loc_descr (DW_OP_implicit_value,
13606 length * elt_size, 0);
13607 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13608 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
13609 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
13610 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13612 break;
13614 case CONST:
13615 if (mode == VOIDmode
13616 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
13617 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
13618 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
13620 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
13621 break;
13623 /* FALLTHROUGH */
13624 case SYMBOL_REF:
13625 if (!const_ok_for_output (rtl))
13626 break;
13627 case LABEL_REF:
13628 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
13629 && (dwarf_version >= 4 || !dwarf_strict))
13631 loc_result = new_addr_loc_descr (rtl, dtprel_false);
13632 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
13633 vec_safe_push (used_rtx_array, rtl);
13635 break;
13637 case DEBUG_IMPLICIT_PTR:
13638 loc_result = implicit_ptr_descriptor (rtl, 0);
13639 break;
13641 case PLUS:
13642 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
13643 && CONST_INT_P (XEXP (rtl, 1)))
13645 loc_result
13646 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
13647 break;
13649 /* FALLTHRU */
13650 do_default:
13651 default:
13652 if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
13653 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
13654 && dwarf_version >= 4)
13655 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
13657 /* Value expression. */
13658 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
13659 if (loc_result)
13660 add_loc_descr (&loc_result,
13661 new_loc_descr (DW_OP_stack_value, 0, 0));
13663 break;
13666 return loc_result;
13669 /* We need to figure out what section we should use as the base for the
13670 address ranges where a given location is valid.
13671 1. If this particular DECL has a section associated with it, use that.
13672 2. If this function has a section associated with it, use that.
13673 3. Otherwise, use the text section.
13674 XXX: If you split a variable across multiple sections, we won't notice. */
13676 static const char *
13677 secname_for_decl (const_tree decl)
13679 const char *secname;
13681 if (VAR_OR_FUNCTION_DECL_P (decl)
13682 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
13683 && DECL_SECTION_NAME (decl))
13684 secname = DECL_SECTION_NAME (decl);
13685 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
13686 secname = DECL_SECTION_NAME (current_function_decl);
13687 else if (cfun && in_cold_section_p)
13688 secname = crtl->subsections.cold_section_label;
13689 else
13690 secname = text_section_label;
13692 return secname;
13695 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
13697 static bool
13698 decl_by_reference_p (tree decl)
13700 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
13701 || TREE_CODE (decl) == VAR_DECL)
13702 && DECL_BY_REFERENCE (decl));
13705 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13706 for VARLOC. */
13708 static dw_loc_descr_ref
13709 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
13710 enum var_init_status initialized)
13712 int have_address = 0;
13713 dw_loc_descr_ref descr;
13714 machine_mode mode;
13716 if (want_address != 2)
13718 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
13719 /* Single part. */
13720 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13722 varloc = PAT_VAR_LOCATION_LOC (varloc);
13723 if (GET_CODE (varloc) == EXPR_LIST)
13724 varloc = XEXP (varloc, 0);
13725 mode = GET_MODE (varloc);
13726 if (MEM_P (varloc))
13728 rtx addr = XEXP (varloc, 0);
13729 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
13730 mode, initialized);
13731 if (descr)
13732 have_address = 1;
13733 else
13735 rtx x = avoid_constant_pool_reference (varloc);
13736 if (x != varloc)
13737 descr = mem_loc_descriptor (x, mode, VOIDmode,
13738 initialized);
13741 else
13742 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
13744 else
13745 return 0;
13747 else
13749 if (GET_CODE (varloc) == VAR_LOCATION)
13750 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
13751 else
13752 mode = DECL_MODE (loc);
13753 descr = loc_descriptor (varloc, mode, initialized);
13754 have_address = 1;
13757 if (!descr)
13758 return 0;
13760 if (want_address == 2 && !have_address
13761 && (dwarf_version >= 4 || !dwarf_strict))
13763 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
13765 expansion_failed (loc, NULL_RTX,
13766 "DWARF address size mismatch");
13767 return 0;
13769 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
13770 have_address = 1;
13772 /* Show if we can't fill the request for an address. */
13773 if (want_address && !have_address)
13775 expansion_failed (loc, NULL_RTX,
13776 "Want address and only have value");
13777 return 0;
13780 /* If we've got an address and don't want one, dereference. */
13781 if (!want_address && have_address)
13783 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
13784 enum dwarf_location_atom op;
13786 if (size > DWARF2_ADDR_SIZE || size == -1)
13788 expansion_failed (loc, NULL_RTX,
13789 "DWARF address size mismatch");
13790 return 0;
13792 else if (size == DWARF2_ADDR_SIZE)
13793 op = DW_OP_deref;
13794 else
13795 op = DW_OP_deref_size;
13797 add_loc_descr (&descr, new_loc_descr (op, size, 0));
13800 return descr;
13803 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
13804 if it is not possible. */
13806 static dw_loc_descr_ref
13807 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
13809 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
13810 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
13811 else if (dwarf_version >= 3 || !dwarf_strict)
13812 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
13813 else
13814 return NULL;
13817 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13818 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
13820 static dw_loc_descr_ref
13821 dw_sra_loc_expr (tree decl, rtx loc)
13823 rtx p;
13824 unsigned int padsize = 0;
13825 dw_loc_descr_ref descr, *descr_tail;
13826 unsigned HOST_WIDE_INT decl_size;
13827 rtx varloc;
13828 enum var_init_status initialized;
13830 if (DECL_SIZE (decl) == NULL
13831 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
13832 return NULL;
13834 decl_size = tree_to_uhwi (DECL_SIZE (decl));
13835 descr = NULL;
13836 descr_tail = &descr;
13838 for (p = loc; p; p = XEXP (p, 1))
13840 unsigned int bitsize = decl_piece_bitsize (p);
13841 rtx loc_note = *decl_piece_varloc_ptr (p);
13842 dw_loc_descr_ref cur_descr;
13843 dw_loc_descr_ref *tail, last = NULL;
13844 unsigned int opsize = 0;
13846 if (loc_note == NULL_RTX
13847 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
13849 padsize += bitsize;
13850 continue;
13852 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
13853 varloc = NOTE_VAR_LOCATION (loc_note);
13854 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
13855 if (cur_descr == NULL)
13857 padsize += bitsize;
13858 continue;
13861 /* Check that cur_descr either doesn't use
13862 DW_OP_*piece operations, or their sum is equal
13863 to bitsize. Otherwise we can't embed it. */
13864 for (tail = &cur_descr; *tail != NULL;
13865 tail = &(*tail)->dw_loc_next)
13866 if ((*tail)->dw_loc_opc == DW_OP_piece)
13868 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
13869 * BITS_PER_UNIT;
13870 last = *tail;
13872 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
13874 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
13875 last = *tail;
13878 if (last != NULL && opsize != bitsize)
13880 padsize += bitsize;
13881 /* Discard the current piece of the descriptor and release any
13882 addr_table entries it uses. */
13883 remove_loc_list_addr_table_entries (cur_descr);
13884 continue;
13887 /* If there is a hole, add DW_OP_*piece after empty DWARF
13888 expression, which means that those bits are optimized out. */
13889 if (padsize)
13891 if (padsize > decl_size)
13893 remove_loc_list_addr_table_entries (cur_descr);
13894 goto discard_descr;
13896 decl_size -= padsize;
13897 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
13898 if (*descr_tail == NULL)
13900 remove_loc_list_addr_table_entries (cur_descr);
13901 goto discard_descr;
13903 descr_tail = &(*descr_tail)->dw_loc_next;
13904 padsize = 0;
13906 *descr_tail = cur_descr;
13907 descr_tail = tail;
13908 if (bitsize > decl_size)
13909 goto discard_descr;
13910 decl_size -= bitsize;
13911 if (last == NULL)
13913 HOST_WIDE_INT offset = 0;
13914 if (GET_CODE (varloc) == VAR_LOCATION
13915 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13917 varloc = PAT_VAR_LOCATION_LOC (varloc);
13918 if (GET_CODE (varloc) == EXPR_LIST)
13919 varloc = XEXP (varloc, 0);
13923 if (GET_CODE (varloc) == CONST
13924 || GET_CODE (varloc) == SIGN_EXTEND
13925 || GET_CODE (varloc) == ZERO_EXTEND)
13926 varloc = XEXP (varloc, 0);
13927 else if (GET_CODE (varloc) == SUBREG)
13928 varloc = SUBREG_REG (varloc);
13929 else
13930 break;
13932 while (1);
13933 /* DW_OP_bit_size offset should be zero for register
13934 or implicit location descriptions and empty location
13935 descriptions, but for memory addresses needs big endian
13936 adjustment. */
13937 if (MEM_P (varloc))
13939 unsigned HOST_WIDE_INT memsize
13940 = MEM_SIZE (varloc) * BITS_PER_UNIT;
13941 if (memsize != bitsize)
13943 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
13944 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
13945 goto discard_descr;
13946 if (memsize < bitsize)
13947 goto discard_descr;
13948 if (BITS_BIG_ENDIAN)
13949 offset = memsize - bitsize;
13953 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
13954 if (*descr_tail == NULL)
13955 goto discard_descr;
13956 descr_tail = &(*descr_tail)->dw_loc_next;
13960 /* If there were any non-empty expressions, add padding till the end of
13961 the decl. */
13962 if (descr != NULL && decl_size != 0)
13964 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
13965 if (*descr_tail == NULL)
13966 goto discard_descr;
13968 return descr;
13970 discard_descr:
13971 /* Discard the descriptor and release any addr_table entries it uses. */
13972 remove_loc_list_addr_table_entries (descr);
13973 return NULL;
13976 /* Return the dwarf representation of the location list LOC_LIST of
13977 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
13978 function. */
13980 static dw_loc_list_ref
13981 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
13983 const char *endname, *secname;
13984 rtx varloc;
13985 enum var_init_status initialized;
13986 struct var_loc_node *node;
13987 dw_loc_descr_ref descr;
13988 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
13989 dw_loc_list_ref list = NULL;
13990 dw_loc_list_ref *listp = &list;
13992 /* Now that we know what section we are using for a base,
13993 actually construct the list of locations.
13994 The first location information is what is passed to the
13995 function that creates the location list, and the remaining
13996 locations just get added on to that list.
13997 Note that we only know the start address for a location
13998 (IE location changes), so to build the range, we use
13999 the range [current location start, next location start].
14000 This means we have to special case the last node, and generate
14001 a range of [last location start, end of function label]. */
14003 secname = secname_for_decl (decl);
14005 for (node = loc_list->first; node; node = node->next)
14006 if (GET_CODE (node->loc) == EXPR_LIST
14007 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
14009 if (GET_CODE (node->loc) == EXPR_LIST)
14011 /* This requires DW_OP_{,bit_}piece, which is not usable
14012 inside DWARF expressions. */
14013 if (want_address != 2)
14014 continue;
14015 descr = dw_sra_loc_expr (decl, node->loc);
14016 if (descr == NULL)
14017 continue;
14019 else
14021 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14022 varloc = NOTE_VAR_LOCATION (node->loc);
14023 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
14025 if (descr)
14027 bool range_across_switch = false;
14028 /* If section switch happens in between node->label
14029 and node->next->label (or end of function) and
14030 we can't emit it as a single entry list,
14031 emit two ranges, first one ending at the end
14032 of first partition and second one starting at the
14033 beginning of second partition. */
14034 if (node == loc_list->last_before_switch
14035 && (node != loc_list->first || loc_list->first->next)
14036 && current_function_decl)
14038 endname = cfun->fde->dw_fde_end;
14039 range_across_switch = true;
14041 /* The variable has a location between NODE->LABEL and
14042 NODE->NEXT->LABEL. */
14043 else if (node->next)
14044 endname = node->next->label;
14045 /* If the variable has a location at the last label
14046 it keeps its location until the end of function. */
14047 else if (!current_function_decl)
14048 endname = text_end_label;
14049 else
14051 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
14052 current_function_funcdef_no);
14053 endname = ggc_strdup (label_id);
14056 *listp = new_loc_list (descr, node->label, endname, secname);
14057 if (TREE_CODE (decl) == PARM_DECL
14058 && node == loc_list->first
14059 && NOTE_P (node->loc)
14060 && strcmp (node->label, endname) == 0)
14061 (*listp)->force = true;
14062 listp = &(*listp)->dw_loc_next;
14064 if (range_across_switch)
14066 if (GET_CODE (node->loc) == EXPR_LIST)
14067 descr = dw_sra_loc_expr (decl, node->loc);
14068 else
14070 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14071 varloc = NOTE_VAR_LOCATION (node->loc);
14072 descr = dw_loc_list_1 (decl, varloc, want_address,
14073 initialized);
14075 gcc_assert (descr);
14076 /* The variable has a location between NODE->LABEL and
14077 NODE->NEXT->LABEL. */
14078 if (node->next)
14079 endname = node->next->label;
14080 else
14081 endname = cfun->fde->dw_fde_second_end;
14082 *listp = new_loc_list (descr,
14083 cfun->fde->dw_fde_second_begin,
14084 endname, secname);
14085 listp = &(*listp)->dw_loc_next;
14090 /* Try to avoid the overhead of a location list emitting a location
14091 expression instead, but only if we didn't have more than one
14092 location entry in the first place. If some entries were not
14093 representable, we don't want to pretend a single entry that was
14094 applies to the entire scope in which the variable is
14095 available. */
14096 if (list && loc_list->first->next)
14097 gen_llsym (list);
14099 return list;
14102 /* Return if the loc_list has only single element and thus can be represented
14103 as location description. */
14105 static bool
14106 single_element_loc_list_p (dw_loc_list_ref list)
14108 gcc_assert (!list->dw_loc_next || list->ll_symbol);
14109 return !list->ll_symbol;
14112 /* To each location in list LIST add loc descr REF. */
14114 static void
14115 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
14117 dw_loc_descr_ref copy;
14118 add_loc_descr (&list->expr, ref);
14119 list = list->dw_loc_next;
14120 while (list)
14122 copy = ggc_alloc<dw_loc_descr_node> ();
14123 memcpy (copy, ref, sizeof (dw_loc_descr_node));
14124 add_loc_descr (&list->expr, copy);
14125 while (copy->dw_loc_next)
14127 dw_loc_descr_ref new_copy = ggc_alloc<dw_loc_descr_node> ();
14128 memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
14129 copy->dw_loc_next = new_copy;
14130 copy = new_copy;
14132 list = list->dw_loc_next;
14136 /* Given two lists RET and LIST
14137 produce location list that is result of adding expression in LIST
14138 to expression in RET on each position in program.
14139 Might be destructive on both RET and LIST.
14141 TODO: We handle only simple cases of RET or LIST having at most one
14142 element. General case would inolve sorting the lists in program order
14143 and merging them that will need some additional work.
14144 Adding that will improve quality of debug info especially for SRA-ed
14145 structures. */
14147 static void
14148 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
14150 if (!list)
14151 return;
14152 if (!*ret)
14154 *ret = list;
14155 return;
14157 if (!list->dw_loc_next)
14159 add_loc_descr_to_each (*ret, list->expr);
14160 return;
14162 if (!(*ret)->dw_loc_next)
14164 add_loc_descr_to_each (list, (*ret)->expr);
14165 *ret = list;
14166 return;
14168 expansion_failed (NULL_TREE, NULL_RTX,
14169 "Don't know how to merge two non-trivial"
14170 " location lists.\n");
14171 *ret = NULL;
14172 return;
14175 /* LOC is constant expression. Try a luck, look it up in constant
14176 pool and return its loc_descr of its address. */
14178 static dw_loc_descr_ref
14179 cst_pool_loc_descr (tree loc)
14181 /* Get an RTL for this, if something has been emitted. */
14182 rtx rtl = lookup_constant_def (loc);
14184 if (!rtl || !MEM_P (rtl))
14186 gcc_assert (!rtl);
14187 return 0;
14189 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
14191 /* TODO: We might get more coverage if we was actually delaying expansion
14192 of all expressions till end of compilation when constant pools are fully
14193 populated. */
14194 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
14196 expansion_failed (loc, NULL_RTX,
14197 "CST value in contant pool but not marked.");
14198 return 0;
14200 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
14201 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
14204 /* Return dw_loc_list representing address of addr_expr LOC
14205 by looking for inner INDIRECT_REF expression and turning
14206 it into simple arithmetics. */
14208 static dw_loc_list_ref
14209 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
14211 tree obj, offset;
14212 HOST_WIDE_INT bitsize, bitpos, bytepos;
14213 machine_mode mode;
14214 int unsignedp, volatilep = 0;
14215 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14217 obj = get_inner_reference (TREE_OPERAND (loc, 0),
14218 &bitsize, &bitpos, &offset, &mode,
14219 &unsignedp, &volatilep, false);
14220 STRIP_NOPS (obj);
14221 if (bitpos % BITS_PER_UNIT)
14223 expansion_failed (loc, NULL_RTX, "bitfield access");
14224 return 0;
14226 if (!INDIRECT_REF_P (obj))
14228 expansion_failed (obj,
14229 NULL_RTX, "no indirect ref in inner refrence");
14230 return 0;
14232 if (!offset && !bitpos)
14233 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
14234 else if (toplev
14235 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
14236 && (dwarf_version >= 4 || !dwarf_strict))
14238 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
14239 if (!list_ret)
14240 return 0;
14241 if (offset)
14243 /* Variable offset. */
14244 list_ret1 = loc_list_from_tree (offset, 0);
14245 if (list_ret1 == 0)
14246 return 0;
14247 add_loc_list (&list_ret, list_ret1);
14248 if (!list_ret)
14249 return 0;
14250 add_loc_descr_to_each (list_ret,
14251 new_loc_descr (DW_OP_plus, 0, 0));
14253 bytepos = bitpos / BITS_PER_UNIT;
14254 if (bytepos > 0)
14255 add_loc_descr_to_each (list_ret,
14256 new_loc_descr (DW_OP_plus_uconst,
14257 bytepos, 0));
14258 else if (bytepos < 0)
14259 loc_list_plus_const (list_ret, bytepos);
14260 add_loc_descr_to_each (list_ret,
14261 new_loc_descr (DW_OP_stack_value, 0, 0));
14263 return list_ret;
14267 /* Generate Dwarf location list representing LOC.
14268 If WANT_ADDRESS is false, expression computing LOC will be computed
14269 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
14270 if WANT_ADDRESS is 2, expression computing address useable in location
14271 will be returned (i.e. DW_OP_reg can be used
14272 to refer to register values). */
14274 static dw_loc_list_ref
14275 loc_list_from_tree (tree loc, int want_address)
14277 dw_loc_descr_ref ret = NULL, ret1 = NULL;
14278 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14279 int have_address = 0;
14280 enum dwarf_location_atom op;
14282 /* ??? Most of the time we do not take proper care for sign/zero
14283 extending the values properly. Hopefully this won't be a real
14284 problem... */
14286 switch (TREE_CODE (loc))
14288 case ERROR_MARK:
14289 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
14290 return 0;
14292 case PLACEHOLDER_EXPR:
14293 /* This case involves extracting fields from an object to determine the
14294 position of other fields. We don't try to encode this here. The
14295 only user of this is Ada, which encodes the needed information using
14296 the names of types. */
14297 expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
14298 return 0;
14300 case CALL_EXPR:
14301 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
14302 /* There are no opcodes for these operations. */
14303 return 0;
14305 case PREINCREMENT_EXPR:
14306 case PREDECREMENT_EXPR:
14307 case POSTINCREMENT_EXPR:
14308 case POSTDECREMENT_EXPR:
14309 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
14310 /* There are no opcodes for these operations. */
14311 return 0;
14313 case ADDR_EXPR:
14314 /* If we already want an address, see if there is INDIRECT_REF inside
14315 e.g. for &this->field. */
14316 if (want_address)
14318 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
14319 (loc, want_address == 2);
14320 if (list_ret)
14321 have_address = 1;
14322 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
14323 && (ret = cst_pool_loc_descr (loc)))
14324 have_address = 1;
14326 /* Otherwise, process the argument and look for the address. */
14327 if (!list_ret && !ret)
14328 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
14329 else
14331 if (want_address)
14332 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
14333 return NULL;
14335 break;
14337 case VAR_DECL:
14338 if (DECL_THREAD_LOCAL_P (loc))
14340 rtx rtl;
14341 enum dwarf_location_atom tls_op;
14342 enum dtprel_bool dtprel = dtprel_false;
14344 if (targetm.have_tls)
14346 /* If this is not defined, we have no way to emit the
14347 data. */
14348 if (!targetm.asm_out.output_dwarf_dtprel)
14349 return 0;
14351 /* The way DW_OP_GNU_push_tls_address is specified, we
14352 can only look up addresses of objects in the current
14353 module. We used DW_OP_addr as first op, but that's
14354 wrong, because DW_OP_addr is relocated by the debug
14355 info consumer, while DW_OP_GNU_push_tls_address
14356 operand shouldn't be. */
14357 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
14358 return 0;
14359 dtprel = dtprel_true;
14360 tls_op = DW_OP_GNU_push_tls_address;
14362 else
14364 if (!targetm.emutls.debug_form_tls_address
14365 || !(dwarf_version >= 3 || !dwarf_strict))
14366 return 0;
14367 /* We stuffed the control variable into the DECL_VALUE_EXPR
14368 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
14369 no longer appear in gimple code. We used the control
14370 variable in specific so that we could pick it up here. */
14371 loc = DECL_VALUE_EXPR (loc);
14372 tls_op = DW_OP_form_tls_address;
14375 rtl = rtl_for_decl_location (loc);
14376 if (rtl == NULL_RTX)
14377 return 0;
14379 if (!MEM_P (rtl))
14380 return 0;
14381 rtl = XEXP (rtl, 0);
14382 if (! CONSTANT_P (rtl))
14383 return 0;
14385 ret = new_addr_loc_descr (rtl, dtprel);
14386 ret1 = new_loc_descr (tls_op, 0, 0);
14387 add_loc_descr (&ret, ret1);
14389 have_address = 1;
14390 break;
14392 /* FALLTHRU */
14394 case PARM_DECL:
14395 case RESULT_DECL:
14396 if (DECL_HAS_VALUE_EXPR_P (loc))
14397 return loc_list_from_tree (DECL_VALUE_EXPR (loc),
14398 want_address);
14399 /* FALLTHRU */
14401 case FUNCTION_DECL:
14403 rtx rtl;
14404 var_loc_list *loc_list = lookup_decl_loc (loc);
14406 if (loc_list && loc_list->first)
14408 list_ret = dw_loc_list (loc_list, loc, want_address);
14409 have_address = want_address != 0;
14410 break;
14412 rtl = rtl_for_decl_location (loc);
14413 if (rtl == NULL_RTX)
14415 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
14416 return 0;
14418 else if (CONST_INT_P (rtl))
14420 HOST_WIDE_INT val = INTVAL (rtl);
14421 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14422 val &= GET_MODE_MASK (DECL_MODE (loc));
14423 ret = int_loc_descriptor (val);
14425 else if (GET_CODE (rtl) == CONST_STRING)
14427 expansion_failed (loc, NULL_RTX, "CONST_STRING");
14428 return 0;
14430 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
14431 ret = new_addr_loc_descr (rtl, dtprel_false);
14432 else
14434 machine_mode mode, mem_mode;
14436 /* Certain constructs can only be represented at top-level. */
14437 if (want_address == 2)
14439 ret = loc_descriptor (rtl, VOIDmode,
14440 VAR_INIT_STATUS_INITIALIZED);
14441 have_address = 1;
14443 else
14445 mode = GET_MODE (rtl);
14446 mem_mode = VOIDmode;
14447 if (MEM_P (rtl))
14449 mem_mode = mode;
14450 mode = get_address_mode (rtl);
14451 rtl = XEXP (rtl, 0);
14452 have_address = 1;
14454 ret = mem_loc_descriptor (rtl, mode, mem_mode,
14455 VAR_INIT_STATUS_INITIALIZED);
14457 if (!ret)
14458 expansion_failed (loc, rtl,
14459 "failed to produce loc descriptor for rtl");
14462 break;
14464 case MEM_REF:
14465 if (!integer_zerop (TREE_OPERAND (loc, 1)))
14467 have_address = 1;
14468 goto do_plus;
14470 /* Fallthru. */
14471 case INDIRECT_REF:
14472 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14473 have_address = 1;
14474 break;
14476 case TARGET_MEM_REF:
14477 case SSA_NAME:
14478 return NULL;
14480 case COMPOUND_EXPR:
14481 return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
14483 CASE_CONVERT:
14484 case VIEW_CONVERT_EXPR:
14485 case SAVE_EXPR:
14486 case MODIFY_EXPR:
14487 return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
14489 case COMPONENT_REF:
14490 case BIT_FIELD_REF:
14491 case ARRAY_REF:
14492 case ARRAY_RANGE_REF:
14493 case REALPART_EXPR:
14494 case IMAGPART_EXPR:
14496 tree obj, offset;
14497 HOST_WIDE_INT bitsize, bitpos, bytepos;
14498 machine_mode mode;
14499 int unsignedp, volatilep = 0;
14501 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
14502 &unsignedp, &volatilep, false);
14504 gcc_assert (obj != loc);
14506 list_ret = loc_list_from_tree (obj,
14507 want_address == 2
14508 && !bitpos && !offset ? 2 : 1);
14509 /* TODO: We can extract value of the small expression via shifting even
14510 for nonzero bitpos. */
14511 if (list_ret == 0)
14512 return 0;
14513 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
14515 expansion_failed (loc, NULL_RTX,
14516 "bitfield access");
14517 return 0;
14520 if (offset != NULL_TREE)
14522 /* Variable offset. */
14523 list_ret1 = loc_list_from_tree (offset, 0);
14524 if (list_ret1 == 0)
14525 return 0;
14526 add_loc_list (&list_ret, list_ret1);
14527 if (!list_ret)
14528 return 0;
14529 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
14532 bytepos = bitpos / BITS_PER_UNIT;
14533 if (bytepos > 0)
14534 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
14535 else if (bytepos < 0)
14536 loc_list_plus_const (list_ret, bytepos);
14538 have_address = 1;
14539 break;
14542 case INTEGER_CST:
14543 if ((want_address || !tree_fits_shwi_p (loc))
14544 && (ret = cst_pool_loc_descr (loc)))
14545 have_address = 1;
14546 else if (want_address == 2
14547 && tree_fits_shwi_p (loc)
14548 && (ret = address_of_int_loc_descriptor
14549 (int_size_in_bytes (TREE_TYPE (loc)),
14550 tree_to_shwi (loc))))
14551 have_address = 1;
14552 else if (tree_fits_shwi_p (loc))
14553 ret = int_loc_descriptor (tree_to_shwi (loc));
14554 else
14556 expansion_failed (loc, NULL_RTX,
14557 "Integer operand is not host integer");
14558 return 0;
14560 break;
14562 case CONSTRUCTOR:
14563 case REAL_CST:
14564 case STRING_CST:
14565 case COMPLEX_CST:
14566 if ((ret = cst_pool_loc_descr (loc)))
14567 have_address = 1;
14568 else
14569 /* We can construct small constants here using int_loc_descriptor. */
14570 expansion_failed (loc, NULL_RTX,
14571 "constructor or constant not in constant pool");
14572 break;
14574 case TRUTH_AND_EXPR:
14575 case TRUTH_ANDIF_EXPR:
14576 case BIT_AND_EXPR:
14577 op = DW_OP_and;
14578 goto do_binop;
14580 case TRUTH_XOR_EXPR:
14581 case BIT_XOR_EXPR:
14582 op = DW_OP_xor;
14583 goto do_binop;
14585 case TRUTH_OR_EXPR:
14586 case TRUTH_ORIF_EXPR:
14587 case BIT_IOR_EXPR:
14588 op = DW_OP_or;
14589 goto do_binop;
14591 case FLOOR_DIV_EXPR:
14592 case CEIL_DIV_EXPR:
14593 case ROUND_DIV_EXPR:
14594 case TRUNC_DIV_EXPR:
14595 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14596 return 0;
14597 op = DW_OP_div;
14598 goto do_binop;
14600 case MINUS_EXPR:
14601 op = DW_OP_minus;
14602 goto do_binop;
14604 case FLOOR_MOD_EXPR:
14605 case CEIL_MOD_EXPR:
14606 case ROUND_MOD_EXPR:
14607 case TRUNC_MOD_EXPR:
14608 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14610 op = DW_OP_mod;
14611 goto do_binop;
14613 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14614 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14615 if (list_ret == 0 || list_ret1 == 0)
14616 return 0;
14618 add_loc_list (&list_ret, list_ret1);
14619 if (list_ret == 0)
14620 return 0;
14621 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14622 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14623 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
14624 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
14625 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
14626 break;
14628 case MULT_EXPR:
14629 op = DW_OP_mul;
14630 goto do_binop;
14632 case LSHIFT_EXPR:
14633 op = DW_OP_shl;
14634 goto do_binop;
14636 case RSHIFT_EXPR:
14637 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
14638 goto do_binop;
14640 case POINTER_PLUS_EXPR:
14641 case PLUS_EXPR:
14642 do_plus:
14643 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
14645 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14646 if (list_ret == 0)
14647 return 0;
14649 loc_list_plus_const (list_ret, tree_to_shwi (TREE_OPERAND (loc, 1)));
14650 break;
14653 op = DW_OP_plus;
14654 goto do_binop;
14656 case LE_EXPR:
14657 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14658 return 0;
14660 op = DW_OP_le;
14661 goto do_binop;
14663 case GE_EXPR:
14664 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14665 return 0;
14667 op = DW_OP_ge;
14668 goto do_binop;
14670 case LT_EXPR:
14671 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14672 return 0;
14674 op = DW_OP_lt;
14675 goto do_binop;
14677 case GT_EXPR:
14678 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14679 return 0;
14681 op = DW_OP_gt;
14682 goto do_binop;
14684 case EQ_EXPR:
14685 op = DW_OP_eq;
14686 goto do_binop;
14688 case NE_EXPR:
14689 op = DW_OP_ne;
14690 goto do_binop;
14692 do_binop:
14693 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14694 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14695 if (list_ret == 0 || list_ret1 == 0)
14696 return 0;
14698 add_loc_list (&list_ret, list_ret1);
14699 if (list_ret == 0)
14700 return 0;
14701 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14702 break;
14704 case TRUTH_NOT_EXPR:
14705 case BIT_NOT_EXPR:
14706 op = DW_OP_not;
14707 goto do_unop;
14709 case ABS_EXPR:
14710 op = DW_OP_abs;
14711 goto do_unop;
14713 case NEGATE_EXPR:
14714 op = DW_OP_neg;
14715 goto do_unop;
14717 do_unop:
14718 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14719 if (list_ret == 0)
14720 return 0;
14722 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14723 break;
14725 case MIN_EXPR:
14726 case MAX_EXPR:
14728 const enum tree_code code =
14729 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
14731 loc = build3 (COND_EXPR, TREE_TYPE (loc),
14732 build2 (code, integer_type_node,
14733 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
14734 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
14737 /* ... fall through ... */
14739 case COND_EXPR:
14741 dw_loc_descr_ref lhs
14742 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
14743 dw_loc_list_ref rhs
14744 = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
14745 dw_loc_descr_ref bra_node, jump_node, tmp;
14747 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14748 if (list_ret == 0 || lhs == 0 || rhs == 0)
14749 return 0;
14751 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14752 add_loc_descr_to_each (list_ret, bra_node);
14754 add_loc_list (&list_ret, rhs);
14755 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
14756 add_loc_descr_to_each (list_ret, jump_node);
14758 add_loc_descr_to_each (list_ret, lhs);
14759 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14760 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
14762 /* ??? Need a node to point the skip at. Use a nop. */
14763 tmp = new_loc_descr (DW_OP_nop, 0, 0);
14764 add_loc_descr_to_each (list_ret, tmp);
14765 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14766 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
14768 break;
14770 case FIX_TRUNC_EXPR:
14771 return 0;
14773 default:
14774 /* Leave front-end specific codes as simply unknown. This comes
14775 up, for instance, with the C STMT_EXPR. */
14776 if ((unsigned int) TREE_CODE (loc)
14777 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
14779 expansion_failed (loc, NULL_RTX,
14780 "language specific tree node");
14781 return 0;
14784 #ifdef ENABLE_CHECKING
14785 /* Otherwise this is a generic code; we should just lists all of
14786 these explicitly. We forgot one. */
14787 gcc_unreachable ();
14788 #else
14789 /* In a release build, we want to degrade gracefully: better to
14790 generate incomplete debugging information than to crash. */
14791 return NULL;
14792 #endif
14795 if (!ret && !list_ret)
14796 return 0;
14798 if (want_address == 2 && !have_address
14799 && (dwarf_version >= 4 || !dwarf_strict))
14801 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14803 expansion_failed (loc, NULL_RTX,
14804 "DWARF address size mismatch");
14805 return 0;
14807 if (ret)
14808 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
14809 else
14810 add_loc_descr_to_each (list_ret,
14811 new_loc_descr (DW_OP_stack_value, 0, 0));
14812 have_address = 1;
14814 /* Show if we can't fill the request for an address. */
14815 if (want_address && !have_address)
14817 expansion_failed (loc, NULL_RTX,
14818 "Want address and only have value");
14819 return 0;
14822 gcc_assert (!ret || !list_ret);
14824 /* If we've got an address and don't want one, dereference. */
14825 if (!want_address && have_address)
14827 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14829 if (size > DWARF2_ADDR_SIZE || size == -1)
14831 expansion_failed (loc, NULL_RTX,
14832 "DWARF address size mismatch");
14833 return 0;
14835 else if (size == DWARF2_ADDR_SIZE)
14836 op = DW_OP_deref;
14837 else
14838 op = DW_OP_deref_size;
14840 if (ret)
14841 add_loc_descr (&ret, new_loc_descr (op, size, 0));
14842 else
14843 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
14845 if (ret)
14846 list_ret = new_loc_list (ret, NULL, NULL, NULL);
14848 return list_ret;
14851 /* Same as above but return only single location expression. */
14852 static dw_loc_descr_ref
14853 loc_descriptor_from_tree (tree loc, int want_address)
14855 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
14856 if (!ret)
14857 return NULL;
14858 if (ret->dw_loc_next)
14860 expansion_failed (loc, NULL_RTX,
14861 "Location list where only loc descriptor needed");
14862 return NULL;
14864 return ret->expr;
14867 /* Given a value, round it up to the lowest multiple of `boundary'
14868 which is not less than the value itself. */
14870 static inline HOST_WIDE_INT
14871 ceiling (HOST_WIDE_INT value, unsigned int boundary)
14873 return (((value + boundary - 1) / boundary) * boundary);
14876 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
14877 pointer to the declared type for the relevant field variable, or return
14878 `integer_type_node' if the given node turns out to be an
14879 ERROR_MARK node. */
14881 static inline tree
14882 field_type (const_tree decl)
14884 tree type;
14886 if (TREE_CODE (decl) == ERROR_MARK)
14887 return integer_type_node;
14889 type = DECL_BIT_FIELD_TYPE (decl);
14890 if (type == NULL_TREE)
14891 type = TREE_TYPE (decl);
14893 return type;
14896 /* Given a pointer to a tree node, return the alignment in bits for
14897 it, or else return BITS_PER_WORD if the node actually turns out to
14898 be an ERROR_MARK node. */
14900 static inline unsigned
14901 simple_type_align_in_bits (const_tree type)
14903 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
14906 static inline unsigned
14907 simple_decl_align_in_bits (const_tree decl)
14909 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
14912 /* Return the result of rounding T up to ALIGN. */
14914 static inline offset_int
14915 round_up_to_align (const offset_int &t, unsigned int align)
14917 return wi::udiv_trunc (t + align - 1, align) * align;
14920 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
14921 lowest addressed byte of the "containing object" for the given FIELD_DECL,
14922 or return 0 if we are unable to determine what that offset is, either
14923 because the argument turns out to be a pointer to an ERROR_MARK node, or
14924 because the offset is actually variable. (We can't handle the latter case
14925 just yet). */
14927 static HOST_WIDE_INT
14928 field_byte_offset (const_tree decl)
14930 offset_int object_offset_in_bits;
14931 offset_int object_offset_in_bytes;
14932 offset_int bitpos_int;
14934 if (TREE_CODE (decl) == ERROR_MARK)
14935 return 0;
14937 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
14939 /* We cannot yet cope with fields whose positions are variable, so
14940 for now, when we see such things, we simply return 0. Someday, we may
14941 be able to handle such cases, but it will be damn difficult. */
14942 if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
14943 return 0;
14945 bitpos_int = wi::to_offset (bit_position (decl));
14947 #ifdef PCC_BITFIELD_TYPE_MATTERS
14948 if (PCC_BITFIELD_TYPE_MATTERS)
14950 tree type;
14951 tree field_size_tree;
14952 offset_int deepest_bitpos;
14953 offset_int field_size_in_bits;
14954 unsigned int type_align_in_bits;
14955 unsigned int decl_align_in_bits;
14956 offset_int type_size_in_bits;
14958 type = field_type (decl);
14959 type_size_in_bits = offset_int_type_size_in_bits (type);
14960 type_align_in_bits = simple_type_align_in_bits (type);
14962 field_size_tree = DECL_SIZE (decl);
14964 /* The size could be unspecified if there was an error, or for
14965 a flexible array member. */
14966 if (!field_size_tree)
14967 field_size_tree = bitsize_zero_node;
14969 /* If the size of the field is not constant, use the type size. */
14970 if (TREE_CODE (field_size_tree) == INTEGER_CST)
14971 field_size_in_bits = wi::to_offset (field_size_tree);
14972 else
14973 field_size_in_bits = type_size_in_bits;
14975 decl_align_in_bits = simple_decl_align_in_bits (decl);
14977 /* The GCC front-end doesn't make any attempt to keep track of the
14978 starting bit offset (relative to the start of the containing
14979 structure type) of the hypothetical "containing object" for a
14980 bit-field. Thus, when computing the byte offset value for the
14981 start of the "containing object" of a bit-field, we must deduce
14982 this information on our own. This can be rather tricky to do in
14983 some cases. For example, handling the following structure type
14984 definition when compiling for an i386/i486 target (which only
14985 aligns long long's to 32-bit boundaries) can be very tricky:
14987 struct S { int field1; long long field2:31; };
14989 Fortunately, there is a simple rule-of-thumb which can be used
14990 in such cases. When compiling for an i386/i486, GCC will
14991 allocate 8 bytes for the structure shown above. It decides to
14992 do this based upon one simple rule for bit-field allocation.
14993 GCC allocates each "containing object" for each bit-field at
14994 the first (i.e. lowest addressed) legitimate alignment boundary
14995 (based upon the required minimum alignment for the declared
14996 type of the field) which it can possibly use, subject to the
14997 condition that there is still enough available space remaining
14998 in the containing object (when allocated at the selected point)
14999 to fully accommodate all of the bits of the bit-field itself.
15001 This simple rule makes it obvious why GCC allocates 8 bytes for
15002 each object of the structure type shown above. When looking
15003 for a place to allocate the "containing object" for `field2',
15004 the compiler simply tries to allocate a 64-bit "containing
15005 object" at each successive 32-bit boundary (starting at zero)
15006 until it finds a place to allocate that 64- bit field such that
15007 at least 31 contiguous (and previously unallocated) bits remain
15008 within that selected 64 bit field. (As it turns out, for the
15009 example above, the compiler finds it is OK to allocate the
15010 "containing object" 64-bit field at bit-offset zero within the
15011 structure type.)
15013 Here we attempt to work backwards from the limited set of facts
15014 we're given, and we try to deduce from those facts, where GCC
15015 must have believed that the containing object started (within
15016 the structure type). The value we deduce is then used (by the
15017 callers of this routine) to generate DW_AT_location and
15018 DW_AT_bit_offset attributes for fields (both bit-fields and, in
15019 the case of DW_AT_location, regular fields as well). */
15021 /* Figure out the bit-distance from the start of the structure to
15022 the "deepest" bit of the bit-field. */
15023 deepest_bitpos = bitpos_int + field_size_in_bits;
15025 /* This is the tricky part. Use some fancy footwork to deduce
15026 where the lowest addressed bit of the containing object must
15027 be. */
15028 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
15030 /* Round up to type_align by default. This works best for
15031 bitfields. */
15032 object_offset_in_bits
15033 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
15035 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
15037 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
15039 /* Round up to decl_align instead. */
15040 object_offset_in_bits
15041 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
15044 else
15045 #endif /* PCC_BITFIELD_TYPE_MATTERS */
15046 object_offset_in_bits = bitpos_int;
15048 object_offset_in_bytes
15049 = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
15050 return object_offset_in_bytes.to_shwi ();
15053 /* The following routines define various Dwarf attributes and any data
15054 associated with them. */
15056 /* Add a location description attribute value to a DIE.
15058 This emits location attributes suitable for whole variables and
15059 whole parameters. Note that the location attributes for struct fields are
15060 generated by the routine `data_member_location_attribute' below. */
15062 static inline void
15063 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
15064 dw_loc_list_ref descr)
15066 if (descr == 0)
15067 return;
15068 if (single_element_loc_list_p (descr))
15069 add_AT_loc (die, attr_kind, descr->expr);
15070 else
15071 add_AT_loc_list (die, attr_kind, descr);
15074 /* Add DW_AT_accessibility attribute to DIE if needed. */
15076 static void
15077 add_accessibility_attribute (dw_die_ref die, tree decl)
15079 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
15080 children, otherwise the default is DW_ACCESS_public. In DWARF2
15081 the default has always been DW_ACCESS_public. */
15082 if (TREE_PROTECTED (decl))
15083 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
15084 else if (TREE_PRIVATE (decl))
15086 if (dwarf_version == 2
15087 || die->die_parent == NULL
15088 || die->die_parent->die_tag != DW_TAG_class_type)
15089 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
15091 else if (dwarf_version > 2
15092 && die->die_parent
15093 && die->die_parent->die_tag == DW_TAG_class_type)
15094 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
15097 /* Attach the specialized form of location attribute used for data members of
15098 struct and union types. In the special case of a FIELD_DECL node which
15099 represents a bit-field, the "offset" part of this special location
15100 descriptor must indicate the distance in bytes from the lowest-addressed
15101 byte of the containing struct or union type to the lowest-addressed byte of
15102 the "containing object" for the bit-field. (See the `field_byte_offset'
15103 function above).
15105 For any given bit-field, the "containing object" is a hypothetical object
15106 (of some integral or enum type) within which the given bit-field lives. The
15107 type of this hypothetical "containing object" is always the same as the
15108 declared type of the individual bit-field itself (for GCC anyway... the
15109 DWARF spec doesn't actually mandate this). Note that it is the size (in
15110 bytes) of the hypothetical "containing object" which will be given in the
15111 DW_AT_byte_size attribute for this bit-field. (See the
15112 `byte_size_attribute' function below.) It is also used when calculating the
15113 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
15114 function below.) */
15116 static void
15117 add_data_member_location_attribute (dw_die_ref die, tree decl)
15119 HOST_WIDE_INT offset;
15120 dw_loc_descr_ref loc_descr = 0;
15122 if (TREE_CODE (decl) == TREE_BINFO)
15124 /* We're working on the TAG_inheritance for a base class. */
15125 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
15127 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
15128 aren't at a fixed offset from all (sub)objects of the same
15129 type. We need to extract the appropriate offset from our
15130 vtable. The following dwarf expression means
15132 BaseAddr = ObAddr + *((*ObAddr) - Offset)
15134 This is specific to the V3 ABI, of course. */
15136 dw_loc_descr_ref tmp;
15138 /* Make a copy of the object address. */
15139 tmp = new_loc_descr (DW_OP_dup, 0, 0);
15140 add_loc_descr (&loc_descr, tmp);
15142 /* Extract the vtable address. */
15143 tmp = new_loc_descr (DW_OP_deref, 0, 0);
15144 add_loc_descr (&loc_descr, tmp);
15146 /* Calculate the address of the offset. */
15147 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
15148 gcc_assert (offset < 0);
15150 tmp = int_loc_descriptor (-offset);
15151 add_loc_descr (&loc_descr, tmp);
15152 tmp = new_loc_descr (DW_OP_minus, 0, 0);
15153 add_loc_descr (&loc_descr, tmp);
15155 /* Extract the offset. */
15156 tmp = new_loc_descr (DW_OP_deref, 0, 0);
15157 add_loc_descr (&loc_descr, tmp);
15159 /* Add it to the object address. */
15160 tmp = new_loc_descr (DW_OP_plus, 0, 0);
15161 add_loc_descr (&loc_descr, tmp);
15163 else
15164 offset = tree_to_shwi (BINFO_OFFSET (decl));
15166 else
15167 offset = field_byte_offset (decl);
15169 if (! loc_descr)
15171 if (dwarf_version > 2)
15173 /* Don't need to output a location expression, just the constant. */
15174 if (offset < 0)
15175 add_AT_int (die, DW_AT_data_member_location, offset);
15176 else
15177 add_AT_unsigned (die, DW_AT_data_member_location, offset);
15178 return;
15180 else
15182 enum dwarf_location_atom op;
15184 /* The DWARF2 standard says that we should assume that the structure
15185 address is already on the stack, so we can specify a structure
15186 field address by using DW_OP_plus_uconst. */
15187 op = DW_OP_plus_uconst;
15188 loc_descr = new_loc_descr (op, offset, 0);
15192 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
15195 /* Writes integer values to dw_vec_const array. */
15197 static void
15198 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
15200 while (size != 0)
15202 *dest++ = val & 0xff;
15203 val >>= 8;
15204 --size;
15208 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
15210 static HOST_WIDE_INT
15211 extract_int (const unsigned char *src, unsigned int size)
15213 HOST_WIDE_INT val = 0;
15215 src += size;
15216 while (size != 0)
15218 val <<= 8;
15219 val |= *--src & 0xff;
15220 --size;
15222 return val;
15225 /* Writes wide_int values to dw_vec_const array. */
15227 static void
15228 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
15230 int i;
15232 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
15234 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
15235 return;
15238 /* We'd have to extend this code to support odd sizes. */
15239 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
15241 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
15243 if (WORDS_BIG_ENDIAN)
15244 for (i = n - 1; i >= 0; i--)
15246 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
15247 dest += sizeof (HOST_WIDE_INT);
15249 else
15250 for (i = 0; i < n; i++)
15252 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
15253 dest += sizeof (HOST_WIDE_INT);
15257 /* Writes floating point values to dw_vec_const array. */
15259 static void
15260 insert_float (const_rtx rtl, unsigned char *array)
15262 REAL_VALUE_TYPE rv;
15263 long val[4];
15264 int i;
15266 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
15267 real_to_target (val, &rv, GET_MODE (rtl));
15269 /* real_to_target puts 32-bit pieces in each long. Pack them. */
15270 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
15272 insert_int (val[i], 4, array);
15273 array += 4;
15277 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
15278 does not have a "location" either in memory or in a register. These
15279 things can arise in GNU C when a constant is passed as an actual parameter
15280 to an inlined function. They can also arise in C++ where declared
15281 constants do not necessarily get memory "homes". */
15283 static bool
15284 add_const_value_attribute (dw_die_ref die, rtx rtl)
15286 switch (GET_CODE (rtl))
15288 case CONST_INT:
15290 HOST_WIDE_INT val = INTVAL (rtl);
15292 if (val < 0)
15293 add_AT_int (die, DW_AT_const_value, val);
15294 else
15295 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
15297 return true;
15299 case CONST_WIDE_INT:
15300 add_AT_wide (die, DW_AT_const_value,
15301 std::make_pair (rtl, GET_MODE (rtl)));
15302 return true;
15304 case CONST_DOUBLE:
15305 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
15306 floating-point constant. A CONST_DOUBLE is used whenever the
15307 constant requires more than one word in order to be adequately
15308 represented. */
15310 machine_mode mode = GET_MODE (rtl);
15312 if (TARGET_SUPPORTS_WIDE_INT == 0 && !SCALAR_FLOAT_MODE_P (mode))
15313 add_AT_double (die, DW_AT_const_value,
15314 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
15315 else
15317 unsigned int length = GET_MODE_SIZE (mode);
15318 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15320 insert_float (rtl, array);
15321 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
15324 return true;
15326 case CONST_VECTOR:
15328 machine_mode mode = GET_MODE (rtl);
15329 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
15330 unsigned int length = CONST_VECTOR_NUNITS (rtl);
15331 unsigned char *array
15332 = ggc_vec_alloc<unsigned char> (length * elt_size);
15333 unsigned int i;
15334 unsigned char *p;
15335 machine_mode imode = GET_MODE_INNER (mode);
15337 switch (GET_MODE_CLASS (mode))
15339 case MODE_VECTOR_INT:
15340 for (i = 0, p = array; i < length; i++, p += elt_size)
15342 rtx elt = CONST_VECTOR_ELT (rtl, i);
15343 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
15345 break;
15347 case MODE_VECTOR_FLOAT:
15348 for (i = 0, p = array; i < length; i++, p += elt_size)
15350 rtx elt = CONST_VECTOR_ELT (rtl, i);
15351 insert_float (elt, p);
15353 break;
15355 default:
15356 gcc_unreachable ();
15359 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
15361 return true;
15363 case CONST_STRING:
15364 if (dwarf_version >= 4 || !dwarf_strict)
15366 dw_loc_descr_ref loc_result;
15367 resolve_one_addr (&rtl);
15368 rtl_addr:
15369 loc_result = new_addr_loc_descr (rtl, dtprel_false);
15370 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
15371 add_AT_loc (die, DW_AT_location, loc_result);
15372 vec_safe_push (used_rtx_array, rtl);
15373 return true;
15375 return false;
15377 case CONST:
15378 if (CONSTANT_P (XEXP (rtl, 0)))
15379 return add_const_value_attribute (die, XEXP (rtl, 0));
15380 /* FALLTHROUGH */
15381 case SYMBOL_REF:
15382 if (!const_ok_for_output (rtl))
15383 return false;
15384 case LABEL_REF:
15385 if (dwarf_version >= 4 || !dwarf_strict)
15386 goto rtl_addr;
15387 return false;
15389 case PLUS:
15390 /* In cases where an inlined instance of an inline function is passed
15391 the address of an `auto' variable (which is local to the caller) we
15392 can get a situation where the DECL_RTL of the artificial local
15393 variable (for the inlining) which acts as a stand-in for the
15394 corresponding formal parameter (of the inline function) will look
15395 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
15396 exactly a compile-time constant expression, but it isn't the address
15397 of the (artificial) local variable either. Rather, it represents the
15398 *value* which the artificial local variable always has during its
15399 lifetime. We currently have no way to represent such quasi-constant
15400 values in Dwarf, so for now we just punt and generate nothing. */
15401 return false;
15403 case HIGH:
15404 case CONST_FIXED:
15405 return false;
15407 case MEM:
15408 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
15409 && MEM_READONLY_P (rtl)
15410 && GET_MODE (rtl) == BLKmode)
15412 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
15413 return true;
15415 return false;
15417 default:
15418 /* No other kinds of rtx should be possible here. */
15419 gcc_unreachable ();
15421 return false;
15424 /* Determine whether the evaluation of EXPR references any variables
15425 or functions which aren't otherwise used (and therefore may not be
15426 output). */
15427 static tree
15428 reference_to_unused (tree * tp, int * walk_subtrees,
15429 void * data ATTRIBUTE_UNUSED)
15431 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
15432 *walk_subtrees = 0;
15434 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
15435 && ! TREE_ASM_WRITTEN (*tp))
15436 return *tp;
15437 /* ??? The C++ FE emits debug information for using decls, so
15438 putting gcc_unreachable here falls over. See PR31899. For now
15439 be conservative. */
15440 else if (!symtab->global_info_ready
15441 && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
15442 return *tp;
15443 else if (TREE_CODE (*tp) == VAR_DECL)
15445 varpool_node *node = varpool_node::get (*tp);
15446 if (!node || !node->definition)
15447 return *tp;
15449 else if (TREE_CODE (*tp) == FUNCTION_DECL
15450 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
15452 /* The call graph machinery must have finished analyzing,
15453 optimizing and gimplifying the CU by now.
15454 So if *TP has no call graph node associated
15455 to it, it means *TP will not be emitted. */
15456 if (!cgraph_node::get (*tp))
15457 return *tp;
15459 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
15460 return *tp;
15462 return NULL_TREE;
15465 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
15466 for use in a later add_const_value_attribute call. */
15468 static rtx
15469 rtl_for_decl_init (tree init, tree type)
15471 rtx rtl = NULL_RTX;
15473 STRIP_NOPS (init);
15475 /* If a variable is initialized with a string constant without embedded
15476 zeros, build CONST_STRING. */
15477 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
15479 tree enttype = TREE_TYPE (type);
15480 tree domain = TYPE_DOMAIN (type);
15481 machine_mode mode = TYPE_MODE (enttype);
15483 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
15484 && domain
15485 && integer_zerop (TYPE_MIN_VALUE (domain))
15486 && compare_tree_int (TYPE_MAX_VALUE (domain),
15487 TREE_STRING_LENGTH (init) - 1) == 0
15488 && ((size_t) TREE_STRING_LENGTH (init)
15489 == strlen (TREE_STRING_POINTER (init)) + 1))
15491 rtl = gen_rtx_CONST_STRING (VOIDmode,
15492 ggc_strdup (TREE_STRING_POINTER (init)));
15493 rtl = gen_rtx_MEM (BLKmode, rtl);
15494 MEM_READONLY_P (rtl) = 1;
15497 /* Other aggregates, and complex values, could be represented using
15498 CONCAT: FIXME! */
15499 else if (AGGREGATE_TYPE_P (type)
15500 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
15501 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
15502 || TREE_CODE (type) == COMPLEX_TYPE)
15504 /* Vectors only work if their mode is supported by the target.
15505 FIXME: generic vectors ought to work too. */
15506 else if (TREE_CODE (type) == VECTOR_TYPE
15507 && !VECTOR_MODE_P (TYPE_MODE (type)))
15509 /* If the initializer is something that we know will expand into an
15510 immediate RTL constant, expand it now. We must be careful not to
15511 reference variables which won't be output. */
15512 else if (initializer_constant_valid_p (init, type)
15513 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
15515 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
15516 possible. */
15517 if (TREE_CODE (type) == VECTOR_TYPE)
15518 switch (TREE_CODE (init))
15520 case VECTOR_CST:
15521 break;
15522 case CONSTRUCTOR:
15523 if (TREE_CONSTANT (init))
15525 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
15526 bool constant_p = true;
15527 tree value;
15528 unsigned HOST_WIDE_INT ix;
15530 /* Even when ctor is constant, it might contain non-*_CST
15531 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
15532 belong into VECTOR_CST nodes. */
15533 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
15534 if (!CONSTANT_CLASS_P (value))
15536 constant_p = false;
15537 break;
15540 if (constant_p)
15542 init = build_vector_from_ctor (type, elts);
15543 break;
15546 /* FALLTHRU */
15548 default:
15549 return NULL;
15552 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
15554 /* If expand_expr returns a MEM, it wasn't immediate. */
15555 gcc_assert (!rtl || !MEM_P (rtl));
15558 return rtl;
15561 /* Generate RTL for the variable DECL to represent its location. */
15563 static rtx
15564 rtl_for_decl_location (tree decl)
15566 rtx rtl;
15568 /* Here we have to decide where we are going to say the parameter "lives"
15569 (as far as the debugger is concerned). We only have a couple of
15570 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
15572 DECL_RTL normally indicates where the parameter lives during most of the
15573 activation of the function. If optimization is enabled however, this
15574 could be either NULL or else a pseudo-reg. Both of those cases indicate
15575 that the parameter doesn't really live anywhere (as far as the code
15576 generation parts of GCC are concerned) during most of the function's
15577 activation. That will happen (for example) if the parameter is never
15578 referenced within the function.
15580 We could just generate a location descriptor here for all non-NULL
15581 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
15582 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
15583 where DECL_RTL is NULL or is a pseudo-reg.
15585 Note however that we can only get away with using DECL_INCOMING_RTL as
15586 a backup substitute for DECL_RTL in certain limited cases. In cases
15587 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
15588 we can be sure that the parameter was passed using the same type as it is
15589 declared to have within the function, and that its DECL_INCOMING_RTL
15590 points us to a place where a value of that type is passed.
15592 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
15593 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
15594 because in these cases DECL_INCOMING_RTL points us to a value of some
15595 type which is *different* from the type of the parameter itself. Thus,
15596 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
15597 such cases, the debugger would end up (for example) trying to fetch a
15598 `float' from a place which actually contains the first part of a
15599 `double'. That would lead to really incorrect and confusing
15600 output at debug-time.
15602 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
15603 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
15604 are a couple of exceptions however. On little-endian machines we can
15605 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
15606 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
15607 an integral type that is smaller than TREE_TYPE (decl). These cases arise
15608 when (on a little-endian machine) a non-prototyped function has a
15609 parameter declared to be of type `short' or `char'. In such cases,
15610 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
15611 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
15612 passed `int' value. If the debugger then uses that address to fetch
15613 a `short' or a `char' (on a little-endian machine) the result will be
15614 the correct data, so we allow for such exceptional cases below.
15616 Note that our goal here is to describe the place where the given formal
15617 parameter lives during most of the function's activation (i.e. between the
15618 end of the prologue and the start of the epilogue). We'll do that as best
15619 as we can. Note however that if the given formal parameter is modified
15620 sometime during the execution of the function, then a stack backtrace (at
15621 debug-time) will show the function as having been called with the *new*
15622 value rather than the value which was originally passed in. This happens
15623 rarely enough that it is not a major problem, but it *is* a problem, and
15624 I'd like to fix it.
15626 A future version of dwarf2out.c may generate two additional attributes for
15627 any given DW_TAG_formal_parameter DIE which will describe the "passed
15628 type" and the "passed location" for the given formal parameter in addition
15629 to the attributes we now generate to indicate the "declared type" and the
15630 "active location" for each parameter. This additional set of attributes
15631 could be used by debuggers for stack backtraces. Separately, note that
15632 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
15633 This happens (for example) for inlined-instances of inline function formal
15634 parameters which are never referenced. This really shouldn't be
15635 happening. All PARM_DECL nodes should get valid non-NULL
15636 DECL_INCOMING_RTL values. FIXME. */
15638 /* Use DECL_RTL as the "location" unless we find something better. */
15639 rtl = DECL_RTL_IF_SET (decl);
15641 /* When generating abstract instances, ignore everything except
15642 constants, symbols living in memory, and symbols living in
15643 fixed registers. */
15644 if (! reload_completed)
15646 if (rtl
15647 && (CONSTANT_P (rtl)
15648 || (MEM_P (rtl)
15649 && CONSTANT_P (XEXP (rtl, 0)))
15650 || (REG_P (rtl)
15651 && TREE_CODE (decl) == VAR_DECL
15652 && TREE_STATIC (decl))))
15654 rtl = targetm.delegitimize_address (rtl);
15655 return rtl;
15657 rtl = NULL_RTX;
15659 else if (TREE_CODE (decl) == PARM_DECL)
15661 if (rtl == NULL_RTX
15662 || is_pseudo_reg (rtl)
15663 || (MEM_P (rtl)
15664 && is_pseudo_reg (XEXP (rtl, 0))
15665 && DECL_INCOMING_RTL (decl)
15666 && MEM_P (DECL_INCOMING_RTL (decl))
15667 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
15669 tree declared_type = TREE_TYPE (decl);
15670 tree passed_type = DECL_ARG_TYPE (decl);
15671 machine_mode dmode = TYPE_MODE (declared_type);
15672 machine_mode pmode = TYPE_MODE (passed_type);
15674 /* This decl represents a formal parameter which was optimized out.
15675 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
15676 all cases where (rtl == NULL_RTX) just below. */
15677 if (dmode == pmode)
15678 rtl = DECL_INCOMING_RTL (decl);
15679 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
15680 && SCALAR_INT_MODE_P (dmode)
15681 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
15682 && DECL_INCOMING_RTL (decl))
15684 rtx inc = DECL_INCOMING_RTL (decl);
15685 if (REG_P (inc))
15686 rtl = inc;
15687 else if (MEM_P (inc))
15689 if (BYTES_BIG_ENDIAN)
15690 rtl = adjust_address_nv (inc, dmode,
15691 GET_MODE_SIZE (pmode)
15692 - GET_MODE_SIZE (dmode));
15693 else
15694 rtl = inc;
15699 /* If the parm was passed in registers, but lives on the stack, then
15700 make a big endian correction if the mode of the type of the
15701 parameter is not the same as the mode of the rtl. */
15702 /* ??? This is the same series of checks that are made in dbxout.c before
15703 we reach the big endian correction code there. It isn't clear if all
15704 of these checks are necessary here, but keeping them all is the safe
15705 thing to do. */
15706 else if (MEM_P (rtl)
15707 && XEXP (rtl, 0) != const0_rtx
15708 && ! CONSTANT_P (XEXP (rtl, 0))
15709 /* Not passed in memory. */
15710 && !MEM_P (DECL_INCOMING_RTL (decl))
15711 /* Not passed by invisible reference. */
15712 && (!REG_P (XEXP (rtl, 0))
15713 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
15714 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
15715 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
15716 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
15717 #endif
15719 /* Big endian correction check. */
15720 && BYTES_BIG_ENDIAN
15721 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
15722 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
15723 < UNITS_PER_WORD))
15725 machine_mode addr_mode = get_address_mode (rtl);
15726 int offset = (UNITS_PER_WORD
15727 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
15729 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15730 plus_constant (addr_mode, XEXP (rtl, 0), offset));
15733 else if (TREE_CODE (decl) == VAR_DECL
15734 && rtl
15735 && MEM_P (rtl)
15736 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
15737 && BYTES_BIG_ENDIAN)
15739 machine_mode addr_mode = get_address_mode (rtl);
15740 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
15741 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
15743 /* If a variable is declared "register" yet is smaller than
15744 a register, then if we store the variable to memory, it
15745 looks like we're storing a register-sized value, when in
15746 fact we are not. We need to adjust the offset of the
15747 storage location to reflect the actual value's bytes,
15748 else gdb will not be able to display it. */
15749 if (rsize > dsize)
15750 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15751 plus_constant (addr_mode, XEXP (rtl, 0),
15752 rsize - dsize));
15755 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
15756 and will have been substituted directly into all expressions that use it.
15757 C does not have such a concept, but C++ and other languages do. */
15758 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
15759 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
15761 if (rtl)
15762 rtl = targetm.delegitimize_address (rtl);
15764 /* If we don't look past the constant pool, we risk emitting a
15765 reference to a constant pool entry that isn't referenced from
15766 code, and thus is not emitted. */
15767 if (rtl)
15768 rtl = avoid_constant_pool_reference (rtl);
15770 /* Try harder to get a rtl. If this symbol ends up not being emitted
15771 in the current CU, resolve_addr will remove the expression referencing
15772 it. */
15773 if (rtl == NULL_RTX
15774 && TREE_CODE (decl) == VAR_DECL
15775 && !DECL_EXTERNAL (decl)
15776 && TREE_STATIC (decl)
15777 && DECL_NAME (decl)
15778 && !DECL_HARD_REGISTER (decl)
15779 && DECL_MODE (decl) != VOIDmode)
15781 rtl = make_decl_rtl_for_debug (decl);
15782 if (!MEM_P (rtl)
15783 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
15784 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
15785 rtl = NULL_RTX;
15788 return rtl;
15791 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
15792 returned. If so, the decl for the COMMON block is returned, and the
15793 value is the offset into the common block for the symbol. */
15795 static tree
15796 fortran_common (tree decl, HOST_WIDE_INT *value)
15798 tree val_expr, cvar;
15799 machine_mode mode;
15800 HOST_WIDE_INT bitsize, bitpos;
15801 tree offset;
15802 int unsignedp, volatilep = 0;
15804 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
15805 it does not have a value (the offset into the common area), or if it
15806 is thread local (as opposed to global) then it isn't common, and shouldn't
15807 be handled as such. */
15808 if (TREE_CODE (decl) != VAR_DECL
15809 || !TREE_STATIC (decl)
15810 || !DECL_HAS_VALUE_EXPR_P (decl)
15811 || !is_fortran ())
15812 return NULL_TREE;
15814 val_expr = DECL_VALUE_EXPR (decl);
15815 if (TREE_CODE (val_expr) != COMPONENT_REF)
15816 return NULL_TREE;
15818 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
15819 &mode, &unsignedp, &volatilep, true);
15821 if (cvar == NULL_TREE
15822 || TREE_CODE (cvar) != VAR_DECL
15823 || DECL_ARTIFICIAL (cvar)
15824 || !TREE_PUBLIC (cvar))
15825 return NULL_TREE;
15827 *value = 0;
15828 if (offset != NULL)
15830 if (!tree_fits_shwi_p (offset))
15831 return NULL_TREE;
15832 *value = tree_to_shwi (offset);
15834 if (bitpos != 0)
15835 *value += bitpos / BITS_PER_UNIT;
15837 return cvar;
15840 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
15841 data attribute for a variable or a parameter. We generate the
15842 DW_AT_const_value attribute only in those cases where the given variable
15843 or parameter does not have a true "location" either in memory or in a
15844 register. This can happen (for example) when a constant is passed as an
15845 actual argument in a call to an inline function. (It's possible that
15846 these things can crop up in other ways also.) Note that one type of
15847 constant value which can be passed into an inlined function is a constant
15848 pointer. This can happen for example if an actual argument in an inlined
15849 function call evaluates to a compile-time constant address.
15851 CACHE_P is true if it is worth caching the location list for DECL,
15852 so that future calls can reuse it rather than regenerate it from scratch.
15853 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
15854 since we will need to refer to them each time the function is inlined. */
15856 static bool
15857 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
15858 enum dwarf_attribute attr)
15860 rtx rtl;
15861 dw_loc_list_ref list;
15862 var_loc_list *loc_list;
15863 cached_dw_loc_list *cache;
15865 if (TREE_CODE (decl) == ERROR_MARK)
15866 return false;
15868 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
15869 || TREE_CODE (decl) == RESULT_DECL);
15871 /* Try to get some constant RTL for this decl, and use that as the value of
15872 the location. */
15874 rtl = rtl_for_decl_location (decl);
15875 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15876 && add_const_value_attribute (die, rtl))
15877 return true;
15879 /* See if we have single element location list that is equivalent to
15880 a constant value. That way we are better to use add_const_value_attribute
15881 rather than expanding constant value equivalent. */
15882 loc_list = lookup_decl_loc (decl);
15883 if (loc_list
15884 && loc_list->first
15885 && loc_list->first->next == NULL
15886 && NOTE_P (loc_list->first->loc)
15887 && NOTE_VAR_LOCATION (loc_list->first->loc)
15888 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
15890 struct var_loc_node *node;
15892 node = loc_list->first;
15893 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
15894 if (GET_CODE (rtl) == EXPR_LIST)
15895 rtl = XEXP (rtl, 0);
15896 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15897 && add_const_value_attribute (die, rtl))
15898 return true;
15900 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
15901 list several times. See if we've already cached the contents. */
15902 list = NULL;
15903 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
15904 cache_p = false;
15905 if (cache_p)
15907 cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
15908 if (cache)
15909 list = cache->loc_list;
15911 if (list == NULL)
15913 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
15914 /* It is usually worth caching this result if the decl is from
15915 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
15916 if (cache_p && list && list->dw_loc_next)
15918 cached_dw_loc_list **slot
15919 = cached_dw_loc_list_table->find_slot_with_hash (decl,
15920 DECL_UID (decl),
15921 INSERT);
15922 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
15923 cache->decl_id = DECL_UID (decl);
15924 cache->loc_list = list;
15925 *slot = cache;
15928 if (list)
15930 add_AT_location_description (die, attr, list);
15931 return true;
15933 /* None of that worked, so it must not really have a location;
15934 try adding a constant value attribute from the DECL_INITIAL. */
15935 return tree_add_const_value_attribute_for_decl (die, decl);
15938 /* Add VARIABLE and DIE into deferred locations list. */
15940 static void
15941 defer_location (tree variable, dw_die_ref die)
15943 deferred_locations entry;
15944 entry.variable = variable;
15945 entry.die = die;
15946 vec_safe_push (deferred_locations_list, entry);
15949 /* Helper function for tree_add_const_value_attribute. Natively encode
15950 initializer INIT into an array. Return true if successful. */
15952 static bool
15953 native_encode_initializer (tree init, unsigned char *array, int size)
15955 tree type;
15957 if (init == NULL_TREE)
15958 return false;
15960 STRIP_NOPS (init);
15961 switch (TREE_CODE (init))
15963 case STRING_CST:
15964 type = TREE_TYPE (init);
15965 if (TREE_CODE (type) == ARRAY_TYPE)
15967 tree enttype = TREE_TYPE (type);
15968 machine_mode mode = TYPE_MODE (enttype);
15970 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
15971 return false;
15972 if (int_size_in_bytes (type) != size)
15973 return false;
15974 if (size > TREE_STRING_LENGTH (init))
15976 memcpy (array, TREE_STRING_POINTER (init),
15977 TREE_STRING_LENGTH (init));
15978 memset (array + TREE_STRING_LENGTH (init),
15979 '\0', size - TREE_STRING_LENGTH (init));
15981 else
15982 memcpy (array, TREE_STRING_POINTER (init), size);
15983 return true;
15985 return false;
15986 case CONSTRUCTOR:
15987 type = TREE_TYPE (init);
15988 if (int_size_in_bytes (type) != size)
15989 return false;
15990 if (TREE_CODE (type) == ARRAY_TYPE)
15992 HOST_WIDE_INT min_index;
15993 unsigned HOST_WIDE_INT cnt;
15994 int curpos = 0, fieldsize;
15995 constructor_elt *ce;
15997 if (TYPE_DOMAIN (type) == NULL_TREE
15998 || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
15999 return false;
16001 fieldsize = int_size_in_bytes (TREE_TYPE (type));
16002 if (fieldsize <= 0)
16003 return false;
16005 min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
16006 memset (array, '\0', size);
16007 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
16009 tree val = ce->value;
16010 tree index = ce->index;
16011 int pos = curpos;
16012 if (index && TREE_CODE (index) == RANGE_EXPR)
16013 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
16014 * fieldsize;
16015 else if (index)
16016 pos = (tree_to_shwi (index) - min_index) * fieldsize;
16018 if (val)
16020 STRIP_NOPS (val);
16021 if (!native_encode_initializer (val, array + pos, fieldsize))
16022 return false;
16024 curpos = pos + fieldsize;
16025 if (index && TREE_CODE (index) == RANGE_EXPR)
16027 int count = tree_to_shwi (TREE_OPERAND (index, 1))
16028 - tree_to_shwi (TREE_OPERAND (index, 0));
16029 while (count-- > 0)
16031 if (val)
16032 memcpy (array + curpos, array + pos, fieldsize);
16033 curpos += fieldsize;
16036 gcc_assert (curpos <= size);
16038 return true;
16040 else if (TREE_CODE (type) == RECORD_TYPE
16041 || TREE_CODE (type) == UNION_TYPE)
16043 tree field = NULL_TREE;
16044 unsigned HOST_WIDE_INT cnt;
16045 constructor_elt *ce;
16047 if (int_size_in_bytes (type) != size)
16048 return false;
16050 if (TREE_CODE (type) == RECORD_TYPE)
16051 field = TYPE_FIELDS (type);
16053 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
16055 tree val = ce->value;
16056 int pos, fieldsize;
16058 if (ce->index != 0)
16059 field = ce->index;
16061 if (val)
16062 STRIP_NOPS (val);
16064 if (field == NULL_TREE || DECL_BIT_FIELD (field))
16065 return false;
16067 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
16068 && TYPE_DOMAIN (TREE_TYPE (field))
16069 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
16070 return false;
16071 else if (DECL_SIZE_UNIT (field) == NULL_TREE
16072 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
16073 return false;
16074 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
16075 pos = int_byte_position (field);
16076 gcc_assert (pos + fieldsize <= size);
16077 if (val
16078 && !native_encode_initializer (val, array + pos, fieldsize))
16079 return false;
16081 return true;
16083 return false;
16084 case VIEW_CONVERT_EXPR:
16085 case NON_LVALUE_EXPR:
16086 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
16087 default:
16088 return native_encode_expr (init, array, size) == size;
16092 /* Attach a DW_AT_const_value attribute to DIE. The value of the
16093 attribute is the const value T. */
16095 static bool
16096 tree_add_const_value_attribute (dw_die_ref die, tree t)
16098 tree init;
16099 tree type = TREE_TYPE (t);
16100 rtx rtl;
16102 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
16103 return false;
16105 init = t;
16106 gcc_assert (!DECL_P (init));
16108 rtl = rtl_for_decl_init (init, type);
16109 if (rtl)
16110 return add_const_value_attribute (die, rtl);
16111 /* If the host and target are sane, try harder. */
16112 else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
16113 && initializer_constant_valid_p (init, type))
16115 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
16116 if (size > 0 && (int) size == size)
16118 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
16120 if (native_encode_initializer (init, array, size))
16122 add_AT_vec (die, DW_AT_const_value, size, 1, array);
16123 return true;
16125 ggc_free (array);
16128 return false;
16131 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
16132 attribute is the const value of T, where T is an integral constant
16133 variable with static storage duration
16134 (so it can't be a PARM_DECL or a RESULT_DECL). */
16136 static bool
16137 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
16140 if (!decl
16141 || (TREE_CODE (decl) != VAR_DECL
16142 && TREE_CODE (decl) != CONST_DECL)
16143 || (TREE_CODE (decl) == VAR_DECL
16144 && !TREE_STATIC (decl)))
16145 return false;
16147 if (TREE_READONLY (decl)
16148 && ! TREE_THIS_VOLATILE (decl)
16149 && DECL_INITIAL (decl))
16150 /* OK */;
16151 else
16152 return false;
16154 /* Don't add DW_AT_const_value if abstract origin already has one. */
16155 if (get_AT (var_die, DW_AT_const_value))
16156 return false;
16158 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
16161 /* Convert the CFI instructions for the current function into a
16162 location list. This is used for DW_AT_frame_base when we targeting
16163 a dwarf2 consumer that does not support the dwarf3
16164 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
16165 expressions. */
16167 static dw_loc_list_ref
16168 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
16170 int ix;
16171 dw_fde_ref fde;
16172 dw_loc_list_ref list, *list_tail;
16173 dw_cfi_ref cfi;
16174 dw_cfa_location last_cfa, next_cfa;
16175 const char *start_label, *last_label, *section;
16176 dw_cfa_location remember;
16178 fde = cfun->fde;
16179 gcc_assert (fde != NULL);
16181 section = secname_for_decl (current_function_decl);
16182 list_tail = &list;
16183 list = NULL;
16185 memset (&next_cfa, 0, sizeof (next_cfa));
16186 next_cfa.reg = INVALID_REGNUM;
16187 remember = next_cfa;
16189 start_label = fde->dw_fde_begin;
16191 /* ??? Bald assumption that the CIE opcode list does not contain
16192 advance opcodes. */
16193 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
16194 lookup_cfa_1 (cfi, &next_cfa, &remember);
16196 last_cfa = next_cfa;
16197 last_label = start_label;
16199 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
16201 /* If the first partition contained no CFI adjustments, the
16202 CIE opcodes apply to the whole first partition. */
16203 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16204 fde->dw_fde_begin, fde->dw_fde_end, section);
16205 list_tail =&(*list_tail)->dw_loc_next;
16206 start_label = last_label = fde->dw_fde_second_begin;
16209 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
16211 switch (cfi->dw_cfi_opc)
16213 case DW_CFA_set_loc:
16214 case DW_CFA_advance_loc1:
16215 case DW_CFA_advance_loc2:
16216 case DW_CFA_advance_loc4:
16217 if (!cfa_equal_p (&last_cfa, &next_cfa))
16219 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16220 start_label, last_label, section);
16222 list_tail = &(*list_tail)->dw_loc_next;
16223 last_cfa = next_cfa;
16224 start_label = last_label;
16226 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
16227 break;
16229 case DW_CFA_advance_loc:
16230 /* The encoding is complex enough that we should never emit this. */
16231 gcc_unreachable ();
16233 default:
16234 lookup_cfa_1 (cfi, &next_cfa, &remember);
16235 break;
16237 if (ix + 1 == fde->dw_fde_switch_cfi_index)
16239 if (!cfa_equal_p (&last_cfa, &next_cfa))
16241 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16242 start_label, last_label, section);
16244 list_tail = &(*list_tail)->dw_loc_next;
16245 last_cfa = next_cfa;
16246 start_label = last_label;
16248 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16249 start_label, fde->dw_fde_end, section);
16250 list_tail = &(*list_tail)->dw_loc_next;
16251 start_label = last_label = fde->dw_fde_second_begin;
16255 if (!cfa_equal_p (&last_cfa, &next_cfa))
16257 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16258 start_label, last_label, section);
16259 list_tail = &(*list_tail)->dw_loc_next;
16260 start_label = last_label;
16263 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
16264 start_label,
16265 fde->dw_fde_second_begin
16266 ? fde->dw_fde_second_end : fde->dw_fde_end,
16267 section);
16269 if (list && list->dw_loc_next)
16270 gen_llsym (list);
16272 return list;
16275 /* Compute a displacement from the "steady-state frame pointer" to the
16276 frame base (often the same as the CFA), and store it in
16277 frame_pointer_fb_offset. OFFSET is added to the displacement
16278 before the latter is negated. */
16280 static void
16281 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
16283 rtx reg, elim;
16285 #ifdef FRAME_POINTER_CFA_OFFSET
16286 reg = frame_pointer_rtx;
16287 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
16288 #else
16289 reg = arg_pointer_rtx;
16290 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
16291 #endif
16293 elim = (ira_use_lra_p
16294 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
16295 : eliminate_regs (reg, VOIDmode, NULL_RTX));
16296 if (GET_CODE (elim) == PLUS)
16298 offset += INTVAL (XEXP (elim, 1));
16299 elim = XEXP (elim, 0);
16302 frame_pointer_fb_offset = -offset;
16304 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
16305 in which to eliminate. This is because it's stack pointer isn't
16306 directly accessible as a register within the ISA. To work around
16307 this, assume that while we cannot provide a proper value for
16308 frame_pointer_fb_offset, we won't need one either. */
16309 frame_pointer_fb_offset_valid
16310 = ((SUPPORTS_STACK_ALIGNMENT
16311 && (elim == hard_frame_pointer_rtx
16312 || elim == stack_pointer_rtx))
16313 || elim == (frame_pointer_needed
16314 ? hard_frame_pointer_rtx
16315 : stack_pointer_rtx));
16318 /* Generate a DW_AT_name attribute given some string value to be included as
16319 the value of the attribute. */
16321 static void
16322 add_name_attribute (dw_die_ref die, const char *name_string)
16324 if (name_string != NULL && *name_string != 0)
16326 if (demangle_name_func)
16327 name_string = (*demangle_name_func) (name_string);
16329 add_AT_string (die, DW_AT_name, name_string);
16333 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
16334 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
16335 of TYPE accordingly.
16337 ??? This is a temporary measure until after we're able to generate
16338 regular DWARF for the complex Ada type system. */
16340 static void
16341 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
16342 dw_die_ref context_die)
16344 tree dtype;
16345 dw_die_ref dtype_die;
16347 if (!lang_hooks.types.descriptive_type)
16348 return;
16350 dtype = lang_hooks.types.descriptive_type (type);
16351 if (!dtype)
16352 return;
16354 dtype_die = lookup_type_die (dtype);
16355 if (!dtype_die)
16357 gen_type_die (dtype, context_die);
16358 dtype_die = lookup_type_die (dtype);
16359 gcc_assert (dtype_die);
16362 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
16365 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
16367 static const char *
16368 comp_dir_string (void)
16370 const char *wd;
16371 char *wd1;
16372 static const char *cached_wd = NULL;
16374 if (cached_wd != NULL)
16375 return cached_wd;
16377 wd = get_src_pwd ();
16378 if (wd == NULL)
16379 return NULL;
16381 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
16383 int wdlen;
16385 wdlen = strlen (wd);
16386 wd1 = ggc_vec_alloc<char> (wdlen + 2);
16387 strcpy (wd1, wd);
16388 wd1 [wdlen] = DIR_SEPARATOR;
16389 wd1 [wdlen + 1] = 0;
16390 wd = wd1;
16393 cached_wd = remap_debug_filename (wd);
16394 return cached_wd;
16397 /* Generate a DW_AT_comp_dir attribute for DIE. */
16399 static void
16400 add_comp_dir_attribute (dw_die_ref die)
16402 const char * wd = comp_dir_string ();
16403 if (wd != NULL)
16404 add_AT_string (die, DW_AT_comp_dir, wd);
16407 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
16408 default. */
16410 static int
16411 lower_bound_default (void)
16413 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
16415 case DW_LANG_C:
16416 case DW_LANG_C89:
16417 case DW_LANG_C99:
16418 case DW_LANG_C11:
16419 case DW_LANG_C_plus_plus:
16420 case DW_LANG_C_plus_plus_11:
16421 case DW_LANG_C_plus_plus_14:
16422 case DW_LANG_ObjC:
16423 case DW_LANG_ObjC_plus_plus:
16424 case DW_LANG_Java:
16425 return 0;
16426 case DW_LANG_Fortran77:
16427 case DW_LANG_Fortran90:
16428 case DW_LANG_Fortran95:
16429 return 1;
16430 case DW_LANG_UPC:
16431 case DW_LANG_D:
16432 case DW_LANG_Python:
16433 return dwarf_version >= 4 ? 0 : -1;
16434 case DW_LANG_Ada95:
16435 case DW_LANG_Ada83:
16436 case DW_LANG_Cobol74:
16437 case DW_LANG_Cobol85:
16438 case DW_LANG_Pascal83:
16439 case DW_LANG_Modula2:
16440 case DW_LANG_PLI:
16441 return dwarf_version >= 4 ? 1 : -1;
16442 default:
16443 return -1;
16447 /* Given a tree node describing an array bound (either lower or upper) output
16448 a representation for that bound. */
16450 static void
16451 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
16453 switch (TREE_CODE (bound))
16455 case ERROR_MARK:
16456 return;
16458 /* All fixed-bounds are represented by INTEGER_CST nodes. */
16459 case INTEGER_CST:
16461 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
16462 int dflt;
16464 /* Use the default if possible. */
16465 if (bound_attr == DW_AT_lower_bound
16466 && tree_fits_shwi_p (bound)
16467 && (dflt = lower_bound_default ()) != -1
16468 && tree_to_shwi (bound) == dflt)
16471 /* If HOST_WIDE_INT is big enough then represent the bound as
16472 a constant value. We need to choose a form based on
16473 whether the type is signed or unsigned. We cannot just
16474 call add_AT_unsigned if the value itself is positive
16475 (add_AT_unsigned might add the unsigned value encoded as
16476 DW_FORM_data[1248]). Some DWARF consumers will lookup the
16477 bounds type and then sign extend any unsigned values found
16478 for signed types. This is needed only for
16479 DW_AT_{lower,upper}_bound, since for most other attributes,
16480 consumers will treat DW_FORM_data[1248] as unsigned values,
16481 regardless of the underlying type. */
16482 else if (prec <= HOST_BITS_PER_WIDE_INT
16483 || tree_fits_uhwi_p (bound))
16485 if (TYPE_UNSIGNED (TREE_TYPE (bound)))
16486 add_AT_unsigned (subrange_die, bound_attr,
16487 TREE_INT_CST_LOW (bound));
16488 else
16489 add_AT_int (subrange_die, bound_attr, TREE_INT_CST_LOW (bound));
16491 else
16492 /* Otherwise represent the bound as an unsigned value with
16493 the precision of its type. The precision and signedness
16494 of the type will be necessary to re-interpret it
16495 unambiguously. */
16496 add_AT_wide (subrange_die, bound_attr, bound);
16498 break;
16500 CASE_CONVERT:
16501 case VIEW_CONVERT_EXPR:
16502 add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
16503 break;
16505 case SAVE_EXPR:
16506 break;
16508 case VAR_DECL:
16509 case PARM_DECL:
16510 case RESULT_DECL:
16512 dw_die_ref decl_die = lookup_decl_die (bound);
16514 /* ??? Can this happen, or should the variable have been bound
16515 first? Probably it can, since I imagine that we try to create
16516 the types of parameters in the order in which they exist in
16517 the list, and won't have created a forward reference to a
16518 later parameter. */
16519 if (decl_die != NULL)
16521 add_AT_die_ref (subrange_die, bound_attr, decl_die);
16522 break;
16525 /* FALLTHRU */
16527 default:
16529 /* Otherwise try to create a stack operation procedure to
16530 evaluate the value of the array bound. */
16532 dw_die_ref ctx, decl_die;
16533 dw_loc_list_ref list;
16535 list = loc_list_from_tree (bound, 2);
16536 if (list == NULL || single_element_loc_list_p (list))
16538 /* If DW_AT_*bound is not a reference nor constant, it is
16539 a DWARF expression rather than location description.
16540 For that loc_list_from_tree (bound, 0) is needed.
16541 If that fails to give a single element list,
16542 fall back to outputting this as a reference anyway. */
16543 dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
16544 if (list2 && single_element_loc_list_p (list2))
16546 add_AT_loc (subrange_die, bound_attr, list2->expr);
16547 break;
16550 if (list == NULL)
16551 break;
16553 if (current_function_decl == 0)
16554 ctx = comp_unit_die ();
16555 else
16556 ctx = lookup_decl_die (current_function_decl);
16558 decl_die = new_die (DW_TAG_variable, ctx, bound);
16559 add_AT_flag (decl_die, DW_AT_artificial, 1);
16560 add_type_attribute (decl_die, TREE_TYPE (bound), TYPE_QUAL_CONST, ctx);
16561 add_AT_location_description (decl_die, DW_AT_location, list);
16562 add_AT_die_ref (subrange_die, bound_attr, decl_die);
16563 break;
16568 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
16569 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
16570 Note that the block of subscript information for an array type also
16571 includes information about the element type of the given array type. */
16573 static void
16574 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
16576 unsigned dimension_number;
16577 tree lower, upper;
16578 dw_die_ref subrange_die;
16580 for (dimension_number = 0;
16581 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
16582 type = TREE_TYPE (type), dimension_number++)
16584 tree domain = TYPE_DOMAIN (type);
16586 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
16587 break;
16589 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
16590 and (in GNU C only) variable bounds. Handle all three forms
16591 here. */
16592 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
16593 if (domain)
16595 /* We have an array type with specified bounds. */
16596 lower = TYPE_MIN_VALUE (domain);
16597 upper = TYPE_MAX_VALUE (domain);
16599 /* Define the index type. */
16600 if (TREE_TYPE (domain))
16602 /* ??? This is probably an Ada unnamed subrange type. Ignore the
16603 TREE_TYPE field. We can't emit debug info for this
16604 because it is an unnamed integral type. */
16605 if (TREE_CODE (domain) == INTEGER_TYPE
16606 && TYPE_NAME (domain) == NULL_TREE
16607 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
16608 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
16610 else
16611 add_type_attribute (subrange_die, TREE_TYPE (domain),
16612 TYPE_UNQUALIFIED, type_die);
16615 /* ??? If upper is NULL, the array has unspecified length,
16616 but it does have a lower bound. This happens with Fortran
16617 dimension arr(N:*)
16618 Since the debugger is definitely going to need to know N
16619 to produce useful results, go ahead and output the lower
16620 bound solo, and hope the debugger can cope. */
16622 add_bound_info (subrange_die, DW_AT_lower_bound, lower);
16623 if (upper)
16624 add_bound_info (subrange_die, DW_AT_upper_bound, upper);
16627 /* Otherwise we have an array type with an unspecified length. The
16628 DWARF-2 spec does not say how to handle this; let's just leave out the
16629 bounds. */
16633 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
16635 static void
16636 add_byte_size_attribute (dw_die_ref die, tree tree_node)
16638 dw_die_ref decl_die;
16639 HOST_WIDE_INT size;
16641 switch (TREE_CODE (tree_node))
16643 case ERROR_MARK:
16644 size = 0;
16645 break;
16646 case ENUMERAL_TYPE:
16647 case RECORD_TYPE:
16648 case UNION_TYPE:
16649 case QUAL_UNION_TYPE:
16650 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
16651 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
16653 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
16654 return;
16656 size = int_size_in_bytes (tree_node);
16657 break;
16658 case FIELD_DECL:
16659 /* For a data member of a struct or union, the DW_AT_byte_size is
16660 generally given as the number of bytes normally allocated for an
16661 object of the *declared* type of the member itself. This is true
16662 even for bit-fields. */
16663 size = int_size_in_bytes (field_type (tree_node));
16664 break;
16665 default:
16666 gcc_unreachable ();
16669 /* Note that `size' might be -1 when we get to this point. If it is, that
16670 indicates that the byte size of the entity in question is variable. We
16671 have no good way of expressing this fact in Dwarf at the present time,
16672 when location description was not used by the caller code instead. */
16673 if (size >= 0)
16674 add_AT_unsigned (die, DW_AT_byte_size, size);
16677 /* For a FIELD_DECL node which represents a bit-field, output an attribute
16678 which specifies the distance in bits from the highest order bit of the
16679 "containing object" for the bit-field to the highest order bit of the
16680 bit-field itself.
16682 For any given bit-field, the "containing object" is a hypothetical object
16683 (of some integral or enum type) within which the given bit-field lives. The
16684 type of this hypothetical "containing object" is always the same as the
16685 declared type of the individual bit-field itself. The determination of the
16686 exact location of the "containing object" for a bit-field is rather
16687 complicated. It's handled by the `field_byte_offset' function (above).
16689 Note that it is the size (in bytes) of the hypothetical "containing object"
16690 which will be given in the DW_AT_byte_size attribute for this bit-field.
16691 (See `byte_size_attribute' above). */
16693 static inline void
16694 add_bit_offset_attribute (dw_die_ref die, tree decl)
16696 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
16697 tree type = DECL_BIT_FIELD_TYPE (decl);
16698 HOST_WIDE_INT bitpos_int;
16699 HOST_WIDE_INT highest_order_object_bit_offset;
16700 HOST_WIDE_INT highest_order_field_bit_offset;
16701 HOST_WIDE_INT bit_offset;
16703 /* Must be a field and a bit field. */
16704 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
16706 /* We can't yet handle bit-fields whose offsets are variable, so if we
16707 encounter such things, just return without generating any attribute
16708 whatsoever. Likewise for variable or too large size. */
16709 if (! tree_fits_shwi_p (bit_position (decl))
16710 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
16711 return;
16713 bitpos_int = int_bit_position (decl);
16715 /* Note that the bit offset is always the distance (in bits) from the
16716 highest-order bit of the "containing object" to the highest-order bit of
16717 the bit-field itself. Since the "high-order end" of any object or field
16718 is different on big-endian and little-endian machines, the computation
16719 below must take account of these differences. */
16720 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
16721 highest_order_field_bit_offset = bitpos_int;
16723 if (! BYTES_BIG_ENDIAN)
16725 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
16726 highest_order_object_bit_offset += simple_type_size_in_bits (type);
16729 bit_offset
16730 = (! BYTES_BIG_ENDIAN
16731 ? highest_order_object_bit_offset - highest_order_field_bit_offset
16732 : highest_order_field_bit_offset - highest_order_object_bit_offset);
16734 if (bit_offset < 0)
16735 add_AT_int (die, DW_AT_bit_offset, bit_offset);
16736 else
16737 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
16740 /* For a FIELD_DECL node which represents a bit field, output an attribute
16741 which specifies the length in bits of the given field. */
16743 static inline void
16744 add_bit_size_attribute (dw_die_ref die, tree decl)
16746 /* Must be a field and a bit field. */
16747 gcc_assert (TREE_CODE (decl) == FIELD_DECL
16748 && DECL_BIT_FIELD_TYPE (decl));
16750 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
16751 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
16754 /* If the compiled language is ANSI C, then add a 'prototyped'
16755 attribute, if arg types are given for the parameters of a function. */
16757 static inline void
16758 add_prototyped_attribute (dw_die_ref die, tree func_type)
16760 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
16762 case DW_LANG_C:
16763 case DW_LANG_C89:
16764 case DW_LANG_C99:
16765 case DW_LANG_C11:
16766 case DW_LANG_ObjC:
16767 if (prototype_p (func_type))
16768 add_AT_flag (die, DW_AT_prototyped, 1);
16769 break;
16770 default:
16771 break;
16775 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
16776 by looking in either the type declaration or object declaration
16777 equate table. */
16779 static inline dw_die_ref
16780 add_abstract_origin_attribute (dw_die_ref die, tree origin)
16782 dw_die_ref origin_die = NULL;
16784 if (TREE_CODE (origin) != FUNCTION_DECL)
16786 /* We may have gotten separated from the block for the inlined
16787 function, if we're in an exception handler or some such; make
16788 sure that the abstract function has been written out.
16790 Doing this for nested functions is wrong, however; functions are
16791 distinct units, and our context might not even be inline. */
16792 tree fn = origin;
16794 if (TYPE_P (fn))
16795 fn = TYPE_STUB_DECL (fn);
16797 fn = decl_function_context (fn);
16798 if (fn)
16799 dwarf2out_abstract_function (fn);
16802 if (DECL_P (origin))
16803 origin_die = lookup_decl_die (origin);
16804 else if (TYPE_P (origin))
16805 origin_die = lookup_type_die (origin);
16807 /* XXX: Functions that are never lowered don't always have correct block
16808 trees (in the case of java, they simply have no block tree, in some other
16809 languages). For these functions, there is nothing we can really do to
16810 output correct debug info for inlined functions in all cases. Rather
16811 than die, we'll just produce deficient debug info now, in that we will
16812 have variables without a proper abstract origin. In the future, when all
16813 functions are lowered, we should re-add a gcc_assert (origin_die)
16814 here. */
16816 if (origin_die)
16817 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
16818 return origin_die;
16821 /* We do not currently support the pure_virtual attribute. */
16823 static inline void
16824 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
16826 if (DECL_VINDEX (func_decl))
16828 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
16830 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
16831 add_AT_loc (die, DW_AT_vtable_elem_location,
16832 new_loc_descr (DW_OP_constu,
16833 tree_to_shwi (DECL_VINDEX (func_decl)),
16834 0));
16836 /* GNU extension: Record what type this method came from originally. */
16837 if (debug_info_level > DINFO_LEVEL_TERSE
16838 && DECL_CONTEXT (func_decl))
16839 add_AT_die_ref (die, DW_AT_containing_type,
16840 lookup_type_die (DECL_CONTEXT (func_decl)));
16844 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
16845 given decl. This used to be a vendor extension until after DWARF 4
16846 standardized it. */
16848 static void
16849 add_linkage_attr (dw_die_ref die, tree decl)
16851 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
16853 /* Mimic what assemble_name_raw does with a leading '*'. */
16854 if (name[0] == '*')
16855 name = &name[1];
16857 if (dwarf_version >= 4)
16858 add_AT_string (die, DW_AT_linkage_name, name);
16859 else
16860 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
16863 /* Add source coordinate attributes for the given decl. */
16865 static void
16866 add_src_coords_attributes (dw_die_ref die, tree decl)
16868 expanded_location s;
16870 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
16871 return;
16872 s = expand_location (DECL_SOURCE_LOCATION (decl));
16873 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
16874 add_AT_unsigned (die, DW_AT_decl_line, s.line);
16877 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
16879 static void
16880 add_linkage_name (dw_die_ref die, tree decl)
16882 if (debug_info_level > DINFO_LEVEL_NONE
16883 && (TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
16884 && TREE_PUBLIC (decl)
16885 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
16886 && die->die_tag != DW_TAG_member)
16888 /* Defer until we have an assembler name set. */
16889 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
16891 limbo_die_node *asm_name;
16893 asm_name = ggc_cleared_alloc<limbo_die_node> ();
16894 asm_name->die = die;
16895 asm_name->created_for = decl;
16896 asm_name->next = deferred_asm_name;
16897 deferred_asm_name = asm_name;
16899 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
16900 add_linkage_attr (die, decl);
16904 /* Add a DW_AT_name attribute and source coordinate attribute for the
16905 given decl, but only if it actually has a name. */
16907 static void
16908 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
16910 tree decl_name;
16912 decl_name = DECL_NAME (decl);
16913 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
16915 const char *name = dwarf2_name (decl, 0);
16916 if (name)
16917 add_name_attribute (die, name);
16918 if (! DECL_ARTIFICIAL (decl))
16919 add_src_coords_attributes (die, decl);
16921 add_linkage_name (die, decl);
16924 #ifdef VMS_DEBUGGING_INFO
16925 /* Get the function's name, as described by its RTL. This may be different
16926 from the DECL_NAME name used in the source file. */
16927 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
16929 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
16930 XEXP (DECL_RTL (decl), 0), false);
16931 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
16933 #endif /* VMS_DEBUGGING_INFO */
16936 #ifdef VMS_DEBUGGING_INFO
16937 /* Output the debug main pointer die for VMS */
16939 void
16940 dwarf2out_vms_debug_main_pointer (void)
16942 char label[MAX_ARTIFICIAL_LABEL_BYTES];
16943 dw_die_ref die;
16945 /* Allocate the VMS debug main subprogram die. */
16946 die = ggc_cleared_alloc<die_node> ();
16947 die->die_tag = DW_TAG_subprogram;
16948 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
16949 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
16950 current_function_funcdef_no);
16951 add_AT_lbl_id (die, DW_AT_entry_pc, label);
16953 /* Make it the first child of comp_unit_die (). */
16954 die->die_parent = comp_unit_die ();
16955 if (comp_unit_die ()->die_child)
16957 die->die_sib = comp_unit_die ()->die_child->die_sib;
16958 comp_unit_die ()->die_child->die_sib = die;
16960 else
16962 die->die_sib = die;
16963 comp_unit_die ()->die_child = die;
16966 #endif /* VMS_DEBUGGING_INFO */
16968 /* Push a new declaration scope. */
16970 static void
16971 push_decl_scope (tree scope)
16973 vec_safe_push (decl_scope_table, scope);
16976 /* Pop a declaration scope. */
16978 static inline void
16979 pop_decl_scope (void)
16981 decl_scope_table->pop ();
16984 /* walk_tree helper function for uses_local_type, below. */
16986 static tree
16987 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
16989 if (!TYPE_P (*tp))
16990 *walk_subtrees = 0;
16991 else
16993 tree name = TYPE_NAME (*tp);
16994 if (name && DECL_P (name) && decl_function_context (name))
16995 return *tp;
16997 return NULL_TREE;
17000 /* If TYPE involves a function-local type (including a local typedef to a
17001 non-local type), returns that type; otherwise returns NULL_TREE. */
17003 static tree
17004 uses_local_type (tree type)
17006 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
17007 return used;
17010 /* Return the DIE for the scope that immediately contains this type.
17011 Non-named types that do not involve a function-local type get global
17012 scope. Named types nested in namespaces or other types get their
17013 containing scope. All other types (i.e. function-local named types) get
17014 the current active scope. */
17016 static dw_die_ref
17017 scope_die_for (tree t, dw_die_ref context_die)
17019 dw_die_ref scope_die = NULL;
17020 tree containing_scope;
17022 /* Non-types always go in the current scope. */
17023 gcc_assert (TYPE_P (t));
17025 /* Use the scope of the typedef, rather than the scope of the type
17026 it refers to. */
17027 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
17028 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
17029 else
17030 containing_scope = TYPE_CONTEXT (t);
17032 /* Use the containing namespace if there is one. */
17033 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
17035 if (context_die == lookup_decl_die (containing_scope))
17036 /* OK */;
17037 else if (debug_info_level > DINFO_LEVEL_TERSE)
17038 context_die = get_context_die (containing_scope);
17039 else
17040 containing_scope = NULL_TREE;
17043 /* Ignore function type "scopes" from the C frontend. They mean that
17044 a tagged type is local to a parmlist of a function declarator, but
17045 that isn't useful to DWARF. */
17046 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
17047 containing_scope = NULL_TREE;
17049 if (SCOPE_FILE_SCOPE_P (containing_scope))
17051 /* If T uses a local type keep it local as well, to avoid references
17052 to function-local DIEs from outside the function. */
17053 if (current_function_decl && uses_local_type (t))
17054 scope_die = context_die;
17055 else
17056 scope_die = comp_unit_die ();
17058 else if (TYPE_P (containing_scope))
17060 /* For types, we can just look up the appropriate DIE. */
17061 if (debug_info_level > DINFO_LEVEL_TERSE)
17062 scope_die = get_context_die (containing_scope);
17063 else
17065 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
17066 if (scope_die == NULL)
17067 scope_die = comp_unit_die ();
17070 else
17071 scope_die = context_die;
17073 return scope_die;
17076 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
17078 static inline int
17079 local_scope_p (dw_die_ref context_die)
17081 for (; context_die; context_die = context_die->die_parent)
17082 if (context_die->die_tag == DW_TAG_inlined_subroutine
17083 || context_die->die_tag == DW_TAG_subprogram)
17084 return 1;
17086 return 0;
17089 /* Returns nonzero if CONTEXT_DIE is a class. */
17091 static inline int
17092 class_scope_p (dw_die_ref context_die)
17094 return (context_die
17095 && (context_die->die_tag == DW_TAG_structure_type
17096 || context_die->die_tag == DW_TAG_class_type
17097 || context_die->die_tag == DW_TAG_interface_type
17098 || context_die->die_tag == DW_TAG_union_type));
17101 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
17102 whether or not to treat a DIE in this context as a declaration. */
17104 static inline int
17105 class_or_namespace_scope_p (dw_die_ref context_die)
17107 return (class_scope_p (context_die)
17108 || (context_die && context_die->die_tag == DW_TAG_namespace));
17111 /* Many forms of DIEs require a "type description" attribute. This
17112 routine locates the proper "type descriptor" die for the type given
17113 by 'type' plus any additional qualifiers given by 'cv_quals', and
17114 adds a DW_AT_type attribute below the given die. */
17116 static void
17117 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
17118 dw_die_ref context_die)
17120 enum tree_code code = TREE_CODE (type);
17121 dw_die_ref type_die = NULL;
17123 /* ??? If this type is an unnamed subrange type of an integral, floating-point
17124 or fixed-point type, use the inner type. This is because we have no
17125 support for unnamed types in base_type_die. This can happen if this is
17126 an Ada subrange type. Correct solution is emit a subrange type die. */
17127 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
17128 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
17129 type = TREE_TYPE (type), code = TREE_CODE (type);
17131 if (code == ERROR_MARK
17132 /* Handle a special case. For functions whose return type is void, we
17133 generate *no* type attribute. (Note that no object may have type
17134 `void', so this only applies to function return types). */
17135 || code == VOID_TYPE)
17136 return;
17138 type_die = modified_type_die (type,
17139 cv_quals | TYPE_QUALS_NO_ADDR_SPACE (type),
17140 context_die);
17142 if (type_die != NULL)
17143 add_AT_die_ref (object_die, DW_AT_type, type_die);
17146 /* Given an object die, add the calling convention attribute for the
17147 function call type. */
17148 static void
17149 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
17151 enum dwarf_calling_convention value = DW_CC_normal;
17153 value = ((enum dwarf_calling_convention)
17154 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
17156 if (is_fortran ()
17157 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
17159 /* DWARF 2 doesn't provide a way to identify a program's source-level
17160 entry point. DW_AT_calling_convention attributes are only meant
17161 to describe functions' calling conventions. However, lacking a
17162 better way to signal the Fortran main program, we used this for
17163 a long time, following existing custom. Now, DWARF 4 has
17164 DW_AT_main_subprogram, which we add below, but some tools still
17165 rely on the old way, which we thus keep. */
17166 value = DW_CC_program;
17168 if (dwarf_version >= 4 || !dwarf_strict)
17169 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
17172 /* Only add the attribute if the backend requests it, and
17173 is not DW_CC_normal. */
17174 if (value && (value != DW_CC_normal))
17175 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
17178 /* Given a tree pointer to a struct, class, union, or enum type node, return
17179 a pointer to the (string) tag name for the given type, or zero if the type
17180 was declared without a tag. */
17182 static const char *
17183 type_tag (const_tree type)
17185 const char *name = 0;
17187 if (TYPE_NAME (type) != 0)
17189 tree t = 0;
17191 /* Find the IDENTIFIER_NODE for the type name. */
17192 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
17193 && !TYPE_NAMELESS (type))
17194 t = TYPE_NAME (type);
17196 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
17197 a TYPE_DECL node, regardless of whether or not a `typedef' was
17198 involved. */
17199 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
17200 && ! DECL_IGNORED_P (TYPE_NAME (type)))
17202 /* We want to be extra verbose. Don't call dwarf_name if
17203 DECL_NAME isn't set. The default hook for decl_printable_name
17204 doesn't like that, and in this context it's correct to return
17205 0, instead of "<anonymous>" or the like. */
17206 if (DECL_NAME (TYPE_NAME (type))
17207 && !DECL_NAMELESS (TYPE_NAME (type)))
17208 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
17211 /* Now get the name as a string, or invent one. */
17212 if (!name && t != 0)
17213 name = IDENTIFIER_POINTER (t);
17216 return (name == 0 || *name == '\0') ? 0 : name;
17219 /* Return the type associated with a data member, make a special check
17220 for bit field types. */
17222 static inline tree
17223 member_declared_type (const_tree member)
17225 return (DECL_BIT_FIELD_TYPE (member)
17226 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
17229 /* Get the decl's label, as described by its RTL. This may be different
17230 from the DECL_NAME name used in the source file. */
17232 #if 0
17233 static const char *
17234 decl_start_label (tree decl)
17236 rtx x;
17237 const char *fnname;
17239 x = DECL_RTL (decl);
17240 gcc_assert (MEM_P (x));
17242 x = XEXP (x, 0);
17243 gcc_assert (GET_CODE (x) == SYMBOL_REF);
17245 fnname = XSTR (x, 0);
17246 return fnname;
17248 #endif
17250 /* These routines generate the internal representation of the DIE's for
17251 the compilation unit. Debugging information is collected by walking
17252 the declaration trees passed in from dwarf2out_decl(). */
17254 static void
17255 gen_array_type_die (tree type, dw_die_ref context_die)
17257 dw_die_ref scope_die = scope_die_for (type, context_die);
17258 dw_die_ref array_die;
17260 /* GNU compilers represent multidimensional array types as sequences of one
17261 dimensional array types whose element types are themselves array types.
17262 We sometimes squish that down to a single array_type DIE with multiple
17263 subscripts in the Dwarf debugging info. The draft Dwarf specification
17264 say that we are allowed to do this kind of compression in C, because
17265 there is no difference between an array of arrays and a multidimensional
17266 array. We don't do this for Ada to remain as close as possible to the
17267 actual representation, which is especially important against the language
17268 flexibilty wrt arrays of variable size. */
17270 bool collapse_nested_arrays = !is_ada ();
17271 tree element_type;
17273 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
17274 DW_TAG_string_type doesn't have DW_AT_type attribute). */
17275 if (TYPE_STRING_FLAG (type)
17276 && TREE_CODE (type) == ARRAY_TYPE
17277 && is_fortran ()
17278 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
17280 HOST_WIDE_INT size;
17282 array_die = new_die (DW_TAG_string_type, scope_die, type);
17283 add_name_attribute (array_die, type_tag (type));
17284 equate_type_number_to_die (type, array_die);
17285 size = int_size_in_bytes (type);
17286 if (size >= 0)
17287 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17288 else if (TYPE_DOMAIN (type) != NULL_TREE
17289 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
17290 && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
17292 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
17293 dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
17295 size = int_size_in_bytes (TREE_TYPE (szdecl));
17296 if (loc && size > 0)
17298 add_AT_location_description (array_die, DW_AT_string_length, loc);
17299 if (size != DWARF2_ADDR_SIZE)
17300 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17303 return;
17306 array_die = new_die (DW_TAG_array_type, scope_die, type);
17307 add_name_attribute (array_die, type_tag (type));
17308 equate_type_number_to_die (type, array_die);
17310 if (TREE_CODE (type) == VECTOR_TYPE)
17311 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
17313 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
17314 if (is_fortran ()
17315 && TREE_CODE (type) == ARRAY_TYPE
17316 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
17317 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
17318 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17320 #if 0
17321 /* We default the array ordering. SDB will probably do
17322 the right things even if DW_AT_ordering is not present. It's not even
17323 an issue until we start to get into multidimensional arrays anyway. If
17324 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
17325 then we'll have to put the DW_AT_ordering attribute back in. (But if
17326 and when we find out that we need to put these in, we will only do so
17327 for multidimensional arrays. */
17328 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17329 #endif
17331 if (TREE_CODE (type) == VECTOR_TYPE)
17333 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
17334 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
17335 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
17336 add_bound_info (subrange_die, DW_AT_upper_bound,
17337 size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
17339 else
17340 add_subscript_info (array_die, type, collapse_nested_arrays);
17342 /* Add representation of the type of the elements of this array type and
17343 emit the corresponding DIE if we haven't done it already. */
17344 element_type = TREE_TYPE (type);
17345 if (collapse_nested_arrays)
17346 while (TREE_CODE (element_type) == ARRAY_TYPE)
17348 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
17349 break;
17350 element_type = TREE_TYPE (element_type);
17353 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED, context_die);
17355 add_gnat_descriptive_type_attribute (array_die, type, context_die);
17356 if (TYPE_ARTIFICIAL (type))
17357 add_AT_flag (array_die, DW_AT_artificial, 1);
17359 if (get_AT (array_die, DW_AT_name))
17360 add_pubtype (type, array_die);
17363 static dw_loc_descr_ref
17364 descr_info_loc (tree val, tree base_decl)
17366 HOST_WIDE_INT size;
17367 dw_loc_descr_ref loc, loc2;
17368 enum dwarf_location_atom op;
17370 if (val == base_decl)
17371 return new_loc_descr (DW_OP_push_object_address, 0, 0);
17373 switch (TREE_CODE (val))
17375 CASE_CONVERT:
17376 return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17377 case VAR_DECL:
17378 return loc_descriptor_from_tree (val, 0);
17379 case INTEGER_CST:
17380 if (tree_fits_shwi_p (val))
17381 return int_loc_descriptor (tree_to_shwi (val));
17382 break;
17383 case INDIRECT_REF:
17384 size = int_size_in_bytes (TREE_TYPE (val));
17385 if (size < 0)
17386 break;
17387 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17388 if (!loc)
17389 break;
17390 if (size == DWARF2_ADDR_SIZE)
17391 add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
17392 else
17393 add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
17394 return loc;
17395 case POINTER_PLUS_EXPR:
17396 case PLUS_EXPR:
17397 if (tree_fits_uhwi_p (TREE_OPERAND (val, 1))
17398 && tree_to_uhwi (TREE_OPERAND (val, 1)) < 16384)
17400 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17401 if (!loc)
17402 break;
17403 loc_descr_plus_const (&loc, tree_to_shwi (TREE_OPERAND (val, 1)));
17405 else
17407 op = DW_OP_plus;
17408 do_binop:
17409 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17410 if (!loc)
17411 break;
17412 loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
17413 if (!loc2)
17414 break;
17415 add_loc_descr (&loc, loc2);
17416 add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
17418 return loc;
17419 case MINUS_EXPR:
17420 op = DW_OP_minus;
17421 goto do_binop;
17422 case MULT_EXPR:
17423 op = DW_OP_mul;
17424 goto do_binop;
17425 case EQ_EXPR:
17426 op = DW_OP_eq;
17427 goto do_binop;
17428 case NE_EXPR:
17429 op = DW_OP_ne;
17430 goto do_binop;
17431 default:
17432 break;
17434 return NULL;
17437 static void
17438 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
17439 tree val, tree base_decl)
17441 dw_loc_descr_ref loc;
17443 if (tree_fits_shwi_p (val))
17445 add_AT_unsigned (die, attr, tree_to_shwi (val));
17446 return;
17449 loc = descr_info_loc (val, base_decl);
17450 if (!loc)
17451 return;
17453 add_AT_loc (die, attr, loc);
17456 /* This routine generates DIE for array with hidden descriptor, details
17457 are filled into *info by a langhook. */
17459 static void
17460 gen_descr_array_type_die (tree type, struct array_descr_info *info,
17461 dw_die_ref context_die)
17463 dw_die_ref scope_die = scope_die_for (type, context_die);
17464 dw_die_ref array_die;
17465 int dim;
17467 array_die = new_die (DW_TAG_array_type, scope_die, type);
17468 add_name_attribute (array_die, type_tag (type));
17469 equate_type_number_to_die (type, array_die);
17471 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
17472 if (is_fortran ()
17473 && info->ndimensions >= 2)
17474 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17476 if (info->data_location)
17477 add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
17478 info->base_decl);
17479 if (info->associated)
17480 add_descr_info_field (array_die, DW_AT_associated, info->associated,
17481 info->base_decl);
17482 if (info->allocated)
17483 add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
17484 info->base_decl);
17486 for (dim = 0; dim < info->ndimensions; dim++)
17488 dw_die_ref subrange_die
17489 = new_die (DW_TAG_subrange_type, array_die, NULL);
17491 if (info->dimen[dim].lower_bound)
17493 /* If it is the default value, omit it. */
17494 int dflt;
17496 if (tree_fits_shwi_p (info->dimen[dim].lower_bound)
17497 && (dflt = lower_bound_default ()) != -1
17498 && tree_to_shwi (info->dimen[dim].lower_bound) == dflt)
17500 else
17501 add_descr_info_field (subrange_die, DW_AT_lower_bound,
17502 info->dimen[dim].lower_bound,
17503 info->base_decl);
17505 if (info->dimen[dim].upper_bound)
17506 add_descr_info_field (subrange_die, DW_AT_upper_bound,
17507 info->dimen[dim].upper_bound,
17508 info->base_decl);
17509 if (info->dimen[dim].stride)
17510 add_descr_info_field (subrange_die, DW_AT_byte_stride,
17511 info->dimen[dim].stride,
17512 info->base_decl);
17515 gen_type_die (info->element_type, context_die);
17516 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
17517 context_die);
17519 if (get_AT (array_die, DW_AT_name))
17520 add_pubtype (type, array_die);
17523 #if 0
17524 static void
17525 gen_entry_point_die (tree decl, dw_die_ref context_die)
17527 tree origin = decl_ultimate_origin (decl);
17528 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
17530 if (origin != NULL)
17531 add_abstract_origin_attribute (decl_die, origin);
17532 else
17534 add_name_and_src_coords_attributes (decl_die, decl);
17535 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
17536 TYPE_UNQUALIFIED, context_die);
17539 if (DECL_ABSTRACT_P (decl))
17540 equate_decl_number_to_die (decl, decl_die);
17541 else
17542 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
17544 #endif
17546 /* Walk through the list of incomplete types again, trying once more to
17547 emit full debugging info for them. */
17549 static void
17550 retry_incomplete_types (void)
17552 int i;
17554 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
17555 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
17556 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
17559 /* Determine what tag to use for a record type. */
17561 static enum dwarf_tag
17562 record_type_tag (tree type)
17564 if (! lang_hooks.types.classify_record)
17565 return DW_TAG_structure_type;
17567 switch (lang_hooks.types.classify_record (type))
17569 case RECORD_IS_STRUCT:
17570 return DW_TAG_structure_type;
17572 case RECORD_IS_CLASS:
17573 return DW_TAG_class_type;
17575 case RECORD_IS_INTERFACE:
17576 if (dwarf_version >= 3 || !dwarf_strict)
17577 return DW_TAG_interface_type;
17578 return DW_TAG_structure_type;
17580 default:
17581 gcc_unreachable ();
17585 /* Generate a DIE to represent an enumeration type. Note that these DIEs
17586 include all of the information about the enumeration values also. Each
17587 enumerated type name/value is listed as a child of the enumerated type
17588 DIE. */
17590 static dw_die_ref
17591 gen_enumeration_type_die (tree type, dw_die_ref context_die)
17593 dw_die_ref type_die = lookup_type_die (type);
17595 if (type_die == NULL)
17597 type_die = new_die (DW_TAG_enumeration_type,
17598 scope_die_for (type, context_die), type);
17599 equate_type_number_to_die (type, type_die);
17600 add_name_attribute (type_die, type_tag (type));
17601 if (dwarf_version >= 4 || !dwarf_strict)
17603 if (ENUM_IS_SCOPED (type))
17604 add_AT_flag (type_die, DW_AT_enum_class, 1);
17605 if (ENUM_IS_OPAQUE (type))
17606 add_AT_flag (type_die, DW_AT_declaration, 1);
17609 else if (! TYPE_SIZE (type))
17610 return type_die;
17611 else
17612 remove_AT (type_die, DW_AT_declaration);
17614 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
17615 given enum type is incomplete, do not generate the DW_AT_byte_size
17616 attribute or the DW_AT_element_list attribute. */
17617 if (TYPE_SIZE (type))
17619 tree link;
17621 TREE_ASM_WRITTEN (type) = 1;
17622 add_byte_size_attribute (type_die, type);
17623 if (dwarf_version >= 3 || !dwarf_strict)
17625 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
17626 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED,
17627 context_die);
17629 if (TYPE_STUB_DECL (type) != NULL_TREE)
17631 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
17632 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
17635 /* If the first reference to this type was as the return type of an
17636 inline function, then it may not have a parent. Fix this now. */
17637 if (type_die->die_parent == NULL)
17638 add_child_die (scope_die_for (type, context_die), type_die);
17640 for (link = TYPE_VALUES (type);
17641 link != NULL; link = TREE_CHAIN (link))
17643 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
17644 tree value = TREE_VALUE (link);
17646 add_name_attribute (enum_die,
17647 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
17649 if (TREE_CODE (value) == CONST_DECL)
17650 value = DECL_INITIAL (value);
17652 if (simple_type_size_in_bits (TREE_TYPE (value))
17653 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
17655 /* For constant forms created by add_AT_unsigned DWARF
17656 consumers (GDB, elfutils, etc.) always zero extend
17657 the value. Only when the actual value is negative
17658 do we need to use add_AT_int to generate a constant
17659 form that can represent negative values. */
17660 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
17661 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
17662 add_AT_unsigned (enum_die, DW_AT_const_value,
17663 (unsigned HOST_WIDE_INT) val);
17664 else
17665 add_AT_int (enum_die, DW_AT_const_value, val);
17667 else
17668 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
17669 that here. TODO: This should be re-worked to use correct
17670 signed/unsigned double tags for all cases. */
17671 add_AT_wide (enum_die, DW_AT_const_value, value);
17674 add_gnat_descriptive_type_attribute (type_die, type, context_die);
17675 if (TYPE_ARTIFICIAL (type))
17676 add_AT_flag (type_die, DW_AT_artificial, 1);
17678 else
17679 add_AT_flag (type_die, DW_AT_declaration, 1);
17681 add_pubtype (type, type_die);
17683 return type_die;
17686 /* Generate a DIE to represent either a real live formal parameter decl or to
17687 represent just the type of some formal parameter position in some function
17688 type.
17690 Note that this routine is a bit unusual because its argument may be a
17691 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
17692 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
17693 node. If it's the former then this function is being called to output a
17694 DIE to represent a formal parameter object (or some inlining thereof). If
17695 it's the latter, then this function is only being called to output a
17696 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
17697 argument type of some subprogram type.
17698 If EMIT_NAME_P is true, name and source coordinate attributes
17699 are emitted. */
17701 static dw_die_ref
17702 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
17703 dw_die_ref context_die)
17705 tree node_or_origin = node ? node : origin;
17706 tree ultimate_origin;
17707 dw_die_ref parm_die
17708 = new_die (DW_TAG_formal_parameter, context_die, node);
17710 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
17712 case tcc_declaration:
17713 ultimate_origin = decl_ultimate_origin (node_or_origin);
17714 if (node || ultimate_origin)
17715 origin = ultimate_origin;
17716 if (origin != NULL)
17717 add_abstract_origin_attribute (parm_die, origin);
17718 else if (emit_name_p)
17719 add_name_and_src_coords_attributes (parm_die, node);
17720 if (origin == NULL
17721 || (! DECL_ABSTRACT_P (node_or_origin)
17722 && variably_modified_type_p (TREE_TYPE (node_or_origin),
17723 decl_function_context
17724 (node_or_origin))))
17726 tree type = TREE_TYPE (node_or_origin);
17727 if (decl_by_reference_p (node_or_origin))
17728 add_type_attribute (parm_die, TREE_TYPE (type),
17729 TYPE_UNQUALIFIED, context_die);
17730 else
17731 add_type_attribute (parm_die, type,
17732 decl_quals (node_or_origin),
17733 context_die);
17735 if (origin == NULL && DECL_ARTIFICIAL (node))
17736 add_AT_flag (parm_die, DW_AT_artificial, 1);
17738 if (node && node != origin)
17739 equate_decl_number_to_die (node, parm_die);
17740 if (! DECL_ABSTRACT_P (node_or_origin))
17741 add_location_or_const_value_attribute (parm_die, node_or_origin,
17742 node == NULL, DW_AT_location);
17744 break;
17746 case tcc_type:
17747 /* We were called with some kind of a ..._TYPE node. */
17748 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED,
17749 context_die);
17750 break;
17752 default:
17753 gcc_unreachable ();
17756 return parm_die;
17759 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
17760 children DW_TAG_formal_parameter DIEs representing the arguments of the
17761 parameter pack.
17763 PARM_PACK must be a function parameter pack.
17764 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
17765 must point to the subsequent arguments of the function PACK_ARG belongs to.
17766 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
17767 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
17768 following the last one for which a DIE was generated. */
17770 static dw_die_ref
17771 gen_formal_parameter_pack_die (tree parm_pack,
17772 tree pack_arg,
17773 dw_die_ref subr_die,
17774 tree *next_arg)
17776 tree arg;
17777 dw_die_ref parm_pack_die;
17779 gcc_assert (parm_pack
17780 && lang_hooks.function_parameter_pack_p (parm_pack)
17781 && subr_die);
17783 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
17784 add_src_coords_attributes (parm_pack_die, parm_pack);
17786 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
17788 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
17789 parm_pack))
17790 break;
17791 gen_formal_parameter_die (arg, NULL,
17792 false /* Don't emit name attribute. */,
17793 parm_pack_die);
17795 if (next_arg)
17796 *next_arg = arg;
17797 return parm_pack_die;
17800 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
17801 at the end of an (ANSI prototyped) formal parameters list. */
17803 static void
17804 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
17806 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
17809 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
17810 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
17811 parameters as specified in some function type specification (except for
17812 those which appear as part of a function *definition*). */
17814 static void
17815 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
17817 tree link;
17818 tree formal_type = NULL;
17819 tree first_parm_type;
17820 tree arg;
17822 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
17824 arg = DECL_ARGUMENTS (function_or_method_type);
17825 function_or_method_type = TREE_TYPE (function_or_method_type);
17827 else
17828 arg = NULL_TREE;
17830 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
17832 /* Make our first pass over the list of formal parameter types and output a
17833 DW_TAG_formal_parameter DIE for each one. */
17834 for (link = first_parm_type; link; )
17836 dw_die_ref parm_die;
17838 formal_type = TREE_VALUE (link);
17839 if (formal_type == void_type_node)
17840 break;
17842 /* Output a (nameless) DIE to represent the formal parameter itself. */
17843 if (!POINTER_BOUNDS_TYPE_P (formal_type))
17845 parm_die = gen_formal_parameter_die (formal_type, NULL,
17846 true /* Emit name attribute. */,
17847 context_die);
17848 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
17849 && link == first_parm_type)
17851 add_AT_flag (parm_die, DW_AT_artificial, 1);
17852 if (dwarf_version >= 3 || !dwarf_strict)
17853 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
17855 else if (arg && DECL_ARTIFICIAL (arg))
17856 add_AT_flag (parm_die, DW_AT_artificial, 1);
17859 link = TREE_CHAIN (link);
17860 if (arg)
17861 arg = DECL_CHAIN (arg);
17864 /* If this function type has an ellipsis, add a
17865 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
17866 if (formal_type != void_type_node)
17867 gen_unspecified_parameters_die (function_or_method_type, context_die);
17869 /* Make our second (and final) pass over the list of formal parameter types
17870 and output DIEs to represent those types (as necessary). */
17871 for (link = TYPE_ARG_TYPES (function_or_method_type);
17872 link && TREE_VALUE (link);
17873 link = TREE_CHAIN (link))
17874 gen_type_die (TREE_VALUE (link), context_die);
17877 /* We want to generate the DIE for TYPE so that we can generate the
17878 die for MEMBER, which has been defined; we will need to refer back
17879 to the member declaration nested within TYPE. If we're trying to
17880 generate minimal debug info for TYPE, processing TYPE won't do the
17881 trick; we need to attach the member declaration by hand. */
17883 static void
17884 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
17886 gen_type_die (type, context_die);
17888 /* If we're trying to avoid duplicate debug info, we may not have
17889 emitted the member decl for this function. Emit it now. */
17890 if (TYPE_STUB_DECL (type)
17891 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
17892 && ! lookup_decl_die (member))
17894 dw_die_ref type_die;
17895 gcc_assert (!decl_ultimate_origin (member));
17897 push_decl_scope (type);
17898 type_die = lookup_type_die_strip_naming_typedef (type);
17899 if (TREE_CODE (member) == FUNCTION_DECL)
17900 gen_subprogram_die (member, type_die);
17901 else if (TREE_CODE (member) == FIELD_DECL)
17903 /* Ignore the nameless fields that are used to skip bits but handle
17904 C++ anonymous unions and structs. */
17905 if (DECL_NAME (member) != NULL_TREE
17906 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
17907 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
17909 gen_type_die (member_declared_type (member), type_die);
17910 gen_field_die (member, type_die);
17913 else
17914 gen_variable_die (member, NULL_TREE, type_die);
17916 pop_decl_scope ();
17920 /* Forward declare these functions, because they are mutually recursive
17921 with their set_block_* pairing functions. */
17922 static void set_decl_origin_self (tree);
17923 static void set_decl_abstract_flags (tree, int);
17925 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
17926 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
17927 that it points to the node itself, thus indicating that the node is its
17928 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
17929 the given node is NULL, recursively descend the decl/block tree which
17930 it is the root of, and for each other ..._DECL or BLOCK node contained
17931 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
17932 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
17933 values to point to themselves. */
17935 static void
17936 set_block_origin_self (tree stmt)
17938 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
17940 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
17943 tree local_decl;
17945 for (local_decl = BLOCK_VARS (stmt);
17946 local_decl != NULL_TREE;
17947 local_decl = DECL_CHAIN (local_decl))
17948 /* Do not recurse on nested functions since the inlining status
17949 of parent and child can be different as per the DWARF spec. */
17950 if (TREE_CODE (local_decl) != FUNCTION_DECL
17951 && !DECL_EXTERNAL (local_decl))
17952 set_decl_origin_self (local_decl);
17956 tree subblock;
17958 for (subblock = BLOCK_SUBBLOCKS (stmt);
17959 subblock != NULL_TREE;
17960 subblock = BLOCK_CHAIN (subblock))
17961 set_block_origin_self (subblock); /* Recurse. */
17966 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
17967 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
17968 node to so that it points to the node itself, thus indicating that the
17969 node represents its own (abstract) origin. Additionally, if the
17970 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
17971 the decl/block tree of which the given node is the root of, and for
17972 each other ..._DECL or BLOCK node contained therein whose
17973 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
17974 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
17975 point to themselves. */
17977 static void
17978 set_decl_origin_self (tree decl)
17980 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
17982 DECL_ABSTRACT_ORIGIN (decl) = decl;
17983 if (TREE_CODE (decl) == FUNCTION_DECL)
17985 tree arg;
17987 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
17988 DECL_ABSTRACT_ORIGIN (arg) = arg;
17989 if (DECL_INITIAL (decl) != NULL_TREE
17990 && DECL_INITIAL (decl) != error_mark_node)
17991 set_block_origin_self (DECL_INITIAL (decl));
17996 /* Given a pointer to some BLOCK node, and a boolean value to set the
17997 "abstract" flags to, set that value into the BLOCK_ABSTRACT flag for
17998 the given block, and for all local decls and all local sub-blocks
17999 (recursively) which are contained therein. */
18001 static void
18002 set_block_abstract_flags (tree stmt, int setting)
18004 tree local_decl;
18005 tree subblock;
18006 unsigned int i;
18008 BLOCK_ABSTRACT (stmt) = setting;
18010 for (local_decl = BLOCK_VARS (stmt);
18011 local_decl != NULL_TREE;
18012 local_decl = DECL_CHAIN (local_decl))
18013 if (! DECL_EXTERNAL (local_decl))
18014 set_decl_abstract_flags (local_decl, setting);
18016 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
18018 local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
18019 if ((TREE_CODE (local_decl) == VAR_DECL && !TREE_STATIC (local_decl))
18020 || TREE_CODE (local_decl) == PARM_DECL)
18021 set_decl_abstract_flags (local_decl, setting);
18024 for (subblock = BLOCK_SUBBLOCKS (stmt);
18025 subblock != NULL_TREE;
18026 subblock = BLOCK_CHAIN (subblock))
18027 set_block_abstract_flags (subblock, setting);
18030 /* Given a pointer to some ..._DECL node, and a boolean value to set the
18031 "abstract" flags to, set that value into the DECL_ABSTRACT_P flag for the
18032 given decl, and (in the case where the decl is a FUNCTION_DECL) also
18033 set the abstract flags for all of the parameters, local vars, local
18034 blocks and sub-blocks (recursively) to the same setting. */
18036 static void
18037 set_decl_abstract_flags (tree decl, int setting)
18039 DECL_ABSTRACT_P (decl) = setting;
18040 if (TREE_CODE (decl) == FUNCTION_DECL)
18042 tree arg;
18044 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
18045 DECL_ABSTRACT_P (arg) = setting;
18046 if (DECL_INITIAL (decl) != NULL_TREE
18047 && DECL_INITIAL (decl) != error_mark_node)
18048 set_block_abstract_flags (DECL_INITIAL (decl), setting);
18052 /* Generate the DWARF2 info for the "abstract" instance of a function which we
18053 may later generate inlined and/or out-of-line instances of. */
18055 static void
18056 dwarf2out_abstract_function (tree decl)
18058 dw_die_ref old_die;
18059 tree save_fn;
18060 tree context;
18061 int was_abstract;
18062 hash_table<decl_loc_hasher> *old_decl_loc_table;
18063 hash_table<dw_loc_list_hasher> *old_cached_dw_loc_list_table;
18064 int old_call_site_count, old_tail_call_site_count;
18065 struct call_arg_loc_node *old_call_arg_locations;
18067 /* Make sure we have the actual abstract inline, not a clone. */
18068 decl = DECL_ORIGIN (decl);
18070 old_die = lookup_decl_die (decl);
18071 if (old_die && get_AT (old_die, DW_AT_inline))
18072 /* We've already generated the abstract instance. */
18073 return;
18075 /* We can be called while recursively when seeing block defining inlined subroutine
18076 DIE. Be sure to not clobber the outer location table nor use it or we would
18077 get locations in abstract instantces. */
18078 old_decl_loc_table = decl_loc_table;
18079 decl_loc_table = NULL;
18080 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
18081 cached_dw_loc_list_table = NULL;
18082 old_call_arg_locations = call_arg_locations;
18083 call_arg_locations = NULL;
18084 old_call_site_count = call_site_count;
18085 call_site_count = -1;
18086 old_tail_call_site_count = tail_call_site_count;
18087 tail_call_site_count = -1;
18089 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
18090 we don't get confused by DECL_ABSTRACT_P. */
18091 if (debug_info_level > DINFO_LEVEL_TERSE)
18093 context = decl_class_context (decl);
18094 if (context)
18095 gen_type_die_for_member
18096 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
18099 /* Pretend we've just finished compiling this function. */
18100 save_fn = current_function_decl;
18101 current_function_decl = decl;
18103 was_abstract = DECL_ABSTRACT_P (decl);
18104 set_decl_abstract_flags (decl, 1);
18105 dwarf2out_decl (decl);
18106 if (! was_abstract)
18107 set_decl_abstract_flags (decl, 0);
18109 current_function_decl = save_fn;
18110 decl_loc_table = old_decl_loc_table;
18111 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
18112 call_arg_locations = old_call_arg_locations;
18113 call_site_count = old_call_site_count;
18114 tail_call_site_count = old_tail_call_site_count;
18117 /* Helper function of premark_used_types() which gets called through
18118 htab_traverse.
18120 Marks the DIE of a given type in *SLOT as perennial, so it never gets
18121 marked as unused by prune_unused_types. */
18123 bool
18124 premark_used_types_helper (tree const &type, void *)
18126 dw_die_ref die;
18128 die = lookup_type_die (type);
18129 if (die != NULL)
18130 die->die_perennial_p = 1;
18131 return true;
18134 /* Helper function of premark_types_used_by_global_vars which gets called
18135 through htab_traverse.
18137 Marks the DIE of a given type in *SLOT as perennial, so it never gets
18138 marked as unused by prune_unused_types. The DIE of the type is marked
18139 only if the global variable using the type will actually be emitted. */
18142 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
18143 void *)
18145 struct types_used_by_vars_entry *entry;
18146 dw_die_ref die;
18148 entry = (struct types_used_by_vars_entry *) *slot;
18149 gcc_assert (entry->type != NULL
18150 && entry->var_decl != NULL);
18151 die = lookup_type_die (entry->type);
18152 if (die)
18154 /* Ask cgraph if the global variable really is to be emitted.
18155 If yes, then we'll keep the DIE of ENTRY->TYPE. */
18156 varpool_node *node = varpool_node::get (entry->var_decl);
18157 if (node && node->definition)
18159 die->die_perennial_p = 1;
18160 /* Keep the parent DIEs as well. */
18161 while ((die = die->die_parent) && die->die_perennial_p == 0)
18162 die->die_perennial_p = 1;
18165 return 1;
18168 /* Mark all members of used_types_hash as perennial. */
18170 static void
18171 premark_used_types (struct function *fun)
18173 if (fun && fun->used_types_hash)
18174 fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
18177 /* Mark all members of types_used_by_vars_entry as perennial. */
18179 static void
18180 premark_types_used_by_global_vars (void)
18182 if (types_used_by_vars_hash)
18183 types_used_by_vars_hash
18184 ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
18187 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
18188 for CA_LOC call arg loc node. */
18190 static dw_die_ref
18191 gen_call_site_die (tree decl, dw_die_ref subr_die,
18192 struct call_arg_loc_node *ca_loc)
18194 dw_die_ref stmt_die = NULL, die;
18195 tree block = ca_loc->block;
18197 while (block
18198 && block != DECL_INITIAL (decl)
18199 && TREE_CODE (block) == BLOCK)
18201 if (block_map.length () > BLOCK_NUMBER (block))
18202 stmt_die = block_map[BLOCK_NUMBER (block)];
18203 if (stmt_die)
18204 break;
18205 block = BLOCK_SUPERCONTEXT (block);
18207 if (stmt_die == NULL)
18208 stmt_die = subr_die;
18209 die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
18210 add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
18211 if (ca_loc->tail_call_p)
18212 add_AT_flag (die, DW_AT_GNU_tail_call, 1);
18213 if (ca_loc->symbol_ref)
18215 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
18216 if (tdie)
18217 add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
18218 else
18219 add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref, false);
18221 return die;
18224 /* Generate a DIE to represent a declared function (either file-scope or
18225 block-local). */
18227 static void
18228 gen_subprogram_die (tree decl, dw_die_ref context_die)
18230 tree origin = decl_ultimate_origin (decl);
18231 dw_die_ref subr_die;
18232 tree outer_scope;
18233 dw_die_ref old_die = lookup_decl_die (decl);
18234 int declaration = (current_function_decl != decl
18235 || class_or_namespace_scope_p (context_die));
18237 premark_used_types (DECL_STRUCT_FUNCTION (decl));
18239 /* It is possible to have both DECL_ABSTRACT_P and DECLARATION be true if we
18240 started to generate the abstract instance of an inline, decided to output
18241 its containing class, and proceeded to emit the declaration of the inline
18242 from the member list for the class. If so, DECLARATION takes priority;
18243 we'll get back to the abstract instance when done with the class. */
18245 /* The class-scope declaration DIE must be the primary DIE. */
18246 if (origin && declaration && class_or_namespace_scope_p (context_die))
18248 origin = NULL;
18249 gcc_assert (!old_die);
18252 /* Now that the C++ front end lazily declares artificial member fns, we
18253 might need to retrofit the declaration into its class. */
18254 if (!declaration && !origin && !old_die
18255 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
18256 && !class_or_namespace_scope_p (context_die)
18257 && debug_info_level > DINFO_LEVEL_TERSE)
18258 old_die = force_decl_die (decl);
18260 if (origin != NULL)
18262 gcc_assert (!declaration || local_scope_p (context_die));
18264 /* Fixup die_parent for the abstract instance of a nested
18265 inline function. */
18266 if (old_die && old_die->die_parent == NULL)
18267 add_child_die (context_die, old_die);
18269 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18270 add_abstract_origin_attribute (subr_die, origin);
18271 /* This is where the actual code for a cloned function is.
18272 Let's emit linkage name attribute for it. This helps
18273 debuggers to e.g, set breakpoints into
18274 constructors/destructors when the user asks "break
18275 K::K". */
18276 add_linkage_name (subr_die, decl);
18278 else if (old_die)
18280 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18281 struct dwarf_file_data * file_index = lookup_filename (s.file);
18283 if (!get_AT_flag (old_die, DW_AT_declaration)
18284 /* We can have a normal definition following an inline one in the
18285 case of redefinition of GNU C extern inlines.
18286 It seems reasonable to use AT_specification in this case. */
18287 && !get_AT (old_die, DW_AT_inline))
18289 /* Detect and ignore this case, where we are trying to output
18290 something we have already output. */
18291 return;
18294 /* If the definition comes from the same place as the declaration,
18295 maybe use the old DIE. We always want the DIE for this function
18296 that has the *_pc attributes to be under comp_unit_die so the
18297 debugger can find it. We also need to do this for abstract
18298 instances of inlines, since the spec requires the out-of-line copy
18299 to have the same parent. For local class methods, this doesn't
18300 apply; we just use the old DIE. */
18301 if ((is_cu_die (old_die->die_parent) || context_die == NULL)
18302 && (DECL_ARTIFICIAL (decl)
18303 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
18304 && (get_AT_unsigned (old_die, DW_AT_decl_line)
18305 == (unsigned) s.line))))
18307 subr_die = old_die;
18309 /* Clear out the declaration attribute and the formal parameters.
18310 Do not remove all children, because it is possible that this
18311 declaration die was forced using force_decl_die(). In such
18312 cases die that forced declaration die (e.g. TAG_imported_module)
18313 is one of the children that we do not want to remove. */
18314 remove_AT (subr_die, DW_AT_declaration);
18315 remove_AT (subr_die, DW_AT_object_pointer);
18316 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
18318 else
18320 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18321 add_AT_specification (subr_die, old_die);
18322 add_pubname (decl, subr_die);
18323 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18324 add_AT_file (subr_die, DW_AT_decl_file, file_index);
18325 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18326 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
18328 /* If the prototype had an 'auto' or 'decltype(auto)' return type,
18329 emit the real type on the definition die. */
18330 if (is_cxx() && debug_info_level > DINFO_LEVEL_TERSE)
18332 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
18333 if (die == auto_die || die == decltype_auto_die)
18334 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18335 TYPE_UNQUALIFIED, context_die);
18339 else
18341 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18343 if (TREE_PUBLIC (decl))
18344 add_AT_flag (subr_die, DW_AT_external, 1);
18346 add_name_and_src_coords_attributes (subr_die, decl);
18347 add_pubname (decl, subr_die);
18348 if (debug_info_level > DINFO_LEVEL_TERSE)
18350 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
18351 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18352 TYPE_UNQUALIFIED, context_die);
18355 add_pure_or_virtual_attribute (subr_die, decl);
18356 if (DECL_ARTIFICIAL (decl))
18357 add_AT_flag (subr_die, DW_AT_artificial, 1);
18359 if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
18360 add_AT_flag (subr_die, DW_AT_noreturn, 1);
18362 add_accessibility_attribute (subr_die, decl);
18365 if (declaration)
18367 if (!old_die || !get_AT (old_die, DW_AT_inline))
18369 add_AT_flag (subr_die, DW_AT_declaration, 1);
18371 /* If this is an explicit function declaration then generate
18372 a DW_AT_explicit attribute. */
18373 if (lang_hooks.decls.function_decl_explicit_p (decl)
18374 && (dwarf_version >= 3 || !dwarf_strict))
18375 add_AT_flag (subr_die, DW_AT_explicit, 1);
18377 /* If this is a C++11 deleted special function member then generate
18378 a DW_AT_GNU_deleted attribute. */
18379 if (lang_hooks.decls.function_decl_deleted_p (decl)
18380 && (! dwarf_strict))
18381 add_AT_flag (subr_die, DW_AT_GNU_deleted, 1);
18383 /* The first time we see a member function, it is in the context of
18384 the class to which it belongs. We make sure of this by emitting
18385 the class first. The next time is the definition, which is
18386 handled above. The two may come from the same source text.
18388 Note that force_decl_die() forces function declaration die. It is
18389 later reused to represent definition. */
18390 equate_decl_number_to_die (decl, subr_die);
18393 else if (DECL_ABSTRACT_P (decl))
18395 if (DECL_DECLARED_INLINE_P (decl))
18397 if (cgraph_function_possibly_inlined_p (decl))
18398 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
18399 else
18400 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
18402 else
18404 if (cgraph_function_possibly_inlined_p (decl))
18405 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
18406 else
18407 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
18410 if (DECL_DECLARED_INLINE_P (decl)
18411 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
18412 add_AT_flag (subr_die, DW_AT_artificial, 1);
18414 equate_decl_number_to_die (decl, subr_die);
18416 else if (!DECL_EXTERNAL (decl))
18418 HOST_WIDE_INT cfa_fb_offset;
18419 struct function *fun = DECL_STRUCT_FUNCTION (decl);
18421 if (!old_die || !get_AT (old_die, DW_AT_inline))
18422 equate_decl_number_to_die (decl, subr_die);
18424 gcc_checking_assert (fun);
18425 if (!flag_reorder_blocks_and_partition)
18427 dw_fde_ref fde = fun->fde;
18428 if (fde->dw_fde_begin)
18430 /* We have already generated the labels. */
18431 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18432 fde->dw_fde_end, false);
18434 else
18436 /* Create start/end labels and add the range. */
18437 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
18438 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
18439 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
18440 current_function_funcdef_no);
18441 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
18442 current_function_funcdef_no);
18443 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
18444 false);
18447 #if VMS_DEBUGGING_INFO
18448 /* HP OpenVMS Industry Standard 64: DWARF Extensions
18449 Section 2.3 Prologue and Epilogue Attributes:
18450 When a breakpoint is set on entry to a function, it is generally
18451 desirable for execution to be suspended, not on the very first
18452 instruction of the function, but rather at a point after the
18453 function's frame has been set up, after any language defined local
18454 declaration processing has been completed, and before execution of
18455 the first statement of the function begins. Debuggers generally
18456 cannot properly determine where this point is. Similarly for a
18457 breakpoint set on exit from a function. The prologue and epilogue
18458 attributes allow a compiler to communicate the location(s) to use. */
18461 if (fde->dw_fde_vms_end_prologue)
18462 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
18463 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
18465 if (fde->dw_fde_vms_begin_epilogue)
18466 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
18467 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
18469 #endif
18472 else
18474 /* Generate pubnames entries for the split function code ranges. */
18475 dw_fde_ref fde = fun->fde;
18477 if (fde->dw_fde_second_begin)
18479 if (dwarf_version >= 3 || !dwarf_strict)
18481 /* We should use ranges for non-contiguous code section
18482 addresses. Use the actual code range for the initial
18483 section, since the HOT/COLD labels might precede an
18484 alignment offset. */
18485 bool range_list_added = false;
18486 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
18487 fde->dw_fde_end, &range_list_added,
18488 false);
18489 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
18490 fde->dw_fde_second_end,
18491 &range_list_added, false);
18492 if (range_list_added)
18493 add_ranges (NULL);
18495 else
18497 /* There is no real support in DW2 for this .. so we make
18498 a work-around. First, emit the pub name for the segment
18499 containing the function label. Then make and emit a
18500 simplified subprogram DIE for the second segment with the
18501 name pre-fixed by __hot/cold_sect_of_. We use the same
18502 linkage name for the second die so that gdb will find both
18503 sections when given "b foo". */
18504 const char *name = NULL;
18505 tree decl_name = DECL_NAME (decl);
18506 dw_die_ref seg_die;
18508 /* Do the 'primary' section. */
18509 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18510 fde->dw_fde_end, false);
18512 /* Build a minimal DIE for the secondary section. */
18513 seg_die = new_die (DW_TAG_subprogram,
18514 subr_die->die_parent, decl);
18516 if (TREE_PUBLIC (decl))
18517 add_AT_flag (seg_die, DW_AT_external, 1);
18519 if (decl_name != NULL
18520 && IDENTIFIER_POINTER (decl_name) != NULL)
18522 name = dwarf2_name (decl, 1);
18523 if (! DECL_ARTIFICIAL (decl))
18524 add_src_coords_attributes (seg_die, decl);
18526 add_linkage_name (seg_die, decl);
18528 gcc_assert (name != NULL);
18529 add_pure_or_virtual_attribute (seg_die, decl);
18530 if (DECL_ARTIFICIAL (decl))
18531 add_AT_flag (seg_die, DW_AT_artificial, 1);
18533 name = concat ("__second_sect_of_", name, NULL);
18534 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
18535 fde->dw_fde_second_end, false);
18536 add_name_attribute (seg_die, name);
18537 if (want_pubnames ())
18538 add_pubname_string (name, seg_die);
18541 else
18542 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
18543 false);
18546 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
18548 /* We define the "frame base" as the function's CFA. This is more
18549 convenient for several reasons: (1) It's stable across the prologue
18550 and epilogue, which makes it better than just a frame pointer,
18551 (2) With dwarf3, there exists a one-byte encoding that allows us
18552 to reference the .debug_frame data by proxy, but failing that,
18553 (3) We can at least reuse the code inspection and interpretation
18554 code that determines the CFA position at various points in the
18555 function. */
18556 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
18558 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
18559 add_AT_loc (subr_die, DW_AT_frame_base, op);
18561 else
18563 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
18564 if (list->dw_loc_next)
18565 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
18566 else
18567 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
18570 /* Compute a displacement from the "steady-state frame pointer" to
18571 the CFA. The former is what all stack slots and argument slots
18572 will reference in the rtl; the latter is what we've told the
18573 debugger about. We'll need to adjust all frame_base references
18574 by this displacement. */
18575 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
18577 if (fun->static_chain_decl)
18578 add_AT_location_description (subr_die, DW_AT_static_link,
18579 loc_list_from_tree (fun->static_chain_decl, 2));
18582 /* Generate child dies for template paramaters. */
18583 if (debug_info_level > DINFO_LEVEL_TERSE)
18584 gen_generic_params_dies (decl);
18586 /* Now output descriptions of the arguments for this function. This gets
18587 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
18588 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
18589 `...' at the end of the formal parameter list. In order to find out if
18590 there was a trailing ellipsis or not, we must instead look at the type
18591 associated with the FUNCTION_DECL. This will be a node of type
18592 FUNCTION_TYPE. If the chain of type nodes hanging off of this
18593 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
18594 an ellipsis at the end. */
18596 /* In the case where we are describing a mere function declaration, all we
18597 need to do here (and all we *can* do here) is to describe the *types* of
18598 its formal parameters. */
18599 if (debug_info_level <= DINFO_LEVEL_TERSE)
18601 else if (declaration)
18602 gen_formal_types_die (decl, subr_die);
18603 else
18605 /* Generate DIEs to represent all known formal parameters. */
18606 tree parm = DECL_ARGUMENTS (decl);
18607 tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
18608 tree generic_decl_parm = generic_decl
18609 ? DECL_ARGUMENTS (generic_decl)
18610 : NULL;
18612 /* Now we want to walk the list of parameters of the function and
18613 emit their relevant DIEs.
18615 We consider the case of DECL being an instance of a generic function
18616 as well as it being a normal function.
18618 If DECL is an instance of a generic function we walk the
18619 parameters of the generic function declaration _and_ the parameters of
18620 DECL itself. This is useful because we want to emit specific DIEs for
18621 function parameter packs and those are declared as part of the
18622 generic function declaration. In that particular case,
18623 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
18624 That DIE has children DIEs representing the set of arguments
18625 of the pack. Note that the set of pack arguments can be empty.
18626 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
18627 children DIE.
18629 Otherwise, we just consider the parameters of DECL. */
18630 while (generic_decl_parm || parm)
18632 if (generic_decl_parm
18633 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
18634 gen_formal_parameter_pack_die (generic_decl_parm,
18635 parm, subr_die,
18636 &parm);
18637 else if (parm && !POINTER_BOUNDS_P (parm))
18639 dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
18641 if (parm == DECL_ARGUMENTS (decl)
18642 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
18643 && parm_die
18644 && (dwarf_version >= 3 || !dwarf_strict))
18645 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
18647 parm = DECL_CHAIN (parm);
18649 else if (parm)
18650 parm = DECL_CHAIN (parm);
18652 if (generic_decl_parm)
18653 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
18656 /* Decide whether we need an unspecified_parameters DIE at the end.
18657 There are 2 more cases to do this for: 1) the ansi ... declaration -
18658 this is detectable when the end of the arg list is not a
18659 void_type_node 2) an unprototyped function declaration (not a
18660 definition). This just means that we have no info about the
18661 parameters at all. */
18662 if (prototype_p (TREE_TYPE (decl)))
18664 /* This is the prototyped case, check for.... */
18665 if (stdarg_p (TREE_TYPE (decl)))
18666 gen_unspecified_parameters_die (decl, subr_die);
18668 else if (DECL_INITIAL (decl) == NULL_TREE)
18669 gen_unspecified_parameters_die (decl, subr_die);
18672 /* Output Dwarf info for all of the stuff within the body of the function
18673 (if it has one - it may be just a declaration). */
18674 outer_scope = DECL_INITIAL (decl);
18676 /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
18677 a function. This BLOCK actually represents the outermost binding contour
18678 for the function, i.e. the contour in which the function's formal
18679 parameters and labels get declared. Curiously, it appears that the front
18680 end doesn't actually put the PARM_DECL nodes for the current function onto
18681 the BLOCK_VARS list for this outer scope, but are strung off of the
18682 DECL_ARGUMENTS list for the function instead.
18684 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
18685 the LABEL_DECL nodes for the function however, and we output DWARF info
18686 for those in decls_for_scope. Just within the `outer_scope' there will be
18687 a BLOCK node representing the function's outermost pair of curly braces,
18688 and any blocks used for the base and member initializers of a C++
18689 constructor function. */
18690 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
18692 int call_site_note_count = 0;
18693 int tail_call_site_note_count = 0;
18695 /* Emit a DW_TAG_variable DIE for a named return value. */
18696 if (DECL_NAME (DECL_RESULT (decl)))
18697 gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
18699 decls_for_scope (outer_scope, subr_die, 0);
18701 if (call_arg_locations && !dwarf_strict)
18703 struct call_arg_loc_node *ca_loc;
18704 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
18706 dw_die_ref die = NULL;
18707 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
18708 rtx arg, next_arg;
18710 for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
18711 arg; arg = next_arg)
18713 dw_loc_descr_ref reg, val;
18714 machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
18715 dw_die_ref cdie, tdie = NULL;
18717 next_arg = XEXP (arg, 1);
18718 if (REG_P (XEXP (XEXP (arg, 0), 0))
18719 && next_arg
18720 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
18721 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
18722 && REGNO (XEXP (XEXP (arg, 0), 0))
18723 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
18724 next_arg = XEXP (next_arg, 1);
18725 if (mode == VOIDmode)
18727 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
18728 if (mode == VOIDmode)
18729 mode = GET_MODE (XEXP (arg, 0));
18731 if (mode == VOIDmode || mode == BLKmode)
18732 continue;
18733 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
18735 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18736 tloc = XEXP (XEXP (arg, 0), 1);
18737 continue;
18739 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
18740 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
18742 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18743 tlocc = XEXP (XEXP (arg, 0), 1);
18744 continue;
18746 reg = NULL;
18747 if (REG_P (XEXP (XEXP (arg, 0), 0)))
18748 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
18749 VAR_INIT_STATUS_INITIALIZED);
18750 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
18752 rtx mem = XEXP (XEXP (arg, 0), 0);
18753 reg = mem_loc_descriptor (XEXP (mem, 0),
18754 get_address_mode (mem),
18755 GET_MODE (mem),
18756 VAR_INIT_STATUS_INITIALIZED);
18758 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
18759 == DEBUG_PARAMETER_REF)
18761 tree tdecl
18762 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
18763 tdie = lookup_decl_die (tdecl);
18764 if (tdie == NULL)
18765 continue;
18767 else
18768 continue;
18769 if (reg == NULL
18770 && GET_CODE (XEXP (XEXP (arg, 0), 0))
18771 != DEBUG_PARAMETER_REF)
18772 continue;
18773 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
18774 VOIDmode,
18775 VAR_INIT_STATUS_INITIALIZED);
18776 if (val == NULL)
18777 continue;
18778 if (die == NULL)
18779 die = gen_call_site_die (decl, subr_die, ca_loc);
18780 cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
18781 NULL_TREE);
18782 if (reg != NULL)
18783 add_AT_loc (cdie, DW_AT_location, reg);
18784 else if (tdie != NULL)
18785 add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
18786 add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
18787 if (next_arg != XEXP (arg, 1))
18789 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
18790 if (mode == VOIDmode)
18791 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
18792 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
18793 0), 1),
18794 mode, VOIDmode,
18795 VAR_INIT_STATUS_INITIALIZED);
18796 if (val != NULL)
18797 add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
18800 if (die == NULL
18801 && (ca_loc->symbol_ref || tloc))
18802 die = gen_call_site_die (decl, subr_die, ca_loc);
18803 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
18805 dw_loc_descr_ref tval = NULL;
18807 if (tloc != NULL_RTX)
18808 tval = mem_loc_descriptor (tloc,
18809 GET_MODE (tloc) == VOIDmode
18810 ? Pmode : GET_MODE (tloc),
18811 VOIDmode,
18812 VAR_INIT_STATUS_INITIALIZED);
18813 if (tval)
18814 add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
18815 else if (tlocc != NULL_RTX)
18817 tval = mem_loc_descriptor (tlocc,
18818 GET_MODE (tlocc) == VOIDmode
18819 ? Pmode : GET_MODE (tlocc),
18820 VOIDmode,
18821 VAR_INIT_STATUS_INITIALIZED);
18822 if (tval)
18823 add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
18824 tval);
18827 if (die != NULL)
18829 call_site_note_count++;
18830 if (ca_loc->tail_call_p)
18831 tail_call_site_note_count++;
18835 call_arg_locations = NULL;
18836 call_arg_loc_last = NULL;
18837 if (tail_call_site_count >= 0
18838 && tail_call_site_count == tail_call_site_note_count
18839 && !dwarf_strict)
18841 if (call_site_count >= 0
18842 && call_site_count == call_site_note_count)
18843 add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
18844 else
18845 add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
18847 call_site_count = -1;
18848 tail_call_site_count = -1;
18851 if (subr_die != old_die)
18852 /* Add the calling convention attribute if requested. */
18853 add_calling_convention_attribute (subr_die, decl);
18856 /* Returns a hash value for X (which really is a die_struct). */
18858 hashval_t
18859 block_die_hasher::hash (die_struct *d)
18861 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
18864 /* Return nonzero if decl_id and die_parent of die_struct X is the same
18865 as decl_id and die_parent of die_struct Y. */
18867 bool
18868 block_die_hasher::equal (die_struct *x, die_struct *y)
18870 return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
18873 /* Generate a DIE to represent a declared data object.
18874 Either DECL or ORIGIN must be non-null. */
18876 static void
18877 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
18879 HOST_WIDE_INT off = 0;
18880 tree com_decl;
18881 tree decl_or_origin = decl ? decl : origin;
18882 tree ultimate_origin;
18883 dw_die_ref var_die;
18884 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
18885 dw_die_ref origin_die;
18886 bool declaration = (DECL_EXTERNAL (decl_or_origin)
18887 || class_or_namespace_scope_p (context_die));
18888 bool specialization_p = false;
18890 ultimate_origin = decl_ultimate_origin (decl_or_origin);
18891 if (decl || ultimate_origin)
18892 origin = ultimate_origin;
18893 com_decl = fortran_common (decl_or_origin, &off);
18895 /* Symbol in common gets emitted as a child of the common block, in the form
18896 of a data member. */
18897 if (com_decl)
18899 dw_die_ref com_die;
18900 dw_loc_list_ref loc;
18901 die_node com_die_arg;
18903 var_die = lookup_decl_die (decl_or_origin);
18904 if (var_die)
18906 if (get_AT (var_die, DW_AT_location) == NULL)
18908 loc = loc_list_from_tree (com_decl, off ? 1 : 2);
18909 if (loc)
18911 if (off)
18913 /* Optimize the common case. */
18914 if (single_element_loc_list_p (loc)
18915 && loc->expr->dw_loc_opc == DW_OP_addr
18916 && loc->expr->dw_loc_next == NULL
18917 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
18918 == SYMBOL_REF)
18920 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
18921 loc->expr->dw_loc_oprnd1.v.val_addr
18922 = plus_constant (GET_MODE (x), x , off);
18924 else
18925 loc_list_plus_const (loc, off);
18927 add_AT_location_description (var_die, DW_AT_location, loc);
18928 remove_AT (var_die, DW_AT_declaration);
18931 return;
18934 if (common_block_die_table == NULL)
18935 common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
18937 com_die_arg.decl_id = DECL_UID (com_decl);
18938 com_die_arg.die_parent = context_die;
18939 com_die = common_block_die_table->find (&com_die_arg);
18940 loc = loc_list_from_tree (com_decl, 2);
18941 if (com_die == NULL)
18943 const char *cnam
18944 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
18945 die_node **slot;
18947 com_die = new_die (DW_TAG_common_block, context_die, decl);
18948 add_name_and_src_coords_attributes (com_die, com_decl);
18949 if (loc)
18951 add_AT_location_description (com_die, DW_AT_location, loc);
18952 /* Avoid sharing the same loc descriptor between
18953 DW_TAG_common_block and DW_TAG_variable. */
18954 loc = loc_list_from_tree (com_decl, 2);
18956 else if (DECL_EXTERNAL (decl))
18957 add_AT_flag (com_die, DW_AT_declaration, 1);
18958 if (want_pubnames ())
18959 add_pubname_string (cnam, com_die); /* ??? needed? */
18960 com_die->decl_id = DECL_UID (com_decl);
18961 slot = common_block_die_table->find_slot (com_die, INSERT);
18962 *slot = com_die;
18964 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
18966 add_AT_location_description (com_die, DW_AT_location, loc);
18967 loc = loc_list_from_tree (com_decl, 2);
18968 remove_AT (com_die, DW_AT_declaration);
18970 var_die = new_die (DW_TAG_variable, com_die, decl);
18971 add_name_and_src_coords_attributes (var_die, decl);
18972 add_type_attribute (var_die, TREE_TYPE (decl), decl_quals (decl),
18973 context_die);
18974 add_AT_flag (var_die, DW_AT_external, 1);
18975 if (loc)
18977 if (off)
18979 /* Optimize the common case. */
18980 if (single_element_loc_list_p (loc)
18981 && loc->expr->dw_loc_opc == DW_OP_addr
18982 && loc->expr->dw_loc_next == NULL
18983 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
18985 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
18986 loc->expr->dw_loc_oprnd1.v.val_addr
18987 = plus_constant (GET_MODE (x), x, off);
18989 else
18990 loc_list_plus_const (loc, off);
18992 add_AT_location_description (var_die, DW_AT_location, loc);
18994 else if (DECL_EXTERNAL (decl))
18995 add_AT_flag (var_die, DW_AT_declaration, 1);
18996 equate_decl_number_to_die (decl, var_die);
18997 return;
19000 /* If the compiler emitted a definition for the DECL declaration
19001 and if we already emitted a DIE for it, don't emit a second
19002 DIE for it again. Allow re-declarations of DECLs that are
19003 inside functions, though. */
19004 if (old_die && declaration && !local_scope_p (context_die))
19005 return;
19007 /* For static data members, the declaration in the class is supposed
19008 to have DW_TAG_member tag; the specification should still be
19009 DW_TAG_variable referencing the DW_TAG_member DIE. */
19010 if (declaration && class_scope_p (context_die))
19011 var_die = new_die (DW_TAG_member, context_die, decl);
19012 else
19013 var_die = new_die (DW_TAG_variable, context_die, decl);
19015 origin_die = NULL;
19016 if (origin != NULL)
19017 origin_die = add_abstract_origin_attribute (var_die, origin);
19019 /* Loop unrolling can create multiple blocks that refer to the same
19020 static variable, so we must test for the DW_AT_declaration flag.
19022 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
19023 copy decls and set the DECL_ABSTRACT_P flag on them instead of
19024 sharing them.
19026 ??? Duplicated blocks have been rewritten to use .debug_ranges.
19028 ??? The declare_in_namespace support causes us to get two DIEs for one
19029 variable, both of which are declarations. We want to avoid considering
19030 one to be a specification, so we must test that this DIE is not a
19031 declaration. */
19032 else if (old_die && TREE_STATIC (decl) && ! declaration
19033 && get_AT_flag (old_die, DW_AT_declaration) == 1)
19035 /* This is a definition of a C++ class level static. */
19036 add_AT_specification (var_die, old_die);
19037 specialization_p = true;
19038 if (DECL_NAME (decl))
19040 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
19041 struct dwarf_file_data * file_index = lookup_filename (s.file);
19043 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
19044 add_AT_file (var_die, DW_AT_decl_file, file_index);
19046 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
19047 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
19049 if (old_die->die_tag == DW_TAG_member)
19050 add_linkage_name (var_die, decl);
19053 else
19054 add_name_and_src_coords_attributes (var_die, decl);
19056 if ((origin == NULL && !specialization_p)
19057 || (origin != NULL
19058 && !DECL_ABSTRACT_P (decl_or_origin)
19059 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
19060 decl_function_context
19061 (decl_or_origin))))
19063 tree type = TREE_TYPE (decl_or_origin);
19065 if (decl_by_reference_p (decl_or_origin))
19066 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19067 context_die);
19068 else
19069 add_type_attribute (var_die, type, decl_quals (decl_or_origin),
19070 context_die);
19073 if (origin == NULL && !specialization_p)
19075 if (TREE_PUBLIC (decl))
19076 add_AT_flag (var_die, DW_AT_external, 1);
19078 if (DECL_ARTIFICIAL (decl))
19079 add_AT_flag (var_die, DW_AT_artificial, 1);
19081 add_accessibility_attribute (var_die, decl);
19084 if (declaration)
19085 add_AT_flag (var_die, DW_AT_declaration, 1);
19087 if (decl && (DECL_ABSTRACT_P (decl) || declaration || old_die == NULL))
19088 equate_decl_number_to_die (decl, var_die);
19090 if (! declaration
19091 && (! DECL_ABSTRACT_P (decl_or_origin)
19092 /* Local static vars are shared between all clones/inlines,
19093 so emit DW_AT_location on the abstract DIE if DECL_RTL is
19094 already set. */
19095 || (TREE_CODE (decl_or_origin) == VAR_DECL
19096 && TREE_STATIC (decl_or_origin)
19097 && DECL_RTL_SET_P (decl_or_origin)))
19098 /* When abstract origin already has DW_AT_location attribute, no need
19099 to add it again. */
19100 && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
19102 if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
19103 && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
19104 defer_location (decl_or_origin, var_die);
19105 else
19106 add_location_or_const_value_attribute (var_die, decl_or_origin,
19107 decl == NULL, DW_AT_location);
19108 add_pubname (decl_or_origin, var_die);
19110 else
19111 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
19114 /* Generate a DIE to represent a named constant. */
19116 static void
19117 gen_const_die (tree decl, dw_die_ref context_die)
19119 dw_die_ref const_die;
19120 tree type = TREE_TYPE (decl);
19122 const_die = new_die (DW_TAG_constant, context_die, decl);
19123 add_name_and_src_coords_attributes (const_die, decl);
19124 add_type_attribute (const_die, type, TYPE_QUAL_CONST, context_die);
19125 if (TREE_PUBLIC (decl))
19126 add_AT_flag (const_die, DW_AT_external, 1);
19127 if (DECL_ARTIFICIAL (decl))
19128 add_AT_flag (const_die, DW_AT_artificial, 1);
19129 tree_add_const_value_attribute_for_decl (const_die, decl);
19132 /* Generate a DIE to represent a label identifier. */
19134 static void
19135 gen_label_die (tree decl, dw_die_ref context_die)
19137 tree origin = decl_ultimate_origin (decl);
19138 dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
19139 rtx insn;
19140 char label[MAX_ARTIFICIAL_LABEL_BYTES];
19142 if (origin != NULL)
19143 add_abstract_origin_attribute (lbl_die, origin);
19144 else
19145 add_name_and_src_coords_attributes (lbl_die, decl);
19147 if (DECL_ABSTRACT_P (decl))
19148 equate_decl_number_to_die (decl, lbl_die);
19149 else
19151 insn = DECL_RTL_IF_SET (decl);
19153 /* Deleted labels are programmer specified labels which have been
19154 eliminated because of various optimizations. We still emit them
19155 here so that it is possible to put breakpoints on them. */
19156 if (insn
19157 && (LABEL_P (insn)
19158 || ((NOTE_P (insn)
19159 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
19161 /* When optimization is enabled (via -O) some parts of the compiler
19162 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
19163 represent source-level labels which were explicitly declared by
19164 the user. This really shouldn't be happening though, so catch
19165 it if it ever does happen. */
19166 gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
19168 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
19169 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19171 else if (insn
19172 && NOTE_P (insn)
19173 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
19174 && CODE_LABEL_NUMBER (insn) != -1)
19176 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
19177 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19182 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
19183 attributes to the DIE for a block STMT, to describe where the inlined
19184 function was called from. This is similar to add_src_coords_attributes. */
19186 static inline void
19187 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
19189 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
19191 if (dwarf_version >= 3 || !dwarf_strict)
19193 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
19194 add_AT_unsigned (die, DW_AT_call_line, s.line);
19199 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
19200 Add low_pc and high_pc attributes to the DIE for a block STMT. */
19202 static inline void
19203 add_high_low_attributes (tree stmt, dw_die_ref die)
19205 char label[MAX_ARTIFICIAL_LABEL_BYTES];
19207 if (BLOCK_FRAGMENT_CHAIN (stmt)
19208 && (dwarf_version >= 3 || !dwarf_strict))
19210 tree chain, superblock = NULL_TREE;
19211 dw_die_ref pdie;
19212 dw_attr_ref attr = NULL;
19214 if (inlined_function_outer_scope_p (stmt))
19216 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19217 BLOCK_NUMBER (stmt));
19218 add_AT_lbl_id (die, DW_AT_entry_pc, label);
19221 /* Optimize duplicate .debug_ranges lists or even tails of
19222 lists. If this BLOCK has same ranges as its supercontext,
19223 lookup DW_AT_ranges attribute in the supercontext (and
19224 recursively so), verify that the ranges_table contains the
19225 right values and use it instead of adding a new .debug_range. */
19226 for (chain = stmt, pdie = die;
19227 BLOCK_SAME_RANGE (chain);
19228 chain = BLOCK_SUPERCONTEXT (chain))
19230 dw_attr_ref new_attr;
19232 pdie = pdie->die_parent;
19233 if (pdie == NULL)
19234 break;
19235 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
19236 break;
19237 new_attr = get_AT (pdie, DW_AT_ranges);
19238 if (new_attr == NULL
19239 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
19240 break;
19241 attr = new_attr;
19242 superblock = BLOCK_SUPERCONTEXT (chain);
19244 if (attr != NULL
19245 && (ranges_table[attr->dw_attr_val.v.val_offset
19246 / 2 / DWARF2_ADDR_SIZE].num
19247 == BLOCK_NUMBER (superblock))
19248 && BLOCK_FRAGMENT_CHAIN (superblock))
19250 unsigned long off = attr->dw_attr_val.v.val_offset
19251 / 2 / DWARF2_ADDR_SIZE;
19252 unsigned long supercnt = 0, thiscnt = 0;
19253 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
19254 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
19256 ++supercnt;
19257 gcc_checking_assert (ranges_table[off + supercnt].num
19258 == BLOCK_NUMBER (chain));
19260 gcc_checking_assert (ranges_table[off + supercnt + 1].num == 0);
19261 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
19262 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
19263 ++thiscnt;
19264 gcc_assert (supercnt >= thiscnt);
19265 add_AT_range_list (die, DW_AT_ranges,
19266 ((off + supercnt - thiscnt)
19267 * 2 * DWARF2_ADDR_SIZE),
19268 false);
19269 return;
19272 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt), false);
19274 chain = BLOCK_FRAGMENT_CHAIN (stmt);
19277 add_ranges (chain);
19278 chain = BLOCK_FRAGMENT_CHAIN (chain);
19280 while (chain);
19281 add_ranges (NULL);
19283 else
19285 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
19286 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19287 BLOCK_NUMBER (stmt));
19288 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
19289 BLOCK_NUMBER (stmt));
19290 add_AT_low_high_pc (die, label, label_high, false);
19294 /* Generate a DIE for a lexical block. */
19296 static void
19297 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
19299 dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
19301 if (call_arg_locations)
19303 if (block_map.length () <= BLOCK_NUMBER (stmt))
19304 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
19305 block_map[BLOCK_NUMBER (stmt)] = stmt_die;
19308 if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
19309 add_high_low_attributes (stmt, stmt_die);
19311 decls_for_scope (stmt, stmt_die, depth);
19314 /* Generate a DIE for an inlined subprogram. */
19316 static void
19317 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
19319 tree decl;
19321 /* The instance of function that is effectively being inlined shall not
19322 be abstract. */
19323 gcc_assert (! BLOCK_ABSTRACT (stmt));
19325 decl = block_ultimate_origin (stmt);
19327 /* Emit info for the abstract instance first, if we haven't yet. We
19328 must emit this even if the block is abstract, otherwise when we
19329 emit the block below (or elsewhere), we may end up trying to emit
19330 a die whose origin die hasn't been emitted, and crashing. */
19331 dwarf2out_abstract_function (decl);
19333 if (! BLOCK_ABSTRACT (stmt))
19335 dw_die_ref subr_die
19336 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
19338 if (call_arg_locations)
19340 if (block_map.length () <= BLOCK_NUMBER (stmt))
19341 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
19342 block_map[BLOCK_NUMBER (stmt)] = subr_die;
19344 add_abstract_origin_attribute (subr_die, decl);
19345 if (TREE_ASM_WRITTEN (stmt))
19346 add_high_low_attributes (stmt, subr_die);
19347 add_call_src_coords_attributes (stmt, subr_die);
19349 decls_for_scope (stmt, subr_die, depth);
19353 /* Generate a DIE for a field in a record, or structure. */
19355 static void
19356 gen_field_die (tree decl, dw_die_ref context_die)
19358 dw_die_ref decl_die;
19360 if (TREE_TYPE (decl) == error_mark_node)
19361 return;
19363 decl_die = new_die (DW_TAG_member, context_die, decl);
19364 add_name_and_src_coords_attributes (decl_die, decl);
19365 add_type_attribute (decl_die, member_declared_type (decl),
19366 decl_quals (decl), context_die);
19368 if (DECL_BIT_FIELD_TYPE (decl))
19370 add_byte_size_attribute (decl_die, decl);
19371 add_bit_size_attribute (decl_die, decl);
19372 add_bit_offset_attribute (decl_die, decl);
19375 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
19376 add_data_member_location_attribute (decl_die, decl);
19378 if (DECL_ARTIFICIAL (decl))
19379 add_AT_flag (decl_die, DW_AT_artificial, 1);
19381 add_accessibility_attribute (decl_die, decl);
19383 /* Equate decl number to die, so that we can look up this decl later on. */
19384 equate_decl_number_to_die (decl, decl_die);
19387 #if 0
19388 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19389 Use modified_type_die instead.
19390 We keep this code here just in case these types of DIEs may be needed to
19391 represent certain things in other languages (e.g. Pascal) someday. */
19393 static void
19394 gen_pointer_type_die (tree type, dw_die_ref context_die)
19396 dw_die_ref ptr_die
19397 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
19399 equate_type_number_to_die (type, ptr_die);
19400 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19401 context_die);
19402 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19405 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19406 Use modified_type_die instead.
19407 We keep this code here just in case these types of DIEs may be needed to
19408 represent certain things in other languages (e.g. Pascal) someday. */
19410 static void
19411 gen_reference_type_die (tree type, dw_die_ref context_die)
19413 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
19415 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
19416 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
19417 else
19418 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
19420 equate_type_number_to_die (type, ref_die);
19421 add_type_attribute (ref_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19422 context_die);
19423 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19425 #endif
19427 /* Generate a DIE for a pointer to a member type. */
19429 static void
19430 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
19432 dw_die_ref ptr_die
19433 = new_die (DW_TAG_ptr_to_member_type,
19434 scope_die_for (type, context_die), type);
19436 equate_type_number_to_die (type, ptr_die);
19437 add_AT_die_ref (ptr_die, DW_AT_containing_type,
19438 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
19439 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19440 context_die);
19443 typedef const char *dchar_p; /* For DEF_VEC_P. */
19445 static char *producer_string;
19447 /* Return a heap allocated producer string including command line options
19448 if -grecord-gcc-switches. */
19450 static char *
19451 gen_producer_string (void)
19453 size_t j;
19454 auto_vec<dchar_p> switches;
19455 const char *language_string = lang_hooks.name;
19456 char *producer, *tail;
19457 const char *p;
19458 size_t len = dwarf_record_gcc_switches ? 0 : 3;
19459 size_t plen = strlen (language_string) + 1 + strlen (version_string);
19461 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
19462 switch (save_decoded_options[j].opt_index)
19464 case OPT_o:
19465 case OPT_d:
19466 case OPT_dumpbase:
19467 case OPT_dumpdir:
19468 case OPT_auxbase:
19469 case OPT_auxbase_strip:
19470 case OPT_quiet:
19471 case OPT_version:
19472 case OPT_v:
19473 case OPT_w:
19474 case OPT_L:
19475 case OPT_D:
19476 case OPT_I:
19477 case OPT_U:
19478 case OPT_SPECIAL_unknown:
19479 case OPT_SPECIAL_ignore:
19480 case OPT_SPECIAL_program_name:
19481 case OPT_SPECIAL_input_file:
19482 case OPT_grecord_gcc_switches:
19483 case OPT_gno_record_gcc_switches:
19484 case OPT__output_pch_:
19485 case OPT_fdiagnostics_show_location_:
19486 case OPT_fdiagnostics_show_option:
19487 case OPT_fdiagnostics_show_caret:
19488 case OPT_fdiagnostics_color_:
19489 case OPT_fverbose_asm:
19490 case OPT____:
19491 case OPT__sysroot_:
19492 case OPT_nostdinc:
19493 case OPT_nostdinc__:
19494 /* Ignore these. */
19495 continue;
19496 default:
19497 if (cl_options[save_decoded_options[j].opt_index].flags
19498 & CL_NO_DWARF_RECORD)
19499 continue;
19500 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
19501 == '-');
19502 switch (save_decoded_options[j].canonical_option[0][1])
19504 case 'M':
19505 case 'i':
19506 case 'W':
19507 continue;
19508 case 'f':
19509 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
19510 "dump", 4) == 0)
19511 continue;
19512 break;
19513 default:
19514 break;
19516 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
19517 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
19518 break;
19521 producer = XNEWVEC (char, plen + 1 + len + 1);
19522 tail = producer;
19523 sprintf (tail, "%s %s", language_string, version_string);
19524 tail += plen;
19526 FOR_EACH_VEC_ELT (switches, j, p)
19528 len = strlen (p);
19529 *tail = ' ';
19530 memcpy (tail + 1, p, len);
19531 tail += len + 1;
19534 *tail = '\0';
19535 return producer;
19538 /* Given a C and/or C++ language/version string return the "highest".
19539 C++ is assumed to be "higher" than C in this case. Used for merging
19540 LTO translation unit languages. */
19541 static const char *
19542 highest_c_language (const char *lang1, const char *lang2)
19544 if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
19545 return "GNU C++14";
19546 if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
19547 return "GNU C++11";
19548 if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
19549 return "GNU C++98";
19551 if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
19552 return "GNU C11";
19553 if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
19554 return "GNU C99";
19555 if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
19556 return "GNU C89";
19558 gcc_unreachable ();
19562 /* Generate the DIE for the compilation unit. */
19564 static dw_die_ref
19565 gen_compile_unit_die (const char *filename)
19567 dw_die_ref die;
19568 const char *language_string = lang_hooks.name;
19569 int language;
19571 die = new_die (DW_TAG_compile_unit, NULL, NULL);
19573 if (filename)
19575 add_name_attribute (die, filename);
19576 /* Don't add cwd for <built-in>. */
19577 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
19578 add_comp_dir_attribute (die);
19581 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
19583 /* If our producer is LTO try to figure out a common language to use
19584 from the global list of translation units. */
19585 if (strcmp (language_string, "GNU GIMPLE") == 0)
19587 unsigned i;
19588 tree t;
19589 const char *common_lang = NULL;
19591 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
19593 if (!TRANSLATION_UNIT_LANGUAGE (t))
19594 continue;
19595 if (!common_lang)
19596 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
19597 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
19599 else if (strncmp (common_lang, "GNU C", 5) == 0
19600 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
19601 /* Mixing C and C++ is ok, use C++ in that case. */
19602 common_lang = highest_c_language (common_lang,
19603 TRANSLATION_UNIT_LANGUAGE (t));
19604 else
19606 /* Fall back to C. */
19607 common_lang = NULL;
19608 break;
19612 if (common_lang)
19613 language_string = common_lang;
19616 language = DW_LANG_C;
19617 if (strncmp (language_string, "GNU C", 5) == 0
19618 && ISDIGIT (language_string[5]))
19620 language = DW_LANG_C89;
19621 if (dwarf_version >= 3 || !dwarf_strict)
19623 if (strcmp (language_string, "GNU C89") != 0)
19624 language = DW_LANG_C99;
19626 if (dwarf_version >= 5 /* || !dwarf_strict */)
19627 if (strcmp (language_string, "GNU C11") == 0)
19628 language = DW_LANG_C11;
19631 else if (strncmp (language_string, "GNU C++", 7) == 0)
19633 language = DW_LANG_C_plus_plus;
19634 if (dwarf_version >= 5 /* || !dwarf_strict */)
19636 if (strcmp (language_string, "GNU C++11") == 0)
19637 language = DW_LANG_C_plus_plus_11;
19638 else if (strcmp (language_string, "GNU C++14") == 0)
19639 language = DW_LANG_C_plus_plus_14;
19642 else if (strcmp (language_string, "GNU F77") == 0)
19643 language = DW_LANG_Fortran77;
19644 else if (strcmp (language_string, "GNU Pascal") == 0)
19645 language = DW_LANG_Pascal83;
19646 else if (dwarf_version >= 3 || !dwarf_strict)
19648 if (strcmp (language_string, "GNU Ada") == 0)
19649 language = DW_LANG_Ada95;
19650 else if (strcmp (language_string, "GNU Fortran") == 0)
19651 language = DW_LANG_Fortran95;
19652 else if (strcmp (language_string, "GNU Java") == 0)
19653 language = DW_LANG_Java;
19654 else if (strcmp (language_string, "GNU Objective-C") == 0)
19655 language = DW_LANG_ObjC;
19656 else if (strcmp (language_string, "GNU Objective-C++") == 0)
19657 language = DW_LANG_ObjC_plus_plus;
19658 else if (dwarf_version >= 5 || !dwarf_strict)
19660 if (strcmp (language_string, "GNU Go") == 0)
19661 language = DW_LANG_Go;
19664 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
19665 else if (strcmp (language_string, "GNU Fortran") == 0)
19666 language = DW_LANG_Fortran90;
19668 add_AT_unsigned (die, DW_AT_language, language);
19670 switch (language)
19672 case DW_LANG_Fortran77:
19673 case DW_LANG_Fortran90:
19674 case DW_LANG_Fortran95:
19675 /* Fortran has case insensitive identifiers and the front-end
19676 lowercases everything. */
19677 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
19678 break;
19679 default:
19680 /* The default DW_ID_case_sensitive doesn't need to be specified. */
19681 break;
19683 return die;
19686 /* Generate the DIE for a base class. */
19688 static void
19689 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
19691 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
19693 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, context_die);
19694 add_data_member_location_attribute (die, binfo);
19696 if (BINFO_VIRTUAL_P (binfo))
19697 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
19699 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
19700 children, otherwise the default is DW_ACCESS_public. In DWARF2
19701 the default has always been DW_ACCESS_private. */
19702 if (access == access_public_node)
19704 if (dwarf_version == 2
19705 || context_die->die_tag == DW_TAG_class_type)
19706 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
19708 else if (access == access_protected_node)
19709 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
19710 else if (dwarf_version > 2
19711 && context_die->die_tag != DW_TAG_class_type)
19712 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
19715 /* Generate a DIE for a class member. */
19717 static void
19718 gen_member_die (tree type, dw_die_ref context_die)
19720 tree member;
19721 tree binfo = TYPE_BINFO (type);
19722 dw_die_ref child;
19724 /* If this is not an incomplete type, output descriptions of each of its
19725 members. Note that as we output the DIEs necessary to represent the
19726 members of this record or union type, we will also be trying to output
19727 DIEs to represent the *types* of those members. However the `type'
19728 function (above) will specifically avoid generating type DIEs for member
19729 types *within* the list of member DIEs for this (containing) type except
19730 for those types (of members) which are explicitly marked as also being
19731 members of this (containing) type themselves. The g++ front- end can
19732 force any given type to be treated as a member of some other (containing)
19733 type by setting the TYPE_CONTEXT of the given (member) type to point to
19734 the TREE node representing the appropriate (containing) type. */
19736 /* First output info about the base classes. */
19737 if (binfo)
19739 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
19740 int i;
19741 tree base;
19743 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
19744 gen_inheritance_die (base,
19745 (accesses ? (*accesses)[i] : access_public_node),
19746 context_die);
19749 /* Now output info about the data members and type members. */
19750 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
19752 /* If we thought we were generating minimal debug info for TYPE
19753 and then changed our minds, some of the member declarations
19754 may have already been defined. Don't define them again, but
19755 do put them in the right order. */
19757 child = lookup_decl_die (member);
19758 if (child)
19759 splice_child_die (context_die, child);
19760 else
19761 gen_decl_die (member, NULL, context_die);
19764 /* Now output info about the function members (if any). */
19765 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
19767 /* Don't include clones in the member list. */
19768 if (DECL_ABSTRACT_ORIGIN (member))
19769 continue;
19771 child = lookup_decl_die (member);
19772 if (child)
19773 splice_child_die (context_die, child);
19774 else
19775 gen_decl_die (member, NULL, context_die);
19779 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
19780 is set, we pretend that the type was never defined, so we only get the
19781 member DIEs needed by later specification DIEs. */
19783 static void
19784 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
19785 enum debug_info_usage usage)
19787 dw_die_ref type_die = lookup_type_die (type);
19788 dw_die_ref scope_die = 0;
19789 int nested = 0;
19790 int complete = (TYPE_SIZE (type)
19791 && (! TYPE_STUB_DECL (type)
19792 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
19793 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
19794 complete = complete && should_emit_struct_debug (type, usage);
19796 if (type_die && ! complete)
19797 return;
19799 if (TYPE_CONTEXT (type) != NULL_TREE
19800 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19801 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
19802 nested = 1;
19804 scope_die = scope_die_for (type, context_die);
19806 /* Generate child dies for template paramaters. */
19807 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
19808 schedule_generic_params_dies_gen (type);
19810 if (! type_die || (nested && is_cu_die (scope_die)))
19811 /* First occurrence of type or toplevel definition of nested class. */
19813 dw_die_ref old_die = type_die;
19815 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
19816 ? record_type_tag (type) : DW_TAG_union_type,
19817 scope_die, type);
19818 equate_type_number_to_die (type, type_die);
19819 if (old_die)
19820 add_AT_specification (type_die, old_die);
19821 else
19822 add_name_attribute (type_die, type_tag (type));
19824 else
19825 remove_AT (type_die, DW_AT_declaration);
19827 /* If this type has been completed, then give it a byte_size attribute and
19828 then give a list of members. */
19829 if (complete && !ns_decl)
19831 /* Prevent infinite recursion in cases where the type of some member of
19832 this type is expressed in terms of this type itself. */
19833 TREE_ASM_WRITTEN (type) = 1;
19834 add_byte_size_attribute (type_die, type);
19835 if (TYPE_STUB_DECL (type) != NULL_TREE)
19837 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
19838 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
19841 /* If the first reference to this type was as the return type of an
19842 inline function, then it may not have a parent. Fix this now. */
19843 if (type_die->die_parent == NULL)
19844 add_child_die (scope_die, type_die);
19846 push_decl_scope (type);
19847 gen_member_die (type, type_die);
19848 pop_decl_scope ();
19850 add_gnat_descriptive_type_attribute (type_die, type, context_die);
19851 if (TYPE_ARTIFICIAL (type))
19852 add_AT_flag (type_die, DW_AT_artificial, 1);
19854 /* GNU extension: Record what type our vtable lives in. */
19855 if (TYPE_VFIELD (type))
19857 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
19859 gen_type_die (vtype, context_die);
19860 add_AT_die_ref (type_die, DW_AT_containing_type,
19861 lookup_type_die (vtype));
19864 else
19866 add_AT_flag (type_die, DW_AT_declaration, 1);
19868 /* We don't need to do this for function-local types. */
19869 if (TYPE_STUB_DECL (type)
19870 && ! decl_function_context (TYPE_STUB_DECL (type)))
19871 vec_safe_push (incomplete_types, type);
19874 if (get_AT (type_die, DW_AT_name))
19875 add_pubtype (type, type_die);
19878 /* Generate a DIE for a subroutine _type_. */
19880 static void
19881 gen_subroutine_type_die (tree type, dw_die_ref context_die)
19883 tree return_type = TREE_TYPE (type);
19884 dw_die_ref subr_die
19885 = new_die (DW_TAG_subroutine_type,
19886 scope_die_for (type, context_die), type);
19888 equate_type_number_to_die (type, subr_die);
19889 add_prototyped_attribute (subr_die, type);
19890 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, context_die);
19891 gen_formal_types_die (type, subr_die);
19893 if (get_AT (subr_die, DW_AT_name))
19894 add_pubtype (type, subr_die);
19897 /* Generate a DIE for a type definition. */
19899 static void
19900 gen_typedef_die (tree decl, dw_die_ref context_die)
19902 dw_die_ref type_die;
19903 tree origin;
19905 if (TREE_ASM_WRITTEN (decl))
19906 return;
19908 TREE_ASM_WRITTEN (decl) = 1;
19909 type_die = new_die (DW_TAG_typedef, context_die, decl);
19910 origin = decl_ultimate_origin (decl);
19911 if (origin != NULL)
19912 add_abstract_origin_attribute (type_die, origin);
19913 else
19915 tree type;
19917 add_name_and_src_coords_attributes (type_die, decl);
19918 if (DECL_ORIGINAL_TYPE (decl))
19920 type = DECL_ORIGINAL_TYPE (decl);
19922 gcc_assert (type != TREE_TYPE (decl));
19923 equate_type_number_to_die (TREE_TYPE (decl), type_die);
19925 else
19927 type = TREE_TYPE (decl);
19929 if (is_naming_typedef_decl (TYPE_NAME (type)))
19931 /* Here, we are in the case of decl being a typedef naming
19932 an anonymous type, e.g:
19933 typedef struct {...} foo;
19934 In that case TREE_TYPE (decl) is not a typedef variant
19935 type and TYPE_NAME of the anonymous type is set to the
19936 TYPE_DECL of the typedef. This construct is emitted by
19937 the C++ FE.
19939 TYPE is the anonymous struct named by the typedef
19940 DECL. As we need the DW_AT_type attribute of the
19941 DW_TAG_typedef to point to the DIE of TYPE, let's
19942 generate that DIE right away. add_type_attribute
19943 called below will then pick (via lookup_type_die) that
19944 anonymous struct DIE. */
19945 if (!TREE_ASM_WRITTEN (type))
19946 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
19948 /* This is a GNU Extension. We are adding a
19949 DW_AT_linkage_name attribute to the DIE of the
19950 anonymous struct TYPE. The value of that attribute
19951 is the name of the typedef decl naming the anonymous
19952 struct. This greatly eases the work of consumers of
19953 this debug info. */
19954 add_linkage_attr (lookup_type_die (type), decl);
19958 add_type_attribute (type_die, type, decl_quals (decl), context_die);
19960 if (is_naming_typedef_decl (decl))
19961 /* We want that all subsequent calls to lookup_type_die with
19962 TYPE in argument yield the DW_TAG_typedef we have just
19963 created. */
19964 equate_type_number_to_die (type, type_die);
19966 add_accessibility_attribute (type_die, decl);
19969 if (DECL_ABSTRACT_P (decl))
19970 equate_decl_number_to_die (decl, type_die);
19972 if (get_AT (type_die, DW_AT_name))
19973 add_pubtype (decl, type_die);
19976 /* Generate a DIE for a struct, class, enum or union type. */
19978 static void
19979 gen_tagged_type_die (tree type,
19980 dw_die_ref context_die,
19981 enum debug_info_usage usage)
19983 int need_pop;
19985 if (type == NULL_TREE
19986 || !is_tagged_type (type))
19987 return;
19989 /* If this is a nested type whose containing class hasn't been written
19990 out yet, writing it out will cover this one, too. This does not apply
19991 to instantiations of member class templates; they need to be added to
19992 the containing class as they are generated. FIXME: This hurts the
19993 idea of combining type decls from multiple TUs, since we can't predict
19994 what set of template instantiations we'll get. */
19995 if (TYPE_CONTEXT (type)
19996 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19997 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
19999 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
20001 if (TREE_ASM_WRITTEN (type))
20002 return;
20004 /* If that failed, attach ourselves to the stub. */
20005 push_decl_scope (TYPE_CONTEXT (type));
20006 context_die = lookup_type_die (TYPE_CONTEXT (type));
20007 need_pop = 1;
20009 else if (TYPE_CONTEXT (type) != NULL_TREE
20010 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
20012 /* If this type is local to a function that hasn't been written
20013 out yet, use a NULL context for now; it will be fixed up in
20014 decls_for_scope. */
20015 context_die = lookup_decl_die (TYPE_CONTEXT (type));
20016 /* A declaration DIE doesn't count; nested types need to go in the
20017 specification. */
20018 if (context_die && is_declaration_die (context_die))
20019 context_die = NULL;
20020 need_pop = 0;
20022 else
20024 context_die = declare_in_namespace (type, context_die);
20025 need_pop = 0;
20028 if (TREE_CODE (type) == ENUMERAL_TYPE)
20030 /* This might have been written out by the call to
20031 declare_in_namespace. */
20032 if (!TREE_ASM_WRITTEN (type))
20033 gen_enumeration_type_die (type, context_die);
20035 else
20036 gen_struct_or_union_type_die (type, context_die, usage);
20038 if (need_pop)
20039 pop_decl_scope ();
20041 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
20042 it up if it is ever completed. gen_*_type_die will set it for us
20043 when appropriate. */
20046 /* Generate a type description DIE. */
20048 static void
20049 gen_type_die_with_usage (tree type, dw_die_ref context_die,
20050 enum debug_info_usage usage)
20052 struct array_descr_info info;
20054 if (type == NULL_TREE || type == error_mark_node)
20055 return;
20057 if (TYPE_NAME (type) != NULL_TREE
20058 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
20059 && is_redundant_typedef (TYPE_NAME (type))
20060 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
20061 /* The DECL of this type is a typedef we don't want to emit debug
20062 info for but we want debug info for its underlying typedef.
20063 This can happen for e.g, the injected-class-name of a C++
20064 type. */
20065 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
20067 /* If TYPE is a typedef type variant, let's generate debug info
20068 for the parent typedef which TYPE is a type of. */
20069 if (typedef_variant_p (type))
20071 if (TREE_ASM_WRITTEN (type))
20072 return;
20074 /* Prevent broken recursion; we can't hand off to the same type. */
20075 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
20077 /* Give typedefs the right scope. */
20078 context_die = scope_die_for (type, context_die);
20080 TREE_ASM_WRITTEN (type) = 1;
20082 gen_decl_die (TYPE_NAME (type), NULL, context_die);
20083 return;
20086 /* If type is an anonymous tagged type named by a typedef, let's
20087 generate debug info for the typedef. */
20088 if (is_naming_typedef_decl (TYPE_NAME (type)))
20090 /* Use the DIE of the containing namespace as the parent DIE of
20091 the type description DIE we want to generate. */
20092 if (DECL_CONTEXT (TYPE_NAME (type))
20093 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
20094 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
20096 gen_decl_die (TYPE_NAME (type), NULL, context_die);
20097 return;
20100 /* If this is an array type with hidden descriptor, handle it first. */
20101 if (!TREE_ASM_WRITTEN (type)
20102 && lang_hooks.types.get_array_descr_info
20103 && lang_hooks.types.get_array_descr_info (type, &info)
20104 && (dwarf_version >= 3 || !dwarf_strict))
20106 gen_descr_array_type_die (type, &info, context_die);
20107 TREE_ASM_WRITTEN (type) = 1;
20108 return;
20111 /* We are going to output a DIE to represent the unqualified version
20112 of this type (i.e. without any const or volatile qualifiers) so
20113 get the main variant (i.e. the unqualified version) of this type
20114 now. (Vectors are special because the debugging info is in the
20115 cloned type itself). */
20116 if (TREE_CODE (type) != VECTOR_TYPE)
20117 type = type_main_variant (type);
20119 if (TREE_ASM_WRITTEN (type))
20120 return;
20122 switch (TREE_CODE (type))
20124 case ERROR_MARK:
20125 break;
20127 case POINTER_TYPE:
20128 case REFERENCE_TYPE:
20129 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
20130 ensures that the gen_type_die recursion will terminate even if the
20131 type is recursive. Recursive types are possible in Ada. */
20132 /* ??? We could perhaps do this for all types before the switch
20133 statement. */
20134 TREE_ASM_WRITTEN (type) = 1;
20136 /* For these types, all that is required is that we output a DIE (or a
20137 set of DIEs) to represent the "basis" type. */
20138 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20139 DINFO_USAGE_IND_USE);
20140 break;
20142 case OFFSET_TYPE:
20143 /* This code is used for C++ pointer-to-data-member types.
20144 Output a description of the relevant class type. */
20145 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
20146 DINFO_USAGE_IND_USE);
20148 /* Output a description of the type of the object pointed to. */
20149 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20150 DINFO_USAGE_IND_USE);
20152 /* Now output a DIE to represent this pointer-to-data-member type
20153 itself. */
20154 gen_ptr_to_mbr_type_die (type, context_die);
20155 break;
20157 case FUNCTION_TYPE:
20158 /* Force out return type (in case it wasn't forced out already). */
20159 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20160 DINFO_USAGE_DIR_USE);
20161 gen_subroutine_type_die (type, context_die);
20162 break;
20164 case METHOD_TYPE:
20165 /* Force out return type (in case it wasn't forced out already). */
20166 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20167 DINFO_USAGE_DIR_USE);
20168 gen_subroutine_type_die (type, context_die);
20169 break;
20171 case ARRAY_TYPE:
20172 gen_array_type_die (type, context_die);
20173 break;
20175 case VECTOR_TYPE:
20176 gen_array_type_die (type, context_die);
20177 break;
20179 case ENUMERAL_TYPE:
20180 case RECORD_TYPE:
20181 case UNION_TYPE:
20182 case QUAL_UNION_TYPE:
20183 gen_tagged_type_die (type, context_die, usage);
20184 return;
20186 case VOID_TYPE:
20187 case INTEGER_TYPE:
20188 case REAL_TYPE:
20189 case FIXED_POINT_TYPE:
20190 case COMPLEX_TYPE:
20191 case BOOLEAN_TYPE:
20192 case POINTER_BOUNDS_TYPE:
20193 /* No DIEs needed for fundamental types. */
20194 break;
20196 case NULLPTR_TYPE:
20197 case LANG_TYPE:
20198 /* Just use DW_TAG_unspecified_type. */
20200 dw_die_ref type_die = lookup_type_die (type);
20201 if (type_die == NULL)
20203 tree name = TYPE_IDENTIFIER (type);
20204 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
20205 type);
20206 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
20207 equate_type_number_to_die (type, type_die);
20210 break;
20212 default:
20213 if (is_cxx_auto (type))
20215 tree name = TYPE_IDENTIFIER (type);
20216 dw_die_ref *die = (name == get_identifier ("auto")
20217 ? &auto_die : &decltype_auto_die);
20218 if (!*die)
20220 *die = new_die (DW_TAG_unspecified_type,
20221 comp_unit_die (), NULL_TREE);
20222 add_name_attribute (*die, IDENTIFIER_POINTER (name));
20224 equate_type_number_to_die (type, *die);
20225 break;
20227 gcc_unreachable ();
20230 TREE_ASM_WRITTEN (type) = 1;
20233 static void
20234 gen_type_die (tree type, dw_die_ref context_die)
20236 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
20239 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
20240 things which are local to the given block. */
20242 static void
20243 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
20245 int must_output_die = 0;
20246 bool inlined_func;
20248 /* Ignore blocks that are NULL. */
20249 if (stmt == NULL_TREE)
20250 return;
20252 inlined_func = inlined_function_outer_scope_p (stmt);
20254 /* If the block is one fragment of a non-contiguous block, do not
20255 process the variables, since they will have been done by the
20256 origin block. Do process subblocks. */
20257 if (BLOCK_FRAGMENT_ORIGIN (stmt))
20259 tree sub;
20261 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
20262 gen_block_die (sub, context_die, depth + 1);
20264 return;
20267 /* Determine if we need to output any Dwarf DIEs at all to represent this
20268 block. */
20269 if (inlined_func)
20270 /* The outer scopes for inlinings *must* always be represented. We
20271 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
20272 must_output_die = 1;
20273 else
20275 /* Determine if this block directly contains any "significant"
20276 local declarations which we will need to output DIEs for. */
20277 if (debug_info_level > DINFO_LEVEL_TERSE)
20278 /* We are not in terse mode so *any* local declaration counts
20279 as being a "significant" one. */
20280 must_output_die = ((BLOCK_VARS (stmt) != NULL
20281 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
20282 && (TREE_USED (stmt)
20283 || TREE_ASM_WRITTEN (stmt)
20284 || BLOCK_ABSTRACT (stmt)));
20285 else if ((TREE_USED (stmt)
20286 || TREE_ASM_WRITTEN (stmt)
20287 || BLOCK_ABSTRACT (stmt))
20288 && !dwarf2out_ignore_block (stmt))
20289 must_output_die = 1;
20292 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
20293 DIE for any block which contains no significant local declarations at
20294 all. Rather, in such cases we just call `decls_for_scope' so that any
20295 needed Dwarf info for any sub-blocks will get properly generated. Note
20296 that in terse mode, our definition of what constitutes a "significant"
20297 local declaration gets restricted to include only inlined function
20298 instances and local (nested) function definitions. */
20299 if (must_output_die)
20301 if (inlined_func)
20303 /* If STMT block is abstract, that means we have been called
20304 indirectly from dwarf2out_abstract_function.
20305 That function rightfully marks the descendent blocks (of
20306 the abstract function it is dealing with) as being abstract,
20307 precisely to prevent us from emitting any
20308 DW_TAG_inlined_subroutine DIE as a descendent
20309 of an abstract function instance. So in that case, we should
20310 not call gen_inlined_subroutine_die.
20312 Later though, when cgraph asks dwarf2out to emit info
20313 for the concrete instance of the function decl into which
20314 the concrete instance of STMT got inlined, the later will lead
20315 to the generation of a DW_TAG_inlined_subroutine DIE. */
20316 if (! BLOCK_ABSTRACT (stmt))
20317 gen_inlined_subroutine_die (stmt, context_die, depth);
20319 else
20320 gen_lexical_block_die (stmt, context_die, depth);
20322 else
20323 decls_for_scope (stmt, context_die, depth);
20326 /* Process variable DECL (or variable with origin ORIGIN) within
20327 block STMT and add it to CONTEXT_DIE. */
20328 static void
20329 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
20331 dw_die_ref die;
20332 tree decl_or_origin = decl ? decl : origin;
20334 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
20335 die = lookup_decl_die (decl_or_origin);
20336 else if (TREE_CODE (decl_or_origin) == TYPE_DECL
20337 && TYPE_DECL_IS_STUB (decl_or_origin))
20338 die = lookup_type_die (TREE_TYPE (decl_or_origin));
20339 else
20340 die = NULL;
20342 if (die != NULL && die->die_parent == NULL)
20343 add_child_die (context_die, die);
20344 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
20345 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
20346 stmt, context_die);
20347 else
20348 gen_decl_die (decl, origin, context_die);
20351 /* Generate all of the decls declared within a given scope and (recursively)
20352 all of its sub-blocks. */
20354 static void
20355 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
20357 tree decl;
20358 unsigned int i;
20359 tree subblocks;
20361 /* Ignore NULL blocks. */
20362 if (stmt == NULL_TREE)
20363 return;
20365 /* Output the DIEs to represent all of the data objects and typedefs
20366 declared directly within this block but not within any nested
20367 sub-blocks. Also, nested function and tag DIEs have been
20368 generated with a parent of NULL; fix that up now. We don't
20369 have to do this if we're at -g1. */
20370 if (debug_info_level > DINFO_LEVEL_TERSE)
20372 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
20373 process_scope_var (stmt, decl, NULL_TREE, context_die);
20374 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
20375 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
20376 context_die);
20379 /* Even if we're at -g1, we need to process the subblocks in order to get
20380 inlined call information. */
20382 /* Output the DIEs to represent all sub-blocks (and the items declared
20383 therein) of this block. */
20384 for (subblocks = BLOCK_SUBBLOCKS (stmt);
20385 subblocks != NULL;
20386 subblocks = BLOCK_CHAIN (subblocks))
20387 gen_block_die (subblocks, context_die, depth + 1);
20390 /* Is this a typedef we can avoid emitting? */
20392 static inline int
20393 is_redundant_typedef (const_tree decl)
20395 if (TYPE_DECL_IS_STUB (decl))
20396 return 1;
20398 if (DECL_ARTIFICIAL (decl)
20399 && DECL_CONTEXT (decl)
20400 && is_tagged_type (DECL_CONTEXT (decl))
20401 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
20402 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
20403 /* Also ignore the artificial member typedef for the class name. */
20404 return 1;
20406 return 0;
20409 /* Return TRUE if TYPE is a typedef that names a type for linkage
20410 purposes. This kind of typedefs is produced by the C++ FE for
20411 constructs like:
20413 typedef struct {...} foo;
20415 In that case, there is no typedef variant type produced for foo.
20416 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
20417 struct type. */
20419 static bool
20420 is_naming_typedef_decl (const_tree decl)
20422 if (decl == NULL_TREE
20423 || TREE_CODE (decl) != TYPE_DECL
20424 || !is_tagged_type (TREE_TYPE (decl))
20425 || DECL_IS_BUILTIN (decl)
20426 || is_redundant_typedef (decl)
20427 /* It looks like Ada produces TYPE_DECLs that are very similar
20428 to C++ naming typedefs but that have different
20429 semantics. Let's be specific to c++ for now. */
20430 || !is_cxx ())
20431 return FALSE;
20433 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
20434 && TYPE_NAME (TREE_TYPE (decl)) == decl
20435 && (TYPE_STUB_DECL (TREE_TYPE (decl))
20436 != TYPE_NAME (TREE_TYPE (decl))));
20439 /* Returns the DIE for a context. */
20441 static inline dw_die_ref
20442 get_context_die (tree context)
20444 if (context)
20446 /* Find die that represents this context. */
20447 if (TYPE_P (context))
20449 context = TYPE_MAIN_VARIANT (context);
20450 return strip_naming_typedef (context, force_type_die (context));
20452 else
20453 return force_decl_die (context);
20455 return comp_unit_die ();
20458 /* Returns the DIE for decl. A DIE will always be returned. */
20460 static dw_die_ref
20461 force_decl_die (tree decl)
20463 dw_die_ref decl_die;
20464 unsigned saved_external_flag;
20465 tree save_fn = NULL_TREE;
20466 decl_die = lookup_decl_die (decl);
20467 if (!decl_die)
20469 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
20471 decl_die = lookup_decl_die (decl);
20472 if (decl_die)
20473 return decl_die;
20475 switch (TREE_CODE (decl))
20477 case FUNCTION_DECL:
20478 /* Clear current_function_decl, so that gen_subprogram_die thinks
20479 that this is a declaration. At this point, we just want to force
20480 declaration die. */
20481 save_fn = current_function_decl;
20482 current_function_decl = NULL_TREE;
20483 gen_subprogram_die (decl, context_die);
20484 current_function_decl = save_fn;
20485 break;
20487 case VAR_DECL:
20488 /* Set external flag to force declaration die. Restore it after
20489 gen_decl_die() call. */
20490 saved_external_flag = DECL_EXTERNAL (decl);
20491 DECL_EXTERNAL (decl) = 1;
20492 gen_decl_die (decl, NULL, context_die);
20493 DECL_EXTERNAL (decl) = saved_external_flag;
20494 break;
20496 case NAMESPACE_DECL:
20497 if (dwarf_version >= 3 || !dwarf_strict)
20498 dwarf2out_decl (decl);
20499 else
20500 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
20501 decl_die = comp_unit_die ();
20502 break;
20504 case TRANSLATION_UNIT_DECL:
20505 decl_die = comp_unit_die ();
20506 break;
20508 default:
20509 gcc_unreachable ();
20512 /* We should be able to find the DIE now. */
20513 if (!decl_die)
20514 decl_die = lookup_decl_die (decl);
20515 gcc_assert (decl_die);
20518 return decl_die;
20521 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
20522 always returned. */
20524 static dw_die_ref
20525 force_type_die (tree type)
20527 dw_die_ref type_die;
20529 type_die = lookup_type_die (type);
20530 if (!type_die)
20532 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
20534 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
20535 context_die);
20536 gcc_assert (type_die);
20538 return type_die;
20541 /* Force out any required namespaces to be able to output DECL,
20542 and return the new context_die for it, if it's changed. */
20544 static dw_die_ref
20545 setup_namespace_context (tree thing, dw_die_ref context_die)
20547 tree context = (DECL_P (thing)
20548 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
20549 if (context && TREE_CODE (context) == NAMESPACE_DECL)
20550 /* Force out the namespace. */
20551 context_die = force_decl_die (context);
20553 return context_die;
20556 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
20557 type) within its namespace, if appropriate.
20559 For compatibility with older debuggers, namespace DIEs only contain
20560 declarations; all definitions are emitted at CU scope. */
20562 static dw_die_ref
20563 declare_in_namespace (tree thing, dw_die_ref context_die)
20565 dw_die_ref ns_context;
20567 if (debug_info_level <= DINFO_LEVEL_TERSE)
20568 return context_die;
20570 /* External declarations in the local scope only need to be emitted
20571 once, not once in the namespace and once in the scope.
20573 This avoids declaring the `extern' below in the
20574 namespace DIE as well as in the innermost scope:
20576 namespace S
20578 int i=5;
20579 int foo()
20581 int i=8;
20582 extern int i;
20583 return i;
20587 if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
20588 return context_die;
20590 /* If this decl is from an inlined function, then don't try to emit it in its
20591 namespace, as we will get confused. It would have already been emitted
20592 when the abstract instance of the inline function was emitted anyways. */
20593 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
20594 return context_die;
20596 ns_context = setup_namespace_context (thing, context_die);
20598 if (ns_context != context_die)
20600 if (is_fortran ())
20601 return ns_context;
20602 if (DECL_P (thing))
20603 gen_decl_die (thing, NULL, ns_context);
20604 else
20605 gen_type_die (thing, ns_context);
20607 return context_die;
20610 /* Generate a DIE for a namespace or namespace alias. */
20612 static void
20613 gen_namespace_die (tree decl, dw_die_ref context_die)
20615 dw_die_ref namespace_die;
20617 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
20618 they are an alias of. */
20619 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
20621 /* Output a real namespace or module. */
20622 context_die = setup_namespace_context (decl, comp_unit_die ());
20623 namespace_die = new_die (is_fortran ()
20624 ? DW_TAG_module : DW_TAG_namespace,
20625 context_die, decl);
20626 /* For Fortran modules defined in different CU don't add src coords. */
20627 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
20629 const char *name = dwarf2_name (decl, 0);
20630 if (name)
20631 add_name_attribute (namespace_die, name);
20633 else
20634 add_name_and_src_coords_attributes (namespace_die, decl);
20635 if (DECL_EXTERNAL (decl))
20636 add_AT_flag (namespace_die, DW_AT_declaration, 1);
20637 equate_decl_number_to_die (decl, namespace_die);
20639 else
20641 /* Output a namespace alias. */
20643 /* Force out the namespace we are an alias of, if necessary. */
20644 dw_die_ref origin_die
20645 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
20647 if (DECL_FILE_SCOPE_P (decl)
20648 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
20649 context_die = setup_namespace_context (decl, comp_unit_die ());
20650 /* Now create the namespace alias DIE. */
20651 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
20652 add_name_and_src_coords_attributes (namespace_die, decl);
20653 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
20654 equate_decl_number_to_die (decl, namespace_die);
20656 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
20657 if (want_pubnames ())
20658 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
20661 /* Generate Dwarf debug information for a decl described by DECL.
20662 The return value is currently only meaningful for PARM_DECLs,
20663 for all other decls it returns NULL. */
20665 static dw_die_ref
20666 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
20668 tree decl_or_origin = decl ? decl : origin;
20669 tree class_origin = NULL, ultimate_origin;
20671 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
20672 return NULL;
20674 /* Ignore pointer bounds decls. */
20675 if (DECL_P (decl_or_origin)
20676 && TREE_TYPE (decl_or_origin)
20677 && POINTER_BOUNDS_P (decl_or_origin))
20678 return NULL;
20680 switch (TREE_CODE (decl_or_origin))
20682 case ERROR_MARK:
20683 break;
20685 case CONST_DECL:
20686 if (!is_fortran () && !is_ada ())
20688 /* The individual enumerators of an enum type get output when we output
20689 the Dwarf representation of the relevant enum type itself. */
20690 break;
20693 /* Emit its type. */
20694 gen_type_die (TREE_TYPE (decl), context_die);
20696 /* And its containing namespace. */
20697 context_die = declare_in_namespace (decl, context_die);
20699 gen_const_die (decl, context_die);
20700 break;
20702 case FUNCTION_DECL:
20703 /* Don't output any DIEs to represent mere function declarations,
20704 unless they are class members or explicit block externs. */
20705 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
20706 && DECL_FILE_SCOPE_P (decl_or_origin)
20707 && (current_function_decl == NULL_TREE
20708 || DECL_ARTIFICIAL (decl_or_origin)))
20709 break;
20711 #if 0
20712 /* FIXME */
20713 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
20714 on local redeclarations of global functions. That seems broken. */
20715 if (current_function_decl != decl)
20716 /* This is only a declaration. */;
20717 #endif
20719 /* If we're emitting a clone, emit info for the abstract instance. */
20720 if (origin || DECL_ORIGIN (decl) != decl)
20721 dwarf2out_abstract_function (origin
20722 ? DECL_ORIGIN (origin)
20723 : DECL_ABSTRACT_ORIGIN (decl));
20725 /* If we're emitting an out-of-line copy of an inline function,
20726 emit info for the abstract instance and set up to refer to it. */
20727 else if (cgraph_function_possibly_inlined_p (decl)
20728 && ! DECL_ABSTRACT_P (decl)
20729 && ! class_or_namespace_scope_p (context_die)
20730 /* dwarf2out_abstract_function won't emit a die if this is just
20731 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
20732 that case, because that works only if we have a die. */
20733 && DECL_INITIAL (decl) != NULL_TREE)
20735 dwarf2out_abstract_function (decl);
20736 set_decl_origin_self (decl);
20739 /* Otherwise we're emitting the primary DIE for this decl. */
20740 else if (debug_info_level > DINFO_LEVEL_TERSE)
20742 /* Before we describe the FUNCTION_DECL itself, make sure that we
20743 have its containing type. */
20744 if (!origin)
20745 origin = decl_class_context (decl);
20746 if (origin != NULL_TREE)
20747 gen_type_die (origin, context_die);
20749 /* And its return type. */
20750 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
20752 /* And its virtual context. */
20753 if (DECL_VINDEX (decl) != NULL_TREE)
20754 gen_type_die (DECL_CONTEXT (decl), context_die);
20756 /* Make sure we have a member DIE for decl. */
20757 if (origin != NULL_TREE)
20758 gen_type_die_for_member (origin, decl, context_die);
20760 /* And its containing namespace. */
20761 context_die = declare_in_namespace (decl, context_die);
20764 /* Now output a DIE to represent the function itself. */
20765 if (decl)
20766 gen_subprogram_die (decl, context_die);
20767 break;
20769 case TYPE_DECL:
20770 /* If we are in terse mode, don't generate any DIEs to represent any
20771 actual typedefs. */
20772 if (debug_info_level <= DINFO_LEVEL_TERSE)
20773 break;
20775 /* In the special case of a TYPE_DECL node representing the declaration
20776 of some type tag, if the given TYPE_DECL is marked as having been
20777 instantiated from some other (original) TYPE_DECL node (e.g. one which
20778 was generated within the original definition of an inline function) we
20779 used to generate a special (abbreviated) DW_TAG_structure_type,
20780 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
20781 should be actually referencing those DIEs, as variable DIEs with that
20782 type would be emitted already in the abstract origin, so it was always
20783 removed during unused type prunning. Don't add anything in this
20784 case. */
20785 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
20786 break;
20788 if (is_redundant_typedef (decl))
20789 gen_type_die (TREE_TYPE (decl), context_die);
20790 else
20791 /* Output a DIE to represent the typedef itself. */
20792 gen_typedef_die (decl, context_die);
20793 break;
20795 case LABEL_DECL:
20796 if (debug_info_level >= DINFO_LEVEL_NORMAL)
20797 gen_label_die (decl, context_die);
20798 break;
20800 case VAR_DECL:
20801 case RESULT_DECL:
20802 /* If we are in terse mode, don't generate any DIEs to represent any
20803 variable declarations or definitions. */
20804 if (debug_info_level <= DINFO_LEVEL_TERSE)
20805 break;
20807 /* Output any DIEs that are needed to specify the type of this data
20808 object. */
20809 if (decl_by_reference_p (decl_or_origin))
20810 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20811 else
20812 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20814 /* And its containing type. */
20815 class_origin = decl_class_context (decl_or_origin);
20816 if (class_origin != NULL_TREE)
20817 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
20819 /* And its containing namespace. */
20820 context_die = declare_in_namespace (decl_or_origin, context_die);
20822 /* Now output the DIE to represent the data object itself. This gets
20823 complicated because of the possibility that the VAR_DECL really
20824 represents an inlined instance of a formal parameter for an inline
20825 function. */
20826 ultimate_origin = decl_ultimate_origin (decl_or_origin);
20827 if (ultimate_origin != NULL_TREE
20828 && TREE_CODE (ultimate_origin) == PARM_DECL)
20829 gen_formal_parameter_die (decl, origin,
20830 true /* Emit name attribute. */,
20831 context_die);
20832 else
20833 gen_variable_die (decl, origin, context_die);
20834 break;
20836 case FIELD_DECL:
20837 /* Ignore the nameless fields that are used to skip bits but handle C++
20838 anonymous unions and structs. */
20839 if (DECL_NAME (decl) != NULL_TREE
20840 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
20841 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
20843 gen_type_die (member_declared_type (decl), context_die);
20844 gen_field_die (decl, context_die);
20846 break;
20848 case PARM_DECL:
20849 if (DECL_BY_REFERENCE (decl_or_origin))
20850 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20851 else
20852 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20853 return gen_formal_parameter_die (decl, origin,
20854 true /* Emit name attribute. */,
20855 context_die);
20857 case NAMESPACE_DECL:
20858 case IMPORTED_DECL:
20859 if (dwarf_version >= 3 || !dwarf_strict)
20860 gen_namespace_die (decl, context_die);
20861 break;
20863 case NAMELIST_DECL:
20864 gen_namelist_decl (DECL_NAME (decl), context_die,
20865 NAMELIST_DECL_ASSOCIATED_DECL (decl));
20866 break;
20868 default:
20869 /* Probably some frontend-internal decl. Assume we don't care. */
20870 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
20871 break;
20874 return NULL;
20877 /* Output debug information for global decl DECL. Called from toplev.c after
20878 compilation proper has finished. */
20880 static void
20881 dwarf2out_global_decl (tree decl)
20883 /* Output DWARF2 information for file-scope tentative data object
20884 declarations, file-scope (extern) function declarations (which
20885 had no corresponding body) and file-scope tagged type declarations
20886 and definitions which have not yet been forced out. */
20887 if ((TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
20888 && !POINTER_BOUNDS_P (decl))
20889 dwarf2out_decl (decl);
20892 /* Output debug information for type decl DECL. Called from toplev.c
20893 and from language front ends (to record built-in types). */
20894 static void
20895 dwarf2out_type_decl (tree decl, int local)
20897 if (!local)
20898 dwarf2out_decl (decl);
20901 /* Output debug information for imported module or decl DECL.
20902 NAME is non-NULL name in the lexical block if the decl has been renamed.
20903 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
20904 that DECL belongs to.
20905 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
20906 static void
20907 dwarf2out_imported_module_or_decl_1 (tree decl,
20908 tree name,
20909 tree lexical_block,
20910 dw_die_ref lexical_block_die)
20912 expanded_location xloc;
20913 dw_die_ref imported_die = NULL;
20914 dw_die_ref at_import_die;
20916 if (TREE_CODE (decl) == IMPORTED_DECL)
20918 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
20919 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
20920 gcc_assert (decl);
20922 else
20923 xloc = expand_location (input_location);
20925 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
20927 at_import_die = force_type_die (TREE_TYPE (decl));
20928 /* For namespace N { typedef void T; } using N::T; base_type_die
20929 returns NULL, but DW_TAG_imported_declaration requires
20930 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
20931 if (!at_import_die)
20933 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
20934 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
20935 at_import_die = lookup_type_die (TREE_TYPE (decl));
20936 gcc_assert (at_import_die);
20939 else
20941 at_import_die = lookup_decl_die (decl);
20942 if (!at_import_die)
20944 /* If we're trying to avoid duplicate debug info, we may not have
20945 emitted the member decl for this field. Emit it now. */
20946 if (TREE_CODE (decl) == FIELD_DECL)
20948 tree type = DECL_CONTEXT (decl);
20950 if (TYPE_CONTEXT (type)
20951 && TYPE_P (TYPE_CONTEXT (type))
20952 && !should_emit_struct_debug (TYPE_CONTEXT (type),
20953 DINFO_USAGE_DIR_USE))
20954 return;
20955 gen_type_die_for_member (type, decl,
20956 get_context_die (TYPE_CONTEXT (type)));
20958 if (TREE_CODE (decl) == NAMELIST_DECL)
20959 at_import_die = gen_namelist_decl (DECL_NAME (decl),
20960 get_context_die (DECL_CONTEXT (decl)),
20961 NULL_TREE);
20962 else
20963 at_import_die = force_decl_die (decl);
20967 if (TREE_CODE (decl) == NAMESPACE_DECL)
20969 if (dwarf_version >= 3 || !dwarf_strict)
20970 imported_die = new_die (DW_TAG_imported_module,
20971 lexical_block_die,
20972 lexical_block);
20973 else
20974 return;
20976 else
20977 imported_die = new_die (DW_TAG_imported_declaration,
20978 lexical_block_die,
20979 lexical_block);
20981 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
20982 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
20983 if (name)
20984 add_AT_string (imported_die, DW_AT_name,
20985 IDENTIFIER_POINTER (name));
20986 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
20989 /* Output debug information for imported module or decl DECL.
20990 NAME is non-NULL name in context if the decl has been renamed.
20991 CHILD is true if decl is one of the renamed decls as part of
20992 importing whole module. */
20994 static void
20995 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
20996 bool child)
20998 /* dw_die_ref at_import_die; */
20999 dw_die_ref scope_die;
21001 if (debug_info_level <= DINFO_LEVEL_TERSE)
21002 return;
21004 gcc_assert (decl);
21006 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
21007 We need decl DIE for reference and scope die. First, get DIE for the decl
21008 itself. */
21010 /* Get the scope die for decl context. Use comp_unit_die for global module
21011 or decl. If die is not found for non globals, force new die. */
21012 if (context
21013 && TYPE_P (context)
21014 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
21015 return;
21017 if (!(dwarf_version >= 3 || !dwarf_strict))
21018 return;
21020 scope_die = get_context_die (context);
21022 if (child)
21024 gcc_assert (scope_die->die_child);
21025 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
21026 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
21027 scope_die = scope_die->die_child;
21030 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
21031 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
21035 /* Output debug information for namelists. */
21037 static dw_die_ref
21038 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
21040 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
21041 tree value;
21042 unsigned i;
21044 if (debug_info_level <= DINFO_LEVEL_TERSE)
21045 return NULL;
21047 gcc_assert (scope_die != NULL);
21048 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
21049 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
21051 /* If there are no item_decls, we have a nondefining namelist, e.g.
21052 with USE association; hence, set DW_AT_declaration. */
21053 if (item_decls == NULL_TREE)
21055 add_AT_flag (nml_die, DW_AT_declaration, 1);
21056 return nml_die;
21059 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
21061 nml_item_ref_die = lookup_decl_die (value);
21062 if (!nml_item_ref_die)
21063 nml_item_ref_die = force_decl_die (value);
21065 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
21066 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
21068 return nml_die;
21072 /* Write the debugging output for DECL. */
21074 static void
21075 dwarf2out_decl (tree decl)
21077 dw_die_ref context_die = comp_unit_die ();
21079 switch (TREE_CODE (decl))
21081 case ERROR_MARK:
21082 return;
21084 case FUNCTION_DECL:
21085 /* What we would really like to do here is to filter out all mere
21086 file-scope declarations of file-scope functions which are never
21087 referenced later within this translation unit (and keep all of ones
21088 that *are* referenced later on) but we aren't clairvoyant, so we have
21089 no idea which functions will be referenced in the future (i.e. later
21090 on within the current translation unit). So here we just ignore all
21091 file-scope function declarations which are not also definitions. If
21092 and when the debugger needs to know something about these functions,
21093 it will have to hunt around and find the DWARF information associated
21094 with the definition of the function.
21096 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
21097 nodes represent definitions and which ones represent mere
21098 declarations. We have to check DECL_INITIAL instead. That's because
21099 the C front-end supports some weird semantics for "extern inline"
21100 function definitions. These can get inlined within the current
21101 translation unit (and thus, we need to generate Dwarf info for their
21102 abstract instances so that the Dwarf info for the concrete inlined
21103 instances can have something to refer to) but the compiler never
21104 generates any out-of-lines instances of such things (despite the fact
21105 that they *are* definitions).
21107 The important point is that the C front-end marks these "extern
21108 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
21109 them anyway. Note that the C++ front-end also plays some similar games
21110 for inline function definitions appearing within include files which
21111 also contain `#pragma interface' pragmas.
21113 If we are called from dwarf2out_abstract_function output a DIE
21114 anyway. We can end up here this way with early inlining and LTO
21115 where the inlined function is output in a different LTRANS unit
21116 or not at all. */
21117 if (DECL_INITIAL (decl) == NULL_TREE
21118 && ! DECL_ABSTRACT_P (decl))
21119 return;
21121 /* If we're a nested function, initially use a parent of NULL; if we're
21122 a plain function, this will be fixed up in decls_for_scope. If
21123 we're a method, it will be ignored, since we already have a DIE. */
21124 if (decl_function_context (decl)
21125 /* But if we're in terse mode, we don't care about scope. */
21126 && debug_info_level > DINFO_LEVEL_TERSE)
21127 context_die = NULL;
21128 break;
21130 case VAR_DECL:
21131 /* Ignore this VAR_DECL if it refers to a file-scope extern data object
21132 declaration and if the declaration was never even referenced from
21133 within this entire compilation unit. We suppress these DIEs in
21134 order to save space in the .debug section (by eliminating entries
21135 which are probably useless). Note that we must not suppress
21136 block-local extern declarations (whether used or not) because that
21137 would screw-up the debugger's name lookup mechanism and cause it to
21138 miss things which really ought to be in scope at a given point. */
21139 if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
21140 return;
21142 /* For local statics lookup proper context die. */
21143 if (TREE_STATIC (decl)
21144 && DECL_CONTEXT (decl)
21145 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
21146 context_die = lookup_decl_die (DECL_CONTEXT (decl));
21148 /* If we are in terse mode, don't generate any DIEs to represent any
21149 variable declarations or definitions. */
21150 if (debug_info_level <= DINFO_LEVEL_TERSE)
21151 return;
21152 break;
21154 case CONST_DECL:
21155 if (debug_info_level <= DINFO_LEVEL_TERSE)
21156 return;
21157 if (!is_fortran () && !is_ada ())
21158 return;
21159 if (TREE_STATIC (decl) && decl_function_context (decl))
21160 context_die = lookup_decl_die (DECL_CONTEXT (decl));
21161 break;
21163 case NAMESPACE_DECL:
21164 case IMPORTED_DECL:
21165 if (debug_info_level <= DINFO_LEVEL_TERSE)
21166 return;
21167 if (lookup_decl_die (decl) != NULL)
21168 return;
21169 break;
21171 case TYPE_DECL:
21172 /* Don't emit stubs for types unless they are needed by other DIEs. */
21173 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
21174 return;
21176 /* Don't bother trying to generate any DIEs to represent any of the
21177 normal built-in types for the language we are compiling. */
21178 if (DECL_IS_BUILTIN (decl))
21179 return;
21181 /* If we are in terse mode, don't generate any DIEs for types. */
21182 if (debug_info_level <= DINFO_LEVEL_TERSE)
21183 return;
21185 /* If we're a function-scope tag, initially use a parent of NULL;
21186 this will be fixed up in decls_for_scope. */
21187 if (decl_function_context (decl))
21188 context_die = NULL;
21190 break;
21192 case NAMELIST_DECL:
21193 break;
21195 default:
21196 return;
21199 gen_decl_die (decl, NULL, context_die);
21202 /* Write the debugging output for DECL. */
21204 static void
21205 dwarf2out_function_decl (tree decl)
21207 dwarf2out_decl (decl);
21208 call_arg_locations = NULL;
21209 call_arg_loc_last = NULL;
21210 call_site_count = -1;
21211 tail_call_site_count = -1;
21212 block_map.release ();
21213 decl_loc_table->empty ();
21214 cached_dw_loc_list_table->empty ();
21217 /* Output a marker (i.e. a label) for the beginning of the generated code for
21218 a lexical block. */
21220 static void
21221 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
21222 unsigned int blocknum)
21224 switch_to_section (current_function_section ());
21225 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
21228 /* Output a marker (i.e. a label) for the end of the generated code for a
21229 lexical block. */
21231 static void
21232 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
21234 switch_to_section (current_function_section ());
21235 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
21238 /* Returns nonzero if it is appropriate not to emit any debugging
21239 information for BLOCK, because it doesn't contain any instructions.
21241 Don't allow this for blocks with nested functions or local classes
21242 as we would end up with orphans, and in the presence of scheduling
21243 we may end up calling them anyway. */
21245 static bool
21246 dwarf2out_ignore_block (const_tree block)
21248 tree decl;
21249 unsigned int i;
21251 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
21252 if (TREE_CODE (decl) == FUNCTION_DECL
21253 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21254 return 0;
21255 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
21257 decl = BLOCK_NONLOCALIZED_VAR (block, i);
21258 if (TREE_CODE (decl) == FUNCTION_DECL
21259 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21260 return 0;
21263 return 1;
21266 /* Hash table routines for file_hash. */
21268 bool
21269 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
21271 return filename_cmp (p1->filename, p2) == 0;
21274 hashval_t
21275 dwarf_file_hasher::hash (dwarf_file_data *p)
21277 return htab_hash_string (p->filename);
21280 /* Lookup FILE_NAME (in the list of filenames that we know about here in
21281 dwarf2out.c) and return its "index". The index of each (known) filename is
21282 just a unique number which is associated with only that one filename. We
21283 need such numbers for the sake of generating labels (in the .debug_sfnames
21284 section) and references to those files numbers (in the .debug_srcinfo
21285 and.debug_macinfo sections). If the filename given as an argument is not
21286 found in our current list, add it to the list and assign it the next
21287 available unique index number. In order to speed up searches, we remember
21288 the index of the filename was looked up last. This handles the majority of
21289 all searches. */
21291 static struct dwarf_file_data *
21292 lookup_filename (const char *file_name)
21294 struct dwarf_file_data * created;
21296 /* Check to see if the file name that was searched on the previous
21297 call matches this file name. If so, return the index. */
21298 if (file_table_last_lookup
21299 && (file_name == file_table_last_lookup->filename
21300 || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
21301 return file_table_last_lookup;
21303 /* Didn't match the previous lookup, search the table. */
21304 dwarf_file_data **slot
21305 = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
21306 INSERT);
21307 if (*slot)
21308 return *slot;
21310 created = ggc_alloc<dwarf_file_data> ();
21311 created->filename = file_name;
21312 created->emitted_number = 0;
21313 *slot = created;
21314 return created;
21317 /* If the assembler will construct the file table, then translate the compiler
21318 internal file table number into the assembler file table number, and emit
21319 a .file directive if we haven't already emitted one yet. The file table
21320 numbers are different because we prune debug info for unused variables and
21321 types, which may include filenames. */
21323 static int
21324 maybe_emit_file (struct dwarf_file_data * fd)
21326 if (! fd->emitted_number)
21328 if (last_emitted_file)
21329 fd->emitted_number = last_emitted_file->emitted_number + 1;
21330 else
21331 fd->emitted_number = 1;
21332 last_emitted_file = fd;
21334 if (DWARF2_ASM_LINE_DEBUG_INFO)
21336 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
21337 output_quoted_string (asm_out_file,
21338 remap_debug_filename (fd->filename));
21339 fputc ('\n', asm_out_file);
21343 return fd->emitted_number;
21346 /* Schedule generation of a DW_AT_const_value attribute to DIE.
21347 That generation should happen after function debug info has been
21348 generated. The value of the attribute is the constant value of ARG. */
21350 static void
21351 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
21353 die_arg_entry entry;
21355 if (!die || !arg)
21356 return;
21358 if (!tmpl_value_parm_die_table)
21359 vec_alloc (tmpl_value_parm_die_table, 32);
21361 entry.die = die;
21362 entry.arg = arg;
21363 vec_safe_push (tmpl_value_parm_die_table, entry);
21366 /* Return TRUE if T is an instance of generic type, FALSE
21367 otherwise. */
21369 static bool
21370 generic_type_p (tree t)
21372 if (t == NULL_TREE || !TYPE_P (t))
21373 return false;
21374 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
21377 /* Schedule the generation of the generic parameter dies for the
21378 instance of generic type T. The proper generation itself is later
21379 done by gen_scheduled_generic_parms_dies. */
21381 static void
21382 schedule_generic_params_dies_gen (tree t)
21384 if (!generic_type_p (t))
21385 return;
21387 if (!generic_type_instances)
21388 vec_alloc (generic_type_instances, 256);
21390 vec_safe_push (generic_type_instances, t);
21393 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
21394 by append_entry_to_tmpl_value_parm_die_table. This function must
21395 be called after function DIEs have been generated. */
21397 static void
21398 gen_remaining_tmpl_value_param_die_attribute (void)
21400 if (tmpl_value_parm_die_table)
21402 unsigned i;
21403 die_arg_entry *e;
21405 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
21406 tree_add_const_value_attribute (e->die, e->arg);
21410 /* Generate generic parameters DIEs for instances of generic types
21411 that have been previously scheduled by
21412 schedule_generic_params_dies_gen. This function must be called
21413 after all the types of the CU have been laid out. */
21415 static void
21416 gen_scheduled_generic_parms_dies (void)
21418 unsigned i;
21419 tree t;
21421 if (!generic_type_instances)
21422 return;
21424 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
21425 if (COMPLETE_TYPE_P (t))
21426 gen_generic_params_dies (t);
21430 /* Replace DW_AT_name for the decl with name. */
21432 static void
21433 dwarf2out_set_name (tree decl, tree name)
21435 dw_die_ref die;
21436 dw_attr_ref attr;
21437 const char *dname;
21439 die = TYPE_SYMTAB_DIE (decl);
21440 if (!die)
21441 return;
21443 dname = dwarf2_name (name, 0);
21444 if (!dname)
21445 return;
21447 attr = get_AT (die, DW_AT_name);
21448 if (attr)
21450 struct indirect_string_node *node;
21452 node = find_AT_string (dname);
21453 /* replace the string. */
21454 attr->dw_attr_val.v.val_str = node;
21457 else
21458 add_name_attribute (die, dname);
21461 /* True if before or during processing of the first function being emitted. */
21462 static bool in_first_function_p = true;
21463 /* True if loc_note during dwarf2out_var_location call might still be
21464 before first real instruction at address equal to .Ltext0. */
21465 static bool maybe_at_text_label_p = true;
21466 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
21467 static unsigned int first_loclabel_num_not_at_text_label;
21469 /* Called by the final INSN scan whenever we see a var location. We
21470 use it to drop labels in the right places, and throw the location in
21471 our lookup table. */
21473 static void
21474 dwarf2out_var_location (rtx_insn *loc_note)
21476 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
21477 struct var_loc_node *newloc;
21478 rtx_insn *next_real, *next_note;
21479 static const char *last_label;
21480 static const char *last_postcall_label;
21481 static bool last_in_cold_section_p;
21482 static rtx_insn *expected_next_loc_note;
21483 tree decl;
21484 bool var_loc_p;
21486 if (!NOTE_P (loc_note))
21488 if (CALL_P (loc_note))
21490 call_site_count++;
21491 if (SIBLING_CALL_P (loc_note))
21492 tail_call_site_count++;
21494 return;
21497 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
21498 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
21499 return;
21501 /* Optimize processing a large consecutive sequence of location
21502 notes so we don't spend too much time in next_real_insn. If the
21503 next insn is another location note, remember the next_real_insn
21504 calculation for next time. */
21505 next_real = cached_next_real_insn;
21506 if (next_real)
21508 if (expected_next_loc_note != loc_note)
21509 next_real = NULL;
21512 next_note = NEXT_INSN (loc_note);
21513 if (! next_note
21514 || next_note->deleted ()
21515 || ! NOTE_P (next_note)
21516 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
21517 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
21518 next_note = NULL;
21520 if (! next_real)
21521 next_real = next_real_insn (loc_note);
21523 if (next_note)
21525 expected_next_loc_note = next_note;
21526 cached_next_real_insn = next_real;
21528 else
21529 cached_next_real_insn = NULL;
21531 /* If there are no instructions which would be affected by this note,
21532 don't do anything. */
21533 if (var_loc_p
21534 && next_real == NULL_RTX
21535 && !NOTE_DURING_CALL_P (loc_note))
21536 return;
21538 if (next_real == NULL_RTX)
21539 next_real = get_last_insn ();
21541 /* If there were any real insns between note we processed last time
21542 and this note (or if it is the first note), clear
21543 last_{,postcall_}label so that they are not reused this time. */
21544 if (last_var_location_insn == NULL_RTX
21545 || last_var_location_insn != next_real
21546 || last_in_cold_section_p != in_cold_section_p)
21548 last_label = NULL;
21549 last_postcall_label = NULL;
21552 if (var_loc_p)
21554 decl = NOTE_VAR_LOCATION_DECL (loc_note);
21555 newloc = add_var_loc_to_decl (decl, loc_note,
21556 NOTE_DURING_CALL_P (loc_note)
21557 ? last_postcall_label : last_label);
21558 if (newloc == NULL)
21559 return;
21561 else
21563 decl = NULL_TREE;
21564 newloc = NULL;
21567 /* If there were no real insns between note we processed last time
21568 and this note, use the label we emitted last time. Otherwise
21569 create a new label and emit it. */
21570 if (last_label == NULL)
21572 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
21573 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
21574 loclabel_num++;
21575 last_label = ggc_strdup (loclabel);
21576 /* See if loclabel might be equal to .Ltext0. If yes,
21577 bump first_loclabel_num_not_at_text_label. */
21578 if (!have_multiple_function_sections
21579 && in_first_function_p
21580 && maybe_at_text_label_p)
21582 static rtx_insn *last_start;
21583 rtx_insn *insn;
21584 for (insn = loc_note; insn; insn = previous_insn (insn))
21585 if (insn == last_start)
21586 break;
21587 else if (!NONDEBUG_INSN_P (insn))
21588 continue;
21589 else
21591 rtx body = PATTERN (insn);
21592 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
21593 continue;
21594 /* Inline asm could occupy zero bytes. */
21595 else if (GET_CODE (body) == ASM_INPUT
21596 || asm_noperands (body) >= 0)
21597 continue;
21598 #ifdef HAVE_attr_length
21599 else if (get_attr_min_length (insn) == 0)
21600 continue;
21601 #endif
21602 else
21604 /* Assume insn has non-zero length. */
21605 maybe_at_text_label_p = false;
21606 break;
21609 if (maybe_at_text_label_p)
21611 last_start = loc_note;
21612 first_loclabel_num_not_at_text_label = loclabel_num;
21617 if (!var_loc_p)
21619 struct call_arg_loc_node *ca_loc
21620 = ggc_cleared_alloc<call_arg_loc_node> ();
21621 rtx_insn *prev = prev_real_insn (loc_note);
21622 rtx x;
21623 ca_loc->call_arg_loc_note = loc_note;
21624 ca_loc->next = NULL;
21625 ca_loc->label = last_label;
21626 gcc_assert (prev
21627 && (CALL_P (prev)
21628 || (NONJUMP_INSN_P (prev)
21629 && GET_CODE (PATTERN (prev)) == SEQUENCE
21630 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
21631 if (!CALL_P (prev))
21632 prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
21633 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
21634 x = get_call_rtx_from (PATTERN (prev));
21635 if (x)
21637 x = XEXP (XEXP (x, 0), 0);
21638 if (GET_CODE (x) == SYMBOL_REF
21639 && SYMBOL_REF_DECL (x)
21640 && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
21641 ca_loc->symbol_ref = x;
21643 ca_loc->block = insn_scope (prev);
21644 if (call_arg_locations)
21645 call_arg_loc_last->next = ca_loc;
21646 else
21647 call_arg_locations = ca_loc;
21648 call_arg_loc_last = ca_loc;
21650 else if (!NOTE_DURING_CALL_P (loc_note))
21651 newloc->label = last_label;
21652 else
21654 if (!last_postcall_label)
21656 sprintf (loclabel, "%s-1", last_label);
21657 last_postcall_label = ggc_strdup (loclabel);
21659 newloc->label = last_postcall_label;
21662 last_var_location_insn = next_real;
21663 last_in_cold_section_p = in_cold_section_p;
21666 /* Note in one location list that text section has changed. */
21669 var_location_switch_text_section_1 (var_loc_list **slot, void *)
21671 var_loc_list *list = *slot;
21672 if (list->first)
21673 list->last_before_switch
21674 = list->last->next ? list->last->next : list->last;
21675 return 1;
21678 /* Note in all location lists that text section has changed. */
21680 static void
21681 var_location_switch_text_section (void)
21683 if (decl_loc_table == NULL)
21684 return;
21686 decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
21689 /* Create a new line number table. */
21691 static dw_line_info_table *
21692 new_line_info_table (void)
21694 dw_line_info_table *table;
21696 table = ggc_cleared_alloc<dw_line_info_table_struct> ();
21697 table->file_num = 1;
21698 table->line_num = 1;
21699 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
21701 return table;
21704 /* Lookup the "current" table into which we emit line info, so
21705 that we don't have to do it for every source line. */
21707 static void
21708 set_cur_line_info_table (section *sec)
21710 dw_line_info_table *table;
21712 if (sec == text_section)
21713 table = text_section_line_info;
21714 else if (sec == cold_text_section)
21716 table = cold_text_section_line_info;
21717 if (!table)
21719 cold_text_section_line_info = table = new_line_info_table ();
21720 table->end_label = cold_end_label;
21723 else
21725 const char *end_label;
21727 if (flag_reorder_blocks_and_partition)
21729 if (in_cold_section_p)
21730 end_label = crtl->subsections.cold_section_end_label;
21731 else
21732 end_label = crtl->subsections.hot_section_end_label;
21734 else
21736 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21737 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
21738 current_function_funcdef_no);
21739 end_label = ggc_strdup (label);
21742 table = new_line_info_table ();
21743 table->end_label = end_label;
21745 vec_safe_push (separate_line_info, table);
21748 if (DWARF2_ASM_LINE_DEBUG_INFO)
21749 table->is_stmt = (cur_line_info_table
21750 ? cur_line_info_table->is_stmt
21751 : DWARF_LINE_DEFAULT_IS_STMT_START);
21752 cur_line_info_table = table;
21756 /* We need to reset the locations at the beginning of each
21757 function. We can't do this in the end_function hook, because the
21758 declarations that use the locations won't have been output when
21759 that hook is called. Also compute have_multiple_function_sections here. */
21761 static void
21762 dwarf2out_begin_function (tree fun)
21764 section *sec = function_section (fun);
21766 if (sec != text_section)
21767 have_multiple_function_sections = true;
21769 if (flag_reorder_blocks_and_partition && !cold_text_section)
21771 gcc_assert (current_function_decl == fun);
21772 cold_text_section = unlikely_text_section ();
21773 switch_to_section (cold_text_section);
21774 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
21775 switch_to_section (sec);
21778 dwarf2out_note_section_used ();
21779 call_site_count = 0;
21780 tail_call_site_count = 0;
21782 set_cur_line_info_table (sec);
21785 /* Helper function of dwarf2out_end_function, called only after emitting
21786 the very first function into assembly. Check if some .debug_loc range
21787 might end with a .LVL* label that could be equal to .Ltext0.
21788 In that case we must force using absolute addresses in .debug_loc ranges,
21789 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
21790 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
21791 list terminator.
21792 Set have_multiple_function_sections to true in that case and
21793 terminate htab traversal. */
21796 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
21798 var_loc_list *entry = *slot;
21799 struct var_loc_node *node;
21801 node = entry->first;
21802 if (node && node->next && node->next->label)
21804 unsigned int i;
21805 const char *label = node->next->label;
21806 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
21808 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
21810 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
21811 if (strcmp (label, loclabel) == 0)
21813 have_multiple_function_sections = true;
21814 return 0;
21818 return 1;
21821 /* Hook called after emitting a function into assembly.
21822 This does something only for the very first function emitted. */
21824 static void
21825 dwarf2out_end_function (unsigned int)
21827 if (in_first_function_p
21828 && !have_multiple_function_sections
21829 && first_loclabel_num_not_at_text_label
21830 && decl_loc_table)
21831 decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
21832 in_first_function_p = false;
21833 maybe_at_text_label_p = false;
21836 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
21838 static void
21839 push_dw_line_info_entry (dw_line_info_table *table,
21840 enum dw_line_info_opcode opcode, unsigned int val)
21842 dw_line_info_entry e;
21843 e.opcode = opcode;
21844 e.val = val;
21845 vec_safe_push (table->entries, e);
21848 /* Output a label to mark the beginning of a source code line entry
21849 and record information relating to this source line, in
21850 'line_info_table' for later output of the .debug_line section. */
21851 /* ??? The discriminator parameter ought to be unsigned. */
21853 static void
21854 dwarf2out_source_line (unsigned int line, const char *filename,
21855 int discriminator, bool is_stmt)
21857 unsigned int file_num;
21858 dw_line_info_table *table;
21860 if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
21861 return;
21863 /* The discriminator column was added in dwarf4. Simplify the below
21864 by simply removing it if we're not supposed to output it. */
21865 if (dwarf_version < 4 && dwarf_strict)
21866 discriminator = 0;
21868 table = cur_line_info_table;
21869 file_num = maybe_emit_file (lookup_filename (filename));
21871 /* ??? TODO: Elide duplicate line number entries. Traditionally,
21872 the debugger has used the second (possibly duplicate) line number
21873 at the beginning of the function to mark the end of the prologue.
21874 We could eliminate any other duplicates within the function. For
21875 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
21876 that second line number entry. */
21877 /* Recall that this end-of-prologue indication is *not* the same thing
21878 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
21879 to which the hook corresponds, follows the last insn that was
21880 emitted by gen_prologue. What we need is to precede the first insn
21881 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
21882 insn that corresponds to something the user wrote. These may be
21883 very different locations once scheduling is enabled. */
21885 if (0 && file_num == table->file_num
21886 && line == table->line_num
21887 && discriminator == table->discrim_num
21888 && is_stmt == table->is_stmt)
21889 return;
21891 switch_to_section (current_function_section ());
21893 /* If requested, emit something human-readable. */
21894 if (flag_debug_asm)
21895 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
21897 if (DWARF2_ASM_LINE_DEBUG_INFO)
21899 /* Emit the .loc directive understood by GNU as. */
21900 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
21901 file_num, line, is_stmt, discriminator */
21902 fputs ("\t.loc ", asm_out_file);
21903 fprint_ul (asm_out_file, file_num);
21904 putc (' ', asm_out_file);
21905 fprint_ul (asm_out_file, line);
21906 putc (' ', asm_out_file);
21907 putc ('0', asm_out_file);
21909 if (is_stmt != table->is_stmt)
21911 fputs (" is_stmt ", asm_out_file);
21912 putc (is_stmt ? '1' : '0', asm_out_file);
21914 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
21916 gcc_assert (discriminator > 0);
21917 fputs (" discriminator ", asm_out_file);
21918 fprint_ul (asm_out_file, (unsigned long) discriminator);
21920 putc ('\n', asm_out_file);
21922 else
21924 unsigned int label_num = ++line_info_label_num;
21926 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
21928 push_dw_line_info_entry (table, LI_set_address, label_num);
21929 if (file_num != table->file_num)
21930 push_dw_line_info_entry (table, LI_set_file, file_num);
21931 if (discriminator != table->discrim_num)
21932 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
21933 if (is_stmt != table->is_stmt)
21934 push_dw_line_info_entry (table, LI_negate_stmt, 0);
21935 push_dw_line_info_entry (table, LI_set_line, line);
21938 table->file_num = file_num;
21939 table->line_num = line;
21940 table->discrim_num = discriminator;
21941 table->is_stmt = is_stmt;
21942 table->in_use = true;
21945 /* Record the beginning of a new source file. */
21947 static void
21948 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
21950 if (flag_eliminate_dwarf2_dups)
21952 /* Record the beginning of the file for break_out_includes. */
21953 dw_die_ref bincl_die;
21955 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
21956 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
21959 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21961 macinfo_entry e;
21962 e.code = DW_MACINFO_start_file;
21963 e.lineno = lineno;
21964 e.info = ggc_strdup (filename);
21965 vec_safe_push (macinfo_table, e);
21969 /* Record the end of a source file. */
21971 static void
21972 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
21974 if (flag_eliminate_dwarf2_dups)
21975 /* Record the end of the file for break_out_includes. */
21976 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
21978 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21980 macinfo_entry e;
21981 e.code = DW_MACINFO_end_file;
21982 e.lineno = lineno;
21983 e.info = NULL;
21984 vec_safe_push (macinfo_table, e);
21988 /* Called from debug_define in toplev.c. The `buffer' parameter contains
21989 the tail part of the directive line, i.e. the part which is past the
21990 initial whitespace, #, whitespace, directive-name, whitespace part. */
21992 static void
21993 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
21994 const char *buffer ATTRIBUTE_UNUSED)
21996 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21998 macinfo_entry e;
21999 /* Insert a dummy first entry to be able to optimize the whole
22000 predefined macro block using DW_MACRO_GNU_transparent_include. */
22001 if (macinfo_table->is_empty () && lineno <= 1)
22003 e.code = 0;
22004 e.lineno = 0;
22005 e.info = NULL;
22006 vec_safe_push (macinfo_table, e);
22008 e.code = DW_MACINFO_define;
22009 e.lineno = lineno;
22010 e.info = ggc_strdup (buffer);
22011 vec_safe_push (macinfo_table, e);
22015 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
22016 the tail part of the directive line, i.e. the part which is past the
22017 initial whitespace, #, whitespace, directive-name, whitespace part. */
22019 static void
22020 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
22021 const char *buffer ATTRIBUTE_UNUSED)
22023 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22025 macinfo_entry e;
22026 /* Insert a dummy first entry to be able to optimize the whole
22027 predefined macro block using DW_MACRO_GNU_transparent_include. */
22028 if (macinfo_table->is_empty () && lineno <= 1)
22030 e.code = 0;
22031 e.lineno = 0;
22032 e.info = NULL;
22033 vec_safe_push (macinfo_table, e);
22035 e.code = DW_MACINFO_undef;
22036 e.lineno = lineno;
22037 e.info = ggc_strdup (buffer);
22038 vec_safe_push (macinfo_table, e);
22042 /* Helpers to manipulate hash table of CUs. */
22044 struct macinfo_entry_hasher : typed_noop_remove <macinfo_entry>
22046 typedef macinfo_entry value_type;
22047 typedef macinfo_entry compare_type;
22048 static inline hashval_t hash (const value_type *);
22049 static inline bool equal (const value_type *, const compare_type *);
22052 inline hashval_t
22053 macinfo_entry_hasher::hash (const value_type *entry)
22055 return htab_hash_string (entry->info);
22058 inline bool
22059 macinfo_entry_hasher::equal (const value_type *entry1,
22060 const compare_type *entry2)
22062 return !strcmp (entry1->info, entry2->info);
22065 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
22067 /* Output a single .debug_macinfo entry. */
22069 static void
22070 output_macinfo_op (macinfo_entry *ref)
22072 int file_num;
22073 size_t len;
22074 struct indirect_string_node *node;
22075 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22076 struct dwarf_file_data *fd;
22078 switch (ref->code)
22080 case DW_MACINFO_start_file:
22081 fd = lookup_filename (ref->info);
22082 file_num = maybe_emit_file (fd);
22083 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
22084 dw2_asm_output_data_uleb128 (ref->lineno,
22085 "Included from line number %lu",
22086 (unsigned long) ref->lineno);
22087 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
22088 break;
22089 case DW_MACINFO_end_file:
22090 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
22091 break;
22092 case DW_MACINFO_define:
22093 case DW_MACINFO_undef:
22094 len = strlen (ref->info) + 1;
22095 if (!dwarf_strict
22096 && len > DWARF_OFFSET_SIZE
22097 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
22098 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
22100 ref->code = ref->code == DW_MACINFO_define
22101 ? DW_MACRO_GNU_define_indirect
22102 : DW_MACRO_GNU_undef_indirect;
22103 output_macinfo_op (ref);
22104 return;
22106 dw2_asm_output_data (1, ref->code,
22107 ref->code == DW_MACINFO_define
22108 ? "Define macro" : "Undefine macro");
22109 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
22110 (unsigned long) ref->lineno);
22111 dw2_asm_output_nstring (ref->info, -1, "The macro");
22112 break;
22113 case DW_MACRO_GNU_define_indirect:
22114 case DW_MACRO_GNU_undef_indirect:
22115 node = find_AT_string (ref->info);
22116 gcc_assert (node
22117 && ((node->form == DW_FORM_strp)
22118 || (node->form == DW_FORM_GNU_str_index)));
22119 dw2_asm_output_data (1, ref->code,
22120 ref->code == DW_MACRO_GNU_define_indirect
22121 ? "Define macro indirect"
22122 : "Undefine macro indirect");
22123 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
22124 (unsigned long) ref->lineno);
22125 if (node->form == DW_FORM_strp)
22126 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
22127 debug_str_section, "The macro: \"%s\"",
22128 ref->info);
22129 else
22130 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
22131 ref->info);
22132 break;
22133 case DW_MACRO_GNU_transparent_include:
22134 dw2_asm_output_data (1, ref->code, "Transparent include");
22135 ASM_GENERATE_INTERNAL_LABEL (label,
22136 DEBUG_MACRO_SECTION_LABEL, ref->lineno);
22137 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
22138 break;
22139 default:
22140 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
22141 ASM_COMMENT_START, (unsigned long) ref->code);
22142 break;
22146 /* Attempt to make a sequence of define/undef macinfo ops shareable with
22147 other compilation unit .debug_macinfo sections. IDX is the first
22148 index of a define/undef, return the number of ops that should be
22149 emitted in a comdat .debug_macinfo section and emit
22150 a DW_MACRO_GNU_transparent_include entry referencing it.
22151 If the define/undef entry should be emitted normally, return 0. */
22153 static unsigned
22154 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
22155 macinfo_hash_type **macinfo_htab)
22157 macinfo_entry *first, *second, *cur, *inc;
22158 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
22159 unsigned char checksum[16];
22160 struct md5_ctx ctx;
22161 char *grp_name, *tail;
22162 const char *base;
22163 unsigned int i, count, encoded_filename_len, linebuf_len;
22164 macinfo_entry **slot;
22166 first = &(*macinfo_table)[idx];
22167 second = &(*macinfo_table)[idx + 1];
22169 /* Optimize only if there are at least two consecutive define/undef ops,
22170 and either all of them are before first DW_MACINFO_start_file
22171 with lineno {0,1} (i.e. predefined macro block), or all of them are
22172 in some included header file. */
22173 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
22174 return 0;
22175 if (vec_safe_is_empty (files))
22177 if (first->lineno > 1 || second->lineno > 1)
22178 return 0;
22180 else if (first->lineno == 0)
22181 return 0;
22183 /* Find the last define/undef entry that can be grouped together
22184 with first and at the same time compute md5 checksum of their
22185 codes, linenumbers and strings. */
22186 md5_init_ctx (&ctx);
22187 for (i = idx; macinfo_table->iterate (i, &cur); i++)
22188 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
22189 break;
22190 else if (vec_safe_is_empty (files) && cur->lineno > 1)
22191 break;
22192 else
22194 unsigned char code = cur->code;
22195 md5_process_bytes (&code, 1, &ctx);
22196 checksum_uleb128 (cur->lineno, &ctx);
22197 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
22199 md5_finish_ctx (&ctx, checksum);
22200 count = i - idx;
22202 /* From the containing include filename (if any) pick up just
22203 usable characters from its basename. */
22204 if (vec_safe_is_empty (files))
22205 base = "";
22206 else
22207 base = lbasename (files->last ().info);
22208 for (encoded_filename_len = 0, i = 0; base[i]; i++)
22209 if (ISIDNUM (base[i]) || base[i] == '.')
22210 encoded_filename_len++;
22211 /* Count . at the end. */
22212 if (encoded_filename_len)
22213 encoded_filename_len++;
22215 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
22216 linebuf_len = strlen (linebuf);
22218 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
22219 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
22220 + 16 * 2 + 1);
22221 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
22222 tail = grp_name + 4;
22223 if (encoded_filename_len)
22225 for (i = 0; base[i]; i++)
22226 if (ISIDNUM (base[i]) || base[i] == '.')
22227 *tail++ = base[i];
22228 *tail++ = '.';
22230 memcpy (tail, linebuf, linebuf_len);
22231 tail += linebuf_len;
22232 *tail++ = '.';
22233 for (i = 0; i < 16; i++)
22234 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
22236 /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
22237 in the empty vector entry before the first define/undef. */
22238 inc = &(*macinfo_table)[idx - 1];
22239 inc->code = DW_MACRO_GNU_transparent_include;
22240 inc->lineno = 0;
22241 inc->info = ggc_strdup (grp_name);
22242 if (!*macinfo_htab)
22243 *macinfo_htab = new macinfo_hash_type (10);
22244 /* Avoid emitting duplicates. */
22245 slot = (*macinfo_htab)->find_slot (inc, INSERT);
22246 if (*slot != NULL)
22248 inc->code = 0;
22249 inc->info = NULL;
22250 /* If such an entry has been used before, just emit
22251 a DW_MACRO_GNU_transparent_include op. */
22252 inc = *slot;
22253 output_macinfo_op (inc);
22254 /* And clear all macinfo_entry in the range to avoid emitting them
22255 in the second pass. */
22256 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
22258 cur->code = 0;
22259 cur->info = NULL;
22262 else
22264 *slot = inc;
22265 inc->lineno = (*macinfo_htab)->elements ();
22266 output_macinfo_op (inc);
22268 return count;
22271 /* Save any strings needed by the macinfo table in the debug str
22272 table. All strings must be collected into the table by the time
22273 index_string is called. */
22275 static void
22276 save_macinfo_strings (void)
22278 unsigned len;
22279 unsigned i;
22280 macinfo_entry *ref;
22282 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
22284 switch (ref->code)
22286 /* Match the logic in output_macinfo_op to decide on
22287 indirect strings. */
22288 case DW_MACINFO_define:
22289 case DW_MACINFO_undef:
22290 len = strlen (ref->info) + 1;
22291 if (!dwarf_strict
22292 && len > DWARF_OFFSET_SIZE
22293 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
22294 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
22295 set_indirect_string (find_AT_string (ref->info));
22296 break;
22297 case DW_MACRO_GNU_define_indirect:
22298 case DW_MACRO_GNU_undef_indirect:
22299 set_indirect_string (find_AT_string (ref->info));
22300 break;
22301 default:
22302 break;
22307 /* Output macinfo section(s). */
22309 static void
22310 output_macinfo (void)
22312 unsigned i;
22313 unsigned long length = vec_safe_length (macinfo_table);
22314 macinfo_entry *ref;
22315 vec<macinfo_entry, va_gc> *files = NULL;
22316 macinfo_hash_type *macinfo_htab = NULL;
22318 if (! length)
22319 return;
22321 /* output_macinfo* uses these interchangeably. */
22322 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
22323 && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
22324 && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
22325 && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
22327 /* For .debug_macro emit the section header. */
22328 if (!dwarf_strict)
22330 dw2_asm_output_data (2, 4, "DWARF macro version number");
22331 if (DWARF_OFFSET_SIZE == 8)
22332 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
22333 else
22334 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
22335 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
22336 (!dwarf_split_debug_info ? debug_line_section_label
22337 : debug_skeleton_line_section_label),
22338 debug_line_section, NULL);
22341 /* In the first loop, it emits the primary .debug_macinfo section
22342 and after each emitted op the macinfo_entry is cleared.
22343 If a longer range of define/undef ops can be optimized using
22344 DW_MACRO_GNU_transparent_include, the
22345 DW_MACRO_GNU_transparent_include op is emitted and kept in
22346 the vector before the first define/undef in the range and the
22347 whole range of define/undef ops is not emitted and kept. */
22348 for (i = 0; macinfo_table->iterate (i, &ref); i++)
22350 switch (ref->code)
22352 case DW_MACINFO_start_file:
22353 vec_safe_push (files, *ref);
22354 break;
22355 case DW_MACINFO_end_file:
22356 if (!vec_safe_is_empty (files))
22357 files->pop ();
22358 break;
22359 case DW_MACINFO_define:
22360 case DW_MACINFO_undef:
22361 if (!dwarf_strict
22362 && HAVE_COMDAT_GROUP
22363 && vec_safe_length (files) != 1
22364 && i > 0
22365 && i + 1 < length
22366 && (*macinfo_table)[i - 1].code == 0)
22368 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
22369 if (count)
22371 i += count - 1;
22372 continue;
22375 break;
22376 case 0:
22377 /* A dummy entry may be inserted at the beginning to be able
22378 to optimize the whole block of predefined macros. */
22379 if (i == 0)
22380 continue;
22381 default:
22382 break;
22384 output_macinfo_op (ref);
22385 ref->info = NULL;
22386 ref->code = 0;
22389 if (!macinfo_htab)
22390 return;
22392 delete macinfo_htab;
22393 macinfo_htab = NULL;
22395 /* If any DW_MACRO_GNU_transparent_include were used, on those
22396 DW_MACRO_GNU_transparent_include entries terminate the
22397 current chain and switch to a new comdat .debug_macinfo
22398 section and emit the define/undef entries within it. */
22399 for (i = 0; macinfo_table->iterate (i, &ref); i++)
22400 switch (ref->code)
22402 case 0:
22403 continue;
22404 case DW_MACRO_GNU_transparent_include:
22406 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22407 tree comdat_key = get_identifier (ref->info);
22408 /* Terminate the previous .debug_macinfo section. */
22409 dw2_asm_output_data (1, 0, "End compilation unit");
22410 targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
22411 SECTION_DEBUG
22412 | SECTION_LINKONCE,
22413 comdat_key);
22414 ASM_GENERATE_INTERNAL_LABEL (label,
22415 DEBUG_MACRO_SECTION_LABEL,
22416 ref->lineno);
22417 ASM_OUTPUT_LABEL (asm_out_file, label);
22418 ref->code = 0;
22419 ref->info = NULL;
22420 dw2_asm_output_data (2, 4, "DWARF macro version number");
22421 if (DWARF_OFFSET_SIZE == 8)
22422 dw2_asm_output_data (1, 1, "Flags: 64-bit");
22423 else
22424 dw2_asm_output_data (1, 0, "Flags: 32-bit");
22426 break;
22427 case DW_MACINFO_define:
22428 case DW_MACINFO_undef:
22429 output_macinfo_op (ref);
22430 ref->code = 0;
22431 ref->info = NULL;
22432 break;
22433 default:
22434 gcc_unreachable ();
22438 /* Set up for Dwarf output at the start of compilation. */
22440 static void
22441 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
22443 /* Allocate the file_table. */
22444 file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
22446 /* Allocate the decl_die_table. */
22447 decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
22449 /* Allocate the decl_loc_table. */
22450 decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
22452 /* Allocate the cached_dw_loc_list_table. */
22453 cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
22455 /* Allocate the initial hunk of the decl_scope_table. */
22456 vec_alloc (decl_scope_table, 256);
22458 /* Allocate the initial hunk of the abbrev_die_table. */
22459 abbrev_die_table = ggc_cleared_vec_alloc<dw_die_ref>
22460 (ABBREV_DIE_TABLE_INCREMENT);
22461 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
22462 /* Zero-th entry is allocated, but unused. */
22463 abbrev_die_table_in_use = 1;
22465 /* Allocate the pubtypes and pubnames vectors. */
22466 vec_alloc (pubname_table, 32);
22467 vec_alloc (pubtype_table, 32);
22469 vec_alloc (incomplete_types, 64);
22471 vec_alloc (used_rtx_array, 32);
22473 if (!dwarf_split_debug_info)
22475 debug_info_section = get_section (DEBUG_INFO_SECTION,
22476 SECTION_DEBUG, NULL);
22477 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22478 SECTION_DEBUG, NULL);
22479 debug_loc_section = get_section (DEBUG_LOC_SECTION,
22480 SECTION_DEBUG, NULL);
22482 else
22484 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
22485 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22486 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
22487 SECTION_DEBUG | SECTION_EXCLUDE,
22488 NULL);
22489 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
22490 SECTION_DEBUG, NULL);
22491 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
22492 SECTION_DEBUG, NULL);
22493 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22494 SECTION_DEBUG, NULL);
22495 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
22496 DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
22498 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
22499 the main .o, but the skeleton_line goes into the split off dwo. */
22500 debug_skeleton_line_section
22501 = get_section (DEBUG_DWO_LINE_SECTION,
22502 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22503 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
22504 DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
22505 debug_str_offsets_section = get_section (DEBUG_STR_OFFSETS_SECTION,
22506 SECTION_DEBUG | SECTION_EXCLUDE,
22507 NULL);
22508 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
22509 DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
22510 debug_loc_section = get_section (DEBUG_DWO_LOC_SECTION,
22511 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22512 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
22513 DEBUG_STR_DWO_SECTION_FLAGS, NULL);
22515 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
22516 SECTION_DEBUG, NULL);
22517 debug_macinfo_section = get_section (dwarf_strict
22518 ? DEBUG_MACINFO_SECTION
22519 : DEBUG_MACRO_SECTION,
22520 DEBUG_MACRO_SECTION_FLAGS, NULL);
22521 debug_line_section = get_section (DEBUG_LINE_SECTION,
22522 SECTION_DEBUG, NULL);
22523 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
22524 SECTION_DEBUG, NULL);
22525 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
22526 SECTION_DEBUG, NULL);
22527 debug_str_section = get_section (DEBUG_STR_SECTION,
22528 DEBUG_STR_SECTION_FLAGS, NULL);
22529 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
22530 SECTION_DEBUG, NULL);
22531 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
22532 SECTION_DEBUG, NULL);
22534 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
22535 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
22536 DEBUG_ABBREV_SECTION_LABEL, 0);
22537 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
22538 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
22539 COLD_TEXT_SECTION_LABEL, 0);
22540 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
22542 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
22543 DEBUG_INFO_SECTION_LABEL, 0);
22544 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
22545 DEBUG_LINE_SECTION_LABEL, 0);
22546 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
22547 DEBUG_RANGES_SECTION_LABEL, 0);
22548 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
22549 DEBUG_ADDR_SECTION_LABEL, 0);
22550 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
22551 dwarf_strict
22552 ? DEBUG_MACINFO_SECTION_LABEL
22553 : DEBUG_MACRO_SECTION_LABEL, 0);
22554 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
22556 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22557 vec_alloc (macinfo_table, 64);
22559 switch_to_section (text_section);
22560 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
22562 /* Make sure the line number table for .text always exists. */
22563 text_section_line_info = new_line_info_table ();
22564 text_section_line_info->end_label = text_end_label;
22567 /* Called before compile () starts outputtting functions, variables
22568 and toplevel asms into assembly. */
22570 static void
22571 dwarf2out_assembly_start (void)
22573 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
22574 && dwarf2out_do_cfi_asm ()
22575 && (!(flag_unwind_tables || flag_exceptions)
22576 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
22577 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
22580 /* A helper function for dwarf2out_finish called through
22581 htab_traverse. Assign a string its index. All strings must be
22582 collected into the table by the time index_string is called,
22583 because the indexing code relies on htab_traverse to traverse nodes
22584 in the same order for each run. */
22587 index_string (indirect_string_node **h, unsigned int *index)
22589 indirect_string_node *node = *h;
22591 find_string_form (node);
22592 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22594 gcc_assert (node->index == NO_INDEX_ASSIGNED);
22595 node->index = *index;
22596 *index += 1;
22598 return 1;
22601 /* A helper function for output_indirect_strings called through
22602 htab_traverse. Output the offset to a string and update the
22603 current offset. */
22606 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
22608 indirect_string_node *node = *h;
22610 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22612 /* Assert that this node has been assigned an index. */
22613 gcc_assert (node->index != NO_INDEX_ASSIGNED
22614 && node->index != NOT_INDEXED);
22615 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
22616 "indexed string 0x%x: %s", node->index, node->str);
22617 *offset += strlen (node->str) + 1;
22619 return 1;
22622 /* A helper function for dwarf2out_finish called through
22623 htab_traverse. Output the indexed string. */
22626 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
22628 struct indirect_string_node *node = *h;
22630 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22632 /* Assert that the strings are output in the same order as their
22633 indexes were assigned. */
22634 gcc_assert (*cur_idx == node->index);
22635 assemble_string (node->str, strlen (node->str) + 1);
22636 *cur_idx += 1;
22638 return 1;
22641 /* A helper function for dwarf2out_finish called through
22642 htab_traverse. Emit one queued .debug_str string. */
22645 output_indirect_string (indirect_string_node **h, void *)
22647 struct indirect_string_node *node = *h;
22649 node->form = find_string_form (node);
22650 if (node->form == DW_FORM_strp && node->refcount > 0)
22652 ASM_OUTPUT_LABEL (asm_out_file, node->label);
22653 assemble_string (node->str, strlen (node->str) + 1);
22656 return 1;
22659 /* Output the indexed string table. */
22661 static void
22662 output_indirect_strings (void)
22664 switch_to_section (debug_str_section);
22665 if (!dwarf_split_debug_info)
22666 debug_str_hash->traverse<void *, output_indirect_string> (NULL);
22667 else
22669 unsigned int offset = 0;
22670 unsigned int cur_idx = 0;
22672 skeleton_debug_str_hash->traverse<void *, output_indirect_string> (NULL);
22674 switch_to_section (debug_str_offsets_section);
22675 debug_str_hash->traverse_noresize
22676 <unsigned int *, output_index_string_offset> (&offset);
22677 switch_to_section (debug_str_dwo_section);
22678 debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
22679 (&cur_idx);
22683 /* Callback for htab_traverse to assign an index to an entry in the
22684 table, and to write that entry to the .debug_addr section. */
22687 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
22689 addr_table_entry *entry = *slot;
22691 if (entry->refcount == 0)
22693 gcc_assert (entry->index == NO_INDEX_ASSIGNED
22694 || entry->index == NOT_INDEXED);
22695 return 1;
22698 gcc_assert (entry->index == *cur_index);
22699 (*cur_index)++;
22701 switch (entry->kind)
22703 case ate_kind_rtx:
22704 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
22705 "0x%x", entry->index);
22706 break;
22707 case ate_kind_rtx_dtprel:
22708 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
22709 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
22710 DWARF2_ADDR_SIZE,
22711 entry->addr.rtl);
22712 fputc ('\n', asm_out_file);
22713 break;
22714 case ate_kind_label:
22715 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
22716 "0x%x", entry->index);
22717 break;
22718 default:
22719 gcc_unreachable ();
22721 return 1;
22724 /* Produce the .debug_addr section. */
22726 static void
22727 output_addr_table (void)
22729 unsigned int index = 0;
22730 if (addr_index_table == NULL || addr_index_table->size () == 0)
22731 return;
22733 switch_to_section (debug_addr_section);
22734 addr_index_table
22735 ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
22738 #if ENABLE_ASSERT_CHECKING
22739 /* Verify that all marks are clear. */
22741 static void
22742 verify_marks_clear (dw_die_ref die)
22744 dw_die_ref c;
22746 gcc_assert (! die->die_mark);
22747 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
22749 #endif /* ENABLE_ASSERT_CHECKING */
22751 /* Clear the marks for a die and its children.
22752 Be cool if the mark isn't set. */
22754 static void
22755 prune_unmark_dies (dw_die_ref die)
22757 dw_die_ref c;
22759 if (die->die_mark)
22760 die->die_mark = 0;
22761 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
22764 /* Given DIE that we're marking as used, find any other dies
22765 it references as attributes and mark them as used. */
22767 static void
22768 prune_unused_types_walk_attribs (dw_die_ref die)
22770 dw_attr_ref a;
22771 unsigned ix;
22773 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22775 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
22777 /* A reference to another DIE.
22778 Make sure that it will get emitted.
22779 If it was broken out into a comdat group, don't follow it. */
22780 if (! AT_ref (a)->comdat_type_p
22781 || a->dw_attr == DW_AT_specification)
22782 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
22784 /* Set the string's refcount to 0 so that prune_unused_types_mark
22785 accounts properly for it. */
22786 if (AT_class (a) == dw_val_class_str)
22787 a->dw_attr_val.v.val_str->refcount = 0;
22791 /* Mark the generic parameters and arguments children DIEs of DIE. */
22793 static void
22794 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
22796 dw_die_ref c;
22798 if (die == NULL || die->die_child == NULL)
22799 return;
22800 c = die->die_child;
22803 if (is_template_parameter (c))
22804 prune_unused_types_mark (c, 1);
22805 c = c->die_sib;
22806 } while (c && c != die->die_child);
22809 /* Mark DIE as being used. If DOKIDS is true, then walk down
22810 to DIE's children. */
22812 static void
22813 prune_unused_types_mark (dw_die_ref die, int dokids)
22815 dw_die_ref c;
22817 if (die->die_mark == 0)
22819 /* We haven't done this node yet. Mark it as used. */
22820 die->die_mark = 1;
22821 /* If this is the DIE of a generic type instantiation,
22822 mark the children DIEs that describe its generic parms and
22823 args. */
22824 prune_unused_types_mark_generic_parms_dies (die);
22826 /* We also have to mark its parents as used.
22827 (But we don't want to mark our parent's kids due to this,
22828 unless it is a class.) */
22829 if (die->die_parent)
22830 prune_unused_types_mark (die->die_parent,
22831 class_scope_p (die->die_parent));
22833 /* Mark any referenced nodes. */
22834 prune_unused_types_walk_attribs (die);
22836 /* If this node is a specification,
22837 also mark the definition, if it exists. */
22838 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
22839 prune_unused_types_mark (die->die_definition, 1);
22842 if (dokids && die->die_mark != 2)
22844 /* We need to walk the children, but haven't done so yet.
22845 Remember that we've walked the kids. */
22846 die->die_mark = 2;
22848 /* If this is an array type, we need to make sure our
22849 kids get marked, even if they're types. If we're
22850 breaking out types into comdat sections, do this
22851 for all type definitions. */
22852 if (die->die_tag == DW_TAG_array_type
22853 || (use_debug_types
22854 && is_type_die (die) && ! is_declaration_die (die)))
22855 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
22856 else
22857 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22861 /* For local classes, look if any static member functions were emitted
22862 and if so, mark them. */
22864 static void
22865 prune_unused_types_walk_local_classes (dw_die_ref die)
22867 dw_die_ref c;
22869 if (die->die_mark == 2)
22870 return;
22872 switch (die->die_tag)
22874 case DW_TAG_structure_type:
22875 case DW_TAG_union_type:
22876 case DW_TAG_class_type:
22877 break;
22879 case DW_TAG_subprogram:
22880 if (!get_AT_flag (die, DW_AT_declaration)
22881 || die->die_definition != NULL)
22882 prune_unused_types_mark (die, 1);
22883 return;
22885 default:
22886 return;
22889 /* Mark children. */
22890 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
22893 /* Walk the tree DIE and mark types that we actually use. */
22895 static void
22896 prune_unused_types_walk (dw_die_ref die)
22898 dw_die_ref c;
22900 /* Don't do anything if this node is already marked and
22901 children have been marked as well. */
22902 if (die->die_mark == 2)
22903 return;
22905 switch (die->die_tag)
22907 case DW_TAG_structure_type:
22908 case DW_TAG_union_type:
22909 case DW_TAG_class_type:
22910 if (die->die_perennial_p)
22911 break;
22913 for (c = die->die_parent; c; c = c->die_parent)
22914 if (c->die_tag == DW_TAG_subprogram)
22915 break;
22917 /* Finding used static member functions inside of classes
22918 is needed just for local classes, because for other classes
22919 static member function DIEs with DW_AT_specification
22920 are emitted outside of the DW_TAG_*_type. If we ever change
22921 it, we'd need to call this even for non-local classes. */
22922 if (c)
22923 prune_unused_types_walk_local_classes (die);
22925 /* It's a type node --- don't mark it. */
22926 return;
22928 case DW_TAG_const_type:
22929 case DW_TAG_packed_type:
22930 case DW_TAG_pointer_type:
22931 case DW_TAG_reference_type:
22932 case DW_TAG_rvalue_reference_type:
22933 case DW_TAG_volatile_type:
22934 case DW_TAG_typedef:
22935 case DW_TAG_array_type:
22936 case DW_TAG_interface_type:
22937 case DW_TAG_friend:
22938 case DW_TAG_variant_part:
22939 case DW_TAG_enumeration_type:
22940 case DW_TAG_subroutine_type:
22941 case DW_TAG_string_type:
22942 case DW_TAG_set_type:
22943 case DW_TAG_subrange_type:
22944 case DW_TAG_ptr_to_member_type:
22945 case DW_TAG_file_type:
22946 if (die->die_perennial_p)
22947 break;
22949 /* It's a type node --- don't mark it. */
22950 return;
22952 default:
22953 /* Mark everything else. */
22954 break;
22957 if (die->die_mark == 0)
22959 die->die_mark = 1;
22961 /* Now, mark any dies referenced from here. */
22962 prune_unused_types_walk_attribs (die);
22965 die->die_mark = 2;
22967 /* Mark children. */
22968 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22971 /* Increment the string counts on strings referred to from DIE's
22972 attributes. */
22974 static void
22975 prune_unused_types_update_strings (dw_die_ref die)
22977 dw_attr_ref a;
22978 unsigned ix;
22980 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22981 if (AT_class (a) == dw_val_class_str)
22983 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
22984 s->refcount++;
22985 /* Avoid unnecessarily putting strings that are used less than
22986 twice in the hash table. */
22987 if (s->refcount
22988 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
22990 indirect_string_node **slot
22991 = debug_str_hash->find_slot_with_hash (s->str,
22992 htab_hash_string (s->str),
22993 INSERT);
22994 gcc_assert (*slot == NULL);
22995 *slot = s;
23000 /* Remove from the tree DIE any dies that aren't marked. */
23002 static void
23003 prune_unused_types_prune (dw_die_ref die)
23005 dw_die_ref c;
23007 gcc_assert (die->die_mark);
23008 prune_unused_types_update_strings (die);
23010 if (! die->die_child)
23011 return;
23013 c = die->die_child;
23014 do {
23015 dw_die_ref prev = c;
23016 for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
23017 if (c == die->die_child)
23019 /* No marked children between 'prev' and the end of the list. */
23020 if (prev == c)
23021 /* No marked children at all. */
23022 die->die_child = NULL;
23023 else
23025 prev->die_sib = c->die_sib;
23026 die->die_child = prev;
23028 return;
23031 if (c != prev->die_sib)
23032 prev->die_sib = c;
23033 prune_unused_types_prune (c);
23034 } while (c != die->die_child);
23037 /* Remove dies representing declarations that we never use. */
23039 static void
23040 prune_unused_types (void)
23042 unsigned int i;
23043 limbo_die_node *node;
23044 comdat_type_node *ctnode;
23045 pubname_ref pub;
23046 dw_die_ref base_type;
23048 #if ENABLE_ASSERT_CHECKING
23049 /* All the marks should already be clear. */
23050 verify_marks_clear (comp_unit_die ());
23051 for (node = limbo_die_list; node; node = node->next)
23052 verify_marks_clear (node->die);
23053 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23054 verify_marks_clear (ctnode->root_die);
23055 #endif /* ENABLE_ASSERT_CHECKING */
23057 /* Mark types that are used in global variables. */
23058 premark_types_used_by_global_vars ();
23060 /* Set the mark on nodes that are actually used. */
23061 prune_unused_types_walk (comp_unit_die ());
23062 for (node = limbo_die_list; node; node = node->next)
23063 prune_unused_types_walk (node->die);
23064 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23066 prune_unused_types_walk (ctnode->root_die);
23067 prune_unused_types_mark (ctnode->type_die, 1);
23070 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
23071 are unusual in that they are pubnames that are the children of pubtypes.
23072 They should only be marked via their parent DW_TAG_enumeration_type die,
23073 not as roots in themselves. */
23074 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
23075 if (pub->die->die_tag != DW_TAG_enumerator)
23076 prune_unused_types_mark (pub->die, 1);
23077 for (i = 0; base_types.iterate (i, &base_type); i++)
23078 prune_unused_types_mark (base_type, 1);
23080 if (debug_str_hash)
23081 debug_str_hash->empty ();
23082 if (skeleton_debug_str_hash)
23083 skeleton_debug_str_hash->empty ();
23084 prune_unused_types_prune (comp_unit_die ());
23085 for (node = limbo_die_list; node; node = node->next)
23086 prune_unused_types_prune (node->die);
23087 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23088 prune_unused_types_prune (ctnode->root_die);
23090 /* Leave the marks clear. */
23091 prune_unmark_dies (comp_unit_die ());
23092 for (node = limbo_die_list; node; node = node->next)
23093 prune_unmark_dies (node->die);
23094 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23095 prune_unmark_dies (ctnode->root_die);
23098 /* Set the parameter to true if there are any relative pathnames in
23099 the file table. */
23101 file_table_relative_p (dwarf_file_data **slot, bool *p)
23103 struct dwarf_file_data *d = *slot;
23104 if (!IS_ABSOLUTE_PATH (d->filename))
23106 *p = true;
23107 return 0;
23109 return 1;
23112 /* Helpers to manipulate hash table of comdat type units. */
23114 struct comdat_type_hasher : typed_noop_remove <comdat_type_node>
23116 typedef comdat_type_node value_type;
23117 typedef comdat_type_node compare_type;
23118 static inline hashval_t hash (const value_type *);
23119 static inline bool equal (const value_type *, const compare_type *);
23122 inline hashval_t
23123 comdat_type_hasher::hash (const value_type *type_node)
23125 hashval_t h;
23126 memcpy (&h, type_node->signature, sizeof (h));
23127 return h;
23130 inline bool
23131 comdat_type_hasher::equal (const value_type *type_node_1,
23132 const compare_type *type_node_2)
23134 return (! memcmp (type_node_1->signature, type_node_2->signature,
23135 DWARF_TYPE_SIGNATURE_SIZE));
23138 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
23139 to the location it would have been added, should we know its
23140 DECL_ASSEMBLER_NAME when we added other attributes. This will
23141 probably improve compactness of debug info, removing equivalent
23142 abbrevs, and hide any differences caused by deferring the
23143 computation of the assembler name, triggered by e.g. PCH. */
23145 static inline void
23146 move_linkage_attr (dw_die_ref die)
23148 unsigned ix = vec_safe_length (die->die_attr);
23149 dw_attr_node linkage = (*die->die_attr)[ix - 1];
23151 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
23152 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
23154 while (--ix > 0)
23156 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
23158 if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
23159 break;
23162 if (ix != vec_safe_length (die->die_attr) - 1)
23164 die->die_attr->pop ();
23165 die->die_attr->quick_insert (ix, linkage);
23169 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
23170 referenced from typed stack ops and count how often they are used. */
23172 static void
23173 mark_base_types (dw_loc_descr_ref loc)
23175 dw_die_ref base_type = NULL;
23177 for (; loc; loc = loc->dw_loc_next)
23179 switch (loc->dw_loc_opc)
23181 case DW_OP_GNU_regval_type:
23182 case DW_OP_GNU_deref_type:
23183 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
23184 break;
23185 case DW_OP_GNU_convert:
23186 case DW_OP_GNU_reinterpret:
23187 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
23188 continue;
23189 /* FALLTHRU */
23190 case DW_OP_GNU_const_type:
23191 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
23192 break;
23193 case DW_OP_GNU_entry_value:
23194 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
23195 continue;
23196 default:
23197 continue;
23199 gcc_assert (base_type->die_parent == comp_unit_die ());
23200 if (base_type->die_mark)
23201 base_type->die_mark++;
23202 else
23204 base_types.safe_push (base_type);
23205 base_type->die_mark = 1;
23210 /* Comparison function for sorting marked base types. */
23212 static int
23213 base_type_cmp (const void *x, const void *y)
23215 dw_die_ref dx = *(const dw_die_ref *) x;
23216 dw_die_ref dy = *(const dw_die_ref *) y;
23217 unsigned int byte_size1, byte_size2;
23218 unsigned int encoding1, encoding2;
23219 if (dx->die_mark > dy->die_mark)
23220 return -1;
23221 if (dx->die_mark < dy->die_mark)
23222 return 1;
23223 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
23224 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
23225 if (byte_size1 < byte_size2)
23226 return 1;
23227 if (byte_size1 > byte_size2)
23228 return -1;
23229 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
23230 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
23231 if (encoding1 < encoding2)
23232 return 1;
23233 if (encoding1 > encoding2)
23234 return -1;
23235 return 0;
23238 /* Move base types marked by mark_base_types as early as possible
23239 in the CU, sorted by decreasing usage count both to make the
23240 uleb128 references as small as possible and to make sure they
23241 will have die_offset already computed by calc_die_sizes when
23242 sizes of typed stack loc ops is computed. */
23244 static void
23245 move_marked_base_types (void)
23247 unsigned int i;
23248 dw_die_ref base_type, die, c;
23250 if (base_types.is_empty ())
23251 return;
23253 /* Sort by decreasing usage count, they will be added again in that
23254 order later on. */
23255 base_types.qsort (base_type_cmp);
23256 die = comp_unit_die ();
23257 c = die->die_child;
23260 dw_die_ref prev = c;
23261 c = c->die_sib;
23262 while (c->die_mark)
23264 remove_child_with_prev (c, prev);
23265 /* As base types got marked, there must be at least
23266 one node other than DW_TAG_base_type. */
23267 gcc_assert (c != c->die_sib);
23268 c = c->die_sib;
23271 while (c != die->die_child);
23272 gcc_assert (die->die_child);
23273 c = die->die_child;
23274 for (i = 0; base_types.iterate (i, &base_type); i++)
23276 base_type->die_mark = 0;
23277 base_type->die_sib = c->die_sib;
23278 c->die_sib = base_type;
23279 c = base_type;
23283 /* Helper function for resolve_addr, attempt to resolve
23284 one CONST_STRING, return true if successful. Similarly verify that
23285 SYMBOL_REFs refer to variables emitted in the current CU. */
23287 static bool
23288 resolve_one_addr (rtx *addr)
23290 rtx rtl = *addr;
23292 if (GET_CODE (rtl) == CONST_STRING)
23294 size_t len = strlen (XSTR (rtl, 0)) + 1;
23295 tree t = build_string (len, XSTR (rtl, 0));
23296 tree tlen = size_int (len - 1);
23297 TREE_TYPE (t)
23298 = build_array_type (char_type_node, build_index_type (tlen));
23299 rtl = lookup_constant_def (t);
23300 if (!rtl || !MEM_P (rtl))
23301 return false;
23302 rtl = XEXP (rtl, 0);
23303 if (GET_CODE (rtl) == SYMBOL_REF
23304 && SYMBOL_REF_DECL (rtl)
23305 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
23306 return false;
23307 vec_safe_push (used_rtx_array, rtl);
23308 *addr = rtl;
23309 return true;
23312 if (GET_CODE (rtl) == SYMBOL_REF
23313 && SYMBOL_REF_DECL (rtl))
23315 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
23317 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
23318 return false;
23320 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
23321 return false;
23324 if (GET_CODE (rtl) == CONST)
23326 subrtx_ptr_iterator::array_type array;
23327 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
23328 if (!resolve_one_addr (*iter))
23329 return false;
23332 return true;
23335 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
23336 if possible, and create DW_TAG_dwarf_procedure that can be referenced
23337 from DW_OP_GNU_implicit_pointer if the string hasn't been seen yet. */
23339 static rtx
23340 string_cst_pool_decl (tree t)
23342 rtx rtl = output_constant_def (t, 1);
23343 unsigned char *array;
23344 dw_loc_descr_ref l;
23345 tree decl;
23346 size_t len;
23347 dw_die_ref ref;
23349 if (!rtl || !MEM_P (rtl))
23350 return NULL_RTX;
23351 rtl = XEXP (rtl, 0);
23352 if (GET_CODE (rtl) != SYMBOL_REF
23353 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
23354 return NULL_RTX;
23356 decl = SYMBOL_REF_DECL (rtl);
23357 if (!lookup_decl_die (decl))
23359 len = TREE_STRING_LENGTH (t);
23360 vec_safe_push (used_rtx_array, rtl);
23361 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
23362 array = ggc_vec_alloc<unsigned char> (len);
23363 memcpy (array, TREE_STRING_POINTER (t), len);
23364 l = new_loc_descr (DW_OP_implicit_value, len, 0);
23365 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
23366 l->dw_loc_oprnd2.v.val_vec.length = len;
23367 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
23368 l->dw_loc_oprnd2.v.val_vec.array = array;
23369 add_AT_loc (ref, DW_AT_location, l);
23370 equate_decl_number_to_die (decl, ref);
23372 return rtl;
23375 /* Helper function of resolve_addr_in_expr. LOC is
23376 a DW_OP_addr followed by DW_OP_stack_value, either at the start
23377 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
23378 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
23379 with DW_OP_GNU_implicit_pointer if possible
23380 and return true, if unsuccessful, return false. */
23382 static bool
23383 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
23385 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
23386 HOST_WIDE_INT offset = 0;
23387 dw_die_ref ref = NULL;
23388 tree decl;
23390 if (GET_CODE (rtl) == CONST
23391 && GET_CODE (XEXP (rtl, 0)) == PLUS
23392 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
23394 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
23395 rtl = XEXP (XEXP (rtl, 0), 0);
23397 if (GET_CODE (rtl) == CONST_STRING)
23399 size_t len = strlen (XSTR (rtl, 0)) + 1;
23400 tree t = build_string (len, XSTR (rtl, 0));
23401 tree tlen = size_int (len - 1);
23403 TREE_TYPE (t)
23404 = build_array_type (char_type_node, build_index_type (tlen));
23405 rtl = string_cst_pool_decl (t);
23406 if (!rtl)
23407 return false;
23409 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
23411 decl = SYMBOL_REF_DECL (rtl);
23412 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
23414 ref = lookup_decl_die (decl);
23415 if (ref && (get_AT (ref, DW_AT_location)
23416 || get_AT (ref, DW_AT_const_value)))
23418 loc->dw_loc_opc = DW_OP_GNU_implicit_pointer;
23419 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23420 loc->dw_loc_oprnd1.val_entry = NULL;
23421 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
23422 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
23423 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
23424 loc->dw_loc_oprnd2.v.val_int = offset;
23425 return true;
23429 return false;
23432 /* Helper function for resolve_addr, handle one location
23433 expression, return false if at least one CONST_STRING or SYMBOL_REF in
23434 the location list couldn't be resolved. */
23436 static bool
23437 resolve_addr_in_expr (dw_loc_descr_ref loc)
23439 dw_loc_descr_ref keep = NULL;
23440 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
23441 switch (loc->dw_loc_opc)
23443 case DW_OP_addr:
23444 if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
23446 if ((prev == NULL
23447 || prev->dw_loc_opc == DW_OP_piece
23448 || prev->dw_loc_opc == DW_OP_bit_piece)
23449 && loc->dw_loc_next
23450 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
23451 && !dwarf_strict
23452 && optimize_one_addr_into_implicit_ptr (loc))
23453 break;
23454 return false;
23456 break;
23457 case DW_OP_GNU_addr_index:
23458 case DW_OP_GNU_const_index:
23459 if (loc->dw_loc_opc == DW_OP_GNU_addr_index
23460 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
23462 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
23463 if (!resolve_one_addr (&rtl))
23464 return false;
23465 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
23466 loc->dw_loc_oprnd1.val_entry =
23467 add_addr_table_entry (rtl, ate_kind_rtx);
23469 break;
23470 case DW_OP_const4u:
23471 case DW_OP_const8u:
23472 if (loc->dtprel
23473 && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
23474 return false;
23475 break;
23476 case DW_OP_plus_uconst:
23477 if (size_of_loc_descr (loc)
23478 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
23480 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
23482 dw_loc_descr_ref repl
23483 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
23484 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
23485 add_loc_descr (&repl, loc->dw_loc_next);
23486 *loc = *repl;
23488 break;
23489 case DW_OP_implicit_value:
23490 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
23491 && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
23492 return false;
23493 break;
23494 case DW_OP_GNU_implicit_pointer:
23495 case DW_OP_GNU_parameter_ref:
23496 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
23498 dw_die_ref ref
23499 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
23500 if (ref == NULL)
23501 return false;
23502 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23503 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
23504 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
23506 break;
23507 case DW_OP_GNU_const_type:
23508 case DW_OP_GNU_regval_type:
23509 case DW_OP_GNU_deref_type:
23510 case DW_OP_GNU_convert:
23511 case DW_OP_GNU_reinterpret:
23512 while (loc->dw_loc_next
23513 && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
23515 dw_die_ref base1, base2;
23516 unsigned enc1, enc2, size1, size2;
23517 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
23518 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
23519 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
23520 else if (loc->dw_loc_oprnd1.val_class
23521 == dw_val_class_unsigned_const)
23522 break;
23523 else
23524 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
23525 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
23526 == dw_val_class_unsigned_const)
23527 break;
23528 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
23529 gcc_assert (base1->die_tag == DW_TAG_base_type
23530 && base2->die_tag == DW_TAG_base_type);
23531 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
23532 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
23533 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
23534 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
23535 if (size1 == size2
23536 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
23537 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
23538 && loc != keep)
23539 || enc1 == enc2))
23541 /* Optimize away next DW_OP_GNU_convert after
23542 adjusting LOC's base type die reference. */
23543 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
23544 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
23545 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
23546 else
23547 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
23548 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
23549 continue;
23551 /* Don't change integer DW_OP_GNU_convert after e.g. floating
23552 point typed stack entry. */
23553 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
23554 keep = loc->dw_loc_next;
23555 break;
23557 break;
23558 default:
23559 break;
23561 return true;
23564 /* Helper function of resolve_addr. DIE had DW_AT_location of
23565 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
23566 and DW_OP_addr couldn't be resolved. resolve_addr has already
23567 removed the DW_AT_location attribute. This function attempts to
23568 add a new DW_AT_location attribute with DW_OP_GNU_implicit_pointer
23569 to it or DW_AT_const_value attribute, if possible. */
23571 static void
23572 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
23574 if (TREE_CODE (decl) != VAR_DECL
23575 || lookup_decl_die (decl) != die
23576 || DECL_EXTERNAL (decl)
23577 || !TREE_STATIC (decl)
23578 || DECL_INITIAL (decl) == NULL_TREE
23579 || DECL_P (DECL_INITIAL (decl))
23580 || get_AT (die, DW_AT_const_value))
23581 return;
23583 tree init = DECL_INITIAL (decl);
23584 HOST_WIDE_INT offset = 0;
23585 /* For variables that have been optimized away and thus
23586 don't have a memory location, see if we can emit
23587 DW_AT_const_value instead. */
23588 if (tree_add_const_value_attribute (die, init))
23589 return;
23590 if (dwarf_strict)
23591 return;
23592 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
23593 and ADDR_EXPR refers to a decl that has DW_AT_location or
23594 DW_AT_const_value (but isn't addressable, otherwise
23595 resolving the original DW_OP_addr wouldn't fail), see if
23596 we can add DW_OP_GNU_implicit_pointer. */
23597 STRIP_NOPS (init);
23598 if (TREE_CODE (init) == POINTER_PLUS_EXPR
23599 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
23601 offset = tree_to_shwi (TREE_OPERAND (init, 1));
23602 init = TREE_OPERAND (init, 0);
23603 STRIP_NOPS (init);
23605 if (TREE_CODE (init) != ADDR_EXPR)
23606 return;
23607 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
23608 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
23609 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
23610 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
23611 && TREE_OPERAND (init, 0) != decl))
23613 dw_die_ref ref;
23614 dw_loc_descr_ref l;
23616 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
23618 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
23619 if (!rtl)
23620 return;
23621 decl = SYMBOL_REF_DECL (rtl);
23623 else
23624 decl = TREE_OPERAND (init, 0);
23625 ref = lookup_decl_die (decl);
23626 if (ref == NULL
23627 || (!get_AT (ref, DW_AT_location)
23628 && !get_AT (ref, DW_AT_const_value)))
23629 return;
23630 l = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
23631 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23632 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
23633 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
23634 add_AT_loc (die, DW_AT_location, l);
23638 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
23639 an address in .rodata section if the string literal is emitted there,
23640 or remove the containing location list or replace DW_AT_const_value
23641 with DW_AT_location and empty location expression, if it isn't found
23642 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
23643 to something that has been emitted in the current CU. */
23645 static void
23646 resolve_addr (dw_die_ref die)
23648 dw_die_ref c;
23649 dw_attr_ref a;
23650 dw_loc_list_ref *curr, *start, loc;
23651 unsigned ix;
23653 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23654 switch (AT_class (a))
23656 case dw_val_class_loc_list:
23657 start = curr = AT_loc_list_ptr (a);
23658 loc = *curr;
23659 gcc_assert (loc);
23660 /* The same list can be referenced more than once. See if we have
23661 already recorded the result from a previous pass. */
23662 if (loc->replaced)
23663 *curr = loc->dw_loc_next;
23664 else if (!loc->resolved_addr)
23666 /* As things stand, we do not expect or allow one die to
23667 reference a suffix of another die's location list chain.
23668 References must be identical or completely separate.
23669 There is therefore no need to cache the result of this
23670 pass on any list other than the first; doing so
23671 would lead to unnecessary writes. */
23672 while (*curr)
23674 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
23675 if (!resolve_addr_in_expr ((*curr)->expr))
23677 dw_loc_list_ref next = (*curr)->dw_loc_next;
23678 dw_loc_descr_ref l = (*curr)->expr;
23680 if (next && (*curr)->ll_symbol)
23682 gcc_assert (!next->ll_symbol);
23683 next->ll_symbol = (*curr)->ll_symbol;
23685 if (dwarf_split_debug_info)
23686 remove_loc_list_addr_table_entries (l);
23687 *curr = next;
23689 else
23691 mark_base_types ((*curr)->expr);
23692 curr = &(*curr)->dw_loc_next;
23695 if (loc == *start)
23696 loc->resolved_addr = 1;
23697 else
23699 loc->replaced = 1;
23700 loc->dw_loc_next = *start;
23703 if (!*start)
23705 remove_AT (die, a->dw_attr);
23706 ix--;
23708 break;
23709 case dw_val_class_loc:
23711 dw_loc_descr_ref l = AT_loc (a);
23712 /* For -gdwarf-2 don't attempt to optimize
23713 DW_AT_data_member_location containing
23714 DW_OP_plus_uconst - older consumers might
23715 rely on it being that op instead of a more complex,
23716 but shorter, location description. */
23717 if ((dwarf_version > 2
23718 || a->dw_attr != DW_AT_data_member_location
23719 || l == NULL
23720 || l->dw_loc_opc != DW_OP_plus_uconst
23721 || l->dw_loc_next != NULL)
23722 && !resolve_addr_in_expr (l))
23724 if (dwarf_split_debug_info)
23725 remove_loc_list_addr_table_entries (l);
23726 if (l != NULL
23727 && l->dw_loc_next == NULL
23728 && l->dw_loc_opc == DW_OP_addr
23729 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
23730 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
23731 && a->dw_attr == DW_AT_location)
23733 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
23734 remove_AT (die, a->dw_attr);
23735 ix--;
23736 optimize_location_into_implicit_ptr (die, decl);
23737 break;
23739 remove_AT (die, a->dw_attr);
23740 ix--;
23742 else
23743 mark_base_types (l);
23745 break;
23746 case dw_val_class_addr:
23747 if (a->dw_attr == DW_AT_const_value
23748 && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
23750 if (AT_index (a) != NOT_INDEXED)
23751 remove_addr_table_entry (a->dw_attr_val.val_entry);
23752 remove_AT (die, a->dw_attr);
23753 ix--;
23755 if (die->die_tag == DW_TAG_GNU_call_site
23756 && a->dw_attr == DW_AT_abstract_origin)
23758 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
23759 dw_die_ref tdie = lookup_decl_die (tdecl);
23760 if (tdie == NULL
23761 && DECL_EXTERNAL (tdecl)
23762 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE)
23764 force_decl_die (tdecl);
23765 tdie = lookup_decl_die (tdecl);
23767 if (tdie)
23769 a->dw_attr_val.val_class = dw_val_class_die_ref;
23770 a->dw_attr_val.v.val_die_ref.die = tdie;
23771 a->dw_attr_val.v.val_die_ref.external = 0;
23773 else
23775 if (AT_index (a) != NOT_INDEXED)
23776 remove_addr_table_entry (a->dw_attr_val.val_entry);
23777 remove_AT (die, a->dw_attr);
23778 ix--;
23781 break;
23782 default:
23783 break;
23786 FOR_EACH_CHILD (die, c, resolve_addr (c));
23789 /* Helper routines for optimize_location_lists.
23790 This pass tries to share identical local lists in .debug_loc
23791 section. */
23793 /* Iteratively hash operands of LOC opcode into HSTATE. */
23795 static void
23796 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
23798 dw_val_ref val1 = &loc->dw_loc_oprnd1;
23799 dw_val_ref val2 = &loc->dw_loc_oprnd2;
23801 switch (loc->dw_loc_opc)
23803 case DW_OP_const4u:
23804 case DW_OP_const8u:
23805 if (loc->dtprel)
23806 goto hash_addr;
23807 /* FALLTHRU */
23808 case DW_OP_const1u:
23809 case DW_OP_const1s:
23810 case DW_OP_const2u:
23811 case DW_OP_const2s:
23812 case DW_OP_const4s:
23813 case DW_OP_const8s:
23814 case DW_OP_constu:
23815 case DW_OP_consts:
23816 case DW_OP_pick:
23817 case DW_OP_plus_uconst:
23818 case DW_OP_breg0:
23819 case DW_OP_breg1:
23820 case DW_OP_breg2:
23821 case DW_OP_breg3:
23822 case DW_OP_breg4:
23823 case DW_OP_breg5:
23824 case DW_OP_breg6:
23825 case DW_OP_breg7:
23826 case DW_OP_breg8:
23827 case DW_OP_breg9:
23828 case DW_OP_breg10:
23829 case DW_OP_breg11:
23830 case DW_OP_breg12:
23831 case DW_OP_breg13:
23832 case DW_OP_breg14:
23833 case DW_OP_breg15:
23834 case DW_OP_breg16:
23835 case DW_OP_breg17:
23836 case DW_OP_breg18:
23837 case DW_OP_breg19:
23838 case DW_OP_breg20:
23839 case DW_OP_breg21:
23840 case DW_OP_breg22:
23841 case DW_OP_breg23:
23842 case DW_OP_breg24:
23843 case DW_OP_breg25:
23844 case DW_OP_breg26:
23845 case DW_OP_breg27:
23846 case DW_OP_breg28:
23847 case DW_OP_breg29:
23848 case DW_OP_breg30:
23849 case DW_OP_breg31:
23850 case DW_OP_regx:
23851 case DW_OP_fbreg:
23852 case DW_OP_piece:
23853 case DW_OP_deref_size:
23854 case DW_OP_xderef_size:
23855 hstate.add_object (val1->v.val_int);
23856 break;
23857 case DW_OP_skip:
23858 case DW_OP_bra:
23860 int offset;
23862 gcc_assert (val1->val_class == dw_val_class_loc);
23863 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
23864 hstate.add_object (offset);
23866 break;
23867 case DW_OP_implicit_value:
23868 hstate.add_object (val1->v.val_unsigned);
23869 switch (val2->val_class)
23871 case dw_val_class_const:
23872 hstate.add_object (val2->v.val_int);
23873 break;
23874 case dw_val_class_vec:
23876 unsigned int elt_size = val2->v.val_vec.elt_size;
23877 unsigned int len = val2->v.val_vec.length;
23879 hstate.add_int (elt_size);
23880 hstate.add_int (len);
23881 hstate.add (val2->v.val_vec.array, len * elt_size);
23883 break;
23884 case dw_val_class_const_double:
23885 hstate.add_object (val2->v.val_double.low);
23886 hstate.add_object (val2->v.val_double.high);
23887 break;
23888 case dw_val_class_wide_int:
23889 hstate.add_object (*val2->v.val_wide);
23890 break;
23891 case dw_val_class_addr:
23892 inchash::add_rtx (val2->v.val_addr, hstate);
23893 break;
23894 default:
23895 gcc_unreachable ();
23897 break;
23898 case DW_OP_bregx:
23899 case DW_OP_bit_piece:
23900 hstate.add_object (val1->v.val_int);
23901 hstate.add_object (val2->v.val_int);
23902 break;
23903 case DW_OP_addr:
23904 hash_addr:
23905 if (loc->dtprel)
23907 unsigned char dtprel = 0xd1;
23908 hstate.add_object (dtprel);
23910 inchash::add_rtx (val1->v.val_addr, hstate);
23911 break;
23912 case DW_OP_GNU_addr_index:
23913 case DW_OP_GNU_const_index:
23915 if (loc->dtprel)
23917 unsigned char dtprel = 0xd1;
23918 hstate.add_object (dtprel);
23920 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
23922 break;
23923 case DW_OP_GNU_implicit_pointer:
23924 hstate.add_int (val2->v.val_int);
23925 break;
23926 case DW_OP_GNU_entry_value:
23927 hstate.add_object (val1->v.val_loc);
23928 break;
23929 case DW_OP_GNU_regval_type:
23930 case DW_OP_GNU_deref_type:
23932 unsigned int byte_size
23933 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
23934 unsigned int encoding
23935 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
23936 hstate.add_object (val1->v.val_int);
23937 hstate.add_object (byte_size);
23938 hstate.add_object (encoding);
23940 break;
23941 case DW_OP_GNU_convert:
23942 case DW_OP_GNU_reinterpret:
23943 if (val1->val_class == dw_val_class_unsigned_const)
23945 hstate.add_object (val1->v.val_unsigned);
23946 break;
23948 /* FALLTHRU */
23949 case DW_OP_GNU_const_type:
23951 unsigned int byte_size
23952 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
23953 unsigned int encoding
23954 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
23955 hstate.add_object (byte_size);
23956 hstate.add_object (encoding);
23957 if (loc->dw_loc_opc != DW_OP_GNU_const_type)
23958 break;
23959 hstate.add_object (val2->val_class);
23960 switch (val2->val_class)
23962 case dw_val_class_const:
23963 hstate.add_object (val2->v.val_int);
23964 break;
23965 case dw_val_class_vec:
23967 unsigned int elt_size = val2->v.val_vec.elt_size;
23968 unsigned int len = val2->v.val_vec.length;
23970 hstate.add_object (elt_size);
23971 hstate.add_object (len);
23972 hstate.add (val2->v.val_vec.array, len * elt_size);
23974 break;
23975 case dw_val_class_const_double:
23976 hstate.add_object (val2->v.val_double.low);
23977 hstate.add_object (val2->v.val_double.high);
23978 break;
23979 case dw_val_class_wide_int:
23980 hstate.add_object (*val2->v.val_wide);
23981 break;
23982 default:
23983 gcc_unreachable ();
23986 break;
23988 default:
23989 /* Other codes have no operands. */
23990 break;
23994 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
23996 static inline void
23997 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
23999 dw_loc_descr_ref l;
24000 bool sizes_computed = false;
24001 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
24002 size_of_locs (loc);
24004 for (l = loc; l != NULL; l = l->dw_loc_next)
24006 enum dwarf_location_atom opc = l->dw_loc_opc;
24007 hstate.add_object (opc);
24008 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
24010 size_of_locs (loc);
24011 sizes_computed = true;
24013 hash_loc_operands (l, hstate);
24017 /* Compute hash of the whole location list LIST_HEAD. */
24019 static inline void
24020 hash_loc_list (dw_loc_list_ref list_head)
24022 dw_loc_list_ref curr = list_head;
24023 inchash::hash hstate;
24025 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
24027 hstate.add (curr->begin, strlen (curr->begin) + 1);
24028 hstate.add (curr->end, strlen (curr->end) + 1);
24029 if (curr->section)
24030 hstate.add (curr->section, strlen (curr->section) + 1);
24031 hash_locs (curr->expr, hstate);
24033 list_head->hash = hstate.end ();
24036 /* Return true if X and Y opcodes have the same operands. */
24038 static inline bool
24039 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
24041 dw_val_ref valx1 = &x->dw_loc_oprnd1;
24042 dw_val_ref valx2 = &x->dw_loc_oprnd2;
24043 dw_val_ref valy1 = &y->dw_loc_oprnd1;
24044 dw_val_ref valy2 = &y->dw_loc_oprnd2;
24046 switch (x->dw_loc_opc)
24048 case DW_OP_const4u:
24049 case DW_OP_const8u:
24050 if (x->dtprel)
24051 goto hash_addr;
24052 /* FALLTHRU */
24053 case DW_OP_const1u:
24054 case DW_OP_const1s:
24055 case DW_OP_const2u:
24056 case DW_OP_const2s:
24057 case DW_OP_const4s:
24058 case DW_OP_const8s:
24059 case DW_OP_constu:
24060 case DW_OP_consts:
24061 case DW_OP_pick:
24062 case DW_OP_plus_uconst:
24063 case DW_OP_breg0:
24064 case DW_OP_breg1:
24065 case DW_OP_breg2:
24066 case DW_OP_breg3:
24067 case DW_OP_breg4:
24068 case DW_OP_breg5:
24069 case DW_OP_breg6:
24070 case DW_OP_breg7:
24071 case DW_OP_breg8:
24072 case DW_OP_breg9:
24073 case DW_OP_breg10:
24074 case DW_OP_breg11:
24075 case DW_OP_breg12:
24076 case DW_OP_breg13:
24077 case DW_OP_breg14:
24078 case DW_OP_breg15:
24079 case DW_OP_breg16:
24080 case DW_OP_breg17:
24081 case DW_OP_breg18:
24082 case DW_OP_breg19:
24083 case DW_OP_breg20:
24084 case DW_OP_breg21:
24085 case DW_OP_breg22:
24086 case DW_OP_breg23:
24087 case DW_OP_breg24:
24088 case DW_OP_breg25:
24089 case DW_OP_breg26:
24090 case DW_OP_breg27:
24091 case DW_OP_breg28:
24092 case DW_OP_breg29:
24093 case DW_OP_breg30:
24094 case DW_OP_breg31:
24095 case DW_OP_regx:
24096 case DW_OP_fbreg:
24097 case DW_OP_piece:
24098 case DW_OP_deref_size:
24099 case DW_OP_xderef_size:
24100 return valx1->v.val_int == valy1->v.val_int;
24101 case DW_OP_skip:
24102 case DW_OP_bra:
24103 /* If splitting debug info, the use of DW_OP_GNU_addr_index
24104 can cause irrelevant differences in dw_loc_addr. */
24105 gcc_assert (valx1->val_class == dw_val_class_loc
24106 && valy1->val_class == dw_val_class_loc
24107 && (dwarf_split_debug_info
24108 || x->dw_loc_addr == y->dw_loc_addr));
24109 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
24110 case DW_OP_implicit_value:
24111 if (valx1->v.val_unsigned != valy1->v.val_unsigned
24112 || valx2->val_class != valy2->val_class)
24113 return false;
24114 switch (valx2->val_class)
24116 case dw_val_class_const:
24117 return valx2->v.val_int == valy2->v.val_int;
24118 case dw_val_class_vec:
24119 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
24120 && valx2->v.val_vec.length == valy2->v.val_vec.length
24121 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
24122 valx2->v.val_vec.elt_size
24123 * valx2->v.val_vec.length) == 0;
24124 case dw_val_class_const_double:
24125 return valx2->v.val_double.low == valy2->v.val_double.low
24126 && valx2->v.val_double.high == valy2->v.val_double.high;
24127 case dw_val_class_wide_int:
24128 return *valx2->v.val_wide == *valy2->v.val_wide;
24129 case dw_val_class_addr:
24130 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
24131 default:
24132 gcc_unreachable ();
24134 case DW_OP_bregx:
24135 case DW_OP_bit_piece:
24136 return valx1->v.val_int == valy1->v.val_int
24137 && valx2->v.val_int == valy2->v.val_int;
24138 case DW_OP_addr:
24139 hash_addr:
24140 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
24141 case DW_OP_GNU_addr_index:
24142 case DW_OP_GNU_const_index:
24144 rtx ax1 = valx1->val_entry->addr.rtl;
24145 rtx ay1 = valy1->val_entry->addr.rtl;
24146 return rtx_equal_p (ax1, ay1);
24148 case DW_OP_GNU_implicit_pointer:
24149 return valx1->val_class == dw_val_class_die_ref
24150 && valx1->val_class == valy1->val_class
24151 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
24152 && valx2->v.val_int == valy2->v.val_int;
24153 case DW_OP_GNU_entry_value:
24154 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
24155 case DW_OP_GNU_const_type:
24156 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
24157 || valx2->val_class != valy2->val_class)
24158 return false;
24159 switch (valx2->val_class)
24161 case dw_val_class_const:
24162 return valx2->v.val_int == valy2->v.val_int;
24163 case dw_val_class_vec:
24164 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
24165 && valx2->v.val_vec.length == valy2->v.val_vec.length
24166 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
24167 valx2->v.val_vec.elt_size
24168 * valx2->v.val_vec.length) == 0;
24169 case dw_val_class_const_double:
24170 return valx2->v.val_double.low == valy2->v.val_double.low
24171 && valx2->v.val_double.high == valy2->v.val_double.high;
24172 case dw_val_class_wide_int:
24173 return *valx2->v.val_wide == *valy2->v.val_wide;
24174 default:
24175 gcc_unreachable ();
24177 case DW_OP_GNU_regval_type:
24178 case DW_OP_GNU_deref_type:
24179 return valx1->v.val_int == valy1->v.val_int
24180 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
24181 case DW_OP_GNU_convert:
24182 case DW_OP_GNU_reinterpret:
24183 if (valx1->val_class != valy1->val_class)
24184 return false;
24185 if (valx1->val_class == dw_val_class_unsigned_const)
24186 return valx1->v.val_unsigned == valy1->v.val_unsigned;
24187 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
24188 case DW_OP_GNU_parameter_ref:
24189 return valx1->val_class == dw_val_class_die_ref
24190 && valx1->val_class == valy1->val_class
24191 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
24192 default:
24193 /* Other codes have no operands. */
24194 return true;
24198 /* Return true if DWARF location expressions X and Y are the same. */
24200 static inline bool
24201 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
24203 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
24204 if (x->dw_loc_opc != y->dw_loc_opc
24205 || x->dtprel != y->dtprel
24206 || !compare_loc_operands (x, y))
24207 break;
24208 return x == NULL && y == NULL;
24211 /* Hashtable helpers. */
24213 struct loc_list_hasher : typed_noop_remove <dw_loc_list_struct>
24215 typedef dw_loc_list_struct value_type;
24216 typedef dw_loc_list_struct compare_type;
24217 static inline hashval_t hash (const value_type *);
24218 static inline bool equal (const value_type *, const compare_type *);
24221 /* Return precomputed hash of location list X. */
24223 inline hashval_t
24224 loc_list_hasher::hash (const value_type *x)
24226 return x->hash;
24229 /* Return true if location lists A and B are the same. */
24231 inline bool
24232 loc_list_hasher::equal (const value_type *a, const compare_type *b)
24234 if (a == b)
24235 return 1;
24236 if (a->hash != b->hash)
24237 return 0;
24238 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
24239 if (strcmp (a->begin, b->begin) != 0
24240 || strcmp (a->end, b->end) != 0
24241 || (a->section == NULL) != (b->section == NULL)
24242 || (a->section && strcmp (a->section, b->section) != 0)
24243 || !compare_locs (a->expr, b->expr))
24244 break;
24245 return a == NULL && b == NULL;
24248 typedef hash_table<loc_list_hasher> loc_list_hash_type;
24251 /* Recursively optimize location lists referenced from DIE
24252 children and share them whenever possible. */
24254 static void
24255 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
24257 dw_die_ref c;
24258 dw_attr_ref a;
24259 unsigned ix;
24260 dw_loc_list_struct **slot;
24262 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
24263 if (AT_class (a) == dw_val_class_loc_list)
24265 dw_loc_list_ref list = AT_loc_list (a);
24266 /* TODO: perform some optimizations here, before hashing
24267 it and storing into the hash table. */
24268 hash_loc_list (list);
24269 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
24270 if (*slot == NULL)
24271 *slot = list;
24272 else
24273 a->dw_attr_val.v.val_loc_list = *slot;
24276 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
24280 /* Recursively assign each location list a unique index into the debug_addr
24281 section. */
24283 static void
24284 index_location_lists (dw_die_ref die)
24286 dw_die_ref c;
24287 dw_attr_ref a;
24288 unsigned ix;
24290 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
24291 if (AT_class (a) == dw_val_class_loc_list)
24293 dw_loc_list_ref list = AT_loc_list (a);
24294 dw_loc_list_ref curr;
24295 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
24297 /* Don't index an entry that has already been indexed
24298 or won't be output. */
24299 if (curr->begin_entry != NULL
24300 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
24301 continue;
24303 curr->begin_entry
24304 = add_addr_table_entry (xstrdup (curr->begin),
24305 ate_kind_label);
24309 FOR_EACH_CHILD (die, c, index_location_lists (c));
24312 /* Optimize location lists referenced from DIE
24313 children and share them whenever possible. */
24315 static void
24316 optimize_location_lists (dw_die_ref die)
24318 loc_list_hash_type htab (500);
24319 optimize_location_lists_1 (die, &htab);
24322 /* Output stuff that dwarf requires at the end of every file,
24323 and generate the DWARF-2 debugging info. */
24325 static void
24326 dwarf2out_finish (const char *filename)
24328 limbo_die_node *node, *next_node;
24329 comdat_type_node *ctnode;
24330 unsigned int i;
24331 dw_die_ref main_comp_unit_die;
24333 /* PCH might result in DW_AT_producer string being restored from the
24334 header compilation, so always fill it with empty string initially
24335 and overwrite only here. */
24336 dw_attr_ref producer = get_AT (comp_unit_die (), DW_AT_producer);
24337 producer_string = gen_producer_string ();
24338 producer->dw_attr_val.v.val_str->refcount--;
24339 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
24341 gen_scheduled_generic_parms_dies ();
24342 gen_remaining_tmpl_value_param_die_attribute ();
24344 /* Add the name for the main input file now. We delayed this from
24345 dwarf2out_init to avoid complications with PCH. */
24346 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
24347 if (!IS_ABSOLUTE_PATH (filename) || targetm.force_at_comp_dir)
24348 add_comp_dir_attribute (comp_unit_die ());
24349 else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
24351 bool p = false;
24352 file_table->traverse<bool *, file_table_relative_p> (&p);
24353 if (p)
24354 add_comp_dir_attribute (comp_unit_die ());
24357 if (deferred_locations_list)
24358 for (i = 0; i < deferred_locations_list->length (); i++)
24360 add_location_or_const_value_attribute (
24361 (*deferred_locations_list)[i].die,
24362 (*deferred_locations_list)[i].variable,
24363 false,
24364 DW_AT_location);
24367 /* Traverse the limbo die list, and add parent/child links. The only
24368 dies without parents that should be here are concrete instances of
24369 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
24370 For concrete instances, we can get the parent die from the abstract
24371 instance. */
24372 for (node = limbo_die_list; node; node = next_node)
24374 dw_die_ref die = node->die;
24375 next_node = node->next;
24377 if (die->die_parent == NULL)
24379 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
24381 if (origin && origin->die_parent)
24382 add_child_die (origin->die_parent, die);
24383 else if (is_cu_die (die))
24385 else if (seen_error ())
24386 /* It's OK to be confused by errors in the input. */
24387 add_child_die (comp_unit_die (), die);
24388 else
24390 /* In certain situations, the lexical block containing a
24391 nested function can be optimized away, which results
24392 in the nested function die being orphaned. Likewise
24393 with the return type of that nested function. Force
24394 this to be a child of the containing function.
24396 It may happen that even the containing function got fully
24397 inlined and optimized out. In that case we are lost and
24398 assign the empty child. This should not be big issue as
24399 the function is likely unreachable too. */
24400 gcc_assert (node->created_for);
24402 if (DECL_P (node->created_for))
24403 origin = get_context_die (DECL_CONTEXT (node->created_for));
24404 else if (TYPE_P (node->created_for))
24405 origin = scope_die_for (node->created_for, comp_unit_die ());
24406 else
24407 origin = comp_unit_die ();
24409 add_child_die (origin, die);
24414 limbo_die_list = NULL;
24416 #if ENABLE_ASSERT_CHECKING
24418 dw_die_ref die = comp_unit_die (), c;
24419 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
24421 #endif
24422 resolve_addr (comp_unit_die ());
24423 move_marked_base_types ();
24425 for (node = deferred_asm_name; node; node = node->next)
24427 tree decl = node->created_for;
24428 /* When generating LTO bytecode we can not generate new assembler
24429 names at this point and all important decls got theirs via
24430 free-lang-data. */
24431 if (((!flag_generate_lto && !flag_generate_offload)
24432 || DECL_ASSEMBLER_NAME_SET_P (decl))
24433 && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
24435 add_linkage_attr (node->die, decl);
24436 move_linkage_attr (node->die);
24440 deferred_asm_name = NULL;
24442 /* Walk through the list of incomplete types again, trying once more to
24443 emit full debugging info for them. */
24444 retry_incomplete_types ();
24446 if (flag_eliminate_unused_debug_types)
24447 prune_unused_types ();
24449 /* Generate separate COMDAT sections for type DIEs. */
24450 if (use_debug_types)
24452 break_out_comdat_types (comp_unit_die ());
24454 /* Each new type_unit DIE was added to the limbo die list when created.
24455 Since these have all been added to comdat_type_list, clear the
24456 limbo die list. */
24457 limbo_die_list = NULL;
24459 /* For each new comdat type unit, copy declarations for incomplete
24460 types to make the new unit self-contained (i.e., no direct
24461 references to the main compile unit). */
24462 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24463 copy_decls_for_unworthy_types (ctnode->root_die);
24464 copy_decls_for_unworthy_types (comp_unit_die ());
24466 /* In the process of copying declarations from one unit to another,
24467 we may have left some declarations behind that are no longer
24468 referenced. Prune them. */
24469 prune_unused_types ();
24472 /* Generate separate CUs for each of the include files we've seen.
24473 They will go into limbo_die_list. */
24474 if (flag_eliminate_dwarf2_dups)
24475 break_out_includes (comp_unit_die ());
24477 /* Traverse the DIE's and add add sibling attributes to those DIE's
24478 that have children. */
24479 add_sibling_attributes (comp_unit_die ());
24480 for (node = limbo_die_list; node; node = node->next)
24481 add_sibling_attributes (node->die);
24482 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24483 add_sibling_attributes (ctnode->root_die);
24485 /* When splitting DWARF info, we put some attributes in the
24486 skeleton compile_unit DIE that remains in the .o, while
24487 most attributes go in the DWO compile_unit_die. */
24488 if (dwarf_split_debug_info)
24489 main_comp_unit_die = gen_compile_unit_die (NULL);
24490 else
24491 main_comp_unit_die = comp_unit_die ();
24493 /* Output a terminator label for the .text section. */
24494 switch_to_section (text_section);
24495 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
24496 if (cold_text_section)
24498 switch_to_section (cold_text_section);
24499 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
24502 /* We can only use the low/high_pc attributes if all of the code was
24503 in .text. */
24504 if (!have_multiple_function_sections
24505 || (dwarf_version < 3 && dwarf_strict))
24507 /* Don't add if the CU has no associated code. */
24508 if (text_section_used)
24509 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
24510 text_end_label, true);
24512 else
24514 unsigned fde_idx;
24515 dw_fde_ref fde;
24516 bool range_list_added = false;
24518 if (text_section_used)
24519 add_ranges_by_labels (main_comp_unit_die, text_section_label,
24520 text_end_label, &range_list_added, true);
24521 if (cold_text_section_used)
24522 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
24523 cold_end_label, &range_list_added, true);
24525 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
24527 if (DECL_IGNORED_P (fde->decl))
24528 continue;
24529 if (!fde->in_std_section)
24530 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
24531 fde->dw_fde_end, &range_list_added,
24532 true);
24533 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
24534 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
24535 fde->dw_fde_second_end, &range_list_added,
24536 true);
24539 if (range_list_added)
24541 /* We need to give .debug_loc and .debug_ranges an appropriate
24542 "base address". Use zero so that these addresses become
24543 absolute. Historically, we've emitted the unexpected
24544 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
24545 Emit both to give time for other tools to adapt. */
24546 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
24547 if (! dwarf_strict && dwarf_version < 4)
24548 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
24550 add_ranges (NULL);
24554 if (debug_info_level >= DINFO_LEVEL_TERSE)
24555 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
24556 debug_line_section_label);
24558 if (have_macinfo)
24559 add_AT_macptr (comp_unit_die (),
24560 dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
24561 macinfo_section_label);
24563 if (dwarf_split_debug_info)
24565 /* optimize_location_lists calculates the size of the lists,
24566 so index them first, and assign indices to the entries.
24567 Although optimize_location_lists will remove entries from
24568 the table, it only does so for duplicates, and therefore
24569 only reduces ref_counts to 1. */
24570 index_location_lists (comp_unit_die ());
24572 if (addr_index_table != NULL)
24574 unsigned int index = 0;
24575 addr_index_table
24576 ->traverse_noresize<unsigned int *, index_addr_table_entry>
24577 (&index);
24581 if (have_location_lists)
24582 optimize_location_lists (comp_unit_die ());
24584 save_macinfo_strings ();
24586 if (dwarf_split_debug_info)
24588 unsigned int index = 0;
24590 /* Add attributes common to skeleton compile_units and
24591 type_units. Because these attributes include strings, it
24592 must be done before freezing the string table. Top-level
24593 skeleton die attrs are added when the skeleton type unit is
24594 created, so ensure it is created by this point. */
24595 add_top_level_skeleton_die_attrs (main_comp_unit_die);
24596 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
24599 /* Output all of the compilation units. We put the main one last so that
24600 the offsets are available to output_pubnames. */
24601 for (node = limbo_die_list; node; node = node->next)
24602 output_comp_unit (node->die, 0);
24604 hash_table<comdat_type_hasher> comdat_type_table (100);
24605 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24607 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
24609 /* Don't output duplicate types. */
24610 if (*slot != HTAB_EMPTY_ENTRY)
24611 continue;
24613 /* Add a pointer to the line table for the main compilation unit
24614 so that the debugger can make sense of DW_AT_decl_file
24615 attributes. */
24616 if (debug_info_level >= DINFO_LEVEL_TERSE)
24617 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
24618 (!dwarf_split_debug_info
24619 ? debug_line_section_label
24620 : debug_skeleton_line_section_label));
24622 output_comdat_type_unit (ctnode);
24623 *slot = ctnode;
24626 /* The AT_pubnames attribute needs to go in all skeleton dies, including
24627 both the main_cu and all skeleton TUs. Making this call unconditional
24628 would end up either adding a second copy of the AT_pubnames attribute, or
24629 requiring a special case in add_top_level_skeleton_die_attrs. */
24630 if (!dwarf_split_debug_info)
24631 add_AT_pubnames (comp_unit_die ());
24633 if (dwarf_split_debug_info)
24635 int mark;
24636 unsigned char checksum[16];
24637 struct md5_ctx ctx;
24639 /* Compute a checksum of the comp_unit to use as the dwo_id. */
24640 md5_init_ctx (&ctx);
24641 mark = 0;
24642 die_checksum (comp_unit_die (), &ctx, &mark);
24643 unmark_all_dies (comp_unit_die ());
24644 md5_finish_ctx (&ctx, checksum);
24646 /* Use the first 8 bytes of the checksum as the dwo_id,
24647 and add it to both comp-unit DIEs. */
24648 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
24649 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
24651 /* Add the base offset of the ranges table to the skeleton
24652 comp-unit DIE. */
24653 if (ranges_table_in_use)
24654 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
24655 ranges_section_label);
24657 switch_to_section (debug_addr_section);
24658 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
24659 output_addr_table ();
24662 /* Output the main compilation unit if non-empty or if .debug_macinfo
24663 or .debug_macro will be emitted. */
24664 output_comp_unit (comp_unit_die (), have_macinfo);
24666 if (dwarf_split_debug_info && info_section_emitted)
24667 output_skeleton_debug_sections (main_comp_unit_die);
24669 /* Output the abbreviation table. */
24670 if (abbrev_die_table_in_use != 1)
24672 switch_to_section (debug_abbrev_section);
24673 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
24674 output_abbrev_section ();
24677 /* Output location list section if necessary. */
24678 if (have_location_lists)
24680 /* Output the location lists info. */
24681 switch_to_section (debug_loc_section);
24682 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
24683 output_location_lists (comp_unit_die ());
24686 output_pubtables ();
24688 /* Output the address range information if a CU (.debug_info section)
24689 was emitted. We output an empty table even if we had no functions
24690 to put in it. This because the consumer has no way to tell the
24691 difference between an empty table that we omitted and failure to
24692 generate a table that would have contained data. */
24693 if (info_section_emitted)
24695 unsigned long aranges_length = size_of_aranges ();
24697 switch_to_section (debug_aranges_section);
24698 output_aranges (aranges_length);
24701 /* Output ranges section if necessary. */
24702 if (ranges_table_in_use)
24704 switch_to_section (debug_ranges_section);
24705 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
24706 output_ranges ();
24709 /* Have to end the macro section. */
24710 if (have_macinfo)
24712 switch_to_section (debug_macinfo_section);
24713 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
24714 output_macinfo ();
24715 dw2_asm_output_data (1, 0, "End compilation unit");
24718 /* Output the source line correspondence table. We must do this
24719 even if there is no line information. Otherwise, on an empty
24720 translation unit, we will generate a present, but empty,
24721 .debug_info section. IRIX 6.5 `nm' will then complain when
24722 examining the file. This is done late so that any filenames
24723 used by the debug_info section are marked as 'used'. */
24724 switch_to_section (debug_line_section);
24725 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
24726 if (! DWARF2_ASM_LINE_DEBUG_INFO)
24727 output_line_info (false);
24729 if (dwarf_split_debug_info && info_section_emitted)
24731 switch_to_section (debug_skeleton_line_section);
24732 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
24733 output_line_info (true);
24736 /* If we emitted any indirect strings, output the string table too. */
24737 if (debug_str_hash || skeleton_debug_str_hash)
24738 output_indirect_strings ();
24741 /* Reset all state within dwarf2out.c so that we can rerun the compiler
24742 within the same process. For use by toplev::finalize. */
24744 void
24745 dwarf2out_c_finalize (void)
24747 last_var_location_insn = NULL;
24748 cached_next_real_insn = NULL;
24749 used_rtx_array = NULL;
24750 incomplete_types = NULL;
24751 decl_scope_table = NULL;
24752 debug_info_section = NULL;
24753 debug_skeleton_info_section = NULL;
24754 debug_abbrev_section = NULL;
24755 debug_skeleton_abbrev_section = NULL;
24756 debug_aranges_section = NULL;
24757 debug_addr_section = NULL;
24758 debug_macinfo_section = NULL;
24759 debug_line_section = NULL;
24760 debug_skeleton_line_section = NULL;
24761 debug_loc_section = NULL;
24762 debug_pubnames_section = NULL;
24763 debug_pubtypes_section = NULL;
24764 debug_str_section = NULL;
24765 debug_str_dwo_section = NULL;
24766 debug_str_offsets_section = NULL;
24767 debug_ranges_section = NULL;
24768 debug_frame_section = NULL;
24769 fde_vec = NULL;
24770 debug_str_hash = NULL;
24771 skeleton_debug_str_hash = NULL;
24772 dw2_string_counter = 0;
24773 have_multiple_function_sections = false;
24774 text_section_used = false;
24775 cold_text_section_used = false;
24776 cold_text_section = NULL;
24777 current_unit_personality = NULL;
24779 deferred_locations_list = NULL;
24781 next_die_offset = 0;
24782 single_comp_unit_die = NULL;
24783 comdat_type_list = NULL;
24784 limbo_die_list = NULL;
24785 deferred_asm_name = NULL;
24786 file_table = NULL;
24787 decl_die_table = NULL;
24788 common_block_die_table = NULL;
24789 decl_loc_table = NULL;
24790 call_arg_locations = NULL;
24791 call_arg_loc_last = NULL;
24792 call_site_count = -1;
24793 tail_call_site_count = -1;
24794 //block_map = NULL;
24795 cached_dw_loc_list_table = NULL;
24796 abbrev_die_table = NULL;
24797 abbrev_die_table_allocated = 0;
24798 abbrev_die_table_in_use = 0;
24799 line_info_label_num = 0;
24800 cur_line_info_table = NULL;
24801 text_section_line_info = NULL;
24802 cold_text_section_line_info = NULL;
24803 separate_line_info = NULL;
24804 info_section_emitted = false;
24805 pubname_table = NULL;
24806 pubtype_table = NULL;
24807 macinfo_table = NULL;
24808 ranges_table = NULL;
24809 ranges_table_allocated = 0;
24810 ranges_table_in_use = 0;
24811 ranges_by_label = 0;
24812 ranges_by_label_allocated = 0;
24813 ranges_by_label_in_use = 0;
24814 have_location_lists = false;
24815 loclabel_num = 0;
24816 poc_label_num = 0;
24817 last_emitted_file = NULL;
24818 label_num = 0;
24819 file_table_last_lookup = NULL;
24820 tmpl_value_parm_die_table = NULL;
24821 generic_type_instances = NULL;
24822 frame_pointer_fb_offset = 0;
24823 frame_pointer_fb_offset_valid = false;
24824 base_types.release ();
24825 XDELETEVEC (producer_string);
24826 producer_string = NULL;
24829 #include "gt-dwarf2out.h"