2015-06-25 Zhouyi Zhou <yizhouzhou@ict.ac.cn>
[official-gcc.git] / gcc / dwarf2out.c
blob843fc1e85fb58a93b13934f31db022b043676c73
1 /* Output Dwarf2 format symbol table information from GCC.
2 Copyright (C) 1992-2015 Free Software Foundation, Inc.
3 Contributed by Gary Funck (gary@intrepid.com).
4 Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
5 Extensively modified by Jason Merrill (jason@cygnus.com).
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 /* TODO: Emit .debug_line header even when there are no functions, since
24 the file numbers are used by .debug_info. Alternately, leave
25 out locations for types and decls.
26 Avoid talking about ctors and op= for PODs.
27 Factor out common prologue sequences into multiple CIEs. */
29 /* The first part of this file deals with the DWARF 2 frame unwind
30 information, which is also used by the GCC efficient exception handling
31 mechanism. The second part, controlled only by an #ifdef
32 DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
33 information. */
35 /* DWARF2 Abbreviation Glossary:
37 CFA = Canonical Frame Address
38 a fixed address on the stack which identifies a call frame.
39 We define it to be the value of SP just before the call insn.
40 The CFA register and offset, which may change during the course
41 of the function, are used to calculate its value at runtime.
43 CFI = Call Frame Instruction
44 an instruction for the DWARF2 abstract machine
46 CIE = Common Information Entry
47 information describing information common to one or more FDEs
49 DIE = Debugging Information Entry
51 FDE = Frame Description Entry
52 information describing the stack call frame, in particular,
53 how to restore registers
55 DW_CFA_... = DWARF2 CFA call frame instruction
56 DW_TAG_... = DWARF2 DIE tag */
58 #include "config.h"
59 #include "system.h"
60 #include "coretypes.h"
61 #include "tm.h"
62 #include "rtl.h"
63 #include "alias.h"
64 #include "symtab.h"
65 #include "tree.h"
66 #include "fold-const.h"
67 #include "stringpool.h"
68 #include "stor-layout.h"
69 #include "varasm.h"
70 #include "hard-reg-set.h"
71 #include "function.h"
72 #include "emit-rtl.h"
73 #include "version.h"
74 #include "flags.h"
75 #include "regs.h"
76 #include "rtlhash.h"
77 #include "insn-config.h"
78 #include "reload.h"
79 #include "output.h"
80 #include "expmed.h"
81 #include "dojump.h"
82 #include "explow.h"
83 #include "calls.h"
84 #include "stmt.h"
85 #include "expr.h"
86 #include "except.h"
87 #include "dwarf2.h"
88 #include "dwarf2out.h"
89 #include "dwarf2asm.h"
90 #include "toplev.h"
91 #include "md5.h"
92 #include "tm_p.h"
93 #include "diagnostic.h"
94 #include "tree-pretty-print.h"
95 #include "debug.h"
96 #include "target.h"
97 #include "common/common-target.h"
98 #include "langhooks.h"
99 #include "plugin-api.h"
100 #include "ipa-ref.h"
101 #include "cgraph.h"
102 #include "ira.h"
103 #include "lra.h"
104 #include "dumpfile.h"
105 #include "opts.h"
106 #include "tree-dfa.h"
107 #include "gdb/gdb-index.h"
108 #include "rtl-iter.h"
110 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
111 static rtx_insn *last_var_location_insn;
112 static rtx_insn *cached_next_real_insn;
113 static void dwarf2out_decl (tree);
115 #ifdef VMS_DEBUGGING_INFO
116 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
118 /* Define this macro to be a nonzero value if the directory specifications
119 which are output in the debug info should end with a separator. */
120 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
121 /* Define this macro to evaluate to a nonzero value if GCC should refrain
122 from generating indirect strings in DWARF2 debug information, for instance
123 if your target is stuck with an old version of GDB that is unable to
124 process them properly or uses VMS Debug. */
125 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
126 #else
127 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
128 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
129 #endif
131 /* ??? Poison these here until it can be done generically. They've been
132 totally replaced in this file; make sure it stays that way. */
133 #undef DWARF2_UNWIND_INFO
134 #undef DWARF2_FRAME_INFO
135 #if (GCC_VERSION >= 3000)
136 #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
137 #endif
139 /* The size of the target's pointer type. */
140 #ifndef PTR_SIZE
141 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
142 #endif
144 /* Array of RTXes referenced by the debugging information, which therefore
145 must be kept around forever. */
146 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
148 /* A pointer to the base of a list of incomplete types which might be
149 completed at some later time. incomplete_types_list needs to be a
150 vec<tree, va_gc> *because we want to tell the garbage collector about
151 it. */
152 static GTY(()) vec<tree, va_gc> *incomplete_types;
154 /* A pointer to the base of a table of references to declaration
155 scopes. This table is a display which tracks the nesting
156 of declaration scopes at the current scope and containing
157 scopes. This table is used to find the proper place to
158 define type declaration DIE's. */
159 static GTY(()) vec<tree, va_gc> *decl_scope_table;
161 /* Pointers to various DWARF2 sections. */
162 static GTY(()) section *debug_info_section;
163 static GTY(()) section *debug_skeleton_info_section;
164 static GTY(()) section *debug_abbrev_section;
165 static GTY(()) section *debug_skeleton_abbrev_section;
166 static GTY(()) section *debug_aranges_section;
167 static GTY(()) section *debug_addr_section;
168 static GTY(()) section *debug_macinfo_section;
169 static GTY(()) section *debug_line_section;
170 static GTY(()) section *debug_skeleton_line_section;
171 static GTY(()) section *debug_loc_section;
172 static GTY(()) section *debug_pubnames_section;
173 static GTY(()) section *debug_pubtypes_section;
174 static GTY(()) section *debug_str_section;
175 static GTY(()) section *debug_str_dwo_section;
176 static GTY(()) section *debug_str_offsets_section;
177 static GTY(()) section *debug_ranges_section;
178 static GTY(()) section *debug_frame_section;
180 /* Maximum size (in bytes) of an artificially generated label. */
181 #define MAX_ARTIFICIAL_LABEL_BYTES 30
183 /* According to the (draft) DWARF 3 specification, the initial length
184 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
185 bytes are 0xffffffff, followed by the length stored in the next 8
186 bytes.
188 However, the SGI/MIPS ABI uses an initial length which is equal to
189 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
191 #ifndef DWARF_INITIAL_LENGTH_SIZE
192 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
193 #endif
195 /* Round SIZE up to the nearest BOUNDARY. */
196 #define DWARF_ROUND(SIZE,BOUNDARY) \
197 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
199 /* CIE identifier. */
200 #if HOST_BITS_PER_WIDE_INT >= 64
201 #define DWARF_CIE_ID \
202 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
203 #else
204 #define DWARF_CIE_ID DW_CIE_ID
205 #endif
208 /* A vector for a table that contains frame description
209 information for each routine. */
210 #define NOT_INDEXED (-1U)
211 #define NO_INDEX_ASSIGNED (-2U)
213 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
215 struct GTY((for_user)) indirect_string_node {
216 const char *str;
217 unsigned int refcount;
218 enum dwarf_form form;
219 char *label;
220 unsigned int index;
223 struct indirect_string_hasher : ggc_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 /* Describe an entry into the .debug_addr section. */
1259 enum ate_kind {
1260 ate_kind_rtx,
1261 ate_kind_rtx_dtprel,
1262 ate_kind_label
1265 typedef struct GTY((for_user)) addr_table_entry_struct {
1266 enum ate_kind kind;
1267 unsigned int refcount;
1268 unsigned int index;
1269 union addr_table_entry_struct_union
1271 rtx GTY ((tag ("0"))) rtl;
1272 char * GTY ((tag ("1"))) label;
1274 GTY ((desc ("%1.kind"))) addr;
1276 addr_table_entry;
1278 /* Location lists are ranges + location descriptions for that range,
1279 so you can track variables that are in different places over
1280 their entire life. */
1281 typedef struct GTY(()) dw_loc_list_struct {
1282 dw_loc_list_ref dw_loc_next;
1283 const char *begin; /* Label and addr_entry for start of range */
1284 addr_table_entry *begin_entry;
1285 const char *end; /* Label for end of range */
1286 char *ll_symbol; /* Label for beginning of location list.
1287 Only on head of list */
1288 const char *section; /* Section this loclist is relative to */
1289 dw_loc_descr_ref expr;
1290 hashval_t hash;
1291 /* True if all addresses in this and subsequent lists are known to be
1292 resolved. */
1293 bool resolved_addr;
1294 /* True if this list has been replaced by dw_loc_next. */
1295 bool replaced;
1296 bool emitted;
1297 /* True if the range should be emitted even if begin and end
1298 are the same. */
1299 bool force;
1300 } dw_loc_list_node;
1302 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1304 /* Convert a DWARF stack opcode into its string name. */
1306 static const char *
1307 dwarf_stack_op_name (unsigned int op)
1309 const char *name = get_DW_OP_name (op);
1311 if (name != NULL)
1312 return name;
1314 return "OP_<unknown>";
1317 /* Return a pointer to a newly allocated location description. Location
1318 descriptions are simple expression terms that can be strung
1319 together to form more complicated location (address) descriptions. */
1321 static inline dw_loc_descr_ref
1322 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1323 unsigned HOST_WIDE_INT oprnd2)
1325 dw_loc_descr_ref descr = ggc_cleared_alloc<dw_loc_descr_node> ();
1327 descr->dw_loc_opc = op;
1328 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1329 descr->dw_loc_oprnd1.val_entry = NULL;
1330 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1331 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1332 descr->dw_loc_oprnd2.val_entry = NULL;
1333 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1335 return descr;
1338 /* Return a pointer to a newly allocated location description for
1339 REG and OFFSET. */
1341 static inline dw_loc_descr_ref
1342 new_reg_loc_descr (unsigned int reg, unsigned HOST_WIDE_INT offset)
1344 if (reg <= 31)
1345 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1346 offset, 0);
1347 else
1348 return new_loc_descr (DW_OP_bregx, reg, offset);
1351 /* Add a location description term to a location description expression. */
1353 static inline void
1354 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1356 dw_loc_descr_ref *d;
1358 /* Find the end of the chain. */
1359 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1362 *d = descr;
1365 /* Compare two location operands for exact equality. */
1367 static bool
1368 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1370 if (a->val_class != b->val_class)
1371 return false;
1372 switch (a->val_class)
1374 case dw_val_class_none:
1375 return true;
1376 case dw_val_class_addr:
1377 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1379 case dw_val_class_offset:
1380 case dw_val_class_unsigned_const:
1381 case dw_val_class_const:
1382 case dw_val_class_range_list:
1383 case dw_val_class_lineptr:
1384 case dw_val_class_macptr:
1385 /* These are all HOST_WIDE_INT, signed or unsigned. */
1386 return a->v.val_unsigned == b->v.val_unsigned;
1388 case dw_val_class_loc:
1389 return a->v.val_loc == b->v.val_loc;
1390 case dw_val_class_loc_list:
1391 return a->v.val_loc_list == b->v.val_loc_list;
1392 case dw_val_class_die_ref:
1393 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1394 case dw_val_class_fde_ref:
1395 return a->v.val_fde_index == b->v.val_fde_index;
1396 case dw_val_class_lbl_id:
1397 case dw_val_class_high_pc:
1398 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1399 case dw_val_class_str:
1400 return a->v.val_str == b->v.val_str;
1401 case dw_val_class_flag:
1402 return a->v.val_flag == b->v.val_flag;
1403 case dw_val_class_file:
1404 return a->v.val_file == b->v.val_file;
1405 case dw_val_class_decl_ref:
1406 return a->v.val_decl_ref == b->v.val_decl_ref;
1408 case dw_val_class_const_double:
1409 return (a->v.val_double.high == b->v.val_double.high
1410 && a->v.val_double.low == b->v.val_double.low);
1412 case dw_val_class_wide_int:
1413 return *a->v.val_wide == *b->v.val_wide;
1415 case dw_val_class_vec:
1417 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1418 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1420 return (a_len == b_len
1421 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1424 case dw_val_class_data8:
1425 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1427 case dw_val_class_vms_delta:
1428 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1429 && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1431 gcc_unreachable ();
1434 /* Compare two location atoms for exact equality. */
1436 static bool
1437 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1439 if (a->dw_loc_opc != b->dw_loc_opc)
1440 return false;
1442 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1443 address size, but since we always allocate cleared storage it
1444 should be zero for other types of locations. */
1445 if (a->dtprel != b->dtprel)
1446 return false;
1448 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1449 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1452 /* Compare two complete location expressions for exact equality. */
1454 bool
1455 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1457 while (1)
1459 if (a == b)
1460 return true;
1461 if (a == NULL || b == NULL)
1462 return false;
1463 if (!loc_descr_equal_p_1 (a, b))
1464 return false;
1466 a = a->dw_loc_next;
1467 b = b->dw_loc_next;
1472 /* Add a constant OFFSET to a location expression. */
1474 static void
1475 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1477 dw_loc_descr_ref loc;
1478 HOST_WIDE_INT *p;
1480 gcc_assert (*list_head != NULL);
1482 if (!offset)
1483 return;
1485 /* Find the end of the chain. */
1486 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1489 p = NULL;
1490 if (loc->dw_loc_opc == DW_OP_fbreg
1491 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1492 p = &loc->dw_loc_oprnd1.v.val_int;
1493 else if (loc->dw_loc_opc == DW_OP_bregx)
1494 p = &loc->dw_loc_oprnd2.v.val_int;
1496 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1497 offset. Don't optimize if an signed integer overflow would happen. */
1498 if (p != NULL
1499 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1500 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1501 *p += offset;
1503 else if (offset > 0)
1504 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1506 else
1508 loc->dw_loc_next = int_loc_descriptor (-offset);
1509 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1513 /* Add a constant OFFSET to a location list. */
1515 static void
1516 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1518 dw_loc_list_ref d;
1519 for (d = list_head; d != NULL; d = d->dw_loc_next)
1520 loc_descr_plus_const (&d->expr, offset);
1523 #define DWARF_REF_SIZE \
1524 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1526 static unsigned long int get_base_type_offset (dw_die_ref);
1528 /* Return the size of a location descriptor. */
1530 static unsigned long
1531 size_of_loc_descr (dw_loc_descr_ref loc)
1533 unsigned long size = 1;
1535 switch (loc->dw_loc_opc)
1537 case DW_OP_addr:
1538 size += DWARF2_ADDR_SIZE;
1539 break;
1540 case DW_OP_GNU_addr_index:
1541 case DW_OP_GNU_const_index:
1542 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1543 size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1544 break;
1545 case DW_OP_const1u:
1546 case DW_OP_const1s:
1547 size += 1;
1548 break;
1549 case DW_OP_const2u:
1550 case DW_OP_const2s:
1551 size += 2;
1552 break;
1553 case DW_OP_const4u:
1554 case DW_OP_const4s:
1555 size += 4;
1556 break;
1557 case DW_OP_const8u:
1558 case DW_OP_const8s:
1559 size += 8;
1560 break;
1561 case DW_OP_constu:
1562 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1563 break;
1564 case DW_OP_consts:
1565 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1566 break;
1567 case DW_OP_pick:
1568 size += 1;
1569 break;
1570 case DW_OP_plus_uconst:
1571 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1572 break;
1573 case DW_OP_skip:
1574 case DW_OP_bra:
1575 size += 2;
1576 break;
1577 case DW_OP_breg0:
1578 case DW_OP_breg1:
1579 case DW_OP_breg2:
1580 case DW_OP_breg3:
1581 case DW_OP_breg4:
1582 case DW_OP_breg5:
1583 case DW_OP_breg6:
1584 case DW_OP_breg7:
1585 case DW_OP_breg8:
1586 case DW_OP_breg9:
1587 case DW_OP_breg10:
1588 case DW_OP_breg11:
1589 case DW_OP_breg12:
1590 case DW_OP_breg13:
1591 case DW_OP_breg14:
1592 case DW_OP_breg15:
1593 case DW_OP_breg16:
1594 case DW_OP_breg17:
1595 case DW_OP_breg18:
1596 case DW_OP_breg19:
1597 case DW_OP_breg20:
1598 case DW_OP_breg21:
1599 case DW_OP_breg22:
1600 case DW_OP_breg23:
1601 case DW_OP_breg24:
1602 case DW_OP_breg25:
1603 case DW_OP_breg26:
1604 case DW_OP_breg27:
1605 case DW_OP_breg28:
1606 case DW_OP_breg29:
1607 case DW_OP_breg30:
1608 case DW_OP_breg31:
1609 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1610 break;
1611 case DW_OP_regx:
1612 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1613 break;
1614 case DW_OP_fbreg:
1615 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1616 break;
1617 case DW_OP_bregx:
1618 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1619 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1620 break;
1621 case DW_OP_piece:
1622 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1623 break;
1624 case DW_OP_bit_piece:
1625 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1626 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1627 break;
1628 case DW_OP_deref_size:
1629 case DW_OP_xderef_size:
1630 size += 1;
1631 break;
1632 case DW_OP_call2:
1633 size += 2;
1634 break;
1635 case DW_OP_call4:
1636 size += 4;
1637 break;
1638 case DW_OP_call_ref:
1639 size += DWARF_REF_SIZE;
1640 break;
1641 case DW_OP_implicit_value:
1642 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1643 + loc->dw_loc_oprnd1.v.val_unsigned;
1644 break;
1645 case DW_OP_GNU_implicit_pointer:
1646 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1647 break;
1648 case DW_OP_GNU_entry_value:
1650 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1651 size += size_of_uleb128 (op_size) + op_size;
1652 break;
1654 case DW_OP_GNU_const_type:
1656 unsigned long o
1657 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1658 size += size_of_uleb128 (o) + 1;
1659 switch (loc->dw_loc_oprnd2.val_class)
1661 case dw_val_class_vec:
1662 size += loc->dw_loc_oprnd2.v.val_vec.length
1663 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1664 break;
1665 case dw_val_class_const:
1666 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1667 break;
1668 case dw_val_class_const_double:
1669 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1670 break;
1671 case dw_val_class_wide_int:
1672 size += (get_full_len (*loc->dw_loc_oprnd2.v.val_wide)
1673 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
1674 break;
1675 default:
1676 gcc_unreachable ();
1678 break;
1680 case DW_OP_GNU_regval_type:
1682 unsigned long o
1683 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1684 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1685 + size_of_uleb128 (o);
1687 break;
1688 case DW_OP_GNU_deref_type:
1690 unsigned long o
1691 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1692 size += 1 + size_of_uleb128 (o);
1694 break;
1695 case DW_OP_GNU_convert:
1696 case DW_OP_GNU_reinterpret:
1697 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1698 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1699 else
1701 unsigned long o
1702 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1703 size += size_of_uleb128 (o);
1705 break;
1706 case DW_OP_GNU_parameter_ref:
1707 size += 4;
1708 break;
1709 default:
1710 break;
1713 return size;
1716 /* Return the size of a series of location descriptors. */
1718 unsigned long
1719 size_of_locs (dw_loc_descr_ref loc)
1721 dw_loc_descr_ref l;
1722 unsigned long size;
1724 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1725 field, to avoid writing to a PCH file. */
1726 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1728 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1729 break;
1730 size += size_of_loc_descr (l);
1732 if (! l)
1733 return size;
1735 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1737 l->dw_loc_addr = size;
1738 size += size_of_loc_descr (l);
1741 return size;
1744 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1745 static void get_ref_die_offset_label (char *, dw_die_ref);
1746 static unsigned long int get_ref_die_offset (dw_die_ref);
1748 /* Output location description stack opcode's operands (if any).
1749 The for_eh_or_skip parameter controls whether register numbers are
1750 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1751 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1752 info). This should be suppressed for the cases that have not been converted
1753 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
1755 static void
1756 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1758 dw_val_ref val1 = &loc->dw_loc_oprnd1;
1759 dw_val_ref val2 = &loc->dw_loc_oprnd2;
1761 switch (loc->dw_loc_opc)
1763 #ifdef DWARF2_DEBUGGING_INFO
1764 case DW_OP_const2u:
1765 case DW_OP_const2s:
1766 dw2_asm_output_data (2, val1->v.val_int, NULL);
1767 break;
1768 case DW_OP_const4u:
1769 if (loc->dtprel)
1771 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1772 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1773 val1->v.val_addr);
1774 fputc ('\n', asm_out_file);
1775 break;
1777 /* FALLTHRU */
1778 case DW_OP_const4s:
1779 dw2_asm_output_data (4, val1->v.val_int, NULL);
1780 break;
1781 case DW_OP_const8u:
1782 if (loc->dtprel)
1784 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1785 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1786 val1->v.val_addr);
1787 fputc ('\n', asm_out_file);
1788 break;
1790 /* FALLTHRU */
1791 case DW_OP_const8s:
1792 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
1793 dw2_asm_output_data (8, val1->v.val_int, NULL);
1794 break;
1795 case DW_OP_skip:
1796 case DW_OP_bra:
1798 int offset;
1800 gcc_assert (val1->val_class == dw_val_class_loc);
1801 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
1803 dw2_asm_output_data (2, offset, NULL);
1805 break;
1806 case DW_OP_implicit_value:
1807 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1808 switch (val2->val_class)
1810 case dw_val_class_const:
1811 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
1812 break;
1813 case dw_val_class_vec:
1815 unsigned int elt_size = val2->v.val_vec.elt_size;
1816 unsigned int len = val2->v.val_vec.length;
1817 unsigned int i;
1818 unsigned char *p;
1820 if (elt_size > sizeof (HOST_WIDE_INT))
1822 elt_size /= 2;
1823 len *= 2;
1825 for (i = 0, p = val2->v.val_vec.array;
1826 i < len;
1827 i++, p += elt_size)
1828 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
1829 "fp or vector constant word %u", i);
1831 break;
1832 case dw_val_class_const_double:
1834 unsigned HOST_WIDE_INT first, second;
1836 if (WORDS_BIG_ENDIAN)
1838 first = val2->v.val_double.high;
1839 second = val2->v.val_double.low;
1841 else
1843 first = val2->v.val_double.low;
1844 second = val2->v.val_double.high;
1846 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1847 first, NULL);
1848 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1849 second, NULL);
1851 break;
1852 case dw_val_class_wide_int:
1854 int i;
1855 int len = get_full_len (*val2->v.val_wide);
1856 if (WORDS_BIG_ENDIAN)
1857 for (i = len - 1; i >= 0; --i)
1858 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1859 val2->v.val_wide->elt (i), NULL);
1860 else
1861 for (i = 0; i < len; ++i)
1862 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1863 val2->v.val_wide->elt (i), NULL);
1865 break;
1866 case dw_val_class_addr:
1867 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
1868 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
1869 break;
1870 default:
1871 gcc_unreachable ();
1873 break;
1874 #else
1875 case DW_OP_const2u:
1876 case DW_OP_const2s:
1877 case DW_OP_const4u:
1878 case DW_OP_const4s:
1879 case DW_OP_const8u:
1880 case DW_OP_const8s:
1881 case DW_OP_skip:
1882 case DW_OP_bra:
1883 case DW_OP_implicit_value:
1884 /* We currently don't make any attempt to make sure these are
1885 aligned properly like we do for the main unwind info, so
1886 don't support emitting things larger than a byte if we're
1887 only doing unwinding. */
1888 gcc_unreachable ();
1889 #endif
1890 case DW_OP_const1u:
1891 case DW_OP_const1s:
1892 dw2_asm_output_data (1, val1->v.val_int, NULL);
1893 break;
1894 case DW_OP_constu:
1895 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1896 break;
1897 case DW_OP_consts:
1898 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1899 break;
1900 case DW_OP_pick:
1901 dw2_asm_output_data (1, val1->v.val_int, NULL);
1902 break;
1903 case DW_OP_plus_uconst:
1904 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1905 break;
1906 case DW_OP_breg0:
1907 case DW_OP_breg1:
1908 case DW_OP_breg2:
1909 case DW_OP_breg3:
1910 case DW_OP_breg4:
1911 case DW_OP_breg5:
1912 case DW_OP_breg6:
1913 case DW_OP_breg7:
1914 case DW_OP_breg8:
1915 case DW_OP_breg9:
1916 case DW_OP_breg10:
1917 case DW_OP_breg11:
1918 case DW_OP_breg12:
1919 case DW_OP_breg13:
1920 case DW_OP_breg14:
1921 case DW_OP_breg15:
1922 case DW_OP_breg16:
1923 case DW_OP_breg17:
1924 case DW_OP_breg18:
1925 case DW_OP_breg19:
1926 case DW_OP_breg20:
1927 case DW_OP_breg21:
1928 case DW_OP_breg22:
1929 case DW_OP_breg23:
1930 case DW_OP_breg24:
1931 case DW_OP_breg25:
1932 case DW_OP_breg26:
1933 case DW_OP_breg27:
1934 case DW_OP_breg28:
1935 case DW_OP_breg29:
1936 case DW_OP_breg30:
1937 case DW_OP_breg31:
1938 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1939 break;
1940 case DW_OP_regx:
1942 unsigned r = val1->v.val_unsigned;
1943 if (for_eh_or_skip >= 0)
1944 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1945 gcc_assert (size_of_uleb128 (r)
1946 == size_of_uleb128 (val1->v.val_unsigned));
1947 dw2_asm_output_data_uleb128 (r, NULL);
1949 break;
1950 case DW_OP_fbreg:
1951 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1952 break;
1953 case DW_OP_bregx:
1955 unsigned r = val1->v.val_unsigned;
1956 if (for_eh_or_skip >= 0)
1957 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1958 gcc_assert (size_of_uleb128 (r)
1959 == size_of_uleb128 (val1->v.val_unsigned));
1960 dw2_asm_output_data_uleb128 (r, NULL);
1961 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
1963 break;
1964 case DW_OP_piece:
1965 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1966 break;
1967 case DW_OP_bit_piece:
1968 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1969 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
1970 break;
1971 case DW_OP_deref_size:
1972 case DW_OP_xderef_size:
1973 dw2_asm_output_data (1, val1->v.val_int, NULL);
1974 break;
1976 case DW_OP_addr:
1977 if (loc->dtprel)
1979 if (targetm.asm_out.output_dwarf_dtprel)
1981 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
1982 DWARF2_ADDR_SIZE,
1983 val1->v.val_addr);
1984 fputc ('\n', asm_out_file);
1986 else
1987 gcc_unreachable ();
1989 else
1991 #ifdef DWARF2_DEBUGGING_INFO
1992 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
1993 #else
1994 gcc_unreachable ();
1995 #endif
1997 break;
1999 case DW_OP_GNU_addr_index:
2000 case DW_OP_GNU_const_index:
2001 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
2002 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
2003 "(index into .debug_addr)");
2004 break;
2006 case DW_OP_GNU_implicit_pointer:
2008 char label[MAX_ARTIFICIAL_LABEL_BYTES
2009 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2010 gcc_assert (val1->val_class == dw_val_class_die_ref);
2011 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2012 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2013 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2015 break;
2017 case DW_OP_GNU_entry_value:
2018 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2019 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2020 break;
2022 case DW_OP_GNU_const_type:
2024 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2025 gcc_assert (o);
2026 dw2_asm_output_data_uleb128 (o, NULL);
2027 switch (val2->val_class)
2029 case dw_val_class_const:
2030 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2031 dw2_asm_output_data (1, l, NULL);
2032 dw2_asm_output_data (l, val2->v.val_int, NULL);
2033 break;
2034 case dw_val_class_vec:
2036 unsigned int elt_size = val2->v.val_vec.elt_size;
2037 unsigned int len = val2->v.val_vec.length;
2038 unsigned int i;
2039 unsigned char *p;
2041 l = len * elt_size;
2042 dw2_asm_output_data (1, l, NULL);
2043 if (elt_size > sizeof (HOST_WIDE_INT))
2045 elt_size /= 2;
2046 len *= 2;
2048 for (i = 0, p = val2->v.val_vec.array;
2049 i < len;
2050 i++, p += elt_size)
2051 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2052 "fp or vector constant word %u", i);
2054 break;
2055 case dw_val_class_const_double:
2057 unsigned HOST_WIDE_INT first, second;
2058 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2060 dw2_asm_output_data (1, 2 * l, NULL);
2061 if (WORDS_BIG_ENDIAN)
2063 first = val2->v.val_double.high;
2064 second = val2->v.val_double.low;
2066 else
2068 first = val2->v.val_double.low;
2069 second = val2->v.val_double.high;
2071 dw2_asm_output_data (l, first, NULL);
2072 dw2_asm_output_data (l, second, NULL);
2074 break;
2075 case dw_val_class_wide_int:
2077 int i;
2078 int len = get_full_len (*val2->v.val_wide);
2079 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2081 dw2_asm_output_data (1, len * l, NULL);
2082 if (WORDS_BIG_ENDIAN)
2083 for (i = len - 1; i >= 0; --i)
2084 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2085 else
2086 for (i = 0; i < len; ++i)
2087 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2089 break;
2090 default:
2091 gcc_unreachable ();
2094 break;
2095 case DW_OP_GNU_regval_type:
2097 unsigned r = val1->v.val_unsigned;
2098 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2099 gcc_assert (o);
2100 if (for_eh_or_skip >= 0)
2102 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2103 gcc_assert (size_of_uleb128 (r)
2104 == size_of_uleb128 (val1->v.val_unsigned));
2106 dw2_asm_output_data_uleb128 (r, NULL);
2107 dw2_asm_output_data_uleb128 (o, NULL);
2109 break;
2110 case DW_OP_GNU_deref_type:
2112 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2113 gcc_assert (o);
2114 dw2_asm_output_data (1, val1->v.val_int, NULL);
2115 dw2_asm_output_data_uleb128 (o, NULL);
2117 break;
2118 case DW_OP_GNU_convert:
2119 case DW_OP_GNU_reinterpret:
2120 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2121 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2122 else
2124 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2125 gcc_assert (o);
2126 dw2_asm_output_data_uleb128 (o, NULL);
2128 break;
2130 case DW_OP_GNU_parameter_ref:
2132 unsigned long o;
2133 gcc_assert (val1->val_class == dw_val_class_die_ref);
2134 o = get_ref_die_offset (val1->v.val_die_ref.die);
2135 dw2_asm_output_data (4, o, NULL);
2137 break;
2139 default:
2140 /* Other codes have no operands. */
2141 break;
2145 /* Output a sequence of location operations.
2146 The for_eh_or_skip parameter controls whether register numbers are
2147 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2148 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2149 info). This should be suppressed for the cases that have not been converted
2150 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2152 void
2153 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2155 for (; loc != NULL; loc = loc->dw_loc_next)
2157 enum dwarf_location_atom opc = loc->dw_loc_opc;
2158 /* Output the opcode. */
2159 if (for_eh_or_skip >= 0
2160 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2162 unsigned r = (opc - DW_OP_breg0);
2163 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2164 gcc_assert (r <= 31);
2165 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2167 else if (for_eh_or_skip >= 0
2168 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2170 unsigned r = (opc - DW_OP_reg0);
2171 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2172 gcc_assert (r <= 31);
2173 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2176 dw2_asm_output_data (1, opc,
2177 "%s", dwarf_stack_op_name (opc));
2179 /* Output the operand(s) (if any). */
2180 output_loc_operands (loc, for_eh_or_skip);
2184 /* Output location description stack opcode's operands (if any).
2185 The output is single bytes on a line, suitable for .cfi_escape. */
2187 static void
2188 output_loc_operands_raw (dw_loc_descr_ref loc)
2190 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2191 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2193 switch (loc->dw_loc_opc)
2195 case DW_OP_addr:
2196 case DW_OP_GNU_addr_index:
2197 case DW_OP_GNU_const_index:
2198 case DW_OP_implicit_value:
2199 /* We cannot output addresses in .cfi_escape, only bytes. */
2200 gcc_unreachable ();
2202 case DW_OP_const1u:
2203 case DW_OP_const1s:
2204 case DW_OP_pick:
2205 case DW_OP_deref_size:
2206 case DW_OP_xderef_size:
2207 fputc (',', asm_out_file);
2208 dw2_asm_output_data_raw (1, val1->v.val_int);
2209 break;
2211 case DW_OP_const2u:
2212 case DW_OP_const2s:
2213 fputc (',', asm_out_file);
2214 dw2_asm_output_data_raw (2, val1->v.val_int);
2215 break;
2217 case DW_OP_const4u:
2218 case DW_OP_const4s:
2219 fputc (',', asm_out_file);
2220 dw2_asm_output_data_raw (4, val1->v.val_int);
2221 break;
2223 case DW_OP_const8u:
2224 case DW_OP_const8s:
2225 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2226 fputc (',', asm_out_file);
2227 dw2_asm_output_data_raw (8, val1->v.val_int);
2228 break;
2230 case DW_OP_skip:
2231 case DW_OP_bra:
2233 int offset;
2235 gcc_assert (val1->val_class == dw_val_class_loc);
2236 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2238 fputc (',', asm_out_file);
2239 dw2_asm_output_data_raw (2, offset);
2241 break;
2243 case DW_OP_regx:
2245 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2246 gcc_assert (size_of_uleb128 (r)
2247 == size_of_uleb128 (val1->v.val_unsigned));
2248 fputc (',', asm_out_file);
2249 dw2_asm_output_data_uleb128_raw (r);
2251 break;
2253 case DW_OP_constu:
2254 case DW_OP_plus_uconst:
2255 case DW_OP_piece:
2256 fputc (',', asm_out_file);
2257 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2258 break;
2260 case DW_OP_bit_piece:
2261 fputc (',', asm_out_file);
2262 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2263 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2264 break;
2266 case DW_OP_consts:
2267 case DW_OP_breg0:
2268 case DW_OP_breg1:
2269 case DW_OP_breg2:
2270 case DW_OP_breg3:
2271 case DW_OP_breg4:
2272 case DW_OP_breg5:
2273 case DW_OP_breg6:
2274 case DW_OP_breg7:
2275 case DW_OP_breg8:
2276 case DW_OP_breg9:
2277 case DW_OP_breg10:
2278 case DW_OP_breg11:
2279 case DW_OP_breg12:
2280 case DW_OP_breg13:
2281 case DW_OP_breg14:
2282 case DW_OP_breg15:
2283 case DW_OP_breg16:
2284 case DW_OP_breg17:
2285 case DW_OP_breg18:
2286 case DW_OP_breg19:
2287 case DW_OP_breg20:
2288 case DW_OP_breg21:
2289 case DW_OP_breg22:
2290 case DW_OP_breg23:
2291 case DW_OP_breg24:
2292 case DW_OP_breg25:
2293 case DW_OP_breg26:
2294 case DW_OP_breg27:
2295 case DW_OP_breg28:
2296 case DW_OP_breg29:
2297 case DW_OP_breg30:
2298 case DW_OP_breg31:
2299 case DW_OP_fbreg:
2300 fputc (',', asm_out_file);
2301 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2302 break;
2304 case DW_OP_bregx:
2306 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2307 gcc_assert (size_of_uleb128 (r)
2308 == size_of_uleb128 (val1->v.val_unsigned));
2309 fputc (',', asm_out_file);
2310 dw2_asm_output_data_uleb128_raw (r);
2311 fputc (',', asm_out_file);
2312 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2314 break;
2316 case DW_OP_GNU_implicit_pointer:
2317 case DW_OP_GNU_entry_value:
2318 case DW_OP_GNU_const_type:
2319 case DW_OP_GNU_regval_type:
2320 case DW_OP_GNU_deref_type:
2321 case DW_OP_GNU_convert:
2322 case DW_OP_GNU_reinterpret:
2323 case DW_OP_GNU_parameter_ref:
2324 gcc_unreachable ();
2325 break;
2327 default:
2328 /* Other codes have no operands. */
2329 break;
2333 void
2334 output_loc_sequence_raw (dw_loc_descr_ref loc)
2336 while (1)
2338 enum dwarf_location_atom opc = loc->dw_loc_opc;
2339 /* Output the opcode. */
2340 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2342 unsigned r = (opc - DW_OP_breg0);
2343 r = DWARF2_FRAME_REG_OUT (r, 1);
2344 gcc_assert (r <= 31);
2345 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2347 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2349 unsigned r = (opc - DW_OP_reg0);
2350 r = DWARF2_FRAME_REG_OUT (r, 1);
2351 gcc_assert (r <= 31);
2352 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2354 /* Output the opcode. */
2355 fprintf (asm_out_file, "%#x", opc);
2356 output_loc_operands_raw (loc);
2358 if (!loc->dw_loc_next)
2359 break;
2360 loc = loc->dw_loc_next;
2362 fputc (',', asm_out_file);
2366 /* This function builds a dwarf location descriptor sequence from a
2367 dw_cfa_location, adding the given OFFSET to the result of the
2368 expression. */
2370 struct dw_loc_descr_node *
2371 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2373 struct dw_loc_descr_node *head, *tmp;
2375 offset += cfa->offset;
2377 if (cfa->indirect)
2379 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2380 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2381 head->dw_loc_oprnd1.val_entry = NULL;
2382 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2383 add_loc_descr (&head, tmp);
2384 if (offset != 0)
2386 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2387 add_loc_descr (&head, tmp);
2390 else
2391 head = new_reg_loc_descr (cfa->reg, offset);
2393 return head;
2396 /* This function builds a dwarf location descriptor sequence for
2397 the address at OFFSET from the CFA when stack is aligned to
2398 ALIGNMENT byte. */
2400 struct dw_loc_descr_node *
2401 build_cfa_aligned_loc (dw_cfa_location *cfa,
2402 HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2404 struct dw_loc_descr_node *head;
2405 unsigned int dwarf_fp
2406 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2408 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2409 if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2411 head = new_reg_loc_descr (dwarf_fp, 0);
2412 add_loc_descr (&head, int_loc_descriptor (alignment));
2413 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2414 loc_descr_plus_const (&head, offset);
2416 else
2417 head = new_reg_loc_descr (dwarf_fp, offset);
2418 return head;
2421 /* And now, the support for symbolic debugging information. */
2423 /* .debug_str support. */
2425 static void dwarf2out_init (const char *);
2426 static void dwarf2out_finish (const char *);
2427 static void dwarf2out_early_finish (void);
2428 static void dwarf2out_assembly_start (void);
2429 static void dwarf2out_define (unsigned int, const char *);
2430 static void dwarf2out_undef (unsigned int, const char *);
2431 static void dwarf2out_start_source_file (unsigned, const char *);
2432 static void dwarf2out_end_source_file (unsigned);
2433 static void dwarf2out_function_decl (tree);
2434 static void dwarf2out_begin_block (unsigned, unsigned);
2435 static void dwarf2out_end_block (unsigned, unsigned);
2436 static bool dwarf2out_ignore_block (const_tree);
2437 static void dwarf2out_early_global_decl (tree);
2438 static void dwarf2out_late_global_decl (tree);
2439 static void dwarf2out_type_decl (tree, int);
2440 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
2441 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2442 dw_die_ref);
2443 static void dwarf2out_abstract_function (tree);
2444 static void dwarf2out_var_location (rtx_insn *);
2445 static void dwarf2out_begin_function (tree);
2446 static void dwarf2out_end_function (unsigned int);
2447 static void dwarf2out_register_main_translation_unit (tree unit);
2448 static void dwarf2out_set_name (tree, tree);
2450 /* The debug hooks structure. */
2452 const struct gcc_debug_hooks dwarf2_debug_hooks =
2454 dwarf2out_init,
2455 dwarf2out_finish,
2456 dwarf2out_early_finish,
2457 dwarf2out_assembly_start,
2458 dwarf2out_define,
2459 dwarf2out_undef,
2460 dwarf2out_start_source_file,
2461 dwarf2out_end_source_file,
2462 dwarf2out_begin_block,
2463 dwarf2out_end_block,
2464 dwarf2out_ignore_block,
2465 dwarf2out_source_line,
2466 dwarf2out_begin_prologue,
2467 #if VMS_DEBUGGING_INFO
2468 dwarf2out_vms_end_prologue,
2469 dwarf2out_vms_begin_epilogue,
2470 #else
2471 debug_nothing_int_charstar,
2472 debug_nothing_int_charstar,
2473 #endif
2474 dwarf2out_end_epilogue,
2475 dwarf2out_begin_function,
2476 dwarf2out_end_function, /* end_function */
2477 dwarf2out_register_main_translation_unit,
2478 dwarf2out_function_decl, /* function_decl */
2479 dwarf2out_early_global_decl,
2480 dwarf2out_late_global_decl,
2481 dwarf2out_type_decl, /* type_decl */
2482 dwarf2out_imported_module_or_decl,
2483 debug_nothing_tree, /* deferred_inline_function */
2484 /* The DWARF 2 backend tries to reduce debugging bloat by not
2485 emitting the abstract description of inline functions until
2486 something tries to reference them. */
2487 dwarf2out_abstract_function, /* outlining_inline_function */
2488 debug_nothing_rtx_code_label, /* label */
2489 debug_nothing_int, /* handle_pch */
2490 dwarf2out_var_location,
2491 dwarf2out_switch_text_section,
2492 dwarf2out_set_name,
2493 1, /* start_end_main_source_file */
2494 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2497 /* NOTE: In the comments in this file, many references are made to
2498 "Debugging Information Entries". This term is abbreviated as `DIE'
2499 throughout the remainder of this file. */
2501 /* An internal representation of the DWARF output is built, and then
2502 walked to generate the DWARF debugging info. The walk of the internal
2503 representation is done after the entire program has been compiled.
2504 The types below are used to describe the internal representation. */
2506 /* Whether to put type DIEs into their own section .debug_types instead
2507 of making them part of the .debug_info section. Only supported for
2508 Dwarf V4 or higher and the user didn't disable them through
2509 -fno-debug-types-section. It is more efficient to put them in a
2510 separate comdat sections since the linker will then be able to
2511 remove duplicates. But not all tools support .debug_types sections
2512 yet. */
2514 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2516 /* Various DIE's use offsets relative to the beginning of the
2517 .debug_info section to refer to each other. */
2519 typedef long int dw_offset;
2521 /* Define typedefs here to avoid circular dependencies. */
2523 typedef struct dw_attr_struct *dw_attr_ref;
2524 typedef struct dw_line_info_struct *dw_line_info_ref;
2525 typedef struct pubname_struct *pubname_ref;
2526 typedef struct dw_ranges_struct *dw_ranges_ref;
2527 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
2528 typedef struct comdat_type_struct *comdat_type_node_ref;
2530 /* The entries in the line_info table more-or-less mirror the opcodes
2531 that are used in the real dwarf line table. Arrays of these entries
2532 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2533 supported. */
2535 enum dw_line_info_opcode {
2536 /* Emit DW_LNE_set_address; the operand is the label index. */
2537 LI_set_address,
2539 /* Emit a row to the matrix with the given line. This may be done
2540 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2541 special opcodes. */
2542 LI_set_line,
2544 /* Emit a DW_LNS_set_file. */
2545 LI_set_file,
2547 /* Emit a DW_LNS_set_column. */
2548 LI_set_column,
2550 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
2551 LI_negate_stmt,
2553 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
2554 LI_set_prologue_end,
2555 LI_set_epilogue_begin,
2557 /* Emit a DW_LNE_set_discriminator. */
2558 LI_set_discriminator
2561 typedef struct GTY(()) dw_line_info_struct {
2562 enum dw_line_info_opcode opcode;
2563 unsigned int val;
2564 } dw_line_info_entry;
2567 typedef struct GTY(()) dw_line_info_table_struct {
2568 /* The label that marks the end of this section. */
2569 const char *end_label;
2571 /* The values for the last row of the matrix, as collected in the table.
2572 These are used to minimize the changes to the next row. */
2573 unsigned int file_num;
2574 unsigned int line_num;
2575 unsigned int column_num;
2576 int discrim_num;
2577 bool is_stmt;
2578 bool in_use;
2580 vec<dw_line_info_entry, va_gc> *entries;
2581 } dw_line_info_table;
2583 typedef dw_line_info_table *dw_line_info_table_p;
2586 /* Each DIE attribute has a field specifying the attribute kind,
2587 a link to the next attribute in the chain, and an attribute value.
2588 Attributes are typically linked below the DIE they modify. */
2590 typedef struct GTY(()) dw_attr_struct {
2591 enum dwarf_attribute dw_attr;
2592 dw_val_node dw_attr_val;
2594 dw_attr_node;
2597 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
2598 The children of each node form a circular list linked by
2599 die_sib. die_child points to the node *before* the "first" child node. */
2601 typedef struct GTY((chain_circular ("%h.die_sib"), for_user)) die_struct {
2602 union die_symbol_or_type_node
2604 const char * GTY ((tag ("0"))) die_symbol;
2605 comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
2607 GTY ((desc ("%0.comdat_type_p"))) die_id;
2608 vec<dw_attr_node, va_gc> *die_attr;
2609 dw_die_ref die_parent;
2610 dw_die_ref die_child;
2611 dw_die_ref die_sib;
2612 dw_die_ref die_definition; /* ref from a specification to its definition */
2613 dw_offset die_offset;
2614 unsigned long die_abbrev;
2615 int die_mark;
2616 unsigned int decl_id;
2617 enum dwarf_tag die_tag;
2618 /* Die is used and must not be pruned as unused. */
2619 BOOL_BITFIELD die_perennial_p : 1;
2620 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2621 /* Lots of spare bits. */
2623 die_node;
2625 /* Set to TRUE while dwarf2out_early_global_decl is running. */
2626 static bool early_dwarf;
2627 struct set_early_dwarf {
2628 bool saved;
2629 set_early_dwarf () : saved(early_dwarf) { early_dwarf = true; }
2630 ~set_early_dwarf () { early_dwarf = saved; }
2633 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
2634 #define FOR_EACH_CHILD(die, c, expr) do { \
2635 c = die->die_child; \
2636 if (c) do { \
2637 c = c->die_sib; \
2638 expr; \
2639 } while (c != die->die_child); \
2640 } while (0)
2642 /* The pubname structure */
2644 typedef struct GTY(()) pubname_struct {
2645 dw_die_ref die;
2646 const char *name;
2648 pubname_entry;
2651 struct GTY(()) dw_ranges_struct {
2652 /* If this is positive, it's a block number, otherwise it's a
2653 bitwise-negated index into dw_ranges_by_label. */
2654 int num;
2657 /* A structure to hold a macinfo entry. */
2659 typedef struct GTY(()) macinfo_struct {
2660 unsigned char code;
2661 unsigned HOST_WIDE_INT lineno;
2662 const char *info;
2664 macinfo_entry;
2667 struct GTY(()) dw_ranges_by_label_struct {
2668 const char *begin;
2669 const char *end;
2672 /* The comdat type node structure. */
2673 typedef struct GTY(()) comdat_type_struct
2675 dw_die_ref root_die;
2676 dw_die_ref type_die;
2677 dw_die_ref skeleton_die;
2678 char signature[DWARF_TYPE_SIGNATURE_SIZE];
2679 struct comdat_type_struct *next;
2681 comdat_type_node;
2683 /* A list of DIEs for which we can't determine ancestry (parent_die
2684 field) just yet. Later in dwarf2out_finish we will fill in the
2685 missing bits. */
2686 typedef struct GTY(()) limbo_die_struct {
2687 dw_die_ref die;
2688 /* The tree for which this DIE was created. We use this to
2689 determine ancestry later. */
2690 tree created_for;
2691 struct limbo_die_struct *next;
2693 limbo_die_node;
2695 typedef struct skeleton_chain_struct
2697 dw_die_ref old_die;
2698 dw_die_ref new_die;
2699 struct skeleton_chain_struct *parent;
2701 skeleton_chain_node;
2703 /* Define a macro which returns nonzero for a TYPE_DECL which was
2704 implicitly generated for a type.
2706 Note that, unlike the C front-end (which generates a NULL named
2707 TYPE_DECL node for each complete tagged type, each array type,
2708 and each function type node created) the C++ front-end generates
2709 a _named_ TYPE_DECL node for each tagged type node created.
2710 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
2711 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
2712 front-end, but for each type, tagged or not. */
2714 #define TYPE_DECL_IS_STUB(decl) \
2715 (DECL_NAME (decl) == NULL_TREE \
2716 || (DECL_ARTIFICIAL (decl) \
2717 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
2718 /* This is necessary for stub decls that \
2719 appear in nested inline functions. */ \
2720 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
2721 && (decl_ultimate_origin (decl) \
2722 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
2724 /* Information concerning the compilation unit's programming
2725 language, and compiler version. */
2727 /* Fixed size portion of the DWARF compilation unit header. */
2728 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
2729 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
2731 /* Fixed size portion of the DWARF comdat type unit header. */
2732 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
2733 (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
2734 + DWARF_OFFSET_SIZE)
2736 /* Fixed size portion of public names info. */
2737 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
2739 /* Fixed size portion of the address range info. */
2740 #define DWARF_ARANGES_HEADER_SIZE \
2741 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2742 DWARF2_ADDR_SIZE * 2) \
2743 - DWARF_INITIAL_LENGTH_SIZE)
2745 /* Size of padding portion in the address range info. It must be
2746 aligned to twice the pointer size. */
2747 #define DWARF_ARANGES_PAD_SIZE \
2748 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2749 DWARF2_ADDR_SIZE * 2) \
2750 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
2752 /* Use assembler line directives if available. */
2753 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
2754 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
2755 #define DWARF2_ASM_LINE_DEBUG_INFO 1
2756 #else
2757 #define DWARF2_ASM_LINE_DEBUG_INFO 0
2758 #endif
2759 #endif
2761 /* Minimum line offset in a special line info. opcode.
2762 This value was chosen to give a reasonable range of values. */
2763 #define DWARF_LINE_BASE -10
2765 /* First special line opcode - leave room for the standard opcodes. */
2766 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
2768 /* Range of line offsets in a special line info. opcode. */
2769 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
2771 /* Flag that indicates the initial value of the is_stmt_start flag.
2772 In the present implementation, we do not mark any lines as
2773 the beginning of a source statement, because that information
2774 is not made available by the GCC front-end. */
2775 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
2777 /* Maximum number of operations per instruction bundle. */
2778 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
2779 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
2780 #endif
2782 /* This location is used by calc_die_sizes() to keep track
2783 the offset of each DIE within the .debug_info section. */
2784 static unsigned long next_die_offset;
2786 /* Record the root of the DIE's built for the current compilation unit. */
2787 static GTY(()) dw_die_ref single_comp_unit_die;
2789 /* A list of type DIEs that have been separated into comdat sections. */
2790 static GTY(()) comdat_type_node *comdat_type_list;
2792 /* A list of DIEs with a NULL parent waiting to be relocated. */
2793 static GTY(()) limbo_die_node *limbo_die_list;
2795 /* A list of DIEs for which we may have to generate
2796 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
2797 static GTY(()) limbo_die_node *deferred_asm_name;
2799 struct dwarf_file_hasher : ggc_hasher<dwarf_file_data *>
2801 typedef const char *compare_type;
2803 static hashval_t hash (dwarf_file_data *);
2804 static bool equal (dwarf_file_data *, const char *);
2807 /* Filenames referenced by this compilation unit. */
2808 static GTY(()) hash_table<dwarf_file_hasher> *file_table;
2810 struct decl_die_hasher : ggc_hasher<die_node *>
2812 typedef tree compare_type;
2814 static hashval_t hash (die_node *);
2815 static bool equal (die_node *, tree);
2817 /* A hash table of references to DIE's that describe declarations.
2818 The key is a DECL_UID() which is a unique number identifying each decl. */
2819 static GTY (()) hash_table<decl_die_hasher> *decl_die_table;
2821 struct block_die_hasher : ggc_hasher<die_struct *>
2823 static hashval_t hash (die_struct *);
2824 static bool equal (die_struct *, die_struct *);
2827 /* A hash table of references to DIE's that describe COMMON blocks.
2828 The key is DECL_UID() ^ die_parent. */
2829 static GTY (()) hash_table<block_die_hasher> *common_block_die_table;
2831 typedef struct GTY(()) die_arg_entry_struct {
2832 dw_die_ref die;
2833 tree arg;
2834 } die_arg_entry;
2837 /* Node of the variable location list. */
2838 struct GTY ((chain_next ("%h.next"))) var_loc_node {
2839 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
2840 EXPR_LIST chain. For small bitsizes, bitsize is encoded
2841 in mode of the EXPR_LIST node and first EXPR_LIST operand
2842 is either NOTE_INSN_VAR_LOCATION for a piece with a known
2843 location or NULL for padding. For larger bitsizes,
2844 mode is 0 and first operand is a CONCAT with bitsize
2845 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
2846 NULL as second operand. */
2847 rtx GTY (()) loc;
2848 const char * GTY (()) label;
2849 struct var_loc_node * GTY (()) next;
2852 /* Variable location list. */
2853 struct GTY ((for_user)) var_loc_list_def {
2854 struct var_loc_node * GTY (()) first;
2856 /* Pointer to the last but one or last element of the
2857 chained list. If the list is empty, both first and
2858 last are NULL, if the list contains just one node
2859 or the last node certainly is not redundant, it points
2860 to the last node, otherwise points to the last but one.
2861 Do not mark it for GC because it is marked through the chain. */
2862 struct var_loc_node * GTY ((skip ("%h"))) last;
2864 /* Pointer to the last element before section switch,
2865 if NULL, either sections weren't switched or first
2866 is after section switch. */
2867 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
2869 /* DECL_UID of the variable decl. */
2870 unsigned int decl_id;
2872 typedef struct var_loc_list_def var_loc_list;
2874 /* Call argument location list. */
2875 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
2876 rtx GTY (()) call_arg_loc_note;
2877 const char * GTY (()) label;
2878 tree GTY (()) block;
2879 bool tail_call_p;
2880 rtx GTY (()) symbol_ref;
2881 struct call_arg_loc_node * GTY (()) next;
2885 struct decl_loc_hasher : ggc_hasher<var_loc_list *>
2887 typedef const_tree compare_type;
2889 static hashval_t hash (var_loc_list *);
2890 static bool equal (var_loc_list *, const_tree);
2893 /* Table of decl location linked lists. */
2894 static GTY (()) hash_table<decl_loc_hasher> *decl_loc_table;
2896 /* Head and tail of call_arg_loc chain. */
2897 static GTY (()) struct call_arg_loc_node *call_arg_locations;
2898 static struct call_arg_loc_node *call_arg_loc_last;
2900 /* Number of call sites in the current function. */
2901 static int call_site_count = -1;
2902 /* Number of tail call sites in the current function. */
2903 static int tail_call_site_count = -1;
2905 /* A cached location list. */
2906 struct GTY ((for_user)) cached_dw_loc_list_def {
2907 /* The DECL_UID of the decl that this entry describes. */
2908 unsigned int decl_id;
2910 /* The cached location list. */
2911 dw_loc_list_ref loc_list;
2913 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
2915 struct dw_loc_list_hasher : ggc_hasher<cached_dw_loc_list *>
2918 typedef const_tree compare_type;
2920 static hashval_t hash (cached_dw_loc_list *);
2921 static bool equal (cached_dw_loc_list *, const_tree);
2924 /* Table of cached location lists. */
2925 static GTY (()) hash_table<dw_loc_list_hasher> *cached_dw_loc_list_table;
2927 /* A pointer to the base of a list of references to DIE's that
2928 are uniquely identified by their tag, presence/absence of
2929 children DIE's, and list of attribute/value pairs. */
2930 static GTY((length ("abbrev_die_table_allocated")))
2931 dw_die_ref *abbrev_die_table;
2933 /* Number of elements currently allocated for abbrev_die_table. */
2934 static GTY(()) unsigned abbrev_die_table_allocated;
2936 /* Number of elements in abbrev_die_table currently in use. */
2937 static GTY(()) unsigned abbrev_die_table_in_use;
2939 /* Size (in elements) of increments by which we may expand the
2940 abbrev_die_table. */
2941 #define ABBREV_DIE_TABLE_INCREMENT 256
2943 /* A global counter for generating labels for line number data. */
2944 static unsigned int line_info_label_num;
2946 /* The current table to which we should emit line number information
2947 for the current function. This will be set up at the beginning of
2948 assembly for the function. */
2949 static dw_line_info_table *cur_line_info_table;
2951 /* The two default tables of line number info. */
2952 static GTY(()) dw_line_info_table *text_section_line_info;
2953 static GTY(()) dw_line_info_table *cold_text_section_line_info;
2955 /* The set of all non-default tables of line number info. */
2956 static GTY(()) vec<dw_line_info_table_p, va_gc> *separate_line_info;
2958 /* A flag to tell pubnames/types export if there is an info section to
2959 refer to. */
2960 static bool info_section_emitted;
2962 /* A pointer to the base of a table that contains a list of publicly
2963 accessible names. */
2964 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
2966 /* A pointer to the base of a table that contains a list of publicly
2967 accessible types. */
2968 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
2970 /* A pointer to the base of a table that contains a list of macro
2971 defines/undefines (and file start/end markers). */
2972 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
2974 /* True if .debug_macinfo or .debug_macros section is going to be
2975 emitted. */
2976 #define have_macinfo \
2977 (debug_info_level >= DINFO_LEVEL_VERBOSE \
2978 && !macinfo_table->is_empty ())
2980 /* Array of dies for which we should generate .debug_ranges info. */
2981 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
2983 /* Number of elements currently allocated for ranges_table. */
2984 static GTY(()) unsigned ranges_table_allocated;
2986 /* Number of elements in ranges_table currently in use. */
2987 static GTY(()) unsigned ranges_table_in_use;
2989 /* Array of pairs of labels referenced in ranges_table. */
2990 static GTY ((length ("ranges_by_label_allocated")))
2991 dw_ranges_by_label_ref ranges_by_label;
2993 /* Number of elements currently allocated for ranges_by_label. */
2994 static GTY(()) unsigned ranges_by_label_allocated;
2996 /* Number of elements in ranges_by_label currently in use. */
2997 static GTY(()) unsigned ranges_by_label_in_use;
2999 /* Size (in elements) of increments by which we may expand the
3000 ranges_table. */
3001 #define RANGES_TABLE_INCREMENT 64
3003 /* Whether we have location lists that need outputting */
3004 static GTY(()) bool have_location_lists;
3006 /* Unique label counter. */
3007 static GTY(()) unsigned int loclabel_num;
3009 /* Unique label counter for point-of-call tables. */
3010 static GTY(()) unsigned int poc_label_num;
3012 /* The last file entry emitted by maybe_emit_file(). */
3013 static GTY(()) struct dwarf_file_data * last_emitted_file;
3015 /* Number of internal labels generated by gen_internal_sym(). */
3016 static GTY(()) int label_num;
3018 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
3020 /* Instances of generic types for which we need to generate debug
3021 info that describe their generic parameters and arguments. That
3022 generation needs to happen once all types are properly laid out so
3023 we do it at the end of compilation. */
3024 static GTY(()) vec<tree, va_gc> *generic_type_instances;
3026 /* Offset from the "steady-state frame pointer" to the frame base,
3027 within the current function. */
3028 static HOST_WIDE_INT frame_pointer_fb_offset;
3029 static bool frame_pointer_fb_offset_valid;
3031 static vec<dw_die_ref> base_types;
3033 /* Flags to represent a set of attribute classes for attributes that represent
3034 a scalar value (bounds, pointers, ...). */
3035 enum dw_scalar_form
3037 dw_scalar_form_constant = 0x01,
3038 dw_scalar_form_exprloc = 0x02,
3039 dw_scalar_form_reference = 0x04
3042 /* Forward declarations for functions defined in this file. */
3044 static int is_pseudo_reg (const_rtx);
3045 static tree type_main_variant (tree);
3046 static int is_tagged_type (const_tree);
3047 static const char *dwarf_tag_name (unsigned);
3048 static const char *dwarf_attr_name (unsigned);
3049 static const char *dwarf_form_name (unsigned);
3050 static tree decl_ultimate_origin (const_tree);
3051 static tree decl_class_context (tree);
3052 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
3053 static inline enum dw_val_class AT_class (dw_attr_ref);
3054 static inline unsigned int AT_index (dw_attr_ref);
3055 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3056 static inline unsigned AT_flag (dw_attr_ref);
3057 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3058 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
3059 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3060 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
3061 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3062 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3063 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3064 unsigned int, unsigned char *);
3065 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3066 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3067 static inline const char *AT_string (dw_attr_ref);
3068 static enum dwarf_form AT_string_form (dw_attr_ref);
3069 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3070 static void add_AT_specification (dw_die_ref, dw_die_ref);
3071 static inline dw_die_ref AT_ref (dw_attr_ref);
3072 static inline int AT_ref_external (dw_attr_ref);
3073 static inline void set_AT_ref_external (dw_attr_ref, int);
3074 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3075 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3076 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
3077 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3078 dw_loc_list_ref);
3079 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
3080 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3081 static void remove_addr_table_entry (addr_table_entry *);
3082 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3083 static inline rtx AT_addr (dw_attr_ref);
3084 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3085 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3086 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3087 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3088 unsigned HOST_WIDE_INT);
3089 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3090 unsigned long, bool);
3091 static inline const char *AT_lbl (dw_attr_ref);
3092 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
3093 static const char *get_AT_low_pc (dw_die_ref);
3094 static const char *get_AT_hi_pc (dw_die_ref);
3095 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3096 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3097 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3098 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3099 static bool is_cxx (void);
3100 static bool is_fortran (void);
3101 static bool is_ada (void);
3102 static bool remove_AT (dw_die_ref, enum dwarf_attribute);
3103 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3104 static void add_child_die (dw_die_ref, dw_die_ref);
3105 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3106 static dw_die_ref lookup_type_die (tree);
3107 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3108 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3109 static void equate_type_number_to_die (tree, dw_die_ref);
3110 static dw_die_ref lookup_decl_die (tree);
3111 static var_loc_list *lookup_decl_loc (const_tree);
3112 static void equate_decl_number_to_die (tree, dw_die_ref);
3113 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3114 static void print_spaces (FILE *);
3115 static void print_die (dw_die_ref, FILE *);
3116 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3117 static dw_die_ref pop_compile_unit (dw_die_ref);
3118 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3119 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
3120 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3121 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3122 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3123 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3124 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
3125 struct md5_ctx *, int *);
3126 struct checksum_attributes;
3127 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3128 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3129 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3130 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3131 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3132 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3133 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
3134 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3135 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3136 static void compute_section_prefix (dw_die_ref);
3137 static int is_type_die (dw_die_ref);
3138 static int is_comdat_die (dw_die_ref);
3139 static int is_symbol_die (dw_die_ref);
3140 static inline bool is_template_instantiation (dw_die_ref);
3141 static void assign_symbol_names (dw_die_ref);
3142 static void break_out_includes (dw_die_ref);
3143 static int is_declaration_die (dw_die_ref);
3144 static int should_move_die_to_comdat (dw_die_ref);
3145 static dw_die_ref clone_as_declaration (dw_die_ref);
3146 static dw_die_ref clone_die (dw_die_ref);
3147 static dw_die_ref clone_tree (dw_die_ref);
3148 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3149 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3150 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3151 static dw_die_ref generate_skeleton (dw_die_ref);
3152 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3153 dw_die_ref,
3154 dw_die_ref);
3155 static void break_out_comdat_types (dw_die_ref);
3156 static void copy_decls_for_unworthy_types (dw_die_ref);
3158 static void add_sibling_attributes (dw_die_ref);
3159 static void output_location_lists (dw_die_ref);
3160 static int constant_size (unsigned HOST_WIDE_INT);
3161 static unsigned long size_of_die (dw_die_ref);
3162 static void calc_die_sizes (dw_die_ref);
3163 static void calc_base_type_die_sizes (void);
3164 static void mark_dies (dw_die_ref);
3165 static void unmark_dies (dw_die_ref);
3166 static void unmark_all_dies (dw_die_ref);
3167 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3168 static unsigned long size_of_aranges (void);
3169 static enum dwarf_form value_format (dw_attr_ref);
3170 static void output_value_format (dw_attr_ref);
3171 static void output_abbrev_section (void);
3172 static void output_die_abbrevs (unsigned long, dw_die_ref);
3173 static void output_die_symbol (dw_die_ref);
3174 static void output_die (dw_die_ref);
3175 static void output_compilation_unit_header (void);
3176 static void output_comp_unit (dw_die_ref, int);
3177 static void output_comdat_type_unit (comdat_type_node *);
3178 static const char *dwarf2_name (tree, int);
3179 static void add_pubname (tree, dw_die_ref);
3180 static void add_enumerator_pubname (const char *, dw_die_ref);
3181 static void add_pubname_string (const char *, dw_die_ref);
3182 static void add_pubtype (tree, dw_die_ref);
3183 static void output_pubnames (vec<pubname_entry, va_gc> *);
3184 static void output_aranges (unsigned long);
3185 static unsigned int add_ranges_num (int);
3186 static unsigned int add_ranges (const_tree);
3187 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3188 bool *, bool);
3189 static void output_ranges (void);
3190 static dw_line_info_table *new_line_info_table (void);
3191 static void output_line_info (bool);
3192 static void output_file_names (void);
3193 static dw_die_ref base_type_die (tree);
3194 static int is_base_type (tree);
3195 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
3196 static int decl_quals (const_tree);
3197 static dw_die_ref modified_type_die (tree, int, dw_die_ref);
3198 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3199 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3200 static int type_is_enum (const_tree);
3201 static unsigned int dbx_reg_number (const_rtx);
3202 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3203 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3204 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3205 enum var_init_status);
3206 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3207 enum var_init_status);
3208 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3209 enum var_init_status);
3210 static int is_based_loc (const_rtx);
3211 static bool resolve_one_addr (rtx *);
3212 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3213 enum var_init_status);
3214 static dw_loc_descr_ref loc_descriptor (rtx, machine_mode mode,
3215 enum var_init_status);
3216 struct loc_descr_context;
3217 static dw_loc_list_ref loc_list_from_tree (tree, int,
3218 const struct loc_descr_context *);
3219 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int,
3220 const struct loc_descr_context *);
3221 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3222 static tree field_type (const_tree);
3223 static unsigned int simple_type_align_in_bits (const_tree);
3224 static unsigned int simple_decl_align_in_bits (const_tree);
3225 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3226 static HOST_WIDE_INT field_byte_offset (const_tree);
3227 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3228 dw_loc_list_ref);
3229 static void add_data_member_location_attribute (dw_die_ref, tree);
3230 static bool add_const_value_attribute (dw_die_ref, rtx);
3231 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3232 static void insert_wide_int (const wide_int &, unsigned char *, int);
3233 static void insert_float (const_rtx, unsigned char *);
3234 static rtx rtl_for_decl_location (tree);
3235 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool,
3236 enum dwarf_attribute);
3237 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3238 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3239 static void add_name_attribute (dw_die_ref, const char *);
3240 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3241 static void add_comp_dir_attribute (dw_die_ref);
3242 static void add_scalar_info (dw_die_ref, enum dwarf_attribute, tree, int,
3243 const struct loc_descr_context *);
3244 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree,
3245 const struct loc_descr_context *);
3246 static void add_subscript_info (dw_die_ref, tree, bool);
3247 static void add_byte_size_attribute (dw_die_ref, tree);
3248 static void add_bit_offset_attribute (dw_die_ref, tree);
3249 static void add_bit_size_attribute (dw_die_ref, tree);
3250 static void add_prototyped_attribute (dw_die_ref, tree);
3251 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3252 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3253 static void add_src_coords_attributes (dw_die_ref, tree);
3254 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3255 static void push_decl_scope (tree);
3256 static void pop_decl_scope (void);
3257 static dw_die_ref scope_die_for (tree, dw_die_ref);
3258 static inline int local_scope_p (dw_die_ref);
3259 static inline int class_scope_p (dw_die_ref);
3260 static inline int class_or_namespace_scope_p (dw_die_ref);
3261 static void add_type_attribute (dw_die_ref, tree, int, dw_die_ref);
3262 static void add_calling_convention_attribute (dw_die_ref, tree);
3263 static const char *type_tag (const_tree);
3264 static tree member_declared_type (const_tree);
3265 #if 0
3266 static const char *decl_start_label (tree);
3267 #endif
3268 static void gen_array_type_die (tree, dw_die_ref);
3269 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3270 #if 0
3271 static void gen_entry_point_die (tree, dw_die_ref);
3272 #endif
3273 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3274 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3275 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3276 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3277 static void gen_formal_types_die (tree, dw_die_ref);
3278 static void gen_subprogram_die (tree, dw_die_ref);
3279 static void gen_variable_die (tree, tree, dw_die_ref);
3280 static void gen_const_die (tree, dw_die_ref);
3281 static void gen_label_die (tree, dw_die_ref);
3282 static void gen_lexical_block_die (tree, dw_die_ref);
3283 static void gen_inlined_subroutine_die (tree, dw_die_ref);
3284 static void gen_field_die (tree, dw_die_ref);
3285 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3286 static dw_die_ref gen_compile_unit_die (const char *);
3287 static void gen_inheritance_die (tree, tree, dw_die_ref);
3288 static void gen_member_die (tree, dw_die_ref);
3289 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3290 enum debug_info_usage);
3291 static void gen_subroutine_type_die (tree, dw_die_ref);
3292 static void gen_typedef_die (tree, dw_die_ref);
3293 static void gen_type_die (tree, dw_die_ref);
3294 static void gen_block_die (tree, dw_die_ref);
3295 static void decls_for_scope (tree, dw_die_ref);
3296 static inline int is_redundant_typedef (const_tree);
3297 static bool is_naming_typedef_decl (const_tree);
3298 static inline dw_die_ref get_context_die (tree);
3299 static void gen_namespace_die (tree, dw_die_ref);
3300 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3301 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
3302 static dw_die_ref force_decl_die (tree);
3303 static dw_die_ref force_type_die (tree);
3304 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3305 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3306 static struct dwarf_file_data * lookup_filename (const char *);
3307 static void retry_incomplete_types (void);
3308 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3309 static void gen_generic_params_dies (tree);
3310 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3311 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3312 static void splice_child_die (dw_die_ref, dw_die_ref);
3313 static int file_info_cmp (const void *, const void *);
3314 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3315 const char *, const char *);
3316 static void output_loc_list (dw_loc_list_ref);
3317 static char *gen_internal_sym (const char *);
3318 static bool want_pubnames (void);
3320 static void prune_unmark_dies (dw_die_ref);
3321 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3322 static void prune_unused_types_mark (dw_die_ref, int);
3323 static void prune_unused_types_walk (dw_die_ref);
3324 static void prune_unused_types_walk_attribs (dw_die_ref);
3325 static void prune_unused_types_prune (dw_die_ref);
3326 static void prune_unused_types (void);
3327 static int maybe_emit_file (struct dwarf_file_data *fd);
3328 static inline const char *AT_vms_delta1 (dw_attr_ref);
3329 static inline const char *AT_vms_delta2 (dw_attr_ref);
3330 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3331 const char *, const char *);
3332 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3333 static void gen_remaining_tmpl_value_param_die_attribute (void);
3334 static bool generic_type_p (tree);
3335 static void schedule_generic_params_dies_gen (tree t);
3336 static void gen_scheduled_generic_parms_dies (void);
3338 static const char *comp_dir_string (void);
3340 static void hash_loc_operands (dw_loc_descr_ref, inchash::hash &);
3342 /* enum for tracking thread-local variables whose address is really an offset
3343 relative to the TLS pointer, which will need link-time relocation, but will
3344 not need relocation by the DWARF consumer. */
3346 enum dtprel_bool
3348 dtprel_false = 0,
3349 dtprel_true = 1
3352 /* Return the operator to use for an address of a variable. For dtprel_true, we
3353 use DW_OP_const*. For regular variables, which need both link-time
3354 relocation and consumer-level relocation (e.g., to account for shared objects
3355 loaded at a random address), we use DW_OP_addr*. */
3357 static inline enum dwarf_location_atom
3358 dw_addr_op (enum dtprel_bool dtprel)
3360 if (dtprel == dtprel_true)
3361 return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3362 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3363 else
3364 return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3367 /* Return a pointer to a newly allocated address location description. If
3368 dwarf_split_debug_info is true, then record the address with the appropriate
3369 relocation. */
3370 static inline dw_loc_descr_ref
3371 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3373 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3375 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3376 ref->dw_loc_oprnd1.v.val_addr = addr;
3377 ref->dtprel = dtprel;
3378 if (dwarf_split_debug_info)
3379 ref->dw_loc_oprnd1.val_entry
3380 = add_addr_table_entry (addr,
3381 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3382 else
3383 ref->dw_loc_oprnd1.val_entry = NULL;
3385 return ref;
3388 /* Section names used to hold DWARF debugging information. */
3390 #ifndef DEBUG_INFO_SECTION
3391 #define DEBUG_INFO_SECTION ".debug_info"
3392 #endif
3393 #ifndef DEBUG_DWO_INFO_SECTION
3394 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3395 #endif
3396 #ifndef DEBUG_ABBREV_SECTION
3397 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3398 #endif
3399 #ifndef DEBUG_DWO_ABBREV_SECTION
3400 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3401 #endif
3402 #ifndef DEBUG_ARANGES_SECTION
3403 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3404 #endif
3405 #ifndef DEBUG_ADDR_SECTION
3406 #define DEBUG_ADDR_SECTION ".debug_addr"
3407 #endif
3408 #ifndef DEBUG_NORM_MACINFO_SECTION
3409 #define DEBUG_NORM_MACINFO_SECTION ".debug_macinfo"
3410 #endif
3411 #ifndef DEBUG_DWO_MACINFO_SECTION
3412 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
3413 #endif
3414 #ifndef DEBUG_MACINFO_SECTION
3415 #define DEBUG_MACINFO_SECTION \
3416 (!dwarf_split_debug_info \
3417 ? (DEBUG_NORM_MACINFO_SECTION) : (DEBUG_DWO_MACINFO_SECTION))
3418 #endif
3419 #ifndef DEBUG_NORM_MACRO_SECTION
3420 #define DEBUG_NORM_MACRO_SECTION ".debug_macro"
3421 #endif
3422 #ifndef DEBUG_DWO_MACRO_SECTION
3423 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
3424 #endif
3425 #ifndef DEBUG_MACRO_SECTION
3426 #define DEBUG_MACRO_SECTION \
3427 (!dwarf_split_debug_info \
3428 ? (DEBUG_NORM_MACRO_SECTION) : (DEBUG_DWO_MACRO_SECTION))
3429 #endif
3430 #ifndef DEBUG_LINE_SECTION
3431 #define DEBUG_LINE_SECTION ".debug_line"
3432 #endif
3433 #ifndef DEBUG_DWO_LINE_SECTION
3434 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3435 #endif
3436 #ifndef DEBUG_LOC_SECTION
3437 #define DEBUG_LOC_SECTION ".debug_loc"
3438 #endif
3439 #ifndef DEBUG_DWO_LOC_SECTION
3440 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
3441 #endif
3442 #ifndef DEBUG_PUBNAMES_SECTION
3443 #define DEBUG_PUBNAMES_SECTION \
3444 ((debug_generate_pub_sections == 2) \
3445 ? ".debug_gnu_pubnames" : ".debug_pubnames")
3446 #endif
3447 #ifndef DEBUG_PUBTYPES_SECTION
3448 #define DEBUG_PUBTYPES_SECTION \
3449 ((debug_generate_pub_sections == 2) \
3450 ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
3451 #endif
3452 #define DEBUG_NORM_STR_OFFSETS_SECTION ".debug_str_offsets"
3453 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3454 #ifndef DEBUG_STR_OFFSETS_SECTION
3455 #define DEBUG_STR_OFFSETS_SECTION \
3456 (!dwarf_split_debug_info \
3457 ? (DEBUG_NORM_STR_OFFSETS_SECTION) : (DEBUG_DWO_STR_OFFSETS_SECTION))
3458 #endif
3459 #ifndef DEBUG_STR_DWO_SECTION
3460 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
3461 #endif
3462 #ifndef DEBUG_STR_SECTION
3463 #define DEBUG_STR_SECTION ".debug_str"
3464 #endif
3465 #ifndef DEBUG_RANGES_SECTION
3466 #define DEBUG_RANGES_SECTION ".debug_ranges"
3467 #endif
3469 /* Standard ELF section names for compiled code and data. */
3470 #ifndef TEXT_SECTION_NAME
3471 #define TEXT_SECTION_NAME ".text"
3472 #endif
3474 /* Section flags for .debug_macinfo/.debug_macro section. */
3475 #define DEBUG_MACRO_SECTION_FLAGS \
3476 (dwarf_split_debug_info ? SECTION_DEBUG | SECTION_EXCLUDE : SECTION_DEBUG)
3478 /* Section flags for .debug_str section. */
3479 #define DEBUG_STR_SECTION_FLAGS \
3480 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
3481 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3482 : SECTION_DEBUG)
3484 /* Section flags for .debug_str.dwo section. */
3485 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
3487 /* Labels we insert at beginning sections we can reference instead of
3488 the section names themselves. */
3490 #ifndef TEXT_SECTION_LABEL
3491 #define TEXT_SECTION_LABEL "Ltext"
3492 #endif
3493 #ifndef COLD_TEXT_SECTION_LABEL
3494 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
3495 #endif
3496 #ifndef DEBUG_LINE_SECTION_LABEL
3497 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3498 #endif
3499 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3500 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
3501 #endif
3502 #ifndef DEBUG_INFO_SECTION_LABEL
3503 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3504 #endif
3505 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3506 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
3507 #endif
3508 #ifndef DEBUG_ABBREV_SECTION_LABEL
3509 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3510 #endif
3511 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3512 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3513 #endif
3514 #ifndef DEBUG_ADDR_SECTION_LABEL
3515 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
3516 #endif
3517 #ifndef DEBUG_LOC_SECTION_LABEL
3518 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3519 #endif
3520 #ifndef DEBUG_RANGES_SECTION_LABEL
3521 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3522 #endif
3523 #ifndef DEBUG_MACINFO_SECTION_LABEL
3524 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3525 #endif
3526 #ifndef DEBUG_MACRO_SECTION_LABEL
3527 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
3528 #endif
3529 #define SKELETON_COMP_DIE_ABBREV 1
3530 #define SKELETON_TYPE_DIE_ABBREV 2
3532 /* Definitions of defaults for formats and names of various special
3533 (artificial) labels which may be generated within this file (when the -g
3534 options is used and DWARF2_DEBUGGING_INFO is in effect.
3535 If necessary, these may be overridden from within the tm.h file, but
3536 typically, overriding these defaults is unnecessary. */
3538 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3539 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3540 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3541 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3542 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3543 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3544 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3545 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3546 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3547 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3548 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3549 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3550 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3551 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3553 #ifndef TEXT_END_LABEL
3554 #define TEXT_END_LABEL "Letext"
3555 #endif
3556 #ifndef COLD_END_LABEL
3557 #define COLD_END_LABEL "Letext_cold"
3558 #endif
3559 #ifndef BLOCK_BEGIN_LABEL
3560 #define BLOCK_BEGIN_LABEL "LBB"
3561 #endif
3562 #ifndef BLOCK_END_LABEL
3563 #define BLOCK_END_LABEL "LBE"
3564 #endif
3565 #ifndef LINE_CODE_LABEL
3566 #define LINE_CODE_LABEL "LM"
3567 #endif
3570 /* Return the root of the DIE's built for the current compilation unit. */
3571 static dw_die_ref
3572 comp_unit_die (void)
3574 if (!single_comp_unit_die)
3575 single_comp_unit_die = gen_compile_unit_die (NULL);
3576 return single_comp_unit_die;
3579 /* We allow a language front-end to designate a function that is to be
3580 called to "demangle" any name before it is put into a DIE. */
3582 static const char *(*demangle_name_func) (const char *);
3584 void
3585 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3587 demangle_name_func = func;
3590 /* Test if rtl node points to a pseudo register. */
3592 static inline int
3593 is_pseudo_reg (const_rtx rtl)
3595 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3596 || (GET_CODE (rtl) == SUBREG
3597 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3600 /* Return a reference to a type, with its const and volatile qualifiers
3601 removed. */
3603 static inline tree
3604 type_main_variant (tree type)
3606 type = TYPE_MAIN_VARIANT (type);
3608 /* ??? There really should be only one main variant among any group of
3609 variants of a given type (and all of the MAIN_VARIANT values for all
3610 members of the group should point to that one type) but sometimes the C
3611 front-end messes this up for array types, so we work around that bug
3612 here. */
3613 if (TREE_CODE (type) == ARRAY_TYPE)
3614 while (type != TYPE_MAIN_VARIANT (type))
3615 type = TYPE_MAIN_VARIANT (type);
3617 return type;
3620 /* Return nonzero if the given type node represents a tagged type. */
3622 static inline int
3623 is_tagged_type (const_tree type)
3625 enum tree_code code = TREE_CODE (type);
3627 return (code == RECORD_TYPE || code == UNION_TYPE
3628 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3631 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
3633 static void
3634 get_ref_die_offset_label (char *label, dw_die_ref ref)
3636 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3639 /* Return die_offset of a DIE reference to a base type. */
3641 static unsigned long int
3642 get_base_type_offset (dw_die_ref ref)
3644 if (ref->die_offset)
3645 return ref->die_offset;
3646 if (comp_unit_die ()->die_abbrev)
3648 calc_base_type_die_sizes ();
3649 gcc_assert (ref->die_offset);
3651 return ref->die_offset;
3654 /* Return die_offset of a DIE reference other than base type. */
3656 static unsigned long int
3657 get_ref_die_offset (dw_die_ref ref)
3659 gcc_assert (ref->die_offset);
3660 return ref->die_offset;
3663 /* Convert a DIE tag into its string name. */
3665 static const char *
3666 dwarf_tag_name (unsigned int tag)
3668 const char *name = get_DW_TAG_name (tag);
3670 if (name != NULL)
3671 return name;
3673 return "DW_TAG_<unknown>";
3676 /* Convert a DWARF attribute code into its string name. */
3678 static const char *
3679 dwarf_attr_name (unsigned int attr)
3681 const char *name;
3683 switch (attr)
3685 #if VMS_DEBUGGING_INFO
3686 case DW_AT_HP_prologue:
3687 return "DW_AT_HP_prologue";
3688 #else
3689 case DW_AT_MIPS_loop_unroll_factor:
3690 return "DW_AT_MIPS_loop_unroll_factor";
3691 #endif
3693 #if VMS_DEBUGGING_INFO
3694 case DW_AT_HP_epilogue:
3695 return "DW_AT_HP_epilogue";
3696 #else
3697 case DW_AT_MIPS_stride:
3698 return "DW_AT_MIPS_stride";
3699 #endif
3702 name = get_DW_AT_name (attr);
3704 if (name != NULL)
3705 return name;
3707 return "DW_AT_<unknown>";
3710 /* Convert a DWARF value form code into its string name. */
3712 static const char *
3713 dwarf_form_name (unsigned int form)
3715 const char *name = get_DW_FORM_name (form);
3717 if (name != NULL)
3718 return name;
3720 return "DW_FORM_<unknown>";
3723 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
3724 instance of an inlined instance of a decl which is local to an inline
3725 function, so we have to trace all of the way back through the origin chain
3726 to find out what sort of node actually served as the original seed for the
3727 given block. */
3729 static tree
3730 decl_ultimate_origin (const_tree decl)
3732 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
3733 return NULL_TREE;
3735 /* DECL_ABSTRACT_ORIGIN can point to itself; ignore that if
3736 we're trying to output the abstract instance of this function. */
3737 if (DECL_ABSTRACT_P (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
3738 return NULL_TREE;
3740 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
3741 most distant ancestor, this should never happen. */
3742 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
3744 return DECL_ABSTRACT_ORIGIN (decl);
3747 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
3748 of a virtual function may refer to a base class, so we check the 'this'
3749 parameter. */
3751 static tree
3752 decl_class_context (tree decl)
3754 tree context = NULL_TREE;
3756 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
3757 context = DECL_CONTEXT (decl);
3758 else
3759 context = TYPE_MAIN_VARIANT
3760 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
3762 if (context && !TYPE_P (context))
3763 context = NULL_TREE;
3765 return context;
3768 /* Add an attribute/value pair to a DIE. */
3770 static inline void
3771 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
3773 /* Maybe this should be an assert? */
3774 if (die == NULL)
3775 return;
3777 vec_safe_reserve (die->die_attr, 1);
3778 vec_safe_push (die->die_attr, *attr);
3781 static inline enum dw_val_class
3782 AT_class (dw_attr_ref a)
3784 return a->dw_attr_val.val_class;
3787 /* Return the index for any attribute that will be referenced with a
3788 DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index. String indices
3789 are stored in dw_attr_val.v.val_str for reference counting
3790 pruning. */
3792 static inline unsigned int
3793 AT_index (dw_attr_ref a)
3795 if (AT_class (a) == dw_val_class_str)
3796 return a->dw_attr_val.v.val_str->index;
3797 else if (a->dw_attr_val.val_entry != NULL)
3798 return a->dw_attr_val.val_entry->index;
3799 return NOT_INDEXED;
3802 /* Add a flag value attribute to a DIE. */
3804 static inline void
3805 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
3807 dw_attr_node attr;
3809 attr.dw_attr = attr_kind;
3810 attr.dw_attr_val.val_class = dw_val_class_flag;
3811 attr.dw_attr_val.val_entry = NULL;
3812 attr.dw_attr_val.v.val_flag = flag;
3813 add_dwarf_attr (die, &attr);
3816 static inline unsigned
3817 AT_flag (dw_attr_ref a)
3819 gcc_assert (a && AT_class (a) == dw_val_class_flag);
3820 return a->dw_attr_val.v.val_flag;
3823 /* Add a signed integer attribute value to a DIE. */
3825 static inline void
3826 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
3828 dw_attr_node attr;
3830 attr.dw_attr = attr_kind;
3831 attr.dw_attr_val.val_class = dw_val_class_const;
3832 attr.dw_attr_val.val_entry = NULL;
3833 attr.dw_attr_val.v.val_int = int_val;
3834 add_dwarf_attr (die, &attr);
3837 static inline HOST_WIDE_INT
3838 AT_int (dw_attr_ref a)
3840 gcc_assert (a && AT_class (a) == dw_val_class_const);
3841 return a->dw_attr_val.v.val_int;
3844 /* Add an unsigned integer attribute value to a DIE. */
3846 static inline void
3847 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
3848 unsigned HOST_WIDE_INT unsigned_val)
3850 dw_attr_node attr;
3852 attr.dw_attr = attr_kind;
3853 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
3854 attr.dw_attr_val.val_entry = NULL;
3855 attr.dw_attr_val.v.val_unsigned = unsigned_val;
3856 add_dwarf_attr (die, &attr);
3859 static inline unsigned HOST_WIDE_INT
3860 AT_unsigned (dw_attr_ref a)
3862 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
3863 return a->dw_attr_val.v.val_unsigned;
3866 /* Add an unsigned wide integer attribute value to a DIE. */
3868 static inline void
3869 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
3870 const wide_int& w)
3872 dw_attr_node attr;
3874 attr.dw_attr = attr_kind;
3875 attr.dw_attr_val.val_class = dw_val_class_wide_int;
3876 attr.dw_attr_val.val_entry = NULL;
3877 attr.dw_attr_val.v.val_wide = ggc_alloc<wide_int> ();
3878 *attr.dw_attr_val.v.val_wide = w;
3879 add_dwarf_attr (die, &attr);
3882 /* Add an unsigned double integer attribute value to a DIE. */
3884 static inline void
3885 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
3886 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
3888 dw_attr_node attr;
3890 attr.dw_attr = attr_kind;
3891 attr.dw_attr_val.val_class = dw_val_class_const_double;
3892 attr.dw_attr_val.val_entry = NULL;
3893 attr.dw_attr_val.v.val_double.high = high;
3894 attr.dw_attr_val.v.val_double.low = low;
3895 add_dwarf_attr (die, &attr);
3898 /* Add a floating point attribute value to a DIE and return it. */
3900 static inline void
3901 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
3902 unsigned int length, unsigned int elt_size, unsigned char *array)
3904 dw_attr_node attr;
3906 attr.dw_attr = attr_kind;
3907 attr.dw_attr_val.val_class = dw_val_class_vec;
3908 attr.dw_attr_val.val_entry = NULL;
3909 attr.dw_attr_val.v.val_vec.length = length;
3910 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
3911 attr.dw_attr_val.v.val_vec.array = array;
3912 add_dwarf_attr (die, &attr);
3915 /* Add an 8-byte data attribute value to a DIE. */
3917 static inline void
3918 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
3919 unsigned char data8[8])
3921 dw_attr_node attr;
3923 attr.dw_attr = attr_kind;
3924 attr.dw_attr_val.val_class = dw_val_class_data8;
3925 attr.dw_attr_val.val_entry = NULL;
3926 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
3927 add_dwarf_attr (die, &attr);
3930 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
3931 dwarf_split_debug_info, address attributes in dies destined for the
3932 final executable have force_direct set to avoid using indexed
3933 references. */
3935 static inline void
3936 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
3937 bool force_direct)
3939 dw_attr_node attr;
3940 char * lbl_id;
3942 lbl_id = xstrdup (lbl_low);
3943 attr.dw_attr = DW_AT_low_pc;
3944 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3945 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3946 if (dwarf_split_debug_info && !force_direct)
3947 attr.dw_attr_val.val_entry
3948 = add_addr_table_entry (lbl_id, ate_kind_label);
3949 else
3950 attr.dw_attr_val.val_entry = NULL;
3951 add_dwarf_attr (die, &attr);
3953 attr.dw_attr = DW_AT_high_pc;
3954 if (dwarf_version < 4)
3955 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3956 else
3957 attr.dw_attr_val.val_class = dw_val_class_high_pc;
3958 lbl_id = xstrdup (lbl_high);
3959 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3960 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
3961 && dwarf_split_debug_info && !force_direct)
3962 attr.dw_attr_val.val_entry
3963 = add_addr_table_entry (lbl_id, ate_kind_label);
3964 else
3965 attr.dw_attr_val.val_entry = NULL;
3966 add_dwarf_attr (die, &attr);
3969 /* Hash and equality functions for debug_str_hash. */
3971 hashval_t
3972 indirect_string_hasher::hash (indirect_string_node *x)
3974 return htab_hash_string (x->str);
3977 bool
3978 indirect_string_hasher::equal (indirect_string_node *x1, const char *x2)
3980 return strcmp (x1->str, x2) == 0;
3983 /* Add STR to the given string hash table. */
3985 static struct indirect_string_node *
3986 find_AT_string_in_table (const char *str,
3987 hash_table<indirect_string_hasher> *table)
3989 struct indirect_string_node *node;
3991 indirect_string_node **slot
3992 = table->find_slot_with_hash (str, htab_hash_string (str), INSERT);
3993 if (*slot == NULL)
3995 node = ggc_cleared_alloc<indirect_string_node> ();
3996 node->str = ggc_strdup (str);
3997 *slot = node;
3999 else
4000 node = *slot;
4002 node->refcount++;
4003 return node;
4006 /* Add STR to the indirect string hash table. */
4008 static struct indirect_string_node *
4009 find_AT_string (const char *str)
4011 if (! debug_str_hash)
4012 debug_str_hash = hash_table<indirect_string_hasher>::create_ggc (10);
4014 return find_AT_string_in_table (str, debug_str_hash);
4017 /* Add a string attribute value to a DIE. */
4019 static inline void
4020 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4022 dw_attr_node attr;
4023 struct indirect_string_node *node;
4025 node = find_AT_string (str);
4027 attr.dw_attr = attr_kind;
4028 attr.dw_attr_val.val_class = dw_val_class_str;
4029 attr.dw_attr_val.val_entry = NULL;
4030 attr.dw_attr_val.v.val_str = node;
4031 add_dwarf_attr (die, &attr);
4034 static inline const char *
4035 AT_string (dw_attr_ref a)
4037 gcc_assert (a && AT_class (a) == dw_val_class_str);
4038 return a->dw_attr_val.v.val_str->str;
4041 /* Call this function directly to bypass AT_string_form's logic to put
4042 the string inline in the die. */
4044 static void
4045 set_indirect_string (struct indirect_string_node *node)
4047 char label[32];
4048 /* Already indirect is a no op. */
4049 if (node->form == DW_FORM_strp || node->form == DW_FORM_GNU_str_index)
4051 gcc_assert (node->label);
4052 return;
4054 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4055 ++dw2_string_counter;
4056 node->label = xstrdup (label);
4058 if (!dwarf_split_debug_info)
4060 node->form = DW_FORM_strp;
4061 node->index = NOT_INDEXED;
4063 else
4065 node->form = DW_FORM_GNU_str_index;
4066 node->index = NO_INDEX_ASSIGNED;
4070 /* Find out whether a string should be output inline in DIE
4071 or out-of-line in .debug_str section. */
4073 static enum dwarf_form
4074 find_string_form (struct indirect_string_node *node)
4076 unsigned int len;
4078 if (node->form)
4079 return node->form;
4081 len = strlen (node->str) + 1;
4083 /* If the string is shorter or equal to the size of the reference, it is
4084 always better to put it inline. */
4085 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4086 return node->form = DW_FORM_string;
4088 /* If we cannot expect the linker to merge strings in .debug_str
4089 section, only put it into .debug_str if it is worth even in this
4090 single module. */
4091 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4092 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4093 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
4094 return node->form = DW_FORM_string;
4096 set_indirect_string (node);
4098 return node->form;
4101 /* Find out whether the string referenced from the attribute should be
4102 output inline in DIE or out-of-line in .debug_str section. */
4104 static enum dwarf_form
4105 AT_string_form (dw_attr_ref a)
4107 gcc_assert (a && AT_class (a) == dw_val_class_str);
4108 return find_string_form (a->dw_attr_val.v.val_str);
4111 /* Add a DIE reference attribute value to a DIE. */
4113 static inline void
4114 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4116 dw_attr_node attr;
4118 #ifdef ENABLE_CHECKING
4119 gcc_assert (targ_die != NULL);
4120 #else
4121 /* With LTO we can end up trying to reference something we didn't create
4122 a DIE for. Avoid crashing later on a NULL referenced DIE. */
4123 if (targ_die == NULL)
4124 return;
4125 #endif
4127 attr.dw_attr = attr_kind;
4128 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4129 attr.dw_attr_val.val_entry = NULL;
4130 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4131 attr.dw_attr_val.v.val_die_ref.external = 0;
4132 add_dwarf_attr (die, &attr);
4135 /* Change DIE reference REF to point to NEW_DIE instead. */
4137 static inline void
4138 change_AT_die_ref (dw_attr_ref ref, dw_die_ref new_die)
4140 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4141 ref->dw_attr_val.v.val_die_ref.die = new_die;
4142 ref->dw_attr_val.v.val_die_ref.external = 0;
4145 /* Add an AT_specification attribute to a DIE, and also make the back
4146 pointer from the specification to the definition. */
4148 static inline void
4149 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4151 add_AT_die_ref (die, DW_AT_specification, targ_die);
4152 gcc_assert (!targ_die->die_definition);
4153 targ_die->die_definition = die;
4156 static inline dw_die_ref
4157 AT_ref (dw_attr_ref a)
4159 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4160 return a->dw_attr_val.v.val_die_ref.die;
4163 static inline int
4164 AT_ref_external (dw_attr_ref a)
4166 if (a && AT_class (a) == dw_val_class_die_ref)
4167 return a->dw_attr_val.v.val_die_ref.external;
4169 return 0;
4172 static inline void
4173 set_AT_ref_external (dw_attr_ref a, int i)
4175 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4176 a->dw_attr_val.v.val_die_ref.external = i;
4179 /* Add an FDE reference attribute value to a DIE. */
4181 static inline void
4182 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4184 dw_attr_node attr;
4186 attr.dw_attr = attr_kind;
4187 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4188 attr.dw_attr_val.val_entry = NULL;
4189 attr.dw_attr_val.v.val_fde_index = targ_fde;
4190 add_dwarf_attr (die, &attr);
4193 /* Add a location description attribute value to a DIE. */
4195 static inline void
4196 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4198 dw_attr_node attr;
4200 attr.dw_attr = attr_kind;
4201 attr.dw_attr_val.val_class = dw_val_class_loc;
4202 attr.dw_attr_val.val_entry = NULL;
4203 attr.dw_attr_val.v.val_loc = loc;
4204 add_dwarf_attr (die, &attr);
4207 static inline dw_loc_descr_ref
4208 AT_loc (dw_attr_ref a)
4210 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4211 return a->dw_attr_val.v.val_loc;
4214 static inline void
4215 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4217 dw_attr_node attr;
4219 attr.dw_attr = attr_kind;
4220 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4221 attr.dw_attr_val.val_entry = NULL;
4222 attr.dw_attr_val.v.val_loc_list = loc_list;
4223 add_dwarf_attr (die, &attr);
4224 have_location_lists = true;
4227 static inline dw_loc_list_ref
4228 AT_loc_list (dw_attr_ref a)
4230 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4231 return a->dw_attr_val.v.val_loc_list;
4234 static inline dw_loc_list_ref *
4235 AT_loc_list_ptr (dw_attr_ref a)
4237 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4238 return &a->dw_attr_val.v.val_loc_list;
4241 struct addr_hasher : ggc_hasher<addr_table_entry *>
4243 static hashval_t hash (addr_table_entry *);
4244 static bool equal (addr_table_entry *, addr_table_entry *);
4247 /* Table of entries into the .debug_addr section. */
4249 static GTY (()) hash_table<addr_hasher> *addr_index_table;
4251 /* Hash an address_table_entry. */
4253 hashval_t
4254 addr_hasher::hash (addr_table_entry *a)
4256 inchash::hash hstate;
4257 switch (a->kind)
4259 case ate_kind_rtx:
4260 hstate.add_int (0);
4261 break;
4262 case ate_kind_rtx_dtprel:
4263 hstate.add_int (1);
4264 break;
4265 case ate_kind_label:
4266 return htab_hash_string (a->addr.label);
4267 default:
4268 gcc_unreachable ();
4270 inchash::add_rtx (a->addr.rtl, hstate);
4271 return hstate.end ();
4274 /* Determine equality for two address_table_entries. */
4276 bool
4277 addr_hasher::equal (addr_table_entry *a1, addr_table_entry *a2)
4279 if (a1->kind != a2->kind)
4280 return 0;
4281 switch (a1->kind)
4283 case ate_kind_rtx:
4284 case ate_kind_rtx_dtprel:
4285 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4286 case ate_kind_label:
4287 return strcmp (a1->addr.label, a2->addr.label) == 0;
4288 default:
4289 gcc_unreachable ();
4293 /* Initialize an addr_table_entry. */
4295 void
4296 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4298 e->kind = kind;
4299 switch (kind)
4301 case ate_kind_rtx:
4302 case ate_kind_rtx_dtprel:
4303 e->addr.rtl = (rtx) addr;
4304 break;
4305 case ate_kind_label:
4306 e->addr.label = (char *) addr;
4307 break;
4309 e->refcount = 0;
4310 e->index = NO_INDEX_ASSIGNED;
4313 /* Add attr to the address table entry to the table. Defer setting an
4314 index until output time. */
4316 static addr_table_entry *
4317 add_addr_table_entry (void *addr, enum ate_kind kind)
4319 addr_table_entry *node;
4320 addr_table_entry finder;
4322 gcc_assert (dwarf_split_debug_info);
4323 if (! addr_index_table)
4324 addr_index_table = hash_table<addr_hasher>::create_ggc (10);
4325 init_addr_table_entry (&finder, kind, addr);
4326 addr_table_entry **slot = addr_index_table->find_slot (&finder, INSERT);
4328 if (*slot == HTAB_EMPTY_ENTRY)
4330 node = ggc_cleared_alloc<addr_table_entry> ();
4331 init_addr_table_entry (node, kind, addr);
4332 *slot = node;
4334 else
4335 node = *slot;
4337 node->refcount++;
4338 return node;
4341 /* Remove an entry from the addr table by decrementing its refcount.
4342 Strictly, decrementing the refcount would be enough, but the
4343 assertion that the entry is actually in the table has found
4344 bugs. */
4346 static void
4347 remove_addr_table_entry (addr_table_entry *entry)
4349 gcc_assert (dwarf_split_debug_info && addr_index_table);
4350 /* After an index is assigned, the table is frozen. */
4351 gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
4352 entry->refcount--;
4355 /* Given a location list, remove all addresses it refers to from the
4356 address_table. */
4358 static void
4359 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4361 for (; descr; descr = descr->dw_loc_next)
4362 if (descr->dw_loc_oprnd1.val_entry != NULL)
4364 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4365 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4369 /* A helper function for dwarf2out_finish called through
4370 htab_traverse. Assign an addr_table_entry its index. All entries
4371 must be collected into the table when this function is called,
4372 because the indexing code relies on htab_traverse to traverse nodes
4373 in the same order for each run. */
4376 index_addr_table_entry (addr_table_entry **h, unsigned int *index)
4378 addr_table_entry *node = *h;
4380 /* Don't index unreferenced nodes. */
4381 if (node->refcount == 0)
4382 return 1;
4384 gcc_assert (node->index == NO_INDEX_ASSIGNED);
4385 node->index = *index;
4386 *index += 1;
4388 return 1;
4391 /* Add an address constant attribute value to a DIE. When using
4392 dwarf_split_debug_info, address attributes in dies destined for the
4393 final executable should be direct references--setting the parameter
4394 force_direct ensures this behavior. */
4396 static inline void
4397 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4398 bool force_direct)
4400 dw_attr_node attr;
4402 attr.dw_attr = attr_kind;
4403 attr.dw_attr_val.val_class = dw_val_class_addr;
4404 attr.dw_attr_val.v.val_addr = addr;
4405 if (dwarf_split_debug_info && !force_direct)
4406 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4407 else
4408 attr.dw_attr_val.val_entry = NULL;
4409 add_dwarf_attr (die, &attr);
4412 /* Get the RTX from to an address DIE attribute. */
4414 static inline rtx
4415 AT_addr (dw_attr_ref a)
4417 gcc_assert (a && AT_class (a) == dw_val_class_addr);
4418 return a->dw_attr_val.v.val_addr;
4421 /* Add a file attribute value to a DIE. */
4423 static inline void
4424 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4425 struct dwarf_file_data *fd)
4427 dw_attr_node attr;
4429 attr.dw_attr = attr_kind;
4430 attr.dw_attr_val.val_class = dw_val_class_file;
4431 attr.dw_attr_val.val_entry = NULL;
4432 attr.dw_attr_val.v.val_file = fd;
4433 add_dwarf_attr (die, &attr);
4436 /* Get the dwarf_file_data from a file DIE attribute. */
4438 static inline struct dwarf_file_data *
4439 AT_file (dw_attr_ref a)
4441 gcc_assert (a && AT_class (a) == dw_val_class_file);
4442 return a->dw_attr_val.v.val_file;
4445 /* Add a vms delta attribute value to a DIE. */
4447 static inline void
4448 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4449 const char *lbl1, const char *lbl2)
4451 dw_attr_node attr;
4453 attr.dw_attr = attr_kind;
4454 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4455 attr.dw_attr_val.val_entry = NULL;
4456 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4457 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4458 add_dwarf_attr (die, &attr);
4461 /* Add a label identifier attribute value to a DIE. */
4463 static inline void
4464 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4465 const char *lbl_id)
4467 dw_attr_node attr;
4469 attr.dw_attr = attr_kind;
4470 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4471 attr.dw_attr_val.val_entry = NULL;
4472 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4473 if (dwarf_split_debug_info)
4474 attr.dw_attr_val.val_entry
4475 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4476 ate_kind_label);
4477 add_dwarf_attr (die, &attr);
4480 /* Add a section offset attribute value to a DIE, an offset into the
4481 debug_line section. */
4483 static inline void
4484 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4485 const char *label)
4487 dw_attr_node attr;
4489 attr.dw_attr = attr_kind;
4490 attr.dw_attr_val.val_class = dw_val_class_lineptr;
4491 attr.dw_attr_val.val_entry = NULL;
4492 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4493 add_dwarf_attr (die, &attr);
4496 /* Add a section offset attribute value to a DIE, an offset into the
4497 debug_macinfo section. */
4499 static inline void
4500 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4501 const char *label)
4503 dw_attr_node attr;
4505 attr.dw_attr = attr_kind;
4506 attr.dw_attr_val.val_class = dw_val_class_macptr;
4507 attr.dw_attr_val.val_entry = NULL;
4508 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4509 add_dwarf_attr (die, &attr);
4512 /* Add an offset attribute value to a DIE. */
4514 static inline void
4515 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4516 unsigned HOST_WIDE_INT offset)
4518 dw_attr_node attr;
4520 attr.dw_attr = attr_kind;
4521 attr.dw_attr_val.val_class = dw_val_class_offset;
4522 attr.dw_attr_val.val_entry = NULL;
4523 attr.dw_attr_val.v.val_offset = offset;
4524 add_dwarf_attr (die, &attr);
4527 /* Add a range_list attribute value to a DIE. When using
4528 dwarf_split_debug_info, address attributes in dies destined for the
4529 final executable should be direct references--setting the parameter
4530 force_direct ensures this behavior. */
4532 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4533 #define RELOCATED_OFFSET (NULL)
4535 static void
4536 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4537 long unsigned int offset, bool force_direct)
4539 dw_attr_node attr;
4541 attr.dw_attr = attr_kind;
4542 attr.dw_attr_val.val_class = dw_val_class_range_list;
4543 /* For the range_list attribute, use val_entry to store whether the
4544 offset should follow split-debug-info or normal semantics. This
4545 value is read in output_range_list_offset. */
4546 if (dwarf_split_debug_info && !force_direct)
4547 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4548 else
4549 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4550 attr.dw_attr_val.v.val_offset = offset;
4551 add_dwarf_attr (die, &attr);
4554 /* Return the start label of a delta attribute. */
4556 static inline const char *
4557 AT_vms_delta1 (dw_attr_ref a)
4559 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4560 return a->dw_attr_val.v.val_vms_delta.lbl1;
4563 /* Return the end label of a delta attribute. */
4565 static inline const char *
4566 AT_vms_delta2 (dw_attr_ref a)
4568 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4569 return a->dw_attr_val.v.val_vms_delta.lbl2;
4572 static inline const char *
4573 AT_lbl (dw_attr_ref a)
4575 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4576 || AT_class (a) == dw_val_class_lineptr
4577 || AT_class (a) == dw_val_class_macptr
4578 || AT_class (a) == dw_val_class_high_pc));
4579 return a->dw_attr_val.v.val_lbl_id;
4582 /* Get the attribute of type attr_kind. */
4584 static dw_attr_ref
4585 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4587 dw_attr_ref a;
4588 unsigned ix;
4589 dw_die_ref spec = NULL;
4591 if (! die)
4592 return NULL;
4594 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4595 if (a->dw_attr == attr_kind)
4596 return a;
4597 else if (a->dw_attr == DW_AT_specification
4598 || a->dw_attr == DW_AT_abstract_origin)
4599 spec = AT_ref (a);
4601 if (spec)
4602 return get_AT (spec, attr_kind);
4604 return NULL;
4607 /* Returns the parent of the declaration of DIE. */
4609 static dw_die_ref
4610 get_die_parent (dw_die_ref die)
4612 dw_die_ref t;
4614 if (!die)
4615 return NULL;
4617 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4618 || (t = get_AT_ref (die, DW_AT_specification)))
4619 die = t;
4621 return die->die_parent;
4624 /* Return the "low pc" attribute value, typically associated with a subprogram
4625 DIE. Return null if the "low pc" attribute is either not present, or if it
4626 cannot be represented as an assembler label identifier. */
4628 static inline const char *
4629 get_AT_low_pc (dw_die_ref die)
4631 dw_attr_ref a = get_AT (die, DW_AT_low_pc);
4633 return a ? AT_lbl (a) : NULL;
4636 /* Return the "high pc" attribute value, typically associated with a subprogram
4637 DIE. Return null if the "high pc" attribute is either not present, or if it
4638 cannot be represented as an assembler label identifier. */
4640 static inline const char *
4641 get_AT_hi_pc (dw_die_ref die)
4643 dw_attr_ref a = get_AT (die, DW_AT_high_pc);
4645 return a ? AT_lbl (a) : NULL;
4648 /* Return the value of the string attribute designated by ATTR_KIND, or
4649 NULL if it is not present. */
4651 static inline const char *
4652 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4654 dw_attr_ref a = get_AT (die, attr_kind);
4656 return a ? AT_string (a) : NULL;
4659 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4660 if it is not present. */
4662 static inline int
4663 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4665 dw_attr_ref a = get_AT (die, attr_kind);
4667 return a ? AT_flag (a) : 0;
4670 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4671 if it is not present. */
4673 static inline unsigned
4674 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4676 dw_attr_ref a = get_AT (die, attr_kind);
4678 return a ? AT_unsigned (a) : 0;
4681 static inline dw_die_ref
4682 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4684 dw_attr_ref a = get_AT (die, attr_kind);
4686 return a ? AT_ref (a) : NULL;
4689 static inline struct dwarf_file_data *
4690 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4692 dw_attr_ref a = get_AT (die, attr_kind);
4694 return a ? AT_file (a) : NULL;
4697 /* Return TRUE if the language is C++. */
4699 static inline bool
4700 is_cxx (void)
4702 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4704 return (lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus
4705 || lang == DW_LANG_C_plus_plus_11 || lang == DW_LANG_C_plus_plus_14);
4708 /* Return TRUE if the language is Java. */
4710 static inline bool
4711 is_java (void)
4713 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4715 return lang == DW_LANG_Java;
4718 /* Return TRUE if the language is Fortran. */
4720 static inline bool
4721 is_fortran (void)
4723 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4725 return (lang == DW_LANG_Fortran77
4726 || lang == DW_LANG_Fortran90
4727 || lang == DW_LANG_Fortran95
4728 || lang == DW_LANG_Fortran03
4729 || lang == DW_LANG_Fortran08);
4732 /* Return TRUE if the language is Ada. */
4734 static inline bool
4735 is_ada (void)
4737 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4739 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4742 /* Remove the specified attribute if present. Return TRUE if removal
4743 was successful. */
4745 static bool
4746 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4748 dw_attr_ref a;
4749 unsigned ix;
4751 if (! die)
4752 return false;
4754 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4755 if (a->dw_attr == attr_kind)
4757 if (AT_class (a) == dw_val_class_str)
4758 if (a->dw_attr_val.v.val_str->refcount)
4759 a->dw_attr_val.v.val_str->refcount--;
4761 /* vec::ordered_remove should help reduce the number of abbrevs
4762 that are needed. */
4763 die->die_attr->ordered_remove (ix);
4764 return true;
4766 return false;
4769 /* Remove CHILD from its parent. PREV must have the property that
4770 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
4772 static void
4773 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4775 gcc_assert (child->die_parent == prev->die_parent);
4776 gcc_assert (prev->die_sib == child);
4777 if (prev == child)
4779 gcc_assert (child->die_parent->die_child == child);
4780 prev = NULL;
4782 else
4783 prev->die_sib = child->die_sib;
4784 if (child->die_parent->die_child == child)
4785 child->die_parent->die_child = prev;
4788 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
4789 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
4791 static void
4792 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4794 dw_die_ref parent = old_child->die_parent;
4796 gcc_assert (parent == prev->die_parent);
4797 gcc_assert (prev->die_sib == old_child);
4799 new_child->die_parent = parent;
4800 if (prev == old_child)
4802 gcc_assert (parent->die_child == old_child);
4803 new_child->die_sib = new_child;
4805 else
4807 prev->die_sib = new_child;
4808 new_child->die_sib = old_child->die_sib;
4810 if (old_child->die_parent->die_child == old_child)
4811 old_child->die_parent->die_child = new_child;
4814 /* Move all children from OLD_PARENT to NEW_PARENT. */
4816 static void
4817 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4819 dw_die_ref c;
4820 new_parent->die_child = old_parent->die_child;
4821 old_parent->die_child = NULL;
4822 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4825 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
4826 matches TAG. */
4828 static void
4829 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4831 dw_die_ref c;
4833 c = die->die_child;
4834 if (c) do {
4835 dw_die_ref prev = c;
4836 c = c->die_sib;
4837 while (c->die_tag == tag)
4839 remove_child_with_prev (c, prev);
4840 c->die_parent = NULL;
4841 /* Might have removed every child. */
4842 if (c == c->die_sib)
4843 return;
4844 c = c->die_sib;
4846 } while (c != die->die_child);
4849 /* Add a CHILD_DIE as the last child of DIE. */
4851 static void
4852 add_child_die (dw_die_ref die, dw_die_ref child_die)
4854 /* FIXME this should probably be an assert. */
4855 if (! die || ! child_die)
4856 return;
4857 gcc_assert (die != child_die);
4859 child_die->die_parent = die;
4860 if (die->die_child)
4862 child_die->die_sib = die->die_child->die_sib;
4863 die->die_child->die_sib = child_die;
4865 else
4866 child_die->die_sib = child_die;
4867 die->die_child = child_die;
4870 /* Unassociate CHILD from its parent, and make its parent be
4871 NEW_PARENT. */
4873 static void
4874 reparent_child (dw_die_ref child, dw_die_ref new_parent)
4876 for (dw_die_ref p = child->die_parent->die_child; ; p = p->die_sib)
4877 if (p->die_sib == child)
4879 remove_child_with_prev (child, p);
4880 break;
4882 add_child_die (new_parent, child);
4885 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
4886 is the specification, to the end of PARENT's list of children.
4887 This is done by removing and re-adding it. */
4889 static void
4890 splice_child_die (dw_die_ref parent, dw_die_ref child)
4892 /* We want the declaration DIE from inside the class, not the
4893 specification DIE at toplevel. */
4894 if (child->die_parent != parent)
4896 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
4898 if (tmp)
4899 child = tmp;
4902 gcc_assert (child->die_parent == parent
4903 || (child->die_parent
4904 == get_AT_ref (parent, DW_AT_specification)));
4906 reparent_child (child, parent);
4909 /* Create and return a new die with a parent of PARENT_DIE. If
4910 PARENT_DIE is NULL, the new DIE is placed in limbo and an
4911 associated tree T must be supplied to determine parenthood
4912 later. */
4914 static inline dw_die_ref
4915 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
4917 dw_die_ref die = ggc_cleared_alloc<die_node> ();
4919 die->die_tag = tag_value;
4921 if (parent_die != NULL)
4922 add_child_die (parent_die, die);
4923 else
4925 limbo_die_node *limbo_node;
4927 /* No DIEs created after early dwarf should end up in limbo,
4928 because the limbo list should not persist past LTO
4929 streaming. */
4930 if (tag_value != DW_TAG_compile_unit
4931 /* These are allowed because they're generated while
4932 breaking out COMDAT units late. */
4933 && tag_value != DW_TAG_type_unit
4934 && !early_dwarf
4935 /* Allow nested functions to live in limbo because they will
4936 only temporarily live there, as decls_for_scope will fix
4937 them up. */
4938 && (TREE_CODE (t) != FUNCTION_DECL
4939 || !decl_function_context (t))
4940 /* Same as nested functions above but for types. Types that
4941 are local to a function will be fixed in
4942 decls_for_scope. */
4943 && (!RECORD_OR_UNION_TYPE_P (t)
4944 || !TYPE_CONTEXT (t)
4945 || TREE_CODE (TYPE_CONTEXT (t)) != FUNCTION_DECL)
4946 /* FIXME debug-early: Allow late limbo DIE creation for LTO,
4947 especially in the ltrans stage, but once we implement LTO
4948 dwarf streaming, we should remove this exception. */
4949 && !in_lto_p)
4951 fprintf (stderr, "symbol ended up in limbo too late:");
4952 debug_generic_stmt (t);
4953 gcc_unreachable ();
4956 limbo_node = ggc_cleared_alloc<limbo_die_node> ();
4957 limbo_node->die = die;
4958 limbo_node->created_for = t;
4959 limbo_node->next = limbo_die_list;
4960 limbo_die_list = limbo_node;
4963 return die;
4966 /* Return the DIE associated with the given type specifier. */
4968 static inline dw_die_ref
4969 lookup_type_die (tree type)
4971 return TYPE_SYMTAB_DIE (type);
4974 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
4975 anonymous type named by the typedef TYPE_DIE, return the DIE of the
4976 anonymous type instead the one of the naming typedef. */
4978 static inline dw_die_ref
4979 strip_naming_typedef (tree type, dw_die_ref type_die)
4981 if (type
4982 && TREE_CODE (type) == RECORD_TYPE
4983 && type_die
4984 && type_die->die_tag == DW_TAG_typedef
4985 && is_naming_typedef_decl (TYPE_NAME (type)))
4986 type_die = get_AT_ref (type_die, DW_AT_type);
4987 return type_die;
4990 /* Like lookup_type_die, but if type is an anonymous type named by a
4991 typedef[1], return the DIE of the anonymous type instead the one of
4992 the naming typedef. This is because in gen_typedef_die, we did
4993 equate the anonymous struct named by the typedef with the DIE of
4994 the naming typedef. So by default, lookup_type_die on an anonymous
4995 struct yields the DIE of the naming typedef.
4997 [1]: Read the comment of is_naming_typedef_decl to learn about what
4998 a naming typedef is. */
5000 static inline dw_die_ref
5001 lookup_type_die_strip_naming_typedef (tree type)
5003 dw_die_ref die = lookup_type_die (type);
5004 return strip_naming_typedef (type, die);
5007 /* Equate a DIE to a given type specifier. */
5009 static inline void
5010 equate_type_number_to_die (tree type, dw_die_ref type_die)
5012 TYPE_SYMTAB_DIE (type) = type_die;
5015 /* Returns a hash value for X (which really is a die_struct). */
5017 inline hashval_t
5018 decl_die_hasher::hash (die_node *x)
5020 return (hashval_t) x->decl_id;
5023 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
5025 inline bool
5026 decl_die_hasher::equal (die_node *x, tree y)
5028 return (x->decl_id == DECL_UID (y));
5031 /* Return the DIE associated with a given declaration. */
5033 static inline dw_die_ref
5034 lookup_decl_die (tree decl)
5036 return decl_die_table->find_with_hash (decl, DECL_UID (decl));
5039 /* Returns a hash value for X (which really is a var_loc_list). */
5041 inline hashval_t
5042 decl_loc_hasher::hash (var_loc_list *x)
5044 return (hashval_t) x->decl_id;
5047 /* Return nonzero if decl_id of var_loc_list X is the same as
5048 UID of decl *Y. */
5050 inline bool
5051 decl_loc_hasher::equal (var_loc_list *x, const_tree y)
5053 return (x->decl_id == DECL_UID (y));
5056 /* Return the var_loc list associated with a given declaration. */
5058 static inline var_loc_list *
5059 lookup_decl_loc (const_tree decl)
5061 if (!decl_loc_table)
5062 return NULL;
5063 return decl_loc_table->find_with_hash (decl, DECL_UID (decl));
5066 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
5068 inline hashval_t
5069 dw_loc_list_hasher::hash (cached_dw_loc_list *x)
5071 return (hashval_t) x->decl_id;
5074 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
5075 UID of decl *Y. */
5077 inline bool
5078 dw_loc_list_hasher::equal (cached_dw_loc_list *x, const_tree y)
5080 return (x->decl_id == DECL_UID (y));
5083 /* Equate a DIE to a particular declaration. */
5085 static void
5086 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5088 unsigned int decl_id = DECL_UID (decl);
5090 *decl_die_table->find_slot_with_hash (decl, decl_id, INSERT) = decl_die;
5091 decl_die->decl_id = decl_id;
5094 /* Return how many bits covers PIECE EXPR_LIST. */
5096 static HOST_WIDE_INT
5097 decl_piece_bitsize (rtx piece)
5099 int ret = (int) GET_MODE (piece);
5100 if (ret)
5101 return ret;
5102 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
5103 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
5104 return INTVAL (XEXP (XEXP (piece, 0), 0));
5107 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
5109 static rtx *
5110 decl_piece_varloc_ptr (rtx piece)
5112 if ((int) GET_MODE (piece))
5113 return &XEXP (piece, 0);
5114 else
5115 return &XEXP (XEXP (piece, 0), 1);
5118 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
5119 Next is the chain of following piece nodes. */
5121 static rtx_expr_list *
5122 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
5124 if (bitsize > 0 && bitsize <= (int) MAX_MACHINE_MODE)
5125 return alloc_EXPR_LIST (bitsize, loc_note, next);
5126 else
5127 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
5128 GEN_INT (bitsize),
5129 loc_note), next);
5132 /* Return rtx that should be stored into loc field for
5133 LOC_NOTE and BITPOS/BITSIZE. */
5135 static rtx
5136 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
5137 HOST_WIDE_INT bitsize)
5139 if (bitsize != -1)
5141 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
5142 if (bitpos != 0)
5143 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
5145 return loc_note;
5148 /* This function either modifies location piece list *DEST in
5149 place (if SRC and INNER is NULL), or copies location piece list
5150 *SRC to *DEST while modifying it. Location BITPOS is modified
5151 to contain LOC_NOTE, any pieces overlapping it are removed resp.
5152 not copied and if needed some padding around it is added.
5153 When modifying in place, DEST should point to EXPR_LIST where
5154 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
5155 to the start of the whole list and INNER points to the EXPR_LIST
5156 where earlier pieces cover PIECE_BITPOS bits. */
5158 static void
5159 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5160 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5161 HOST_WIDE_INT bitsize, rtx loc_note)
5163 HOST_WIDE_INT diff;
5164 bool copy = inner != NULL;
5166 if (copy)
5168 /* First copy all nodes preceding the current bitpos. */
5169 while (src != inner)
5171 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5172 decl_piece_bitsize (*src), NULL_RTX);
5173 dest = &XEXP (*dest, 1);
5174 src = &XEXP (*src, 1);
5177 /* Add padding if needed. */
5178 if (bitpos != piece_bitpos)
5180 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5181 copy ? NULL_RTX : *dest);
5182 dest = &XEXP (*dest, 1);
5184 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5186 gcc_assert (!copy);
5187 /* A piece with correct bitpos and bitsize already exist,
5188 just update the location for it and return. */
5189 *decl_piece_varloc_ptr (*dest) = loc_note;
5190 return;
5192 /* Add the piece that changed. */
5193 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5194 dest = &XEXP (*dest, 1);
5195 /* Skip over pieces that overlap it. */
5196 diff = bitpos - piece_bitpos + bitsize;
5197 if (!copy)
5198 src = dest;
5199 while (diff > 0 && *src)
5201 rtx piece = *src;
5202 diff -= decl_piece_bitsize (piece);
5203 if (copy)
5204 src = &XEXP (piece, 1);
5205 else
5207 *src = XEXP (piece, 1);
5208 free_EXPR_LIST_node (piece);
5211 /* Add padding if needed. */
5212 if (diff < 0 && *src)
5214 if (!copy)
5215 dest = src;
5216 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5217 dest = &XEXP (*dest, 1);
5219 if (!copy)
5220 return;
5221 /* Finally copy all nodes following it. */
5222 while (*src)
5224 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5225 decl_piece_bitsize (*src), NULL_RTX);
5226 dest = &XEXP (*dest, 1);
5227 src = &XEXP (*src, 1);
5231 /* Add a variable location node to the linked list for DECL. */
5233 static struct var_loc_node *
5234 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5236 unsigned int decl_id;
5237 var_loc_list *temp;
5238 struct var_loc_node *loc = NULL;
5239 HOST_WIDE_INT bitsize = -1, bitpos = -1;
5241 if (TREE_CODE (decl) == VAR_DECL
5242 && DECL_HAS_DEBUG_EXPR_P (decl))
5244 tree realdecl = DECL_DEBUG_EXPR (decl);
5245 if (handled_component_p (realdecl)
5246 || (TREE_CODE (realdecl) == MEM_REF
5247 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5249 HOST_WIDE_INT maxsize;
5250 tree innerdecl;
5251 innerdecl
5252 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
5253 if (!DECL_P (innerdecl)
5254 || DECL_IGNORED_P (innerdecl)
5255 || TREE_STATIC (innerdecl)
5256 || bitsize <= 0
5257 || bitpos + bitsize > 256
5258 || bitsize != maxsize)
5259 return NULL;
5260 decl = innerdecl;
5264 decl_id = DECL_UID (decl);
5265 var_loc_list **slot
5266 = decl_loc_table->find_slot_with_hash (decl, decl_id, INSERT);
5267 if (*slot == NULL)
5269 temp = ggc_cleared_alloc<var_loc_list> ();
5270 temp->decl_id = decl_id;
5271 *slot = temp;
5273 else
5274 temp = *slot;
5276 /* For PARM_DECLs try to keep around the original incoming value,
5277 even if that means we'll emit a zero-range .debug_loc entry. */
5278 if (temp->last
5279 && temp->first == temp->last
5280 && TREE_CODE (decl) == PARM_DECL
5281 && NOTE_P (temp->first->loc)
5282 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5283 && DECL_INCOMING_RTL (decl)
5284 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5285 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5286 == GET_CODE (DECL_INCOMING_RTL (decl))
5287 && prev_real_insn (temp->first->loc) == NULL_RTX
5288 && (bitsize != -1
5289 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5290 NOTE_VAR_LOCATION_LOC (loc_note))
5291 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5292 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5294 loc = ggc_cleared_alloc<var_loc_node> ();
5295 temp->first->next = loc;
5296 temp->last = loc;
5297 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5299 else if (temp->last)
5301 struct var_loc_node *last = temp->last, *unused = NULL;
5302 rtx *piece_loc = NULL, last_loc_note;
5303 HOST_WIDE_INT piece_bitpos = 0;
5304 if (last->next)
5306 last = last->next;
5307 gcc_assert (last->next == NULL);
5309 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5311 piece_loc = &last->loc;
5314 HOST_WIDE_INT cur_bitsize = decl_piece_bitsize (*piece_loc);
5315 if (piece_bitpos + cur_bitsize > bitpos)
5316 break;
5317 piece_bitpos += cur_bitsize;
5318 piece_loc = &XEXP (*piece_loc, 1);
5320 while (*piece_loc);
5322 /* TEMP->LAST here is either pointer to the last but one or
5323 last element in the chained list, LAST is pointer to the
5324 last element. */
5325 if (label && strcmp (last->label, label) == 0)
5327 /* For SRA optimized variables if there weren't any real
5328 insns since last note, just modify the last node. */
5329 if (piece_loc != NULL)
5331 adjust_piece_list (piece_loc, NULL, NULL,
5332 bitpos, piece_bitpos, bitsize, loc_note);
5333 return NULL;
5335 /* If the last note doesn't cover any instructions, remove it. */
5336 if (temp->last != last)
5338 temp->last->next = NULL;
5339 unused = last;
5340 last = temp->last;
5341 gcc_assert (strcmp (last->label, label) != 0);
5343 else
5345 gcc_assert (temp->first == temp->last
5346 || (temp->first->next == temp->last
5347 && TREE_CODE (decl) == PARM_DECL));
5348 memset (temp->last, '\0', sizeof (*temp->last));
5349 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5350 return temp->last;
5353 if (bitsize == -1 && NOTE_P (last->loc))
5354 last_loc_note = last->loc;
5355 else if (piece_loc != NULL
5356 && *piece_loc != NULL_RTX
5357 && piece_bitpos == bitpos
5358 && decl_piece_bitsize (*piece_loc) == bitsize)
5359 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5360 else
5361 last_loc_note = NULL_RTX;
5362 /* If the current location is the same as the end of the list,
5363 and either both or neither of the locations is uninitialized,
5364 we have nothing to do. */
5365 if (last_loc_note == NULL_RTX
5366 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5367 NOTE_VAR_LOCATION_LOC (loc_note)))
5368 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5369 != NOTE_VAR_LOCATION_STATUS (loc_note))
5370 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5371 == VAR_INIT_STATUS_UNINITIALIZED)
5372 || (NOTE_VAR_LOCATION_STATUS (loc_note)
5373 == VAR_INIT_STATUS_UNINITIALIZED))))
5375 /* Add LOC to the end of list and update LAST. If the last
5376 element of the list has been removed above, reuse its
5377 memory for the new node, otherwise allocate a new one. */
5378 if (unused)
5380 loc = unused;
5381 memset (loc, '\0', sizeof (*loc));
5383 else
5384 loc = ggc_cleared_alloc<var_loc_node> ();
5385 if (bitsize == -1 || piece_loc == NULL)
5386 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5387 else
5388 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5389 bitpos, piece_bitpos, bitsize, loc_note);
5390 last->next = loc;
5391 /* Ensure TEMP->LAST will point either to the new last but one
5392 element of the chain, or to the last element in it. */
5393 if (last != temp->last)
5394 temp->last = last;
5396 else if (unused)
5397 ggc_free (unused);
5399 else
5401 loc = ggc_cleared_alloc<var_loc_node> ();
5402 temp->first = loc;
5403 temp->last = loc;
5404 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5406 return loc;
5409 /* Keep track of the number of spaces used to indent the
5410 output of the debugging routines that print the structure of
5411 the DIE internal representation. */
5412 static int print_indent;
5414 /* Indent the line the number of spaces given by print_indent. */
5416 static inline void
5417 print_spaces (FILE *outfile)
5419 fprintf (outfile, "%*s", print_indent, "");
5422 /* Print a type signature in hex. */
5424 static inline void
5425 print_signature (FILE *outfile, char *sig)
5427 int i;
5429 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5430 fprintf (outfile, "%02x", sig[i] & 0xff);
5433 static void print_loc_descr (dw_loc_descr_ref, FILE *);
5435 /* Print the value associated to the VAL DWARF value node to OUTFILE. If
5436 RECURSE, output location descriptor operations. */
5438 static void
5439 print_dw_val (dw_val_node *val, bool recurse, FILE *outfile)
5441 switch (val->val_class)
5443 case dw_val_class_addr:
5444 fprintf (outfile, "address");
5445 break;
5446 case dw_val_class_offset:
5447 fprintf (outfile, "offset");
5448 break;
5449 case dw_val_class_loc:
5450 fprintf (outfile, "location descriptor");
5451 if (val->v.val_loc == NULL)
5452 fprintf (outfile, " -> <null>\n");
5453 else if (recurse)
5455 fprintf (outfile, ":\n");
5456 print_indent += 4;
5457 print_loc_descr (val->v.val_loc, outfile);
5458 print_indent -= 4;
5460 else
5461 fprintf (outfile, " (%p)\n", (void *) val->v.val_loc);
5462 break;
5463 case dw_val_class_loc_list:
5464 fprintf (outfile, "location list -> label:%s",
5465 val->v.val_loc_list->ll_symbol);
5466 break;
5467 case dw_val_class_range_list:
5468 fprintf (outfile, "range list");
5469 break;
5470 case dw_val_class_const:
5471 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, val->v.val_int);
5472 break;
5473 case dw_val_class_unsigned_const:
5474 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, val->v.val_unsigned);
5475 break;
5476 case dw_val_class_const_double:
5477 fprintf (outfile, "constant (" HOST_WIDE_INT_PRINT_DEC","\
5478 HOST_WIDE_INT_PRINT_UNSIGNED")",
5479 val->v.val_double.high,
5480 val->v.val_double.low);
5481 break;
5482 case dw_val_class_wide_int:
5484 int i = val->v.val_wide->get_len ();
5485 fprintf (outfile, "constant (");
5486 gcc_assert (i > 0);
5487 if (val->v.val_wide->elt (i - 1) == 0)
5488 fprintf (outfile, "0x");
5489 fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
5490 val->v.val_wide->elt (--i));
5491 while (--i >= 0)
5492 fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
5493 val->v.val_wide->elt (i));
5494 fprintf (outfile, ")");
5495 break;
5497 case dw_val_class_vec:
5498 fprintf (outfile, "floating-point or vector constant");
5499 break;
5500 case dw_val_class_flag:
5501 fprintf (outfile, "%u", val->v.val_flag);
5502 break;
5503 case dw_val_class_die_ref:
5504 if (val->v.val_die_ref.die != NULL)
5506 dw_die_ref die = val->v.val_die_ref.die;
5508 if (die->comdat_type_p)
5510 fprintf (outfile, "die -> signature: ");
5511 print_signature (outfile,
5512 die->die_id.die_type_node->signature);
5514 else if (die->die_id.die_symbol)
5515 fprintf (outfile, "die -> label: %s", die->die_id.die_symbol);
5516 else
5517 fprintf (outfile, "die -> %ld", die->die_offset);
5518 fprintf (outfile, " (%p)", (void *) die);
5520 else
5521 fprintf (outfile, "die -> <null>");
5522 break;
5523 case dw_val_class_vms_delta:
5524 fprintf (outfile, "delta: @slotcount(%s-%s)",
5525 val->v.val_vms_delta.lbl2, val->v.val_vms_delta.lbl1);
5526 break;
5527 case dw_val_class_lbl_id:
5528 case dw_val_class_lineptr:
5529 case dw_val_class_macptr:
5530 case dw_val_class_high_pc:
5531 fprintf (outfile, "label: %s", val->v.val_lbl_id);
5532 break;
5533 case dw_val_class_str:
5534 if (val->v.val_str->str != NULL)
5535 fprintf (outfile, "\"%s\"", val->v.val_str->str);
5536 else
5537 fprintf (outfile, "<null>");
5538 break;
5539 case dw_val_class_file:
5540 fprintf (outfile, "\"%s\" (%d)", val->v.val_file->filename,
5541 val->v.val_file->emitted_number);
5542 break;
5543 case dw_val_class_data8:
5545 int i;
5547 for (i = 0; i < 8; i++)
5548 fprintf (outfile, "%02x", val->v.val_data8[i]);
5549 break;
5551 default:
5552 break;
5556 /* Likewise, for a DIE attribute. */
5558 static void
5559 print_attribute (dw_attr_ref a, bool recurse, FILE *outfile)
5561 print_dw_val (&a->dw_attr_val, recurse, outfile);
5565 /* Print the list of operands in the LOC location description to OUTFILE. This
5566 routine is a debugging aid only. */
5568 static void
5569 print_loc_descr (dw_loc_descr_ref loc, FILE *outfile)
5571 dw_loc_descr_ref l = loc;
5573 if (loc == NULL)
5575 print_spaces (outfile);
5576 fprintf (outfile, "<null>\n");
5577 return;
5580 for (l = loc; l != NULL; l = l->dw_loc_next)
5582 print_spaces (outfile);
5583 fprintf (outfile, "(%p) %s",
5584 (void *) l,
5585 dwarf_stack_op_name (l->dw_loc_opc));
5586 if (l->dw_loc_oprnd1.val_class != dw_val_class_none)
5588 fprintf (outfile, " ");
5589 print_dw_val (&l->dw_loc_oprnd1, false, outfile);
5591 if (l->dw_loc_oprnd2.val_class != dw_val_class_none)
5593 fprintf (outfile, ", ");
5594 print_dw_val (&l->dw_loc_oprnd2, false, outfile);
5596 fprintf (outfile, "\n");
5600 /* Print the information associated with a given DIE, and its children.
5601 This routine is a debugging aid only. */
5603 static void
5604 print_die (dw_die_ref die, FILE *outfile)
5606 dw_attr_ref a;
5607 dw_die_ref c;
5608 unsigned ix;
5610 print_spaces (outfile);
5611 fprintf (outfile, "DIE %4ld: %s (%p)\n",
5612 die->die_offset, dwarf_tag_name (die->die_tag),
5613 (void*) die);
5614 print_spaces (outfile);
5615 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5616 fprintf (outfile, " offset: %ld", die->die_offset);
5617 fprintf (outfile, " mark: %d\n", die->die_mark);
5619 if (die->comdat_type_p)
5621 print_spaces (outfile);
5622 fprintf (outfile, " signature: ");
5623 print_signature (outfile, die->die_id.die_type_node->signature);
5624 fprintf (outfile, "\n");
5627 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5629 print_spaces (outfile);
5630 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5632 print_attribute (a, true, outfile);
5633 fprintf (outfile, "\n");
5636 if (die->die_child != NULL)
5638 print_indent += 4;
5639 FOR_EACH_CHILD (die, c, print_die (c, outfile));
5640 print_indent -= 4;
5642 if (print_indent == 0)
5643 fprintf (outfile, "\n");
5646 /* Print the list of operations in the LOC location description. */
5648 DEBUG_FUNCTION void
5649 debug_dwarf_loc_descr (dw_loc_descr_ref loc)
5651 print_loc_descr (loc, stderr);
5654 /* Print the information collected for a given DIE. */
5656 DEBUG_FUNCTION void
5657 debug_dwarf_die (dw_die_ref die)
5659 print_die (die, stderr);
5662 DEBUG_FUNCTION void
5663 debug (die_struct &ref)
5665 print_die (&ref, stderr);
5668 DEBUG_FUNCTION void
5669 debug (die_struct *ptr)
5671 if (ptr)
5672 debug (*ptr);
5673 else
5674 fprintf (stderr, "<nil>\n");
5678 /* Print all DWARF information collected for the compilation unit.
5679 This routine is a debugging aid only. */
5681 DEBUG_FUNCTION void
5682 debug_dwarf (void)
5684 print_indent = 0;
5685 print_die (comp_unit_die (), stderr);
5688 #ifdef ENABLE_CHECKING
5689 /* Sanity checks on DIEs. */
5691 static void
5692 check_die (dw_die_ref die)
5694 unsigned ix;
5695 dw_attr_ref a;
5696 bool inline_found = false;
5697 int n_location = 0, n_low_pc = 0, n_high_pc = 0, n_artificial = 0;
5698 int n_decl_line = 0, n_decl_file = 0;
5699 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5701 switch (a->dw_attr)
5703 case DW_AT_inline:
5704 if (a->dw_attr_val.v.val_unsigned)
5705 inline_found = true;
5706 break;
5707 case DW_AT_location:
5708 ++n_location;
5709 break;
5710 case DW_AT_low_pc:
5711 ++n_low_pc;
5712 break;
5713 case DW_AT_high_pc:
5714 ++n_high_pc;
5715 break;
5716 case DW_AT_artificial:
5717 ++n_artificial;
5718 break;
5719 case DW_AT_decl_line:
5720 ++n_decl_line;
5721 break;
5722 case DW_AT_decl_file:
5723 ++n_decl_file;
5724 break;
5725 default:
5726 break;
5729 if (n_location > 1 || n_low_pc > 1 || n_high_pc > 1 || n_artificial > 1
5730 || n_decl_line > 1 || n_decl_file > 1)
5732 fprintf (stderr, "Duplicate attributes in DIE:\n");
5733 debug_dwarf_die (die);
5734 gcc_unreachable ();
5736 if (inline_found)
5738 /* A debugging information entry that is a member of an abstract
5739 instance tree [that has DW_AT_inline] should not contain any
5740 attributes which describe aspects of the subroutine which vary
5741 between distinct inlined expansions or distinct out-of-line
5742 expansions. */
5743 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5744 gcc_assert (a->dw_attr != DW_AT_low_pc
5745 && a->dw_attr != DW_AT_high_pc
5746 && a->dw_attr != DW_AT_location
5747 && a->dw_attr != DW_AT_frame_base
5748 && a->dw_attr != DW_AT_GNU_all_call_sites);
5751 #endif
5753 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
5754 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
5755 DIE that marks the start of the DIEs for this include file. */
5757 static dw_die_ref
5758 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5760 const char *filename = get_AT_string (bincl_die, DW_AT_name);
5761 dw_die_ref new_unit = gen_compile_unit_die (filename);
5763 new_unit->die_sib = old_unit;
5764 return new_unit;
5767 /* Close an include-file CU and reopen the enclosing one. */
5769 static dw_die_ref
5770 pop_compile_unit (dw_die_ref old_unit)
5772 dw_die_ref new_unit = old_unit->die_sib;
5774 old_unit->die_sib = NULL;
5775 return new_unit;
5778 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5779 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5780 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5782 /* Calculate the checksum of a location expression. */
5784 static inline void
5785 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5787 int tem;
5788 inchash::hash hstate;
5789 hashval_t hash;
5791 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
5792 CHECKSUM (tem);
5793 hash_loc_operands (loc, hstate);
5794 hash = hstate.end();
5795 CHECKSUM (hash);
5798 /* Calculate the checksum of an attribute. */
5800 static void
5801 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5803 dw_loc_descr_ref loc;
5804 rtx r;
5806 CHECKSUM (at->dw_attr);
5808 /* We don't care that this was compiled with a different compiler
5809 snapshot; if the output is the same, that's what matters. */
5810 if (at->dw_attr == DW_AT_producer)
5811 return;
5813 switch (AT_class (at))
5815 case dw_val_class_const:
5816 CHECKSUM (at->dw_attr_val.v.val_int);
5817 break;
5818 case dw_val_class_unsigned_const:
5819 CHECKSUM (at->dw_attr_val.v.val_unsigned);
5820 break;
5821 case dw_val_class_const_double:
5822 CHECKSUM (at->dw_attr_val.v.val_double);
5823 break;
5824 case dw_val_class_wide_int:
5825 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
5826 get_full_len (*at->dw_attr_val.v.val_wide)
5827 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
5828 break;
5829 case dw_val_class_vec:
5830 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5831 (at->dw_attr_val.v.val_vec.length
5832 * at->dw_attr_val.v.val_vec.elt_size));
5833 break;
5834 case dw_val_class_flag:
5835 CHECKSUM (at->dw_attr_val.v.val_flag);
5836 break;
5837 case dw_val_class_str:
5838 CHECKSUM_STRING (AT_string (at));
5839 break;
5841 case dw_val_class_addr:
5842 r = AT_addr (at);
5843 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5844 CHECKSUM_STRING (XSTR (r, 0));
5845 break;
5847 case dw_val_class_offset:
5848 CHECKSUM (at->dw_attr_val.v.val_offset);
5849 break;
5851 case dw_val_class_loc:
5852 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5853 loc_checksum (loc, ctx);
5854 break;
5856 case dw_val_class_die_ref:
5857 die_checksum (AT_ref (at), ctx, mark);
5858 break;
5860 case dw_val_class_fde_ref:
5861 case dw_val_class_vms_delta:
5862 case dw_val_class_lbl_id:
5863 case dw_val_class_lineptr:
5864 case dw_val_class_macptr:
5865 case dw_val_class_high_pc:
5866 break;
5868 case dw_val_class_file:
5869 CHECKSUM_STRING (AT_file (at)->filename);
5870 break;
5872 case dw_val_class_data8:
5873 CHECKSUM (at->dw_attr_val.v.val_data8);
5874 break;
5876 default:
5877 break;
5881 /* Calculate the checksum of a DIE. */
5883 static void
5884 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5886 dw_die_ref c;
5887 dw_attr_ref a;
5888 unsigned ix;
5890 /* To avoid infinite recursion. */
5891 if (die->die_mark)
5893 CHECKSUM (die->die_mark);
5894 return;
5896 die->die_mark = ++(*mark);
5898 CHECKSUM (die->die_tag);
5900 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5901 attr_checksum (a, ctx, mark);
5903 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
5906 #undef CHECKSUM
5907 #undef CHECKSUM_BLOCK
5908 #undef CHECKSUM_STRING
5910 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
5911 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5912 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5913 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
5914 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
5915 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
5916 #define CHECKSUM_ATTR(FOO) \
5917 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
5919 /* Calculate the checksum of a number in signed LEB128 format. */
5921 static void
5922 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
5924 unsigned char byte;
5925 bool more;
5927 while (1)
5929 byte = (value & 0x7f);
5930 value >>= 7;
5931 more = !((value == 0 && (byte & 0x40) == 0)
5932 || (value == -1 && (byte & 0x40) != 0));
5933 if (more)
5934 byte |= 0x80;
5935 CHECKSUM (byte);
5936 if (!more)
5937 break;
5941 /* Calculate the checksum of a number in unsigned LEB128 format. */
5943 static void
5944 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
5946 while (1)
5948 unsigned char byte = (value & 0x7f);
5949 value >>= 7;
5950 if (value != 0)
5951 /* More bytes to follow. */
5952 byte |= 0x80;
5953 CHECKSUM (byte);
5954 if (value == 0)
5955 break;
5959 /* Checksum the context of the DIE. This adds the names of any
5960 surrounding namespaces or structures to the checksum. */
5962 static void
5963 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
5965 const char *name;
5966 dw_die_ref spec;
5967 int tag = die->die_tag;
5969 if (tag != DW_TAG_namespace
5970 && tag != DW_TAG_structure_type
5971 && tag != DW_TAG_class_type)
5972 return;
5974 name = get_AT_string (die, DW_AT_name);
5976 spec = get_AT_ref (die, DW_AT_specification);
5977 if (spec != NULL)
5978 die = spec;
5980 if (die->die_parent != NULL)
5981 checksum_die_context (die->die_parent, ctx);
5983 CHECKSUM_ULEB128 ('C');
5984 CHECKSUM_ULEB128 (tag);
5985 if (name != NULL)
5986 CHECKSUM_STRING (name);
5989 /* Calculate the checksum of a location expression. */
5991 static inline void
5992 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5994 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
5995 were emitted as a DW_FORM_sdata instead of a location expression. */
5996 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
5998 CHECKSUM_ULEB128 (DW_FORM_sdata);
5999 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
6000 return;
6003 /* Otherwise, just checksum the raw location expression. */
6004 while (loc != NULL)
6006 inchash::hash hstate;
6007 hashval_t hash;
6009 CHECKSUM_ULEB128 (loc->dtprel);
6010 CHECKSUM_ULEB128 (loc->dw_loc_opc);
6011 hash_loc_operands (loc, hstate);
6012 hash = hstate.end ();
6013 CHECKSUM (hash);
6014 loc = loc->dw_loc_next;
6018 /* Calculate the checksum of an attribute. */
6020 static void
6021 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
6022 struct md5_ctx *ctx, int *mark)
6024 dw_loc_descr_ref loc;
6025 rtx r;
6027 if (AT_class (at) == dw_val_class_die_ref)
6029 dw_die_ref target_die = AT_ref (at);
6031 /* For pointer and reference types, we checksum only the (qualified)
6032 name of the target type (if there is a name). For friend entries,
6033 we checksum only the (qualified) name of the target type or function.
6034 This allows the checksum to remain the same whether the target type
6035 is complete or not. */
6036 if ((at->dw_attr == DW_AT_type
6037 && (tag == DW_TAG_pointer_type
6038 || tag == DW_TAG_reference_type
6039 || tag == DW_TAG_rvalue_reference_type
6040 || tag == DW_TAG_ptr_to_member_type))
6041 || (at->dw_attr == DW_AT_friend
6042 && tag == DW_TAG_friend))
6044 dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
6046 if (name_attr != NULL)
6048 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6050 if (decl == NULL)
6051 decl = target_die;
6052 CHECKSUM_ULEB128 ('N');
6053 CHECKSUM_ULEB128 (at->dw_attr);
6054 if (decl->die_parent != NULL)
6055 checksum_die_context (decl->die_parent, ctx);
6056 CHECKSUM_ULEB128 ('E');
6057 CHECKSUM_STRING (AT_string (name_attr));
6058 return;
6062 /* For all other references to another DIE, we check to see if the
6063 target DIE has already been visited. If it has, we emit a
6064 backward reference; if not, we descend recursively. */
6065 if (target_die->die_mark > 0)
6067 CHECKSUM_ULEB128 ('R');
6068 CHECKSUM_ULEB128 (at->dw_attr);
6069 CHECKSUM_ULEB128 (target_die->die_mark);
6071 else
6073 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6075 if (decl == NULL)
6076 decl = target_die;
6077 target_die->die_mark = ++(*mark);
6078 CHECKSUM_ULEB128 ('T');
6079 CHECKSUM_ULEB128 (at->dw_attr);
6080 if (decl->die_parent != NULL)
6081 checksum_die_context (decl->die_parent, ctx);
6082 die_checksum_ordered (target_die, ctx, mark);
6084 return;
6087 CHECKSUM_ULEB128 ('A');
6088 CHECKSUM_ULEB128 (at->dw_attr);
6090 switch (AT_class (at))
6092 case dw_val_class_const:
6093 CHECKSUM_ULEB128 (DW_FORM_sdata);
6094 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
6095 break;
6097 case dw_val_class_unsigned_const:
6098 CHECKSUM_ULEB128 (DW_FORM_sdata);
6099 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
6100 break;
6102 case dw_val_class_const_double:
6103 CHECKSUM_ULEB128 (DW_FORM_block);
6104 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
6105 CHECKSUM (at->dw_attr_val.v.val_double);
6106 break;
6108 case dw_val_class_wide_int:
6109 CHECKSUM_ULEB128 (DW_FORM_block);
6110 CHECKSUM_ULEB128 (get_full_len (*at->dw_attr_val.v.val_wide)
6111 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
6112 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6113 get_full_len (*at->dw_attr_val.v.val_wide)
6114 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6115 break;
6117 case dw_val_class_vec:
6118 CHECKSUM_ULEB128 (DW_FORM_block);
6119 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
6120 * at->dw_attr_val.v.val_vec.elt_size);
6121 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6122 (at->dw_attr_val.v.val_vec.length
6123 * at->dw_attr_val.v.val_vec.elt_size));
6124 break;
6126 case dw_val_class_flag:
6127 CHECKSUM_ULEB128 (DW_FORM_flag);
6128 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
6129 break;
6131 case dw_val_class_str:
6132 CHECKSUM_ULEB128 (DW_FORM_string);
6133 CHECKSUM_STRING (AT_string (at));
6134 break;
6136 case dw_val_class_addr:
6137 r = AT_addr (at);
6138 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6139 CHECKSUM_ULEB128 (DW_FORM_string);
6140 CHECKSUM_STRING (XSTR (r, 0));
6141 break;
6143 case dw_val_class_offset:
6144 CHECKSUM_ULEB128 (DW_FORM_sdata);
6145 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
6146 break;
6148 case dw_val_class_loc:
6149 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6150 loc_checksum_ordered (loc, ctx);
6151 break;
6153 case dw_val_class_fde_ref:
6154 case dw_val_class_lbl_id:
6155 case dw_val_class_lineptr:
6156 case dw_val_class_macptr:
6157 case dw_val_class_high_pc:
6158 break;
6160 case dw_val_class_file:
6161 CHECKSUM_ULEB128 (DW_FORM_string);
6162 CHECKSUM_STRING (AT_file (at)->filename);
6163 break;
6165 case dw_val_class_data8:
6166 CHECKSUM (at->dw_attr_val.v.val_data8);
6167 break;
6169 default:
6170 break;
6174 struct checksum_attributes
6176 dw_attr_ref at_name;
6177 dw_attr_ref at_type;
6178 dw_attr_ref at_friend;
6179 dw_attr_ref at_accessibility;
6180 dw_attr_ref at_address_class;
6181 dw_attr_ref at_allocated;
6182 dw_attr_ref at_artificial;
6183 dw_attr_ref at_associated;
6184 dw_attr_ref at_binary_scale;
6185 dw_attr_ref at_bit_offset;
6186 dw_attr_ref at_bit_size;
6187 dw_attr_ref at_bit_stride;
6188 dw_attr_ref at_byte_size;
6189 dw_attr_ref at_byte_stride;
6190 dw_attr_ref at_const_value;
6191 dw_attr_ref at_containing_type;
6192 dw_attr_ref at_count;
6193 dw_attr_ref at_data_location;
6194 dw_attr_ref at_data_member_location;
6195 dw_attr_ref at_decimal_scale;
6196 dw_attr_ref at_decimal_sign;
6197 dw_attr_ref at_default_value;
6198 dw_attr_ref at_digit_count;
6199 dw_attr_ref at_discr;
6200 dw_attr_ref at_discr_list;
6201 dw_attr_ref at_discr_value;
6202 dw_attr_ref at_encoding;
6203 dw_attr_ref at_endianity;
6204 dw_attr_ref at_explicit;
6205 dw_attr_ref at_is_optional;
6206 dw_attr_ref at_location;
6207 dw_attr_ref at_lower_bound;
6208 dw_attr_ref at_mutable;
6209 dw_attr_ref at_ordering;
6210 dw_attr_ref at_picture_string;
6211 dw_attr_ref at_prototyped;
6212 dw_attr_ref at_small;
6213 dw_attr_ref at_segment;
6214 dw_attr_ref at_string_length;
6215 dw_attr_ref at_threads_scaled;
6216 dw_attr_ref at_upper_bound;
6217 dw_attr_ref at_use_location;
6218 dw_attr_ref at_use_UTF8;
6219 dw_attr_ref at_variable_parameter;
6220 dw_attr_ref at_virtuality;
6221 dw_attr_ref at_visibility;
6222 dw_attr_ref at_vtable_elem_location;
6225 /* Collect the attributes that we will want to use for the checksum. */
6227 static void
6228 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
6230 dw_attr_ref a;
6231 unsigned ix;
6233 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6235 switch (a->dw_attr)
6237 case DW_AT_name:
6238 attrs->at_name = a;
6239 break;
6240 case DW_AT_type:
6241 attrs->at_type = a;
6242 break;
6243 case DW_AT_friend:
6244 attrs->at_friend = a;
6245 break;
6246 case DW_AT_accessibility:
6247 attrs->at_accessibility = a;
6248 break;
6249 case DW_AT_address_class:
6250 attrs->at_address_class = a;
6251 break;
6252 case DW_AT_allocated:
6253 attrs->at_allocated = a;
6254 break;
6255 case DW_AT_artificial:
6256 attrs->at_artificial = a;
6257 break;
6258 case DW_AT_associated:
6259 attrs->at_associated = a;
6260 break;
6261 case DW_AT_binary_scale:
6262 attrs->at_binary_scale = a;
6263 break;
6264 case DW_AT_bit_offset:
6265 attrs->at_bit_offset = a;
6266 break;
6267 case DW_AT_bit_size:
6268 attrs->at_bit_size = a;
6269 break;
6270 case DW_AT_bit_stride:
6271 attrs->at_bit_stride = a;
6272 break;
6273 case DW_AT_byte_size:
6274 attrs->at_byte_size = a;
6275 break;
6276 case DW_AT_byte_stride:
6277 attrs->at_byte_stride = a;
6278 break;
6279 case DW_AT_const_value:
6280 attrs->at_const_value = a;
6281 break;
6282 case DW_AT_containing_type:
6283 attrs->at_containing_type = a;
6284 break;
6285 case DW_AT_count:
6286 attrs->at_count = a;
6287 break;
6288 case DW_AT_data_location:
6289 attrs->at_data_location = a;
6290 break;
6291 case DW_AT_data_member_location:
6292 attrs->at_data_member_location = a;
6293 break;
6294 case DW_AT_decimal_scale:
6295 attrs->at_decimal_scale = a;
6296 break;
6297 case DW_AT_decimal_sign:
6298 attrs->at_decimal_sign = a;
6299 break;
6300 case DW_AT_default_value:
6301 attrs->at_default_value = a;
6302 break;
6303 case DW_AT_digit_count:
6304 attrs->at_digit_count = a;
6305 break;
6306 case DW_AT_discr:
6307 attrs->at_discr = a;
6308 break;
6309 case DW_AT_discr_list:
6310 attrs->at_discr_list = a;
6311 break;
6312 case DW_AT_discr_value:
6313 attrs->at_discr_value = a;
6314 break;
6315 case DW_AT_encoding:
6316 attrs->at_encoding = a;
6317 break;
6318 case DW_AT_endianity:
6319 attrs->at_endianity = a;
6320 break;
6321 case DW_AT_explicit:
6322 attrs->at_explicit = a;
6323 break;
6324 case DW_AT_is_optional:
6325 attrs->at_is_optional = a;
6326 break;
6327 case DW_AT_location:
6328 attrs->at_location = a;
6329 break;
6330 case DW_AT_lower_bound:
6331 attrs->at_lower_bound = a;
6332 break;
6333 case DW_AT_mutable:
6334 attrs->at_mutable = a;
6335 break;
6336 case DW_AT_ordering:
6337 attrs->at_ordering = a;
6338 break;
6339 case DW_AT_picture_string:
6340 attrs->at_picture_string = a;
6341 break;
6342 case DW_AT_prototyped:
6343 attrs->at_prototyped = a;
6344 break;
6345 case DW_AT_small:
6346 attrs->at_small = a;
6347 break;
6348 case DW_AT_segment:
6349 attrs->at_segment = a;
6350 break;
6351 case DW_AT_string_length:
6352 attrs->at_string_length = a;
6353 break;
6354 case DW_AT_threads_scaled:
6355 attrs->at_threads_scaled = a;
6356 break;
6357 case DW_AT_upper_bound:
6358 attrs->at_upper_bound = a;
6359 break;
6360 case DW_AT_use_location:
6361 attrs->at_use_location = a;
6362 break;
6363 case DW_AT_use_UTF8:
6364 attrs->at_use_UTF8 = a;
6365 break;
6366 case DW_AT_variable_parameter:
6367 attrs->at_variable_parameter = a;
6368 break;
6369 case DW_AT_virtuality:
6370 attrs->at_virtuality = a;
6371 break;
6372 case DW_AT_visibility:
6373 attrs->at_visibility = a;
6374 break;
6375 case DW_AT_vtable_elem_location:
6376 attrs->at_vtable_elem_location = a;
6377 break;
6378 default:
6379 break;
6384 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
6386 static void
6387 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6389 dw_die_ref c;
6390 dw_die_ref decl;
6391 struct checksum_attributes attrs;
6393 CHECKSUM_ULEB128 ('D');
6394 CHECKSUM_ULEB128 (die->die_tag);
6396 memset (&attrs, 0, sizeof (attrs));
6398 decl = get_AT_ref (die, DW_AT_specification);
6399 if (decl != NULL)
6400 collect_checksum_attributes (&attrs, decl);
6401 collect_checksum_attributes (&attrs, die);
6403 CHECKSUM_ATTR (attrs.at_name);
6404 CHECKSUM_ATTR (attrs.at_accessibility);
6405 CHECKSUM_ATTR (attrs.at_address_class);
6406 CHECKSUM_ATTR (attrs.at_allocated);
6407 CHECKSUM_ATTR (attrs.at_artificial);
6408 CHECKSUM_ATTR (attrs.at_associated);
6409 CHECKSUM_ATTR (attrs.at_binary_scale);
6410 CHECKSUM_ATTR (attrs.at_bit_offset);
6411 CHECKSUM_ATTR (attrs.at_bit_size);
6412 CHECKSUM_ATTR (attrs.at_bit_stride);
6413 CHECKSUM_ATTR (attrs.at_byte_size);
6414 CHECKSUM_ATTR (attrs.at_byte_stride);
6415 CHECKSUM_ATTR (attrs.at_const_value);
6416 CHECKSUM_ATTR (attrs.at_containing_type);
6417 CHECKSUM_ATTR (attrs.at_count);
6418 CHECKSUM_ATTR (attrs.at_data_location);
6419 CHECKSUM_ATTR (attrs.at_data_member_location);
6420 CHECKSUM_ATTR (attrs.at_decimal_scale);
6421 CHECKSUM_ATTR (attrs.at_decimal_sign);
6422 CHECKSUM_ATTR (attrs.at_default_value);
6423 CHECKSUM_ATTR (attrs.at_digit_count);
6424 CHECKSUM_ATTR (attrs.at_discr);
6425 CHECKSUM_ATTR (attrs.at_discr_list);
6426 CHECKSUM_ATTR (attrs.at_discr_value);
6427 CHECKSUM_ATTR (attrs.at_encoding);
6428 CHECKSUM_ATTR (attrs.at_endianity);
6429 CHECKSUM_ATTR (attrs.at_explicit);
6430 CHECKSUM_ATTR (attrs.at_is_optional);
6431 CHECKSUM_ATTR (attrs.at_location);
6432 CHECKSUM_ATTR (attrs.at_lower_bound);
6433 CHECKSUM_ATTR (attrs.at_mutable);
6434 CHECKSUM_ATTR (attrs.at_ordering);
6435 CHECKSUM_ATTR (attrs.at_picture_string);
6436 CHECKSUM_ATTR (attrs.at_prototyped);
6437 CHECKSUM_ATTR (attrs.at_small);
6438 CHECKSUM_ATTR (attrs.at_segment);
6439 CHECKSUM_ATTR (attrs.at_string_length);
6440 CHECKSUM_ATTR (attrs.at_threads_scaled);
6441 CHECKSUM_ATTR (attrs.at_upper_bound);
6442 CHECKSUM_ATTR (attrs.at_use_location);
6443 CHECKSUM_ATTR (attrs.at_use_UTF8);
6444 CHECKSUM_ATTR (attrs.at_variable_parameter);
6445 CHECKSUM_ATTR (attrs.at_virtuality);
6446 CHECKSUM_ATTR (attrs.at_visibility);
6447 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6448 CHECKSUM_ATTR (attrs.at_type);
6449 CHECKSUM_ATTR (attrs.at_friend);
6451 /* Checksum the child DIEs. */
6452 c = die->die_child;
6453 if (c) do {
6454 dw_attr_ref name_attr;
6456 c = c->die_sib;
6457 name_attr = get_AT (c, DW_AT_name);
6458 if (is_template_instantiation (c))
6460 /* Ignore instantiations of member type and function templates. */
6462 else if (name_attr != NULL
6463 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
6465 /* Use a shallow checksum for named nested types and member
6466 functions. */
6467 CHECKSUM_ULEB128 ('S');
6468 CHECKSUM_ULEB128 (c->die_tag);
6469 CHECKSUM_STRING (AT_string (name_attr));
6471 else
6473 /* Use a deep checksum for other children. */
6474 /* Mark this DIE so it gets processed when unmarking. */
6475 if (c->die_mark == 0)
6476 c->die_mark = -1;
6477 die_checksum_ordered (c, ctx, mark);
6479 } while (c != die->die_child);
6481 CHECKSUM_ULEB128 (0);
6484 /* Add a type name and tag to a hash. */
6485 static void
6486 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
6488 CHECKSUM_ULEB128 (tag);
6489 CHECKSUM_STRING (name);
6492 #undef CHECKSUM
6493 #undef CHECKSUM_STRING
6494 #undef CHECKSUM_ATTR
6495 #undef CHECKSUM_LEB128
6496 #undef CHECKSUM_ULEB128
6498 /* Generate the type signature for DIE. This is computed by generating an
6499 MD5 checksum over the DIE's tag, its relevant attributes, and its
6500 children. Attributes that are references to other DIEs are processed
6501 by recursion, using the MARK field to prevent infinite recursion.
6502 If the DIE is nested inside a namespace or another type, we also
6503 need to include that context in the signature. The lower 64 bits
6504 of the resulting MD5 checksum comprise the signature. */
6506 static void
6507 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6509 int mark;
6510 const char *name;
6511 unsigned char checksum[16];
6512 struct md5_ctx ctx;
6513 dw_die_ref decl;
6514 dw_die_ref parent;
6516 name = get_AT_string (die, DW_AT_name);
6517 decl = get_AT_ref (die, DW_AT_specification);
6518 parent = get_die_parent (die);
6520 /* First, compute a signature for just the type name (and its surrounding
6521 context, if any. This is stored in the type unit DIE for link-time
6522 ODR (one-definition rule) checking. */
6524 if (is_cxx () && name != NULL)
6526 md5_init_ctx (&ctx);
6528 /* Checksum the names of surrounding namespaces and structures. */
6529 if (parent != NULL)
6530 checksum_die_context (parent, &ctx);
6532 /* Checksum the current DIE. */
6533 die_odr_checksum (die->die_tag, name, &ctx);
6534 md5_finish_ctx (&ctx, checksum);
6536 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6539 /* Next, compute the complete type signature. */
6541 md5_init_ctx (&ctx);
6542 mark = 1;
6543 die->die_mark = mark;
6545 /* Checksum the names of surrounding namespaces and structures. */
6546 if (parent != NULL)
6547 checksum_die_context (parent, &ctx);
6549 /* Checksum the DIE and its children. */
6550 die_checksum_ordered (die, &ctx, &mark);
6551 unmark_all_dies (die);
6552 md5_finish_ctx (&ctx, checksum);
6554 /* Store the signature in the type node and link the type DIE and the
6555 type node together. */
6556 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6557 DWARF_TYPE_SIGNATURE_SIZE);
6558 die->comdat_type_p = true;
6559 die->die_id.die_type_node = type_node;
6560 type_node->type_die = die;
6562 /* If the DIE is a specification, link its declaration to the type node
6563 as well. */
6564 if (decl != NULL)
6566 decl->comdat_type_p = true;
6567 decl->die_id.die_type_node = type_node;
6571 /* Do the location expressions look same? */
6572 static inline int
6573 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6575 return loc1->dw_loc_opc == loc2->dw_loc_opc
6576 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6577 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6580 /* Do the values look the same? */
6581 static int
6582 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6584 dw_loc_descr_ref loc1, loc2;
6585 rtx r1, r2;
6587 if (v1->val_class != v2->val_class)
6588 return 0;
6590 switch (v1->val_class)
6592 case dw_val_class_const:
6593 return v1->v.val_int == v2->v.val_int;
6594 case dw_val_class_unsigned_const:
6595 return v1->v.val_unsigned == v2->v.val_unsigned;
6596 case dw_val_class_const_double:
6597 return v1->v.val_double.high == v2->v.val_double.high
6598 && v1->v.val_double.low == v2->v.val_double.low;
6599 case dw_val_class_wide_int:
6600 return *v1->v.val_wide == *v2->v.val_wide;
6601 case dw_val_class_vec:
6602 if (v1->v.val_vec.length != v2->v.val_vec.length
6603 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6604 return 0;
6605 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6606 v1->v.val_vec.length * v1->v.val_vec.elt_size))
6607 return 0;
6608 return 1;
6609 case dw_val_class_flag:
6610 return v1->v.val_flag == v2->v.val_flag;
6611 case dw_val_class_str:
6612 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
6614 case dw_val_class_addr:
6615 r1 = v1->v.val_addr;
6616 r2 = v2->v.val_addr;
6617 if (GET_CODE (r1) != GET_CODE (r2))
6618 return 0;
6619 return !rtx_equal_p (r1, r2);
6621 case dw_val_class_offset:
6622 return v1->v.val_offset == v2->v.val_offset;
6624 case dw_val_class_loc:
6625 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6626 loc1 && loc2;
6627 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6628 if (!same_loc_p (loc1, loc2, mark))
6629 return 0;
6630 return !loc1 && !loc2;
6632 case dw_val_class_die_ref:
6633 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6635 case dw_val_class_fde_ref:
6636 case dw_val_class_vms_delta:
6637 case dw_val_class_lbl_id:
6638 case dw_val_class_lineptr:
6639 case dw_val_class_macptr:
6640 case dw_val_class_high_pc:
6641 return 1;
6643 case dw_val_class_file:
6644 return v1->v.val_file == v2->v.val_file;
6646 case dw_val_class_data8:
6647 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
6649 default:
6650 return 1;
6654 /* Do the attributes look the same? */
6656 static int
6657 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6659 if (at1->dw_attr != at2->dw_attr)
6660 return 0;
6662 /* We don't care that this was compiled with a different compiler
6663 snapshot; if the output is the same, that's what matters. */
6664 if (at1->dw_attr == DW_AT_producer)
6665 return 1;
6667 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6670 /* Do the dies look the same? */
6672 static int
6673 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6675 dw_die_ref c1, c2;
6676 dw_attr_ref a1;
6677 unsigned ix;
6679 /* To avoid infinite recursion. */
6680 if (die1->die_mark)
6681 return die1->die_mark == die2->die_mark;
6682 die1->die_mark = die2->die_mark = ++(*mark);
6684 if (die1->die_tag != die2->die_tag)
6685 return 0;
6687 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
6688 return 0;
6690 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
6691 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
6692 return 0;
6694 c1 = die1->die_child;
6695 c2 = die2->die_child;
6696 if (! c1)
6698 if (c2)
6699 return 0;
6701 else
6702 for (;;)
6704 if (!same_die_p (c1, c2, mark))
6705 return 0;
6706 c1 = c1->die_sib;
6707 c2 = c2->die_sib;
6708 if (c1 == die1->die_child)
6710 if (c2 == die2->die_child)
6711 break;
6712 else
6713 return 0;
6717 return 1;
6720 /* Do the dies look the same? Wrapper around same_die_p. */
6722 static int
6723 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6725 int mark = 0;
6726 int ret = same_die_p (die1, die2, &mark);
6728 unmark_all_dies (die1);
6729 unmark_all_dies (die2);
6731 return ret;
6734 /* The prefix to attach to symbols on DIEs in the current comdat debug
6735 info section. */
6736 static const char *comdat_symbol_id;
6738 /* The index of the current symbol within the current comdat CU. */
6739 static unsigned int comdat_symbol_number;
6741 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6742 children, and set comdat_symbol_id accordingly. */
6744 static void
6745 compute_section_prefix (dw_die_ref unit_die)
6747 const char *die_name = get_AT_string (unit_die, DW_AT_name);
6748 const char *base = die_name ? lbasename (die_name) : "anonymous";
6749 char *name = XALLOCAVEC (char, strlen (base) + 64);
6750 char *p;
6751 int i, mark;
6752 unsigned char checksum[16];
6753 struct md5_ctx ctx;
6755 /* Compute the checksum of the DIE, then append part of it as hex digits to
6756 the name filename of the unit. */
6758 md5_init_ctx (&ctx);
6759 mark = 0;
6760 die_checksum (unit_die, &ctx, &mark);
6761 unmark_all_dies (unit_die);
6762 md5_finish_ctx (&ctx, checksum);
6764 sprintf (name, "%s.", base);
6765 clean_symbol_name (name);
6767 p = name + strlen (name);
6768 for (i = 0; i < 4; i++)
6770 sprintf (p, "%.2x", checksum[i]);
6771 p += 2;
6774 comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
6775 comdat_symbol_number = 0;
6778 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
6780 static int
6781 is_type_die (dw_die_ref die)
6783 switch (die->die_tag)
6785 case DW_TAG_array_type:
6786 case DW_TAG_class_type:
6787 case DW_TAG_interface_type:
6788 case DW_TAG_enumeration_type:
6789 case DW_TAG_pointer_type:
6790 case DW_TAG_reference_type:
6791 case DW_TAG_rvalue_reference_type:
6792 case DW_TAG_string_type:
6793 case DW_TAG_structure_type:
6794 case DW_TAG_subroutine_type:
6795 case DW_TAG_union_type:
6796 case DW_TAG_ptr_to_member_type:
6797 case DW_TAG_set_type:
6798 case DW_TAG_subrange_type:
6799 case DW_TAG_base_type:
6800 case DW_TAG_const_type:
6801 case DW_TAG_file_type:
6802 case DW_TAG_packed_type:
6803 case DW_TAG_volatile_type:
6804 case DW_TAG_typedef:
6805 return 1;
6806 default:
6807 return 0;
6811 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6812 Basically, we want to choose the bits that are likely to be shared between
6813 compilations (types) and leave out the bits that are specific to individual
6814 compilations (functions). */
6816 static int
6817 is_comdat_die (dw_die_ref c)
6819 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6820 we do for stabs. The advantage is a greater likelihood of sharing between
6821 objects that don't include headers in the same order (and therefore would
6822 put the base types in a different comdat). jason 8/28/00 */
6824 if (c->die_tag == DW_TAG_base_type)
6825 return 0;
6827 if (c->die_tag == DW_TAG_pointer_type
6828 || c->die_tag == DW_TAG_reference_type
6829 || c->die_tag == DW_TAG_rvalue_reference_type
6830 || c->die_tag == DW_TAG_const_type
6831 || c->die_tag == DW_TAG_volatile_type)
6833 dw_die_ref t = get_AT_ref (c, DW_AT_type);
6835 return t ? is_comdat_die (t) : 0;
6838 return is_type_die (c);
6841 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6842 compilation unit. */
6844 static int
6845 is_symbol_die (dw_die_ref c)
6847 return (is_type_die (c)
6848 || is_declaration_die (c)
6849 || c->die_tag == DW_TAG_namespace
6850 || c->die_tag == DW_TAG_module);
6853 /* Returns true iff C is a compile-unit DIE. */
6855 static inline bool
6856 is_cu_die (dw_die_ref c)
6858 return c && c->die_tag == DW_TAG_compile_unit;
6861 /* Returns true iff C is a unit DIE of some sort. */
6863 static inline bool
6864 is_unit_die (dw_die_ref c)
6866 return c && (c->die_tag == DW_TAG_compile_unit
6867 || c->die_tag == DW_TAG_partial_unit
6868 || c->die_tag == DW_TAG_type_unit);
6871 /* Returns true iff C is a namespace DIE. */
6873 static inline bool
6874 is_namespace_die (dw_die_ref c)
6876 return c && c->die_tag == DW_TAG_namespace;
6879 /* Returns true iff C is a class or structure DIE. */
6881 static inline bool
6882 is_class_die (dw_die_ref c)
6884 return c && (c->die_tag == DW_TAG_class_type
6885 || c->die_tag == DW_TAG_structure_type);
6888 /* Return non-zero if this DIE is a template parameter. */
6890 static inline bool
6891 is_template_parameter (dw_die_ref die)
6893 switch (die->die_tag)
6895 case DW_TAG_template_type_param:
6896 case DW_TAG_template_value_param:
6897 case DW_TAG_GNU_template_template_param:
6898 case DW_TAG_GNU_template_parameter_pack:
6899 return true;
6900 default:
6901 return false;
6905 /* Return non-zero if this DIE represents a template instantiation. */
6907 static inline bool
6908 is_template_instantiation (dw_die_ref die)
6910 dw_die_ref c;
6912 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
6913 return false;
6914 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
6915 return false;
6918 static char *
6919 gen_internal_sym (const char *prefix)
6921 char buf[256];
6923 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6924 return xstrdup (buf);
6927 /* Assign symbols to all worthy DIEs under DIE. */
6929 static void
6930 assign_symbol_names (dw_die_ref die)
6932 dw_die_ref c;
6934 if (is_symbol_die (die) && !die->comdat_type_p)
6936 if (comdat_symbol_id)
6938 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
6940 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6941 comdat_symbol_id, comdat_symbol_number++);
6942 die->die_id.die_symbol = xstrdup (p);
6944 else
6945 die->die_id.die_symbol = gen_internal_sym ("LDIE");
6948 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6951 struct cu_hash_table_entry
6953 dw_die_ref cu;
6954 unsigned min_comdat_num, max_comdat_num;
6955 struct cu_hash_table_entry *next;
6958 /* Helpers to manipulate hash table of CUs. */
6960 struct cu_hash_table_entry_hasher
6962 typedef cu_hash_table_entry *value_type;
6963 typedef die_struct *compare_type;
6964 static inline hashval_t hash (const cu_hash_table_entry *);
6965 static inline bool equal (const cu_hash_table_entry *, const die_struct *);
6966 static inline void remove (cu_hash_table_entry *);
6969 inline hashval_t
6970 cu_hash_table_entry_hasher::hash (const cu_hash_table_entry *entry)
6972 return htab_hash_string (entry->cu->die_id.die_symbol);
6975 inline bool
6976 cu_hash_table_entry_hasher::equal (const cu_hash_table_entry *entry1,
6977 const die_struct *entry2)
6979 return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
6982 inline void
6983 cu_hash_table_entry_hasher::remove (cu_hash_table_entry *entry)
6985 struct cu_hash_table_entry *next;
6987 while (entry)
6989 next = entry->next;
6990 free (entry);
6991 entry = next;
6995 typedef hash_table<cu_hash_table_entry_hasher> cu_hash_type;
6997 /* Check whether we have already seen this CU and set up SYM_NUM
6998 accordingly. */
6999 static int
7000 check_duplicate_cu (dw_die_ref cu, cu_hash_type *htable, unsigned int *sym_num)
7002 struct cu_hash_table_entry dummy;
7003 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
7005 dummy.max_comdat_num = 0;
7007 slot = htable->find_slot_with_hash (cu,
7008 htab_hash_string (cu->die_id.die_symbol),
7009 INSERT);
7010 entry = *slot;
7012 for (; entry; last = entry, entry = entry->next)
7014 if (same_die_p_wrap (cu, entry->cu))
7015 break;
7018 if (entry)
7020 *sym_num = entry->min_comdat_num;
7021 return 1;
7024 entry = XCNEW (struct cu_hash_table_entry);
7025 entry->cu = cu;
7026 entry->min_comdat_num = *sym_num = last->max_comdat_num;
7027 entry->next = *slot;
7028 *slot = entry;
7030 return 0;
7033 /* Record SYM_NUM to record of CU in HTABLE. */
7034 static void
7035 record_comdat_symbol_number (dw_die_ref cu, cu_hash_type *htable,
7036 unsigned int sym_num)
7038 struct cu_hash_table_entry **slot, *entry;
7040 slot = htable->find_slot_with_hash (cu,
7041 htab_hash_string (cu->die_id.die_symbol),
7042 NO_INSERT);
7043 entry = *slot;
7045 entry->max_comdat_num = sym_num;
7048 /* Traverse the DIE (which is always comp_unit_die), and set up
7049 additional compilation units for each of the include files we see
7050 bracketed by BINCL/EINCL. */
7052 static void
7053 break_out_includes (dw_die_ref die)
7055 dw_die_ref c;
7056 dw_die_ref unit = NULL;
7057 limbo_die_node *node, **pnode;
7059 c = die->die_child;
7060 if (c) do {
7061 dw_die_ref prev = c;
7062 c = c->die_sib;
7063 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
7064 || (unit && is_comdat_die (c)))
7066 dw_die_ref next = c->die_sib;
7068 /* This DIE is for a secondary CU; remove it from the main one. */
7069 remove_child_with_prev (c, prev);
7071 if (c->die_tag == DW_TAG_GNU_BINCL)
7072 unit = push_new_compile_unit (unit, c);
7073 else if (c->die_tag == DW_TAG_GNU_EINCL)
7074 unit = pop_compile_unit (unit);
7075 else
7076 add_child_die (unit, c);
7077 c = next;
7078 if (c == die->die_child)
7079 break;
7081 } while (c != die->die_child);
7083 #if 0
7084 /* We can only use this in debugging, since the frontend doesn't check
7085 to make sure that we leave every include file we enter. */
7086 gcc_assert (!unit);
7087 #endif
7089 assign_symbol_names (die);
7090 cu_hash_type cu_hash_table (10);
7091 for (node = limbo_die_list, pnode = &limbo_die_list;
7092 node;
7093 node = node->next)
7095 int is_dupl;
7097 compute_section_prefix (node->die);
7098 is_dupl = check_duplicate_cu (node->die, &cu_hash_table,
7099 &comdat_symbol_number);
7100 assign_symbol_names (node->die);
7101 if (is_dupl)
7102 *pnode = node->next;
7103 else
7105 pnode = &node->next;
7106 record_comdat_symbol_number (node->die, &cu_hash_table,
7107 comdat_symbol_number);
7112 /* Return non-zero if this DIE is a declaration. */
7114 static int
7115 is_declaration_die (dw_die_ref die)
7117 dw_attr_ref a;
7118 unsigned ix;
7120 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7121 if (a->dw_attr == DW_AT_declaration)
7122 return 1;
7124 return 0;
7127 /* Return non-zero if this DIE is nested inside a subprogram. */
7129 static int
7130 is_nested_in_subprogram (dw_die_ref die)
7132 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
7134 if (decl == NULL)
7135 decl = die;
7136 return local_scope_p (decl);
7139 /* Return non-zero if this DIE contains a defining declaration of a
7140 subprogram. */
7142 static int
7143 contains_subprogram_definition (dw_die_ref die)
7145 dw_die_ref c;
7147 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
7148 return 1;
7149 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
7150 return 0;
7153 /* Return non-zero if this is a type DIE that should be moved to a
7154 COMDAT .debug_types section. */
7156 static int
7157 should_move_die_to_comdat (dw_die_ref die)
7159 switch (die->die_tag)
7161 case DW_TAG_class_type:
7162 case DW_TAG_structure_type:
7163 case DW_TAG_enumeration_type:
7164 case DW_TAG_union_type:
7165 /* Don't move declarations, inlined instances, types nested in a
7166 subprogram, or types that contain subprogram definitions. */
7167 if (is_declaration_die (die)
7168 || get_AT (die, DW_AT_abstract_origin)
7169 || is_nested_in_subprogram (die)
7170 || contains_subprogram_definition (die))
7171 return 0;
7172 return 1;
7173 case DW_TAG_array_type:
7174 case DW_TAG_interface_type:
7175 case DW_TAG_pointer_type:
7176 case DW_TAG_reference_type:
7177 case DW_TAG_rvalue_reference_type:
7178 case DW_TAG_string_type:
7179 case DW_TAG_subroutine_type:
7180 case DW_TAG_ptr_to_member_type:
7181 case DW_TAG_set_type:
7182 case DW_TAG_subrange_type:
7183 case DW_TAG_base_type:
7184 case DW_TAG_const_type:
7185 case DW_TAG_file_type:
7186 case DW_TAG_packed_type:
7187 case DW_TAG_volatile_type:
7188 case DW_TAG_typedef:
7189 default:
7190 return 0;
7194 /* Make a clone of DIE. */
7196 static dw_die_ref
7197 clone_die (dw_die_ref die)
7199 dw_die_ref clone;
7200 dw_attr_ref a;
7201 unsigned ix;
7203 clone = ggc_cleared_alloc<die_node> ();
7204 clone->die_tag = die->die_tag;
7206 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7207 add_dwarf_attr (clone, a);
7209 return clone;
7212 /* Make a clone of the tree rooted at DIE. */
7214 static dw_die_ref
7215 clone_tree (dw_die_ref die)
7217 dw_die_ref c;
7218 dw_die_ref clone = clone_die (die);
7220 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
7222 return clone;
7225 /* Make a clone of DIE as a declaration. */
7227 static dw_die_ref
7228 clone_as_declaration (dw_die_ref die)
7230 dw_die_ref clone;
7231 dw_die_ref decl;
7232 dw_attr_ref a;
7233 unsigned ix;
7235 /* If the DIE is already a declaration, just clone it. */
7236 if (is_declaration_die (die))
7237 return clone_die (die);
7239 /* If the DIE is a specification, just clone its declaration DIE. */
7240 decl = get_AT_ref (die, DW_AT_specification);
7241 if (decl != NULL)
7243 clone = clone_die (decl);
7244 if (die->comdat_type_p)
7245 add_AT_die_ref (clone, DW_AT_signature, die);
7246 return clone;
7249 clone = ggc_cleared_alloc<die_node> ();
7250 clone->die_tag = die->die_tag;
7252 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7254 /* We don't want to copy over all attributes.
7255 For example we don't want DW_AT_byte_size because otherwise we will no
7256 longer have a declaration and GDB will treat it as a definition. */
7258 switch (a->dw_attr)
7260 case DW_AT_abstract_origin:
7261 case DW_AT_artificial:
7262 case DW_AT_containing_type:
7263 case DW_AT_external:
7264 case DW_AT_name:
7265 case DW_AT_type:
7266 case DW_AT_virtuality:
7267 case DW_AT_linkage_name:
7268 case DW_AT_MIPS_linkage_name:
7269 add_dwarf_attr (clone, a);
7270 break;
7271 case DW_AT_byte_size:
7272 default:
7273 break;
7277 if (die->comdat_type_p)
7278 add_AT_die_ref (clone, DW_AT_signature, die);
7280 add_AT_flag (clone, DW_AT_declaration, 1);
7281 return clone;
7285 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
7287 struct decl_table_entry
7289 dw_die_ref orig;
7290 dw_die_ref copy;
7293 /* Helpers to manipulate hash table of copied declarations. */
7295 /* Hashtable helpers. */
7297 struct decl_table_entry_hasher : typed_free_remove <decl_table_entry>
7299 typedef decl_table_entry *value_type;
7300 typedef die_struct *compare_type;
7301 static inline hashval_t hash (const decl_table_entry *);
7302 static inline bool equal (const decl_table_entry *, const die_struct *);
7305 inline hashval_t
7306 decl_table_entry_hasher::hash (const decl_table_entry *entry)
7308 return htab_hash_pointer (entry->orig);
7311 inline bool
7312 decl_table_entry_hasher::equal (const decl_table_entry *entry1,
7313 const die_struct *entry2)
7315 return entry1->orig == entry2;
7318 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
7320 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7321 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
7322 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
7323 to check if the ancestor has already been copied into UNIT. */
7325 static dw_die_ref
7326 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
7327 decl_hash_type *decl_table)
7329 dw_die_ref parent = die->die_parent;
7330 dw_die_ref new_parent = unit;
7331 dw_die_ref copy;
7332 decl_table_entry **slot = NULL;
7333 struct decl_table_entry *entry = NULL;
7335 if (decl_table)
7337 /* Check if the entry has already been copied to UNIT. */
7338 slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
7339 INSERT);
7340 if (*slot != HTAB_EMPTY_ENTRY)
7342 entry = *slot;
7343 return entry->copy;
7346 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
7347 entry = XCNEW (struct decl_table_entry);
7348 entry->orig = die;
7349 entry->copy = NULL;
7350 *slot = entry;
7353 if (parent != NULL)
7355 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7356 if (spec != NULL)
7357 parent = spec;
7358 if (!is_unit_die (parent))
7359 new_parent = copy_ancestor_tree (unit, parent, decl_table);
7362 copy = clone_as_declaration (die);
7363 add_child_die (new_parent, copy);
7365 if (decl_table)
7367 /* Record the pointer to the copy. */
7368 entry->copy = copy;
7371 return copy;
7373 /* Copy the declaration context to the new type unit DIE. This includes
7374 any surrounding namespace or type declarations. If the DIE has an
7375 AT_specification attribute, it also includes attributes and children
7376 attached to the specification, and returns a pointer to the original
7377 parent of the declaration DIE. Returns NULL otherwise. */
7379 static dw_die_ref
7380 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7382 dw_die_ref decl;
7383 dw_die_ref new_decl;
7384 dw_die_ref orig_parent = NULL;
7386 decl = get_AT_ref (die, DW_AT_specification);
7387 if (decl == NULL)
7388 decl = die;
7389 else
7391 unsigned ix;
7392 dw_die_ref c;
7393 dw_attr_ref a;
7395 /* The original DIE will be changed to a declaration, and must
7396 be moved to be a child of the original declaration DIE. */
7397 orig_parent = decl->die_parent;
7399 /* Copy the type node pointer from the new DIE to the original
7400 declaration DIE so we can forward references later. */
7401 decl->comdat_type_p = true;
7402 decl->die_id.die_type_node = die->die_id.die_type_node;
7404 remove_AT (die, DW_AT_specification);
7406 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7408 if (a->dw_attr != DW_AT_name
7409 && a->dw_attr != DW_AT_declaration
7410 && a->dw_attr != DW_AT_external)
7411 add_dwarf_attr (die, a);
7414 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7417 if (decl->die_parent != NULL
7418 && !is_unit_die (decl->die_parent))
7420 new_decl = copy_ancestor_tree (unit, decl, NULL);
7421 if (new_decl != NULL)
7423 remove_AT (new_decl, DW_AT_signature);
7424 add_AT_specification (die, new_decl);
7428 return orig_parent;
7431 /* Generate the skeleton ancestor tree for the given NODE, then clone
7432 the DIE and add the clone into the tree. */
7434 static void
7435 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7437 if (node->new_die != NULL)
7438 return;
7440 node->new_die = clone_as_declaration (node->old_die);
7442 if (node->parent != NULL)
7444 generate_skeleton_ancestor_tree (node->parent);
7445 add_child_die (node->parent->new_die, node->new_die);
7449 /* Generate a skeleton tree of DIEs containing any declarations that are
7450 found in the original tree. We traverse the tree looking for declaration
7451 DIEs, and construct the skeleton from the bottom up whenever we find one. */
7453 static void
7454 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7456 skeleton_chain_node node;
7457 dw_die_ref c;
7458 dw_die_ref first;
7459 dw_die_ref prev = NULL;
7460 dw_die_ref next = NULL;
7462 node.parent = parent;
7464 first = c = parent->old_die->die_child;
7465 if (c)
7466 next = c->die_sib;
7467 if (c) do {
7468 if (prev == NULL || prev->die_sib == c)
7469 prev = c;
7470 c = next;
7471 next = (c == first ? NULL : c->die_sib);
7472 node.old_die = c;
7473 node.new_die = NULL;
7474 if (is_declaration_die (c))
7476 if (is_template_instantiation (c))
7478 /* Instantiated templates do not need to be cloned into the
7479 type unit. Just move the DIE and its children back to
7480 the skeleton tree (in the main CU). */
7481 remove_child_with_prev (c, prev);
7482 add_child_die (parent->new_die, c);
7483 c = prev;
7485 else
7487 /* Clone the existing DIE, move the original to the skeleton
7488 tree (which is in the main CU), and put the clone, with
7489 all the original's children, where the original came from
7490 (which is about to be moved to the type unit). */
7491 dw_die_ref clone = clone_die (c);
7492 move_all_children (c, clone);
7494 /* If the original has a DW_AT_object_pointer attribute,
7495 it would now point to a child DIE just moved to the
7496 cloned tree, so we need to remove that attribute from
7497 the original. */
7498 remove_AT (c, DW_AT_object_pointer);
7500 replace_child (c, clone, prev);
7501 generate_skeleton_ancestor_tree (parent);
7502 add_child_die (parent->new_die, c);
7503 node.new_die = c;
7504 c = clone;
7507 generate_skeleton_bottom_up (&node);
7508 } while (next != NULL);
7511 /* Wrapper function for generate_skeleton_bottom_up. */
7513 static dw_die_ref
7514 generate_skeleton (dw_die_ref die)
7516 skeleton_chain_node node;
7518 node.old_die = die;
7519 node.new_die = NULL;
7520 node.parent = NULL;
7522 /* If this type definition is nested inside another type,
7523 and is not an instantiation of a template, always leave
7524 at least a declaration in its place. */
7525 if (die->die_parent != NULL
7526 && is_type_die (die->die_parent)
7527 && !is_template_instantiation (die))
7528 node.new_die = clone_as_declaration (die);
7530 generate_skeleton_bottom_up (&node);
7531 return node.new_die;
7534 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
7535 declaration. The original DIE is moved to a new compile unit so that
7536 existing references to it follow it to the new location. If any of the
7537 original DIE's descendants is a declaration, we need to replace the
7538 original DIE with a skeleton tree and move the declarations back into the
7539 skeleton tree. */
7541 static dw_die_ref
7542 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
7543 dw_die_ref prev)
7545 dw_die_ref skeleton, orig_parent;
7547 /* Copy the declaration context to the type unit DIE. If the returned
7548 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
7549 that DIE. */
7550 orig_parent = copy_declaration_context (unit, child);
7552 skeleton = generate_skeleton (child);
7553 if (skeleton == NULL)
7554 remove_child_with_prev (child, prev);
7555 else
7557 skeleton->comdat_type_p = true;
7558 skeleton->die_id.die_type_node = child->die_id.die_type_node;
7560 /* If the original DIE was a specification, we need to put
7561 the skeleton under the parent DIE of the declaration.
7562 This leaves the original declaration in the tree, but
7563 it will be pruned later since there are no longer any
7564 references to it. */
7565 if (orig_parent != NULL)
7567 remove_child_with_prev (child, prev);
7568 add_child_die (orig_parent, skeleton);
7570 else
7571 replace_child (child, skeleton, prev);
7574 return skeleton;
7577 /* Traverse the DIE and set up additional .debug_types sections for each
7578 type worthy of being placed in a COMDAT section. */
7580 static void
7581 break_out_comdat_types (dw_die_ref die)
7583 dw_die_ref c;
7584 dw_die_ref first;
7585 dw_die_ref prev = NULL;
7586 dw_die_ref next = NULL;
7587 dw_die_ref unit = NULL;
7589 first = c = die->die_child;
7590 if (c)
7591 next = c->die_sib;
7592 if (c) do {
7593 if (prev == NULL || prev->die_sib == c)
7594 prev = c;
7595 c = next;
7596 next = (c == first ? NULL : c->die_sib);
7597 if (should_move_die_to_comdat (c))
7599 dw_die_ref replacement;
7600 comdat_type_node_ref type_node;
7602 /* Break out nested types into their own type units. */
7603 break_out_comdat_types (c);
7605 /* Create a new type unit DIE as the root for the new tree, and
7606 add it to the list of comdat types. */
7607 unit = new_die (DW_TAG_type_unit, NULL, NULL);
7608 add_AT_unsigned (unit, DW_AT_language,
7609 get_AT_unsigned (comp_unit_die (), DW_AT_language));
7610 type_node = ggc_cleared_alloc<comdat_type_node> ();
7611 type_node->root_die = unit;
7612 type_node->next = comdat_type_list;
7613 comdat_type_list = type_node;
7615 /* Generate the type signature. */
7616 generate_type_signature (c, type_node);
7618 /* Copy the declaration context, attributes, and children of the
7619 declaration into the new type unit DIE, then remove this DIE
7620 from the main CU (or replace it with a skeleton if necessary). */
7621 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
7622 type_node->skeleton_die = replacement;
7624 /* Add the DIE to the new compunit. */
7625 add_child_die (unit, c);
7627 if (replacement != NULL)
7628 c = replacement;
7630 else if (c->die_tag == DW_TAG_namespace
7631 || c->die_tag == DW_TAG_class_type
7632 || c->die_tag == DW_TAG_structure_type
7633 || c->die_tag == DW_TAG_union_type)
7635 /* Look for nested types that can be broken out. */
7636 break_out_comdat_types (c);
7638 } while (next != NULL);
7641 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
7642 Enter all the cloned children into the hash table decl_table. */
7644 static dw_die_ref
7645 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
7647 dw_die_ref c;
7648 dw_die_ref clone;
7649 struct decl_table_entry *entry;
7650 decl_table_entry **slot;
7652 if (die->die_tag == DW_TAG_subprogram)
7653 clone = clone_as_declaration (die);
7654 else
7655 clone = clone_die (die);
7657 slot = decl_table->find_slot_with_hash (die,
7658 htab_hash_pointer (die), INSERT);
7660 /* Assert that DIE isn't in the hash table yet. If it would be there
7661 before, the ancestors would be necessarily there as well, therefore
7662 clone_tree_partial wouldn't be called. */
7663 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
7665 entry = XCNEW (struct decl_table_entry);
7666 entry->orig = die;
7667 entry->copy = clone;
7668 *slot = entry;
7670 if (die->die_tag != DW_TAG_subprogram)
7671 FOR_EACH_CHILD (die, c,
7672 add_child_die (clone, clone_tree_partial (c, decl_table)));
7674 return clone;
7677 /* Walk the DIE and its children, looking for references to incomplete
7678 or trivial types that are unmarked (i.e., that are not in the current
7679 type_unit). */
7681 static void
7682 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
7684 dw_die_ref c;
7685 dw_attr_ref a;
7686 unsigned ix;
7688 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7690 if (AT_class (a) == dw_val_class_die_ref)
7692 dw_die_ref targ = AT_ref (a);
7693 decl_table_entry **slot;
7694 struct decl_table_entry *entry;
7696 if (targ->die_mark != 0 || targ->comdat_type_p)
7697 continue;
7699 slot = decl_table->find_slot_with_hash (targ,
7700 htab_hash_pointer (targ),
7701 INSERT);
7703 if (*slot != HTAB_EMPTY_ENTRY)
7705 /* TARG has already been copied, so we just need to
7706 modify the reference to point to the copy. */
7707 entry = *slot;
7708 a->dw_attr_val.v.val_die_ref.die = entry->copy;
7710 else
7712 dw_die_ref parent = unit;
7713 dw_die_ref copy = clone_die (targ);
7715 /* Record in DECL_TABLE that TARG has been copied.
7716 Need to do this now, before the recursive call,
7717 because DECL_TABLE may be expanded and SLOT
7718 would no longer be a valid pointer. */
7719 entry = XCNEW (struct decl_table_entry);
7720 entry->orig = targ;
7721 entry->copy = copy;
7722 *slot = entry;
7724 /* If TARG is not a declaration DIE, we need to copy its
7725 children. */
7726 if (!is_declaration_die (targ))
7728 FOR_EACH_CHILD (
7729 targ, c,
7730 add_child_die (copy,
7731 clone_tree_partial (c, decl_table)));
7734 /* Make sure the cloned tree is marked as part of the
7735 type unit. */
7736 mark_dies (copy);
7738 /* If TARG has surrounding context, copy its ancestor tree
7739 into the new type unit. */
7740 if (targ->die_parent != NULL
7741 && !is_unit_die (targ->die_parent))
7742 parent = copy_ancestor_tree (unit, targ->die_parent,
7743 decl_table);
7745 add_child_die (parent, copy);
7746 a->dw_attr_val.v.val_die_ref.die = copy;
7748 /* Make sure the newly-copied DIE is walked. If it was
7749 installed in a previously-added context, it won't
7750 get visited otherwise. */
7751 if (parent != unit)
7753 /* Find the highest point of the newly-added tree,
7754 mark each node along the way, and walk from there. */
7755 parent->die_mark = 1;
7756 while (parent->die_parent
7757 && parent->die_parent->die_mark == 0)
7759 parent = parent->die_parent;
7760 parent->die_mark = 1;
7762 copy_decls_walk (unit, parent, decl_table);
7768 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
7771 /* Copy declarations for "unworthy" types into the new comdat section.
7772 Incomplete types, modified types, and certain other types aren't broken
7773 out into comdat sections of their own, so they don't have a signature,
7774 and we need to copy the declaration into the same section so that we
7775 don't have an external reference. */
7777 static void
7778 copy_decls_for_unworthy_types (dw_die_ref unit)
7780 mark_dies (unit);
7781 decl_hash_type decl_table (10);
7782 copy_decls_walk (unit, unit, &decl_table);
7783 unmark_dies (unit);
7786 /* Traverse the DIE and add a sibling attribute if it may have the
7787 effect of speeding up access to siblings. To save some space,
7788 avoid generating sibling attributes for DIE's without children. */
7790 static void
7791 add_sibling_attributes (dw_die_ref die)
7793 dw_die_ref c;
7795 if (! die->die_child)
7796 return;
7798 if (die->die_parent && die != die->die_parent->die_child)
7799 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7801 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7804 /* Output all location lists for the DIE and its children. */
7806 static void
7807 output_location_lists (dw_die_ref die)
7809 dw_die_ref c;
7810 dw_attr_ref a;
7811 unsigned ix;
7813 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7814 if (AT_class (a) == dw_val_class_loc_list)
7815 output_loc_list (AT_loc_list (a));
7817 FOR_EACH_CHILD (die, c, output_location_lists (c));
7820 /* We want to limit the number of external references, because they are
7821 larger than local references: a relocation takes multiple words, and
7822 even a sig8 reference is always eight bytes, whereas a local reference
7823 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
7824 So if we encounter multiple external references to the same type DIE, we
7825 make a local typedef stub for it and redirect all references there.
7827 This is the element of the hash table for keeping track of these
7828 references. */
7830 struct external_ref
7832 dw_die_ref type;
7833 dw_die_ref stub;
7834 unsigned n_refs;
7837 /* Hashtable helpers. */
7839 struct external_ref_hasher : typed_free_remove <external_ref>
7841 typedef external_ref *value_type;
7842 typedef external_ref *compare_type;
7843 static inline hashval_t hash (const external_ref *);
7844 static inline bool equal (const external_ref *, const external_ref *);
7847 inline hashval_t
7848 external_ref_hasher::hash (const external_ref *r)
7850 dw_die_ref die = r->type;
7851 hashval_t h = 0;
7853 /* We can't use the address of the DIE for hashing, because
7854 that will make the order of the stub DIEs non-deterministic. */
7855 if (! die->comdat_type_p)
7856 /* We have a symbol; use it to compute a hash. */
7857 h = htab_hash_string (die->die_id.die_symbol);
7858 else
7860 /* We have a type signature; use a subset of the bits as the hash.
7861 The 8-byte signature is at least as large as hashval_t. */
7862 comdat_type_node_ref type_node = die->die_id.die_type_node;
7863 memcpy (&h, type_node->signature, sizeof (h));
7865 return h;
7868 inline bool
7869 external_ref_hasher::equal (const external_ref *r1, const external_ref *r2)
7871 return r1->type == r2->type;
7874 typedef hash_table<external_ref_hasher> external_ref_hash_type;
7876 /* Return a pointer to the external_ref for references to DIE. */
7878 static struct external_ref *
7879 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
7881 struct external_ref ref, *ref_p;
7882 external_ref **slot;
7884 ref.type = die;
7885 slot = map->find_slot (&ref, INSERT);
7886 if (*slot != HTAB_EMPTY_ENTRY)
7887 return *slot;
7889 ref_p = XCNEW (struct external_ref);
7890 ref_p->type = die;
7891 *slot = ref_p;
7892 return ref_p;
7895 /* Subroutine of optimize_external_refs, below.
7897 If we see a type skeleton, record it as our stub. If we see external
7898 references, remember how many we've seen. */
7900 static void
7901 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
7903 dw_die_ref c;
7904 dw_attr_ref a;
7905 unsigned ix;
7906 struct external_ref *ref_p;
7908 if (is_type_die (die)
7909 && (c = get_AT_ref (die, DW_AT_signature)))
7911 /* This is a local skeleton; use it for local references. */
7912 ref_p = lookup_external_ref (map, c);
7913 ref_p->stub = die;
7916 /* Scan the DIE references, and remember any that refer to DIEs from
7917 other CUs (i.e. those which are not marked). */
7918 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7919 if (AT_class (a) == dw_val_class_die_ref
7920 && (c = AT_ref (a))->die_mark == 0
7921 && is_type_die (c))
7923 ref_p = lookup_external_ref (map, c);
7924 ref_p->n_refs++;
7927 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
7930 /* htab_traverse callback function for optimize_external_refs, below. SLOT
7931 points to an external_ref, DATA is the CU we're processing. If we don't
7932 already have a local stub, and we have multiple refs, build a stub. */
7935 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
7937 struct external_ref *ref_p = *slot;
7939 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
7941 /* We have multiple references to this type, so build a small stub.
7942 Both of these forms are a bit dodgy from the perspective of the
7943 DWARF standard, since technically they should have names. */
7944 dw_die_ref cu = data;
7945 dw_die_ref type = ref_p->type;
7946 dw_die_ref stub = NULL;
7948 if (type->comdat_type_p)
7950 /* If we refer to this type via sig8, use AT_signature. */
7951 stub = new_die (type->die_tag, cu, NULL_TREE);
7952 add_AT_die_ref (stub, DW_AT_signature, type);
7954 else
7956 /* Otherwise, use a typedef with no name. */
7957 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
7958 add_AT_die_ref (stub, DW_AT_type, type);
7961 stub->die_mark++;
7962 ref_p->stub = stub;
7964 return 1;
7967 /* DIE is a unit; look through all the DIE references to see if there are
7968 any external references to types, and if so, create local stubs for
7969 them which will be applied in build_abbrev_table. This is useful because
7970 references to local DIEs are smaller. */
7972 static external_ref_hash_type *
7973 optimize_external_refs (dw_die_ref die)
7975 external_ref_hash_type *map = new external_ref_hash_type (10);
7976 optimize_external_refs_1 (die, map);
7977 map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
7978 return map;
7981 /* The format of each DIE (and its attribute value pairs) is encoded in an
7982 abbreviation table. This routine builds the abbreviation table and assigns
7983 a unique abbreviation id for each abbreviation entry. The children of each
7984 die are visited recursively. */
7986 static void
7987 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
7989 unsigned long abbrev_id;
7990 unsigned int n_alloc;
7991 dw_die_ref c;
7992 dw_attr_ref a;
7993 unsigned ix;
7995 /* Scan the DIE references, and replace any that refer to
7996 DIEs from other CUs (i.e. those which are not marked) with
7997 the local stubs we built in optimize_external_refs. */
7998 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7999 if (AT_class (a) == dw_val_class_die_ref
8000 && (c = AT_ref (a))->die_mark == 0)
8002 struct external_ref *ref_p;
8003 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
8005 ref_p = lookup_external_ref (extern_map, c);
8006 if (ref_p->stub && ref_p->stub != die)
8007 change_AT_die_ref (a, ref_p->stub);
8008 else
8009 /* We aren't changing this reference, so mark it external. */
8010 set_AT_ref_external (a, 1);
8013 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8015 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
8016 dw_attr_ref die_a, abbrev_a;
8017 unsigned ix;
8018 bool ok = true;
8020 if (abbrev->die_tag != die->die_tag)
8021 continue;
8022 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
8023 continue;
8025 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
8026 continue;
8028 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
8030 abbrev_a = &(*abbrev->die_attr)[ix];
8031 if ((abbrev_a->dw_attr != die_a->dw_attr)
8032 || (value_format (abbrev_a) != value_format (die_a)))
8034 ok = false;
8035 break;
8038 if (ok)
8039 break;
8042 if (abbrev_id >= abbrev_die_table_in_use)
8044 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
8046 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
8047 abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
8048 n_alloc);
8050 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
8051 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
8052 abbrev_die_table_allocated = n_alloc;
8055 ++abbrev_die_table_in_use;
8056 abbrev_die_table[abbrev_id] = die;
8059 die->die_abbrev = abbrev_id;
8060 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
8063 /* Return the power-of-two number of bytes necessary to represent VALUE. */
8065 static int
8066 constant_size (unsigned HOST_WIDE_INT value)
8068 int log;
8070 if (value == 0)
8071 log = 0;
8072 else
8073 log = floor_log2 (value);
8075 log = log / 8;
8076 log = 1 << (floor_log2 (log) + 1);
8078 return log;
8081 /* Return the size of a DIE as it is represented in the
8082 .debug_info section. */
8084 static unsigned long
8085 size_of_die (dw_die_ref die)
8087 unsigned long size = 0;
8088 dw_attr_ref a;
8089 unsigned ix;
8090 enum dwarf_form form;
8092 size += size_of_uleb128 (die->die_abbrev);
8093 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8095 switch (AT_class (a))
8097 case dw_val_class_addr:
8098 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8100 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8101 size += size_of_uleb128 (AT_index (a));
8103 else
8104 size += DWARF2_ADDR_SIZE;
8105 break;
8106 case dw_val_class_offset:
8107 size += DWARF_OFFSET_SIZE;
8108 break;
8109 case dw_val_class_loc:
8111 unsigned long lsize = size_of_locs (AT_loc (a));
8113 /* Block length. */
8114 if (dwarf_version >= 4)
8115 size += size_of_uleb128 (lsize);
8116 else
8117 size += constant_size (lsize);
8118 size += lsize;
8120 break;
8121 case dw_val_class_loc_list:
8122 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8124 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8125 size += size_of_uleb128 (AT_index (a));
8127 else
8128 size += DWARF_OFFSET_SIZE;
8129 break;
8130 case dw_val_class_range_list:
8131 size += DWARF_OFFSET_SIZE;
8132 break;
8133 case dw_val_class_const:
8134 size += size_of_sleb128 (AT_int (a));
8135 break;
8136 case dw_val_class_unsigned_const:
8138 int csize = constant_size (AT_unsigned (a));
8139 if (dwarf_version == 3
8140 && a->dw_attr == DW_AT_data_member_location
8141 && csize >= 4)
8142 size += size_of_uleb128 (AT_unsigned (a));
8143 else
8144 size += csize;
8146 break;
8147 case dw_val_class_const_double:
8148 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
8149 if (HOST_BITS_PER_WIDE_INT >= 64)
8150 size++; /* block */
8151 break;
8152 case dw_val_class_wide_int:
8153 size += (get_full_len (*a->dw_attr_val.v.val_wide)
8154 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
8155 if (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT
8156 > 64)
8157 size++; /* block */
8158 break;
8159 case dw_val_class_vec:
8160 size += constant_size (a->dw_attr_val.v.val_vec.length
8161 * a->dw_attr_val.v.val_vec.elt_size)
8162 + a->dw_attr_val.v.val_vec.length
8163 * a->dw_attr_val.v.val_vec.elt_size; /* block */
8164 break;
8165 case dw_val_class_flag:
8166 if (dwarf_version >= 4)
8167 /* Currently all add_AT_flag calls pass in 1 as last argument,
8168 so DW_FORM_flag_present can be used. If that ever changes,
8169 we'll need to use DW_FORM_flag and have some optimization
8170 in build_abbrev_table that will change those to
8171 DW_FORM_flag_present if it is set to 1 in all DIEs using
8172 the same abbrev entry. */
8173 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8174 else
8175 size += 1;
8176 break;
8177 case dw_val_class_die_ref:
8178 if (AT_ref_external (a))
8180 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
8181 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
8182 is sized by target address length, whereas in DWARF3
8183 it's always sized as an offset. */
8184 if (use_debug_types)
8185 size += DWARF_TYPE_SIGNATURE_SIZE;
8186 else if (dwarf_version == 2)
8187 size += DWARF2_ADDR_SIZE;
8188 else
8189 size += DWARF_OFFSET_SIZE;
8191 else
8192 size += DWARF_OFFSET_SIZE;
8193 break;
8194 case dw_val_class_fde_ref:
8195 size += DWARF_OFFSET_SIZE;
8196 break;
8197 case dw_val_class_lbl_id:
8198 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8200 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8201 size += size_of_uleb128 (AT_index (a));
8203 else
8204 size += DWARF2_ADDR_SIZE;
8205 break;
8206 case dw_val_class_lineptr:
8207 case dw_val_class_macptr:
8208 size += DWARF_OFFSET_SIZE;
8209 break;
8210 case dw_val_class_str:
8211 form = AT_string_form (a);
8212 if (form == DW_FORM_strp)
8213 size += DWARF_OFFSET_SIZE;
8214 else if (form == DW_FORM_GNU_str_index)
8215 size += size_of_uleb128 (AT_index (a));
8216 else
8217 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
8218 break;
8219 case dw_val_class_file:
8220 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
8221 break;
8222 case dw_val_class_data8:
8223 size += 8;
8224 break;
8225 case dw_val_class_vms_delta:
8226 size += DWARF_OFFSET_SIZE;
8227 break;
8228 case dw_val_class_high_pc:
8229 size += DWARF2_ADDR_SIZE;
8230 break;
8231 default:
8232 gcc_unreachable ();
8236 return size;
8239 /* Size the debugging information associated with a given DIE. Visits the
8240 DIE's children recursively. Updates the global variable next_die_offset, on
8241 each time through. Uses the current value of next_die_offset to update the
8242 die_offset field in each DIE. */
8244 static void
8245 calc_die_sizes (dw_die_ref die)
8247 dw_die_ref c;
8249 gcc_assert (die->die_offset == 0
8250 || (unsigned long int) die->die_offset == next_die_offset);
8251 die->die_offset = next_die_offset;
8252 next_die_offset += size_of_die (die);
8254 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
8256 if (die->die_child != NULL)
8257 /* Count the null byte used to terminate sibling lists. */
8258 next_die_offset += 1;
8261 /* Size just the base type children at the start of the CU.
8262 This is needed because build_abbrev needs to size locs
8263 and sizing of type based stack ops needs to know die_offset
8264 values for the base types. */
8266 static void
8267 calc_base_type_die_sizes (void)
8269 unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8270 unsigned int i;
8271 dw_die_ref base_type;
8272 #if ENABLE_ASSERT_CHECKING
8273 dw_die_ref prev = comp_unit_die ()->die_child;
8274 #endif
8276 die_offset += size_of_die (comp_unit_die ());
8277 for (i = 0; base_types.iterate (i, &base_type); i++)
8279 #if ENABLE_ASSERT_CHECKING
8280 gcc_assert (base_type->die_offset == 0
8281 && prev->die_sib == base_type
8282 && base_type->die_child == NULL
8283 && base_type->die_abbrev);
8284 prev = base_type;
8285 #endif
8286 base_type->die_offset = die_offset;
8287 die_offset += size_of_die (base_type);
8291 /* Set the marks for a die and its children. We do this so
8292 that we know whether or not a reference needs to use FORM_ref_addr; only
8293 DIEs in the same CU will be marked. We used to clear out the offset
8294 and use that as the flag, but ran into ordering problems. */
8296 static void
8297 mark_dies (dw_die_ref die)
8299 dw_die_ref c;
8301 gcc_assert (!die->die_mark);
8303 die->die_mark = 1;
8304 FOR_EACH_CHILD (die, c, mark_dies (c));
8307 /* Clear the marks for a die and its children. */
8309 static void
8310 unmark_dies (dw_die_ref die)
8312 dw_die_ref c;
8314 if (! use_debug_types)
8315 gcc_assert (die->die_mark);
8317 die->die_mark = 0;
8318 FOR_EACH_CHILD (die, c, unmark_dies (c));
8321 /* Clear the marks for a die, its children and referred dies. */
8323 static void
8324 unmark_all_dies (dw_die_ref die)
8326 dw_die_ref c;
8327 dw_attr_ref a;
8328 unsigned ix;
8330 if (!die->die_mark)
8331 return;
8332 die->die_mark = 0;
8334 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
8336 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8337 if (AT_class (a) == dw_val_class_die_ref)
8338 unmark_all_dies (AT_ref (a));
8341 /* Calculate if the entry should appear in the final output file. It may be
8342 from a pruned a type. */
8344 static bool
8345 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
8347 /* By limiting gnu pubnames to definitions only, gold can generate a
8348 gdb index without entries for declarations, which don't include
8349 enough information to be useful. */
8350 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
8351 return false;
8353 if (table == pubname_table)
8355 /* Enumerator names are part of the pubname table, but the
8356 parent DW_TAG_enumeration_type die may have been pruned.
8357 Don't output them if that is the case. */
8358 if (p->die->die_tag == DW_TAG_enumerator &&
8359 (p->die->die_parent == NULL
8360 || !p->die->die_parent->die_perennial_p))
8361 return false;
8363 /* Everything else in the pubname table is included. */
8364 return true;
8367 /* The pubtypes table shouldn't include types that have been
8368 pruned. */
8369 return (p->die->die_offset != 0
8370 || !flag_eliminate_unused_debug_types);
8373 /* Return the size of the .debug_pubnames or .debug_pubtypes table
8374 generated for the compilation unit. */
8376 static unsigned long
8377 size_of_pubnames (vec<pubname_entry, va_gc> *names)
8379 unsigned long size;
8380 unsigned i;
8381 pubname_ref p;
8382 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
8384 size = DWARF_PUBNAMES_HEADER_SIZE;
8385 FOR_EACH_VEC_ELT (*names, i, p)
8386 if (include_pubname_in_output (names, p))
8387 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
8389 size += DWARF_OFFSET_SIZE;
8390 return size;
8393 /* Return the size of the information in the .debug_aranges section. */
8395 static unsigned long
8396 size_of_aranges (void)
8398 unsigned long size;
8400 size = DWARF_ARANGES_HEADER_SIZE;
8402 /* Count the address/length pair for this compilation unit. */
8403 if (text_section_used)
8404 size += 2 * DWARF2_ADDR_SIZE;
8405 if (cold_text_section_used)
8406 size += 2 * DWARF2_ADDR_SIZE;
8407 if (have_multiple_function_sections)
8409 unsigned fde_idx;
8410 dw_fde_ref fde;
8412 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
8414 if (DECL_IGNORED_P (fde->decl))
8415 continue;
8416 if (!fde->in_std_section)
8417 size += 2 * DWARF2_ADDR_SIZE;
8418 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
8419 size += 2 * DWARF2_ADDR_SIZE;
8423 /* Count the two zero words used to terminated the address range table. */
8424 size += 2 * DWARF2_ADDR_SIZE;
8425 return size;
8428 /* Select the encoding of an attribute value. */
8430 static enum dwarf_form
8431 value_format (dw_attr_ref a)
8433 switch (AT_class (a))
8435 case dw_val_class_addr:
8436 /* Only very few attributes allow DW_FORM_addr. */
8437 switch (a->dw_attr)
8439 case DW_AT_low_pc:
8440 case DW_AT_high_pc:
8441 case DW_AT_entry_pc:
8442 case DW_AT_trampoline:
8443 return (AT_index (a) == NOT_INDEXED
8444 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8445 default:
8446 break;
8448 switch (DWARF2_ADDR_SIZE)
8450 case 1:
8451 return DW_FORM_data1;
8452 case 2:
8453 return DW_FORM_data2;
8454 case 4:
8455 return DW_FORM_data4;
8456 case 8:
8457 return DW_FORM_data8;
8458 default:
8459 gcc_unreachable ();
8461 case dw_val_class_range_list:
8462 case dw_val_class_loc_list:
8463 if (dwarf_version >= 4)
8464 return DW_FORM_sec_offset;
8465 /* FALLTHRU */
8466 case dw_val_class_vms_delta:
8467 case dw_val_class_offset:
8468 switch (DWARF_OFFSET_SIZE)
8470 case 4:
8471 return DW_FORM_data4;
8472 case 8:
8473 return DW_FORM_data8;
8474 default:
8475 gcc_unreachable ();
8477 case dw_val_class_loc:
8478 if (dwarf_version >= 4)
8479 return DW_FORM_exprloc;
8480 switch (constant_size (size_of_locs (AT_loc (a))))
8482 case 1:
8483 return DW_FORM_block1;
8484 case 2:
8485 return DW_FORM_block2;
8486 case 4:
8487 return DW_FORM_block4;
8488 default:
8489 gcc_unreachable ();
8491 case dw_val_class_const:
8492 return DW_FORM_sdata;
8493 case dw_val_class_unsigned_const:
8494 switch (constant_size (AT_unsigned (a)))
8496 case 1:
8497 return DW_FORM_data1;
8498 case 2:
8499 return DW_FORM_data2;
8500 case 4:
8501 /* In DWARF3 DW_AT_data_member_location with
8502 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
8503 constant, so we need to use DW_FORM_udata if we need
8504 a large constant. */
8505 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8506 return DW_FORM_udata;
8507 return DW_FORM_data4;
8508 case 8:
8509 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8510 return DW_FORM_udata;
8511 return DW_FORM_data8;
8512 default:
8513 gcc_unreachable ();
8515 case dw_val_class_const_double:
8516 switch (HOST_BITS_PER_WIDE_INT)
8518 case 8:
8519 return DW_FORM_data2;
8520 case 16:
8521 return DW_FORM_data4;
8522 case 32:
8523 return DW_FORM_data8;
8524 case 64:
8525 default:
8526 return DW_FORM_block1;
8528 case dw_val_class_wide_int:
8529 switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
8531 case 8:
8532 return DW_FORM_data1;
8533 case 16:
8534 return DW_FORM_data2;
8535 case 32:
8536 return DW_FORM_data4;
8537 case 64:
8538 return DW_FORM_data8;
8539 default:
8540 return DW_FORM_block1;
8542 case dw_val_class_vec:
8543 switch (constant_size (a->dw_attr_val.v.val_vec.length
8544 * a->dw_attr_val.v.val_vec.elt_size))
8546 case 1:
8547 return DW_FORM_block1;
8548 case 2:
8549 return DW_FORM_block2;
8550 case 4:
8551 return DW_FORM_block4;
8552 default:
8553 gcc_unreachable ();
8555 case dw_val_class_flag:
8556 if (dwarf_version >= 4)
8558 /* Currently all add_AT_flag calls pass in 1 as last argument,
8559 so DW_FORM_flag_present can be used. If that ever changes,
8560 we'll need to use DW_FORM_flag and have some optimization
8561 in build_abbrev_table that will change those to
8562 DW_FORM_flag_present if it is set to 1 in all DIEs using
8563 the same abbrev entry. */
8564 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8565 return DW_FORM_flag_present;
8567 return DW_FORM_flag;
8568 case dw_val_class_die_ref:
8569 if (AT_ref_external (a))
8570 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
8571 else
8572 return DW_FORM_ref;
8573 case dw_val_class_fde_ref:
8574 return DW_FORM_data;
8575 case dw_val_class_lbl_id:
8576 return (AT_index (a) == NOT_INDEXED
8577 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8578 case dw_val_class_lineptr:
8579 case dw_val_class_macptr:
8580 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
8581 case dw_val_class_str:
8582 return AT_string_form (a);
8583 case dw_val_class_file:
8584 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8586 case 1:
8587 return DW_FORM_data1;
8588 case 2:
8589 return DW_FORM_data2;
8590 case 4:
8591 return DW_FORM_data4;
8592 default:
8593 gcc_unreachable ();
8596 case dw_val_class_data8:
8597 return DW_FORM_data8;
8599 case dw_val_class_high_pc:
8600 switch (DWARF2_ADDR_SIZE)
8602 case 1:
8603 return DW_FORM_data1;
8604 case 2:
8605 return DW_FORM_data2;
8606 case 4:
8607 return DW_FORM_data4;
8608 case 8:
8609 return DW_FORM_data8;
8610 default:
8611 gcc_unreachable ();
8614 default:
8615 gcc_unreachable ();
8619 /* Output the encoding of an attribute value. */
8621 static void
8622 output_value_format (dw_attr_ref a)
8624 enum dwarf_form form = value_format (a);
8626 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8629 /* Given a die and id, produce the appropriate abbreviations. */
8631 static void
8632 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
8634 unsigned ix;
8635 dw_attr_ref a_attr;
8637 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8638 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8639 dwarf_tag_name (abbrev->die_tag));
8641 if (abbrev->die_child != NULL)
8642 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8643 else
8644 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8646 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
8648 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8649 dwarf_attr_name (a_attr->dw_attr));
8650 output_value_format (a_attr);
8653 dw2_asm_output_data (1, 0, NULL);
8654 dw2_asm_output_data (1, 0, NULL);
8658 /* Output the .debug_abbrev section which defines the DIE abbreviation
8659 table. */
8661 static void
8662 output_abbrev_section (void)
8664 unsigned long abbrev_id;
8666 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8667 output_die_abbrevs (abbrev_id, abbrev_die_table[abbrev_id]);
8669 /* Terminate the table. */
8670 dw2_asm_output_data (1, 0, NULL);
8673 /* Output a symbol we can use to refer to this DIE from another CU. */
8675 static inline void
8676 output_die_symbol (dw_die_ref die)
8678 const char *sym = die->die_id.die_symbol;
8680 gcc_assert (!die->comdat_type_p);
8682 if (sym == 0)
8683 return;
8685 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8686 /* We make these global, not weak; if the target doesn't support
8687 .linkonce, it doesn't support combining the sections, so debugging
8688 will break. */
8689 targetm.asm_out.globalize_label (asm_out_file, sym);
8691 ASM_OUTPUT_LABEL (asm_out_file, sym);
8694 /* Return a new location list, given the begin and end range, and the
8695 expression. */
8697 static inline dw_loc_list_ref
8698 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8699 const char *section)
8701 dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
8703 retlist->begin = begin;
8704 retlist->begin_entry = NULL;
8705 retlist->end = end;
8706 retlist->expr = expr;
8707 retlist->section = section;
8709 return retlist;
8712 /* Generate a new internal symbol for this location list node, if it
8713 hasn't got one yet. */
8715 static inline void
8716 gen_llsym (dw_loc_list_ref list)
8718 gcc_assert (!list->ll_symbol);
8719 list->ll_symbol = gen_internal_sym ("LLST");
8722 /* Output the location list given to us. */
8724 static void
8725 output_loc_list (dw_loc_list_ref list_head)
8727 dw_loc_list_ref curr = list_head;
8729 if (list_head->emitted)
8730 return;
8731 list_head->emitted = true;
8733 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8735 /* Walk the location list, and output each range + expression. */
8736 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8738 unsigned long size;
8739 /* Don't output an entry that starts and ends at the same address. */
8740 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
8741 continue;
8742 size = size_of_locs (curr->expr);
8743 /* If the expression is too large, drop it on the floor. We could
8744 perhaps put it into DW_TAG_dwarf_procedure and refer to that
8745 in the expression, but >= 64KB expressions for a single value
8746 in a single range are unlikely very useful. */
8747 if (size > 0xffff)
8748 continue;
8749 if (dwarf_split_debug_info)
8751 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
8752 "Location list start/length entry (%s)",
8753 list_head->ll_symbol);
8754 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
8755 "Location list range start index (%s)",
8756 curr->begin);
8757 /* The length field is 4 bytes. If we ever need to support
8758 an 8-byte length, we can add a new DW_LLE code or fall back
8759 to DW_LLE_GNU_start_end_entry. */
8760 dw2_asm_output_delta (4, curr->end, curr->begin,
8761 "Location list range length (%s)",
8762 list_head->ll_symbol);
8764 else if (!have_multiple_function_sections)
8766 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8767 "Location list begin address (%s)",
8768 list_head->ll_symbol);
8769 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8770 "Location list end address (%s)",
8771 list_head->ll_symbol);
8773 else
8775 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8776 "Location list begin address (%s)",
8777 list_head->ll_symbol);
8778 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8779 "Location list end address (%s)",
8780 list_head->ll_symbol);
8783 /* Output the block length for this list of location operations. */
8784 gcc_assert (size <= 0xffff);
8785 dw2_asm_output_data (2, size, "%s", "Location expression size");
8787 output_loc_sequence (curr->expr, -1);
8790 if (dwarf_split_debug_info)
8791 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
8792 "Location list terminator (%s)",
8793 list_head->ll_symbol);
8794 else
8796 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8797 "Location list terminator begin (%s)",
8798 list_head->ll_symbol);
8799 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8800 "Location list terminator end (%s)",
8801 list_head->ll_symbol);
8805 /* Output a range_list offset into the debug_range section. Emit a
8806 relocated reference if val_entry is NULL, otherwise, emit an
8807 indirect reference. */
8809 static void
8810 output_range_list_offset (dw_attr_ref a)
8812 const char *name = dwarf_attr_name (a->dw_attr);
8814 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
8816 char *p = strchr (ranges_section_label, '\0');
8817 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX, a->dw_attr_val.v.val_offset);
8818 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8819 debug_ranges_section, "%s", name);
8820 *p = '\0';
8822 else
8823 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8824 "%s (offset from %s)", name, ranges_section_label);
8827 /* Output the offset into the debug_loc section. */
8829 static void
8830 output_loc_list_offset (dw_attr_ref a)
8832 char *sym = AT_loc_list (a)->ll_symbol;
8834 gcc_assert (sym);
8835 if (dwarf_split_debug_info)
8836 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
8837 "%s", dwarf_attr_name (a->dw_attr));
8838 else
8839 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8840 "%s", dwarf_attr_name (a->dw_attr));
8843 /* Output an attribute's index or value appropriately. */
8845 static void
8846 output_attr_index_or_value (dw_attr_ref a)
8848 const char *name = dwarf_attr_name (a->dw_attr);
8850 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8852 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
8853 return;
8855 switch (AT_class (a))
8857 case dw_val_class_addr:
8858 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8859 break;
8860 case dw_val_class_high_pc:
8861 case dw_val_class_lbl_id:
8862 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8863 break;
8864 case dw_val_class_loc_list:
8865 output_loc_list_offset (a);
8866 break;
8867 default:
8868 gcc_unreachable ();
8872 /* Output a type signature. */
8874 static inline void
8875 output_signature (const char *sig, const char *name)
8877 int i;
8879 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8880 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
8883 /* Output the DIE and its attributes. Called recursively to generate
8884 the definitions of each child DIE. */
8886 static void
8887 output_die (dw_die_ref die)
8889 dw_attr_ref a;
8890 dw_die_ref c;
8891 unsigned long size;
8892 unsigned ix;
8894 /* If someone in another CU might refer to us, set up a symbol for
8895 them to point to. */
8896 if (! die->comdat_type_p && die->die_id.die_symbol)
8897 output_die_symbol (die);
8899 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
8900 (unsigned long)die->die_offset,
8901 dwarf_tag_name (die->die_tag));
8903 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8905 const char *name = dwarf_attr_name (a->dw_attr);
8907 switch (AT_class (a))
8909 case dw_val_class_addr:
8910 output_attr_index_or_value (a);
8911 break;
8913 case dw_val_class_offset:
8914 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8915 "%s", name);
8916 break;
8918 case dw_val_class_range_list:
8919 output_range_list_offset (a);
8920 break;
8922 case dw_val_class_loc:
8923 size = size_of_locs (AT_loc (a));
8925 /* Output the block length for this list of location operations. */
8926 if (dwarf_version >= 4)
8927 dw2_asm_output_data_uleb128 (size, "%s", name);
8928 else
8929 dw2_asm_output_data (constant_size (size), size, "%s", name);
8931 output_loc_sequence (AT_loc (a), -1);
8932 break;
8934 case dw_val_class_const:
8935 /* ??? It would be slightly more efficient to use a scheme like is
8936 used for unsigned constants below, but gdb 4.x does not sign
8937 extend. Gdb 5.x does sign extend. */
8938 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8939 break;
8941 case dw_val_class_unsigned_const:
8943 int csize = constant_size (AT_unsigned (a));
8944 if (dwarf_version == 3
8945 && a->dw_attr == DW_AT_data_member_location
8946 && csize >= 4)
8947 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
8948 else
8949 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
8951 break;
8953 case dw_val_class_const_double:
8955 unsigned HOST_WIDE_INT first, second;
8957 if (HOST_BITS_PER_WIDE_INT >= 64)
8958 dw2_asm_output_data (1,
8959 HOST_BITS_PER_DOUBLE_INT
8960 / HOST_BITS_PER_CHAR,
8961 NULL);
8963 if (WORDS_BIG_ENDIAN)
8965 first = a->dw_attr_val.v.val_double.high;
8966 second = a->dw_attr_val.v.val_double.low;
8968 else
8970 first = a->dw_attr_val.v.val_double.low;
8971 second = a->dw_attr_val.v.val_double.high;
8974 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8975 first, "%s", name);
8976 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8977 second, NULL);
8979 break;
8981 case dw_val_class_wide_int:
8983 int i;
8984 int len = get_full_len (*a->dw_attr_val.v.val_wide);
8985 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
8986 if (len * HOST_BITS_PER_WIDE_INT > 64)
8987 dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide) * l,
8988 NULL);
8990 if (WORDS_BIG_ENDIAN)
8991 for (i = len - 1; i >= 0; --i)
8993 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
8994 "%s", name);
8995 name = NULL;
8997 else
8998 for (i = 0; i < len; ++i)
9000 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
9001 "%s", name);
9002 name = NULL;
9005 break;
9007 case dw_val_class_vec:
9009 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
9010 unsigned int len = a->dw_attr_val.v.val_vec.length;
9011 unsigned int i;
9012 unsigned char *p;
9014 dw2_asm_output_data (constant_size (len * elt_size),
9015 len * elt_size, "%s", name);
9016 if (elt_size > sizeof (HOST_WIDE_INT))
9018 elt_size /= 2;
9019 len *= 2;
9021 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
9022 i < len;
9023 i++, p += elt_size)
9024 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
9025 "fp or vector constant word %u", i);
9026 break;
9029 case dw_val_class_flag:
9030 if (dwarf_version >= 4)
9032 /* Currently all add_AT_flag calls pass in 1 as last argument,
9033 so DW_FORM_flag_present can be used. If that ever changes,
9034 we'll need to use DW_FORM_flag and have some optimization
9035 in build_abbrev_table that will change those to
9036 DW_FORM_flag_present if it is set to 1 in all DIEs using
9037 the same abbrev entry. */
9038 gcc_assert (AT_flag (a) == 1);
9039 if (flag_debug_asm)
9040 fprintf (asm_out_file, "\t\t\t%s %s\n",
9041 ASM_COMMENT_START, name);
9042 break;
9044 dw2_asm_output_data (1, AT_flag (a), "%s", name);
9045 break;
9047 case dw_val_class_loc_list:
9048 output_attr_index_or_value (a);
9049 break;
9051 case dw_val_class_die_ref:
9052 if (AT_ref_external (a))
9054 if (AT_ref (a)->comdat_type_p)
9056 comdat_type_node_ref type_node =
9057 AT_ref (a)->die_id.die_type_node;
9059 gcc_assert (type_node);
9060 output_signature (type_node->signature, name);
9062 else
9064 const char *sym = AT_ref (a)->die_id.die_symbol;
9065 int size;
9067 gcc_assert (sym);
9068 /* In DWARF2, DW_FORM_ref_addr is sized by target address
9069 length, whereas in DWARF3 it's always sized as an
9070 offset. */
9071 if (dwarf_version == 2)
9072 size = DWARF2_ADDR_SIZE;
9073 else
9074 size = DWARF_OFFSET_SIZE;
9075 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
9076 name);
9079 else
9081 gcc_assert (AT_ref (a)->die_offset);
9082 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
9083 "%s", name);
9085 break;
9087 case dw_val_class_fde_ref:
9089 char l1[20];
9091 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
9092 a->dw_attr_val.v.val_fde_index * 2);
9093 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
9094 "%s", name);
9096 break;
9098 case dw_val_class_vms_delta:
9099 #ifdef ASM_OUTPUT_DWARF_VMS_DELTA
9100 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
9101 AT_vms_delta2 (a), AT_vms_delta1 (a),
9102 "%s", name);
9103 #else
9104 dw2_asm_output_delta (DWARF_OFFSET_SIZE,
9105 AT_vms_delta2 (a), AT_vms_delta1 (a),
9106 "%s", name);
9107 #endif
9108 break;
9110 case dw_val_class_lbl_id:
9111 output_attr_index_or_value (a);
9112 break;
9114 case dw_val_class_lineptr:
9115 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
9116 debug_line_section, "%s", name);
9117 break;
9119 case dw_val_class_macptr:
9120 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
9121 debug_macinfo_section, "%s", name);
9122 break;
9124 case dw_val_class_str:
9125 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
9126 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
9127 a->dw_attr_val.v.val_str->label,
9128 debug_str_section,
9129 "%s: \"%s\"", name, AT_string (a));
9130 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
9131 dw2_asm_output_data_uleb128 (AT_index (a),
9132 "%s: \"%s\"", name, AT_string (a));
9133 else
9134 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
9135 break;
9137 case dw_val_class_file:
9139 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
9141 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
9142 a->dw_attr_val.v.val_file->filename);
9143 break;
9146 case dw_val_class_data8:
9148 int i;
9150 for (i = 0; i < 8; i++)
9151 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
9152 i == 0 ? "%s" : NULL, name);
9153 break;
9156 case dw_val_class_high_pc:
9157 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
9158 get_AT_low_pc (die), "DW_AT_high_pc");
9159 break;
9161 default:
9162 gcc_unreachable ();
9166 FOR_EACH_CHILD (die, c, output_die (c));
9168 /* Add null byte to terminate sibling list. */
9169 if (die->die_child != NULL)
9170 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
9171 (unsigned long) die->die_offset);
9174 /* Output the compilation unit that appears at the beginning of the
9175 .debug_info section, and precedes the DIE descriptions. */
9177 static void
9178 output_compilation_unit_header (void)
9180 /* We don't support actual DWARFv5 units yet, we just use some
9181 DWARFv5 draft DIE tags in DWARFv4 format. */
9182 int ver = dwarf_version < 5 ? dwarf_version : 4;
9184 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9185 dw2_asm_output_data (4, 0xffffffff,
9186 "Initial length escape value indicating 64-bit DWARF extension");
9187 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9188 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
9189 "Length of Compilation Unit Info");
9190 dw2_asm_output_data (2, ver, "DWARF version number");
9191 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
9192 debug_abbrev_section,
9193 "Offset Into Abbrev. Section");
9194 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9197 /* Output the compilation unit DIE and its children. */
9199 static void
9200 output_comp_unit (dw_die_ref die, int output_if_empty)
9202 const char *secname, *oldsym;
9203 char *tmp;
9205 /* Unless we are outputting main CU, we may throw away empty ones. */
9206 if (!output_if_empty && die->die_child == NULL)
9207 return;
9209 /* Even if there are no children of this DIE, we must output the information
9210 about the compilation unit. Otherwise, on an empty translation unit, we
9211 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
9212 will then complain when examining the file. First mark all the DIEs in
9213 this CU so we know which get local refs. */
9214 mark_dies (die);
9216 external_ref_hash_type *extern_map = optimize_external_refs (die);
9218 build_abbrev_table (die, extern_map);
9220 delete extern_map;
9222 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
9223 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
9224 calc_die_sizes (die);
9226 oldsym = die->die_id.die_symbol;
9227 if (oldsym)
9229 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
9231 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
9232 secname = tmp;
9233 die->die_id.die_symbol = NULL;
9234 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9236 else
9238 switch_to_section (debug_info_section);
9239 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
9240 info_section_emitted = true;
9243 /* Output debugging information. */
9244 output_compilation_unit_header ();
9245 output_die (die);
9247 /* Leave the marks on the main CU, so we can check them in
9248 output_pubnames. */
9249 if (oldsym)
9251 unmark_dies (die);
9252 die->die_id.die_symbol = oldsym;
9256 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
9257 and .debug_pubtypes. This is configured per-target, but can be
9258 overridden by the -gpubnames or -gno-pubnames options. */
9260 static inline bool
9261 want_pubnames (void)
9263 if (debug_info_level <= DINFO_LEVEL_TERSE)
9264 return false;
9265 if (debug_generate_pub_sections != -1)
9266 return debug_generate_pub_sections;
9267 return targetm.want_debug_pub_sections;
9270 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
9272 static void
9273 add_AT_pubnames (dw_die_ref die)
9275 if (want_pubnames ())
9276 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
9279 /* Add a string attribute value to a skeleton DIE. */
9281 static inline void
9282 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
9283 const char *str)
9285 dw_attr_node attr;
9286 struct indirect_string_node *node;
9288 if (! skeleton_debug_str_hash)
9289 skeleton_debug_str_hash
9290 = hash_table<indirect_string_hasher>::create_ggc (10);
9292 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
9293 find_string_form (node);
9294 if (node->form == DW_FORM_GNU_str_index)
9295 node->form = DW_FORM_strp;
9297 attr.dw_attr = attr_kind;
9298 attr.dw_attr_val.val_class = dw_val_class_str;
9299 attr.dw_attr_val.val_entry = NULL;
9300 attr.dw_attr_val.v.val_str = node;
9301 add_dwarf_attr (die, &attr);
9304 /* Helper function to generate top-level dies for skeleton debug_info and
9305 debug_types. */
9307 static void
9308 add_top_level_skeleton_die_attrs (dw_die_ref die)
9310 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
9311 const char *comp_dir = comp_dir_string ();
9313 add_skeleton_AT_string (die, DW_AT_GNU_dwo_name, dwo_file_name);
9314 if (comp_dir != NULL)
9315 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
9316 add_AT_pubnames (die);
9317 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
9320 /* Output skeleton debug sections that point to the dwo file. */
9322 static void
9323 output_skeleton_debug_sections (dw_die_ref comp_unit)
9325 /* We don't support actual DWARFv5 units yet, we just use some
9326 DWARFv5 draft DIE tags in DWARFv4 format. */
9327 int ver = dwarf_version < 5 ? dwarf_version : 4;
9329 /* These attributes will be found in the full debug_info section. */
9330 remove_AT (comp_unit, DW_AT_producer);
9331 remove_AT (comp_unit, DW_AT_language);
9333 switch_to_section (debug_skeleton_info_section);
9334 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
9336 /* Produce the skeleton compilation-unit header. This one differs enough from
9337 a normal CU header that it's better not to call output_compilation_unit
9338 header. */
9339 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9340 dw2_asm_output_data (4, 0xffffffff,
9341 "Initial length escape value indicating 64-bit DWARF extension");
9343 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9344 DWARF_COMPILE_UNIT_HEADER_SIZE
9345 - DWARF_INITIAL_LENGTH_SIZE
9346 + size_of_die (comp_unit),
9347 "Length of Compilation Unit Info");
9348 dw2_asm_output_data (2, ver, "DWARF version number");
9349 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
9350 debug_abbrev_section,
9351 "Offset Into Abbrev. Section");
9352 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9354 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
9355 output_die (comp_unit);
9357 /* Build the skeleton debug_abbrev section. */
9358 switch_to_section (debug_skeleton_abbrev_section);
9359 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
9361 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
9363 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
9366 /* Output a comdat type unit DIE and its children. */
9368 static void
9369 output_comdat_type_unit (comdat_type_node *node)
9371 const char *secname;
9372 char *tmp;
9373 int i;
9374 #if defined (OBJECT_FORMAT_ELF)
9375 tree comdat_key;
9376 #endif
9378 /* First mark all the DIEs in this CU so we know which get local refs. */
9379 mark_dies (node->root_die);
9381 external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
9383 build_abbrev_table (node->root_die, extern_map);
9385 delete extern_map;
9386 extern_map = NULL;
9388 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
9389 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
9390 calc_die_sizes (node->root_die);
9392 #if defined (OBJECT_FORMAT_ELF)
9393 if (!dwarf_split_debug_info)
9394 secname = ".debug_types";
9395 else
9396 secname = ".debug_types.dwo";
9398 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9399 sprintf (tmp, "wt.");
9400 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9401 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
9402 comdat_key = get_identifier (tmp);
9403 targetm.asm_out.named_section (secname,
9404 SECTION_DEBUG | SECTION_LINKONCE,
9405 comdat_key);
9406 #else
9407 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9408 sprintf (tmp, ".gnu.linkonce.wt.");
9409 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9410 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
9411 secname = tmp;
9412 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9413 #endif
9415 /* Output debugging information. */
9416 output_compilation_unit_header ();
9417 output_signature (node->signature, "Type Signature");
9418 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
9419 "Offset to Type DIE");
9420 output_die (node->root_die);
9422 unmark_dies (node->root_die);
9425 /* Return the DWARF2/3 pubname associated with a decl. */
9427 static const char *
9428 dwarf2_name (tree decl, int scope)
9430 if (DECL_NAMELESS (decl))
9431 return NULL;
9432 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
9435 /* Add a new entry to .debug_pubnames if appropriate. */
9437 static void
9438 add_pubname_string (const char *str, dw_die_ref die)
9440 pubname_entry e;
9442 e.die = die;
9443 e.name = xstrdup (str);
9444 vec_safe_push (pubname_table, e);
9447 static void
9448 add_pubname (tree decl, dw_die_ref die)
9450 if (!want_pubnames ())
9451 return;
9453 /* Don't add items to the table when we expect that the consumer will have
9454 just read the enclosing die. For example, if the consumer is looking at a
9455 class_member, it will either be inside the class already, or will have just
9456 looked up the class to find the member. Either way, searching the class is
9457 faster than searching the index. */
9458 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
9459 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9461 const char *name = dwarf2_name (decl, 1);
9463 if (name)
9464 add_pubname_string (name, die);
9468 /* Add an enumerator to the pubnames section. */
9470 static void
9471 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
9473 pubname_entry e;
9475 gcc_assert (scope_name);
9476 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
9477 e.die = die;
9478 vec_safe_push (pubname_table, e);
9481 /* Add a new entry to .debug_pubtypes if appropriate. */
9483 static void
9484 add_pubtype (tree decl, dw_die_ref die)
9486 pubname_entry e;
9488 if (!want_pubnames ())
9489 return;
9491 if ((TREE_PUBLIC (decl)
9492 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9493 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
9495 tree scope = NULL;
9496 const char *scope_name = "";
9497 const char *sep = is_cxx () ? "::" : ".";
9498 const char *name;
9500 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
9501 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
9503 scope_name = lang_hooks.dwarf_name (scope, 1);
9504 if (scope_name != NULL && scope_name[0] != '\0')
9505 scope_name = concat (scope_name, sep, NULL);
9506 else
9507 scope_name = "";
9510 if (TYPE_P (decl))
9511 name = type_tag (decl);
9512 else
9513 name = lang_hooks.dwarf_name (decl, 1);
9515 /* If we don't have a name for the type, there's no point in adding
9516 it to the table. */
9517 if (name != NULL && name[0] != '\0')
9519 e.die = die;
9520 e.name = concat (scope_name, name, NULL);
9521 vec_safe_push (pubtype_table, e);
9524 /* Although it might be more consistent to add the pubinfo for the
9525 enumerators as their dies are created, they should only be added if the
9526 enum type meets the criteria above. So rather than re-check the parent
9527 enum type whenever an enumerator die is created, just output them all
9528 here. This isn't protected by the name conditional because anonymous
9529 enums don't have names. */
9530 if (die->die_tag == DW_TAG_enumeration_type)
9532 dw_die_ref c;
9534 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
9539 /* Output a single entry in the pubnames table. */
9541 static void
9542 output_pubname (dw_offset die_offset, pubname_entry *entry)
9544 dw_die_ref die = entry->die;
9545 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
9547 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
9549 if (debug_generate_pub_sections == 2)
9551 /* This logic follows gdb's method for determining the value of the flag
9552 byte. */
9553 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
9554 switch (die->die_tag)
9556 case DW_TAG_typedef:
9557 case DW_TAG_base_type:
9558 case DW_TAG_subrange_type:
9559 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9560 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9561 break;
9562 case DW_TAG_enumerator:
9563 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9564 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9565 if (!is_cxx () && !is_java ())
9566 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9567 break;
9568 case DW_TAG_subprogram:
9569 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9570 GDB_INDEX_SYMBOL_KIND_FUNCTION);
9571 if (!is_ada ())
9572 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9573 break;
9574 case DW_TAG_constant:
9575 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9576 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9577 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9578 break;
9579 case DW_TAG_variable:
9580 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9581 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9582 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9583 break;
9584 case DW_TAG_namespace:
9585 case DW_TAG_imported_declaration:
9586 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9587 break;
9588 case DW_TAG_class_type:
9589 case DW_TAG_interface_type:
9590 case DW_TAG_structure_type:
9591 case DW_TAG_union_type:
9592 case DW_TAG_enumeration_type:
9593 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9594 if (!is_cxx () && !is_java ())
9595 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9596 break;
9597 default:
9598 /* An unusual tag. Leave the flag-byte empty. */
9599 break;
9601 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
9602 "GDB-index flags");
9605 dw2_asm_output_nstring (entry->name, -1, "external name");
9609 /* Output the public names table used to speed up access to externally
9610 visible names; or the public types table used to find type definitions. */
9612 static void
9613 output_pubnames (vec<pubname_entry, va_gc> *names)
9615 unsigned i;
9616 unsigned long pubnames_length = size_of_pubnames (names);
9617 pubname_ref pub;
9619 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9620 dw2_asm_output_data (4, 0xffffffff,
9621 "Initial length escape value indicating 64-bit DWARF extension");
9622 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length, "Pub Info Length");
9624 /* Version number for pubnames/pubtypes is independent of dwarf version. */
9625 dw2_asm_output_data (2, 2, "DWARF Version");
9627 if (dwarf_split_debug_info)
9628 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9629 debug_skeleton_info_section,
9630 "Offset of Compilation Unit Info");
9631 else
9632 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9633 debug_info_section,
9634 "Offset of Compilation Unit Info");
9635 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
9636 "Compilation Unit Length");
9638 FOR_EACH_VEC_ELT (*names, i, pub)
9640 if (include_pubname_in_output (names, pub))
9642 dw_offset die_offset = pub->die->die_offset;
9644 /* We shouldn't see pubnames for DIEs outside of the main CU. */
9645 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
9646 gcc_assert (pub->die->die_mark);
9648 /* If we're putting types in their own .debug_types sections,
9649 the .debug_pubtypes table will still point to the compile
9650 unit (not the type unit), so we want to use the offset of
9651 the skeleton DIE (if there is one). */
9652 if (pub->die->comdat_type_p && names == pubtype_table)
9654 comdat_type_node_ref type_node = pub->die->die_id.die_type_node;
9656 if (type_node != NULL)
9657 die_offset = (type_node->skeleton_die != NULL
9658 ? type_node->skeleton_die->die_offset
9659 : comp_unit_die ()->die_offset);
9662 output_pubname (die_offset, pub);
9666 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
9669 /* Output public names and types tables if necessary. */
9671 static void
9672 output_pubtables (void)
9674 if (!want_pubnames () || !info_section_emitted)
9675 return;
9677 switch_to_section (debug_pubnames_section);
9678 output_pubnames (pubname_table);
9679 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
9680 It shouldn't hurt to emit it always, since pure DWARF2 consumers
9681 simply won't look for the section. */
9682 switch_to_section (debug_pubtypes_section);
9683 output_pubnames (pubtype_table);
9687 /* Output the information that goes into the .debug_aranges table.
9688 Namely, define the beginning and ending address range of the
9689 text section generated for this compilation unit. */
9691 static void
9692 output_aranges (unsigned long aranges_length)
9694 unsigned i;
9696 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9697 dw2_asm_output_data (4, 0xffffffff,
9698 "Initial length escape value indicating 64-bit DWARF extension");
9699 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
9700 "Length of Address Ranges Info");
9701 /* Version number for aranges is still 2, even up to DWARF5. */
9702 dw2_asm_output_data (2, 2, "DWARF Version");
9703 if (dwarf_split_debug_info)
9704 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9705 debug_skeleton_info_section,
9706 "Offset of Compilation Unit Info");
9707 else
9708 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9709 debug_info_section,
9710 "Offset of Compilation Unit Info");
9711 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
9712 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
9714 /* We need to align to twice the pointer size here. */
9715 if (DWARF_ARANGES_PAD_SIZE)
9717 /* Pad using a 2 byte words so that padding is correct for any
9718 pointer size. */
9719 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
9720 2 * DWARF2_ADDR_SIZE);
9721 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
9722 dw2_asm_output_data (2, 0, NULL);
9725 /* It is necessary not to output these entries if the sections were
9726 not used; if the sections were not used, the length will be 0 and
9727 the address may end up as 0 if the section is discarded by ld
9728 --gc-sections, leaving an invalid (0, 0) entry that can be
9729 confused with the terminator. */
9730 if (text_section_used)
9732 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
9733 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
9734 text_section_label, "Length");
9736 if (cold_text_section_used)
9738 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
9739 "Address");
9740 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
9741 cold_text_section_label, "Length");
9744 if (have_multiple_function_sections)
9746 unsigned fde_idx;
9747 dw_fde_ref fde;
9749 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9751 if (DECL_IGNORED_P (fde->decl))
9752 continue;
9753 if (!fde->in_std_section)
9755 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
9756 "Address");
9757 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
9758 fde->dw_fde_begin, "Length");
9760 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9762 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
9763 "Address");
9764 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
9765 fde->dw_fde_second_begin, "Length");
9770 /* Output the terminator words. */
9771 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9772 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9775 /* Add a new entry to .debug_ranges. Return the offset at which it
9776 was placed. */
9778 static unsigned int
9779 add_ranges_num (int num)
9781 unsigned int in_use = ranges_table_in_use;
9783 if (in_use == ranges_table_allocated)
9785 ranges_table_allocated += RANGES_TABLE_INCREMENT;
9786 ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
9787 ranges_table_allocated);
9788 memset (ranges_table + ranges_table_in_use, 0,
9789 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
9792 ranges_table[in_use].num = num;
9793 ranges_table_in_use = in_use + 1;
9795 return in_use * 2 * DWARF2_ADDR_SIZE;
9798 /* Add a new entry to .debug_ranges corresponding to a block, or a
9799 range terminator if BLOCK is NULL. */
9801 static unsigned int
9802 add_ranges (const_tree block)
9804 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
9807 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
9808 When using dwarf_split_debug_info, address attributes in dies destined
9809 for the final executable should be direct references--setting the
9810 parameter force_direct ensures this behavior. */
9812 static void
9813 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
9814 bool *added, bool force_direct)
9816 unsigned int in_use = ranges_by_label_in_use;
9817 unsigned int offset;
9819 if (in_use == ranges_by_label_allocated)
9821 ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
9822 ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
9823 ranges_by_label,
9824 ranges_by_label_allocated);
9825 memset (ranges_by_label + ranges_by_label_in_use, 0,
9826 RANGES_TABLE_INCREMENT
9827 * sizeof (struct dw_ranges_by_label_struct));
9830 ranges_by_label[in_use].begin = begin;
9831 ranges_by_label[in_use].end = end;
9832 ranges_by_label_in_use = in_use + 1;
9834 offset = add_ranges_num (-(int)in_use - 1);
9835 if (!*added)
9837 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
9838 *added = true;
9842 static void
9843 output_ranges (void)
9845 unsigned i;
9846 static const char *const start_fmt = "Offset %#x";
9847 const char *fmt = start_fmt;
9849 for (i = 0; i < ranges_table_in_use; i++)
9851 int block_num = ranges_table[i].num;
9853 if (block_num > 0)
9855 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
9856 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
9858 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
9859 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
9861 /* If all code is in the text section, then the compilation
9862 unit base address defaults to DW_AT_low_pc, which is the
9863 base of the text section. */
9864 if (!have_multiple_function_sections)
9866 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
9867 text_section_label,
9868 fmt, i * 2 * DWARF2_ADDR_SIZE);
9869 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
9870 text_section_label, NULL);
9873 /* Otherwise, the compilation unit base address is zero,
9874 which allows us to use absolute addresses, and not worry
9875 about whether the target supports cross-section
9876 arithmetic. */
9877 else
9879 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
9880 fmt, i * 2 * DWARF2_ADDR_SIZE);
9881 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
9884 fmt = NULL;
9887 /* Negative block_num stands for an index into ranges_by_label. */
9888 else if (block_num < 0)
9890 int lab_idx = - block_num - 1;
9892 if (!have_multiple_function_sections)
9894 gcc_unreachable ();
9895 #if 0
9896 /* If we ever use add_ranges_by_labels () for a single
9897 function section, all we have to do is to take out
9898 the #if 0 above. */
9899 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9900 ranges_by_label[lab_idx].begin,
9901 text_section_label,
9902 fmt, i * 2 * DWARF2_ADDR_SIZE);
9903 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9904 ranges_by_label[lab_idx].end,
9905 text_section_label, NULL);
9906 #endif
9908 else
9910 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9911 ranges_by_label[lab_idx].begin,
9912 fmt, i * 2 * DWARF2_ADDR_SIZE);
9913 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9914 ranges_by_label[lab_idx].end,
9915 NULL);
9918 else
9920 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9921 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9922 fmt = start_fmt;
9927 /* Data structure containing information about input files. */
9928 struct file_info
9930 const char *path; /* Complete file name. */
9931 const char *fname; /* File name part. */
9932 int length; /* Length of entire string. */
9933 struct dwarf_file_data * file_idx; /* Index in input file table. */
9934 int dir_idx; /* Index in directory table. */
9937 /* Data structure containing information about directories with source
9938 files. */
9939 struct dir_info
9941 const char *path; /* Path including directory name. */
9942 int length; /* Path length. */
9943 int prefix; /* Index of directory entry which is a prefix. */
9944 int count; /* Number of files in this directory. */
9945 int dir_idx; /* Index of directory used as base. */
9948 /* Callback function for file_info comparison. We sort by looking at
9949 the directories in the path. */
9951 static int
9952 file_info_cmp (const void *p1, const void *p2)
9954 const struct file_info *const s1 = (const struct file_info *) p1;
9955 const struct file_info *const s2 = (const struct file_info *) p2;
9956 const unsigned char *cp1;
9957 const unsigned char *cp2;
9959 /* Take care of file names without directories. We need to make sure that
9960 we return consistent values to qsort since some will get confused if
9961 we return the same value when identical operands are passed in opposite
9962 orders. So if neither has a directory, return 0 and otherwise return
9963 1 or -1 depending on which one has the directory. */
9964 if ((s1->path == s1->fname || s2->path == s2->fname))
9965 return (s2->path == s2->fname) - (s1->path == s1->fname);
9967 cp1 = (const unsigned char *) s1->path;
9968 cp2 = (const unsigned char *) s2->path;
9970 while (1)
9972 ++cp1;
9973 ++cp2;
9974 /* Reached the end of the first path? If so, handle like above. */
9975 if ((cp1 == (const unsigned char *) s1->fname)
9976 || (cp2 == (const unsigned char *) s2->fname))
9977 return ((cp2 == (const unsigned char *) s2->fname)
9978 - (cp1 == (const unsigned char *) s1->fname));
9980 /* Character of current path component the same? */
9981 else if (*cp1 != *cp2)
9982 return *cp1 - *cp2;
9986 struct file_name_acquire_data
9988 struct file_info *files;
9989 int used_files;
9990 int max_files;
9993 /* Traversal function for the hash table. */
9996 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
9998 struct dwarf_file_data *d = *slot;
9999 struct file_info *fi;
10000 const char *f;
10002 gcc_assert (fnad->max_files >= d->emitted_number);
10004 if (! d->emitted_number)
10005 return 1;
10007 gcc_assert (fnad->max_files != fnad->used_files);
10009 fi = fnad->files + fnad->used_files++;
10011 /* Skip all leading "./". */
10012 f = d->filename;
10013 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
10014 f += 2;
10016 /* Create a new array entry. */
10017 fi->path = f;
10018 fi->length = strlen (f);
10019 fi->file_idx = d;
10021 /* Search for the file name part. */
10022 f = strrchr (f, DIR_SEPARATOR);
10023 #if defined (DIR_SEPARATOR_2)
10025 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
10027 if (g != NULL)
10029 if (f == NULL || f < g)
10030 f = g;
10033 #endif
10035 fi->fname = f == NULL ? fi->path : f + 1;
10036 return 1;
10039 /* Output the directory table and the file name table. We try to minimize
10040 the total amount of memory needed. A heuristic is used to avoid large
10041 slowdowns with many input files. */
10043 static void
10044 output_file_names (void)
10046 struct file_name_acquire_data fnad;
10047 int numfiles;
10048 struct file_info *files;
10049 struct dir_info *dirs;
10050 int *saved;
10051 int *savehere;
10052 int *backmap;
10053 int ndirs;
10054 int idx_offset;
10055 int i;
10057 if (!last_emitted_file)
10059 dw2_asm_output_data (1, 0, "End directory table");
10060 dw2_asm_output_data (1, 0, "End file name table");
10061 return;
10064 numfiles = last_emitted_file->emitted_number;
10066 /* Allocate the various arrays we need. */
10067 files = XALLOCAVEC (struct file_info, numfiles);
10068 dirs = XALLOCAVEC (struct dir_info, numfiles);
10070 fnad.files = files;
10071 fnad.used_files = 0;
10072 fnad.max_files = numfiles;
10073 file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
10074 gcc_assert (fnad.used_files == fnad.max_files);
10076 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
10078 /* Find all the different directories used. */
10079 dirs[0].path = files[0].path;
10080 dirs[0].length = files[0].fname - files[0].path;
10081 dirs[0].prefix = -1;
10082 dirs[0].count = 1;
10083 dirs[0].dir_idx = 0;
10084 files[0].dir_idx = 0;
10085 ndirs = 1;
10087 for (i = 1; i < numfiles; i++)
10088 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
10089 && memcmp (dirs[ndirs - 1].path, files[i].path,
10090 dirs[ndirs - 1].length) == 0)
10092 /* Same directory as last entry. */
10093 files[i].dir_idx = ndirs - 1;
10094 ++dirs[ndirs - 1].count;
10096 else
10098 int j;
10100 /* This is a new directory. */
10101 dirs[ndirs].path = files[i].path;
10102 dirs[ndirs].length = files[i].fname - files[i].path;
10103 dirs[ndirs].count = 1;
10104 dirs[ndirs].dir_idx = ndirs;
10105 files[i].dir_idx = ndirs;
10107 /* Search for a prefix. */
10108 dirs[ndirs].prefix = -1;
10109 for (j = 0; j < ndirs; j++)
10110 if (dirs[j].length < dirs[ndirs].length
10111 && dirs[j].length > 1
10112 && (dirs[ndirs].prefix == -1
10113 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
10114 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
10115 dirs[ndirs].prefix = j;
10117 ++ndirs;
10120 /* Now to the actual work. We have to find a subset of the directories which
10121 allow expressing the file name using references to the directory table
10122 with the least amount of characters. We do not do an exhaustive search
10123 where we would have to check out every combination of every single
10124 possible prefix. Instead we use a heuristic which provides nearly optimal
10125 results in most cases and never is much off. */
10126 saved = XALLOCAVEC (int, ndirs);
10127 savehere = XALLOCAVEC (int, ndirs);
10129 memset (saved, '\0', ndirs * sizeof (saved[0]));
10130 for (i = 0; i < ndirs; i++)
10132 int j;
10133 int total;
10135 /* We can always save some space for the current directory. But this
10136 does not mean it will be enough to justify adding the directory. */
10137 savehere[i] = dirs[i].length;
10138 total = (savehere[i] - saved[i]) * dirs[i].count;
10140 for (j = i + 1; j < ndirs; j++)
10142 savehere[j] = 0;
10143 if (saved[j] < dirs[i].length)
10145 /* Determine whether the dirs[i] path is a prefix of the
10146 dirs[j] path. */
10147 int k;
10149 k = dirs[j].prefix;
10150 while (k != -1 && k != (int) i)
10151 k = dirs[k].prefix;
10153 if (k == (int) i)
10155 /* Yes it is. We can possibly save some memory by
10156 writing the filenames in dirs[j] relative to
10157 dirs[i]. */
10158 savehere[j] = dirs[i].length;
10159 total += (savehere[j] - saved[j]) * dirs[j].count;
10164 /* Check whether we can save enough to justify adding the dirs[i]
10165 directory. */
10166 if (total > dirs[i].length + 1)
10168 /* It's worthwhile adding. */
10169 for (j = i; j < ndirs; j++)
10170 if (savehere[j] > 0)
10172 /* Remember how much we saved for this directory so far. */
10173 saved[j] = savehere[j];
10175 /* Remember the prefix directory. */
10176 dirs[j].dir_idx = i;
10181 /* Emit the directory name table. */
10182 idx_offset = dirs[0].length > 0 ? 1 : 0;
10183 for (i = 1 - idx_offset; i < ndirs; i++)
10184 dw2_asm_output_nstring (dirs[i].path,
10185 dirs[i].length
10186 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
10187 "Directory Entry: %#x", i + idx_offset);
10189 dw2_asm_output_data (1, 0, "End directory table");
10191 /* We have to emit them in the order of emitted_number since that's
10192 used in the debug info generation. To do this efficiently we
10193 generate a back-mapping of the indices first. */
10194 backmap = XALLOCAVEC (int, numfiles);
10195 for (i = 0; i < numfiles; i++)
10196 backmap[files[i].file_idx->emitted_number - 1] = i;
10198 /* Now write all the file names. */
10199 for (i = 0; i < numfiles; i++)
10201 int file_idx = backmap[i];
10202 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
10204 #ifdef VMS_DEBUGGING_INFO
10205 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
10207 /* Setting these fields can lead to debugger miscomparisons,
10208 but VMS Debug requires them to be set correctly. */
10210 int ver;
10211 long long cdt;
10212 long siz;
10213 int maxfilelen = strlen (files[file_idx].path)
10214 + dirs[dir_idx].length
10215 + MAX_VMS_VERSION_LEN + 1;
10216 char *filebuf = XALLOCAVEC (char, maxfilelen);
10218 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
10219 snprintf (filebuf, maxfilelen, "%s;%d",
10220 files[file_idx].path + dirs[dir_idx].length, ver);
10222 dw2_asm_output_nstring
10223 (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
10225 /* Include directory index. */
10226 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10228 /* Modification time. */
10229 dw2_asm_output_data_uleb128
10230 ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
10231 ? cdt : 0,
10232 NULL);
10234 /* File length in bytes. */
10235 dw2_asm_output_data_uleb128
10236 ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
10237 ? siz : 0,
10238 NULL);
10239 #else
10240 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
10241 "File Entry: %#x", (unsigned) i + 1);
10243 /* Include directory index. */
10244 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10246 /* Modification time. */
10247 dw2_asm_output_data_uleb128 (0, NULL);
10249 /* File length in bytes. */
10250 dw2_asm_output_data_uleb128 (0, NULL);
10251 #endif /* VMS_DEBUGGING_INFO */
10254 dw2_asm_output_data (1, 0, "End file name table");
10258 /* Output one line number table into the .debug_line section. */
10260 static void
10261 output_one_line_info_table (dw_line_info_table *table)
10263 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
10264 unsigned int current_line = 1;
10265 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
10266 dw_line_info_entry *ent;
10267 size_t i;
10269 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
10271 switch (ent->opcode)
10273 case LI_set_address:
10274 /* ??? Unfortunately, we have little choice here currently, and
10275 must always use the most general form. GCC does not know the
10276 address delta itself, so we can't use DW_LNS_advance_pc. Many
10277 ports do have length attributes which will give an upper bound
10278 on the address range. We could perhaps use length attributes
10279 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
10280 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
10282 /* This can handle any delta. This takes
10283 4+DWARF2_ADDR_SIZE bytes. */
10284 dw2_asm_output_data (1, 0, "set address %s", line_label);
10285 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10286 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10287 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
10288 break;
10290 case LI_set_line:
10291 if (ent->val == current_line)
10293 /* We still need to start a new row, so output a copy insn. */
10294 dw2_asm_output_data (1, DW_LNS_copy,
10295 "copy line %u", current_line);
10297 else
10299 int line_offset = ent->val - current_line;
10300 int line_delta = line_offset - DWARF_LINE_BASE;
10302 current_line = ent->val;
10303 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
10305 /* This can handle deltas from -10 to 234, using the current
10306 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
10307 This takes 1 byte. */
10308 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
10309 "line %u", current_line);
10311 else
10313 /* This can handle any delta. This takes at least 4 bytes,
10314 depending on the value being encoded. */
10315 dw2_asm_output_data (1, DW_LNS_advance_line,
10316 "advance to line %u", current_line);
10317 dw2_asm_output_data_sleb128 (line_offset, NULL);
10318 dw2_asm_output_data (1, DW_LNS_copy, NULL);
10321 break;
10323 case LI_set_file:
10324 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
10325 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10326 break;
10328 case LI_set_column:
10329 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
10330 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10331 break;
10333 case LI_negate_stmt:
10334 current_is_stmt = !current_is_stmt;
10335 dw2_asm_output_data (1, DW_LNS_negate_stmt,
10336 "is_stmt %d", current_is_stmt);
10337 break;
10339 case LI_set_prologue_end:
10340 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
10341 "set prologue end");
10342 break;
10344 case LI_set_epilogue_begin:
10345 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
10346 "set epilogue begin");
10347 break;
10349 case LI_set_discriminator:
10350 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
10351 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
10352 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
10353 dw2_asm_output_data_uleb128 (ent->val, NULL);
10354 break;
10358 /* Emit debug info for the address of the end of the table. */
10359 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
10360 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10361 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10362 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
10364 dw2_asm_output_data (1, 0, "end sequence");
10365 dw2_asm_output_data_uleb128 (1, NULL);
10366 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
10369 /* Output the source line number correspondence information. This
10370 information goes into the .debug_line section. */
10372 static void
10373 output_line_info (bool prologue_only)
10375 char l1[20], l2[20], p1[20], p2[20];
10376 /* We don't support DWARFv5 line tables yet. */
10377 int ver = dwarf_version < 5 ? dwarf_version : 4;
10378 bool saw_one = false;
10379 int opc;
10381 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
10382 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
10383 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
10384 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
10386 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10387 dw2_asm_output_data (4, 0xffffffff,
10388 "Initial length escape value indicating 64-bit DWARF extension");
10389 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
10390 "Length of Source Line Info");
10391 ASM_OUTPUT_LABEL (asm_out_file, l1);
10393 dw2_asm_output_data (2, ver, "DWARF Version");
10394 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
10395 ASM_OUTPUT_LABEL (asm_out_file, p1);
10397 /* Define the architecture-dependent minimum instruction length (in bytes).
10398 In this implementation of DWARF, this field is used for information
10399 purposes only. Since GCC generates assembly language, we have no
10400 a priori knowledge of how many instruction bytes are generated for each
10401 source line, and therefore can use only the DW_LNE_set_address and
10402 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
10403 this as '1', which is "correct enough" for all architectures,
10404 and don't let the target override. */
10405 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
10407 if (ver >= 4)
10408 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
10409 "Maximum Operations Per Instruction");
10410 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
10411 "Default is_stmt_start flag");
10412 dw2_asm_output_data (1, DWARF_LINE_BASE,
10413 "Line Base Value (Special Opcodes)");
10414 dw2_asm_output_data (1, DWARF_LINE_RANGE,
10415 "Line Range Value (Special Opcodes)");
10416 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
10417 "Special Opcode Base");
10419 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
10421 int n_op_args;
10422 switch (opc)
10424 case DW_LNS_advance_pc:
10425 case DW_LNS_advance_line:
10426 case DW_LNS_set_file:
10427 case DW_LNS_set_column:
10428 case DW_LNS_fixed_advance_pc:
10429 case DW_LNS_set_isa:
10430 n_op_args = 1;
10431 break;
10432 default:
10433 n_op_args = 0;
10434 break;
10437 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
10438 opc, n_op_args);
10441 /* Write out the information about the files we use. */
10442 output_file_names ();
10443 ASM_OUTPUT_LABEL (asm_out_file, p2);
10444 if (prologue_only)
10446 /* Output the marker for the end of the line number info. */
10447 ASM_OUTPUT_LABEL (asm_out_file, l2);
10448 return;
10451 if (separate_line_info)
10453 dw_line_info_table *table;
10454 size_t i;
10456 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
10457 if (table->in_use)
10459 output_one_line_info_table (table);
10460 saw_one = true;
10463 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
10465 output_one_line_info_table (cold_text_section_line_info);
10466 saw_one = true;
10469 /* ??? Some Darwin linkers crash on a .debug_line section with no
10470 sequences. Further, merely a DW_LNE_end_sequence entry is not
10471 sufficient -- the address column must also be initialized.
10472 Make sure to output at least one set_address/end_sequence pair,
10473 choosing .text since that section is always present. */
10474 if (text_section_line_info->in_use || !saw_one)
10475 output_one_line_info_table (text_section_line_info);
10477 /* Output the marker for the end of the line number info. */
10478 ASM_OUTPUT_LABEL (asm_out_file, l2);
10481 /* Given a pointer to a tree node for some base type, return a pointer to
10482 a DIE that describes the given type.
10484 This routine must only be called for GCC type nodes that correspond to
10485 Dwarf base (fundamental) types. */
10487 static dw_die_ref
10488 base_type_die (tree type)
10490 dw_die_ref base_type_result;
10491 enum dwarf_type encoding;
10493 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
10494 return 0;
10496 /* If this is a subtype that should not be emitted as a subrange type,
10497 use the base type. See subrange_type_for_debug_p. */
10498 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
10499 type = TREE_TYPE (type);
10501 switch (TREE_CODE (type))
10503 case INTEGER_TYPE:
10504 if ((dwarf_version >= 4 || !dwarf_strict)
10505 && TYPE_NAME (type)
10506 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10507 && DECL_IS_BUILTIN (TYPE_NAME (type))
10508 && DECL_NAME (TYPE_NAME (type)))
10510 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
10511 if (strcmp (name, "char16_t") == 0
10512 || strcmp (name, "char32_t") == 0)
10514 encoding = DW_ATE_UTF;
10515 break;
10518 if (TYPE_STRING_FLAG (type))
10520 if (TYPE_UNSIGNED (type))
10521 encoding = DW_ATE_unsigned_char;
10522 else
10523 encoding = DW_ATE_signed_char;
10525 else if (TYPE_UNSIGNED (type))
10526 encoding = DW_ATE_unsigned;
10527 else
10528 encoding = DW_ATE_signed;
10529 break;
10531 case REAL_TYPE:
10532 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
10534 if (dwarf_version >= 3 || !dwarf_strict)
10535 encoding = DW_ATE_decimal_float;
10536 else
10537 encoding = DW_ATE_lo_user;
10539 else
10540 encoding = DW_ATE_float;
10541 break;
10543 case FIXED_POINT_TYPE:
10544 if (!(dwarf_version >= 3 || !dwarf_strict))
10545 encoding = DW_ATE_lo_user;
10546 else if (TYPE_UNSIGNED (type))
10547 encoding = DW_ATE_unsigned_fixed;
10548 else
10549 encoding = DW_ATE_signed_fixed;
10550 break;
10552 /* Dwarf2 doesn't know anything about complex ints, so use
10553 a user defined type for it. */
10554 case COMPLEX_TYPE:
10555 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
10556 encoding = DW_ATE_complex_float;
10557 else
10558 encoding = DW_ATE_lo_user;
10559 break;
10561 case BOOLEAN_TYPE:
10562 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
10563 encoding = DW_ATE_boolean;
10564 break;
10566 default:
10567 /* No other TREE_CODEs are Dwarf fundamental types. */
10568 gcc_unreachable ();
10571 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
10573 add_AT_unsigned (base_type_result, DW_AT_byte_size,
10574 int_size_in_bytes (type));
10575 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
10576 add_pubtype (type, base_type_result);
10578 return base_type_result;
10581 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
10582 named 'auto' in its type: return true for it, false otherwise. */
10584 static inline bool
10585 is_cxx_auto (tree type)
10587 if (is_cxx ())
10589 tree name = TYPE_IDENTIFIER (type);
10590 if (name == get_identifier ("auto")
10591 || name == get_identifier ("decltype(auto)"))
10592 return true;
10594 return false;
10597 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
10598 given input type is a Dwarf "fundamental" type. Otherwise return null. */
10600 static inline int
10601 is_base_type (tree type)
10603 switch (TREE_CODE (type))
10605 case ERROR_MARK:
10606 case VOID_TYPE:
10607 case INTEGER_TYPE:
10608 case REAL_TYPE:
10609 case FIXED_POINT_TYPE:
10610 case COMPLEX_TYPE:
10611 case BOOLEAN_TYPE:
10612 case POINTER_BOUNDS_TYPE:
10613 return 1;
10615 case ARRAY_TYPE:
10616 case RECORD_TYPE:
10617 case UNION_TYPE:
10618 case QUAL_UNION_TYPE:
10619 case ENUMERAL_TYPE:
10620 case FUNCTION_TYPE:
10621 case METHOD_TYPE:
10622 case POINTER_TYPE:
10623 case REFERENCE_TYPE:
10624 case NULLPTR_TYPE:
10625 case OFFSET_TYPE:
10626 case LANG_TYPE:
10627 case VECTOR_TYPE:
10628 return 0;
10630 default:
10631 if (is_cxx_auto (type))
10632 return 0;
10633 gcc_unreachable ();
10636 return 0;
10639 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
10640 node, return the size in bits for the type if it is a constant, or else
10641 return the alignment for the type if the type's size is not constant, or
10642 else return BITS_PER_WORD if the type actually turns out to be an
10643 ERROR_MARK node. */
10645 static inline unsigned HOST_WIDE_INT
10646 simple_type_size_in_bits (const_tree type)
10648 if (TREE_CODE (type) == ERROR_MARK)
10649 return BITS_PER_WORD;
10650 else if (TYPE_SIZE (type) == NULL_TREE)
10651 return 0;
10652 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
10653 return tree_to_uhwi (TYPE_SIZE (type));
10654 else
10655 return TYPE_ALIGN (type);
10658 /* Similarly, but return an offset_int instead of UHWI. */
10660 static inline offset_int
10661 offset_int_type_size_in_bits (const_tree type)
10663 if (TREE_CODE (type) == ERROR_MARK)
10664 return BITS_PER_WORD;
10665 else if (TYPE_SIZE (type) == NULL_TREE)
10666 return 0;
10667 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
10668 return wi::to_offset (TYPE_SIZE (type));
10669 else
10670 return TYPE_ALIGN (type);
10673 /* Given a pointer to a tree node for a subrange type, return a pointer
10674 to a DIE that describes the given type. */
10676 static dw_die_ref
10677 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
10679 dw_die_ref subrange_die;
10680 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
10682 if (context_die == NULL)
10683 context_die = comp_unit_die ();
10685 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
10687 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
10689 /* The size of the subrange type and its base type do not match,
10690 so we need to generate a size attribute for the subrange type. */
10691 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
10694 if (low)
10695 add_bound_info (subrange_die, DW_AT_lower_bound, low, NULL);
10696 if (high)
10697 add_bound_info (subrange_die, DW_AT_upper_bound, high, NULL);
10699 return subrange_die;
10702 /* Returns the (const and/or volatile) cv_qualifiers associated with
10703 the decl node. This will normally be augmented with the
10704 cv_qualifiers of the underlying type in add_type_attribute. */
10706 static int
10707 decl_quals (const_tree decl)
10709 return ((TREE_READONLY (decl)
10710 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
10711 | (TREE_THIS_VOLATILE (decl)
10712 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
10715 /* Determine the TYPE whose qualifiers match the largest strict subset
10716 of the given TYPE_QUALS, and return its qualifiers. Ignore all
10717 qualifiers outside QUAL_MASK. */
10719 static int
10720 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
10722 tree t;
10723 int best_rank = 0, best_qual = 0, max_rank;
10725 type_quals &= qual_mask;
10726 max_rank = popcount_hwi (type_quals) - 1;
10728 for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
10729 t = TYPE_NEXT_VARIANT (t))
10731 int q = TYPE_QUALS (t) & qual_mask;
10733 if ((q & type_quals) == q && q != type_quals
10734 && check_base_type (t, type))
10736 int rank = popcount_hwi (q);
10738 if (rank > best_rank)
10740 best_rank = rank;
10741 best_qual = q;
10746 return best_qual;
10749 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
10750 entry that chains various modifiers in front of the given type. */
10752 static dw_die_ref
10753 modified_type_die (tree type, int cv_quals, dw_die_ref context_die)
10755 enum tree_code code = TREE_CODE (type);
10756 dw_die_ref mod_type_die;
10757 dw_die_ref sub_die = NULL;
10758 tree item_type = NULL;
10759 tree qualified_type;
10760 tree name, low, high;
10761 dw_die_ref mod_scope;
10762 /* Only these cv-qualifiers are currently handled. */
10763 const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
10764 | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC);
10766 if (code == ERROR_MARK)
10767 return NULL;
10769 cv_quals &= cv_qual_mask;
10771 /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
10772 tag modifier (and not an attribute) old consumers won't be able
10773 to handle it. */
10774 if (dwarf_version < 3)
10775 cv_quals &= ~TYPE_QUAL_RESTRICT;
10777 /* Likewise for DW_TAG_atomic_type for DWARFv5. */
10778 if (dwarf_version < 5)
10779 cv_quals &= ~TYPE_QUAL_ATOMIC;
10781 /* See if we already have the appropriately qualified variant of
10782 this type. */
10783 qualified_type = get_qualified_type (type, cv_quals);
10785 if (qualified_type == sizetype
10786 && TYPE_NAME (qualified_type)
10787 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
10789 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
10791 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
10792 && TYPE_PRECISION (t)
10793 == TYPE_PRECISION (qualified_type)
10794 && TYPE_UNSIGNED (t)
10795 == TYPE_UNSIGNED (qualified_type));
10796 qualified_type = t;
10799 /* If we do, then we can just use its DIE, if it exists. */
10800 if (qualified_type)
10802 mod_type_die = lookup_type_die (qualified_type);
10803 if (mod_type_die)
10804 return mod_type_die;
10807 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
10809 /* Handle C typedef types. */
10810 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
10811 && !DECL_ARTIFICIAL (name))
10813 tree dtype = TREE_TYPE (name);
10815 if (qualified_type == dtype)
10817 /* For a named type, use the typedef. */
10818 gen_type_die (qualified_type, context_die);
10819 return lookup_type_die (qualified_type);
10821 else
10823 int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
10824 dquals &= cv_qual_mask;
10825 if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
10826 || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
10827 /* cv-unqualified version of named type. Just use
10828 the unnamed type to which it refers. */
10829 return modified_type_die (DECL_ORIGINAL_TYPE (name),
10830 cv_quals, context_die);
10831 /* Else cv-qualified version of named type; fall through. */
10835 mod_scope = scope_die_for (type, context_die);
10837 if (cv_quals)
10839 struct qual_info { int q; enum dwarf_tag t; };
10840 static const struct qual_info qual_info[] =
10842 { TYPE_QUAL_ATOMIC, DW_TAG_atomic_type },
10843 { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
10844 { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
10845 { TYPE_QUAL_CONST, DW_TAG_const_type },
10847 int sub_quals;
10848 unsigned i;
10850 /* Determine a lesser qualified type that most closely matches
10851 this one. Then generate DW_TAG_* entries for the remaining
10852 qualifiers. */
10853 sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
10854 cv_qual_mask);
10855 mod_type_die = modified_type_die (type, sub_quals, context_die);
10857 for (i = 0; i < sizeof (qual_info) / sizeof (qual_info[0]); i++)
10858 if (qual_info[i].q & cv_quals & ~sub_quals)
10860 dw_die_ref d = new_die (qual_info[i].t, mod_scope, type);
10861 if (mod_type_die)
10862 add_AT_die_ref (d, DW_AT_type, mod_type_die);
10863 mod_type_die = d;
10866 else if (code == POINTER_TYPE)
10868 mod_type_die = new_die (DW_TAG_pointer_type, mod_scope, type);
10869 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10870 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10871 item_type = TREE_TYPE (type);
10872 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10873 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10874 TYPE_ADDR_SPACE (item_type));
10876 else if (code == REFERENCE_TYPE)
10878 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
10879 mod_type_die = new_die (DW_TAG_rvalue_reference_type, mod_scope,
10880 type);
10881 else
10882 mod_type_die = new_die (DW_TAG_reference_type, mod_scope, type);
10883 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10884 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10885 item_type = TREE_TYPE (type);
10886 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10887 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10888 TYPE_ADDR_SPACE (item_type));
10890 else if (code == INTEGER_TYPE
10891 && TREE_TYPE (type) != NULL_TREE
10892 && subrange_type_for_debug_p (type, &low, &high))
10894 mod_type_die = subrange_type_die (type, low, high, context_die);
10895 item_type = TREE_TYPE (type);
10897 else if (is_base_type (type))
10898 mod_type_die = base_type_die (type);
10899 else
10901 gen_type_die (type, context_die);
10903 /* We have to get the type_main_variant here (and pass that to the
10904 `lookup_type_die' routine) because the ..._TYPE node we have
10905 might simply be a *copy* of some original type node (where the
10906 copy was created to help us keep track of typedef names) and
10907 that copy might have a different TYPE_UID from the original
10908 ..._TYPE node. */
10909 if (TREE_CODE (type) != VECTOR_TYPE)
10910 return lookup_type_die (type_main_variant (type));
10911 else
10912 /* Vectors have the debugging information in the type,
10913 not the main variant. */
10914 return lookup_type_die (type);
10917 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
10918 don't output a DW_TAG_typedef, since there isn't one in the
10919 user's program; just attach a DW_AT_name to the type.
10920 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
10921 if the base type already has the same name. */
10922 if (name
10923 && ((TREE_CODE (name) != TYPE_DECL
10924 && (qualified_type == TYPE_MAIN_VARIANT (type)
10925 || (cv_quals == TYPE_UNQUALIFIED)))
10926 || (TREE_CODE (name) == TYPE_DECL
10927 && TREE_TYPE (name) == qualified_type
10928 && DECL_NAME (name))))
10930 if (TREE_CODE (name) == TYPE_DECL)
10931 /* Could just call add_name_and_src_coords_attributes here,
10932 but since this is a builtin type it doesn't have any
10933 useful source coordinates anyway. */
10934 name = DECL_NAME (name);
10935 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
10937 /* This probably indicates a bug. */
10938 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
10940 name = TYPE_IDENTIFIER (type);
10941 add_name_attribute (mod_type_die,
10942 name ? IDENTIFIER_POINTER (name) : "__unknown__");
10945 if (qualified_type)
10946 equate_type_number_to_die (qualified_type, mod_type_die);
10948 if (item_type)
10949 /* We must do this after the equate_type_number_to_die call, in case
10950 this is a recursive type. This ensures that the modified_type_die
10951 recursion will terminate even if the type is recursive. Recursive
10952 types are possible in Ada. */
10953 sub_die = modified_type_die (item_type,
10954 TYPE_QUALS_NO_ADDR_SPACE (item_type),
10955 context_die);
10957 if (sub_die != NULL)
10958 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
10960 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
10961 if (TYPE_ARTIFICIAL (type))
10962 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
10964 return mod_type_die;
10967 /* Generate DIEs for the generic parameters of T.
10968 T must be either a generic type or a generic function.
10969 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
10971 static void
10972 gen_generic_params_dies (tree t)
10974 tree parms, args;
10975 int parms_num, i;
10976 dw_die_ref die = NULL;
10977 int non_default;
10979 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
10980 return;
10982 if (TYPE_P (t))
10983 die = lookup_type_die (t);
10984 else if (DECL_P (t))
10985 die = lookup_decl_die (t);
10987 gcc_assert (die);
10989 parms = lang_hooks.get_innermost_generic_parms (t);
10990 if (!parms)
10991 /* T has no generic parameter. It means T is neither a generic type
10992 or function. End of story. */
10993 return;
10995 parms_num = TREE_VEC_LENGTH (parms);
10996 args = lang_hooks.get_innermost_generic_args (t);
10997 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
10998 non_default = int_cst_value (TREE_CHAIN (args));
10999 else
11000 non_default = TREE_VEC_LENGTH (args);
11001 for (i = 0; i < parms_num; i++)
11003 tree parm, arg, arg_pack_elems;
11004 dw_die_ref parm_die;
11006 parm = TREE_VEC_ELT (parms, i);
11007 arg = TREE_VEC_ELT (args, i);
11008 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
11009 gcc_assert (parm && TREE_VALUE (parm) && arg);
11011 if (parm && TREE_VALUE (parm) && arg)
11013 /* If PARM represents a template parameter pack,
11014 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
11015 by DW_TAG_template_*_parameter DIEs for the argument
11016 pack elements of ARG. Note that ARG would then be
11017 an argument pack. */
11018 if (arg_pack_elems)
11019 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
11020 arg_pack_elems,
11021 die);
11022 else
11023 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
11024 true /* emit name */, die);
11025 if (i >= non_default)
11026 add_AT_flag (parm_die, DW_AT_default_value, 1);
11031 /* Create and return a DIE for PARM which should be
11032 the representation of a generic type parameter.
11033 For instance, in the C++ front end, PARM would be a template parameter.
11034 ARG is the argument to PARM.
11035 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
11036 name of the PARM.
11037 PARENT_DIE is the parent DIE which the new created DIE should be added to,
11038 as a child node. */
11040 static dw_die_ref
11041 generic_parameter_die (tree parm, tree arg,
11042 bool emit_name_p,
11043 dw_die_ref parent_die)
11045 dw_die_ref tmpl_die = NULL;
11046 const char *name = NULL;
11048 if (!parm || !DECL_NAME (parm) || !arg)
11049 return NULL;
11051 /* We support non-type generic parameters and arguments,
11052 type generic parameters and arguments, as well as
11053 generic generic parameters (a.k.a. template template parameters in C++)
11054 and arguments. */
11055 if (TREE_CODE (parm) == PARM_DECL)
11056 /* PARM is a nontype generic parameter */
11057 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
11058 else if (TREE_CODE (parm) == TYPE_DECL)
11059 /* PARM is a type generic parameter. */
11060 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
11061 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
11062 /* PARM is a generic generic parameter.
11063 Its DIE is a GNU extension. It shall have a
11064 DW_AT_name attribute to represent the name of the template template
11065 parameter, and a DW_AT_GNU_template_name attribute to represent the
11066 name of the template template argument. */
11067 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
11068 parent_die, parm);
11069 else
11070 gcc_unreachable ();
11072 if (tmpl_die)
11074 tree tmpl_type;
11076 /* If PARM is a generic parameter pack, it means we are
11077 emitting debug info for a template argument pack element.
11078 In other terms, ARG is a template argument pack element.
11079 In that case, we don't emit any DW_AT_name attribute for
11080 the die. */
11081 if (emit_name_p)
11083 name = IDENTIFIER_POINTER (DECL_NAME (parm));
11084 gcc_assert (name);
11085 add_AT_string (tmpl_die, DW_AT_name, name);
11088 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
11090 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
11091 TMPL_DIE should have a child DW_AT_type attribute that is set
11092 to the type of the argument to PARM, which is ARG.
11093 If PARM is a type generic parameter, TMPL_DIE should have a
11094 child DW_AT_type that is set to ARG. */
11095 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
11096 add_type_attribute (tmpl_die, tmpl_type,
11097 (TREE_THIS_VOLATILE (tmpl_type)
11098 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
11099 parent_die);
11101 else
11103 /* So TMPL_DIE is a DIE representing a
11104 a generic generic template parameter, a.k.a template template
11105 parameter in C++ and arg is a template. */
11107 /* The DW_AT_GNU_template_name attribute of the DIE must be set
11108 to the name of the argument. */
11109 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
11110 if (name)
11111 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
11114 if (TREE_CODE (parm) == PARM_DECL)
11115 /* So PARM is a non-type generic parameter.
11116 DWARF3 5.6.8 says we must set a DW_AT_const_value child
11117 attribute of TMPL_DIE which value represents the value
11118 of ARG.
11119 We must be careful here:
11120 The value of ARG might reference some function decls.
11121 We might currently be emitting debug info for a generic
11122 type and types are emitted before function decls, we don't
11123 know if the function decls referenced by ARG will actually be
11124 emitted after cgraph computations.
11125 So must defer the generation of the DW_AT_const_value to
11126 after cgraph is ready. */
11127 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
11130 return tmpl_die;
11133 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
11134 PARM_PACK must be a template parameter pack. The returned DIE
11135 will be child DIE of PARENT_DIE. */
11137 static dw_die_ref
11138 template_parameter_pack_die (tree parm_pack,
11139 tree parm_pack_args,
11140 dw_die_ref parent_die)
11142 dw_die_ref die;
11143 int j;
11145 gcc_assert (parent_die && parm_pack);
11147 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
11148 add_name_and_src_coords_attributes (die, parm_pack);
11149 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
11150 generic_parameter_die (parm_pack,
11151 TREE_VEC_ELT (parm_pack_args, j),
11152 false /* Don't emit DW_AT_name */,
11153 die);
11154 return die;
11157 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
11158 an enumerated type. */
11160 static inline int
11161 type_is_enum (const_tree type)
11163 return TREE_CODE (type) == ENUMERAL_TYPE;
11166 /* Return the DBX register number described by a given RTL node. */
11168 static unsigned int
11169 dbx_reg_number (const_rtx rtl)
11171 unsigned regno = REGNO (rtl);
11173 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
11175 #ifdef LEAF_REG_REMAP
11176 if (crtl->uses_only_leaf_regs)
11178 int leaf_reg = LEAF_REG_REMAP (regno);
11179 if (leaf_reg != -1)
11180 regno = (unsigned) leaf_reg;
11182 #endif
11184 regno = DBX_REGISTER_NUMBER (regno);
11185 gcc_assert (regno != INVALID_REGNUM);
11186 return regno;
11189 /* Optionally add a DW_OP_piece term to a location description expression.
11190 DW_OP_piece is only added if the location description expression already
11191 doesn't end with DW_OP_piece. */
11193 static void
11194 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
11196 dw_loc_descr_ref loc;
11198 if (*list_head != NULL)
11200 /* Find the end of the chain. */
11201 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
11204 if (loc->dw_loc_opc != DW_OP_piece)
11205 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
11209 /* Return a location descriptor that designates a machine register or
11210 zero if there is none. */
11212 static dw_loc_descr_ref
11213 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
11215 rtx regs;
11217 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
11218 return 0;
11220 /* We only use "frame base" when we're sure we're talking about the
11221 post-prologue local stack frame. We do this by *not* running
11222 register elimination until this point, and recognizing the special
11223 argument pointer and soft frame pointer rtx's.
11224 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
11225 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
11226 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
11228 dw_loc_descr_ref result = NULL;
11230 if (dwarf_version >= 4 || !dwarf_strict)
11232 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
11233 initialized);
11234 if (result)
11235 add_loc_descr (&result,
11236 new_loc_descr (DW_OP_stack_value, 0, 0));
11238 return result;
11241 regs = targetm.dwarf_register_span (rtl);
11243 if (REG_NREGS (rtl) > 1 || regs)
11244 return multiple_reg_loc_descriptor (rtl, regs, initialized);
11245 else
11247 unsigned int dbx_regnum = dbx_reg_number (rtl);
11248 if (dbx_regnum == IGNORED_DWARF_REGNUM)
11249 return 0;
11250 return one_reg_loc_descriptor (dbx_regnum, initialized);
11254 /* Return a location descriptor that designates a machine register for
11255 a given hard register number. */
11257 static dw_loc_descr_ref
11258 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
11260 dw_loc_descr_ref reg_loc_descr;
11262 if (regno <= 31)
11263 reg_loc_descr
11264 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
11265 else
11266 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
11268 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11269 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11271 return reg_loc_descr;
11274 /* Given an RTL of a register, return a location descriptor that
11275 designates a value that spans more than one register. */
11277 static dw_loc_descr_ref
11278 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
11279 enum var_init_status initialized)
11281 int size, i;
11282 dw_loc_descr_ref loc_result = NULL;
11284 /* Simple, contiguous registers. */
11285 if (regs == NULL_RTX)
11287 unsigned reg = REGNO (rtl);
11288 int nregs;
11290 #ifdef LEAF_REG_REMAP
11291 if (crtl->uses_only_leaf_regs)
11293 int leaf_reg = LEAF_REG_REMAP (reg);
11294 if (leaf_reg != -1)
11295 reg = (unsigned) leaf_reg;
11297 #endif
11299 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
11300 nregs = REG_NREGS (rtl);
11302 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
11304 loc_result = NULL;
11305 while (nregs--)
11307 dw_loc_descr_ref t;
11309 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
11310 VAR_INIT_STATUS_INITIALIZED);
11311 add_loc_descr (&loc_result, t);
11312 add_loc_descr_op_piece (&loc_result, size);
11313 ++reg;
11315 return loc_result;
11318 /* Now onto stupid register sets in non contiguous locations. */
11320 gcc_assert (GET_CODE (regs) == PARALLEL);
11322 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
11323 loc_result = NULL;
11325 for (i = 0; i < XVECLEN (regs, 0); ++i)
11327 dw_loc_descr_ref t;
11329 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
11330 VAR_INIT_STATUS_INITIALIZED);
11331 add_loc_descr (&loc_result, t);
11332 add_loc_descr_op_piece (&loc_result, size);
11335 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
11336 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11337 return loc_result;
11340 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
11342 /* Return a location descriptor that designates a constant i,
11343 as a compound operation from constant (i >> shift), constant shift
11344 and DW_OP_shl. */
11346 static dw_loc_descr_ref
11347 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11349 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
11350 add_loc_descr (&ret, int_loc_descriptor (shift));
11351 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11352 return ret;
11355 /* Return a location descriptor that designates a constant. */
11357 static dw_loc_descr_ref
11358 int_loc_descriptor (HOST_WIDE_INT i)
11360 enum dwarf_location_atom op;
11362 /* Pick the smallest representation of a constant, rather than just
11363 defaulting to the LEB encoding. */
11364 if (i >= 0)
11366 int clz = clz_hwi (i);
11367 int ctz = ctz_hwi (i);
11368 if (i <= 31)
11369 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
11370 else if (i <= 0xff)
11371 op = DW_OP_const1u;
11372 else if (i <= 0xffff)
11373 op = DW_OP_const2u;
11374 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11375 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11376 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
11377 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
11378 while DW_OP_const4u is 5 bytes. */
11379 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
11380 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11381 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11382 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
11383 while DW_OP_const4u is 5 bytes. */
11384 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11385 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11386 op = DW_OP_const4u;
11387 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11388 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11389 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
11390 while DW_OP_constu of constant >= 0x100000000 takes at least
11391 6 bytes. */
11392 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11393 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11394 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
11395 >= HOST_BITS_PER_WIDE_INT)
11396 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
11397 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
11398 while DW_OP_constu takes in this case at least 6 bytes. */
11399 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
11400 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11401 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11402 && size_of_uleb128 (i) > 6)
11403 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
11404 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
11405 else
11406 op = DW_OP_constu;
11408 else
11410 if (i >= -0x80)
11411 op = DW_OP_const1s;
11412 else if (i >= -0x8000)
11413 op = DW_OP_const2s;
11414 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11416 if (size_of_int_loc_descriptor (i) < 5)
11418 dw_loc_descr_ref ret = int_loc_descriptor (-i);
11419 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11420 return ret;
11422 op = DW_OP_const4s;
11424 else
11426 if (size_of_int_loc_descriptor (i)
11427 < (unsigned long) 1 + size_of_sleb128 (i))
11429 dw_loc_descr_ref ret = int_loc_descriptor (-i);
11430 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11431 return ret;
11433 op = DW_OP_consts;
11437 return new_loc_descr (op, i, 0);
11440 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
11441 without actually allocating it. */
11443 static unsigned long
11444 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11446 return size_of_int_loc_descriptor (i >> shift)
11447 + size_of_int_loc_descriptor (shift)
11448 + 1;
11451 /* Return size_of_locs (int_loc_descriptor (i)) without
11452 actually allocating it. */
11454 static unsigned long
11455 size_of_int_loc_descriptor (HOST_WIDE_INT i)
11457 unsigned long s;
11459 if (i >= 0)
11461 int clz, ctz;
11462 if (i <= 31)
11463 return 1;
11464 else if (i <= 0xff)
11465 return 2;
11466 else if (i <= 0xffff)
11467 return 3;
11468 clz = clz_hwi (i);
11469 ctz = ctz_hwi (i);
11470 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11471 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11472 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11473 - clz - 5);
11474 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11475 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11476 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11477 - clz - 8);
11478 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11479 return 5;
11480 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
11481 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11482 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11483 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11484 - clz - 8);
11485 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11486 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
11487 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11488 - clz - 16);
11489 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11490 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11491 && s > 6)
11492 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11493 - clz - 32);
11494 else
11495 return 1 + s;
11497 else
11499 if (i >= -0x80)
11500 return 2;
11501 else if (i >= -0x8000)
11502 return 3;
11503 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11505 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11507 s = size_of_int_loc_descriptor (-i) + 1;
11508 if (s < 5)
11509 return s;
11511 return 5;
11513 else
11515 unsigned long r = 1 + size_of_sleb128 (i);
11516 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11518 s = size_of_int_loc_descriptor (-i) + 1;
11519 if (s < r)
11520 return s;
11522 return r;
11527 /* Return loc description representing "address" of integer value.
11528 This can appear only as toplevel expression. */
11530 static dw_loc_descr_ref
11531 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
11533 int litsize;
11534 dw_loc_descr_ref loc_result = NULL;
11536 if (!(dwarf_version >= 4 || !dwarf_strict))
11537 return NULL;
11539 litsize = size_of_int_loc_descriptor (i);
11540 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
11541 is more compact. For DW_OP_stack_value we need:
11542 litsize + 1 (DW_OP_stack_value)
11543 and for DW_OP_implicit_value:
11544 1 (DW_OP_implicit_value) + 1 (length) + size. */
11545 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
11547 loc_result = int_loc_descriptor (i);
11548 add_loc_descr (&loc_result,
11549 new_loc_descr (DW_OP_stack_value, 0, 0));
11550 return loc_result;
11553 loc_result = new_loc_descr (DW_OP_implicit_value,
11554 size, 0);
11555 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
11556 loc_result->dw_loc_oprnd2.v.val_int = i;
11557 return loc_result;
11560 /* Return a location descriptor that designates a base+offset location. */
11562 static dw_loc_descr_ref
11563 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
11564 enum var_init_status initialized)
11566 unsigned int regno;
11567 dw_loc_descr_ref result;
11568 dw_fde_ref fde = cfun->fde;
11570 /* We only use "frame base" when we're sure we're talking about the
11571 post-prologue local stack frame. We do this by *not* running
11572 register elimination until this point, and recognizing the special
11573 argument pointer and soft frame pointer rtx's. */
11574 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
11576 rtx elim = (ira_use_lra_p
11577 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
11578 : eliminate_regs (reg, VOIDmode, NULL_RTX));
11580 if (elim != reg)
11582 if (GET_CODE (elim) == PLUS)
11584 offset += INTVAL (XEXP (elim, 1));
11585 elim = XEXP (elim, 0);
11587 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
11588 && (elim == hard_frame_pointer_rtx
11589 || elim == stack_pointer_rtx))
11590 || elim == (frame_pointer_needed
11591 ? hard_frame_pointer_rtx
11592 : stack_pointer_rtx));
11594 /* If drap register is used to align stack, use frame
11595 pointer + offset to access stack variables. If stack
11596 is aligned without drap, use stack pointer + offset to
11597 access stack variables. */
11598 if (crtl->stack_realign_tried
11599 && reg == frame_pointer_rtx)
11601 int base_reg
11602 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
11603 ? HARD_FRAME_POINTER_REGNUM
11604 : REGNO (elim));
11605 return new_reg_loc_descr (base_reg, offset);
11608 gcc_assert (frame_pointer_fb_offset_valid);
11609 offset += frame_pointer_fb_offset;
11610 return new_loc_descr (DW_OP_fbreg, offset, 0);
11614 regno = REGNO (reg);
11615 #ifdef LEAF_REG_REMAP
11616 if (crtl->uses_only_leaf_regs)
11618 int leaf_reg = LEAF_REG_REMAP (regno);
11619 if (leaf_reg != -1)
11620 regno = (unsigned) leaf_reg;
11622 #endif
11623 regno = DWARF_FRAME_REGNUM (regno);
11625 if (!optimize && fde
11626 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
11628 /* Use cfa+offset to represent the location of arguments passed
11629 on the stack when drap is used to align stack.
11630 Only do this when not optimizing, for optimized code var-tracking
11631 is supposed to track where the arguments live and the register
11632 used as vdrap or drap in some spot might be used for something
11633 else in other part of the routine. */
11634 return new_loc_descr (DW_OP_fbreg, offset, 0);
11637 if (regno <= 31)
11638 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
11639 offset, 0);
11640 else
11641 result = new_loc_descr (DW_OP_bregx, regno, offset);
11643 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11644 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11646 return result;
11649 /* Return true if this RTL expression describes a base+offset calculation. */
11651 static inline int
11652 is_based_loc (const_rtx rtl)
11654 return (GET_CODE (rtl) == PLUS
11655 && ((REG_P (XEXP (rtl, 0))
11656 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
11657 && CONST_INT_P (XEXP (rtl, 1)))));
11660 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
11661 failed. */
11663 static dw_loc_descr_ref
11664 tls_mem_loc_descriptor (rtx mem)
11666 tree base;
11667 dw_loc_descr_ref loc_result;
11669 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
11670 return NULL;
11672 base = get_base_address (MEM_EXPR (mem));
11673 if (base == NULL
11674 || TREE_CODE (base) != VAR_DECL
11675 || !DECL_THREAD_LOCAL_P (base))
11676 return NULL;
11678 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
11679 if (loc_result == NULL)
11680 return NULL;
11682 if (MEM_OFFSET (mem))
11683 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
11685 return loc_result;
11688 /* Output debug info about reason why we failed to expand expression as dwarf
11689 expression. */
11691 static void
11692 expansion_failed (tree expr, rtx rtl, char const *reason)
11694 if (dump_file && (dump_flags & TDF_DETAILS))
11696 fprintf (dump_file, "Failed to expand as dwarf: ");
11697 if (expr)
11698 print_generic_expr (dump_file, expr, dump_flags);
11699 if (rtl)
11701 fprintf (dump_file, "\n");
11702 print_rtl (dump_file, rtl);
11704 fprintf (dump_file, "\nReason: %s\n", reason);
11708 /* Helper function for const_ok_for_output. */
11710 static bool
11711 const_ok_for_output_1 (rtx rtl)
11713 if (GET_CODE (rtl) == UNSPEC)
11715 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
11716 we can't express it in the debug info. */
11717 #ifdef ENABLE_CHECKING
11718 /* Don't complain about TLS UNSPECs, those are just too hard to
11719 delegitimize. Note this could be a non-decl SYMBOL_REF such as
11720 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
11721 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
11722 if (XVECLEN (rtl, 0) == 0
11723 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
11724 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE)
11725 inform (current_function_decl
11726 ? DECL_SOURCE_LOCATION (current_function_decl)
11727 : UNKNOWN_LOCATION,
11728 #if NUM_UNSPEC_VALUES > 0
11729 "non-delegitimized UNSPEC %s (%d) found in variable location",
11730 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
11731 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
11732 XINT (rtl, 1));
11733 #else
11734 "non-delegitimized UNSPEC %d found in variable location",
11735 XINT (rtl, 1));
11736 #endif
11737 #endif
11738 expansion_failed (NULL_TREE, rtl,
11739 "UNSPEC hasn't been delegitimized.\n");
11740 return false;
11743 if (targetm.const_not_ok_for_debug_p (rtl))
11745 expansion_failed (NULL_TREE, rtl,
11746 "Expression rejected for debug by the backend.\n");
11747 return false;
11750 /* FIXME: Refer to PR60655. It is possible for simplification
11751 of rtl expressions in var tracking to produce such expressions.
11752 We should really identify / validate expressions
11753 enclosed in CONST that can be handled by assemblers on various
11754 targets and only handle legitimate cases here. */
11755 if (GET_CODE (rtl) != SYMBOL_REF)
11757 if (GET_CODE (rtl) == NOT)
11758 return false;
11759 return true;
11762 if (CONSTANT_POOL_ADDRESS_P (rtl))
11764 bool marked;
11765 get_pool_constant_mark (rtl, &marked);
11766 /* If all references to this pool constant were optimized away,
11767 it was not output and thus we can't represent it. */
11768 if (!marked)
11770 expansion_failed (NULL_TREE, rtl,
11771 "Constant was removed from constant pool.\n");
11772 return false;
11776 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11777 return false;
11779 /* Avoid references to external symbols in debug info, on several targets
11780 the linker might even refuse to link when linking a shared library,
11781 and in many other cases the relocations for .debug_info/.debug_loc are
11782 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
11783 to be defined within the same shared library or executable are fine. */
11784 if (SYMBOL_REF_EXTERNAL_P (rtl))
11786 tree decl = SYMBOL_REF_DECL (rtl);
11788 if (decl == NULL || !targetm.binds_local_p (decl))
11790 expansion_failed (NULL_TREE, rtl,
11791 "Symbol not defined in current TU.\n");
11792 return false;
11796 return true;
11799 /* Return true if constant RTL can be emitted in DW_OP_addr or
11800 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
11801 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
11803 static bool
11804 const_ok_for_output (rtx rtl)
11806 if (GET_CODE (rtl) == SYMBOL_REF)
11807 return const_ok_for_output_1 (rtl);
11809 if (GET_CODE (rtl) == CONST)
11811 subrtx_var_iterator::array_type array;
11812 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
11813 if (!const_ok_for_output_1 (*iter))
11814 return false;
11815 return true;
11818 return true;
11821 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
11822 if possible, NULL otherwise. */
11824 static dw_die_ref
11825 base_type_for_mode (machine_mode mode, bool unsignedp)
11827 dw_die_ref type_die;
11828 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11830 if (type == NULL)
11831 return NULL;
11832 switch (TREE_CODE (type))
11834 case INTEGER_TYPE:
11835 case REAL_TYPE:
11836 break;
11837 default:
11838 return NULL;
11840 type_die = lookup_type_die (type);
11841 if (!type_die)
11842 type_die = modified_type_die (type, TYPE_UNQUALIFIED, comp_unit_die ());
11843 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
11844 return NULL;
11845 return type_die;
11848 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
11849 type matching MODE, or, if MODE is narrower than or as wide as
11850 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
11851 possible. */
11853 static dw_loc_descr_ref
11854 convert_descriptor_to_mode (machine_mode mode, dw_loc_descr_ref op)
11856 machine_mode outer_mode = mode;
11857 dw_die_ref type_die;
11858 dw_loc_descr_ref cvt;
11860 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11862 add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
11863 return op;
11865 type_die = base_type_for_mode (outer_mode, 1);
11866 if (type_die == NULL)
11867 return NULL;
11868 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11869 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11870 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11871 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11872 add_loc_descr (&op, cvt);
11873 return op;
11876 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
11878 static dw_loc_descr_ref
11879 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
11880 dw_loc_descr_ref op1)
11882 dw_loc_descr_ref ret = op0;
11883 add_loc_descr (&ret, op1);
11884 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11885 if (STORE_FLAG_VALUE != 1)
11887 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
11888 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
11890 return ret;
11893 /* Return location descriptor for signed comparison OP RTL. */
11895 static dw_loc_descr_ref
11896 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11897 machine_mode mem_mode)
11899 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11900 dw_loc_descr_ref op0, op1;
11901 int shift;
11903 if (op_mode == VOIDmode)
11904 op_mode = GET_MODE (XEXP (rtl, 1));
11905 if (op_mode == VOIDmode)
11906 return NULL;
11908 if (dwarf_strict
11909 && (GET_MODE_CLASS (op_mode) != MODE_INT
11910 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
11911 return NULL;
11913 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11914 VAR_INIT_STATUS_INITIALIZED);
11915 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11916 VAR_INIT_STATUS_INITIALIZED);
11918 if (op0 == NULL || op1 == NULL)
11919 return NULL;
11921 if (GET_MODE_CLASS (op_mode) != MODE_INT
11922 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11923 return compare_loc_descriptor (op, op0, op1);
11925 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11927 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
11928 dw_loc_descr_ref cvt;
11930 if (type_die == NULL)
11931 return NULL;
11932 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11933 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11934 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11935 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11936 add_loc_descr (&op0, cvt);
11937 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11938 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11939 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11940 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11941 add_loc_descr (&op1, cvt);
11942 return compare_loc_descriptor (op, op0, op1);
11945 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
11946 /* For eq/ne, if the operands are known to be zero-extended,
11947 there is no need to do the fancy shifting up. */
11948 if (op == DW_OP_eq || op == DW_OP_ne)
11950 dw_loc_descr_ref last0, last1;
11951 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11953 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11955 /* deref_size zero extends, and for constants we can check
11956 whether they are zero extended or not. */
11957 if (((last0->dw_loc_opc == DW_OP_deref_size
11958 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11959 || (CONST_INT_P (XEXP (rtl, 0))
11960 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
11961 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
11962 && ((last1->dw_loc_opc == DW_OP_deref_size
11963 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11964 || (CONST_INT_P (XEXP (rtl, 1))
11965 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
11966 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
11967 return compare_loc_descriptor (op, op0, op1);
11969 /* EQ/NE comparison against constant in narrower type than
11970 DWARF2_ADDR_SIZE can be performed either as
11971 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
11972 DW_OP_{eq,ne}
11974 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
11975 DW_OP_{eq,ne}. Pick whatever is shorter. */
11976 if (CONST_INT_P (XEXP (rtl, 1))
11977 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
11978 && (size_of_int_loc_descriptor (shift) + 1
11979 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
11980 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
11981 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11982 & GET_MODE_MASK (op_mode))))
11984 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
11985 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11986 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11987 & GET_MODE_MASK (op_mode));
11988 return compare_loc_descriptor (op, op0, op1);
11991 add_loc_descr (&op0, int_loc_descriptor (shift));
11992 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11993 if (CONST_INT_P (XEXP (rtl, 1)))
11994 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
11995 else
11997 add_loc_descr (&op1, int_loc_descriptor (shift));
11998 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
12000 return compare_loc_descriptor (op, op0, op1);
12003 /* Return location descriptor for unsigned comparison OP RTL. */
12005 static dw_loc_descr_ref
12006 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
12007 machine_mode mem_mode)
12009 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
12010 dw_loc_descr_ref op0, op1;
12012 if (op_mode == VOIDmode)
12013 op_mode = GET_MODE (XEXP (rtl, 1));
12014 if (op_mode == VOIDmode)
12015 return NULL;
12016 if (GET_MODE_CLASS (op_mode) != MODE_INT)
12017 return NULL;
12019 if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
12020 return NULL;
12022 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
12023 VAR_INIT_STATUS_INITIALIZED);
12024 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
12025 VAR_INIT_STATUS_INITIALIZED);
12027 if (op0 == NULL || op1 == NULL)
12028 return NULL;
12030 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
12032 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
12033 dw_loc_descr_ref last0, last1;
12034 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
12036 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
12038 if (CONST_INT_P (XEXP (rtl, 0)))
12039 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
12040 /* deref_size zero extends, so no need to mask it again. */
12041 else if (last0->dw_loc_opc != DW_OP_deref_size
12042 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
12044 add_loc_descr (&op0, int_loc_descriptor (mask));
12045 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
12047 if (CONST_INT_P (XEXP (rtl, 1)))
12048 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
12049 /* deref_size zero extends, so no need to mask it again. */
12050 else if (last1->dw_loc_opc != DW_OP_deref_size
12051 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
12053 add_loc_descr (&op1, int_loc_descriptor (mask));
12054 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
12057 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
12059 HOST_WIDE_INT bias = 1;
12060 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
12061 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12062 if (CONST_INT_P (XEXP (rtl, 1)))
12063 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
12064 + INTVAL (XEXP (rtl, 1)));
12065 else
12066 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
12067 bias, 0));
12069 return compare_loc_descriptor (op, op0, op1);
12072 /* Return location descriptor for {U,S}{MIN,MAX}. */
12074 static dw_loc_descr_ref
12075 minmax_loc_descriptor (rtx rtl, machine_mode mode,
12076 machine_mode mem_mode)
12078 enum dwarf_location_atom op;
12079 dw_loc_descr_ref op0, op1, ret;
12080 dw_loc_descr_ref bra_node, drop_node;
12082 if (dwarf_strict
12083 && (GET_MODE_CLASS (mode) != MODE_INT
12084 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
12085 return NULL;
12087 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12088 VAR_INIT_STATUS_INITIALIZED);
12089 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12090 VAR_INIT_STATUS_INITIALIZED);
12092 if (op0 == NULL || op1 == NULL)
12093 return NULL;
12095 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
12096 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
12097 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
12098 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
12100 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12102 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
12103 add_loc_descr (&op0, int_loc_descriptor (mask));
12104 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
12105 add_loc_descr (&op1, int_loc_descriptor (mask));
12106 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
12108 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12110 HOST_WIDE_INT bias = 1;
12111 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
12112 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12113 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12116 else if (GET_MODE_CLASS (mode) == MODE_INT
12117 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12119 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
12120 add_loc_descr (&op0, int_loc_descriptor (shift));
12121 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
12122 add_loc_descr (&op1, int_loc_descriptor (shift));
12123 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
12125 else if (GET_MODE_CLASS (mode) == MODE_INT
12126 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12128 dw_die_ref type_die = base_type_for_mode (mode, 0);
12129 dw_loc_descr_ref cvt;
12130 if (type_die == NULL)
12131 return NULL;
12132 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12133 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12134 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12135 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12136 add_loc_descr (&op0, cvt);
12137 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12138 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12139 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12140 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12141 add_loc_descr (&op1, cvt);
12144 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
12145 op = DW_OP_lt;
12146 else
12147 op = DW_OP_gt;
12148 ret = op0;
12149 add_loc_descr (&ret, op1);
12150 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
12151 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12152 add_loc_descr (&ret, bra_node);
12153 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12154 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
12155 add_loc_descr (&ret, drop_node);
12156 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12157 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
12158 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
12159 && GET_MODE_CLASS (mode) == MODE_INT
12160 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12161 ret = convert_descriptor_to_mode (mode, ret);
12162 return ret;
12165 /* Helper function for mem_loc_descriptor. Perform OP binary op,
12166 but after converting arguments to type_die, afterwards
12167 convert back to unsigned. */
12169 static dw_loc_descr_ref
12170 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
12171 machine_mode mode, machine_mode mem_mode)
12173 dw_loc_descr_ref cvt, op0, op1;
12175 if (type_die == NULL)
12176 return NULL;
12177 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12178 VAR_INIT_STATUS_INITIALIZED);
12179 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12180 VAR_INIT_STATUS_INITIALIZED);
12181 if (op0 == NULL || op1 == NULL)
12182 return NULL;
12183 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12184 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12185 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12186 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12187 add_loc_descr (&op0, cvt);
12188 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12189 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12190 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12191 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12192 add_loc_descr (&op1, cvt);
12193 add_loc_descr (&op0, op1);
12194 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
12195 return convert_descriptor_to_mode (mode, op0);
12198 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
12199 const0 is DW_OP_lit0 or corresponding typed constant,
12200 const1 is DW_OP_lit1 or corresponding typed constant
12201 and constMSB is constant with just the MSB bit set
12202 for the mode):
12203 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
12204 L1: const0 DW_OP_swap
12205 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
12206 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12207 L3: DW_OP_drop
12208 L4: DW_OP_nop
12210 CTZ is similar:
12211 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
12212 L1: const0 DW_OP_swap
12213 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
12214 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12215 L3: DW_OP_drop
12216 L4: DW_OP_nop
12218 FFS is similar:
12219 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
12220 L1: const1 DW_OP_swap
12221 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
12222 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12223 L3: DW_OP_drop
12224 L4: DW_OP_nop */
12226 static dw_loc_descr_ref
12227 clz_loc_descriptor (rtx rtl, machine_mode mode,
12228 machine_mode mem_mode)
12230 dw_loc_descr_ref op0, ret, tmp;
12231 HOST_WIDE_INT valv;
12232 dw_loc_descr_ref l1jump, l1label;
12233 dw_loc_descr_ref l2jump, l2label;
12234 dw_loc_descr_ref l3jump, l3label;
12235 dw_loc_descr_ref l4jump, l4label;
12236 rtx msb;
12238 if (GET_MODE_CLASS (mode) != MODE_INT
12239 || GET_MODE (XEXP (rtl, 0)) != mode)
12240 return NULL;
12242 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12243 VAR_INIT_STATUS_INITIALIZED);
12244 if (op0 == NULL)
12245 return NULL;
12246 ret = op0;
12247 if (GET_CODE (rtl) == CLZ)
12249 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12250 valv = GET_MODE_BITSIZE (mode);
12252 else if (GET_CODE (rtl) == FFS)
12253 valv = 0;
12254 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12255 valv = GET_MODE_BITSIZE (mode);
12256 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12257 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
12258 add_loc_descr (&ret, l1jump);
12259 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12260 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
12261 VAR_INIT_STATUS_INITIALIZED);
12262 if (tmp == NULL)
12263 return NULL;
12264 add_loc_descr (&ret, tmp);
12265 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
12266 add_loc_descr (&ret, l4jump);
12267 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
12268 ? const1_rtx : const0_rtx,
12269 mode, mem_mode,
12270 VAR_INIT_STATUS_INITIALIZED);
12271 if (l1label == NULL)
12272 return NULL;
12273 add_loc_descr (&ret, l1label);
12274 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12275 l2label = new_loc_descr (DW_OP_dup, 0, 0);
12276 add_loc_descr (&ret, l2label);
12277 if (GET_CODE (rtl) != CLZ)
12278 msb = const1_rtx;
12279 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
12280 msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
12281 << (GET_MODE_BITSIZE (mode) - 1));
12282 else
12283 msb = immed_wide_int_const
12284 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
12285 GET_MODE_PRECISION (mode)), mode);
12286 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
12287 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12288 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
12289 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
12290 else
12291 tmp = mem_loc_descriptor (msb, mode, mem_mode,
12292 VAR_INIT_STATUS_INITIALIZED);
12293 if (tmp == NULL)
12294 return NULL;
12295 add_loc_descr (&ret, tmp);
12296 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12297 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
12298 add_loc_descr (&ret, l3jump);
12299 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12300 VAR_INIT_STATUS_INITIALIZED);
12301 if (tmp == NULL)
12302 return NULL;
12303 add_loc_descr (&ret, tmp);
12304 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
12305 ? DW_OP_shl : DW_OP_shr, 0, 0));
12306 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12307 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
12308 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12309 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
12310 add_loc_descr (&ret, l2jump);
12311 l3label = new_loc_descr (DW_OP_drop, 0, 0);
12312 add_loc_descr (&ret, l3label);
12313 l4label = new_loc_descr (DW_OP_nop, 0, 0);
12314 add_loc_descr (&ret, l4label);
12315 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12316 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12317 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12318 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12319 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12320 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
12321 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12322 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
12323 return ret;
12326 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
12327 const1 is DW_OP_lit1 or corresponding typed constant):
12328 const0 DW_OP_swap
12329 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
12330 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
12331 L2: DW_OP_drop
12333 PARITY is similar:
12334 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
12335 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
12336 L2: DW_OP_drop */
12338 static dw_loc_descr_ref
12339 popcount_loc_descriptor (rtx rtl, machine_mode mode,
12340 machine_mode mem_mode)
12342 dw_loc_descr_ref op0, ret, tmp;
12343 dw_loc_descr_ref l1jump, l1label;
12344 dw_loc_descr_ref l2jump, l2label;
12346 if (GET_MODE_CLASS (mode) != MODE_INT
12347 || GET_MODE (XEXP (rtl, 0)) != mode)
12348 return NULL;
12350 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12351 VAR_INIT_STATUS_INITIALIZED);
12352 if (op0 == NULL)
12353 return NULL;
12354 ret = op0;
12355 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12356 VAR_INIT_STATUS_INITIALIZED);
12357 if (tmp == NULL)
12358 return NULL;
12359 add_loc_descr (&ret, tmp);
12360 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12361 l1label = new_loc_descr (DW_OP_dup, 0, 0);
12362 add_loc_descr (&ret, l1label);
12363 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12364 add_loc_descr (&ret, l2jump);
12365 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12366 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12367 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12368 VAR_INIT_STATUS_INITIALIZED);
12369 if (tmp == NULL)
12370 return NULL;
12371 add_loc_descr (&ret, tmp);
12372 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12373 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
12374 ? DW_OP_plus : DW_OP_xor, 0, 0));
12375 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12376 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12377 VAR_INIT_STATUS_INITIALIZED);
12378 add_loc_descr (&ret, tmp);
12379 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12380 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12381 add_loc_descr (&ret, l1jump);
12382 l2label = new_loc_descr (DW_OP_drop, 0, 0);
12383 add_loc_descr (&ret, l2label);
12384 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12385 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12386 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12387 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12388 return ret;
12391 /* BSWAP (constS is initial shift count, either 56 or 24):
12392 constS const0
12393 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
12394 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
12395 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
12396 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
12397 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
12399 static dw_loc_descr_ref
12400 bswap_loc_descriptor (rtx rtl, machine_mode mode,
12401 machine_mode mem_mode)
12403 dw_loc_descr_ref op0, ret, tmp;
12404 dw_loc_descr_ref l1jump, l1label;
12405 dw_loc_descr_ref l2jump, l2label;
12407 if (GET_MODE_CLASS (mode) != MODE_INT
12408 || BITS_PER_UNIT != 8
12409 || (GET_MODE_BITSIZE (mode) != 32
12410 && GET_MODE_BITSIZE (mode) != 64))
12411 return NULL;
12413 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12414 VAR_INIT_STATUS_INITIALIZED);
12415 if (op0 == NULL)
12416 return NULL;
12418 ret = op0;
12419 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12420 mode, mem_mode,
12421 VAR_INIT_STATUS_INITIALIZED);
12422 if (tmp == NULL)
12423 return NULL;
12424 add_loc_descr (&ret, tmp);
12425 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12426 VAR_INIT_STATUS_INITIALIZED);
12427 if (tmp == NULL)
12428 return NULL;
12429 add_loc_descr (&ret, tmp);
12430 l1label = new_loc_descr (DW_OP_pick, 2, 0);
12431 add_loc_descr (&ret, l1label);
12432 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12433 mode, mem_mode,
12434 VAR_INIT_STATUS_INITIALIZED);
12435 add_loc_descr (&ret, tmp);
12436 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
12437 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12438 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12439 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
12440 VAR_INIT_STATUS_INITIALIZED);
12441 if (tmp == NULL)
12442 return NULL;
12443 add_loc_descr (&ret, tmp);
12444 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12445 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
12446 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12447 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12448 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12449 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12450 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12451 VAR_INIT_STATUS_INITIALIZED);
12452 add_loc_descr (&ret, tmp);
12453 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
12454 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12455 add_loc_descr (&ret, l2jump);
12456 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
12457 VAR_INIT_STATUS_INITIALIZED);
12458 add_loc_descr (&ret, tmp);
12459 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12460 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12461 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12462 add_loc_descr (&ret, l1jump);
12463 l2label = new_loc_descr (DW_OP_drop, 0, 0);
12464 add_loc_descr (&ret, l2label);
12465 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12466 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12467 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12468 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12469 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12470 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12471 return ret;
12474 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
12475 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12476 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
12477 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
12479 ROTATERT is similar:
12480 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
12481 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12482 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
12484 static dw_loc_descr_ref
12485 rotate_loc_descriptor (rtx rtl, machine_mode mode,
12486 machine_mode mem_mode)
12488 rtx rtlop1 = XEXP (rtl, 1);
12489 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
12490 int i;
12492 if (GET_MODE_CLASS (mode) != MODE_INT)
12493 return NULL;
12495 if (GET_MODE (rtlop1) != VOIDmode
12496 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
12497 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12498 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12499 VAR_INIT_STATUS_INITIALIZED);
12500 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12501 VAR_INIT_STATUS_INITIALIZED);
12502 if (op0 == NULL || op1 == NULL)
12503 return NULL;
12504 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12505 for (i = 0; i < 2; i++)
12507 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
12508 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
12509 mode, mem_mode,
12510 VAR_INIT_STATUS_INITIALIZED);
12511 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12512 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12513 ? DW_OP_const4u
12514 : HOST_BITS_PER_WIDE_INT == 64
12515 ? DW_OP_const8u : DW_OP_constu,
12516 GET_MODE_MASK (mode), 0);
12517 else
12518 mask[i] = NULL;
12519 if (mask[i] == NULL)
12520 return NULL;
12521 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
12523 ret = op0;
12524 add_loc_descr (&ret, op1);
12525 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12526 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12527 if (GET_CODE (rtl) == ROTATERT)
12529 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12530 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12531 GET_MODE_BITSIZE (mode), 0));
12533 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12534 if (mask[0] != NULL)
12535 add_loc_descr (&ret, mask[0]);
12536 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12537 if (mask[1] != NULL)
12539 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12540 add_loc_descr (&ret, mask[1]);
12541 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12543 if (GET_CODE (rtl) == ROTATE)
12545 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12546 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12547 GET_MODE_BITSIZE (mode), 0));
12549 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12550 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12551 return ret;
12554 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
12555 for DEBUG_PARAMETER_REF RTL. */
12557 static dw_loc_descr_ref
12558 parameter_ref_descriptor (rtx rtl)
12560 dw_loc_descr_ref ret;
12561 dw_die_ref ref;
12563 if (dwarf_strict)
12564 return NULL;
12565 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
12566 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
12567 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
12568 if (ref)
12570 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12571 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
12572 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
12574 else
12576 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
12577 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
12579 return ret;
12582 /* The following routine converts the RTL for a variable or parameter
12583 (resident in memory) into an equivalent Dwarf representation of a
12584 mechanism for getting the address of that same variable onto the top of a
12585 hypothetical "address evaluation" stack.
12587 When creating memory location descriptors, we are effectively transforming
12588 the RTL for a memory-resident object into its Dwarf postfix expression
12589 equivalent. This routine recursively descends an RTL tree, turning
12590 it into Dwarf postfix code as it goes.
12592 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
12594 MEM_MODE is the mode of the memory reference, needed to handle some
12595 autoincrement addressing modes.
12597 Return 0 if we can't represent the location. */
12599 dw_loc_descr_ref
12600 mem_loc_descriptor (rtx rtl, machine_mode mode,
12601 machine_mode mem_mode,
12602 enum var_init_status initialized)
12604 dw_loc_descr_ref mem_loc_result = NULL;
12605 enum dwarf_location_atom op;
12606 dw_loc_descr_ref op0, op1;
12607 rtx inner = NULL_RTX;
12609 if (mode == VOIDmode)
12610 mode = GET_MODE (rtl);
12612 /* Note that for a dynamically sized array, the location we will generate a
12613 description of here will be the lowest numbered location which is
12614 actually within the array. That's *not* necessarily the same as the
12615 zeroth element of the array. */
12617 rtl = targetm.delegitimize_address (rtl);
12619 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
12620 return NULL;
12622 switch (GET_CODE (rtl))
12624 case POST_INC:
12625 case POST_DEC:
12626 case POST_MODIFY:
12627 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
12629 case SUBREG:
12630 /* The case of a subreg may arise when we have a local (register)
12631 variable or a formal (register) parameter which doesn't quite fill
12632 up an entire register. For now, just assume that it is
12633 legitimate to make the Dwarf info refer to the whole register which
12634 contains the given subreg. */
12635 if (!subreg_lowpart_p (rtl))
12636 break;
12637 inner = SUBREG_REG (rtl);
12638 case TRUNCATE:
12639 if (inner == NULL_RTX)
12640 inner = XEXP (rtl, 0);
12641 if (GET_MODE_CLASS (mode) == MODE_INT
12642 && GET_MODE_CLASS (GET_MODE (inner)) == MODE_INT
12643 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12644 #ifdef POINTERS_EXTEND_UNSIGNED
12645 || (mode == Pmode && mem_mode != VOIDmode)
12646 #endif
12648 && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
12650 mem_loc_result = mem_loc_descriptor (inner,
12651 GET_MODE (inner),
12652 mem_mode, initialized);
12653 break;
12655 if (dwarf_strict)
12656 break;
12657 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
12658 break;
12659 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
12660 && (GET_MODE_CLASS (mode) != MODE_INT
12661 || GET_MODE_CLASS (GET_MODE (inner)) != MODE_INT))
12662 break;
12663 else
12665 dw_die_ref type_die;
12666 dw_loc_descr_ref cvt;
12668 mem_loc_result = mem_loc_descriptor (inner,
12669 GET_MODE (inner),
12670 mem_mode, initialized);
12671 if (mem_loc_result == NULL)
12672 break;
12673 type_die = base_type_for_mode (mode,
12674 GET_MODE_CLASS (mode) == MODE_INT);
12675 if (type_die == NULL)
12677 mem_loc_result = NULL;
12678 break;
12680 if (GET_MODE_SIZE (mode)
12681 != GET_MODE_SIZE (GET_MODE (inner)))
12682 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12683 else
12684 cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
12685 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12686 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12687 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12688 add_loc_descr (&mem_loc_result, cvt);
12690 break;
12692 case REG:
12693 if (GET_MODE_CLASS (mode) != MODE_INT
12694 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12695 && rtl != arg_pointer_rtx
12696 && rtl != frame_pointer_rtx
12697 #ifdef POINTERS_EXTEND_UNSIGNED
12698 && (mode != Pmode || mem_mode == VOIDmode)
12699 #endif
12702 dw_die_ref type_die;
12703 unsigned int dbx_regnum;
12705 if (dwarf_strict)
12706 break;
12707 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
12708 break;
12709 type_die = base_type_for_mode (mode,
12710 GET_MODE_CLASS (mode) == MODE_INT);
12711 if (type_die == NULL)
12712 break;
12714 dbx_regnum = dbx_reg_number (rtl);
12715 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12716 break;
12717 mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
12718 dbx_regnum, 0);
12719 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12720 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12721 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
12722 break;
12724 /* Whenever a register number forms a part of the description of the
12725 method for calculating the (dynamic) address of a memory resident
12726 object, DWARF rules require the register number be referred to as
12727 a "base register". This distinction is not based in any way upon
12728 what category of register the hardware believes the given register
12729 belongs to. This is strictly DWARF terminology we're dealing with
12730 here. Note that in cases where the location of a memory-resident
12731 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
12732 OP_CONST (0)) the actual DWARF location descriptor that we generate
12733 may just be OP_BASEREG (basereg). This may look deceptively like
12734 the object in question was allocated to a register (rather than in
12735 memory) so DWARF consumers need to be aware of the subtle
12736 distinction between OP_REG and OP_BASEREG. */
12737 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
12738 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
12739 else if (stack_realign_drap
12740 && crtl->drap_reg
12741 && crtl->args.internal_arg_pointer == rtl
12742 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
12744 /* If RTL is internal_arg_pointer, which has been optimized
12745 out, use DRAP instead. */
12746 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
12747 VAR_INIT_STATUS_INITIALIZED);
12749 break;
12751 case SIGN_EXTEND:
12752 case ZERO_EXTEND:
12753 if (GET_MODE_CLASS (mode) != MODE_INT)
12754 break;
12755 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12756 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12757 if (op0 == 0)
12758 break;
12759 else if (GET_CODE (rtl) == ZERO_EXTEND
12760 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12761 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12762 < HOST_BITS_PER_WIDE_INT
12763 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
12764 to expand zero extend as two shifts instead of
12765 masking. */
12766 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
12768 machine_mode imode = GET_MODE (XEXP (rtl, 0));
12769 mem_loc_result = op0;
12770 add_loc_descr (&mem_loc_result,
12771 int_loc_descriptor (GET_MODE_MASK (imode)));
12772 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
12774 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12776 int shift = DWARF2_ADDR_SIZE
12777 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
12778 shift *= BITS_PER_UNIT;
12779 if (GET_CODE (rtl) == SIGN_EXTEND)
12780 op = DW_OP_shra;
12781 else
12782 op = DW_OP_shr;
12783 mem_loc_result = op0;
12784 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12785 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12786 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12787 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12789 else if (!dwarf_strict)
12791 dw_die_ref type_die1, type_die2;
12792 dw_loc_descr_ref cvt;
12794 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12795 GET_CODE (rtl) == ZERO_EXTEND);
12796 if (type_die1 == NULL)
12797 break;
12798 type_die2 = base_type_for_mode (mode, 1);
12799 if (type_die2 == NULL)
12800 break;
12801 mem_loc_result = op0;
12802 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12803 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12804 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
12805 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12806 add_loc_descr (&mem_loc_result, cvt);
12807 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12808 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12809 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
12810 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12811 add_loc_descr (&mem_loc_result, cvt);
12813 break;
12815 case MEM:
12817 rtx new_rtl = avoid_constant_pool_reference (rtl);
12818 if (new_rtl != rtl)
12820 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
12821 initialized);
12822 if (mem_loc_result != NULL)
12823 return mem_loc_result;
12826 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
12827 get_address_mode (rtl), mode,
12828 VAR_INIT_STATUS_INITIALIZED);
12829 if (mem_loc_result == NULL)
12830 mem_loc_result = tls_mem_loc_descriptor (rtl);
12831 if (mem_loc_result != NULL)
12833 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12834 || GET_MODE_CLASS (mode) != MODE_INT)
12836 dw_die_ref type_die;
12837 dw_loc_descr_ref deref;
12839 if (dwarf_strict)
12840 return NULL;
12841 type_die
12842 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
12843 if (type_die == NULL)
12844 return NULL;
12845 deref = new_loc_descr (DW_OP_GNU_deref_type,
12846 GET_MODE_SIZE (mode), 0);
12847 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12848 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12849 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
12850 add_loc_descr (&mem_loc_result, deref);
12852 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12853 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
12854 else
12855 add_loc_descr (&mem_loc_result,
12856 new_loc_descr (DW_OP_deref_size,
12857 GET_MODE_SIZE (mode), 0));
12859 break;
12861 case LO_SUM:
12862 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
12864 case LABEL_REF:
12865 /* Some ports can transform a symbol ref into a label ref, because
12866 the symbol ref is too far away and has to be dumped into a constant
12867 pool. */
12868 case CONST:
12869 case SYMBOL_REF:
12870 if ((GET_MODE_CLASS (mode) != MODE_INT
12871 && GET_MODE_CLASS (mode) != MODE_PARTIAL_INT)
12872 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12873 #ifdef POINTERS_EXTEND_UNSIGNED
12874 && (mode != Pmode || mem_mode == VOIDmode)
12875 #endif
12877 break;
12878 if (GET_CODE (rtl) == SYMBOL_REF
12879 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12881 dw_loc_descr_ref temp;
12883 /* If this is not defined, we have no way to emit the data. */
12884 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
12885 break;
12887 temp = new_addr_loc_descr (rtl, dtprel_true);
12889 mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
12890 add_loc_descr (&mem_loc_result, temp);
12892 break;
12895 if (!const_ok_for_output (rtl))
12897 if (GET_CODE (rtl) == CONST)
12898 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12899 initialized);
12900 break;
12903 symref:
12904 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
12905 vec_safe_push (used_rtx_array, rtl);
12906 break;
12908 case CONCAT:
12909 case CONCATN:
12910 case VAR_LOCATION:
12911 case DEBUG_IMPLICIT_PTR:
12912 expansion_failed (NULL_TREE, rtl,
12913 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
12914 return 0;
12916 case ENTRY_VALUE:
12917 if (dwarf_strict)
12918 return NULL;
12919 if (REG_P (ENTRY_VALUE_EXP (rtl)))
12921 if (GET_MODE_CLASS (mode) != MODE_INT
12922 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12923 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12924 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12925 else
12927 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
12928 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12929 return NULL;
12930 op0 = one_reg_loc_descriptor (dbx_regnum,
12931 VAR_INIT_STATUS_INITIALIZED);
12934 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
12935 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
12937 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12938 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12939 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
12940 return NULL;
12942 else
12943 gcc_unreachable ();
12944 if (op0 == NULL)
12945 return NULL;
12946 mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
12947 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
12948 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
12949 break;
12951 case DEBUG_PARAMETER_REF:
12952 mem_loc_result = parameter_ref_descriptor (rtl);
12953 break;
12955 case PRE_MODIFY:
12956 /* Extract the PLUS expression nested inside and fall into
12957 PLUS code below. */
12958 rtl = XEXP (rtl, 1);
12959 goto plus;
12961 case PRE_INC:
12962 case PRE_DEC:
12963 /* Turn these into a PLUS expression and fall into the PLUS code
12964 below. */
12965 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
12966 gen_int_mode (GET_CODE (rtl) == PRE_INC
12967 ? GET_MODE_UNIT_SIZE (mem_mode)
12968 : -GET_MODE_UNIT_SIZE (mem_mode),
12969 mode));
12971 /* ... fall through ... */
12973 case PLUS:
12974 plus:
12975 if (is_based_loc (rtl)
12976 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12977 || XEXP (rtl, 0) == arg_pointer_rtx
12978 || XEXP (rtl, 0) == frame_pointer_rtx)
12979 && GET_MODE_CLASS (mode) == MODE_INT)
12980 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
12981 INTVAL (XEXP (rtl, 1)),
12982 VAR_INIT_STATUS_INITIALIZED);
12983 else
12985 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12986 VAR_INIT_STATUS_INITIALIZED);
12987 if (mem_loc_result == 0)
12988 break;
12990 if (CONST_INT_P (XEXP (rtl, 1))
12991 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12992 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
12993 else
12995 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12996 VAR_INIT_STATUS_INITIALIZED);
12997 if (op1 == 0)
12998 return NULL;
12999 add_loc_descr (&mem_loc_result, op1);
13000 add_loc_descr (&mem_loc_result,
13001 new_loc_descr (DW_OP_plus, 0, 0));
13004 break;
13006 /* If a pseudo-reg is optimized away, it is possible for it to
13007 be replaced with a MEM containing a multiply or shift. */
13008 case MINUS:
13009 op = DW_OP_minus;
13010 goto do_binop;
13012 case MULT:
13013 op = DW_OP_mul;
13014 goto do_binop;
13016 case DIV:
13017 if (!dwarf_strict
13018 && GET_MODE_CLASS (mode) == MODE_INT
13019 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
13021 mem_loc_result = typed_binop (DW_OP_div, rtl,
13022 base_type_for_mode (mode, 0),
13023 mode, mem_mode);
13024 break;
13026 op = DW_OP_div;
13027 goto do_binop;
13029 case UMOD:
13030 op = DW_OP_mod;
13031 goto do_binop;
13033 case ASHIFT:
13034 op = DW_OP_shl;
13035 goto do_shift;
13037 case ASHIFTRT:
13038 op = DW_OP_shra;
13039 goto do_shift;
13041 case LSHIFTRT:
13042 op = DW_OP_shr;
13043 goto do_shift;
13045 do_shift:
13046 if (GET_MODE_CLASS (mode) != MODE_INT)
13047 break;
13048 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13049 VAR_INIT_STATUS_INITIALIZED);
13051 rtx rtlop1 = XEXP (rtl, 1);
13052 if (GET_MODE (rtlop1) != VOIDmode
13053 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
13054 < GET_MODE_BITSIZE (mode))
13055 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
13056 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
13057 VAR_INIT_STATUS_INITIALIZED);
13060 if (op0 == 0 || op1 == 0)
13061 break;
13063 mem_loc_result = op0;
13064 add_loc_descr (&mem_loc_result, op1);
13065 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13066 break;
13068 case AND:
13069 op = DW_OP_and;
13070 goto do_binop;
13072 case IOR:
13073 op = DW_OP_or;
13074 goto do_binop;
13076 case XOR:
13077 op = DW_OP_xor;
13078 goto do_binop;
13080 do_binop:
13081 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13082 VAR_INIT_STATUS_INITIALIZED);
13083 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13084 VAR_INIT_STATUS_INITIALIZED);
13086 if (op0 == 0 || op1 == 0)
13087 break;
13089 mem_loc_result = op0;
13090 add_loc_descr (&mem_loc_result, op1);
13091 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13092 break;
13094 case MOD:
13095 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
13097 mem_loc_result = typed_binop (DW_OP_mod, rtl,
13098 base_type_for_mode (mode, 0),
13099 mode, mem_mode);
13100 break;
13103 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13104 VAR_INIT_STATUS_INITIALIZED);
13105 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13106 VAR_INIT_STATUS_INITIALIZED);
13108 if (op0 == 0 || op1 == 0)
13109 break;
13111 mem_loc_result = op0;
13112 add_loc_descr (&mem_loc_result, op1);
13113 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13114 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13115 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
13116 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
13117 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
13118 break;
13120 case UDIV:
13121 if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
13123 if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
13125 op = DW_OP_div;
13126 goto do_binop;
13128 mem_loc_result = typed_binop (DW_OP_div, rtl,
13129 base_type_for_mode (mode, 1),
13130 mode, mem_mode);
13132 break;
13134 case NOT:
13135 op = DW_OP_not;
13136 goto do_unop;
13138 case ABS:
13139 op = DW_OP_abs;
13140 goto do_unop;
13142 case NEG:
13143 op = DW_OP_neg;
13144 goto do_unop;
13146 do_unop:
13147 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13148 VAR_INIT_STATUS_INITIALIZED);
13150 if (op0 == 0)
13151 break;
13153 mem_loc_result = op0;
13154 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13155 break;
13157 case CONST_INT:
13158 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13159 #ifdef POINTERS_EXTEND_UNSIGNED
13160 || (mode == Pmode
13161 && mem_mode != VOIDmode
13162 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
13163 #endif
13166 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13167 break;
13169 if (!dwarf_strict
13170 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
13171 || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
13173 dw_die_ref type_die = base_type_for_mode (mode, 1);
13174 machine_mode amode;
13175 if (type_die == NULL)
13176 return NULL;
13177 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
13178 MODE_INT, 0);
13179 if (INTVAL (rtl) >= 0
13180 && amode != BLKmode
13181 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
13182 /* const DW_OP_GNU_convert <XXX> vs.
13183 DW_OP_GNU_const_type <XXX, 1, const>. */
13184 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
13185 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
13187 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13188 op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13189 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13190 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13191 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
13192 add_loc_descr (&mem_loc_result, op0);
13193 return mem_loc_result;
13195 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
13196 INTVAL (rtl));
13197 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13198 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13199 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13200 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
13201 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13202 else
13204 mem_loc_result->dw_loc_oprnd2.val_class
13205 = dw_val_class_const_double;
13206 mem_loc_result->dw_loc_oprnd2.v.val_double
13207 = double_int::from_shwi (INTVAL (rtl));
13210 break;
13212 case CONST_DOUBLE:
13213 if (!dwarf_strict)
13215 dw_die_ref type_die;
13217 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
13218 CONST_DOUBLE rtx could represent either a large integer
13219 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
13220 the value is always a floating point constant.
13222 When it is an integer, a CONST_DOUBLE is used whenever
13223 the constant requires 2 HWIs to be adequately represented.
13224 We output CONST_DOUBLEs as blocks. */
13225 if (mode == VOIDmode
13226 || (GET_MODE (rtl) == VOIDmode
13227 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
13228 break;
13229 type_die = base_type_for_mode (mode,
13230 GET_MODE_CLASS (mode) == MODE_INT);
13231 if (type_die == NULL)
13232 return NULL;
13233 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
13234 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13235 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13236 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13237 #if TARGET_SUPPORTS_WIDE_INT == 0
13238 if (!SCALAR_FLOAT_MODE_P (mode))
13240 mem_loc_result->dw_loc_oprnd2.val_class
13241 = dw_val_class_const_double;
13242 mem_loc_result->dw_loc_oprnd2.v.val_double
13243 = rtx_to_double_int (rtl);
13245 else
13246 #endif
13248 unsigned int length = GET_MODE_SIZE (mode);
13249 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
13251 insert_float (rtl, array);
13252 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13253 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13254 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13255 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13258 break;
13260 case CONST_WIDE_INT:
13261 if (!dwarf_strict)
13263 dw_die_ref type_die;
13265 type_die = base_type_for_mode (mode,
13266 GET_MODE_CLASS (mode) == MODE_INT);
13267 if (type_die == NULL)
13268 return NULL;
13269 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
13270 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13271 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13272 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13273 mem_loc_result->dw_loc_oprnd2.val_class
13274 = dw_val_class_wide_int;
13275 mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
13276 *mem_loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
13278 break;
13280 case EQ:
13281 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
13282 break;
13284 case GE:
13285 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
13286 break;
13288 case GT:
13289 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
13290 break;
13292 case LE:
13293 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
13294 break;
13296 case LT:
13297 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
13298 break;
13300 case NE:
13301 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
13302 break;
13304 case GEU:
13305 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
13306 break;
13308 case GTU:
13309 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
13310 break;
13312 case LEU:
13313 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
13314 break;
13316 case LTU:
13317 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
13318 break;
13320 case UMIN:
13321 case UMAX:
13322 if (GET_MODE_CLASS (mode) != MODE_INT)
13323 break;
13324 /* FALLTHRU */
13325 case SMIN:
13326 case SMAX:
13327 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
13328 break;
13330 case ZERO_EXTRACT:
13331 case SIGN_EXTRACT:
13332 if (CONST_INT_P (XEXP (rtl, 1))
13333 && CONST_INT_P (XEXP (rtl, 2))
13334 && ((unsigned) INTVAL (XEXP (rtl, 1))
13335 + (unsigned) INTVAL (XEXP (rtl, 2))
13336 <= GET_MODE_BITSIZE (mode))
13337 && GET_MODE_CLASS (mode) == MODE_INT
13338 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13339 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
13341 int shift, size;
13342 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13343 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13344 if (op0 == 0)
13345 break;
13346 if (GET_CODE (rtl) == SIGN_EXTRACT)
13347 op = DW_OP_shra;
13348 else
13349 op = DW_OP_shr;
13350 mem_loc_result = op0;
13351 size = INTVAL (XEXP (rtl, 1));
13352 shift = INTVAL (XEXP (rtl, 2));
13353 if (BITS_BIG_ENDIAN)
13354 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
13355 - shift - size;
13356 if (shift + size != (int) DWARF2_ADDR_SIZE)
13358 add_loc_descr (&mem_loc_result,
13359 int_loc_descriptor (DWARF2_ADDR_SIZE
13360 - shift - size));
13361 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13363 if (size != (int) DWARF2_ADDR_SIZE)
13365 add_loc_descr (&mem_loc_result,
13366 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
13367 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13370 break;
13372 case IF_THEN_ELSE:
13374 dw_loc_descr_ref op2, bra_node, drop_node;
13375 op0 = mem_loc_descriptor (XEXP (rtl, 0),
13376 GET_MODE (XEXP (rtl, 0)) == VOIDmode
13377 ? word_mode : GET_MODE (XEXP (rtl, 0)),
13378 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13379 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13380 VAR_INIT_STATUS_INITIALIZED);
13381 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
13382 VAR_INIT_STATUS_INITIALIZED);
13383 if (op0 == NULL || op1 == NULL || op2 == NULL)
13384 break;
13386 mem_loc_result = op1;
13387 add_loc_descr (&mem_loc_result, op2);
13388 add_loc_descr (&mem_loc_result, op0);
13389 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13390 add_loc_descr (&mem_loc_result, bra_node);
13391 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
13392 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
13393 add_loc_descr (&mem_loc_result, drop_node);
13394 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13395 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
13397 break;
13399 case FLOAT_EXTEND:
13400 case FLOAT_TRUNCATE:
13401 case FLOAT:
13402 case UNSIGNED_FLOAT:
13403 case FIX:
13404 case UNSIGNED_FIX:
13405 if (!dwarf_strict)
13407 dw_die_ref type_die;
13408 dw_loc_descr_ref cvt;
13410 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13411 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13412 if (op0 == NULL)
13413 break;
13414 if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
13415 && (GET_CODE (rtl) == FLOAT
13416 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
13417 <= DWARF2_ADDR_SIZE))
13419 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
13420 GET_CODE (rtl) == UNSIGNED_FLOAT);
13421 if (type_die == NULL)
13422 break;
13423 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13424 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13425 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13426 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13427 add_loc_descr (&op0, cvt);
13429 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
13430 if (type_die == NULL)
13431 break;
13432 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13433 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13434 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13435 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13436 add_loc_descr (&op0, cvt);
13437 if (GET_MODE_CLASS (mode) == MODE_INT
13438 && (GET_CODE (rtl) == FIX
13439 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
13441 op0 = convert_descriptor_to_mode (mode, op0);
13442 if (op0 == NULL)
13443 break;
13445 mem_loc_result = op0;
13447 break;
13449 case CLZ:
13450 case CTZ:
13451 case FFS:
13452 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
13453 break;
13455 case POPCOUNT:
13456 case PARITY:
13457 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
13458 break;
13460 case BSWAP:
13461 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
13462 break;
13464 case ROTATE:
13465 case ROTATERT:
13466 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
13467 break;
13469 case COMPARE:
13470 /* In theory, we could implement the above. */
13471 /* DWARF cannot represent the unsigned compare operations
13472 natively. */
13473 case SS_MULT:
13474 case US_MULT:
13475 case SS_DIV:
13476 case US_DIV:
13477 case SS_PLUS:
13478 case US_PLUS:
13479 case SS_MINUS:
13480 case US_MINUS:
13481 case SS_NEG:
13482 case US_NEG:
13483 case SS_ABS:
13484 case SS_ASHIFT:
13485 case US_ASHIFT:
13486 case SS_TRUNCATE:
13487 case US_TRUNCATE:
13488 case UNORDERED:
13489 case ORDERED:
13490 case UNEQ:
13491 case UNGE:
13492 case UNGT:
13493 case UNLE:
13494 case UNLT:
13495 case LTGT:
13496 case FRACT_CONVERT:
13497 case UNSIGNED_FRACT_CONVERT:
13498 case SAT_FRACT:
13499 case UNSIGNED_SAT_FRACT:
13500 case SQRT:
13501 case ASM_OPERANDS:
13502 case VEC_MERGE:
13503 case VEC_SELECT:
13504 case VEC_CONCAT:
13505 case VEC_DUPLICATE:
13506 case UNSPEC:
13507 case HIGH:
13508 case FMA:
13509 case STRICT_LOW_PART:
13510 case CONST_VECTOR:
13511 case CONST_FIXED:
13512 case CLRSB:
13513 case CLOBBER:
13514 /* If delegitimize_address couldn't do anything with the UNSPEC, we
13515 can't express it in the debug info. This can happen e.g. with some
13516 TLS UNSPECs. */
13517 break;
13519 case CONST_STRING:
13520 resolve_one_addr (&rtl);
13521 goto symref;
13523 default:
13524 #ifdef ENABLE_CHECKING
13525 print_rtl (stderr, rtl);
13526 gcc_unreachable ();
13527 #else
13528 break;
13529 #endif
13532 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13533 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13535 return mem_loc_result;
13538 /* Return a descriptor that describes the concatenation of two locations.
13539 This is typically a complex variable. */
13541 static dw_loc_descr_ref
13542 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
13544 dw_loc_descr_ref cc_loc_result = NULL;
13545 dw_loc_descr_ref x0_ref
13546 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13547 dw_loc_descr_ref x1_ref
13548 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13550 if (x0_ref == 0 || x1_ref == 0)
13551 return 0;
13553 cc_loc_result = x0_ref;
13554 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
13556 add_loc_descr (&cc_loc_result, x1_ref);
13557 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
13559 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13560 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13562 return cc_loc_result;
13565 /* Return a descriptor that describes the concatenation of N
13566 locations. */
13568 static dw_loc_descr_ref
13569 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
13571 unsigned int i;
13572 dw_loc_descr_ref cc_loc_result = NULL;
13573 unsigned int n = XVECLEN (concatn, 0);
13575 for (i = 0; i < n; ++i)
13577 dw_loc_descr_ref ref;
13578 rtx x = XVECEXP (concatn, 0, i);
13580 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13581 if (ref == NULL)
13582 return NULL;
13584 add_loc_descr (&cc_loc_result, ref);
13585 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
13588 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13589 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13591 return cc_loc_result;
13594 /* Helper function for loc_descriptor. Return DW_OP_GNU_implicit_pointer
13595 for DEBUG_IMPLICIT_PTR RTL. */
13597 static dw_loc_descr_ref
13598 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
13600 dw_loc_descr_ref ret;
13601 dw_die_ref ref;
13603 if (dwarf_strict)
13604 return NULL;
13605 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
13606 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
13607 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
13608 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
13609 ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
13610 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
13611 if (ref)
13613 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13614 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
13615 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
13617 else
13619 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
13620 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
13622 return ret;
13625 /* Output a proper Dwarf location descriptor for a variable or parameter
13626 which is either allocated in a register or in a memory location. For a
13627 register, we just generate an OP_REG and the register number. For a
13628 memory location we provide a Dwarf postfix expression describing how to
13629 generate the (dynamic) address of the object onto the address stack.
13631 MODE is mode of the decl if this loc_descriptor is going to be used in
13632 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
13633 allowed, VOIDmode otherwise.
13635 If we don't know how to describe it, return 0. */
13637 static dw_loc_descr_ref
13638 loc_descriptor (rtx rtl, machine_mode mode,
13639 enum var_init_status initialized)
13641 dw_loc_descr_ref loc_result = NULL;
13643 switch (GET_CODE (rtl))
13645 case SUBREG:
13646 /* The case of a subreg may arise when we have a local (register)
13647 variable or a formal (register) parameter which doesn't quite fill
13648 up an entire register. For now, just assume that it is
13649 legitimate to make the Dwarf info refer to the whole register which
13650 contains the given subreg. */
13651 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
13652 loc_result = loc_descriptor (SUBREG_REG (rtl),
13653 GET_MODE (SUBREG_REG (rtl)), initialized);
13654 else
13655 goto do_default;
13656 break;
13658 case REG:
13659 loc_result = reg_loc_descriptor (rtl, initialized);
13660 break;
13662 case MEM:
13663 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13664 GET_MODE (rtl), initialized);
13665 if (loc_result == NULL)
13666 loc_result = tls_mem_loc_descriptor (rtl);
13667 if (loc_result == NULL)
13669 rtx new_rtl = avoid_constant_pool_reference (rtl);
13670 if (new_rtl != rtl)
13671 loc_result = loc_descriptor (new_rtl, mode, initialized);
13673 break;
13675 case CONCAT:
13676 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
13677 initialized);
13678 break;
13680 case CONCATN:
13681 loc_result = concatn_loc_descriptor (rtl, initialized);
13682 break;
13684 case VAR_LOCATION:
13685 /* Single part. */
13686 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
13688 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
13689 if (GET_CODE (loc) == EXPR_LIST)
13690 loc = XEXP (loc, 0);
13691 loc_result = loc_descriptor (loc, mode, initialized);
13692 break;
13695 rtl = XEXP (rtl, 1);
13696 /* FALLTHRU */
13698 case PARALLEL:
13700 rtvec par_elems = XVEC (rtl, 0);
13701 int num_elem = GET_NUM_ELEM (par_elems);
13702 machine_mode mode;
13703 int i;
13705 /* Create the first one, so we have something to add to. */
13706 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
13707 VOIDmode, initialized);
13708 if (loc_result == NULL)
13709 return NULL;
13710 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
13711 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13712 for (i = 1; i < num_elem; i++)
13714 dw_loc_descr_ref temp;
13716 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
13717 VOIDmode, initialized);
13718 if (temp == NULL)
13719 return NULL;
13720 add_loc_descr (&loc_result, temp);
13721 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
13722 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13725 break;
13727 case CONST_INT:
13728 if (mode != VOIDmode && mode != BLKmode)
13729 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
13730 INTVAL (rtl));
13731 break;
13733 case CONST_DOUBLE:
13734 if (mode == VOIDmode)
13735 mode = GET_MODE (rtl);
13737 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13739 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13741 /* Note that a CONST_DOUBLE rtx could represent either an integer
13742 or a floating-point constant. A CONST_DOUBLE is used whenever
13743 the constant requires more than one word in order to be
13744 adequately represented. We output CONST_DOUBLEs as blocks. */
13745 loc_result = new_loc_descr (DW_OP_implicit_value,
13746 GET_MODE_SIZE (mode), 0);
13747 #if TARGET_SUPPORTS_WIDE_INT == 0
13748 if (!SCALAR_FLOAT_MODE_P (mode))
13750 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
13751 loc_result->dw_loc_oprnd2.v.val_double
13752 = rtx_to_double_int (rtl);
13754 else
13755 #endif
13757 unsigned int length = GET_MODE_SIZE (mode);
13758 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
13760 insert_float (rtl, array);
13761 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13762 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13763 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13764 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13767 break;
13769 case CONST_WIDE_INT:
13770 if (mode == VOIDmode)
13771 mode = GET_MODE (rtl);
13773 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13775 loc_result = new_loc_descr (DW_OP_implicit_value,
13776 GET_MODE_SIZE (mode), 0);
13777 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
13778 loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
13779 *loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
13781 break;
13783 case CONST_VECTOR:
13784 if (mode == VOIDmode)
13785 mode = GET_MODE (rtl);
13787 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13789 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
13790 unsigned int length = CONST_VECTOR_NUNITS (rtl);
13791 unsigned char *array
13792 = ggc_vec_alloc<unsigned char> (length * elt_size);
13793 unsigned int i;
13794 unsigned char *p;
13795 machine_mode imode = GET_MODE_INNER (mode);
13797 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13798 switch (GET_MODE_CLASS (mode))
13800 case MODE_VECTOR_INT:
13801 for (i = 0, p = array; i < length; i++, p += elt_size)
13803 rtx elt = CONST_VECTOR_ELT (rtl, i);
13804 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
13806 break;
13808 case MODE_VECTOR_FLOAT:
13809 for (i = 0, p = array; i < length; i++, p += elt_size)
13811 rtx elt = CONST_VECTOR_ELT (rtl, i);
13812 insert_float (elt, p);
13814 break;
13816 default:
13817 gcc_unreachable ();
13820 loc_result = new_loc_descr (DW_OP_implicit_value,
13821 length * elt_size, 0);
13822 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13823 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
13824 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
13825 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13827 break;
13829 case CONST:
13830 if (mode == VOIDmode
13831 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
13832 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
13833 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
13835 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
13836 break;
13838 /* FALLTHROUGH */
13839 case SYMBOL_REF:
13840 if (!const_ok_for_output (rtl))
13841 break;
13842 case LABEL_REF:
13843 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
13844 && (dwarf_version >= 4 || !dwarf_strict))
13846 loc_result = new_addr_loc_descr (rtl, dtprel_false);
13847 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
13848 vec_safe_push (used_rtx_array, rtl);
13850 break;
13852 case DEBUG_IMPLICIT_PTR:
13853 loc_result = implicit_ptr_descriptor (rtl, 0);
13854 break;
13856 case PLUS:
13857 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
13858 && CONST_INT_P (XEXP (rtl, 1)))
13860 loc_result
13861 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
13862 break;
13864 /* FALLTHRU */
13865 do_default:
13866 default:
13867 if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
13868 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
13869 && dwarf_version >= 4)
13870 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
13872 /* Value expression. */
13873 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
13874 if (loc_result)
13875 add_loc_descr (&loc_result,
13876 new_loc_descr (DW_OP_stack_value, 0, 0));
13878 break;
13881 return loc_result;
13884 /* We need to figure out what section we should use as the base for the
13885 address ranges where a given location is valid.
13886 1. If this particular DECL has a section associated with it, use that.
13887 2. If this function has a section associated with it, use that.
13888 3. Otherwise, use the text section.
13889 XXX: If you split a variable across multiple sections, we won't notice. */
13891 static const char *
13892 secname_for_decl (const_tree decl)
13894 const char *secname;
13896 if (VAR_OR_FUNCTION_DECL_P (decl)
13897 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
13898 && DECL_SECTION_NAME (decl))
13899 secname = DECL_SECTION_NAME (decl);
13900 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
13901 secname = DECL_SECTION_NAME (current_function_decl);
13902 else if (cfun && in_cold_section_p)
13903 secname = crtl->subsections.cold_section_label;
13904 else
13905 secname = text_section_label;
13907 return secname;
13910 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
13912 static bool
13913 decl_by_reference_p (tree decl)
13915 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
13916 || TREE_CODE (decl) == VAR_DECL)
13917 && DECL_BY_REFERENCE (decl));
13920 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13921 for VARLOC. */
13923 static dw_loc_descr_ref
13924 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
13925 enum var_init_status initialized)
13927 int have_address = 0;
13928 dw_loc_descr_ref descr;
13929 machine_mode mode;
13931 if (want_address != 2)
13933 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
13934 /* Single part. */
13935 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13937 varloc = PAT_VAR_LOCATION_LOC (varloc);
13938 if (GET_CODE (varloc) == EXPR_LIST)
13939 varloc = XEXP (varloc, 0);
13940 mode = GET_MODE (varloc);
13941 if (MEM_P (varloc))
13943 rtx addr = XEXP (varloc, 0);
13944 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
13945 mode, initialized);
13946 if (descr)
13947 have_address = 1;
13948 else
13950 rtx x = avoid_constant_pool_reference (varloc);
13951 if (x != varloc)
13952 descr = mem_loc_descriptor (x, mode, VOIDmode,
13953 initialized);
13956 else
13957 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
13959 else
13960 return 0;
13962 else
13964 if (GET_CODE (varloc) == VAR_LOCATION)
13965 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
13966 else
13967 mode = DECL_MODE (loc);
13968 descr = loc_descriptor (varloc, mode, initialized);
13969 have_address = 1;
13972 if (!descr)
13973 return 0;
13975 if (want_address == 2 && !have_address
13976 && (dwarf_version >= 4 || !dwarf_strict))
13978 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
13980 expansion_failed (loc, NULL_RTX,
13981 "DWARF address size mismatch");
13982 return 0;
13984 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
13985 have_address = 1;
13987 /* Show if we can't fill the request for an address. */
13988 if (want_address && !have_address)
13990 expansion_failed (loc, NULL_RTX,
13991 "Want address and only have value");
13992 return 0;
13995 /* If we've got an address and don't want one, dereference. */
13996 if (!want_address && have_address)
13998 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
13999 enum dwarf_location_atom op;
14001 if (size > DWARF2_ADDR_SIZE || size == -1)
14003 expansion_failed (loc, NULL_RTX,
14004 "DWARF address size mismatch");
14005 return 0;
14007 else if (size == DWARF2_ADDR_SIZE)
14008 op = DW_OP_deref;
14009 else
14010 op = DW_OP_deref_size;
14012 add_loc_descr (&descr, new_loc_descr (op, size, 0));
14015 return descr;
14018 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
14019 if it is not possible. */
14021 static dw_loc_descr_ref
14022 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
14024 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
14025 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
14026 else if (dwarf_version >= 3 || !dwarf_strict)
14027 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
14028 else
14029 return NULL;
14032 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
14033 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
14035 static dw_loc_descr_ref
14036 dw_sra_loc_expr (tree decl, rtx loc)
14038 rtx p;
14039 unsigned HOST_WIDE_INT padsize = 0;
14040 dw_loc_descr_ref descr, *descr_tail;
14041 unsigned HOST_WIDE_INT decl_size;
14042 rtx varloc;
14043 enum var_init_status initialized;
14045 if (DECL_SIZE (decl) == NULL
14046 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
14047 return NULL;
14049 decl_size = tree_to_uhwi (DECL_SIZE (decl));
14050 descr = NULL;
14051 descr_tail = &descr;
14053 for (p = loc; p; p = XEXP (p, 1))
14055 unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
14056 rtx loc_note = *decl_piece_varloc_ptr (p);
14057 dw_loc_descr_ref cur_descr;
14058 dw_loc_descr_ref *tail, last = NULL;
14059 unsigned HOST_WIDE_INT opsize = 0;
14061 if (loc_note == NULL_RTX
14062 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
14064 padsize += bitsize;
14065 continue;
14067 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
14068 varloc = NOTE_VAR_LOCATION (loc_note);
14069 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
14070 if (cur_descr == NULL)
14072 padsize += bitsize;
14073 continue;
14076 /* Check that cur_descr either doesn't use
14077 DW_OP_*piece operations, or their sum is equal
14078 to bitsize. Otherwise we can't embed it. */
14079 for (tail = &cur_descr; *tail != NULL;
14080 tail = &(*tail)->dw_loc_next)
14081 if ((*tail)->dw_loc_opc == DW_OP_piece)
14083 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
14084 * BITS_PER_UNIT;
14085 last = *tail;
14087 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
14089 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
14090 last = *tail;
14093 if (last != NULL && opsize != bitsize)
14095 padsize += bitsize;
14096 /* Discard the current piece of the descriptor and release any
14097 addr_table entries it uses. */
14098 remove_loc_list_addr_table_entries (cur_descr);
14099 continue;
14102 /* If there is a hole, add DW_OP_*piece after empty DWARF
14103 expression, which means that those bits are optimized out. */
14104 if (padsize)
14106 if (padsize > decl_size)
14108 remove_loc_list_addr_table_entries (cur_descr);
14109 goto discard_descr;
14111 decl_size -= padsize;
14112 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
14113 if (*descr_tail == NULL)
14115 remove_loc_list_addr_table_entries (cur_descr);
14116 goto discard_descr;
14118 descr_tail = &(*descr_tail)->dw_loc_next;
14119 padsize = 0;
14121 *descr_tail = cur_descr;
14122 descr_tail = tail;
14123 if (bitsize > decl_size)
14124 goto discard_descr;
14125 decl_size -= bitsize;
14126 if (last == NULL)
14128 HOST_WIDE_INT offset = 0;
14129 if (GET_CODE (varloc) == VAR_LOCATION
14130 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14132 varloc = PAT_VAR_LOCATION_LOC (varloc);
14133 if (GET_CODE (varloc) == EXPR_LIST)
14134 varloc = XEXP (varloc, 0);
14138 if (GET_CODE (varloc) == CONST
14139 || GET_CODE (varloc) == SIGN_EXTEND
14140 || GET_CODE (varloc) == ZERO_EXTEND)
14141 varloc = XEXP (varloc, 0);
14142 else if (GET_CODE (varloc) == SUBREG)
14143 varloc = SUBREG_REG (varloc);
14144 else
14145 break;
14147 while (1);
14148 /* DW_OP_bit_size offset should be zero for register
14149 or implicit location descriptions and empty location
14150 descriptions, but for memory addresses needs big endian
14151 adjustment. */
14152 if (MEM_P (varloc))
14154 unsigned HOST_WIDE_INT memsize
14155 = MEM_SIZE (varloc) * BITS_PER_UNIT;
14156 if (memsize != bitsize)
14158 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
14159 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
14160 goto discard_descr;
14161 if (memsize < bitsize)
14162 goto discard_descr;
14163 if (BITS_BIG_ENDIAN)
14164 offset = memsize - bitsize;
14168 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
14169 if (*descr_tail == NULL)
14170 goto discard_descr;
14171 descr_tail = &(*descr_tail)->dw_loc_next;
14175 /* If there were any non-empty expressions, add padding till the end of
14176 the decl. */
14177 if (descr != NULL && decl_size != 0)
14179 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
14180 if (*descr_tail == NULL)
14181 goto discard_descr;
14183 return descr;
14185 discard_descr:
14186 /* Discard the descriptor and release any addr_table entries it uses. */
14187 remove_loc_list_addr_table_entries (descr);
14188 return NULL;
14191 /* Return the dwarf representation of the location list LOC_LIST of
14192 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
14193 function. */
14195 static dw_loc_list_ref
14196 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
14198 const char *endname, *secname;
14199 rtx varloc;
14200 enum var_init_status initialized;
14201 struct var_loc_node *node;
14202 dw_loc_descr_ref descr;
14203 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
14204 dw_loc_list_ref list = NULL;
14205 dw_loc_list_ref *listp = &list;
14207 /* Now that we know what section we are using for a base,
14208 actually construct the list of locations.
14209 The first location information is what is passed to the
14210 function that creates the location list, and the remaining
14211 locations just get added on to that list.
14212 Note that we only know the start address for a location
14213 (IE location changes), so to build the range, we use
14214 the range [current location start, next location start].
14215 This means we have to special case the last node, and generate
14216 a range of [last location start, end of function label]. */
14218 secname = secname_for_decl (decl);
14220 for (node = loc_list->first; node; node = node->next)
14221 if (GET_CODE (node->loc) == EXPR_LIST
14222 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
14224 if (GET_CODE (node->loc) == EXPR_LIST)
14226 /* This requires DW_OP_{,bit_}piece, which is not usable
14227 inside DWARF expressions. */
14228 if (want_address != 2)
14229 continue;
14230 descr = dw_sra_loc_expr (decl, node->loc);
14231 if (descr == NULL)
14232 continue;
14234 else
14236 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14237 varloc = NOTE_VAR_LOCATION (node->loc);
14238 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
14240 if (descr)
14242 bool range_across_switch = false;
14243 /* If section switch happens in between node->label
14244 and node->next->label (or end of function) and
14245 we can't emit it as a single entry list,
14246 emit two ranges, first one ending at the end
14247 of first partition and second one starting at the
14248 beginning of second partition. */
14249 if (node == loc_list->last_before_switch
14250 && (node != loc_list->first || loc_list->first->next)
14251 && current_function_decl)
14253 endname = cfun->fde->dw_fde_end;
14254 range_across_switch = true;
14256 /* The variable has a location between NODE->LABEL and
14257 NODE->NEXT->LABEL. */
14258 else if (node->next)
14259 endname = node->next->label;
14260 /* If the variable has a location at the last label
14261 it keeps its location until the end of function. */
14262 else if (!current_function_decl)
14263 endname = text_end_label;
14264 else
14266 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
14267 current_function_funcdef_no);
14268 endname = ggc_strdup (label_id);
14271 *listp = new_loc_list (descr, node->label, endname, secname);
14272 if (TREE_CODE (decl) == PARM_DECL
14273 && node == loc_list->first
14274 && NOTE_P (node->loc)
14275 && strcmp (node->label, endname) == 0)
14276 (*listp)->force = true;
14277 listp = &(*listp)->dw_loc_next;
14279 if (range_across_switch)
14281 if (GET_CODE (node->loc) == EXPR_LIST)
14282 descr = dw_sra_loc_expr (decl, node->loc);
14283 else
14285 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14286 varloc = NOTE_VAR_LOCATION (node->loc);
14287 descr = dw_loc_list_1 (decl, varloc, want_address,
14288 initialized);
14290 gcc_assert (descr);
14291 /* The variable has a location between NODE->LABEL and
14292 NODE->NEXT->LABEL. */
14293 if (node->next)
14294 endname = node->next->label;
14295 else
14296 endname = cfun->fde->dw_fde_second_end;
14297 *listp = new_loc_list (descr,
14298 cfun->fde->dw_fde_second_begin,
14299 endname, secname);
14300 listp = &(*listp)->dw_loc_next;
14305 /* Try to avoid the overhead of a location list emitting a location
14306 expression instead, but only if we didn't have more than one
14307 location entry in the first place. If some entries were not
14308 representable, we don't want to pretend a single entry that was
14309 applies to the entire scope in which the variable is
14310 available. */
14311 if (list && loc_list->first->next)
14312 gen_llsym (list);
14314 return list;
14317 /* Return if the loc_list has only single element and thus can be represented
14318 as location description. */
14320 static bool
14321 single_element_loc_list_p (dw_loc_list_ref list)
14323 gcc_assert (!list->dw_loc_next || list->ll_symbol);
14324 return !list->ll_symbol;
14327 /* To each location in list LIST add loc descr REF. */
14329 static void
14330 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
14332 dw_loc_descr_ref copy;
14333 add_loc_descr (&list->expr, ref);
14334 list = list->dw_loc_next;
14335 while (list)
14337 copy = ggc_alloc<dw_loc_descr_node> ();
14338 memcpy (copy, ref, sizeof (dw_loc_descr_node));
14339 add_loc_descr (&list->expr, copy);
14340 while (copy->dw_loc_next)
14342 dw_loc_descr_ref new_copy = ggc_alloc<dw_loc_descr_node> ();
14343 memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
14344 copy->dw_loc_next = new_copy;
14345 copy = new_copy;
14347 list = list->dw_loc_next;
14351 /* Given two lists RET and LIST
14352 produce location list that is result of adding expression in LIST
14353 to expression in RET on each position in program.
14354 Might be destructive on both RET and LIST.
14356 TODO: We handle only simple cases of RET or LIST having at most one
14357 element. General case would inolve sorting the lists in program order
14358 and merging them that will need some additional work.
14359 Adding that will improve quality of debug info especially for SRA-ed
14360 structures. */
14362 static void
14363 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
14365 if (!list)
14366 return;
14367 if (!*ret)
14369 *ret = list;
14370 return;
14372 if (!list->dw_loc_next)
14374 add_loc_descr_to_each (*ret, list->expr);
14375 return;
14377 if (!(*ret)->dw_loc_next)
14379 add_loc_descr_to_each (list, (*ret)->expr);
14380 *ret = list;
14381 return;
14383 expansion_failed (NULL_TREE, NULL_RTX,
14384 "Don't know how to merge two non-trivial"
14385 " location lists.\n");
14386 *ret = NULL;
14387 return;
14390 /* LOC is constant expression. Try a luck, look it up in constant
14391 pool and return its loc_descr of its address. */
14393 static dw_loc_descr_ref
14394 cst_pool_loc_descr (tree loc)
14396 /* Get an RTL for this, if something has been emitted. */
14397 rtx rtl = lookup_constant_def (loc);
14399 if (!rtl || !MEM_P (rtl))
14401 gcc_assert (!rtl);
14402 return 0;
14404 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
14406 /* TODO: We might get more coverage if we was actually delaying expansion
14407 of all expressions till end of compilation when constant pools are fully
14408 populated. */
14409 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
14411 expansion_failed (loc, NULL_RTX,
14412 "CST value in contant pool but not marked.");
14413 return 0;
14415 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
14416 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
14419 /* Return dw_loc_list representing address of addr_expr LOC
14420 by looking for inner INDIRECT_REF expression and turning
14421 it into simple arithmetics.
14423 See loc_list_from_tree for the meaning of CONTEXT. */
14425 static dw_loc_list_ref
14426 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
14427 const loc_descr_context *context)
14429 tree obj, offset;
14430 HOST_WIDE_INT bitsize, bitpos, bytepos;
14431 machine_mode mode;
14432 int unsignedp, volatilep = 0;
14433 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14435 obj = get_inner_reference (TREE_OPERAND (loc, 0),
14436 &bitsize, &bitpos, &offset, &mode,
14437 &unsignedp, &volatilep, false);
14438 STRIP_NOPS (obj);
14439 if (bitpos % BITS_PER_UNIT)
14441 expansion_failed (loc, NULL_RTX, "bitfield access");
14442 return 0;
14444 if (!INDIRECT_REF_P (obj))
14446 expansion_failed (obj,
14447 NULL_RTX, "no indirect ref in inner refrence");
14448 return 0;
14450 if (!offset && !bitpos)
14451 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
14452 context);
14453 else if (toplev
14454 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
14455 && (dwarf_version >= 4 || !dwarf_strict))
14457 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
14458 if (!list_ret)
14459 return 0;
14460 if (offset)
14462 /* Variable offset. */
14463 list_ret1 = loc_list_from_tree (offset, 0, context);
14464 if (list_ret1 == 0)
14465 return 0;
14466 add_loc_list (&list_ret, list_ret1);
14467 if (!list_ret)
14468 return 0;
14469 add_loc_descr_to_each (list_ret,
14470 new_loc_descr (DW_OP_plus, 0, 0));
14472 bytepos = bitpos / BITS_PER_UNIT;
14473 if (bytepos > 0)
14474 add_loc_descr_to_each (list_ret,
14475 new_loc_descr (DW_OP_plus_uconst,
14476 bytepos, 0));
14477 else if (bytepos < 0)
14478 loc_list_plus_const (list_ret, bytepos);
14479 add_loc_descr_to_each (list_ret,
14480 new_loc_descr (DW_OP_stack_value, 0, 0));
14482 return list_ret;
14486 /* Helper structure for location descriptions generation. */
14487 struct loc_descr_context
14489 /* The type that is implicitly referenced by DW_OP_push_object_address, or
14490 NULL_TREE if DW_OP_push_object_address in invalid for this location
14491 description. This is used when processing PLACEHOLDER_EXPR nodes. */
14492 tree context_type;
14493 /* The ..._DECL node that should be translated as a
14494 DW_OP_push_object_address operation. */
14495 tree base_decl;
14498 /* Generate Dwarf location list representing LOC.
14499 If WANT_ADDRESS is false, expression computing LOC will be computed
14500 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
14501 if WANT_ADDRESS is 2, expression computing address useable in location
14502 will be returned (i.e. DW_OP_reg can be used
14503 to refer to register values).
14505 CONTEXT provides information to customize the location descriptions
14506 generation. Its context_type field specifies what type is implicitly
14507 referenced by DW_OP_push_object_address. If it is NULL_TREE, this operation
14508 will not be generated.
14510 If CONTEXT is NULL, the behavior is the same as if both context_type and
14511 base_decl fields were NULL_TREE. */
14513 static dw_loc_list_ref
14514 loc_list_from_tree (tree loc, int want_address,
14515 const struct loc_descr_context *context)
14517 dw_loc_descr_ref ret = NULL, ret1 = NULL;
14518 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14519 int have_address = 0;
14520 enum dwarf_location_atom op;
14522 /* ??? Most of the time we do not take proper care for sign/zero
14523 extending the values properly. Hopefully this won't be a real
14524 problem... */
14526 if (context != NULL
14527 && context->base_decl == loc
14528 && want_address == 0)
14530 if (dwarf_version >= 3 || !dwarf_strict)
14531 return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
14532 NULL, NULL, NULL);
14533 else
14534 return NULL;
14537 switch (TREE_CODE (loc))
14539 case ERROR_MARK:
14540 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
14541 return 0;
14543 case PLACEHOLDER_EXPR:
14544 /* This case involves extracting fields from an object to determine the
14545 position of other fields. It is supposed to appear only as the first
14546 operand of COMPONENT_REF nodes and to reference precisely the type
14547 that the context allows. */
14548 if (context != NULL
14549 && TREE_TYPE (loc) == context->context_type
14550 && want_address >= 1)
14552 if (dwarf_version >= 3 || !dwarf_strict)
14554 ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
14555 have_address = 1;
14556 break;
14558 else
14559 return NULL;
14561 else
14562 expansion_failed (loc, NULL_RTX,
14563 "PLACEHOLDER_EXPR for an unexpected type");
14564 break;
14566 case CALL_EXPR:
14567 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
14568 /* There are no opcodes for these operations. */
14569 return 0;
14571 case PREINCREMENT_EXPR:
14572 case PREDECREMENT_EXPR:
14573 case POSTINCREMENT_EXPR:
14574 case POSTDECREMENT_EXPR:
14575 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
14576 /* There are no opcodes for these operations. */
14577 return 0;
14579 case ADDR_EXPR:
14580 /* If we already want an address, see if there is INDIRECT_REF inside
14581 e.g. for &this->field. */
14582 if (want_address)
14584 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
14585 (loc, want_address == 2, context);
14586 if (list_ret)
14587 have_address = 1;
14588 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
14589 && (ret = cst_pool_loc_descr (loc)))
14590 have_address = 1;
14592 /* Otherwise, process the argument and look for the address. */
14593 if (!list_ret && !ret)
14594 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1, context);
14595 else
14597 if (want_address)
14598 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
14599 return NULL;
14601 break;
14603 case VAR_DECL:
14604 if (DECL_THREAD_LOCAL_P (loc))
14606 rtx rtl;
14607 enum dwarf_location_atom tls_op;
14608 enum dtprel_bool dtprel = dtprel_false;
14610 if (targetm.have_tls)
14612 /* If this is not defined, we have no way to emit the
14613 data. */
14614 if (!targetm.asm_out.output_dwarf_dtprel)
14615 return 0;
14617 /* The way DW_OP_GNU_push_tls_address is specified, we
14618 can only look up addresses of objects in the current
14619 module. We used DW_OP_addr as first op, but that's
14620 wrong, because DW_OP_addr is relocated by the debug
14621 info consumer, while DW_OP_GNU_push_tls_address
14622 operand shouldn't be. */
14623 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
14624 return 0;
14625 dtprel = dtprel_true;
14626 tls_op = DW_OP_GNU_push_tls_address;
14628 else
14630 if (!targetm.emutls.debug_form_tls_address
14631 || !(dwarf_version >= 3 || !dwarf_strict))
14632 return 0;
14633 /* We stuffed the control variable into the DECL_VALUE_EXPR
14634 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
14635 no longer appear in gimple code. We used the control
14636 variable in specific so that we could pick it up here. */
14637 loc = DECL_VALUE_EXPR (loc);
14638 tls_op = DW_OP_form_tls_address;
14641 rtl = rtl_for_decl_location (loc);
14642 if (rtl == NULL_RTX)
14643 return 0;
14645 if (!MEM_P (rtl))
14646 return 0;
14647 rtl = XEXP (rtl, 0);
14648 if (! CONSTANT_P (rtl))
14649 return 0;
14651 ret = new_addr_loc_descr (rtl, dtprel);
14652 ret1 = new_loc_descr (tls_op, 0, 0);
14653 add_loc_descr (&ret, ret1);
14655 have_address = 1;
14656 break;
14658 /* FALLTHRU */
14660 case PARM_DECL:
14661 case RESULT_DECL:
14662 if (DECL_HAS_VALUE_EXPR_P (loc))
14663 return loc_list_from_tree (DECL_VALUE_EXPR (loc),
14664 want_address, context);
14665 /* FALLTHRU */
14667 case FUNCTION_DECL:
14669 rtx rtl;
14670 var_loc_list *loc_list = lookup_decl_loc (loc);
14672 if (loc_list && loc_list->first)
14674 list_ret = dw_loc_list (loc_list, loc, want_address);
14675 have_address = want_address != 0;
14676 break;
14678 rtl = rtl_for_decl_location (loc);
14679 if (rtl == NULL_RTX)
14681 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
14682 return 0;
14684 else if (CONST_INT_P (rtl))
14686 HOST_WIDE_INT val = INTVAL (rtl);
14687 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14688 val &= GET_MODE_MASK (DECL_MODE (loc));
14689 ret = int_loc_descriptor (val);
14691 else if (GET_CODE (rtl) == CONST_STRING)
14693 expansion_failed (loc, NULL_RTX, "CONST_STRING");
14694 return 0;
14696 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
14697 ret = new_addr_loc_descr (rtl, dtprel_false);
14698 else
14700 machine_mode mode, mem_mode;
14702 /* Certain constructs can only be represented at top-level. */
14703 if (want_address == 2)
14705 ret = loc_descriptor (rtl, VOIDmode,
14706 VAR_INIT_STATUS_INITIALIZED);
14707 have_address = 1;
14709 else
14711 mode = GET_MODE (rtl);
14712 mem_mode = VOIDmode;
14713 if (MEM_P (rtl))
14715 mem_mode = mode;
14716 mode = get_address_mode (rtl);
14717 rtl = XEXP (rtl, 0);
14718 have_address = 1;
14720 ret = mem_loc_descriptor (rtl, mode, mem_mode,
14721 VAR_INIT_STATUS_INITIALIZED);
14723 if (!ret)
14724 expansion_failed (loc, rtl,
14725 "failed to produce loc descriptor for rtl");
14728 break;
14730 case MEM_REF:
14731 if (!integer_zerop (TREE_OPERAND (loc, 1)))
14733 have_address = 1;
14734 goto do_plus;
14736 /* Fallthru. */
14737 case INDIRECT_REF:
14738 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14739 have_address = 1;
14740 break;
14742 case TARGET_MEM_REF:
14743 case SSA_NAME:
14744 case DEBUG_EXPR_DECL:
14745 return NULL;
14747 case COMPOUND_EXPR:
14748 return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address, context);
14750 CASE_CONVERT:
14751 case VIEW_CONVERT_EXPR:
14752 case SAVE_EXPR:
14753 case MODIFY_EXPR:
14754 return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address, context);
14756 case COMPONENT_REF:
14757 case BIT_FIELD_REF:
14758 case ARRAY_REF:
14759 case ARRAY_RANGE_REF:
14760 case REALPART_EXPR:
14761 case IMAGPART_EXPR:
14763 tree obj, offset;
14764 HOST_WIDE_INT bitsize, bitpos, bytepos;
14765 machine_mode mode;
14766 int unsignedp, volatilep = 0;
14768 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
14769 &unsignedp, &volatilep, false);
14771 gcc_assert (obj != loc);
14773 list_ret = loc_list_from_tree (obj,
14774 want_address == 2
14775 && !bitpos && !offset ? 2 : 1,
14776 context);
14777 /* TODO: We can extract value of the small expression via shifting even
14778 for nonzero bitpos. */
14779 if (list_ret == 0)
14780 return 0;
14781 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
14783 expansion_failed (loc, NULL_RTX,
14784 "bitfield access");
14785 return 0;
14788 if (offset != NULL_TREE)
14790 /* Variable offset. */
14791 list_ret1 = loc_list_from_tree (offset, 0, context);
14792 if (list_ret1 == 0)
14793 return 0;
14794 add_loc_list (&list_ret, list_ret1);
14795 if (!list_ret)
14796 return 0;
14797 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
14800 bytepos = bitpos / BITS_PER_UNIT;
14801 if (bytepos > 0)
14802 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
14803 else if (bytepos < 0)
14804 loc_list_plus_const (list_ret, bytepos);
14806 have_address = 1;
14807 break;
14810 case INTEGER_CST:
14811 if ((want_address || !tree_fits_shwi_p (loc))
14812 && (ret = cst_pool_loc_descr (loc)))
14813 have_address = 1;
14814 else if (want_address == 2
14815 && tree_fits_shwi_p (loc)
14816 && (ret = address_of_int_loc_descriptor
14817 (int_size_in_bytes (TREE_TYPE (loc)),
14818 tree_to_shwi (loc))))
14819 have_address = 1;
14820 else if (tree_fits_shwi_p (loc))
14821 ret = int_loc_descriptor (tree_to_shwi (loc));
14822 else
14824 expansion_failed (loc, NULL_RTX,
14825 "Integer operand is not host integer");
14826 return 0;
14828 break;
14830 case CONSTRUCTOR:
14831 case REAL_CST:
14832 case STRING_CST:
14833 case COMPLEX_CST:
14834 if ((ret = cst_pool_loc_descr (loc)))
14835 have_address = 1;
14836 else
14837 /* We can construct small constants here using int_loc_descriptor. */
14838 expansion_failed (loc, NULL_RTX,
14839 "constructor or constant not in constant pool");
14840 break;
14842 case TRUTH_AND_EXPR:
14843 case TRUTH_ANDIF_EXPR:
14844 case BIT_AND_EXPR:
14845 op = DW_OP_and;
14846 goto do_binop;
14848 case TRUTH_XOR_EXPR:
14849 case BIT_XOR_EXPR:
14850 op = DW_OP_xor;
14851 goto do_binop;
14853 case TRUTH_OR_EXPR:
14854 case TRUTH_ORIF_EXPR:
14855 case BIT_IOR_EXPR:
14856 op = DW_OP_or;
14857 goto do_binop;
14859 case FLOOR_DIV_EXPR:
14860 case CEIL_DIV_EXPR:
14861 case ROUND_DIV_EXPR:
14862 case TRUNC_DIV_EXPR:
14863 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14864 return 0;
14865 op = DW_OP_div;
14866 goto do_binop;
14868 case MINUS_EXPR:
14869 op = DW_OP_minus;
14870 goto do_binop;
14872 case FLOOR_MOD_EXPR:
14873 case CEIL_MOD_EXPR:
14874 case ROUND_MOD_EXPR:
14875 case TRUNC_MOD_EXPR:
14876 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14878 op = DW_OP_mod;
14879 goto do_binop;
14881 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14882 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
14883 if (list_ret == 0 || list_ret1 == 0)
14884 return 0;
14886 add_loc_list (&list_ret, list_ret1);
14887 if (list_ret == 0)
14888 return 0;
14889 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14890 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14891 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
14892 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
14893 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
14894 break;
14896 case MULT_EXPR:
14897 op = DW_OP_mul;
14898 goto do_binop;
14900 case LSHIFT_EXPR:
14901 op = DW_OP_shl;
14902 goto do_binop;
14904 case RSHIFT_EXPR:
14905 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
14906 goto do_binop;
14908 case POINTER_PLUS_EXPR:
14909 case PLUS_EXPR:
14910 do_plus:
14911 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
14913 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14914 if (list_ret == 0)
14915 return 0;
14917 loc_list_plus_const (list_ret, tree_to_shwi (TREE_OPERAND (loc, 1)));
14918 break;
14921 op = DW_OP_plus;
14922 goto do_binop;
14924 case LE_EXPR:
14925 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14926 return 0;
14928 op = DW_OP_le;
14929 goto do_binop;
14931 case GE_EXPR:
14932 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14933 return 0;
14935 op = DW_OP_ge;
14936 goto do_binop;
14938 case LT_EXPR:
14939 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14940 return 0;
14942 op = DW_OP_lt;
14943 goto do_binop;
14945 case GT_EXPR:
14946 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14947 return 0;
14949 op = DW_OP_gt;
14950 goto do_binop;
14952 case EQ_EXPR:
14953 op = DW_OP_eq;
14954 goto do_binop;
14956 case NE_EXPR:
14957 op = DW_OP_ne;
14958 goto do_binop;
14960 do_binop:
14961 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14962 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
14963 if (list_ret == 0 || list_ret1 == 0)
14964 return 0;
14966 add_loc_list (&list_ret, list_ret1);
14967 if (list_ret == 0)
14968 return 0;
14969 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14970 break;
14972 case TRUTH_NOT_EXPR:
14973 case BIT_NOT_EXPR:
14974 op = DW_OP_not;
14975 goto do_unop;
14977 case ABS_EXPR:
14978 op = DW_OP_abs;
14979 goto do_unop;
14981 case NEGATE_EXPR:
14982 op = DW_OP_neg;
14983 goto do_unop;
14985 do_unop:
14986 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14987 if (list_ret == 0)
14988 return 0;
14990 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14991 break;
14993 case MIN_EXPR:
14994 case MAX_EXPR:
14996 const enum tree_code code =
14997 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
14999 loc = build3 (COND_EXPR, TREE_TYPE (loc),
15000 build2 (code, integer_type_node,
15001 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
15002 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
15005 /* ... fall through ... */
15007 case COND_EXPR:
15009 dw_loc_descr_ref lhs
15010 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
15011 dw_loc_list_ref rhs
15012 = loc_list_from_tree (TREE_OPERAND (loc, 2), 0, context);
15013 dw_loc_descr_ref bra_node, jump_node, tmp;
15015 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
15016 if (list_ret == 0 || lhs == 0 || rhs == 0)
15017 return 0;
15019 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15020 add_loc_descr_to_each (list_ret, bra_node);
15022 add_loc_list (&list_ret, rhs);
15023 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
15024 add_loc_descr_to_each (list_ret, jump_node);
15026 add_loc_descr_to_each (list_ret, lhs);
15027 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15028 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
15030 /* ??? Need a node to point the skip at. Use a nop. */
15031 tmp = new_loc_descr (DW_OP_nop, 0, 0);
15032 add_loc_descr_to_each (list_ret, tmp);
15033 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15034 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
15036 break;
15038 case FIX_TRUNC_EXPR:
15039 return 0;
15041 default:
15042 /* Leave front-end specific codes as simply unknown. This comes
15043 up, for instance, with the C STMT_EXPR. */
15044 if ((unsigned int) TREE_CODE (loc)
15045 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
15047 expansion_failed (loc, NULL_RTX,
15048 "language specific tree node");
15049 return 0;
15052 #ifdef ENABLE_CHECKING
15053 /* Otherwise this is a generic code; we should just lists all of
15054 these explicitly. We forgot one. */
15055 gcc_unreachable ();
15056 #else
15057 /* In a release build, we want to degrade gracefully: better to
15058 generate incomplete debugging information than to crash. */
15059 return NULL;
15060 #endif
15063 if (!ret && !list_ret)
15064 return 0;
15066 if (want_address == 2 && !have_address
15067 && (dwarf_version >= 4 || !dwarf_strict))
15069 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
15071 expansion_failed (loc, NULL_RTX,
15072 "DWARF address size mismatch");
15073 return 0;
15075 if (ret)
15076 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
15077 else
15078 add_loc_descr_to_each (list_ret,
15079 new_loc_descr (DW_OP_stack_value, 0, 0));
15080 have_address = 1;
15082 /* Show if we can't fill the request for an address. */
15083 if (want_address && !have_address)
15085 expansion_failed (loc, NULL_RTX,
15086 "Want address and only have value");
15087 return 0;
15090 gcc_assert (!ret || !list_ret);
15092 /* If we've got an address and don't want one, dereference. */
15093 if (!want_address && have_address)
15095 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
15097 if (size > DWARF2_ADDR_SIZE || size == -1)
15099 expansion_failed (loc, NULL_RTX,
15100 "DWARF address size mismatch");
15101 return 0;
15103 else if (size == DWARF2_ADDR_SIZE)
15104 op = DW_OP_deref;
15105 else
15106 op = DW_OP_deref_size;
15108 if (ret)
15109 add_loc_descr (&ret, new_loc_descr (op, size, 0));
15110 else
15111 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
15113 if (ret)
15114 list_ret = new_loc_list (ret, NULL, NULL, NULL);
15116 return list_ret;
15119 /* Same as above but return only single location expression. */
15120 static dw_loc_descr_ref
15121 loc_descriptor_from_tree (tree loc, int want_address,
15122 const struct loc_descr_context *context)
15124 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
15125 if (!ret)
15126 return NULL;
15127 if (ret->dw_loc_next)
15129 expansion_failed (loc, NULL_RTX,
15130 "Location list where only loc descriptor needed");
15131 return NULL;
15133 return ret->expr;
15136 /* Given a value, round it up to the lowest multiple of `boundary'
15137 which is not less than the value itself. */
15139 static inline HOST_WIDE_INT
15140 ceiling (HOST_WIDE_INT value, unsigned int boundary)
15142 return (((value + boundary - 1) / boundary) * boundary);
15145 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
15146 pointer to the declared type for the relevant field variable, or return
15147 `integer_type_node' if the given node turns out to be an
15148 ERROR_MARK node. */
15150 static inline tree
15151 field_type (const_tree decl)
15153 tree type;
15155 if (TREE_CODE (decl) == ERROR_MARK)
15156 return integer_type_node;
15158 type = DECL_BIT_FIELD_TYPE (decl);
15159 if (type == NULL_TREE)
15160 type = TREE_TYPE (decl);
15162 return type;
15165 /* Given a pointer to a tree node, return the alignment in bits for
15166 it, or else return BITS_PER_WORD if the node actually turns out to
15167 be an ERROR_MARK node. */
15169 static inline unsigned
15170 simple_type_align_in_bits (const_tree type)
15172 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
15175 static inline unsigned
15176 simple_decl_align_in_bits (const_tree decl)
15178 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
15181 /* Return the result of rounding T up to ALIGN. */
15183 static inline offset_int
15184 round_up_to_align (const offset_int &t, unsigned int align)
15186 return wi::udiv_trunc (t + align - 1, align) * align;
15189 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
15190 lowest addressed byte of the "containing object" for the given FIELD_DECL,
15191 or return 0 if we are unable to determine what that offset is, either
15192 because the argument turns out to be a pointer to an ERROR_MARK node, or
15193 because the offset is actually variable. (We can't handle the latter case
15194 just yet). */
15196 static HOST_WIDE_INT
15197 field_byte_offset (const_tree decl)
15199 offset_int object_offset_in_bits;
15200 offset_int object_offset_in_bytes;
15201 offset_int bitpos_int;
15203 if (TREE_CODE (decl) == ERROR_MARK)
15204 return 0;
15206 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
15208 /* We cannot yet cope with fields whose positions are variable, so
15209 for now, when we see such things, we simply return 0. Someday, we may
15210 be able to handle such cases, but it will be damn difficult. */
15211 if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
15212 return 0;
15214 bitpos_int = wi::to_offset (bit_position (decl));
15216 if (PCC_BITFIELD_TYPE_MATTERS)
15218 tree type;
15219 tree field_size_tree;
15220 offset_int deepest_bitpos;
15221 offset_int field_size_in_bits;
15222 unsigned int type_align_in_bits;
15223 unsigned int decl_align_in_bits;
15224 offset_int type_size_in_bits;
15226 type = field_type (decl);
15227 type_size_in_bits = offset_int_type_size_in_bits (type);
15228 type_align_in_bits = simple_type_align_in_bits (type);
15230 field_size_tree = DECL_SIZE (decl);
15232 /* The size could be unspecified if there was an error, or for
15233 a flexible array member. */
15234 if (!field_size_tree)
15235 field_size_tree = bitsize_zero_node;
15237 /* If the size of the field is not constant, use the type size. */
15238 if (TREE_CODE (field_size_tree) == INTEGER_CST)
15239 field_size_in_bits = wi::to_offset (field_size_tree);
15240 else
15241 field_size_in_bits = type_size_in_bits;
15243 decl_align_in_bits = simple_decl_align_in_bits (decl);
15245 /* The GCC front-end doesn't make any attempt to keep track of the
15246 starting bit offset (relative to the start of the containing
15247 structure type) of the hypothetical "containing object" for a
15248 bit-field. Thus, when computing the byte offset value for the
15249 start of the "containing object" of a bit-field, we must deduce
15250 this information on our own. This can be rather tricky to do in
15251 some cases. For example, handling the following structure type
15252 definition when compiling for an i386/i486 target (which only
15253 aligns long long's to 32-bit boundaries) can be very tricky:
15255 struct S { int field1; long long field2:31; };
15257 Fortunately, there is a simple rule-of-thumb which can be used
15258 in such cases. When compiling for an i386/i486, GCC will
15259 allocate 8 bytes for the structure shown above. It decides to
15260 do this based upon one simple rule for bit-field allocation.
15261 GCC allocates each "containing object" for each bit-field at
15262 the first (i.e. lowest addressed) legitimate alignment boundary
15263 (based upon the required minimum alignment for the declared
15264 type of the field) which it can possibly use, subject to the
15265 condition that there is still enough available space remaining
15266 in the containing object (when allocated at the selected point)
15267 to fully accommodate all of the bits of the bit-field itself.
15269 This simple rule makes it obvious why GCC allocates 8 bytes for
15270 each object of the structure type shown above. When looking
15271 for a place to allocate the "containing object" for `field2',
15272 the compiler simply tries to allocate a 64-bit "containing
15273 object" at each successive 32-bit boundary (starting at zero)
15274 until it finds a place to allocate that 64- bit field such that
15275 at least 31 contiguous (and previously unallocated) bits remain
15276 within that selected 64 bit field. (As it turns out, for the
15277 example above, the compiler finds it is OK to allocate the
15278 "containing object" 64-bit field at bit-offset zero within the
15279 structure type.)
15281 Here we attempt to work backwards from the limited set of facts
15282 we're given, and we try to deduce from those facts, where GCC
15283 must have believed that the containing object started (within
15284 the structure type). The value we deduce is then used (by the
15285 callers of this routine) to generate DW_AT_location and
15286 DW_AT_bit_offset attributes for fields (both bit-fields and, in
15287 the case of DW_AT_location, regular fields as well). */
15289 /* Figure out the bit-distance from the start of the structure to
15290 the "deepest" bit of the bit-field. */
15291 deepest_bitpos = bitpos_int + field_size_in_bits;
15293 /* This is the tricky part. Use some fancy footwork to deduce
15294 where the lowest addressed bit of the containing object must
15295 be. */
15296 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
15298 /* Round up to type_align by default. This works best for
15299 bitfields. */
15300 object_offset_in_bits
15301 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
15303 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
15305 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
15307 /* Round up to decl_align instead. */
15308 object_offset_in_bits
15309 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
15312 else
15313 object_offset_in_bits = bitpos_int;
15315 object_offset_in_bytes
15316 = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
15317 return object_offset_in_bytes.to_shwi ();
15320 /* The following routines define various Dwarf attributes and any data
15321 associated with them. */
15323 /* Add a location description attribute value to a DIE.
15325 This emits location attributes suitable for whole variables and
15326 whole parameters. Note that the location attributes for struct fields are
15327 generated by the routine `data_member_location_attribute' below. */
15329 static inline void
15330 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
15331 dw_loc_list_ref descr)
15333 if (descr == 0)
15334 return;
15335 if (single_element_loc_list_p (descr))
15336 add_AT_loc (die, attr_kind, descr->expr);
15337 else
15338 add_AT_loc_list (die, attr_kind, descr);
15341 /* Add DW_AT_accessibility attribute to DIE if needed. */
15343 static void
15344 add_accessibility_attribute (dw_die_ref die, tree decl)
15346 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
15347 children, otherwise the default is DW_ACCESS_public. In DWARF2
15348 the default has always been DW_ACCESS_public. */
15349 if (TREE_PROTECTED (decl))
15350 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
15351 else if (TREE_PRIVATE (decl))
15353 if (dwarf_version == 2
15354 || die->die_parent == NULL
15355 || die->die_parent->die_tag != DW_TAG_class_type)
15356 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
15358 else if (dwarf_version > 2
15359 && die->die_parent
15360 && die->die_parent->die_tag == DW_TAG_class_type)
15361 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
15364 /* Attach the specialized form of location attribute used for data members of
15365 struct and union types. In the special case of a FIELD_DECL node which
15366 represents a bit-field, the "offset" part of this special location
15367 descriptor must indicate the distance in bytes from the lowest-addressed
15368 byte of the containing struct or union type to the lowest-addressed byte of
15369 the "containing object" for the bit-field. (See the `field_byte_offset'
15370 function above).
15372 For any given bit-field, the "containing object" is a hypothetical object
15373 (of some integral or enum type) within which the given bit-field lives. The
15374 type of this hypothetical "containing object" is always the same as the
15375 declared type of the individual bit-field itself (for GCC anyway... the
15376 DWARF spec doesn't actually mandate this). Note that it is the size (in
15377 bytes) of the hypothetical "containing object" which will be given in the
15378 DW_AT_byte_size attribute for this bit-field. (See the
15379 `byte_size_attribute' function below.) It is also used when calculating the
15380 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
15381 function below.) */
15383 static void
15384 add_data_member_location_attribute (dw_die_ref die, tree decl)
15386 HOST_WIDE_INT offset;
15387 dw_loc_descr_ref loc_descr = 0;
15389 if (TREE_CODE (decl) == TREE_BINFO)
15391 /* We're working on the TAG_inheritance for a base class. */
15392 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
15394 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
15395 aren't at a fixed offset from all (sub)objects of the same
15396 type. We need to extract the appropriate offset from our
15397 vtable. The following dwarf expression means
15399 BaseAddr = ObAddr + *((*ObAddr) - Offset)
15401 This is specific to the V3 ABI, of course. */
15403 dw_loc_descr_ref tmp;
15405 /* Make a copy of the object address. */
15406 tmp = new_loc_descr (DW_OP_dup, 0, 0);
15407 add_loc_descr (&loc_descr, tmp);
15409 /* Extract the vtable address. */
15410 tmp = new_loc_descr (DW_OP_deref, 0, 0);
15411 add_loc_descr (&loc_descr, tmp);
15413 /* Calculate the address of the offset. */
15414 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
15415 gcc_assert (offset < 0);
15417 tmp = int_loc_descriptor (-offset);
15418 add_loc_descr (&loc_descr, tmp);
15419 tmp = new_loc_descr (DW_OP_minus, 0, 0);
15420 add_loc_descr (&loc_descr, tmp);
15422 /* Extract the offset. */
15423 tmp = new_loc_descr (DW_OP_deref, 0, 0);
15424 add_loc_descr (&loc_descr, tmp);
15426 /* Add it to the object address. */
15427 tmp = new_loc_descr (DW_OP_plus, 0, 0);
15428 add_loc_descr (&loc_descr, tmp);
15430 else
15431 offset = tree_to_shwi (BINFO_OFFSET (decl));
15433 else
15434 offset = field_byte_offset (decl);
15436 if (! loc_descr)
15438 if (dwarf_version > 2)
15440 /* Don't need to output a location expression, just the constant. */
15441 if (offset < 0)
15442 add_AT_int (die, DW_AT_data_member_location, offset);
15443 else
15444 add_AT_unsigned (die, DW_AT_data_member_location, offset);
15445 return;
15447 else
15449 enum dwarf_location_atom op;
15451 /* The DWARF2 standard says that we should assume that the structure
15452 address is already on the stack, so we can specify a structure
15453 field address by using DW_OP_plus_uconst. */
15454 op = DW_OP_plus_uconst;
15455 loc_descr = new_loc_descr (op, offset, 0);
15459 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
15462 /* Writes integer values to dw_vec_const array. */
15464 static void
15465 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
15467 while (size != 0)
15469 *dest++ = val & 0xff;
15470 val >>= 8;
15471 --size;
15475 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
15477 static HOST_WIDE_INT
15478 extract_int (const unsigned char *src, unsigned int size)
15480 HOST_WIDE_INT val = 0;
15482 src += size;
15483 while (size != 0)
15485 val <<= 8;
15486 val |= *--src & 0xff;
15487 --size;
15489 return val;
15492 /* Writes wide_int values to dw_vec_const array. */
15494 static void
15495 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
15497 int i;
15499 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
15501 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
15502 return;
15505 /* We'd have to extend this code to support odd sizes. */
15506 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
15508 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
15510 if (WORDS_BIG_ENDIAN)
15511 for (i = n - 1; i >= 0; i--)
15513 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
15514 dest += sizeof (HOST_WIDE_INT);
15516 else
15517 for (i = 0; i < n; i++)
15519 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
15520 dest += sizeof (HOST_WIDE_INT);
15524 /* Writes floating point values to dw_vec_const array. */
15526 static void
15527 insert_float (const_rtx rtl, unsigned char *array)
15529 REAL_VALUE_TYPE rv;
15530 long val[4];
15531 int i;
15533 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
15534 real_to_target (val, &rv, GET_MODE (rtl));
15536 /* real_to_target puts 32-bit pieces in each long. Pack them. */
15537 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
15539 insert_int (val[i], 4, array);
15540 array += 4;
15544 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
15545 does not have a "location" either in memory or in a register. These
15546 things can arise in GNU C when a constant is passed as an actual parameter
15547 to an inlined function. They can also arise in C++ where declared
15548 constants do not necessarily get memory "homes". */
15550 static bool
15551 add_const_value_attribute (dw_die_ref die, rtx rtl)
15553 switch (GET_CODE (rtl))
15555 case CONST_INT:
15557 HOST_WIDE_INT val = INTVAL (rtl);
15559 if (val < 0)
15560 add_AT_int (die, DW_AT_const_value, val);
15561 else
15562 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
15564 return true;
15566 case CONST_WIDE_INT:
15567 add_AT_wide (die, DW_AT_const_value,
15568 std::make_pair (rtl, GET_MODE (rtl)));
15569 return true;
15571 case CONST_DOUBLE:
15572 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
15573 floating-point constant. A CONST_DOUBLE is used whenever the
15574 constant requires more than one word in order to be adequately
15575 represented. */
15577 machine_mode mode = GET_MODE (rtl);
15579 if (TARGET_SUPPORTS_WIDE_INT == 0 && !SCALAR_FLOAT_MODE_P (mode))
15580 add_AT_double (die, DW_AT_const_value,
15581 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
15582 else
15584 unsigned int length = GET_MODE_SIZE (mode);
15585 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15587 insert_float (rtl, array);
15588 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
15591 return true;
15593 case CONST_VECTOR:
15595 machine_mode mode = GET_MODE (rtl);
15596 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
15597 unsigned int length = CONST_VECTOR_NUNITS (rtl);
15598 unsigned char *array
15599 = ggc_vec_alloc<unsigned char> (length * elt_size);
15600 unsigned int i;
15601 unsigned char *p;
15602 machine_mode imode = GET_MODE_INNER (mode);
15604 switch (GET_MODE_CLASS (mode))
15606 case MODE_VECTOR_INT:
15607 for (i = 0, p = array; i < length; i++, p += elt_size)
15609 rtx elt = CONST_VECTOR_ELT (rtl, i);
15610 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
15612 break;
15614 case MODE_VECTOR_FLOAT:
15615 for (i = 0, p = array; i < length; i++, p += elt_size)
15617 rtx elt = CONST_VECTOR_ELT (rtl, i);
15618 insert_float (elt, p);
15620 break;
15622 default:
15623 gcc_unreachable ();
15626 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
15628 return true;
15630 case CONST_STRING:
15631 if (dwarf_version >= 4 || !dwarf_strict)
15633 dw_loc_descr_ref loc_result;
15634 resolve_one_addr (&rtl);
15635 rtl_addr:
15636 loc_result = new_addr_loc_descr (rtl, dtprel_false);
15637 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
15638 add_AT_loc (die, DW_AT_location, loc_result);
15639 vec_safe_push (used_rtx_array, rtl);
15640 return true;
15642 return false;
15644 case CONST:
15645 if (CONSTANT_P (XEXP (rtl, 0)))
15646 return add_const_value_attribute (die, XEXP (rtl, 0));
15647 /* FALLTHROUGH */
15648 case SYMBOL_REF:
15649 if (!const_ok_for_output (rtl))
15650 return false;
15651 case LABEL_REF:
15652 if (dwarf_version >= 4 || !dwarf_strict)
15653 goto rtl_addr;
15654 return false;
15656 case PLUS:
15657 /* In cases where an inlined instance of an inline function is passed
15658 the address of an `auto' variable (which is local to the caller) we
15659 can get a situation where the DECL_RTL of the artificial local
15660 variable (for the inlining) which acts as a stand-in for the
15661 corresponding formal parameter (of the inline function) will look
15662 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
15663 exactly a compile-time constant expression, but it isn't the address
15664 of the (artificial) local variable either. Rather, it represents the
15665 *value* which the artificial local variable always has during its
15666 lifetime. We currently have no way to represent such quasi-constant
15667 values in Dwarf, so for now we just punt and generate nothing. */
15668 return false;
15670 case HIGH:
15671 case CONST_FIXED:
15672 return false;
15674 case MEM:
15675 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
15676 && MEM_READONLY_P (rtl)
15677 && GET_MODE (rtl) == BLKmode)
15679 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
15680 return true;
15682 return false;
15684 default:
15685 /* No other kinds of rtx should be possible here. */
15686 gcc_unreachable ();
15688 return false;
15691 /* Determine whether the evaluation of EXPR references any variables
15692 or functions which aren't otherwise used (and therefore may not be
15693 output). */
15694 static tree
15695 reference_to_unused (tree * tp, int * walk_subtrees,
15696 void * data ATTRIBUTE_UNUSED)
15698 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
15699 *walk_subtrees = 0;
15701 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
15702 && ! TREE_ASM_WRITTEN (*tp))
15703 return *tp;
15704 /* ??? The C++ FE emits debug information for using decls, so
15705 putting gcc_unreachable here falls over. See PR31899. For now
15706 be conservative. */
15707 else if (!symtab->global_info_ready
15708 && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
15709 return *tp;
15710 else if (TREE_CODE (*tp) == VAR_DECL)
15712 varpool_node *node = varpool_node::get (*tp);
15713 if (!node || !node->definition)
15714 return *tp;
15716 else if (TREE_CODE (*tp) == FUNCTION_DECL
15717 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
15719 /* The call graph machinery must have finished analyzing,
15720 optimizing and gimplifying the CU by now.
15721 So if *TP has no call graph node associated
15722 to it, it means *TP will not be emitted. */
15723 if (!cgraph_node::get (*tp))
15724 return *tp;
15726 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
15727 return *tp;
15729 return NULL_TREE;
15732 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
15733 for use in a later add_const_value_attribute call. */
15735 static rtx
15736 rtl_for_decl_init (tree init, tree type)
15738 rtx rtl = NULL_RTX;
15740 STRIP_NOPS (init);
15742 /* If a variable is initialized with a string constant without embedded
15743 zeros, build CONST_STRING. */
15744 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
15746 tree enttype = TREE_TYPE (type);
15747 tree domain = TYPE_DOMAIN (type);
15748 machine_mode mode = TYPE_MODE (enttype);
15750 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
15751 && domain
15752 && integer_zerop (TYPE_MIN_VALUE (domain))
15753 && compare_tree_int (TYPE_MAX_VALUE (domain),
15754 TREE_STRING_LENGTH (init) - 1) == 0
15755 && ((size_t) TREE_STRING_LENGTH (init)
15756 == strlen (TREE_STRING_POINTER (init)) + 1))
15758 rtl = gen_rtx_CONST_STRING (VOIDmode,
15759 ggc_strdup (TREE_STRING_POINTER (init)));
15760 rtl = gen_rtx_MEM (BLKmode, rtl);
15761 MEM_READONLY_P (rtl) = 1;
15764 /* Other aggregates, and complex values, could be represented using
15765 CONCAT: FIXME! */
15766 else if (AGGREGATE_TYPE_P (type)
15767 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
15768 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
15769 || TREE_CODE (type) == COMPLEX_TYPE)
15771 /* Vectors only work if their mode is supported by the target.
15772 FIXME: generic vectors ought to work too. */
15773 else if (TREE_CODE (type) == VECTOR_TYPE
15774 && !VECTOR_MODE_P (TYPE_MODE (type)))
15776 /* If the initializer is something that we know will expand into an
15777 immediate RTL constant, expand it now. We must be careful not to
15778 reference variables which won't be output. */
15779 else if (initializer_constant_valid_p (init, type)
15780 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
15782 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
15783 possible. */
15784 if (TREE_CODE (type) == VECTOR_TYPE)
15785 switch (TREE_CODE (init))
15787 case VECTOR_CST:
15788 break;
15789 case CONSTRUCTOR:
15790 if (TREE_CONSTANT (init))
15792 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
15793 bool constant_p = true;
15794 tree value;
15795 unsigned HOST_WIDE_INT ix;
15797 /* Even when ctor is constant, it might contain non-*_CST
15798 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
15799 belong into VECTOR_CST nodes. */
15800 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
15801 if (!CONSTANT_CLASS_P (value))
15803 constant_p = false;
15804 break;
15807 if (constant_p)
15809 init = build_vector_from_ctor (type, elts);
15810 break;
15813 /* FALLTHRU */
15815 default:
15816 return NULL;
15819 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
15821 /* If expand_expr returns a MEM, it wasn't immediate. */
15822 gcc_assert (!rtl || !MEM_P (rtl));
15825 return rtl;
15828 /* Generate RTL for the variable DECL to represent its location. */
15830 static rtx
15831 rtl_for_decl_location (tree decl)
15833 rtx rtl;
15835 /* Here we have to decide where we are going to say the parameter "lives"
15836 (as far as the debugger is concerned). We only have a couple of
15837 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
15839 DECL_RTL normally indicates where the parameter lives during most of the
15840 activation of the function. If optimization is enabled however, this
15841 could be either NULL or else a pseudo-reg. Both of those cases indicate
15842 that the parameter doesn't really live anywhere (as far as the code
15843 generation parts of GCC are concerned) during most of the function's
15844 activation. That will happen (for example) if the parameter is never
15845 referenced within the function.
15847 We could just generate a location descriptor here for all non-NULL
15848 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
15849 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
15850 where DECL_RTL is NULL or is a pseudo-reg.
15852 Note however that we can only get away with using DECL_INCOMING_RTL as
15853 a backup substitute for DECL_RTL in certain limited cases. In cases
15854 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
15855 we can be sure that the parameter was passed using the same type as it is
15856 declared to have within the function, and that its DECL_INCOMING_RTL
15857 points us to a place where a value of that type is passed.
15859 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
15860 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
15861 because in these cases DECL_INCOMING_RTL points us to a value of some
15862 type which is *different* from the type of the parameter itself. Thus,
15863 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
15864 such cases, the debugger would end up (for example) trying to fetch a
15865 `float' from a place which actually contains the first part of a
15866 `double'. That would lead to really incorrect and confusing
15867 output at debug-time.
15869 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
15870 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
15871 are a couple of exceptions however. On little-endian machines we can
15872 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
15873 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
15874 an integral type that is smaller than TREE_TYPE (decl). These cases arise
15875 when (on a little-endian machine) a non-prototyped function has a
15876 parameter declared to be of type `short' or `char'. In such cases,
15877 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
15878 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
15879 passed `int' value. If the debugger then uses that address to fetch
15880 a `short' or a `char' (on a little-endian machine) the result will be
15881 the correct data, so we allow for such exceptional cases below.
15883 Note that our goal here is to describe the place where the given formal
15884 parameter lives during most of the function's activation (i.e. between the
15885 end of the prologue and the start of the epilogue). We'll do that as best
15886 as we can. Note however that if the given formal parameter is modified
15887 sometime during the execution of the function, then a stack backtrace (at
15888 debug-time) will show the function as having been called with the *new*
15889 value rather than the value which was originally passed in. This happens
15890 rarely enough that it is not a major problem, but it *is* a problem, and
15891 I'd like to fix it.
15893 A future version of dwarf2out.c may generate two additional attributes for
15894 any given DW_TAG_formal_parameter DIE which will describe the "passed
15895 type" and the "passed location" for the given formal parameter in addition
15896 to the attributes we now generate to indicate the "declared type" and the
15897 "active location" for each parameter. This additional set of attributes
15898 could be used by debuggers for stack backtraces. Separately, note that
15899 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
15900 This happens (for example) for inlined-instances of inline function formal
15901 parameters which are never referenced. This really shouldn't be
15902 happening. All PARM_DECL nodes should get valid non-NULL
15903 DECL_INCOMING_RTL values. FIXME. */
15905 /* Use DECL_RTL as the "location" unless we find something better. */
15906 rtl = DECL_RTL_IF_SET (decl);
15908 /* When generating abstract instances, ignore everything except
15909 constants, symbols living in memory, and symbols living in
15910 fixed registers. */
15911 if (! reload_completed)
15913 if (rtl
15914 && (CONSTANT_P (rtl)
15915 || (MEM_P (rtl)
15916 && CONSTANT_P (XEXP (rtl, 0)))
15917 || (REG_P (rtl)
15918 && TREE_CODE (decl) == VAR_DECL
15919 && TREE_STATIC (decl))))
15921 rtl = targetm.delegitimize_address (rtl);
15922 return rtl;
15924 rtl = NULL_RTX;
15926 else if (TREE_CODE (decl) == PARM_DECL)
15928 if (rtl == NULL_RTX
15929 || is_pseudo_reg (rtl)
15930 || (MEM_P (rtl)
15931 && is_pseudo_reg (XEXP (rtl, 0))
15932 && DECL_INCOMING_RTL (decl)
15933 && MEM_P (DECL_INCOMING_RTL (decl))
15934 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
15936 tree declared_type = TREE_TYPE (decl);
15937 tree passed_type = DECL_ARG_TYPE (decl);
15938 machine_mode dmode = TYPE_MODE (declared_type);
15939 machine_mode pmode = TYPE_MODE (passed_type);
15941 /* This decl represents a formal parameter which was optimized out.
15942 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
15943 all cases where (rtl == NULL_RTX) just below. */
15944 if (dmode == pmode)
15945 rtl = DECL_INCOMING_RTL (decl);
15946 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
15947 && SCALAR_INT_MODE_P (dmode)
15948 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
15949 && DECL_INCOMING_RTL (decl))
15951 rtx inc = DECL_INCOMING_RTL (decl);
15952 if (REG_P (inc))
15953 rtl = inc;
15954 else if (MEM_P (inc))
15956 if (BYTES_BIG_ENDIAN)
15957 rtl = adjust_address_nv (inc, dmode,
15958 GET_MODE_SIZE (pmode)
15959 - GET_MODE_SIZE (dmode));
15960 else
15961 rtl = inc;
15966 /* If the parm was passed in registers, but lives on the stack, then
15967 make a big endian correction if the mode of the type of the
15968 parameter is not the same as the mode of the rtl. */
15969 /* ??? This is the same series of checks that are made in dbxout.c before
15970 we reach the big endian correction code there. It isn't clear if all
15971 of these checks are necessary here, but keeping them all is the safe
15972 thing to do. */
15973 else if (MEM_P (rtl)
15974 && XEXP (rtl, 0) != const0_rtx
15975 && ! CONSTANT_P (XEXP (rtl, 0))
15976 /* Not passed in memory. */
15977 && !MEM_P (DECL_INCOMING_RTL (decl))
15978 /* Not passed by invisible reference. */
15979 && (!REG_P (XEXP (rtl, 0))
15980 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
15981 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
15982 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
15983 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
15984 #endif
15986 /* Big endian correction check. */
15987 && BYTES_BIG_ENDIAN
15988 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
15989 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
15990 < UNITS_PER_WORD))
15992 machine_mode addr_mode = get_address_mode (rtl);
15993 int offset = (UNITS_PER_WORD
15994 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
15996 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15997 plus_constant (addr_mode, XEXP (rtl, 0), offset));
16000 else if (TREE_CODE (decl) == VAR_DECL
16001 && rtl
16002 && MEM_P (rtl)
16003 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
16004 && BYTES_BIG_ENDIAN)
16006 machine_mode addr_mode = get_address_mode (rtl);
16007 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
16008 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
16010 /* If a variable is declared "register" yet is smaller than
16011 a register, then if we store the variable to memory, it
16012 looks like we're storing a register-sized value, when in
16013 fact we are not. We need to adjust the offset of the
16014 storage location to reflect the actual value's bytes,
16015 else gdb will not be able to display it. */
16016 if (rsize > dsize)
16017 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
16018 plus_constant (addr_mode, XEXP (rtl, 0),
16019 rsize - dsize));
16022 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
16023 and will have been substituted directly into all expressions that use it.
16024 C does not have such a concept, but C++ and other languages do. */
16025 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
16026 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
16028 if (rtl)
16029 rtl = targetm.delegitimize_address (rtl);
16031 /* If we don't look past the constant pool, we risk emitting a
16032 reference to a constant pool entry that isn't referenced from
16033 code, and thus is not emitted. */
16034 if (rtl)
16035 rtl = avoid_constant_pool_reference (rtl);
16037 /* Try harder to get a rtl. If this symbol ends up not being emitted
16038 in the current CU, resolve_addr will remove the expression referencing
16039 it. */
16040 if (rtl == NULL_RTX
16041 && TREE_CODE (decl) == VAR_DECL
16042 && !DECL_EXTERNAL (decl)
16043 && TREE_STATIC (decl)
16044 && DECL_NAME (decl)
16045 && !DECL_HARD_REGISTER (decl)
16046 && DECL_MODE (decl) != VOIDmode)
16048 rtl = make_decl_rtl_for_debug (decl);
16049 if (!MEM_P (rtl)
16050 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
16051 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
16052 rtl = NULL_RTX;
16055 return rtl;
16058 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
16059 returned. If so, the decl for the COMMON block is returned, and the
16060 value is the offset into the common block for the symbol. */
16062 static tree
16063 fortran_common (tree decl, HOST_WIDE_INT *value)
16065 tree val_expr, cvar;
16066 machine_mode mode;
16067 HOST_WIDE_INT bitsize, bitpos;
16068 tree offset;
16069 int unsignedp, volatilep = 0;
16071 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
16072 it does not have a value (the offset into the common area), or if it
16073 is thread local (as opposed to global) then it isn't common, and shouldn't
16074 be handled as such. */
16075 if (TREE_CODE (decl) != VAR_DECL
16076 || !TREE_STATIC (decl)
16077 || !DECL_HAS_VALUE_EXPR_P (decl)
16078 || !is_fortran ())
16079 return NULL_TREE;
16081 val_expr = DECL_VALUE_EXPR (decl);
16082 if (TREE_CODE (val_expr) != COMPONENT_REF)
16083 return NULL_TREE;
16085 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
16086 &mode, &unsignedp, &volatilep, true);
16088 if (cvar == NULL_TREE
16089 || TREE_CODE (cvar) != VAR_DECL
16090 || DECL_ARTIFICIAL (cvar)
16091 || !TREE_PUBLIC (cvar))
16092 return NULL_TREE;
16094 *value = 0;
16095 if (offset != NULL)
16097 if (!tree_fits_shwi_p (offset))
16098 return NULL_TREE;
16099 *value = tree_to_shwi (offset);
16101 if (bitpos != 0)
16102 *value += bitpos / BITS_PER_UNIT;
16104 return cvar;
16107 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
16108 data attribute for a variable or a parameter. We generate the
16109 DW_AT_const_value attribute only in those cases where the given variable
16110 or parameter does not have a true "location" either in memory or in a
16111 register. This can happen (for example) when a constant is passed as an
16112 actual argument in a call to an inline function. (It's possible that
16113 these things can crop up in other ways also.) Note that one type of
16114 constant value which can be passed into an inlined function is a constant
16115 pointer. This can happen for example if an actual argument in an inlined
16116 function call evaluates to a compile-time constant address.
16118 CACHE_P is true if it is worth caching the location list for DECL,
16119 so that future calls can reuse it rather than regenerate it from scratch.
16120 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
16121 since we will need to refer to them each time the function is inlined. */
16123 static bool
16124 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
16125 enum dwarf_attribute attr)
16127 rtx rtl;
16128 dw_loc_list_ref list;
16129 var_loc_list *loc_list;
16130 cached_dw_loc_list *cache;
16132 if (TREE_CODE (decl) == ERROR_MARK)
16133 return false;
16135 if (get_AT (die, attr))
16136 return true;
16138 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
16139 || TREE_CODE (decl) == RESULT_DECL);
16141 /* Try to get some constant RTL for this decl, and use that as the value of
16142 the location. */
16144 rtl = rtl_for_decl_location (decl);
16145 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16146 && add_const_value_attribute (die, rtl))
16147 return true;
16149 /* See if we have single element location list that is equivalent to
16150 a constant value. That way we are better to use add_const_value_attribute
16151 rather than expanding constant value equivalent. */
16152 loc_list = lookup_decl_loc (decl);
16153 if (loc_list
16154 && loc_list->first
16155 && loc_list->first->next == NULL
16156 && NOTE_P (loc_list->first->loc)
16157 && NOTE_VAR_LOCATION (loc_list->first->loc)
16158 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
16160 struct var_loc_node *node;
16162 node = loc_list->first;
16163 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
16164 if (GET_CODE (rtl) == EXPR_LIST)
16165 rtl = XEXP (rtl, 0);
16166 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16167 && add_const_value_attribute (die, rtl))
16168 return true;
16170 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
16171 list several times. See if we've already cached the contents. */
16172 list = NULL;
16173 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
16174 cache_p = false;
16175 if (cache_p)
16177 cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
16178 if (cache)
16179 list = cache->loc_list;
16181 if (list == NULL)
16183 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
16184 NULL);
16185 /* It is usually worth caching this result if the decl is from
16186 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
16187 if (cache_p && list && list->dw_loc_next)
16189 cached_dw_loc_list **slot
16190 = cached_dw_loc_list_table->find_slot_with_hash (decl,
16191 DECL_UID (decl),
16192 INSERT);
16193 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
16194 cache->decl_id = DECL_UID (decl);
16195 cache->loc_list = list;
16196 *slot = cache;
16199 if (list)
16201 add_AT_location_description (die, attr, list);
16202 return true;
16204 /* None of that worked, so it must not really have a location;
16205 try adding a constant value attribute from the DECL_INITIAL. */
16206 return tree_add_const_value_attribute_for_decl (die, decl);
16209 /* Helper function for tree_add_const_value_attribute. Natively encode
16210 initializer INIT into an array. Return true if successful. */
16212 static bool
16213 native_encode_initializer (tree init, unsigned char *array, int size)
16215 tree type;
16217 if (init == NULL_TREE)
16218 return false;
16220 STRIP_NOPS (init);
16221 switch (TREE_CODE (init))
16223 case STRING_CST:
16224 type = TREE_TYPE (init);
16225 if (TREE_CODE (type) == ARRAY_TYPE)
16227 tree enttype = TREE_TYPE (type);
16228 machine_mode mode = TYPE_MODE (enttype);
16230 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
16231 return false;
16232 if (int_size_in_bytes (type) != size)
16233 return false;
16234 if (size > TREE_STRING_LENGTH (init))
16236 memcpy (array, TREE_STRING_POINTER (init),
16237 TREE_STRING_LENGTH (init));
16238 memset (array + TREE_STRING_LENGTH (init),
16239 '\0', size - TREE_STRING_LENGTH (init));
16241 else
16242 memcpy (array, TREE_STRING_POINTER (init), size);
16243 return true;
16245 return false;
16246 case CONSTRUCTOR:
16247 type = TREE_TYPE (init);
16248 if (int_size_in_bytes (type) != size)
16249 return false;
16250 if (TREE_CODE (type) == ARRAY_TYPE)
16252 HOST_WIDE_INT min_index;
16253 unsigned HOST_WIDE_INT cnt;
16254 int curpos = 0, fieldsize;
16255 constructor_elt *ce;
16257 if (TYPE_DOMAIN (type) == NULL_TREE
16258 || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
16259 return false;
16261 fieldsize = int_size_in_bytes (TREE_TYPE (type));
16262 if (fieldsize <= 0)
16263 return false;
16265 min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
16266 memset (array, '\0', size);
16267 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
16269 tree val = ce->value;
16270 tree index = ce->index;
16271 int pos = curpos;
16272 if (index && TREE_CODE (index) == RANGE_EXPR)
16273 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
16274 * fieldsize;
16275 else if (index)
16276 pos = (tree_to_shwi (index) - min_index) * fieldsize;
16278 if (val)
16280 STRIP_NOPS (val);
16281 if (!native_encode_initializer (val, array + pos, fieldsize))
16282 return false;
16284 curpos = pos + fieldsize;
16285 if (index && TREE_CODE (index) == RANGE_EXPR)
16287 int count = tree_to_shwi (TREE_OPERAND (index, 1))
16288 - tree_to_shwi (TREE_OPERAND (index, 0));
16289 while (count-- > 0)
16291 if (val)
16292 memcpy (array + curpos, array + pos, fieldsize);
16293 curpos += fieldsize;
16296 gcc_assert (curpos <= size);
16298 return true;
16300 else if (TREE_CODE (type) == RECORD_TYPE
16301 || TREE_CODE (type) == UNION_TYPE)
16303 tree field = NULL_TREE;
16304 unsigned HOST_WIDE_INT cnt;
16305 constructor_elt *ce;
16307 if (int_size_in_bytes (type) != size)
16308 return false;
16310 if (TREE_CODE (type) == RECORD_TYPE)
16311 field = TYPE_FIELDS (type);
16313 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
16315 tree val = ce->value;
16316 int pos, fieldsize;
16318 if (ce->index != 0)
16319 field = ce->index;
16321 if (val)
16322 STRIP_NOPS (val);
16324 if (field == NULL_TREE || DECL_BIT_FIELD (field))
16325 return false;
16327 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
16328 && TYPE_DOMAIN (TREE_TYPE (field))
16329 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
16330 return false;
16331 else if (DECL_SIZE_UNIT (field) == NULL_TREE
16332 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
16333 return false;
16334 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
16335 pos = int_byte_position (field);
16336 gcc_assert (pos + fieldsize <= size);
16337 if (val
16338 && !native_encode_initializer (val, array + pos, fieldsize))
16339 return false;
16341 return true;
16343 return false;
16344 case VIEW_CONVERT_EXPR:
16345 case NON_LVALUE_EXPR:
16346 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
16347 default:
16348 return native_encode_expr (init, array, size) == size;
16352 /* Attach a DW_AT_const_value attribute to DIE. The value of the
16353 attribute is the const value T. */
16355 static bool
16356 tree_add_const_value_attribute (dw_die_ref die, tree t)
16358 tree init;
16359 tree type = TREE_TYPE (t);
16360 rtx rtl;
16362 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
16363 return false;
16365 init = t;
16366 gcc_assert (!DECL_P (init));
16368 rtl = rtl_for_decl_init (init, type);
16369 if (rtl)
16370 return add_const_value_attribute (die, rtl);
16371 /* If the host and target are sane, try harder. */
16372 else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
16373 && initializer_constant_valid_p (init, type))
16375 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
16376 if (size > 0 && (int) size == size)
16378 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
16380 if (native_encode_initializer (init, array, size))
16382 add_AT_vec (die, DW_AT_const_value, size, 1, array);
16383 return true;
16385 ggc_free (array);
16388 return false;
16391 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
16392 attribute is the const value of T, where T is an integral constant
16393 variable with static storage duration
16394 (so it can't be a PARM_DECL or a RESULT_DECL). */
16396 static bool
16397 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
16400 if (!decl
16401 || (TREE_CODE (decl) != VAR_DECL
16402 && TREE_CODE (decl) != CONST_DECL)
16403 || (TREE_CODE (decl) == VAR_DECL
16404 && !TREE_STATIC (decl)))
16405 return false;
16407 if (TREE_READONLY (decl)
16408 && ! TREE_THIS_VOLATILE (decl)
16409 && DECL_INITIAL (decl))
16410 /* OK */;
16411 else
16412 return false;
16414 /* Don't add DW_AT_const_value if abstract origin already has one. */
16415 if (get_AT (var_die, DW_AT_const_value))
16416 return false;
16418 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
16421 /* Convert the CFI instructions for the current function into a
16422 location list. This is used for DW_AT_frame_base when we targeting
16423 a dwarf2 consumer that does not support the dwarf3
16424 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
16425 expressions. */
16427 static dw_loc_list_ref
16428 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
16430 int ix;
16431 dw_fde_ref fde;
16432 dw_loc_list_ref list, *list_tail;
16433 dw_cfi_ref cfi;
16434 dw_cfa_location last_cfa, next_cfa;
16435 const char *start_label, *last_label, *section;
16436 dw_cfa_location remember;
16438 fde = cfun->fde;
16439 gcc_assert (fde != NULL);
16441 section = secname_for_decl (current_function_decl);
16442 list_tail = &list;
16443 list = NULL;
16445 memset (&next_cfa, 0, sizeof (next_cfa));
16446 next_cfa.reg = INVALID_REGNUM;
16447 remember = next_cfa;
16449 start_label = fde->dw_fde_begin;
16451 /* ??? Bald assumption that the CIE opcode list does not contain
16452 advance opcodes. */
16453 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
16454 lookup_cfa_1 (cfi, &next_cfa, &remember);
16456 last_cfa = next_cfa;
16457 last_label = start_label;
16459 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
16461 /* If the first partition contained no CFI adjustments, the
16462 CIE opcodes apply to the whole first partition. */
16463 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16464 fde->dw_fde_begin, fde->dw_fde_end, section);
16465 list_tail =&(*list_tail)->dw_loc_next;
16466 start_label = last_label = fde->dw_fde_second_begin;
16469 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
16471 switch (cfi->dw_cfi_opc)
16473 case DW_CFA_set_loc:
16474 case DW_CFA_advance_loc1:
16475 case DW_CFA_advance_loc2:
16476 case DW_CFA_advance_loc4:
16477 if (!cfa_equal_p (&last_cfa, &next_cfa))
16479 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16480 start_label, last_label, section);
16482 list_tail = &(*list_tail)->dw_loc_next;
16483 last_cfa = next_cfa;
16484 start_label = last_label;
16486 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
16487 break;
16489 case DW_CFA_advance_loc:
16490 /* The encoding is complex enough that we should never emit this. */
16491 gcc_unreachable ();
16493 default:
16494 lookup_cfa_1 (cfi, &next_cfa, &remember);
16495 break;
16497 if (ix + 1 == fde->dw_fde_switch_cfi_index)
16499 if (!cfa_equal_p (&last_cfa, &next_cfa))
16501 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16502 start_label, last_label, section);
16504 list_tail = &(*list_tail)->dw_loc_next;
16505 last_cfa = next_cfa;
16506 start_label = last_label;
16508 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16509 start_label, fde->dw_fde_end, section);
16510 list_tail = &(*list_tail)->dw_loc_next;
16511 start_label = last_label = fde->dw_fde_second_begin;
16515 if (!cfa_equal_p (&last_cfa, &next_cfa))
16517 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16518 start_label, last_label, section);
16519 list_tail = &(*list_tail)->dw_loc_next;
16520 start_label = last_label;
16523 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
16524 start_label,
16525 fde->dw_fde_second_begin
16526 ? fde->dw_fde_second_end : fde->dw_fde_end,
16527 section);
16529 if (list && list->dw_loc_next)
16530 gen_llsym (list);
16532 return list;
16535 /* Compute a displacement from the "steady-state frame pointer" to the
16536 frame base (often the same as the CFA), and store it in
16537 frame_pointer_fb_offset. OFFSET is added to the displacement
16538 before the latter is negated. */
16540 static void
16541 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
16543 rtx reg, elim;
16545 #ifdef FRAME_POINTER_CFA_OFFSET
16546 reg = frame_pointer_rtx;
16547 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
16548 #else
16549 reg = arg_pointer_rtx;
16550 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
16551 #endif
16553 elim = (ira_use_lra_p
16554 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
16555 : eliminate_regs (reg, VOIDmode, NULL_RTX));
16556 if (GET_CODE (elim) == PLUS)
16558 offset += INTVAL (XEXP (elim, 1));
16559 elim = XEXP (elim, 0);
16562 frame_pointer_fb_offset = -offset;
16564 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
16565 in which to eliminate. This is because it's stack pointer isn't
16566 directly accessible as a register within the ISA. To work around
16567 this, assume that while we cannot provide a proper value for
16568 frame_pointer_fb_offset, we won't need one either. */
16569 frame_pointer_fb_offset_valid
16570 = ((SUPPORTS_STACK_ALIGNMENT
16571 && (elim == hard_frame_pointer_rtx
16572 || elim == stack_pointer_rtx))
16573 || elim == (frame_pointer_needed
16574 ? hard_frame_pointer_rtx
16575 : stack_pointer_rtx));
16578 /* Generate a DW_AT_name attribute given some string value to be included as
16579 the value of the attribute. */
16581 static void
16582 add_name_attribute (dw_die_ref die, const char *name_string)
16584 if (name_string != NULL && *name_string != 0)
16586 if (demangle_name_func)
16587 name_string = (*demangle_name_func) (name_string);
16589 add_AT_string (die, DW_AT_name, name_string);
16593 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
16594 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
16595 of TYPE accordingly.
16597 ??? This is a temporary measure until after we're able to generate
16598 regular DWARF for the complex Ada type system. */
16600 static void
16601 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
16602 dw_die_ref context_die)
16604 tree dtype;
16605 dw_die_ref dtype_die;
16607 if (!lang_hooks.types.descriptive_type)
16608 return;
16610 dtype = lang_hooks.types.descriptive_type (type);
16611 if (!dtype)
16612 return;
16614 dtype_die = lookup_type_die (dtype);
16615 if (!dtype_die)
16617 gen_type_die (dtype, context_die);
16618 dtype_die = lookup_type_die (dtype);
16619 gcc_assert (dtype_die);
16622 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
16625 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
16627 static const char *
16628 comp_dir_string (void)
16630 const char *wd;
16631 char *wd1;
16632 static const char *cached_wd = NULL;
16634 if (cached_wd != NULL)
16635 return cached_wd;
16637 wd = get_src_pwd ();
16638 if (wd == NULL)
16639 return NULL;
16641 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
16643 int wdlen;
16645 wdlen = strlen (wd);
16646 wd1 = ggc_vec_alloc<char> (wdlen + 2);
16647 strcpy (wd1, wd);
16648 wd1 [wdlen] = DIR_SEPARATOR;
16649 wd1 [wdlen + 1] = 0;
16650 wd = wd1;
16653 cached_wd = remap_debug_filename (wd);
16654 return cached_wd;
16657 /* Generate a DW_AT_comp_dir attribute for DIE. */
16659 static void
16660 add_comp_dir_attribute (dw_die_ref die)
16662 const char * wd = comp_dir_string ();
16663 if (wd != NULL)
16664 add_AT_string (die, DW_AT_comp_dir, wd);
16667 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
16668 pointer computation, ...), output a representation for that bound according
16669 to the accepted FORMS (see enum dw_scalar_form) and add it to DIE. See
16670 loc_list_from_tree for the meaning of CONTEXT. */
16672 static void
16673 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
16674 int forms, const struct loc_descr_context *context)
16676 dw_die_ref ctx, decl_die;
16677 dw_loc_list_ref list;
16679 bool strip_conversions = true;
16681 while (strip_conversions)
16682 switch (TREE_CODE (value))
16684 case ERROR_MARK:
16685 case SAVE_EXPR:
16686 return;
16688 CASE_CONVERT:
16689 case VIEW_CONVERT_EXPR:
16690 value = TREE_OPERAND (value, 0);
16691 break;
16693 default:
16694 strip_conversions = false;
16695 break;
16698 /* If possible and permitted, output the attribute as a constant. */
16699 if ((forms & dw_scalar_form_constant) != 0
16700 && TREE_CODE (value) == INTEGER_CST)
16702 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
16704 /* If HOST_WIDE_INT is big enough then represent the bound as
16705 a constant value. We need to choose a form based on
16706 whether the type is signed or unsigned. We cannot just
16707 call add_AT_unsigned if the value itself is positive
16708 (add_AT_unsigned might add the unsigned value encoded as
16709 DW_FORM_data[1248]). Some DWARF consumers will lookup the
16710 bounds type and then sign extend any unsigned values found
16711 for signed types. This is needed only for
16712 DW_AT_{lower,upper}_bound, since for most other attributes,
16713 consumers will treat DW_FORM_data[1248] as unsigned values,
16714 regardless of the underlying type. */
16715 if (prec <= HOST_BITS_PER_WIDE_INT
16716 || tree_fits_uhwi_p (value))
16718 if (TYPE_UNSIGNED (TREE_TYPE (value)))
16719 add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
16720 else
16721 add_AT_int (die, attr, TREE_INT_CST_LOW (value));
16723 else
16724 /* Otherwise represent the bound as an unsigned value with
16725 the precision of its type. The precision and signedness
16726 of the type will be necessary to re-interpret it
16727 unambiguously. */
16728 add_AT_wide (die, attr, value);
16729 return;
16732 /* Otherwise, if it's possible and permitted too, output a reference to
16733 another DIE. */
16734 if ((forms & dw_scalar_form_reference) != 0)
16736 tree decl = NULL_TREE;
16738 /* Some type attributes reference an outer type. For instance, the upper
16739 bound of an array may reference an embedding record (this happens in
16740 Ada). */
16741 if (TREE_CODE (value) == COMPONENT_REF
16742 && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
16743 && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
16744 decl = TREE_OPERAND (value, 1);
16746 else if (TREE_CODE (value) == VAR_DECL
16747 || TREE_CODE (value) == PARM_DECL
16748 || TREE_CODE (value) == RESULT_DECL)
16749 decl = value;
16751 if (decl != NULL_TREE)
16753 dw_die_ref decl_die = lookup_decl_die (decl);
16755 /* ??? Can this happen, or should the variable have been bound
16756 first? Probably it can, since I imagine that we try to create
16757 the types of parameters in the order in which they exist in
16758 the list, and won't have created a forward reference to a
16759 later parameter. */
16760 if (decl_die != NULL)
16762 add_AT_die_ref (die, attr, decl_die);
16763 return;
16768 /* Last chance: try to create a stack operation procedure to evaluate the
16769 value. Do nothing if even that is not possible or permitted. */
16770 if ((forms & dw_scalar_form_exprloc) == 0)
16771 return;
16773 list = loc_list_from_tree (value, 2, context);
16774 if (list == NULL || single_element_loc_list_p (list))
16776 /* If this attribute is not a reference nor constant, it is
16777 a DWARF expression rather than location description. For that
16778 loc_list_from_tree (value, 0, &context) is needed. */
16779 dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
16780 if (list2 && single_element_loc_list_p (list2))
16782 add_AT_loc (die, attr, list2->expr);
16783 return;
16787 /* If that failed to give a single element location list, fall back to
16788 outputting this as a reference... still if permitted. */
16789 if (list == NULL || (forms & dw_scalar_form_reference) == 0)
16790 return;
16792 if (current_function_decl == 0)
16793 ctx = comp_unit_die ();
16794 else
16795 ctx = lookup_decl_die (current_function_decl);
16797 decl_die = new_die (DW_TAG_variable, ctx, value);
16798 add_AT_flag (decl_die, DW_AT_artificial, 1);
16799 add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, ctx);
16800 add_AT_location_description (decl_die, DW_AT_location, list);
16801 add_AT_die_ref (die, attr, decl_die);
16804 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
16805 default. */
16807 static int
16808 lower_bound_default (void)
16810 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
16812 case DW_LANG_C:
16813 case DW_LANG_C89:
16814 case DW_LANG_C99:
16815 case DW_LANG_C11:
16816 case DW_LANG_C_plus_plus:
16817 case DW_LANG_C_plus_plus_11:
16818 case DW_LANG_C_plus_plus_14:
16819 case DW_LANG_ObjC:
16820 case DW_LANG_ObjC_plus_plus:
16821 case DW_LANG_Java:
16822 return 0;
16823 case DW_LANG_Fortran77:
16824 case DW_LANG_Fortran90:
16825 case DW_LANG_Fortran95:
16826 case DW_LANG_Fortran03:
16827 case DW_LANG_Fortran08:
16828 return 1;
16829 case DW_LANG_UPC:
16830 case DW_LANG_D:
16831 case DW_LANG_Python:
16832 return dwarf_version >= 4 ? 0 : -1;
16833 case DW_LANG_Ada95:
16834 case DW_LANG_Ada83:
16835 case DW_LANG_Cobol74:
16836 case DW_LANG_Cobol85:
16837 case DW_LANG_Pascal83:
16838 case DW_LANG_Modula2:
16839 case DW_LANG_PLI:
16840 return dwarf_version >= 4 ? 1 : -1;
16841 default:
16842 return -1;
16846 /* Given a tree node describing an array bound (either lower or upper) output
16847 a representation for that bound. */
16849 static void
16850 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
16851 tree bound, const struct loc_descr_context *context)
16853 int dflt;
16855 while (1)
16856 switch (TREE_CODE (bound))
16858 /* Strip all conversions. */
16859 CASE_CONVERT:
16860 case VIEW_CONVERT_EXPR:
16861 bound = TREE_OPERAND (bound, 0);
16862 break;
16864 /* All fixed-bounds are represented by INTEGER_CST nodes. Lower bounds
16865 are even omitted when they are the default. */
16866 case INTEGER_CST:
16867 /* If the value for this bound is the default one, we can even omit the
16868 attribute. */
16869 if (bound_attr == DW_AT_lower_bound
16870 && tree_fits_shwi_p (bound)
16871 && (dflt = lower_bound_default ()) != -1
16872 && tree_to_shwi (bound) == dflt)
16873 return;
16875 /* FALLTHRU */
16877 default:
16878 add_scalar_info (subrange_die, bound_attr, bound,
16879 dw_scalar_form_constant
16880 | dw_scalar_form_exprloc
16881 | dw_scalar_form_reference,
16882 context);
16883 return;
16887 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
16888 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
16889 Note that the block of subscript information for an array type also
16890 includes information about the element type of the given array type.
16892 This function reuses previously set type and bound information if
16893 available. */
16895 static void
16896 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
16898 unsigned dimension_number;
16899 tree lower, upper;
16900 dw_die_ref child = type_die->die_child;
16902 for (dimension_number = 0;
16903 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
16904 type = TREE_TYPE (type), dimension_number++)
16906 tree domain = TYPE_DOMAIN (type);
16908 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
16909 break;
16911 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
16912 and (in GNU C only) variable bounds. Handle all three forms
16913 here. */
16915 /* Find and reuse a previously generated DW_TAG_subrange_type if
16916 available.
16918 For multi-dimensional arrays, as we iterate through the
16919 various dimensions in the enclosing for loop above, we also
16920 iterate through the DIE children and pick at each
16921 DW_TAG_subrange_type previously generated (if available).
16922 Each child DW_TAG_subrange_type DIE describes the range of
16923 the current dimension. At this point we should have as many
16924 DW_TAG_subrange_type's as we have dimensions in the
16925 array. */
16926 dw_die_ref subrange_die = NULL;
16927 if (child)
16928 while (1)
16930 child = child->die_sib;
16931 if (child->die_tag == DW_TAG_subrange_type)
16932 subrange_die = child;
16933 if (child == type_die->die_child)
16935 /* If we wrapped around, stop looking next time. */
16936 child = NULL;
16937 break;
16939 if (child->die_tag == DW_TAG_subrange_type)
16940 break;
16942 if (!subrange_die)
16943 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
16945 if (domain)
16947 /* We have an array type with specified bounds. */
16948 lower = TYPE_MIN_VALUE (domain);
16949 upper = TYPE_MAX_VALUE (domain);
16951 /* Define the index type. */
16952 if (TREE_TYPE (domain)
16953 && !get_AT (subrange_die, DW_AT_type))
16955 /* ??? This is probably an Ada unnamed subrange type. Ignore the
16956 TREE_TYPE field. We can't emit debug info for this
16957 because it is an unnamed integral type. */
16958 if (TREE_CODE (domain) == INTEGER_TYPE
16959 && TYPE_NAME (domain) == NULL_TREE
16960 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
16961 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
16963 else
16964 add_type_attribute (subrange_die, TREE_TYPE (domain),
16965 TYPE_UNQUALIFIED, type_die);
16968 /* ??? If upper is NULL, the array has unspecified length,
16969 but it does have a lower bound. This happens with Fortran
16970 dimension arr(N:*)
16971 Since the debugger is definitely going to need to know N
16972 to produce useful results, go ahead and output the lower
16973 bound solo, and hope the debugger can cope. */
16975 if (!get_AT (subrange_die, DW_AT_lower_bound))
16976 add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
16977 if (upper && !get_AT (subrange_die, DW_AT_upper_bound))
16978 add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
16981 /* Otherwise we have an array type with an unspecified length. The
16982 DWARF-2 spec does not say how to handle this; let's just leave out the
16983 bounds. */
16987 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
16989 static void
16990 add_byte_size_attribute (dw_die_ref die, tree tree_node)
16992 dw_die_ref decl_die;
16993 HOST_WIDE_INT size;
16995 switch (TREE_CODE (tree_node))
16997 case ERROR_MARK:
16998 size = 0;
16999 break;
17000 case ENUMERAL_TYPE:
17001 case RECORD_TYPE:
17002 case UNION_TYPE:
17003 case QUAL_UNION_TYPE:
17004 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
17005 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
17007 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
17008 return;
17010 size = int_size_in_bytes (tree_node);
17011 break;
17012 case FIELD_DECL:
17013 /* For a data member of a struct or union, the DW_AT_byte_size is
17014 generally given as the number of bytes normally allocated for an
17015 object of the *declared* type of the member itself. This is true
17016 even for bit-fields. */
17017 size = int_size_in_bytes (field_type (tree_node));
17018 break;
17019 default:
17020 gcc_unreachable ();
17023 /* Note that `size' might be -1 when we get to this point. If it is, that
17024 indicates that the byte size of the entity in question is variable. We
17025 have no good way of expressing this fact in Dwarf at the present time,
17026 when location description was not used by the caller code instead. */
17027 if (size >= 0)
17028 add_AT_unsigned (die, DW_AT_byte_size, size);
17031 /* For a FIELD_DECL node which represents a bit-field, output an attribute
17032 which specifies the distance in bits from the highest order bit of the
17033 "containing object" for the bit-field to the highest order bit of the
17034 bit-field itself.
17036 For any given bit-field, the "containing object" is a hypothetical object
17037 (of some integral or enum type) within which the given bit-field lives. The
17038 type of this hypothetical "containing object" is always the same as the
17039 declared type of the individual bit-field itself. The determination of the
17040 exact location of the "containing object" for a bit-field is rather
17041 complicated. It's handled by the `field_byte_offset' function (above).
17043 Note that it is the size (in bytes) of the hypothetical "containing object"
17044 which will be given in the DW_AT_byte_size attribute for this bit-field.
17045 (See `byte_size_attribute' above). */
17047 static inline void
17048 add_bit_offset_attribute (dw_die_ref die, tree decl)
17050 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
17051 tree type = DECL_BIT_FIELD_TYPE (decl);
17052 HOST_WIDE_INT bitpos_int;
17053 HOST_WIDE_INT highest_order_object_bit_offset;
17054 HOST_WIDE_INT highest_order_field_bit_offset;
17055 HOST_WIDE_INT bit_offset;
17057 /* Must be a field and a bit field. */
17058 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
17060 /* We can't yet handle bit-fields whose offsets are variable, so if we
17061 encounter such things, just return without generating any attribute
17062 whatsoever. Likewise for variable or too large size. */
17063 if (! tree_fits_shwi_p (bit_position (decl))
17064 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
17065 return;
17067 bitpos_int = int_bit_position (decl);
17069 /* Note that the bit offset is always the distance (in bits) from the
17070 highest-order bit of the "containing object" to the highest-order bit of
17071 the bit-field itself. Since the "high-order end" of any object or field
17072 is different on big-endian and little-endian machines, the computation
17073 below must take account of these differences. */
17074 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
17075 highest_order_field_bit_offset = bitpos_int;
17077 if (! BYTES_BIG_ENDIAN)
17079 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
17080 highest_order_object_bit_offset += simple_type_size_in_bits (type);
17083 bit_offset
17084 = (! BYTES_BIG_ENDIAN
17085 ? highest_order_object_bit_offset - highest_order_field_bit_offset
17086 : highest_order_field_bit_offset - highest_order_object_bit_offset);
17088 if (bit_offset < 0)
17089 add_AT_int (die, DW_AT_bit_offset, bit_offset);
17090 else
17091 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
17094 /* For a FIELD_DECL node which represents a bit field, output an attribute
17095 which specifies the length in bits of the given field. */
17097 static inline void
17098 add_bit_size_attribute (dw_die_ref die, tree decl)
17100 /* Must be a field and a bit field. */
17101 gcc_assert (TREE_CODE (decl) == FIELD_DECL
17102 && DECL_BIT_FIELD_TYPE (decl));
17104 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
17105 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
17108 /* If the compiled language is ANSI C, then add a 'prototyped'
17109 attribute, if arg types are given for the parameters of a function. */
17111 static inline void
17112 add_prototyped_attribute (dw_die_ref die, tree func_type)
17114 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
17116 case DW_LANG_C:
17117 case DW_LANG_C89:
17118 case DW_LANG_C99:
17119 case DW_LANG_C11:
17120 case DW_LANG_ObjC:
17121 if (prototype_p (func_type))
17122 add_AT_flag (die, DW_AT_prototyped, 1);
17123 break;
17124 default:
17125 break;
17129 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
17130 by looking in either the type declaration or object declaration
17131 equate table. */
17133 static inline dw_die_ref
17134 add_abstract_origin_attribute (dw_die_ref die, tree origin)
17136 dw_die_ref origin_die = NULL;
17138 if (TREE_CODE (origin) != FUNCTION_DECL)
17140 /* We may have gotten separated from the block for the inlined
17141 function, if we're in an exception handler or some such; make
17142 sure that the abstract function has been written out.
17144 Doing this for nested functions is wrong, however; functions are
17145 distinct units, and our context might not even be inline. */
17146 tree fn = origin;
17148 if (TYPE_P (fn))
17149 fn = TYPE_STUB_DECL (fn);
17151 fn = decl_function_context (fn);
17152 if (fn)
17153 dwarf2out_abstract_function (fn);
17156 if (DECL_P (origin))
17157 origin_die = lookup_decl_die (origin);
17158 else if (TYPE_P (origin))
17159 origin_die = lookup_type_die (origin);
17161 /* XXX: Functions that are never lowered don't always have correct block
17162 trees (in the case of java, they simply have no block tree, in some other
17163 languages). For these functions, there is nothing we can really do to
17164 output correct debug info for inlined functions in all cases. Rather
17165 than die, we'll just produce deficient debug info now, in that we will
17166 have variables without a proper abstract origin. In the future, when all
17167 functions are lowered, we should re-add a gcc_assert (origin_die)
17168 here. */
17170 if (origin_die)
17171 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
17172 return origin_die;
17175 /* We do not currently support the pure_virtual attribute. */
17177 static inline void
17178 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
17180 if (DECL_VINDEX (func_decl))
17182 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
17184 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
17185 add_AT_loc (die, DW_AT_vtable_elem_location,
17186 new_loc_descr (DW_OP_constu,
17187 tree_to_shwi (DECL_VINDEX (func_decl)),
17188 0));
17190 /* GNU extension: Record what type this method came from originally. */
17191 if (debug_info_level > DINFO_LEVEL_TERSE
17192 && DECL_CONTEXT (func_decl))
17193 add_AT_die_ref (die, DW_AT_containing_type,
17194 lookup_type_die (DECL_CONTEXT (func_decl)));
17198 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
17199 given decl. This used to be a vendor extension until after DWARF 4
17200 standardized it. */
17202 static void
17203 add_linkage_attr (dw_die_ref die, tree decl)
17205 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
17207 /* Mimic what assemble_name_raw does with a leading '*'. */
17208 if (name[0] == '*')
17209 name = &name[1];
17211 if (dwarf_version >= 4)
17212 add_AT_string (die, DW_AT_linkage_name, name);
17213 else
17214 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
17217 /* Add source coordinate attributes for the given decl. */
17219 static void
17220 add_src_coords_attributes (dw_die_ref die, tree decl)
17222 expanded_location s;
17224 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
17225 return;
17226 s = expand_location (DECL_SOURCE_LOCATION (decl));
17227 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
17228 add_AT_unsigned (die, DW_AT_decl_line, s.line);
17231 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
17233 static void
17234 add_linkage_name (dw_die_ref die, tree decl)
17236 if (debug_info_level > DINFO_LEVEL_NONE
17237 && (TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
17238 && TREE_PUBLIC (decl)
17239 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
17240 && die->die_tag != DW_TAG_member)
17242 /* Defer until we have an assembler name set. */
17243 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
17245 limbo_die_node *asm_name;
17247 asm_name = ggc_cleared_alloc<limbo_die_node> ();
17248 asm_name->die = die;
17249 asm_name->created_for = decl;
17250 asm_name->next = deferred_asm_name;
17251 deferred_asm_name = asm_name;
17253 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
17254 add_linkage_attr (die, decl);
17258 /* Add a DW_AT_name attribute and source coordinate attribute for the
17259 given decl, but only if it actually has a name. */
17261 static void
17262 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
17264 tree decl_name;
17266 decl_name = DECL_NAME (decl);
17267 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
17269 const char *name = dwarf2_name (decl, 0);
17270 if (name)
17271 add_name_attribute (die, name);
17272 if (! DECL_ARTIFICIAL (decl))
17273 add_src_coords_attributes (die, decl);
17275 add_linkage_name (die, decl);
17278 #ifdef VMS_DEBUGGING_INFO
17279 /* Get the function's name, as described by its RTL. This may be different
17280 from the DECL_NAME name used in the source file. */
17281 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
17283 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
17284 XEXP (DECL_RTL (decl), 0), false);
17285 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
17287 #endif /* VMS_DEBUGGING_INFO */
17290 #ifdef VMS_DEBUGGING_INFO
17291 /* Output the debug main pointer die for VMS */
17293 void
17294 dwarf2out_vms_debug_main_pointer (void)
17296 char label[MAX_ARTIFICIAL_LABEL_BYTES];
17297 dw_die_ref die;
17299 /* Allocate the VMS debug main subprogram die. */
17300 die = ggc_cleared_alloc<die_node> ();
17301 die->die_tag = DW_TAG_subprogram;
17302 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
17303 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
17304 current_function_funcdef_no);
17305 add_AT_lbl_id (die, DW_AT_entry_pc, label);
17307 /* Make it the first child of comp_unit_die (). */
17308 die->die_parent = comp_unit_die ();
17309 if (comp_unit_die ()->die_child)
17311 die->die_sib = comp_unit_die ()->die_child->die_sib;
17312 comp_unit_die ()->die_child->die_sib = die;
17314 else
17316 die->die_sib = die;
17317 comp_unit_die ()->die_child = die;
17320 #endif /* VMS_DEBUGGING_INFO */
17322 /* Push a new declaration scope. */
17324 static void
17325 push_decl_scope (tree scope)
17327 vec_safe_push (decl_scope_table, scope);
17330 /* Pop a declaration scope. */
17332 static inline void
17333 pop_decl_scope (void)
17335 decl_scope_table->pop ();
17338 /* walk_tree helper function for uses_local_type, below. */
17340 static tree
17341 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
17343 if (!TYPE_P (*tp))
17344 *walk_subtrees = 0;
17345 else
17347 tree name = TYPE_NAME (*tp);
17348 if (name && DECL_P (name) && decl_function_context (name))
17349 return *tp;
17351 return NULL_TREE;
17354 /* If TYPE involves a function-local type (including a local typedef to a
17355 non-local type), returns that type; otherwise returns NULL_TREE. */
17357 static tree
17358 uses_local_type (tree type)
17360 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
17361 return used;
17364 /* Return the DIE for the scope that immediately contains this type.
17365 Non-named types that do not involve a function-local type get global
17366 scope. Named types nested in namespaces or other types get their
17367 containing scope. All other types (i.e. function-local named types) get
17368 the current active scope. */
17370 static dw_die_ref
17371 scope_die_for (tree t, dw_die_ref context_die)
17373 dw_die_ref scope_die = NULL;
17374 tree containing_scope;
17376 /* Non-types always go in the current scope. */
17377 gcc_assert (TYPE_P (t));
17379 /* Use the scope of the typedef, rather than the scope of the type
17380 it refers to. */
17381 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
17382 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
17383 else
17384 containing_scope = TYPE_CONTEXT (t);
17386 /* Use the containing namespace if there is one. */
17387 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
17389 if (context_die == lookup_decl_die (containing_scope))
17390 /* OK */;
17391 else if (debug_info_level > DINFO_LEVEL_TERSE)
17392 context_die = get_context_die (containing_scope);
17393 else
17394 containing_scope = NULL_TREE;
17397 /* Ignore function type "scopes" from the C frontend. They mean that
17398 a tagged type is local to a parmlist of a function declarator, but
17399 that isn't useful to DWARF. */
17400 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
17401 containing_scope = NULL_TREE;
17403 if (SCOPE_FILE_SCOPE_P (containing_scope))
17405 /* If T uses a local type keep it local as well, to avoid references
17406 to function-local DIEs from outside the function. */
17407 if (current_function_decl && uses_local_type (t))
17408 scope_die = context_die;
17409 else
17410 scope_die = comp_unit_die ();
17412 else if (TYPE_P (containing_scope))
17414 /* For types, we can just look up the appropriate DIE. */
17415 if (debug_info_level > DINFO_LEVEL_TERSE)
17416 scope_die = get_context_die (containing_scope);
17417 else
17419 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
17420 if (scope_die == NULL)
17421 scope_die = comp_unit_die ();
17424 else
17425 scope_die = context_die;
17427 return scope_die;
17430 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
17432 static inline int
17433 local_scope_p (dw_die_ref context_die)
17435 for (; context_die; context_die = context_die->die_parent)
17436 if (context_die->die_tag == DW_TAG_inlined_subroutine
17437 || context_die->die_tag == DW_TAG_subprogram)
17438 return 1;
17440 return 0;
17443 /* Returns nonzero if CONTEXT_DIE is a class. */
17445 static inline int
17446 class_scope_p (dw_die_ref context_die)
17448 return (context_die
17449 && (context_die->die_tag == DW_TAG_structure_type
17450 || context_die->die_tag == DW_TAG_class_type
17451 || context_die->die_tag == DW_TAG_interface_type
17452 || context_die->die_tag == DW_TAG_union_type));
17455 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
17456 whether or not to treat a DIE in this context as a declaration. */
17458 static inline int
17459 class_or_namespace_scope_p (dw_die_ref context_die)
17461 return (class_scope_p (context_die)
17462 || (context_die && context_die->die_tag == DW_TAG_namespace));
17465 /* Many forms of DIEs require a "type description" attribute. This
17466 routine locates the proper "type descriptor" die for the type given
17467 by 'type' plus any additional qualifiers given by 'cv_quals', and
17468 adds a DW_AT_type attribute below the given die. */
17470 static void
17471 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
17472 dw_die_ref context_die)
17474 enum tree_code code = TREE_CODE (type);
17475 dw_die_ref type_die = NULL;
17477 /* ??? If this type is an unnamed subrange type of an integral, floating-point
17478 or fixed-point type, use the inner type. This is because we have no
17479 support for unnamed types in base_type_die. This can happen if this is
17480 an Ada subrange type. Correct solution is emit a subrange type die. */
17481 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
17482 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
17483 type = TREE_TYPE (type), code = TREE_CODE (type);
17485 if (code == ERROR_MARK
17486 /* Handle a special case. For functions whose return type is void, we
17487 generate *no* type attribute. (Note that no object may have type
17488 `void', so this only applies to function return types). */
17489 || code == VOID_TYPE)
17490 return;
17492 type_die = modified_type_die (type,
17493 cv_quals | TYPE_QUALS_NO_ADDR_SPACE (type),
17494 context_die);
17496 if (type_die != NULL)
17497 add_AT_die_ref (object_die, DW_AT_type, type_die);
17500 /* Given an object die, add the calling convention attribute for the
17501 function call type. */
17502 static void
17503 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
17505 enum dwarf_calling_convention value = DW_CC_normal;
17507 value = ((enum dwarf_calling_convention)
17508 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
17510 if (is_fortran ()
17511 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
17513 /* DWARF 2 doesn't provide a way to identify a program's source-level
17514 entry point. DW_AT_calling_convention attributes are only meant
17515 to describe functions' calling conventions. However, lacking a
17516 better way to signal the Fortran main program, we used this for
17517 a long time, following existing custom. Now, DWARF 4 has
17518 DW_AT_main_subprogram, which we add below, but some tools still
17519 rely on the old way, which we thus keep. */
17520 value = DW_CC_program;
17522 if (dwarf_version >= 4 || !dwarf_strict)
17523 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
17526 /* Only add the attribute if the backend requests it, and
17527 is not DW_CC_normal. */
17528 if (value && (value != DW_CC_normal))
17529 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
17532 /* Given a tree pointer to a struct, class, union, or enum type node, return
17533 a pointer to the (string) tag name for the given type, or zero if the type
17534 was declared without a tag. */
17536 static const char *
17537 type_tag (const_tree type)
17539 const char *name = 0;
17541 if (TYPE_NAME (type) != 0)
17543 tree t = 0;
17545 /* Find the IDENTIFIER_NODE for the type name. */
17546 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
17547 && !TYPE_NAMELESS (type))
17548 t = TYPE_NAME (type);
17550 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
17551 a TYPE_DECL node, regardless of whether or not a `typedef' was
17552 involved. */
17553 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
17554 && ! DECL_IGNORED_P (TYPE_NAME (type)))
17556 /* We want to be extra verbose. Don't call dwarf_name if
17557 DECL_NAME isn't set. The default hook for decl_printable_name
17558 doesn't like that, and in this context it's correct to return
17559 0, instead of "<anonymous>" or the like. */
17560 if (DECL_NAME (TYPE_NAME (type))
17561 && !DECL_NAMELESS (TYPE_NAME (type)))
17562 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
17565 /* Now get the name as a string, or invent one. */
17566 if (!name && t != 0)
17567 name = IDENTIFIER_POINTER (t);
17570 return (name == 0 || *name == '\0') ? 0 : name;
17573 /* Return the type associated with a data member, make a special check
17574 for bit field types. */
17576 static inline tree
17577 member_declared_type (const_tree member)
17579 return (DECL_BIT_FIELD_TYPE (member)
17580 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
17583 /* Get the decl's label, as described by its RTL. This may be different
17584 from the DECL_NAME name used in the source file. */
17586 #if 0
17587 static const char *
17588 decl_start_label (tree decl)
17590 rtx x;
17591 const char *fnname;
17593 x = DECL_RTL (decl);
17594 gcc_assert (MEM_P (x));
17596 x = XEXP (x, 0);
17597 gcc_assert (GET_CODE (x) == SYMBOL_REF);
17599 fnname = XSTR (x, 0);
17600 return fnname;
17602 #endif
17604 /* For variable-length arrays that have been previously generated, but
17605 may be incomplete due to missing subscript info, fill the subscript
17606 info. Return TRUE if this is one of those cases. */
17607 static bool
17608 fill_variable_array_bounds (tree type)
17610 if (TREE_ASM_WRITTEN (type)
17611 && TREE_CODE (type) == ARRAY_TYPE
17612 && variably_modified_type_p (type, NULL))
17614 dw_die_ref array_die = lookup_type_die (type);
17615 if (!array_die)
17616 return false;
17617 add_subscript_info (array_die, type, !is_ada ());
17618 return true;
17620 return false;
17623 /* These routines generate the internal representation of the DIE's for
17624 the compilation unit. Debugging information is collected by walking
17625 the declaration trees passed in from dwarf2out_decl(). */
17627 static void
17628 gen_array_type_die (tree type, dw_die_ref context_die)
17630 dw_die_ref array_die;
17632 /* GNU compilers represent multidimensional array types as sequences of one
17633 dimensional array types whose element types are themselves array types.
17634 We sometimes squish that down to a single array_type DIE with multiple
17635 subscripts in the Dwarf debugging info. The draft Dwarf specification
17636 say that we are allowed to do this kind of compression in C, because
17637 there is no difference between an array of arrays and a multidimensional
17638 array. We don't do this for Ada to remain as close as possible to the
17639 actual representation, which is especially important against the language
17640 flexibilty wrt arrays of variable size. */
17642 bool collapse_nested_arrays = !is_ada ();
17644 if (fill_variable_array_bounds (type))
17645 return;
17647 dw_die_ref scope_die = scope_die_for (type, context_die);
17648 tree element_type;
17650 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
17651 DW_TAG_string_type doesn't have DW_AT_type attribute). */
17652 if (TYPE_STRING_FLAG (type)
17653 && TREE_CODE (type) == ARRAY_TYPE
17654 && is_fortran ()
17655 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
17657 HOST_WIDE_INT size;
17659 array_die = new_die (DW_TAG_string_type, scope_die, type);
17660 add_name_attribute (array_die, type_tag (type));
17661 equate_type_number_to_die (type, array_die);
17662 size = int_size_in_bytes (type);
17663 if (size >= 0)
17664 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17665 else if (TYPE_DOMAIN (type) != NULL_TREE
17666 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
17667 && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
17669 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
17670 dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2, NULL);
17672 size = int_size_in_bytes (TREE_TYPE (szdecl));
17673 if (loc && size > 0)
17675 add_AT_location_description (array_die, DW_AT_string_length, loc);
17676 if (size != DWARF2_ADDR_SIZE)
17677 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17680 return;
17683 array_die = new_die (DW_TAG_array_type, scope_die, type);
17684 add_name_attribute (array_die, type_tag (type));
17685 equate_type_number_to_die (type, array_die);
17687 if (TREE_CODE (type) == VECTOR_TYPE)
17688 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
17690 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
17691 if (is_fortran ()
17692 && TREE_CODE (type) == ARRAY_TYPE
17693 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
17694 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
17695 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17697 #if 0
17698 /* We default the array ordering. SDB will probably do
17699 the right things even if DW_AT_ordering is not present. It's not even
17700 an issue until we start to get into multidimensional arrays anyway. If
17701 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
17702 then we'll have to put the DW_AT_ordering attribute back in. (But if
17703 and when we find out that we need to put these in, we will only do so
17704 for multidimensional arrays. */
17705 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17706 #endif
17708 if (TREE_CODE (type) == VECTOR_TYPE)
17710 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
17711 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
17712 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node, NULL);
17713 add_bound_info (subrange_die, DW_AT_upper_bound,
17714 size_int (TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
17716 else
17717 add_subscript_info (array_die, type, collapse_nested_arrays);
17719 /* Add representation of the type of the elements of this array type and
17720 emit the corresponding DIE if we haven't done it already. */
17721 element_type = TREE_TYPE (type);
17722 if (collapse_nested_arrays)
17723 while (TREE_CODE (element_type) == ARRAY_TYPE)
17725 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
17726 break;
17727 element_type = TREE_TYPE (element_type);
17730 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED, context_die);
17732 add_gnat_descriptive_type_attribute (array_die, type, context_die);
17733 if (TYPE_ARTIFICIAL (type))
17734 add_AT_flag (array_die, DW_AT_artificial, 1);
17736 if (get_AT (array_die, DW_AT_name))
17737 add_pubtype (type, array_die);
17740 /* This routine generates DIE for array with hidden descriptor, details
17741 are filled into *info by a langhook. */
17743 static void
17744 gen_descr_array_type_die (tree type, struct array_descr_info *info,
17745 dw_die_ref context_die)
17747 const dw_die_ref scope_die = scope_die_for (type, context_die);
17748 const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
17749 const struct loc_descr_context context = { type, info->base_decl };
17750 int dim;
17752 add_name_attribute (array_die, type_tag (type));
17753 equate_type_number_to_die (type, array_die);
17755 if (info->ndimensions > 1)
17756 switch (info->ordering)
17758 case array_descr_ordering_row_major:
17759 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17760 break;
17761 case array_descr_ordering_column_major:
17762 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17763 break;
17764 default:
17765 break;
17768 if (dwarf_version >= 3 || !dwarf_strict)
17770 if (info->data_location)
17771 add_scalar_info (array_die, DW_AT_data_location, info->data_location,
17772 dw_scalar_form_exprloc, &context);
17773 if (info->associated)
17774 add_scalar_info (array_die, DW_AT_associated, info->associated,
17775 dw_scalar_form_constant
17776 | dw_scalar_form_exprloc
17777 | dw_scalar_form_reference, &context);
17778 if (info->allocated)
17779 add_scalar_info (array_die, DW_AT_allocated, info->allocated,
17780 dw_scalar_form_constant
17781 | dw_scalar_form_exprloc
17782 | dw_scalar_form_reference, &context);
17785 add_gnat_descriptive_type_attribute (array_die, type, context_die);
17787 for (dim = 0; dim < info->ndimensions; dim++)
17789 dw_die_ref subrange_die
17790 = new_die (DW_TAG_subrange_type, array_die, NULL);
17792 if (info->dimen[dim].bounds_type)
17793 add_type_attribute (subrange_die,
17794 info->dimen[dim].bounds_type, 0,
17795 context_die);
17796 if (info->dimen[dim].lower_bound)
17797 add_bound_info (subrange_die, DW_AT_lower_bound,
17798 info->dimen[dim].lower_bound, &context);
17799 if (info->dimen[dim].upper_bound)
17800 add_bound_info (subrange_die, DW_AT_upper_bound,
17801 info->dimen[dim].upper_bound, &context);
17802 if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
17803 add_scalar_info (subrange_die, DW_AT_byte_stride,
17804 info->dimen[dim].stride,
17805 dw_scalar_form_constant
17806 | dw_scalar_form_exprloc
17807 | dw_scalar_form_reference,
17808 &context);
17811 gen_type_die (info->element_type, context_die);
17812 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
17813 context_die);
17815 if (get_AT (array_die, DW_AT_name))
17816 add_pubtype (type, array_die);
17819 #if 0
17820 static void
17821 gen_entry_point_die (tree decl, dw_die_ref context_die)
17823 tree origin = decl_ultimate_origin (decl);
17824 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
17826 if (origin != NULL)
17827 add_abstract_origin_attribute (decl_die, origin);
17828 else
17830 add_name_and_src_coords_attributes (decl_die, decl);
17831 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
17832 TYPE_UNQUALIFIED, context_die);
17835 if (DECL_ABSTRACT_P (decl))
17836 equate_decl_number_to_die (decl, decl_die);
17837 else
17838 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
17840 #endif
17842 /* Walk through the list of incomplete types again, trying once more to
17843 emit full debugging info for them. */
17845 static void
17846 retry_incomplete_types (void)
17848 int i;
17850 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
17851 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
17852 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
17855 /* Determine what tag to use for a record type. */
17857 static enum dwarf_tag
17858 record_type_tag (tree type)
17860 if (! lang_hooks.types.classify_record)
17861 return DW_TAG_structure_type;
17863 switch (lang_hooks.types.classify_record (type))
17865 case RECORD_IS_STRUCT:
17866 return DW_TAG_structure_type;
17868 case RECORD_IS_CLASS:
17869 return DW_TAG_class_type;
17871 case RECORD_IS_INTERFACE:
17872 if (dwarf_version >= 3 || !dwarf_strict)
17873 return DW_TAG_interface_type;
17874 return DW_TAG_structure_type;
17876 default:
17877 gcc_unreachable ();
17881 /* Generate a DIE to represent an enumeration type. Note that these DIEs
17882 include all of the information about the enumeration values also. Each
17883 enumerated type name/value is listed as a child of the enumerated type
17884 DIE. */
17886 static dw_die_ref
17887 gen_enumeration_type_die (tree type, dw_die_ref context_die)
17889 dw_die_ref type_die = lookup_type_die (type);
17891 if (type_die == NULL)
17893 type_die = new_die (DW_TAG_enumeration_type,
17894 scope_die_for (type, context_die), type);
17895 equate_type_number_to_die (type, type_die);
17896 add_name_attribute (type_die, type_tag (type));
17897 if (dwarf_version >= 4 || !dwarf_strict)
17899 if (ENUM_IS_SCOPED (type))
17900 add_AT_flag (type_die, DW_AT_enum_class, 1);
17901 if (ENUM_IS_OPAQUE (type))
17902 add_AT_flag (type_die, DW_AT_declaration, 1);
17905 else if (! TYPE_SIZE (type))
17906 return type_die;
17907 else
17908 remove_AT (type_die, DW_AT_declaration);
17910 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
17911 given enum type is incomplete, do not generate the DW_AT_byte_size
17912 attribute or the DW_AT_element_list attribute. */
17913 if (TYPE_SIZE (type))
17915 tree link;
17917 TREE_ASM_WRITTEN (type) = 1;
17918 add_byte_size_attribute (type_die, type);
17919 if (dwarf_version >= 3 || !dwarf_strict)
17921 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
17922 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED,
17923 context_die);
17925 if (TYPE_STUB_DECL (type) != NULL_TREE)
17927 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
17928 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
17931 /* If the first reference to this type was as the return type of an
17932 inline function, then it may not have a parent. Fix this now. */
17933 if (type_die->die_parent == NULL)
17934 add_child_die (scope_die_for (type, context_die), type_die);
17936 for (link = TYPE_VALUES (type);
17937 link != NULL; link = TREE_CHAIN (link))
17939 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
17940 tree value = TREE_VALUE (link);
17942 add_name_attribute (enum_die,
17943 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
17945 if (TREE_CODE (value) == CONST_DECL)
17946 value = DECL_INITIAL (value);
17948 if (simple_type_size_in_bits (TREE_TYPE (value))
17949 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
17951 /* For constant forms created by add_AT_unsigned DWARF
17952 consumers (GDB, elfutils, etc.) always zero extend
17953 the value. Only when the actual value is negative
17954 do we need to use add_AT_int to generate a constant
17955 form that can represent negative values. */
17956 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
17957 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
17958 add_AT_unsigned (enum_die, DW_AT_const_value,
17959 (unsigned HOST_WIDE_INT) val);
17960 else
17961 add_AT_int (enum_die, DW_AT_const_value, val);
17963 else
17964 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
17965 that here. TODO: This should be re-worked to use correct
17966 signed/unsigned double tags for all cases. */
17967 add_AT_wide (enum_die, DW_AT_const_value, value);
17970 add_gnat_descriptive_type_attribute (type_die, type, context_die);
17971 if (TYPE_ARTIFICIAL (type))
17972 add_AT_flag (type_die, DW_AT_artificial, 1);
17974 else
17975 add_AT_flag (type_die, DW_AT_declaration, 1);
17977 add_pubtype (type, type_die);
17979 return type_die;
17982 /* Generate a DIE to represent either a real live formal parameter decl or to
17983 represent just the type of some formal parameter position in some function
17984 type.
17986 Note that this routine is a bit unusual because its argument may be a
17987 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
17988 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
17989 node. If it's the former then this function is being called to output a
17990 DIE to represent a formal parameter object (or some inlining thereof). If
17991 it's the latter, then this function is only being called to output a
17992 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
17993 argument type of some subprogram type.
17994 If EMIT_NAME_P is true, name and source coordinate attributes
17995 are emitted. */
17997 static dw_die_ref
17998 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
17999 dw_die_ref context_die)
18001 tree node_or_origin = node ? node : origin;
18002 tree ultimate_origin;
18003 dw_die_ref parm_die = NULL;
18005 if (TREE_CODE_CLASS (TREE_CODE (node_or_origin)) == tcc_declaration)
18007 parm_die = lookup_decl_die (node);
18009 /* If the contexts differ, we may not be talking about the same
18010 thing. */
18011 if (parm_die && parm_die->die_parent != context_die)
18013 if (!DECL_ABSTRACT_P (node))
18015 /* This can happen when creating an inlined instance, in
18016 which case we need to create a new DIE that will get
18017 annotated with DW_AT_abstract_origin. */
18018 parm_die = NULL;
18020 else
18022 /* FIXME: Reuse DIE even with a differing context.
18024 This can happen when calling
18025 dwarf2out_abstract_function to build debug info for
18026 the abstract instance of a function for which we have
18027 already generated a DIE in
18028 dwarf2out_early_global_decl.
18030 Once we remove dwarf2out_abstract_function, we should
18031 have a call to gcc_unreachable here. */
18035 if (parm_die && parm_die->die_parent == NULL)
18037 /* Check that parm_die already has the right attributes that
18038 we would have added below. If any attributes are
18039 missing, fall through to add them. */
18040 if (! DECL_ABSTRACT_P (node_or_origin)
18041 && !get_AT (parm_die, DW_AT_location)
18042 && !get_AT (parm_die, DW_AT_const_value))
18043 /* We are missing location info, and are about to add it. */
18045 else
18047 add_child_die (context_die, parm_die);
18048 return parm_die;
18053 /* If we have a previously generated DIE, use it, unless this is an
18054 concrete instance (origin != NULL), in which case we need a new
18055 DIE with a corresponding DW_AT_abstract_origin. */
18056 bool reusing_die;
18057 if (parm_die && origin == NULL)
18058 reusing_die = true;
18059 else
18061 parm_die = new_die (DW_TAG_formal_parameter, context_die, node);
18062 reusing_die = false;
18065 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
18067 case tcc_declaration:
18068 ultimate_origin = decl_ultimate_origin (node_or_origin);
18069 if (node || ultimate_origin)
18070 origin = ultimate_origin;
18072 if (reusing_die)
18073 goto add_location;
18075 if (origin != NULL)
18076 add_abstract_origin_attribute (parm_die, origin);
18077 else if (emit_name_p)
18078 add_name_and_src_coords_attributes (parm_die, node);
18079 if (origin == NULL
18080 || (! DECL_ABSTRACT_P (node_or_origin)
18081 && variably_modified_type_p (TREE_TYPE (node_or_origin),
18082 decl_function_context
18083 (node_or_origin))))
18085 tree type = TREE_TYPE (node_or_origin);
18086 if (decl_by_reference_p (node_or_origin))
18087 add_type_attribute (parm_die, TREE_TYPE (type),
18088 TYPE_UNQUALIFIED, context_die);
18089 else
18090 add_type_attribute (parm_die, type,
18091 decl_quals (node_or_origin),
18092 context_die);
18094 if (origin == NULL && DECL_ARTIFICIAL (node))
18095 add_AT_flag (parm_die, DW_AT_artificial, 1);
18096 add_location:
18097 if (node && node != origin)
18098 equate_decl_number_to_die (node, parm_die);
18099 if (! DECL_ABSTRACT_P (node_or_origin))
18100 add_location_or_const_value_attribute (parm_die, node_or_origin,
18101 node == NULL, DW_AT_location);
18103 break;
18105 case tcc_type:
18106 /* We were called with some kind of a ..._TYPE node. */
18107 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED,
18108 context_die);
18109 break;
18111 default:
18112 gcc_unreachable ();
18115 return parm_die;
18118 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
18119 children DW_TAG_formal_parameter DIEs representing the arguments of the
18120 parameter pack.
18122 PARM_PACK must be a function parameter pack.
18123 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
18124 must point to the subsequent arguments of the function PACK_ARG belongs to.
18125 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
18126 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
18127 following the last one for which a DIE was generated. */
18129 static dw_die_ref
18130 gen_formal_parameter_pack_die (tree parm_pack,
18131 tree pack_arg,
18132 dw_die_ref subr_die,
18133 tree *next_arg)
18135 tree arg;
18136 dw_die_ref parm_pack_die;
18138 gcc_assert (parm_pack
18139 && lang_hooks.function_parameter_pack_p (parm_pack)
18140 && subr_die);
18142 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
18143 add_src_coords_attributes (parm_pack_die, parm_pack);
18145 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
18147 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
18148 parm_pack))
18149 break;
18150 gen_formal_parameter_die (arg, NULL,
18151 false /* Don't emit name attribute. */,
18152 parm_pack_die);
18154 if (next_arg)
18155 *next_arg = arg;
18156 return parm_pack_die;
18159 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
18160 at the end of an (ANSI prototyped) formal parameters list. */
18162 static void
18163 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
18165 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
18168 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
18169 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
18170 parameters as specified in some function type specification (except for
18171 those which appear as part of a function *definition*). */
18173 static void
18174 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
18176 tree link;
18177 tree formal_type = NULL;
18178 tree first_parm_type;
18179 tree arg;
18181 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
18183 arg = DECL_ARGUMENTS (function_or_method_type);
18184 function_or_method_type = TREE_TYPE (function_or_method_type);
18186 else
18187 arg = NULL_TREE;
18189 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
18191 /* Make our first pass over the list of formal parameter types and output a
18192 DW_TAG_formal_parameter DIE for each one. */
18193 for (link = first_parm_type; link; )
18195 dw_die_ref parm_die;
18197 formal_type = TREE_VALUE (link);
18198 if (formal_type == void_type_node)
18199 break;
18201 /* Output a (nameless) DIE to represent the formal parameter itself. */
18202 if (!POINTER_BOUNDS_TYPE_P (formal_type))
18204 parm_die = gen_formal_parameter_die (formal_type, NULL,
18205 true /* Emit name attribute. */,
18206 context_die);
18207 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
18208 && link == first_parm_type)
18210 add_AT_flag (parm_die, DW_AT_artificial, 1);
18211 if (dwarf_version >= 3 || !dwarf_strict)
18212 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
18214 else if (arg && DECL_ARTIFICIAL (arg))
18215 add_AT_flag (parm_die, DW_AT_artificial, 1);
18218 link = TREE_CHAIN (link);
18219 if (arg)
18220 arg = DECL_CHAIN (arg);
18223 /* If this function type has an ellipsis, add a
18224 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
18225 if (formal_type != void_type_node)
18226 gen_unspecified_parameters_die (function_or_method_type, context_die);
18228 /* Make our second (and final) pass over the list of formal parameter types
18229 and output DIEs to represent those types (as necessary). */
18230 for (link = TYPE_ARG_TYPES (function_or_method_type);
18231 link && TREE_VALUE (link);
18232 link = TREE_CHAIN (link))
18233 gen_type_die (TREE_VALUE (link), context_die);
18236 /* We want to generate the DIE for TYPE so that we can generate the
18237 die for MEMBER, which has been defined; we will need to refer back
18238 to the member declaration nested within TYPE. If we're trying to
18239 generate minimal debug info for TYPE, processing TYPE won't do the
18240 trick; we need to attach the member declaration by hand. */
18242 static void
18243 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
18245 gen_type_die (type, context_die);
18247 /* If we're trying to avoid duplicate debug info, we may not have
18248 emitted the member decl for this function. Emit it now. */
18249 if (TYPE_STUB_DECL (type)
18250 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
18251 && ! lookup_decl_die (member))
18253 dw_die_ref type_die;
18254 gcc_assert (!decl_ultimate_origin (member));
18256 push_decl_scope (type);
18257 type_die = lookup_type_die_strip_naming_typedef (type);
18258 if (TREE_CODE (member) == FUNCTION_DECL)
18259 gen_subprogram_die (member, type_die);
18260 else if (TREE_CODE (member) == FIELD_DECL)
18262 /* Ignore the nameless fields that are used to skip bits but handle
18263 C++ anonymous unions and structs. */
18264 if (DECL_NAME (member) != NULL_TREE
18265 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
18266 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
18268 gen_type_die (member_declared_type (member), type_die);
18269 gen_field_die (member, type_die);
18272 else
18273 gen_variable_die (member, NULL_TREE, type_die);
18275 pop_decl_scope ();
18279 /* Forward declare these functions, because they are mutually recursive
18280 with their set_block_* pairing functions. */
18281 static void set_decl_origin_self (tree);
18282 static void set_decl_abstract_flags (tree, vec<tree> &);
18284 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
18285 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
18286 that it points to the node itself, thus indicating that the node is its
18287 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
18288 the given node is NULL, recursively descend the decl/block tree which
18289 it is the root of, and for each other ..._DECL or BLOCK node contained
18290 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
18291 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
18292 values to point to themselves. */
18294 static void
18295 set_block_origin_self (tree stmt)
18297 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
18299 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
18302 tree local_decl;
18304 for (local_decl = BLOCK_VARS (stmt);
18305 local_decl != NULL_TREE;
18306 local_decl = DECL_CHAIN (local_decl))
18307 /* Do not recurse on nested functions since the inlining status
18308 of parent and child can be different as per the DWARF spec. */
18309 if (TREE_CODE (local_decl) != FUNCTION_DECL
18310 && !DECL_EXTERNAL (local_decl))
18311 set_decl_origin_self (local_decl);
18315 tree subblock;
18317 for (subblock = BLOCK_SUBBLOCKS (stmt);
18318 subblock != NULL_TREE;
18319 subblock = BLOCK_CHAIN (subblock))
18320 set_block_origin_self (subblock); /* Recurse. */
18325 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
18326 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
18327 node to so that it points to the node itself, thus indicating that the
18328 node represents its own (abstract) origin. Additionally, if the
18329 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
18330 the decl/block tree of which the given node is the root of, and for
18331 each other ..._DECL or BLOCK node contained therein whose
18332 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
18333 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
18334 point to themselves. */
18336 static void
18337 set_decl_origin_self (tree decl)
18339 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
18341 DECL_ABSTRACT_ORIGIN (decl) = decl;
18342 if (TREE_CODE (decl) == FUNCTION_DECL)
18344 tree arg;
18346 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
18347 DECL_ABSTRACT_ORIGIN (arg) = arg;
18348 if (DECL_INITIAL (decl) != NULL_TREE
18349 && DECL_INITIAL (decl) != error_mark_node)
18350 set_block_origin_self (DECL_INITIAL (decl));
18355 /* Given a pointer to some BLOCK node, set the BLOCK_ABSTRACT flag to 1
18356 and if it wasn't 1 before, push it to abstract_vec vector.
18357 For all local decls and all local sub-blocks (recursively) do it
18358 too. */
18360 static void
18361 set_block_abstract_flags (tree stmt, vec<tree> &abstract_vec)
18363 tree local_decl;
18364 tree subblock;
18365 unsigned int i;
18367 if (!BLOCK_ABSTRACT (stmt))
18369 abstract_vec.safe_push (stmt);
18370 BLOCK_ABSTRACT (stmt) = 1;
18373 for (local_decl = BLOCK_VARS (stmt);
18374 local_decl != NULL_TREE;
18375 local_decl = DECL_CHAIN (local_decl))
18376 if (! DECL_EXTERNAL (local_decl))
18377 set_decl_abstract_flags (local_decl, abstract_vec);
18379 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
18381 local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
18382 if ((TREE_CODE (local_decl) == VAR_DECL && !TREE_STATIC (local_decl))
18383 || TREE_CODE (local_decl) == PARM_DECL)
18384 set_decl_abstract_flags (local_decl, abstract_vec);
18387 for (subblock = BLOCK_SUBBLOCKS (stmt);
18388 subblock != NULL_TREE;
18389 subblock = BLOCK_CHAIN (subblock))
18390 set_block_abstract_flags (subblock, abstract_vec);
18393 /* Given a pointer to some ..._DECL node, set DECL_ABSTRACT_P flag on it
18394 to 1 and if it wasn't 1 before, push to abstract_vec vector.
18395 In the case where the decl is a FUNCTION_DECL also set the abstract
18396 flags for all of the parameters, local vars, local
18397 blocks and sub-blocks (recursively). */
18399 static void
18400 set_decl_abstract_flags (tree decl, vec<tree> &abstract_vec)
18402 if (!DECL_ABSTRACT_P (decl))
18404 abstract_vec.safe_push (decl);
18405 DECL_ABSTRACT_P (decl) = 1;
18408 if (TREE_CODE (decl) == FUNCTION_DECL)
18410 tree arg;
18412 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
18413 if (!DECL_ABSTRACT_P (arg))
18415 abstract_vec.safe_push (arg);
18416 DECL_ABSTRACT_P (arg) = 1;
18418 if (DECL_INITIAL (decl) != NULL_TREE
18419 && DECL_INITIAL (decl) != error_mark_node)
18420 set_block_abstract_flags (DECL_INITIAL (decl), abstract_vec);
18424 /* Generate the DWARF2 info for the "abstract" instance of a function which we
18425 may later generate inlined and/or out-of-line instances of.
18427 FIXME: In the early-dwarf world, this function, and most of the
18428 DECL_ABSTRACT code should be obsoleted. The early DIE _is_
18429 the abstract instance. All we would need to do is annotate
18430 the early DIE with the appropriate DW_AT_inline in late
18431 dwarf (perhaps in gen_inlined_subroutine_die).
18433 However, we can't do this yet, because LTO streaming of DIEs
18434 has not been implemented yet. */
18436 static void
18437 dwarf2out_abstract_function (tree decl)
18439 dw_die_ref old_die;
18440 tree save_fn;
18441 tree context;
18442 hash_table<decl_loc_hasher> *old_decl_loc_table;
18443 hash_table<dw_loc_list_hasher> *old_cached_dw_loc_list_table;
18444 int old_call_site_count, old_tail_call_site_count;
18445 struct call_arg_loc_node *old_call_arg_locations;
18447 /* Make sure we have the actual abstract inline, not a clone. */
18448 decl = DECL_ORIGIN (decl);
18450 old_die = lookup_decl_die (decl);
18451 if (old_die && get_AT (old_die, DW_AT_inline))
18452 /* We've already generated the abstract instance. */
18453 return;
18455 /* We can be called while recursively when seeing block defining inlined subroutine
18456 DIE. Be sure to not clobber the outer location table nor use it or we would
18457 get locations in abstract instantces. */
18458 old_decl_loc_table = decl_loc_table;
18459 decl_loc_table = NULL;
18460 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
18461 cached_dw_loc_list_table = NULL;
18462 old_call_arg_locations = call_arg_locations;
18463 call_arg_locations = NULL;
18464 old_call_site_count = call_site_count;
18465 call_site_count = -1;
18466 old_tail_call_site_count = tail_call_site_count;
18467 tail_call_site_count = -1;
18469 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
18470 we don't get confused by DECL_ABSTRACT_P. */
18471 if (debug_info_level > DINFO_LEVEL_TERSE)
18473 context = decl_class_context (decl);
18474 if (context)
18475 gen_type_die_for_member
18476 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
18479 /* Pretend we've just finished compiling this function. */
18480 save_fn = current_function_decl;
18481 current_function_decl = decl;
18483 auto_vec<tree, 64> abstract_vec;
18484 set_decl_abstract_flags (decl, abstract_vec);
18485 dwarf2out_decl (decl);
18486 unsigned int i;
18487 tree t;
18488 FOR_EACH_VEC_ELT (abstract_vec, i, t)
18489 if (TREE_CODE (t) == BLOCK)
18490 BLOCK_ABSTRACT (t) = 0;
18491 else
18492 DECL_ABSTRACT_P (t) = 0;
18494 current_function_decl = save_fn;
18495 decl_loc_table = old_decl_loc_table;
18496 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
18497 call_arg_locations = old_call_arg_locations;
18498 call_site_count = old_call_site_count;
18499 tail_call_site_count = old_tail_call_site_count;
18502 /* Helper function of premark_used_types() which gets called through
18503 htab_traverse.
18505 Marks the DIE of a given type in *SLOT as perennial, so it never gets
18506 marked as unused by prune_unused_types. */
18508 bool
18509 premark_used_types_helper (tree const &type, void *)
18511 dw_die_ref die;
18513 die = lookup_type_die (type);
18514 if (die != NULL)
18515 die->die_perennial_p = 1;
18516 return true;
18519 /* Helper function of premark_types_used_by_global_vars which gets called
18520 through htab_traverse.
18522 Marks the DIE of a given type in *SLOT as perennial, so it never gets
18523 marked as unused by prune_unused_types. The DIE of the type is marked
18524 only if the global variable using the type will actually be emitted. */
18527 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
18528 void *)
18530 struct types_used_by_vars_entry *entry;
18531 dw_die_ref die;
18533 entry = (struct types_used_by_vars_entry *) *slot;
18534 gcc_assert (entry->type != NULL
18535 && entry->var_decl != NULL);
18536 die = lookup_type_die (entry->type);
18537 if (die)
18539 /* Ask cgraph if the global variable really is to be emitted.
18540 If yes, then we'll keep the DIE of ENTRY->TYPE. */
18541 varpool_node *node = varpool_node::get (entry->var_decl);
18542 if (node && node->definition)
18544 die->die_perennial_p = 1;
18545 /* Keep the parent DIEs as well. */
18546 while ((die = die->die_parent) && die->die_perennial_p == 0)
18547 die->die_perennial_p = 1;
18550 return 1;
18553 /* Mark all members of used_types_hash as perennial. */
18555 static void
18556 premark_used_types (struct function *fun)
18558 if (fun && fun->used_types_hash)
18559 fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
18562 /* Mark all members of types_used_by_vars_entry as perennial. */
18564 static void
18565 premark_types_used_by_global_vars (void)
18567 if (types_used_by_vars_hash)
18568 types_used_by_vars_hash
18569 ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
18572 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
18573 for CA_LOC call arg loc node. */
18575 static dw_die_ref
18576 gen_call_site_die (tree decl, dw_die_ref subr_die,
18577 struct call_arg_loc_node *ca_loc)
18579 dw_die_ref stmt_die = NULL, die;
18580 tree block = ca_loc->block;
18582 while (block
18583 && block != DECL_INITIAL (decl)
18584 && TREE_CODE (block) == BLOCK)
18586 stmt_die = BLOCK_DIE (block);
18587 if (stmt_die)
18588 break;
18589 block = BLOCK_SUPERCONTEXT (block);
18591 if (stmt_die == NULL)
18592 stmt_die = subr_die;
18593 die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
18594 add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
18595 if (ca_loc->tail_call_p)
18596 add_AT_flag (die, DW_AT_GNU_tail_call, 1);
18597 if (ca_loc->symbol_ref)
18599 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
18600 if (tdie)
18601 add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
18602 else
18603 add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref, false);
18605 return die;
18608 /* Generate a DIE to represent a declared function (either file-scope or
18609 block-local). */
18611 static void
18612 gen_subprogram_die (tree decl, dw_die_ref context_die)
18614 tree origin = decl_ultimate_origin (decl);
18615 dw_die_ref subr_die;
18616 dw_die_ref old_die = lookup_decl_die (decl);
18618 /* This function gets called multiple times for different stages of
18619 the debug process. For example, for func() in this code:
18621 namespace S
18623 void func() { ... }
18626 ...we get called 4 times. Twice in early debug and twice in
18627 late debug:
18629 Early debug
18630 -----------
18632 1. Once while generating func() within the namespace. This is
18633 the declaration. The declaration bit below is set, as the
18634 context is the namespace.
18636 A new DIE will be generated with DW_AT_declaration set.
18638 2. Once for func() itself. This is the specification. The
18639 declaration bit below is clear as the context is the CU.
18641 We will use the cached DIE from (1) to create a new DIE with
18642 DW_AT_specification pointing to the declaration in (1).
18644 Late debug via rest_of_handle_final()
18645 -------------------------------------
18647 3. Once generating func() within the namespace. This is also the
18648 declaration, as in (1), but this time we will early exit below
18649 as we have a cached DIE and a declaration needs no additional
18650 annotations (no locations), as the source declaration line
18651 info is enough.
18653 4. Once for func() itself. As in (2), this is the specification,
18654 but this time we will re-use the cached DIE, and just annotate
18655 it with the location information that should now be available.
18657 For something without namespaces, but with abstract instances, we
18658 are also called a multiple times:
18660 class Base
18662 public:
18663 Base (); // constructor declaration (1)
18666 Base::Base () { } // constructor specification (2)
18668 Early debug
18669 -----------
18671 1. Once for the Base() constructor by virtue of it being a
18672 member of the Base class. This is done via
18673 rest_of_type_compilation.
18675 This is a declaration, so a new DIE will be created with
18676 DW_AT_declaration.
18678 2. Once for the Base() constructor definition, but this time
18679 while generating the abstract instance of the base
18680 constructor (__base_ctor) which is being generated via early
18681 debug of reachable functions.
18683 Even though we have a cached version of the declaration (1),
18684 we will create a DW_AT_specification of the declaration DIE
18685 in (1).
18687 3. Once for the __base_ctor itself, but this time, we generate
18688 an DW_AT_abstract_origin version of the DW_AT_specification in
18689 (2).
18691 Late debug via rest_of_handle_final
18692 -----------------------------------
18694 4. One final time for the __base_ctor (which will have a cached
18695 DIE with DW_AT_abstract_origin created in (3). This time,
18696 we will just annotate the location information now
18697 available.
18699 int declaration = (current_function_decl != decl
18700 || class_or_namespace_scope_p (context_die));
18702 premark_used_types (DECL_STRUCT_FUNCTION (decl));
18704 /* Now that the C++ front end lazily declares artificial member fns, we
18705 might need to retrofit the declaration into its class. */
18706 if (!declaration && !origin && !old_die
18707 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
18708 && !class_or_namespace_scope_p (context_die)
18709 && debug_info_level > DINFO_LEVEL_TERSE)
18710 old_die = force_decl_die (decl);
18712 /* An inlined instance, tag a new DIE with DW_AT_abstract_origin. */
18713 if (origin != NULL)
18715 gcc_assert (!declaration || local_scope_p (context_die));
18717 /* Fixup die_parent for the abstract instance of a nested
18718 inline function. */
18719 if (old_die && old_die->die_parent == NULL)
18720 add_child_die (context_die, old_die);
18722 if (old_die && get_AT_ref (old_die, DW_AT_abstract_origin))
18724 /* If we have a DW_AT_abstract_origin we have a working
18725 cached version. */
18726 subr_die = old_die;
18728 else
18730 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18731 add_abstract_origin_attribute (subr_die, origin);
18732 /* This is where the actual code for a cloned function is.
18733 Let's emit linkage name attribute for it. This helps
18734 debuggers to e.g, set breakpoints into
18735 constructors/destructors when the user asks "break
18736 K::K". */
18737 add_linkage_name (subr_die, decl);
18740 /* A cached copy, possibly from early dwarf generation. Reuse as
18741 much as possible. */
18742 else if (old_die)
18744 /* A declaration that has been previously dumped needs no
18745 additional information. */
18746 if (declaration)
18747 return;
18749 if (!get_AT_flag (old_die, DW_AT_declaration)
18750 /* We can have a normal definition following an inline one in the
18751 case of redefinition of GNU C extern inlines.
18752 It seems reasonable to use AT_specification in this case. */
18753 && !get_AT (old_die, DW_AT_inline))
18755 /* Detect and ignore this case, where we are trying to output
18756 something we have already output. */
18757 if (get_AT (old_die, DW_AT_low_pc)
18758 || get_AT (old_die, DW_AT_ranges))
18759 return;
18761 /* If we have no location information, this must be a
18762 partially generated DIE from early dwarf generation.
18763 Fall through and generate it. */
18766 /* If the definition comes from the same place as the declaration,
18767 maybe use the old DIE. We always want the DIE for this function
18768 that has the *_pc attributes to be under comp_unit_die so the
18769 debugger can find it. We also need to do this for abstract
18770 instances of inlines, since the spec requires the out-of-line copy
18771 to have the same parent. For local class methods, this doesn't
18772 apply; we just use the old DIE. */
18773 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18774 struct dwarf_file_data * file_index = lookup_filename (s.file);
18775 if ((is_cu_die (old_die->die_parent)
18776 /* This condition fixes the inconsistency/ICE with the
18777 following Fortran test (or some derivative thereof) while
18778 building libgfortran:
18780 module some_m
18781 contains
18782 logical function funky (FLAG)
18783 funky = .true.
18784 end function
18785 end module
18787 || (old_die->die_parent
18788 && old_die->die_parent->die_tag == DW_TAG_module)
18789 || context_die == NULL)
18790 && (DECL_ARTIFICIAL (decl)
18791 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
18792 && (get_AT_unsigned (old_die, DW_AT_decl_line)
18793 == (unsigned) s.line))))
18795 subr_die = old_die;
18797 /* Clear out the declaration attribute, but leave the
18798 parameters so they can be augmented with location
18799 information later. Unless this was a declaration, in
18800 which case, wipe out the nameless parameters and recreate
18801 them further down. */
18802 if (remove_AT (subr_die, DW_AT_declaration))
18805 remove_AT (subr_die, DW_AT_object_pointer);
18806 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
18809 /* Make a specification pointing to the previously built
18810 declaration. */
18811 else
18813 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18814 add_AT_specification (subr_die, old_die);
18815 add_pubname (decl, subr_die);
18816 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18817 add_AT_file (subr_die, DW_AT_decl_file, file_index);
18818 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18819 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
18821 /* If the prototype had an 'auto' or 'decltype(auto)' return type,
18822 emit the real type on the definition die. */
18823 if (is_cxx() && debug_info_level > DINFO_LEVEL_TERSE)
18825 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
18826 if (die == auto_die || die == decltype_auto_die)
18827 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18828 TYPE_UNQUALIFIED, context_die);
18832 /* Create a fresh DIE for anything else. */
18833 else
18835 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18837 if (TREE_PUBLIC (decl))
18838 add_AT_flag (subr_die, DW_AT_external, 1);
18840 add_name_and_src_coords_attributes (subr_die, decl);
18841 add_pubname (decl, subr_die);
18842 if (debug_info_level > DINFO_LEVEL_TERSE)
18844 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
18845 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18846 TYPE_UNQUALIFIED, context_die);
18849 add_pure_or_virtual_attribute (subr_die, decl);
18850 if (DECL_ARTIFICIAL (decl))
18851 add_AT_flag (subr_die, DW_AT_artificial, 1);
18853 if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
18854 add_AT_flag (subr_die, DW_AT_noreturn, 1);
18856 add_accessibility_attribute (subr_die, decl);
18859 /* Unless we have an existing non-declaration DIE, equate the new
18860 DIE. */
18861 if (!old_die || is_declaration_die (old_die))
18862 equate_decl_number_to_die (decl, subr_die);
18864 if (declaration)
18866 if (!old_die || !get_AT (old_die, DW_AT_inline))
18868 add_AT_flag (subr_die, DW_AT_declaration, 1);
18870 /* If this is an explicit function declaration then generate
18871 a DW_AT_explicit attribute. */
18872 if (lang_hooks.decls.function_decl_explicit_p (decl)
18873 && (dwarf_version >= 3 || !dwarf_strict))
18874 add_AT_flag (subr_die, DW_AT_explicit, 1);
18876 /* If this is a C++11 deleted special function member then generate
18877 a DW_AT_GNU_deleted attribute. */
18878 if (lang_hooks.decls.function_decl_deleted_p (decl)
18879 && (! dwarf_strict))
18880 add_AT_flag (subr_die, DW_AT_GNU_deleted, 1);
18883 /* Tag abstract instances with DW_AT_inline. */
18884 else if (DECL_ABSTRACT_P (decl))
18886 if (DECL_DECLARED_INLINE_P (decl))
18888 if (cgraph_function_possibly_inlined_p (decl))
18889 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
18890 else
18891 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
18893 else
18895 if (cgraph_function_possibly_inlined_p (decl))
18896 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
18897 else
18898 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
18901 if (DECL_DECLARED_INLINE_P (decl)
18902 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
18903 add_AT_flag (subr_die, DW_AT_artificial, 1);
18905 /* For non DECL_EXTERNALs, if range information is available, fill
18906 the DIE with it. */
18907 else if (!DECL_EXTERNAL (decl) && !early_dwarf)
18909 HOST_WIDE_INT cfa_fb_offset;
18911 struct function *fun = DECL_STRUCT_FUNCTION (decl);
18913 if (!flag_reorder_blocks_and_partition)
18915 dw_fde_ref fde = fun->fde;
18916 if (fde->dw_fde_begin)
18918 /* We have already generated the labels. */
18919 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18920 fde->dw_fde_end, false);
18922 else
18924 /* Create start/end labels and add the range. */
18925 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
18926 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
18927 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
18928 current_function_funcdef_no);
18929 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
18930 current_function_funcdef_no);
18931 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
18932 false);
18935 #if VMS_DEBUGGING_INFO
18936 /* HP OpenVMS Industry Standard 64: DWARF Extensions
18937 Section 2.3 Prologue and Epilogue Attributes:
18938 When a breakpoint is set on entry to a function, it is generally
18939 desirable for execution to be suspended, not on the very first
18940 instruction of the function, but rather at a point after the
18941 function's frame has been set up, after any language defined local
18942 declaration processing has been completed, and before execution of
18943 the first statement of the function begins. Debuggers generally
18944 cannot properly determine where this point is. Similarly for a
18945 breakpoint set on exit from a function. The prologue and epilogue
18946 attributes allow a compiler to communicate the location(s) to use. */
18949 if (fde->dw_fde_vms_end_prologue)
18950 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
18951 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
18953 if (fde->dw_fde_vms_begin_epilogue)
18954 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
18955 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
18957 #endif
18960 else
18962 /* Generate pubnames entries for the split function code ranges. */
18963 dw_fde_ref fde = fun->fde;
18965 if (fde->dw_fde_second_begin)
18967 if (dwarf_version >= 3 || !dwarf_strict)
18969 /* We should use ranges for non-contiguous code section
18970 addresses. Use the actual code range for the initial
18971 section, since the HOT/COLD labels might precede an
18972 alignment offset. */
18973 bool range_list_added = false;
18974 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
18975 fde->dw_fde_end, &range_list_added,
18976 false);
18977 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
18978 fde->dw_fde_second_end,
18979 &range_list_added, false);
18980 if (range_list_added)
18981 add_ranges (NULL);
18983 else
18985 /* There is no real support in DW2 for this .. so we make
18986 a work-around. First, emit the pub name for the segment
18987 containing the function label. Then make and emit a
18988 simplified subprogram DIE for the second segment with the
18989 name pre-fixed by __hot/cold_sect_of_. We use the same
18990 linkage name for the second die so that gdb will find both
18991 sections when given "b foo". */
18992 const char *name = NULL;
18993 tree decl_name = DECL_NAME (decl);
18994 dw_die_ref seg_die;
18996 /* Do the 'primary' section. */
18997 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18998 fde->dw_fde_end, false);
19000 /* Build a minimal DIE for the secondary section. */
19001 seg_die = new_die (DW_TAG_subprogram,
19002 subr_die->die_parent, decl);
19004 if (TREE_PUBLIC (decl))
19005 add_AT_flag (seg_die, DW_AT_external, 1);
19007 if (decl_name != NULL
19008 && IDENTIFIER_POINTER (decl_name) != NULL)
19010 name = dwarf2_name (decl, 1);
19011 if (! DECL_ARTIFICIAL (decl))
19012 add_src_coords_attributes (seg_die, decl);
19014 add_linkage_name (seg_die, decl);
19016 gcc_assert (name != NULL);
19017 add_pure_or_virtual_attribute (seg_die, decl);
19018 if (DECL_ARTIFICIAL (decl))
19019 add_AT_flag (seg_die, DW_AT_artificial, 1);
19021 name = concat ("__second_sect_of_", name, NULL);
19022 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
19023 fde->dw_fde_second_end, false);
19024 add_name_attribute (seg_die, name);
19025 if (want_pubnames ())
19026 add_pubname_string (name, seg_die);
19029 else
19030 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
19031 false);
19034 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
19036 /* We define the "frame base" as the function's CFA. This is more
19037 convenient for several reasons: (1) It's stable across the prologue
19038 and epilogue, which makes it better than just a frame pointer,
19039 (2) With dwarf3, there exists a one-byte encoding that allows us
19040 to reference the .debug_frame data by proxy, but failing that,
19041 (3) We can at least reuse the code inspection and interpretation
19042 code that determines the CFA position at various points in the
19043 function. */
19044 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
19046 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
19047 add_AT_loc (subr_die, DW_AT_frame_base, op);
19049 else
19051 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
19052 if (list->dw_loc_next)
19053 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
19054 else
19055 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
19058 /* Compute a displacement from the "steady-state frame pointer" to
19059 the CFA. The former is what all stack slots and argument slots
19060 will reference in the rtl; the latter is what we've told the
19061 debugger about. We'll need to adjust all frame_base references
19062 by this displacement. */
19063 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
19065 if (fun->static_chain_decl)
19066 add_AT_location_description
19067 (subr_die, DW_AT_static_link,
19068 loc_list_from_tree (fun->static_chain_decl, 2, NULL));
19071 /* Generate child dies for template paramaters. */
19072 if (early_dwarf && debug_info_level > DINFO_LEVEL_TERSE)
19073 gen_generic_params_dies (decl);
19075 /* Now output descriptions of the arguments for this function. This gets
19076 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
19077 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
19078 `...' at the end of the formal parameter list. In order to find out if
19079 there was a trailing ellipsis or not, we must instead look at the type
19080 associated with the FUNCTION_DECL. This will be a node of type
19081 FUNCTION_TYPE. If the chain of type nodes hanging off of this
19082 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
19083 an ellipsis at the end. */
19085 /* In the case where we are describing a mere function declaration, all we
19086 need to do here (and all we *can* do here) is to describe the *types* of
19087 its formal parameters. */
19088 if (debug_info_level <= DINFO_LEVEL_TERSE)
19090 else if (declaration)
19091 gen_formal_types_die (decl, subr_die);
19092 else
19094 /* Generate DIEs to represent all known formal parameters. */
19095 tree parm = DECL_ARGUMENTS (decl);
19096 tree generic_decl = early_dwarf
19097 ? lang_hooks.decls.get_generic_function_decl (decl) : NULL;
19098 tree generic_decl_parm = generic_decl
19099 ? DECL_ARGUMENTS (generic_decl)
19100 : NULL;
19102 /* Now we want to walk the list of parameters of the function and
19103 emit their relevant DIEs.
19105 We consider the case of DECL being an instance of a generic function
19106 as well as it being a normal function.
19108 If DECL is an instance of a generic function we walk the
19109 parameters of the generic function declaration _and_ the parameters of
19110 DECL itself. This is useful because we want to emit specific DIEs for
19111 function parameter packs and those are declared as part of the
19112 generic function declaration. In that particular case,
19113 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
19114 That DIE has children DIEs representing the set of arguments
19115 of the pack. Note that the set of pack arguments can be empty.
19116 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
19117 children DIE.
19119 Otherwise, we just consider the parameters of DECL. */
19120 while (generic_decl_parm || parm)
19122 if (generic_decl_parm
19123 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
19124 gen_formal_parameter_pack_die (generic_decl_parm,
19125 parm, subr_die,
19126 &parm);
19127 else if (parm && !POINTER_BOUNDS_P (parm))
19129 dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
19131 if (parm == DECL_ARGUMENTS (decl)
19132 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
19133 && parm_die
19134 && (dwarf_version >= 3 || !dwarf_strict))
19135 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
19137 parm = DECL_CHAIN (parm);
19139 else if (parm)
19140 parm = DECL_CHAIN (parm);
19142 if (generic_decl_parm)
19143 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
19146 /* Decide whether we need an unspecified_parameters DIE at the end.
19147 There are 2 more cases to do this for: 1) the ansi ... declaration -
19148 this is detectable when the end of the arg list is not a
19149 void_type_node 2) an unprototyped function declaration (not a
19150 definition). This just means that we have no info about the
19151 parameters at all. */
19152 if (prototype_p (TREE_TYPE (decl)))
19154 /* This is the prototyped case, check for.... */
19155 if (stdarg_p (TREE_TYPE (decl)))
19156 gen_unspecified_parameters_die (decl, subr_die);
19158 else if (DECL_INITIAL (decl) == NULL_TREE)
19159 gen_unspecified_parameters_die (decl, subr_die);
19162 if (subr_die != old_die)
19163 /* Add the calling convention attribute if requested. */
19164 add_calling_convention_attribute (subr_die, decl);
19166 /* Output Dwarf info for all of the stuff within the body of the function
19167 (if it has one - it may be just a declaration).
19169 OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
19170 a function. This BLOCK actually represents the outermost binding contour
19171 for the function, i.e. the contour in which the function's formal
19172 parameters and labels get declared. Curiously, it appears that the front
19173 end doesn't actually put the PARM_DECL nodes for the current function onto
19174 the BLOCK_VARS list for this outer scope, but are strung off of the
19175 DECL_ARGUMENTS list for the function instead.
19177 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
19178 the LABEL_DECL nodes for the function however, and we output DWARF info
19179 for those in decls_for_scope. Just within the `outer_scope' there will be
19180 a BLOCK node representing the function's outermost pair of curly braces,
19181 and any blocks used for the base and member initializers of a C++
19182 constructor function. */
19183 tree outer_scope = DECL_INITIAL (decl);
19184 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
19186 int call_site_note_count = 0;
19187 int tail_call_site_note_count = 0;
19189 /* Emit a DW_TAG_variable DIE for a named return value. */
19190 if (DECL_NAME (DECL_RESULT (decl)))
19191 gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
19193 /* The first time through decls_for_scope we will generate the
19194 DIEs for the locals. The second time, we fill in the
19195 location info. */
19196 decls_for_scope (outer_scope, subr_die);
19198 if (call_arg_locations && !dwarf_strict)
19200 struct call_arg_loc_node *ca_loc;
19201 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
19203 dw_die_ref die = NULL;
19204 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
19205 rtx arg, next_arg;
19207 for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
19208 arg; arg = next_arg)
19210 dw_loc_descr_ref reg, val;
19211 machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
19212 dw_die_ref cdie, tdie = NULL;
19214 next_arg = XEXP (arg, 1);
19215 if (REG_P (XEXP (XEXP (arg, 0), 0))
19216 && next_arg
19217 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
19218 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
19219 && REGNO (XEXP (XEXP (arg, 0), 0))
19220 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
19221 next_arg = XEXP (next_arg, 1);
19222 if (mode == VOIDmode)
19224 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
19225 if (mode == VOIDmode)
19226 mode = GET_MODE (XEXP (arg, 0));
19228 if (mode == VOIDmode || mode == BLKmode)
19229 continue;
19230 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
19232 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
19233 tloc = XEXP (XEXP (arg, 0), 1);
19234 continue;
19236 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
19237 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
19239 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
19240 tlocc = XEXP (XEXP (arg, 0), 1);
19241 continue;
19243 reg = NULL;
19244 if (REG_P (XEXP (XEXP (arg, 0), 0)))
19245 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
19246 VAR_INIT_STATUS_INITIALIZED);
19247 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
19249 rtx mem = XEXP (XEXP (arg, 0), 0);
19250 reg = mem_loc_descriptor (XEXP (mem, 0),
19251 get_address_mode (mem),
19252 GET_MODE (mem),
19253 VAR_INIT_STATUS_INITIALIZED);
19255 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
19256 == DEBUG_PARAMETER_REF)
19258 tree tdecl
19259 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
19260 tdie = lookup_decl_die (tdecl);
19261 if (tdie == NULL)
19262 continue;
19264 else
19265 continue;
19266 if (reg == NULL
19267 && GET_CODE (XEXP (XEXP (arg, 0), 0))
19268 != DEBUG_PARAMETER_REF)
19269 continue;
19270 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
19271 VOIDmode,
19272 VAR_INIT_STATUS_INITIALIZED);
19273 if (val == NULL)
19274 continue;
19275 if (die == NULL)
19276 die = gen_call_site_die (decl, subr_die, ca_loc);
19277 cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
19278 NULL_TREE);
19279 if (reg != NULL)
19280 add_AT_loc (cdie, DW_AT_location, reg);
19281 else if (tdie != NULL)
19282 add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
19283 add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
19284 if (next_arg != XEXP (arg, 1))
19286 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
19287 if (mode == VOIDmode)
19288 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
19289 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
19290 0), 1),
19291 mode, VOIDmode,
19292 VAR_INIT_STATUS_INITIALIZED);
19293 if (val != NULL)
19294 add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
19297 if (die == NULL
19298 && (ca_loc->symbol_ref || tloc))
19299 die = gen_call_site_die (decl, subr_die, ca_loc);
19300 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
19302 dw_loc_descr_ref tval = NULL;
19304 if (tloc != NULL_RTX)
19305 tval = mem_loc_descriptor (tloc,
19306 GET_MODE (tloc) == VOIDmode
19307 ? Pmode : GET_MODE (tloc),
19308 VOIDmode,
19309 VAR_INIT_STATUS_INITIALIZED);
19310 if (tval)
19311 add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
19312 else if (tlocc != NULL_RTX)
19314 tval = mem_loc_descriptor (tlocc,
19315 GET_MODE (tlocc) == VOIDmode
19316 ? Pmode : GET_MODE (tlocc),
19317 VOIDmode,
19318 VAR_INIT_STATUS_INITIALIZED);
19319 if (tval)
19320 add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
19321 tval);
19324 if (die != NULL)
19326 call_site_note_count++;
19327 if (ca_loc->tail_call_p)
19328 tail_call_site_note_count++;
19332 call_arg_locations = NULL;
19333 call_arg_loc_last = NULL;
19334 if (tail_call_site_count >= 0
19335 && tail_call_site_count == tail_call_site_note_count
19336 && !dwarf_strict)
19338 if (call_site_count >= 0
19339 && call_site_count == call_site_note_count)
19340 add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
19341 else
19342 add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
19344 call_site_count = -1;
19345 tail_call_site_count = -1;
19349 /* Returns a hash value for X (which really is a die_struct). */
19351 hashval_t
19352 block_die_hasher::hash (die_struct *d)
19354 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
19357 /* Return nonzero if decl_id and die_parent of die_struct X is the same
19358 as decl_id and die_parent of die_struct Y. */
19360 bool
19361 block_die_hasher::equal (die_struct *x, die_struct *y)
19363 return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
19366 /* Return TRUE if DECL, which may have been previously generated as
19367 OLD_DIE, is a candidate for a DW_AT_specification. DECLARATION is
19368 true if decl (or its origin) is either an extern declaration or a
19369 class/namespace scoped declaration.
19371 The declare_in_namespace support causes us to get two DIEs for one
19372 variable, both of which are declarations. We want to avoid
19373 considering one to be a specification, so we must test for
19374 DECLARATION and DW_AT_declaration. */
19375 static inline bool
19376 decl_will_get_specification_p (dw_die_ref old_die, tree decl, bool declaration)
19378 return (old_die && TREE_STATIC (decl) && !declaration
19379 && get_AT_flag (old_die, DW_AT_declaration) == 1);
19382 /* Return true if DECL is a local static. */
19384 static inline bool
19385 local_function_static (tree decl)
19387 gcc_assert (TREE_CODE (decl) == VAR_DECL);
19388 return TREE_STATIC (decl)
19389 && DECL_CONTEXT (decl)
19390 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL;
19393 /* Generate a DIE to represent a declared data object.
19394 Either DECL or ORIGIN must be non-null. */
19396 static void
19397 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
19399 HOST_WIDE_INT off = 0;
19400 tree com_decl;
19401 tree decl_or_origin = decl ? decl : origin;
19402 tree ultimate_origin;
19403 dw_die_ref var_die;
19404 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
19405 dw_die_ref origin_die = NULL;
19406 bool declaration = (DECL_EXTERNAL (decl_or_origin)
19407 || class_or_namespace_scope_p (context_die));
19408 bool specialization_p = false;
19410 ultimate_origin = decl_ultimate_origin (decl_or_origin);
19411 if (decl || ultimate_origin)
19412 origin = ultimate_origin;
19413 com_decl = fortran_common (decl_or_origin, &off);
19415 /* Symbol in common gets emitted as a child of the common block, in the form
19416 of a data member. */
19417 if (com_decl)
19419 dw_die_ref com_die;
19420 dw_loc_list_ref loc;
19421 die_node com_die_arg;
19423 var_die = lookup_decl_die (decl_or_origin);
19424 if (var_die)
19426 if (get_AT (var_die, DW_AT_location) == NULL)
19428 loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
19429 if (loc)
19431 if (off)
19433 /* Optimize the common case. */
19434 if (single_element_loc_list_p (loc)
19435 && loc->expr->dw_loc_opc == DW_OP_addr
19436 && loc->expr->dw_loc_next == NULL
19437 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
19438 == SYMBOL_REF)
19440 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
19441 loc->expr->dw_loc_oprnd1.v.val_addr
19442 = plus_constant (GET_MODE (x), x , off);
19444 else
19445 loc_list_plus_const (loc, off);
19447 add_AT_location_description (var_die, DW_AT_location, loc);
19448 remove_AT (var_die, DW_AT_declaration);
19451 return;
19454 if (common_block_die_table == NULL)
19455 common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
19457 com_die_arg.decl_id = DECL_UID (com_decl);
19458 com_die_arg.die_parent = context_die;
19459 com_die = common_block_die_table->find (&com_die_arg);
19460 loc = loc_list_from_tree (com_decl, 2, NULL);
19461 if (com_die == NULL)
19463 const char *cnam
19464 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
19465 die_node **slot;
19467 com_die = new_die (DW_TAG_common_block, context_die, decl);
19468 add_name_and_src_coords_attributes (com_die, com_decl);
19469 if (loc)
19471 add_AT_location_description (com_die, DW_AT_location, loc);
19472 /* Avoid sharing the same loc descriptor between
19473 DW_TAG_common_block and DW_TAG_variable. */
19474 loc = loc_list_from_tree (com_decl, 2, NULL);
19476 else if (DECL_EXTERNAL (decl))
19477 add_AT_flag (com_die, DW_AT_declaration, 1);
19478 if (want_pubnames ())
19479 add_pubname_string (cnam, com_die); /* ??? needed? */
19480 com_die->decl_id = DECL_UID (com_decl);
19481 slot = common_block_die_table->find_slot (com_die, INSERT);
19482 *slot = com_die;
19484 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
19486 add_AT_location_description (com_die, DW_AT_location, loc);
19487 loc = loc_list_from_tree (com_decl, 2, NULL);
19488 remove_AT (com_die, DW_AT_declaration);
19490 var_die = new_die (DW_TAG_variable, com_die, decl);
19491 add_name_and_src_coords_attributes (var_die, decl);
19492 add_type_attribute (var_die, TREE_TYPE (decl), decl_quals (decl),
19493 context_die);
19494 add_AT_flag (var_die, DW_AT_external, 1);
19495 if (loc)
19497 if (off)
19499 /* Optimize the common case. */
19500 if (single_element_loc_list_p (loc)
19501 && loc->expr->dw_loc_opc == DW_OP_addr
19502 && loc->expr->dw_loc_next == NULL
19503 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
19505 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
19506 loc->expr->dw_loc_oprnd1.v.val_addr
19507 = plus_constant (GET_MODE (x), x, off);
19509 else
19510 loc_list_plus_const (loc, off);
19512 add_AT_location_description (var_die, DW_AT_location, loc);
19514 else if (DECL_EXTERNAL (decl))
19515 add_AT_flag (var_die, DW_AT_declaration, 1);
19516 equate_decl_number_to_die (decl, var_die);
19517 return;
19520 if (old_die)
19522 if (declaration)
19524 /* A declaration that has been previously dumped, needs no
19525 further annotations, since it doesn't need location on
19526 the second pass. */
19527 return;
19529 else if (decl_will_get_specification_p (old_die, decl, declaration)
19530 && !get_AT (old_die, DW_AT_specification))
19532 /* Fall-thru so we can make a new variable die along with a
19533 DW_AT_specification. */
19535 else if (origin && old_die->die_parent != context_die)
19537 /* If we will be creating an inlined instance, we need a
19538 new DIE that will get annotated with
19539 DW_AT_abstract_origin. Clear things so we can get a
19540 new DIE. */
19541 gcc_assert (!DECL_ABSTRACT_P (decl));
19542 old_die = NULL;
19544 else
19546 /* If a DIE was dumped early, it still needs location info.
19547 Skip to where we fill the location bits. */
19548 var_die = old_die;
19549 goto gen_variable_die_location;
19553 /* For static data members, the declaration in the class is supposed
19554 to have DW_TAG_member tag; the specification should still be
19555 DW_TAG_variable referencing the DW_TAG_member DIE. */
19556 if (declaration && class_scope_p (context_die))
19557 var_die = new_die (DW_TAG_member, context_die, decl);
19558 else
19559 var_die = new_die (DW_TAG_variable, context_die, decl);
19561 if (origin != NULL)
19562 origin_die = add_abstract_origin_attribute (var_die, origin);
19564 /* Loop unrolling can create multiple blocks that refer to the same
19565 static variable, so we must test for the DW_AT_declaration flag.
19567 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
19568 copy decls and set the DECL_ABSTRACT_P flag on them instead of
19569 sharing them.
19571 ??? Duplicated blocks have been rewritten to use .debug_ranges. */
19572 else if (decl_will_get_specification_p (old_die, decl, declaration))
19574 /* This is a definition of a C++ class level static. */
19575 add_AT_specification (var_die, old_die);
19576 specialization_p = true;
19577 if (DECL_NAME (decl))
19579 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
19580 struct dwarf_file_data * file_index = lookup_filename (s.file);
19582 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
19583 add_AT_file (var_die, DW_AT_decl_file, file_index);
19585 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
19586 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
19588 if (old_die->die_tag == DW_TAG_member)
19589 add_linkage_name (var_die, decl);
19592 else
19593 add_name_and_src_coords_attributes (var_die, decl);
19595 if ((origin == NULL && !specialization_p)
19596 || (origin != NULL
19597 && !DECL_ABSTRACT_P (decl_or_origin)
19598 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
19599 decl_function_context
19600 (decl_or_origin))))
19602 tree type = TREE_TYPE (decl_or_origin);
19604 if (decl_by_reference_p (decl_or_origin))
19605 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19606 context_die);
19607 else
19608 add_type_attribute (var_die, type, decl_quals (decl_or_origin),
19609 context_die);
19612 if (origin == NULL && !specialization_p)
19614 if (TREE_PUBLIC (decl))
19615 add_AT_flag (var_die, DW_AT_external, 1);
19617 if (DECL_ARTIFICIAL (decl))
19618 add_AT_flag (var_die, DW_AT_artificial, 1);
19620 add_accessibility_attribute (var_die, decl);
19623 if (declaration)
19624 add_AT_flag (var_die, DW_AT_declaration, 1);
19626 if (decl && (DECL_ABSTRACT_P (decl)
19627 || !old_die || is_declaration_die (old_die)))
19628 equate_decl_number_to_die (decl, var_die);
19630 gen_variable_die_location:
19631 if (! declaration
19632 && (! DECL_ABSTRACT_P (decl_or_origin)
19633 /* Local static vars are shared between all clones/inlines,
19634 so emit DW_AT_location on the abstract DIE if DECL_RTL is
19635 already set. */
19636 || (TREE_CODE (decl_or_origin) == VAR_DECL
19637 && TREE_STATIC (decl_or_origin)
19638 && DECL_RTL_SET_P (decl_or_origin)))
19639 /* When abstract origin already has DW_AT_location attribute, no need
19640 to add it again. */
19641 && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
19643 if (early_dwarf)
19644 add_pubname (decl_or_origin, var_die);
19645 else
19646 add_location_or_const_value_attribute (var_die, decl_or_origin,
19647 decl == NULL, DW_AT_location);
19649 else
19650 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
19653 /* Generate a DIE to represent a named constant. */
19655 static void
19656 gen_const_die (tree decl, dw_die_ref context_die)
19658 dw_die_ref const_die;
19659 tree type = TREE_TYPE (decl);
19661 const_die = lookup_decl_die (decl);
19662 if (const_die)
19663 return;
19665 const_die = new_die (DW_TAG_constant, context_die, decl);
19666 equate_decl_number_to_die (decl, const_die);
19667 add_name_and_src_coords_attributes (const_die, decl);
19668 add_type_attribute (const_die, type, TYPE_QUAL_CONST, context_die);
19669 if (TREE_PUBLIC (decl))
19670 add_AT_flag (const_die, DW_AT_external, 1);
19671 if (DECL_ARTIFICIAL (decl))
19672 add_AT_flag (const_die, DW_AT_artificial, 1);
19673 tree_add_const_value_attribute_for_decl (const_die, decl);
19676 /* Generate a DIE to represent a label identifier. */
19678 static void
19679 gen_label_die (tree decl, dw_die_ref context_die)
19681 tree origin = decl_ultimate_origin (decl);
19682 dw_die_ref lbl_die = lookup_decl_die (decl);
19683 rtx insn;
19684 char label[MAX_ARTIFICIAL_LABEL_BYTES];
19686 if (!lbl_die)
19688 lbl_die = new_die (DW_TAG_label, context_die, decl);
19689 equate_decl_number_to_die (decl, lbl_die);
19691 if (origin != NULL)
19692 add_abstract_origin_attribute (lbl_die, origin);
19693 else
19694 add_name_and_src_coords_attributes (lbl_die, decl);
19697 if (DECL_ABSTRACT_P (decl))
19698 equate_decl_number_to_die (decl, lbl_die);
19699 else
19701 insn = DECL_RTL_IF_SET (decl);
19703 /* Deleted labels are programmer specified labels which have been
19704 eliminated because of various optimizations. We still emit them
19705 here so that it is possible to put breakpoints on them. */
19706 if (insn
19707 && (LABEL_P (insn)
19708 || ((NOTE_P (insn)
19709 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
19711 /* When optimization is enabled (via -O) some parts of the compiler
19712 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
19713 represent source-level labels which were explicitly declared by
19714 the user. This really shouldn't be happening though, so catch
19715 it if it ever does happen. */
19716 gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
19718 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
19719 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19721 else if (insn
19722 && NOTE_P (insn)
19723 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
19724 && CODE_LABEL_NUMBER (insn) != -1)
19726 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
19727 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19732 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
19733 attributes to the DIE for a block STMT, to describe where the inlined
19734 function was called from. This is similar to add_src_coords_attributes. */
19736 static inline void
19737 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
19739 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
19741 if (dwarf_version >= 3 || !dwarf_strict)
19743 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
19744 add_AT_unsigned (die, DW_AT_call_line, s.line);
19749 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
19750 Add low_pc and high_pc attributes to the DIE for a block STMT. */
19752 static inline void
19753 add_high_low_attributes (tree stmt, dw_die_ref die)
19755 char label[MAX_ARTIFICIAL_LABEL_BYTES];
19757 if (BLOCK_FRAGMENT_CHAIN (stmt)
19758 && (dwarf_version >= 3 || !dwarf_strict))
19760 tree chain, superblock = NULL_TREE;
19761 dw_die_ref pdie;
19762 dw_attr_ref attr = NULL;
19764 if (inlined_function_outer_scope_p (stmt))
19766 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19767 BLOCK_NUMBER (stmt));
19768 add_AT_lbl_id (die, DW_AT_entry_pc, label);
19771 /* Optimize duplicate .debug_ranges lists or even tails of
19772 lists. If this BLOCK has same ranges as its supercontext,
19773 lookup DW_AT_ranges attribute in the supercontext (and
19774 recursively so), verify that the ranges_table contains the
19775 right values and use it instead of adding a new .debug_range. */
19776 for (chain = stmt, pdie = die;
19777 BLOCK_SAME_RANGE (chain);
19778 chain = BLOCK_SUPERCONTEXT (chain))
19780 dw_attr_ref new_attr;
19782 pdie = pdie->die_parent;
19783 if (pdie == NULL)
19784 break;
19785 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
19786 break;
19787 new_attr = get_AT (pdie, DW_AT_ranges);
19788 if (new_attr == NULL
19789 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
19790 break;
19791 attr = new_attr;
19792 superblock = BLOCK_SUPERCONTEXT (chain);
19794 if (attr != NULL
19795 && (ranges_table[attr->dw_attr_val.v.val_offset
19796 / 2 / DWARF2_ADDR_SIZE].num
19797 == BLOCK_NUMBER (superblock))
19798 && BLOCK_FRAGMENT_CHAIN (superblock))
19800 unsigned long off = attr->dw_attr_val.v.val_offset
19801 / 2 / DWARF2_ADDR_SIZE;
19802 unsigned long supercnt = 0, thiscnt = 0;
19803 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
19804 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
19806 ++supercnt;
19807 gcc_checking_assert (ranges_table[off + supercnt].num
19808 == BLOCK_NUMBER (chain));
19810 gcc_checking_assert (ranges_table[off + supercnt + 1].num == 0);
19811 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
19812 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
19813 ++thiscnt;
19814 gcc_assert (supercnt >= thiscnt);
19815 add_AT_range_list (die, DW_AT_ranges,
19816 ((off + supercnt - thiscnt)
19817 * 2 * DWARF2_ADDR_SIZE),
19818 false);
19819 return;
19822 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt), false);
19824 chain = BLOCK_FRAGMENT_CHAIN (stmt);
19827 add_ranges (chain);
19828 chain = BLOCK_FRAGMENT_CHAIN (chain);
19830 while (chain);
19831 add_ranges (NULL);
19833 else
19835 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
19836 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19837 BLOCK_NUMBER (stmt));
19838 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
19839 BLOCK_NUMBER (stmt));
19840 add_AT_low_high_pc (die, label, label_high, false);
19844 /* Generate a DIE for a lexical block. */
19846 static void
19847 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
19849 dw_die_ref old_die = BLOCK_DIE (stmt);
19850 dw_die_ref stmt_die = NULL;
19851 if (!old_die)
19853 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
19854 BLOCK_DIE (stmt) = stmt_die;
19857 if (BLOCK_ABSTRACT (stmt))
19859 if (old_die)
19861 #ifdef ENABLE_CHECKING
19862 /* This must have been generated early and it won't even
19863 need location information since it's a DW_AT_inline
19864 function. */
19865 for (dw_die_ref c = context_die; c; c = c->die_parent)
19866 if (c->die_tag == DW_TAG_inlined_subroutine
19867 || c->die_tag == DW_TAG_subprogram)
19869 gcc_assert (get_AT (c, DW_AT_inline));
19870 break;
19872 #endif
19873 return;
19876 else if (BLOCK_ABSTRACT_ORIGIN (stmt))
19878 /* If this is an inlined instance, create a new lexical die for
19879 anything below to attach DW_AT_abstract_origin to. */
19880 if (old_die)
19882 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
19883 BLOCK_DIE (stmt) = stmt_die;
19884 old_die = NULL;
19888 if (old_die)
19889 stmt_die = old_die;
19891 /* A non abstract block whose blocks have already been reordered
19892 should have the instruction range for this block. If so, set the
19893 high/low attributes. */
19894 if (!early_dwarf && !BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
19896 gcc_assert (stmt_die);
19897 add_high_low_attributes (stmt, stmt_die);
19900 decls_for_scope (stmt, stmt_die);
19903 /* Generate a DIE for an inlined subprogram. */
19905 static void
19906 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
19908 tree decl;
19910 /* The instance of function that is effectively being inlined shall not
19911 be abstract. */
19912 gcc_assert (! BLOCK_ABSTRACT (stmt));
19914 decl = block_ultimate_origin (stmt);
19916 /* Emit info for the abstract instance first, if we haven't yet. We
19917 must emit this even if the block is abstract, otherwise when we
19918 emit the block below (or elsewhere), we may end up trying to emit
19919 a die whose origin die hasn't been emitted, and crashing. */
19920 dwarf2out_abstract_function (decl);
19922 if (! BLOCK_ABSTRACT (stmt))
19924 dw_die_ref subr_die
19925 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
19927 if (call_arg_locations)
19928 BLOCK_DIE (stmt) = subr_die;
19929 add_abstract_origin_attribute (subr_die, decl);
19930 if (TREE_ASM_WRITTEN (stmt))
19931 add_high_low_attributes (stmt, subr_die);
19932 add_call_src_coords_attributes (stmt, subr_die);
19934 decls_for_scope (stmt, subr_die);
19938 /* Generate a DIE for a field in a record, or structure. */
19940 static void
19941 gen_field_die (tree decl, dw_die_ref context_die)
19943 dw_die_ref decl_die;
19945 if (TREE_TYPE (decl) == error_mark_node)
19946 return;
19948 decl_die = new_die (DW_TAG_member, context_die, decl);
19949 add_name_and_src_coords_attributes (decl_die, decl);
19950 add_type_attribute (decl_die, member_declared_type (decl),
19951 decl_quals (decl), context_die);
19953 if (DECL_BIT_FIELD_TYPE (decl))
19955 add_byte_size_attribute (decl_die, decl);
19956 add_bit_size_attribute (decl_die, decl);
19957 add_bit_offset_attribute (decl_die, decl);
19960 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
19961 add_data_member_location_attribute (decl_die, decl);
19963 if (DECL_ARTIFICIAL (decl))
19964 add_AT_flag (decl_die, DW_AT_artificial, 1);
19966 add_accessibility_attribute (decl_die, decl);
19968 /* Equate decl number to die, so that we can look up this decl later on. */
19969 equate_decl_number_to_die (decl, decl_die);
19972 #if 0
19973 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19974 Use modified_type_die instead.
19975 We keep this code here just in case these types of DIEs may be needed to
19976 represent certain things in other languages (e.g. Pascal) someday. */
19978 static void
19979 gen_pointer_type_die (tree type, dw_die_ref context_die)
19981 dw_die_ref ptr_die
19982 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
19984 equate_type_number_to_die (type, ptr_die);
19985 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19986 context_die);
19987 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19990 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19991 Use modified_type_die instead.
19992 We keep this code here just in case these types of DIEs may be needed to
19993 represent certain things in other languages (e.g. Pascal) someday. */
19995 static void
19996 gen_reference_type_die (tree type, dw_die_ref context_die)
19998 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
20000 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
20001 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
20002 else
20003 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
20005 equate_type_number_to_die (type, ref_die);
20006 add_type_attribute (ref_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
20007 context_die);
20008 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
20010 #endif
20012 /* Generate a DIE for a pointer to a member type. */
20014 static void
20015 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
20017 dw_die_ref ptr_die
20018 = new_die (DW_TAG_ptr_to_member_type,
20019 scope_die_for (type, context_die), type);
20021 equate_type_number_to_die (type, ptr_die);
20022 add_AT_die_ref (ptr_die, DW_AT_containing_type,
20023 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
20024 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
20025 context_die);
20028 typedef const char *dchar_p; /* For DEF_VEC_P. */
20030 static char *producer_string;
20032 /* Return a heap allocated producer string including command line options
20033 if -grecord-gcc-switches. */
20035 static char *
20036 gen_producer_string (void)
20038 size_t j;
20039 auto_vec<dchar_p> switches;
20040 const char *language_string = lang_hooks.name;
20041 char *producer, *tail;
20042 const char *p;
20043 size_t len = dwarf_record_gcc_switches ? 0 : 3;
20044 size_t plen = strlen (language_string) + 1 + strlen (version_string);
20046 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
20047 switch (save_decoded_options[j].opt_index)
20049 case OPT_o:
20050 case OPT_d:
20051 case OPT_dumpbase:
20052 case OPT_dumpdir:
20053 case OPT_auxbase:
20054 case OPT_auxbase_strip:
20055 case OPT_quiet:
20056 case OPT_version:
20057 case OPT_v:
20058 case OPT_w:
20059 case OPT_L:
20060 case OPT_D:
20061 case OPT_I:
20062 case OPT_U:
20063 case OPT_SPECIAL_unknown:
20064 case OPT_SPECIAL_ignore:
20065 case OPT_SPECIAL_program_name:
20066 case OPT_SPECIAL_input_file:
20067 case OPT_grecord_gcc_switches:
20068 case OPT_gno_record_gcc_switches:
20069 case OPT__output_pch_:
20070 case OPT_fdiagnostics_show_location_:
20071 case OPT_fdiagnostics_show_option:
20072 case OPT_fdiagnostics_show_caret:
20073 case OPT_fdiagnostics_color_:
20074 case OPT_fverbose_asm:
20075 case OPT____:
20076 case OPT__sysroot_:
20077 case OPT_nostdinc:
20078 case OPT_nostdinc__:
20079 case OPT_fpreprocessed:
20080 case OPT_fltrans_output_list_:
20081 case OPT_fresolution_:
20082 /* Ignore these. */
20083 continue;
20084 default:
20085 if (cl_options[save_decoded_options[j].opt_index].flags
20086 & CL_NO_DWARF_RECORD)
20087 continue;
20088 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
20089 == '-');
20090 switch (save_decoded_options[j].canonical_option[0][1])
20092 case 'M':
20093 case 'i':
20094 case 'W':
20095 continue;
20096 case 'f':
20097 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
20098 "dump", 4) == 0)
20099 continue;
20100 break;
20101 default:
20102 break;
20104 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
20105 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
20106 break;
20109 producer = XNEWVEC (char, plen + 1 + len + 1);
20110 tail = producer;
20111 sprintf (tail, "%s %s", language_string, version_string);
20112 tail += plen;
20114 FOR_EACH_VEC_ELT (switches, j, p)
20116 len = strlen (p);
20117 *tail = ' ';
20118 memcpy (tail + 1, p, len);
20119 tail += len + 1;
20122 *tail = '\0';
20123 return producer;
20126 /* Given a C and/or C++ language/version string return the "highest".
20127 C++ is assumed to be "higher" than C in this case. Used for merging
20128 LTO translation unit languages. */
20129 static const char *
20130 highest_c_language (const char *lang1, const char *lang2)
20132 if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
20133 return "GNU C++14";
20134 if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
20135 return "GNU C++11";
20136 if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
20137 return "GNU C++98";
20139 if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
20140 return "GNU C11";
20141 if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
20142 return "GNU C99";
20143 if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
20144 return "GNU C89";
20146 gcc_unreachable ();
20150 /* Generate the DIE for the compilation unit. */
20152 static dw_die_ref
20153 gen_compile_unit_die (const char *filename)
20155 dw_die_ref die;
20156 const char *language_string = lang_hooks.name;
20157 int language;
20159 die = new_die (DW_TAG_compile_unit, NULL, NULL);
20161 if (filename)
20163 add_name_attribute (die, filename);
20164 /* Don't add cwd for <built-in>. */
20165 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
20166 add_comp_dir_attribute (die);
20169 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
20171 /* If our producer is LTO try to figure out a common language to use
20172 from the global list of translation units. */
20173 if (strcmp (language_string, "GNU GIMPLE") == 0)
20175 unsigned i;
20176 tree t;
20177 const char *common_lang = NULL;
20179 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
20181 if (!TRANSLATION_UNIT_LANGUAGE (t))
20182 continue;
20183 if (!common_lang)
20184 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
20185 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
20187 else if (strncmp (common_lang, "GNU C", 5) == 0
20188 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
20189 /* Mixing C and C++ is ok, use C++ in that case. */
20190 common_lang = highest_c_language (common_lang,
20191 TRANSLATION_UNIT_LANGUAGE (t));
20192 else
20194 /* Fall back to C. */
20195 common_lang = NULL;
20196 break;
20200 if (common_lang)
20201 language_string = common_lang;
20204 language = DW_LANG_C;
20205 if (strncmp (language_string, "GNU C", 5) == 0
20206 && ISDIGIT (language_string[5]))
20208 language = DW_LANG_C89;
20209 if (dwarf_version >= 3 || !dwarf_strict)
20211 if (strcmp (language_string, "GNU C89") != 0)
20212 language = DW_LANG_C99;
20214 if (dwarf_version >= 5 /* || !dwarf_strict */)
20215 if (strcmp (language_string, "GNU C11") == 0)
20216 language = DW_LANG_C11;
20219 else if (strncmp (language_string, "GNU C++", 7) == 0)
20221 language = DW_LANG_C_plus_plus;
20222 if (dwarf_version >= 5 /* || !dwarf_strict */)
20224 if (strcmp (language_string, "GNU C++11") == 0)
20225 language = DW_LANG_C_plus_plus_11;
20226 else if (strcmp (language_string, "GNU C++14") == 0)
20227 language = DW_LANG_C_plus_plus_14;
20230 else if (strcmp (language_string, "GNU F77") == 0)
20231 language = DW_LANG_Fortran77;
20232 else if (strcmp (language_string, "GNU Pascal") == 0)
20233 language = DW_LANG_Pascal83;
20234 else if (dwarf_version >= 3 || !dwarf_strict)
20236 if (strcmp (language_string, "GNU Ada") == 0)
20237 language = DW_LANG_Ada95;
20238 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
20240 language = DW_LANG_Fortran95;
20241 if (dwarf_version >= 5 /* || !dwarf_strict */)
20243 if (strcmp (language_string, "GNU Fortran2003") == 0)
20244 language = DW_LANG_Fortran03;
20245 else if (strcmp (language_string, "GNU Fortran2008") == 0)
20246 language = DW_LANG_Fortran08;
20249 else if (strcmp (language_string, "GNU Java") == 0)
20250 language = DW_LANG_Java;
20251 else if (strcmp (language_string, "GNU Objective-C") == 0)
20252 language = DW_LANG_ObjC;
20253 else if (strcmp (language_string, "GNU Objective-C++") == 0)
20254 language = DW_LANG_ObjC_plus_plus;
20255 else if (dwarf_version >= 5 || !dwarf_strict)
20257 if (strcmp (language_string, "GNU Go") == 0)
20258 language = DW_LANG_Go;
20261 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
20262 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
20263 language = DW_LANG_Fortran90;
20265 add_AT_unsigned (die, DW_AT_language, language);
20267 switch (language)
20269 case DW_LANG_Fortran77:
20270 case DW_LANG_Fortran90:
20271 case DW_LANG_Fortran95:
20272 case DW_LANG_Fortran03:
20273 case DW_LANG_Fortran08:
20274 /* Fortran has case insensitive identifiers and the front-end
20275 lowercases everything. */
20276 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
20277 break;
20278 default:
20279 /* The default DW_ID_case_sensitive doesn't need to be specified. */
20280 break;
20282 return die;
20285 /* Generate the DIE for a base class. */
20287 static void
20288 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
20290 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
20292 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, context_die);
20293 add_data_member_location_attribute (die, binfo);
20295 if (BINFO_VIRTUAL_P (binfo))
20296 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
20298 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
20299 children, otherwise the default is DW_ACCESS_public. In DWARF2
20300 the default has always been DW_ACCESS_private. */
20301 if (access == access_public_node)
20303 if (dwarf_version == 2
20304 || context_die->die_tag == DW_TAG_class_type)
20305 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
20307 else if (access == access_protected_node)
20308 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
20309 else if (dwarf_version > 2
20310 && context_die->die_tag != DW_TAG_class_type)
20311 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
20314 /* Generate a DIE for a class member. */
20316 static void
20317 gen_member_die (tree type, dw_die_ref context_die)
20319 tree member;
20320 tree binfo = TYPE_BINFO (type);
20321 dw_die_ref child;
20323 /* If this is not an incomplete type, output descriptions of each of its
20324 members. Note that as we output the DIEs necessary to represent the
20325 members of this record or union type, we will also be trying to output
20326 DIEs to represent the *types* of those members. However the `type'
20327 function (above) will specifically avoid generating type DIEs for member
20328 types *within* the list of member DIEs for this (containing) type except
20329 for those types (of members) which are explicitly marked as also being
20330 members of this (containing) type themselves. The g++ front- end can
20331 force any given type to be treated as a member of some other (containing)
20332 type by setting the TYPE_CONTEXT of the given (member) type to point to
20333 the TREE node representing the appropriate (containing) type. */
20335 /* First output info about the base classes. */
20336 if (binfo)
20338 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
20339 int i;
20340 tree base;
20342 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
20343 gen_inheritance_die (base,
20344 (accesses ? (*accesses)[i] : access_public_node),
20345 context_die);
20348 /* Now output info about the data members and type members. */
20349 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
20351 /* If we thought we were generating minimal debug info for TYPE
20352 and then changed our minds, some of the member declarations
20353 may have already been defined. Don't define them again, but
20354 do put them in the right order. */
20356 child = lookup_decl_die (member);
20357 if (child)
20358 splice_child_die (context_die, child);
20359 else
20360 gen_decl_die (member, NULL, context_die);
20363 /* We do not keep type methods in type variants. */
20364 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
20365 /* Now output info about the function members (if any). */
20366 if (TYPE_METHODS (type) != error_mark_node)
20367 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
20369 /* Don't include clones in the member list. */
20370 if (DECL_ABSTRACT_ORIGIN (member))
20371 continue;
20372 /* Nor constructors for anonymous classes. */
20373 if (DECL_ARTIFICIAL (member)
20374 && dwarf2_name (member, 0) == NULL)
20375 continue;
20377 child = lookup_decl_die (member);
20378 if (child)
20379 splice_child_die (context_die, child);
20380 else
20381 gen_decl_die (member, NULL, context_die);
20385 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
20386 is set, we pretend that the type was never defined, so we only get the
20387 member DIEs needed by later specification DIEs. */
20389 static void
20390 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
20391 enum debug_info_usage usage)
20393 if (TREE_ASM_WRITTEN (type))
20395 /* Fill in the bound of variable-length fields in late dwarf if
20396 still incomplete. */
20397 if (!early_dwarf && variably_modified_type_p (type, NULL))
20398 for (tree member = TYPE_FIELDS (type);
20399 member;
20400 member = DECL_CHAIN (member))
20401 fill_variable_array_bounds (TREE_TYPE (member));
20402 return;
20405 dw_die_ref type_die = lookup_type_die (type);
20406 dw_die_ref scope_die = 0;
20407 int nested = 0;
20408 int complete = (TYPE_SIZE (type)
20409 && (! TYPE_STUB_DECL (type)
20410 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
20411 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
20412 complete = complete && should_emit_struct_debug (type, usage);
20414 if (type_die && ! complete)
20415 return;
20417 if (TYPE_CONTEXT (type) != NULL_TREE
20418 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
20419 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
20420 nested = 1;
20422 scope_die = scope_die_for (type, context_die);
20424 /* Generate child dies for template paramaters. */
20425 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
20426 schedule_generic_params_dies_gen (type);
20428 if (! type_die || (nested && is_cu_die (scope_die)))
20429 /* First occurrence of type or toplevel definition of nested class. */
20431 dw_die_ref old_die = type_die;
20433 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
20434 ? record_type_tag (type) : DW_TAG_union_type,
20435 scope_die, type);
20436 equate_type_number_to_die (type, type_die);
20437 if (old_die)
20438 add_AT_specification (type_die, old_die);
20439 else
20440 add_name_attribute (type_die, type_tag (type));
20442 else
20443 remove_AT (type_die, DW_AT_declaration);
20445 /* If this type has been completed, then give it a byte_size attribute and
20446 then give a list of members. */
20447 if (complete && !ns_decl)
20449 /* Prevent infinite recursion in cases where the type of some member of
20450 this type is expressed in terms of this type itself. */
20451 TREE_ASM_WRITTEN (type) = 1;
20452 add_byte_size_attribute (type_die, type);
20453 if (TYPE_STUB_DECL (type) != NULL_TREE)
20455 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
20456 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
20459 /* If the first reference to this type was as the return type of an
20460 inline function, then it may not have a parent. Fix this now. */
20461 if (type_die->die_parent == NULL)
20462 add_child_die (scope_die, type_die);
20464 push_decl_scope (type);
20465 gen_member_die (type, type_die);
20466 pop_decl_scope ();
20468 add_gnat_descriptive_type_attribute (type_die, type, context_die);
20469 if (TYPE_ARTIFICIAL (type))
20470 add_AT_flag (type_die, DW_AT_artificial, 1);
20472 /* GNU extension: Record what type our vtable lives in. */
20473 if (TYPE_VFIELD (type))
20475 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
20477 gen_type_die (vtype, context_die);
20478 add_AT_die_ref (type_die, DW_AT_containing_type,
20479 lookup_type_die (vtype));
20482 else
20484 add_AT_flag (type_die, DW_AT_declaration, 1);
20486 /* We don't need to do this for function-local types. */
20487 if (TYPE_STUB_DECL (type)
20488 && ! decl_function_context (TYPE_STUB_DECL (type)))
20489 vec_safe_push (incomplete_types, type);
20492 if (get_AT (type_die, DW_AT_name))
20493 add_pubtype (type, type_die);
20496 /* Generate a DIE for a subroutine _type_. */
20498 static void
20499 gen_subroutine_type_die (tree type, dw_die_ref context_die)
20501 tree return_type = TREE_TYPE (type);
20502 dw_die_ref subr_die
20503 = new_die (DW_TAG_subroutine_type,
20504 scope_die_for (type, context_die), type);
20506 equate_type_number_to_die (type, subr_die);
20507 add_prototyped_attribute (subr_die, type);
20508 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, context_die);
20509 gen_formal_types_die (type, subr_die);
20511 if (get_AT (subr_die, DW_AT_name))
20512 add_pubtype (type, subr_die);
20515 /* Generate a DIE for a type definition. */
20517 static void
20518 gen_typedef_die (tree decl, dw_die_ref context_die)
20520 dw_die_ref type_die;
20521 tree origin;
20523 if (TREE_ASM_WRITTEN (decl))
20525 if (DECL_ORIGINAL_TYPE (decl))
20526 fill_variable_array_bounds (DECL_ORIGINAL_TYPE (decl));
20527 return;
20530 TREE_ASM_WRITTEN (decl) = 1;
20531 type_die = new_die (DW_TAG_typedef, context_die, decl);
20532 origin = decl_ultimate_origin (decl);
20533 if (origin != NULL)
20534 add_abstract_origin_attribute (type_die, origin);
20535 else
20537 tree type;
20539 add_name_and_src_coords_attributes (type_die, decl);
20540 if (DECL_ORIGINAL_TYPE (decl))
20542 type = DECL_ORIGINAL_TYPE (decl);
20544 if (type == error_mark_node)
20545 return;
20547 gcc_assert (type != TREE_TYPE (decl));
20548 equate_type_number_to_die (TREE_TYPE (decl), type_die);
20550 else
20552 type = TREE_TYPE (decl);
20554 if (type == error_mark_node)
20555 return;
20557 if (is_naming_typedef_decl (TYPE_NAME (type)))
20559 /* Here, we are in the case of decl being a typedef naming
20560 an anonymous type, e.g:
20561 typedef struct {...} foo;
20562 In that case TREE_TYPE (decl) is not a typedef variant
20563 type and TYPE_NAME of the anonymous type is set to the
20564 TYPE_DECL of the typedef. This construct is emitted by
20565 the C++ FE.
20567 TYPE is the anonymous struct named by the typedef
20568 DECL. As we need the DW_AT_type attribute of the
20569 DW_TAG_typedef to point to the DIE of TYPE, let's
20570 generate that DIE right away. add_type_attribute
20571 called below will then pick (via lookup_type_die) that
20572 anonymous struct DIE. */
20573 if (!TREE_ASM_WRITTEN (type))
20574 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
20576 /* This is a GNU Extension. We are adding a
20577 DW_AT_linkage_name attribute to the DIE of the
20578 anonymous struct TYPE. The value of that attribute
20579 is the name of the typedef decl naming the anonymous
20580 struct. This greatly eases the work of consumers of
20581 this debug info. */
20582 add_linkage_attr (lookup_type_die (type), decl);
20586 add_type_attribute (type_die, type, decl_quals (decl), context_die);
20588 if (is_naming_typedef_decl (decl))
20589 /* We want that all subsequent calls to lookup_type_die with
20590 TYPE in argument yield the DW_TAG_typedef we have just
20591 created. */
20592 equate_type_number_to_die (type, type_die);
20594 add_accessibility_attribute (type_die, decl);
20597 if (DECL_ABSTRACT_P (decl))
20598 equate_decl_number_to_die (decl, type_die);
20600 if (get_AT (type_die, DW_AT_name))
20601 add_pubtype (decl, type_die);
20604 /* Generate a DIE for a struct, class, enum or union type. */
20606 static void
20607 gen_tagged_type_die (tree type,
20608 dw_die_ref context_die,
20609 enum debug_info_usage usage)
20611 int need_pop;
20613 if (type == NULL_TREE
20614 || !is_tagged_type (type))
20615 return;
20617 if (TREE_ASM_WRITTEN (type))
20618 need_pop = 0;
20619 /* If this is a nested type whose containing class hasn't been written
20620 out yet, writing it out will cover this one, too. This does not apply
20621 to instantiations of member class templates; they need to be added to
20622 the containing class as they are generated. FIXME: This hurts the
20623 idea of combining type decls from multiple TUs, since we can't predict
20624 what set of template instantiations we'll get. */
20625 else if (TYPE_CONTEXT (type)
20626 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
20627 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
20629 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
20631 if (TREE_ASM_WRITTEN (type))
20632 return;
20634 /* If that failed, attach ourselves to the stub. */
20635 push_decl_scope (TYPE_CONTEXT (type));
20636 context_die = lookup_type_die (TYPE_CONTEXT (type));
20637 need_pop = 1;
20639 else if (TYPE_CONTEXT (type) != NULL_TREE
20640 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
20642 /* If this type is local to a function that hasn't been written
20643 out yet, use a NULL context for now; it will be fixed up in
20644 decls_for_scope. */
20645 context_die = lookup_decl_die (TYPE_CONTEXT (type));
20646 /* A declaration DIE doesn't count; nested types need to go in the
20647 specification. */
20648 if (context_die && is_declaration_die (context_die))
20649 context_die = NULL;
20650 need_pop = 0;
20652 else
20654 context_die = declare_in_namespace (type, context_die);
20655 need_pop = 0;
20658 if (TREE_CODE (type) == ENUMERAL_TYPE)
20660 /* This might have been written out by the call to
20661 declare_in_namespace. */
20662 if (!TREE_ASM_WRITTEN (type))
20663 gen_enumeration_type_die (type, context_die);
20665 else
20666 gen_struct_or_union_type_die (type, context_die, usage);
20668 if (need_pop)
20669 pop_decl_scope ();
20671 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
20672 it up if it is ever completed. gen_*_type_die will set it for us
20673 when appropriate. */
20676 /* Generate a type description DIE. */
20678 static void
20679 gen_type_die_with_usage (tree type, dw_die_ref context_die,
20680 enum debug_info_usage usage)
20682 struct array_descr_info info;
20684 if (type == NULL_TREE || type == error_mark_node)
20685 return;
20687 #ifdef ENABLE_CHECKING
20688 if (type)
20689 verify_type (type);
20690 #endif
20692 if (TYPE_NAME (type) != NULL_TREE
20693 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
20694 && is_redundant_typedef (TYPE_NAME (type))
20695 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
20696 /* The DECL of this type is a typedef we don't want to emit debug
20697 info for but we want debug info for its underlying typedef.
20698 This can happen for e.g, the injected-class-name of a C++
20699 type. */
20700 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
20702 /* If TYPE is a typedef type variant, let's generate debug info
20703 for the parent typedef which TYPE is a type of. */
20704 if (typedef_variant_p (type))
20706 if (TREE_ASM_WRITTEN (type))
20707 return;
20709 /* Prevent broken recursion; we can't hand off to the same type. */
20710 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
20712 /* Give typedefs the right scope. */
20713 context_die = scope_die_for (type, context_die);
20715 TREE_ASM_WRITTEN (type) = 1;
20717 gen_decl_die (TYPE_NAME (type), NULL, context_die);
20718 return;
20721 /* If type is an anonymous tagged type named by a typedef, let's
20722 generate debug info for the typedef. */
20723 if (is_naming_typedef_decl (TYPE_NAME (type)))
20725 /* Use the DIE of the containing namespace as the parent DIE of
20726 the type description DIE we want to generate. */
20727 if (DECL_CONTEXT (TYPE_NAME (type))
20728 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
20729 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
20731 gen_decl_die (TYPE_NAME (type), NULL, context_die);
20732 return;
20735 /* We are going to output a DIE to represent the unqualified version
20736 of this type (i.e. without any const or volatile qualifiers) so
20737 get the main variant (i.e. the unqualified version) of this type
20738 now. (Vectors are special because the debugging info is in the
20739 cloned type itself). */
20740 if (TREE_CODE (type) != VECTOR_TYPE)
20741 type = type_main_variant (type);
20743 /* If this is an array type with hidden descriptor, handle it first. */
20744 if (!TREE_ASM_WRITTEN (type)
20745 && lang_hooks.types.get_array_descr_info)
20747 memset (&info, 0, sizeof (info));
20748 if (lang_hooks.types.get_array_descr_info (type, &info))
20750 gen_descr_array_type_die (type, &info, context_die);
20751 TREE_ASM_WRITTEN (type) = 1;
20752 return;
20756 if (TREE_ASM_WRITTEN (type))
20758 /* Variable-length types may be incomplete even if
20759 TREE_ASM_WRITTEN. For such types, fall through to
20760 gen_array_type_die() and possibly fill in
20761 DW_AT_{upper,lower}_bound attributes. */
20762 if ((TREE_CODE (type) != ARRAY_TYPE
20763 && TREE_CODE (type) != RECORD_TYPE
20764 && TREE_CODE (type) != UNION_TYPE
20765 && TREE_CODE (type) != QUAL_UNION_TYPE)
20766 || !variably_modified_type_p (type, NULL))
20767 return;
20770 switch (TREE_CODE (type))
20772 case ERROR_MARK:
20773 break;
20775 case POINTER_TYPE:
20776 case REFERENCE_TYPE:
20777 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
20778 ensures that the gen_type_die recursion will terminate even if the
20779 type is recursive. Recursive types are possible in Ada. */
20780 /* ??? We could perhaps do this for all types before the switch
20781 statement. */
20782 TREE_ASM_WRITTEN (type) = 1;
20784 /* For these types, all that is required is that we output a DIE (or a
20785 set of DIEs) to represent the "basis" type. */
20786 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20787 DINFO_USAGE_IND_USE);
20788 break;
20790 case OFFSET_TYPE:
20791 /* This code is used for C++ pointer-to-data-member types.
20792 Output a description of the relevant class type. */
20793 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
20794 DINFO_USAGE_IND_USE);
20796 /* Output a description of the type of the object pointed to. */
20797 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20798 DINFO_USAGE_IND_USE);
20800 /* Now output a DIE to represent this pointer-to-data-member type
20801 itself. */
20802 gen_ptr_to_mbr_type_die (type, context_die);
20803 break;
20805 case FUNCTION_TYPE:
20806 /* Force out return type (in case it wasn't forced out already). */
20807 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20808 DINFO_USAGE_DIR_USE);
20809 gen_subroutine_type_die (type, context_die);
20810 break;
20812 case METHOD_TYPE:
20813 /* Force out return type (in case it wasn't forced out already). */
20814 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20815 DINFO_USAGE_DIR_USE);
20816 gen_subroutine_type_die (type, context_die);
20817 break;
20819 case ARRAY_TYPE:
20820 case VECTOR_TYPE:
20821 gen_array_type_die (type, context_die);
20822 break;
20824 case ENUMERAL_TYPE:
20825 case RECORD_TYPE:
20826 case UNION_TYPE:
20827 case QUAL_UNION_TYPE:
20828 gen_tagged_type_die (type, context_die, usage);
20829 return;
20831 case VOID_TYPE:
20832 case INTEGER_TYPE:
20833 case REAL_TYPE:
20834 case FIXED_POINT_TYPE:
20835 case COMPLEX_TYPE:
20836 case BOOLEAN_TYPE:
20837 case POINTER_BOUNDS_TYPE:
20838 /* No DIEs needed for fundamental types. */
20839 break;
20841 case NULLPTR_TYPE:
20842 case LANG_TYPE:
20843 /* Just use DW_TAG_unspecified_type. */
20845 dw_die_ref type_die = lookup_type_die (type);
20846 if (type_die == NULL)
20848 tree name = TYPE_IDENTIFIER (type);
20849 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
20850 type);
20851 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
20852 equate_type_number_to_die (type, type_die);
20855 break;
20857 default:
20858 if (is_cxx_auto (type))
20860 tree name = TYPE_IDENTIFIER (type);
20861 dw_die_ref *die = (name == get_identifier ("auto")
20862 ? &auto_die : &decltype_auto_die);
20863 if (!*die)
20865 *die = new_die (DW_TAG_unspecified_type,
20866 comp_unit_die (), NULL_TREE);
20867 add_name_attribute (*die, IDENTIFIER_POINTER (name));
20869 equate_type_number_to_die (type, *die);
20870 break;
20872 gcc_unreachable ();
20875 TREE_ASM_WRITTEN (type) = 1;
20878 static void
20879 gen_type_die (tree type, dw_die_ref context_die)
20881 if (type != error_mark_node)
20883 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
20884 #ifdef ENABLE_CHECKING
20885 dw_die_ref die = lookup_type_die (type);
20886 if (die)
20887 check_die (die);
20888 #endif
20892 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
20893 things which are local to the given block. */
20895 static void
20896 gen_block_die (tree stmt, dw_die_ref context_die)
20898 int must_output_die = 0;
20899 bool inlined_func;
20901 /* Ignore blocks that are NULL. */
20902 if (stmt == NULL_TREE)
20903 return;
20905 inlined_func = inlined_function_outer_scope_p (stmt);
20907 /* If the block is one fragment of a non-contiguous block, do not
20908 process the variables, since they will have been done by the
20909 origin block. Do process subblocks. */
20910 if (BLOCK_FRAGMENT_ORIGIN (stmt))
20912 tree sub;
20914 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
20915 gen_block_die (sub, context_die);
20917 return;
20920 /* Determine if we need to output any Dwarf DIEs at all to represent this
20921 block. */
20922 if (inlined_func)
20923 /* The outer scopes for inlinings *must* always be represented. We
20924 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
20925 must_output_die = 1;
20926 else
20928 /* Determine if this block directly contains any "significant"
20929 local declarations which we will need to output DIEs for. */
20930 if (debug_info_level > DINFO_LEVEL_TERSE)
20931 /* We are not in terse mode so *any* local declaration counts
20932 as being a "significant" one. */
20933 must_output_die = ((BLOCK_VARS (stmt) != NULL
20934 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
20935 && (TREE_USED (stmt)
20936 || TREE_ASM_WRITTEN (stmt)
20937 || BLOCK_ABSTRACT (stmt)));
20938 else if ((TREE_USED (stmt)
20939 || TREE_ASM_WRITTEN (stmt)
20940 || BLOCK_ABSTRACT (stmt))
20941 && !dwarf2out_ignore_block (stmt))
20942 must_output_die = 1;
20945 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
20946 DIE for any block which contains no significant local declarations at
20947 all. Rather, in such cases we just call `decls_for_scope' so that any
20948 needed Dwarf info for any sub-blocks will get properly generated. Note
20949 that in terse mode, our definition of what constitutes a "significant"
20950 local declaration gets restricted to include only inlined function
20951 instances and local (nested) function definitions. */
20952 if (must_output_die)
20954 if (inlined_func)
20956 /* If STMT block is abstract, that means we have been called
20957 indirectly from dwarf2out_abstract_function.
20958 That function rightfully marks the descendent blocks (of
20959 the abstract function it is dealing with) as being abstract,
20960 precisely to prevent us from emitting any
20961 DW_TAG_inlined_subroutine DIE as a descendent
20962 of an abstract function instance. So in that case, we should
20963 not call gen_inlined_subroutine_die.
20965 Later though, when cgraph asks dwarf2out to emit info
20966 for the concrete instance of the function decl into which
20967 the concrete instance of STMT got inlined, the later will lead
20968 to the generation of a DW_TAG_inlined_subroutine DIE. */
20969 if (! BLOCK_ABSTRACT (stmt))
20970 gen_inlined_subroutine_die (stmt, context_die);
20972 else
20973 gen_lexical_block_die (stmt, context_die);
20975 else
20976 decls_for_scope (stmt, context_die);
20979 /* Process variable DECL (or variable with origin ORIGIN) within
20980 block STMT and add it to CONTEXT_DIE. */
20981 static void
20982 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
20984 dw_die_ref die;
20985 tree decl_or_origin = decl ? decl : origin;
20987 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
20988 die = lookup_decl_die (decl_or_origin);
20989 else if (TREE_CODE (decl_or_origin) == TYPE_DECL
20990 && TYPE_DECL_IS_STUB (decl_or_origin))
20991 die = lookup_type_die (TREE_TYPE (decl_or_origin));
20992 else
20993 die = NULL;
20995 if (die != NULL && die->die_parent == NULL)
20996 add_child_die (context_die, die);
20997 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
20999 if (early_dwarf)
21000 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
21001 stmt, context_die);
21003 else
21004 gen_decl_die (decl, origin, context_die);
21007 /* Generate all of the decls declared within a given scope and (recursively)
21008 all of its sub-blocks. */
21010 static void
21011 decls_for_scope (tree stmt, dw_die_ref context_die)
21013 tree decl;
21014 unsigned int i;
21015 tree subblocks;
21017 /* Ignore NULL blocks. */
21018 if (stmt == NULL_TREE)
21019 return;
21021 /* Output the DIEs to represent all of the data objects and typedefs
21022 declared directly within this block but not within any nested
21023 sub-blocks. Also, nested function and tag DIEs have been
21024 generated with a parent of NULL; fix that up now. We don't
21025 have to do this if we're at -g1. */
21026 if (debug_info_level > DINFO_LEVEL_TERSE)
21028 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
21029 process_scope_var (stmt, decl, NULL_TREE, context_die);
21030 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
21031 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
21032 context_die);
21035 /* Even if we're at -g1, we need to process the subblocks in order to get
21036 inlined call information. */
21038 /* Output the DIEs to represent all sub-blocks (and the items declared
21039 therein) of this block. */
21040 for (subblocks = BLOCK_SUBBLOCKS (stmt);
21041 subblocks != NULL;
21042 subblocks = BLOCK_CHAIN (subblocks))
21043 gen_block_die (subblocks, context_die);
21046 /* Is this a typedef we can avoid emitting? */
21048 static inline int
21049 is_redundant_typedef (const_tree decl)
21051 if (TYPE_DECL_IS_STUB (decl))
21052 return 1;
21054 if (DECL_ARTIFICIAL (decl)
21055 && DECL_CONTEXT (decl)
21056 && is_tagged_type (DECL_CONTEXT (decl))
21057 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
21058 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
21059 /* Also ignore the artificial member typedef for the class name. */
21060 return 1;
21062 return 0;
21065 /* Return TRUE if TYPE is a typedef that names a type for linkage
21066 purposes. This kind of typedefs is produced by the C++ FE for
21067 constructs like:
21069 typedef struct {...} foo;
21071 In that case, there is no typedef variant type produced for foo.
21072 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
21073 struct type. */
21075 static bool
21076 is_naming_typedef_decl (const_tree decl)
21078 if (decl == NULL_TREE
21079 || TREE_CODE (decl) != TYPE_DECL
21080 || !is_tagged_type (TREE_TYPE (decl))
21081 || DECL_IS_BUILTIN (decl)
21082 || is_redundant_typedef (decl)
21083 /* It looks like Ada produces TYPE_DECLs that are very similar
21084 to C++ naming typedefs but that have different
21085 semantics. Let's be specific to c++ for now. */
21086 || !is_cxx ())
21087 return FALSE;
21089 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
21090 && TYPE_NAME (TREE_TYPE (decl)) == decl
21091 && (TYPE_STUB_DECL (TREE_TYPE (decl))
21092 != TYPE_NAME (TREE_TYPE (decl))));
21095 /* Looks up the DIE for a context. */
21097 static inline dw_die_ref
21098 lookup_context_die (tree context)
21100 if (context)
21102 /* Find die that represents this context. */
21103 if (TYPE_P (context))
21105 context = TYPE_MAIN_VARIANT (context);
21106 dw_die_ref ctx = lookup_type_die (context);
21107 if (!ctx)
21108 return NULL;
21109 return strip_naming_typedef (context, ctx);
21111 else
21112 return lookup_decl_die (context);
21114 return comp_unit_die ();
21117 /* Returns the DIE for a context. */
21119 static inline dw_die_ref
21120 get_context_die (tree context)
21122 if (context)
21124 /* Find die that represents this context. */
21125 if (TYPE_P (context))
21127 context = TYPE_MAIN_VARIANT (context);
21128 return strip_naming_typedef (context, force_type_die (context));
21130 else
21131 return force_decl_die (context);
21133 return comp_unit_die ();
21136 /* Returns the DIE for decl. A DIE will always be returned. */
21138 static dw_die_ref
21139 force_decl_die (tree decl)
21141 dw_die_ref decl_die;
21142 unsigned saved_external_flag;
21143 tree save_fn = NULL_TREE;
21144 decl_die = lookup_decl_die (decl);
21145 if (!decl_die)
21147 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
21149 decl_die = lookup_decl_die (decl);
21150 if (decl_die)
21151 return decl_die;
21153 switch (TREE_CODE (decl))
21155 case FUNCTION_DECL:
21156 /* Clear current_function_decl, so that gen_subprogram_die thinks
21157 that this is a declaration. At this point, we just want to force
21158 declaration die. */
21159 save_fn = current_function_decl;
21160 current_function_decl = NULL_TREE;
21161 gen_subprogram_die (decl, context_die);
21162 current_function_decl = save_fn;
21163 break;
21165 case VAR_DECL:
21166 /* Set external flag to force declaration die. Restore it after
21167 gen_decl_die() call. */
21168 saved_external_flag = DECL_EXTERNAL (decl);
21169 DECL_EXTERNAL (decl) = 1;
21170 gen_decl_die (decl, NULL, context_die);
21171 DECL_EXTERNAL (decl) = saved_external_flag;
21172 break;
21174 case NAMESPACE_DECL:
21175 if (dwarf_version >= 3 || !dwarf_strict)
21176 dwarf2out_decl (decl);
21177 else
21178 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
21179 decl_die = comp_unit_die ();
21180 break;
21182 case TRANSLATION_UNIT_DECL:
21183 decl_die = comp_unit_die ();
21184 break;
21186 default:
21187 gcc_unreachable ();
21190 /* We should be able to find the DIE now. */
21191 if (!decl_die)
21192 decl_die = lookup_decl_die (decl);
21193 gcc_assert (decl_die);
21196 return decl_die;
21199 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
21200 always returned. */
21202 static dw_die_ref
21203 force_type_die (tree type)
21205 dw_die_ref type_die;
21207 type_die = lookup_type_die (type);
21208 if (!type_die)
21210 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
21212 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
21213 context_die);
21214 gcc_assert (type_die);
21216 return type_die;
21219 /* Force out any required namespaces to be able to output DECL,
21220 and return the new context_die for it, if it's changed. */
21222 static dw_die_ref
21223 setup_namespace_context (tree thing, dw_die_ref context_die)
21225 tree context = (DECL_P (thing)
21226 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
21227 if (context && TREE_CODE (context) == NAMESPACE_DECL)
21228 /* Force out the namespace. */
21229 context_die = force_decl_die (context);
21231 return context_die;
21234 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
21235 type) within its namespace, if appropriate.
21237 For compatibility with older debuggers, namespace DIEs only contain
21238 declarations; all definitions are emitted at CU scope, with
21239 DW_AT_specification pointing to the declaration (like with class
21240 members). */
21242 static dw_die_ref
21243 declare_in_namespace (tree thing, dw_die_ref context_die)
21245 dw_die_ref ns_context;
21247 if (debug_info_level <= DINFO_LEVEL_TERSE)
21248 return context_die;
21250 /* External declarations in the local scope only need to be emitted
21251 once, not once in the namespace and once in the scope.
21253 This avoids declaring the `extern' below in the
21254 namespace DIE as well as in the innermost scope:
21256 namespace S
21258 int i=5;
21259 int foo()
21261 int i=8;
21262 extern int i;
21263 return i;
21267 if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
21268 return context_die;
21270 /* If this decl is from an inlined function, then don't try to emit it in its
21271 namespace, as we will get confused. It would have already been emitted
21272 when the abstract instance of the inline function was emitted anyways. */
21273 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
21274 return context_die;
21276 ns_context = setup_namespace_context (thing, context_die);
21278 if (ns_context != context_die)
21280 if (is_fortran ())
21281 return ns_context;
21282 if (DECL_P (thing))
21283 gen_decl_die (thing, NULL, ns_context);
21284 else
21285 gen_type_die (thing, ns_context);
21287 return context_die;
21290 /* Generate a DIE for a namespace or namespace alias. */
21292 static void
21293 gen_namespace_die (tree decl, dw_die_ref context_die)
21295 dw_die_ref namespace_die;
21297 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
21298 they are an alias of. */
21299 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
21301 /* Output a real namespace or module. */
21302 context_die = setup_namespace_context (decl, comp_unit_die ());
21303 namespace_die = new_die (is_fortran ()
21304 ? DW_TAG_module : DW_TAG_namespace,
21305 context_die, decl);
21306 /* For Fortran modules defined in different CU don't add src coords. */
21307 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
21309 const char *name = dwarf2_name (decl, 0);
21310 if (name)
21311 add_name_attribute (namespace_die, name);
21313 else
21314 add_name_and_src_coords_attributes (namespace_die, decl);
21315 if (DECL_EXTERNAL (decl))
21316 add_AT_flag (namespace_die, DW_AT_declaration, 1);
21317 equate_decl_number_to_die (decl, namespace_die);
21319 else
21321 /* Output a namespace alias. */
21323 /* Force out the namespace we are an alias of, if necessary. */
21324 dw_die_ref origin_die
21325 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
21327 if (DECL_FILE_SCOPE_P (decl)
21328 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
21329 context_die = setup_namespace_context (decl, comp_unit_die ());
21330 /* Now create the namespace alias DIE. */
21331 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
21332 add_name_and_src_coords_attributes (namespace_die, decl);
21333 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
21334 equate_decl_number_to_die (decl, namespace_die);
21336 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
21337 if (want_pubnames ())
21338 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
21341 /* Generate Dwarf debug information for a decl described by DECL.
21342 The return value is currently only meaningful for PARM_DECLs,
21343 for all other decls it returns NULL. */
21345 static dw_die_ref
21346 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
21348 tree decl_or_origin = decl ? decl : origin;
21349 tree class_origin = NULL, ultimate_origin;
21351 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
21352 return NULL;
21354 /* Ignore pointer bounds decls. */
21355 if (DECL_P (decl_or_origin)
21356 && TREE_TYPE (decl_or_origin)
21357 && POINTER_BOUNDS_P (decl_or_origin))
21358 return NULL;
21360 switch (TREE_CODE (decl_or_origin))
21362 case ERROR_MARK:
21363 break;
21365 case CONST_DECL:
21366 if (!is_fortran () && !is_ada ())
21368 /* The individual enumerators of an enum type get output when we output
21369 the Dwarf representation of the relevant enum type itself. */
21370 break;
21373 /* Emit its type. */
21374 gen_type_die (TREE_TYPE (decl), context_die);
21376 /* And its containing namespace. */
21377 context_die = declare_in_namespace (decl, context_die);
21379 gen_const_die (decl, context_die);
21380 break;
21382 case FUNCTION_DECL:
21383 /* Don't output any DIEs to represent mere function declarations,
21384 unless they are class members or explicit block externs. */
21385 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
21386 && DECL_FILE_SCOPE_P (decl_or_origin)
21387 && (current_function_decl == NULL_TREE
21388 || DECL_ARTIFICIAL (decl_or_origin)))
21389 break;
21391 #if 0
21392 /* FIXME */
21393 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
21394 on local redeclarations of global functions. That seems broken. */
21395 if (current_function_decl != decl)
21396 /* This is only a declaration. */;
21397 #endif
21399 /* If we're emitting a clone, emit info for the abstract instance. */
21400 if (origin || DECL_ORIGIN (decl) != decl)
21401 dwarf2out_abstract_function (origin
21402 ? DECL_ORIGIN (origin)
21403 : DECL_ABSTRACT_ORIGIN (decl));
21405 /* If we're emitting an out-of-line copy of an inline function,
21406 emit info for the abstract instance and set up to refer to it. */
21407 else if (cgraph_function_possibly_inlined_p (decl)
21408 && ! DECL_ABSTRACT_P (decl)
21409 && ! class_or_namespace_scope_p (context_die)
21410 /* dwarf2out_abstract_function won't emit a die if this is just
21411 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
21412 that case, because that works only if we have a die. */
21413 && DECL_INITIAL (decl) != NULL_TREE)
21415 dwarf2out_abstract_function (decl);
21416 set_decl_origin_self (decl);
21419 /* Otherwise we're emitting the primary DIE for this decl. */
21420 else if (debug_info_level > DINFO_LEVEL_TERSE)
21422 /* Before we describe the FUNCTION_DECL itself, make sure that we
21423 have its containing type. */
21424 if (!origin)
21425 origin = decl_class_context (decl);
21426 if (origin != NULL_TREE)
21427 gen_type_die (origin, context_die);
21429 /* And its return type. */
21430 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
21432 /* And its virtual context. */
21433 if (DECL_VINDEX (decl) != NULL_TREE)
21434 gen_type_die (DECL_CONTEXT (decl), context_die);
21436 /* Make sure we have a member DIE for decl. */
21437 if (origin != NULL_TREE)
21438 gen_type_die_for_member (origin, decl, context_die);
21440 /* And its containing namespace. */
21441 context_die = declare_in_namespace (decl, context_die);
21444 /* Now output a DIE to represent the function itself. */
21445 if (decl)
21446 gen_subprogram_die (decl, context_die);
21447 break;
21449 case TYPE_DECL:
21450 /* If we are in terse mode, don't generate any DIEs to represent any
21451 actual typedefs. */
21452 if (debug_info_level <= DINFO_LEVEL_TERSE)
21453 break;
21455 /* In the special case of a TYPE_DECL node representing the declaration
21456 of some type tag, if the given TYPE_DECL is marked as having been
21457 instantiated from some other (original) TYPE_DECL node (e.g. one which
21458 was generated within the original definition of an inline function) we
21459 used to generate a special (abbreviated) DW_TAG_structure_type,
21460 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
21461 should be actually referencing those DIEs, as variable DIEs with that
21462 type would be emitted already in the abstract origin, so it was always
21463 removed during unused type prunning. Don't add anything in this
21464 case. */
21465 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
21466 break;
21468 if (is_redundant_typedef (decl))
21469 gen_type_die (TREE_TYPE (decl), context_die);
21470 else
21471 /* Output a DIE to represent the typedef itself. */
21472 gen_typedef_die (decl, context_die);
21473 break;
21475 case LABEL_DECL:
21476 if (debug_info_level >= DINFO_LEVEL_NORMAL)
21477 gen_label_die (decl, context_die);
21478 break;
21480 case VAR_DECL:
21481 case RESULT_DECL:
21482 /* If we are in terse mode, don't generate any DIEs to represent any
21483 variable declarations or definitions. */
21484 if (debug_info_level <= DINFO_LEVEL_TERSE)
21485 break;
21487 /* Output any DIEs that are needed to specify the type of this data
21488 object. */
21489 if (decl_by_reference_p (decl_or_origin))
21490 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
21491 else
21492 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
21494 /* And its containing type. */
21495 class_origin = decl_class_context (decl_or_origin);
21496 if (class_origin != NULL_TREE)
21497 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
21499 /* And its containing namespace. */
21500 context_die = declare_in_namespace (decl_or_origin, context_die);
21502 /* Now output the DIE to represent the data object itself. This gets
21503 complicated because of the possibility that the VAR_DECL really
21504 represents an inlined instance of a formal parameter for an inline
21505 function. */
21506 ultimate_origin = decl_ultimate_origin (decl_or_origin);
21507 if (ultimate_origin != NULL_TREE
21508 && TREE_CODE (ultimate_origin) == PARM_DECL)
21509 gen_formal_parameter_die (decl, origin,
21510 true /* Emit name attribute. */,
21511 context_die);
21512 else
21513 gen_variable_die (decl, origin, context_die);
21514 break;
21516 case FIELD_DECL:
21517 /* Ignore the nameless fields that are used to skip bits but handle C++
21518 anonymous unions and structs. */
21519 if (DECL_NAME (decl) != NULL_TREE
21520 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
21521 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
21523 gen_type_die (member_declared_type (decl), context_die);
21524 gen_field_die (decl, context_die);
21526 break;
21528 case PARM_DECL:
21529 if (DECL_BY_REFERENCE (decl_or_origin))
21530 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
21531 else
21532 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
21533 return gen_formal_parameter_die (decl, origin,
21534 true /* Emit name attribute. */,
21535 context_die);
21537 case NAMESPACE_DECL:
21538 case IMPORTED_DECL:
21539 if (dwarf_version >= 3 || !dwarf_strict)
21540 gen_namespace_die (decl, context_die);
21541 break;
21543 case NAMELIST_DECL:
21544 gen_namelist_decl (DECL_NAME (decl), context_die,
21545 NAMELIST_DECL_ASSOCIATED_DECL (decl));
21546 break;
21548 default:
21549 /* Probably some frontend-internal decl. Assume we don't care. */
21550 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
21551 break;
21554 return NULL;
21557 /* Output initial debug information for global DECL. Called at the
21558 end of the parsing process.
21560 This is the initial debug generation process. As such, the DIEs
21561 generated may be incomplete. A later debug generation pass
21562 (dwarf2out_late_global_decl) will augment the information generated
21563 in this pass (e.g., with complete location info). */
21565 static void
21566 dwarf2out_early_global_decl (tree decl)
21568 set_early_dwarf s;
21570 /* gen_decl_die() will set DECL_ABSTRACT because
21571 cgraph_function_possibly_inlined_p() returns true. This is in
21572 turn will cause DW_AT_inline attributes to be set.
21574 This happens because at early dwarf generation, there is no
21575 cgraph information, causing cgraph_function_possibly_inlined_p()
21576 to return true. Trick cgraph_function_possibly_inlined_p()
21577 while we generate dwarf early. */
21578 bool save = symtab->global_info_ready;
21579 symtab->global_info_ready = true;
21581 /* We don't handle TYPE_DECLs. If required, they'll be reached via
21582 other DECLs and they can point to template types or other things
21583 that dwarf2out can't handle when done via dwarf2out_decl. */
21584 if (TREE_CODE (decl) != TYPE_DECL
21585 && TREE_CODE (decl) != PARM_DECL)
21587 tree save_fndecl = current_function_decl;
21588 if (TREE_CODE (decl) == FUNCTION_DECL)
21590 /* No cfun means the symbol has no body, so there's nothing
21591 to emit. */
21592 if (!DECL_STRUCT_FUNCTION (decl))
21593 goto early_decl_exit;
21595 current_function_decl = decl;
21597 dwarf2out_decl (decl);
21598 if (TREE_CODE (decl) == FUNCTION_DECL)
21599 current_function_decl = save_fndecl;
21601 early_decl_exit:
21602 symtab->global_info_ready = save;
21605 /* Output debug information for global decl DECL. Called from
21606 toplev.c after compilation proper has finished. */
21608 static void
21609 dwarf2out_late_global_decl (tree decl)
21611 /* Output any global decls we missed or fill-in any location
21612 information we were unable to determine on the first pass.
21614 Skip over functions because they were handled by the
21615 debug_hooks->function_decl() call in rest_of_handle_final. */
21616 if ((TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
21617 && !POINTER_BOUNDS_P (decl))
21618 dwarf2out_decl (decl);
21621 /* Output debug information for type decl DECL. Called from toplev.c
21622 and from language front ends (to record built-in types). */
21623 static void
21624 dwarf2out_type_decl (tree decl, int local)
21626 if (!local)
21628 set_early_dwarf s;
21629 dwarf2out_decl (decl);
21633 /* Output debug information for imported module or decl DECL.
21634 NAME is non-NULL name in the lexical block if the decl has been renamed.
21635 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
21636 that DECL belongs to.
21637 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
21638 static void
21639 dwarf2out_imported_module_or_decl_1 (tree decl,
21640 tree name,
21641 tree lexical_block,
21642 dw_die_ref lexical_block_die)
21644 expanded_location xloc;
21645 dw_die_ref imported_die = NULL;
21646 dw_die_ref at_import_die;
21648 if (TREE_CODE (decl) == IMPORTED_DECL)
21650 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
21651 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
21652 gcc_assert (decl);
21654 else
21655 xloc = expand_location (input_location);
21657 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
21659 at_import_die = force_type_die (TREE_TYPE (decl));
21660 /* For namespace N { typedef void T; } using N::T; base_type_die
21661 returns NULL, but DW_TAG_imported_declaration requires
21662 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
21663 if (!at_import_die)
21665 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
21666 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
21667 at_import_die = lookup_type_die (TREE_TYPE (decl));
21668 gcc_assert (at_import_die);
21671 else
21673 at_import_die = lookup_decl_die (decl);
21674 if (!at_import_die)
21676 /* If we're trying to avoid duplicate debug info, we may not have
21677 emitted the member decl for this field. Emit it now. */
21678 if (TREE_CODE (decl) == FIELD_DECL)
21680 tree type = DECL_CONTEXT (decl);
21682 if (TYPE_CONTEXT (type)
21683 && TYPE_P (TYPE_CONTEXT (type))
21684 && !should_emit_struct_debug (TYPE_CONTEXT (type),
21685 DINFO_USAGE_DIR_USE))
21686 return;
21687 gen_type_die_for_member (type, decl,
21688 get_context_die (TYPE_CONTEXT (type)));
21690 if (TREE_CODE (decl) == NAMELIST_DECL)
21691 at_import_die = gen_namelist_decl (DECL_NAME (decl),
21692 get_context_die (DECL_CONTEXT (decl)),
21693 NULL_TREE);
21694 else
21695 at_import_die = force_decl_die (decl);
21699 if (TREE_CODE (decl) == NAMESPACE_DECL)
21701 if (dwarf_version >= 3 || !dwarf_strict)
21702 imported_die = new_die (DW_TAG_imported_module,
21703 lexical_block_die,
21704 lexical_block);
21705 else
21706 return;
21708 else
21709 imported_die = new_die (DW_TAG_imported_declaration,
21710 lexical_block_die,
21711 lexical_block);
21713 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
21714 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
21715 if (name)
21716 add_AT_string (imported_die, DW_AT_name,
21717 IDENTIFIER_POINTER (name));
21718 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
21721 /* Output debug information for imported module or decl DECL.
21722 NAME is non-NULL name in context if the decl has been renamed.
21723 CHILD is true if decl is one of the renamed decls as part of
21724 importing whole module. */
21726 static void
21727 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
21728 bool child)
21730 /* dw_die_ref at_import_die; */
21731 dw_die_ref scope_die;
21733 if (debug_info_level <= DINFO_LEVEL_TERSE)
21734 return;
21736 gcc_assert (decl);
21738 set_early_dwarf s;
21740 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
21741 We need decl DIE for reference and scope die. First, get DIE for the decl
21742 itself. */
21744 /* Get the scope die for decl context. Use comp_unit_die for global module
21745 or decl. If die is not found for non globals, force new die. */
21746 if (context
21747 && TYPE_P (context)
21748 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
21749 return;
21751 if (!(dwarf_version >= 3 || !dwarf_strict))
21752 return;
21754 scope_die = get_context_die (context);
21756 if (child)
21758 gcc_assert (scope_die->die_child);
21759 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
21760 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
21761 scope_die = scope_die->die_child;
21764 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
21765 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
21768 /* Output debug information for namelists. */
21770 static dw_die_ref
21771 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
21773 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
21774 tree value;
21775 unsigned i;
21777 if (debug_info_level <= DINFO_LEVEL_TERSE)
21778 return NULL;
21780 gcc_assert (scope_die != NULL);
21781 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
21782 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
21784 /* If there are no item_decls, we have a nondefining namelist, e.g.
21785 with USE association; hence, set DW_AT_declaration. */
21786 if (item_decls == NULL_TREE)
21788 add_AT_flag (nml_die, DW_AT_declaration, 1);
21789 return nml_die;
21792 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
21794 nml_item_ref_die = lookup_decl_die (value);
21795 if (!nml_item_ref_die)
21796 nml_item_ref_die = force_decl_die (value);
21798 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
21799 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
21801 return nml_die;
21805 /* Write the debugging output for DECL and return the DIE. */
21807 static void
21808 dwarf2out_decl (tree decl)
21810 dw_die_ref context_die = comp_unit_die ();
21812 switch (TREE_CODE (decl))
21814 case ERROR_MARK:
21815 return;
21817 case FUNCTION_DECL:
21818 /* What we would really like to do here is to filter out all mere
21819 file-scope declarations of file-scope functions which are never
21820 referenced later within this translation unit (and keep all of ones
21821 that *are* referenced later on) but we aren't clairvoyant, so we have
21822 no idea which functions will be referenced in the future (i.e. later
21823 on within the current translation unit). So here we just ignore all
21824 file-scope function declarations which are not also definitions. If
21825 and when the debugger needs to know something about these functions,
21826 it will have to hunt around and find the DWARF information associated
21827 with the definition of the function.
21829 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
21830 nodes represent definitions and which ones represent mere
21831 declarations. We have to check DECL_INITIAL instead. That's because
21832 the C front-end supports some weird semantics for "extern inline"
21833 function definitions. These can get inlined within the current
21834 translation unit (and thus, we need to generate Dwarf info for their
21835 abstract instances so that the Dwarf info for the concrete inlined
21836 instances can have something to refer to) but the compiler never
21837 generates any out-of-lines instances of such things (despite the fact
21838 that they *are* definitions).
21840 The important point is that the C front-end marks these "extern
21841 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
21842 them anyway. Note that the C++ front-end also plays some similar games
21843 for inline function definitions appearing within include files which
21844 also contain `#pragma interface' pragmas.
21846 If we are called from dwarf2out_abstract_function output a DIE
21847 anyway. We can end up here this way with early inlining and LTO
21848 where the inlined function is output in a different LTRANS unit
21849 or not at all. */
21850 if (DECL_INITIAL (decl) == NULL_TREE
21851 && ! DECL_ABSTRACT_P (decl))
21852 return;
21854 /* If we're a nested function, initially use a parent of NULL; if we're
21855 a plain function, this will be fixed up in decls_for_scope. If
21856 we're a method, it will be ignored, since we already have a DIE. */
21857 if (decl_function_context (decl)
21858 /* But if we're in terse mode, we don't care about scope. */
21859 && debug_info_level > DINFO_LEVEL_TERSE)
21860 context_die = NULL;
21861 break;
21863 case VAR_DECL:
21864 /* For local statics lookup proper context die. */
21865 if (local_function_static (decl))
21866 context_die = lookup_decl_die (DECL_CONTEXT (decl));
21868 /* If we are in terse mode, don't generate any DIEs to represent any
21869 variable declarations or definitions. */
21870 if (debug_info_level <= DINFO_LEVEL_TERSE)
21871 return;
21872 break;
21874 case CONST_DECL:
21875 if (debug_info_level <= DINFO_LEVEL_TERSE)
21876 return;
21877 if (!is_fortran () && !is_ada ())
21878 return;
21879 if (TREE_STATIC (decl) && decl_function_context (decl))
21880 context_die = lookup_decl_die (DECL_CONTEXT (decl));
21881 break;
21883 case NAMESPACE_DECL:
21884 case IMPORTED_DECL:
21885 if (debug_info_level <= DINFO_LEVEL_TERSE)
21886 return;
21887 if (lookup_decl_die (decl) != NULL)
21888 return;
21889 break;
21891 case TYPE_DECL:
21892 /* Don't emit stubs for types unless they are needed by other DIEs. */
21893 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
21894 return;
21896 /* Don't bother trying to generate any DIEs to represent any of the
21897 normal built-in types for the language we are compiling. */
21898 if (DECL_IS_BUILTIN (decl))
21899 return;
21901 /* If we are in terse mode, don't generate any DIEs for types. */
21902 if (debug_info_level <= DINFO_LEVEL_TERSE)
21903 return;
21905 /* If we're a function-scope tag, initially use a parent of NULL;
21906 this will be fixed up in decls_for_scope. */
21907 if (decl_function_context (decl))
21908 context_die = NULL;
21910 break;
21912 case NAMELIST_DECL:
21913 break;
21915 default:
21916 return;
21919 gen_decl_die (decl, NULL, context_die);
21921 #ifdef ENABLE_CHECKING
21922 dw_die_ref die = lookup_decl_die (decl);
21923 if (die)
21924 check_die (die);
21925 #endif
21928 /* Write the debugging output for DECL. */
21930 static void
21931 dwarf2out_function_decl (tree decl)
21933 dwarf2out_decl (decl);
21934 call_arg_locations = NULL;
21935 call_arg_loc_last = NULL;
21936 call_site_count = -1;
21937 tail_call_site_count = -1;
21938 decl_loc_table->empty ();
21939 cached_dw_loc_list_table->empty ();
21942 /* Output a marker (i.e. a label) for the beginning of the generated code for
21943 a lexical block. */
21945 static void
21946 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
21947 unsigned int blocknum)
21949 switch_to_section (current_function_section ());
21950 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
21953 /* Output a marker (i.e. a label) for the end of the generated code for a
21954 lexical block. */
21956 static void
21957 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
21959 switch_to_section (current_function_section ());
21960 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
21963 /* Returns nonzero if it is appropriate not to emit any debugging
21964 information for BLOCK, because it doesn't contain any instructions.
21966 Don't allow this for blocks with nested functions or local classes
21967 as we would end up with orphans, and in the presence of scheduling
21968 we may end up calling them anyway. */
21970 static bool
21971 dwarf2out_ignore_block (const_tree block)
21973 tree decl;
21974 unsigned int i;
21976 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
21977 if (TREE_CODE (decl) == FUNCTION_DECL
21978 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21979 return 0;
21980 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
21982 decl = BLOCK_NONLOCALIZED_VAR (block, i);
21983 if (TREE_CODE (decl) == FUNCTION_DECL
21984 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21985 return 0;
21988 return 1;
21991 /* Hash table routines for file_hash. */
21993 bool
21994 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
21996 return filename_cmp (p1->filename, p2) == 0;
21999 hashval_t
22000 dwarf_file_hasher::hash (dwarf_file_data *p)
22002 return htab_hash_string (p->filename);
22005 /* Lookup FILE_NAME (in the list of filenames that we know about here in
22006 dwarf2out.c) and return its "index". The index of each (known) filename is
22007 just a unique number which is associated with only that one filename. We
22008 need such numbers for the sake of generating labels (in the .debug_sfnames
22009 section) and references to those files numbers (in the .debug_srcinfo
22010 and.debug_macinfo sections). If the filename given as an argument is not
22011 found in our current list, add it to the list and assign it the next
22012 available unique index number. */
22014 static struct dwarf_file_data *
22015 lookup_filename (const char *file_name)
22017 struct dwarf_file_data * created;
22019 if (!file_name)
22020 return NULL;
22022 dwarf_file_data **slot
22023 = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
22024 INSERT);
22025 if (*slot)
22026 return *slot;
22028 created = ggc_alloc<dwarf_file_data> ();
22029 created->filename = file_name;
22030 created->emitted_number = 0;
22031 *slot = created;
22032 return created;
22035 /* If the assembler will construct the file table, then translate the compiler
22036 internal file table number into the assembler file table number, and emit
22037 a .file directive if we haven't already emitted one yet. The file table
22038 numbers are different because we prune debug info for unused variables and
22039 types, which may include filenames. */
22041 static int
22042 maybe_emit_file (struct dwarf_file_data * fd)
22044 if (! fd->emitted_number)
22046 if (last_emitted_file)
22047 fd->emitted_number = last_emitted_file->emitted_number + 1;
22048 else
22049 fd->emitted_number = 1;
22050 last_emitted_file = fd;
22052 if (DWARF2_ASM_LINE_DEBUG_INFO)
22054 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
22055 output_quoted_string (asm_out_file,
22056 remap_debug_filename (fd->filename));
22057 fputc ('\n', asm_out_file);
22061 return fd->emitted_number;
22064 /* Schedule generation of a DW_AT_const_value attribute to DIE.
22065 That generation should happen after function debug info has been
22066 generated. The value of the attribute is the constant value of ARG. */
22068 static void
22069 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
22071 die_arg_entry entry;
22073 if (!die || !arg)
22074 return;
22076 if (!tmpl_value_parm_die_table)
22077 vec_alloc (tmpl_value_parm_die_table, 32);
22079 entry.die = die;
22080 entry.arg = arg;
22081 vec_safe_push (tmpl_value_parm_die_table, entry);
22084 /* Return TRUE if T is an instance of generic type, FALSE
22085 otherwise. */
22087 static bool
22088 generic_type_p (tree t)
22090 if (t == NULL_TREE || !TYPE_P (t))
22091 return false;
22092 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
22095 /* Schedule the generation of the generic parameter dies for the
22096 instance of generic type T. The proper generation itself is later
22097 done by gen_scheduled_generic_parms_dies. */
22099 static void
22100 schedule_generic_params_dies_gen (tree t)
22102 if (!generic_type_p (t))
22103 return;
22105 if (!generic_type_instances)
22106 vec_alloc (generic_type_instances, 256);
22108 vec_safe_push (generic_type_instances, t);
22111 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
22112 by append_entry_to_tmpl_value_parm_die_table. This function must
22113 be called after function DIEs have been generated. */
22115 static void
22116 gen_remaining_tmpl_value_param_die_attribute (void)
22118 if (tmpl_value_parm_die_table)
22120 unsigned i;
22121 die_arg_entry *e;
22123 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
22124 tree_add_const_value_attribute (e->die, e->arg);
22128 /* Generate generic parameters DIEs for instances of generic types
22129 that have been previously scheduled by
22130 schedule_generic_params_dies_gen. This function must be called
22131 after all the types of the CU have been laid out. */
22133 static void
22134 gen_scheduled_generic_parms_dies (void)
22136 unsigned i;
22137 tree t;
22139 if (!generic_type_instances)
22140 return;
22142 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
22143 if (COMPLETE_TYPE_P (t))
22144 gen_generic_params_dies (t);
22148 /* Replace DW_AT_name for the decl with name. */
22150 static void
22151 dwarf2out_set_name (tree decl, tree name)
22153 dw_die_ref die;
22154 dw_attr_ref attr;
22155 const char *dname;
22157 die = TYPE_SYMTAB_DIE (decl);
22158 if (!die)
22159 return;
22161 dname = dwarf2_name (name, 0);
22162 if (!dname)
22163 return;
22165 attr = get_AT (die, DW_AT_name);
22166 if (attr)
22168 struct indirect_string_node *node;
22170 node = find_AT_string (dname);
22171 /* replace the string. */
22172 attr->dw_attr_val.v.val_str = node;
22175 else
22176 add_name_attribute (die, dname);
22179 /* True if before or during processing of the first function being emitted. */
22180 static bool in_first_function_p = true;
22181 /* True if loc_note during dwarf2out_var_location call might still be
22182 before first real instruction at address equal to .Ltext0. */
22183 static bool maybe_at_text_label_p = true;
22184 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
22185 static unsigned int first_loclabel_num_not_at_text_label;
22187 /* Called by the final INSN scan whenever we see a var location. We
22188 use it to drop labels in the right places, and throw the location in
22189 our lookup table. */
22191 static void
22192 dwarf2out_var_location (rtx_insn *loc_note)
22194 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
22195 struct var_loc_node *newloc;
22196 rtx_insn *next_real, *next_note;
22197 static const char *last_label;
22198 static const char *last_postcall_label;
22199 static bool last_in_cold_section_p;
22200 static rtx_insn *expected_next_loc_note;
22201 tree decl;
22202 bool var_loc_p;
22204 if (!NOTE_P (loc_note))
22206 if (CALL_P (loc_note))
22208 call_site_count++;
22209 if (SIBLING_CALL_P (loc_note))
22210 tail_call_site_count++;
22212 return;
22215 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
22216 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
22217 return;
22219 /* Optimize processing a large consecutive sequence of location
22220 notes so we don't spend too much time in next_real_insn. If the
22221 next insn is another location note, remember the next_real_insn
22222 calculation for next time. */
22223 next_real = cached_next_real_insn;
22224 if (next_real)
22226 if (expected_next_loc_note != loc_note)
22227 next_real = NULL;
22230 next_note = NEXT_INSN (loc_note);
22231 if (! next_note
22232 || next_note->deleted ()
22233 || ! NOTE_P (next_note)
22234 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
22235 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
22236 next_note = NULL;
22238 if (! next_real)
22239 next_real = next_real_insn (loc_note);
22241 if (next_note)
22243 expected_next_loc_note = next_note;
22244 cached_next_real_insn = next_real;
22246 else
22247 cached_next_real_insn = NULL;
22249 /* If there are no instructions which would be affected by this note,
22250 don't do anything. */
22251 if (var_loc_p
22252 && next_real == NULL_RTX
22253 && !NOTE_DURING_CALL_P (loc_note))
22254 return;
22256 if (next_real == NULL_RTX)
22257 next_real = get_last_insn ();
22259 /* If there were any real insns between note we processed last time
22260 and this note (or if it is the first note), clear
22261 last_{,postcall_}label so that they are not reused this time. */
22262 if (last_var_location_insn == NULL_RTX
22263 || last_var_location_insn != next_real
22264 || last_in_cold_section_p != in_cold_section_p)
22266 last_label = NULL;
22267 last_postcall_label = NULL;
22270 if (var_loc_p)
22272 decl = NOTE_VAR_LOCATION_DECL (loc_note);
22273 newloc = add_var_loc_to_decl (decl, loc_note,
22274 NOTE_DURING_CALL_P (loc_note)
22275 ? last_postcall_label : last_label);
22276 if (newloc == NULL)
22277 return;
22279 else
22281 decl = NULL_TREE;
22282 newloc = NULL;
22285 /* If there were no real insns between note we processed last time
22286 and this note, use the label we emitted last time. Otherwise
22287 create a new label and emit it. */
22288 if (last_label == NULL)
22290 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
22291 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
22292 loclabel_num++;
22293 last_label = ggc_strdup (loclabel);
22294 /* See if loclabel might be equal to .Ltext0. If yes,
22295 bump first_loclabel_num_not_at_text_label. */
22296 if (!have_multiple_function_sections
22297 && in_first_function_p
22298 && maybe_at_text_label_p)
22300 static rtx_insn *last_start;
22301 rtx_insn *insn;
22302 for (insn = loc_note; insn; insn = previous_insn (insn))
22303 if (insn == last_start)
22304 break;
22305 else if (!NONDEBUG_INSN_P (insn))
22306 continue;
22307 else
22309 rtx body = PATTERN (insn);
22310 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
22311 continue;
22312 /* Inline asm could occupy zero bytes. */
22313 else if (GET_CODE (body) == ASM_INPUT
22314 || asm_noperands (body) >= 0)
22315 continue;
22316 #ifdef HAVE_attr_length
22317 else if (get_attr_min_length (insn) == 0)
22318 continue;
22319 #endif
22320 else
22322 /* Assume insn has non-zero length. */
22323 maybe_at_text_label_p = false;
22324 break;
22327 if (maybe_at_text_label_p)
22329 last_start = loc_note;
22330 first_loclabel_num_not_at_text_label = loclabel_num;
22335 if (!var_loc_p)
22337 struct call_arg_loc_node *ca_loc
22338 = ggc_cleared_alloc<call_arg_loc_node> ();
22339 rtx_insn *prev = prev_real_insn (loc_note);
22340 rtx x;
22341 ca_loc->call_arg_loc_note = loc_note;
22342 ca_loc->next = NULL;
22343 ca_loc->label = last_label;
22344 gcc_assert (prev
22345 && (CALL_P (prev)
22346 || (NONJUMP_INSN_P (prev)
22347 && GET_CODE (PATTERN (prev)) == SEQUENCE
22348 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
22349 if (!CALL_P (prev))
22350 prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
22351 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
22352 x = get_call_rtx_from (PATTERN (prev));
22353 if (x)
22355 x = XEXP (XEXP (x, 0), 0);
22356 if (GET_CODE (x) == SYMBOL_REF
22357 && SYMBOL_REF_DECL (x)
22358 && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
22359 ca_loc->symbol_ref = x;
22361 ca_loc->block = insn_scope (prev);
22362 if (call_arg_locations)
22363 call_arg_loc_last->next = ca_loc;
22364 else
22365 call_arg_locations = ca_loc;
22366 call_arg_loc_last = ca_loc;
22368 else if (!NOTE_DURING_CALL_P (loc_note))
22369 newloc->label = last_label;
22370 else
22372 if (!last_postcall_label)
22374 sprintf (loclabel, "%s-1", last_label);
22375 last_postcall_label = ggc_strdup (loclabel);
22377 newloc->label = last_postcall_label;
22380 last_var_location_insn = next_real;
22381 last_in_cold_section_p = in_cold_section_p;
22384 /* Note in one location list that text section has changed. */
22387 var_location_switch_text_section_1 (var_loc_list **slot, void *)
22389 var_loc_list *list = *slot;
22390 if (list->first)
22391 list->last_before_switch
22392 = list->last->next ? list->last->next : list->last;
22393 return 1;
22396 /* Note in all location lists that text section has changed. */
22398 static void
22399 var_location_switch_text_section (void)
22401 if (decl_loc_table == NULL)
22402 return;
22404 decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
22407 /* Create a new line number table. */
22409 static dw_line_info_table *
22410 new_line_info_table (void)
22412 dw_line_info_table *table;
22414 table = ggc_cleared_alloc<dw_line_info_table_struct> ();
22415 table->file_num = 1;
22416 table->line_num = 1;
22417 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
22419 return table;
22422 /* Lookup the "current" table into which we emit line info, so
22423 that we don't have to do it for every source line. */
22425 static void
22426 set_cur_line_info_table (section *sec)
22428 dw_line_info_table *table;
22430 if (sec == text_section)
22431 table = text_section_line_info;
22432 else if (sec == cold_text_section)
22434 table = cold_text_section_line_info;
22435 if (!table)
22437 cold_text_section_line_info = table = new_line_info_table ();
22438 table->end_label = cold_end_label;
22441 else
22443 const char *end_label;
22445 if (flag_reorder_blocks_and_partition)
22447 if (in_cold_section_p)
22448 end_label = crtl->subsections.cold_section_end_label;
22449 else
22450 end_label = crtl->subsections.hot_section_end_label;
22452 else
22454 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22455 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
22456 current_function_funcdef_no);
22457 end_label = ggc_strdup (label);
22460 table = new_line_info_table ();
22461 table->end_label = end_label;
22463 vec_safe_push (separate_line_info, table);
22466 if (DWARF2_ASM_LINE_DEBUG_INFO)
22467 table->is_stmt = (cur_line_info_table
22468 ? cur_line_info_table->is_stmt
22469 : DWARF_LINE_DEFAULT_IS_STMT_START);
22470 cur_line_info_table = table;
22474 /* We need to reset the locations at the beginning of each
22475 function. We can't do this in the end_function hook, because the
22476 declarations that use the locations won't have been output when
22477 that hook is called. Also compute have_multiple_function_sections here. */
22479 static void
22480 dwarf2out_begin_function (tree fun)
22482 section *sec = function_section (fun);
22484 if (sec != text_section)
22485 have_multiple_function_sections = true;
22487 if (flag_reorder_blocks_and_partition && !cold_text_section)
22489 gcc_assert (current_function_decl == fun);
22490 cold_text_section = unlikely_text_section ();
22491 switch_to_section (cold_text_section);
22492 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
22493 switch_to_section (sec);
22496 dwarf2out_note_section_used ();
22497 call_site_count = 0;
22498 tail_call_site_count = 0;
22500 set_cur_line_info_table (sec);
22503 /* Helper function of dwarf2out_end_function, called only after emitting
22504 the very first function into assembly. Check if some .debug_loc range
22505 might end with a .LVL* label that could be equal to .Ltext0.
22506 In that case we must force using absolute addresses in .debug_loc ranges,
22507 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
22508 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
22509 list terminator.
22510 Set have_multiple_function_sections to true in that case and
22511 terminate htab traversal. */
22514 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
22516 var_loc_list *entry = *slot;
22517 struct var_loc_node *node;
22519 node = entry->first;
22520 if (node && node->next && node->next->label)
22522 unsigned int i;
22523 const char *label = node->next->label;
22524 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
22526 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
22528 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
22529 if (strcmp (label, loclabel) == 0)
22531 have_multiple_function_sections = true;
22532 return 0;
22536 return 1;
22539 /* Hook called after emitting a function into assembly.
22540 This does something only for the very first function emitted. */
22542 static void
22543 dwarf2out_end_function (unsigned int)
22545 if (in_first_function_p
22546 && !have_multiple_function_sections
22547 && first_loclabel_num_not_at_text_label
22548 && decl_loc_table)
22549 decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
22550 in_first_function_p = false;
22551 maybe_at_text_label_p = false;
22554 /* Temporary holder for dwarf2out_register_main_translation_unit. Used to let
22555 front-ends register a translation unit even before dwarf2out_init is
22556 called. */
22557 static tree main_translation_unit = NULL_TREE;
22559 /* Hook called by front-ends after they built their main translation unit.
22560 Associate comp_unit_die to UNIT. */
22562 static void
22563 dwarf2out_register_main_translation_unit (tree unit)
22565 gcc_assert (TREE_CODE (unit) == TRANSLATION_UNIT_DECL
22566 && main_translation_unit == NULL_TREE);
22567 main_translation_unit = unit;
22568 /* If dwarf2out_init has not been called yet, it will perform the association
22569 itself looking at main_translation_unit. */
22570 if (decl_die_table != NULL)
22571 equate_decl_number_to_die (unit, comp_unit_die ());
22574 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
22576 static void
22577 push_dw_line_info_entry (dw_line_info_table *table,
22578 enum dw_line_info_opcode opcode, unsigned int val)
22580 dw_line_info_entry e;
22581 e.opcode = opcode;
22582 e.val = val;
22583 vec_safe_push (table->entries, e);
22586 /* Output a label to mark the beginning of a source code line entry
22587 and record information relating to this source line, in
22588 'line_info_table' for later output of the .debug_line section. */
22589 /* ??? The discriminator parameter ought to be unsigned. */
22591 static void
22592 dwarf2out_source_line (unsigned int line, const char *filename,
22593 int discriminator, bool is_stmt)
22595 unsigned int file_num;
22596 dw_line_info_table *table;
22598 if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
22599 return;
22601 /* The discriminator column was added in dwarf4. Simplify the below
22602 by simply removing it if we're not supposed to output it. */
22603 if (dwarf_version < 4 && dwarf_strict)
22604 discriminator = 0;
22606 table = cur_line_info_table;
22607 file_num = maybe_emit_file (lookup_filename (filename));
22609 /* ??? TODO: Elide duplicate line number entries. Traditionally,
22610 the debugger has used the second (possibly duplicate) line number
22611 at the beginning of the function to mark the end of the prologue.
22612 We could eliminate any other duplicates within the function. For
22613 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
22614 that second line number entry. */
22615 /* Recall that this end-of-prologue indication is *not* the same thing
22616 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
22617 to which the hook corresponds, follows the last insn that was
22618 emitted by gen_prologue. What we need is to precede the first insn
22619 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
22620 insn that corresponds to something the user wrote. These may be
22621 very different locations once scheduling is enabled. */
22623 if (0 && file_num == table->file_num
22624 && line == table->line_num
22625 && discriminator == table->discrim_num
22626 && is_stmt == table->is_stmt)
22627 return;
22629 switch_to_section (current_function_section ());
22631 /* If requested, emit something human-readable. */
22632 if (flag_debug_asm)
22633 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
22635 if (DWARF2_ASM_LINE_DEBUG_INFO)
22637 /* Emit the .loc directive understood by GNU as. */
22638 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
22639 file_num, line, is_stmt, discriminator */
22640 fputs ("\t.loc ", asm_out_file);
22641 fprint_ul (asm_out_file, file_num);
22642 putc (' ', asm_out_file);
22643 fprint_ul (asm_out_file, line);
22644 putc (' ', asm_out_file);
22645 putc ('0', asm_out_file);
22647 if (is_stmt != table->is_stmt)
22649 fputs (" is_stmt ", asm_out_file);
22650 putc (is_stmt ? '1' : '0', asm_out_file);
22652 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
22654 gcc_assert (discriminator > 0);
22655 fputs (" discriminator ", asm_out_file);
22656 fprint_ul (asm_out_file, (unsigned long) discriminator);
22658 putc ('\n', asm_out_file);
22660 else
22662 unsigned int label_num = ++line_info_label_num;
22664 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
22666 push_dw_line_info_entry (table, LI_set_address, label_num);
22667 if (file_num != table->file_num)
22668 push_dw_line_info_entry (table, LI_set_file, file_num);
22669 if (discriminator != table->discrim_num)
22670 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
22671 if (is_stmt != table->is_stmt)
22672 push_dw_line_info_entry (table, LI_negate_stmt, 0);
22673 push_dw_line_info_entry (table, LI_set_line, line);
22676 table->file_num = file_num;
22677 table->line_num = line;
22678 table->discrim_num = discriminator;
22679 table->is_stmt = is_stmt;
22680 table->in_use = true;
22683 /* Record the beginning of a new source file. */
22685 static void
22686 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
22688 if (flag_eliminate_dwarf2_dups)
22690 /* Record the beginning of the file for break_out_includes. */
22691 dw_die_ref bincl_die;
22693 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
22694 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
22697 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22699 macinfo_entry e;
22700 e.code = DW_MACINFO_start_file;
22701 e.lineno = lineno;
22702 e.info = ggc_strdup (filename);
22703 vec_safe_push (macinfo_table, e);
22707 /* Record the end of a source file. */
22709 static void
22710 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
22712 if (flag_eliminate_dwarf2_dups)
22713 /* Record the end of the file for break_out_includes. */
22714 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
22716 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22718 macinfo_entry e;
22719 e.code = DW_MACINFO_end_file;
22720 e.lineno = lineno;
22721 e.info = NULL;
22722 vec_safe_push (macinfo_table, e);
22726 /* Called from debug_define in toplev.c. The `buffer' parameter contains
22727 the tail part of the directive line, i.e. the part which is past the
22728 initial whitespace, #, whitespace, directive-name, whitespace part. */
22730 static void
22731 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
22732 const char *buffer ATTRIBUTE_UNUSED)
22734 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22736 macinfo_entry e;
22737 /* Insert a dummy first entry to be able to optimize the whole
22738 predefined macro block using DW_MACRO_GNU_transparent_include. */
22739 if (macinfo_table->is_empty () && lineno <= 1)
22741 e.code = 0;
22742 e.lineno = 0;
22743 e.info = NULL;
22744 vec_safe_push (macinfo_table, e);
22746 e.code = DW_MACINFO_define;
22747 e.lineno = lineno;
22748 e.info = ggc_strdup (buffer);
22749 vec_safe_push (macinfo_table, e);
22753 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
22754 the tail part of the directive line, i.e. the part which is past the
22755 initial whitespace, #, whitespace, directive-name, whitespace part. */
22757 static void
22758 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
22759 const char *buffer ATTRIBUTE_UNUSED)
22761 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22763 macinfo_entry e;
22764 /* Insert a dummy first entry to be able to optimize the whole
22765 predefined macro block using DW_MACRO_GNU_transparent_include. */
22766 if (macinfo_table->is_empty () && lineno <= 1)
22768 e.code = 0;
22769 e.lineno = 0;
22770 e.info = NULL;
22771 vec_safe_push (macinfo_table, e);
22773 e.code = DW_MACINFO_undef;
22774 e.lineno = lineno;
22775 e.info = ggc_strdup (buffer);
22776 vec_safe_push (macinfo_table, e);
22780 /* Helpers to manipulate hash table of CUs. */
22782 struct macinfo_entry_hasher : typed_noop_remove <macinfo_entry>
22784 typedef macinfo_entry *value_type;
22785 typedef macinfo_entry *compare_type;
22786 static inline hashval_t hash (const macinfo_entry *);
22787 static inline bool equal (const macinfo_entry *, const macinfo_entry *);
22790 inline hashval_t
22791 macinfo_entry_hasher::hash (const macinfo_entry *entry)
22793 return htab_hash_string (entry->info);
22796 inline bool
22797 macinfo_entry_hasher::equal (const macinfo_entry *entry1,
22798 const macinfo_entry *entry2)
22800 return !strcmp (entry1->info, entry2->info);
22803 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
22805 /* Output a single .debug_macinfo entry. */
22807 static void
22808 output_macinfo_op (macinfo_entry *ref)
22810 int file_num;
22811 size_t len;
22812 struct indirect_string_node *node;
22813 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22814 struct dwarf_file_data *fd;
22816 switch (ref->code)
22818 case DW_MACINFO_start_file:
22819 fd = lookup_filename (ref->info);
22820 file_num = maybe_emit_file (fd);
22821 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
22822 dw2_asm_output_data_uleb128 (ref->lineno,
22823 "Included from line number %lu",
22824 (unsigned long) ref->lineno);
22825 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
22826 break;
22827 case DW_MACINFO_end_file:
22828 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
22829 break;
22830 case DW_MACINFO_define:
22831 case DW_MACINFO_undef:
22832 len = strlen (ref->info) + 1;
22833 if (!dwarf_strict
22834 && len > DWARF_OFFSET_SIZE
22835 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
22836 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
22838 ref->code = ref->code == DW_MACINFO_define
22839 ? DW_MACRO_GNU_define_indirect
22840 : DW_MACRO_GNU_undef_indirect;
22841 output_macinfo_op (ref);
22842 return;
22844 dw2_asm_output_data (1, ref->code,
22845 ref->code == DW_MACINFO_define
22846 ? "Define macro" : "Undefine macro");
22847 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
22848 (unsigned long) ref->lineno);
22849 dw2_asm_output_nstring (ref->info, -1, "The macro");
22850 break;
22851 case DW_MACRO_GNU_define_indirect:
22852 case DW_MACRO_GNU_undef_indirect:
22853 node = find_AT_string (ref->info);
22854 gcc_assert (node
22855 && ((node->form == DW_FORM_strp)
22856 || (node->form == DW_FORM_GNU_str_index)));
22857 dw2_asm_output_data (1, ref->code,
22858 ref->code == DW_MACRO_GNU_define_indirect
22859 ? "Define macro indirect"
22860 : "Undefine macro indirect");
22861 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
22862 (unsigned long) ref->lineno);
22863 if (node->form == DW_FORM_strp)
22864 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
22865 debug_str_section, "The macro: \"%s\"",
22866 ref->info);
22867 else
22868 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
22869 ref->info);
22870 break;
22871 case DW_MACRO_GNU_transparent_include:
22872 dw2_asm_output_data (1, ref->code, "Transparent include");
22873 ASM_GENERATE_INTERNAL_LABEL (label,
22874 DEBUG_MACRO_SECTION_LABEL, ref->lineno);
22875 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
22876 break;
22877 default:
22878 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
22879 ASM_COMMENT_START, (unsigned long) ref->code);
22880 break;
22884 /* Attempt to make a sequence of define/undef macinfo ops shareable with
22885 other compilation unit .debug_macinfo sections. IDX is the first
22886 index of a define/undef, return the number of ops that should be
22887 emitted in a comdat .debug_macinfo section and emit
22888 a DW_MACRO_GNU_transparent_include entry referencing it.
22889 If the define/undef entry should be emitted normally, return 0. */
22891 static unsigned
22892 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
22893 macinfo_hash_type **macinfo_htab)
22895 macinfo_entry *first, *second, *cur, *inc;
22896 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
22897 unsigned char checksum[16];
22898 struct md5_ctx ctx;
22899 char *grp_name, *tail;
22900 const char *base;
22901 unsigned int i, count, encoded_filename_len, linebuf_len;
22902 macinfo_entry **slot;
22904 first = &(*macinfo_table)[idx];
22905 second = &(*macinfo_table)[idx + 1];
22907 /* Optimize only if there are at least two consecutive define/undef ops,
22908 and either all of them are before first DW_MACINFO_start_file
22909 with lineno {0,1} (i.e. predefined macro block), or all of them are
22910 in some included header file. */
22911 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
22912 return 0;
22913 if (vec_safe_is_empty (files))
22915 if (first->lineno > 1 || second->lineno > 1)
22916 return 0;
22918 else if (first->lineno == 0)
22919 return 0;
22921 /* Find the last define/undef entry that can be grouped together
22922 with first and at the same time compute md5 checksum of their
22923 codes, linenumbers and strings. */
22924 md5_init_ctx (&ctx);
22925 for (i = idx; macinfo_table->iterate (i, &cur); i++)
22926 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
22927 break;
22928 else if (vec_safe_is_empty (files) && cur->lineno > 1)
22929 break;
22930 else
22932 unsigned char code = cur->code;
22933 md5_process_bytes (&code, 1, &ctx);
22934 checksum_uleb128 (cur->lineno, &ctx);
22935 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
22937 md5_finish_ctx (&ctx, checksum);
22938 count = i - idx;
22940 /* From the containing include filename (if any) pick up just
22941 usable characters from its basename. */
22942 if (vec_safe_is_empty (files))
22943 base = "";
22944 else
22945 base = lbasename (files->last ().info);
22946 for (encoded_filename_len = 0, i = 0; base[i]; i++)
22947 if (ISIDNUM (base[i]) || base[i] == '.')
22948 encoded_filename_len++;
22949 /* Count . at the end. */
22950 if (encoded_filename_len)
22951 encoded_filename_len++;
22953 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
22954 linebuf_len = strlen (linebuf);
22956 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
22957 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
22958 + 16 * 2 + 1);
22959 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
22960 tail = grp_name + 4;
22961 if (encoded_filename_len)
22963 for (i = 0; base[i]; i++)
22964 if (ISIDNUM (base[i]) || base[i] == '.')
22965 *tail++ = base[i];
22966 *tail++ = '.';
22968 memcpy (tail, linebuf, linebuf_len);
22969 tail += linebuf_len;
22970 *tail++ = '.';
22971 for (i = 0; i < 16; i++)
22972 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
22974 /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
22975 in the empty vector entry before the first define/undef. */
22976 inc = &(*macinfo_table)[idx - 1];
22977 inc->code = DW_MACRO_GNU_transparent_include;
22978 inc->lineno = 0;
22979 inc->info = ggc_strdup (grp_name);
22980 if (!*macinfo_htab)
22981 *macinfo_htab = new macinfo_hash_type (10);
22982 /* Avoid emitting duplicates. */
22983 slot = (*macinfo_htab)->find_slot (inc, INSERT);
22984 if (*slot != NULL)
22986 inc->code = 0;
22987 inc->info = NULL;
22988 /* If such an entry has been used before, just emit
22989 a DW_MACRO_GNU_transparent_include op. */
22990 inc = *slot;
22991 output_macinfo_op (inc);
22992 /* And clear all macinfo_entry in the range to avoid emitting them
22993 in the second pass. */
22994 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
22996 cur->code = 0;
22997 cur->info = NULL;
23000 else
23002 *slot = inc;
23003 inc->lineno = (*macinfo_htab)->elements ();
23004 output_macinfo_op (inc);
23006 return count;
23009 /* Save any strings needed by the macinfo table in the debug str
23010 table. All strings must be collected into the table by the time
23011 index_string is called. */
23013 static void
23014 save_macinfo_strings (void)
23016 unsigned len;
23017 unsigned i;
23018 macinfo_entry *ref;
23020 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
23022 switch (ref->code)
23024 /* Match the logic in output_macinfo_op to decide on
23025 indirect strings. */
23026 case DW_MACINFO_define:
23027 case DW_MACINFO_undef:
23028 len = strlen (ref->info) + 1;
23029 if (!dwarf_strict
23030 && len > DWARF_OFFSET_SIZE
23031 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
23032 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
23033 set_indirect_string (find_AT_string (ref->info));
23034 break;
23035 case DW_MACRO_GNU_define_indirect:
23036 case DW_MACRO_GNU_undef_indirect:
23037 set_indirect_string (find_AT_string (ref->info));
23038 break;
23039 default:
23040 break;
23045 /* Output macinfo section(s). */
23047 static void
23048 output_macinfo (void)
23050 unsigned i;
23051 unsigned long length = vec_safe_length (macinfo_table);
23052 macinfo_entry *ref;
23053 vec<macinfo_entry, va_gc> *files = NULL;
23054 macinfo_hash_type *macinfo_htab = NULL;
23056 if (! length)
23057 return;
23059 /* output_macinfo* uses these interchangeably. */
23060 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
23061 && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
23062 && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
23063 && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
23065 /* For .debug_macro emit the section header. */
23066 if (!dwarf_strict)
23068 dw2_asm_output_data (2, 4, "DWARF macro version number");
23069 if (DWARF_OFFSET_SIZE == 8)
23070 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
23071 else
23072 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
23073 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
23074 (!dwarf_split_debug_info ? debug_line_section_label
23075 : debug_skeleton_line_section_label),
23076 debug_line_section, NULL);
23079 /* In the first loop, it emits the primary .debug_macinfo section
23080 and after each emitted op the macinfo_entry is cleared.
23081 If a longer range of define/undef ops can be optimized using
23082 DW_MACRO_GNU_transparent_include, the
23083 DW_MACRO_GNU_transparent_include op is emitted and kept in
23084 the vector before the first define/undef in the range and the
23085 whole range of define/undef ops is not emitted and kept. */
23086 for (i = 0; macinfo_table->iterate (i, &ref); i++)
23088 switch (ref->code)
23090 case DW_MACINFO_start_file:
23091 vec_safe_push (files, *ref);
23092 break;
23093 case DW_MACINFO_end_file:
23094 if (!vec_safe_is_empty (files))
23095 files->pop ();
23096 break;
23097 case DW_MACINFO_define:
23098 case DW_MACINFO_undef:
23099 if (!dwarf_strict
23100 && HAVE_COMDAT_GROUP
23101 && vec_safe_length (files) != 1
23102 && i > 0
23103 && i + 1 < length
23104 && (*macinfo_table)[i - 1].code == 0)
23106 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
23107 if (count)
23109 i += count - 1;
23110 continue;
23113 break;
23114 case 0:
23115 /* A dummy entry may be inserted at the beginning to be able
23116 to optimize the whole block of predefined macros. */
23117 if (i == 0)
23118 continue;
23119 default:
23120 break;
23122 output_macinfo_op (ref);
23123 ref->info = NULL;
23124 ref->code = 0;
23127 if (!macinfo_htab)
23128 return;
23130 delete macinfo_htab;
23131 macinfo_htab = NULL;
23133 /* If any DW_MACRO_GNU_transparent_include were used, on those
23134 DW_MACRO_GNU_transparent_include entries terminate the
23135 current chain and switch to a new comdat .debug_macinfo
23136 section and emit the define/undef entries within it. */
23137 for (i = 0; macinfo_table->iterate (i, &ref); i++)
23138 switch (ref->code)
23140 case 0:
23141 continue;
23142 case DW_MACRO_GNU_transparent_include:
23144 char label[MAX_ARTIFICIAL_LABEL_BYTES];
23145 tree comdat_key = get_identifier (ref->info);
23146 /* Terminate the previous .debug_macinfo section. */
23147 dw2_asm_output_data (1, 0, "End compilation unit");
23148 targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
23149 SECTION_DEBUG
23150 | SECTION_LINKONCE,
23151 comdat_key);
23152 ASM_GENERATE_INTERNAL_LABEL (label,
23153 DEBUG_MACRO_SECTION_LABEL,
23154 ref->lineno);
23155 ASM_OUTPUT_LABEL (asm_out_file, label);
23156 ref->code = 0;
23157 ref->info = NULL;
23158 dw2_asm_output_data (2, 4, "DWARF macro version number");
23159 if (DWARF_OFFSET_SIZE == 8)
23160 dw2_asm_output_data (1, 1, "Flags: 64-bit");
23161 else
23162 dw2_asm_output_data (1, 0, "Flags: 32-bit");
23164 break;
23165 case DW_MACINFO_define:
23166 case DW_MACINFO_undef:
23167 output_macinfo_op (ref);
23168 ref->code = 0;
23169 ref->info = NULL;
23170 break;
23171 default:
23172 gcc_unreachable ();
23176 /* Set up for Dwarf output at the start of compilation. */
23178 static void
23179 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
23181 /* This option is currently broken, see (PR53118 and PR46102). */
23182 if (flag_eliminate_dwarf2_dups
23183 && strstr (lang_hooks.name, "C++"))
23185 warning (0, "-feliminate-dwarf2-dups is broken for C++, ignoring");
23186 flag_eliminate_dwarf2_dups = 0;
23189 /* Allocate the file_table. */
23190 file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
23192 /* Allocate the decl_die_table. */
23193 decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
23195 /* Allocate the decl_loc_table. */
23196 decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
23198 /* Allocate the cached_dw_loc_list_table. */
23199 cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
23201 /* Allocate the initial hunk of the decl_scope_table. */
23202 vec_alloc (decl_scope_table, 256);
23204 /* Allocate the initial hunk of the abbrev_die_table. */
23205 abbrev_die_table = ggc_cleared_vec_alloc<dw_die_ref>
23206 (ABBREV_DIE_TABLE_INCREMENT);
23207 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
23208 /* Zero-th entry is allocated, but unused. */
23209 abbrev_die_table_in_use = 1;
23211 /* Allocate the pubtypes and pubnames vectors. */
23212 vec_alloc (pubname_table, 32);
23213 vec_alloc (pubtype_table, 32);
23215 vec_alloc (incomplete_types, 64);
23217 vec_alloc (used_rtx_array, 32);
23219 if (!dwarf_split_debug_info)
23221 debug_info_section = get_section (DEBUG_INFO_SECTION,
23222 SECTION_DEBUG, NULL);
23223 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
23224 SECTION_DEBUG, NULL);
23225 debug_loc_section = get_section (DEBUG_LOC_SECTION,
23226 SECTION_DEBUG, NULL);
23228 else
23230 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
23231 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
23232 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
23233 SECTION_DEBUG | SECTION_EXCLUDE,
23234 NULL);
23235 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
23236 SECTION_DEBUG, NULL);
23237 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
23238 SECTION_DEBUG, NULL);
23239 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
23240 SECTION_DEBUG, NULL);
23241 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
23242 DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
23244 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
23245 the main .o, but the skeleton_line goes into the split off dwo. */
23246 debug_skeleton_line_section
23247 = get_section (DEBUG_DWO_LINE_SECTION,
23248 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
23249 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
23250 DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
23251 debug_str_offsets_section = get_section (DEBUG_STR_OFFSETS_SECTION,
23252 SECTION_DEBUG | SECTION_EXCLUDE,
23253 NULL);
23254 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
23255 DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
23256 debug_loc_section = get_section (DEBUG_DWO_LOC_SECTION,
23257 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
23258 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
23259 DEBUG_STR_DWO_SECTION_FLAGS, NULL);
23261 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
23262 SECTION_DEBUG, NULL);
23263 debug_macinfo_section = get_section (dwarf_strict
23264 ? DEBUG_MACINFO_SECTION
23265 : DEBUG_MACRO_SECTION,
23266 DEBUG_MACRO_SECTION_FLAGS, NULL);
23267 debug_line_section = get_section (DEBUG_LINE_SECTION,
23268 SECTION_DEBUG, NULL);
23269 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
23270 SECTION_DEBUG, NULL);
23271 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
23272 SECTION_DEBUG, NULL);
23273 debug_str_section = get_section (DEBUG_STR_SECTION,
23274 DEBUG_STR_SECTION_FLAGS, NULL);
23275 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
23276 SECTION_DEBUG, NULL);
23277 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
23278 SECTION_DEBUG, NULL);
23280 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
23281 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
23282 DEBUG_ABBREV_SECTION_LABEL, 0);
23283 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
23284 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
23285 COLD_TEXT_SECTION_LABEL, 0);
23286 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
23288 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
23289 DEBUG_INFO_SECTION_LABEL, 0);
23290 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
23291 DEBUG_LINE_SECTION_LABEL, 0);
23292 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
23293 DEBUG_RANGES_SECTION_LABEL, 0);
23294 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
23295 DEBUG_ADDR_SECTION_LABEL, 0);
23296 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
23297 dwarf_strict
23298 ? DEBUG_MACINFO_SECTION_LABEL
23299 : DEBUG_MACRO_SECTION_LABEL, 0);
23300 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
23302 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
23303 vec_alloc (macinfo_table, 64);
23305 switch_to_section (text_section);
23306 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
23308 /* Make sure the line number table for .text always exists. */
23309 text_section_line_info = new_line_info_table ();
23310 text_section_line_info->end_label = text_end_label;
23312 /* If front-ends already registered a main translation unit but we were not
23313 ready to perform the association, do this now. */
23314 if (main_translation_unit != NULL_TREE)
23315 equate_decl_number_to_die (main_translation_unit, comp_unit_die ());
23318 /* Called before compile () starts outputtting functions, variables
23319 and toplevel asms into assembly. */
23321 static void
23322 dwarf2out_assembly_start (void)
23324 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
23325 && dwarf2out_do_cfi_asm ()
23326 && (!(flag_unwind_tables || flag_exceptions)
23327 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
23328 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
23331 /* A helper function for dwarf2out_finish called through
23332 htab_traverse. Assign a string its index. All strings must be
23333 collected into the table by the time index_string is called,
23334 because the indexing code relies on htab_traverse to traverse nodes
23335 in the same order for each run. */
23338 index_string (indirect_string_node **h, unsigned int *index)
23340 indirect_string_node *node = *h;
23342 find_string_form (node);
23343 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
23345 gcc_assert (node->index == NO_INDEX_ASSIGNED);
23346 node->index = *index;
23347 *index += 1;
23349 return 1;
23352 /* A helper function for output_indirect_strings called through
23353 htab_traverse. Output the offset to a string and update the
23354 current offset. */
23357 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
23359 indirect_string_node *node = *h;
23361 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
23363 /* Assert that this node has been assigned an index. */
23364 gcc_assert (node->index != NO_INDEX_ASSIGNED
23365 && node->index != NOT_INDEXED);
23366 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
23367 "indexed string 0x%x: %s", node->index, node->str);
23368 *offset += strlen (node->str) + 1;
23370 return 1;
23373 /* A helper function for dwarf2out_finish called through
23374 htab_traverse. Output the indexed string. */
23377 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
23379 struct indirect_string_node *node = *h;
23381 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
23383 /* Assert that the strings are output in the same order as their
23384 indexes were assigned. */
23385 gcc_assert (*cur_idx == node->index);
23386 assemble_string (node->str, strlen (node->str) + 1);
23387 *cur_idx += 1;
23389 return 1;
23392 /* A helper function for dwarf2out_finish called through
23393 htab_traverse. Emit one queued .debug_str string. */
23396 output_indirect_string (indirect_string_node **h, void *)
23398 struct indirect_string_node *node = *h;
23400 node->form = find_string_form (node);
23401 if (node->form == DW_FORM_strp && node->refcount > 0)
23403 ASM_OUTPUT_LABEL (asm_out_file, node->label);
23404 assemble_string (node->str, strlen (node->str) + 1);
23407 return 1;
23410 /* Output the indexed string table. */
23412 static void
23413 output_indirect_strings (void)
23415 switch_to_section (debug_str_section);
23416 if (!dwarf_split_debug_info)
23417 debug_str_hash->traverse<void *, output_indirect_string> (NULL);
23418 else
23420 unsigned int offset = 0;
23421 unsigned int cur_idx = 0;
23423 skeleton_debug_str_hash->traverse<void *, output_indirect_string> (NULL);
23425 switch_to_section (debug_str_offsets_section);
23426 debug_str_hash->traverse_noresize
23427 <unsigned int *, output_index_string_offset> (&offset);
23428 switch_to_section (debug_str_dwo_section);
23429 debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
23430 (&cur_idx);
23434 /* Callback for htab_traverse to assign an index to an entry in the
23435 table, and to write that entry to the .debug_addr section. */
23438 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
23440 addr_table_entry *entry = *slot;
23442 if (entry->refcount == 0)
23444 gcc_assert (entry->index == NO_INDEX_ASSIGNED
23445 || entry->index == NOT_INDEXED);
23446 return 1;
23449 gcc_assert (entry->index == *cur_index);
23450 (*cur_index)++;
23452 switch (entry->kind)
23454 case ate_kind_rtx:
23455 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
23456 "0x%x", entry->index);
23457 break;
23458 case ate_kind_rtx_dtprel:
23459 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
23460 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
23461 DWARF2_ADDR_SIZE,
23462 entry->addr.rtl);
23463 fputc ('\n', asm_out_file);
23464 break;
23465 case ate_kind_label:
23466 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
23467 "0x%x", entry->index);
23468 break;
23469 default:
23470 gcc_unreachable ();
23472 return 1;
23475 /* Produce the .debug_addr section. */
23477 static void
23478 output_addr_table (void)
23480 unsigned int index = 0;
23481 if (addr_index_table == NULL || addr_index_table->size () == 0)
23482 return;
23484 switch_to_section (debug_addr_section);
23485 addr_index_table
23486 ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
23489 #if ENABLE_ASSERT_CHECKING
23490 /* Verify that all marks are clear. */
23492 static void
23493 verify_marks_clear (dw_die_ref die)
23495 dw_die_ref c;
23497 gcc_assert (! die->die_mark);
23498 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
23500 #endif /* ENABLE_ASSERT_CHECKING */
23502 /* Clear the marks for a die and its children.
23503 Be cool if the mark isn't set. */
23505 static void
23506 prune_unmark_dies (dw_die_ref die)
23508 dw_die_ref c;
23510 if (die->die_mark)
23511 die->die_mark = 0;
23512 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
23515 /* Given DIE that we're marking as used, find any other dies
23516 it references as attributes and mark them as used. */
23518 static void
23519 prune_unused_types_walk_attribs (dw_die_ref die)
23521 dw_attr_ref a;
23522 unsigned ix;
23524 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23526 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
23528 /* A reference to another DIE.
23529 Make sure that it will get emitted.
23530 If it was broken out into a comdat group, don't follow it. */
23531 if (! AT_ref (a)->comdat_type_p
23532 || a->dw_attr == DW_AT_specification)
23533 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
23535 /* Set the string's refcount to 0 so that prune_unused_types_mark
23536 accounts properly for it. */
23537 if (AT_class (a) == dw_val_class_str)
23538 a->dw_attr_val.v.val_str->refcount = 0;
23542 /* Mark the generic parameters and arguments children DIEs of DIE. */
23544 static void
23545 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
23547 dw_die_ref c;
23549 if (die == NULL || die->die_child == NULL)
23550 return;
23551 c = die->die_child;
23554 if (is_template_parameter (c))
23555 prune_unused_types_mark (c, 1);
23556 c = c->die_sib;
23557 } while (c && c != die->die_child);
23560 /* Mark DIE as being used. If DOKIDS is true, then walk down
23561 to DIE's children. */
23563 static void
23564 prune_unused_types_mark (dw_die_ref die, int dokids)
23566 dw_die_ref c;
23568 if (die->die_mark == 0)
23570 /* We haven't done this node yet. Mark it as used. */
23571 die->die_mark = 1;
23572 /* If this is the DIE of a generic type instantiation,
23573 mark the children DIEs that describe its generic parms and
23574 args. */
23575 prune_unused_types_mark_generic_parms_dies (die);
23577 /* We also have to mark its parents as used.
23578 (But we don't want to mark our parent's kids due to this,
23579 unless it is a class.) */
23580 if (die->die_parent)
23581 prune_unused_types_mark (die->die_parent,
23582 class_scope_p (die->die_parent));
23584 /* Mark any referenced nodes. */
23585 prune_unused_types_walk_attribs (die);
23587 /* If this node is a specification,
23588 also mark the definition, if it exists. */
23589 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
23590 prune_unused_types_mark (die->die_definition, 1);
23593 if (dokids && die->die_mark != 2)
23595 /* We need to walk the children, but haven't done so yet.
23596 Remember that we've walked the kids. */
23597 die->die_mark = 2;
23599 /* If this is an array type, we need to make sure our
23600 kids get marked, even if they're types. If we're
23601 breaking out types into comdat sections, do this
23602 for all type definitions. */
23603 if (die->die_tag == DW_TAG_array_type
23604 || (use_debug_types
23605 && is_type_die (die) && ! is_declaration_die (die)))
23606 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
23607 else
23608 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
23612 /* For local classes, look if any static member functions were emitted
23613 and if so, mark them. */
23615 static void
23616 prune_unused_types_walk_local_classes (dw_die_ref die)
23618 dw_die_ref c;
23620 if (die->die_mark == 2)
23621 return;
23623 switch (die->die_tag)
23625 case DW_TAG_structure_type:
23626 case DW_TAG_union_type:
23627 case DW_TAG_class_type:
23628 break;
23630 case DW_TAG_subprogram:
23631 if (!get_AT_flag (die, DW_AT_declaration)
23632 || die->die_definition != NULL)
23633 prune_unused_types_mark (die, 1);
23634 return;
23636 default:
23637 return;
23640 /* Mark children. */
23641 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
23644 /* Walk the tree DIE and mark types that we actually use. */
23646 static void
23647 prune_unused_types_walk (dw_die_ref die)
23649 dw_die_ref c;
23651 /* Don't do anything if this node is already marked and
23652 children have been marked as well. */
23653 if (die->die_mark == 2)
23654 return;
23656 switch (die->die_tag)
23658 case DW_TAG_structure_type:
23659 case DW_TAG_union_type:
23660 case DW_TAG_class_type:
23661 if (die->die_perennial_p)
23662 break;
23664 for (c = die->die_parent; c; c = c->die_parent)
23665 if (c->die_tag == DW_TAG_subprogram)
23666 break;
23668 /* Finding used static member functions inside of classes
23669 is needed just for local classes, because for other classes
23670 static member function DIEs with DW_AT_specification
23671 are emitted outside of the DW_TAG_*_type. If we ever change
23672 it, we'd need to call this even for non-local classes. */
23673 if (c)
23674 prune_unused_types_walk_local_classes (die);
23676 /* It's a type node --- don't mark it. */
23677 return;
23679 case DW_TAG_const_type:
23680 case DW_TAG_packed_type:
23681 case DW_TAG_pointer_type:
23682 case DW_TAG_reference_type:
23683 case DW_TAG_rvalue_reference_type:
23684 case DW_TAG_volatile_type:
23685 case DW_TAG_typedef:
23686 case DW_TAG_array_type:
23687 case DW_TAG_interface_type:
23688 case DW_TAG_friend:
23689 case DW_TAG_variant_part:
23690 case DW_TAG_enumeration_type:
23691 case DW_TAG_subroutine_type:
23692 case DW_TAG_string_type:
23693 case DW_TAG_set_type:
23694 case DW_TAG_subrange_type:
23695 case DW_TAG_ptr_to_member_type:
23696 case DW_TAG_file_type:
23697 if (die->die_perennial_p)
23698 break;
23700 /* It's a type node --- don't mark it. */
23701 return;
23703 default:
23704 /* Mark everything else. */
23705 break;
23708 if (die->die_mark == 0)
23710 die->die_mark = 1;
23712 /* Now, mark any dies referenced from here. */
23713 prune_unused_types_walk_attribs (die);
23716 die->die_mark = 2;
23718 /* Mark children. */
23719 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
23722 /* Increment the string counts on strings referred to from DIE's
23723 attributes. */
23725 static void
23726 prune_unused_types_update_strings (dw_die_ref die)
23728 dw_attr_ref a;
23729 unsigned ix;
23731 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23732 if (AT_class (a) == dw_val_class_str)
23734 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
23735 s->refcount++;
23736 /* Avoid unnecessarily putting strings that are used less than
23737 twice in the hash table. */
23738 if (s->refcount
23739 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
23741 indirect_string_node **slot
23742 = debug_str_hash->find_slot_with_hash (s->str,
23743 htab_hash_string (s->str),
23744 INSERT);
23745 gcc_assert (*slot == NULL);
23746 *slot = s;
23751 /* Remove from the tree DIE any dies that aren't marked. */
23753 static void
23754 prune_unused_types_prune (dw_die_ref die)
23756 dw_die_ref c;
23758 gcc_assert (die->die_mark);
23759 prune_unused_types_update_strings (die);
23761 if (! die->die_child)
23762 return;
23764 c = die->die_child;
23765 do {
23766 dw_die_ref prev = c;
23767 for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
23768 if (c == die->die_child)
23770 /* No marked children between 'prev' and the end of the list. */
23771 if (prev == c)
23772 /* No marked children at all. */
23773 die->die_child = NULL;
23774 else
23776 prev->die_sib = c->die_sib;
23777 die->die_child = prev;
23779 return;
23782 if (c != prev->die_sib)
23783 prev->die_sib = c;
23784 prune_unused_types_prune (c);
23785 } while (c != die->die_child);
23788 /* Remove dies representing declarations that we never use. */
23790 static void
23791 prune_unused_types (void)
23793 unsigned int i;
23794 limbo_die_node *node;
23795 comdat_type_node *ctnode;
23796 pubname_ref pub;
23797 dw_die_ref base_type;
23799 #if ENABLE_ASSERT_CHECKING
23800 /* All the marks should already be clear. */
23801 verify_marks_clear (comp_unit_die ());
23802 for (node = limbo_die_list; node; node = node->next)
23803 verify_marks_clear (node->die);
23804 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23805 verify_marks_clear (ctnode->root_die);
23806 #endif /* ENABLE_ASSERT_CHECKING */
23808 /* Mark types that are used in global variables. */
23809 premark_types_used_by_global_vars ();
23811 /* Set the mark on nodes that are actually used. */
23812 prune_unused_types_walk (comp_unit_die ());
23813 for (node = limbo_die_list; node; node = node->next)
23814 prune_unused_types_walk (node->die);
23815 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23817 prune_unused_types_walk (ctnode->root_die);
23818 prune_unused_types_mark (ctnode->type_die, 1);
23821 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
23822 are unusual in that they are pubnames that are the children of pubtypes.
23823 They should only be marked via their parent DW_TAG_enumeration_type die,
23824 not as roots in themselves. */
23825 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
23826 if (pub->die->die_tag != DW_TAG_enumerator)
23827 prune_unused_types_mark (pub->die, 1);
23828 for (i = 0; base_types.iterate (i, &base_type); i++)
23829 prune_unused_types_mark (base_type, 1);
23831 if (debug_str_hash)
23832 debug_str_hash->empty ();
23833 if (skeleton_debug_str_hash)
23834 skeleton_debug_str_hash->empty ();
23835 prune_unused_types_prune (comp_unit_die ());
23836 for (limbo_die_node **pnode = &limbo_die_list; *pnode; )
23838 node = *pnode;
23839 if (!node->die->die_mark)
23840 *pnode = node->next;
23841 else
23843 prune_unused_types_prune (node->die);
23844 pnode = &node->next;
23847 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23848 prune_unused_types_prune (ctnode->root_die);
23850 /* Leave the marks clear. */
23851 prune_unmark_dies (comp_unit_die ());
23852 for (node = limbo_die_list; node; node = node->next)
23853 prune_unmark_dies (node->die);
23854 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23855 prune_unmark_dies (ctnode->root_die);
23858 /* Set the parameter to true if there are any relative pathnames in
23859 the file table. */
23861 file_table_relative_p (dwarf_file_data **slot, bool *p)
23863 struct dwarf_file_data *d = *slot;
23864 if (!IS_ABSOLUTE_PATH (d->filename))
23866 *p = true;
23867 return 0;
23869 return 1;
23872 /* Helpers to manipulate hash table of comdat type units. */
23874 struct comdat_type_hasher : typed_noop_remove <comdat_type_node>
23876 typedef comdat_type_node *value_type;
23877 typedef comdat_type_node *compare_type;
23878 static inline hashval_t hash (const comdat_type_node *);
23879 static inline bool equal (const comdat_type_node *, const comdat_type_node *);
23882 inline hashval_t
23883 comdat_type_hasher::hash (const comdat_type_node *type_node)
23885 hashval_t h;
23886 memcpy (&h, type_node->signature, sizeof (h));
23887 return h;
23890 inline bool
23891 comdat_type_hasher::equal (const comdat_type_node *type_node_1,
23892 const comdat_type_node *type_node_2)
23894 return (! memcmp (type_node_1->signature, type_node_2->signature,
23895 DWARF_TYPE_SIGNATURE_SIZE));
23898 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
23899 to the location it would have been added, should we know its
23900 DECL_ASSEMBLER_NAME when we added other attributes. This will
23901 probably improve compactness of debug info, removing equivalent
23902 abbrevs, and hide any differences caused by deferring the
23903 computation of the assembler name, triggered by e.g. PCH. */
23905 static inline void
23906 move_linkage_attr (dw_die_ref die)
23908 unsigned ix = vec_safe_length (die->die_attr);
23909 dw_attr_node linkage = (*die->die_attr)[ix - 1];
23911 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
23912 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
23914 while (--ix > 0)
23916 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
23918 if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
23919 break;
23922 if (ix != vec_safe_length (die->die_attr) - 1)
23924 die->die_attr->pop ();
23925 die->die_attr->quick_insert (ix, linkage);
23929 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
23930 referenced from typed stack ops and count how often they are used. */
23932 static void
23933 mark_base_types (dw_loc_descr_ref loc)
23935 dw_die_ref base_type = NULL;
23937 for (; loc; loc = loc->dw_loc_next)
23939 switch (loc->dw_loc_opc)
23941 case DW_OP_GNU_regval_type:
23942 case DW_OP_GNU_deref_type:
23943 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
23944 break;
23945 case DW_OP_GNU_convert:
23946 case DW_OP_GNU_reinterpret:
23947 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
23948 continue;
23949 /* FALLTHRU */
23950 case DW_OP_GNU_const_type:
23951 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
23952 break;
23953 case DW_OP_GNU_entry_value:
23954 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
23955 continue;
23956 default:
23957 continue;
23959 gcc_assert (base_type->die_parent == comp_unit_die ());
23960 if (base_type->die_mark)
23961 base_type->die_mark++;
23962 else
23964 base_types.safe_push (base_type);
23965 base_type->die_mark = 1;
23970 /* Comparison function for sorting marked base types. */
23972 static int
23973 base_type_cmp (const void *x, const void *y)
23975 dw_die_ref dx = *(const dw_die_ref *) x;
23976 dw_die_ref dy = *(const dw_die_ref *) y;
23977 unsigned int byte_size1, byte_size2;
23978 unsigned int encoding1, encoding2;
23979 if (dx->die_mark > dy->die_mark)
23980 return -1;
23981 if (dx->die_mark < dy->die_mark)
23982 return 1;
23983 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
23984 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
23985 if (byte_size1 < byte_size2)
23986 return 1;
23987 if (byte_size1 > byte_size2)
23988 return -1;
23989 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
23990 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
23991 if (encoding1 < encoding2)
23992 return 1;
23993 if (encoding1 > encoding2)
23994 return -1;
23995 return 0;
23998 /* Move base types marked by mark_base_types as early as possible
23999 in the CU, sorted by decreasing usage count both to make the
24000 uleb128 references as small as possible and to make sure they
24001 will have die_offset already computed by calc_die_sizes when
24002 sizes of typed stack loc ops is computed. */
24004 static void
24005 move_marked_base_types (void)
24007 unsigned int i;
24008 dw_die_ref base_type, die, c;
24010 if (base_types.is_empty ())
24011 return;
24013 /* Sort by decreasing usage count, they will be added again in that
24014 order later on. */
24015 base_types.qsort (base_type_cmp);
24016 die = comp_unit_die ();
24017 c = die->die_child;
24020 dw_die_ref prev = c;
24021 c = c->die_sib;
24022 while (c->die_mark)
24024 remove_child_with_prev (c, prev);
24025 /* As base types got marked, there must be at least
24026 one node other than DW_TAG_base_type. */
24027 gcc_assert (c != c->die_sib);
24028 c = c->die_sib;
24031 while (c != die->die_child);
24032 gcc_assert (die->die_child);
24033 c = die->die_child;
24034 for (i = 0; base_types.iterate (i, &base_type); i++)
24036 base_type->die_mark = 0;
24037 base_type->die_sib = c->die_sib;
24038 c->die_sib = base_type;
24039 c = base_type;
24043 /* Helper function for resolve_addr, attempt to resolve
24044 one CONST_STRING, return true if successful. Similarly verify that
24045 SYMBOL_REFs refer to variables emitted in the current CU. */
24047 static bool
24048 resolve_one_addr (rtx *addr)
24050 rtx rtl = *addr;
24052 if (GET_CODE (rtl) == CONST_STRING)
24054 size_t len = strlen (XSTR (rtl, 0)) + 1;
24055 tree t = build_string (len, XSTR (rtl, 0));
24056 tree tlen = size_int (len - 1);
24057 TREE_TYPE (t)
24058 = build_array_type (char_type_node, build_index_type (tlen));
24059 rtl = lookup_constant_def (t);
24060 if (!rtl || !MEM_P (rtl))
24061 return false;
24062 rtl = XEXP (rtl, 0);
24063 if (GET_CODE (rtl) == SYMBOL_REF
24064 && SYMBOL_REF_DECL (rtl)
24065 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
24066 return false;
24067 vec_safe_push (used_rtx_array, rtl);
24068 *addr = rtl;
24069 return true;
24072 if (GET_CODE (rtl) == SYMBOL_REF
24073 && SYMBOL_REF_DECL (rtl))
24075 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
24077 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
24078 return false;
24080 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
24081 return false;
24084 if (GET_CODE (rtl) == CONST)
24086 subrtx_ptr_iterator::array_type array;
24087 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
24088 if (!resolve_one_addr (*iter))
24089 return false;
24092 return true;
24095 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
24096 if possible, and create DW_TAG_dwarf_procedure that can be referenced
24097 from DW_OP_GNU_implicit_pointer if the string hasn't been seen yet. */
24099 static rtx
24100 string_cst_pool_decl (tree t)
24102 rtx rtl = output_constant_def (t, 1);
24103 unsigned char *array;
24104 dw_loc_descr_ref l;
24105 tree decl;
24106 size_t len;
24107 dw_die_ref ref;
24109 if (!rtl || !MEM_P (rtl))
24110 return NULL_RTX;
24111 rtl = XEXP (rtl, 0);
24112 if (GET_CODE (rtl) != SYMBOL_REF
24113 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
24114 return NULL_RTX;
24116 decl = SYMBOL_REF_DECL (rtl);
24117 if (!lookup_decl_die (decl))
24119 len = TREE_STRING_LENGTH (t);
24120 vec_safe_push (used_rtx_array, rtl);
24121 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
24122 array = ggc_vec_alloc<unsigned char> (len);
24123 memcpy (array, TREE_STRING_POINTER (t), len);
24124 l = new_loc_descr (DW_OP_implicit_value, len, 0);
24125 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
24126 l->dw_loc_oprnd2.v.val_vec.length = len;
24127 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
24128 l->dw_loc_oprnd2.v.val_vec.array = array;
24129 add_AT_loc (ref, DW_AT_location, l);
24130 equate_decl_number_to_die (decl, ref);
24132 return rtl;
24135 /* Helper function of resolve_addr_in_expr. LOC is
24136 a DW_OP_addr followed by DW_OP_stack_value, either at the start
24137 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
24138 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
24139 with DW_OP_GNU_implicit_pointer if possible
24140 and return true, if unsuccessful, return false. */
24142 static bool
24143 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
24145 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
24146 HOST_WIDE_INT offset = 0;
24147 dw_die_ref ref = NULL;
24148 tree decl;
24150 if (GET_CODE (rtl) == CONST
24151 && GET_CODE (XEXP (rtl, 0)) == PLUS
24152 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
24154 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
24155 rtl = XEXP (XEXP (rtl, 0), 0);
24157 if (GET_CODE (rtl) == CONST_STRING)
24159 size_t len = strlen (XSTR (rtl, 0)) + 1;
24160 tree t = build_string (len, XSTR (rtl, 0));
24161 tree tlen = size_int (len - 1);
24163 TREE_TYPE (t)
24164 = build_array_type (char_type_node, build_index_type (tlen));
24165 rtl = string_cst_pool_decl (t);
24166 if (!rtl)
24167 return false;
24169 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
24171 decl = SYMBOL_REF_DECL (rtl);
24172 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
24174 ref = lookup_decl_die (decl);
24175 if (ref && (get_AT (ref, DW_AT_location)
24176 || get_AT (ref, DW_AT_const_value)))
24178 loc->dw_loc_opc = DW_OP_GNU_implicit_pointer;
24179 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
24180 loc->dw_loc_oprnd1.val_entry = NULL;
24181 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
24182 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
24183 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
24184 loc->dw_loc_oprnd2.v.val_int = offset;
24185 return true;
24189 return false;
24192 /* Helper function for resolve_addr, handle one location
24193 expression, return false if at least one CONST_STRING or SYMBOL_REF in
24194 the location list couldn't be resolved. */
24196 static bool
24197 resolve_addr_in_expr (dw_loc_descr_ref loc)
24199 dw_loc_descr_ref keep = NULL;
24200 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
24201 switch (loc->dw_loc_opc)
24203 case DW_OP_addr:
24204 if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
24206 if ((prev == NULL
24207 || prev->dw_loc_opc == DW_OP_piece
24208 || prev->dw_loc_opc == DW_OP_bit_piece)
24209 && loc->dw_loc_next
24210 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
24211 && !dwarf_strict
24212 && optimize_one_addr_into_implicit_ptr (loc))
24213 break;
24214 return false;
24216 break;
24217 case DW_OP_GNU_addr_index:
24218 case DW_OP_GNU_const_index:
24219 if (loc->dw_loc_opc == DW_OP_GNU_addr_index
24220 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
24222 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
24223 if (!resolve_one_addr (&rtl))
24224 return false;
24225 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
24226 loc->dw_loc_oprnd1.val_entry =
24227 add_addr_table_entry (rtl, ate_kind_rtx);
24229 break;
24230 case DW_OP_const4u:
24231 case DW_OP_const8u:
24232 if (loc->dtprel
24233 && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
24234 return false;
24235 break;
24236 case DW_OP_plus_uconst:
24237 if (size_of_loc_descr (loc)
24238 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
24240 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
24242 dw_loc_descr_ref repl
24243 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
24244 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
24245 add_loc_descr (&repl, loc->dw_loc_next);
24246 *loc = *repl;
24248 break;
24249 case DW_OP_implicit_value:
24250 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
24251 && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
24252 return false;
24253 break;
24254 case DW_OP_GNU_implicit_pointer:
24255 case DW_OP_GNU_parameter_ref:
24256 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
24258 dw_die_ref ref
24259 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
24260 if (ref == NULL)
24261 return false;
24262 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
24263 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
24264 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
24266 break;
24267 case DW_OP_GNU_const_type:
24268 case DW_OP_GNU_regval_type:
24269 case DW_OP_GNU_deref_type:
24270 case DW_OP_GNU_convert:
24271 case DW_OP_GNU_reinterpret:
24272 while (loc->dw_loc_next
24273 && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
24275 dw_die_ref base1, base2;
24276 unsigned enc1, enc2, size1, size2;
24277 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
24278 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
24279 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
24280 else if (loc->dw_loc_oprnd1.val_class
24281 == dw_val_class_unsigned_const)
24282 break;
24283 else
24284 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
24285 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
24286 == dw_val_class_unsigned_const)
24287 break;
24288 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
24289 gcc_assert (base1->die_tag == DW_TAG_base_type
24290 && base2->die_tag == DW_TAG_base_type);
24291 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
24292 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
24293 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
24294 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
24295 if (size1 == size2
24296 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
24297 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
24298 && loc != keep)
24299 || enc1 == enc2))
24301 /* Optimize away next DW_OP_GNU_convert after
24302 adjusting LOC's base type die reference. */
24303 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
24304 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
24305 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
24306 else
24307 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
24308 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
24309 continue;
24311 /* Don't change integer DW_OP_GNU_convert after e.g. floating
24312 point typed stack entry. */
24313 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
24314 keep = loc->dw_loc_next;
24315 break;
24317 break;
24318 default:
24319 break;
24321 return true;
24324 /* Helper function of resolve_addr. DIE had DW_AT_location of
24325 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
24326 and DW_OP_addr couldn't be resolved. resolve_addr has already
24327 removed the DW_AT_location attribute. This function attempts to
24328 add a new DW_AT_location attribute with DW_OP_GNU_implicit_pointer
24329 to it or DW_AT_const_value attribute, if possible. */
24331 static void
24332 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
24334 if (TREE_CODE (decl) != VAR_DECL
24335 || lookup_decl_die (decl) != die
24336 || DECL_EXTERNAL (decl)
24337 || !TREE_STATIC (decl)
24338 || DECL_INITIAL (decl) == NULL_TREE
24339 || DECL_P (DECL_INITIAL (decl))
24340 || get_AT (die, DW_AT_const_value))
24341 return;
24343 tree init = DECL_INITIAL (decl);
24344 HOST_WIDE_INT offset = 0;
24345 /* For variables that have been optimized away and thus
24346 don't have a memory location, see if we can emit
24347 DW_AT_const_value instead. */
24348 if (tree_add_const_value_attribute (die, init))
24349 return;
24350 if (dwarf_strict)
24351 return;
24352 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
24353 and ADDR_EXPR refers to a decl that has DW_AT_location or
24354 DW_AT_const_value (but isn't addressable, otherwise
24355 resolving the original DW_OP_addr wouldn't fail), see if
24356 we can add DW_OP_GNU_implicit_pointer. */
24357 STRIP_NOPS (init);
24358 if (TREE_CODE (init) == POINTER_PLUS_EXPR
24359 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
24361 offset = tree_to_shwi (TREE_OPERAND (init, 1));
24362 init = TREE_OPERAND (init, 0);
24363 STRIP_NOPS (init);
24365 if (TREE_CODE (init) != ADDR_EXPR)
24366 return;
24367 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
24368 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
24369 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
24370 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
24371 && TREE_OPERAND (init, 0) != decl))
24373 dw_die_ref ref;
24374 dw_loc_descr_ref l;
24376 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
24378 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
24379 if (!rtl)
24380 return;
24381 decl = SYMBOL_REF_DECL (rtl);
24383 else
24384 decl = TREE_OPERAND (init, 0);
24385 ref = lookup_decl_die (decl);
24386 if (ref == NULL
24387 || (!get_AT (ref, DW_AT_location)
24388 && !get_AT (ref, DW_AT_const_value)))
24389 return;
24390 l = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
24391 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
24392 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
24393 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
24394 add_AT_loc (die, DW_AT_location, l);
24398 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
24399 an address in .rodata section if the string literal is emitted there,
24400 or remove the containing location list or replace DW_AT_const_value
24401 with DW_AT_location and empty location expression, if it isn't found
24402 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
24403 to something that has been emitted in the current CU. */
24405 static void
24406 resolve_addr (dw_die_ref die)
24408 dw_die_ref c;
24409 dw_attr_ref a;
24410 dw_loc_list_ref *curr, *start, loc;
24411 unsigned ix;
24413 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
24414 switch (AT_class (a))
24416 case dw_val_class_loc_list:
24417 start = curr = AT_loc_list_ptr (a);
24418 loc = *curr;
24419 gcc_assert (loc);
24420 /* The same list can be referenced more than once. See if we have
24421 already recorded the result from a previous pass. */
24422 if (loc->replaced)
24423 *curr = loc->dw_loc_next;
24424 else if (!loc->resolved_addr)
24426 /* As things stand, we do not expect or allow one die to
24427 reference a suffix of another die's location list chain.
24428 References must be identical or completely separate.
24429 There is therefore no need to cache the result of this
24430 pass on any list other than the first; doing so
24431 would lead to unnecessary writes. */
24432 while (*curr)
24434 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
24435 if (!resolve_addr_in_expr ((*curr)->expr))
24437 dw_loc_list_ref next = (*curr)->dw_loc_next;
24438 dw_loc_descr_ref l = (*curr)->expr;
24440 if (next && (*curr)->ll_symbol)
24442 gcc_assert (!next->ll_symbol);
24443 next->ll_symbol = (*curr)->ll_symbol;
24445 if (dwarf_split_debug_info)
24446 remove_loc_list_addr_table_entries (l);
24447 *curr = next;
24449 else
24451 mark_base_types ((*curr)->expr);
24452 curr = &(*curr)->dw_loc_next;
24455 if (loc == *start)
24456 loc->resolved_addr = 1;
24457 else
24459 loc->replaced = 1;
24460 loc->dw_loc_next = *start;
24463 if (!*start)
24465 remove_AT (die, a->dw_attr);
24466 ix--;
24468 break;
24469 case dw_val_class_loc:
24471 dw_loc_descr_ref l = AT_loc (a);
24472 /* For -gdwarf-2 don't attempt to optimize
24473 DW_AT_data_member_location containing
24474 DW_OP_plus_uconst - older consumers might
24475 rely on it being that op instead of a more complex,
24476 but shorter, location description. */
24477 if ((dwarf_version > 2
24478 || a->dw_attr != DW_AT_data_member_location
24479 || l == NULL
24480 || l->dw_loc_opc != DW_OP_plus_uconst
24481 || l->dw_loc_next != NULL)
24482 && !resolve_addr_in_expr (l))
24484 if (dwarf_split_debug_info)
24485 remove_loc_list_addr_table_entries (l);
24486 if (l != NULL
24487 && l->dw_loc_next == NULL
24488 && l->dw_loc_opc == DW_OP_addr
24489 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
24490 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
24491 && a->dw_attr == DW_AT_location)
24493 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
24494 remove_AT (die, a->dw_attr);
24495 ix--;
24496 optimize_location_into_implicit_ptr (die, decl);
24497 break;
24499 remove_AT (die, a->dw_attr);
24500 ix--;
24502 else
24503 mark_base_types (l);
24505 break;
24506 case dw_val_class_addr:
24507 if (a->dw_attr == DW_AT_const_value
24508 && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
24510 if (AT_index (a) != NOT_INDEXED)
24511 remove_addr_table_entry (a->dw_attr_val.val_entry);
24512 remove_AT (die, a->dw_attr);
24513 ix--;
24515 if (die->die_tag == DW_TAG_GNU_call_site
24516 && a->dw_attr == DW_AT_abstract_origin)
24518 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
24519 dw_die_ref tdie = lookup_decl_die (tdecl);
24520 dw_die_ref cdie;
24521 if (tdie == NULL
24522 && DECL_EXTERNAL (tdecl)
24523 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE
24524 && (cdie = lookup_context_die (DECL_CONTEXT (tdecl))))
24526 /* Creating a full DIE for tdecl is overly expensive and
24527 at this point even wrong when in the LTO phase
24528 as it can end up generating new type DIEs we didn't
24529 output and thus optimize_external_refs will crash. */
24530 tdie = new_die (DW_TAG_subprogram, cdie, NULL_TREE);
24531 add_AT_flag (tdie, DW_AT_external, 1);
24532 add_AT_flag (tdie, DW_AT_declaration, 1);
24533 add_linkage_attr (tdie, tdecl);
24534 add_name_and_src_coords_attributes (tdie, tdecl);
24535 equate_decl_number_to_die (tdecl, tdie);
24537 if (tdie)
24539 a->dw_attr_val.val_class = dw_val_class_die_ref;
24540 a->dw_attr_val.v.val_die_ref.die = tdie;
24541 a->dw_attr_val.v.val_die_ref.external = 0;
24543 else
24545 if (AT_index (a) != NOT_INDEXED)
24546 remove_addr_table_entry (a->dw_attr_val.val_entry);
24547 remove_AT (die, a->dw_attr);
24548 ix--;
24551 break;
24552 default:
24553 break;
24556 FOR_EACH_CHILD (die, c, resolve_addr (c));
24559 /* Helper routines for optimize_location_lists.
24560 This pass tries to share identical local lists in .debug_loc
24561 section. */
24563 /* Iteratively hash operands of LOC opcode into HSTATE. */
24565 static void
24566 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
24568 dw_val_ref val1 = &loc->dw_loc_oprnd1;
24569 dw_val_ref val2 = &loc->dw_loc_oprnd2;
24571 switch (loc->dw_loc_opc)
24573 case DW_OP_const4u:
24574 case DW_OP_const8u:
24575 if (loc->dtprel)
24576 goto hash_addr;
24577 /* FALLTHRU */
24578 case DW_OP_const1u:
24579 case DW_OP_const1s:
24580 case DW_OP_const2u:
24581 case DW_OP_const2s:
24582 case DW_OP_const4s:
24583 case DW_OP_const8s:
24584 case DW_OP_constu:
24585 case DW_OP_consts:
24586 case DW_OP_pick:
24587 case DW_OP_plus_uconst:
24588 case DW_OP_breg0:
24589 case DW_OP_breg1:
24590 case DW_OP_breg2:
24591 case DW_OP_breg3:
24592 case DW_OP_breg4:
24593 case DW_OP_breg5:
24594 case DW_OP_breg6:
24595 case DW_OP_breg7:
24596 case DW_OP_breg8:
24597 case DW_OP_breg9:
24598 case DW_OP_breg10:
24599 case DW_OP_breg11:
24600 case DW_OP_breg12:
24601 case DW_OP_breg13:
24602 case DW_OP_breg14:
24603 case DW_OP_breg15:
24604 case DW_OP_breg16:
24605 case DW_OP_breg17:
24606 case DW_OP_breg18:
24607 case DW_OP_breg19:
24608 case DW_OP_breg20:
24609 case DW_OP_breg21:
24610 case DW_OP_breg22:
24611 case DW_OP_breg23:
24612 case DW_OP_breg24:
24613 case DW_OP_breg25:
24614 case DW_OP_breg26:
24615 case DW_OP_breg27:
24616 case DW_OP_breg28:
24617 case DW_OP_breg29:
24618 case DW_OP_breg30:
24619 case DW_OP_breg31:
24620 case DW_OP_regx:
24621 case DW_OP_fbreg:
24622 case DW_OP_piece:
24623 case DW_OP_deref_size:
24624 case DW_OP_xderef_size:
24625 hstate.add_object (val1->v.val_int);
24626 break;
24627 case DW_OP_skip:
24628 case DW_OP_bra:
24630 int offset;
24632 gcc_assert (val1->val_class == dw_val_class_loc);
24633 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
24634 hstate.add_object (offset);
24636 break;
24637 case DW_OP_implicit_value:
24638 hstate.add_object (val1->v.val_unsigned);
24639 switch (val2->val_class)
24641 case dw_val_class_const:
24642 hstate.add_object (val2->v.val_int);
24643 break;
24644 case dw_val_class_vec:
24646 unsigned int elt_size = val2->v.val_vec.elt_size;
24647 unsigned int len = val2->v.val_vec.length;
24649 hstate.add_int (elt_size);
24650 hstate.add_int (len);
24651 hstate.add (val2->v.val_vec.array, len * elt_size);
24653 break;
24654 case dw_val_class_const_double:
24655 hstate.add_object (val2->v.val_double.low);
24656 hstate.add_object (val2->v.val_double.high);
24657 break;
24658 case dw_val_class_wide_int:
24659 hstate.add (val2->v.val_wide->get_val (),
24660 get_full_len (*val2->v.val_wide)
24661 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
24662 break;
24663 case dw_val_class_addr:
24664 inchash::add_rtx (val2->v.val_addr, hstate);
24665 break;
24666 default:
24667 gcc_unreachable ();
24669 break;
24670 case DW_OP_bregx:
24671 case DW_OP_bit_piece:
24672 hstate.add_object (val1->v.val_int);
24673 hstate.add_object (val2->v.val_int);
24674 break;
24675 case DW_OP_addr:
24676 hash_addr:
24677 if (loc->dtprel)
24679 unsigned char dtprel = 0xd1;
24680 hstate.add_object (dtprel);
24682 inchash::add_rtx (val1->v.val_addr, hstate);
24683 break;
24684 case DW_OP_GNU_addr_index:
24685 case DW_OP_GNU_const_index:
24687 if (loc->dtprel)
24689 unsigned char dtprel = 0xd1;
24690 hstate.add_object (dtprel);
24692 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
24694 break;
24695 case DW_OP_GNU_implicit_pointer:
24696 hstate.add_int (val2->v.val_int);
24697 break;
24698 case DW_OP_GNU_entry_value:
24699 hstate.add_object (val1->v.val_loc);
24700 break;
24701 case DW_OP_GNU_regval_type:
24702 case DW_OP_GNU_deref_type:
24704 unsigned int byte_size
24705 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
24706 unsigned int encoding
24707 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
24708 hstate.add_object (val1->v.val_int);
24709 hstate.add_object (byte_size);
24710 hstate.add_object (encoding);
24712 break;
24713 case DW_OP_GNU_convert:
24714 case DW_OP_GNU_reinterpret:
24715 if (val1->val_class == dw_val_class_unsigned_const)
24717 hstate.add_object (val1->v.val_unsigned);
24718 break;
24720 /* FALLTHRU */
24721 case DW_OP_GNU_const_type:
24723 unsigned int byte_size
24724 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
24725 unsigned int encoding
24726 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
24727 hstate.add_object (byte_size);
24728 hstate.add_object (encoding);
24729 if (loc->dw_loc_opc != DW_OP_GNU_const_type)
24730 break;
24731 hstate.add_object (val2->val_class);
24732 switch (val2->val_class)
24734 case dw_val_class_const:
24735 hstate.add_object (val2->v.val_int);
24736 break;
24737 case dw_val_class_vec:
24739 unsigned int elt_size = val2->v.val_vec.elt_size;
24740 unsigned int len = val2->v.val_vec.length;
24742 hstate.add_object (elt_size);
24743 hstate.add_object (len);
24744 hstate.add (val2->v.val_vec.array, len * elt_size);
24746 break;
24747 case dw_val_class_const_double:
24748 hstate.add_object (val2->v.val_double.low);
24749 hstate.add_object (val2->v.val_double.high);
24750 break;
24751 case dw_val_class_wide_int:
24752 hstate.add (val2->v.val_wide->get_val (),
24753 get_full_len (*val2->v.val_wide)
24754 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
24755 break;
24756 default:
24757 gcc_unreachable ();
24760 break;
24762 default:
24763 /* Other codes have no operands. */
24764 break;
24768 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
24770 static inline void
24771 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
24773 dw_loc_descr_ref l;
24774 bool sizes_computed = false;
24775 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
24776 size_of_locs (loc);
24778 for (l = loc; l != NULL; l = l->dw_loc_next)
24780 enum dwarf_location_atom opc = l->dw_loc_opc;
24781 hstate.add_object (opc);
24782 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
24784 size_of_locs (loc);
24785 sizes_computed = true;
24787 hash_loc_operands (l, hstate);
24791 /* Compute hash of the whole location list LIST_HEAD. */
24793 static inline void
24794 hash_loc_list (dw_loc_list_ref list_head)
24796 dw_loc_list_ref curr = list_head;
24797 inchash::hash hstate;
24799 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
24801 hstate.add (curr->begin, strlen (curr->begin) + 1);
24802 hstate.add (curr->end, strlen (curr->end) + 1);
24803 if (curr->section)
24804 hstate.add (curr->section, strlen (curr->section) + 1);
24805 hash_locs (curr->expr, hstate);
24807 list_head->hash = hstate.end ();
24810 /* Return true if X and Y opcodes have the same operands. */
24812 static inline bool
24813 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
24815 dw_val_ref valx1 = &x->dw_loc_oprnd1;
24816 dw_val_ref valx2 = &x->dw_loc_oprnd2;
24817 dw_val_ref valy1 = &y->dw_loc_oprnd1;
24818 dw_val_ref valy2 = &y->dw_loc_oprnd2;
24820 switch (x->dw_loc_opc)
24822 case DW_OP_const4u:
24823 case DW_OP_const8u:
24824 if (x->dtprel)
24825 goto hash_addr;
24826 /* FALLTHRU */
24827 case DW_OP_const1u:
24828 case DW_OP_const1s:
24829 case DW_OP_const2u:
24830 case DW_OP_const2s:
24831 case DW_OP_const4s:
24832 case DW_OP_const8s:
24833 case DW_OP_constu:
24834 case DW_OP_consts:
24835 case DW_OP_pick:
24836 case DW_OP_plus_uconst:
24837 case DW_OP_breg0:
24838 case DW_OP_breg1:
24839 case DW_OP_breg2:
24840 case DW_OP_breg3:
24841 case DW_OP_breg4:
24842 case DW_OP_breg5:
24843 case DW_OP_breg6:
24844 case DW_OP_breg7:
24845 case DW_OP_breg8:
24846 case DW_OP_breg9:
24847 case DW_OP_breg10:
24848 case DW_OP_breg11:
24849 case DW_OP_breg12:
24850 case DW_OP_breg13:
24851 case DW_OP_breg14:
24852 case DW_OP_breg15:
24853 case DW_OP_breg16:
24854 case DW_OP_breg17:
24855 case DW_OP_breg18:
24856 case DW_OP_breg19:
24857 case DW_OP_breg20:
24858 case DW_OP_breg21:
24859 case DW_OP_breg22:
24860 case DW_OP_breg23:
24861 case DW_OP_breg24:
24862 case DW_OP_breg25:
24863 case DW_OP_breg26:
24864 case DW_OP_breg27:
24865 case DW_OP_breg28:
24866 case DW_OP_breg29:
24867 case DW_OP_breg30:
24868 case DW_OP_breg31:
24869 case DW_OP_regx:
24870 case DW_OP_fbreg:
24871 case DW_OP_piece:
24872 case DW_OP_deref_size:
24873 case DW_OP_xderef_size:
24874 return valx1->v.val_int == valy1->v.val_int;
24875 case DW_OP_skip:
24876 case DW_OP_bra:
24877 /* If splitting debug info, the use of DW_OP_GNU_addr_index
24878 can cause irrelevant differences in dw_loc_addr. */
24879 gcc_assert (valx1->val_class == dw_val_class_loc
24880 && valy1->val_class == dw_val_class_loc
24881 && (dwarf_split_debug_info
24882 || x->dw_loc_addr == y->dw_loc_addr));
24883 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
24884 case DW_OP_implicit_value:
24885 if (valx1->v.val_unsigned != valy1->v.val_unsigned
24886 || valx2->val_class != valy2->val_class)
24887 return false;
24888 switch (valx2->val_class)
24890 case dw_val_class_const:
24891 return valx2->v.val_int == valy2->v.val_int;
24892 case dw_val_class_vec:
24893 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
24894 && valx2->v.val_vec.length == valy2->v.val_vec.length
24895 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
24896 valx2->v.val_vec.elt_size
24897 * valx2->v.val_vec.length) == 0;
24898 case dw_val_class_const_double:
24899 return valx2->v.val_double.low == valy2->v.val_double.low
24900 && valx2->v.val_double.high == valy2->v.val_double.high;
24901 case dw_val_class_wide_int:
24902 return *valx2->v.val_wide == *valy2->v.val_wide;
24903 case dw_val_class_addr:
24904 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
24905 default:
24906 gcc_unreachable ();
24908 case DW_OP_bregx:
24909 case DW_OP_bit_piece:
24910 return valx1->v.val_int == valy1->v.val_int
24911 && valx2->v.val_int == valy2->v.val_int;
24912 case DW_OP_addr:
24913 hash_addr:
24914 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
24915 case DW_OP_GNU_addr_index:
24916 case DW_OP_GNU_const_index:
24918 rtx ax1 = valx1->val_entry->addr.rtl;
24919 rtx ay1 = valy1->val_entry->addr.rtl;
24920 return rtx_equal_p (ax1, ay1);
24922 case DW_OP_GNU_implicit_pointer:
24923 return valx1->val_class == dw_val_class_die_ref
24924 && valx1->val_class == valy1->val_class
24925 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
24926 && valx2->v.val_int == valy2->v.val_int;
24927 case DW_OP_GNU_entry_value:
24928 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
24929 case DW_OP_GNU_const_type:
24930 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
24931 || valx2->val_class != valy2->val_class)
24932 return false;
24933 switch (valx2->val_class)
24935 case dw_val_class_const:
24936 return valx2->v.val_int == valy2->v.val_int;
24937 case dw_val_class_vec:
24938 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
24939 && valx2->v.val_vec.length == valy2->v.val_vec.length
24940 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
24941 valx2->v.val_vec.elt_size
24942 * valx2->v.val_vec.length) == 0;
24943 case dw_val_class_const_double:
24944 return valx2->v.val_double.low == valy2->v.val_double.low
24945 && valx2->v.val_double.high == valy2->v.val_double.high;
24946 case dw_val_class_wide_int:
24947 return *valx2->v.val_wide == *valy2->v.val_wide;
24948 default:
24949 gcc_unreachable ();
24951 case DW_OP_GNU_regval_type:
24952 case DW_OP_GNU_deref_type:
24953 return valx1->v.val_int == valy1->v.val_int
24954 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
24955 case DW_OP_GNU_convert:
24956 case DW_OP_GNU_reinterpret:
24957 if (valx1->val_class != valy1->val_class)
24958 return false;
24959 if (valx1->val_class == dw_val_class_unsigned_const)
24960 return valx1->v.val_unsigned == valy1->v.val_unsigned;
24961 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
24962 case DW_OP_GNU_parameter_ref:
24963 return valx1->val_class == dw_val_class_die_ref
24964 && valx1->val_class == valy1->val_class
24965 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
24966 default:
24967 /* Other codes have no operands. */
24968 return true;
24972 /* Return true if DWARF location expressions X and Y are the same. */
24974 static inline bool
24975 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
24977 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
24978 if (x->dw_loc_opc != y->dw_loc_opc
24979 || x->dtprel != y->dtprel
24980 || !compare_loc_operands (x, y))
24981 break;
24982 return x == NULL && y == NULL;
24985 /* Hashtable helpers. */
24987 struct loc_list_hasher : typed_noop_remove <dw_loc_list_struct>
24989 typedef dw_loc_list_struct *value_type;
24990 typedef dw_loc_list_struct *compare_type;
24991 static inline hashval_t hash (const dw_loc_list_struct *);
24992 static inline bool equal (const dw_loc_list_struct *,
24993 const dw_loc_list_struct *);
24996 /* Return precomputed hash of location list X. */
24998 inline hashval_t
24999 loc_list_hasher::hash (const dw_loc_list_struct *x)
25001 return x->hash;
25004 /* Return true if location lists A and B are the same. */
25006 inline bool
25007 loc_list_hasher::equal (const dw_loc_list_struct *a,
25008 const dw_loc_list_struct *b)
25010 if (a == b)
25011 return 1;
25012 if (a->hash != b->hash)
25013 return 0;
25014 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
25015 if (strcmp (a->begin, b->begin) != 0
25016 || strcmp (a->end, b->end) != 0
25017 || (a->section == NULL) != (b->section == NULL)
25018 || (a->section && strcmp (a->section, b->section) != 0)
25019 || !compare_locs (a->expr, b->expr))
25020 break;
25021 return a == NULL && b == NULL;
25024 typedef hash_table<loc_list_hasher> loc_list_hash_type;
25027 /* Recursively optimize location lists referenced from DIE
25028 children and share them whenever possible. */
25030 static void
25031 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
25033 dw_die_ref c;
25034 dw_attr_ref a;
25035 unsigned ix;
25036 dw_loc_list_struct **slot;
25038 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
25039 if (AT_class (a) == dw_val_class_loc_list)
25041 dw_loc_list_ref list = AT_loc_list (a);
25042 /* TODO: perform some optimizations here, before hashing
25043 it and storing into the hash table. */
25044 hash_loc_list (list);
25045 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
25046 if (*slot == NULL)
25047 *slot = list;
25048 else
25049 a->dw_attr_val.v.val_loc_list = *slot;
25052 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
25056 /* Recursively assign each location list a unique index into the debug_addr
25057 section. */
25059 static void
25060 index_location_lists (dw_die_ref die)
25062 dw_die_ref c;
25063 dw_attr_ref a;
25064 unsigned ix;
25066 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
25067 if (AT_class (a) == dw_val_class_loc_list)
25069 dw_loc_list_ref list = AT_loc_list (a);
25070 dw_loc_list_ref curr;
25071 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
25073 /* Don't index an entry that has already been indexed
25074 or won't be output. */
25075 if (curr->begin_entry != NULL
25076 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
25077 continue;
25079 curr->begin_entry
25080 = add_addr_table_entry (xstrdup (curr->begin),
25081 ate_kind_label);
25085 FOR_EACH_CHILD (die, c, index_location_lists (c));
25088 /* Optimize location lists referenced from DIE
25089 children and share them whenever possible. */
25091 static void
25092 optimize_location_lists (dw_die_ref die)
25094 loc_list_hash_type htab (500);
25095 optimize_location_lists_1 (die, &htab);
25098 /* Output stuff that dwarf requires at the end of every file,
25099 and generate the DWARF-2 debugging info. */
25101 static void
25102 dwarf2out_finish (const char *filename)
25104 comdat_type_node *ctnode;
25105 dw_die_ref main_comp_unit_die;
25107 /* Flush out any latecomers to the limbo party. */
25108 dwarf2out_early_finish ();
25110 /* PCH might result in DW_AT_producer string being restored from the
25111 header compilation, so always fill it with empty string initially
25112 and overwrite only here. */
25113 dw_attr_ref producer = get_AT (comp_unit_die (), DW_AT_producer);
25114 producer_string = gen_producer_string ();
25115 producer->dw_attr_val.v.val_str->refcount--;
25116 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
25118 gen_scheduled_generic_parms_dies ();
25119 gen_remaining_tmpl_value_param_die_attribute ();
25121 /* Add the name for the main input file now. We delayed this from
25122 dwarf2out_init to avoid complications with PCH.
25123 For LTO produced units use a fixed artificial name to avoid
25124 leaking tempfile names into the dwarf. */
25125 if (!in_lto_p)
25126 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
25127 else
25128 add_name_attribute (comp_unit_die (), "<artificial>");
25129 if (!IS_ABSOLUTE_PATH (filename) || targetm.force_at_comp_dir)
25130 add_comp_dir_attribute (comp_unit_die ());
25131 else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
25133 bool p = false;
25134 file_table->traverse<bool *, file_table_relative_p> (&p);
25135 if (p)
25136 add_comp_dir_attribute (comp_unit_die ());
25139 #if ENABLE_ASSERT_CHECKING
25141 dw_die_ref die = comp_unit_die (), c;
25142 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
25144 #endif
25145 resolve_addr (comp_unit_die ());
25146 move_marked_base_types ();
25148 /* Walk through the list of incomplete types again, trying once more to
25149 emit full debugging info for them. */
25150 retry_incomplete_types ();
25152 if (flag_eliminate_unused_debug_types)
25153 prune_unused_types ();
25155 /* Generate separate COMDAT sections for type DIEs. */
25156 if (use_debug_types)
25158 break_out_comdat_types (comp_unit_die ());
25160 /* Each new type_unit DIE was added to the limbo die list when created.
25161 Since these have all been added to comdat_type_list, clear the
25162 limbo die list. */
25163 limbo_die_list = NULL;
25165 /* For each new comdat type unit, copy declarations for incomplete
25166 types to make the new unit self-contained (i.e., no direct
25167 references to the main compile unit). */
25168 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
25169 copy_decls_for_unworthy_types (ctnode->root_die);
25170 copy_decls_for_unworthy_types (comp_unit_die ());
25172 /* In the process of copying declarations from one unit to another,
25173 we may have left some declarations behind that are no longer
25174 referenced. Prune them. */
25175 prune_unused_types ();
25178 /* Generate separate CUs for each of the include files we've seen.
25179 They will go into limbo_die_list. */
25180 if (flag_eliminate_dwarf2_dups)
25181 break_out_includes (comp_unit_die ());
25183 /* Traverse the DIE's and add add sibling attributes to those DIE's
25184 that have children. */
25185 add_sibling_attributes (comp_unit_die ());
25186 limbo_die_node *node;
25187 for (node = limbo_die_list; node; node = node->next)
25188 add_sibling_attributes (node->die);
25189 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
25190 add_sibling_attributes (ctnode->root_die);
25192 /* When splitting DWARF info, we put some attributes in the
25193 skeleton compile_unit DIE that remains in the .o, while
25194 most attributes go in the DWO compile_unit_die. */
25195 if (dwarf_split_debug_info)
25196 main_comp_unit_die = gen_compile_unit_die (NULL);
25197 else
25198 main_comp_unit_die = comp_unit_die ();
25200 /* Output a terminator label for the .text section. */
25201 switch_to_section (text_section);
25202 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
25203 if (cold_text_section)
25205 switch_to_section (cold_text_section);
25206 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
25209 /* We can only use the low/high_pc attributes if all of the code was
25210 in .text. */
25211 if (!have_multiple_function_sections
25212 || (dwarf_version < 3 && dwarf_strict))
25214 /* Don't add if the CU has no associated code. */
25215 if (text_section_used)
25216 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
25217 text_end_label, true);
25219 else
25221 unsigned fde_idx;
25222 dw_fde_ref fde;
25223 bool range_list_added = false;
25225 if (text_section_used)
25226 add_ranges_by_labels (main_comp_unit_die, text_section_label,
25227 text_end_label, &range_list_added, true);
25228 if (cold_text_section_used)
25229 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
25230 cold_end_label, &range_list_added, true);
25232 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
25234 if (DECL_IGNORED_P (fde->decl))
25235 continue;
25236 if (!fde->in_std_section)
25237 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
25238 fde->dw_fde_end, &range_list_added,
25239 true);
25240 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
25241 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
25242 fde->dw_fde_second_end, &range_list_added,
25243 true);
25246 if (range_list_added)
25248 /* We need to give .debug_loc and .debug_ranges an appropriate
25249 "base address". Use zero so that these addresses become
25250 absolute. Historically, we've emitted the unexpected
25251 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
25252 Emit both to give time for other tools to adapt. */
25253 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
25254 if (! dwarf_strict && dwarf_version < 4)
25255 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
25257 add_ranges (NULL);
25261 if (debug_info_level >= DINFO_LEVEL_TERSE)
25262 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
25263 debug_line_section_label);
25265 if (have_macinfo)
25266 add_AT_macptr (comp_unit_die (),
25267 dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
25268 macinfo_section_label);
25270 if (dwarf_split_debug_info)
25272 /* optimize_location_lists calculates the size of the lists,
25273 so index them first, and assign indices to the entries.
25274 Although optimize_location_lists will remove entries from
25275 the table, it only does so for duplicates, and therefore
25276 only reduces ref_counts to 1. */
25277 index_location_lists (comp_unit_die ());
25279 if (addr_index_table != NULL)
25281 unsigned int index = 0;
25282 addr_index_table
25283 ->traverse_noresize<unsigned int *, index_addr_table_entry>
25284 (&index);
25288 if (have_location_lists)
25289 optimize_location_lists (comp_unit_die ());
25291 save_macinfo_strings ();
25293 if (dwarf_split_debug_info)
25295 unsigned int index = 0;
25297 /* Add attributes common to skeleton compile_units and
25298 type_units. Because these attributes include strings, it
25299 must be done before freezing the string table. Top-level
25300 skeleton die attrs are added when the skeleton type unit is
25301 created, so ensure it is created by this point. */
25302 add_top_level_skeleton_die_attrs (main_comp_unit_die);
25303 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
25306 /* Output all of the compilation units. We put the main one last so that
25307 the offsets are available to output_pubnames. */
25308 for (node = limbo_die_list; node; node = node->next)
25309 output_comp_unit (node->die, 0);
25311 hash_table<comdat_type_hasher> comdat_type_table (100);
25312 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
25314 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
25316 /* Don't output duplicate types. */
25317 if (*slot != HTAB_EMPTY_ENTRY)
25318 continue;
25320 /* Add a pointer to the line table for the main compilation unit
25321 so that the debugger can make sense of DW_AT_decl_file
25322 attributes. */
25323 if (debug_info_level >= DINFO_LEVEL_TERSE)
25324 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
25325 (!dwarf_split_debug_info
25326 ? debug_line_section_label
25327 : debug_skeleton_line_section_label));
25329 output_comdat_type_unit (ctnode);
25330 *slot = ctnode;
25333 /* The AT_pubnames attribute needs to go in all skeleton dies, including
25334 both the main_cu and all skeleton TUs. Making this call unconditional
25335 would end up either adding a second copy of the AT_pubnames attribute, or
25336 requiring a special case in add_top_level_skeleton_die_attrs. */
25337 if (!dwarf_split_debug_info)
25338 add_AT_pubnames (comp_unit_die ());
25340 if (dwarf_split_debug_info)
25342 int mark;
25343 unsigned char checksum[16];
25344 struct md5_ctx ctx;
25346 /* Compute a checksum of the comp_unit to use as the dwo_id. */
25347 md5_init_ctx (&ctx);
25348 mark = 0;
25349 die_checksum (comp_unit_die (), &ctx, &mark);
25350 unmark_all_dies (comp_unit_die ());
25351 md5_finish_ctx (&ctx, checksum);
25353 /* Use the first 8 bytes of the checksum as the dwo_id,
25354 and add it to both comp-unit DIEs. */
25355 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
25356 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
25358 /* Add the base offset of the ranges table to the skeleton
25359 comp-unit DIE. */
25360 if (ranges_table_in_use)
25361 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
25362 ranges_section_label);
25364 switch_to_section (debug_addr_section);
25365 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
25366 output_addr_table ();
25369 /* Output the main compilation unit if non-empty or if .debug_macinfo
25370 or .debug_macro will be emitted. */
25371 output_comp_unit (comp_unit_die (), have_macinfo);
25373 if (dwarf_split_debug_info && info_section_emitted)
25374 output_skeleton_debug_sections (main_comp_unit_die);
25376 /* Output the abbreviation table. */
25377 if (abbrev_die_table_in_use != 1)
25379 switch_to_section (debug_abbrev_section);
25380 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
25381 output_abbrev_section ();
25384 /* Output location list section if necessary. */
25385 if (have_location_lists)
25387 /* Output the location lists info. */
25388 switch_to_section (debug_loc_section);
25389 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
25390 output_location_lists (comp_unit_die ());
25393 output_pubtables ();
25395 /* Output the address range information if a CU (.debug_info section)
25396 was emitted. We output an empty table even if we had no functions
25397 to put in it. This because the consumer has no way to tell the
25398 difference between an empty table that we omitted and failure to
25399 generate a table that would have contained data. */
25400 if (info_section_emitted)
25402 unsigned long aranges_length = size_of_aranges ();
25404 switch_to_section (debug_aranges_section);
25405 output_aranges (aranges_length);
25408 /* Output ranges section if necessary. */
25409 if (ranges_table_in_use)
25411 switch_to_section (debug_ranges_section);
25412 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
25413 output_ranges ();
25416 /* Have to end the macro section. */
25417 if (have_macinfo)
25419 switch_to_section (debug_macinfo_section);
25420 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
25421 output_macinfo ();
25422 dw2_asm_output_data (1, 0, "End compilation unit");
25425 /* Output the source line correspondence table. We must do this
25426 even if there is no line information. Otherwise, on an empty
25427 translation unit, we will generate a present, but empty,
25428 .debug_info section. IRIX 6.5 `nm' will then complain when
25429 examining the file. This is done late so that any filenames
25430 used by the debug_info section are marked as 'used'. */
25431 switch_to_section (debug_line_section);
25432 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
25433 if (! DWARF2_ASM_LINE_DEBUG_INFO)
25434 output_line_info (false);
25436 if (dwarf_split_debug_info && info_section_emitted)
25438 switch_to_section (debug_skeleton_line_section);
25439 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
25440 output_line_info (true);
25443 /* If we emitted any indirect strings, output the string table too. */
25444 if (debug_str_hash || skeleton_debug_str_hash)
25445 output_indirect_strings ();
25448 /* Perform any cleanups needed after the early debug generation pass
25449 has run. */
25451 static void
25452 dwarf2out_early_finish (void)
25454 limbo_die_node *node, *next_node;
25456 /* Add DW_AT_linkage_name for all deferred DIEs. */
25457 for (node = deferred_asm_name; node; node = node->next)
25459 tree decl = node->created_for;
25460 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
25461 /* A missing DECL_ASSEMBLER_NAME can be a constant DIE that
25462 ended up in in deferred_asm_name before we knew it was
25463 constant and never written to disk. */
25464 && DECL_ASSEMBLER_NAME (decl))
25466 add_linkage_attr (node->die, decl);
25467 move_linkage_attr (node->die);
25470 deferred_asm_name = NULL;
25472 /* Traverse the limbo die list, and add parent/child links. The only
25473 dies without parents that should be here are concrete instances of
25474 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
25475 For concrete instances, we can get the parent die from the abstract
25476 instance.
25478 The point here is to flush out the limbo list so that it is empty
25479 and we don't need to stream it for LTO. */
25480 for (node = limbo_die_list; node; node = next_node)
25482 dw_die_ref die = node->die;
25483 next_node = node->next;
25485 if (die->die_parent == NULL)
25487 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
25489 if (origin && origin->die_parent)
25490 add_child_die (origin->die_parent, die);
25491 else if (is_cu_die (die))
25493 else if (seen_error ())
25494 /* It's OK to be confused by errors in the input. */
25495 add_child_die (comp_unit_die (), die);
25496 else
25498 /* In certain situations, the lexical block containing a
25499 nested function can be optimized away, which results
25500 in the nested function die being orphaned. Likewise
25501 with the return type of that nested function. Force
25502 this to be a child of the containing function.
25504 It may happen that even the containing function got fully
25505 inlined and optimized out. In that case we are lost and
25506 assign the empty child. This should not be big issue as
25507 the function is likely unreachable too. */
25508 gcc_assert (node->created_for);
25510 if (DECL_P (node->created_for))
25511 origin = get_context_die (DECL_CONTEXT (node->created_for));
25512 else if (TYPE_P (node->created_for))
25513 origin = scope_die_for (node->created_for, comp_unit_die ());
25514 else
25515 origin = comp_unit_die ();
25517 add_child_die (origin, die);
25522 limbo_die_list = NULL;
25525 /* Reset all state within dwarf2out.c so that we can rerun the compiler
25526 within the same process. For use by toplev::finalize. */
25528 void
25529 dwarf2out_c_finalize (void)
25531 last_var_location_insn = NULL;
25532 cached_next_real_insn = NULL;
25533 used_rtx_array = NULL;
25534 incomplete_types = NULL;
25535 decl_scope_table = NULL;
25536 debug_info_section = NULL;
25537 debug_skeleton_info_section = NULL;
25538 debug_abbrev_section = NULL;
25539 debug_skeleton_abbrev_section = NULL;
25540 debug_aranges_section = NULL;
25541 debug_addr_section = NULL;
25542 debug_macinfo_section = NULL;
25543 debug_line_section = NULL;
25544 debug_skeleton_line_section = NULL;
25545 debug_loc_section = NULL;
25546 debug_pubnames_section = NULL;
25547 debug_pubtypes_section = NULL;
25548 debug_str_section = NULL;
25549 debug_str_dwo_section = NULL;
25550 debug_str_offsets_section = NULL;
25551 debug_ranges_section = NULL;
25552 debug_frame_section = NULL;
25553 fde_vec = NULL;
25554 debug_str_hash = NULL;
25555 skeleton_debug_str_hash = NULL;
25556 dw2_string_counter = 0;
25557 have_multiple_function_sections = false;
25558 text_section_used = false;
25559 cold_text_section_used = false;
25560 cold_text_section = NULL;
25561 current_unit_personality = NULL;
25563 next_die_offset = 0;
25564 single_comp_unit_die = NULL;
25565 comdat_type_list = NULL;
25566 limbo_die_list = NULL;
25567 file_table = NULL;
25568 decl_die_table = NULL;
25569 common_block_die_table = NULL;
25570 decl_loc_table = NULL;
25571 call_arg_locations = NULL;
25572 call_arg_loc_last = NULL;
25573 call_site_count = -1;
25574 tail_call_site_count = -1;
25575 cached_dw_loc_list_table = NULL;
25576 abbrev_die_table = NULL;
25577 abbrev_die_table_allocated = 0;
25578 abbrev_die_table_in_use = 0;
25579 line_info_label_num = 0;
25580 cur_line_info_table = NULL;
25581 text_section_line_info = NULL;
25582 cold_text_section_line_info = NULL;
25583 separate_line_info = NULL;
25584 info_section_emitted = false;
25585 pubname_table = NULL;
25586 pubtype_table = NULL;
25587 macinfo_table = NULL;
25588 ranges_table = NULL;
25589 ranges_table_allocated = 0;
25590 ranges_table_in_use = 0;
25591 ranges_by_label = 0;
25592 ranges_by_label_allocated = 0;
25593 ranges_by_label_in_use = 0;
25594 have_location_lists = false;
25595 loclabel_num = 0;
25596 poc_label_num = 0;
25597 last_emitted_file = NULL;
25598 label_num = 0;
25599 tmpl_value_parm_die_table = NULL;
25600 generic_type_instances = NULL;
25601 frame_pointer_fb_offset = 0;
25602 frame_pointer_fb_offset_valid = false;
25603 base_types.release ();
25604 XDELETEVEC (producer_string);
25605 producer_string = NULL;
25608 #include "gt-dwarf2out.h"