Daily bump.
[official-gcc.git] / gcc / dwarf2out.c
blobd9d3063be1dda620f3e1e800d3d53cc5f6616d8c
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 "tree.h"
65 #include "fold-const.h"
66 #include "stringpool.h"
67 #include "stor-layout.h"
68 #include "varasm.h"
69 #include "function.h"
70 #include "emit-rtl.h"
71 #include "version.h"
72 #include "flags.h"
73 #include "regs.h"
74 #include "rtlhash.h"
75 #include "insn-config.h"
76 #include "reload.h"
77 #include "output.h"
78 #include "expmed.h"
79 #include "dojump.h"
80 #include "explow.h"
81 #include "calls.h"
82 #include "stmt.h"
83 #include "expr.h"
84 #include "except.h"
85 #include "dwarf2.h"
86 #include "dwarf2out.h"
87 #include "dwarf2asm.h"
88 #include "toplev.h"
89 #include "md5.h"
90 #include "tm_p.h"
91 #include "diagnostic.h"
92 #include "tree-pretty-print.h"
93 #include "debug.h"
94 #include "target.h"
95 #include "common/common-target.h"
96 #include "langhooks.h"
97 #include "cgraph.h"
98 #include "ira.h"
99 #include "lra.h"
100 #include "dumpfile.h"
101 #include "opts.h"
102 #include "tree-dfa.h"
103 #include "gdb/gdb-index.h"
104 #include "rtl-iter.h"
106 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
107 static rtx_insn *last_var_location_insn;
108 static rtx_insn *cached_next_real_insn;
109 static void dwarf2out_decl (tree);
111 #ifdef VMS_DEBUGGING_INFO
112 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
114 /* Define this macro to be a nonzero value if the directory specifications
115 which are output in the debug info should end with a separator. */
116 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
117 /* Define this macro to evaluate to a nonzero value if GCC should refrain
118 from generating indirect strings in DWARF2 debug information, for instance
119 if your target is stuck with an old version of GDB that is unable to
120 process them properly or uses VMS Debug. */
121 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
122 #else
123 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
124 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
125 #endif
127 /* ??? Poison these here until it can be done generically. They've been
128 totally replaced in this file; make sure it stays that way. */
129 #undef DWARF2_UNWIND_INFO
130 #undef DWARF2_FRAME_INFO
131 #if (GCC_VERSION >= 3000)
132 #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
133 #endif
135 /* The size of the target's pointer type. */
136 #ifndef PTR_SIZE
137 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
138 #endif
140 /* Array of RTXes referenced by the debugging information, which therefore
141 must be kept around forever. */
142 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
144 /* A pointer to the base of a list of incomplete types which might be
145 completed at some later time. incomplete_types_list needs to be a
146 vec<tree, va_gc> *because we want to tell the garbage collector about
147 it. */
148 static GTY(()) vec<tree, va_gc> *incomplete_types;
150 /* A pointer to the base of a table of references to declaration
151 scopes. This table is a display which tracks the nesting
152 of declaration scopes at the current scope and containing
153 scopes. This table is used to find the proper place to
154 define type declaration DIE's. */
155 static GTY(()) vec<tree, va_gc> *decl_scope_table;
157 /* Pointers to various DWARF2 sections. */
158 static GTY(()) section *debug_info_section;
159 static GTY(()) section *debug_skeleton_info_section;
160 static GTY(()) section *debug_abbrev_section;
161 static GTY(()) section *debug_skeleton_abbrev_section;
162 static GTY(()) section *debug_aranges_section;
163 static GTY(()) section *debug_addr_section;
164 static GTY(()) section *debug_macinfo_section;
165 static GTY(()) section *debug_line_section;
166 static GTY(()) section *debug_skeleton_line_section;
167 static GTY(()) section *debug_loc_section;
168 static GTY(()) section *debug_pubnames_section;
169 static GTY(()) section *debug_pubtypes_section;
170 static GTY(()) section *debug_str_section;
171 static GTY(()) section *debug_str_dwo_section;
172 static GTY(()) section *debug_str_offsets_section;
173 static GTY(()) section *debug_ranges_section;
174 static GTY(()) section *debug_frame_section;
176 /* Maximum size (in bytes) of an artificially generated label. */
177 #define MAX_ARTIFICIAL_LABEL_BYTES 30
179 /* According to the (draft) DWARF 3 specification, the initial length
180 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
181 bytes are 0xffffffff, followed by the length stored in the next 8
182 bytes.
184 However, the SGI/MIPS ABI uses an initial length which is equal to
185 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
187 #ifndef DWARF_INITIAL_LENGTH_SIZE
188 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
189 #endif
191 /* Round SIZE up to the nearest BOUNDARY. */
192 #define DWARF_ROUND(SIZE,BOUNDARY) \
193 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
195 /* CIE identifier. */
196 #if HOST_BITS_PER_WIDE_INT >= 64
197 #define DWARF_CIE_ID \
198 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
199 #else
200 #define DWARF_CIE_ID DW_CIE_ID
201 #endif
204 /* A vector for a table that contains frame description
205 information for each routine. */
206 #define NOT_INDEXED (-1U)
207 #define NO_INDEX_ASSIGNED (-2U)
209 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
211 struct GTY((for_user)) indirect_string_node {
212 const char *str;
213 unsigned int refcount;
214 enum dwarf_form form;
215 char *label;
216 unsigned int index;
219 struct indirect_string_hasher : ggc_ptr_hash<indirect_string_node>
221 typedef const char *compare_type;
223 static hashval_t hash (indirect_string_node *);
224 static bool equal (indirect_string_node *, const char *);
227 static GTY (()) hash_table<indirect_string_hasher> *debug_str_hash;
229 /* With split_debug_info, both the comp_dir and dwo_name go in the
230 main object file, rather than the dwo, similar to the force_direct
231 parameter elsewhere but with additional complications:
233 1) The string is needed in both the main object file and the dwo.
234 That is, the comp_dir and dwo_name will appear in both places.
236 2) Strings can use three forms: DW_FORM_string, DW_FORM_strp or
237 DW_FORM_GNU_str_index.
239 3) GCC chooses the form to use late, depending on the size and
240 reference count.
242 Rather than forcing the all debug string handling functions and
243 callers to deal with these complications, simply use a separate,
244 special-cased string table for any attribute that should go in the
245 main object file. This limits the complexity to just the places
246 that need it. */
248 static GTY (()) hash_table<indirect_string_hasher> *skeleton_debug_str_hash;
250 static GTY(()) int dw2_string_counter;
252 /* True if the compilation unit places functions in more than one section. */
253 static GTY(()) bool have_multiple_function_sections = false;
255 /* Whether the default text and cold text sections have been used at all. */
257 static GTY(()) bool text_section_used = false;
258 static GTY(()) bool cold_text_section_used = false;
260 /* The default cold text section. */
261 static GTY(()) section *cold_text_section;
263 /* The DIE for C++14 'auto' in a function return type. */
264 static GTY(()) dw_die_ref auto_die;
266 /* The DIE for C++14 'decltype(auto)' in a function return type. */
267 static GTY(()) dw_die_ref decltype_auto_die;
269 /* Forward declarations for functions defined in this file. */
271 static char *stripattributes (const char *);
272 static void output_call_frame_info (int);
273 static void dwarf2out_note_section_used (void);
275 /* Personality decl of current unit. Used only when assembler does not support
276 personality CFI. */
277 static GTY(()) rtx current_unit_personality;
279 /* Data and reference forms for relocatable data. */
280 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
281 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
283 #ifndef DEBUG_FRAME_SECTION
284 #define DEBUG_FRAME_SECTION ".debug_frame"
285 #endif
287 #ifndef FUNC_BEGIN_LABEL
288 #define FUNC_BEGIN_LABEL "LFB"
289 #endif
291 #ifndef FUNC_END_LABEL
292 #define FUNC_END_LABEL "LFE"
293 #endif
295 #ifndef PROLOGUE_END_LABEL
296 #define PROLOGUE_END_LABEL "LPE"
297 #endif
299 #ifndef EPILOGUE_BEGIN_LABEL
300 #define EPILOGUE_BEGIN_LABEL "LEB"
301 #endif
303 #ifndef FRAME_BEGIN_LABEL
304 #define FRAME_BEGIN_LABEL "Lframe"
305 #endif
306 #define CIE_AFTER_SIZE_LABEL "LSCIE"
307 #define CIE_END_LABEL "LECIE"
308 #define FDE_LABEL "LSFDE"
309 #define FDE_AFTER_SIZE_LABEL "LASFDE"
310 #define FDE_END_LABEL "LEFDE"
311 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
312 #define LINE_NUMBER_END_LABEL "LELT"
313 #define LN_PROLOG_AS_LABEL "LASLTP"
314 #define LN_PROLOG_END_LABEL "LELTP"
315 #define DIE_LABEL_PREFIX "DW"
317 /* Match the base name of a file to the base name of a compilation unit. */
319 static int
320 matches_main_base (const char *path)
322 /* Cache the last query. */
323 static const char *last_path = NULL;
324 static int last_match = 0;
325 if (path != last_path)
327 const char *base;
328 int length = base_of_path (path, &base);
329 last_path = path;
330 last_match = (length == main_input_baselength
331 && memcmp (base, main_input_basename, length) == 0);
333 return last_match;
336 #ifdef DEBUG_DEBUG_STRUCT
338 static int
339 dump_struct_debug (tree type, enum debug_info_usage usage,
340 enum debug_struct_file criterion, int generic,
341 int matches, int result)
343 /* Find the type name. */
344 tree type_decl = TYPE_STUB_DECL (type);
345 tree t = type_decl;
346 const char *name = 0;
347 if (TREE_CODE (t) == TYPE_DECL)
348 t = DECL_NAME (t);
349 if (t)
350 name = IDENTIFIER_POINTER (t);
352 fprintf (stderr, " struct %d %s %s %s %s %d %p %s\n",
353 criterion,
354 DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
355 matches ? "bas" : "hdr",
356 generic ? "gen" : "ord",
357 usage == DINFO_USAGE_DFN ? ";" :
358 usage == DINFO_USAGE_DIR_USE ? "." : "*",
359 result,
360 (void*) type_decl, name);
361 return result;
363 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
364 dump_struct_debug (type, usage, criterion, generic, matches, result)
366 #else
368 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
369 (result)
371 #endif
373 /* Get the number of HOST_WIDE_INTs needed to represent the precision
374 of the number. */
376 static unsigned int
377 get_full_len (const wide_int &op)
379 return ((op.get_precision () + HOST_BITS_PER_WIDE_INT - 1)
380 / HOST_BITS_PER_WIDE_INT);
383 static bool
384 should_emit_struct_debug (tree type, enum debug_info_usage usage)
386 enum debug_struct_file criterion;
387 tree type_decl;
388 bool generic = lang_hooks.types.generic_p (type);
390 if (generic)
391 criterion = debug_struct_generic[usage];
392 else
393 criterion = debug_struct_ordinary[usage];
395 if (criterion == DINFO_STRUCT_FILE_NONE)
396 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
397 if (criterion == DINFO_STRUCT_FILE_ANY)
398 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
400 type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
402 if (type_decl != NULL)
404 if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
405 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
407 if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
408 return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
411 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
414 /* Return a pointer to a copy of the section string name S with all
415 attributes stripped off, and an asterisk prepended (for assemble_name). */
417 static inline char *
418 stripattributes (const char *s)
420 char *stripped = XNEWVEC (char, strlen (s) + 2);
421 char *p = stripped;
423 *p++ = '*';
425 while (*s && *s != ',')
426 *p++ = *s++;
428 *p = '\0';
429 return stripped;
432 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
433 switch to the data section instead, and write out a synthetic start label
434 for collect2 the first time around. */
436 static void
437 switch_to_eh_frame_section (bool back)
439 tree label;
441 #ifdef EH_FRAME_SECTION_NAME
442 if (eh_frame_section == 0)
444 int flags;
446 if (EH_TABLES_CAN_BE_READ_ONLY)
448 int fde_encoding;
449 int per_encoding;
450 int lsda_encoding;
452 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
453 /*global=*/0);
454 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
455 /*global=*/1);
456 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
457 /*global=*/0);
458 flags = ((! flag_pic
459 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
460 && (fde_encoding & 0x70) != DW_EH_PE_aligned
461 && (per_encoding & 0x70) != DW_EH_PE_absptr
462 && (per_encoding & 0x70) != DW_EH_PE_aligned
463 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
464 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
465 ? 0 : SECTION_WRITE);
467 else
468 flags = SECTION_WRITE;
469 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
471 #endif /* EH_FRAME_SECTION_NAME */
473 if (eh_frame_section)
474 switch_to_section (eh_frame_section);
475 else
477 /* We have no special eh_frame section. Put the information in
478 the data section and emit special labels to guide collect2. */
479 switch_to_section (data_section);
481 if (!back)
483 label = get_file_function_name ("F");
484 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
485 targetm.asm_out.globalize_label (asm_out_file,
486 IDENTIFIER_POINTER (label));
487 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
492 /* Switch [BACK] to the eh or debug frame table section, depending on
493 FOR_EH. */
495 static void
496 switch_to_frame_table_section (int for_eh, bool back)
498 if (for_eh)
499 switch_to_eh_frame_section (back);
500 else
502 if (!debug_frame_section)
503 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
504 SECTION_DEBUG, NULL);
505 switch_to_section (debug_frame_section);
509 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
511 enum dw_cfi_oprnd_type
512 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
514 switch (cfi)
516 case DW_CFA_nop:
517 case DW_CFA_GNU_window_save:
518 case DW_CFA_remember_state:
519 case DW_CFA_restore_state:
520 return dw_cfi_oprnd_unused;
522 case DW_CFA_set_loc:
523 case DW_CFA_advance_loc1:
524 case DW_CFA_advance_loc2:
525 case DW_CFA_advance_loc4:
526 case DW_CFA_MIPS_advance_loc8:
527 return dw_cfi_oprnd_addr;
529 case DW_CFA_offset:
530 case DW_CFA_offset_extended:
531 case DW_CFA_def_cfa:
532 case DW_CFA_offset_extended_sf:
533 case DW_CFA_def_cfa_sf:
534 case DW_CFA_restore:
535 case DW_CFA_restore_extended:
536 case DW_CFA_undefined:
537 case DW_CFA_same_value:
538 case DW_CFA_def_cfa_register:
539 case DW_CFA_register:
540 case DW_CFA_expression:
541 return dw_cfi_oprnd_reg_num;
543 case DW_CFA_def_cfa_offset:
544 case DW_CFA_GNU_args_size:
545 case DW_CFA_def_cfa_offset_sf:
546 return dw_cfi_oprnd_offset;
548 case DW_CFA_def_cfa_expression:
549 return dw_cfi_oprnd_loc;
551 default:
552 gcc_unreachable ();
556 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
558 enum dw_cfi_oprnd_type
559 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
561 switch (cfi)
563 case DW_CFA_def_cfa:
564 case DW_CFA_def_cfa_sf:
565 case DW_CFA_offset:
566 case DW_CFA_offset_extended_sf:
567 case DW_CFA_offset_extended:
568 return dw_cfi_oprnd_offset;
570 case DW_CFA_register:
571 return dw_cfi_oprnd_reg_num;
573 case DW_CFA_expression:
574 return dw_cfi_oprnd_loc;
576 default:
577 return dw_cfi_oprnd_unused;
581 /* Output one FDE. */
583 static void
584 output_fde (dw_fde_ref fde, bool for_eh, bool second,
585 char *section_start_label, int fde_encoding, char *augmentation,
586 bool any_lsda_needed, int lsda_encoding)
588 const char *begin, *end;
589 static unsigned int j;
590 char l1[20], l2[20];
592 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
593 /* empty */ 0);
594 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
595 for_eh + j);
596 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
597 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
598 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
599 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
600 " indicating 64-bit DWARF extension");
601 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
602 "FDE Length");
603 ASM_OUTPUT_LABEL (asm_out_file, l1);
605 if (for_eh)
606 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
607 else
608 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
609 debug_frame_section, "FDE CIE offset");
611 begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
612 end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
614 if (for_eh)
616 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
617 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
618 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
619 "FDE initial location");
620 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
621 end, begin, "FDE address range");
623 else
625 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
626 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
629 if (augmentation[0])
631 if (any_lsda_needed)
633 int size = size_of_encoded_value (lsda_encoding);
635 if (lsda_encoding == DW_EH_PE_aligned)
637 int offset = ( 4 /* Length */
638 + 4 /* CIE offset */
639 + 2 * size_of_encoded_value (fde_encoding)
640 + 1 /* Augmentation size */ );
641 int pad = -offset & (PTR_SIZE - 1);
643 size += pad;
644 gcc_assert (size_of_uleb128 (size) == 1);
647 dw2_asm_output_data_uleb128 (size, "Augmentation size");
649 if (fde->uses_eh_lsda)
651 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
652 fde->funcdef_number);
653 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
654 gen_rtx_SYMBOL_REF (Pmode, l1),
655 false,
656 "Language Specific Data Area");
658 else
660 if (lsda_encoding == DW_EH_PE_aligned)
661 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
662 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
663 "Language Specific Data Area (none)");
666 else
667 dw2_asm_output_data_uleb128 (0, "Augmentation size");
670 /* Loop through the Call Frame Instructions associated with this FDE. */
671 fde->dw_fde_current_label = begin;
673 size_t from, until, i;
675 from = 0;
676 until = vec_safe_length (fde->dw_fde_cfi);
678 if (fde->dw_fde_second_begin == NULL)
680 else if (!second)
681 until = fde->dw_fde_switch_cfi_index;
682 else
683 from = fde->dw_fde_switch_cfi_index;
685 for (i = from; i < until; i++)
686 output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
689 /* If we are to emit a ref/link from function bodies to their frame tables,
690 do it now. This is typically performed to make sure that tables
691 associated with functions are dragged with them and not discarded in
692 garbage collecting links. We need to do this on a per function basis to
693 cope with -ffunction-sections. */
695 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
696 /* Switch to the function section, emit the ref to the tables, and
697 switch *back* into the table section. */
698 switch_to_section (function_section (fde->decl));
699 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
700 switch_to_frame_table_section (for_eh, true);
701 #endif
703 /* Pad the FDE out to an address sized boundary. */
704 ASM_OUTPUT_ALIGN (asm_out_file,
705 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
706 ASM_OUTPUT_LABEL (asm_out_file, l2);
708 j += 2;
711 /* Return true if frame description entry FDE is needed for EH. */
713 static bool
714 fde_needed_for_eh_p (dw_fde_ref fde)
716 if (flag_asynchronous_unwind_tables)
717 return true;
719 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
720 return true;
722 if (fde->uses_eh_lsda)
723 return true;
725 /* If exceptions are enabled, we have collected nothrow info. */
726 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
727 return false;
729 return true;
732 /* Output the call frame information used to record information
733 that relates to calculating the frame pointer, and records the
734 location of saved registers. */
736 static void
737 output_call_frame_info (int for_eh)
739 unsigned int i;
740 dw_fde_ref fde;
741 dw_cfi_ref cfi;
742 char l1[20], l2[20], section_start_label[20];
743 bool any_lsda_needed = false;
744 char augmentation[6];
745 int augmentation_size;
746 int fde_encoding = DW_EH_PE_absptr;
747 int per_encoding = DW_EH_PE_absptr;
748 int lsda_encoding = DW_EH_PE_absptr;
749 int return_reg;
750 rtx personality = NULL;
751 int dw_cie_version;
753 /* Don't emit a CIE if there won't be any FDEs. */
754 if (!fde_vec)
755 return;
757 /* Nothing to do if the assembler's doing it all. */
758 if (dwarf2out_do_cfi_asm ())
759 return;
761 /* If we don't have any functions we'll want to unwind out of, don't emit
762 any EH unwind information. If we make FDEs linkonce, we may have to
763 emit an empty label for an FDE that wouldn't otherwise be emitted. We
764 want to avoid having an FDE kept around when the function it refers to
765 is discarded. Example where this matters: a primary function template
766 in C++ requires EH information, an explicit specialization doesn't. */
767 if (for_eh)
769 bool any_eh_needed = false;
771 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
773 if (fde->uses_eh_lsda)
774 any_eh_needed = any_lsda_needed = true;
775 else if (fde_needed_for_eh_p (fde))
776 any_eh_needed = true;
777 else if (TARGET_USES_WEAK_UNWIND_INFO)
778 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
781 if (!any_eh_needed)
782 return;
785 /* We're going to be generating comments, so turn on app. */
786 if (flag_debug_asm)
787 app_enable ();
789 /* Switch to the proper frame section, first time. */
790 switch_to_frame_table_section (for_eh, false);
792 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
793 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
795 /* Output the CIE. */
796 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
797 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
798 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
799 dw2_asm_output_data (4, 0xffffffff,
800 "Initial length escape value indicating 64-bit DWARF extension");
801 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
802 "Length of Common Information Entry");
803 ASM_OUTPUT_LABEL (asm_out_file, l1);
805 /* Now that the CIE pointer is PC-relative for EH,
806 use 0 to identify the CIE. */
807 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
808 (for_eh ? 0 : DWARF_CIE_ID),
809 "CIE Identifier Tag");
811 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
812 use CIE version 1, unless that would produce incorrect results
813 due to overflowing the return register column. */
814 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
815 dw_cie_version = 1;
816 if (return_reg >= 256 || dwarf_version > 2)
817 dw_cie_version = 3;
818 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
820 augmentation[0] = 0;
821 augmentation_size = 0;
823 personality = current_unit_personality;
824 if (for_eh)
826 char *p;
828 /* Augmentation:
829 z Indicates that a uleb128 is present to size the
830 augmentation section.
831 L Indicates the encoding (and thus presence) of
832 an LSDA pointer in the FDE augmentation.
833 R Indicates a non-default pointer encoding for
834 FDE code pointers.
835 P Indicates the presence of an encoding + language
836 personality routine in the CIE augmentation. */
838 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
839 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
840 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
842 p = augmentation + 1;
843 if (personality)
845 *p++ = 'P';
846 augmentation_size += 1 + size_of_encoded_value (per_encoding);
847 assemble_external_libcall (personality);
849 if (any_lsda_needed)
851 *p++ = 'L';
852 augmentation_size += 1;
854 if (fde_encoding != DW_EH_PE_absptr)
856 *p++ = 'R';
857 augmentation_size += 1;
859 if (p > augmentation + 1)
861 augmentation[0] = 'z';
862 *p = '\0';
865 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
866 if (personality && per_encoding == DW_EH_PE_aligned)
868 int offset = ( 4 /* Length */
869 + 4 /* CIE Id */
870 + 1 /* CIE version */
871 + strlen (augmentation) + 1 /* Augmentation */
872 + size_of_uleb128 (1) /* Code alignment */
873 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
874 + 1 /* RA column */
875 + 1 /* Augmentation size */
876 + 1 /* Personality encoding */ );
877 int pad = -offset & (PTR_SIZE - 1);
879 augmentation_size += pad;
881 /* Augmentations should be small, so there's scarce need to
882 iterate for a solution. Die if we exceed one uleb128 byte. */
883 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
887 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
888 if (dw_cie_version >= 4)
890 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
891 dw2_asm_output_data (1, 0, "CIE Segment Size");
893 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
894 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
895 "CIE Data Alignment Factor");
897 if (dw_cie_version == 1)
898 dw2_asm_output_data (1, return_reg, "CIE RA Column");
899 else
900 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
902 if (augmentation[0])
904 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
905 if (personality)
907 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
908 eh_data_format_name (per_encoding));
909 dw2_asm_output_encoded_addr_rtx (per_encoding,
910 personality,
911 true, NULL);
914 if (any_lsda_needed)
915 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
916 eh_data_format_name (lsda_encoding));
918 if (fde_encoding != DW_EH_PE_absptr)
919 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
920 eh_data_format_name (fde_encoding));
923 FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
924 output_cfi (cfi, NULL, for_eh);
926 /* Pad the CIE out to an address sized boundary. */
927 ASM_OUTPUT_ALIGN (asm_out_file,
928 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
929 ASM_OUTPUT_LABEL (asm_out_file, l2);
931 /* Loop through all of the FDE's. */
932 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
934 unsigned int k;
936 /* Don't emit EH unwind info for leaf functions that don't need it. */
937 if (for_eh && !fde_needed_for_eh_p (fde))
938 continue;
940 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
941 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
942 augmentation, any_lsda_needed, lsda_encoding);
945 if (for_eh && targetm.terminate_dw2_eh_frame_info)
946 dw2_asm_output_data (4, 0, "End of Table");
948 /* Turn off app to make assembly quicker. */
949 if (flag_debug_asm)
950 app_disable ();
953 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
955 static void
956 dwarf2out_do_cfi_startproc (bool second)
958 int enc;
959 rtx ref;
960 rtx personality = get_personality_function (current_function_decl);
962 fprintf (asm_out_file, "\t.cfi_startproc\n");
964 if (personality)
966 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
967 ref = personality;
969 /* ??? The GAS support isn't entirely consistent. We have to
970 handle indirect support ourselves, but PC-relative is done
971 in the assembler. Further, the assembler can't handle any
972 of the weirder relocation types. */
973 if (enc & DW_EH_PE_indirect)
974 ref = dw2_force_const_mem (ref, true);
976 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
977 output_addr_const (asm_out_file, ref);
978 fputc ('\n', asm_out_file);
981 if (crtl->uses_eh_lsda)
983 char lab[20];
985 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
986 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
987 current_function_funcdef_no);
988 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
989 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
991 if (enc & DW_EH_PE_indirect)
992 ref = dw2_force_const_mem (ref, true);
994 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
995 output_addr_const (asm_out_file, ref);
996 fputc ('\n', asm_out_file);
1000 /* Allocate CURRENT_FDE. Immediately initialize all we can, noting that
1001 this allocation may be done before pass_final. */
1003 dw_fde_ref
1004 dwarf2out_alloc_current_fde (void)
1006 dw_fde_ref fde;
1008 fde = ggc_cleared_alloc<dw_fde_node> ();
1009 fde->decl = current_function_decl;
1010 fde->funcdef_number = current_function_funcdef_no;
1011 fde->fde_index = vec_safe_length (fde_vec);
1012 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
1013 fde->uses_eh_lsda = crtl->uses_eh_lsda;
1014 fde->nothrow = crtl->nothrow;
1015 fde->drap_reg = INVALID_REGNUM;
1016 fde->vdrap_reg = INVALID_REGNUM;
1018 /* Record the FDE associated with this function. */
1019 cfun->fde = fde;
1020 vec_safe_push (fde_vec, fde);
1022 return fde;
1025 /* Output a marker (i.e. a label) for the beginning of a function, before
1026 the prologue. */
1028 void
1029 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1030 const char *file ATTRIBUTE_UNUSED)
1032 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1033 char * dup_label;
1034 dw_fde_ref fde;
1035 section *fnsec;
1036 bool do_frame;
1038 current_function_func_begin_label = NULL;
1040 do_frame = dwarf2out_do_frame ();
1042 /* ??? current_function_func_begin_label is also used by except.c for
1043 call-site information. We must emit this label if it might be used. */
1044 if (!do_frame
1045 && (!flag_exceptions
1046 || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1047 return;
1049 fnsec = function_section (current_function_decl);
1050 switch_to_section (fnsec);
1051 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1052 current_function_funcdef_no);
1053 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1054 current_function_funcdef_no);
1055 dup_label = xstrdup (label);
1056 current_function_func_begin_label = dup_label;
1058 /* We can elide the fde allocation if we're not emitting debug info. */
1059 if (!do_frame)
1060 return;
1062 /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1063 emit insns as rtx but bypass the bulk of rest_of_compilation, which
1064 would include pass_dwarf2_frame. If we've not created the FDE yet,
1065 do so now. */
1066 fde = cfun->fde;
1067 if (fde == NULL)
1068 fde = dwarf2out_alloc_current_fde ();
1070 /* Initialize the bits of CURRENT_FDE that were not available earlier. */
1071 fde->dw_fde_begin = dup_label;
1072 fde->dw_fde_current_label = dup_label;
1073 fde->in_std_section = (fnsec == text_section
1074 || (cold_text_section && fnsec == cold_text_section));
1076 /* We only want to output line number information for the genuine dwarf2
1077 prologue case, not the eh frame case. */
1078 #ifdef DWARF2_DEBUGGING_INFO
1079 if (file)
1080 dwarf2out_source_line (line, file, 0, true);
1081 #endif
1083 if (dwarf2out_do_cfi_asm ())
1084 dwarf2out_do_cfi_startproc (false);
1085 else
1087 rtx personality = get_personality_function (current_function_decl);
1088 if (!current_unit_personality)
1089 current_unit_personality = personality;
1091 /* We cannot keep a current personality per function as without CFI
1092 asm, at the point where we emit the CFI data, there is no current
1093 function anymore. */
1094 if (personality && current_unit_personality != personality)
1095 sorry ("multiple EH personalities are supported only with assemblers "
1096 "supporting .cfi_personality directive");
1100 /* Output a marker (i.e. a label) for the end of the generated code
1101 for a function prologue. This gets called *after* the prologue code has
1102 been generated. */
1104 void
1105 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1106 const char *file ATTRIBUTE_UNUSED)
1108 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1110 /* Output a label to mark the endpoint of the code generated for this
1111 function. */
1112 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1113 current_function_funcdef_no);
1114 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1115 current_function_funcdef_no);
1116 cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1119 /* Output a marker (i.e. a label) for the beginning of the generated code
1120 for a function epilogue. This gets called *before* the prologue code has
1121 been generated. */
1123 void
1124 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1125 const char *file ATTRIBUTE_UNUSED)
1127 dw_fde_ref fde = cfun->fde;
1128 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1130 if (fde->dw_fde_vms_begin_epilogue)
1131 return;
1133 /* Output a label to mark the endpoint of the code generated for this
1134 function. */
1135 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1136 current_function_funcdef_no);
1137 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1138 current_function_funcdef_no);
1139 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1142 /* Output a marker (i.e. a label) for the absolute end of the generated code
1143 for a function definition. This gets called *after* the epilogue code has
1144 been generated. */
1146 void
1147 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1148 const char *file ATTRIBUTE_UNUSED)
1150 dw_fde_ref fde;
1151 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1153 last_var_location_insn = NULL;
1154 cached_next_real_insn = NULL;
1156 if (dwarf2out_do_cfi_asm ())
1157 fprintf (asm_out_file, "\t.cfi_endproc\n");
1159 /* Output a label to mark the endpoint of the code generated for this
1160 function. */
1161 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1162 current_function_funcdef_no);
1163 ASM_OUTPUT_LABEL (asm_out_file, label);
1164 fde = cfun->fde;
1165 gcc_assert (fde != NULL);
1166 if (fde->dw_fde_second_begin == NULL)
1167 fde->dw_fde_end = xstrdup (label);
1170 void
1171 dwarf2out_frame_finish (void)
1173 /* Output call frame information. */
1174 if (targetm.debug_unwind_info () == UI_DWARF2)
1175 output_call_frame_info (0);
1177 /* Output another copy for the unwinder. */
1178 if ((flag_unwind_tables || flag_exceptions)
1179 && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1180 output_call_frame_info (1);
1183 /* Note that the current function section is being used for code. */
1185 static void
1186 dwarf2out_note_section_used (void)
1188 section *sec = current_function_section ();
1189 if (sec == text_section)
1190 text_section_used = true;
1191 else if (sec == cold_text_section)
1192 cold_text_section_used = true;
1195 static void var_location_switch_text_section (void);
1196 static void set_cur_line_info_table (section *);
1198 void
1199 dwarf2out_switch_text_section (void)
1201 section *sect;
1202 dw_fde_ref fde = cfun->fde;
1204 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1206 if (!in_cold_section_p)
1208 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1209 fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1210 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1212 else
1214 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1215 fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1216 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1218 have_multiple_function_sections = true;
1220 /* There is no need to mark used sections when not debugging. */
1221 if (cold_text_section != NULL)
1222 dwarf2out_note_section_used ();
1224 if (dwarf2out_do_cfi_asm ())
1225 fprintf (asm_out_file, "\t.cfi_endproc\n");
1227 /* Now do the real section switch. */
1228 sect = current_function_section ();
1229 switch_to_section (sect);
1231 fde->second_in_std_section
1232 = (sect == text_section
1233 || (cold_text_section && sect == cold_text_section));
1235 if (dwarf2out_do_cfi_asm ())
1236 dwarf2out_do_cfi_startproc (true);
1238 var_location_switch_text_section ();
1240 if (cold_text_section != NULL)
1241 set_cur_line_info_table (sect);
1244 /* And now, the subset of the debugging information support code necessary
1245 for emitting location expressions. */
1247 /* Data about a single source file. */
1248 struct GTY((for_user)) dwarf_file_data {
1249 const char * filename;
1250 int emitted_number;
1253 /* Describe an entry into the .debug_addr section. */
1255 enum ate_kind {
1256 ate_kind_rtx,
1257 ate_kind_rtx_dtprel,
1258 ate_kind_label
1261 struct GTY((for_user)) addr_table_entry {
1262 enum ate_kind kind;
1263 unsigned int refcount;
1264 unsigned int index;
1265 union addr_table_entry_struct_union
1267 rtx GTY ((tag ("0"))) rtl;
1268 char * GTY ((tag ("1"))) label;
1270 GTY ((desc ("%1.kind"))) addr;
1273 /* Location lists are ranges + location descriptions for that range,
1274 so you can track variables that are in different places over
1275 their entire life. */
1276 typedef struct GTY(()) dw_loc_list_struct {
1277 dw_loc_list_ref dw_loc_next;
1278 const char *begin; /* Label and addr_entry for start of range */
1279 addr_table_entry *begin_entry;
1280 const char *end; /* Label for end of range */
1281 char *ll_symbol; /* Label for beginning of location list.
1282 Only on head of list */
1283 const char *section; /* Section this loclist is relative to */
1284 dw_loc_descr_ref expr;
1285 hashval_t hash;
1286 /* True if all addresses in this and subsequent lists are known to be
1287 resolved. */
1288 bool resolved_addr;
1289 /* True if this list has been replaced by dw_loc_next. */
1290 bool replaced;
1291 bool emitted;
1292 /* True if the range should be emitted even if begin and end
1293 are the same. */
1294 bool force;
1295 } dw_loc_list_node;
1297 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1299 /* Convert a DWARF stack opcode into its string name. */
1301 static const char *
1302 dwarf_stack_op_name (unsigned int op)
1304 const char *name = get_DW_OP_name (op);
1306 if (name != NULL)
1307 return name;
1309 return "OP_<unknown>";
1312 /* Return a pointer to a newly allocated location description. Location
1313 descriptions are simple expression terms that can be strung
1314 together to form more complicated location (address) descriptions. */
1316 static inline dw_loc_descr_ref
1317 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1318 unsigned HOST_WIDE_INT oprnd2)
1320 dw_loc_descr_ref descr = ggc_cleared_alloc<dw_loc_descr_node> ();
1322 descr->dw_loc_opc = op;
1323 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1324 descr->dw_loc_oprnd1.val_entry = NULL;
1325 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1326 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1327 descr->dw_loc_oprnd2.val_entry = NULL;
1328 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1330 return descr;
1333 /* Return a pointer to a newly allocated location description for
1334 REG and OFFSET. */
1336 static inline dw_loc_descr_ref
1337 new_reg_loc_descr (unsigned int reg, unsigned HOST_WIDE_INT offset)
1339 if (reg <= 31)
1340 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1341 offset, 0);
1342 else
1343 return new_loc_descr (DW_OP_bregx, reg, offset);
1346 /* Add a location description term to a location description expression. */
1348 static inline void
1349 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1351 dw_loc_descr_ref *d;
1353 /* Find the end of the chain. */
1354 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1357 *d = descr;
1360 /* Compare two location operands for exact equality. */
1362 static bool
1363 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1365 if (a->val_class != b->val_class)
1366 return false;
1367 switch (a->val_class)
1369 case dw_val_class_none:
1370 return true;
1371 case dw_val_class_addr:
1372 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1374 case dw_val_class_offset:
1375 case dw_val_class_unsigned_const:
1376 case dw_val_class_const:
1377 case dw_val_class_range_list:
1378 case dw_val_class_lineptr:
1379 case dw_val_class_macptr:
1380 /* These are all HOST_WIDE_INT, signed or unsigned. */
1381 return a->v.val_unsigned == b->v.val_unsigned;
1383 case dw_val_class_loc:
1384 return a->v.val_loc == b->v.val_loc;
1385 case dw_val_class_loc_list:
1386 return a->v.val_loc_list == b->v.val_loc_list;
1387 case dw_val_class_die_ref:
1388 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1389 case dw_val_class_fde_ref:
1390 return a->v.val_fde_index == b->v.val_fde_index;
1391 case dw_val_class_lbl_id:
1392 case dw_val_class_high_pc:
1393 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1394 case dw_val_class_str:
1395 return a->v.val_str == b->v.val_str;
1396 case dw_val_class_flag:
1397 return a->v.val_flag == b->v.val_flag;
1398 case dw_val_class_file:
1399 return a->v.val_file == b->v.val_file;
1400 case dw_val_class_decl_ref:
1401 return a->v.val_decl_ref == b->v.val_decl_ref;
1403 case dw_val_class_const_double:
1404 return (a->v.val_double.high == b->v.val_double.high
1405 && a->v.val_double.low == b->v.val_double.low);
1407 case dw_val_class_wide_int:
1408 return *a->v.val_wide == *b->v.val_wide;
1410 case dw_val_class_vec:
1412 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1413 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1415 return (a_len == b_len
1416 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1419 case dw_val_class_data8:
1420 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1422 case dw_val_class_vms_delta:
1423 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1424 && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1426 gcc_unreachable ();
1429 /* Compare two location atoms for exact equality. */
1431 static bool
1432 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1434 if (a->dw_loc_opc != b->dw_loc_opc)
1435 return false;
1437 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1438 address size, but since we always allocate cleared storage it
1439 should be zero for other types of locations. */
1440 if (a->dtprel != b->dtprel)
1441 return false;
1443 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1444 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1447 /* Compare two complete location expressions for exact equality. */
1449 bool
1450 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1452 while (1)
1454 if (a == b)
1455 return true;
1456 if (a == NULL || b == NULL)
1457 return false;
1458 if (!loc_descr_equal_p_1 (a, b))
1459 return false;
1461 a = a->dw_loc_next;
1462 b = b->dw_loc_next;
1467 /* Add a constant OFFSET to a location expression. */
1469 static void
1470 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1472 dw_loc_descr_ref loc;
1473 HOST_WIDE_INT *p;
1475 gcc_assert (*list_head != NULL);
1477 if (!offset)
1478 return;
1480 /* Find the end of the chain. */
1481 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1484 p = NULL;
1485 if (loc->dw_loc_opc == DW_OP_fbreg
1486 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1487 p = &loc->dw_loc_oprnd1.v.val_int;
1488 else if (loc->dw_loc_opc == DW_OP_bregx)
1489 p = &loc->dw_loc_oprnd2.v.val_int;
1491 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1492 offset. Don't optimize if an signed integer overflow would happen. */
1493 if (p != NULL
1494 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1495 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1496 *p += offset;
1498 else if (offset > 0)
1499 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1501 else
1503 loc->dw_loc_next = int_loc_descriptor (-offset);
1504 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1508 /* Add a constant OFFSET to a location list. */
1510 static void
1511 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1513 dw_loc_list_ref d;
1514 for (d = list_head; d != NULL; d = d->dw_loc_next)
1515 loc_descr_plus_const (&d->expr, offset);
1518 #define DWARF_REF_SIZE \
1519 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1521 static unsigned long int get_base_type_offset (dw_die_ref);
1523 /* Return the size of a location descriptor. */
1525 static unsigned long
1526 size_of_loc_descr (dw_loc_descr_ref loc)
1528 unsigned long size = 1;
1530 switch (loc->dw_loc_opc)
1532 case DW_OP_addr:
1533 size += DWARF2_ADDR_SIZE;
1534 break;
1535 case DW_OP_GNU_addr_index:
1536 case DW_OP_GNU_const_index:
1537 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1538 size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1539 break;
1540 case DW_OP_const1u:
1541 case DW_OP_const1s:
1542 size += 1;
1543 break;
1544 case DW_OP_const2u:
1545 case DW_OP_const2s:
1546 size += 2;
1547 break;
1548 case DW_OP_const4u:
1549 case DW_OP_const4s:
1550 size += 4;
1551 break;
1552 case DW_OP_const8u:
1553 case DW_OP_const8s:
1554 size += 8;
1555 break;
1556 case DW_OP_constu:
1557 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1558 break;
1559 case DW_OP_consts:
1560 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1561 break;
1562 case DW_OP_pick:
1563 size += 1;
1564 break;
1565 case DW_OP_plus_uconst:
1566 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1567 break;
1568 case DW_OP_skip:
1569 case DW_OP_bra:
1570 size += 2;
1571 break;
1572 case DW_OP_breg0:
1573 case DW_OP_breg1:
1574 case DW_OP_breg2:
1575 case DW_OP_breg3:
1576 case DW_OP_breg4:
1577 case DW_OP_breg5:
1578 case DW_OP_breg6:
1579 case DW_OP_breg7:
1580 case DW_OP_breg8:
1581 case DW_OP_breg9:
1582 case DW_OP_breg10:
1583 case DW_OP_breg11:
1584 case DW_OP_breg12:
1585 case DW_OP_breg13:
1586 case DW_OP_breg14:
1587 case DW_OP_breg15:
1588 case DW_OP_breg16:
1589 case DW_OP_breg17:
1590 case DW_OP_breg18:
1591 case DW_OP_breg19:
1592 case DW_OP_breg20:
1593 case DW_OP_breg21:
1594 case DW_OP_breg22:
1595 case DW_OP_breg23:
1596 case DW_OP_breg24:
1597 case DW_OP_breg25:
1598 case DW_OP_breg26:
1599 case DW_OP_breg27:
1600 case DW_OP_breg28:
1601 case DW_OP_breg29:
1602 case DW_OP_breg30:
1603 case DW_OP_breg31:
1604 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1605 break;
1606 case DW_OP_regx:
1607 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1608 break;
1609 case DW_OP_fbreg:
1610 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1611 break;
1612 case DW_OP_bregx:
1613 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1614 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1615 break;
1616 case DW_OP_piece:
1617 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1618 break;
1619 case DW_OP_bit_piece:
1620 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1621 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1622 break;
1623 case DW_OP_deref_size:
1624 case DW_OP_xderef_size:
1625 size += 1;
1626 break;
1627 case DW_OP_call2:
1628 size += 2;
1629 break;
1630 case DW_OP_call4:
1631 size += 4;
1632 break;
1633 case DW_OP_call_ref:
1634 size += DWARF_REF_SIZE;
1635 break;
1636 case DW_OP_implicit_value:
1637 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1638 + loc->dw_loc_oprnd1.v.val_unsigned;
1639 break;
1640 case DW_OP_GNU_implicit_pointer:
1641 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1642 break;
1643 case DW_OP_GNU_entry_value:
1645 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1646 size += size_of_uleb128 (op_size) + op_size;
1647 break;
1649 case DW_OP_GNU_const_type:
1651 unsigned long o
1652 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1653 size += size_of_uleb128 (o) + 1;
1654 switch (loc->dw_loc_oprnd2.val_class)
1656 case dw_val_class_vec:
1657 size += loc->dw_loc_oprnd2.v.val_vec.length
1658 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1659 break;
1660 case dw_val_class_const:
1661 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1662 break;
1663 case dw_val_class_const_double:
1664 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1665 break;
1666 case dw_val_class_wide_int:
1667 size += (get_full_len (*loc->dw_loc_oprnd2.v.val_wide)
1668 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
1669 break;
1670 default:
1671 gcc_unreachable ();
1673 break;
1675 case DW_OP_GNU_regval_type:
1677 unsigned long o
1678 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1679 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1680 + size_of_uleb128 (o);
1682 break;
1683 case DW_OP_GNU_deref_type:
1685 unsigned long o
1686 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1687 size += 1 + size_of_uleb128 (o);
1689 break;
1690 case DW_OP_GNU_convert:
1691 case DW_OP_GNU_reinterpret:
1692 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1693 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1694 else
1696 unsigned long o
1697 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1698 size += size_of_uleb128 (o);
1700 break;
1701 case DW_OP_GNU_parameter_ref:
1702 size += 4;
1703 break;
1704 default:
1705 break;
1708 return size;
1711 /* Return the size of a series of location descriptors. */
1713 unsigned long
1714 size_of_locs (dw_loc_descr_ref loc)
1716 dw_loc_descr_ref l;
1717 unsigned long size;
1719 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1720 field, to avoid writing to a PCH file. */
1721 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1723 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1724 break;
1725 size += size_of_loc_descr (l);
1727 if (! l)
1728 return size;
1730 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1732 l->dw_loc_addr = size;
1733 size += size_of_loc_descr (l);
1736 return size;
1739 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1740 static void get_ref_die_offset_label (char *, dw_die_ref);
1741 static unsigned long int get_ref_die_offset (dw_die_ref);
1743 /* Output location description stack opcode's operands (if any).
1744 The for_eh_or_skip parameter controls whether register numbers are
1745 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1746 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1747 info). This should be suppressed for the cases that have not been converted
1748 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
1750 static void
1751 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1753 dw_val_ref val1 = &loc->dw_loc_oprnd1;
1754 dw_val_ref val2 = &loc->dw_loc_oprnd2;
1756 switch (loc->dw_loc_opc)
1758 #ifdef DWARF2_DEBUGGING_INFO
1759 case DW_OP_const2u:
1760 case DW_OP_const2s:
1761 dw2_asm_output_data (2, val1->v.val_int, NULL);
1762 break;
1763 case DW_OP_const4u:
1764 if (loc->dtprel)
1766 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1767 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1768 val1->v.val_addr);
1769 fputc ('\n', asm_out_file);
1770 break;
1772 /* FALLTHRU */
1773 case DW_OP_const4s:
1774 dw2_asm_output_data (4, val1->v.val_int, NULL);
1775 break;
1776 case DW_OP_const8u:
1777 if (loc->dtprel)
1779 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1780 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1781 val1->v.val_addr);
1782 fputc ('\n', asm_out_file);
1783 break;
1785 /* FALLTHRU */
1786 case DW_OP_const8s:
1787 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
1788 dw2_asm_output_data (8, val1->v.val_int, NULL);
1789 break;
1790 case DW_OP_skip:
1791 case DW_OP_bra:
1793 int offset;
1795 gcc_assert (val1->val_class == dw_val_class_loc);
1796 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
1798 dw2_asm_output_data (2, offset, NULL);
1800 break;
1801 case DW_OP_implicit_value:
1802 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1803 switch (val2->val_class)
1805 case dw_val_class_const:
1806 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
1807 break;
1808 case dw_val_class_vec:
1810 unsigned int elt_size = val2->v.val_vec.elt_size;
1811 unsigned int len = val2->v.val_vec.length;
1812 unsigned int i;
1813 unsigned char *p;
1815 if (elt_size > sizeof (HOST_WIDE_INT))
1817 elt_size /= 2;
1818 len *= 2;
1820 for (i = 0, p = val2->v.val_vec.array;
1821 i < len;
1822 i++, p += elt_size)
1823 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
1824 "fp or vector constant word %u", i);
1826 break;
1827 case dw_val_class_const_double:
1829 unsigned HOST_WIDE_INT first, second;
1831 if (WORDS_BIG_ENDIAN)
1833 first = val2->v.val_double.high;
1834 second = val2->v.val_double.low;
1836 else
1838 first = val2->v.val_double.low;
1839 second = val2->v.val_double.high;
1841 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1842 first, NULL);
1843 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1844 second, NULL);
1846 break;
1847 case dw_val_class_wide_int:
1849 int i;
1850 int len = get_full_len (*val2->v.val_wide);
1851 if (WORDS_BIG_ENDIAN)
1852 for (i = len - 1; i >= 0; --i)
1853 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1854 val2->v.val_wide->elt (i), NULL);
1855 else
1856 for (i = 0; i < len; ++i)
1857 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1858 val2->v.val_wide->elt (i), NULL);
1860 break;
1861 case dw_val_class_addr:
1862 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
1863 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
1864 break;
1865 default:
1866 gcc_unreachable ();
1868 break;
1869 #else
1870 case DW_OP_const2u:
1871 case DW_OP_const2s:
1872 case DW_OP_const4u:
1873 case DW_OP_const4s:
1874 case DW_OP_const8u:
1875 case DW_OP_const8s:
1876 case DW_OP_skip:
1877 case DW_OP_bra:
1878 case DW_OP_implicit_value:
1879 /* We currently don't make any attempt to make sure these are
1880 aligned properly like we do for the main unwind info, so
1881 don't support emitting things larger than a byte if we're
1882 only doing unwinding. */
1883 gcc_unreachable ();
1884 #endif
1885 case DW_OP_const1u:
1886 case DW_OP_const1s:
1887 dw2_asm_output_data (1, val1->v.val_int, NULL);
1888 break;
1889 case DW_OP_constu:
1890 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1891 break;
1892 case DW_OP_consts:
1893 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1894 break;
1895 case DW_OP_pick:
1896 dw2_asm_output_data (1, val1->v.val_int, NULL);
1897 break;
1898 case DW_OP_plus_uconst:
1899 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1900 break;
1901 case DW_OP_breg0:
1902 case DW_OP_breg1:
1903 case DW_OP_breg2:
1904 case DW_OP_breg3:
1905 case DW_OP_breg4:
1906 case DW_OP_breg5:
1907 case DW_OP_breg6:
1908 case DW_OP_breg7:
1909 case DW_OP_breg8:
1910 case DW_OP_breg9:
1911 case DW_OP_breg10:
1912 case DW_OP_breg11:
1913 case DW_OP_breg12:
1914 case DW_OP_breg13:
1915 case DW_OP_breg14:
1916 case DW_OP_breg15:
1917 case DW_OP_breg16:
1918 case DW_OP_breg17:
1919 case DW_OP_breg18:
1920 case DW_OP_breg19:
1921 case DW_OP_breg20:
1922 case DW_OP_breg21:
1923 case DW_OP_breg22:
1924 case DW_OP_breg23:
1925 case DW_OP_breg24:
1926 case DW_OP_breg25:
1927 case DW_OP_breg26:
1928 case DW_OP_breg27:
1929 case DW_OP_breg28:
1930 case DW_OP_breg29:
1931 case DW_OP_breg30:
1932 case DW_OP_breg31:
1933 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1934 break;
1935 case DW_OP_regx:
1937 unsigned r = val1->v.val_unsigned;
1938 if (for_eh_or_skip >= 0)
1939 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1940 gcc_assert (size_of_uleb128 (r)
1941 == size_of_uleb128 (val1->v.val_unsigned));
1942 dw2_asm_output_data_uleb128 (r, NULL);
1944 break;
1945 case DW_OP_fbreg:
1946 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1947 break;
1948 case DW_OP_bregx:
1950 unsigned r = val1->v.val_unsigned;
1951 if (for_eh_or_skip >= 0)
1952 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1953 gcc_assert (size_of_uleb128 (r)
1954 == size_of_uleb128 (val1->v.val_unsigned));
1955 dw2_asm_output_data_uleb128 (r, NULL);
1956 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
1958 break;
1959 case DW_OP_piece:
1960 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1961 break;
1962 case DW_OP_bit_piece:
1963 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1964 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
1965 break;
1966 case DW_OP_deref_size:
1967 case DW_OP_xderef_size:
1968 dw2_asm_output_data (1, val1->v.val_int, NULL);
1969 break;
1971 case DW_OP_addr:
1972 if (loc->dtprel)
1974 if (targetm.asm_out.output_dwarf_dtprel)
1976 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
1977 DWARF2_ADDR_SIZE,
1978 val1->v.val_addr);
1979 fputc ('\n', asm_out_file);
1981 else
1982 gcc_unreachable ();
1984 else
1986 #ifdef DWARF2_DEBUGGING_INFO
1987 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
1988 #else
1989 gcc_unreachable ();
1990 #endif
1992 break;
1994 case DW_OP_GNU_addr_index:
1995 case DW_OP_GNU_const_index:
1996 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1997 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
1998 "(index into .debug_addr)");
1999 break;
2001 case DW_OP_GNU_implicit_pointer:
2003 char label[MAX_ARTIFICIAL_LABEL_BYTES
2004 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2005 gcc_assert (val1->val_class == dw_val_class_die_ref);
2006 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2007 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2008 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2010 break;
2012 case DW_OP_GNU_entry_value:
2013 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2014 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2015 break;
2017 case DW_OP_GNU_const_type:
2019 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2020 gcc_assert (o);
2021 dw2_asm_output_data_uleb128 (o, NULL);
2022 switch (val2->val_class)
2024 case dw_val_class_const:
2025 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2026 dw2_asm_output_data (1, l, NULL);
2027 dw2_asm_output_data (l, val2->v.val_int, NULL);
2028 break;
2029 case dw_val_class_vec:
2031 unsigned int elt_size = val2->v.val_vec.elt_size;
2032 unsigned int len = val2->v.val_vec.length;
2033 unsigned int i;
2034 unsigned char *p;
2036 l = len * elt_size;
2037 dw2_asm_output_data (1, l, NULL);
2038 if (elt_size > sizeof (HOST_WIDE_INT))
2040 elt_size /= 2;
2041 len *= 2;
2043 for (i = 0, p = val2->v.val_vec.array;
2044 i < len;
2045 i++, p += elt_size)
2046 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2047 "fp or vector constant word %u", i);
2049 break;
2050 case dw_val_class_const_double:
2052 unsigned HOST_WIDE_INT first, second;
2053 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2055 dw2_asm_output_data (1, 2 * l, NULL);
2056 if (WORDS_BIG_ENDIAN)
2058 first = val2->v.val_double.high;
2059 second = val2->v.val_double.low;
2061 else
2063 first = val2->v.val_double.low;
2064 second = val2->v.val_double.high;
2066 dw2_asm_output_data (l, first, NULL);
2067 dw2_asm_output_data (l, second, NULL);
2069 break;
2070 case dw_val_class_wide_int:
2072 int i;
2073 int len = get_full_len (*val2->v.val_wide);
2074 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2076 dw2_asm_output_data (1, len * l, NULL);
2077 if (WORDS_BIG_ENDIAN)
2078 for (i = len - 1; i >= 0; --i)
2079 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2080 else
2081 for (i = 0; i < len; ++i)
2082 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2084 break;
2085 default:
2086 gcc_unreachable ();
2089 break;
2090 case DW_OP_GNU_regval_type:
2092 unsigned r = val1->v.val_unsigned;
2093 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2094 gcc_assert (o);
2095 if (for_eh_or_skip >= 0)
2097 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2098 gcc_assert (size_of_uleb128 (r)
2099 == size_of_uleb128 (val1->v.val_unsigned));
2101 dw2_asm_output_data_uleb128 (r, NULL);
2102 dw2_asm_output_data_uleb128 (o, NULL);
2104 break;
2105 case DW_OP_GNU_deref_type:
2107 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2108 gcc_assert (o);
2109 dw2_asm_output_data (1, val1->v.val_int, NULL);
2110 dw2_asm_output_data_uleb128 (o, NULL);
2112 break;
2113 case DW_OP_GNU_convert:
2114 case DW_OP_GNU_reinterpret:
2115 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2116 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2117 else
2119 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2120 gcc_assert (o);
2121 dw2_asm_output_data_uleb128 (o, NULL);
2123 break;
2125 case DW_OP_GNU_parameter_ref:
2127 unsigned long o;
2128 gcc_assert (val1->val_class == dw_val_class_die_ref);
2129 o = get_ref_die_offset (val1->v.val_die_ref.die);
2130 dw2_asm_output_data (4, o, NULL);
2132 break;
2134 default:
2135 /* Other codes have no operands. */
2136 break;
2140 /* Output a sequence of location operations.
2141 The for_eh_or_skip parameter controls whether register numbers are
2142 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2143 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2144 info). This should be suppressed for the cases that have not been converted
2145 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2147 void
2148 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2150 for (; loc != NULL; loc = loc->dw_loc_next)
2152 enum dwarf_location_atom opc = loc->dw_loc_opc;
2153 /* Output the opcode. */
2154 if (for_eh_or_skip >= 0
2155 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2157 unsigned r = (opc - DW_OP_breg0);
2158 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2159 gcc_assert (r <= 31);
2160 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2162 else if (for_eh_or_skip >= 0
2163 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2165 unsigned r = (opc - DW_OP_reg0);
2166 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2167 gcc_assert (r <= 31);
2168 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2171 dw2_asm_output_data (1, opc,
2172 "%s", dwarf_stack_op_name (opc));
2174 /* Output the operand(s) (if any). */
2175 output_loc_operands (loc, for_eh_or_skip);
2179 /* Output location description stack opcode's operands (if any).
2180 The output is single bytes on a line, suitable for .cfi_escape. */
2182 static void
2183 output_loc_operands_raw (dw_loc_descr_ref loc)
2185 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2186 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2188 switch (loc->dw_loc_opc)
2190 case DW_OP_addr:
2191 case DW_OP_GNU_addr_index:
2192 case DW_OP_GNU_const_index:
2193 case DW_OP_implicit_value:
2194 /* We cannot output addresses in .cfi_escape, only bytes. */
2195 gcc_unreachable ();
2197 case DW_OP_const1u:
2198 case DW_OP_const1s:
2199 case DW_OP_pick:
2200 case DW_OP_deref_size:
2201 case DW_OP_xderef_size:
2202 fputc (',', asm_out_file);
2203 dw2_asm_output_data_raw (1, val1->v.val_int);
2204 break;
2206 case DW_OP_const2u:
2207 case DW_OP_const2s:
2208 fputc (',', asm_out_file);
2209 dw2_asm_output_data_raw (2, val1->v.val_int);
2210 break;
2212 case DW_OP_const4u:
2213 case DW_OP_const4s:
2214 fputc (',', asm_out_file);
2215 dw2_asm_output_data_raw (4, val1->v.val_int);
2216 break;
2218 case DW_OP_const8u:
2219 case DW_OP_const8s:
2220 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2221 fputc (',', asm_out_file);
2222 dw2_asm_output_data_raw (8, val1->v.val_int);
2223 break;
2225 case DW_OP_skip:
2226 case DW_OP_bra:
2228 int offset;
2230 gcc_assert (val1->val_class == dw_val_class_loc);
2231 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2233 fputc (',', asm_out_file);
2234 dw2_asm_output_data_raw (2, offset);
2236 break;
2238 case DW_OP_regx:
2240 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2241 gcc_assert (size_of_uleb128 (r)
2242 == size_of_uleb128 (val1->v.val_unsigned));
2243 fputc (',', asm_out_file);
2244 dw2_asm_output_data_uleb128_raw (r);
2246 break;
2248 case DW_OP_constu:
2249 case DW_OP_plus_uconst:
2250 case DW_OP_piece:
2251 fputc (',', asm_out_file);
2252 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2253 break;
2255 case DW_OP_bit_piece:
2256 fputc (',', asm_out_file);
2257 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2258 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2259 break;
2261 case DW_OP_consts:
2262 case DW_OP_breg0:
2263 case DW_OP_breg1:
2264 case DW_OP_breg2:
2265 case DW_OP_breg3:
2266 case DW_OP_breg4:
2267 case DW_OP_breg5:
2268 case DW_OP_breg6:
2269 case DW_OP_breg7:
2270 case DW_OP_breg8:
2271 case DW_OP_breg9:
2272 case DW_OP_breg10:
2273 case DW_OP_breg11:
2274 case DW_OP_breg12:
2275 case DW_OP_breg13:
2276 case DW_OP_breg14:
2277 case DW_OP_breg15:
2278 case DW_OP_breg16:
2279 case DW_OP_breg17:
2280 case DW_OP_breg18:
2281 case DW_OP_breg19:
2282 case DW_OP_breg20:
2283 case DW_OP_breg21:
2284 case DW_OP_breg22:
2285 case DW_OP_breg23:
2286 case DW_OP_breg24:
2287 case DW_OP_breg25:
2288 case DW_OP_breg26:
2289 case DW_OP_breg27:
2290 case DW_OP_breg28:
2291 case DW_OP_breg29:
2292 case DW_OP_breg30:
2293 case DW_OP_breg31:
2294 case DW_OP_fbreg:
2295 fputc (',', asm_out_file);
2296 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2297 break;
2299 case DW_OP_bregx:
2301 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2302 gcc_assert (size_of_uleb128 (r)
2303 == size_of_uleb128 (val1->v.val_unsigned));
2304 fputc (',', asm_out_file);
2305 dw2_asm_output_data_uleb128_raw (r);
2306 fputc (',', asm_out_file);
2307 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2309 break;
2311 case DW_OP_GNU_implicit_pointer:
2312 case DW_OP_GNU_entry_value:
2313 case DW_OP_GNU_const_type:
2314 case DW_OP_GNU_regval_type:
2315 case DW_OP_GNU_deref_type:
2316 case DW_OP_GNU_convert:
2317 case DW_OP_GNU_reinterpret:
2318 case DW_OP_GNU_parameter_ref:
2319 gcc_unreachable ();
2320 break;
2322 default:
2323 /* Other codes have no operands. */
2324 break;
2328 void
2329 output_loc_sequence_raw (dw_loc_descr_ref loc)
2331 while (1)
2333 enum dwarf_location_atom opc = loc->dw_loc_opc;
2334 /* Output the opcode. */
2335 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2337 unsigned r = (opc - DW_OP_breg0);
2338 r = DWARF2_FRAME_REG_OUT (r, 1);
2339 gcc_assert (r <= 31);
2340 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2342 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2344 unsigned r = (opc - DW_OP_reg0);
2345 r = DWARF2_FRAME_REG_OUT (r, 1);
2346 gcc_assert (r <= 31);
2347 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2349 /* Output the opcode. */
2350 fprintf (asm_out_file, "%#x", opc);
2351 output_loc_operands_raw (loc);
2353 if (!loc->dw_loc_next)
2354 break;
2355 loc = loc->dw_loc_next;
2357 fputc (',', asm_out_file);
2361 /* This function builds a dwarf location descriptor sequence from a
2362 dw_cfa_location, adding the given OFFSET to the result of the
2363 expression. */
2365 struct dw_loc_descr_node *
2366 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2368 struct dw_loc_descr_node *head, *tmp;
2370 offset += cfa->offset;
2372 if (cfa->indirect)
2374 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2375 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2376 head->dw_loc_oprnd1.val_entry = NULL;
2377 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2378 add_loc_descr (&head, tmp);
2379 if (offset != 0)
2381 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2382 add_loc_descr (&head, tmp);
2385 else
2386 head = new_reg_loc_descr (cfa->reg, offset);
2388 return head;
2391 /* This function builds a dwarf location descriptor sequence for
2392 the address at OFFSET from the CFA when stack is aligned to
2393 ALIGNMENT byte. */
2395 struct dw_loc_descr_node *
2396 build_cfa_aligned_loc (dw_cfa_location *cfa,
2397 HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2399 struct dw_loc_descr_node *head;
2400 unsigned int dwarf_fp
2401 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2403 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2404 if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2406 head = new_reg_loc_descr (dwarf_fp, 0);
2407 add_loc_descr (&head, int_loc_descriptor (alignment));
2408 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2409 loc_descr_plus_const (&head, offset);
2411 else
2412 head = new_reg_loc_descr (dwarf_fp, offset);
2413 return head;
2416 /* And now, the support for symbolic debugging information. */
2418 /* .debug_str support. */
2420 static void dwarf2out_init (const char *);
2421 static void dwarf2out_finish (const char *);
2422 static void dwarf2out_early_finish (void);
2423 static void dwarf2out_assembly_start (void);
2424 static void dwarf2out_define (unsigned int, const char *);
2425 static void dwarf2out_undef (unsigned int, const char *);
2426 static void dwarf2out_start_source_file (unsigned, const char *);
2427 static void dwarf2out_end_source_file (unsigned);
2428 static void dwarf2out_function_decl (tree);
2429 static void dwarf2out_begin_block (unsigned, unsigned);
2430 static void dwarf2out_end_block (unsigned, unsigned);
2431 static bool dwarf2out_ignore_block (const_tree);
2432 static void dwarf2out_early_global_decl (tree);
2433 static void dwarf2out_late_global_decl (tree);
2434 static void dwarf2out_type_decl (tree, int);
2435 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
2436 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2437 dw_die_ref);
2438 static void dwarf2out_abstract_function (tree);
2439 static void dwarf2out_var_location (rtx_insn *);
2440 static void dwarf2out_begin_function (tree);
2441 static void dwarf2out_end_function (unsigned int);
2442 static void dwarf2out_register_main_translation_unit (tree unit);
2443 static void dwarf2out_set_name (tree, tree);
2445 /* The debug hooks structure. */
2447 const struct gcc_debug_hooks dwarf2_debug_hooks =
2449 dwarf2out_init,
2450 dwarf2out_finish,
2451 dwarf2out_early_finish,
2452 dwarf2out_assembly_start,
2453 dwarf2out_define,
2454 dwarf2out_undef,
2455 dwarf2out_start_source_file,
2456 dwarf2out_end_source_file,
2457 dwarf2out_begin_block,
2458 dwarf2out_end_block,
2459 dwarf2out_ignore_block,
2460 dwarf2out_source_line,
2461 dwarf2out_begin_prologue,
2462 #if VMS_DEBUGGING_INFO
2463 dwarf2out_vms_end_prologue,
2464 dwarf2out_vms_begin_epilogue,
2465 #else
2466 debug_nothing_int_charstar,
2467 debug_nothing_int_charstar,
2468 #endif
2469 dwarf2out_end_epilogue,
2470 dwarf2out_begin_function,
2471 dwarf2out_end_function, /* end_function */
2472 dwarf2out_register_main_translation_unit,
2473 dwarf2out_function_decl, /* function_decl */
2474 dwarf2out_early_global_decl,
2475 dwarf2out_late_global_decl,
2476 dwarf2out_type_decl, /* type_decl */
2477 dwarf2out_imported_module_or_decl,
2478 debug_nothing_tree, /* deferred_inline_function */
2479 /* The DWARF 2 backend tries to reduce debugging bloat by not
2480 emitting the abstract description of inline functions until
2481 something tries to reference them. */
2482 dwarf2out_abstract_function, /* outlining_inline_function */
2483 debug_nothing_rtx_code_label, /* label */
2484 debug_nothing_int, /* handle_pch */
2485 dwarf2out_var_location,
2486 dwarf2out_switch_text_section,
2487 dwarf2out_set_name,
2488 1, /* start_end_main_source_file */
2489 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2492 const struct gcc_debug_hooks dwarf2_lineno_debug_hooks =
2494 dwarf2out_init,
2495 debug_nothing_charstar,
2496 debug_nothing_void,
2497 debug_nothing_void,
2498 debug_nothing_int_charstar,
2499 debug_nothing_int_charstar,
2500 debug_nothing_int_charstar,
2501 debug_nothing_int,
2502 debug_nothing_int_int, /* begin_block */
2503 debug_nothing_int_int, /* end_block */
2504 debug_true_const_tree, /* ignore_block */
2505 dwarf2out_source_line, /* source_line */
2506 debug_nothing_int_charstar, /* begin_prologue */
2507 debug_nothing_int_charstar, /* end_prologue */
2508 debug_nothing_int_charstar, /* begin_epilogue */
2509 debug_nothing_int_charstar, /* end_epilogue */
2510 debug_nothing_tree, /* begin_function */
2511 debug_nothing_int, /* end_function */
2512 debug_nothing_tree, /* register_main_translation_unit */
2513 debug_nothing_tree, /* function_decl */
2514 debug_nothing_tree, /* early_global_decl */
2515 debug_nothing_tree, /* late_global_decl */
2516 debug_nothing_tree_int, /* type_decl */
2517 debug_nothing_tree_tree_tree_bool, /* imported_module_or_decl */
2518 debug_nothing_tree, /* deferred_inline_function */
2519 debug_nothing_tree, /* outlining_inline_function */
2520 debug_nothing_rtx_code_label, /* label */
2521 debug_nothing_int, /* handle_pch */
2522 debug_nothing_rtx_insn, /* var_location */
2523 debug_nothing_void, /* switch_text_section */
2524 debug_nothing_tree_tree, /* set_name */
2525 0, /* start_end_main_source_file */
2526 TYPE_SYMTAB_IS_ADDRESS /* tree_type_symtab_field */
2529 /* NOTE: In the comments in this file, many references are made to
2530 "Debugging Information Entries". This term is abbreviated as `DIE'
2531 throughout the remainder of this file. */
2533 /* An internal representation of the DWARF output is built, and then
2534 walked to generate the DWARF debugging info. The walk of the internal
2535 representation is done after the entire program has been compiled.
2536 The types below are used to describe the internal representation. */
2538 /* Whether to put type DIEs into their own section .debug_types instead
2539 of making them part of the .debug_info section. Only supported for
2540 Dwarf V4 or higher and the user didn't disable them through
2541 -fno-debug-types-section. It is more efficient to put them in a
2542 separate comdat sections since the linker will then be able to
2543 remove duplicates. But not all tools support .debug_types sections
2544 yet. */
2546 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2548 /* Various DIE's use offsets relative to the beginning of the
2549 .debug_info section to refer to each other. */
2551 typedef long int dw_offset;
2553 /* Define typedefs here to avoid circular dependencies. */
2555 typedef struct dw_attr_struct *dw_attr_ref;
2556 typedef struct dw_line_info_struct *dw_line_info_ref;
2557 typedef struct pubname_struct *pubname_ref;
2558 typedef struct dw_ranges_struct *dw_ranges_ref;
2559 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
2560 typedef struct comdat_type_struct *comdat_type_node_ref;
2562 /* The entries in the line_info table more-or-less mirror the opcodes
2563 that are used in the real dwarf line table. Arrays of these entries
2564 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2565 supported. */
2567 enum dw_line_info_opcode {
2568 /* Emit DW_LNE_set_address; the operand is the label index. */
2569 LI_set_address,
2571 /* Emit a row to the matrix with the given line. This may be done
2572 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2573 special opcodes. */
2574 LI_set_line,
2576 /* Emit a DW_LNS_set_file. */
2577 LI_set_file,
2579 /* Emit a DW_LNS_set_column. */
2580 LI_set_column,
2582 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
2583 LI_negate_stmt,
2585 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
2586 LI_set_prologue_end,
2587 LI_set_epilogue_begin,
2589 /* Emit a DW_LNE_set_discriminator. */
2590 LI_set_discriminator
2593 typedef struct GTY(()) dw_line_info_struct {
2594 enum dw_line_info_opcode opcode;
2595 unsigned int val;
2596 } dw_line_info_entry;
2599 typedef struct GTY(()) dw_line_info_table_struct {
2600 /* The label that marks the end of this section. */
2601 const char *end_label;
2603 /* The values for the last row of the matrix, as collected in the table.
2604 These are used to minimize the changes to the next row. */
2605 unsigned int file_num;
2606 unsigned int line_num;
2607 unsigned int column_num;
2608 int discrim_num;
2609 bool is_stmt;
2610 bool in_use;
2612 vec<dw_line_info_entry, va_gc> *entries;
2613 } dw_line_info_table;
2615 typedef dw_line_info_table *dw_line_info_table_p;
2618 /* Each DIE attribute has a field specifying the attribute kind,
2619 a link to the next attribute in the chain, and an attribute value.
2620 Attributes are typically linked below the DIE they modify. */
2622 typedef struct GTY(()) dw_attr_struct {
2623 enum dwarf_attribute dw_attr;
2624 dw_val_node dw_attr_val;
2626 dw_attr_node;
2629 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
2630 The children of each node form a circular list linked by
2631 die_sib. die_child points to the node *before* the "first" child node. */
2633 typedef struct GTY((chain_circular ("%h.die_sib"), for_user)) die_struct {
2634 union die_symbol_or_type_node
2636 const char * GTY ((tag ("0"))) die_symbol;
2637 comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
2639 GTY ((desc ("%0.comdat_type_p"))) die_id;
2640 vec<dw_attr_node, va_gc> *die_attr;
2641 dw_die_ref die_parent;
2642 dw_die_ref die_child;
2643 dw_die_ref die_sib;
2644 dw_die_ref die_definition; /* ref from a specification to its definition */
2645 dw_offset die_offset;
2646 unsigned long die_abbrev;
2647 int die_mark;
2648 unsigned int decl_id;
2649 enum dwarf_tag die_tag;
2650 /* Die is used and must not be pruned as unused. */
2651 BOOL_BITFIELD die_perennial_p : 1;
2652 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2653 /* Lots of spare bits. */
2655 die_node;
2657 /* Set to TRUE while dwarf2out_early_global_decl is running. */
2658 static bool early_dwarf;
2659 struct set_early_dwarf {
2660 bool saved;
2661 set_early_dwarf () : saved(early_dwarf) { early_dwarf = true; }
2662 ~set_early_dwarf () { early_dwarf = saved; }
2665 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
2666 #define FOR_EACH_CHILD(die, c, expr) do { \
2667 c = die->die_child; \
2668 if (c) do { \
2669 c = c->die_sib; \
2670 expr; \
2671 } while (c != die->die_child); \
2672 } while (0)
2674 /* The pubname structure */
2676 typedef struct GTY(()) pubname_struct {
2677 dw_die_ref die;
2678 const char *name;
2680 pubname_entry;
2683 struct GTY(()) dw_ranges_struct {
2684 /* If this is positive, it's a block number, otherwise it's a
2685 bitwise-negated index into dw_ranges_by_label. */
2686 int num;
2689 /* A structure to hold a macinfo entry. */
2691 typedef struct GTY(()) macinfo_struct {
2692 unsigned char code;
2693 unsigned HOST_WIDE_INT lineno;
2694 const char *info;
2696 macinfo_entry;
2699 struct GTY(()) dw_ranges_by_label_struct {
2700 const char *begin;
2701 const char *end;
2704 /* The comdat type node structure. */
2705 typedef struct GTY(()) comdat_type_struct
2707 dw_die_ref root_die;
2708 dw_die_ref type_die;
2709 dw_die_ref skeleton_die;
2710 char signature[DWARF_TYPE_SIGNATURE_SIZE];
2711 struct comdat_type_struct *next;
2713 comdat_type_node;
2715 /* A list of DIEs for which we can't determine ancestry (parent_die
2716 field) just yet. Later in dwarf2out_finish we will fill in the
2717 missing bits. */
2718 typedef struct GTY(()) limbo_die_struct {
2719 dw_die_ref die;
2720 /* The tree for which this DIE was created. We use this to
2721 determine ancestry later. */
2722 tree created_for;
2723 struct limbo_die_struct *next;
2725 limbo_die_node;
2727 typedef struct skeleton_chain_struct
2729 dw_die_ref old_die;
2730 dw_die_ref new_die;
2731 struct skeleton_chain_struct *parent;
2733 skeleton_chain_node;
2735 /* Define a macro which returns nonzero for a TYPE_DECL which was
2736 implicitly generated for a type.
2738 Note that, unlike the C front-end (which generates a NULL named
2739 TYPE_DECL node for each complete tagged type, each array type,
2740 and each function type node created) the C++ front-end generates
2741 a _named_ TYPE_DECL node for each tagged type node created.
2742 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
2743 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
2744 front-end, but for each type, tagged or not. */
2746 #define TYPE_DECL_IS_STUB(decl) \
2747 (DECL_NAME (decl) == NULL_TREE \
2748 || (DECL_ARTIFICIAL (decl) \
2749 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
2750 /* This is necessary for stub decls that \
2751 appear in nested inline functions. */ \
2752 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
2753 && (decl_ultimate_origin (decl) \
2754 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
2756 /* Information concerning the compilation unit's programming
2757 language, and compiler version. */
2759 /* Fixed size portion of the DWARF compilation unit header. */
2760 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
2761 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
2763 /* Fixed size portion of the DWARF comdat type unit header. */
2764 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
2765 (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
2766 + DWARF_OFFSET_SIZE)
2768 /* Fixed size portion of public names info. */
2769 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
2771 /* Fixed size portion of the address range info. */
2772 #define DWARF_ARANGES_HEADER_SIZE \
2773 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2774 DWARF2_ADDR_SIZE * 2) \
2775 - DWARF_INITIAL_LENGTH_SIZE)
2777 /* Size of padding portion in the address range info. It must be
2778 aligned to twice the pointer size. */
2779 #define DWARF_ARANGES_PAD_SIZE \
2780 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2781 DWARF2_ADDR_SIZE * 2) \
2782 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
2784 /* Use assembler line directives if available. */
2785 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
2786 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
2787 #define DWARF2_ASM_LINE_DEBUG_INFO 1
2788 #else
2789 #define DWARF2_ASM_LINE_DEBUG_INFO 0
2790 #endif
2791 #endif
2793 /* Minimum line offset in a special line info. opcode.
2794 This value was chosen to give a reasonable range of values. */
2795 #define DWARF_LINE_BASE -10
2797 /* First special line opcode - leave room for the standard opcodes. */
2798 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
2800 /* Range of line offsets in a special line info. opcode. */
2801 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
2803 /* Flag that indicates the initial value of the is_stmt_start flag.
2804 In the present implementation, we do not mark any lines as
2805 the beginning of a source statement, because that information
2806 is not made available by the GCC front-end. */
2807 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
2809 /* Maximum number of operations per instruction bundle. */
2810 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
2811 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
2812 #endif
2814 /* This location is used by calc_die_sizes() to keep track
2815 the offset of each DIE within the .debug_info section. */
2816 static unsigned long next_die_offset;
2818 /* Record the root of the DIE's built for the current compilation unit. */
2819 static GTY(()) dw_die_ref single_comp_unit_die;
2821 /* A list of type DIEs that have been separated into comdat sections. */
2822 static GTY(()) comdat_type_node *comdat_type_list;
2824 /* A list of DIEs with a NULL parent waiting to be relocated. */
2825 static GTY(()) limbo_die_node *limbo_die_list;
2827 /* A list of DIEs for which we may have to generate
2828 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
2829 static GTY(()) limbo_die_node *deferred_asm_name;
2831 struct dwarf_file_hasher : ggc_ptr_hash<dwarf_file_data>
2833 typedef const char *compare_type;
2835 static hashval_t hash (dwarf_file_data *);
2836 static bool equal (dwarf_file_data *, const char *);
2839 /* Filenames referenced by this compilation unit. */
2840 static GTY(()) hash_table<dwarf_file_hasher> *file_table;
2842 struct decl_die_hasher : ggc_ptr_hash<die_node>
2844 typedef tree compare_type;
2846 static hashval_t hash (die_node *);
2847 static bool equal (die_node *, tree);
2849 /* A hash table of references to DIE's that describe declarations.
2850 The key is a DECL_UID() which is a unique number identifying each decl. */
2851 static GTY (()) hash_table<decl_die_hasher> *decl_die_table;
2853 struct block_die_hasher : ggc_ptr_hash<die_struct>
2855 static hashval_t hash (die_struct *);
2856 static bool equal (die_struct *, die_struct *);
2859 /* A hash table of references to DIE's that describe COMMON blocks.
2860 The key is DECL_UID() ^ die_parent. */
2861 static GTY (()) hash_table<block_die_hasher> *common_block_die_table;
2863 typedef struct GTY(()) die_arg_entry_struct {
2864 dw_die_ref die;
2865 tree arg;
2866 } die_arg_entry;
2869 /* Node of the variable location list. */
2870 struct GTY ((chain_next ("%h.next"))) var_loc_node {
2871 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
2872 EXPR_LIST chain. For small bitsizes, bitsize is encoded
2873 in mode of the EXPR_LIST node and first EXPR_LIST operand
2874 is either NOTE_INSN_VAR_LOCATION for a piece with a known
2875 location or NULL for padding. For larger bitsizes,
2876 mode is 0 and first operand is a CONCAT with bitsize
2877 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
2878 NULL as second operand. */
2879 rtx GTY (()) loc;
2880 const char * GTY (()) label;
2881 struct var_loc_node * GTY (()) next;
2884 /* Variable location list. */
2885 struct GTY ((for_user)) var_loc_list_def {
2886 struct var_loc_node * GTY (()) first;
2888 /* Pointer to the last but one or last element of the
2889 chained list. If the list is empty, both first and
2890 last are NULL, if the list contains just one node
2891 or the last node certainly is not redundant, it points
2892 to the last node, otherwise points to the last but one.
2893 Do not mark it for GC because it is marked through the chain. */
2894 struct var_loc_node * GTY ((skip ("%h"))) last;
2896 /* Pointer to the last element before section switch,
2897 if NULL, either sections weren't switched or first
2898 is after section switch. */
2899 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
2901 /* DECL_UID of the variable decl. */
2902 unsigned int decl_id;
2904 typedef struct var_loc_list_def var_loc_list;
2906 /* Call argument location list. */
2907 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
2908 rtx GTY (()) call_arg_loc_note;
2909 const char * GTY (()) label;
2910 tree GTY (()) block;
2911 bool tail_call_p;
2912 rtx GTY (()) symbol_ref;
2913 struct call_arg_loc_node * GTY (()) next;
2917 struct decl_loc_hasher : ggc_ptr_hash<var_loc_list>
2919 typedef const_tree compare_type;
2921 static hashval_t hash (var_loc_list *);
2922 static bool equal (var_loc_list *, const_tree);
2925 /* Table of decl location linked lists. */
2926 static GTY (()) hash_table<decl_loc_hasher> *decl_loc_table;
2928 /* Head and tail of call_arg_loc chain. */
2929 static GTY (()) struct call_arg_loc_node *call_arg_locations;
2930 static struct call_arg_loc_node *call_arg_loc_last;
2932 /* Number of call sites in the current function. */
2933 static int call_site_count = -1;
2934 /* Number of tail call sites in the current function. */
2935 static int tail_call_site_count = -1;
2937 /* A cached location list. */
2938 struct GTY ((for_user)) cached_dw_loc_list_def {
2939 /* The DECL_UID of the decl that this entry describes. */
2940 unsigned int decl_id;
2942 /* The cached location list. */
2943 dw_loc_list_ref loc_list;
2945 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
2947 struct dw_loc_list_hasher : ggc_ptr_hash<cached_dw_loc_list>
2950 typedef const_tree compare_type;
2952 static hashval_t hash (cached_dw_loc_list *);
2953 static bool equal (cached_dw_loc_list *, const_tree);
2956 /* Table of cached location lists. */
2957 static GTY (()) hash_table<dw_loc_list_hasher> *cached_dw_loc_list_table;
2959 /* A pointer to the base of a list of references to DIE's that
2960 are uniquely identified by their tag, presence/absence of
2961 children DIE's, and list of attribute/value pairs. */
2962 static GTY((length ("abbrev_die_table_allocated")))
2963 dw_die_ref *abbrev_die_table;
2965 /* Number of elements currently allocated for abbrev_die_table. */
2966 static GTY(()) unsigned abbrev_die_table_allocated;
2968 /* Number of elements in abbrev_die_table currently in use. */
2969 static GTY(()) unsigned abbrev_die_table_in_use;
2971 /* Size (in elements) of increments by which we may expand the
2972 abbrev_die_table. */
2973 #define ABBREV_DIE_TABLE_INCREMENT 256
2975 /* A global counter for generating labels for line number data. */
2976 static unsigned int line_info_label_num;
2978 /* The current table to which we should emit line number information
2979 for the current function. This will be set up at the beginning of
2980 assembly for the function. */
2981 static dw_line_info_table *cur_line_info_table;
2983 /* The two default tables of line number info. */
2984 static GTY(()) dw_line_info_table *text_section_line_info;
2985 static GTY(()) dw_line_info_table *cold_text_section_line_info;
2987 /* The set of all non-default tables of line number info. */
2988 static GTY(()) vec<dw_line_info_table_p, va_gc> *separate_line_info;
2990 /* A flag to tell pubnames/types export if there is an info section to
2991 refer to. */
2992 static bool info_section_emitted;
2994 /* A pointer to the base of a table that contains a list of publicly
2995 accessible names. */
2996 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
2998 /* A pointer to the base of a table that contains a list of publicly
2999 accessible types. */
3000 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
3002 /* A pointer to the base of a table that contains a list of macro
3003 defines/undefines (and file start/end markers). */
3004 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
3006 /* True if .debug_macinfo or .debug_macros section is going to be
3007 emitted. */
3008 #define have_macinfo \
3009 (debug_info_level >= DINFO_LEVEL_VERBOSE \
3010 && !macinfo_table->is_empty ())
3012 /* Array of dies for which we should generate .debug_ranges info. */
3013 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
3015 /* Number of elements currently allocated for ranges_table. */
3016 static GTY(()) unsigned ranges_table_allocated;
3018 /* Number of elements in ranges_table currently in use. */
3019 static GTY(()) unsigned ranges_table_in_use;
3021 /* Array of pairs of labels referenced in ranges_table. */
3022 static GTY ((length ("ranges_by_label_allocated")))
3023 dw_ranges_by_label_ref ranges_by_label;
3025 /* Number of elements currently allocated for ranges_by_label. */
3026 static GTY(()) unsigned ranges_by_label_allocated;
3028 /* Number of elements in ranges_by_label currently in use. */
3029 static GTY(()) unsigned ranges_by_label_in_use;
3031 /* Size (in elements) of increments by which we may expand the
3032 ranges_table. */
3033 #define RANGES_TABLE_INCREMENT 64
3035 /* Whether we have location lists that need outputting */
3036 static GTY(()) bool have_location_lists;
3038 /* Unique label counter. */
3039 static GTY(()) unsigned int loclabel_num;
3041 /* Unique label counter for point-of-call tables. */
3042 static GTY(()) unsigned int poc_label_num;
3044 /* The last file entry emitted by maybe_emit_file(). */
3045 static GTY(()) struct dwarf_file_data * last_emitted_file;
3047 /* Number of internal labels generated by gen_internal_sym(). */
3048 static GTY(()) int label_num;
3050 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
3052 /* Instances of generic types for which we need to generate debug
3053 info that describe their generic parameters and arguments. That
3054 generation needs to happen once all types are properly laid out so
3055 we do it at the end of compilation. */
3056 static GTY(()) vec<tree, va_gc> *generic_type_instances;
3058 /* Offset from the "steady-state frame pointer" to the frame base,
3059 within the current function. */
3060 static HOST_WIDE_INT frame_pointer_fb_offset;
3061 static bool frame_pointer_fb_offset_valid;
3063 static vec<dw_die_ref> base_types;
3065 /* Flags to represent a set of attribute classes for attributes that represent
3066 a scalar value (bounds, pointers, ...). */
3067 enum dw_scalar_form
3069 dw_scalar_form_constant = 0x01,
3070 dw_scalar_form_exprloc = 0x02,
3071 dw_scalar_form_reference = 0x04
3074 /* Forward declarations for functions defined in this file. */
3076 static int is_pseudo_reg (const_rtx);
3077 static tree type_main_variant (tree);
3078 static int is_tagged_type (const_tree);
3079 static const char *dwarf_tag_name (unsigned);
3080 static const char *dwarf_attr_name (unsigned);
3081 static const char *dwarf_form_name (unsigned);
3082 static tree decl_ultimate_origin (const_tree);
3083 static tree decl_class_context (tree);
3084 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
3085 static inline enum dw_val_class AT_class (dw_attr_ref);
3086 static inline unsigned int AT_index (dw_attr_ref);
3087 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3088 static inline unsigned AT_flag (dw_attr_ref);
3089 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3090 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
3091 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3092 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
3093 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3094 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3095 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3096 unsigned int, unsigned char *);
3097 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3098 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3099 static inline const char *AT_string (dw_attr_ref);
3100 static enum dwarf_form AT_string_form (dw_attr_ref);
3101 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3102 static void add_AT_specification (dw_die_ref, dw_die_ref);
3103 static inline dw_die_ref AT_ref (dw_attr_ref);
3104 static inline int AT_ref_external (dw_attr_ref);
3105 static inline void set_AT_ref_external (dw_attr_ref, int);
3106 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3107 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3108 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
3109 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3110 dw_loc_list_ref);
3111 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
3112 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3113 static void remove_addr_table_entry (addr_table_entry *);
3114 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3115 static inline rtx AT_addr (dw_attr_ref);
3116 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3117 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3118 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3119 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3120 unsigned HOST_WIDE_INT);
3121 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3122 unsigned long, bool);
3123 static inline const char *AT_lbl (dw_attr_ref);
3124 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
3125 static const char *get_AT_low_pc (dw_die_ref);
3126 static const char *get_AT_hi_pc (dw_die_ref);
3127 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3128 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3129 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3130 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3131 static bool is_cxx (void);
3132 static bool is_fortran (void);
3133 static bool is_ada (void);
3134 static bool remove_AT (dw_die_ref, enum dwarf_attribute);
3135 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3136 static void add_child_die (dw_die_ref, dw_die_ref);
3137 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3138 static dw_die_ref lookup_type_die (tree);
3139 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3140 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3141 static void equate_type_number_to_die (tree, dw_die_ref);
3142 static dw_die_ref lookup_decl_die (tree);
3143 static var_loc_list *lookup_decl_loc (const_tree);
3144 static void equate_decl_number_to_die (tree, dw_die_ref);
3145 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3146 static void print_spaces (FILE *);
3147 static void print_die (dw_die_ref, FILE *);
3148 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3149 static dw_die_ref pop_compile_unit (dw_die_ref);
3150 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3151 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
3152 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3153 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3154 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3155 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3156 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
3157 struct md5_ctx *, int *);
3158 struct checksum_attributes;
3159 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3160 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3161 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3162 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3163 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3164 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3165 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
3166 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3167 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3168 static void compute_section_prefix (dw_die_ref);
3169 static int is_type_die (dw_die_ref);
3170 static int is_comdat_die (dw_die_ref);
3171 static int is_symbol_die (dw_die_ref);
3172 static inline bool is_template_instantiation (dw_die_ref);
3173 static void assign_symbol_names (dw_die_ref);
3174 static void break_out_includes (dw_die_ref);
3175 static int is_declaration_die (dw_die_ref);
3176 static int should_move_die_to_comdat (dw_die_ref);
3177 static dw_die_ref clone_as_declaration (dw_die_ref);
3178 static dw_die_ref clone_die (dw_die_ref);
3179 static dw_die_ref clone_tree (dw_die_ref);
3180 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3181 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3182 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3183 static dw_die_ref generate_skeleton (dw_die_ref);
3184 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3185 dw_die_ref,
3186 dw_die_ref);
3187 static void break_out_comdat_types (dw_die_ref);
3188 static void copy_decls_for_unworthy_types (dw_die_ref);
3190 static void add_sibling_attributes (dw_die_ref);
3191 static void output_location_lists (dw_die_ref);
3192 static int constant_size (unsigned HOST_WIDE_INT);
3193 static unsigned long size_of_die (dw_die_ref);
3194 static void calc_die_sizes (dw_die_ref);
3195 static void calc_base_type_die_sizes (void);
3196 static void mark_dies (dw_die_ref);
3197 static void unmark_dies (dw_die_ref);
3198 static void unmark_all_dies (dw_die_ref);
3199 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3200 static unsigned long size_of_aranges (void);
3201 static enum dwarf_form value_format (dw_attr_ref);
3202 static void output_value_format (dw_attr_ref);
3203 static void output_abbrev_section (void);
3204 static void output_die_abbrevs (unsigned long, dw_die_ref);
3205 static void output_die_symbol (dw_die_ref);
3206 static void output_die (dw_die_ref);
3207 static void output_compilation_unit_header (void);
3208 static void output_comp_unit (dw_die_ref, int);
3209 static void output_comdat_type_unit (comdat_type_node *);
3210 static const char *dwarf2_name (tree, int);
3211 static void add_pubname (tree, dw_die_ref);
3212 static void add_enumerator_pubname (const char *, dw_die_ref);
3213 static void add_pubname_string (const char *, dw_die_ref);
3214 static void add_pubtype (tree, dw_die_ref);
3215 static void output_pubnames (vec<pubname_entry, va_gc> *);
3216 static void output_aranges (unsigned long);
3217 static unsigned int add_ranges_num (int);
3218 static unsigned int add_ranges (const_tree);
3219 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3220 bool *, bool);
3221 static void output_ranges (void);
3222 static dw_line_info_table *new_line_info_table (void);
3223 static void output_line_info (bool);
3224 static void output_file_names (void);
3225 static dw_die_ref base_type_die (tree);
3226 static int is_base_type (tree);
3227 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
3228 static int decl_quals (const_tree);
3229 static dw_die_ref modified_type_die (tree, int, dw_die_ref);
3230 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3231 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3232 static int type_is_enum (const_tree);
3233 static unsigned int dbx_reg_number (const_rtx);
3234 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3235 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3236 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3237 enum var_init_status);
3238 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3239 enum var_init_status);
3240 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3241 enum var_init_status);
3242 static int is_based_loc (const_rtx);
3243 static bool resolve_one_addr (rtx *);
3244 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3245 enum var_init_status);
3246 static dw_loc_descr_ref loc_descriptor (rtx, machine_mode mode,
3247 enum var_init_status);
3248 struct loc_descr_context;
3249 static dw_loc_list_ref loc_list_from_tree (tree, int,
3250 const struct loc_descr_context *);
3251 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int,
3252 const struct loc_descr_context *);
3253 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3254 static tree field_type (const_tree);
3255 static unsigned int simple_type_align_in_bits (const_tree);
3256 static unsigned int simple_decl_align_in_bits (const_tree);
3257 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3258 static HOST_WIDE_INT field_byte_offset (const_tree);
3259 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3260 dw_loc_list_ref);
3261 static void add_data_member_location_attribute (dw_die_ref, tree);
3262 static bool add_const_value_attribute (dw_die_ref, rtx);
3263 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3264 static void insert_wide_int (const wide_int &, unsigned char *, int);
3265 static void insert_float (const_rtx, unsigned char *);
3266 static rtx rtl_for_decl_location (tree);
3267 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool,
3268 enum dwarf_attribute);
3269 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3270 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3271 static void add_name_attribute (dw_die_ref, const char *);
3272 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3273 static void add_comp_dir_attribute (dw_die_ref);
3274 static void add_scalar_info (dw_die_ref, enum dwarf_attribute, tree, int,
3275 const struct loc_descr_context *);
3276 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree,
3277 const struct loc_descr_context *);
3278 static void add_subscript_info (dw_die_ref, tree, bool);
3279 static void add_byte_size_attribute (dw_die_ref, tree);
3280 static void add_bit_offset_attribute (dw_die_ref, tree);
3281 static void add_bit_size_attribute (dw_die_ref, tree);
3282 static void add_prototyped_attribute (dw_die_ref, tree);
3283 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3284 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3285 static void add_src_coords_attributes (dw_die_ref, tree);
3286 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3287 static void push_decl_scope (tree);
3288 static void pop_decl_scope (void);
3289 static dw_die_ref scope_die_for (tree, dw_die_ref);
3290 static inline int local_scope_p (dw_die_ref);
3291 static inline int class_scope_p (dw_die_ref);
3292 static inline int class_or_namespace_scope_p (dw_die_ref);
3293 static void add_type_attribute (dw_die_ref, tree, int, dw_die_ref);
3294 static void add_calling_convention_attribute (dw_die_ref, tree);
3295 static const char *type_tag (const_tree);
3296 static tree member_declared_type (const_tree);
3297 #if 0
3298 static const char *decl_start_label (tree);
3299 #endif
3300 static void gen_array_type_die (tree, dw_die_ref);
3301 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3302 #if 0
3303 static void gen_entry_point_die (tree, dw_die_ref);
3304 #endif
3305 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3306 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3307 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3308 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3309 static void gen_formal_types_die (tree, dw_die_ref);
3310 static void gen_subprogram_die (tree, dw_die_ref);
3311 static void gen_variable_die (tree, tree, dw_die_ref);
3312 static void gen_const_die (tree, dw_die_ref);
3313 static void gen_label_die (tree, dw_die_ref);
3314 static void gen_lexical_block_die (tree, dw_die_ref);
3315 static void gen_inlined_subroutine_die (tree, dw_die_ref);
3316 static void gen_field_die (tree, dw_die_ref);
3317 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3318 static dw_die_ref gen_compile_unit_die (const char *);
3319 static void gen_inheritance_die (tree, tree, dw_die_ref);
3320 static void gen_member_die (tree, dw_die_ref);
3321 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3322 enum debug_info_usage);
3323 static void gen_subroutine_type_die (tree, dw_die_ref);
3324 static void gen_typedef_die (tree, dw_die_ref);
3325 static void gen_type_die (tree, dw_die_ref);
3326 static void gen_block_die (tree, dw_die_ref);
3327 static void decls_for_scope (tree, dw_die_ref);
3328 static inline int is_redundant_typedef (const_tree);
3329 static bool is_naming_typedef_decl (const_tree);
3330 static inline dw_die_ref get_context_die (tree);
3331 static void gen_namespace_die (tree, dw_die_ref);
3332 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3333 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
3334 static dw_die_ref force_decl_die (tree);
3335 static dw_die_ref force_type_die (tree);
3336 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3337 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3338 static struct dwarf_file_data * lookup_filename (const char *);
3339 static void retry_incomplete_types (void);
3340 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3341 static void gen_generic_params_dies (tree);
3342 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3343 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3344 static void splice_child_die (dw_die_ref, dw_die_ref);
3345 static int file_info_cmp (const void *, const void *);
3346 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3347 const char *, const char *);
3348 static void output_loc_list (dw_loc_list_ref);
3349 static char *gen_internal_sym (const char *);
3350 static bool want_pubnames (void);
3352 static void prune_unmark_dies (dw_die_ref);
3353 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3354 static void prune_unused_types_mark (dw_die_ref, int);
3355 static void prune_unused_types_walk (dw_die_ref);
3356 static void prune_unused_types_walk_attribs (dw_die_ref);
3357 static void prune_unused_types_prune (dw_die_ref);
3358 static void prune_unused_types (void);
3359 static int maybe_emit_file (struct dwarf_file_data *fd);
3360 static inline const char *AT_vms_delta1 (dw_attr_ref);
3361 static inline const char *AT_vms_delta2 (dw_attr_ref);
3362 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3363 const char *, const char *);
3364 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3365 static void gen_remaining_tmpl_value_param_die_attribute (void);
3366 static bool generic_type_p (tree);
3367 static void schedule_generic_params_dies_gen (tree t);
3368 static void gen_scheduled_generic_parms_dies (void);
3370 static const char *comp_dir_string (void);
3372 static void hash_loc_operands (dw_loc_descr_ref, inchash::hash &);
3374 /* enum for tracking thread-local variables whose address is really an offset
3375 relative to the TLS pointer, which will need link-time relocation, but will
3376 not need relocation by the DWARF consumer. */
3378 enum dtprel_bool
3380 dtprel_false = 0,
3381 dtprel_true = 1
3384 /* Return the operator to use for an address of a variable. For dtprel_true, we
3385 use DW_OP_const*. For regular variables, which need both link-time
3386 relocation and consumer-level relocation (e.g., to account for shared objects
3387 loaded at a random address), we use DW_OP_addr*. */
3389 static inline enum dwarf_location_atom
3390 dw_addr_op (enum dtprel_bool dtprel)
3392 if (dtprel == dtprel_true)
3393 return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3394 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3395 else
3396 return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3399 /* Return a pointer to a newly allocated address location description. If
3400 dwarf_split_debug_info is true, then record the address with the appropriate
3401 relocation. */
3402 static inline dw_loc_descr_ref
3403 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3405 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3407 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3408 ref->dw_loc_oprnd1.v.val_addr = addr;
3409 ref->dtprel = dtprel;
3410 if (dwarf_split_debug_info)
3411 ref->dw_loc_oprnd1.val_entry
3412 = add_addr_table_entry (addr,
3413 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3414 else
3415 ref->dw_loc_oprnd1.val_entry = NULL;
3417 return ref;
3420 /* Section names used to hold DWARF debugging information. */
3422 #ifndef DEBUG_INFO_SECTION
3423 #define DEBUG_INFO_SECTION ".debug_info"
3424 #endif
3425 #ifndef DEBUG_DWO_INFO_SECTION
3426 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3427 #endif
3428 #ifndef DEBUG_ABBREV_SECTION
3429 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3430 #endif
3431 #ifndef DEBUG_DWO_ABBREV_SECTION
3432 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3433 #endif
3434 #ifndef DEBUG_ARANGES_SECTION
3435 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3436 #endif
3437 #ifndef DEBUG_ADDR_SECTION
3438 #define DEBUG_ADDR_SECTION ".debug_addr"
3439 #endif
3440 #ifndef DEBUG_NORM_MACINFO_SECTION
3441 #define DEBUG_NORM_MACINFO_SECTION ".debug_macinfo"
3442 #endif
3443 #ifndef DEBUG_DWO_MACINFO_SECTION
3444 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
3445 #endif
3446 #ifndef DEBUG_MACINFO_SECTION
3447 #define DEBUG_MACINFO_SECTION \
3448 (!dwarf_split_debug_info \
3449 ? (DEBUG_NORM_MACINFO_SECTION) : (DEBUG_DWO_MACINFO_SECTION))
3450 #endif
3451 #ifndef DEBUG_NORM_MACRO_SECTION
3452 #define DEBUG_NORM_MACRO_SECTION ".debug_macro"
3453 #endif
3454 #ifndef DEBUG_DWO_MACRO_SECTION
3455 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
3456 #endif
3457 #ifndef DEBUG_MACRO_SECTION
3458 #define DEBUG_MACRO_SECTION \
3459 (!dwarf_split_debug_info \
3460 ? (DEBUG_NORM_MACRO_SECTION) : (DEBUG_DWO_MACRO_SECTION))
3461 #endif
3462 #ifndef DEBUG_LINE_SECTION
3463 #define DEBUG_LINE_SECTION ".debug_line"
3464 #endif
3465 #ifndef DEBUG_DWO_LINE_SECTION
3466 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3467 #endif
3468 #ifndef DEBUG_LOC_SECTION
3469 #define DEBUG_LOC_SECTION ".debug_loc"
3470 #endif
3471 #ifndef DEBUG_DWO_LOC_SECTION
3472 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
3473 #endif
3474 #ifndef DEBUG_PUBNAMES_SECTION
3475 #define DEBUG_PUBNAMES_SECTION \
3476 ((debug_generate_pub_sections == 2) \
3477 ? ".debug_gnu_pubnames" : ".debug_pubnames")
3478 #endif
3479 #ifndef DEBUG_PUBTYPES_SECTION
3480 #define DEBUG_PUBTYPES_SECTION \
3481 ((debug_generate_pub_sections == 2) \
3482 ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
3483 #endif
3484 #define DEBUG_NORM_STR_OFFSETS_SECTION ".debug_str_offsets"
3485 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3486 #ifndef DEBUG_STR_OFFSETS_SECTION
3487 #define DEBUG_STR_OFFSETS_SECTION \
3488 (!dwarf_split_debug_info \
3489 ? (DEBUG_NORM_STR_OFFSETS_SECTION) : (DEBUG_DWO_STR_OFFSETS_SECTION))
3490 #endif
3491 #ifndef DEBUG_STR_DWO_SECTION
3492 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
3493 #endif
3494 #ifndef DEBUG_STR_SECTION
3495 #define DEBUG_STR_SECTION ".debug_str"
3496 #endif
3497 #ifndef DEBUG_RANGES_SECTION
3498 #define DEBUG_RANGES_SECTION ".debug_ranges"
3499 #endif
3501 /* Standard ELF section names for compiled code and data. */
3502 #ifndef TEXT_SECTION_NAME
3503 #define TEXT_SECTION_NAME ".text"
3504 #endif
3506 /* Section flags for .debug_macinfo/.debug_macro section. */
3507 #define DEBUG_MACRO_SECTION_FLAGS \
3508 (dwarf_split_debug_info ? SECTION_DEBUG | SECTION_EXCLUDE : SECTION_DEBUG)
3510 /* Section flags for .debug_str section. */
3511 #define DEBUG_STR_SECTION_FLAGS \
3512 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
3513 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3514 : SECTION_DEBUG)
3516 /* Section flags for .debug_str.dwo section. */
3517 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
3519 /* Labels we insert at beginning sections we can reference instead of
3520 the section names themselves. */
3522 #ifndef TEXT_SECTION_LABEL
3523 #define TEXT_SECTION_LABEL "Ltext"
3524 #endif
3525 #ifndef COLD_TEXT_SECTION_LABEL
3526 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
3527 #endif
3528 #ifndef DEBUG_LINE_SECTION_LABEL
3529 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3530 #endif
3531 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3532 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
3533 #endif
3534 #ifndef DEBUG_INFO_SECTION_LABEL
3535 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3536 #endif
3537 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3538 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
3539 #endif
3540 #ifndef DEBUG_ABBREV_SECTION_LABEL
3541 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3542 #endif
3543 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3544 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3545 #endif
3546 #ifndef DEBUG_ADDR_SECTION_LABEL
3547 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
3548 #endif
3549 #ifndef DEBUG_LOC_SECTION_LABEL
3550 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3551 #endif
3552 #ifndef DEBUG_RANGES_SECTION_LABEL
3553 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3554 #endif
3555 #ifndef DEBUG_MACINFO_SECTION_LABEL
3556 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3557 #endif
3558 #ifndef DEBUG_MACRO_SECTION_LABEL
3559 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
3560 #endif
3561 #define SKELETON_COMP_DIE_ABBREV 1
3562 #define SKELETON_TYPE_DIE_ABBREV 2
3564 /* Definitions of defaults for formats and names of various special
3565 (artificial) labels which may be generated within this file (when the -g
3566 options is used and DWARF2_DEBUGGING_INFO is in effect.
3567 If necessary, these may be overridden from within the tm.h file, but
3568 typically, overriding these defaults is unnecessary. */
3570 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3571 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3572 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3573 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3574 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3575 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3576 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3577 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3578 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3579 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3580 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3581 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3582 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3583 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3585 #ifndef TEXT_END_LABEL
3586 #define TEXT_END_LABEL "Letext"
3587 #endif
3588 #ifndef COLD_END_LABEL
3589 #define COLD_END_LABEL "Letext_cold"
3590 #endif
3591 #ifndef BLOCK_BEGIN_LABEL
3592 #define BLOCK_BEGIN_LABEL "LBB"
3593 #endif
3594 #ifndef BLOCK_END_LABEL
3595 #define BLOCK_END_LABEL "LBE"
3596 #endif
3597 #ifndef LINE_CODE_LABEL
3598 #define LINE_CODE_LABEL "LM"
3599 #endif
3602 /* Return the root of the DIE's built for the current compilation unit. */
3603 static dw_die_ref
3604 comp_unit_die (void)
3606 if (!single_comp_unit_die)
3607 single_comp_unit_die = gen_compile_unit_die (NULL);
3608 return single_comp_unit_die;
3611 /* We allow a language front-end to designate a function that is to be
3612 called to "demangle" any name before it is put into a DIE. */
3614 static const char *(*demangle_name_func) (const char *);
3616 void
3617 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3619 demangle_name_func = func;
3622 /* Test if rtl node points to a pseudo register. */
3624 static inline int
3625 is_pseudo_reg (const_rtx rtl)
3627 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3628 || (GET_CODE (rtl) == SUBREG
3629 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3632 /* Return a reference to a type, with its const and volatile qualifiers
3633 removed. */
3635 static inline tree
3636 type_main_variant (tree type)
3638 type = TYPE_MAIN_VARIANT (type);
3640 /* ??? There really should be only one main variant among any group of
3641 variants of a given type (and all of the MAIN_VARIANT values for all
3642 members of the group should point to that one type) but sometimes the C
3643 front-end messes this up for array types, so we work around that bug
3644 here. */
3645 if (TREE_CODE (type) == ARRAY_TYPE)
3646 while (type != TYPE_MAIN_VARIANT (type))
3647 type = TYPE_MAIN_VARIANT (type);
3649 return type;
3652 /* Return nonzero if the given type node represents a tagged type. */
3654 static inline int
3655 is_tagged_type (const_tree type)
3657 enum tree_code code = TREE_CODE (type);
3659 return (code == RECORD_TYPE || code == UNION_TYPE
3660 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3663 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
3665 static void
3666 get_ref_die_offset_label (char *label, dw_die_ref ref)
3668 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3671 /* Return die_offset of a DIE reference to a base type. */
3673 static unsigned long int
3674 get_base_type_offset (dw_die_ref ref)
3676 if (ref->die_offset)
3677 return ref->die_offset;
3678 if (comp_unit_die ()->die_abbrev)
3680 calc_base_type_die_sizes ();
3681 gcc_assert (ref->die_offset);
3683 return ref->die_offset;
3686 /* Return die_offset of a DIE reference other than base type. */
3688 static unsigned long int
3689 get_ref_die_offset (dw_die_ref ref)
3691 gcc_assert (ref->die_offset);
3692 return ref->die_offset;
3695 /* Convert a DIE tag into its string name. */
3697 static const char *
3698 dwarf_tag_name (unsigned int tag)
3700 const char *name = get_DW_TAG_name (tag);
3702 if (name != NULL)
3703 return name;
3705 return "DW_TAG_<unknown>";
3708 /* Convert a DWARF attribute code into its string name. */
3710 static const char *
3711 dwarf_attr_name (unsigned int attr)
3713 const char *name;
3715 switch (attr)
3717 #if VMS_DEBUGGING_INFO
3718 case DW_AT_HP_prologue:
3719 return "DW_AT_HP_prologue";
3720 #else
3721 case DW_AT_MIPS_loop_unroll_factor:
3722 return "DW_AT_MIPS_loop_unroll_factor";
3723 #endif
3725 #if VMS_DEBUGGING_INFO
3726 case DW_AT_HP_epilogue:
3727 return "DW_AT_HP_epilogue";
3728 #else
3729 case DW_AT_MIPS_stride:
3730 return "DW_AT_MIPS_stride";
3731 #endif
3734 name = get_DW_AT_name (attr);
3736 if (name != NULL)
3737 return name;
3739 return "DW_AT_<unknown>";
3742 /* Convert a DWARF value form code into its string name. */
3744 static const char *
3745 dwarf_form_name (unsigned int form)
3747 const char *name = get_DW_FORM_name (form);
3749 if (name != NULL)
3750 return name;
3752 return "DW_FORM_<unknown>";
3755 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
3756 instance of an inlined instance of a decl which is local to an inline
3757 function, so we have to trace all of the way back through the origin chain
3758 to find out what sort of node actually served as the original seed for the
3759 given block. */
3761 static tree
3762 decl_ultimate_origin (const_tree decl)
3764 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
3765 return NULL_TREE;
3767 /* DECL_ABSTRACT_ORIGIN can point to itself; ignore that if
3768 we're trying to output the abstract instance of this function. */
3769 if (DECL_ABSTRACT_P (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
3770 return NULL_TREE;
3772 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
3773 most distant ancestor, this should never happen. */
3774 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
3776 return DECL_ABSTRACT_ORIGIN (decl);
3779 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
3780 of a virtual function may refer to a base class, so we check the 'this'
3781 parameter. */
3783 static tree
3784 decl_class_context (tree decl)
3786 tree context = NULL_TREE;
3788 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
3789 context = DECL_CONTEXT (decl);
3790 else
3791 context = TYPE_MAIN_VARIANT
3792 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
3794 if (context && !TYPE_P (context))
3795 context = NULL_TREE;
3797 return context;
3800 /* Add an attribute/value pair to a DIE. */
3802 static inline void
3803 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
3805 /* Maybe this should be an assert? */
3806 if (die == NULL)
3807 return;
3809 vec_safe_reserve (die->die_attr, 1);
3810 vec_safe_push (die->die_attr, *attr);
3813 static inline enum dw_val_class
3814 AT_class (dw_attr_ref a)
3816 return a->dw_attr_val.val_class;
3819 /* Return the index for any attribute that will be referenced with a
3820 DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index. String indices
3821 are stored in dw_attr_val.v.val_str for reference counting
3822 pruning. */
3824 static inline unsigned int
3825 AT_index (dw_attr_ref a)
3827 if (AT_class (a) == dw_val_class_str)
3828 return a->dw_attr_val.v.val_str->index;
3829 else if (a->dw_attr_val.val_entry != NULL)
3830 return a->dw_attr_val.val_entry->index;
3831 return NOT_INDEXED;
3834 /* Add a flag value attribute to a DIE. */
3836 static inline void
3837 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
3839 dw_attr_node attr;
3841 attr.dw_attr = attr_kind;
3842 attr.dw_attr_val.val_class = dw_val_class_flag;
3843 attr.dw_attr_val.val_entry = NULL;
3844 attr.dw_attr_val.v.val_flag = flag;
3845 add_dwarf_attr (die, &attr);
3848 static inline unsigned
3849 AT_flag (dw_attr_ref a)
3851 gcc_assert (a && AT_class (a) == dw_val_class_flag);
3852 return a->dw_attr_val.v.val_flag;
3855 /* Add a signed integer attribute value to a DIE. */
3857 static inline void
3858 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
3860 dw_attr_node attr;
3862 attr.dw_attr = attr_kind;
3863 attr.dw_attr_val.val_class = dw_val_class_const;
3864 attr.dw_attr_val.val_entry = NULL;
3865 attr.dw_attr_val.v.val_int = int_val;
3866 add_dwarf_attr (die, &attr);
3869 static inline HOST_WIDE_INT
3870 AT_int (dw_attr_ref a)
3872 gcc_assert (a && AT_class (a) == dw_val_class_const);
3873 return a->dw_attr_val.v.val_int;
3876 /* Add an unsigned integer attribute value to a DIE. */
3878 static inline void
3879 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
3880 unsigned HOST_WIDE_INT unsigned_val)
3882 dw_attr_node attr;
3884 attr.dw_attr = attr_kind;
3885 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
3886 attr.dw_attr_val.val_entry = NULL;
3887 attr.dw_attr_val.v.val_unsigned = unsigned_val;
3888 add_dwarf_attr (die, &attr);
3891 static inline unsigned HOST_WIDE_INT
3892 AT_unsigned (dw_attr_ref a)
3894 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
3895 return a->dw_attr_val.v.val_unsigned;
3898 /* Add an unsigned wide integer attribute value to a DIE. */
3900 static inline void
3901 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
3902 const wide_int& w)
3904 dw_attr_node attr;
3906 attr.dw_attr = attr_kind;
3907 attr.dw_attr_val.val_class = dw_val_class_wide_int;
3908 attr.dw_attr_val.val_entry = NULL;
3909 attr.dw_attr_val.v.val_wide = ggc_alloc<wide_int> ();
3910 *attr.dw_attr_val.v.val_wide = w;
3911 add_dwarf_attr (die, &attr);
3914 /* Add an unsigned double integer attribute value to a DIE. */
3916 static inline void
3917 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
3918 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
3920 dw_attr_node attr;
3922 attr.dw_attr = attr_kind;
3923 attr.dw_attr_val.val_class = dw_val_class_const_double;
3924 attr.dw_attr_val.val_entry = NULL;
3925 attr.dw_attr_val.v.val_double.high = high;
3926 attr.dw_attr_val.v.val_double.low = low;
3927 add_dwarf_attr (die, &attr);
3930 /* Add a floating point attribute value to a DIE and return it. */
3932 static inline void
3933 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
3934 unsigned int length, unsigned int elt_size, unsigned char *array)
3936 dw_attr_node attr;
3938 attr.dw_attr = attr_kind;
3939 attr.dw_attr_val.val_class = dw_val_class_vec;
3940 attr.dw_attr_val.val_entry = NULL;
3941 attr.dw_attr_val.v.val_vec.length = length;
3942 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
3943 attr.dw_attr_val.v.val_vec.array = array;
3944 add_dwarf_attr (die, &attr);
3947 /* Add an 8-byte data attribute value to a DIE. */
3949 static inline void
3950 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
3951 unsigned char data8[8])
3953 dw_attr_node attr;
3955 attr.dw_attr = attr_kind;
3956 attr.dw_attr_val.val_class = dw_val_class_data8;
3957 attr.dw_attr_val.val_entry = NULL;
3958 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
3959 add_dwarf_attr (die, &attr);
3962 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
3963 dwarf_split_debug_info, address attributes in dies destined for the
3964 final executable have force_direct set to avoid using indexed
3965 references. */
3967 static inline void
3968 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
3969 bool force_direct)
3971 dw_attr_node attr;
3972 char * lbl_id;
3974 lbl_id = xstrdup (lbl_low);
3975 attr.dw_attr = DW_AT_low_pc;
3976 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3977 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3978 if (dwarf_split_debug_info && !force_direct)
3979 attr.dw_attr_val.val_entry
3980 = add_addr_table_entry (lbl_id, ate_kind_label);
3981 else
3982 attr.dw_attr_val.val_entry = NULL;
3983 add_dwarf_attr (die, &attr);
3985 attr.dw_attr = DW_AT_high_pc;
3986 if (dwarf_version < 4)
3987 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3988 else
3989 attr.dw_attr_val.val_class = dw_val_class_high_pc;
3990 lbl_id = xstrdup (lbl_high);
3991 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3992 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
3993 && dwarf_split_debug_info && !force_direct)
3994 attr.dw_attr_val.val_entry
3995 = add_addr_table_entry (lbl_id, ate_kind_label);
3996 else
3997 attr.dw_attr_val.val_entry = NULL;
3998 add_dwarf_attr (die, &attr);
4001 /* Hash and equality functions for debug_str_hash. */
4003 hashval_t
4004 indirect_string_hasher::hash (indirect_string_node *x)
4006 return htab_hash_string (x->str);
4009 bool
4010 indirect_string_hasher::equal (indirect_string_node *x1, const char *x2)
4012 return strcmp (x1->str, x2) == 0;
4015 /* Add STR to the given string hash table. */
4017 static struct indirect_string_node *
4018 find_AT_string_in_table (const char *str,
4019 hash_table<indirect_string_hasher> *table)
4021 struct indirect_string_node *node;
4023 indirect_string_node **slot
4024 = table->find_slot_with_hash (str, htab_hash_string (str), INSERT);
4025 if (*slot == NULL)
4027 node = ggc_cleared_alloc<indirect_string_node> ();
4028 node->str = ggc_strdup (str);
4029 *slot = node;
4031 else
4032 node = *slot;
4034 node->refcount++;
4035 return node;
4038 /* Add STR to the indirect string hash table. */
4040 static struct indirect_string_node *
4041 find_AT_string (const char *str)
4043 if (! debug_str_hash)
4044 debug_str_hash = hash_table<indirect_string_hasher>::create_ggc (10);
4046 return find_AT_string_in_table (str, debug_str_hash);
4049 /* Add a string attribute value to a DIE. */
4051 static inline void
4052 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4054 dw_attr_node attr;
4055 struct indirect_string_node *node;
4057 node = find_AT_string (str);
4059 attr.dw_attr = attr_kind;
4060 attr.dw_attr_val.val_class = dw_val_class_str;
4061 attr.dw_attr_val.val_entry = NULL;
4062 attr.dw_attr_val.v.val_str = node;
4063 add_dwarf_attr (die, &attr);
4066 static inline const char *
4067 AT_string (dw_attr_ref a)
4069 gcc_assert (a && AT_class (a) == dw_val_class_str);
4070 return a->dw_attr_val.v.val_str->str;
4073 /* Call this function directly to bypass AT_string_form's logic to put
4074 the string inline in the die. */
4076 static void
4077 set_indirect_string (struct indirect_string_node *node)
4079 char label[32];
4080 /* Already indirect is a no op. */
4081 if (node->form == DW_FORM_strp || node->form == DW_FORM_GNU_str_index)
4083 gcc_assert (node->label);
4084 return;
4086 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4087 ++dw2_string_counter;
4088 node->label = xstrdup (label);
4090 if (!dwarf_split_debug_info)
4092 node->form = DW_FORM_strp;
4093 node->index = NOT_INDEXED;
4095 else
4097 node->form = DW_FORM_GNU_str_index;
4098 node->index = NO_INDEX_ASSIGNED;
4102 /* Find out whether a string should be output inline in DIE
4103 or out-of-line in .debug_str section. */
4105 static enum dwarf_form
4106 find_string_form (struct indirect_string_node *node)
4108 unsigned int len;
4110 if (node->form)
4111 return node->form;
4113 len = strlen (node->str) + 1;
4115 /* If the string is shorter or equal to the size of the reference, it is
4116 always better to put it inline. */
4117 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4118 return node->form = DW_FORM_string;
4120 /* If we cannot expect the linker to merge strings in .debug_str
4121 section, only put it into .debug_str if it is worth even in this
4122 single module. */
4123 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4124 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4125 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
4126 return node->form = DW_FORM_string;
4128 set_indirect_string (node);
4130 return node->form;
4133 /* Find out whether the string referenced from the attribute should be
4134 output inline in DIE or out-of-line in .debug_str section. */
4136 static enum dwarf_form
4137 AT_string_form (dw_attr_ref a)
4139 gcc_assert (a && AT_class (a) == dw_val_class_str);
4140 return find_string_form (a->dw_attr_val.v.val_str);
4143 /* Add a DIE reference attribute value to a DIE. */
4145 static inline void
4146 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4148 dw_attr_node attr;
4150 #ifdef ENABLE_CHECKING
4151 gcc_assert (targ_die != NULL);
4152 #else
4153 /* With LTO we can end up trying to reference something we didn't create
4154 a DIE for. Avoid crashing later on a NULL referenced DIE. */
4155 if (targ_die == NULL)
4156 return;
4157 #endif
4159 attr.dw_attr = attr_kind;
4160 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4161 attr.dw_attr_val.val_entry = NULL;
4162 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4163 attr.dw_attr_val.v.val_die_ref.external = 0;
4164 add_dwarf_attr (die, &attr);
4167 /* Change DIE reference REF to point to NEW_DIE instead. */
4169 static inline void
4170 change_AT_die_ref (dw_attr_ref ref, dw_die_ref new_die)
4172 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4173 ref->dw_attr_val.v.val_die_ref.die = new_die;
4174 ref->dw_attr_val.v.val_die_ref.external = 0;
4177 /* Add an AT_specification attribute to a DIE, and also make the back
4178 pointer from the specification to the definition. */
4180 static inline void
4181 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4183 add_AT_die_ref (die, DW_AT_specification, targ_die);
4184 gcc_assert (!targ_die->die_definition);
4185 targ_die->die_definition = die;
4188 static inline dw_die_ref
4189 AT_ref (dw_attr_ref a)
4191 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4192 return a->dw_attr_val.v.val_die_ref.die;
4195 static inline int
4196 AT_ref_external (dw_attr_ref a)
4198 if (a && AT_class (a) == dw_val_class_die_ref)
4199 return a->dw_attr_val.v.val_die_ref.external;
4201 return 0;
4204 static inline void
4205 set_AT_ref_external (dw_attr_ref a, int i)
4207 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4208 a->dw_attr_val.v.val_die_ref.external = i;
4211 /* Add an FDE reference attribute value to a DIE. */
4213 static inline void
4214 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4216 dw_attr_node attr;
4218 attr.dw_attr = attr_kind;
4219 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4220 attr.dw_attr_val.val_entry = NULL;
4221 attr.dw_attr_val.v.val_fde_index = targ_fde;
4222 add_dwarf_attr (die, &attr);
4225 /* Add a location description attribute value to a DIE. */
4227 static inline void
4228 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4230 dw_attr_node attr;
4232 attr.dw_attr = attr_kind;
4233 attr.dw_attr_val.val_class = dw_val_class_loc;
4234 attr.dw_attr_val.val_entry = NULL;
4235 attr.dw_attr_val.v.val_loc = loc;
4236 add_dwarf_attr (die, &attr);
4239 static inline dw_loc_descr_ref
4240 AT_loc (dw_attr_ref a)
4242 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4243 return a->dw_attr_val.v.val_loc;
4246 static inline void
4247 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4249 dw_attr_node attr;
4251 attr.dw_attr = attr_kind;
4252 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4253 attr.dw_attr_val.val_entry = NULL;
4254 attr.dw_attr_val.v.val_loc_list = loc_list;
4255 add_dwarf_attr (die, &attr);
4256 have_location_lists = true;
4259 static inline dw_loc_list_ref
4260 AT_loc_list (dw_attr_ref a)
4262 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4263 return a->dw_attr_val.v.val_loc_list;
4266 static inline dw_loc_list_ref *
4267 AT_loc_list_ptr (dw_attr_ref a)
4269 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4270 return &a->dw_attr_val.v.val_loc_list;
4273 struct addr_hasher : ggc_ptr_hash<addr_table_entry>
4275 static hashval_t hash (addr_table_entry *);
4276 static bool equal (addr_table_entry *, addr_table_entry *);
4279 /* Table of entries into the .debug_addr section. */
4281 static GTY (()) hash_table<addr_hasher> *addr_index_table;
4283 /* Hash an address_table_entry. */
4285 hashval_t
4286 addr_hasher::hash (addr_table_entry *a)
4288 inchash::hash hstate;
4289 switch (a->kind)
4291 case ate_kind_rtx:
4292 hstate.add_int (0);
4293 break;
4294 case ate_kind_rtx_dtprel:
4295 hstate.add_int (1);
4296 break;
4297 case ate_kind_label:
4298 return htab_hash_string (a->addr.label);
4299 default:
4300 gcc_unreachable ();
4302 inchash::add_rtx (a->addr.rtl, hstate);
4303 return hstate.end ();
4306 /* Determine equality for two address_table_entries. */
4308 bool
4309 addr_hasher::equal (addr_table_entry *a1, addr_table_entry *a2)
4311 if (a1->kind != a2->kind)
4312 return 0;
4313 switch (a1->kind)
4315 case ate_kind_rtx:
4316 case ate_kind_rtx_dtprel:
4317 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4318 case ate_kind_label:
4319 return strcmp (a1->addr.label, a2->addr.label) == 0;
4320 default:
4321 gcc_unreachable ();
4325 /* Initialize an addr_table_entry. */
4327 void
4328 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4330 e->kind = kind;
4331 switch (kind)
4333 case ate_kind_rtx:
4334 case ate_kind_rtx_dtprel:
4335 e->addr.rtl = (rtx) addr;
4336 break;
4337 case ate_kind_label:
4338 e->addr.label = (char *) addr;
4339 break;
4341 e->refcount = 0;
4342 e->index = NO_INDEX_ASSIGNED;
4345 /* Add attr to the address table entry to the table. Defer setting an
4346 index until output time. */
4348 static addr_table_entry *
4349 add_addr_table_entry (void *addr, enum ate_kind kind)
4351 addr_table_entry *node;
4352 addr_table_entry finder;
4354 gcc_assert (dwarf_split_debug_info);
4355 if (! addr_index_table)
4356 addr_index_table = hash_table<addr_hasher>::create_ggc (10);
4357 init_addr_table_entry (&finder, kind, addr);
4358 addr_table_entry **slot = addr_index_table->find_slot (&finder, INSERT);
4360 if (*slot == HTAB_EMPTY_ENTRY)
4362 node = ggc_cleared_alloc<addr_table_entry> ();
4363 init_addr_table_entry (node, kind, addr);
4364 *slot = node;
4366 else
4367 node = *slot;
4369 node->refcount++;
4370 return node;
4373 /* Remove an entry from the addr table by decrementing its refcount.
4374 Strictly, decrementing the refcount would be enough, but the
4375 assertion that the entry is actually in the table has found
4376 bugs. */
4378 static void
4379 remove_addr_table_entry (addr_table_entry *entry)
4381 gcc_assert (dwarf_split_debug_info && addr_index_table);
4382 /* After an index is assigned, the table is frozen. */
4383 gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
4384 entry->refcount--;
4387 /* Given a location list, remove all addresses it refers to from the
4388 address_table. */
4390 static void
4391 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4393 for (; descr; descr = descr->dw_loc_next)
4394 if (descr->dw_loc_oprnd1.val_entry != NULL)
4396 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4397 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4401 /* A helper function for dwarf2out_finish called through
4402 htab_traverse. Assign an addr_table_entry its index. All entries
4403 must be collected into the table when this function is called,
4404 because the indexing code relies on htab_traverse to traverse nodes
4405 in the same order for each run. */
4408 index_addr_table_entry (addr_table_entry **h, unsigned int *index)
4410 addr_table_entry *node = *h;
4412 /* Don't index unreferenced nodes. */
4413 if (node->refcount == 0)
4414 return 1;
4416 gcc_assert (node->index == NO_INDEX_ASSIGNED);
4417 node->index = *index;
4418 *index += 1;
4420 return 1;
4423 /* Add an address constant attribute value to a DIE. When using
4424 dwarf_split_debug_info, address attributes in dies destined for the
4425 final executable should be direct references--setting the parameter
4426 force_direct ensures this behavior. */
4428 static inline void
4429 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4430 bool force_direct)
4432 dw_attr_node attr;
4434 attr.dw_attr = attr_kind;
4435 attr.dw_attr_val.val_class = dw_val_class_addr;
4436 attr.dw_attr_val.v.val_addr = addr;
4437 if (dwarf_split_debug_info && !force_direct)
4438 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4439 else
4440 attr.dw_attr_val.val_entry = NULL;
4441 add_dwarf_attr (die, &attr);
4444 /* Get the RTX from to an address DIE attribute. */
4446 static inline rtx
4447 AT_addr (dw_attr_ref a)
4449 gcc_assert (a && AT_class (a) == dw_val_class_addr);
4450 return a->dw_attr_val.v.val_addr;
4453 /* Add a file attribute value to a DIE. */
4455 static inline void
4456 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4457 struct dwarf_file_data *fd)
4459 dw_attr_node attr;
4461 attr.dw_attr = attr_kind;
4462 attr.dw_attr_val.val_class = dw_val_class_file;
4463 attr.dw_attr_val.val_entry = NULL;
4464 attr.dw_attr_val.v.val_file = fd;
4465 add_dwarf_attr (die, &attr);
4468 /* Get the dwarf_file_data from a file DIE attribute. */
4470 static inline struct dwarf_file_data *
4471 AT_file (dw_attr_ref a)
4473 gcc_assert (a && AT_class (a) == dw_val_class_file);
4474 return a->dw_attr_val.v.val_file;
4477 /* Add a vms delta attribute value to a DIE. */
4479 static inline void
4480 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4481 const char *lbl1, const char *lbl2)
4483 dw_attr_node attr;
4485 attr.dw_attr = attr_kind;
4486 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4487 attr.dw_attr_val.val_entry = NULL;
4488 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4489 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4490 add_dwarf_attr (die, &attr);
4493 /* Add a label identifier attribute value to a DIE. */
4495 static inline void
4496 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4497 const char *lbl_id)
4499 dw_attr_node attr;
4501 attr.dw_attr = attr_kind;
4502 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4503 attr.dw_attr_val.val_entry = NULL;
4504 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4505 if (dwarf_split_debug_info)
4506 attr.dw_attr_val.val_entry
4507 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4508 ate_kind_label);
4509 add_dwarf_attr (die, &attr);
4512 /* Add a section offset attribute value to a DIE, an offset into the
4513 debug_line section. */
4515 static inline void
4516 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4517 const char *label)
4519 dw_attr_node attr;
4521 attr.dw_attr = attr_kind;
4522 attr.dw_attr_val.val_class = dw_val_class_lineptr;
4523 attr.dw_attr_val.val_entry = NULL;
4524 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4525 add_dwarf_attr (die, &attr);
4528 /* Add a section offset attribute value to a DIE, an offset into the
4529 debug_macinfo section. */
4531 static inline void
4532 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4533 const char *label)
4535 dw_attr_node attr;
4537 attr.dw_attr = attr_kind;
4538 attr.dw_attr_val.val_class = dw_val_class_macptr;
4539 attr.dw_attr_val.val_entry = NULL;
4540 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4541 add_dwarf_attr (die, &attr);
4544 /* Add an offset attribute value to a DIE. */
4546 static inline void
4547 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4548 unsigned HOST_WIDE_INT offset)
4550 dw_attr_node attr;
4552 attr.dw_attr = attr_kind;
4553 attr.dw_attr_val.val_class = dw_val_class_offset;
4554 attr.dw_attr_val.val_entry = NULL;
4555 attr.dw_attr_val.v.val_offset = offset;
4556 add_dwarf_attr (die, &attr);
4559 /* Add a range_list attribute value to a DIE. When using
4560 dwarf_split_debug_info, address attributes in dies destined for the
4561 final executable should be direct references--setting the parameter
4562 force_direct ensures this behavior. */
4564 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4565 #define RELOCATED_OFFSET (NULL)
4567 static void
4568 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4569 long unsigned int offset, bool force_direct)
4571 dw_attr_node attr;
4573 attr.dw_attr = attr_kind;
4574 attr.dw_attr_val.val_class = dw_val_class_range_list;
4575 /* For the range_list attribute, use val_entry to store whether the
4576 offset should follow split-debug-info or normal semantics. This
4577 value is read in output_range_list_offset. */
4578 if (dwarf_split_debug_info && !force_direct)
4579 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4580 else
4581 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4582 attr.dw_attr_val.v.val_offset = offset;
4583 add_dwarf_attr (die, &attr);
4586 /* Return the start label of a delta attribute. */
4588 static inline const char *
4589 AT_vms_delta1 (dw_attr_ref a)
4591 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4592 return a->dw_attr_val.v.val_vms_delta.lbl1;
4595 /* Return the end label of a delta attribute. */
4597 static inline const char *
4598 AT_vms_delta2 (dw_attr_ref a)
4600 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4601 return a->dw_attr_val.v.val_vms_delta.lbl2;
4604 static inline const char *
4605 AT_lbl (dw_attr_ref a)
4607 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4608 || AT_class (a) == dw_val_class_lineptr
4609 || AT_class (a) == dw_val_class_macptr
4610 || AT_class (a) == dw_val_class_high_pc));
4611 return a->dw_attr_val.v.val_lbl_id;
4614 /* Get the attribute of type attr_kind. */
4616 static dw_attr_ref
4617 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4619 dw_attr_ref a;
4620 unsigned ix;
4621 dw_die_ref spec = NULL;
4623 if (! die)
4624 return NULL;
4626 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4627 if (a->dw_attr == attr_kind)
4628 return a;
4629 else if (a->dw_attr == DW_AT_specification
4630 || a->dw_attr == DW_AT_abstract_origin)
4631 spec = AT_ref (a);
4633 if (spec)
4634 return get_AT (spec, attr_kind);
4636 return NULL;
4639 /* Returns the parent of the declaration of DIE. */
4641 static dw_die_ref
4642 get_die_parent (dw_die_ref die)
4644 dw_die_ref t;
4646 if (!die)
4647 return NULL;
4649 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4650 || (t = get_AT_ref (die, DW_AT_specification)))
4651 die = t;
4653 return die->die_parent;
4656 /* Return the "low pc" attribute value, typically associated with a subprogram
4657 DIE. Return null if the "low pc" attribute is either not present, or if it
4658 cannot be represented as an assembler label identifier. */
4660 static inline const char *
4661 get_AT_low_pc (dw_die_ref die)
4663 dw_attr_ref a = get_AT (die, DW_AT_low_pc);
4665 return a ? AT_lbl (a) : NULL;
4668 /* Return the "high pc" attribute value, typically associated with a subprogram
4669 DIE. Return null if the "high pc" attribute is either not present, or if it
4670 cannot be represented as an assembler label identifier. */
4672 static inline const char *
4673 get_AT_hi_pc (dw_die_ref die)
4675 dw_attr_ref a = get_AT (die, DW_AT_high_pc);
4677 return a ? AT_lbl (a) : NULL;
4680 /* Return the value of the string attribute designated by ATTR_KIND, or
4681 NULL if it is not present. */
4683 static inline const char *
4684 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4686 dw_attr_ref a = get_AT (die, attr_kind);
4688 return a ? AT_string (a) : NULL;
4691 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4692 if it is not present. */
4694 static inline int
4695 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4697 dw_attr_ref a = get_AT (die, attr_kind);
4699 return a ? AT_flag (a) : 0;
4702 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4703 if it is not present. */
4705 static inline unsigned
4706 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4708 dw_attr_ref a = get_AT (die, attr_kind);
4710 return a ? AT_unsigned (a) : 0;
4713 static inline dw_die_ref
4714 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4716 dw_attr_ref a = get_AT (die, attr_kind);
4718 return a ? AT_ref (a) : NULL;
4721 static inline struct dwarf_file_data *
4722 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4724 dw_attr_ref a = get_AT (die, attr_kind);
4726 return a ? AT_file (a) : NULL;
4729 /* Return TRUE if the language is C++. */
4731 static inline bool
4732 is_cxx (void)
4734 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4736 return (lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus
4737 || lang == DW_LANG_C_plus_plus_11 || lang == DW_LANG_C_plus_plus_14);
4740 /* Return TRUE if the language is Java. */
4742 static inline bool
4743 is_java (void)
4745 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4747 return lang == DW_LANG_Java;
4750 /* Return TRUE if the language is Fortran. */
4752 static inline bool
4753 is_fortran (void)
4755 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4757 return (lang == DW_LANG_Fortran77
4758 || lang == DW_LANG_Fortran90
4759 || lang == DW_LANG_Fortran95
4760 || lang == DW_LANG_Fortran03
4761 || lang == DW_LANG_Fortran08);
4764 /* Return TRUE if the language is Ada. */
4766 static inline bool
4767 is_ada (void)
4769 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4771 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4774 /* Remove the specified attribute if present. Return TRUE if removal
4775 was successful. */
4777 static bool
4778 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4780 dw_attr_ref a;
4781 unsigned ix;
4783 if (! die)
4784 return false;
4786 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4787 if (a->dw_attr == attr_kind)
4789 if (AT_class (a) == dw_val_class_str)
4790 if (a->dw_attr_val.v.val_str->refcount)
4791 a->dw_attr_val.v.val_str->refcount--;
4793 /* vec::ordered_remove should help reduce the number of abbrevs
4794 that are needed. */
4795 die->die_attr->ordered_remove (ix);
4796 return true;
4798 return false;
4801 /* Remove CHILD from its parent. PREV must have the property that
4802 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
4804 static void
4805 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4807 gcc_assert (child->die_parent == prev->die_parent);
4808 gcc_assert (prev->die_sib == child);
4809 if (prev == child)
4811 gcc_assert (child->die_parent->die_child == child);
4812 prev = NULL;
4814 else
4815 prev->die_sib = child->die_sib;
4816 if (child->die_parent->die_child == child)
4817 child->die_parent->die_child = prev;
4820 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
4821 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
4823 static void
4824 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4826 dw_die_ref parent = old_child->die_parent;
4828 gcc_assert (parent == prev->die_parent);
4829 gcc_assert (prev->die_sib == old_child);
4831 new_child->die_parent = parent;
4832 if (prev == old_child)
4834 gcc_assert (parent->die_child == old_child);
4835 new_child->die_sib = new_child;
4837 else
4839 prev->die_sib = new_child;
4840 new_child->die_sib = old_child->die_sib;
4842 if (old_child->die_parent->die_child == old_child)
4843 old_child->die_parent->die_child = new_child;
4846 /* Move all children from OLD_PARENT to NEW_PARENT. */
4848 static void
4849 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4851 dw_die_ref c;
4852 new_parent->die_child = old_parent->die_child;
4853 old_parent->die_child = NULL;
4854 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4857 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
4858 matches TAG. */
4860 static void
4861 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4863 dw_die_ref c;
4865 c = die->die_child;
4866 if (c) do {
4867 dw_die_ref prev = c;
4868 c = c->die_sib;
4869 while (c->die_tag == tag)
4871 remove_child_with_prev (c, prev);
4872 c->die_parent = NULL;
4873 /* Might have removed every child. */
4874 if (c == c->die_sib)
4875 return;
4876 c = c->die_sib;
4878 } while (c != die->die_child);
4881 /* Add a CHILD_DIE as the last child of DIE. */
4883 static void
4884 add_child_die (dw_die_ref die, dw_die_ref child_die)
4886 /* FIXME this should probably be an assert. */
4887 if (! die || ! child_die)
4888 return;
4889 gcc_assert (die != child_die);
4891 child_die->die_parent = die;
4892 if (die->die_child)
4894 child_die->die_sib = die->die_child->die_sib;
4895 die->die_child->die_sib = child_die;
4897 else
4898 child_die->die_sib = child_die;
4899 die->die_child = child_die;
4902 /* Unassociate CHILD from its parent, and make its parent be
4903 NEW_PARENT. */
4905 static void
4906 reparent_child (dw_die_ref child, dw_die_ref new_parent)
4908 for (dw_die_ref p = child->die_parent->die_child; ; p = p->die_sib)
4909 if (p->die_sib == child)
4911 remove_child_with_prev (child, p);
4912 break;
4914 add_child_die (new_parent, child);
4917 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
4918 is the specification, to the end of PARENT's list of children.
4919 This is done by removing and re-adding it. */
4921 static void
4922 splice_child_die (dw_die_ref parent, dw_die_ref child)
4924 /* We want the declaration DIE from inside the class, not the
4925 specification DIE at toplevel. */
4926 if (child->die_parent != parent)
4928 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
4930 if (tmp)
4931 child = tmp;
4934 gcc_assert (child->die_parent == parent
4935 || (child->die_parent
4936 == get_AT_ref (parent, DW_AT_specification)));
4938 reparent_child (child, parent);
4941 /* Create and return a new die with a parent of PARENT_DIE. If
4942 PARENT_DIE is NULL, the new DIE is placed in limbo and an
4943 associated tree T must be supplied to determine parenthood
4944 later. */
4946 static inline dw_die_ref
4947 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
4949 dw_die_ref die = ggc_cleared_alloc<die_node> ();
4951 die->die_tag = tag_value;
4953 if (parent_die != NULL)
4954 add_child_die (parent_die, die);
4955 else
4957 limbo_die_node *limbo_node;
4959 /* No DIEs created after early dwarf should end up in limbo,
4960 because the limbo list should not persist past LTO
4961 streaming. */
4962 if (tag_value != DW_TAG_compile_unit
4963 /* These are allowed because they're generated while
4964 breaking out COMDAT units late. */
4965 && tag_value != DW_TAG_type_unit
4966 && !early_dwarf
4967 /* Allow nested functions to live in limbo because they will
4968 only temporarily live there, as decls_for_scope will fix
4969 them up. */
4970 && (TREE_CODE (t) != FUNCTION_DECL
4971 || !decl_function_context (t))
4972 /* Same as nested functions above but for types. Types that
4973 are local to a function will be fixed in
4974 decls_for_scope. */
4975 && (!RECORD_OR_UNION_TYPE_P (t)
4976 || !TYPE_CONTEXT (t)
4977 || TREE_CODE (TYPE_CONTEXT (t)) != FUNCTION_DECL)
4978 /* FIXME debug-early: Allow late limbo DIE creation for LTO,
4979 especially in the ltrans stage, but once we implement LTO
4980 dwarf streaming, we should remove this exception. */
4981 && !in_lto_p)
4983 fprintf (stderr, "symbol ended up in limbo too late:");
4984 debug_generic_stmt (t);
4985 gcc_unreachable ();
4988 limbo_node = ggc_cleared_alloc<limbo_die_node> ();
4989 limbo_node->die = die;
4990 limbo_node->created_for = t;
4991 limbo_node->next = limbo_die_list;
4992 limbo_die_list = limbo_node;
4995 return die;
4998 /* Return the DIE associated with the given type specifier. */
5000 static inline dw_die_ref
5001 lookup_type_die (tree type)
5003 return TYPE_SYMTAB_DIE (type);
5006 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
5007 anonymous type named by the typedef TYPE_DIE, return the DIE of the
5008 anonymous type instead the one of the naming typedef. */
5010 static inline dw_die_ref
5011 strip_naming_typedef (tree type, dw_die_ref type_die)
5013 if (type
5014 && TREE_CODE (type) == RECORD_TYPE
5015 && type_die
5016 && type_die->die_tag == DW_TAG_typedef
5017 && is_naming_typedef_decl (TYPE_NAME (type)))
5018 type_die = get_AT_ref (type_die, DW_AT_type);
5019 return type_die;
5022 /* Like lookup_type_die, but if type is an anonymous type named by a
5023 typedef[1], return the DIE of the anonymous type instead the one of
5024 the naming typedef. This is because in gen_typedef_die, we did
5025 equate the anonymous struct named by the typedef with the DIE of
5026 the naming typedef. So by default, lookup_type_die on an anonymous
5027 struct yields the DIE of the naming typedef.
5029 [1]: Read the comment of is_naming_typedef_decl to learn about what
5030 a naming typedef is. */
5032 static inline dw_die_ref
5033 lookup_type_die_strip_naming_typedef (tree type)
5035 dw_die_ref die = lookup_type_die (type);
5036 return strip_naming_typedef (type, die);
5039 /* Equate a DIE to a given type specifier. */
5041 static inline void
5042 equate_type_number_to_die (tree type, dw_die_ref type_die)
5044 TYPE_SYMTAB_DIE (type) = type_die;
5047 /* Returns a hash value for X (which really is a die_struct). */
5049 inline hashval_t
5050 decl_die_hasher::hash (die_node *x)
5052 return (hashval_t) x->decl_id;
5055 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
5057 inline bool
5058 decl_die_hasher::equal (die_node *x, tree y)
5060 return (x->decl_id == DECL_UID (y));
5063 /* Return the DIE associated with a given declaration. */
5065 static inline dw_die_ref
5066 lookup_decl_die (tree decl)
5068 return decl_die_table->find_with_hash (decl, DECL_UID (decl));
5071 /* Returns a hash value for X (which really is a var_loc_list). */
5073 inline hashval_t
5074 decl_loc_hasher::hash (var_loc_list *x)
5076 return (hashval_t) x->decl_id;
5079 /* Return nonzero if decl_id of var_loc_list X is the same as
5080 UID of decl *Y. */
5082 inline bool
5083 decl_loc_hasher::equal (var_loc_list *x, const_tree y)
5085 return (x->decl_id == DECL_UID (y));
5088 /* Return the var_loc list associated with a given declaration. */
5090 static inline var_loc_list *
5091 lookup_decl_loc (const_tree decl)
5093 if (!decl_loc_table)
5094 return NULL;
5095 return decl_loc_table->find_with_hash (decl, DECL_UID (decl));
5098 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
5100 inline hashval_t
5101 dw_loc_list_hasher::hash (cached_dw_loc_list *x)
5103 return (hashval_t) x->decl_id;
5106 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
5107 UID of decl *Y. */
5109 inline bool
5110 dw_loc_list_hasher::equal (cached_dw_loc_list *x, const_tree y)
5112 return (x->decl_id == DECL_UID (y));
5115 /* Equate a DIE to a particular declaration. */
5117 static void
5118 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5120 unsigned int decl_id = DECL_UID (decl);
5122 *decl_die_table->find_slot_with_hash (decl, decl_id, INSERT) = decl_die;
5123 decl_die->decl_id = decl_id;
5126 /* Return how many bits covers PIECE EXPR_LIST. */
5128 static HOST_WIDE_INT
5129 decl_piece_bitsize (rtx piece)
5131 int ret = (int) GET_MODE (piece);
5132 if (ret)
5133 return ret;
5134 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
5135 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
5136 return INTVAL (XEXP (XEXP (piece, 0), 0));
5139 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
5141 static rtx *
5142 decl_piece_varloc_ptr (rtx piece)
5144 if ((int) GET_MODE (piece))
5145 return &XEXP (piece, 0);
5146 else
5147 return &XEXP (XEXP (piece, 0), 1);
5150 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
5151 Next is the chain of following piece nodes. */
5153 static rtx_expr_list *
5154 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
5156 if (bitsize > 0 && bitsize <= (int) MAX_MACHINE_MODE)
5157 return alloc_EXPR_LIST (bitsize, loc_note, next);
5158 else
5159 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
5160 GEN_INT (bitsize),
5161 loc_note), next);
5164 /* Return rtx that should be stored into loc field for
5165 LOC_NOTE and BITPOS/BITSIZE. */
5167 static rtx
5168 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
5169 HOST_WIDE_INT bitsize)
5171 if (bitsize != -1)
5173 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
5174 if (bitpos != 0)
5175 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
5177 return loc_note;
5180 /* This function either modifies location piece list *DEST in
5181 place (if SRC and INNER is NULL), or copies location piece list
5182 *SRC to *DEST while modifying it. Location BITPOS is modified
5183 to contain LOC_NOTE, any pieces overlapping it are removed resp.
5184 not copied and if needed some padding around it is added.
5185 When modifying in place, DEST should point to EXPR_LIST where
5186 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
5187 to the start of the whole list and INNER points to the EXPR_LIST
5188 where earlier pieces cover PIECE_BITPOS bits. */
5190 static void
5191 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5192 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5193 HOST_WIDE_INT bitsize, rtx loc_note)
5195 HOST_WIDE_INT diff;
5196 bool copy = inner != NULL;
5198 if (copy)
5200 /* First copy all nodes preceding the current bitpos. */
5201 while (src != inner)
5203 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5204 decl_piece_bitsize (*src), NULL_RTX);
5205 dest = &XEXP (*dest, 1);
5206 src = &XEXP (*src, 1);
5209 /* Add padding if needed. */
5210 if (bitpos != piece_bitpos)
5212 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5213 copy ? NULL_RTX : *dest);
5214 dest = &XEXP (*dest, 1);
5216 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5218 gcc_assert (!copy);
5219 /* A piece with correct bitpos and bitsize already exist,
5220 just update the location for it and return. */
5221 *decl_piece_varloc_ptr (*dest) = loc_note;
5222 return;
5224 /* Add the piece that changed. */
5225 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5226 dest = &XEXP (*dest, 1);
5227 /* Skip over pieces that overlap it. */
5228 diff = bitpos - piece_bitpos + bitsize;
5229 if (!copy)
5230 src = dest;
5231 while (diff > 0 && *src)
5233 rtx piece = *src;
5234 diff -= decl_piece_bitsize (piece);
5235 if (copy)
5236 src = &XEXP (piece, 1);
5237 else
5239 *src = XEXP (piece, 1);
5240 free_EXPR_LIST_node (piece);
5243 /* Add padding if needed. */
5244 if (diff < 0 && *src)
5246 if (!copy)
5247 dest = src;
5248 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5249 dest = &XEXP (*dest, 1);
5251 if (!copy)
5252 return;
5253 /* Finally copy all nodes following it. */
5254 while (*src)
5256 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5257 decl_piece_bitsize (*src), NULL_RTX);
5258 dest = &XEXP (*dest, 1);
5259 src = &XEXP (*src, 1);
5263 /* Add a variable location node to the linked list for DECL. */
5265 static struct var_loc_node *
5266 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5268 unsigned int decl_id;
5269 var_loc_list *temp;
5270 struct var_loc_node *loc = NULL;
5271 HOST_WIDE_INT bitsize = -1, bitpos = -1;
5273 if (TREE_CODE (decl) == VAR_DECL
5274 && DECL_HAS_DEBUG_EXPR_P (decl))
5276 tree realdecl = DECL_DEBUG_EXPR (decl);
5277 if (handled_component_p (realdecl)
5278 || (TREE_CODE (realdecl) == MEM_REF
5279 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5281 HOST_WIDE_INT maxsize;
5282 tree innerdecl;
5283 innerdecl
5284 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
5285 if (!DECL_P (innerdecl)
5286 || DECL_IGNORED_P (innerdecl)
5287 || TREE_STATIC (innerdecl)
5288 || bitsize <= 0
5289 || bitpos + bitsize > 256
5290 || bitsize != maxsize)
5291 return NULL;
5292 decl = innerdecl;
5296 decl_id = DECL_UID (decl);
5297 var_loc_list **slot
5298 = decl_loc_table->find_slot_with_hash (decl, decl_id, INSERT);
5299 if (*slot == NULL)
5301 temp = ggc_cleared_alloc<var_loc_list> ();
5302 temp->decl_id = decl_id;
5303 *slot = temp;
5305 else
5306 temp = *slot;
5308 /* For PARM_DECLs try to keep around the original incoming value,
5309 even if that means we'll emit a zero-range .debug_loc entry. */
5310 if (temp->last
5311 && temp->first == temp->last
5312 && TREE_CODE (decl) == PARM_DECL
5313 && NOTE_P (temp->first->loc)
5314 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5315 && DECL_INCOMING_RTL (decl)
5316 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5317 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5318 == GET_CODE (DECL_INCOMING_RTL (decl))
5319 && prev_real_insn (temp->first->loc) == NULL_RTX
5320 && (bitsize != -1
5321 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5322 NOTE_VAR_LOCATION_LOC (loc_note))
5323 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5324 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5326 loc = ggc_cleared_alloc<var_loc_node> ();
5327 temp->first->next = loc;
5328 temp->last = loc;
5329 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5331 else if (temp->last)
5333 struct var_loc_node *last = temp->last, *unused = NULL;
5334 rtx *piece_loc = NULL, last_loc_note;
5335 HOST_WIDE_INT piece_bitpos = 0;
5336 if (last->next)
5338 last = last->next;
5339 gcc_assert (last->next == NULL);
5341 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5343 piece_loc = &last->loc;
5346 HOST_WIDE_INT cur_bitsize = decl_piece_bitsize (*piece_loc);
5347 if (piece_bitpos + cur_bitsize > bitpos)
5348 break;
5349 piece_bitpos += cur_bitsize;
5350 piece_loc = &XEXP (*piece_loc, 1);
5352 while (*piece_loc);
5354 /* TEMP->LAST here is either pointer to the last but one or
5355 last element in the chained list, LAST is pointer to the
5356 last element. */
5357 if (label && strcmp (last->label, label) == 0)
5359 /* For SRA optimized variables if there weren't any real
5360 insns since last note, just modify the last node. */
5361 if (piece_loc != NULL)
5363 adjust_piece_list (piece_loc, NULL, NULL,
5364 bitpos, piece_bitpos, bitsize, loc_note);
5365 return NULL;
5367 /* If the last note doesn't cover any instructions, remove it. */
5368 if (temp->last != last)
5370 temp->last->next = NULL;
5371 unused = last;
5372 last = temp->last;
5373 gcc_assert (strcmp (last->label, label) != 0);
5375 else
5377 gcc_assert (temp->first == temp->last
5378 || (temp->first->next == temp->last
5379 && TREE_CODE (decl) == PARM_DECL));
5380 memset (temp->last, '\0', sizeof (*temp->last));
5381 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5382 return temp->last;
5385 if (bitsize == -1 && NOTE_P (last->loc))
5386 last_loc_note = last->loc;
5387 else if (piece_loc != NULL
5388 && *piece_loc != NULL_RTX
5389 && piece_bitpos == bitpos
5390 && decl_piece_bitsize (*piece_loc) == bitsize)
5391 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5392 else
5393 last_loc_note = NULL_RTX;
5394 /* If the current location is the same as the end of the list,
5395 and either both or neither of the locations is uninitialized,
5396 we have nothing to do. */
5397 if (last_loc_note == NULL_RTX
5398 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5399 NOTE_VAR_LOCATION_LOC (loc_note)))
5400 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5401 != NOTE_VAR_LOCATION_STATUS (loc_note))
5402 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5403 == VAR_INIT_STATUS_UNINITIALIZED)
5404 || (NOTE_VAR_LOCATION_STATUS (loc_note)
5405 == VAR_INIT_STATUS_UNINITIALIZED))))
5407 /* Add LOC to the end of list and update LAST. If the last
5408 element of the list has been removed above, reuse its
5409 memory for the new node, otherwise allocate a new one. */
5410 if (unused)
5412 loc = unused;
5413 memset (loc, '\0', sizeof (*loc));
5415 else
5416 loc = ggc_cleared_alloc<var_loc_node> ();
5417 if (bitsize == -1 || piece_loc == NULL)
5418 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5419 else
5420 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5421 bitpos, piece_bitpos, bitsize, loc_note);
5422 last->next = loc;
5423 /* Ensure TEMP->LAST will point either to the new last but one
5424 element of the chain, or to the last element in it. */
5425 if (last != temp->last)
5426 temp->last = last;
5428 else if (unused)
5429 ggc_free (unused);
5431 else
5433 loc = ggc_cleared_alloc<var_loc_node> ();
5434 temp->first = loc;
5435 temp->last = loc;
5436 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5438 return loc;
5441 /* Keep track of the number of spaces used to indent the
5442 output of the debugging routines that print the structure of
5443 the DIE internal representation. */
5444 static int print_indent;
5446 /* Indent the line the number of spaces given by print_indent. */
5448 static inline void
5449 print_spaces (FILE *outfile)
5451 fprintf (outfile, "%*s", print_indent, "");
5454 /* Print a type signature in hex. */
5456 static inline void
5457 print_signature (FILE *outfile, char *sig)
5459 int i;
5461 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5462 fprintf (outfile, "%02x", sig[i] & 0xff);
5465 static void print_loc_descr (dw_loc_descr_ref, FILE *);
5467 /* Print the value associated to the VAL DWARF value node to OUTFILE. If
5468 RECURSE, output location descriptor operations. */
5470 static void
5471 print_dw_val (dw_val_node *val, bool recurse, FILE *outfile)
5473 switch (val->val_class)
5475 case dw_val_class_addr:
5476 fprintf (outfile, "address");
5477 break;
5478 case dw_val_class_offset:
5479 fprintf (outfile, "offset");
5480 break;
5481 case dw_val_class_loc:
5482 fprintf (outfile, "location descriptor");
5483 if (val->v.val_loc == NULL)
5484 fprintf (outfile, " -> <null>\n");
5485 else if (recurse)
5487 fprintf (outfile, ":\n");
5488 print_indent += 4;
5489 print_loc_descr (val->v.val_loc, outfile);
5490 print_indent -= 4;
5492 else
5493 fprintf (outfile, " (%p)\n", (void *) val->v.val_loc);
5494 break;
5495 case dw_val_class_loc_list:
5496 fprintf (outfile, "location list -> label:%s",
5497 val->v.val_loc_list->ll_symbol);
5498 break;
5499 case dw_val_class_range_list:
5500 fprintf (outfile, "range list");
5501 break;
5502 case dw_val_class_const:
5503 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, val->v.val_int);
5504 break;
5505 case dw_val_class_unsigned_const:
5506 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, val->v.val_unsigned);
5507 break;
5508 case dw_val_class_const_double:
5509 fprintf (outfile, "constant (" HOST_WIDE_INT_PRINT_DEC","\
5510 HOST_WIDE_INT_PRINT_UNSIGNED")",
5511 val->v.val_double.high,
5512 val->v.val_double.low);
5513 break;
5514 case dw_val_class_wide_int:
5516 int i = val->v.val_wide->get_len ();
5517 fprintf (outfile, "constant (");
5518 gcc_assert (i > 0);
5519 if (val->v.val_wide->elt (i - 1) == 0)
5520 fprintf (outfile, "0x");
5521 fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
5522 val->v.val_wide->elt (--i));
5523 while (--i >= 0)
5524 fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
5525 val->v.val_wide->elt (i));
5526 fprintf (outfile, ")");
5527 break;
5529 case dw_val_class_vec:
5530 fprintf (outfile, "floating-point or vector constant");
5531 break;
5532 case dw_val_class_flag:
5533 fprintf (outfile, "%u", val->v.val_flag);
5534 break;
5535 case dw_val_class_die_ref:
5536 if (val->v.val_die_ref.die != NULL)
5538 dw_die_ref die = val->v.val_die_ref.die;
5540 if (die->comdat_type_p)
5542 fprintf (outfile, "die -> signature: ");
5543 print_signature (outfile,
5544 die->die_id.die_type_node->signature);
5546 else if (die->die_id.die_symbol)
5547 fprintf (outfile, "die -> label: %s", die->die_id.die_symbol);
5548 else
5549 fprintf (outfile, "die -> %ld", die->die_offset);
5550 fprintf (outfile, " (%p)", (void *) die);
5552 else
5553 fprintf (outfile, "die -> <null>");
5554 break;
5555 case dw_val_class_vms_delta:
5556 fprintf (outfile, "delta: @slotcount(%s-%s)",
5557 val->v.val_vms_delta.lbl2, val->v.val_vms_delta.lbl1);
5558 break;
5559 case dw_val_class_lbl_id:
5560 case dw_val_class_lineptr:
5561 case dw_val_class_macptr:
5562 case dw_val_class_high_pc:
5563 fprintf (outfile, "label: %s", val->v.val_lbl_id);
5564 break;
5565 case dw_val_class_str:
5566 if (val->v.val_str->str != NULL)
5567 fprintf (outfile, "\"%s\"", val->v.val_str->str);
5568 else
5569 fprintf (outfile, "<null>");
5570 break;
5571 case dw_val_class_file:
5572 fprintf (outfile, "\"%s\" (%d)", val->v.val_file->filename,
5573 val->v.val_file->emitted_number);
5574 break;
5575 case dw_val_class_data8:
5577 int i;
5579 for (i = 0; i < 8; i++)
5580 fprintf (outfile, "%02x", val->v.val_data8[i]);
5581 break;
5583 default:
5584 break;
5588 /* Likewise, for a DIE attribute. */
5590 static void
5591 print_attribute (dw_attr_ref a, bool recurse, FILE *outfile)
5593 print_dw_val (&a->dw_attr_val, recurse, outfile);
5597 /* Print the list of operands in the LOC location description to OUTFILE. This
5598 routine is a debugging aid only. */
5600 static void
5601 print_loc_descr (dw_loc_descr_ref loc, FILE *outfile)
5603 dw_loc_descr_ref l = loc;
5605 if (loc == NULL)
5607 print_spaces (outfile);
5608 fprintf (outfile, "<null>\n");
5609 return;
5612 for (l = loc; l != NULL; l = l->dw_loc_next)
5614 print_spaces (outfile);
5615 fprintf (outfile, "(%p) %s",
5616 (void *) l,
5617 dwarf_stack_op_name (l->dw_loc_opc));
5618 if (l->dw_loc_oprnd1.val_class != dw_val_class_none)
5620 fprintf (outfile, " ");
5621 print_dw_val (&l->dw_loc_oprnd1, false, outfile);
5623 if (l->dw_loc_oprnd2.val_class != dw_val_class_none)
5625 fprintf (outfile, ", ");
5626 print_dw_val (&l->dw_loc_oprnd2, false, outfile);
5628 fprintf (outfile, "\n");
5632 /* Print the information associated with a given DIE, and its children.
5633 This routine is a debugging aid only. */
5635 static void
5636 print_die (dw_die_ref die, FILE *outfile)
5638 dw_attr_ref a;
5639 dw_die_ref c;
5640 unsigned ix;
5642 print_spaces (outfile);
5643 fprintf (outfile, "DIE %4ld: %s (%p)\n",
5644 die->die_offset, dwarf_tag_name (die->die_tag),
5645 (void*) die);
5646 print_spaces (outfile);
5647 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5648 fprintf (outfile, " offset: %ld", die->die_offset);
5649 fprintf (outfile, " mark: %d\n", die->die_mark);
5651 if (die->comdat_type_p)
5653 print_spaces (outfile);
5654 fprintf (outfile, " signature: ");
5655 print_signature (outfile, die->die_id.die_type_node->signature);
5656 fprintf (outfile, "\n");
5659 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5661 print_spaces (outfile);
5662 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5664 print_attribute (a, true, outfile);
5665 fprintf (outfile, "\n");
5668 if (die->die_child != NULL)
5670 print_indent += 4;
5671 FOR_EACH_CHILD (die, c, print_die (c, outfile));
5672 print_indent -= 4;
5674 if (print_indent == 0)
5675 fprintf (outfile, "\n");
5678 /* Print the list of operations in the LOC location description. */
5680 DEBUG_FUNCTION void
5681 debug_dwarf_loc_descr (dw_loc_descr_ref loc)
5683 print_loc_descr (loc, stderr);
5686 /* Print the information collected for a given DIE. */
5688 DEBUG_FUNCTION void
5689 debug_dwarf_die (dw_die_ref die)
5691 print_die (die, stderr);
5694 DEBUG_FUNCTION void
5695 debug (die_struct &ref)
5697 print_die (&ref, stderr);
5700 DEBUG_FUNCTION void
5701 debug (die_struct *ptr)
5703 if (ptr)
5704 debug (*ptr);
5705 else
5706 fprintf (stderr, "<nil>\n");
5710 /* Print all DWARF information collected for the compilation unit.
5711 This routine is a debugging aid only. */
5713 DEBUG_FUNCTION void
5714 debug_dwarf (void)
5716 print_indent = 0;
5717 print_die (comp_unit_die (), stderr);
5720 #ifdef ENABLE_CHECKING
5721 /* Sanity checks on DIEs. */
5723 static void
5724 check_die (dw_die_ref die)
5726 unsigned ix;
5727 dw_attr_ref a;
5728 bool inline_found = false;
5729 int n_location = 0, n_low_pc = 0, n_high_pc = 0, n_artificial = 0;
5730 int n_decl_line = 0, n_decl_file = 0;
5731 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5733 switch (a->dw_attr)
5735 case DW_AT_inline:
5736 if (a->dw_attr_val.v.val_unsigned)
5737 inline_found = true;
5738 break;
5739 case DW_AT_location:
5740 ++n_location;
5741 break;
5742 case DW_AT_low_pc:
5743 ++n_low_pc;
5744 break;
5745 case DW_AT_high_pc:
5746 ++n_high_pc;
5747 break;
5748 case DW_AT_artificial:
5749 ++n_artificial;
5750 break;
5751 case DW_AT_decl_line:
5752 ++n_decl_line;
5753 break;
5754 case DW_AT_decl_file:
5755 ++n_decl_file;
5756 break;
5757 default:
5758 break;
5761 if (n_location > 1 || n_low_pc > 1 || n_high_pc > 1 || n_artificial > 1
5762 || n_decl_line > 1 || n_decl_file > 1)
5764 fprintf (stderr, "Duplicate attributes in DIE:\n");
5765 debug_dwarf_die (die);
5766 gcc_unreachable ();
5768 if (inline_found)
5770 /* A debugging information entry that is a member of an abstract
5771 instance tree [that has DW_AT_inline] should not contain any
5772 attributes which describe aspects of the subroutine which vary
5773 between distinct inlined expansions or distinct out-of-line
5774 expansions. */
5775 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5776 gcc_assert (a->dw_attr != DW_AT_low_pc
5777 && a->dw_attr != DW_AT_high_pc
5778 && a->dw_attr != DW_AT_location
5779 && a->dw_attr != DW_AT_frame_base
5780 && a->dw_attr != DW_AT_GNU_all_call_sites);
5783 #endif
5785 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
5786 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
5787 DIE that marks the start of the DIEs for this include file. */
5789 static dw_die_ref
5790 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5792 const char *filename = get_AT_string (bincl_die, DW_AT_name);
5793 dw_die_ref new_unit = gen_compile_unit_die (filename);
5795 new_unit->die_sib = old_unit;
5796 return new_unit;
5799 /* Close an include-file CU and reopen the enclosing one. */
5801 static dw_die_ref
5802 pop_compile_unit (dw_die_ref old_unit)
5804 dw_die_ref new_unit = old_unit->die_sib;
5806 old_unit->die_sib = NULL;
5807 return new_unit;
5810 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5811 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5812 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5814 /* Calculate the checksum of a location expression. */
5816 static inline void
5817 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5819 int tem;
5820 inchash::hash hstate;
5821 hashval_t hash;
5823 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
5824 CHECKSUM (tem);
5825 hash_loc_operands (loc, hstate);
5826 hash = hstate.end();
5827 CHECKSUM (hash);
5830 /* Calculate the checksum of an attribute. */
5832 static void
5833 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5835 dw_loc_descr_ref loc;
5836 rtx r;
5838 CHECKSUM (at->dw_attr);
5840 /* We don't care that this was compiled with a different compiler
5841 snapshot; if the output is the same, that's what matters. */
5842 if (at->dw_attr == DW_AT_producer)
5843 return;
5845 switch (AT_class (at))
5847 case dw_val_class_const:
5848 CHECKSUM (at->dw_attr_val.v.val_int);
5849 break;
5850 case dw_val_class_unsigned_const:
5851 CHECKSUM (at->dw_attr_val.v.val_unsigned);
5852 break;
5853 case dw_val_class_const_double:
5854 CHECKSUM (at->dw_attr_val.v.val_double);
5855 break;
5856 case dw_val_class_wide_int:
5857 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
5858 get_full_len (*at->dw_attr_val.v.val_wide)
5859 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
5860 break;
5861 case dw_val_class_vec:
5862 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5863 (at->dw_attr_val.v.val_vec.length
5864 * at->dw_attr_val.v.val_vec.elt_size));
5865 break;
5866 case dw_val_class_flag:
5867 CHECKSUM (at->dw_attr_val.v.val_flag);
5868 break;
5869 case dw_val_class_str:
5870 CHECKSUM_STRING (AT_string (at));
5871 break;
5873 case dw_val_class_addr:
5874 r = AT_addr (at);
5875 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5876 CHECKSUM_STRING (XSTR (r, 0));
5877 break;
5879 case dw_val_class_offset:
5880 CHECKSUM (at->dw_attr_val.v.val_offset);
5881 break;
5883 case dw_val_class_loc:
5884 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5885 loc_checksum (loc, ctx);
5886 break;
5888 case dw_val_class_die_ref:
5889 die_checksum (AT_ref (at), ctx, mark);
5890 break;
5892 case dw_val_class_fde_ref:
5893 case dw_val_class_vms_delta:
5894 case dw_val_class_lbl_id:
5895 case dw_val_class_lineptr:
5896 case dw_val_class_macptr:
5897 case dw_val_class_high_pc:
5898 break;
5900 case dw_val_class_file:
5901 CHECKSUM_STRING (AT_file (at)->filename);
5902 break;
5904 case dw_val_class_data8:
5905 CHECKSUM (at->dw_attr_val.v.val_data8);
5906 break;
5908 default:
5909 break;
5913 /* Calculate the checksum of a DIE. */
5915 static void
5916 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5918 dw_die_ref c;
5919 dw_attr_ref a;
5920 unsigned ix;
5922 /* To avoid infinite recursion. */
5923 if (die->die_mark)
5925 CHECKSUM (die->die_mark);
5926 return;
5928 die->die_mark = ++(*mark);
5930 CHECKSUM (die->die_tag);
5932 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5933 attr_checksum (a, ctx, mark);
5935 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
5938 #undef CHECKSUM
5939 #undef CHECKSUM_BLOCK
5940 #undef CHECKSUM_STRING
5942 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
5943 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5944 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5945 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
5946 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
5947 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
5948 #define CHECKSUM_ATTR(FOO) \
5949 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
5951 /* Calculate the checksum of a number in signed LEB128 format. */
5953 static void
5954 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
5956 unsigned char byte;
5957 bool more;
5959 while (1)
5961 byte = (value & 0x7f);
5962 value >>= 7;
5963 more = !((value == 0 && (byte & 0x40) == 0)
5964 || (value == -1 && (byte & 0x40) != 0));
5965 if (more)
5966 byte |= 0x80;
5967 CHECKSUM (byte);
5968 if (!more)
5969 break;
5973 /* Calculate the checksum of a number in unsigned LEB128 format. */
5975 static void
5976 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
5978 while (1)
5980 unsigned char byte = (value & 0x7f);
5981 value >>= 7;
5982 if (value != 0)
5983 /* More bytes to follow. */
5984 byte |= 0x80;
5985 CHECKSUM (byte);
5986 if (value == 0)
5987 break;
5991 /* Checksum the context of the DIE. This adds the names of any
5992 surrounding namespaces or structures to the checksum. */
5994 static void
5995 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
5997 const char *name;
5998 dw_die_ref spec;
5999 int tag = die->die_tag;
6001 if (tag != DW_TAG_namespace
6002 && tag != DW_TAG_structure_type
6003 && tag != DW_TAG_class_type)
6004 return;
6006 name = get_AT_string (die, DW_AT_name);
6008 spec = get_AT_ref (die, DW_AT_specification);
6009 if (spec != NULL)
6010 die = spec;
6012 if (die->die_parent != NULL)
6013 checksum_die_context (die->die_parent, ctx);
6015 CHECKSUM_ULEB128 ('C');
6016 CHECKSUM_ULEB128 (tag);
6017 if (name != NULL)
6018 CHECKSUM_STRING (name);
6021 /* Calculate the checksum of a location expression. */
6023 static inline void
6024 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6026 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
6027 were emitted as a DW_FORM_sdata instead of a location expression. */
6028 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
6030 CHECKSUM_ULEB128 (DW_FORM_sdata);
6031 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
6032 return;
6035 /* Otherwise, just checksum the raw location expression. */
6036 while (loc != NULL)
6038 inchash::hash hstate;
6039 hashval_t hash;
6041 CHECKSUM_ULEB128 (loc->dtprel);
6042 CHECKSUM_ULEB128 (loc->dw_loc_opc);
6043 hash_loc_operands (loc, hstate);
6044 hash = hstate.end ();
6045 CHECKSUM (hash);
6046 loc = loc->dw_loc_next;
6050 /* Calculate the checksum of an attribute. */
6052 static void
6053 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
6054 struct md5_ctx *ctx, int *mark)
6056 dw_loc_descr_ref loc;
6057 rtx r;
6059 if (AT_class (at) == dw_val_class_die_ref)
6061 dw_die_ref target_die = AT_ref (at);
6063 /* For pointer and reference types, we checksum only the (qualified)
6064 name of the target type (if there is a name). For friend entries,
6065 we checksum only the (qualified) name of the target type or function.
6066 This allows the checksum to remain the same whether the target type
6067 is complete or not. */
6068 if ((at->dw_attr == DW_AT_type
6069 && (tag == DW_TAG_pointer_type
6070 || tag == DW_TAG_reference_type
6071 || tag == DW_TAG_rvalue_reference_type
6072 || tag == DW_TAG_ptr_to_member_type))
6073 || (at->dw_attr == DW_AT_friend
6074 && tag == DW_TAG_friend))
6076 dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
6078 if (name_attr != NULL)
6080 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6082 if (decl == NULL)
6083 decl = target_die;
6084 CHECKSUM_ULEB128 ('N');
6085 CHECKSUM_ULEB128 (at->dw_attr);
6086 if (decl->die_parent != NULL)
6087 checksum_die_context (decl->die_parent, ctx);
6088 CHECKSUM_ULEB128 ('E');
6089 CHECKSUM_STRING (AT_string (name_attr));
6090 return;
6094 /* For all other references to another DIE, we check to see if the
6095 target DIE has already been visited. If it has, we emit a
6096 backward reference; if not, we descend recursively. */
6097 if (target_die->die_mark > 0)
6099 CHECKSUM_ULEB128 ('R');
6100 CHECKSUM_ULEB128 (at->dw_attr);
6101 CHECKSUM_ULEB128 (target_die->die_mark);
6103 else
6105 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6107 if (decl == NULL)
6108 decl = target_die;
6109 target_die->die_mark = ++(*mark);
6110 CHECKSUM_ULEB128 ('T');
6111 CHECKSUM_ULEB128 (at->dw_attr);
6112 if (decl->die_parent != NULL)
6113 checksum_die_context (decl->die_parent, ctx);
6114 die_checksum_ordered (target_die, ctx, mark);
6116 return;
6119 CHECKSUM_ULEB128 ('A');
6120 CHECKSUM_ULEB128 (at->dw_attr);
6122 switch (AT_class (at))
6124 case dw_val_class_const:
6125 CHECKSUM_ULEB128 (DW_FORM_sdata);
6126 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
6127 break;
6129 case dw_val_class_unsigned_const:
6130 CHECKSUM_ULEB128 (DW_FORM_sdata);
6131 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
6132 break;
6134 case dw_val_class_const_double:
6135 CHECKSUM_ULEB128 (DW_FORM_block);
6136 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
6137 CHECKSUM (at->dw_attr_val.v.val_double);
6138 break;
6140 case dw_val_class_wide_int:
6141 CHECKSUM_ULEB128 (DW_FORM_block);
6142 CHECKSUM_ULEB128 (get_full_len (*at->dw_attr_val.v.val_wide)
6143 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
6144 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6145 get_full_len (*at->dw_attr_val.v.val_wide)
6146 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6147 break;
6149 case dw_val_class_vec:
6150 CHECKSUM_ULEB128 (DW_FORM_block);
6151 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
6152 * at->dw_attr_val.v.val_vec.elt_size);
6153 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6154 (at->dw_attr_val.v.val_vec.length
6155 * at->dw_attr_val.v.val_vec.elt_size));
6156 break;
6158 case dw_val_class_flag:
6159 CHECKSUM_ULEB128 (DW_FORM_flag);
6160 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
6161 break;
6163 case dw_val_class_str:
6164 CHECKSUM_ULEB128 (DW_FORM_string);
6165 CHECKSUM_STRING (AT_string (at));
6166 break;
6168 case dw_val_class_addr:
6169 r = AT_addr (at);
6170 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6171 CHECKSUM_ULEB128 (DW_FORM_string);
6172 CHECKSUM_STRING (XSTR (r, 0));
6173 break;
6175 case dw_val_class_offset:
6176 CHECKSUM_ULEB128 (DW_FORM_sdata);
6177 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
6178 break;
6180 case dw_val_class_loc:
6181 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6182 loc_checksum_ordered (loc, ctx);
6183 break;
6185 case dw_val_class_fde_ref:
6186 case dw_val_class_lbl_id:
6187 case dw_val_class_lineptr:
6188 case dw_val_class_macptr:
6189 case dw_val_class_high_pc:
6190 break;
6192 case dw_val_class_file:
6193 CHECKSUM_ULEB128 (DW_FORM_string);
6194 CHECKSUM_STRING (AT_file (at)->filename);
6195 break;
6197 case dw_val_class_data8:
6198 CHECKSUM (at->dw_attr_val.v.val_data8);
6199 break;
6201 default:
6202 break;
6206 struct checksum_attributes
6208 dw_attr_ref at_name;
6209 dw_attr_ref at_type;
6210 dw_attr_ref at_friend;
6211 dw_attr_ref at_accessibility;
6212 dw_attr_ref at_address_class;
6213 dw_attr_ref at_allocated;
6214 dw_attr_ref at_artificial;
6215 dw_attr_ref at_associated;
6216 dw_attr_ref at_binary_scale;
6217 dw_attr_ref at_bit_offset;
6218 dw_attr_ref at_bit_size;
6219 dw_attr_ref at_bit_stride;
6220 dw_attr_ref at_byte_size;
6221 dw_attr_ref at_byte_stride;
6222 dw_attr_ref at_const_value;
6223 dw_attr_ref at_containing_type;
6224 dw_attr_ref at_count;
6225 dw_attr_ref at_data_location;
6226 dw_attr_ref at_data_member_location;
6227 dw_attr_ref at_decimal_scale;
6228 dw_attr_ref at_decimal_sign;
6229 dw_attr_ref at_default_value;
6230 dw_attr_ref at_digit_count;
6231 dw_attr_ref at_discr;
6232 dw_attr_ref at_discr_list;
6233 dw_attr_ref at_discr_value;
6234 dw_attr_ref at_encoding;
6235 dw_attr_ref at_endianity;
6236 dw_attr_ref at_explicit;
6237 dw_attr_ref at_is_optional;
6238 dw_attr_ref at_location;
6239 dw_attr_ref at_lower_bound;
6240 dw_attr_ref at_mutable;
6241 dw_attr_ref at_ordering;
6242 dw_attr_ref at_picture_string;
6243 dw_attr_ref at_prototyped;
6244 dw_attr_ref at_small;
6245 dw_attr_ref at_segment;
6246 dw_attr_ref at_string_length;
6247 dw_attr_ref at_threads_scaled;
6248 dw_attr_ref at_upper_bound;
6249 dw_attr_ref at_use_location;
6250 dw_attr_ref at_use_UTF8;
6251 dw_attr_ref at_variable_parameter;
6252 dw_attr_ref at_virtuality;
6253 dw_attr_ref at_visibility;
6254 dw_attr_ref at_vtable_elem_location;
6257 /* Collect the attributes that we will want to use for the checksum. */
6259 static void
6260 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
6262 dw_attr_ref a;
6263 unsigned ix;
6265 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6267 switch (a->dw_attr)
6269 case DW_AT_name:
6270 attrs->at_name = a;
6271 break;
6272 case DW_AT_type:
6273 attrs->at_type = a;
6274 break;
6275 case DW_AT_friend:
6276 attrs->at_friend = a;
6277 break;
6278 case DW_AT_accessibility:
6279 attrs->at_accessibility = a;
6280 break;
6281 case DW_AT_address_class:
6282 attrs->at_address_class = a;
6283 break;
6284 case DW_AT_allocated:
6285 attrs->at_allocated = a;
6286 break;
6287 case DW_AT_artificial:
6288 attrs->at_artificial = a;
6289 break;
6290 case DW_AT_associated:
6291 attrs->at_associated = a;
6292 break;
6293 case DW_AT_binary_scale:
6294 attrs->at_binary_scale = a;
6295 break;
6296 case DW_AT_bit_offset:
6297 attrs->at_bit_offset = a;
6298 break;
6299 case DW_AT_bit_size:
6300 attrs->at_bit_size = a;
6301 break;
6302 case DW_AT_bit_stride:
6303 attrs->at_bit_stride = a;
6304 break;
6305 case DW_AT_byte_size:
6306 attrs->at_byte_size = a;
6307 break;
6308 case DW_AT_byte_stride:
6309 attrs->at_byte_stride = a;
6310 break;
6311 case DW_AT_const_value:
6312 attrs->at_const_value = a;
6313 break;
6314 case DW_AT_containing_type:
6315 attrs->at_containing_type = a;
6316 break;
6317 case DW_AT_count:
6318 attrs->at_count = a;
6319 break;
6320 case DW_AT_data_location:
6321 attrs->at_data_location = a;
6322 break;
6323 case DW_AT_data_member_location:
6324 attrs->at_data_member_location = a;
6325 break;
6326 case DW_AT_decimal_scale:
6327 attrs->at_decimal_scale = a;
6328 break;
6329 case DW_AT_decimal_sign:
6330 attrs->at_decimal_sign = a;
6331 break;
6332 case DW_AT_default_value:
6333 attrs->at_default_value = a;
6334 break;
6335 case DW_AT_digit_count:
6336 attrs->at_digit_count = a;
6337 break;
6338 case DW_AT_discr:
6339 attrs->at_discr = a;
6340 break;
6341 case DW_AT_discr_list:
6342 attrs->at_discr_list = a;
6343 break;
6344 case DW_AT_discr_value:
6345 attrs->at_discr_value = a;
6346 break;
6347 case DW_AT_encoding:
6348 attrs->at_encoding = a;
6349 break;
6350 case DW_AT_endianity:
6351 attrs->at_endianity = a;
6352 break;
6353 case DW_AT_explicit:
6354 attrs->at_explicit = a;
6355 break;
6356 case DW_AT_is_optional:
6357 attrs->at_is_optional = a;
6358 break;
6359 case DW_AT_location:
6360 attrs->at_location = a;
6361 break;
6362 case DW_AT_lower_bound:
6363 attrs->at_lower_bound = a;
6364 break;
6365 case DW_AT_mutable:
6366 attrs->at_mutable = a;
6367 break;
6368 case DW_AT_ordering:
6369 attrs->at_ordering = a;
6370 break;
6371 case DW_AT_picture_string:
6372 attrs->at_picture_string = a;
6373 break;
6374 case DW_AT_prototyped:
6375 attrs->at_prototyped = a;
6376 break;
6377 case DW_AT_small:
6378 attrs->at_small = a;
6379 break;
6380 case DW_AT_segment:
6381 attrs->at_segment = a;
6382 break;
6383 case DW_AT_string_length:
6384 attrs->at_string_length = a;
6385 break;
6386 case DW_AT_threads_scaled:
6387 attrs->at_threads_scaled = a;
6388 break;
6389 case DW_AT_upper_bound:
6390 attrs->at_upper_bound = a;
6391 break;
6392 case DW_AT_use_location:
6393 attrs->at_use_location = a;
6394 break;
6395 case DW_AT_use_UTF8:
6396 attrs->at_use_UTF8 = a;
6397 break;
6398 case DW_AT_variable_parameter:
6399 attrs->at_variable_parameter = a;
6400 break;
6401 case DW_AT_virtuality:
6402 attrs->at_virtuality = a;
6403 break;
6404 case DW_AT_visibility:
6405 attrs->at_visibility = a;
6406 break;
6407 case DW_AT_vtable_elem_location:
6408 attrs->at_vtable_elem_location = a;
6409 break;
6410 default:
6411 break;
6416 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
6418 static void
6419 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6421 dw_die_ref c;
6422 dw_die_ref decl;
6423 struct checksum_attributes attrs;
6425 CHECKSUM_ULEB128 ('D');
6426 CHECKSUM_ULEB128 (die->die_tag);
6428 memset (&attrs, 0, sizeof (attrs));
6430 decl = get_AT_ref (die, DW_AT_specification);
6431 if (decl != NULL)
6432 collect_checksum_attributes (&attrs, decl);
6433 collect_checksum_attributes (&attrs, die);
6435 CHECKSUM_ATTR (attrs.at_name);
6436 CHECKSUM_ATTR (attrs.at_accessibility);
6437 CHECKSUM_ATTR (attrs.at_address_class);
6438 CHECKSUM_ATTR (attrs.at_allocated);
6439 CHECKSUM_ATTR (attrs.at_artificial);
6440 CHECKSUM_ATTR (attrs.at_associated);
6441 CHECKSUM_ATTR (attrs.at_binary_scale);
6442 CHECKSUM_ATTR (attrs.at_bit_offset);
6443 CHECKSUM_ATTR (attrs.at_bit_size);
6444 CHECKSUM_ATTR (attrs.at_bit_stride);
6445 CHECKSUM_ATTR (attrs.at_byte_size);
6446 CHECKSUM_ATTR (attrs.at_byte_stride);
6447 CHECKSUM_ATTR (attrs.at_const_value);
6448 CHECKSUM_ATTR (attrs.at_containing_type);
6449 CHECKSUM_ATTR (attrs.at_count);
6450 CHECKSUM_ATTR (attrs.at_data_location);
6451 CHECKSUM_ATTR (attrs.at_data_member_location);
6452 CHECKSUM_ATTR (attrs.at_decimal_scale);
6453 CHECKSUM_ATTR (attrs.at_decimal_sign);
6454 CHECKSUM_ATTR (attrs.at_default_value);
6455 CHECKSUM_ATTR (attrs.at_digit_count);
6456 CHECKSUM_ATTR (attrs.at_discr);
6457 CHECKSUM_ATTR (attrs.at_discr_list);
6458 CHECKSUM_ATTR (attrs.at_discr_value);
6459 CHECKSUM_ATTR (attrs.at_encoding);
6460 CHECKSUM_ATTR (attrs.at_endianity);
6461 CHECKSUM_ATTR (attrs.at_explicit);
6462 CHECKSUM_ATTR (attrs.at_is_optional);
6463 CHECKSUM_ATTR (attrs.at_location);
6464 CHECKSUM_ATTR (attrs.at_lower_bound);
6465 CHECKSUM_ATTR (attrs.at_mutable);
6466 CHECKSUM_ATTR (attrs.at_ordering);
6467 CHECKSUM_ATTR (attrs.at_picture_string);
6468 CHECKSUM_ATTR (attrs.at_prototyped);
6469 CHECKSUM_ATTR (attrs.at_small);
6470 CHECKSUM_ATTR (attrs.at_segment);
6471 CHECKSUM_ATTR (attrs.at_string_length);
6472 CHECKSUM_ATTR (attrs.at_threads_scaled);
6473 CHECKSUM_ATTR (attrs.at_upper_bound);
6474 CHECKSUM_ATTR (attrs.at_use_location);
6475 CHECKSUM_ATTR (attrs.at_use_UTF8);
6476 CHECKSUM_ATTR (attrs.at_variable_parameter);
6477 CHECKSUM_ATTR (attrs.at_virtuality);
6478 CHECKSUM_ATTR (attrs.at_visibility);
6479 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6480 CHECKSUM_ATTR (attrs.at_type);
6481 CHECKSUM_ATTR (attrs.at_friend);
6483 /* Checksum the child DIEs. */
6484 c = die->die_child;
6485 if (c) do {
6486 dw_attr_ref name_attr;
6488 c = c->die_sib;
6489 name_attr = get_AT (c, DW_AT_name);
6490 if (is_template_instantiation (c))
6492 /* Ignore instantiations of member type and function templates. */
6494 else if (name_attr != NULL
6495 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
6497 /* Use a shallow checksum for named nested types and member
6498 functions. */
6499 CHECKSUM_ULEB128 ('S');
6500 CHECKSUM_ULEB128 (c->die_tag);
6501 CHECKSUM_STRING (AT_string (name_attr));
6503 else
6505 /* Use a deep checksum for other children. */
6506 /* Mark this DIE so it gets processed when unmarking. */
6507 if (c->die_mark == 0)
6508 c->die_mark = -1;
6509 die_checksum_ordered (c, ctx, mark);
6511 } while (c != die->die_child);
6513 CHECKSUM_ULEB128 (0);
6516 /* Add a type name and tag to a hash. */
6517 static void
6518 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
6520 CHECKSUM_ULEB128 (tag);
6521 CHECKSUM_STRING (name);
6524 #undef CHECKSUM
6525 #undef CHECKSUM_STRING
6526 #undef CHECKSUM_ATTR
6527 #undef CHECKSUM_LEB128
6528 #undef CHECKSUM_ULEB128
6530 /* Generate the type signature for DIE. This is computed by generating an
6531 MD5 checksum over the DIE's tag, its relevant attributes, and its
6532 children. Attributes that are references to other DIEs are processed
6533 by recursion, using the MARK field to prevent infinite recursion.
6534 If the DIE is nested inside a namespace or another type, we also
6535 need to include that context in the signature. The lower 64 bits
6536 of the resulting MD5 checksum comprise the signature. */
6538 static void
6539 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6541 int mark;
6542 const char *name;
6543 unsigned char checksum[16];
6544 struct md5_ctx ctx;
6545 dw_die_ref decl;
6546 dw_die_ref parent;
6548 name = get_AT_string (die, DW_AT_name);
6549 decl = get_AT_ref (die, DW_AT_specification);
6550 parent = get_die_parent (die);
6552 /* First, compute a signature for just the type name (and its surrounding
6553 context, if any. This is stored in the type unit DIE for link-time
6554 ODR (one-definition rule) checking. */
6556 if (is_cxx () && name != NULL)
6558 md5_init_ctx (&ctx);
6560 /* Checksum the names of surrounding namespaces and structures. */
6561 if (parent != NULL)
6562 checksum_die_context (parent, &ctx);
6564 /* Checksum the current DIE. */
6565 die_odr_checksum (die->die_tag, name, &ctx);
6566 md5_finish_ctx (&ctx, checksum);
6568 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6571 /* Next, compute the complete type signature. */
6573 md5_init_ctx (&ctx);
6574 mark = 1;
6575 die->die_mark = mark;
6577 /* Checksum the names of surrounding namespaces and structures. */
6578 if (parent != NULL)
6579 checksum_die_context (parent, &ctx);
6581 /* Checksum the DIE and its children. */
6582 die_checksum_ordered (die, &ctx, &mark);
6583 unmark_all_dies (die);
6584 md5_finish_ctx (&ctx, checksum);
6586 /* Store the signature in the type node and link the type DIE and the
6587 type node together. */
6588 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6589 DWARF_TYPE_SIGNATURE_SIZE);
6590 die->comdat_type_p = true;
6591 die->die_id.die_type_node = type_node;
6592 type_node->type_die = die;
6594 /* If the DIE is a specification, link its declaration to the type node
6595 as well. */
6596 if (decl != NULL)
6598 decl->comdat_type_p = true;
6599 decl->die_id.die_type_node = type_node;
6603 /* Do the location expressions look same? */
6604 static inline int
6605 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6607 return loc1->dw_loc_opc == loc2->dw_loc_opc
6608 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6609 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6612 /* Do the values look the same? */
6613 static int
6614 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6616 dw_loc_descr_ref loc1, loc2;
6617 rtx r1, r2;
6619 if (v1->val_class != v2->val_class)
6620 return 0;
6622 switch (v1->val_class)
6624 case dw_val_class_const:
6625 return v1->v.val_int == v2->v.val_int;
6626 case dw_val_class_unsigned_const:
6627 return v1->v.val_unsigned == v2->v.val_unsigned;
6628 case dw_val_class_const_double:
6629 return v1->v.val_double.high == v2->v.val_double.high
6630 && v1->v.val_double.low == v2->v.val_double.low;
6631 case dw_val_class_wide_int:
6632 return *v1->v.val_wide == *v2->v.val_wide;
6633 case dw_val_class_vec:
6634 if (v1->v.val_vec.length != v2->v.val_vec.length
6635 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6636 return 0;
6637 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6638 v1->v.val_vec.length * v1->v.val_vec.elt_size))
6639 return 0;
6640 return 1;
6641 case dw_val_class_flag:
6642 return v1->v.val_flag == v2->v.val_flag;
6643 case dw_val_class_str:
6644 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
6646 case dw_val_class_addr:
6647 r1 = v1->v.val_addr;
6648 r2 = v2->v.val_addr;
6649 if (GET_CODE (r1) != GET_CODE (r2))
6650 return 0;
6651 return !rtx_equal_p (r1, r2);
6653 case dw_val_class_offset:
6654 return v1->v.val_offset == v2->v.val_offset;
6656 case dw_val_class_loc:
6657 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6658 loc1 && loc2;
6659 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6660 if (!same_loc_p (loc1, loc2, mark))
6661 return 0;
6662 return !loc1 && !loc2;
6664 case dw_val_class_die_ref:
6665 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6667 case dw_val_class_fde_ref:
6668 case dw_val_class_vms_delta:
6669 case dw_val_class_lbl_id:
6670 case dw_val_class_lineptr:
6671 case dw_val_class_macptr:
6672 case dw_val_class_high_pc:
6673 return 1;
6675 case dw_val_class_file:
6676 return v1->v.val_file == v2->v.val_file;
6678 case dw_val_class_data8:
6679 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
6681 default:
6682 return 1;
6686 /* Do the attributes look the same? */
6688 static int
6689 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6691 if (at1->dw_attr != at2->dw_attr)
6692 return 0;
6694 /* We don't care that this was compiled with a different compiler
6695 snapshot; if the output is the same, that's what matters. */
6696 if (at1->dw_attr == DW_AT_producer)
6697 return 1;
6699 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6702 /* Do the dies look the same? */
6704 static int
6705 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6707 dw_die_ref c1, c2;
6708 dw_attr_ref a1;
6709 unsigned ix;
6711 /* To avoid infinite recursion. */
6712 if (die1->die_mark)
6713 return die1->die_mark == die2->die_mark;
6714 die1->die_mark = die2->die_mark = ++(*mark);
6716 if (die1->die_tag != die2->die_tag)
6717 return 0;
6719 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
6720 return 0;
6722 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
6723 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
6724 return 0;
6726 c1 = die1->die_child;
6727 c2 = die2->die_child;
6728 if (! c1)
6730 if (c2)
6731 return 0;
6733 else
6734 for (;;)
6736 if (!same_die_p (c1, c2, mark))
6737 return 0;
6738 c1 = c1->die_sib;
6739 c2 = c2->die_sib;
6740 if (c1 == die1->die_child)
6742 if (c2 == die2->die_child)
6743 break;
6744 else
6745 return 0;
6749 return 1;
6752 /* Do the dies look the same? Wrapper around same_die_p. */
6754 static int
6755 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6757 int mark = 0;
6758 int ret = same_die_p (die1, die2, &mark);
6760 unmark_all_dies (die1);
6761 unmark_all_dies (die2);
6763 return ret;
6766 /* The prefix to attach to symbols on DIEs in the current comdat debug
6767 info section. */
6768 static const char *comdat_symbol_id;
6770 /* The index of the current symbol within the current comdat CU. */
6771 static unsigned int comdat_symbol_number;
6773 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6774 children, and set comdat_symbol_id accordingly. */
6776 static void
6777 compute_section_prefix (dw_die_ref unit_die)
6779 const char *die_name = get_AT_string (unit_die, DW_AT_name);
6780 const char *base = die_name ? lbasename (die_name) : "anonymous";
6781 char *name = XALLOCAVEC (char, strlen (base) + 64);
6782 char *p;
6783 int i, mark;
6784 unsigned char checksum[16];
6785 struct md5_ctx ctx;
6787 /* Compute the checksum of the DIE, then append part of it as hex digits to
6788 the name filename of the unit. */
6790 md5_init_ctx (&ctx);
6791 mark = 0;
6792 die_checksum (unit_die, &ctx, &mark);
6793 unmark_all_dies (unit_die);
6794 md5_finish_ctx (&ctx, checksum);
6796 sprintf (name, "%s.", base);
6797 clean_symbol_name (name);
6799 p = name + strlen (name);
6800 for (i = 0; i < 4; i++)
6802 sprintf (p, "%.2x", checksum[i]);
6803 p += 2;
6806 comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
6807 comdat_symbol_number = 0;
6810 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
6812 static int
6813 is_type_die (dw_die_ref die)
6815 switch (die->die_tag)
6817 case DW_TAG_array_type:
6818 case DW_TAG_class_type:
6819 case DW_TAG_interface_type:
6820 case DW_TAG_enumeration_type:
6821 case DW_TAG_pointer_type:
6822 case DW_TAG_reference_type:
6823 case DW_TAG_rvalue_reference_type:
6824 case DW_TAG_string_type:
6825 case DW_TAG_structure_type:
6826 case DW_TAG_subroutine_type:
6827 case DW_TAG_union_type:
6828 case DW_TAG_ptr_to_member_type:
6829 case DW_TAG_set_type:
6830 case DW_TAG_subrange_type:
6831 case DW_TAG_base_type:
6832 case DW_TAG_const_type:
6833 case DW_TAG_file_type:
6834 case DW_TAG_packed_type:
6835 case DW_TAG_volatile_type:
6836 case DW_TAG_typedef:
6837 return 1;
6838 default:
6839 return 0;
6843 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6844 Basically, we want to choose the bits that are likely to be shared between
6845 compilations (types) and leave out the bits that are specific to individual
6846 compilations (functions). */
6848 static int
6849 is_comdat_die (dw_die_ref c)
6851 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6852 we do for stabs. The advantage is a greater likelihood of sharing between
6853 objects that don't include headers in the same order (and therefore would
6854 put the base types in a different comdat). jason 8/28/00 */
6856 if (c->die_tag == DW_TAG_base_type)
6857 return 0;
6859 if (c->die_tag == DW_TAG_pointer_type
6860 || c->die_tag == DW_TAG_reference_type
6861 || c->die_tag == DW_TAG_rvalue_reference_type
6862 || c->die_tag == DW_TAG_const_type
6863 || c->die_tag == DW_TAG_volatile_type)
6865 dw_die_ref t = get_AT_ref (c, DW_AT_type);
6867 return t ? is_comdat_die (t) : 0;
6870 return is_type_die (c);
6873 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6874 compilation unit. */
6876 static int
6877 is_symbol_die (dw_die_ref c)
6879 return (is_type_die (c)
6880 || is_declaration_die (c)
6881 || c->die_tag == DW_TAG_namespace
6882 || c->die_tag == DW_TAG_module);
6885 /* Returns true iff C is a compile-unit DIE. */
6887 static inline bool
6888 is_cu_die (dw_die_ref c)
6890 return c && c->die_tag == DW_TAG_compile_unit;
6893 /* Returns true iff C is a unit DIE of some sort. */
6895 static inline bool
6896 is_unit_die (dw_die_ref c)
6898 return c && (c->die_tag == DW_TAG_compile_unit
6899 || c->die_tag == DW_TAG_partial_unit
6900 || c->die_tag == DW_TAG_type_unit);
6903 /* Returns true iff C is a namespace DIE. */
6905 static inline bool
6906 is_namespace_die (dw_die_ref c)
6908 return c && c->die_tag == DW_TAG_namespace;
6911 /* Returns true iff C is a class or structure DIE. */
6913 static inline bool
6914 is_class_die (dw_die_ref c)
6916 return c && (c->die_tag == DW_TAG_class_type
6917 || c->die_tag == DW_TAG_structure_type);
6920 /* Return non-zero if this DIE is a template parameter. */
6922 static inline bool
6923 is_template_parameter (dw_die_ref die)
6925 switch (die->die_tag)
6927 case DW_TAG_template_type_param:
6928 case DW_TAG_template_value_param:
6929 case DW_TAG_GNU_template_template_param:
6930 case DW_TAG_GNU_template_parameter_pack:
6931 return true;
6932 default:
6933 return false;
6937 /* Return non-zero if this DIE represents a template instantiation. */
6939 static inline bool
6940 is_template_instantiation (dw_die_ref die)
6942 dw_die_ref c;
6944 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
6945 return false;
6946 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
6947 return false;
6950 static char *
6951 gen_internal_sym (const char *prefix)
6953 char buf[256];
6955 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6956 return xstrdup (buf);
6959 /* Assign symbols to all worthy DIEs under DIE. */
6961 static void
6962 assign_symbol_names (dw_die_ref die)
6964 dw_die_ref c;
6966 if (is_symbol_die (die) && !die->comdat_type_p)
6968 if (comdat_symbol_id)
6970 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
6972 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6973 comdat_symbol_id, comdat_symbol_number++);
6974 die->die_id.die_symbol = xstrdup (p);
6976 else
6977 die->die_id.die_symbol = gen_internal_sym ("LDIE");
6980 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6983 struct cu_hash_table_entry
6985 dw_die_ref cu;
6986 unsigned min_comdat_num, max_comdat_num;
6987 struct cu_hash_table_entry *next;
6990 /* Helpers to manipulate hash table of CUs. */
6992 struct cu_hash_table_entry_hasher : pointer_hash <cu_hash_table_entry>
6994 typedef die_struct *compare_type;
6995 static inline hashval_t hash (const cu_hash_table_entry *);
6996 static inline bool equal (const cu_hash_table_entry *, const die_struct *);
6997 static inline void remove (cu_hash_table_entry *);
7000 inline hashval_t
7001 cu_hash_table_entry_hasher::hash (const cu_hash_table_entry *entry)
7003 return htab_hash_string (entry->cu->die_id.die_symbol);
7006 inline bool
7007 cu_hash_table_entry_hasher::equal (const cu_hash_table_entry *entry1,
7008 const die_struct *entry2)
7010 return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
7013 inline void
7014 cu_hash_table_entry_hasher::remove (cu_hash_table_entry *entry)
7016 struct cu_hash_table_entry *next;
7018 while (entry)
7020 next = entry->next;
7021 free (entry);
7022 entry = next;
7026 typedef hash_table<cu_hash_table_entry_hasher> cu_hash_type;
7028 /* Check whether we have already seen this CU and set up SYM_NUM
7029 accordingly. */
7030 static int
7031 check_duplicate_cu (dw_die_ref cu, cu_hash_type *htable, unsigned int *sym_num)
7033 struct cu_hash_table_entry dummy;
7034 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
7036 dummy.max_comdat_num = 0;
7038 slot = htable->find_slot_with_hash (cu,
7039 htab_hash_string (cu->die_id.die_symbol),
7040 INSERT);
7041 entry = *slot;
7043 for (; entry; last = entry, entry = entry->next)
7045 if (same_die_p_wrap (cu, entry->cu))
7046 break;
7049 if (entry)
7051 *sym_num = entry->min_comdat_num;
7052 return 1;
7055 entry = XCNEW (struct cu_hash_table_entry);
7056 entry->cu = cu;
7057 entry->min_comdat_num = *sym_num = last->max_comdat_num;
7058 entry->next = *slot;
7059 *slot = entry;
7061 return 0;
7064 /* Record SYM_NUM to record of CU in HTABLE. */
7065 static void
7066 record_comdat_symbol_number (dw_die_ref cu, cu_hash_type *htable,
7067 unsigned int sym_num)
7069 struct cu_hash_table_entry **slot, *entry;
7071 slot = htable->find_slot_with_hash (cu,
7072 htab_hash_string (cu->die_id.die_symbol),
7073 NO_INSERT);
7074 entry = *slot;
7076 entry->max_comdat_num = sym_num;
7079 /* Traverse the DIE (which is always comp_unit_die), and set up
7080 additional compilation units for each of the include files we see
7081 bracketed by BINCL/EINCL. */
7083 static void
7084 break_out_includes (dw_die_ref die)
7086 dw_die_ref c;
7087 dw_die_ref unit = NULL;
7088 limbo_die_node *node, **pnode;
7090 c = die->die_child;
7091 if (c) do {
7092 dw_die_ref prev = c;
7093 c = c->die_sib;
7094 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
7095 || (unit && is_comdat_die (c)))
7097 dw_die_ref next = c->die_sib;
7099 /* This DIE is for a secondary CU; remove it from the main one. */
7100 remove_child_with_prev (c, prev);
7102 if (c->die_tag == DW_TAG_GNU_BINCL)
7103 unit = push_new_compile_unit (unit, c);
7104 else if (c->die_tag == DW_TAG_GNU_EINCL)
7105 unit = pop_compile_unit (unit);
7106 else
7107 add_child_die (unit, c);
7108 c = next;
7109 if (c == die->die_child)
7110 break;
7112 } while (c != die->die_child);
7114 #if 0
7115 /* We can only use this in debugging, since the frontend doesn't check
7116 to make sure that we leave every include file we enter. */
7117 gcc_assert (!unit);
7118 #endif
7120 assign_symbol_names (die);
7121 cu_hash_type cu_hash_table (10);
7122 for (node = limbo_die_list, pnode = &limbo_die_list;
7123 node;
7124 node = node->next)
7126 int is_dupl;
7128 compute_section_prefix (node->die);
7129 is_dupl = check_duplicate_cu (node->die, &cu_hash_table,
7130 &comdat_symbol_number);
7131 assign_symbol_names (node->die);
7132 if (is_dupl)
7133 *pnode = node->next;
7134 else
7136 pnode = &node->next;
7137 record_comdat_symbol_number (node->die, &cu_hash_table,
7138 comdat_symbol_number);
7143 /* Return non-zero if this DIE is a declaration. */
7145 static int
7146 is_declaration_die (dw_die_ref die)
7148 dw_attr_ref a;
7149 unsigned ix;
7151 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7152 if (a->dw_attr == DW_AT_declaration)
7153 return 1;
7155 return 0;
7158 /* Return non-zero if this DIE is nested inside a subprogram. */
7160 static int
7161 is_nested_in_subprogram (dw_die_ref die)
7163 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
7165 if (decl == NULL)
7166 decl = die;
7167 return local_scope_p (decl);
7170 /* Return non-zero if this DIE contains a defining declaration of a
7171 subprogram. */
7173 static int
7174 contains_subprogram_definition (dw_die_ref die)
7176 dw_die_ref c;
7178 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
7179 return 1;
7180 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
7181 return 0;
7184 /* Return non-zero if this is a type DIE that should be moved to a
7185 COMDAT .debug_types section. */
7187 static int
7188 should_move_die_to_comdat (dw_die_ref die)
7190 switch (die->die_tag)
7192 case DW_TAG_class_type:
7193 case DW_TAG_structure_type:
7194 case DW_TAG_enumeration_type:
7195 case DW_TAG_union_type:
7196 /* Don't move declarations, inlined instances, types nested in a
7197 subprogram, or types that contain subprogram definitions. */
7198 if (is_declaration_die (die)
7199 || get_AT (die, DW_AT_abstract_origin)
7200 || is_nested_in_subprogram (die)
7201 || contains_subprogram_definition (die))
7202 return 0;
7203 return 1;
7204 case DW_TAG_array_type:
7205 case DW_TAG_interface_type:
7206 case DW_TAG_pointer_type:
7207 case DW_TAG_reference_type:
7208 case DW_TAG_rvalue_reference_type:
7209 case DW_TAG_string_type:
7210 case DW_TAG_subroutine_type:
7211 case DW_TAG_ptr_to_member_type:
7212 case DW_TAG_set_type:
7213 case DW_TAG_subrange_type:
7214 case DW_TAG_base_type:
7215 case DW_TAG_const_type:
7216 case DW_TAG_file_type:
7217 case DW_TAG_packed_type:
7218 case DW_TAG_volatile_type:
7219 case DW_TAG_typedef:
7220 default:
7221 return 0;
7225 /* Make a clone of DIE. */
7227 static dw_die_ref
7228 clone_die (dw_die_ref die)
7230 dw_die_ref clone;
7231 dw_attr_ref a;
7232 unsigned ix;
7234 clone = ggc_cleared_alloc<die_node> ();
7235 clone->die_tag = die->die_tag;
7237 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7238 add_dwarf_attr (clone, a);
7240 return clone;
7243 /* Make a clone of the tree rooted at DIE. */
7245 static dw_die_ref
7246 clone_tree (dw_die_ref die)
7248 dw_die_ref c;
7249 dw_die_ref clone = clone_die (die);
7251 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
7253 return clone;
7256 /* Make a clone of DIE as a declaration. */
7258 static dw_die_ref
7259 clone_as_declaration (dw_die_ref die)
7261 dw_die_ref clone;
7262 dw_die_ref decl;
7263 dw_attr_ref a;
7264 unsigned ix;
7266 /* If the DIE is already a declaration, just clone it. */
7267 if (is_declaration_die (die))
7268 return clone_die (die);
7270 /* If the DIE is a specification, just clone its declaration DIE. */
7271 decl = get_AT_ref (die, DW_AT_specification);
7272 if (decl != NULL)
7274 clone = clone_die (decl);
7275 if (die->comdat_type_p)
7276 add_AT_die_ref (clone, DW_AT_signature, die);
7277 return clone;
7280 clone = ggc_cleared_alloc<die_node> ();
7281 clone->die_tag = die->die_tag;
7283 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7285 /* We don't want to copy over all attributes.
7286 For example we don't want DW_AT_byte_size because otherwise we will no
7287 longer have a declaration and GDB will treat it as a definition. */
7289 switch (a->dw_attr)
7291 case DW_AT_abstract_origin:
7292 case DW_AT_artificial:
7293 case DW_AT_containing_type:
7294 case DW_AT_external:
7295 case DW_AT_name:
7296 case DW_AT_type:
7297 case DW_AT_virtuality:
7298 case DW_AT_linkage_name:
7299 case DW_AT_MIPS_linkage_name:
7300 add_dwarf_attr (clone, a);
7301 break;
7302 case DW_AT_byte_size:
7303 default:
7304 break;
7308 if (die->comdat_type_p)
7309 add_AT_die_ref (clone, DW_AT_signature, die);
7311 add_AT_flag (clone, DW_AT_declaration, 1);
7312 return clone;
7316 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
7318 struct decl_table_entry
7320 dw_die_ref orig;
7321 dw_die_ref copy;
7324 /* Helpers to manipulate hash table of copied declarations. */
7326 /* Hashtable helpers. */
7328 struct decl_table_entry_hasher : free_ptr_hash <decl_table_entry>
7330 typedef die_struct *compare_type;
7331 static inline hashval_t hash (const decl_table_entry *);
7332 static inline bool equal (const decl_table_entry *, const die_struct *);
7335 inline hashval_t
7336 decl_table_entry_hasher::hash (const decl_table_entry *entry)
7338 return htab_hash_pointer (entry->orig);
7341 inline bool
7342 decl_table_entry_hasher::equal (const decl_table_entry *entry1,
7343 const die_struct *entry2)
7345 return entry1->orig == entry2;
7348 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
7350 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7351 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
7352 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
7353 to check if the ancestor has already been copied into UNIT. */
7355 static dw_die_ref
7356 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
7357 decl_hash_type *decl_table)
7359 dw_die_ref parent = die->die_parent;
7360 dw_die_ref new_parent = unit;
7361 dw_die_ref copy;
7362 decl_table_entry **slot = NULL;
7363 struct decl_table_entry *entry = NULL;
7365 if (decl_table)
7367 /* Check if the entry has already been copied to UNIT. */
7368 slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
7369 INSERT);
7370 if (*slot != HTAB_EMPTY_ENTRY)
7372 entry = *slot;
7373 return entry->copy;
7376 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
7377 entry = XCNEW (struct decl_table_entry);
7378 entry->orig = die;
7379 entry->copy = NULL;
7380 *slot = entry;
7383 if (parent != NULL)
7385 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7386 if (spec != NULL)
7387 parent = spec;
7388 if (!is_unit_die (parent))
7389 new_parent = copy_ancestor_tree (unit, parent, decl_table);
7392 copy = clone_as_declaration (die);
7393 add_child_die (new_parent, copy);
7395 if (decl_table)
7397 /* Record the pointer to the copy. */
7398 entry->copy = copy;
7401 return copy;
7403 /* Copy the declaration context to the new type unit DIE. This includes
7404 any surrounding namespace or type declarations. If the DIE has an
7405 AT_specification attribute, it also includes attributes and children
7406 attached to the specification, and returns a pointer to the original
7407 parent of the declaration DIE. Returns NULL otherwise. */
7409 static dw_die_ref
7410 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7412 dw_die_ref decl;
7413 dw_die_ref new_decl;
7414 dw_die_ref orig_parent = NULL;
7416 decl = get_AT_ref (die, DW_AT_specification);
7417 if (decl == NULL)
7418 decl = die;
7419 else
7421 unsigned ix;
7422 dw_die_ref c;
7423 dw_attr_ref a;
7425 /* The original DIE will be changed to a declaration, and must
7426 be moved to be a child of the original declaration DIE. */
7427 orig_parent = decl->die_parent;
7429 /* Copy the type node pointer from the new DIE to the original
7430 declaration DIE so we can forward references later. */
7431 decl->comdat_type_p = true;
7432 decl->die_id.die_type_node = die->die_id.die_type_node;
7434 remove_AT (die, DW_AT_specification);
7436 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7438 if (a->dw_attr != DW_AT_name
7439 && a->dw_attr != DW_AT_declaration
7440 && a->dw_attr != DW_AT_external)
7441 add_dwarf_attr (die, a);
7444 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7447 if (decl->die_parent != NULL
7448 && !is_unit_die (decl->die_parent))
7450 new_decl = copy_ancestor_tree (unit, decl, NULL);
7451 if (new_decl != NULL)
7453 remove_AT (new_decl, DW_AT_signature);
7454 add_AT_specification (die, new_decl);
7458 return orig_parent;
7461 /* Generate the skeleton ancestor tree for the given NODE, then clone
7462 the DIE and add the clone into the tree. */
7464 static void
7465 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7467 if (node->new_die != NULL)
7468 return;
7470 node->new_die = clone_as_declaration (node->old_die);
7472 if (node->parent != NULL)
7474 generate_skeleton_ancestor_tree (node->parent);
7475 add_child_die (node->parent->new_die, node->new_die);
7479 /* Generate a skeleton tree of DIEs containing any declarations that are
7480 found in the original tree. We traverse the tree looking for declaration
7481 DIEs, and construct the skeleton from the bottom up whenever we find one. */
7483 static void
7484 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7486 skeleton_chain_node node;
7487 dw_die_ref c;
7488 dw_die_ref first;
7489 dw_die_ref prev = NULL;
7490 dw_die_ref next = NULL;
7492 node.parent = parent;
7494 first = c = parent->old_die->die_child;
7495 if (c)
7496 next = c->die_sib;
7497 if (c) do {
7498 if (prev == NULL || prev->die_sib == c)
7499 prev = c;
7500 c = next;
7501 next = (c == first ? NULL : c->die_sib);
7502 node.old_die = c;
7503 node.new_die = NULL;
7504 if (is_declaration_die (c))
7506 if (is_template_instantiation (c))
7508 /* Instantiated templates do not need to be cloned into the
7509 type unit. Just move the DIE and its children back to
7510 the skeleton tree (in the main CU). */
7511 remove_child_with_prev (c, prev);
7512 add_child_die (parent->new_die, c);
7513 c = prev;
7515 else
7517 /* Clone the existing DIE, move the original to the skeleton
7518 tree (which is in the main CU), and put the clone, with
7519 all the original's children, where the original came from
7520 (which is about to be moved to the type unit). */
7521 dw_die_ref clone = clone_die (c);
7522 move_all_children (c, clone);
7524 /* If the original has a DW_AT_object_pointer attribute,
7525 it would now point to a child DIE just moved to the
7526 cloned tree, so we need to remove that attribute from
7527 the original. */
7528 remove_AT (c, DW_AT_object_pointer);
7530 replace_child (c, clone, prev);
7531 generate_skeleton_ancestor_tree (parent);
7532 add_child_die (parent->new_die, c);
7533 node.new_die = c;
7534 c = clone;
7537 generate_skeleton_bottom_up (&node);
7538 } while (next != NULL);
7541 /* Wrapper function for generate_skeleton_bottom_up. */
7543 static dw_die_ref
7544 generate_skeleton (dw_die_ref die)
7546 skeleton_chain_node node;
7548 node.old_die = die;
7549 node.new_die = NULL;
7550 node.parent = NULL;
7552 /* If this type definition is nested inside another type,
7553 and is not an instantiation of a template, always leave
7554 at least a declaration in its place. */
7555 if (die->die_parent != NULL
7556 && is_type_die (die->die_parent)
7557 && !is_template_instantiation (die))
7558 node.new_die = clone_as_declaration (die);
7560 generate_skeleton_bottom_up (&node);
7561 return node.new_die;
7564 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
7565 declaration. The original DIE is moved to a new compile unit so that
7566 existing references to it follow it to the new location. If any of the
7567 original DIE's descendants is a declaration, we need to replace the
7568 original DIE with a skeleton tree and move the declarations back into the
7569 skeleton tree. */
7571 static dw_die_ref
7572 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
7573 dw_die_ref prev)
7575 dw_die_ref skeleton, orig_parent;
7577 /* Copy the declaration context to the type unit DIE. If the returned
7578 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
7579 that DIE. */
7580 orig_parent = copy_declaration_context (unit, child);
7582 skeleton = generate_skeleton (child);
7583 if (skeleton == NULL)
7584 remove_child_with_prev (child, prev);
7585 else
7587 skeleton->comdat_type_p = true;
7588 skeleton->die_id.die_type_node = child->die_id.die_type_node;
7590 /* If the original DIE was a specification, we need to put
7591 the skeleton under the parent DIE of the declaration.
7592 This leaves the original declaration in the tree, but
7593 it will be pruned later since there are no longer any
7594 references to it. */
7595 if (orig_parent != NULL)
7597 remove_child_with_prev (child, prev);
7598 add_child_die (orig_parent, skeleton);
7600 else
7601 replace_child (child, skeleton, prev);
7604 return skeleton;
7607 /* Traverse the DIE and set up additional .debug_types sections for each
7608 type worthy of being placed in a COMDAT section. */
7610 static void
7611 break_out_comdat_types (dw_die_ref die)
7613 dw_die_ref c;
7614 dw_die_ref first;
7615 dw_die_ref prev = NULL;
7616 dw_die_ref next = NULL;
7617 dw_die_ref unit = NULL;
7619 first = c = die->die_child;
7620 if (c)
7621 next = c->die_sib;
7622 if (c) do {
7623 if (prev == NULL || prev->die_sib == c)
7624 prev = c;
7625 c = next;
7626 next = (c == first ? NULL : c->die_sib);
7627 if (should_move_die_to_comdat (c))
7629 dw_die_ref replacement;
7630 comdat_type_node_ref type_node;
7632 /* Break out nested types into their own type units. */
7633 break_out_comdat_types (c);
7635 /* Create a new type unit DIE as the root for the new tree, and
7636 add it to the list of comdat types. */
7637 unit = new_die (DW_TAG_type_unit, NULL, NULL);
7638 add_AT_unsigned (unit, DW_AT_language,
7639 get_AT_unsigned (comp_unit_die (), DW_AT_language));
7640 type_node = ggc_cleared_alloc<comdat_type_node> ();
7641 type_node->root_die = unit;
7642 type_node->next = comdat_type_list;
7643 comdat_type_list = type_node;
7645 /* Generate the type signature. */
7646 generate_type_signature (c, type_node);
7648 /* Copy the declaration context, attributes, and children of the
7649 declaration into the new type unit DIE, then remove this DIE
7650 from the main CU (or replace it with a skeleton if necessary). */
7651 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
7652 type_node->skeleton_die = replacement;
7654 /* Add the DIE to the new compunit. */
7655 add_child_die (unit, c);
7657 if (replacement != NULL)
7658 c = replacement;
7660 else if (c->die_tag == DW_TAG_namespace
7661 || c->die_tag == DW_TAG_class_type
7662 || c->die_tag == DW_TAG_structure_type
7663 || c->die_tag == DW_TAG_union_type)
7665 /* Look for nested types that can be broken out. */
7666 break_out_comdat_types (c);
7668 } while (next != NULL);
7671 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
7672 Enter all the cloned children into the hash table decl_table. */
7674 static dw_die_ref
7675 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
7677 dw_die_ref c;
7678 dw_die_ref clone;
7679 struct decl_table_entry *entry;
7680 decl_table_entry **slot;
7682 if (die->die_tag == DW_TAG_subprogram)
7683 clone = clone_as_declaration (die);
7684 else
7685 clone = clone_die (die);
7687 slot = decl_table->find_slot_with_hash (die,
7688 htab_hash_pointer (die), INSERT);
7690 /* Assert that DIE isn't in the hash table yet. If it would be there
7691 before, the ancestors would be necessarily there as well, therefore
7692 clone_tree_partial wouldn't be called. */
7693 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
7695 entry = XCNEW (struct decl_table_entry);
7696 entry->orig = die;
7697 entry->copy = clone;
7698 *slot = entry;
7700 if (die->die_tag != DW_TAG_subprogram)
7701 FOR_EACH_CHILD (die, c,
7702 add_child_die (clone, clone_tree_partial (c, decl_table)));
7704 return clone;
7707 /* Walk the DIE and its children, looking for references to incomplete
7708 or trivial types that are unmarked (i.e., that are not in the current
7709 type_unit). */
7711 static void
7712 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
7714 dw_die_ref c;
7715 dw_attr_ref a;
7716 unsigned ix;
7718 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7720 if (AT_class (a) == dw_val_class_die_ref)
7722 dw_die_ref targ = AT_ref (a);
7723 decl_table_entry **slot;
7724 struct decl_table_entry *entry;
7726 if (targ->die_mark != 0 || targ->comdat_type_p)
7727 continue;
7729 slot = decl_table->find_slot_with_hash (targ,
7730 htab_hash_pointer (targ),
7731 INSERT);
7733 if (*slot != HTAB_EMPTY_ENTRY)
7735 /* TARG has already been copied, so we just need to
7736 modify the reference to point to the copy. */
7737 entry = *slot;
7738 a->dw_attr_val.v.val_die_ref.die = entry->copy;
7740 else
7742 dw_die_ref parent = unit;
7743 dw_die_ref copy = clone_die (targ);
7745 /* Record in DECL_TABLE that TARG has been copied.
7746 Need to do this now, before the recursive call,
7747 because DECL_TABLE may be expanded and SLOT
7748 would no longer be a valid pointer. */
7749 entry = XCNEW (struct decl_table_entry);
7750 entry->orig = targ;
7751 entry->copy = copy;
7752 *slot = entry;
7754 /* If TARG is not a declaration DIE, we need to copy its
7755 children. */
7756 if (!is_declaration_die (targ))
7758 FOR_EACH_CHILD (
7759 targ, c,
7760 add_child_die (copy,
7761 clone_tree_partial (c, decl_table)));
7764 /* Make sure the cloned tree is marked as part of the
7765 type unit. */
7766 mark_dies (copy);
7768 /* If TARG has surrounding context, copy its ancestor tree
7769 into the new type unit. */
7770 if (targ->die_parent != NULL
7771 && !is_unit_die (targ->die_parent))
7772 parent = copy_ancestor_tree (unit, targ->die_parent,
7773 decl_table);
7775 add_child_die (parent, copy);
7776 a->dw_attr_val.v.val_die_ref.die = copy;
7778 /* Make sure the newly-copied DIE is walked. If it was
7779 installed in a previously-added context, it won't
7780 get visited otherwise. */
7781 if (parent != unit)
7783 /* Find the highest point of the newly-added tree,
7784 mark each node along the way, and walk from there. */
7785 parent->die_mark = 1;
7786 while (parent->die_parent
7787 && parent->die_parent->die_mark == 0)
7789 parent = parent->die_parent;
7790 parent->die_mark = 1;
7792 copy_decls_walk (unit, parent, decl_table);
7798 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
7801 /* Copy declarations for "unworthy" types into the new comdat section.
7802 Incomplete types, modified types, and certain other types aren't broken
7803 out into comdat sections of their own, so they don't have a signature,
7804 and we need to copy the declaration into the same section so that we
7805 don't have an external reference. */
7807 static void
7808 copy_decls_for_unworthy_types (dw_die_ref unit)
7810 mark_dies (unit);
7811 decl_hash_type decl_table (10);
7812 copy_decls_walk (unit, unit, &decl_table);
7813 unmark_dies (unit);
7816 /* Traverse the DIE and add a sibling attribute if it may have the
7817 effect of speeding up access to siblings. To save some space,
7818 avoid generating sibling attributes for DIE's without children. */
7820 static void
7821 add_sibling_attributes (dw_die_ref die)
7823 dw_die_ref c;
7825 if (! die->die_child)
7826 return;
7828 if (die->die_parent && die != die->die_parent->die_child)
7829 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7831 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7834 /* Output all location lists for the DIE and its children. */
7836 static void
7837 output_location_lists (dw_die_ref die)
7839 dw_die_ref c;
7840 dw_attr_ref a;
7841 unsigned ix;
7843 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7844 if (AT_class (a) == dw_val_class_loc_list)
7845 output_loc_list (AT_loc_list (a));
7847 FOR_EACH_CHILD (die, c, output_location_lists (c));
7850 /* We want to limit the number of external references, because they are
7851 larger than local references: a relocation takes multiple words, and
7852 even a sig8 reference is always eight bytes, whereas a local reference
7853 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
7854 So if we encounter multiple external references to the same type DIE, we
7855 make a local typedef stub for it and redirect all references there.
7857 This is the element of the hash table for keeping track of these
7858 references. */
7860 struct external_ref
7862 dw_die_ref type;
7863 dw_die_ref stub;
7864 unsigned n_refs;
7867 /* Hashtable helpers. */
7869 struct external_ref_hasher : free_ptr_hash <external_ref>
7871 static inline hashval_t hash (const external_ref *);
7872 static inline bool equal (const external_ref *, const external_ref *);
7875 inline hashval_t
7876 external_ref_hasher::hash (const external_ref *r)
7878 dw_die_ref die = r->type;
7879 hashval_t h = 0;
7881 /* We can't use the address of the DIE for hashing, because
7882 that will make the order of the stub DIEs non-deterministic. */
7883 if (! die->comdat_type_p)
7884 /* We have a symbol; use it to compute a hash. */
7885 h = htab_hash_string (die->die_id.die_symbol);
7886 else
7888 /* We have a type signature; use a subset of the bits as the hash.
7889 The 8-byte signature is at least as large as hashval_t. */
7890 comdat_type_node_ref type_node = die->die_id.die_type_node;
7891 memcpy (&h, type_node->signature, sizeof (h));
7893 return h;
7896 inline bool
7897 external_ref_hasher::equal (const external_ref *r1, const external_ref *r2)
7899 return r1->type == r2->type;
7902 typedef hash_table<external_ref_hasher> external_ref_hash_type;
7904 /* Return a pointer to the external_ref for references to DIE. */
7906 static struct external_ref *
7907 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
7909 struct external_ref ref, *ref_p;
7910 external_ref **slot;
7912 ref.type = die;
7913 slot = map->find_slot (&ref, INSERT);
7914 if (*slot != HTAB_EMPTY_ENTRY)
7915 return *slot;
7917 ref_p = XCNEW (struct external_ref);
7918 ref_p->type = die;
7919 *slot = ref_p;
7920 return ref_p;
7923 /* Subroutine of optimize_external_refs, below.
7925 If we see a type skeleton, record it as our stub. If we see external
7926 references, remember how many we've seen. */
7928 static void
7929 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
7931 dw_die_ref c;
7932 dw_attr_ref a;
7933 unsigned ix;
7934 struct external_ref *ref_p;
7936 if (is_type_die (die)
7937 && (c = get_AT_ref (die, DW_AT_signature)))
7939 /* This is a local skeleton; use it for local references. */
7940 ref_p = lookup_external_ref (map, c);
7941 ref_p->stub = die;
7944 /* Scan the DIE references, and remember any that refer to DIEs from
7945 other CUs (i.e. those which are not marked). */
7946 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7947 if (AT_class (a) == dw_val_class_die_ref
7948 && (c = AT_ref (a))->die_mark == 0
7949 && is_type_die (c))
7951 ref_p = lookup_external_ref (map, c);
7952 ref_p->n_refs++;
7955 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
7958 /* htab_traverse callback function for optimize_external_refs, below. SLOT
7959 points to an external_ref, DATA is the CU we're processing. If we don't
7960 already have a local stub, and we have multiple refs, build a stub. */
7963 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
7965 struct external_ref *ref_p = *slot;
7967 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
7969 /* We have multiple references to this type, so build a small stub.
7970 Both of these forms are a bit dodgy from the perspective of the
7971 DWARF standard, since technically they should have names. */
7972 dw_die_ref cu = data;
7973 dw_die_ref type = ref_p->type;
7974 dw_die_ref stub = NULL;
7976 if (type->comdat_type_p)
7978 /* If we refer to this type via sig8, use AT_signature. */
7979 stub = new_die (type->die_tag, cu, NULL_TREE);
7980 add_AT_die_ref (stub, DW_AT_signature, type);
7982 else
7984 /* Otherwise, use a typedef with no name. */
7985 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
7986 add_AT_die_ref (stub, DW_AT_type, type);
7989 stub->die_mark++;
7990 ref_p->stub = stub;
7992 return 1;
7995 /* DIE is a unit; look through all the DIE references to see if there are
7996 any external references to types, and if so, create local stubs for
7997 them which will be applied in build_abbrev_table. This is useful because
7998 references to local DIEs are smaller. */
8000 static external_ref_hash_type *
8001 optimize_external_refs (dw_die_ref die)
8003 external_ref_hash_type *map = new external_ref_hash_type (10);
8004 optimize_external_refs_1 (die, map);
8005 map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
8006 return map;
8009 /* The format of each DIE (and its attribute value pairs) is encoded in an
8010 abbreviation table. This routine builds the abbreviation table and assigns
8011 a unique abbreviation id for each abbreviation entry. The children of each
8012 die are visited recursively. */
8014 static void
8015 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
8017 unsigned long abbrev_id;
8018 unsigned int n_alloc;
8019 dw_die_ref c;
8020 dw_attr_ref a;
8021 unsigned ix;
8023 /* Scan the DIE references, and replace any that refer to
8024 DIEs from other CUs (i.e. those which are not marked) with
8025 the local stubs we built in optimize_external_refs. */
8026 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8027 if (AT_class (a) == dw_val_class_die_ref
8028 && (c = AT_ref (a))->die_mark == 0)
8030 struct external_ref *ref_p;
8031 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
8033 ref_p = lookup_external_ref (extern_map, c);
8034 if (ref_p->stub && ref_p->stub != die)
8035 change_AT_die_ref (a, ref_p->stub);
8036 else
8037 /* We aren't changing this reference, so mark it external. */
8038 set_AT_ref_external (a, 1);
8041 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8043 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
8044 dw_attr_ref die_a, abbrev_a;
8045 unsigned ix;
8046 bool ok = true;
8048 if (abbrev->die_tag != die->die_tag)
8049 continue;
8050 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
8051 continue;
8053 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
8054 continue;
8056 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
8058 abbrev_a = &(*abbrev->die_attr)[ix];
8059 if ((abbrev_a->dw_attr != die_a->dw_attr)
8060 || (value_format (abbrev_a) != value_format (die_a)))
8062 ok = false;
8063 break;
8066 if (ok)
8067 break;
8070 if (abbrev_id >= abbrev_die_table_in_use)
8072 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
8074 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
8075 abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
8076 n_alloc);
8078 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
8079 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
8080 abbrev_die_table_allocated = n_alloc;
8083 ++abbrev_die_table_in_use;
8084 abbrev_die_table[abbrev_id] = die;
8087 die->die_abbrev = abbrev_id;
8088 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
8091 /* Return the power-of-two number of bytes necessary to represent VALUE. */
8093 static int
8094 constant_size (unsigned HOST_WIDE_INT value)
8096 int log;
8098 if (value == 0)
8099 log = 0;
8100 else
8101 log = floor_log2 (value);
8103 log = log / 8;
8104 log = 1 << (floor_log2 (log) + 1);
8106 return log;
8109 /* Return the size of a DIE as it is represented in the
8110 .debug_info section. */
8112 static unsigned long
8113 size_of_die (dw_die_ref die)
8115 unsigned long size = 0;
8116 dw_attr_ref a;
8117 unsigned ix;
8118 enum dwarf_form form;
8120 size += size_of_uleb128 (die->die_abbrev);
8121 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8123 switch (AT_class (a))
8125 case dw_val_class_addr:
8126 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8128 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8129 size += size_of_uleb128 (AT_index (a));
8131 else
8132 size += DWARF2_ADDR_SIZE;
8133 break;
8134 case dw_val_class_offset:
8135 size += DWARF_OFFSET_SIZE;
8136 break;
8137 case dw_val_class_loc:
8139 unsigned long lsize = size_of_locs (AT_loc (a));
8141 /* Block length. */
8142 if (dwarf_version >= 4)
8143 size += size_of_uleb128 (lsize);
8144 else
8145 size += constant_size (lsize);
8146 size += lsize;
8148 break;
8149 case dw_val_class_loc_list:
8150 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8152 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8153 size += size_of_uleb128 (AT_index (a));
8155 else
8156 size += DWARF_OFFSET_SIZE;
8157 break;
8158 case dw_val_class_range_list:
8159 size += DWARF_OFFSET_SIZE;
8160 break;
8161 case dw_val_class_const:
8162 size += size_of_sleb128 (AT_int (a));
8163 break;
8164 case dw_val_class_unsigned_const:
8166 int csize = constant_size (AT_unsigned (a));
8167 if (dwarf_version == 3
8168 && a->dw_attr == DW_AT_data_member_location
8169 && csize >= 4)
8170 size += size_of_uleb128 (AT_unsigned (a));
8171 else
8172 size += csize;
8174 break;
8175 case dw_val_class_const_double:
8176 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
8177 if (HOST_BITS_PER_WIDE_INT >= 64)
8178 size++; /* block */
8179 break;
8180 case dw_val_class_wide_int:
8181 size += (get_full_len (*a->dw_attr_val.v.val_wide)
8182 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
8183 if (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT
8184 > 64)
8185 size++; /* block */
8186 break;
8187 case dw_val_class_vec:
8188 size += constant_size (a->dw_attr_val.v.val_vec.length
8189 * a->dw_attr_val.v.val_vec.elt_size)
8190 + a->dw_attr_val.v.val_vec.length
8191 * a->dw_attr_val.v.val_vec.elt_size; /* block */
8192 break;
8193 case dw_val_class_flag:
8194 if (dwarf_version >= 4)
8195 /* Currently all add_AT_flag calls pass in 1 as last argument,
8196 so DW_FORM_flag_present can be used. If that ever changes,
8197 we'll need to use DW_FORM_flag and have some optimization
8198 in build_abbrev_table that will change those to
8199 DW_FORM_flag_present if it is set to 1 in all DIEs using
8200 the same abbrev entry. */
8201 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8202 else
8203 size += 1;
8204 break;
8205 case dw_val_class_die_ref:
8206 if (AT_ref_external (a))
8208 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
8209 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
8210 is sized by target address length, whereas in DWARF3
8211 it's always sized as an offset. */
8212 if (use_debug_types)
8213 size += DWARF_TYPE_SIGNATURE_SIZE;
8214 else if (dwarf_version == 2)
8215 size += DWARF2_ADDR_SIZE;
8216 else
8217 size += DWARF_OFFSET_SIZE;
8219 else
8220 size += DWARF_OFFSET_SIZE;
8221 break;
8222 case dw_val_class_fde_ref:
8223 size += DWARF_OFFSET_SIZE;
8224 break;
8225 case dw_val_class_lbl_id:
8226 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8228 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8229 size += size_of_uleb128 (AT_index (a));
8231 else
8232 size += DWARF2_ADDR_SIZE;
8233 break;
8234 case dw_val_class_lineptr:
8235 case dw_val_class_macptr:
8236 size += DWARF_OFFSET_SIZE;
8237 break;
8238 case dw_val_class_str:
8239 form = AT_string_form (a);
8240 if (form == DW_FORM_strp)
8241 size += DWARF_OFFSET_SIZE;
8242 else if (form == DW_FORM_GNU_str_index)
8243 size += size_of_uleb128 (AT_index (a));
8244 else
8245 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
8246 break;
8247 case dw_val_class_file:
8248 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
8249 break;
8250 case dw_val_class_data8:
8251 size += 8;
8252 break;
8253 case dw_val_class_vms_delta:
8254 size += DWARF_OFFSET_SIZE;
8255 break;
8256 case dw_val_class_high_pc:
8257 size += DWARF2_ADDR_SIZE;
8258 break;
8259 default:
8260 gcc_unreachable ();
8264 return size;
8267 /* Size the debugging information associated with a given DIE. Visits the
8268 DIE's children recursively. Updates the global variable next_die_offset, on
8269 each time through. Uses the current value of next_die_offset to update the
8270 die_offset field in each DIE. */
8272 static void
8273 calc_die_sizes (dw_die_ref die)
8275 dw_die_ref c;
8277 gcc_assert (die->die_offset == 0
8278 || (unsigned long int) die->die_offset == next_die_offset);
8279 die->die_offset = next_die_offset;
8280 next_die_offset += size_of_die (die);
8282 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
8284 if (die->die_child != NULL)
8285 /* Count the null byte used to terminate sibling lists. */
8286 next_die_offset += 1;
8289 /* Size just the base type children at the start of the CU.
8290 This is needed because build_abbrev needs to size locs
8291 and sizing of type based stack ops needs to know die_offset
8292 values for the base types. */
8294 static void
8295 calc_base_type_die_sizes (void)
8297 unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8298 unsigned int i;
8299 dw_die_ref base_type;
8300 #if ENABLE_ASSERT_CHECKING
8301 dw_die_ref prev = comp_unit_die ()->die_child;
8302 #endif
8304 die_offset += size_of_die (comp_unit_die ());
8305 for (i = 0; base_types.iterate (i, &base_type); i++)
8307 #if ENABLE_ASSERT_CHECKING
8308 gcc_assert (base_type->die_offset == 0
8309 && prev->die_sib == base_type
8310 && base_type->die_child == NULL
8311 && base_type->die_abbrev);
8312 prev = base_type;
8313 #endif
8314 base_type->die_offset = die_offset;
8315 die_offset += size_of_die (base_type);
8319 /* Set the marks for a die and its children. We do this so
8320 that we know whether or not a reference needs to use FORM_ref_addr; only
8321 DIEs in the same CU will be marked. We used to clear out the offset
8322 and use that as the flag, but ran into ordering problems. */
8324 static void
8325 mark_dies (dw_die_ref die)
8327 dw_die_ref c;
8329 gcc_assert (!die->die_mark);
8331 die->die_mark = 1;
8332 FOR_EACH_CHILD (die, c, mark_dies (c));
8335 /* Clear the marks for a die and its children. */
8337 static void
8338 unmark_dies (dw_die_ref die)
8340 dw_die_ref c;
8342 if (! use_debug_types)
8343 gcc_assert (die->die_mark);
8345 die->die_mark = 0;
8346 FOR_EACH_CHILD (die, c, unmark_dies (c));
8349 /* Clear the marks for a die, its children and referred dies. */
8351 static void
8352 unmark_all_dies (dw_die_ref die)
8354 dw_die_ref c;
8355 dw_attr_ref a;
8356 unsigned ix;
8358 if (!die->die_mark)
8359 return;
8360 die->die_mark = 0;
8362 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
8364 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8365 if (AT_class (a) == dw_val_class_die_ref)
8366 unmark_all_dies (AT_ref (a));
8369 /* Calculate if the entry should appear in the final output file. It may be
8370 from a pruned a type. */
8372 static bool
8373 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
8375 /* By limiting gnu pubnames to definitions only, gold can generate a
8376 gdb index without entries for declarations, which don't include
8377 enough information to be useful. */
8378 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
8379 return false;
8381 if (table == pubname_table)
8383 /* Enumerator names are part of the pubname table, but the
8384 parent DW_TAG_enumeration_type die may have been pruned.
8385 Don't output them if that is the case. */
8386 if (p->die->die_tag == DW_TAG_enumerator &&
8387 (p->die->die_parent == NULL
8388 || !p->die->die_parent->die_perennial_p))
8389 return false;
8391 /* Everything else in the pubname table is included. */
8392 return true;
8395 /* The pubtypes table shouldn't include types that have been
8396 pruned. */
8397 return (p->die->die_offset != 0
8398 || !flag_eliminate_unused_debug_types);
8401 /* Return the size of the .debug_pubnames or .debug_pubtypes table
8402 generated for the compilation unit. */
8404 static unsigned long
8405 size_of_pubnames (vec<pubname_entry, va_gc> *names)
8407 unsigned long size;
8408 unsigned i;
8409 pubname_ref p;
8410 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
8412 size = DWARF_PUBNAMES_HEADER_SIZE;
8413 FOR_EACH_VEC_ELT (*names, i, p)
8414 if (include_pubname_in_output (names, p))
8415 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
8417 size += DWARF_OFFSET_SIZE;
8418 return size;
8421 /* Return the size of the information in the .debug_aranges section. */
8423 static unsigned long
8424 size_of_aranges (void)
8426 unsigned long size;
8428 size = DWARF_ARANGES_HEADER_SIZE;
8430 /* Count the address/length pair for this compilation unit. */
8431 if (text_section_used)
8432 size += 2 * DWARF2_ADDR_SIZE;
8433 if (cold_text_section_used)
8434 size += 2 * DWARF2_ADDR_SIZE;
8435 if (have_multiple_function_sections)
8437 unsigned fde_idx;
8438 dw_fde_ref fde;
8440 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
8442 if (DECL_IGNORED_P (fde->decl))
8443 continue;
8444 if (!fde->in_std_section)
8445 size += 2 * DWARF2_ADDR_SIZE;
8446 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
8447 size += 2 * DWARF2_ADDR_SIZE;
8451 /* Count the two zero words used to terminated the address range table. */
8452 size += 2 * DWARF2_ADDR_SIZE;
8453 return size;
8456 /* Select the encoding of an attribute value. */
8458 static enum dwarf_form
8459 value_format (dw_attr_ref a)
8461 switch (AT_class (a))
8463 case dw_val_class_addr:
8464 /* Only very few attributes allow DW_FORM_addr. */
8465 switch (a->dw_attr)
8467 case DW_AT_low_pc:
8468 case DW_AT_high_pc:
8469 case DW_AT_entry_pc:
8470 case DW_AT_trampoline:
8471 return (AT_index (a) == NOT_INDEXED
8472 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8473 default:
8474 break;
8476 switch (DWARF2_ADDR_SIZE)
8478 case 1:
8479 return DW_FORM_data1;
8480 case 2:
8481 return DW_FORM_data2;
8482 case 4:
8483 return DW_FORM_data4;
8484 case 8:
8485 return DW_FORM_data8;
8486 default:
8487 gcc_unreachable ();
8489 case dw_val_class_range_list:
8490 case dw_val_class_loc_list:
8491 if (dwarf_version >= 4)
8492 return DW_FORM_sec_offset;
8493 /* FALLTHRU */
8494 case dw_val_class_vms_delta:
8495 case dw_val_class_offset:
8496 switch (DWARF_OFFSET_SIZE)
8498 case 4:
8499 return DW_FORM_data4;
8500 case 8:
8501 return DW_FORM_data8;
8502 default:
8503 gcc_unreachable ();
8505 case dw_val_class_loc:
8506 if (dwarf_version >= 4)
8507 return DW_FORM_exprloc;
8508 switch (constant_size (size_of_locs (AT_loc (a))))
8510 case 1:
8511 return DW_FORM_block1;
8512 case 2:
8513 return DW_FORM_block2;
8514 case 4:
8515 return DW_FORM_block4;
8516 default:
8517 gcc_unreachable ();
8519 case dw_val_class_const:
8520 return DW_FORM_sdata;
8521 case dw_val_class_unsigned_const:
8522 switch (constant_size (AT_unsigned (a)))
8524 case 1:
8525 return DW_FORM_data1;
8526 case 2:
8527 return DW_FORM_data2;
8528 case 4:
8529 /* In DWARF3 DW_AT_data_member_location with
8530 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
8531 constant, so we need to use DW_FORM_udata if we need
8532 a large constant. */
8533 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8534 return DW_FORM_udata;
8535 return DW_FORM_data4;
8536 case 8:
8537 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8538 return DW_FORM_udata;
8539 return DW_FORM_data8;
8540 default:
8541 gcc_unreachable ();
8543 case dw_val_class_const_double:
8544 switch (HOST_BITS_PER_WIDE_INT)
8546 case 8:
8547 return DW_FORM_data2;
8548 case 16:
8549 return DW_FORM_data4;
8550 case 32:
8551 return DW_FORM_data8;
8552 case 64:
8553 default:
8554 return DW_FORM_block1;
8556 case dw_val_class_wide_int:
8557 switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
8559 case 8:
8560 return DW_FORM_data1;
8561 case 16:
8562 return DW_FORM_data2;
8563 case 32:
8564 return DW_FORM_data4;
8565 case 64:
8566 return DW_FORM_data8;
8567 default:
8568 return DW_FORM_block1;
8570 case dw_val_class_vec:
8571 switch (constant_size (a->dw_attr_val.v.val_vec.length
8572 * a->dw_attr_val.v.val_vec.elt_size))
8574 case 1:
8575 return DW_FORM_block1;
8576 case 2:
8577 return DW_FORM_block2;
8578 case 4:
8579 return DW_FORM_block4;
8580 default:
8581 gcc_unreachable ();
8583 case dw_val_class_flag:
8584 if (dwarf_version >= 4)
8586 /* Currently all add_AT_flag calls pass in 1 as last argument,
8587 so DW_FORM_flag_present can be used. If that ever changes,
8588 we'll need to use DW_FORM_flag and have some optimization
8589 in build_abbrev_table that will change those to
8590 DW_FORM_flag_present if it is set to 1 in all DIEs using
8591 the same abbrev entry. */
8592 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8593 return DW_FORM_flag_present;
8595 return DW_FORM_flag;
8596 case dw_val_class_die_ref:
8597 if (AT_ref_external (a))
8598 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
8599 else
8600 return DW_FORM_ref;
8601 case dw_val_class_fde_ref:
8602 return DW_FORM_data;
8603 case dw_val_class_lbl_id:
8604 return (AT_index (a) == NOT_INDEXED
8605 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8606 case dw_val_class_lineptr:
8607 case dw_val_class_macptr:
8608 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
8609 case dw_val_class_str:
8610 return AT_string_form (a);
8611 case dw_val_class_file:
8612 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8614 case 1:
8615 return DW_FORM_data1;
8616 case 2:
8617 return DW_FORM_data2;
8618 case 4:
8619 return DW_FORM_data4;
8620 default:
8621 gcc_unreachable ();
8624 case dw_val_class_data8:
8625 return DW_FORM_data8;
8627 case dw_val_class_high_pc:
8628 switch (DWARF2_ADDR_SIZE)
8630 case 1:
8631 return DW_FORM_data1;
8632 case 2:
8633 return DW_FORM_data2;
8634 case 4:
8635 return DW_FORM_data4;
8636 case 8:
8637 return DW_FORM_data8;
8638 default:
8639 gcc_unreachable ();
8642 default:
8643 gcc_unreachable ();
8647 /* Output the encoding of an attribute value. */
8649 static void
8650 output_value_format (dw_attr_ref a)
8652 enum dwarf_form form = value_format (a);
8654 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8657 /* Given a die and id, produce the appropriate abbreviations. */
8659 static void
8660 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
8662 unsigned ix;
8663 dw_attr_ref a_attr;
8665 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8666 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8667 dwarf_tag_name (abbrev->die_tag));
8669 if (abbrev->die_child != NULL)
8670 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8671 else
8672 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8674 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
8676 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8677 dwarf_attr_name (a_attr->dw_attr));
8678 output_value_format (a_attr);
8681 dw2_asm_output_data (1, 0, NULL);
8682 dw2_asm_output_data (1, 0, NULL);
8686 /* Output the .debug_abbrev section which defines the DIE abbreviation
8687 table. */
8689 static void
8690 output_abbrev_section (void)
8692 unsigned long abbrev_id;
8694 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8695 output_die_abbrevs (abbrev_id, abbrev_die_table[abbrev_id]);
8697 /* Terminate the table. */
8698 dw2_asm_output_data (1, 0, NULL);
8701 /* Output a symbol we can use to refer to this DIE from another CU. */
8703 static inline void
8704 output_die_symbol (dw_die_ref die)
8706 const char *sym = die->die_id.die_symbol;
8708 gcc_assert (!die->comdat_type_p);
8710 if (sym == 0)
8711 return;
8713 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8714 /* We make these global, not weak; if the target doesn't support
8715 .linkonce, it doesn't support combining the sections, so debugging
8716 will break. */
8717 targetm.asm_out.globalize_label (asm_out_file, sym);
8719 ASM_OUTPUT_LABEL (asm_out_file, sym);
8722 /* Return a new location list, given the begin and end range, and the
8723 expression. */
8725 static inline dw_loc_list_ref
8726 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8727 const char *section)
8729 dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
8731 retlist->begin = begin;
8732 retlist->begin_entry = NULL;
8733 retlist->end = end;
8734 retlist->expr = expr;
8735 retlist->section = section;
8737 return retlist;
8740 /* Generate a new internal symbol for this location list node, if it
8741 hasn't got one yet. */
8743 static inline void
8744 gen_llsym (dw_loc_list_ref list)
8746 gcc_assert (!list->ll_symbol);
8747 list->ll_symbol = gen_internal_sym ("LLST");
8750 /* Output the location list given to us. */
8752 static void
8753 output_loc_list (dw_loc_list_ref list_head)
8755 dw_loc_list_ref curr = list_head;
8757 if (list_head->emitted)
8758 return;
8759 list_head->emitted = true;
8761 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8763 /* Walk the location list, and output each range + expression. */
8764 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8766 unsigned long size;
8767 /* Don't output an entry that starts and ends at the same address. */
8768 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
8769 continue;
8770 size = size_of_locs (curr->expr);
8771 /* If the expression is too large, drop it on the floor. We could
8772 perhaps put it into DW_TAG_dwarf_procedure and refer to that
8773 in the expression, but >= 64KB expressions for a single value
8774 in a single range are unlikely very useful. */
8775 if (size > 0xffff)
8776 continue;
8777 if (dwarf_split_debug_info)
8779 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
8780 "Location list start/length entry (%s)",
8781 list_head->ll_symbol);
8782 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
8783 "Location list range start index (%s)",
8784 curr->begin);
8785 /* The length field is 4 bytes. If we ever need to support
8786 an 8-byte length, we can add a new DW_LLE code or fall back
8787 to DW_LLE_GNU_start_end_entry. */
8788 dw2_asm_output_delta (4, curr->end, curr->begin,
8789 "Location list range length (%s)",
8790 list_head->ll_symbol);
8792 else if (!have_multiple_function_sections)
8794 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8795 "Location list begin address (%s)",
8796 list_head->ll_symbol);
8797 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8798 "Location list end address (%s)",
8799 list_head->ll_symbol);
8801 else
8803 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8804 "Location list begin address (%s)",
8805 list_head->ll_symbol);
8806 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8807 "Location list end address (%s)",
8808 list_head->ll_symbol);
8811 /* Output the block length for this list of location operations. */
8812 gcc_assert (size <= 0xffff);
8813 dw2_asm_output_data (2, size, "%s", "Location expression size");
8815 output_loc_sequence (curr->expr, -1);
8818 if (dwarf_split_debug_info)
8819 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
8820 "Location list terminator (%s)",
8821 list_head->ll_symbol);
8822 else
8824 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8825 "Location list terminator begin (%s)",
8826 list_head->ll_symbol);
8827 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8828 "Location list terminator end (%s)",
8829 list_head->ll_symbol);
8833 /* Output a range_list offset into the debug_range section. Emit a
8834 relocated reference if val_entry is NULL, otherwise, emit an
8835 indirect reference. */
8837 static void
8838 output_range_list_offset (dw_attr_ref a)
8840 const char *name = dwarf_attr_name (a->dw_attr);
8842 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
8844 char *p = strchr (ranges_section_label, '\0');
8845 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX, a->dw_attr_val.v.val_offset);
8846 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8847 debug_ranges_section, "%s", name);
8848 *p = '\0';
8850 else
8851 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8852 "%s (offset from %s)", name, ranges_section_label);
8855 /* Output the offset into the debug_loc section. */
8857 static void
8858 output_loc_list_offset (dw_attr_ref a)
8860 char *sym = AT_loc_list (a)->ll_symbol;
8862 gcc_assert (sym);
8863 if (dwarf_split_debug_info)
8864 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
8865 "%s", dwarf_attr_name (a->dw_attr));
8866 else
8867 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8868 "%s", dwarf_attr_name (a->dw_attr));
8871 /* Output an attribute's index or value appropriately. */
8873 static void
8874 output_attr_index_or_value (dw_attr_ref a)
8876 const char *name = dwarf_attr_name (a->dw_attr);
8878 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8880 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
8881 return;
8883 switch (AT_class (a))
8885 case dw_val_class_addr:
8886 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8887 break;
8888 case dw_val_class_high_pc:
8889 case dw_val_class_lbl_id:
8890 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8891 break;
8892 case dw_val_class_loc_list:
8893 output_loc_list_offset (a);
8894 break;
8895 default:
8896 gcc_unreachable ();
8900 /* Output a type signature. */
8902 static inline void
8903 output_signature (const char *sig, const char *name)
8905 int i;
8907 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8908 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
8911 /* Output the DIE and its attributes. Called recursively to generate
8912 the definitions of each child DIE. */
8914 static void
8915 output_die (dw_die_ref die)
8917 dw_attr_ref a;
8918 dw_die_ref c;
8919 unsigned long size;
8920 unsigned ix;
8922 /* If someone in another CU might refer to us, set up a symbol for
8923 them to point to. */
8924 if (! die->comdat_type_p && die->die_id.die_symbol)
8925 output_die_symbol (die);
8927 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
8928 (unsigned long)die->die_offset,
8929 dwarf_tag_name (die->die_tag));
8931 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8933 const char *name = dwarf_attr_name (a->dw_attr);
8935 switch (AT_class (a))
8937 case dw_val_class_addr:
8938 output_attr_index_or_value (a);
8939 break;
8941 case dw_val_class_offset:
8942 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8943 "%s", name);
8944 break;
8946 case dw_val_class_range_list:
8947 output_range_list_offset (a);
8948 break;
8950 case dw_val_class_loc:
8951 size = size_of_locs (AT_loc (a));
8953 /* Output the block length for this list of location operations. */
8954 if (dwarf_version >= 4)
8955 dw2_asm_output_data_uleb128 (size, "%s", name);
8956 else
8957 dw2_asm_output_data (constant_size (size), size, "%s", name);
8959 output_loc_sequence (AT_loc (a), -1);
8960 break;
8962 case dw_val_class_const:
8963 /* ??? It would be slightly more efficient to use a scheme like is
8964 used for unsigned constants below, but gdb 4.x does not sign
8965 extend. Gdb 5.x does sign extend. */
8966 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8967 break;
8969 case dw_val_class_unsigned_const:
8971 int csize = constant_size (AT_unsigned (a));
8972 if (dwarf_version == 3
8973 && a->dw_attr == DW_AT_data_member_location
8974 && csize >= 4)
8975 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
8976 else
8977 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
8979 break;
8981 case dw_val_class_const_double:
8983 unsigned HOST_WIDE_INT first, second;
8985 if (HOST_BITS_PER_WIDE_INT >= 64)
8986 dw2_asm_output_data (1,
8987 HOST_BITS_PER_DOUBLE_INT
8988 / HOST_BITS_PER_CHAR,
8989 NULL);
8991 if (WORDS_BIG_ENDIAN)
8993 first = a->dw_attr_val.v.val_double.high;
8994 second = a->dw_attr_val.v.val_double.low;
8996 else
8998 first = a->dw_attr_val.v.val_double.low;
8999 second = a->dw_attr_val.v.val_double.high;
9002 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
9003 first, "%s", name);
9004 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
9005 second, NULL);
9007 break;
9009 case dw_val_class_wide_int:
9011 int i;
9012 int len = get_full_len (*a->dw_attr_val.v.val_wide);
9013 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
9014 if (len * HOST_BITS_PER_WIDE_INT > 64)
9015 dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide) * l,
9016 NULL);
9018 if (WORDS_BIG_ENDIAN)
9019 for (i = len - 1; i >= 0; --i)
9021 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
9022 "%s", name);
9023 name = NULL;
9025 else
9026 for (i = 0; i < len; ++i)
9028 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
9029 "%s", name);
9030 name = NULL;
9033 break;
9035 case dw_val_class_vec:
9037 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
9038 unsigned int len = a->dw_attr_val.v.val_vec.length;
9039 unsigned int i;
9040 unsigned char *p;
9042 dw2_asm_output_data (constant_size (len * elt_size),
9043 len * elt_size, "%s", name);
9044 if (elt_size > sizeof (HOST_WIDE_INT))
9046 elt_size /= 2;
9047 len *= 2;
9049 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
9050 i < len;
9051 i++, p += elt_size)
9052 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
9053 "fp or vector constant word %u", i);
9054 break;
9057 case dw_val_class_flag:
9058 if (dwarf_version >= 4)
9060 /* Currently all add_AT_flag calls pass in 1 as last argument,
9061 so DW_FORM_flag_present can be used. If that ever changes,
9062 we'll need to use DW_FORM_flag and have some optimization
9063 in build_abbrev_table that will change those to
9064 DW_FORM_flag_present if it is set to 1 in all DIEs using
9065 the same abbrev entry. */
9066 gcc_assert (AT_flag (a) == 1);
9067 if (flag_debug_asm)
9068 fprintf (asm_out_file, "\t\t\t%s %s\n",
9069 ASM_COMMENT_START, name);
9070 break;
9072 dw2_asm_output_data (1, AT_flag (a), "%s", name);
9073 break;
9075 case dw_val_class_loc_list:
9076 output_attr_index_or_value (a);
9077 break;
9079 case dw_val_class_die_ref:
9080 if (AT_ref_external (a))
9082 if (AT_ref (a)->comdat_type_p)
9084 comdat_type_node_ref type_node =
9085 AT_ref (a)->die_id.die_type_node;
9087 gcc_assert (type_node);
9088 output_signature (type_node->signature, name);
9090 else
9092 const char *sym = AT_ref (a)->die_id.die_symbol;
9093 int size;
9095 gcc_assert (sym);
9096 /* In DWARF2, DW_FORM_ref_addr is sized by target address
9097 length, whereas in DWARF3 it's always sized as an
9098 offset. */
9099 if (dwarf_version == 2)
9100 size = DWARF2_ADDR_SIZE;
9101 else
9102 size = DWARF_OFFSET_SIZE;
9103 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
9104 name);
9107 else
9109 gcc_assert (AT_ref (a)->die_offset);
9110 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
9111 "%s", name);
9113 break;
9115 case dw_val_class_fde_ref:
9117 char l1[20];
9119 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
9120 a->dw_attr_val.v.val_fde_index * 2);
9121 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
9122 "%s", name);
9124 break;
9126 case dw_val_class_vms_delta:
9127 #ifdef ASM_OUTPUT_DWARF_VMS_DELTA
9128 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
9129 AT_vms_delta2 (a), AT_vms_delta1 (a),
9130 "%s", name);
9131 #else
9132 dw2_asm_output_delta (DWARF_OFFSET_SIZE,
9133 AT_vms_delta2 (a), AT_vms_delta1 (a),
9134 "%s", name);
9135 #endif
9136 break;
9138 case dw_val_class_lbl_id:
9139 output_attr_index_or_value (a);
9140 break;
9142 case dw_val_class_lineptr:
9143 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
9144 debug_line_section, "%s", name);
9145 break;
9147 case dw_val_class_macptr:
9148 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
9149 debug_macinfo_section, "%s", name);
9150 break;
9152 case dw_val_class_str:
9153 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
9154 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
9155 a->dw_attr_val.v.val_str->label,
9156 debug_str_section,
9157 "%s: \"%s\"", name, AT_string (a));
9158 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
9159 dw2_asm_output_data_uleb128 (AT_index (a),
9160 "%s: \"%s\"", name, AT_string (a));
9161 else
9162 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
9163 break;
9165 case dw_val_class_file:
9167 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
9169 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
9170 a->dw_attr_val.v.val_file->filename);
9171 break;
9174 case dw_val_class_data8:
9176 int i;
9178 for (i = 0; i < 8; i++)
9179 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
9180 i == 0 ? "%s" : NULL, name);
9181 break;
9184 case dw_val_class_high_pc:
9185 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
9186 get_AT_low_pc (die), "DW_AT_high_pc");
9187 break;
9189 default:
9190 gcc_unreachable ();
9194 FOR_EACH_CHILD (die, c, output_die (c));
9196 /* Add null byte to terminate sibling list. */
9197 if (die->die_child != NULL)
9198 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
9199 (unsigned long) die->die_offset);
9202 /* Output the compilation unit that appears at the beginning of the
9203 .debug_info section, and precedes the DIE descriptions. */
9205 static void
9206 output_compilation_unit_header (void)
9208 /* We don't support actual DWARFv5 units yet, we just use some
9209 DWARFv5 draft DIE tags in DWARFv4 format. */
9210 int ver = dwarf_version < 5 ? dwarf_version : 4;
9212 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9213 dw2_asm_output_data (4, 0xffffffff,
9214 "Initial length escape value indicating 64-bit DWARF extension");
9215 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9216 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
9217 "Length of Compilation Unit Info");
9218 dw2_asm_output_data (2, ver, "DWARF version number");
9219 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
9220 debug_abbrev_section,
9221 "Offset Into Abbrev. Section");
9222 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9225 /* Output the compilation unit DIE and its children. */
9227 static void
9228 output_comp_unit (dw_die_ref die, int output_if_empty)
9230 const char *secname, *oldsym;
9231 char *tmp;
9233 /* Unless we are outputting main CU, we may throw away empty ones. */
9234 if (!output_if_empty && die->die_child == NULL)
9235 return;
9237 /* Even if there are no children of this DIE, we must output the information
9238 about the compilation unit. Otherwise, on an empty translation unit, we
9239 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
9240 will then complain when examining the file. First mark all the DIEs in
9241 this CU so we know which get local refs. */
9242 mark_dies (die);
9244 external_ref_hash_type *extern_map = optimize_external_refs (die);
9246 build_abbrev_table (die, extern_map);
9248 delete extern_map;
9250 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
9251 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
9252 calc_die_sizes (die);
9254 oldsym = die->die_id.die_symbol;
9255 if (oldsym)
9257 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
9259 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
9260 secname = tmp;
9261 die->die_id.die_symbol = NULL;
9262 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9264 else
9266 switch_to_section (debug_info_section);
9267 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
9268 info_section_emitted = true;
9271 /* Output debugging information. */
9272 output_compilation_unit_header ();
9273 output_die (die);
9275 /* Leave the marks on the main CU, so we can check them in
9276 output_pubnames. */
9277 if (oldsym)
9279 unmark_dies (die);
9280 die->die_id.die_symbol = oldsym;
9284 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
9285 and .debug_pubtypes. This is configured per-target, but can be
9286 overridden by the -gpubnames or -gno-pubnames options. */
9288 static inline bool
9289 want_pubnames (void)
9291 if (debug_info_level <= DINFO_LEVEL_TERSE)
9292 return false;
9293 if (debug_generate_pub_sections != -1)
9294 return debug_generate_pub_sections;
9295 return targetm.want_debug_pub_sections;
9298 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
9300 static void
9301 add_AT_pubnames (dw_die_ref die)
9303 if (want_pubnames ())
9304 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
9307 /* Add a string attribute value to a skeleton DIE. */
9309 static inline void
9310 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
9311 const char *str)
9313 dw_attr_node attr;
9314 struct indirect_string_node *node;
9316 if (! skeleton_debug_str_hash)
9317 skeleton_debug_str_hash
9318 = hash_table<indirect_string_hasher>::create_ggc (10);
9320 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
9321 find_string_form (node);
9322 if (node->form == DW_FORM_GNU_str_index)
9323 node->form = DW_FORM_strp;
9325 attr.dw_attr = attr_kind;
9326 attr.dw_attr_val.val_class = dw_val_class_str;
9327 attr.dw_attr_val.val_entry = NULL;
9328 attr.dw_attr_val.v.val_str = node;
9329 add_dwarf_attr (die, &attr);
9332 /* Helper function to generate top-level dies for skeleton debug_info and
9333 debug_types. */
9335 static void
9336 add_top_level_skeleton_die_attrs (dw_die_ref die)
9338 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
9339 const char *comp_dir = comp_dir_string ();
9341 add_skeleton_AT_string (die, DW_AT_GNU_dwo_name, dwo_file_name);
9342 if (comp_dir != NULL)
9343 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
9344 add_AT_pubnames (die);
9345 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
9348 /* Output skeleton debug sections that point to the dwo file. */
9350 static void
9351 output_skeleton_debug_sections (dw_die_ref comp_unit)
9353 /* We don't support actual DWARFv5 units yet, we just use some
9354 DWARFv5 draft DIE tags in DWARFv4 format. */
9355 int ver = dwarf_version < 5 ? dwarf_version : 4;
9357 /* These attributes will be found in the full debug_info section. */
9358 remove_AT (comp_unit, DW_AT_producer);
9359 remove_AT (comp_unit, DW_AT_language);
9361 switch_to_section (debug_skeleton_info_section);
9362 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
9364 /* Produce the skeleton compilation-unit header. This one differs enough from
9365 a normal CU header that it's better not to call output_compilation_unit
9366 header. */
9367 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9368 dw2_asm_output_data (4, 0xffffffff,
9369 "Initial length escape value indicating 64-bit DWARF extension");
9371 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9372 DWARF_COMPILE_UNIT_HEADER_SIZE
9373 - DWARF_INITIAL_LENGTH_SIZE
9374 + size_of_die (comp_unit),
9375 "Length of Compilation Unit Info");
9376 dw2_asm_output_data (2, ver, "DWARF version number");
9377 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
9378 debug_abbrev_section,
9379 "Offset Into Abbrev. Section");
9380 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9382 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
9383 output_die (comp_unit);
9385 /* Build the skeleton debug_abbrev section. */
9386 switch_to_section (debug_skeleton_abbrev_section);
9387 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
9389 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
9391 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
9394 /* Output a comdat type unit DIE and its children. */
9396 static void
9397 output_comdat_type_unit (comdat_type_node *node)
9399 const char *secname;
9400 char *tmp;
9401 int i;
9402 #if defined (OBJECT_FORMAT_ELF)
9403 tree comdat_key;
9404 #endif
9406 /* First mark all the DIEs in this CU so we know which get local refs. */
9407 mark_dies (node->root_die);
9409 external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
9411 build_abbrev_table (node->root_die, extern_map);
9413 delete extern_map;
9414 extern_map = NULL;
9416 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
9417 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
9418 calc_die_sizes (node->root_die);
9420 #if defined (OBJECT_FORMAT_ELF)
9421 if (!dwarf_split_debug_info)
9422 secname = ".debug_types";
9423 else
9424 secname = ".debug_types.dwo";
9426 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9427 sprintf (tmp, "wt.");
9428 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9429 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
9430 comdat_key = get_identifier (tmp);
9431 targetm.asm_out.named_section (secname,
9432 SECTION_DEBUG | SECTION_LINKONCE,
9433 comdat_key);
9434 #else
9435 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9436 sprintf (tmp, ".gnu.linkonce.wt.");
9437 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9438 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
9439 secname = tmp;
9440 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9441 #endif
9443 /* Output debugging information. */
9444 output_compilation_unit_header ();
9445 output_signature (node->signature, "Type Signature");
9446 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
9447 "Offset to Type DIE");
9448 output_die (node->root_die);
9450 unmark_dies (node->root_die);
9453 /* Return the DWARF2/3 pubname associated with a decl. */
9455 static const char *
9456 dwarf2_name (tree decl, int scope)
9458 if (DECL_NAMELESS (decl))
9459 return NULL;
9460 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
9463 /* Add a new entry to .debug_pubnames if appropriate. */
9465 static void
9466 add_pubname_string (const char *str, dw_die_ref die)
9468 pubname_entry e;
9470 e.die = die;
9471 e.name = xstrdup (str);
9472 vec_safe_push (pubname_table, e);
9475 static void
9476 add_pubname (tree decl, dw_die_ref die)
9478 if (!want_pubnames ())
9479 return;
9481 /* Don't add items to the table when we expect that the consumer will have
9482 just read the enclosing die. For example, if the consumer is looking at a
9483 class_member, it will either be inside the class already, or will have just
9484 looked up the class to find the member. Either way, searching the class is
9485 faster than searching the index. */
9486 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
9487 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9489 const char *name = dwarf2_name (decl, 1);
9491 if (name)
9492 add_pubname_string (name, die);
9496 /* Add an enumerator to the pubnames section. */
9498 static void
9499 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
9501 pubname_entry e;
9503 gcc_assert (scope_name);
9504 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
9505 e.die = die;
9506 vec_safe_push (pubname_table, e);
9509 /* Add a new entry to .debug_pubtypes if appropriate. */
9511 static void
9512 add_pubtype (tree decl, dw_die_ref die)
9514 pubname_entry e;
9516 if (!want_pubnames ())
9517 return;
9519 if ((TREE_PUBLIC (decl)
9520 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9521 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
9523 tree scope = NULL;
9524 const char *scope_name = "";
9525 const char *sep = is_cxx () ? "::" : ".";
9526 const char *name;
9528 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
9529 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
9531 scope_name = lang_hooks.dwarf_name (scope, 1);
9532 if (scope_name != NULL && scope_name[0] != '\0')
9533 scope_name = concat (scope_name, sep, NULL);
9534 else
9535 scope_name = "";
9538 if (TYPE_P (decl))
9539 name = type_tag (decl);
9540 else
9541 name = lang_hooks.dwarf_name (decl, 1);
9543 /* If we don't have a name for the type, there's no point in adding
9544 it to the table. */
9545 if (name != NULL && name[0] != '\0')
9547 e.die = die;
9548 e.name = concat (scope_name, name, NULL);
9549 vec_safe_push (pubtype_table, e);
9552 /* Although it might be more consistent to add the pubinfo for the
9553 enumerators as their dies are created, they should only be added if the
9554 enum type meets the criteria above. So rather than re-check the parent
9555 enum type whenever an enumerator die is created, just output them all
9556 here. This isn't protected by the name conditional because anonymous
9557 enums don't have names. */
9558 if (die->die_tag == DW_TAG_enumeration_type)
9560 dw_die_ref c;
9562 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
9567 /* Output a single entry in the pubnames table. */
9569 static void
9570 output_pubname (dw_offset die_offset, pubname_entry *entry)
9572 dw_die_ref die = entry->die;
9573 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
9575 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
9577 if (debug_generate_pub_sections == 2)
9579 /* This logic follows gdb's method for determining the value of the flag
9580 byte. */
9581 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
9582 switch (die->die_tag)
9584 case DW_TAG_typedef:
9585 case DW_TAG_base_type:
9586 case DW_TAG_subrange_type:
9587 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9588 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9589 break;
9590 case DW_TAG_enumerator:
9591 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9592 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9593 if (!is_cxx () && !is_java ())
9594 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9595 break;
9596 case DW_TAG_subprogram:
9597 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9598 GDB_INDEX_SYMBOL_KIND_FUNCTION);
9599 if (!is_ada ())
9600 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9601 break;
9602 case DW_TAG_constant:
9603 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9604 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9605 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9606 break;
9607 case DW_TAG_variable:
9608 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9609 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9610 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9611 break;
9612 case DW_TAG_namespace:
9613 case DW_TAG_imported_declaration:
9614 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9615 break;
9616 case DW_TAG_class_type:
9617 case DW_TAG_interface_type:
9618 case DW_TAG_structure_type:
9619 case DW_TAG_union_type:
9620 case DW_TAG_enumeration_type:
9621 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9622 if (!is_cxx () && !is_java ())
9623 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9624 break;
9625 default:
9626 /* An unusual tag. Leave the flag-byte empty. */
9627 break;
9629 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
9630 "GDB-index flags");
9633 dw2_asm_output_nstring (entry->name, -1, "external name");
9637 /* Output the public names table used to speed up access to externally
9638 visible names; or the public types table used to find type definitions. */
9640 static void
9641 output_pubnames (vec<pubname_entry, va_gc> *names)
9643 unsigned i;
9644 unsigned long pubnames_length = size_of_pubnames (names);
9645 pubname_ref pub;
9647 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9648 dw2_asm_output_data (4, 0xffffffff,
9649 "Initial length escape value indicating 64-bit DWARF extension");
9650 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length, "Pub Info Length");
9652 /* Version number for pubnames/pubtypes is independent of dwarf version. */
9653 dw2_asm_output_data (2, 2, "DWARF Version");
9655 if (dwarf_split_debug_info)
9656 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9657 debug_skeleton_info_section,
9658 "Offset of Compilation Unit Info");
9659 else
9660 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9661 debug_info_section,
9662 "Offset of Compilation Unit Info");
9663 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
9664 "Compilation Unit Length");
9666 FOR_EACH_VEC_ELT (*names, i, pub)
9668 if (include_pubname_in_output (names, pub))
9670 dw_offset die_offset = pub->die->die_offset;
9672 /* We shouldn't see pubnames for DIEs outside of the main CU. */
9673 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
9674 gcc_assert (pub->die->die_mark);
9676 /* If we're putting types in their own .debug_types sections,
9677 the .debug_pubtypes table will still point to the compile
9678 unit (not the type unit), so we want to use the offset of
9679 the skeleton DIE (if there is one). */
9680 if (pub->die->comdat_type_p && names == pubtype_table)
9682 comdat_type_node_ref type_node = pub->die->die_id.die_type_node;
9684 if (type_node != NULL)
9685 die_offset = (type_node->skeleton_die != NULL
9686 ? type_node->skeleton_die->die_offset
9687 : comp_unit_die ()->die_offset);
9690 output_pubname (die_offset, pub);
9694 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
9697 /* Output public names and types tables if necessary. */
9699 static void
9700 output_pubtables (void)
9702 if (!want_pubnames () || !info_section_emitted)
9703 return;
9705 switch_to_section (debug_pubnames_section);
9706 output_pubnames (pubname_table);
9707 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
9708 It shouldn't hurt to emit it always, since pure DWARF2 consumers
9709 simply won't look for the section. */
9710 switch_to_section (debug_pubtypes_section);
9711 output_pubnames (pubtype_table);
9715 /* Output the information that goes into the .debug_aranges table.
9716 Namely, define the beginning and ending address range of the
9717 text section generated for this compilation unit. */
9719 static void
9720 output_aranges (unsigned long aranges_length)
9722 unsigned i;
9724 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9725 dw2_asm_output_data (4, 0xffffffff,
9726 "Initial length escape value indicating 64-bit DWARF extension");
9727 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
9728 "Length of Address Ranges Info");
9729 /* Version number for aranges is still 2, even up to DWARF5. */
9730 dw2_asm_output_data (2, 2, "DWARF Version");
9731 if (dwarf_split_debug_info)
9732 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9733 debug_skeleton_info_section,
9734 "Offset of Compilation Unit Info");
9735 else
9736 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9737 debug_info_section,
9738 "Offset of Compilation Unit Info");
9739 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
9740 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
9742 /* We need to align to twice the pointer size here. */
9743 if (DWARF_ARANGES_PAD_SIZE)
9745 /* Pad using a 2 byte words so that padding is correct for any
9746 pointer size. */
9747 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
9748 2 * DWARF2_ADDR_SIZE);
9749 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
9750 dw2_asm_output_data (2, 0, NULL);
9753 /* It is necessary not to output these entries if the sections were
9754 not used; if the sections were not used, the length will be 0 and
9755 the address may end up as 0 if the section is discarded by ld
9756 --gc-sections, leaving an invalid (0, 0) entry that can be
9757 confused with the terminator. */
9758 if (text_section_used)
9760 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
9761 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
9762 text_section_label, "Length");
9764 if (cold_text_section_used)
9766 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
9767 "Address");
9768 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
9769 cold_text_section_label, "Length");
9772 if (have_multiple_function_sections)
9774 unsigned fde_idx;
9775 dw_fde_ref fde;
9777 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9779 if (DECL_IGNORED_P (fde->decl))
9780 continue;
9781 if (!fde->in_std_section)
9783 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
9784 "Address");
9785 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
9786 fde->dw_fde_begin, "Length");
9788 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9790 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
9791 "Address");
9792 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
9793 fde->dw_fde_second_begin, "Length");
9798 /* Output the terminator words. */
9799 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9800 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9803 /* Add a new entry to .debug_ranges. Return the offset at which it
9804 was placed. */
9806 static unsigned int
9807 add_ranges_num (int num)
9809 unsigned int in_use = ranges_table_in_use;
9811 if (in_use == ranges_table_allocated)
9813 ranges_table_allocated += RANGES_TABLE_INCREMENT;
9814 ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
9815 ranges_table_allocated);
9816 memset (ranges_table + ranges_table_in_use, 0,
9817 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
9820 ranges_table[in_use].num = num;
9821 ranges_table_in_use = in_use + 1;
9823 return in_use * 2 * DWARF2_ADDR_SIZE;
9826 /* Add a new entry to .debug_ranges corresponding to a block, or a
9827 range terminator if BLOCK is NULL. */
9829 static unsigned int
9830 add_ranges (const_tree block)
9832 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
9835 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
9836 When using dwarf_split_debug_info, address attributes in dies destined
9837 for the final executable should be direct references--setting the
9838 parameter force_direct ensures this behavior. */
9840 static void
9841 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
9842 bool *added, bool force_direct)
9844 unsigned int in_use = ranges_by_label_in_use;
9845 unsigned int offset;
9847 if (in_use == ranges_by_label_allocated)
9849 ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
9850 ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
9851 ranges_by_label,
9852 ranges_by_label_allocated);
9853 memset (ranges_by_label + ranges_by_label_in_use, 0,
9854 RANGES_TABLE_INCREMENT
9855 * sizeof (struct dw_ranges_by_label_struct));
9858 ranges_by_label[in_use].begin = begin;
9859 ranges_by_label[in_use].end = end;
9860 ranges_by_label_in_use = in_use + 1;
9862 offset = add_ranges_num (-(int)in_use - 1);
9863 if (!*added)
9865 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
9866 *added = true;
9870 static void
9871 output_ranges (void)
9873 unsigned i;
9874 static const char *const start_fmt = "Offset %#x";
9875 const char *fmt = start_fmt;
9877 for (i = 0; i < ranges_table_in_use; i++)
9879 int block_num = ranges_table[i].num;
9881 if (block_num > 0)
9883 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
9884 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
9886 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
9887 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
9889 /* If all code is in the text section, then the compilation
9890 unit base address defaults to DW_AT_low_pc, which is the
9891 base of the text section. */
9892 if (!have_multiple_function_sections)
9894 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
9895 text_section_label,
9896 fmt, i * 2 * DWARF2_ADDR_SIZE);
9897 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
9898 text_section_label, NULL);
9901 /* Otherwise, the compilation unit base address is zero,
9902 which allows us to use absolute addresses, and not worry
9903 about whether the target supports cross-section
9904 arithmetic. */
9905 else
9907 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
9908 fmt, i * 2 * DWARF2_ADDR_SIZE);
9909 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
9912 fmt = NULL;
9915 /* Negative block_num stands for an index into ranges_by_label. */
9916 else if (block_num < 0)
9918 int lab_idx = - block_num - 1;
9920 if (!have_multiple_function_sections)
9922 gcc_unreachable ();
9923 #if 0
9924 /* If we ever use add_ranges_by_labels () for a single
9925 function section, all we have to do is to take out
9926 the #if 0 above. */
9927 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9928 ranges_by_label[lab_idx].begin,
9929 text_section_label,
9930 fmt, i * 2 * DWARF2_ADDR_SIZE);
9931 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9932 ranges_by_label[lab_idx].end,
9933 text_section_label, NULL);
9934 #endif
9936 else
9938 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9939 ranges_by_label[lab_idx].begin,
9940 fmt, i * 2 * DWARF2_ADDR_SIZE);
9941 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9942 ranges_by_label[lab_idx].end,
9943 NULL);
9946 else
9948 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9949 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9950 fmt = start_fmt;
9955 /* Data structure containing information about input files. */
9956 struct file_info
9958 const char *path; /* Complete file name. */
9959 const char *fname; /* File name part. */
9960 int length; /* Length of entire string. */
9961 struct dwarf_file_data * file_idx; /* Index in input file table. */
9962 int dir_idx; /* Index in directory table. */
9965 /* Data structure containing information about directories with source
9966 files. */
9967 struct dir_info
9969 const char *path; /* Path including directory name. */
9970 int length; /* Path length. */
9971 int prefix; /* Index of directory entry which is a prefix. */
9972 int count; /* Number of files in this directory. */
9973 int dir_idx; /* Index of directory used as base. */
9976 /* Callback function for file_info comparison. We sort by looking at
9977 the directories in the path. */
9979 static int
9980 file_info_cmp (const void *p1, const void *p2)
9982 const struct file_info *const s1 = (const struct file_info *) p1;
9983 const struct file_info *const s2 = (const struct file_info *) p2;
9984 const unsigned char *cp1;
9985 const unsigned char *cp2;
9987 /* Take care of file names without directories. We need to make sure that
9988 we return consistent values to qsort since some will get confused if
9989 we return the same value when identical operands are passed in opposite
9990 orders. So if neither has a directory, return 0 and otherwise return
9991 1 or -1 depending on which one has the directory. */
9992 if ((s1->path == s1->fname || s2->path == s2->fname))
9993 return (s2->path == s2->fname) - (s1->path == s1->fname);
9995 cp1 = (const unsigned char *) s1->path;
9996 cp2 = (const unsigned char *) s2->path;
9998 while (1)
10000 ++cp1;
10001 ++cp2;
10002 /* Reached the end of the first path? If so, handle like above. */
10003 if ((cp1 == (const unsigned char *) s1->fname)
10004 || (cp2 == (const unsigned char *) s2->fname))
10005 return ((cp2 == (const unsigned char *) s2->fname)
10006 - (cp1 == (const unsigned char *) s1->fname));
10008 /* Character of current path component the same? */
10009 else if (*cp1 != *cp2)
10010 return *cp1 - *cp2;
10014 struct file_name_acquire_data
10016 struct file_info *files;
10017 int used_files;
10018 int max_files;
10021 /* Traversal function for the hash table. */
10024 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
10026 struct dwarf_file_data *d = *slot;
10027 struct file_info *fi;
10028 const char *f;
10030 gcc_assert (fnad->max_files >= d->emitted_number);
10032 if (! d->emitted_number)
10033 return 1;
10035 gcc_assert (fnad->max_files != fnad->used_files);
10037 fi = fnad->files + fnad->used_files++;
10039 /* Skip all leading "./". */
10040 f = d->filename;
10041 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
10042 f += 2;
10044 /* Create a new array entry. */
10045 fi->path = f;
10046 fi->length = strlen (f);
10047 fi->file_idx = d;
10049 /* Search for the file name part. */
10050 f = strrchr (f, DIR_SEPARATOR);
10051 #if defined (DIR_SEPARATOR_2)
10053 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
10055 if (g != NULL)
10057 if (f == NULL || f < g)
10058 f = g;
10061 #endif
10063 fi->fname = f == NULL ? fi->path : f + 1;
10064 return 1;
10067 /* Output the directory table and the file name table. We try to minimize
10068 the total amount of memory needed. A heuristic is used to avoid large
10069 slowdowns with many input files. */
10071 static void
10072 output_file_names (void)
10074 struct file_name_acquire_data fnad;
10075 int numfiles;
10076 struct file_info *files;
10077 struct dir_info *dirs;
10078 int *saved;
10079 int *savehere;
10080 int *backmap;
10081 int ndirs;
10082 int idx_offset;
10083 int i;
10085 if (!last_emitted_file)
10087 dw2_asm_output_data (1, 0, "End directory table");
10088 dw2_asm_output_data (1, 0, "End file name table");
10089 return;
10092 numfiles = last_emitted_file->emitted_number;
10094 /* Allocate the various arrays we need. */
10095 files = XALLOCAVEC (struct file_info, numfiles);
10096 dirs = XALLOCAVEC (struct dir_info, numfiles);
10098 fnad.files = files;
10099 fnad.used_files = 0;
10100 fnad.max_files = numfiles;
10101 file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
10102 gcc_assert (fnad.used_files == fnad.max_files);
10104 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
10106 /* Find all the different directories used. */
10107 dirs[0].path = files[0].path;
10108 dirs[0].length = files[0].fname - files[0].path;
10109 dirs[0].prefix = -1;
10110 dirs[0].count = 1;
10111 dirs[0].dir_idx = 0;
10112 files[0].dir_idx = 0;
10113 ndirs = 1;
10115 for (i = 1; i < numfiles; i++)
10116 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
10117 && memcmp (dirs[ndirs - 1].path, files[i].path,
10118 dirs[ndirs - 1].length) == 0)
10120 /* Same directory as last entry. */
10121 files[i].dir_idx = ndirs - 1;
10122 ++dirs[ndirs - 1].count;
10124 else
10126 int j;
10128 /* This is a new directory. */
10129 dirs[ndirs].path = files[i].path;
10130 dirs[ndirs].length = files[i].fname - files[i].path;
10131 dirs[ndirs].count = 1;
10132 dirs[ndirs].dir_idx = ndirs;
10133 files[i].dir_idx = ndirs;
10135 /* Search for a prefix. */
10136 dirs[ndirs].prefix = -1;
10137 for (j = 0; j < ndirs; j++)
10138 if (dirs[j].length < dirs[ndirs].length
10139 && dirs[j].length > 1
10140 && (dirs[ndirs].prefix == -1
10141 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
10142 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
10143 dirs[ndirs].prefix = j;
10145 ++ndirs;
10148 /* Now to the actual work. We have to find a subset of the directories which
10149 allow expressing the file name using references to the directory table
10150 with the least amount of characters. We do not do an exhaustive search
10151 where we would have to check out every combination of every single
10152 possible prefix. Instead we use a heuristic which provides nearly optimal
10153 results in most cases and never is much off. */
10154 saved = XALLOCAVEC (int, ndirs);
10155 savehere = XALLOCAVEC (int, ndirs);
10157 memset (saved, '\0', ndirs * sizeof (saved[0]));
10158 for (i = 0; i < ndirs; i++)
10160 int j;
10161 int total;
10163 /* We can always save some space for the current directory. But this
10164 does not mean it will be enough to justify adding the directory. */
10165 savehere[i] = dirs[i].length;
10166 total = (savehere[i] - saved[i]) * dirs[i].count;
10168 for (j = i + 1; j < ndirs; j++)
10170 savehere[j] = 0;
10171 if (saved[j] < dirs[i].length)
10173 /* Determine whether the dirs[i] path is a prefix of the
10174 dirs[j] path. */
10175 int k;
10177 k = dirs[j].prefix;
10178 while (k != -1 && k != (int) i)
10179 k = dirs[k].prefix;
10181 if (k == (int) i)
10183 /* Yes it is. We can possibly save some memory by
10184 writing the filenames in dirs[j] relative to
10185 dirs[i]. */
10186 savehere[j] = dirs[i].length;
10187 total += (savehere[j] - saved[j]) * dirs[j].count;
10192 /* Check whether we can save enough to justify adding the dirs[i]
10193 directory. */
10194 if (total > dirs[i].length + 1)
10196 /* It's worthwhile adding. */
10197 for (j = i; j < ndirs; j++)
10198 if (savehere[j] > 0)
10200 /* Remember how much we saved for this directory so far. */
10201 saved[j] = savehere[j];
10203 /* Remember the prefix directory. */
10204 dirs[j].dir_idx = i;
10209 /* Emit the directory name table. */
10210 idx_offset = dirs[0].length > 0 ? 1 : 0;
10211 for (i = 1 - idx_offset; i < ndirs; i++)
10212 dw2_asm_output_nstring (dirs[i].path,
10213 dirs[i].length
10214 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
10215 "Directory Entry: %#x", i + idx_offset);
10217 dw2_asm_output_data (1, 0, "End directory table");
10219 /* We have to emit them in the order of emitted_number since that's
10220 used in the debug info generation. To do this efficiently we
10221 generate a back-mapping of the indices first. */
10222 backmap = XALLOCAVEC (int, numfiles);
10223 for (i = 0; i < numfiles; i++)
10224 backmap[files[i].file_idx->emitted_number - 1] = i;
10226 /* Now write all the file names. */
10227 for (i = 0; i < numfiles; i++)
10229 int file_idx = backmap[i];
10230 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
10232 #ifdef VMS_DEBUGGING_INFO
10233 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
10235 /* Setting these fields can lead to debugger miscomparisons,
10236 but VMS Debug requires them to be set correctly. */
10238 int ver;
10239 long long cdt;
10240 long siz;
10241 int maxfilelen = strlen (files[file_idx].path)
10242 + dirs[dir_idx].length
10243 + MAX_VMS_VERSION_LEN + 1;
10244 char *filebuf = XALLOCAVEC (char, maxfilelen);
10246 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
10247 snprintf (filebuf, maxfilelen, "%s;%d",
10248 files[file_idx].path + dirs[dir_idx].length, ver);
10250 dw2_asm_output_nstring
10251 (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
10253 /* Include directory index. */
10254 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10256 /* Modification time. */
10257 dw2_asm_output_data_uleb128
10258 ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
10259 ? cdt : 0,
10260 NULL);
10262 /* File length in bytes. */
10263 dw2_asm_output_data_uleb128
10264 ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
10265 ? siz : 0,
10266 NULL);
10267 #else
10268 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
10269 "File Entry: %#x", (unsigned) i + 1);
10271 /* Include directory index. */
10272 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10274 /* Modification time. */
10275 dw2_asm_output_data_uleb128 (0, NULL);
10277 /* File length in bytes. */
10278 dw2_asm_output_data_uleb128 (0, NULL);
10279 #endif /* VMS_DEBUGGING_INFO */
10282 dw2_asm_output_data (1, 0, "End file name table");
10286 /* Output one line number table into the .debug_line section. */
10288 static void
10289 output_one_line_info_table (dw_line_info_table *table)
10291 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
10292 unsigned int current_line = 1;
10293 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
10294 dw_line_info_entry *ent;
10295 size_t i;
10297 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
10299 switch (ent->opcode)
10301 case LI_set_address:
10302 /* ??? Unfortunately, we have little choice here currently, and
10303 must always use the most general form. GCC does not know the
10304 address delta itself, so we can't use DW_LNS_advance_pc. Many
10305 ports do have length attributes which will give an upper bound
10306 on the address range. We could perhaps use length attributes
10307 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
10308 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
10310 /* This can handle any delta. This takes
10311 4+DWARF2_ADDR_SIZE bytes. */
10312 dw2_asm_output_data (1, 0, "set address %s", line_label);
10313 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10314 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10315 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
10316 break;
10318 case LI_set_line:
10319 if (ent->val == current_line)
10321 /* We still need to start a new row, so output a copy insn. */
10322 dw2_asm_output_data (1, DW_LNS_copy,
10323 "copy line %u", current_line);
10325 else
10327 int line_offset = ent->val - current_line;
10328 int line_delta = line_offset - DWARF_LINE_BASE;
10330 current_line = ent->val;
10331 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
10333 /* This can handle deltas from -10 to 234, using the current
10334 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
10335 This takes 1 byte. */
10336 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
10337 "line %u", current_line);
10339 else
10341 /* This can handle any delta. This takes at least 4 bytes,
10342 depending on the value being encoded. */
10343 dw2_asm_output_data (1, DW_LNS_advance_line,
10344 "advance to line %u", current_line);
10345 dw2_asm_output_data_sleb128 (line_offset, NULL);
10346 dw2_asm_output_data (1, DW_LNS_copy, NULL);
10349 break;
10351 case LI_set_file:
10352 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
10353 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10354 break;
10356 case LI_set_column:
10357 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
10358 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10359 break;
10361 case LI_negate_stmt:
10362 current_is_stmt = !current_is_stmt;
10363 dw2_asm_output_data (1, DW_LNS_negate_stmt,
10364 "is_stmt %d", current_is_stmt);
10365 break;
10367 case LI_set_prologue_end:
10368 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
10369 "set prologue end");
10370 break;
10372 case LI_set_epilogue_begin:
10373 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
10374 "set epilogue begin");
10375 break;
10377 case LI_set_discriminator:
10378 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
10379 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
10380 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
10381 dw2_asm_output_data_uleb128 (ent->val, NULL);
10382 break;
10386 /* Emit debug info for the address of the end of the table. */
10387 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
10388 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10389 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10390 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
10392 dw2_asm_output_data (1, 0, "end sequence");
10393 dw2_asm_output_data_uleb128 (1, NULL);
10394 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
10397 /* Output the source line number correspondence information. This
10398 information goes into the .debug_line section. */
10400 static void
10401 output_line_info (bool prologue_only)
10403 char l1[20], l2[20], p1[20], p2[20];
10404 /* We don't support DWARFv5 line tables yet. */
10405 int ver = dwarf_version < 5 ? dwarf_version : 4;
10406 bool saw_one = false;
10407 int opc;
10409 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
10410 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
10411 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
10412 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
10414 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10415 dw2_asm_output_data (4, 0xffffffff,
10416 "Initial length escape value indicating 64-bit DWARF extension");
10417 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
10418 "Length of Source Line Info");
10419 ASM_OUTPUT_LABEL (asm_out_file, l1);
10421 dw2_asm_output_data (2, ver, "DWARF Version");
10422 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
10423 ASM_OUTPUT_LABEL (asm_out_file, p1);
10425 /* Define the architecture-dependent minimum instruction length (in bytes).
10426 In this implementation of DWARF, this field is used for information
10427 purposes only. Since GCC generates assembly language, we have no
10428 a priori knowledge of how many instruction bytes are generated for each
10429 source line, and therefore can use only the DW_LNE_set_address and
10430 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
10431 this as '1', which is "correct enough" for all architectures,
10432 and don't let the target override. */
10433 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
10435 if (ver >= 4)
10436 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
10437 "Maximum Operations Per Instruction");
10438 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
10439 "Default is_stmt_start flag");
10440 dw2_asm_output_data (1, DWARF_LINE_BASE,
10441 "Line Base Value (Special Opcodes)");
10442 dw2_asm_output_data (1, DWARF_LINE_RANGE,
10443 "Line Range Value (Special Opcodes)");
10444 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
10445 "Special Opcode Base");
10447 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
10449 int n_op_args;
10450 switch (opc)
10452 case DW_LNS_advance_pc:
10453 case DW_LNS_advance_line:
10454 case DW_LNS_set_file:
10455 case DW_LNS_set_column:
10456 case DW_LNS_fixed_advance_pc:
10457 case DW_LNS_set_isa:
10458 n_op_args = 1;
10459 break;
10460 default:
10461 n_op_args = 0;
10462 break;
10465 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
10466 opc, n_op_args);
10469 /* Write out the information about the files we use. */
10470 output_file_names ();
10471 ASM_OUTPUT_LABEL (asm_out_file, p2);
10472 if (prologue_only)
10474 /* Output the marker for the end of the line number info. */
10475 ASM_OUTPUT_LABEL (asm_out_file, l2);
10476 return;
10479 if (separate_line_info)
10481 dw_line_info_table *table;
10482 size_t i;
10484 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
10485 if (table->in_use)
10487 output_one_line_info_table (table);
10488 saw_one = true;
10491 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
10493 output_one_line_info_table (cold_text_section_line_info);
10494 saw_one = true;
10497 /* ??? Some Darwin linkers crash on a .debug_line section with no
10498 sequences. Further, merely a DW_LNE_end_sequence entry is not
10499 sufficient -- the address column must also be initialized.
10500 Make sure to output at least one set_address/end_sequence pair,
10501 choosing .text since that section is always present. */
10502 if (text_section_line_info->in_use || !saw_one)
10503 output_one_line_info_table (text_section_line_info);
10505 /* Output the marker for the end of the line number info. */
10506 ASM_OUTPUT_LABEL (asm_out_file, l2);
10509 /* Given a pointer to a tree node for some base type, return a pointer to
10510 a DIE that describes the given type.
10512 This routine must only be called for GCC type nodes that correspond to
10513 Dwarf base (fundamental) types. */
10515 static dw_die_ref
10516 base_type_die (tree type)
10518 dw_die_ref base_type_result;
10519 enum dwarf_type encoding;
10521 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
10522 return 0;
10524 /* If this is a subtype that should not be emitted as a subrange type,
10525 use the base type. See subrange_type_for_debug_p. */
10526 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
10527 type = TREE_TYPE (type);
10529 switch (TREE_CODE (type))
10531 case INTEGER_TYPE:
10532 if ((dwarf_version >= 4 || !dwarf_strict)
10533 && TYPE_NAME (type)
10534 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10535 && DECL_IS_BUILTIN (TYPE_NAME (type))
10536 && DECL_NAME (TYPE_NAME (type)))
10538 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
10539 if (strcmp (name, "char16_t") == 0
10540 || strcmp (name, "char32_t") == 0)
10542 encoding = DW_ATE_UTF;
10543 break;
10546 if (TYPE_STRING_FLAG (type))
10548 if (TYPE_UNSIGNED (type))
10549 encoding = DW_ATE_unsigned_char;
10550 else
10551 encoding = DW_ATE_signed_char;
10553 else if (TYPE_UNSIGNED (type))
10554 encoding = DW_ATE_unsigned;
10555 else
10556 encoding = DW_ATE_signed;
10557 break;
10559 case REAL_TYPE:
10560 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
10562 if (dwarf_version >= 3 || !dwarf_strict)
10563 encoding = DW_ATE_decimal_float;
10564 else
10565 encoding = DW_ATE_lo_user;
10567 else
10568 encoding = DW_ATE_float;
10569 break;
10571 case FIXED_POINT_TYPE:
10572 if (!(dwarf_version >= 3 || !dwarf_strict))
10573 encoding = DW_ATE_lo_user;
10574 else if (TYPE_UNSIGNED (type))
10575 encoding = DW_ATE_unsigned_fixed;
10576 else
10577 encoding = DW_ATE_signed_fixed;
10578 break;
10580 /* Dwarf2 doesn't know anything about complex ints, so use
10581 a user defined type for it. */
10582 case COMPLEX_TYPE:
10583 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
10584 encoding = DW_ATE_complex_float;
10585 else
10586 encoding = DW_ATE_lo_user;
10587 break;
10589 case BOOLEAN_TYPE:
10590 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
10591 encoding = DW_ATE_boolean;
10592 break;
10594 default:
10595 /* No other TREE_CODEs are Dwarf fundamental types. */
10596 gcc_unreachable ();
10599 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
10601 add_AT_unsigned (base_type_result, DW_AT_byte_size,
10602 int_size_in_bytes (type));
10603 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
10604 add_pubtype (type, base_type_result);
10606 return base_type_result;
10609 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
10610 named 'auto' in its type: return true for it, false otherwise. */
10612 static inline bool
10613 is_cxx_auto (tree type)
10615 if (is_cxx ())
10617 tree name = TYPE_IDENTIFIER (type);
10618 if (name == get_identifier ("auto")
10619 || name == get_identifier ("decltype(auto)"))
10620 return true;
10622 return false;
10625 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
10626 given input type is a Dwarf "fundamental" type. Otherwise return null. */
10628 static inline int
10629 is_base_type (tree type)
10631 switch (TREE_CODE (type))
10633 case ERROR_MARK:
10634 case VOID_TYPE:
10635 case INTEGER_TYPE:
10636 case REAL_TYPE:
10637 case FIXED_POINT_TYPE:
10638 case COMPLEX_TYPE:
10639 case BOOLEAN_TYPE:
10640 case POINTER_BOUNDS_TYPE:
10641 return 1;
10643 case ARRAY_TYPE:
10644 case RECORD_TYPE:
10645 case UNION_TYPE:
10646 case QUAL_UNION_TYPE:
10647 case ENUMERAL_TYPE:
10648 case FUNCTION_TYPE:
10649 case METHOD_TYPE:
10650 case POINTER_TYPE:
10651 case REFERENCE_TYPE:
10652 case NULLPTR_TYPE:
10653 case OFFSET_TYPE:
10654 case LANG_TYPE:
10655 case VECTOR_TYPE:
10656 return 0;
10658 default:
10659 if (is_cxx_auto (type))
10660 return 0;
10661 gcc_unreachable ();
10664 return 0;
10667 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
10668 node, return the size in bits for the type if it is a constant, or else
10669 return the alignment for the type if the type's size is not constant, or
10670 else return BITS_PER_WORD if the type actually turns out to be an
10671 ERROR_MARK node. */
10673 static inline unsigned HOST_WIDE_INT
10674 simple_type_size_in_bits (const_tree type)
10676 if (TREE_CODE (type) == ERROR_MARK)
10677 return BITS_PER_WORD;
10678 else if (TYPE_SIZE (type) == NULL_TREE)
10679 return 0;
10680 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
10681 return tree_to_uhwi (TYPE_SIZE (type));
10682 else
10683 return TYPE_ALIGN (type);
10686 /* Similarly, but return an offset_int instead of UHWI. */
10688 static inline offset_int
10689 offset_int_type_size_in_bits (const_tree type)
10691 if (TREE_CODE (type) == ERROR_MARK)
10692 return BITS_PER_WORD;
10693 else if (TYPE_SIZE (type) == NULL_TREE)
10694 return 0;
10695 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
10696 return wi::to_offset (TYPE_SIZE (type));
10697 else
10698 return TYPE_ALIGN (type);
10701 /* Given a pointer to a tree node for a subrange type, return a pointer
10702 to a DIE that describes the given type. */
10704 static dw_die_ref
10705 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
10707 dw_die_ref subrange_die;
10708 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
10710 if (context_die == NULL)
10711 context_die = comp_unit_die ();
10713 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
10715 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
10717 /* The size of the subrange type and its base type do not match,
10718 so we need to generate a size attribute for the subrange type. */
10719 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
10722 if (low)
10723 add_bound_info (subrange_die, DW_AT_lower_bound, low, NULL);
10724 if (high)
10725 add_bound_info (subrange_die, DW_AT_upper_bound, high, NULL);
10727 return subrange_die;
10730 /* Returns the (const and/or volatile) cv_qualifiers associated with
10731 the decl node. This will normally be augmented with the
10732 cv_qualifiers of the underlying type in add_type_attribute. */
10734 static int
10735 decl_quals (const_tree decl)
10737 return ((TREE_READONLY (decl)
10738 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
10739 | (TREE_THIS_VOLATILE (decl)
10740 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
10743 /* Determine the TYPE whose qualifiers match the largest strict subset
10744 of the given TYPE_QUALS, and return its qualifiers. Ignore all
10745 qualifiers outside QUAL_MASK. */
10747 static int
10748 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
10750 tree t;
10751 int best_rank = 0, best_qual = 0, max_rank;
10753 type_quals &= qual_mask;
10754 max_rank = popcount_hwi (type_quals) - 1;
10756 for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
10757 t = TYPE_NEXT_VARIANT (t))
10759 int q = TYPE_QUALS (t) & qual_mask;
10761 if ((q & type_quals) == q && q != type_quals
10762 && check_base_type (t, type))
10764 int rank = popcount_hwi (q);
10766 if (rank > best_rank)
10768 best_rank = rank;
10769 best_qual = q;
10774 return best_qual;
10777 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
10778 entry that chains various modifiers in front of the given type. */
10780 static dw_die_ref
10781 modified_type_die (tree type, int cv_quals, dw_die_ref context_die)
10783 enum tree_code code = TREE_CODE (type);
10784 dw_die_ref mod_type_die;
10785 dw_die_ref sub_die = NULL;
10786 tree item_type = NULL;
10787 tree qualified_type;
10788 tree name, low, high;
10789 dw_die_ref mod_scope;
10790 /* Only these cv-qualifiers are currently handled. */
10791 const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
10792 | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC);
10794 if (code == ERROR_MARK)
10795 return NULL;
10797 cv_quals &= cv_qual_mask;
10799 /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
10800 tag modifier (and not an attribute) old consumers won't be able
10801 to handle it. */
10802 if (dwarf_version < 3)
10803 cv_quals &= ~TYPE_QUAL_RESTRICT;
10805 /* Likewise for DW_TAG_atomic_type for DWARFv5. */
10806 if (dwarf_version < 5)
10807 cv_quals &= ~TYPE_QUAL_ATOMIC;
10809 /* See if we already have the appropriately qualified variant of
10810 this type. */
10811 qualified_type = get_qualified_type (type, cv_quals);
10813 if (qualified_type == sizetype
10814 && TYPE_NAME (qualified_type)
10815 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
10817 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
10819 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
10820 && TYPE_PRECISION (t)
10821 == TYPE_PRECISION (qualified_type)
10822 && TYPE_UNSIGNED (t)
10823 == TYPE_UNSIGNED (qualified_type));
10824 qualified_type = t;
10827 /* If we do, then we can just use its DIE, if it exists. */
10828 if (qualified_type)
10830 mod_type_die = lookup_type_die (qualified_type);
10831 if (mod_type_die)
10832 return mod_type_die;
10835 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
10837 /* Handle C typedef types. */
10838 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
10839 && !DECL_ARTIFICIAL (name))
10841 tree dtype = TREE_TYPE (name);
10843 if (qualified_type == dtype)
10845 /* For a named type, use the typedef. */
10846 gen_type_die (qualified_type, context_die);
10847 return lookup_type_die (qualified_type);
10849 else
10851 int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
10852 dquals &= cv_qual_mask;
10853 if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
10854 || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
10855 /* cv-unqualified version of named type. Just use
10856 the unnamed type to which it refers. */
10857 return modified_type_die (DECL_ORIGINAL_TYPE (name),
10858 cv_quals, context_die);
10859 /* Else cv-qualified version of named type; fall through. */
10863 mod_scope = scope_die_for (type, context_die);
10865 if (cv_quals)
10867 struct qual_info { int q; enum dwarf_tag t; };
10868 static const struct qual_info qual_info[] =
10870 { TYPE_QUAL_ATOMIC, DW_TAG_atomic_type },
10871 { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
10872 { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
10873 { TYPE_QUAL_CONST, DW_TAG_const_type },
10875 int sub_quals;
10876 unsigned i;
10878 /* Determine a lesser qualified type that most closely matches
10879 this one. Then generate DW_TAG_* entries for the remaining
10880 qualifiers. */
10881 sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
10882 cv_qual_mask);
10883 mod_type_die = modified_type_die (type, sub_quals, context_die);
10885 for (i = 0; i < sizeof (qual_info) / sizeof (qual_info[0]); i++)
10886 if (qual_info[i].q & cv_quals & ~sub_quals)
10888 dw_die_ref d = new_die (qual_info[i].t, mod_scope, type);
10889 if (mod_type_die)
10890 add_AT_die_ref (d, DW_AT_type, mod_type_die);
10891 mod_type_die = d;
10894 else if (code == POINTER_TYPE)
10896 mod_type_die = new_die (DW_TAG_pointer_type, mod_scope, type);
10897 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10898 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10899 item_type = TREE_TYPE (type);
10900 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10901 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10902 TYPE_ADDR_SPACE (item_type));
10904 else if (code == REFERENCE_TYPE)
10906 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
10907 mod_type_die = new_die (DW_TAG_rvalue_reference_type, mod_scope,
10908 type);
10909 else
10910 mod_type_die = new_die (DW_TAG_reference_type, mod_scope, type);
10911 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10912 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10913 item_type = TREE_TYPE (type);
10914 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10915 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10916 TYPE_ADDR_SPACE (item_type));
10918 else if (code == INTEGER_TYPE
10919 && TREE_TYPE (type) != NULL_TREE
10920 && subrange_type_for_debug_p (type, &low, &high))
10922 mod_type_die = subrange_type_die (type, low, high, context_die);
10923 item_type = TREE_TYPE (type);
10925 else if (is_base_type (type))
10926 mod_type_die = base_type_die (type);
10927 else
10929 gen_type_die (type, context_die);
10931 /* We have to get the type_main_variant here (and pass that to the
10932 `lookup_type_die' routine) because the ..._TYPE node we have
10933 might simply be a *copy* of some original type node (where the
10934 copy was created to help us keep track of typedef names) and
10935 that copy might have a different TYPE_UID from the original
10936 ..._TYPE node. */
10937 if (TREE_CODE (type) != VECTOR_TYPE)
10938 return lookup_type_die (type_main_variant (type));
10939 else
10940 /* Vectors have the debugging information in the type,
10941 not the main variant. */
10942 return lookup_type_die (type);
10945 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
10946 don't output a DW_TAG_typedef, since there isn't one in the
10947 user's program; just attach a DW_AT_name to the type.
10948 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
10949 if the base type already has the same name. */
10950 if (name
10951 && ((TREE_CODE (name) != TYPE_DECL
10952 && (qualified_type == TYPE_MAIN_VARIANT (type)
10953 || (cv_quals == TYPE_UNQUALIFIED)))
10954 || (TREE_CODE (name) == TYPE_DECL
10955 && TREE_TYPE (name) == qualified_type
10956 && DECL_NAME (name))))
10958 if (TREE_CODE (name) == TYPE_DECL)
10959 /* Could just call add_name_and_src_coords_attributes here,
10960 but since this is a builtin type it doesn't have any
10961 useful source coordinates anyway. */
10962 name = DECL_NAME (name);
10963 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
10965 /* This probably indicates a bug. */
10966 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
10968 name = TYPE_IDENTIFIER (type);
10969 add_name_attribute (mod_type_die,
10970 name ? IDENTIFIER_POINTER (name) : "__unknown__");
10973 if (qualified_type)
10974 equate_type_number_to_die (qualified_type, mod_type_die);
10976 if (item_type)
10977 /* We must do this after the equate_type_number_to_die call, in case
10978 this is a recursive type. This ensures that the modified_type_die
10979 recursion will terminate even if the type is recursive. Recursive
10980 types are possible in Ada. */
10981 sub_die = modified_type_die (item_type,
10982 TYPE_QUALS_NO_ADDR_SPACE (item_type),
10983 context_die);
10985 if (sub_die != NULL)
10986 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
10988 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
10989 if (TYPE_ARTIFICIAL (type))
10990 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
10992 return mod_type_die;
10995 /* Generate DIEs for the generic parameters of T.
10996 T must be either a generic type or a generic function.
10997 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
10999 static void
11000 gen_generic_params_dies (tree t)
11002 tree parms, args;
11003 int parms_num, i;
11004 dw_die_ref die = NULL;
11005 int non_default;
11007 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
11008 return;
11010 if (TYPE_P (t))
11011 die = lookup_type_die (t);
11012 else if (DECL_P (t))
11013 die = lookup_decl_die (t);
11015 gcc_assert (die);
11017 parms = lang_hooks.get_innermost_generic_parms (t);
11018 if (!parms)
11019 /* T has no generic parameter. It means T is neither a generic type
11020 or function. End of story. */
11021 return;
11023 parms_num = TREE_VEC_LENGTH (parms);
11024 args = lang_hooks.get_innermost_generic_args (t);
11025 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
11026 non_default = int_cst_value (TREE_CHAIN (args));
11027 else
11028 non_default = TREE_VEC_LENGTH (args);
11029 for (i = 0; i < parms_num; i++)
11031 tree parm, arg, arg_pack_elems;
11032 dw_die_ref parm_die;
11034 parm = TREE_VEC_ELT (parms, i);
11035 arg = TREE_VEC_ELT (args, i);
11036 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
11037 gcc_assert (parm && TREE_VALUE (parm) && arg);
11039 if (parm && TREE_VALUE (parm) && arg)
11041 /* If PARM represents a template parameter pack,
11042 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
11043 by DW_TAG_template_*_parameter DIEs for the argument
11044 pack elements of ARG. Note that ARG would then be
11045 an argument pack. */
11046 if (arg_pack_elems)
11047 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
11048 arg_pack_elems,
11049 die);
11050 else
11051 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
11052 true /* emit name */, die);
11053 if (i >= non_default)
11054 add_AT_flag (parm_die, DW_AT_default_value, 1);
11059 /* Create and return a DIE for PARM which should be
11060 the representation of a generic type parameter.
11061 For instance, in the C++ front end, PARM would be a template parameter.
11062 ARG is the argument to PARM.
11063 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
11064 name of the PARM.
11065 PARENT_DIE is the parent DIE which the new created DIE should be added to,
11066 as a child node. */
11068 static dw_die_ref
11069 generic_parameter_die (tree parm, tree arg,
11070 bool emit_name_p,
11071 dw_die_ref parent_die)
11073 dw_die_ref tmpl_die = NULL;
11074 const char *name = NULL;
11076 if (!parm || !DECL_NAME (parm) || !arg)
11077 return NULL;
11079 /* We support non-type generic parameters and arguments,
11080 type generic parameters and arguments, as well as
11081 generic generic parameters (a.k.a. template template parameters in C++)
11082 and arguments. */
11083 if (TREE_CODE (parm) == PARM_DECL)
11084 /* PARM is a nontype generic parameter */
11085 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
11086 else if (TREE_CODE (parm) == TYPE_DECL)
11087 /* PARM is a type generic parameter. */
11088 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
11089 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
11090 /* PARM is a generic generic parameter.
11091 Its DIE is a GNU extension. It shall have a
11092 DW_AT_name attribute to represent the name of the template template
11093 parameter, and a DW_AT_GNU_template_name attribute to represent the
11094 name of the template template argument. */
11095 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
11096 parent_die, parm);
11097 else
11098 gcc_unreachable ();
11100 if (tmpl_die)
11102 tree tmpl_type;
11104 /* If PARM is a generic parameter pack, it means we are
11105 emitting debug info for a template argument pack element.
11106 In other terms, ARG is a template argument pack element.
11107 In that case, we don't emit any DW_AT_name attribute for
11108 the die. */
11109 if (emit_name_p)
11111 name = IDENTIFIER_POINTER (DECL_NAME (parm));
11112 gcc_assert (name);
11113 add_AT_string (tmpl_die, DW_AT_name, name);
11116 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
11118 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
11119 TMPL_DIE should have a child DW_AT_type attribute that is set
11120 to the type of the argument to PARM, which is ARG.
11121 If PARM is a type generic parameter, TMPL_DIE should have a
11122 child DW_AT_type that is set to ARG. */
11123 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
11124 add_type_attribute (tmpl_die, tmpl_type,
11125 (TREE_THIS_VOLATILE (tmpl_type)
11126 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
11127 parent_die);
11129 else
11131 /* So TMPL_DIE is a DIE representing a
11132 a generic generic template parameter, a.k.a template template
11133 parameter in C++ and arg is a template. */
11135 /* The DW_AT_GNU_template_name attribute of the DIE must be set
11136 to the name of the argument. */
11137 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
11138 if (name)
11139 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
11142 if (TREE_CODE (parm) == PARM_DECL)
11143 /* So PARM is a non-type generic parameter.
11144 DWARF3 5.6.8 says we must set a DW_AT_const_value child
11145 attribute of TMPL_DIE which value represents the value
11146 of ARG.
11147 We must be careful here:
11148 The value of ARG might reference some function decls.
11149 We might currently be emitting debug info for a generic
11150 type and types are emitted before function decls, we don't
11151 know if the function decls referenced by ARG will actually be
11152 emitted after cgraph computations.
11153 So must defer the generation of the DW_AT_const_value to
11154 after cgraph is ready. */
11155 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
11158 return tmpl_die;
11161 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
11162 PARM_PACK must be a template parameter pack. The returned DIE
11163 will be child DIE of PARENT_DIE. */
11165 static dw_die_ref
11166 template_parameter_pack_die (tree parm_pack,
11167 tree parm_pack_args,
11168 dw_die_ref parent_die)
11170 dw_die_ref die;
11171 int j;
11173 gcc_assert (parent_die && parm_pack);
11175 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
11176 add_name_and_src_coords_attributes (die, parm_pack);
11177 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
11178 generic_parameter_die (parm_pack,
11179 TREE_VEC_ELT (parm_pack_args, j),
11180 false /* Don't emit DW_AT_name */,
11181 die);
11182 return die;
11185 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
11186 an enumerated type. */
11188 static inline int
11189 type_is_enum (const_tree type)
11191 return TREE_CODE (type) == ENUMERAL_TYPE;
11194 /* Return the DBX register number described by a given RTL node. */
11196 static unsigned int
11197 dbx_reg_number (const_rtx rtl)
11199 unsigned regno = REGNO (rtl);
11201 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
11203 #ifdef LEAF_REG_REMAP
11204 if (crtl->uses_only_leaf_regs)
11206 int leaf_reg = LEAF_REG_REMAP (regno);
11207 if (leaf_reg != -1)
11208 regno = (unsigned) leaf_reg;
11210 #endif
11212 regno = DBX_REGISTER_NUMBER (regno);
11213 gcc_assert (regno != INVALID_REGNUM);
11214 return regno;
11217 /* Optionally add a DW_OP_piece term to a location description expression.
11218 DW_OP_piece is only added if the location description expression already
11219 doesn't end with DW_OP_piece. */
11221 static void
11222 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
11224 dw_loc_descr_ref loc;
11226 if (*list_head != NULL)
11228 /* Find the end of the chain. */
11229 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
11232 if (loc->dw_loc_opc != DW_OP_piece)
11233 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
11237 /* Return a location descriptor that designates a machine register or
11238 zero if there is none. */
11240 static dw_loc_descr_ref
11241 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
11243 rtx regs;
11245 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
11246 return 0;
11248 /* We only use "frame base" when we're sure we're talking about the
11249 post-prologue local stack frame. We do this by *not* running
11250 register elimination until this point, and recognizing the special
11251 argument pointer and soft frame pointer rtx's.
11252 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
11253 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
11254 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
11256 dw_loc_descr_ref result = NULL;
11258 if (dwarf_version >= 4 || !dwarf_strict)
11260 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
11261 initialized);
11262 if (result)
11263 add_loc_descr (&result,
11264 new_loc_descr (DW_OP_stack_value, 0, 0));
11266 return result;
11269 regs = targetm.dwarf_register_span (rtl);
11271 if (REG_NREGS (rtl) > 1 || regs)
11272 return multiple_reg_loc_descriptor (rtl, regs, initialized);
11273 else
11275 unsigned int dbx_regnum = dbx_reg_number (rtl);
11276 if (dbx_regnum == IGNORED_DWARF_REGNUM)
11277 return 0;
11278 return one_reg_loc_descriptor (dbx_regnum, initialized);
11282 /* Return a location descriptor that designates a machine register for
11283 a given hard register number. */
11285 static dw_loc_descr_ref
11286 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
11288 dw_loc_descr_ref reg_loc_descr;
11290 if (regno <= 31)
11291 reg_loc_descr
11292 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
11293 else
11294 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
11296 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11297 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11299 return reg_loc_descr;
11302 /* Given an RTL of a register, return a location descriptor that
11303 designates a value that spans more than one register. */
11305 static dw_loc_descr_ref
11306 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
11307 enum var_init_status initialized)
11309 int size, i;
11310 dw_loc_descr_ref loc_result = NULL;
11312 /* Simple, contiguous registers. */
11313 if (regs == NULL_RTX)
11315 unsigned reg = REGNO (rtl);
11316 int nregs;
11318 #ifdef LEAF_REG_REMAP
11319 if (crtl->uses_only_leaf_regs)
11321 int leaf_reg = LEAF_REG_REMAP (reg);
11322 if (leaf_reg != -1)
11323 reg = (unsigned) leaf_reg;
11325 #endif
11327 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
11328 nregs = REG_NREGS (rtl);
11330 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
11332 loc_result = NULL;
11333 while (nregs--)
11335 dw_loc_descr_ref t;
11337 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
11338 VAR_INIT_STATUS_INITIALIZED);
11339 add_loc_descr (&loc_result, t);
11340 add_loc_descr_op_piece (&loc_result, size);
11341 ++reg;
11343 return loc_result;
11346 /* Now onto stupid register sets in non contiguous locations. */
11348 gcc_assert (GET_CODE (regs) == PARALLEL);
11350 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
11351 loc_result = NULL;
11353 for (i = 0; i < XVECLEN (regs, 0); ++i)
11355 dw_loc_descr_ref t;
11357 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
11358 VAR_INIT_STATUS_INITIALIZED);
11359 add_loc_descr (&loc_result, t);
11360 add_loc_descr_op_piece (&loc_result, size);
11363 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
11364 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11365 return loc_result;
11368 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
11370 /* Return a location descriptor that designates a constant i,
11371 as a compound operation from constant (i >> shift), constant shift
11372 and DW_OP_shl. */
11374 static dw_loc_descr_ref
11375 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11377 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
11378 add_loc_descr (&ret, int_loc_descriptor (shift));
11379 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11380 return ret;
11383 /* Return a location descriptor that designates a constant. */
11385 static dw_loc_descr_ref
11386 int_loc_descriptor (HOST_WIDE_INT i)
11388 enum dwarf_location_atom op;
11390 /* Pick the smallest representation of a constant, rather than just
11391 defaulting to the LEB encoding. */
11392 if (i >= 0)
11394 int clz = clz_hwi (i);
11395 int ctz = ctz_hwi (i);
11396 if (i <= 31)
11397 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
11398 else if (i <= 0xff)
11399 op = DW_OP_const1u;
11400 else if (i <= 0xffff)
11401 op = DW_OP_const2u;
11402 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11403 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11404 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
11405 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
11406 while DW_OP_const4u is 5 bytes. */
11407 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
11408 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11409 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11410 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
11411 while DW_OP_const4u is 5 bytes. */
11412 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11413 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11414 op = DW_OP_const4u;
11415 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11416 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11417 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
11418 while DW_OP_constu of constant >= 0x100000000 takes at least
11419 6 bytes. */
11420 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11421 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11422 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
11423 >= HOST_BITS_PER_WIDE_INT)
11424 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
11425 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
11426 while DW_OP_constu takes in this case at least 6 bytes. */
11427 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
11428 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11429 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11430 && size_of_uleb128 (i) > 6)
11431 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
11432 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
11433 else
11434 op = DW_OP_constu;
11436 else
11438 if (i >= -0x80)
11439 op = DW_OP_const1s;
11440 else if (i >= -0x8000)
11441 op = DW_OP_const2s;
11442 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11444 if (size_of_int_loc_descriptor (i) < 5)
11446 dw_loc_descr_ref ret = int_loc_descriptor (-i);
11447 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11448 return ret;
11450 op = DW_OP_const4s;
11452 else
11454 if (size_of_int_loc_descriptor (i)
11455 < (unsigned long) 1 + size_of_sleb128 (i))
11457 dw_loc_descr_ref ret = int_loc_descriptor (-i);
11458 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11459 return ret;
11461 op = DW_OP_consts;
11465 return new_loc_descr (op, i, 0);
11468 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
11469 without actually allocating it. */
11471 static unsigned long
11472 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11474 return size_of_int_loc_descriptor (i >> shift)
11475 + size_of_int_loc_descriptor (shift)
11476 + 1;
11479 /* Return size_of_locs (int_loc_descriptor (i)) without
11480 actually allocating it. */
11482 static unsigned long
11483 size_of_int_loc_descriptor (HOST_WIDE_INT i)
11485 unsigned long s;
11487 if (i >= 0)
11489 int clz, ctz;
11490 if (i <= 31)
11491 return 1;
11492 else if (i <= 0xff)
11493 return 2;
11494 else if (i <= 0xffff)
11495 return 3;
11496 clz = clz_hwi (i);
11497 ctz = ctz_hwi (i);
11498 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11499 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11500 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11501 - clz - 5);
11502 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11503 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11504 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11505 - clz - 8);
11506 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11507 return 5;
11508 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
11509 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11510 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11511 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11512 - clz - 8);
11513 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11514 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
11515 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11516 - clz - 16);
11517 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11518 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11519 && s > 6)
11520 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11521 - clz - 32);
11522 else
11523 return 1 + s;
11525 else
11527 if (i >= -0x80)
11528 return 2;
11529 else if (i >= -0x8000)
11530 return 3;
11531 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11533 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11535 s = size_of_int_loc_descriptor (-i) + 1;
11536 if (s < 5)
11537 return s;
11539 return 5;
11541 else
11543 unsigned long r = 1 + size_of_sleb128 (i);
11544 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11546 s = size_of_int_loc_descriptor (-i) + 1;
11547 if (s < r)
11548 return s;
11550 return r;
11555 /* Return loc description representing "address" of integer value.
11556 This can appear only as toplevel expression. */
11558 static dw_loc_descr_ref
11559 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
11561 int litsize;
11562 dw_loc_descr_ref loc_result = NULL;
11564 if (!(dwarf_version >= 4 || !dwarf_strict))
11565 return NULL;
11567 litsize = size_of_int_loc_descriptor (i);
11568 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
11569 is more compact. For DW_OP_stack_value we need:
11570 litsize + 1 (DW_OP_stack_value)
11571 and for DW_OP_implicit_value:
11572 1 (DW_OP_implicit_value) + 1 (length) + size. */
11573 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
11575 loc_result = int_loc_descriptor (i);
11576 add_loc_descr (&loc_result,
11577 new_loc_descr (DW_OP_stack_value, 0, 0));
11578 return loc_result;
11581 loc_result = new_loc_descr (DW_OP_implicit_value,
11582 size, 0);
11583 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
11584 loc_result->dw_loc_oprnd2.v.val_int = i;
11585 return loc_result;
11588 /* Return a location descriptor that designates a base+offset location. */
11590 static dw_loc_descr_ref
11591 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
11592 enum var_init_status initialized)
11594 unsigned int regno;
11595 dw_loc_descr_ref result;
11596 dw_fde_ref fde = cfun->fde;
11598 /* We only use "frame base" when we're sure we're talking about the
11599 post-prologue local stack frame. We do this by *not* running
11600 register elimination until this point, and recognizing the special
11601 argument pointer and soft frame pointer rtx's. */
11602 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
11604 rtx elim = (ira_use_lra_p
11605 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
11606 : eliminate_regs (reg, VOIDmode, NULL_RTX));
11608 if (elim != reg)
11610 if (GET_CODE (elim) == PLUS)
11612 offset += INTVAL (XEXP (elim, 1));
11613 elim = XEXP (elim, 0);
11615 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
11616 && (elim == hard_frame_pointer_rtx
11617 || elim == stack_pointer_rtx))
11618 || elim == (frame_pointer_needed
11619 ? hard_frame_pointer_rtx
11620 : stack_pointer_rtx));
11622 /* If drap register is used to align stack, use frame
11623 pointer + offset to access stack variables. If stack
11624 is aligned without drap, use stack pointer + offset to
11625 access stack variables. */
11626 if (crtl->stack_realign_tried
11627 && reg == frame_pointer_rtx)
11629 int base_reg
11630 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
11631 ? HARD_FRAME_POINTER_REGNUM
11632 : REGNO (elim));
11633 return new_reg_loc_descr (base_reg, offset);
11636 gcc_assert (frame_pointer_fb_offset_valid);
11637 offset += frame_pointer_fb_offset;
11638 return new_loc_descr (DW_OP_fbreg, offset, 0);
11642 regno = REGNO (reg);
11643 #ifdef LEAF_REG_REMAP
11644 if (crtl->uses_only_leaf_regs)
11646 int leaf_reg = LEAF_REG_REMAP (regno);
11647 if (leaf_reg != -1)
11648 regno = (unsigned) leaf_reg;
11650 #endif
11651 regno = DWARF_FRAME_REGNUM (regno);
11653 if (!optimize && fde
11654 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
11656 /* Use cfa+offset to represent the location of arguments passed
11657 on the stack when drap is used to align stack.
11658 Only do this when not optimizing, for optimized code var-tracking
11659 is supposed to track where the arguments live and the register
11660 used as vdrap or drap in some spot might be used for something
11661 else in other part of the routine. */
11662 return new_loc_descr (DW_OP_fbreg, offset, 0);
11665 if (regno <= 31)
11666 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
11667 offset, 0);
11668 else
11669 result = new_loc_descr (DW_OP_bregx, regno, offset);
11671 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11672 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11674 return result;
11677 /* Return true if this RTL expression describes a base+offset calculation. */
11679 static inline int
11680 is_based_loc (const_rtx rtl)
11682 return (GET_CODE (rtl) == PLUS
11683 && ((REG_P (XEXP (rtl, 0))
11684 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
11685 && CONST_INT_P (XEXP (rtl, 1)))));
11688 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
11689 failed. */
11691 static dw_loc_descr_ref
11692 tls_mem_loc_descriptor (rtx mem)
11694 tree base;
11695 dw_loc_descr_ref loc_result;
11697 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
11698 return NULL;
11700 base = get_base_address (MEM_EXPR (mem));
11701 if (base == NULL
11702 || TREE_CODE (base) != VAR_DECL
11703 || !DECL_THREAD_LOCAL_P (base))
11704 return NULL;
11706 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
11707 if (loc_result == NULL)
11708 return NULL;
11710 if (MEM_OFFSET (mem))
11711 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
11713 return loc_result;
11716 /* Output debug info about reason why we failed to expand expression as dwarf
11717 expression. */
11719 static void
11720 expansion_failed (tree expr, rtx rtl, char const *reason)
11722 if (dump_file && (dump_flags & TDF_DETAILS))
11724 fprintf (dump_file, "Failed to expand as dwarf: ");
11725 if (expr)
11726 print_generic_expr (dump_file, expr, dump_flags);
11727 if (rtl)
11729 fprintf (dump_file, "\n");
11730 print_rtl (dump_file, rtl);
11732 fprintf (dump_file, "\nReason: %s\n", reason);
11736 /* Helper function for const_ok_for_output. */
11738 static bool
11739 const_ok_for_output_1 (rtx rtl)
11741 if (GET_CODE (rtl) == UNSPEC)
11743 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
11744 we can't express it in the debug info. */
11745 #ifdef ENABLE_CHECKING
11746 /* Don't complain about TLS UNSPECs, those are just too hard to
11747 delegitimize. Note this could be a non-decl SYMBOL_REF such as
11748 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
11749 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
11750 if (XVECLEN (rtl, 0) == 0
11751 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
11752 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE)
11753 inform (current_function_decl
11754 ? DECL_SOURCE_LOCATION (current_function_decl)
11755 : UNKNOWN_LOCATION,
11756 #if NUM_UNSPEC_VALUES > 0
11757 "non-delegitimized UNSPEC %s (%d) found in variable location",
11758 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
11759 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
11760 XINT (rtl, 1));
11761 #else
11762 "non-delegitimized UNSPEC %d found in variable location",
11763 XINT (rtl, 1));
11764 #endif
11765 #endif
11766 expansion_failed (NULL_TREE, rtl,
11767 "UNSPEC hasn't been delegitimized.\n");
11768 return false;
11771 if (targetm.const_not_ok_for_debug_p (rtl))
11773 expansion_failed (NULL_TREE, rtl,
11774 "Expression rejected for debug by the backend.\n");
11775 return false;
11778 /* FIXME: Refer to PR60655. It is possible for simplification
11779 of rtl expressions in var tracking to produce such expressions.
11780 We should really identify / validate expressions
11781 enclosed in CONST that can be handled by assemblers on various
11782 targets and only handle legitimate cases here. */
11783 if (GET_CODE (rtl) != SYMBOL_REF)
11785 if (GET_CODE (rtl) == NOT)
11786 return false;
11787 return true;
11790 if (CONSTANT_POOL_ADDRESS_P (rtl))
11792 bool marked;
11793 get_pool_constant_mark (rtl, &marked);
11794 /* If all references to this pool constant were optimized away,
11795 it was not output and thus we can't represent it. */
11796 if (!marked)
11798 expansion_failed (NULL_TREE, rtl,
11799 "Constant was removed from constant pool.\n");
11800 return false;
11804 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11805 return false;
11807 /* Avoid references to external symbols in debug info, on several targets
11808 the linker might even refuse to link when linking a shared library,
11809 and in many other cases the relocations for .debug_info/.debug_loc are
11810 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
11811 to be defined within the same shared library or executable are fine. */
11812 if (SYMBOL_REF_EXTERNAL_P (rtl))
11814 tree decl = SYMBOL_REF_DECL (rtl);
11816 if (decl == NULL || !targetm.binds_local_p (decl))
11818 expansion_failed (NULL_TREE, rtl,
11819 "Symbol not defined in current TU.\n");
11820 return false;
11824 return true;
11827 /* Return true if constant RTL can be emitted in DW_OP_addr or
11828 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
11829 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
11831 static bool
11832 const_ok_for_output (rtx rtl)
11834 if (GET_CODE (rtl) == SYMBOL_REF)
11835 return const_ok_for_output_1 (rtl);
11837 if (GET_CODE (rtl) == CONST)
11839 subrtx_var_iterator::array_type array;
11840 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
11841 if (!const_ok_for_output_1 (*iter))
11842 return false;
11843 return true;
11846 return true;
11849 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
11850 if possible, NULL otherwise. */
11852 static dw_die_ref
11853 base_type_for_mode (machine_mode mode, bool unsignedp)
11855 dw_die_ref type_die;
11856 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11858 if (type == NULL)
11859 return NULL;
11860 switch (TREE_CODE (type))
11862 case INTEGER_TYPE:
11863 case REAL_TYPE:
11864 break;
11865 default:
11866 return NULL;
11868 type_die = lookup_type_die (type);
11869 if (!type_die)
11870 type_die = modified_type_die (type, TYPE_UNQUALIFIED, comp_unit_die ());
11871 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
11872 return NULL;
11873 return type_die;
11876 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
11877 type matching MODE, or, if MODE is narrower than or as wide as
11878 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
11879 possible. */
11881 static dw_loc_descr_ref
11882 convert_descriptor_to_mode (machine_mode mode, dw_loc_descr_ref op)
11884 machine_mode outer_mode = mode;
11885 dw_die_ref type_die;
11886 dw_loc_descr_ref cvt;
11888 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11890 add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
11891 return op;
11893 type_die = base_type_for_mode (outer_mode, 1);
11894 if (type_die == NULL)
11895 return NULL;
11896 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11897 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11898 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11899 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11900 add_loc_descr (&op, cvt);
11901 return op;
11904 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
11906 static dw_loc_descr_ref
11907 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
11908 dw_loc_descr_ref op1)
11910 dw_loc_descr_ref ret = op0;
11911 add_loc_descr (&ret, op1);
11912 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11913 if (STORE_FLAG_VALUE != 1)
11915 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
11916 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
11918 return ret;
11921 /* Return location descriptor for signed comparison OP RTL. */
11923 static dw_loc_descr_ref
11924 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11925 machine_mode mem_mode)
11927 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11928 dw_loc_descr_ref op0, op1;
11929 int shift;
11931 if (op_mode == VOIDmode)
11932 op_mode = GET_MODE (XEXP (rtl, 1));
11933 if (op_mode == VOIDmode)
11934 return NULL;
11936 if (dwarf_strict
11937 && (GET_MODE_CLASS (op_mode) != MODE_INT
11938 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
11939 return NULL;
11941 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11942 VAR_INIT_STATUS_INITIALIZED);
11943 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11944 VAR_INIT_STATUS_INITIALIZED);
11946 if (op0 == NULL || op1 == NULL)
11947 return NULL;
11949 if (GET_MODE_CLASS (op_mode) != MODE_INT
11950 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11951 return compare_loc_descriptor (op, op0, op1);
11953 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11955 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
11956 dw_loc_descr_ref cvt;
11958 if (type_die == NULL)
11959 return NULL;
11960 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11961 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11962 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11963 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11964 add_loc_descr (&op0, cvt);
11965 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11966 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11967 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11968 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11969 add_loc_descr (&op1, cvt);
11970 return compare_loc_descriptor (op, op0, op1);
11973 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
11974 /* For eq/ne, if the operands are known to be zero-extended,
11975 there is no need to do the fancy shifting up. */
11976 if (op == DW_OP_eq || op == DW_OP_ne)
11978 dw_loc_descr_ref last0, last1;
11979 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11981 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11983 /* deref_size zero extends, and for constants we can check
11984 whether they are zero extended or not. */
11985 if (((last0->dw_loc_opc == DW_OP_deref_size
11986 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11987 || (CONST_INT_P (XEXP (rtl, 0))
11988 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
11989 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
11990 && ((last1->dw_loc_opc == DW_OP_deref_size
11991 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11992 || (CONST_INT_P (XEXP (rtl, 1))
11993 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
11994 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
11995 return compare_loc_descriptor (op, op0, op1);
11997 /* EQ/NE comparison against constant in narrower type than
11998 DWARF2_ADDR_SIZE can be performed either as
11999 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
12000 DW_OP_{eq,ne}
12002 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
12003 DW_OP_{eq,ne}. Pick whatever is shorter. */
12004 if (CONST_INT_P (XEXP (rtl, 1))
12005 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
12006 && (size_of_int_loc_descriptor (shift) + 1
12007 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
12008 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
12009 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
12010 & GET_MODE_MASK (op_mode))))
12012 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
12013 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
12014 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
12015 & GET_MODE_MASK (op_mode));
12016 return compare_loc_descriptor (op, op0, op1);
12019 add_loc_descr (&op0, int_loc_descriptor (shift));
12020 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
12021 if (CONST_INT_P (XEXP (rtl, 1)))
12022 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
12023 else
12025 add_loc_descr (&op1, int_loc_descriptor (shift));
12026 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
12028 return compare_loc_descriptor (op, op0, op1);
12031 /* Return location descriptor for unsigned comparison OP RTL. */
12033 static dw_loc_descr_ref
12034 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
12035 machine_mode mem_mode)
12037 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
12038 dw_loc_descr_ref op0, op1;
12040 if (op_mode == VOIDmode)
12041 op_mode = GET_MODE (XEXP (rtl, 1));
12042 if (op_mode == VOIDmode)
12043 return NULL;
12044 if (GET_MODE_CLASS (op_mode) != MODE_INT)
12045 return NULL;
12047 if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
12048 return NULL;
12050 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
12051 VAR_INIT_STATUS_INITIALIZED);
12052 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
12053 VAR_INIT_STATUS_INITIALIZED);
12055 if (op0 == NULL || op1 == NULL)
12056 return NULL;
12058 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
12060 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
12061 dw_loc_descr_ref last0, last1;
12062 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
12064 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
12066 if (CONST_INT_P (XEXP (rtl, 0)))
12067 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
12068 /* deref_size zero extends, so no need to mask it again. */
12069 else if (last0->dw_loc_opc != DW_OP_deref_size
12070 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
12072 add_loc_descr (&op0, int_loc_descriptor (mask));
12073 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
12075 if (CONST_INT_P (XEXP (rtl, 1)))
12076 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
12077 /* deref_size zero extends, so no need to mask it again. */
12078 else if (last1->dw_loc_opc != DW_OP_deref_size
12079 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
12081 add_loc_descr (&op1, int_loc_descriptor (mask));
12082 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
12085 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
12087 HOST_WIDE_INT bias = 1;
12088 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
12089 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12090 if (CONST_INT_P (XEXP (rtl, 1)))
12091 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
12092 + INTVAL (XEXP (rtl, 1)));
12093 else
12094 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
12095 bias, 0));
12097 return compare_loc_descriptor (op, op0, op1);
12100 /* Return location descriptor for {U,S}{MIN,MAX}. */
12102 static dw_loc_descr_ref
12103 minmax_loc_descriptor (rtx rtl, machine_mode mode,
12104 machine_mode mem_mode)
12106 enum dwarf_location_atom op;
12107 dw_loc_descr_ref op0, op1, ret;
12108 dw_loc_descr_ref bra_node, drop_node;
12110 if (dwarf_strict
12111 && (GET_MODE_CLASS (mode) != MODE_INT
12112 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
12113 return NULL;
12115 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12116 VAR_INIT_STATUS_INITIALIZED);
12117 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12118 VAR_INIT_STATUS_INITIALIZED);
12120 if (op0 == NULL || op1 == NULL)
12121 return NULL;
12123 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
12124 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
12125 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
12126 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
12128 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12130 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
12131 add_loc_descr (&op0, int_loc_descriptor (mask));
12132 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
12133 add_loc_descr (&op1, int_loc_descriptor (mask));
12134 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
12136 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12138 HOST_WIDE_INT bias = 1;
12139 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
12140 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12141 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12144 else if (GET_MODE_CLASS (mode) == MODE_INT
12145 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12147 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
12148 add_loc_descr (&op0, int_loc_descriptor (shift));
12149 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
12150 add_loc_descr (&op1, int_loc_descriptor (shift));
12151 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
12153 else if (GET_MODE_CLASS (mode) == MODE_INT
12154 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12156 dw_die_ref type_die = base_type_for_mode (mode, 0);
12157 dw_loc_descr_ref cvt;
12158 if (type_die == NULL)
12159 return NULL;
12160 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12161 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12162 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12163 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12164 add_loc_descr (&op0, cvt);
12165 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12166 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12167 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12168 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12169 add_loc_descr (&op1, cvt);
12172 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
12173 op = DW_OP_lt;
12174 else
12175 op = DW_OP_gt;
12176 ret = op0;
12177 add_loc_descr (&ret, op1);
12178 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
12179 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12180 add_loc_descr (&ret, bra_node);
12181 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12182 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
12183 add_loc_descr (&ret, drop_node);
12184 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12185 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
12186 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
12187 && GET_MODE_CLASS (mode) == MODE_INT
12188 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12189 ret = convert_descriptor_to_mode (mode, ret);
12190 return ret;
12193 /* Helper function for mem_loc_descriptor. Perform OP binary op,
12194 but after converting arguments to type_die, afterwards
12195 convert back to unsigned. */
12197 static dw_loc_descr_ref
12198 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
12199 machine_mode mode, machine_mode mem_mode)
12201 dw_loc_descr_ref cvt, op0, op1;
12203 if (type_die == NULL)
12204 return NULL;
12205 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12206 VAR_INIT_STATUS_INITIALIZED);
12207 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12208 VAR_INIT_STATUS_INITIALIZED);
12209 if (op0 == NULL || op1 == NULL)
12210 return NULL;
12211 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12212 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12213 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12214 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12215 add_loc_descr (&op0, cvt);
12216 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12217 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12218 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12219 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12220 add_loc_descr (&op1, cvt);
12221 add_loc_descr (&op0, op1);
12222 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
12223 return convert_descriptor_to_mode (mode, op0);
12226 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
12227 const0 is DW_OP_lit0 or corresponding typed constant,
12228 const1 is DW_OP_lit1 or corresponding typed constant
12229 and constMSB is constant with just the MSB bit set
12230 for the mode):
12231 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
12232 L1: const0 DW_OP_swap
12233 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
12234 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12235 L3: DW_OP_drop
12236 L4: DW_OP_nop
12238 CTZ is similar:
12239 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
12240 L1: const0 DW_OP_swap
12241 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
12242 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12243 L3: DW_OP_drop
12244 L4: DW_OP_nop
12246 FFS is similar:
12247 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
12248 L1: const1 DW_OP_swap
12249 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
12250 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12251 L3: DW_OP_drop
12252 L4: DW_OP_nop */
12254 static dw_loc_descr_ref
12255 clz_loc_descriptor (rtx rtl, machine_mode mode,
12256 machine_mode mem_mode)
12258 dw_loc_descr_ref op0, ret, tmp;
12259 HOST_WIDE_INT valv;
12260 dw_loc_descr_ref l1jump, l1label;
12261 dw_loc_descr_ref l2jump, l2label;
12262 dw_loc_descr_ref l3jump, l3label;
12263 dw_loc_descr_ref l4jump, l4label;
12264 rtx msb;
12266 if (GET_MODE_CLASS (mode) != MODE_INT
12267 || GET_MODE (XEXP (rtl, 0)) != mode)
12268 return NULL;
12270 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12271 VAR_INIT_STATUS_INITIALIZED);
12272 if (op0 == NULL)
12273 return NULL;
12274 ret = op0;
12275 if (GET_CODE (rtl) == CLZ)
12277 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12278 valv = GET_MODE_BITSIZE (mode);
12280 else if (GET_CODE (rtl) == FFS)
12281 valv = 0;
12282 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12283 valv = GET_MODE_BITSIZE (mode);
12284 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12285 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
12286 add_loc_descr (&ret, l1jump);
12287 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12288 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
12289 VAR_INIT_STATUS_INITIALIZED);
12290 if (tmp == NULL)
12291 return NULL;
12292 add_loc_descr (&ret, tmp);
12293 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
12294 add_loc_descr (&ret, l4jump);
12295 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
12296 ? const1_rtx : const0_rtx,
12297 mode, mem_mode,
12298 VAR_INIT_STATUS_INITIALIZED);
12299 if (l1label == NULL)
12300 return NULL;
12301 add_loc_descr (&ret, l1label);
12302 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12303 l2label = new_loc_descr (DW_OP_dup, 0, 0);
12304 add_loc_descr (&ret, l2label);
12305 if (GET_CODE (rtl) != CLZ)
12306 msb = const1_rtx;
12307 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
12308 msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
12309 << (GET_MODE_BITSIZE (mode) - 1));
12310 else
12311 msb = immed_wide_int_const
12312 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
12313 GET_MODE_PRECISION (mode)), mode);
12314 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
12315 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12316 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
12317 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
12318 else
12319 tmp = mem_loc_descriptor (msb, mode, mem_mode,
12320 VAR_INIT_STATUS_INITIALIZED);
12321 if (tmp == NULL)
12322 return NULL;
12323 add_loc_descr (&ret, tmp);
12324 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12325 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
12326 add_loc_descr (&ret, l3jump);
12327 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12328 VAR_INIT_STATUS_INITIALIZED);
12329 if (tmp == NULL)
12330 return NULL;
12331 add_loc_descr (&ret, tmp);
12332 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
12333 ? DW_OP_shl : DW_OP_shr, 0, 0));
12334 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12335 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
12336 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12337 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
12338 add_loc_descr (&ret, l2jump);
12339 l3label = new_loc_descr (DW_OP_drop, 0, 0);
12340 add_loc_descr (&ret, l3label);
12341 l4label = new_loc_descr (DW_OP_nop, 0, 0);
12342 add_loc_descr (&ret, l4label);
12343 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12344 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12345 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12346 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12347 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12348 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
12349 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12350 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
12351 return ret;
12354 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
12355 const1 is DW_OP_lit1 or corresponding typed constant):
12356 const0 DW_OP_swap
12357 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
12358 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
12359 L2: DW_OP_drop
12361 PARITY is similar:
12362 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
12363 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
12364 L2: DW_OP_drop */
12366 static dw_loc_descr_ref
12367 popcount_loc_descriptor (rtx rtl, machine_mode mode,
12368 machine_mode mem_mode)
12370 dw_loc_descr_ref op0, ret, tmp;
12371 dw_loc_descr_ref l1jump, l1label;
12372 dw_loc_descr_ref l2jump, l2label;
12374 if (GET_MODE_CLASS (mode) != MODE_INT
12375 || GET_MODE (XEXP (rtl, 0)) != mode)
12376 return NULL;
12378 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12379 VAR_INIT_STATUS_INITIALIZED);
12380 if (op0 == NULL)
12381 return NULL;
12382 ret = op0;
12383 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12384 VAR_INIT_STATUS_INITIALIZED);
12385 if (tmp == NULL)
12386 return NULL;
12387 add_loc_descr (&ret, tmp);
12388 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12389 l1label = new_loc_descr (DW_OP_dup, 0, 0);
12390 add_loc_descr (&ret, l1label);
12391 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12392 add_loc_descr (&ret, l2jump);
12393 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12394 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12395 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12396 VAR_INIT_STATUS_INITIALIZED);
12397 if (tmp == NULL)
12398 return NULL;
12399 add_loc_descr (&ret, tmp);
12400 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12401 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
12402 ? DW_OP_plus : DW_OP_xor, 0, 0));
12403 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12404 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12405 VAR_INIT_STATUS_INITIALIZED);
12406 add_loc_descr (&ret, tmp);
12407 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12408 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12409 add_loc_descr (&ret, l1jump);
12410 l2label = new_loc_descr (DW_OP_drop, 0, 0);
12411 add_loc_descr (&ret, l2label);
12412 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12413 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12414 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12415 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12416 return ret;
12419 /* BSWAP (constS is initial shift count, either 56 or 24):
12420 constS const0
12421 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
12422 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
12423 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
12424 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
12425 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
12427 static dw_loc_descr_ref
12428 bswap_loc_descriptor (rtx rtl, machine_mode mode,
12429 machine_mode mem_mode)
12431 dw_loc_descr_ref op0, ret, tmp;
12432 dw_loc_descr_ref l1jump, l1label;
12433 dw_loc_descr_ref l2jump, l2label;
12435 if (GET_MODE_CLASS (mode) != MODE_INT
12436 || BITS_PER_UNIT != 8
12437 || (GET_MODE_BITSIZE (mode) != 32
12438 && GET_MODE_BITSIZE (mode) != 64))
12439 return NULL;
12441 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12442 VAR_INIT_STATUS_INITIALIZED);
12443 if (op0 == NULL)
12444 return NULL;
12446 ret = op0;
12447 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12448 mode, mem_mode,
12449 VAR_INIT_STATUS_INITIALIZED);
12450 if (tmp == NULL)
12451 return NULL;
12452 add_loc_descr (&ret, tmp);
12453 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12454 VAR_INIT_STATUS_INITIALIZED);
12455 if (tmp == NULL)
12456 return NULL;
12457 add_loc_descr (&ret, tmp);
12458 l1label = new_loc_descr (DW_OP_pick, 2, 0);
12459 add_loc_descr (&ret, l1label);
12460 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12461 mode, mem_mode,
12462 VAR_INIT_STATUS_INITIALIZED);
12463 add_loc_descr (&ret, tmp);
12464 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
12465 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12466 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12467 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
12468 VAR_INIT_STATUS_INITIALIZED);
12469 if (tmp == NULL)
12470 return NULL;
12471 add_loc_descr (&ret, tmp);
12472 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12473 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
12474 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12475 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12476 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12477 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12478 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12479 VAR_INIT_STATUS_INITIALIZED);
12480 add_loc_descr (&ret, tmp);
12481 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
12482 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12483 add_loc_descr (&ret, l2jump);
12484 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
12485 VAR_INIT_STATUS_INITIALIZED);
12486 add_loc_descr (&ret, tmp);
12487 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12488 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12489 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12490 add_loc_descr (&ret, l1jump);
12491 l2label = new_loc_descr (DW_OP_drop, 0, 0);
12492 add_loc_descr (&ret, l2label);
12493 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12494 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12495 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12496 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12497 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12498 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12499 return ret;
12502 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
12503 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12504 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
12505 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
12507 ROTATERT is similar:
12508 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
12509 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12510 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
12512 static dw_loc_descr_ref
12513 rotate_loc_descriptor (rtx rtl, machine_mode mode,
12514 machine_mode mem_mode)
12516 rtx rtlop1 = XEXP (rtl, 1);
12517 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
12518 int i;
12520 if (GET_MODE_CLASS (mode) != MODE_INT)
12521 return NULL;
12523 if (GET_MODE (rtlop1) != VOIDmode
12524 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
12525 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12526 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12527 VAR_INIT_STATUS_INITIALIZED);
12528 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12529 VAR_INIT_STATUS_INITIALIZED);
12530 if (op0 == NULL || op1 == NULL)
12531 return NULL;
12532 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12533 for (i = 0; i < 2; i++)
12535 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
12536 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
12537 mode, mem_mode,
12538 VAR_INIT_STATUS_INITIALIZED);
12539 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12540 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12541 ? DW_OP_const4u
12542 : HOST_BITS_PER_WIDE_INT == 64
12543 ? DW_OP_const8u : DW_OP_constu,
12544 GET_MODE_MASK (mode), 0);
12545 else
12546 mask[i] = NULL;
12547 if (mask[i] == NULL)
12548 return NULL;
12549 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
12551 ret = op0;
12552 add_loc_descr (&ret, op1);
12553 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12554 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12555 if (GET_CODE (rtl) == ROTATERT)
12557 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12558 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12559 GET_MODE_BITSIZE (mode), 0));
12561 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12562 if (mask[0] != NULL)
12563 add_loc_descr (&ret, mask[0]);
12564 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12565 if (mask[1] != NULL)
12567 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12568 add_loc_descr (&ret, mask[1]);
12569 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12571 if (GET_CODE (rtl) == ROTATE)
12573 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12574 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12575 GET_MODE_BITSIZE (mode), 0));
12577 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12578 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12579 return ret;
12582 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
12583 for DEBUG_PARAMETER_REF RTL. */
12585 static dw_loc_descr_ref
12586 parameter_ref_descriptor (rtx rtl)
12588 dw_loc_descr_ref ret;
12589 dw_die_ref ref;
12591 if (dwarf_strict)
12592 return NULL;
12593 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
12594 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
12595 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
12596 if (ref)
12598 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12599 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
12600 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
12602 else
12604 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
12605 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
12607 return ret;
12610 /* The following routine converts the RTL for a variable or parameter
12611 (resident in memory) into an equivalent Dwarf representation of a
12612 mechanism for getting the address of that same variable onto the top of a
12613 hypothetical "address evaluation" stack.
12615 When creating memory location descriptors, we are effectively transforming
12616 the RTL for a memory-resident object into its Dwarf postfix expression
12617 equivalent. This routine recursively descends an RTL tree, turning
12618 it into Dwarf postfix code as it goes.
12620 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
12622 MEM_MODE is the mode of the memory reference, needed to handle some
12623 autoincrement addressing modes.
12625 Return 0 if we can't represent the location. */
12627 dw_loc_descr_ref
12628 mem_loc_descriptor (rtx rtl, machine_mode mode,
12629 machine_mode mem_mode,
12630 enum var_init_status initialized)
12632 dw_loc_descr_ref mem_loc_result = NULL;
12633 enum dwarf_location_atom op;
12634 dw_loc_descr_ref op0, op1;
12635 rtx inner = NULL_RTX;
12637 if (mode == VOIDmode)
12638 mode = GET_MODE (rtl);
12640 /* Note that for a dynamically sized array, the location we will generate a
12641 description of here will be the lowest numbered location which is
12642 actually within the array. That's *not* necessarily the same as the
12643 zeroth element of the array. */
12645 rtl = targetm.delegitimize_address (rtl);
12647 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
12648 return NULL;
12650 switch (GET_CODE (rtl))
12652 case POST_INC:
12653 case POST_DEC:
12654 case POST_MODIFY:
12655 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
12657 case SUBREG:
12658 /* The case of a subreg may arise when we have a local (register)
12659 variable or a formal (register) parameter which doesn't quite fill
12660 up an entire register. For now, just assume that it is
12661 legitimate to make the Dwarf info refer to the whole register which
12662 contains the given subreg. */
12663 if (!subreg_lowpart_p (rtl))
12664 break;
12665 inner = SUBREG_REG (rtl);
12666 case TRUNCATE:
12667 if (inner == NULL_RTX)
12668 inner = XEXP (rtl, 0);
12669 if (GET_MODE_CLASS (mode) == MODE_INT
12670 && GET_MODE_CLASS (GET_MODE (inner)) == MODE_INT
12671 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12672 #ifdef POINTERS_EXTEND_UNSIGNED
12673 || (mode == Pmode && mem_mode != VOIDmode)
12674 #endif
12676 && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
12678 mem_loc_result = mem_loc_descriptor (inner,
12679 GET_MODE (inner),
12680 mem_mode, initialized);
12681 break;
12683 if (dwarf_strict)
12684 break;
12685 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
12686 break;
12687 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
12688 && (GET_MODE_CLASS (mode) != MODE_INT
12689 || GET_MODE_CLASS (GET_MODE (inner)) != MODE_INT))
12690 break;
12691 else
12693 dw_die_ref type_die;
12694 dw_loc_descr_ref cvt;
12696 mem_loc_result = mem_loc_descriptor (inner,
12697 GET_MODE (inner),
12698 mem_mode, initialized);
12699 if (mem_loc_result == NULL)
12700 break;
12701 type_die = base_type_for_mode (mode,
12702 GET_MODE_CLASS (mode) == MODE_INT);
12703 if (type_die == NULL)
12705 mem_loc_result = NULL;
12706 break;
12708 if (GET_MODE_SIZE (mode)
12709 != GET_MODE_SIZE (GET_MODE (inner)))
12710 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12711 else
12712 cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
12713 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12714 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12715 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12716 add_loc_descr (&mem_loc_result, cvt);
12718 break;
12720 case REG:
12721 if (GET_MODE_CLASS (mode) != MODE_INT
12722 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12723 && rtl != arg_pointer_rtx
12724 && rtl != frame_pointer_rtx
12725 #ifdef POINTERS_EXTEND_UNSIGNED
12726 && (mode != Pmode || mem_mode == VOIDmode)
12727 #endif
12730 dw_die_ref type_die;
12731 unsigned int dbx_regnum;
12733 if (dwarf_strict)
12734 break;
12735 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
12736 break;
12737 type_die = base_type_for_mode (mode,
12738 GET_MODE_CLASS (mode) == MODE_INT);
12739 if (type_die == NULL)
12740 break;
12742 dbx_regnum = dbx_reg_number (rtl);
12743 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12744 break;
12745 mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
12746 dbx_regnum, 0);
12747 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12748 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12749 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
12750 break;
12752 /* Whenever a register number forms a part of the description of the
12753 method for calculating the (dynamic) address of a memory resident
12754 object, DWARF rules require the register number be referred to as
12755 a "base register". This distinction is not based in any way upon
12756 what category of register the hardware believes the given register
12757 belongs to. This is strictly DWARF terminology we're dealing with
12758 here. Note that in cases where the location of a memory-resident
12759 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
12760 OP_CONST (0)) the actual DWARF location descriptor that we generate
12761 may just be OP_BASEREG (basereg). This may look deceptively like
12762 the object in question was allocated to a register (rather than in
12763 memory) so DWARF consumers need to be aware of the subtle
12764 distinction between OP_REG and OP_BASEREG. */
12765 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
12766 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
12767 else if (stack_realign_drap
12768 && crtl->drap_reg
12769 && crtl->args.internal_arg_pointer == rtl
12770 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
12772 /* If RTL is internal_arg_pointer, which has been optimized
12773 out, use DRAP instead. */
12774 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
12775 VAR_INIT_STATUS_INITIALIZED);
12777 break;
12779 case SIGN_EXTEND:
12780 case ZERO_EXTEND:
12781 if (GET_MODE_CLASS (mode) != MODE_INT)
12782 break;
12783 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12784 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12785 if (op0 == 0)
12786 break;
12787 else if (GET_CODE (rtl) == ZERO_EXTEND
12788 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12789 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12790 < HOST_BITS_PER_WIDE_INT
12791 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
12792 to expand zero extend as two shifts instead of
12793 masking. */
12794 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
12796 machine_mode imode = GET_MODE (XEXP (rtl, 0));
12797 mem_loc_result = op0;
12798 add_loc_descr (&mem_loc_result,
12799 int_loc_descriptor (GET_MODE_MASK (imode)));
12800 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
12802 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12804 int shift = DWARF2_ADDR_SIZE
12805 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
12806 shift *= BITS_PER_UNIT;
12807 if (GET_CODE (rtl) == SIGN_EXTEND)
12808 op = DW_OP_shra;
12809 else
12810 op = DW_OP_shr;
12811 mem_loc_result = op0;
12812 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12813 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12814 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12815 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12817 else if (!dwarf_strict)
12819 dw_die_ref type_die1, type_die2;
12820 dw_loc_descr_ref cvt;
12822 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12823 GET_CODE (rtl) == ZERO_EXTEND);
12824 if (type_die1 == NULL)
12825 break;
12826 type_die2 = base_type_for_mode (mode, 1);
12827 if (type_die2 == NULL)
12828 break;
12829 mem_loc_result = op0;
12830 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12831 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12832 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
12833 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12834 add_loc_descr (&mem_loc_result, cvt);
12835 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12836 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12837 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
12838 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12839 add_loc_descr (&mem_loc_result, cvt);
12841 break;
12843 case MEM:
12845 rtx new_rtl = avoid_constant_pool_reference (rtl);
12846 if (new_rtl != rtl)
12848 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
12849 initialized);
12850 if (mem_loc_result != NULL)
12851 return mem_loc_result;
12854 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
12855 get_address_mode (rtl), mode,
12856 VAR_INIT_STATUS_INITIALIZED);
12857 if (mem_loc_result == NULL)
12858 mem_loc_result = tls_mem_loc_descriptor (rtl);
12859 if (mem_loc_result != NULL)
12861 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12862 || GET_MODE_CLASS (mode) != MODE_INT)
12864 dw_die_ref type_die;
12865 dw_loc_descr_ref deref;
12867 if (dwarf_strict)
12868 return NULL;
12869 type_die
12870 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
12871 if (type_die == NULL)
12872 return NULL;
12873 deref = new_loc_descr (DW_OP_GNU_deref_type,
12874 GET_MODE_SIZE (mode), 0);
12875 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12876 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12877 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
12878 add_loc_descr (&mem_loc_result, deref);
12880 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12881 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
12882 else
12883 add_loc_descr (&mem_loc_result,
12884 new_loc_descr (DW_OP_deref_size,
12885 GET_MODE_SIZE (mode), 0));
12887 break;
12889 case LO_SUM:
12890 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
12892 case LABEL_REF:
12893 /* Some ports can transform a symbol ref into a label ref, because
12894 the symbol ref is too far away and has to be dumped into a constant
12895 pool. */
12896 case CONST:
12897 case SYMBOL_REF:
12898 if ((GET_MODE_CLASS (mode) != MODE_INT
12899 && GET_MODE_CLASS (mode) != MODE_PARTIAL_INT)
12900 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12901 #ifdef POINTERS_EXTEND_UNSIGNED
12902 && (mode != Pmode || mem_mode == VOIDmode)
12903 #endif
12905 break;
12906 if (GET_CODE (rtl) == SYMBOL_REF
12907 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12909 dw_loc_descr_ref temp;
12911 /* If this is not defined, we have no way to emit the data. */
12912 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
12913 break;
12915 temp = new_addr_loc_descr (rtl, dtprel_true);
12917 mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
12918 add_loc_descr (&mem_loc_result, temp);
12920 break;
12923 if (!const_ok_for_output (rtl))
12925 if (GET_CODE (rtl) == CONST)
12926 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12927 initialized);
12928 break;
12931 symref:
12932 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
12933 vec_safe_push (used_rtx_array, rtl);
12934 break;
12936 case CONCAT:
12937 case CONCATN:
12938 case VAR_LOCATION:
12939 case DEBUG_IMPLICIT_PTR:
12940 expansion_failed (NULL_TREE, rtl,
12941 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
12942 return 0;
12944 case ENTRY_VALUE:
12945 if (dwarf_strict)
12946 return NULL;
12947 if (REG_P (ENTRY_VALUE_EXP (rtl)))
12949 if (GET_MODE_CLASS (mode) != MODE_INT
12950 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12951 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12952 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12953 else
12955 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
12956 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12957 return NULL;
12958 op0 = one_reg_loc_descriptor (dbx_regnum,
12959 VAR_INIT_STATUS_INITIALIZED);
12962 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
12963 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
12965 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12966 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12967 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
12968 return NULL;
12970 else
12971 gcc_unreachable ();
12972 if (op0 == NULL)
12973 return NULL;
12974 mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
12975 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
12976 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
12977 break;
12979 case DEBUG_PARAMETER_REF:
12980 mem_loc_result = parameter_ref_descriptor (rtl);
12981 break;
12983 case PRE_MODIFY:
12984 /* Extract the PLUS expression nested inside and fall into
12985 PLUS code below. */
12986 rtl = XEXP (rtl, 1);
12987 goto plus;
12989 case PRE_INC:
12990 case PRE_DEC:
12991 /* Turn these into a PLUS expression and fall into the PLUS code
12992 below. */
12993 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
12994 gen_int_mode (GET_CODE (rtl) == PRE_INC
12995 ? GET_MODE_UNIT_SIZE (mem_mode)
12996 : -GET_MODE_UNIT_SIZE (mem_mode),
12997 mode));
12999 /* ... fall through ... */
13001 case PLUS:
13002 plus:
13003 if (is_based_loc (rtl)
13004 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13005 || XEXP (rtl, 0) == arg_pointer_rtx
13006 || XEXP (rtl, 0) == frame_pointer_rtx)
13007 && GET_MODE_CLASS (mode) == MODE_INT)
13008 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
13009 INTVAL (XEXP (rtl, 1)),
13010 VAR_INIT_STATUS_INITIALIZED);
13011 else
13013 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13014 VAR_INIT_STATUS_INITIALIZED);
13015 if (mem_loc_result == 0)
13016 break;
13018 if (CONST_INT_P (XEXP (rtl, 1))
13019 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
13020 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
13021 else
13023 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13024 VAR_INIT_STATUS_INITIALIZED);
13025 if (op1 == 0)
13026 return NULL;
13027 add_loc_descr (&mem_loc_result, op1);
13028 add_loc_descr (&mem_loc_result,
13029 new_loc_descr (DW_OP_plus, 0, 0));
13032 break;
13034 /* If a pseudo-reg is optimized away, it is possible for it to
13035 be replaced with a MEM containing a multiply or shift. */
13036 case MINUS:
13037 op = DW_OP_minus;
13038 goto do_binop;
13040 case MULT:
13041 op = DW_OP_mul;
13042 goto do_binop;
13044 case DIV:
13045 if (!dwarf_strict
13046 && GET_MODE_CLASS (mode) == MODE_INT
13047 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
13049 mem_loc_result = typed_binop (DW_OP_div, rtl,
13050 base_type_for_mode (mode, 0),
13051 mode, mem_mode);
13052 break;
13054 op = DW_OP_div;
13055 goto do_binop;
13057 case UMOD:
13058 op = DW_OP_mod;
13059 goto do_binop;
13061 case ASHIFT:
13062 op = DW_OP_shl;
13063 goto do_shift;
13065 case ASHIFTRT:
13066 op = DW_OP_shra;
13067 goto do_shift;
13069 case LSHIFTRT:
13070 op = DW_OP_shr;
13071 goto do_shift;
13073 do_shift:
13074 if (GET_MODE_CLASS (mode) != MODE_INT)
13075 break;
13076 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13077 VAR_INIT_STATUS_INITIALIZED);
13079 rtx rtlop1 = XEXP (rtl, 1);
13080 if (GET_MODE (rtlop1) != VOIDmode
13081 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
13082 < GET_MODE_BITSIZE (mode))
13083 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
13084 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
13085 VAR_INIT_STATUS_INITIALIZED);
13088 if (op0 == 0 || op1 == 0)
13089 break;
13091 mem_loc_result = op0;
13092 add_loc_descr (&mem_loc_result, op1);
13093 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13094 break;
13096 case AND:
13097 op = DW_OP_and;
13098 goto do_binop;
13100 case IOR:
13101 op = DW_OP_or;
13102 goto do_binop;
13104 case XOR:
13105 op = DW_OP_xor;
13106 goto do_binop;
13108 do_binop:
13109 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13110 VAR_INIT_STATUS_INITIALIZED);
13111 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13112 VAR_INIT_STATUS_INITIALIZED);
13114 if (op0 == 0 || op1 == 0)
13115 break;
13117 mem_loc_result = op0;
13118 add_loc_descr (&mem_loc_result, op1);
13119 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13120 break;
13122 case MOD:
13123 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
13125 mem_loc_result = typed_binop (DW_OP_mod, rtl,
13126 base_type_for_mode (mode, 0),
13127 mode, mem_mode);
13128 break;
13131 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13132 VAR_INIT_STATUS_INITIALIZED);
13133 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13134 VAR_INIT_STATUS_INITIALIZED);
13136 if (op0 == 0 || op1 == 0)
13137 break;
13139 mem_loc_result = op0;
13140 add_loc_descr (&mem_loc_result, op1);
13141 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13142 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13143 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
13144 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
13145 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
13146 break;
13148 case UDIV:
13149 if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
13151 if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
13153 op = DW_OP_div;
13154 goto do_binop;
13156 mem_loc_result = typed_binop (DW_OP_div, rtl,
13157 base_type_for_mode (mode, 1),
13158 mode, mem_mode);
13160 break;
13162 case NOT:
13163 op = DW_OP_not;
13164 goto do_unop;
13166 case ABS:
13167 op = DW_OP_abs;
13168 goto do_unop;
13170 case NEG:
13171 op = DW_OP_neg;
13172 goto do_unop;
13174 do_unop:
13175 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13176 VAR_INIT_STATUS_INITIALIZED);
13178 if (op0 == 0)
13179 break;
13181 mem_loc_result = op0;
13182 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13183 break;
13185 case CONST_INT:
13186 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13187 #ifdef POINTERS_EXTEND_UNSIGNED
13188 || (mode == Pmode
13189 && mem_mode != VOIDmode
13190 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
13191 #endif
13194 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13195 break;
13197 if (!dwarf_strict
13198 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
13199 || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
13201 dw_die_ref type_die = base_type_for_mode (mode, 1);
13202 machine_mode amode;
13203 if (type_die == NULL)
13204 return NULL;
13205 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
13206 MODE_INT, 0);
13207 if (INTVAL (rtl) >= 0
13208 && amode != BLKmode
13209 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
13210 /* const DW_OP_GNU_convert <XXX> vs.
13211 DW_OP_GNU_const_type <XXX, 1, const>. */
13212 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
13213 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
13215 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13216 op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13217 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13218 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13219 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
13220 add_loc_descr (&mem_loc_result, op0);
13221 return mem_loc_result;
13223 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
13224 INTVAL (rtl));
13225 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13226 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13227 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13228 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
13229 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13230 else
13232 mem_loc_result->dw_loc_oprnd2.val_class
13233 = dw_val_class_const_double;
13234 mem_loc_result->dw_loc_oprnd2.v.val_double
13235 = double_int::from_shwi (INTVAL (rtl));
13238 break;
13240 case CONST_DOUBLE:
13241 if (!dwarf_strict)
13243 dw_die_ref type_die;
13245 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
13246 CONST_DOUBLE rtx could represent either a large integer
13247 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
13248 the value is always a floating point constant.
13250 When it is an integer, a CONST_DOUBLE is used whenever
13251 the constant requires 2 HWIs to be adequately represented.
13252 We output CONST_DOUBLEs as blocks. */
13253 if (mode == VOIDmode
13254 || (GET_MODE (rtl) == VOIDmode
13255 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
13256 break;
13257 type_die = base_type_for_mode (mode,
13258 GET_MODE_CLASS (mode) == MODE_INT);
13259 if (type_die == NULL)
13260 return NULL;
13261 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
13262 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13263 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13264 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13265 #if TARGET_SUPPORTS_WIDE_INT == 0
13266 if (!SCALAR_FLOAT_MODE_P (mode))
13268 mem_loc_result->dw_loc_oprnd2.val_class
13269 = dw_val_class_const_double;
13270 mem_loc_result->dw_loc_oprnd2.v.val_double
13271 = rtx_to_double_int (rtl);
13273 else
13274 #endif
13276 unsigned int length = GET_MODE_SIZE (mode);
13277 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
13279 insert_float (rtl, array);
13280 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13281 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13282 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13283 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13286 break;
13288 case CONST_WIDE_INT:
13289 if (!dwarf_strict)
13291 dw_die_ref type_die;
13293 type_die = base_type_for_mode (mode,
13294 GET_MODE_CLASS (mode) == MODE_INT);
13295 if (type_die == NULL)
13296 return NULL;
13297 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
13298 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13299 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13300 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13301 mem_loc_result->dw_loc_oprnd2.val_class
13302 = dw_val_class_wide_int;
13303 mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
13304 *mem_loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
13306 break;
13308 case EQ:
13309 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
13310 break;
13312 case GE:
13313 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
13314 break;
13316 case GT:
13317 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
13318 break;
13320 case LE:
13321 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
13322 break;
13324 case LT:
13325 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
13326 break;
13328 case NE:
13329 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
13330 break;
13332 case GEU:
13333 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
13334 break;
13336 case GTU:
13337 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
13338 break;
13340 case LEU:
13341 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
13342 break;
13344 case LTU:
13345 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
13346 break;
13348 case UMIN:
13349 case UMAX:
13350 if (GET_MODE_CLASS (mode) != MODE_INT)
13351 break;
13352 /* FALLTHRU */
13353 case SMIN:
13354 case SMAX:
13355 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
13356 break;
13358 case ZERO_EXTRACT:
13359 case SIGN_EXTRACT:
13360 if (CONST_INT_P (XEXP (rtl, 1))
13361 && CONST_INT_P (XEXP (rtl, 2))
13362 && ((unsigned) INTVAL (XEXP (rtl, 1))
13363 + (unsigned) INTVAL (XEXP (rtl, 2))
13364 <= GET_MODE_BITSIZE (mode))
13365 && GET_MODE_CLASS (mode) == MODE_INT
13366 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13367 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
13369 int shift, size;
13370 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13371 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13372 if (op0 == 0)
13373 break;
13374 if (GET_CODE (rtl) == SIGN_EXTRACT)
13375 op = DW_OP_shra;
13376 else
13377 op = DW_OP_shr;
13378 mem_loc_result = op0;
13379 size = INTVAL (XEXP (rtl, 1));
13380 shift = INTVAL (XEXP (rtl, 2));
13381 if (BITS_BIG_ENDIAN)
13382 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
13383 - shift - size;
13384 if (shift + size != (int) DWARF2_ADDR_SIZE)
13386 add_loc_descr (&mem_loc_result,
13387 int_loc_descriptor (DWARF2_ADDR_SIZE
13388 - shift - size));
13389 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13391 if (size != (int) DWARF2_ADDR_SIZE)
13393 add_loc_descr (&mem_loc_result,
13394 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
13395 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13398 break;
13400 case IF_THEN_ELSE:
13402 dw_loc_descr_ref op2, bra_node, drop_node;
13403 op0 = mem_loc_descriptor (XEXP (rtl, 0),
13404 GET_MODE (XEXP (rtl, 0)) == VOIDmode
13405 ? word_mode : GET_MODE (XEXP (rtl, 0)),
13406 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13407 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13408 VAR_INIT_STATUS_INITIALIZED);
13409 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
13410 VAR_INIT_STATUS_INITIALIZED);
13411 if (op0 == NULL || op1 == NULL || op2 == NULL)
13412 break;
13414 mem_loc_result = op1;
13415 add_loc_descr (&mem_loc_result, op2);
13416 add_loc_descr (&mem_loc_result, op0);
13417 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13418 add_loc_descr (&mem_loc_result, bra_node);
13419 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
13420 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
13421 add_loc_descr (&mem_loc_result, drop_node);
13422 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13423 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
13425 break;
13427 case FLOAT_EXTEND:
13428 case FLOAT_TRUNCATE:
13429 case FLOAT:
13430 case UNSIGNED_FLOAT:
13431 case FIX:
13432 case UNSIGNED_FIX:
13433 if (!dwarf_strict)
13435 dw_die_ref type_die;
13436 dw_loc_descr_ref cvt;
13438 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13439 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13440 if (op0 == NULL)
13441 break;
13442 if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
13443 && (GET_CODE (rtl) == FLOAT
13444 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
13445 <= DWARF2_ADDR_SIZE))
13447 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
13448 GET_CODE (rtl) == UNSIGNED_FLOAT);
13449 if (type_die == NULL)
13450 break;
13451 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13452 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13453 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13454 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13455 add_loc_descr (&op0, cvt);
13457 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
13458 if (type_die == NULL)
13459 break;
13460 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13461 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13462 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13463 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13464 add_loc_descr (&op0, cvt);
13465 if (GET_MODE_CLASS (mode) == MODE_INT
13466 && (GET_CODE (rtl) == FIX
13467 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
13469 op0 = convert_descriptor_to_mode (mode, op0);
13470 if (op0 == NULL)
13471 break;
13473 mem_loc_result = op0;
13475 break;
13477 case CLZ:
13478 case CTZ:
13479 case FFS:
13480 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
13481 break;
13483 case POPCOUNT:
13484 case PARITY:
13485 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
13486 break;
13488 case BSWAP:
13489 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
13490 break;
13492 case ROTATE:
13493 case ROTATERT:
13494 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
13495 break;
13497 case COMPARE:
13498 /* In theory, we could implement the above. */
13499 /* DWARF cannot represent the unsigned compare operations
13500 natively. */
13501 case SS_MULT:
13502 case US_MULT:
13503 case SS_DIV:
13504 case US_DIV:
13505 case SS_PLUS:
13506 case US_PLUS:
13507 case SS_MINUS:
13508 case US_MINUS:
13509 case SS_NEG:
13510 case US_NEG:
13511 case SS_ABS:
13512 case SS_ASHIFT:
13513 case US_ASHIFT:
13514 case SS_TRUNCATE:
13515 case US_TRUNCATE:
13516 case UNORDERED:
13517 case ORDERED:
13518 case UNEQ:
13519 case UNGE:
13520 case UNGT:
13521 case UNLE:
13522 case UNLT:
13523 case LTGT:
13524 case FRACT_CONVERT:
13525 case UNSIGNED_FRACT_CONVERT:
13526 case SAT_FRACT:
13527 case UNSIGNED_SAT_FRACT:
13528 case SQRT:
13529 case ASM_OPERANDS:
13530 case VEC_MERGE:
13531 case VEC_SELECT:
13532 case VEC_CONCAT:
13533 case VEC_DUPLICATE:
13534 case UNSPEC:
13535 case HIGH:
13536 case FMA:
13537 case STRICT_LOW_PART:
13538 case CONST_VECTOR:
13539 case CONST_FIXED:
13540 case CLRSB:
13541 case CLOBBER:
13542 /* If delegitimize_address couldn't do anything with the UNSPEC, we
13543 can't express it in the debug info. This can happen e.g. with some
13544 TLS UNSPECs. */
13545 break;
13547 case CONST_STRING:
13548 resolve_one_addr (&rtl);
13549 goto symref;
13551 default:
13552 #ifdef ENABLE_CHECKING
13553 print_rtl (stderr, rtl);
13554 gcc_unreachable ();
13555 #else
13556 break;
13557 #endif
13560 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13561 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13563 return mem_loc_result;
13566 /* Return a descriptor that describes the concatenation of two locations.
13567 This is typically a complex variable. */
13569 static dw_loc_descr_ref
13570 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
13572 dw_loc_descr_ref cc_loc_result = NULL;
13573 dw_loc_descr_ref x0_ref
13574 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13575 dw_loc_descr_ref x1_ref
13576 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13578 if (x0_ref == 0 || x1_ref == 0)
13579 return 0;
13581 cc_loc_result = x0_ref;
13582 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
13584 add_loc_descr (&cc_loc_result, x1_ref);
13585 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
13587 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13588 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13590 return cc_loc_result;
13593 /* Return a descriptor that describes the concatenation of N
13594 locations. */
13596 static dw_loc_descr_ref
13597 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
13599 unsigned int i;
13600 dw_loc_descr_ref cc_loc_result = NULL;
13601 unsigned int n = XVECLEN (concatn, 0);
13603 for (i = 0; i < n; ++i)
13605 dw_loc_descr_ref ref;
13606 rtx x = XVECEXP (concatn, 0, i);
13608 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13609 if (ref == NULL)
13610 return NULL;
13612 add_loc_descr (&cc_loc_result, ref);
13613 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
13616 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13617 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13619 return cc_loc_result;
13622 /* Helper function for loc_descriptor. Return DW_OP_GNU_implicit_pointer
13623 for DEBUG_IMPLICIT_PTR RTL. */
13625 static dw_loc_descr_ref
13626 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
13628 dw_loc_descr_ref ret;
13629 dw_die_ref ref;
13631 if (dwarf_strict)
13632 return NULL;
13633 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
13634 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
13635 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
13636 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
13637 ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
13638 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
13639 if (ref)
13641 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13642 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
13643 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
13645 else
13647 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
13648 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
13650 return ret;
13653 /* Output a proper Dwarf location descriptor for a variable or parameter
13654 which is either allocated in a register or in a memory location. For a
13655 register, we just generate an OP_REG and the register number. For a
13656 memory location we provide a Dwarf postfix expression describing how to
13657 generate the (dynamic) address of the object onto the address stack.
13659 MODE is mode of the decl if this loc_descriptor is going to be used in
13660 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
13661 allowed, VOIDmode otherwise.
13663 If we don't know how to describe it, return 0. */
13665 static dw_loc_descr_ref
13666 loc_descriptor (rtx rtl, machine_mode mode,
13667 enum var_init_status initialized)
13669 dw_loc_descr_ref loc_result = NULL;
13671 switch (GET_CODE (rtl))
13673 case SUBREG:
13674 /* The case of a subreg may arise when we have a local (register)
13675 variable or a formal (register) parameter which doesn't quite fill
13676 up an entire register. For now, just assume that it is
13677 legitimate to make the Dwarf info refer to the whole register which
13678 contains the given subreg. */
13679 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
13680 loc_result = loc_descriptor (SUBREG_REG (rtl),
13681 GET_MODE (SUBREG_REG (rtl)), initialized);
13682 else
13683 goto do_default;
13684 break;
13686 case REG:
13687 loc_result = reg_loc_descriptor (rtl, initialized);
13688 break;
13690 case MEM:
13691 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13692 GET_MODE (rtl), initialized);
13693 if (loc_result == NULL)
13694 loc_result = tls_mem_loc_descriptor (rtl);
13695 if (loc_result == NULL)
13697 rtx new_rtl = avoid_constant_pool_reference (rtl);
13698 if (new_rtl != rtl)
13699 loc_result = loc_descriptor (new_rtl, mode, initialized);
13701 break;
13703 case CONCAT:
13704 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
13705 initialized);
13706 break;
13708 case CONCATN:
13709 loc_result = concatn_loc_descriptor (rtl, initialized);
13710 break;
13712 case VAR_LOCATION:
13713 /* Single part. */
13714 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
13716 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
13717 if (GET_CODE (loc) == EXPR_LIST)
13718 loc = XEXP (loc, 0);
13719 loc_result = loc_descriptor (loc, mode, initialized);
13720 break;
13723 rtl = XEXP (rtl, 1);
13724 /* FALLTHRU */
13726 case PARALLEL:
13728 rtvec par_elems = XVEC (rtl, 0);
13729 int num_elem = GET_NUM_ELEM (par_elems);
13730 machine_mode mode;
13731 int i;
13733 /* Create the first one, so we have something to add to. */
13734 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
13735 VOIDmode, initialized);
13736 if (loc_result == NULL)
13737 return NULL;
13738 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
13739 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13740 for (i = 1; i < num_elem; i++)
13742 dw_loc_descr_ref temp;
13744 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
13745 VOIDmode, initialized);
13746 if (temp == NULL)
13747 return NULL;
13748 add_loc_descr (&loc_result, temp);
13749 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
13750 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13753 break;
13755 case CONST_INT:
13756 if (mode != VOIDmode && mode != BLKmode)
13757 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
13758 INTVAL (rtl));
13759 break;
13761 case CONST_DOUBLE:
13762 if (mode == VOIDmode)
13763 mode = GET_MODE (rtl);
13765 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13767 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13769 /* Note that a CONST_DOUBLE rtx could represent either an integer
13770 or a floating-point constant. A CONST_DOUBLE is used whenever
13771 the constant requires more than one word in order to be
13772 adequately represented. We output CONST_DOUBLEs as blocks. */
13773 loc_result = new_loc_descr (DW_OP_implicit_value,
13774 GET_MODE_SIZE (mode), 0);
13775 #if TARGET_SUPPORTS_WIDE_INT == 0
13776 if (!SCALAR_FLOAT_MODE_P (mode))
13778 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
13779 loc_result->dw_loc_oprnd2.v.val_double
13780 = rtx_to_double_int (rtl);
13782 else
13783 #endif
13785 unsigned int length = GET_MODE_SIZE (mode);
13786 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
13788 insert_float (rtl, array);
13789 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13790 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13791 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13792 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13795 break;
13797 case CONST_WIDE_INT:
13798 if (mode == VOIDmode)
13799 mode = GET_MODE (rtl);
13801 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13803 loc_result = new_loc_descr (DW_OP_implicit_value,
13804 GET_MODE_SIZE (mode), 0);
13805 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
13806 loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
13807 *loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
13809 break;
13811 case CONST_VECTOR:
13812 if (mode == VOIDmode)
13813 mode = GET_MODE (rtl);
13815 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13817 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
13818 unsigned int length = CONST_VECTOR_NUNITS (rtl);
13819 unsigned char *array
13820 = ggc_vec_alloc<unsigned char> (length * elt_size);
13821 unsigned int i;
13822 unsigned char *p;
13823 machine_mode imode = GET_MODE_INNER (mode);
13825 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13826 switch (GET_MODE_CLASS (mode))
13828 case MODE_VECTOR_INT:
13829 for (i = 0, p = array; i < length; i++, p += elt_size)
13831 rtx elt = CONST_VECTOR_ELT (rtl, i);
13832 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
13834 break;
13836 case MODE_VECTOR_FLOAT:
13837 for (i = 0, p = array; i < length; i++, p += elt_size)
13839 rtx elt = CONST_VECTOR_ELT (rtl, i);
13840 insert_float (elt, p);
13842 break;
13844 default:
13845 gcc_unreachable ();
13848 loc_result = new_loc_descr (DW_OP_implicit_value,
13849 length * elt_size, 0);
13850 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13851 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
13852 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
13853 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13855 break;
13857 case CONST:
13858 if (mode == VOIDmode
13859 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
13860 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
13861 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
13863 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
13864 break;
13866 /* FALLTHROUGH */
13867 case SYMBOL_REF:
13868 if (!const_ok_for_output (rtl))
13869 break;
13870 case LABEL_REF:
13871 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
13872 && (dwarf_version >= 4 || !dwarf_strict))
13874 loc_result = new_addr_loc_descr (rtl, dtprel_false);
13875 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
13876 vec_safe_push (used_rtx_array, rtl);
13878 break;
13880 case DEBUG_IMPLICIT_PTR:
13881 loc_result = implicit_ptr_descriptor (rtl, 0);
13882 break;
13884 case PLUS:
13885 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
13886 && CONST_INT_P (XEXP (rtl, 1)))
13888 loc_result
13889 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
13890 break;
13892 /* FALLTHRU */
13893 do_default:
13894 default:
13895 if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
13896 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
13897 && dwarf_version >= 4)
13898 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
13900 /* Value expression. */
13901 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
13902 if (loc_result)
13903 add_loc_descr (&loc_result,
13904 new_loc_descr (DW_OP_stack_value, 0, 0));
13906 break;
13909 return loc_result;
13912 /* We need to figure out what section we should use as the base for the
13913 address ranges where a given location is valid.
13914 1. If this particular DECL has a section associated with it, use that.
13915 2. If this function has a section associated with it, use that.
13916 3. Otherwise, use the text section.
13917 XXX: If you split a variable across multiple sections, we won't notice. */
13919 static const char *
13920 secname_for_decl (const_tree decl)
13922 const char *secname;
13924 if (VAR_OR_FUNCTION_DECL_P (decl)
13925 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
13926 && DECL_SECTION_NAME (decl))
13927 secname = DECL_SECTION_NAME (decl);
13928 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
13929 secname = DECL_SECTION_NAME (current_function_decl);
13930 else if (cfun && in_cold_section_p)
13931 secname = crtl->subsections.cold_section_label;
13932 else
13933 secname = text_section_label;
13935 return secname;
13938 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
13940 static bool
13941 decl_by_reference_p (tree decl)
13943 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
13944 || TREE_CODE (decl) == VAR_DECL)
13945 && DECL_BY_REFERENCE (decl));
13948 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13949 for VARLOC. */
13951 static dw_loc_descr_ref
13952 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
13953 enum var_init_status initialized)
13955 int have_address = 0;
13956 dw_loc_descr_ref descr;
13957 machine_mode mode;
13959 if (want_address != 2)
13961 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
13962 /* Single part. */
13963 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13965 varloc = PAT_VAR_LOCATION_LOC (varloc);
13966 if (GET_CODE (varloc) == EXPR_LIST)
13967 varloc = XEXP (varloc, 0);
13968 mode = GET_MODE (varloc);
13969 if (MEM_P (varloc))
13971 rtx addr = XEXP (varloc, 0);
13972 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
13973 mode, initialized);
13974 if (descr)
13975 have_address = 1;
13976 else
13978 rtx x = avoid_constant_pool_reference (varloc);
13979 if (x != varloc)
13980 descr = mem_loc_descriptor (x, mode, VOIDmode,
13981 initialized);
13984 else
13985 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
13987 else
13988 return 0;
13990 else
13992 if (GET_CODE (varloc) == VAR_LOCATION)
13993 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
13994 else
13995 mode = DECL_MODE (loc);
13996 descr = loc_descriptor (varloc, mode, initialized);
13997 have_address = 1;
14000 if (!descr)
14001 return 0;
14003 if (want_address == 2 && !have_address
14004 && (dwarf_version >= 4 || !dwarf_strict))
14006 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14008 expansion_failed (loc, NULL_RTX,
14009 "DWARF address size mismatch");
14010 return 0;
14012 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
14013 have_address = 1;
14015 /* Show if we can't fill the request for an address. */
14016 if (want_address && !have_address)
14018 expansion_failed (loc, NULL_RTX,
14019 "Want address and only have value");
14020 return 0;
14023 /* If we've got an address and don't want one, dereference. */
14024 if (!want_address && have_address)
14026 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14027 enum dwarf_location_atom op;
14029 if (size > DWARF2_ADDR_SIZE || size == -1)
14031 expansion_failed (loc, NULL_RTX,
14032 "DWARF address size mismatch");
14033 return 0;
14035 else if (size == DWARF2_ADDR_SIZE)
14036 op = DW_OP_deref;
14037 else
14038 op = DW_OP_deref_size;
14040 add_loc_descr (&descr, new_loc_descr (op, size, 0));
14043 return descr;
14046 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
14047 if it is not possible. */
14049 static dw_loc_descr_ref
14050 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
14052 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
14053 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
14054 else if (dwarf_version >= 3 || !dwarf_strict)
14055 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
14056 else
14057 return NULL;
14060 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
14061 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
14063 static dw_loc_descr_ref
14064 dw_sra_loc_expr (tree decl, rtx loc)
14066 rtx p;
14067 unsigned HOST_WIDE_INT padsize = 0;
14068 dw_loc_descr_ref descr, *descr_tail;
14069 unsigned HOST_WIDE_INT decl_size;
14070 rtx varloc;
14071 enum var_init_status initialized;
14073 if (DECL_SIZE (decl) == NULL
14074 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
14075 return NULL;
14077 decl_size = tree_to_uhwi (DECL_SIZE (decl));
14078 descr = NULL;
14079 descr_tail = &descr;
14081 for (p = loc; p; p = XEXP (p, 1))
14083 unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
14084 rtx loc_note = *decl_piece_varloc_ptr (p);
14085 dw_loc_descr_ref cur_descr;
14086 dw_loc_descr_ref *tail, last = NULL;
14087 unsigned HOST_WIDE_INT opsize = 0;
14089 if (loc_note == NULL_RTX
14090 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
14092 padsize += bitsize;
14093 continue;
14095 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
14096 varloc = NOTE_VAR_LOCATION (loc_note);
14097 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
14098 if (cur_descr == NULL)
14100 padsize += bitsize;
14101 continue;
14104 /* Check that cur_descr either doesn't use
14105 DW_OP_*piece operations, or their sum is equal
14106 to bitsize. Otherwise we can't embed it. */
14107 for (tail = &cur_descr; *tail != NULL;
14108 tail = &(*tail)->dw_loc_next)
14109 if ((*tail)->dw_loc_opc == DW_OP_piece)
14111 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
14112 * BITS_PER_UNIT;
14113 last = *tail;
14115 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
14117 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
14118 last = *tail;
14121 if (last != NULL && opsize != bitsize)
14123 padsize += bitsize;
14124 /* Discard the current piece of the descriptor and release any
14125 addr_table entries it uses. */
14126 remove_loc_list_addr_table_entries (cur_descr);
14127 continue;
14130 /* If there is a hole, add DW_OP_*piece after empty DWARF
14131 expression, which means that those bits are optimized out. */
14132 if (padsize)
14134 if (padsize > decl_size)
14136 remove_loc_list_addr_table_entries (cur_descr);
14137 goto discard_descr;
14139 decl_size -= padsize;
14140 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
14141 if (*descr_tail == NULL)
14143 remove_loc_list_addr_table_entries (cur_descr);
14144 goto discard_descr;
14146 descr_tail = &(*descr_tail)->dw_loc_next;
14147 padsize = 0;
14149 *descr_tail = cur_descr;
14150 descr_tail = tail;
14151 if (bitsize > decl_size)
14152 goto discard_descr;
14153 decl_size -= bitsize;
14154 if (last == NULL)
14156 HOST_WIDE_INT offset = 0;
14157 if (GET_CODE (varloc) == VAR_LOCATION
14158 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14160 varloc = PAT_VAR_LOCATION_LOC (varloc);
14161 if (GET_CODE (varloc) == EXPR_LIST)
14162 varloc = XEXP (varloc, 0);
14166 if (GET_CODE (varloc) == CONST
14167 || GET_CODE (varloc) == SIGN_EXTEND
14168 || GET_CODE (varloc) == ZERO_EXTEND)
14169 varloc = XEXP (varloc, 0);
14170 else if (GET_CODE (varloc) == SUBREG)
14171 varloc = SUBREG_REG (varloc);
14172 else
14173 break;
14175 while (1);
14176 /* DW_OP_bit_size offset should be zero for register
14177 or implicit location descriptions and empty location
14178 descriptions, but for memory addresses needs big endian
14179 adjustment. */
14180 if (MEM_P (varloc))
14182 unsigned HOST_WIDE_INT memsize
14183 = MEM_SIZE (varloc) * BITS_PER_UNIT;
14184 if (memsize != bitsize)
14186 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
14187 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
14188 goto discard_descr;
14189 if (memsize < bitsize)
14190 goto discard_descr;
14191 if (BITS_BIG_ENDIAN)
14192 offset = memsize - bitsize;
14196 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
14197 if (*descr_tail == NULL)
14198 goto discard_descr;
14199 descr_tail = &(*descr_tail)->dw_loc_next;
14203 /* If there were any non-empty expressions, add padding till the end of
14204 the decl. */
14205 if (descr != NULL && decl_size != 0)
14207 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
14208 if (*descr_tail == NULL)
14209 goto discard_descr;
14211 return descr;
14213 discard_descr:
14214 /* Discard the descriptor and release any addr_table entries it uses. */
14215 remove_loc_list_addr_table_entries (descr);
14216 return NULL;
14219 /* Return the dwarf representation of the location list LOC_LIST of
14220 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
14221 function. */
14223 static dw_loc_list_ref
14224 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
14226 const char *endname, *secname;
14227 rtx varloc;
14228 enum var_init_status initialized;
14229 struct var_loc_node *node;
14230 dw_loc_descr_ref descr;
14231 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
14232 dw_loc_list_ref list = NULL;
14233 dw_loc_list_ref *listp = &list;
14235 /* Now that we know what section we are using for a base,
14236 actually construct the list of locations.
14237 The first location information is what is passed to the
14238 function that creates the location list, and the remaining
14239 locations just get added on to that list.
14240 Note that we only know the start address for a location
14241 (IE location changes), so to build the range, we use
14242 the range [current location start, next location start].
14243 This means we have to special case the last node, and generate
14244 a range of [last location start, end of function label]. */
14246 secname = secname_for_decl (decl);
14248 for (node = loc_list->first; node; node = node->next)
14249 if (GET_CODE (node->loc) == EXPR_LIST
14250 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
14252 if (GET_CODE (node->loc) == EXPR_LIST)
14254 /* This requires DW_OP_{,bit_}piece, which is not usable
14255 inside DWARF expressions. */
14256 if (want_address != 2)
14257 continue;
14258 descr = dw_sra_loc_expr (decl, node->loc);
14259 if (descr == NULL)
14260 continue;
14262 else
14264 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14265 varloc = NOTE_VAR_LOCATION (node->loc);
14266 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
14268 if (descr)
14270 bool range_across_switch = false;
14271 /* If section switch happens in between node->label
14272 and node->next->label (or end of function) and
14273 we can't emit it as a single entry list,
14274 emit two ranges, first one ending at the end
14275 of first partition and second one starting at the
14276 beginning of second partition. */
14277 if (node == loc_list->last_before_switch
14278 && (node != loc_list->first || loc_list->first->next)
14279 && current_function_decl)
14281 endname = cfun->fde->dw_fde_end;
14282 range_across_switch = true;
14284 /* The variable has a location between NODE->LABEL and
14285 NODE->NEXT->LABEL. */
14286 else if (node->next)
14287 endname = node->next->label;
14288 /* If the variable has a location at the last label
14289 it keeps its location until the end of function. */
14290 else if (!current_function_decl)
14291 endname = text_end_label;
14292 else
14294 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
14295 current_function_funcdef_no);
14296 endname = ggc_strdup (label_id);
14299 *listp = new_loc_list (descr, node->label, endname, secname);
14300 if (TREE_CODE (decl) == PARM_DECL
14301 && node == loc_list->first
14302 && NOTE_P (node->loc)
14303 && strcmp (node->label, endname) == 0)
14304 (*listp)->force = true;
14305 listp = &(*listp)->dw_loc_next;
14307 if (range_across_switch)
14309 if (GET_CODE (node->loc) == EXPR_LIST)
14310 descr = dw_sra_loc_expr (decl, node->loc);
14311 else
14313 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14314 varloc = NOTE_VAR_LOCATION (node->loc);
14315 descr = dw_loc_list_1 (decl, varloc, want_address,
14316 initialized);
14318 gcc_assert (descr);
14319 /* The variable has a location between NODE->LABEL and
14320 NODE->NEXT->LABEL. */
14321 if (node->next)
14322 endname = node->next->label;
14323 else
14324 endname = cfun->fde->dw_fde_second_end;
14325 *listp = new_loc_list (descr,
14326 cfun->fde->dw_fde_second_begin,
14327 endname, secname);
14328 listp = &(*listp)->dw_loc_next;
14333 /* Try to avoid the overhead of a location list emitting a location
14334 expression instead, but only if we didn't have more than one
14335 location entry in the first place. If some entries were not
14336 representable, we don't want to pretend a single entry that was
14337 applies to the entire scope in which the variable is
14338 available. */
14339 if (list && loc_list->first->next)
14340 gen_llsym (list);
14342 return list;
14345 /* Return if the loc_list has only single element and thus can be represented
14346 as location description. */
14348 static bool
14349 single_element_loc_list_p (dw_loc_list_ref list)
14351 gcc_assert (!list->dw_loc_next || list->ll_symbol);
14352 return !list->ll_symbol;
14355 /* To each location in list LIST add loc descr REF. */
14357 static void
14358 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
14360 dw_loc_descr_ref copy;
14361 add_loc_descr (&list->expr, ref);
14362 list = list->dw_loc_next;
14363 while (list)
14365 copy = ggc_alloc<dw_loc_descr_node> ();
14366 memcpy (copy, ref, sizeof (dw_loc_descr_node));
14367 add_loc_descr (&list->expr, copy);
14368 while (copy->dw_loc_next)
14370 dw_loc_descr_ref new_copy = ggc_alloc<dw_loc_descr_node> ();
14371 memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
14372 copy->dw_loc_next = new_copy;
14373 copy = new_copy;
14375 list = list->dw_loc_next;
14379 /* Given two lists RET and LIST
14380 produce location list that is result of adding expression in LIST
14381 to expression in RET on each position in program.
14382 Might be destructive on both RET and LIST.
14384 TODO: We handle only simple cases of RET or LIST having at most one
14385 element. General case would inolve sorting the lists in program order
14386 and merging them that will need some additional work.
14387 Adding that will improve quality of debug info especially for SRA-ed
14388 structures. */
14390 static void
14391 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
14393 if (!list)
14394 return;
14395 if (!*ret)
14397 *ret = list;
14398 return;
14400 if (!list->dw_loc_next)
14402 add_loc_descr_to_each (*ret, list->expr);
14403 return;
14405 if (!(*ret)->dw_loc_next)
14407 add_loc_descr_to_each (list, (*ret)->expr);
14408 *ret = list;
14409 return;
14411 expansion_failed (NULL_TREE, NULL_RTX,
14412 "Don't know how to merge two non-trivial"
14413 " location lists.\n");
14414 *ret = NULL;
14415 return;
14418 /* LOC is constant expression. Try a luck, look it up in constant
14419 pool and return its loc_descr of its address. */
14421 static dw_loc_descr_ref
14422 cst_pool_loc_descr (tree loc)
14424 /* Get an RTL for this, if something has been emitted. */
14425 rtx rtl = lookup_constant_def (loc);
14427 if (!rtl || !MEM_P (rtl))
14429 gcc_assert (!rtl);
14430 return 0;
14432 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
14434 /* TODO: We might get more coverage if we was actually delaying expansion
14435 of all expressions till end of compilation when constant pools are fully
14436 populated. */
14437 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
14439 expansion_failed (loc, NULL_RTX,
14440 "CST value in contant pool but not marked.");
14441 return 0;
14443 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
14444 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
14447 /* Return dw_loc_list representing address of addr_expr LOC
14448 by looking for inner INDIRECT_REF expression and turning
14449 it into simple arithmetics.
14451 See loc_list_from_tree for the meaning of CONTEXT. */
14453 static dw_loc_list_ref
14454 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
14455 const loc_descr_context *context)
14457 tree obj, offset;
14458 HOST_WIDE_INT bitsize, bitpos, bytepos;
14459 machine_mode mode;
14460 int unsignedp, volatilep = 0;
14461 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14463 obj = get_inner_reference (TREE_OPERAND (loc, 0),
14464 &bitsize, &bitpos, &offset, &mode,
14465 &unsignedp, &volatilep, false);
14466 STRIP_NOPS (obj);
14467 if (bitpos % BITS_PER_UNIT)
14469 expansion_failed (loc, NULL_RTX, "bitfield access");
14470 return 0;
14472 if (!INDIRECT_REF_P (obj))
14474 expansion_failed (obj,
14475 NULL_RTX, "no indirect ref in inner refrence");
14476 return 0;
14478 if (!offset && !bitpos)
14479 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
14480 context);
14481 else if (toplev
14482 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
14483 && (dwarf_version >= 4 || !dwarf_strict))
14485 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
14486 if (!list_ret)
14487 return 0;
14488 if (offset)
14490 /* Variable offset. */
14491 list_ret1 = loc_list_from_tree (offset, 0, context);
14492 if (list_ret1 == 0)
14493 return 0;
14494 add_loc_list (&list_ret, list_ret1);
14495 if (!list_ret)
14496 return 0;
14497 add_loc_descr_to_each (list_ret,
14498 new_loc_descr (DW_OP_plus, 0, 0));
14500 bytepos = bitpos / BITS_PER_UNIT;
14501 if (bytepos > 0)
14502 add_loc_descr_to_each (list_ret,
14503 new_loc_descr (DW_OP_plus_uconst,
14504 bytepos, 0));
14505 else if (bytepos < 0)
14506 loc_list_plus_const (list_ret, bytepos);
14507 add_loc_descr_to_each (list_ret,
14508 new_loc_descr (DW_OP_stack_value, 0, 0));
14510 return list_ret;
14514 /* Helper structure for location descriptions generation. */
14515 struct loc_descr_context
14517 /* The type that is implicitly referenced by DW_OP_push_object_address, or
14518 NULL_TREE if DW_OP_push_object_address in invalid for this location
14519 description. This is used when processing PLACEHOLDER_EXPR nodes. */
14520 tree context_type;
14521 /* The ..._DECL node that should be translated as a
14522 DW_OP_push_object_address operation. */
14523 tree base_decl;
14526 /* Generate Dwarf location list representing LOC.
14527 If WANT_ADDRESS is false, expression computing LOC will be computed
14528 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
14529 if WANT_ADDRESS is 2, expression computing address useable in location
14530 will be returned (i.e. DW_OP_reg can be used
14531 to refer to register values).
14533 CONTEXT provides information to customize the location descriptions
14534 generation. Its context_type field specifies what type is implicitly
14535 referenced by DW_OP_push_object_address. If it is NULL_TREE, this operation
14536 will not be generated.
14538 If CONTEXT is NULL, the behavior is the same as if both context_type and
14539 base_decl fields were NULL_TREE. */
14541 static dw_loc_list_ref
14542 loc_list_from_tree (tree loc, int want_address,
14543 const struct loc_descr_context *context)
14545 dw_loc_descr_ref ret = NULL, ret1 = NULL;
14546 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14547 int have_address = 0;
14548 enum dwarf_location_atom op;
14550 /* ??? Most of the time we do not take proper care for sign/zero
14551 extending the values properly. Hopefully this won't be a real
14552 problem... */
14554 if (context != NULL
14555 && context->base_decl == loc
14556 && want_address == 0)
14558 if (dwarf_version >= 3 || !dwarf_strict)
14559 return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
14560 NULL, NULL, NULL);
14561 else
14562 return NULL;
14565 switch (TREE_CODE (loc))
14567 case ERROR_MARK:
14568 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
14569 return 0;
14571 case PLACEHOLDER_EXPR:
14572 /* This case involves extracting fields from an object to determine the
14573 position of other fields. It is supposed to appear only as the first
14574 operand of COMPONENT_REF nodes and to reference precisely the type
14575 that the context allows. */
14576 if (context != NULL
14577 && TREE_TYPE (loc) == context->context_type
14578 && want_address >= 1)
14580 if (dwarf_version >= 3 || !dwarf_strict)
14582 ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
14583 have_address = 1;
14584 break;
14586 else
14587 return NULL;
14589 else
14590 expansion_failed (loc, NULL_RTX,
14591 "PLACEHOLDER_EXPR for an unexpected type");
14592 break;
14594 case CALL_EXPR:
14595 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
14596 /* There are no opcodes for these operations. */
14597 return 0;
14599 case PREINCREMENT_EXPR:
14600 case PREDECREMENT_EXPR:
14601 case POSTINCREMENT_EXPR:
14602 case POSTDECREMENT_EXPR:
14603 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
14604 /* There are no opcodes for these operations. */
14605 return 0;
14607 case ADDR_EXPR:
14608 /* If we already want an address, see if there is INDIRECT_REF inside
14609 e.g. for &this->field. */
14610 if (want_address)
14612 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
14613 (loc, want_address == 2, context);
14614 if (list_ret)
14615 have_address = 1;
14616 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
14617 && (ret = cst_pool_loc_descr (loc)))
14618 have_address = 1;
14620 /* Otherwise, process the argument and look for the address. */
14621 if (!list_ret && !ret)
14622 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1, context);
14623 else
14625 if (want_address)
14626 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
14627 return NULL;
14629 break;
14631 case VAR_DECL:
14632 if (DECL_THREAD_LOCAL_P (loc))
14634 rtx rtl;
14635 enum dwarf_location_atom tls_op;
14636 enum dtprel_bool dtprel = dtprel_false;
14638 if (targetm.have_tls)
14640 /* If this is not defined, we have no way to emit the
14641 data. */
14642 if (!targetm.asm_out.output_dwarf_dtprel)
14643 return 0;
14645 /* The way DW_OP_GNU_push_tls_address is specified, we
14646 can only look up addresses of objects in the current
14647 module. We used DW_OP_addr as first op, but that's
14648 wrong, because DW_OP_addr is relocated by the debug
14649 info consumer, while DW_OP_GNU_push_tls_address
14650 operand shouldn't be. */
14651 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
14652 return 0;
14653 dtprel = dtprel_true;
14654 tls_op = DW_OP_GNU_push_tls_address;
14656 else
14658 if (!targetm.emutls.debug_form_tls_address
14659 || !(dwarf_version >= 3 || !dwarf_strict))
14660 return 0;
14661 /* We stuffed the control variable into the DECL_VALUE_EXPR
14662 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
14663 no longer appear in gimple code. We used the control
14664 variable in specific so that we could pick it up here. */
14665 loc = DECL_VALUE_EXPR (loc);
14666 tls_op = DW_OP_form_tls_address;
14669 rtl = rtl_for_decl_location (loc);
14670 if (rtl == NULL_RTX)
14671 return 0;
14673 if (!MEM_P (rtl))
14674 return 0;
14675 rtl = XEXP (rtl, 0);
14676 if (! CONSTANT_P (rtl))
14677 return 0;
14679 ret = new_addr_loc_descr (rtl, dtprel);
14680 ret1 = new_loc_descr (tls_op, 0, 0);
14681 add_loc_descr (&ret, ret1);
14683 have_address = 1;
14684 break;
14686 /* FALLTHRU */
14688 case PARM_DECL:
14689 case RESULT_DECL:
14690 if (DECL_HAS_VALUE_EXPR_P (loc))
14691 return loc_list_from_tree (DECL_VALUE_EXPR (loc),
14692 want_address, context);
14693 /* FALLTHRU */
14695 case FUNCTION_DECL:
14697 rtx rtl;
14698 var_loc_list *loc_list = lookup_decl_loc (loc);
14700 if (loc_list && loc_list->first)
14702 list_ret = dw_loc_list (loc_list, loc, want_address);
14703 have_address = want_address != 0;
14704 break;
14706 rtl = rtl_for_decl_location (loc);
14707 if (rtl == NULL_RTX)
14709 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
14710 return 0;
14712 else if (CONST_INT_P (rtl))
14714 HOST_WIDE_INT val = INTVAL (rtl);
14715 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14716 val &= GET_MODE_MASK (DECL_MODE (loc));
14717 ret = int_loc_descriptor (val);
14719 else if (GET_CODE (rtl) == CONST_STRING)
14721 expansion_failed (loc, NULL_RTX, "CONST_STRING");
14722 return 0;
14724 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
14725 ret = new_addr_loc_descr (rtl, dtprel_false);
14726 else
14728 machine_mode mode, mem_mode;
14730 /* Certain constructs can only be represented at top-level. */
14731 if (want_address == 2)
14733 ret = loc_descriptor (rtl, VOIDmode,
14734 VAR_INIT_STATUS_INITIALIZED);
14735 have_address = 1;
14737 else
14739 mode = GET_MODE (rtl);
14740 mem_mode = VOIDmode;
14741 if (MEM_P (rtl))
14743 mem_mode = mode;
14744 mode = get_address_mode (rtl);
14745 rtl = XEXP (rtl, 0);
14746 have_address = 1;
14748 ret = mem_loc_descriptor (rtl, mode, mem_mode,
14749 VAR_INIT_STATUS_INITIALIZED);
14751 if (!ret)
14752 expansion_failed (loc, rtl,
14753 "failed to produce loc descriptor for rtl");
14756 break;
14758 case MEM_REF:
14759 if (!integer_zerop (TREE_OPERAND (loc, 1)))
14761 have_address = 1;
14762 goto do_plus;
14764 /* Fallthru. */
14765 case INDIRECT_REF:
14766 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14767 have_address = 1;
14768 break;
14770 case TARGET_MEM_REF:
14771 case SSA_NAME:
14772 case DEBUG_EXPR_DECL:
14773 return NULL;
14775 case COMPOUND_EXPR:
14776 return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address, context);
14778 CASE_CONVERT:
14779 case VIEW_CONVERT_EXPR:
14780 case SAVE_EXPR:
14781 case MODIFY_EXPR:
14782 return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address, context);
14784 case COMPONENT_REF:
14785 case BIT_FIELD_REF:
14786 case ARRAY_REF:
14787 case ARRAY_RANGE_REF:
14788 case REALPART_EXPR:
14789 case IMAGPART_EXPR:
14791 tree obj, offset;
14792 HOST_WIDE_INT bitsize, bitpos, bytepos;
14793 machine_mode mode;
14794 int unsignedp, volatilep = 0;
14796 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
14797 &unsignedp, &volatilep, false);
14799 gcc_assert (obj != loc);
14801 list_ret = loc_list_from_tree (obj,
14802 want_address == 2
14803 && !bitpos && !offset ? 2 : 1,
14804 context);
14805 /* TODO: We can extract value of the small expression via shifting even
14806 for nonzero bitpos. */
14807 if (list_ret == 0)
14808 return 0;
14809 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
14811 expansion_failed (loc, NULL_RTX,
14812 "bitfield access");
14813 return 0;
14816 if (offset != NULL_TREE)
14818 /* Variable offset. */
14819 list_ret1 = loc_list_from_tree (offset, 0, context);
14820 if (list_ret1 == 0)
14821 return 0;
14822 add_loc_list (&list_ret, list_ret1);
14823 if (!list_ret)
14824 return 0;
14825 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
14828 bytepos = bitpos / BITS_PER_UNIT;
14829 if (bytepos > 0)
14830 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
14831 else if (bytepos < 0)
14832 loc_list_plus_const (list_ret, bytepos);
14834 have_address = 1;
14835 break;
14838 case INTEGER_CST:
14839 if ((want_address || !tree_fits_shwi_p (loc))
14840 && (ret = cst_pool_loc_descr (loc)))
14841 have_address = 1;
14842 else if (want_address == 2
14843 && tree_fits_shwi_p (loc)
14844 && (ret = address_of_int_loc_descriptor
14845 (int_size_in_bytes (TREE_TYPE (loc)),
14846 tree_to_shwi (loc))))
14847 have_address = 1;
14848 else if (tree_fits_shwi_p (loc))
14849 ret = int_loc_descriptor (tree_to_shwi (loc));
14850 else
14852 expansion_failed (loc, NULL_RTX,
14853 "Integer operand is not host integer");
14854 return 0;
14856 break;
14858 case CONSTRUCTOR:
14859 case REAL_CST:
14860 case STRING_CST:
14861 case COMPLEX_CST:
14862 if ((ret = cst_pool_loc_descr (loc)))
14863 have_address = 1;
14864 else
14865 /* We can construct small constants here using int_loc_descriptor. */
14866 expansion_failed (loc, NULL_RTX,
14867 "constructor or constant not in constant pool");
14868 break;
14870 case TRUTH_AND_EXPR:
14871 case TRUTH_ANDIF_EXPR:
14872 case BIT_AND_EXPR:
14873 op = DW_OP_and;
14874 goto do_binop;
14876 case TRUTH_XOR_EXPR:
14877 case BIT_XOR_EXPR:
14878 op = DW_OP_xor;
14879 goto do_binop;
14881 case TRUTH_OR_EXPR:
14882 case TRUTH_ORIF_EXPR:
14883 case BIT_IOR_EXPR:
14884 op = DW_OP_or;
14885 goto do_binop;
14887 case FLOOR_DIV_EXPR:
14888 case CEIL_DIV_EXPR:
14889 case ROUND_DIV_EXPR:
14890 case TRUNC_DIV_EXPR:
14891 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14892 return 0;
14893 op = DW_OP_div;
14894 goto do_binop;
14896 case MINUS_EXPR:
14897 op = DW_OP_minus;
14898 goto do_binop;
14900 case FLOOR_MOD_EXPR:
14901 case CEIL_MOD_EXPR:
14902 case ROUND_MOD_EXPR:
14903 case TRUNC_MOD_EXPR:
14904 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14906 op = DW_OP_mod;
14907 goto do_binop;
14909 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14910 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
14911 if (list_ret == 0 || list_ret1 == 0)
14912 return 0;
14914 add_loc_list (&list_ret, list_ret1);
14915 if (list_ret == 0)
14916 return 0;
14917 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14918 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14919 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
14920 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
14921 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
14922 break;
14924 case MULT_EXPR:
14925 op = DW_OP_mul;
14926 goto do_binop;
14928 case LSHIFT_EXPR:
14929 op = DW_OP_shl;
14930 goto do_binop;
14932 case RSHIFT_EXPR:
14933 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
14934 goto do_binop;
14936 case POINTER_PLUS_EXPR:
14937 case PLUS_EXPR:
14938 do_plus:
14939 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
14941 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14942 if (list_ret == 0)
14943 return 0;
14945 loc_list_plus_const (list_ret, tree_to_shwi (TREE_OPERAND (loc, 1)));
14946 break;
14949 op = DW_OP_plus;
14950 goto do_binop;
14952 case LE_EXPR:
14953 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14954 return 0;
14956 op = DW_OP_le;
14957 goto do_binop;
14959 case GE_EXPR:
14960 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14961 return 0;
14963 op = DW_OP_ge;
14964 goto do_binop;
14966 case LT_EXPR:
14967 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14968 return 0;
14970 op = DW_OP_lt;
14971 goto do_binop;
14973 case GT_EXPR:
14974 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14975 return 0;
14977 op = DW_OP_gt;
14978 goto do_binop;
14980 case EQ_EXPR:
14981 op = DW_OP_eq;
14982 goto do_binop;
14984 case NE_EXPR:
14985 op = DW_OP_ne;
14986 goto do_binop;
14988 do_binop:
14989 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14990 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
14991 if (list_ret == 0 || list_ret1 == 0)
14992 return 0;
14994 add_loc_list (&list_ret, list_ret1);
14995 if (list_ret == 0)
14996 return 0;
14997 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14998 break;
15000 case TRUTH_NOT_EXPR:
15001 case BIT_NOT_EXPR:
15002 op = DW_OP_not;
15003 goto do_unop;
15005 case ABS_EXPR:
15006 op = DW_OP_abs;
15007 goto do_unop;
15009 case NEGATE_EXPR:
15010 op = DW_OP_neg;
15011 goto do_unop;
15013 do_unop:
15014 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
15015 if (list_ret == 0)
15016 return 0;
15018 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
15019 break;
15021 case MIN_EXPR:
15022 case MAX_EXPR:
15024 const enum tree_code code =
15025 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
15027 loc = build3 (COND_EXPR, TREE_TYPE (loc),
15028 build2 (code, integer_type_node,
15029 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
15030 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
15033 /* ... fall through ... */
15035 case COND_EXPR:
15037 dw_loc_descr_ref lhs
15038 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
15039 dw_loc_list_ref rhs
15040 = loc_list_from_tree (TREE_OPERAND (loc, 2), 0, context);
15041 dw_loc_descr_ref bra_node, jump_node, tmp;
15043 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
15044 if (list_ret == 0 || lhs == 0 || rhs == 0)
15045 return 0;
15047 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15048 add_loc_descr_to_each (list_ret, bra_node);
15050 add_loc_list (&list_ret, rhs);
15051 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
15052 add_loc_descr_to_each (list_ret, jump_node);
15054 add_loc_descr_to_each (list_ret, lhs);
15055 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15056 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
15058 /* ??? Need a node to point the skip at. Use a nop. */
15059 tmp = new_loc_descr (DW_OP_nop, 0, 0);
15060 add_loc_descr_to_each (list_ret, tmp);
15061 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15062 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
15064 break;
15066 case FIX_TRUNC_EXPR:
15067 return 0;
15069 default:
15070 /* Leave front-end specific codes as simply unknown. This comes
15071 up, for instance, with the C STMT_EXPR. */
15072 if ((unsigned int) TREE_CODE (loc)
15073 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
15075 expansion_failed (loc, NULL_RTX,
15076 "language specific tree node");
15077 return 0;
15080 #ifdef ENABLE_CHECKING
15081 /* Otherwise this is a generic code; we should just lists all of
15082 these explicitly. We forgot one. */
15083 gcc_unreachable ();
15084 #else
15085 /* In a release build, we want to degrade gracefully: better to
15086 generate incomplete debugging information than to crash. */
15087 return NULL;
15088 #endif
15091 if (!ret && !list_ret)
15092 return 0;
15094 if (want_address == 2 && !have_address
15095 && (dwarf_version >= 4 || !dwarf_strict))
15097 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
15099 expansion_failed (loc, NULL_RTX,
15100 "DWARF address size mismatch");
15101 return 0;
15103 if (ret)
15104 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
15105 else
15106 add_loc_descr_to_each (list_ret,
15107 new_loc_descr (DW_OP_stack_value, 0, 0));
15108 have_address = 1;
15110 /* Show if we can't fill the request for an address. */
15111 if (want_address && !have_address)
15113 expansion_failed (loc, NULL_RTX,
15114 "Want address and only have value");
15115 return 0;
15118 gcc_assert (!ret || !list_ret);
15120 /* If we've got an address and don't want one, dereference. */
15121 if (!want_address && have_address)
15123 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
15125 if (size > DWARF2_ADDR_SIZE || size == -1)
15127 expansion_failed (loc, NULL_RTX,
15128 "DWARF address size mismatch");
15129 return 0;
15131 else if (size == DWARF2_ADDR_SIZE)
15132 op = DW_OP_deref;
15133 else
15134 op = DW_OP_deref_size;
15136 if (ret)
15137 add_loc_descr (&ret, new_loc_descr (op, size, 0));
15138 else
15139 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
15141 if (ret)
15142 list_ret = new_loc_list (ret, NULL, NULL, NULL);
15144 return list_ret;
15147 /* Same as above but return only single location expression. */
15148 static dw_loc_descr_ref
15149 loc_descriptor_from_tree (tree loc, int want_address,
15150 const struct loc_descr_context *context)
15152 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
15153 if (!ret)
15154 return NULL;
15155 if (ret->dw_loc_next)
15157 expansion_failed (loc, NULL_RTX,
15158 "Location list where only loc descriptor needed");
15159 return NULL;
15161 return ret->expr;
15164 /* Given a value, round it up to the lowest multiple of `boundary'
15165 which is not less than the value itself. */
15167 static inline HOST_WIDE_INT
15168 ceiling (HOST_WIDE_INT value, unsigned int boundary)
15170 return (((value + boundary - 1) / boundary) * boundary);
15173 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
15174 pointer to the declared type for the relevant field variable, or return
15175 `integer_type_node' if the given node turns out to be an
15176 ERROR_MARK node. */
15178 static inline tree
15179 field_type (const_tree decl)
15181 tree type;
15183 if (TREE_CODE (decl) == ERROR_MARK)
15184 return integer_type_node;
15186 type = DECL_BIT_FIELD_TYPE (decl);
15187 if (type == NULL_TREE)
15188 type = TREE_TYPE (decl);
15190 return type;
15193 /* Given a pointer to a tree node, return the alignment in bits for
15194 it, or else return BITS_PER_WORD if the node actually turns out to
15195 be an ERROR_MARK node. */
15197 static inline unsigned
15198 simple_type_align_in_bits (const_tree type)
15200 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
15203 static inline unsigned
15204 simple_decl_align_in_bits (const_tree decl)
15206 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
15209 /* Return the result of rounding T up to ALIGN. */
15211 static inline offset_int
15212 round_up_to_align (const offset_int &t, unsigned int align)
15214 return wi::udiv_trunc (t + align - 1, align) * align;
15217 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
15218 lowest addressed byte of the "containing object" for the given FIELD_DECL,
15219 or return 0 if we are unable to determine what that offset is, either
15220 because the argument turns out to be a pointer to an ERROR_MARK node, or
15221 because the offset is actually variable. (We can't handle the latter case
15222 just yet). */
15224 static HOST_WIDE_INT
15225 field_byte_offset (const_tree decl)
15227 offset_int object_offset_in_bits;
15228 offset_int object_offset_in_bytes;
15229 offset_int bitpos_int;
15231 if (TREE_CODE (decl) == ERROR_MARK)
15232 return 0;
15234 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
15236 /* We cannot yet cope with fields whose positions are variable, so
15237 for now, when we see such things, we simply return 0. Someday, we may
15238 be able to handle such cases, but it will be damn difficult. */
15239 if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
15240 return 0;
15242 bitpos_int = wi::to_offset (bit_position (decl));
15244 if (PCC_BITFIELD_TYPE_MATTERS)
15246 tree type;
15247 tree field_size_tree;
15248 offset_int deepest_bitpos;
15249 offset_int field_size_in_bits;
15250 unsigned int type_align_in_bits;
15251 unsigned int decl_align_in_bits;
15252 offset_int type_size_in_bits;
15254 type = field_type (decl);
15255 type_size_in_bits = offset_int_type_size_in_bits (type);
15256 type_align_in_bits = simple_type_align_in_bits (type);
15258 field_size_tree = DECL_SIZE (decl);
15260 /* The size could be unspecified if there was an error, or for
15261 a flexible array member. */
15262 if (!field_size_tree)
15263 field_size_tree = bitsize_zero_node;
15265 /* If the size of the field is not constant, use the type size. */
15266 if (TREE_CODE (field_size_tree) == INTEGER_CST)
15267 field_size_in_bits = wi::to_offset (field_size_tree);
15268 else
15269 field_size_in_bits = type_size_in_bits;
15271 decl_align_in_bits = simple_decl_align_in_bits (decl);
15273 /* The GCC front-end doesn't make any attempt to keep track of the
15274 starting bit offset (relative to the start of the containing
15275 structure type) of the hypothetical "containing object" for a
15276 bit-field. Thus, when computing the byte offset value for the
15277 start of the "containing object" of a bit-field, we must deduce
15278 this information on our own. This can be rather tricky to do in
15279 some cases. For example, handling the following structure type
15280 definition when compiling for an i386/i486 target (which only
15281 aligns long long's to 32-bit boundaries) can be very tricky:
15283 struct S { int field1; long long field2:31; };
15285 Fortunately, there is a simple rule-of-thumb which can be used
15286 in such cases. When compiling for an i386/i486, GCC will
15287 allocate 8 bytes for the structure shown above. It decides to
15288 do this based upon one simple rule for bit-field allocation.
15289 GCC allocates each "containing object" for each bit-field at
15290 the first (i.e. lowest addressed) legitimate alignment boundary
15291 (based upon the required minimum alignment for the declared
15292 type of the field) which it can possibly use, subject to the
15293 condition that there is still enough available space remaining
15294 in the containing object (when allocated at the selected point)
15295 to fully accommodate all of the bits of the bit-field itself.
15297 This simple rule makes it obvious why GCC allocates 8 bytes for
15298 each object of the structure type shown above. When looking
15299 for a place to allocate the "containing object" for `field2',
15300 the compiler simply tries to allocate a 64-bit "containing
15301 object" at each successive 32-bit boundary (starting at zero)
15302 until it finds a place to allocate that 64- bit field such that
15303 at least 31 contiguous (and previously unallocated) bits remain
15304 within that selected 64 bit field. (As it turns out, for the
15305 example above, the compiler finds it is OK to allocate the
15306 "containing object" 64-bit field at bit-offset zero within the
15307 structure type.)
15309 Here we attempt to work backwards from the limited set of facts
15310 we're given, and we try to deduce from those facts, where GCC
15311 must have believed that the containing object started (within
15312 the structure type). The value we deduce is then used (by the
15313 callers of this routine) to generate DW_AT_location and
15314 DW_AT_bit_offset attributes for fields (both bit-fields and, in
15315 the case of DW_AT_location, regular fields as well). */
15317 /* Figure out the bit-distance from the start of the structure to
15318 the "deepest" bit of the bit-field. */
15319 deepest_bitpos = bitpos_int + field_size_in_bits;
15321 /* This is the tricky part. Use some fancy footwork to deduce
15322 where the lowest addressed bit of the containing object must
15323 be. */
15324 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
15326 /* Round up to type_align by default. This works best for
15327 bitfields. */
15328 object_offset_in_bits
15329 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
15331 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
15333 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
15335 /* Round up to decl_align instead. */
15336 object_offset_in_bits
15337 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
15340 else
15341 object_offset_in_bits = bitpos_int;
15343 object_offset_in_bytes
15344 = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
15345 return object_offset_in_bytes.to_shwi ();
15348 /* The following routines define various Dwarf attributes and any data
15349 associated with them. */
15351 /* Add a location description attribute value to a DIE.
15353 This emits location attributes suitable for whole variables and
15354 whole parameters. Note that the location attributes for struct fields are
15355 generated by the routine `data_member_location_attribute' below. */
15357 static inline void
15358 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
15359 dw_loc_list_ref descr)
15361 if (descr == 0)
15362 return;
15363 if (single_element_loc_list_p (descr))
15364 add_AT_loc (die, attr_kind, descr->expr);
15365 else
15366 add_AT_loc_list (die, attr_kind, descr);
15369 /* Add DW_AT_accessibility attribute to DIE if needed. */
15371 static void
15372 add_accessibility_attribute (dw_die_ref die, tree decl)
15374 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
15375 children, otherwise the default is DW_ACCESS_public. In DWARF2
15376 the default has always been DW_ACCESS_public. */
15377 if (TREE_PROTECTED (decl))
15378 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
15379 else if (TREE_PRIVATE (decl))
15381 if (dwarf_version == 2
15382 || die->die_parent == NULL
15383 || die->die_parent->die_tag != DW_TAG_class_type)
15384 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
15386 else if (dwarf_version > 2
15387 && die->die_parent
15388 && die->die_parent->die_tag == DW_TAG_class_type)
15389 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
15392 /* Attach the specialized form of location attribute used for data members of
15393 struct and union types. In the special case of a FIELD_DECL node which
15394 represents a bit-field, the "offset" part of this special location
15395 descriptor must indicate the distance in bytes from the lowest-addressed
15396 byte of the containing struct or union type to the lowest-addressed byte of
15397 the "containing object" for the bit-field. (See the `field_byte_offset'
15398 function above).
15400 For any given bit-field, the "containing object" is a hypothetical object
15401 (of some integral or enum type) within which the given bit-field lives. The
15402 type of this hypothetical "containing object" is always the same as the
15403 declared type of the individual bit-field itself (for GCC anyway... the
15404 DWARF spec doesn't actually mandate this). Note that it is the size (in
15405 bytes) of the hypothetical "containing object" which will be given in the
15406 DW_AT_byte_size attribute for this bit-field. (See the
15407 `byte_size_attribute' function below.) It is also used when calculating the
15408 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
15409 function below.) */
15411 static void
15412 add_data_member_location_attribute (dw_die_ref die, tree decl)
15414 HOST_WIDE_INT offset;
15415 dw_loc_descr_ref loc_descr = 0;
15417 if (TREE_CODE (decl) == TREE_BINFO)
15419 /* We're working on the TAG_inheritance for a base class. */
15420 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
15422 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
15423 aren't at a fixed offset from all (sub)objects of the same
15424 type. We need to extract the appropriate offset from our
15425 vtable. The following dwarf expression means
15427 BaseAddr = ObAddr + *((*ObAddr) - Offset)
15429 This is specific to the V3 ABI, of course. */
15431 dw_loc_descr_ref tmp;
15433 /* Make a copy of the object address. */
15434 tmp = new_loc_descr (DW_OP_dup, 0, 0);
15435 add_loc_descr (&loc_descr, tmp);
15437 /* Extract the vtable address. */
15438 tmp = new_loc_descr (DW_OP_deref, 0, 0);
15439 add_loc_descr (&loc_descr, tmp);
15441 /* Calculate the address of the offset. */
15442 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
15443 gcc_assert (offset < 0);
15445 tmp = int_loc_descriptor (-offset);
15446 add_loc_descr (&loc_descr, tmp);
15447 tmp = new_loc_descr (DW_OP_minus, 0, 0);
15448 add_loc_descr (&loc_descr, tmp);
15450 /* Extract the offset. */
15451 tmp = new_loc_descr (DW_OP_deref, 0, 0);
15452 add_loc_descr (&loc_descr, tmp);
15454 /* Add it to the object address. */
15455 tmp = new_loc_descr (DW_OP_plus, 0, 0);
15456 add_loc_descr (&loc_descr, tmp);
15458 else
15459 offset = tree_to_shwi (BINFO_OFFSET (decl));
15461 else
15462 offset = field_byte_offset (decl);
15464 if (! loc_descr)
15466 if (dwarf_version > 2)
15468 /* Don't need to output a location expression, just the constant. */
15469 if (offset < 0)
15470 add_AT_int (die, DW_AT_data_member_location, offset);
15471 else
15472 add_AT_unsigned (die, DW_AT_data_member_location, offset);
15473 return;
15475 else
15477 enum dwarf_location_atom op;
15479 /* The DWARF2 standard says that we should assume that the structure
15480 address is already on the stack, so we can specify a structure
15481 field address by using DW_OP_plus_uconst. */
15482 op = DW_OP_plus_uconst;
15483 loc_descr = new_loc_descr (op, offset, 0);
15487 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
15490 /* Writes integer values to dw_vec_const array. */
15492 static void
15493 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
15495 while (size != 0)
15497 *dest++ = val & 0xff;
15498 val >>= 8;
15499 --size;
15503 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
15505 static HOST_WIDE_INT
15506 extract_int (const unsigned char *src, unsigned int size)
15508 HOST_WIDE_INT val = 0;
15510 src += size;
15511 while (size != 0)
15513 val <<= 8;
15514 val |= *--src & 0xff;
15515 --size;
15517 return val;
15520 /* Writes wide_int values to dw_vec_const array. */
15522 static void
15523 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
15525 int i;
15527 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
15529 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
15530 return;
15533 /* We'd have to extend this code to support odd sizes. */
15534 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
15536 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
15538 if (WORDS_BIG_ENDIAN)
15539 for (i = n - 1; i >= 0; i--)
15541 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
15542 dest += sizeof (HOST_WIDE_INT);
15544 else
15545 for (i = 0; i < n; i++)
15547 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
15548 dest += sizeof (HOST_WIDE_INT);
15552 /* Writes floating point values to dw_vec_const array. */
15554 static void
15555 insert_float (const_rtx rtl, unsigned char *array)
15557 REAL_VALUE_TYPE rv;
15558 long val[4];
15559 int i;
15561 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
15562 real_to_target (val, &rv, GET_MODE (rtl));
15564 /* real_to_target puts 32-bit pieces in each long. Pack them. */
15565 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
15567 insert_int (val[i], 4, array);
15568 array += 4;
15572 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
15573 does not have a "location" either in memory or in a register. These
15574 things can arise in GNU C when a constant is passed as an actual parameter
15575 to an inlined function. They can also arise in C++ where declared
15576 constants do not necessarily get memory "homes". */
15578 static bool
15579 add_const_value_attribute (dw_die_ref die, rtx rtl)
15581 switch (GET_CODE (rtl))
15583 case CONST_INT:
15585 HOST_WIDE_INT val = INTVAL (rtl);
15587 if (val < 0)
15588 add_AT_int (die, DW_AT_const_value, val);
15589 else
15590 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
15592 return true;
15594 case CONST_WIDE_INT:
15595 add_AT_wide (die, DW_AT_const_value,
15596 std::make_pair (rtl, GET_MODE (rtl)));
15597 return true;
15599 case CONST_DOUBLE:
15600 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
15601 floating-point constant. A CONST_DOUBLE is used whenever the
15602 constant requires more than one word in order to be adequately
15603 represented. */
15605 machine_mode mode = GET_MODE (rtl);
15607 if (TARGET_SUPPORTS_WIDE_INT == 0 && !SCALAR_FLOAT_MODE_P (mode))
15608 add_AT_double (die, DW_AT_const_value,
15609 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
15610 else
15612 unsigned int length = GET_MODE_SIZE (mode);
15613 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15615 insert_float (rtl, array);
15616 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
15619 return true;
15621 case CONST_VECTOR:
15623 machine_mode mode = GET_MODE (rtl);
15624 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
15625 unsigned int length = CONST_VECTOR_NUNITS (rtl);
15626 unsigned char *array
15627 = ggc_vec_alloc<unsigned char> (length * elt_size);
15628 unsigned int i;
15629 unsigned char *p;
15630 machine_mode imode = GET_MODE_INNER (mode);
15632 switch (GET_MODE_CLASS (mode))
15634 case MODE_VECTOR_INT:
15635 for (i = 0, p = array; i < length; i++, p += elt_size)
15637 rtx elt = CONST_VECTOR_ELT (rtl, i);
15638 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
15640 break;
15642 case MODE_VECTOR_FLOAT:
15643 for (i = 0, p = array; i < length; i++, p += elt_size)
15645 rtx elt = CONST_VECTOR_ELT (rtl, i);
15646 insert_float (elt, p);
15648 break;
15650 default:
15651 gcc_unreachable ();
15654 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
15656 return true;
15658 case CONST_STRING:
15659 if (dwarf_version >= 4 || !dwarf_strict)
15661 dw_loc_descr_ref loc_result;
15662 resolve_one_addr (&rtl);
15663 rtl_addr:
15664 loc_result = new_addr_loc_descr (rtl, dtprel_false);
15665 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
15666 add_AT_loc (die, DW_AT_location, loc_result);
15667 vec_safe_push (used_rtx_array, rtl);
15668 return true;
15670 return false;
15672 case CONST:
15673 if (CONSTANT_P (XEXP (rtl, 0)))
15674 return add_const_value_attribute (die, XEXP (rtl, 0));
15675 /* FALLTHROUGH */
15676 case SYMBOL_REF:
15677 if (!const_ok_for_output (rtl))
15678 return false;
15679 case LABEL_REF:
15680 if (dwarf_version >= 4 || !dwarf_strict)
15681 goto rtl_addr;
15682 return false;
15684 case PLUS:
15685 /* In cases where an inlined instance of an inline function is passed
15686 the address of an `auto' variable (which is local to the caller) we
15687 can get a situation where the DECL_RTL of the artificial local
15688 variable (for the inlining) which acts as a stand-in for the
15689 corresponding formal parameter (of the inline function) will look
15690 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
15691 exactly a compile-time constant expression, but it isn't the address
15692 of the (artificial) local variable either. Rather, it represents the
15693 *value* which the artificial local variable always has during its
15694 lifetime. We currently have no way to represent such quasi-constant
15695 values in Dwarf, so for now we just punt and generate nothing. */
15696 return false;
15698 case HIGH:
15699 case CONST_FIXED:
15700 return false;
15702 case MEM:
15703 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
15704 && MEM_READONLY_P (rtl)
15705 && GET_MODE (rtl) == BLKmode)
15707 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
15708 return true;
15710 return false;
15712 default:
15713 /* No other kinds of rtx should be possible here. */
15714 gcc_unreachable ();
15716 return false;
15719 /* Determine whether the evaluation of EXPR references any variables
15720 or functions which aren't otherwise used (and therefore may not be
15721 output). */
15722 static tree
15723 reference_to_unused (tree * tp, int * walk_subtrees,
15724 void * data ATTRIBUTE_UNUSED)
15726 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
15727 *walk_subtrees = 0;
15729 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
15730 && ! TREE_ASM_WRITTEN (*tp))
15731 return *tp;
15732 /* ??? The C++ FE emits debug information for using decls, so
15733 putting gcc_unreachable here falls over. See PR31899. For now
15734 be conservative. */
15735 else if (!symtab->global_info_ready
15736 && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
15737 return *tp;
15738 else if (TREE_CODE (*tp) == VAR_DECL)
15740 varpool_node *node = varpool_node::get (*tp);
15741 if (!node || !node->definition)
15742 return *tp;
15744 else if (TREE_CODE (*tp) == FUNCTION_DECL
15745 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
15747 /* The call graph machinery must have finished analyzing,
15748 optimizing and gimplifying the CU by now.
15749 So if *TP has no call graph node associated
15750 to it, it means *TP will not be emitted. */
15751 if (!cgraph_node::get (*tp))
15752 return *tp;
15754 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
15755 return *tp;
15757 return NULL_TREE;
15760 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
15761 for use in a later add_const_value_attribute call. */
15763 static rtx
15764 rtl_for_decl_init (tree init, tree type)
15766 rtx rtl = NULL_RTX;
15768 STRIP_NOPS (init);
15770 /* If a variable is initialized with a string constant without embedded
15771 zeros, build CONST_STRING. */
15772 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
15774 tree enttype = TREE_TYPE (type);
15775 tree domain = TYPE_DOMAIN (type);
15776 machine_mode mode = TYPE_MODE (enttype);
15778 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
15779 && domain
15780 && integer_zerop (TYPE_MIN_VALUE (domain))
15781 && compare_tree_int (TYPE_MAX_VALUE (domain),
15782 TREE_STRING_LENGTH (init) - 1) == 0
15783 && ((size_t) TREE_STRING_LENGTH (init)
15784 == strlen (TREE_STRING_POINTER (init)) + 1))
15786 rtl = gen_rtx_CONST_STRING (VOIDmode,
15787 ggc_strdup (TREE_STRING_POINTER (init)));
15788 rtl = gen_rtx_MEM (BLKmode, rtl);
15789 MEM_READONLY_P (rtl) = 1;
15792 /* Other aggregates, and complex values, could be represented using
15793 CONCAT: FIXME! */
15794 else if (AGGREGATE_TYPE_P (type)
15795 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
15796 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
15797 || TREE_CODE (type) == COMPLEX_TYPE)
15799 /* Vectors only work if their mode is supported by the target.
15800 FIXME: generic vectors ought to work too. */
15801 else if (TREE_CODE (type) == VECTOR_TYPE
15802 && !VECTOR_MODE_P (TYPE_MODE (type)))
15804 /* If the initializer is something that we know will expand into an
15805 immediate RTL constant, expand it now. We must be careful not to
15806 reference variables which won't be output. */
15807 else if (initializer_constant_valid_p (init, type)
15808 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
15810 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
15811 possible. */
15812 if (TREE_CODE (type) == VECTOR_TYPE)
15813 switch (TREE_CODE (init))
15815 case VECTOR_CST:
15816 break;
15817 case CONSTRUCTOR:
15818 if (TREE_CONSTANT (init))
15820 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
15821 bool constant_p = true;
15822 tree value;
15823 unsigned HOST_WIDE_INT ix;
15825 /* Even when ctor is constant, it might contain non-*_CST
15826 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
15827 belong into VECTOR_CST nodes. */
15828 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
15829 if (!CONSTANT_CLASS_P (value))
15831 constant_p = false;
15832 break;
15835 if (constant_p)
15837 init = build_vector_from_ctor (type, elts);
15838 break;
15841 /* FALLTHRU */
15843 default:
15844 return NULL;
15847 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
15849 /* If expand_expr returns a MEM, it wasn't immediate. */
15850 gcc_assert (!rtl || !MEM_P (rtl));
15853 return rtl;
15856 /* Generate RTL for the variable DECL to represent its location. */
15858 static rtx
15859 rtl_for_decl_location (tree decl)
15861 rtx rtl;
15863 /* Here we have to decide where we are going to say the parameter "lives"
15864 (as far as the debugger is concerned). We only have a couple of
15865 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
15867 DECL_RTL normally indicates where the parameter lives during most of the
15868 activation of the function. If optimization is enabled however, this
15869 could be either NULL or else a pseudo-reg. Both of those cases indicate
15870 that the parameter doesn't really live anywhere (as far as the code
15871 generation parts of GCC are concerned) during most of the function's
15872 activation. That will happen (for example) if the parameter is never
15873 referenced within the function.
15875 We could just generate a location descriptor here for all non-NULL
15876 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
15877 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
15878 where DECL_RTL is NULL or is a pseudo-reg.
15880 Note however that we can only get away with using DECL_INCOMING_RTL as
15881 a backup substitute for DECL_RTL in certain limited cases. In cases
15882 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
15883 we can be sure that the parameter was passed using the same type as it is
15884 declared to have within the function, and that its DECL_INCOMING_RTL
15885 points us to a place where a value of that type is passed.
15887 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
15888 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
15889 because in these cases DECL_INCOMING_RTL points us to a value of some
15890 type which is *different* from the type of the parameter itself. Thus,
15891 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
15892 such cases, the debugger would end up (for example) trying to fetch a
15893 `float' from a place which actually contains the first part of a
15894 `double'. That would lead to really incorrect and confusing
15895 output at debug-time.
15897 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
15898 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
15899 are a couple of exceptions however. On little-endian machines we can
15900 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
15901 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
15902 an integral type that is smaller than TREE_TYPE (decl). These cases arise
15903 when (on a little-endian machine) a non-prototyped function has a
15904 parameter declared to be of type `short' or `char'. In such cases,
15905 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
15906 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
15907 passed `int' value. If the debugger then uses that address to fetch
15908 a `short' or a `char' (on a little-endian machine) the result will be
15909 the correct data, so we allow for such exceptional cases below.
15911 Note that our goal here is to describe the place where the given formal
15912 parameter lives during most of the function's activation (i.e. between the
15913 end of the prologue and the start of the epilogue). We'll do that as best
15914 as we can. Note however that if the given formal parameter is modified
15915 sometime during the execution of the function, then a stack backtrace (at
15916 debug-time) will show the function as having been called with the *new*
15917 value rather than the value which was originally passed in. This happens
15918 rarely enough that it is not a major problem, but it *is* a problem, and
15919 I'd like to fix it.
15921 A future version of dwarf2out.c may generate two additional attributes for
15922 any given DW_TAG_formal_parameter DIE which will describe the "passed
15923 type" and the "passed location" for the given formal parameter in addition
15924 to the attributes we now generate to indicate the "declared type" and the
15925 "active location" for each parameter. This additional set of attributes
15926 could be used by debuggers for stack backtraces. Separately, note that
15927 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
15928 This happens (for example) for inlined-instances of inline function formal
15929 parameters which are never referenced. This really shouldn't be
15930 happening. All PARM_DECL nodes should get valid non-NULL
15931 DECL_INCOMING_RTL values. FIXME. */
15933 /* Use DECL_RTL as the "location" unless we find something better. */
15934 rtl = DECL_RTL_IF_SET (decl);
15936 /* When generating abstract instances, ignore everything except
15937 constants, symbols living in memory, and symbols living in
15938 fixed registers. */
15939 if (! reload_completed)
15941 if (rtl
15942 && (CONSTANT_P (rtl)
15943 || (MEM_P (rtl)
15944 && CONSTANT_P (XEXP (rtl, 0)))
15945 || (REG_P (rtl)
15946 && TREE_CODE (decl) == VAR_DECL
15947 && TREE_STATIC (decl))))
15949 rtl = targetm.delegitimize_address (rtl);
15950 return rtl;
15952 rtl = NULL_RTX;
15954 else if (TREE_CODE (decl) == PARM_DECL)
15956 if (rtl == NULL_RTX
15957 || is_pseudo_reg (rtl)
15958 || (MEM_P (rtl)
15959 && is_pseudo_reg (XEXP (rtl, 0))
15960 && DECL_INCOMING_RTL (decl)
15961 && MEM_P (DECL_INCOMING_RTL (decl))
15962 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
15964 tree declared_type = TREE_TYPE (decl);
15965 tree passed_type = DECL_ARG_TYPE (decl);
15966 machine_mode dmode = TYPE_MODE (declared_type);
15967 machine_mode pmode = TYPE_MODE (passed_type);
15969 /* This decl represents a formal parameter which was optimized out.
15970 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
15971 all cases where (rtl == NULL_RTX) just below. */
15972 if (dmode == pmode)
15973 rtl = DECL_INCOMING_RTL (decl);
15974 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
15975 && SCALAR_INT_MODE_P (dmode)
15976 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
15977 && DECL_INCOMING_RTL (decl))
15979 rtx inc = DECL_INCOMING_RTL (decl);
15980 if (REG_P (inc))
15981 rtl = inc;
15982 else if (MEM_P (inc))
15984 if (BYTES_BIG_ENDIAN)
15985 rtl = adjust_address_nv (inc, dmode,
15986 GET_MODE_SIZE (pmode)
15987 - GET_MODE_SIZE (dmode));
15988 else
15989 rtl = inc;
15994 /* If the parm was passed in registers, but lives on the stack, then
15995 make a big endian correction if the mode of the type of the
15996 parameter is not the same as the mode of the rtl. */
15997 /* ??? This is the same series of checks that are made in dbxout.c before
15998 we reach the big endian correction code there. It isn't clear if all
15999 of these checks are necessary here, but keeping them all is the safe
16000 thing to do. */
16001 else if (MEM_P (rtl)
16002 && XEXP (rtl, 0) != const0_rtx
16003 && ! CONSTANT_P (XEXP (rtl, 0))
16004 /* Not passed in memory. */
16005 && !MEM_P (DECL_INCOMING_RTL (decl))
16006 /* Not passed by invisible reference. */
16007 && (!REG_P (XEXP (rtl, 0))
16008 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
16009 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
16010 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
16011 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
16012 #endif
16014 /* Big endian correction check. */
16015 && BYTES_BIG_ENDIAN
16016 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
16017 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
16018 < UNITS_PER_WORD))
16020 machine_mode addr_mode = get_address_mode (rtl);
16021 int offset = (UNITS_PER_WORD
16022 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
16024 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
16025 plus_constant (addr_mode, XEXP (rtl, 0), offset));
16028 else if (TREE_CODE (decl) == VAR_DECL
16029 && rtl
16030 && MEM_P (rtl)
16031 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
16032 && BYTES_BIG_ENDIAN)
16034 machine_mode addr_mode = get_address_mode (rtl);
16035 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
16036 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
16038 /* If a variable is declared "register" yet is smaller than
16039 a register, then if we store the variable to memory, it
16040 looks like we're storing a register-sized value, when in
16041 fact we are not. We need to adjust the offset of the
16042 storage location to reflect the actual value's bytes,
16043 else gdb will not be able to display it. */
16044 if (rsize > dsize)
16045 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
16046 plus_constant (addr_mode, XEXP (rtl, 0),
16047 rsize - dsize));
16050 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
16051 and will have been substituted directly into all expressions that use it.
16052 C does not have such a concept, but C++ and other languages do. */
16053 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
16054 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
16056 if (rtl)
16057 rtl = targetm.delegitimize_address (rtl);
16059 /* If we don't look past the constant pool, we risk emitting a
16060 reference to a constant pool entry that isn't referenced from
16061 code, and thus is not emitted. */
16062 if (rtl)
16063 rtl = avoid_constant_pool_reference (rtl);
16065 /* Try harder to get a rtl. If this symbol ends up not being emitted
16066 in the current CU, resolve_addr will remove the expression referencing
16067 it. */
16068 if (rtl == NULL_RTX
16069 && TREE_CODE (decl) == VAR_DECL
16070 && !DECL_EXTERNAL (decl)
16071 && TREE_STATIC (decl)
16072 && DECL_NAME (decl)
16073 && !DECL_HARD_REGISTER (decl)
16074 && DECL_MODE (decl) != VOIDmode)
16076 rtl = make_decl_rtl_for_debug (decl);
16077 if (!MEM_P (rtl)
16078 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
16079 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
16080 rtl = NULL_RTX;
16083 return rtl;
16086 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
16087 returned. If so, the decl for the COMMON block is returned, and the
16088 value is the offset into the common block for the symbol. */
16090 static tree
16091 fortran_common (tree decl, HOST_WIDE_INT *value)
16093 tree val_expr, cvar;
16094 machine_mode mode;
16095 HOST_WIDE_INT bitsize, bitpos;
16096 tree offset;
16097 int unsignedp, volatilep = 0;
16099 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
16100 it does not have a value (the offset into the common area), or if it
16101 is thread local (as opposed to global) then it isn't common, and shouldn't
16102 be handled as such. */
16103 if (TREE_CODE (decl) != VAR_DECL
16104 || !TREE_STATIC (decl)
16105 || !DECL_HAS_VALUE_EXPR_P (decl)
16106 || !is_fortran ())
16107 return NULL_TREE;
16109 val_expr = DECL_VALUE_EXPR (decl);
16110 if (TREE_CODE (val_expr) != COMPONENT_REF)
16111 return NULL_TREE;
16113 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
16114 &mode, &unsignedp, &volatilep, true);
16116 if (cvar == NULL_TREE
16117 || TREE_CODE (cvar) != VAR_DECL
16118 || DECL_ARTIFICIAL (cvar)
16119 || !TREE_PUBLIC (cvar))
16120 return NULL_TREE;
16122 *value = 0;
16123 if (offset != NULL)
16125 if (!tree_fits_shwi_p (offset))
16126 return NULL_TREE;
16127 *value = tree_to_shwi (offset);
16129 if (bitpos != 0)
16130 *value += bitpos / BITS_PER_UNIT;
16132 return cvar;
16135 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
16136 data attribute for a variable or a parameter. We generate the
16137 DW_AT_const_value attribute only in those cases where the given variable
16138 or parameter does not have a true "location" either in memory or in a
16139 register. This can happen (for example) when a constant is passed as an
16140 actual argument in a call to an inline function. (It's possible that
16141 these things can crop up in other ways also.) Note that one type of
16142 constant value which can be passed into an inlined function is a constant
16143 pointer. This can happen for example if an actual argument in an inlined
16144 function call evaluates to a compile-time constant address.
16146 CACHE_P is true if it is worth caching the location list for DECL,
16147 so that future calls can reuse it rather than regenerate it from scratch.
16148 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
16149 since we will need to refer to them each time the function is inlined. */
16151 static bool
16152 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
16153 enum dwarf_attribute attr)
16155 rtx rtl;
16156 dw_loc_list_ref list;
16157 var_loc_list *loc_list;
16158 cached_dw_loc_list *cache;
16160 if (TREE_CODE (decl) == ERROR_MARK)
16161 return false;
16163 if (get_AT (die, attr))
16164 return true;
16166 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
16167 || TREE_CODE (decl) == RESULT_DECL);
16169 /* Try to get some constant RTL for this decl, and use that as the value of
16170 the location. */
16172 rtl = rtl_for_decl_location (decl);
16173 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16174 && add_const_value_attribute (die, rtl))
16175 return true;
16177 /* See if we have single element location list that is equivalent to
16178 a constant value. That way we are better to use add_const_value_attribute
16179 rather than expanding constant value equivalent. */
16180 loc_list = lookup_decl_loc (decl);
16181 if (loc_list
16182 && loc_list->first
16183 && loc_list->first->next == NULL
16184 && NOTE_P (loc_list->first->loc)
16185 && NOTE_VAR_LOCATION (loc_list->first->loc)
16186 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
16188 struct var_loc_node *node;
16190 node = loc_list->first;
16191 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
16192 if (GET_CODE (rtl) == EXPR_LIST)
16193 rtl = XEXP (rtl, 0);
16194 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16195 && add_const_value_attribute (die, rtl))
16196 return true;
16198 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
16199 list several times. See if we've already cached the contents. */
16200 list = NULL;
16201 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
16202 cache_p = false;
16203 if (cache_p)
16205 cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
16206 if (cache)
16207 list = cache->loc_list;
16209 if (list == NULL)
16211 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
16212 NULL);
16213 /* It is usually worth caching this result if the decl is from
16214 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
16215 if (cache_p && list && list->dw_loc_next)
16217 cached_dw_loc_list **slot
16218 = cached_dw_loc_list_table->find_slot_with_hash (decl,
16219 DECL_UID (decl),
16220 INSERT);
16221 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
16222 cache->decl_id = DECL_UID (decl);
16223 cache->loc_list = list;
16224 *slot = cache;
16227 if (list)
16229 add_AT_location_description (die, attr, list);
16230 return true;
16232 /* None of that worked, so it must not really have a location;
16233 try adding a constant value attribute from the DECL_INITIAL. */
16234 return tree_add_const_value_attribute_for_decl (die, decl);
16237 /* Helper function for tree_add_const_value_attribute. Natively encode
16238 initializer INIT into an array. Return true if successful. */
16240 static bool
16241 native_encode_initializer (tree init, unsigned char *array, int size)
16243 tree type;
16245 if (init == NULL_TREE)
16246 return false;
16248 STRIP_NOPS (init);
16249 switch (TREE_CODE (init))
16251 case STRING_CST:
16252 type = TREE_TYPE (init);
16253 if (TREE_CODE (type) == ARRAY_TYPE)
16255 tree enttype = TREE_TYPE (type);
16256 machine_mode mode = TYPE_MODE (enttype);
16258 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
16259 return false;
16260 if (int_size_in_bytes (type) != size)
16261 return false;
16262 if (size > TREE_STRING_LENGTH (init))
16264 memcpy (array, TREE_STRING_POINTER (init),
16265 TREE_STRING_LENGTH (init));
16266 memset (array + TREE_STRING_LENGTH (init),
16267 '\0', size - TREE_STRING_LENGTH (init));
16269 else
16270 memcpy (array, TREE_STRING_POINTER (init), size);
16271 return true;
16273 return false;
16274 case CONSTRUCTOR:
16275 type = TREE_TYPE (init);
16276 if (int_size_in_bytes (type) != size)
16277 return false;
16278 if (TREE_CODE (type) == ARRAY_TYPE)
16280 HOST_WIDE_INT min_index;
16281 unsigned HOST_WIDE_INT cnt;
16282 int curpos = 0, fieldsize;
16283 constructor_elt *ce;
16285 if (TYPE_DOMAIN (type) == NULL_TREE
16286 || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
16287 return false;
16289 fieldsize = int_size_in_bytes (TREE_TYPE (type));
16290 if (fieldsize <= 0)
16291 return false;
16293 min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
16294 memset (array, '\0', size);
16295 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
16297 tree val = ce->value;
16298 tree index = ce->index;
16299 int pos = curpos;
16300 if (index && TREE_CODE (index) == RANGE_EXPR)
16301 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
16302 * fieldsize;
16303 else if (index)
16304 pos = (tree_to_shwi (index) - min_index) * fieldsize;
16306 if (val)
16308 STRIP_NOPS (val);
16309 if (!native_encode_initializer (val, array + pos, fieldsize))
16310 return false;
16312 curpos = pos + fieldsize;
16313 if (index && TREE_CODE (index) == RANGE_EXPR)
16315 int count = tree_to_shwi (TREE_OPERAND (index, 1))
16316 - tree_to_shwi (TREE_OPERAND (index, 0));
16317 while (count-- > 0)
16319 if (val)
16320 memcpy (array + curpos, array + pos, fieldsize);
16321 curpos += fieldsize;
16324 gcc_assert (curpos <= size);
16326 return true;
16328 else if (TREE_CODE (type) == RECORD_TYPE
16329 || TREE_CODE (type) == UNION_TYPE)
16331 tree field = NULL_TREE;
16332 unsigned HOST_WIDE_INT cnt;
16333 constructor_elt *ce;
16335 if (int_size_in_bytes (type) != size)
16336 return false;
16338 if (TREE_CODE (type) == RECORD_TYPE)
16339 field = TYPE_FIELDS (type);
16341 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
16343 tree val = ce->value;
16344 int pos, fieldsize;
16346 if (ce->index != 0)
16347 field = ce->index;
16349 if (val)
16350 STRIP_NOPS (val);
16352 if (field == NULL_TREE || DECL_BIT_FIELD (field))
16353 return false;
16355 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
16356 && TYPE_DOMAIN (TREE_TYPE (field))
16357 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
16358 return false;
16359 else if (DECL_SIZE_UNIT (field) == NULL_TREE
16360 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
16361 return false;
16362 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
16363 pos = int_byte_position (field);
16364 gcc_assert (pos + fieldsize <= size);
16365 if (val
16366 && !native_encode_initializer (val, array + pos, fieldsize))
16367 return false;
16369 return true;
16371 return false;
16372 case VIEW_CONVERT_EXPR:
16373 case NON_LVALUE_EXPR:
16374 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
16375 default:
16376 return native_encode_expr (init, array, size) == size;
16380 /* Attach a DW_AT_const_value attribute to DIE. The value of the
16381 attribute is the const value T. */
16383 static bool
16384 tree_add_const_value_attribute (dw_die_ref die, tree t)
16386 tree init;
16387 tree type = TREE_TYPE (t);
16388 rtx rtl;
16390 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
16391 return false;
16393 init = t;
16394 gcc_assert (!DECL_P (init));
16396 rtl = rtl_for_decl_init (init, type);
16397 if (rtl)
16398 return add_const_value_attribute (die, rtl);
16399 /* If the host and target are sane, try harder. */
16400 else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
16401 && initializer_constant_valid_p (init, type))
16403 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
16404 if (size > 0 && (int) size == size)
16406 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
16408 if (native_encode_initializer (init, array, size))
16410 add_AT_vec (die, DW_AT_const_value, size, 1, array);
16411 return true;
16413 ggc_free (array);
16416 return false;
16419 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
16420 attribute is the const value of T, where T is an integral constant
16421 variable with static storage duration
16422 (so it can't be a PARM_DECL or a RESULT_DECL). */
16424 static bool
16425 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
16428 if (!decl
16429 || (TREE_CODE (decl) != VAR_DECL
16430 && TREE_CODE (decl) != CONST_DECL)
16431 || (TREE_CODE (decl) == VAR_DECL
16432 && !TREE_STATIC (decl)))
16433 return false;
16435 if (TREE_READONLY (decl)
16436 && ! TREE_THIS_VOLATILE (decl)
16437 && DECL_INITIAL (decl))
16438 /* OK */;
16439 else
16440 return false;
16442 /* Don't add DW_AT_const_value if abstract origin already has one. */
16443 if (get_AT (var_die, DW_AT_const_value))
16444 return false;
16446 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
16449 /* Convert the CFI instructions for the current function into a
16450 location list. This is used for DW_AT_frame_base when we targeting
16451 a dwarf2 consumer that does not support the dwarf3
16452 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
16453 expressions. */
16455 static dw_loc_list_ref
16456 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
16458 int ix;
16459 dw_fde_ref fde;
16460 dw_loc_list_ref list, *list_tail;
16461 dw_cfi_ref cfi;
16462 dw_cfa_location last_cfa, next_cfa;
16463 const char *start_label, *last_label, *section;
16464 dw_cfa_location remember;
16466 fde = cfun->fde;
16467 gcc_assert (fde != NULL);
16469 section = secname_for_decl (current_function_decl);
16470 list_tail = &list;
16471 list = NULL;
16473 memset (&next_cfa, 0, sizeof (next_cfa));
16474 next_cfa.reg = INVALID_REGNUM;
16475 remember = next_cfa;
16477 start_label = fde->dw_fde_begin;
16479 /* ??? Bald assumption that the CIE opcode list does not contain
16480 advance opcodes. */
16481 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
16482 lookup_cfa_1 (cfi, &next_cfa, &remember);
16484 last_cfa = next_cfa;
16485 last_label = start_label;
16487 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
16489 /* If the first partition contained no CFI adjustments, the
16490 CIE opcodes apply to the whole first partition. */
16491 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16492 fde->dw_fde_begin, fde->dw_fde_end, section);
16493 list_tail =&(*list_tail)->dw_loc_next;
16494 start_label = last_label = fde->dw_fde_second_begin;
16497 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
16499 switch (cfi->dw_cfi_opc)
16501 case DW_CFA_set_loc:
16502 case DW_CFA_advance_loc1:
16503 case DW_CFA_advance_loc2:
16504 case DW_CFA_advance_loc4:
16505 if (!cfa_equal_p (&last_cfa, &next_cfa))
16507 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16508 start_label, last_label, section);
16510 list_tail = &(*list_tail)->dw_loc_next;
16511 last_cfa = next_cfa;
16512 start_label = last_label;
16514 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
16515 break;
16517 case DW_CFA_advance_loc:
16518 /* The encoding is complex enough that we should never emit this. */
16519 gcc_unreachable ();
16521 default:
16522 lookup_cfa_1 (cfi, &next_cfa, &remember);
16523 break;
16525 if (ix + 1 == fde->dw_fde_switch_cfi_index)
16527 if (!cfa_equal_p (&last_cfa, &next_cfa))
16529 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16530 start_label, last_label, section);
16532 list_tail = &(*list_tail)->dw_loc_next;
16533 last_cfa = next_cfa;
16534 start_label = last_label;
16536 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16537 start_label, fde->dw_fde_end, section);
16538 list_tail = &(*list_tail)->dw_loc_next;
16539 start_label = last_label = fde->dw_fde_second_begin;
16543 if (!cfa_equal_p (&last_cfa, &next_cfa))
16545 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16546 start_label, last_label, section);
16547 list_tail = &(*list_tail)->dw_loc_next;
16548 start_label = last_label;
16551 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
16552 start_label,
16553 fde->dw_fde_second_begin
16554 ? fde->dw_fde_second_end : fde->dw_fde_end,
16555 section);
16557 if (list && list->dw_loc_next)
16558 gen_llsym (list);
16560 return list;
16563 /* Compute a displacement from the "steady-state frame pointer" to the
16564 frame base (often the same as the CFA), and store it in
16565 frame_pointer_fb_offset. OFFSET is added to the displacement
16566 before the latter is negated. */
16568 static void
16569 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
16571 rtx reg, elim;
16573 #ifdef FRAME_POINTER_CFA_OFFSET
16574 reg = frame_pointer_rtx;
16575 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
16576 #else
16577 reg = arg_pointer_rtx;
16578 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
16579 #endif
16581 elim = (ira_use_lra_p
16582 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
16583 : eliminate_regs (reg, VOIDmode, NULL_RTX));
16584 if (GET_CODE (elim) == PLUS)
16586 offset += INTVAL (XEXP (elim, 1));
16587 elim = XEXP (elim, 0);
16590 frame_pointer_fb_offset = -offset;
16592 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
16593 in which to eliminate. This is because it's stack pointer isn't
16594 directly accessible as a register within the ISA. To work around
16595 this, assume that while we cannot provide a proper value for
16596 frame_pointer_fb_offset, we won't need one either. */
16597 frame_pointer_fb_offset_valid
16598 = ((SUPPORTS_STACK_ALIGNMENT
16599 && (elim == hard_frame_pointer_rtx
16600 || elim == stack_pointer_rtx))
16601 || elim == (frame_pointer_needed
16602 ? hard_frame_pointer_rtx
16603 : stack_pointer_rtx));
16606 /* Generate a DW_AT_name attribute given some string value to be included as
16607 the value of the attribute. */
16609 static void
16610 add_name_attribute (dw_die_ref die, const char *name_string)
16612 if (name_string != NULL && *name_string != 0)
16614 if (demangle_name_func)
16615 name_string = (*demangle_name_func) (name_string);
16617 add_AT_string (die, DW_AT_name, name_string);
16621 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
16622 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
16623 of TYPE accordingly.
16625 ??? This is a temporary measure until after we're able to generate
16626 regular DWARF for the complex Ada type system. */
16628 static void
16629 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
16630 dw_die_ref context_die)
16632 tree dtype;
16633 dw_die_ref dtype_die;
16635 if (!lang_hooks.types.descriptive_type)
16636 return;
16638 dtype = lang_hooks.types.descriptive_type (type);
16639 if (!dtype)
16640 return;
16642 dtype_die = lookup_type_die (dtype);
16643 if (!dtype_die)
16645 gen_type_die (dtype, context_die);
16646 dtype_die = lookup_type_die (dtype);
16647 gcc_assert (dtype_die);
16650 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
16653 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
16655 static const char *
16656 comp_dir_string (void)
16658 const char *wd;
16659 char *wd1;
16660 static const char *cached_wd = NULL;
16662 if (cached_wd != NULL)
16663 return cached_wd;
16665 wd = get_src_pwd ();
16666 if (wd == NULL)
16667 return NULL;
16669 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
16671 int wdlen;
16673 wdlen = strlen (wd);
16674 wd1 = ggc_vec_alloc<char> (wdlen + 2);
16675 strcpy (wd1, wd);
16676 wd1 [wdlen] = DIR_SEPARATOR;
16677 wd1 [wdlen + 1] = 0;
16678 wd = wd1;
16681 cached_wd = remap_debug_filename (wd);
16682 return cached_wd;
16685 /* Generate a DW_AT_comp_dir attribute for DIE. */
16687 static void
16688 add_comp_dir_attribute (dw_die_ref die)
16690 const char * wd = comp_dir_string ();
16691 if (wd != NULL)
16692 add_AT_string (die, DW_AT_comp_dir, wd);
16695 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
16696 pointer computation, ...), output a representation for that bound according
16697 to the accepted FORMS (see enum dw_scalar_form) and add it to DIE. See
16698 loc_list_from_tree for the meaning of CONTEXT. */
16700 static void
16701 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
16702 int forms, const struct loc_descr_context *context)
16704 dw_die_ref ctx, decl_die;
16705 dw_loc_list_ref list;
16707 bool strip_conversions = true;
16709 while (strip_conversions)
16710 switch (TREE_CODE (value))
16712 case ERROR_MARK:
16713 case SAVE_EXPR:
16714 return;
16716 CASE_CONVERT:
16717 case VIEW_CONVERT_EXPR:
16718 value = TREE_OPERAND (value, 0);
16719 break;
16721 default:
16722 strip_conversions = false;
16723 break;
16726 /* If possible and permitted, output the attribute as a constant. */
16727 if ((forms & dw_scalar_form_constant) != 0
16728 && TREE_CODE (value) == INTEGER_CST)
16730 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
16732 /* If HOST_WIDE_INT is big enough then represent the bound as
16733 a constant value. We need to choose a form based on
16734 whether the type is signed or unsigned. We cannot just
16735 call add_AT_unsigned if the value itself is positive
16736 (add_AT_unsigned might add the unsigned value encoded as
16737 DW_FORM_data[1248]). Some DWARF consumers will lookup the
16738 bounds type and then sign extend any unsigned values found
16739 for signed types. This is needed only for
16740 DW_AT_{lower,upper}_bound, since for most other attributes,
16741 consumers will treat DW_FORM_data[1248] as unsigned values,
16742 regardless of the underlying type. */
16743 if (prec <= HOST_BITS_PER_WIDE_INT
16744 || tree_fits_uhwi_p (value))
16746 if (TYPE_UNSIGNED (TREE_TYPE (value)))
16747 add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
16748 else
16749 add_AT_int (die, attr, TREE_INT_CST_LOW (value));
16751 else
16752 /* Otherwise represent the bound as an unsigned value with
16753 the precision of its type. The precision and signedness
16754 of the type will be necessary to re-interpret it
16755 unambiguously. */
16756 add_AT_wide (die, attr, value);
16757 return;
16760 /* Otherwise, if it's possible and permitted too, output a reference to
16761 another DIE. */
16762 if ((forms & dw_scalar_form_reference) != 0)
16764 tree decl = NULL_TREE;
16766 /* Some type attributes reference an outer type. For instance, the upper
16767 bound of an array may reference an embedding record (this happens in
16768 Ada). */
16769 if (TREE_CODE (value) == COMPONENT_REF
16770 && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
16771 && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
16772 decl = TREE_OPERAND (value, 1);
16774 else if (TREE_CODE (value) == VAR_DECL
16775 || TREE_CODE (value) == PARM_DECL
16776 || TREE_CODE (value) == RESULT_DECL)
16777 decl = value;
16779 if (decl != NULL_TREE)
16781 dw_die_ref decl_die = lookup_decl_die (decl);
16783 /* ??? Can this happen, or should the variable have been bound
16784 first? Probably it can, since I imagine that we try to create
16785 the types of parameters in the order in which they exist in
16786 the list, and won't have created a forward reference to a
16787 later parameter. */
16788 if (decl_die != NULL)
16790 add_AT_die_ref (die, attr, decl_die);
16791 return;
16796 /* Last chance: try to create a stack operation procedure to evaluate the
16797 value. Do nothing if even that is not possible or permitted. */
16798 if ((forms & dw_scalar_form_exprloc) == 0)
16799 return;
16801 list = loc_list_from_tree (value, 2, context);
16802 if (list == NULL || single_element_loc_list_p (list))
16804 /* If this attribute is not a reference nor constant, it is
16805 a DWARF expression rather than location description. For that
16806 loc_list_from_tree (value, 0, &context) is needed. */
16807 dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
16808 if (list2 && single_element_loc_list_p (list2))
16810 add_AT_loc (die, attr, list2->expr);
16811 return;
16815 /* If that failed to give a single element location list, fall back to
16816 outputting this as a reference... still if permitted. */
16817 if (list == NULL || (forms & dw_scalar_form_reference) == 0)
16818 return;
16820 if (current_function_decl == 0)
16821 ctx = comp_unit_die ();
16822 else
16823 ctx = lookup_decl_die (current_function_decl);
16825 decl_die = new_die (DW_TAG_variable, ctx, value);
16826 add_AT_flag (decl_die, DW_AT_artificial, 1);
16827 add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, ctx);
16828 add_AT_location_description (decl_die, DW_AT_location, list);
16829 add_AT_die_ref (die, attr, decl_die);
16832 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
16833 default. */
16835 static int
16836 lower_bound_default (void)
16838 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
16840 case DW_LANG_C:
16841 case DW_LANG_C89:
16842 case DW_LANG_C99:
16843 case DW_LANG_C11:
16844 case DW_LANG_C_plus_plus:
16845 case DW_LANG_C_plus_plus_11:
16846 case DW_LANG_C_plus_plus_14:
16847 case DW_LANG_ObjC:
16848 case DW_LANG_ObjC_plus_plus:
16849 case DW_LANG_Java:
16850 return 0;
16851 case DW_LANG_Fortran77:
16852 case DW_LANG_Fortran90:
16853 case DW_LANG_Fortran95:
16854 case DW_LANG_Fortran03:
16855 case DW_LANG_Fortran08:
16856 return 1;
16857 case DW_LANG_UPC:
16858 case DW_LANG_D:
16859 case DW_LANG_Python:
16860 return dwarf_version >= 4 ? 0 : -1;
16861 case DW_LANG_Ada95:
16862 case DW_LANG_Ada83:
16863 case DW_LANG_Cobol74:
16864 case DW_LANG_Cobol85:
16865 case DW_LANG_Pascal83:
16866 case DW_LANG_Modula2:
16867 case DW_LANG_PLI:
16868 return dwarf_version >= 4 ? 1 : -1;
16869 default:
16870 return -1;
16874 /* Given a tree node describing an array bound (either lower or upper) output
16875 a representation for that bound. */
16877 static void
16878 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
16879 tree bound, const struct loc_descr_context *context)
16881 int dflt;
16883 while (1)
16884 switch (TREE_CODE (bound))
16886 /* Strip all conversions. */
16887 CASE_CONVERT:
16888 case VIEW_CONVERT_EXPR:
16889 bound = TREE_OPERAND (bound, 0);
16890 break;
16892 /* All fixed-bounds are represented by INTEGER_CST nodes. Lower bounds
16893 are even omitted when they are the default. */
16894 case INTEGER_CST:
16895 /* If the value for this bound is the default one, we can even omit the
16896 attribute. */
16897 if (bound_attr == DW_AT_lower_bound
16898 && tree_fits_shwi_p (bound)
16899 && (dflt = lower_bound_default ()) != -1
16900 && tree_to_shwi (bound) == dflt)
16901 return;
16903 /* FALLTHRU */
16905 default:
16906 add_scalar_info (subrange_die, bound_attr, bound,
16907 dw_scalar_form_constant
16908 | dw_scalar_form_exprloc
16909 | dw_scalar_form_reference,
16910 context);
16911 return;
16915 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
16916 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
16917 Note that the block of subscript information for an array type also
16918 includes information about the element type of the given array type.
16920 This function reuses previously set type and bound information if
16921 available. */
16923 static void
16924 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
16926 unsigned dimension_number;
16927 tree lower, upper;
16928 dw_die_ref child = type_die->die_child;
16930 for (dimension_number = 0;
16931 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
16932 type = TREE_TYPE (type), dimension_number++)
16934 tree domain = TYPE_DOMAIN (type);
16936 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
16937 break;
16939 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
16940 and (in GNU C only) variable bounds. Handle all three forms
16941 here. */
16943 /* Find and reuse a previously generated DW_TAG_subrange_type if
16944 available.
16946 For multi-dimensional arrays, as we iterate through the
16947 various dimensions in the enclosing for loop above, we also
16948 iterate through the DIE children and pick at each
16949 DW_TAG_subrange_type previously generated (if available).
16950 Each child DW_TAG_subrange_type DIE describes the range of
16951 the current dimension. At this point we should have as many
16952 DW_TAG_subrange_type's as we have dimensions in the
16953 array. */
16954 dw_die_ref subrange_die = NULL;
16955 if (child)
16956 while (1)
16958 child = child->die_sib;
16959 if (child->die_tag == DW_TAG_subrange_type)
16960 subrange_die = child;
16961 if (child == type_die->die_child)
16963 /* If we wrapped around, stop looking next time. */
16964 child = NULL;
16965 break;
16967 if (child->die_tag == DW_TAG_subrange_type)
16968 break;
16970 if (!subrange_die)
16971 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
16973 if (domain)
16975 /* We have an array type with specified bounds. */
16976 lower = TYPE_MIN_VALUE (domain);
16977 upper = TYPE_MAX_VALUE (domain);
16979 /* Define the index type. */
16980 if (TREE_TYPE (domain)
16981 && !get_AT (subrange_die, DW_AT_type))
16983 /* ??? This is probably an Ada unnamed subrange type. Ignore the
16984 TREE_TYPE field. We can't emit debug info for this
16985 because it is an unnamed integral type. */
16986 if (TREE_CODE (domain) == INTEGER_TYPE
16987 && TYPE_NAME (domain) == NULL_TREE
16988 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
16989 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
16991 else
16992 add_type_attribute (subrange_die, TREE_TYPE (domain),
16993 TYPE_UNQUALIFIED, type_die);
16996 /* ??? If upper is NULL, the array has unspecified length,
16997 but it does have a lower bound. This happens with Fortran
16998 dimension arr(N:*)
16999 Since the debugger is definitely going to need to know N
17000 to produce useful results, go ahead and output the lower
17001 bound solo, and hope the debugger can cope. */
17003 if (!get_AT (subrange_die, DW_AT_lower_bound))
17004 add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
17005 if (upper && !get_AT (subrange_die, DW_AT_upper_bound))
17006 add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
17009 /* Otherwise we have an array type with an unspecified length. The
17010 DWARF-2 spec does not say how to handle this; let's just leave out the
17011 bounds. */
17015 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
17017 static void
17018 add_byte_size_attribute (dw_die_ref die, tree tree_node)
17020 dw_die_ref decl_die;
17021 HOST_WIDE_INT size;
17023 switch (TREE_CODE (tree_node))
17025 case ERROR_MARK:
17026 size = 0;
17027 break;
17028 case ENUMERAL_TYPE:
17029 case RECORD_TYPE:
17030 case UNION_TYPE:
17031 case QUAL_UNION_TYPE:
17032 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
17033 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
17035 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
17036 return;
17038 size = int_size_in_bytes (tree_node);
17039 break;
17040 case FIELD_DECL:
17041 /* For a data member of a struct or union, the DW_AT_byte_size is
17042 generally given as the number of bytes normally allocated for an
17043 object of the *declared* type of the member itself. This is true
17044 even for bit-fields. */
17045 size = int_size_in_bytes (field_type (tree_node));
17046 break;
17047 default:
17048 gcc_unreachable ();
17051 /* Note that `size' might be -1 when we get to this point. If it is, that
17052 indicates that the byte size of the entity in question is variable. We
17053 have no good way of expressing this fact in Dwarf at the present time,
17054 when location description was not used by the caller code instead. */
17055 if (size >= 0)
17056 add_AT_unsigned (die, DW_AT_byte_size, size);
17059 /* For a FIELD_DECL node which represents a bit-field, output an attribute
17060 which specifies the distance in bits from the highest order bit of the
17061 "containing object" for the bit-field to the highest order bit of the
17062 bit-field itself.
17064 For any given bit-field, the "containing object" is a hypothetical object
17065 (of some integral or enum type) within which the given bit-field lives. The
17066 type of this hypothetical "containing object" is always the same as the
17067 declared type of the individual bit-field itself. The determination of the
17068 exact location of the "containing object" for a bit-field is rather
17069 complicated. It's handled by the `field_byte_offset' function (above).
17071 Note that it is the size (in bytes) of the hypothetical "containing object"
17072 which will be given in the DW_AT_byte_size attribute for this bit-field.
17073 (See `byte_size_attribute' above). */
17075 static inline void
17076 add_bit_offset_attribute (dw_die_ref die, tree decl)
17078 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
17079 tree type = DECL_BIT_FIELD_TYPE (decl);
17080 HOST_WIDE_INT bitpos_int;
17081 HOST_WIDE_INT highest_order_object_bit_offset;
17082 HOST_WIDE_INT highest_order_field_bit_offset;
17083 HOST_WIDE_INT bit_offset;
17085 /* Must be a field and a bit field. */
17086 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
17088 /* We can't yet handle bit-fields whose offsets are variable, so if we
17089 encounter such things, just return without generating any attribute
17090 whatsoever. Likewise for variable or too large size. */
17091 if (! tree_fits_shwi_p (bit_position (decl))
17092 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
17093 return;
17095 bitpos_int = int_bit_position (decl);
17097 /* Note that the bit offset is always the distance (in bits) from the
17098 highest-order bit of the "containing object" to the highest-order bit of
17099 the bit-field itself. Since the "high-order end" of any object or field
17100 is different on big-endian and little-endian machines, the computation
17101 below must take account of these differences. */
17102 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
17103 highest_order_field_bit_offset = bitpos_int;
17105 if (! BYTES_BIG_ENDIAN)
17107 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
17108 highest_order_object_bit_offset += simple_type_size_in_bits (type);
17111 bit_offset
17112 = (! BYTES_BIG_ENDIAN
17113 ? highest_order_object_bit_offset - highest_order_field_bit_offset
17114 : highest_order_field_bit_offset - highest_order_object_bit_offset);
17116 if (bit_offset < 0)
17117 add_AT_int (die, DW_AT_bit_offset, bit_offset);
17118 else
17119 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
17122 /* For a FIELD_DECL node which represents a bit field, output an attribute
17123 which specifies the length in bits of the given field. */
17125 static inline void
17126 add_bit_size_attribute (dw_die_ref die, tree decl)
17128 /* Must be a field and a bit field. */
17129 gcc_assert (TREE_CODE (decl) == FIELD_DECL
17130 && DECL_BIT_FIELD_TYPE (decl));
17132 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
17133 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
17136 /* If the compiled language is ANSI C, then add a 'prototyped'
17137 attribute, if arg types are given for the parameters of a function. */
17139 static inline void
17140 add_prototyped_attribute (dw_die_ref die, tree func_type)
17142 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
17144 case DW_LANG_C:
17145 case DW_LANG_C89:
17146 case DW_LANG_C99:
17147 case DW_LANG_C11:
17148 case DW_LANG_ObjC:
17149 if (prototype_p (func_type))
17150 add_AT_flag (die, DW_AT_prototyped, 1);
17151 break;
17152 default:
17153 break;
17157 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
17158 by looking in either the type declaration or object declaration
17159 equate table. */
17161 static inline dw_die_ref
17162 add_abstract_origin_attribute (dw_die_ref die, tree origin)
17164 dw_die_ref origin_die = NULL;
17166 if (TREE_CODE (origin) != FUNCTION_DECL)
17168 /* We may have gotten separated from the block for the inlined
17169 function, if we're in an exception handler or some such; make
17170 sure that the abstract function has been written out.
17172 Doing this for nested functions is wrong, however; functions are
17173 distinct units, and our context might not even be inline. */
17174 tree fn = origin;
17176 if (TYPE_P (fn))
17177 fn = TYPE_STUB_DECL (fn);
17179 fn = decl_function_context (fn);
17180 if (fn)
17181 dwarf2out_abstract_function (fn);
17184 if (DECL_P (origin))
17185 origin_die = lookup_decl_die (origin);
17186 else if (TYPE_P (origin))
17187 origin_die = lookup_type_die (origin);
17189 /* XXX: Functions that are never lowered don't always have correct block
17190 trees (in the case of java, they simply have no block tree, in some other
17191 languages). For these functions, there is nothing we can really do to
17192 output correct debug info for inlined functions in all cases. Rather
17193 than die, we'll just produce deficient debug info now, in that we will
17194 have variables without a proper abstract origin. In the future, when all
17195 functions are lowered, we should re-add a gcc_assert (origin_die)
17196 here. */
17198 if (origin_die)
17199 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
17200 return origin_die;
17203 /* We do not currently support the pure_virtual attribute. */
17205 static inline void
17206 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
17208 if (DECL_VINDEX (func_decl))
17210 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
17212 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
17213 add_AT_loc (die, DW_AT_vtable_elem_location,
17214 new_loc_descr (DW_OP_constu,
17215 tree_to_shwi (DECL_VINDEX (func_decl)),
17216 0));
17218 /* GNU extension: Record what type this method came from originally. */
17219 if (debug_info_level > DINFO_LEVEL_TERSE
17220 && DECL_CONTEXT (func_decl))
17221 add_AT_die_ref (die, DW_AT_containing_type,
17222 lookup_type_die (DECL_CONTEXT (func_decl)));
17226 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
17227 given decl. This used to be a vendor extension until after DWARF 4
17228 standardized it. */
17230 static void
17231 add_linkage_attr (dw_die_ref die, tree decl)
17233 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
17235 /* Mimic what assemble_name_raw does with a leading '*'. */
17236 if (name[0] == '*')
17237 name = &name[1];
17239 if (dwarf_version >= 4)
17240 add_AT_string (die, DW_AT_linkage_name, name);
17241 else
17242 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
17245 /* Add source coordinate attributes for the given decl. */
17247 static void
17248 add_src_coords_attributes (dw_die_ref die, tree decl)
17250 expanded_location s;
17252 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
17253 return;
17254 s = expand_location (DECL_SOURCE_LOCATION (decl));
17255 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
17256 add_AT_unsigned (die, DW_AT_decl_line, s.line);
17259 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
17261 static void
17262 add_linkage_name (dw_die_ref die, tree decl)
17264 if (debug_info_level > DINFO_LEVEL_NONE
17265 && (TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
17266 && TREE_PUBLIC (decl)
17267 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
17268 && die->die_tag != DW_TAG_member)
17270 /* Defer until we have an assembler name set. */
17271 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
17273 limbo_die_node *asm_name;
17275 asm_name = ggc_cleared_alloc<limbo_die_node> ();
17276 asm_name->die = die;
17277 asm_name->created_for = decl;
17278 asm_name->next = deferred_asm_name;
17279 deferred_asm_name = asm_name;
17281 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
17282 add_linkage_attr (die, decl);
17286 /* Add a DW_AT_name attribute and source coordinate attribute for the
17287 given decl, but only if it actually has a name. */
17289 static void
17290 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
17292 tree decl_name;
17294 decl_name = DECL_NAME (decl);
17295 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
17297 const char *name = dwarf2_name (decl, 0);
17298 if (name)
17299 add_name_attribute (die, name);
17300 if (! DECL_ARTIFICIAL (decl))
17301 add_src_coords_attributes (die, decl);
17303 add_linkage_name (die, decl);
17306 #ifdef VMS_DEBUGGING_INFO
17307 /* Get the function's name, as described by its RTL. This may be different
17308 from the DECL_NAME name used in the source file. */
17309 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
17311 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
17312 XEXP (DECL_RTL (decl), 0), false);
17313 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
17315 #endif /* VMS_DEBUGGING_INFO */
17318 #ifdef VMS_DEBUGGING_INFO
17319 /* Output the debug main pointer die for VMS */
17321 void
17322 dwarf2out_vms_debug_main_pointer (void)
17324 char label[MAX_ARTIFICIAL_LABEL_BYTES];
17325 dw_die_ref die;
17327 /* Allocate the VMS debug main subprogram die. */
17328 die = ggc_cleared_alloc<die_node> ();
17329 die->die_tag = DW_TAG_subprogram;
17330 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
17331 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
17332 current_function_funcdef_no);
17333 add_AT_lbl_id (die, DW_AT_entry_pc, label);
17335 /* Make it the first child of comp_unit_die (). */
17336 die->die_parent = comp_unit_die ();
17337 if (comp_unit_die ()->die_child)
17339 die->die_sib = comp_unit_die ()->die_child->die_sib;
17340 comp_unit_die ()->die_child->die_sib = die;
17342 else
17344 die->die_sib = die;
17345 comp_unit_die ()->die_child = die;
17348 #endif /* VMS_DEBUGGING_INFO */
17350 /* Push a new declaration scope. */
17352 static void
17353 push_decl_scope (tree scope)
17355 vec_safe_push (decl_scope_table, scope);
17358 /* Pop a declaration scope. */
17360 static inline void
17361 pop_decl_scope (void)
17363 decl_scope_table->pop ();
17366 /* walk_tree helper function for uses_local_type, below. */
17368 static tree
17369 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
17371 if (!TYPE_P (*tp))
17372 *walk_subtrees = 0;
17373 else
17375 tree name = TYPE_NAME (*tp);
17376 if (name && DECL_P (name) && decl_function_context (name))
17377 return *tp;
17379 return NULL_TREE;
17382 /* If TYPE involves a function-local type (including a local typedef to a
17383 non-local type), returns that type; otherwise returns NULL_TREE. */
17385 static tree
17386 uses_local_type (tree type)
17388 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
17389 return used;
17392 /* Return the DIE for the scope that immediately contains this type.
17393 Non-named types that do not involve a function-local type get global
17394 scope. Named types nested in namespaces or other types get their
17395 containing scope. All other types (i.e. function-local named types) get
17396 the current active scope. */
17398 static dw_die_ref
17399 scope_die_for (tree t, dw_die_ref context_die)
17401 dw_die_ref scope_die = NULL;
17402 tree containing_scope;
17404 /* Non-types always go in the current scope. */
17405 gcc_assert (TYPE_P (t));
17407 /* Use the scope of the typedef, rather than the scope of the type
17408 it refers to. */
17409 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
17410 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
17411 else
17412 containing_scope = TYPE_CONTEXT (t);
17414 /* Use the containing namespace if there is one. */
17415 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
17417 if (context_die == lookup_decl_die (containing_scope))
17418 /* OK */;
17419 else if (debug_info_level > DINFO_LEVEL_TERSE)
17420 context_die = get_context_die (containing_scope);
17421 else
17422 containing_scope = NULL_TREE;
17425 /* Ignore function type "scopes" from the C frontend. They mean that
17426 a tagged type is local to a parmlist of a function declarator, but
17427 that isn't useful to DWARF. */
17428 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
17429 containing_scope = NULL_TREE;
17431 if (SCOPE_FILE_SCOPE_P (containing_scope))
17433 /* If T uses a local type keep it local as well, to avoid references
17434 to function-local DIEs from outside the function. */
17435 if (current_function_decl && uses_local_type (t))
17436 scope_die = context_die;
17437 else
17438 scope_die = comp_unit_die ();
17440 else if (TYPE_P (containing_scope))
17442 /* For types, we can just look up the appropriate DIE. */
17443 if (debug_info_level > DINFO_LEVEL_TERSE)
17444 scope_die = get_context_die (containing_scope);
17445 else
17447 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
17448 if (scope_die == NULL)
17449 scope_die = comp_unit_die ();
17452 else
17453 scope_die = context_die;
17455 return scope_die;
17458 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
17460 static inline int
17461 local_scope_p (dw_die_ref context_die)
17463 for (; context_die; context_die = context_die->die_parent)
17464 if (context_die->die_tag == DW_TAG_inlined_subroutine
17465 || context_die->die_tag == DW_TAG_subprogram)
17466 return 1;
17468 return 0;
17471 /* Returns nonzero if CONTEXT_DIE is a class. */
17473 static inline int
17474 class_scope_p (dw_die_ref context_die)
17476 return (context_die
17477 && (context_die->die_tag == DW_TAG_structure_type
17478 || context_die->die_tag == DW_TAG_class_type
17479 || context_die->die_tag == DW_TAG_interface_type
17480 || context_die->die_tag == DW_TAG_union_type));
17483 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
17484 whether or not to treat a DIE in this context as a declaration. */
17486 static inline int
17487 class_or_namespace_scope_p (dw_die_ref context_die)
17489 return (class_scope_p (context_die)
17490 || (context_die && context_die->die_tag == DW_TAG_namespace));
17493 /* Many forms of DIEs require a "type description" attribute. This
17494 routine locates the proper "type descriptor" die for the type given
17495 by 'type' plus any additional qualifiers given by 'cv_quals', and
17496 adds a DW_AT_type attribute below the given die. */
17498 static void
17499 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
17500 dw_die_ref context_die)
17502 enum tree_code code = TREE_CODE (type);
17503 dw_die_ref type_die = NULL;
17505 /* ??? If this type is an unnamed subrange type of an integral, floating-point
17506 or fixed-point type, use the inner type. This is because we have no
17507 support for unnamed types in base_type_die. This can happen if this is
17508 an Ada subrange type. Correct solution is emit a subrange type die. */
17509 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
17510 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
17511 type = TREE_TYPE (type), code = TREE_CODE (type);
17513 if (code == ERROR_MARK
17514 /* Handle a special case. For functions whose return type is void, we
17515 generate *no* type attribute. (Note that no object may have type
17516 `void', so this only applies to function return types). */
17517 || code == VOID_TYPE)
17518 return;
17520 type_die = modified_type_die (type,
17521 cv_quals | TYPE_QUALS_NO_ADDR_SPACE (type),
17522 context_die);
17524 if (type_die != NULL)
17525 add_AT_die_ref (object_die, DW_AT_type, type_die);
17528 /* Given an object die, add the calling convention attribute for the
17529 function call type. */
17530 static void
17531 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
17533 enum dwarf_calling_convention value = DW_CC_normal;
17535 value = ((enum dwarf_calling_convention)
17536 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
17538 if (is_fortran ()
17539 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
17541 /* DWARF 2 doesn't provide a way to identify a program's source-level
17542 entry point. DW_AT_calling_convention attributes are only meant
17543 to describe functions' calling conventions. However, lacking a
17544 better way to signal the Fortran main program, we used this for
17545 a long time, following existing custom. Now, DWARF 4 has
17546 DW_AT_main_subprogram, which we add below, but some tools still
17547 rely on the old way, which we thus keep. */
17548 value = DW_CC_program;
17550 if (dwarf_version >= 4 || !dwarf_strict)
17551 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
17554 /* Only add the attribute if the backend requests it, and
17555 is not DW_CC_normal. */
17556 if (value && (value != DW_CC_normal))
17557 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
17560 /* Given a tree pointer to a struct, class, union, or enum type node, return
17561 a pointer to the (string) tag name for the given type, or zero if the type
17562 was declared without a tag. */
17564 static const char *
17565 type_tag (const_tree type)
17567 const char *name = 0;
17569 if (TYPE_NAME (type) != 0)
17571 tree t = 0;
17573 /* Find the IDENTIFIER_NODE for the type name. */
17574 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
17575 && !TYPE_NAMELESS (type))
17576 t = TYPE_NAME (type);
17578 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
17579 a TYPE_DECL node, regardless of whether or not a `typedef' was
17580 involved. */
17581 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
17582 && ! DECL_IGNORED_P (TYPE_NAME (type)))
17584 /* We want to be extra verbose. Don't call dwarf_name if
17585 DECL_NAME isn't set. The default hook for decl_printable_name
17586 doesn't like that, and in this context it's correct to return
17587 0, instead of "<anonymous>" or the like. */
17588 if (DECL_NAME (TYPE_NAME (type))
17589 && !DECL_NAMELESS (TYPE_NAME (type)))
17590 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
17593 /* Now get the name as a string, or invent one. */
17594 if (!name && t != 0)
17595 name = IDENTIFIER_POINTER (t);
17598 return (name == 0 || *name == '\0') ? 0 : name;
17601 /* Return the type associated with a data member, make a special check
17602 for bit field types. */
17604 static inline tree
17605 member_declared_type (const_tree member)
17607 return (DECL_BIT_FIELD_TYPE (member)
17608 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
17611 /* Get the decl's label, as described by its RTL. This may be different
17612 from the DECL_NAME name used in the source file. */
17614 #if 0
17615 static const char *
17616 decl_start_label (tree decl)
17618 rtx x;
17619 const char *fnname;
17621 x = DECL_RTL (decl);
17622 gcc_assert (MEM_P (x));
17624 x = XEXP (x, 0);
17625 gcc_assert (GET_CODE (x) == SYMBOL_REF);
17627 fnname = XSTR (x, 0);
17628 return fnname;
17630 #endif
17632 /* For variable-length arrays that have been previously generated, but
17633 may be incomplete due to missing subscript info, fill the subscript
17634 info. Return TRUE if this is one of those cases. */
17635 static bool
17636 fill_variable_array_bounds (tree type)
17638 if (TREE_ASM_WRITTEN (type)
17639 && TREE_CODE (type) == ARRAY_TYPE
17640 && variably_modified_type_p (type, NULL))
17642 dw_die_ref array_die = lookup_type_die (type);
17643 if (!array_die)
17644 return false;
17645 add_subscript_info (array_die, type, !is_ada ());
17646 return true;
17648 return false;
17651 /* These routines generate the internal representation of the DIE's for
17652 the compilation unit. Debugging information is collected by walking
17653 the declaration trees passed in from dwarf2out_decl(). */
17655 static void
17656 gen_array_type_die (tree type, dw_die_ref context_die)
17658 dw_die_ref array_die;
17660 /* GNU compilers represent multidimensional array types as sequences of one
17661 dimensional array types whose element types are themselves array types.
17662 We sometimes squish that down to a single array_type DIE with multiple
17663 subscripts in the Dwarf debugging info. The draft Dwarf specification
17664 say that we are allowed to do this kind of compression in C, because
17665 there is no difference between an array of arrays and a multidimensional
17666 array. We don't do this for Ada to remain as close as possible to the
17667 actual representation, which is especially important against the language
17668 flexibilty wrt arrays of variable size. */
17670 bool collapse_nested_arrays = !is_ada ();
17672 if (fill_variable_array_bounds (type))
17673 return;
17675 dw_die_ref scope_die = scope_die_for (type, context_die);
17676 tree element_type;
17678 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
17679 DW_TAG_string_type doesn't have DW_AT_type attribute). */
17680 if (TYPE_STRING_FLAG (type)
17681 && TREE_CODE (type) == ARRAY_TYPE
17682 && is_fortran ()
17683 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
17685 HOST_WIDE_INT size;
17687 array_die = new_die (DW_TAG_string_type, scope_die, type);
17688 add_name_attribute (array_die, type_tag (type));
17689 equate_type_number_to_die (type, array_die);
17690 size = int_size_in_bytes (type);
17691 if (size >= 0)
17692 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17693 else if (TYPE_DOMAIN (type) != NULL_TREE
17694 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
17695 && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
17697 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
17698 dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2, NULL);
17700 size = int_size_in_bytes (TREE_TYPE (szdecl));
17701 if (loc && size > 0)
17703 add_AT_location_description (array_die, DW_AT_string_length, loc);
17704 if (size != DWARF2_ADDR_SIZE)
17705 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17708 return;
17711 array_die = new_die (DW_TAG_array_type, scope_die, type);
17712 add_name_attribute (array_die, type_tag (type));
17713 equate_type_number_to_die (type, array_die);
17715 if (TREE_CODE (type) == VECTOR_TYPE)
17716 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
17718 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
17719 if (is_fortran ()
17720 && TREE_CODE (type) == ARRAY_TYPE
17721 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
17722 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
17723 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17725 #if 0
17726 /* We default the array ordering. SDB will probably do
17727 the right things even if DW_AT_ordering is not present. It's not even
17728 an issue until we start to get into multidimensional arrays anyway. If
17729 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
17730 then we'll have to put the DW_AT_ordering attribute back in. (But if
17731 and when we find out that we need to put these in, we will only do so
17732 for multidimensional arrays. */
17733 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17734 #endif
17736 if (TREE_CODE (type) == VECTOR_TYPE)
17738 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
17739 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
17740 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node, NULL);
17741 add_bound_info (subrange_die, DW_AT_upper_bound,
17742 size_int (TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
17744 else
17745 add_subscript_info (array_die, type, collapse_nested_arrays);
17747 /* Add representation of the type of the elements of this array type and
17748 emit the corresponding DIE if we haven't done it already. */
17749 element_type = TREE_TYPE (type);
17750 if (collapse_nested_arrays)
17751 while (TREE_CODE (element_type) == ARRAY_TYPE)
17753 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
17754 break;
17755 element_type = TREE_TYPE (element_type);
17758 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED, context_die);
17760 add_gnat_descriptive_type_attribute (array_die, type, context_die);
17761 if (TYPE_ARTIFICIAL (type))
17762 add_AT_flag (array_die, DW_AT_artificial, 1);
17764 if (get_AT (array_die, DW_AT_name))
17765 add_pubtype (type, array_die);
17768 /* This routine generates DIE for array with hidden descriptor, details
17769 are filled into *info by a langhook. */
17771 static void
17772 gen_descr_array_type_die (tree type, struct array_descr_info *info,
17773 dw_die_ref context_die)
17775 const dw_die_ref scope_die = scope_die_for (type, context_die);
17776 const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
17777 const struct loc_descr_context context = { type, info->base_decl };
17778 int dim;
17780 add_name_attribute (array_die, type_tag (type));
17781 equate_type_number_to_die (type, array_die);
17783 if (info->ndimensions > 1)
17784 switch (info->ordering)
17786 case array_descr_ordering_row_major:
17787 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17788 break;
17789 case array_descr_ordering_column_major:
17790 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17791 break;
17792 default:
17793 break;
17796 if (dwarf_version >= 3 || !dwarf_strict)
17798 if (info->data_location)
17799 add_scalar_info (array_die, DW_AT_data_location, info->data_location,
17800 dw_scalar_form_exprloc, &context);
17801 if (info->associated)
17802 add_scalar_info (array_die, DW_AT_associated, info->associated,
17803 dw_scalar_form_constant
17804 | dw_scalar_form_exprloc
17805 | dw_scalar_form_reference, &context);
17806 if (info->allocated)
17807 add_scalar_info (array_die, DW_AT_allocated, info->allocated,
17808 dw_scalar_form_constant
17809 | dw_scalar_form_exprloc
17810 | dw_scalar_form_reference, &context);
17813 add_gnat_descriptive_type_attribute (array_die, type, context_die);
17815 for (dim = 0; dim < info->ndimensions; dim++)
17817 dw_die_ref subrange_die
17818 = new_die (DW_TAG_subrange_type, array_die, NULL);
17820 if (info->dimen[dim].bounds_type)
17821 add_type_attribute (subrange_die,
17822 info->dimen[dim].bounds_type, 0,
17823 context_die);
17824 if (info->dimen[dim].lower_bound)
17825 add_bound_info (subrange_die, DW_AT_lower_bound,
17826 info->dimen[dim].lower_bound, &context);
17827 if (info->dimen[dim].upper_bound)
17828 add_bound_info (subrange_die, DW_AT_upper_bound,
17829 info->dimen[dim].upper_bound, &context);
17830 if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
17831 add_scalar_info (subrange_die, DW_AT_byte_stride,
17832 info->dimen[dim].stride,
17833 dw_scalar_form_constant
17834 | dw_scalar_form_exprloc
17835 | dw_scalar_form_reference,
17836 &context);
17839 gen_type_die (info->element_type, context_die);
17840 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
17841 context_die);
17843 if (get_AT (array_die, DW_AT_name))
17844 add_pubtype (type, array_die);
17847 #if 0
17848 static void
17849 gen_entry_point_die (tree decl, dw_die_ref context_die)
17851 tree origin = decl_ultimate_origin (decl);
17852 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
17854 if (origin != NULL)
17855 add_abstract_origin_attribute (decl_die, origin);
17856 else
17858 add_name_and_src_coords_attributes (decl_die, decl);
17859 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
17860 TYPE_UNQUALIFIED, context_die);
17863 if (DECL_ABSTRACT_P (decl))
17864 equate_decl_number_to_die (decl, decl_die);
17865 else
17866 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
17868 #endif
17870 /* Walk through the list of incomplete types again, trying once more to
17871 emit full debugging info for them. */
17873 static void
17874 retry_incomplete_types (void)
17876 int i;
17878 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
17879 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
17880 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
17883 /* Determine what tag to use for a record type. */
17885 static enum dwarf_tag
17886 record_type_tag (tree type)
17888 if (! lang_hooks.types.classify_record)
17889 return DW_TAG_structure_type;
17891 switch (lang_hooks.types.classify_record (type))
17893 case RECORD_IS_STRUCT:
17894 return DW_TAG_structure_type;
17896 case RECORD_IS_CLASS:
17897 return DW_TAG_class_type;
17899 case RECORD_IS_INTERFACE:
17900 if (dwarf_version >= 3 || !dwarf_strict)
17901 return DW_TAG_interface_type;
17902 return DW_TAG_structure_type;
17904 default:
17905 gcc_unreachable ();
17909 /* Generate a DIE to represent an enumeration type. Note that these DIEs
17910 include all of the information about the enumeration values also. Each
17911 enumerated type name/value is listed as a child of the enumerated type
17912 DIE. */
17914 static dw_die_ref
17915 gen_enumeration_type_die (tree type, dw_die_ref context_die)
17917 dw_die_ref type_die = lookup_type_die (type);
17919 if (type_die == NULL)
17921 type_die = new_die (DW_TAG_enumeration_type,
17922 scope_die_for (type, context_die), type);
17923 equate_type_number_to_die (type, type_die);
17924 add_name_attribute (type_die, type_tag (type));
17925 if (dwarf_version >= 4 || !dwarf_strict)
17927 if (ENUM_IS_SCOPED (type))
17928 add_AT_flag (type_die, DW_AT_enum_class, 1);
17929 if (ENUM_IS_OPAQUE (type))
17930 add_AT_flag (type_die, DW_AT_declaration, 1);
17933 else if (! TYPE_SIZE (type))
17934 return type_die;
17935 else
17936 remove_AT (type_die, DW_AT_declaration);
17938 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
17939 given enum type is incomplete, do not generate the DW_AT_byte_size
17940 attribute or the DW_AT_element_list attribute. */
17941 if (TYPE_SIZE (type))
17943 tree link;
17945 TREE_ASM_WRITTEN (type) = 1;
17946 add_byte_size_attribute (type_die, type);
17947 if (dwarf_version >= 3 || !dwarf_strict)
17949 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
17950 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED,
17951 context_die);
17953 if (TYPE_STUB_DECL (type) != NULL_TREE)
17955 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
17956 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
17959 /* If the first reference to this type was as the return type of an
17960 inline function, then it may not have a parent. Fix this now. */
17961 if (type_die->die_parent == NULL)
17962 add_child_die (scope_die_for (type, context_die), type_die);
17964 for (link = TYPE_VALUES (type);
17965 link != NULL; link = TREE_CHAIN (link))
17967 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
17968 tree value = TREE_VALUE (link);
17970 add_name_attribute (enum_die,
17971 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
17973 if (TREE_CODE (value) == CONST_DECL)
17974 value = DECL_INITIAL (value);
17976 if (simple_type_size_in_bits (TREE_TYPE (value))
17977 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
17979 /* For constant forms created by add_AT_unsigned DWARF
17980 consumers (GDB, elfutils, etc.) always zero extend
17981 the value. Only when the actual value is negative
17982 do we need to use add_AT_int to generate a constant
17983 form that can represent negative values. */
17984 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
17985 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
17986 add_AT_unsigned (enum_die, DW_AT_const_value,
17987 (unsigned HOST_WIDE_INT) val);
17988 else
17989 add_AT_int (enum_die, DW_AT_const_value, val);
17991 else
17992 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
17993 that here. TODO: This should be re-worked to use correct
17994 signed/unsigned double tags for all cases. */
17995 add_AT_wide (enum_die, DW_AT_const_value, value);
17998 add_gnat_descriptive_type_attribute (type_die, type, context_die);
17999 if (TYPE_ARTIFICIAL (type))
18000 add_AT_flag (type_die, DW_AT_artificial, 1);
18002 else
18003 add_AT_flag (type_die, DW_AT_declaration, 1);
18005 add_pubtype (type, type_die);
18007 return type_die;
18010 /* Generate a DIE to represent either a real live formal parameter decl or to
18011 represent just the type of some formal parameter position in some function
18012 type.
18014 Note that this routine is a bit unusual because its argument may be a
18015 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
18016 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
18017 node. If it's the former then this function is being called to output a
18018 DIE to represent a formal parameter object (or some inlining thereof). If
18019 it's the latter, then this function is only being called to output a
18020 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
18021 argument type of some subprogram type.
18022 If EMIT_NAME_P is true, name and source coordinate attributes
18023 are emitted. */
18025 static dw_die_ref
18026 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
18027 dw_die_ref context_die)
18029 tree node_or_origin = node ? node : origin;
18030 tree ultimate_origin;
18031 dw_die_ref parm_die = NULL;
18033 if (TREE_CODE_CLASS (TREE_CODE (node_or_origin)) == tcc_declaration)
18035 parm_die = lookup_decl_die (node);
18037 /* If the contexts differ, we may not be talking about the same
18038 thing. */
18039 if (parm_die && parm_die->die_parent != context_die)
18041 if (!DECL_ABSTRACT_P (node))
18043 /* This can happen when creating an inlined instance, in
18044 which case we need to create a new DIE that will get
18045 annotated with DW_AT_abstract_origin. */
18046 parm_die = NULL;
18048 else
18050 /* FIXME: Reuse DIE even with a differing context.
18052 This can happen when calling
18053 dwarf2out_abstract_function to build debug info for
18054 the abstract instance of a function for which we have
18055 already generated a DIE in
18056 dwarf2out_early_global_decl.
18058 Once we remove dwarf2out_abstract_function, we should
18059 have a call to gcc_unreachable here. */
18063 if (parm_die && parm_die->die_parent == NULL)
18065 /* Check that parm_die already has the right attributes that
18066 we would have added below. If any attributes are
18067 missing, fall through to add them. */
18068 if (! DECL_ABSTRACT_P (node_or_origin)
18069 && !get_AT (parm_die, DW_AT_location)
18070 && !get_AT (parm_die, DW_AT_const_value))
18071 /* We are missing location info, and are about to add it. */
18073 else
18075 add_child_die (context_die, parm_die);
18076 return parm_die;
18081 /* If we have a previously generated DIE, use it, unless this is an
18082 concrete instance (origin != NULL), in which case we need a new
18083 DIE with a corresponding DW_AT_abstract_origin. */
18084 bool reusing_die;
18085 if (parm_die && origin == NULL)
18086 reusing_die = true;
18087 else
18089 parm_die = new_die (DW_TAG_formal_parameter, context_die, node);
18090 reusing_die = false;
18093 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
18095 case tcc_declaration:
18096 ultimate_origin = decl_ultimate_origin (node_or_origin);
18097 if (node || ultimate_origin)
18098 origin = ultimate_origin;
18100 if (reusing_die)
18101 goto add_location;
18103 if (origin != NULL)
18104 add_abstract_origin_attribute (parm_die, origin);
18105 else if (emit_name_p)
18106 add_name_and_src_coords_attributes (parm_die, node);
18107 if (origin == NULL
18108 || (! DECL_ABSTRACT_P (node_or_origin)
18109 && variably_modified_type_p (TREE_TYPE (node_or_origin),
18110 decl_function_context
18111 (node_or_origin))))
18113 tree type = TREE_TYPE (node_or_origin);
18114 if (decl_by_reference_p (node_or_origin))
18115 add_type_attribute (parm_die, TREE_TYPE (type),
18116 TYPE_UNQUALIFIED, context_die);
18117 else
18118 add_type_attribute (parm_die, type,
18119 decl_quals (node_or_origin),
18120 context_die);
18122 if (origin == NULL && DECL_ARTIFICIAL (node))
18123 add_AT_flag (parm_die, DW_AT_artificial, 1);
18124 add_location:
18125 if (node && node != origin)
18126 equate_decl_number_to_die (node, parm_die);
18127 if (! DECL_ABSTRACT_P (node_or_origin))
18128 add_location_or_const_value_attribute (parm_die, node_or_origin,
18129 node == NULL, DW_AT_location);
18131 break;
18133 case tcc_type:
18134 /* We were called with some kind of a ..._TYPE node. */
18135 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED,
18136 context_die);
18137 break;
18139 default:
18140 gcc_unreachable ();
18143 return parm_die;
18146 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
18147 children DW_TAG_formal_parameter DIEs representing the arguments of the
18148 parameter pack.
18150 PARM_PACK must be a function parameter pack.
18151 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
18152 must point to the subsequent arguments of the function PACK_ARG belongs to.
18153 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
18154 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
18155 following the last one for which a DIE was generated. */
18157 static dw_die_ref
18158 gen_formal_parameter_pack_die (tree parm_pack,
18159 tree pack_arg,
18160 dw_die_ref subr_die,
18161 tree *next_arg)
18163 tree arg;
18164 dw_die_ref parm_pack_die;
18166 gcc_assert (parm_pack
18167 && lang_hooks.function_parameter_pack_p (parm_pack)
18168 && subr_die);
18170 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
18171 add_src_coords_attributes (parm_pack_die, parm_pack);
18173 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
18175 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
18176 parm_pack))
18177 break;
18178 gen_formal_parameter_die (arg, NULL,
18179 false /* Don't emit name attribute. */,
18180 parm_pack_die);
18182 if (next_arg)
18183 *next_arg = arg;
18184 return parm_pack_die;
18187 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
18188 at the end of an (ANSI prototyped) formal parameters list. */
18190 static void
18191 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
18193 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
18196 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
18197 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
18198 parameters as specified in some function type specification (except for
18199 those which appear as part of a function *definition*). */
18201 static void
18202 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
18204 tree link;
18205 tree formal_type = NULL;
18206 tree first_parm_type;
18207 tree arg;
18209 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
18211 arg = DECL_ARGUMENTS (function_or_method_type);
18212 function_or_method_type = TREE_TYPE (function_or_method_type);
18214 else
18215 arg = NULL_TREE;
18217 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
18219 /* Make our first pass over the list of formal parameter types and output a
18220 DW_TAG_formal_parameter DIE for each one. */
18221 for (link = first_parm_type; link; )
18223 dw_die_ref parm_die;
18225 formal_type = TREE_VALUE (link);
18226 if (formal_type == void_type_node)
18227 break;
18229 /* Output a (nameless) DIE to represent the formal parameter itself. */
18230 if (!POINTER_BOUNDS_TYPE_P (formal_type))
18232 parm_die = gen_formal_parameter_die (formal_type, NULL,
18233 true /* Emit name attribute. */,
18234 context_die);
18235 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
18236 && link == first_parm_type)
18238 add_AT_flag (parm_die, DW_AT_artificial, 1);
18239 if (dwarf_version >= 3 || !dwarf_strict)
18240 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
18242 else if (arg && DECL_ARTIFICIAL (arg))
18243 add_AT_flag (parm_die, DW_AT_artificial, 1);
18246 link = TREE_CHAIN (link);
18247 if (arg)
18248 arg = DECL_CHAIN (arg);
18251 /* If this function type has an ellipsis, add a
18252 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
18253 if (formal_type != void_type_node)
18254 gen_unspecified_parameters_die (function_or_method_type, context_die);
18256 /* Make our second (and final) pass over the list of formal parameter types
18257 and output DIEs to represent those types (as necessary). */
18258 for (link = TYPE_ARG_TYPES (function_or_method_type);
18259 link && TREE_VALUE (link);
18260 link = TREE_CHAIN (link))
18261 gen_type_die (TREE_VALUE (link), context_die);
18264 /* We want to generate the DIE for TYPE so that we can generate the
18265 die for MEMBER, which has been defined; we will need to refer back
18266 to the member declaration nested within TYPE. If we're trying to
18267 generate minimal debug info for TYPE, processing TYPE won't do the
18268 trick; we need to attach the member declaration by hand. */
18270 static void
18271 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
18273 gen_type_die (type, context_die);
18275 /* If we're trying to avoid duplicate debug info, we may not have
18276 emitted the member decl for this function. Emit it now. */
18277 if (TYPE_STUB_DECL (type)
18278 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
18279 && ! lookup_decl_die (member))
18281 dw_die_ref type_die;
18282 gcc_assert (!decl_ultimate_origin (member));
18284 push_decl_scope (type);
18285 type_die = lookup_type_die_strip_naming_typedef (type);
18286 if (TREE_CODE (member) == FUNCTION_DECL)
18287 gen_subprogram_die (member, type_die);
18288 else if (TREE_CODE (member) == FIELD_DECL)
18290 /* Ignore the nameless fields that are used to skip bits but handle
18291 C++ anonymous unions and structs. */
18292 if (DECL_NAME (member) != NULL_TREE
18293 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
18294 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
18296 gen_type_die (member_declared_type (member), type_die);
18297 gen_field_die (member, type_die);
18300 else
18301 gen_variable_die (member, NULL_TREE, type_die);
18303 pop_decl_scope ();
18307 /* Forward declare these functions, because they are mutually recursive
18308 with their set_block_* pairing functions. */
18309 static void set_decl_origin_self (tree);
18310 static void set_decl_abstract_flags (tree, vec<tree> &);
18312 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
18313 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
18314 that it points to the node itself, thus indicating that the node is its
18315 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
18316 the given node is NULL, recursively descend the decl/block tree which
18317 it is the root of, and for each other ..._DECL or BLOCK node contained
18318 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
18319 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
18320 values to point to themselves. */
18322 static void
18323 set_block_origin_self (tree stmt)
18325 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
18327 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
18330 tree local_decl;
18332 for (local_decl = BLOCK_VARS (stmt);
18333 local_decl != NULL_TREE;
18334 local_decl = DECL_CHAIN (local_decl))
18335 /* Do not recurse on nested functions since the inlining status
18336 of parent and child can be different as per the DWARF spec. */
18337 if (TREE_CODE (local_decl) != FUNCTION_DECL
18338 && !DECL_EXTERNAL (local_decl))
18339 set_decl_origin_self (local_decl);
18343 tree subblock;
18345 for (subblock = BLOCK_SUBBLOCKS (stmt);
18346 subblock != NULL_TREE;
18347 subblock = BLOCK_CHAIN (subblock))
18348 set_block_origin_self (subblock); /* Recurse. */
18353 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
18354 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
18355 node to so that it points to the node itself, thus indicating that the
18356 node represents its own (abstract) origin. Additionally, if the
18357 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
18358 the decl/block tree of which the given node is the root of, and for
18359 each other ..._DECL or BLOCK node contained therein whose
18360 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
18361 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
18362 point to themselves. */
18364 static void
18365 set_decl_origin_self (tree decl)
18367 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
18369 DECL_ABSTRACT_ORIGIN (decl) = decl;
18370 if (TREE_CODE (decl) == FUNCTION_DECL)
18372 tree arg;
18374 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
18375 DECL_ABSTRACT_ORIGIN (arg) = arg;
18376 if (DECL_INITIAL (decl) != NULL_TREE
18377 && DECL_INITIAL (decl) != error_mark_node)
18378 set_block_origin_self (DECL_INITIAL (decl));
18383 /* Given a pointer to some BLOCK node, set the BLOCK_ABSTRACT flag to 1
18384 and if it wasn't 1 before, push it to abstract_vec vector.
18385 For all local decls and all local sub-blocks (recursively) do it
18386 too. */
18388 static void
18389 set_block_abstract_flags (tree stmt, vec<tree> &abstract_vec)
18391 tree local_decl;
18392 tree subblock;
18393 unsigned int i;
18395 if (!BLOCK_ABSTRACT (stmt))
18397 abstract_vec.safe_push (stmt);
18398 BLOCK_ABSTRACT (stmt) = 1;
18401 for (local_decl = BLOCK_VARS (stmt);
18402 local_decl != NULL_TREE;
18403 local_decl = DECL_CHAIN (local_decl))
18404 if (! DECL_EXTERNAL (local_decl))
18405 set_decl_abstract_flags (local_decl, abstract_vec);
18407 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
18409 local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
18410 if ((TREE_CODE (local_decl) == VAR_DECL && !TREE_STATIC (local_decl))
18411 || TREE_CODE (local_decl) == PARM_DECL)
18412 set_decl_abstract_flags (local_decl, abstract_vec);
18415 for (subblock = BLOCK_SUBBLOCKS (stmt);
18416 subblock != NULL_TREE;
18417 subblock = BLOCK_CHAIN (subblock))
18418 set_block_abstract_flags (subblock, abstract_vec);
18421 /* Given a pointer to some ..._DECL node, set DECL_ABSTRACT_P flag on it
18422 to 1 and if it wasn't 1 before, push to abstract_vec vector.
18423 In the case where the decl is a FUNCTION_DECL also set the abstract
18424 flags for all of the parameters, local vars, local
18425 blocks and sub-blocks (recursively). */
18427 static void
18428 set_decl_abstract_flags (tree decl, vec<tree> &abstract_vec)
18430 if (!DECL_ABSTRACT_P (decl))
18432 abstract_vec.safe_push (decl);
18433 DECL_ABSTRACT_P (decl) = 1;
18436 if (TREE_CODE (decl) == FUNCTION_DECL)
18438 tree arg;
18440 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
18441 if (!DECL_ABSTRACT_P (arg))
18443 abstract_vec.safe_push (arg);
18444 DECL_ABSTRACT_P (arg) = 1;
18446 if (DECL_INITIAL (decl) != NULL_TREE
18447 && DECL_INITIAL (decl) != error_mark_node)
18448 set_block_abstract_flags (DECL_INITIAL (decl), abstract_vec);
18452 /* Generate the DWARF2 info for the "abstract" instance of a function which we
18453 may later generate inlined and/or out-of-line instances of.
18455 FIXME: In the early-dwarf world, this function, and most of the
18456 DECL_ABSTRACT code should be obsoleted. The early DIE _is_
18457 the abstract instance. All we would need to do is annotate
18458 the early DIE with the appropriate DW_AT_inline in late
18459 dwarf (perhaps in gen_inlined_subroutine_die).
18461 However, we can't do this yet, because LTO streaming of DIEs
18462 has not been implemented yet. */
18464 static void
18465 dwarf2out_abstract_function (tree decl)
18467 dw_die_ref old_die;
18468 tree save_fn;
18469 tree context;
18470 hash_table<decl_loc_hasher> *old_decl_loc_table;
18471 hash_table<dw_loc_list_hasher> *old_cached_dw_loc_list_table;
18472 int old_call_site_count, old_tail_call_site_count;
18473 struct call_arg_loc_node *old_call_arg_locations;
18475 /* Make sure we have the actual abstract inline, not a clone. */
18476 decl = DECL_ORIGIN (decl);
18478 old_die = lookup_decl_die (decl);
18479 if (old_die && get_AT (old_die, DW_AT_inline))
18480 /* We've already generated the abstract instance. */
18481 return;
18483 /* We can be called while recursively when seeing block defining inlined subroutine
18484 DIE. Be sure to not clobber the outer location table nor use it or we would
18485 get locations in abstract instantces. */
18486 old_decl_loc_table = decl_loc_table;
18487 decl_loc_table = NULL;
18488 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
18489 cached_dw_loc_list_table = NULL;
18490 old_call_arg_locations = call_arg_locations;
18491 call_arg_locations = NULL;
18492 old_call_site_count = call_site_count;
18493 call_site_count = -1;
18494 old_tail_call_site_count = tail_call_site_count;
18495 tail_call_site_count = -1;
18497 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
18498 we don't get confused by DECL_ABSTRACT_P. */
18499 if (debug_info_level > DINFO_LEVEL_TERSE)
18501 context = decl_class_context (decl);
18502 if (context)
18503 gen_type_die_for_member
18504 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
18507 /* Pretend we've just finished compiling this function. */
18508 save_fn = current_function_decl;
18509 current_function_decl = decl;
18511 auto_vec<tree, 64> abstract_vec;
18512 set_decl_abstract_flags (decl, abstract_vec);
18513 dwarf2out_decl (decl);
18514 unsigned int i;
18515 tree t;
18516 FOR_EACH_VEC_ELT (abstract_vec, i, t)
18517 if (TREE_CODE (t) == BLOCK)
18518 BLOCK_ABSTRACT (t) = 0;
18519 else
18520 DECL_ABSTRACT_P (t) = 0;
18522 current_function_decl = save_fn;
18523 decl_loc_table = old_decl_loc_table;
18524 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
18525 call_arg_locations = old_call_arg_locations;
18526 call_site_count = old_call_site_count;
18527 tail_call_site_count = old_tail_call_site_count;
18530 /* Helper function of premark_used_types() which gets called through
18531 htab_traverse.
18533 Marks the DIE of a given type in *SLOT as perennial, so it never gets
18534 marked as unused by prune_unused_types. */
18536 bool
18537 premark_used_types_helper (tree const &type, void *)
18539 dw_die_ref die;
18541 die = lookup_type_die (type);
18542 if (die != NULL)
18543 die->die_perennial_p = 1;
18544 return true;
18547 /* Helper function of premark_types_used_by_global_vars which gets called
18548 through htab_traverse.
18550 Marks the DIE of a given type in *SLOT as perennial, so it never gets
18551 marked as unused by prune_unused_types. The DIE of the type is marked
18552 only if the global variable using the type will actually be emitted. */
18555 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
18556 void *)
18558 struct types_used_by_vars_entry *entry;
18559 dw_die_ref die;
18561 entry = (struct types_used_by_vars_entry *) *slot;
18562 gcc_assert (entry->type != NULL
18563 && entry->var_decl != NULL);
18564 die = lookup_type_die (entry->type);
18565 if (die)
18567 /* Ask cgraph if the global variable really is to be emitted.
18568 If yes, then we'll keep the DIE of ENTRY->TYPE. */
18569 varpool_node *node = varpool_node::get (entry->var_decl);
18570 if (node && node->definition)
18572 die->die_perennial_p = 1;
18573 /* Keep the parent DIEs as well. */
18574 while ((die = die->die_parent) && die->die_perennial_p == 0)
18575 die->die_perennial_p = 1;
18578 return 1;
18581 /* Mark all members of used_types_hash as perennial. */
18583 static void
18584 premark_used_types (struct function *fun)
18586 if (fun && fun->used_types_hash)
18587 fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
18590 /* Mark all members of types_used_by_vars_entry as perennial. */
18592 static void
18593 premark_types_used_by_global_vars (void)
18595 if (types_used_by_vars_hash)
18596 types_used_by_vars_hash
18597 ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
18600 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
18601 for CA_LOC call arg loc node. */
18603 static dw_die_ref
18604 gen_call_site_die (tree decl, dw_die_ref subr_die,
18605 struct call_arg_loc_node *ca_loc)
18607 dw_die_ref stmt_die = NULL, die;
18608 tree block = ca_loc->block;
18610 while (block
18611 && block != DECL_INITIAL (decl)
18612 && TREE_CODE (block) == BLOCK)
18614 stmt_die = BLOCK_DIE (block);
18615 if (stmt_die)
18616 break;
18617 block = BLOCK_SUPERCONTEXT (block);
18619 if (stmt_die == NULL)
18620 stmt_die = subr_die;
18621 die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
18622 add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
18623 if (ca_loc->tail_call_p)
18624 add_AT_flag (die, DW_AT_GNU_tail_call, 1);
18625 if (ca_loc->symbol_ref)
18627 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
18628 if (tdie)
18629 add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
18630 else
18631 add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref, false);
18633 return die;
18636 /* Generate a DIE to represent a declared function (either file-scope or
18637 block-local). */
18639 static void
18640 gen_subprogram_die (tree decl, dw_die_ref context_die)
18642 tree origin = decl_ultimate_origin (decl);
18643 dw_die_ref subr_die;
18644 dw_die_ref old_die = lookup_decl_die (decl);
18646 /* This function gets called multiple times for different stages of
18647 the debug process. For example, for func() in this code:
18649 namespace S
18651 void func() { ... }
18654 ...we get called 4 times. Twice in early debug and twice in
18655 late debug:
18657 Early debug
18658 -----------
18660 1. Once while generating func() within the namespace. This is
18661 the declaration. The declaration bit below is set, as the
18662 context is the namespace.
18664 A new DIE will be generated with DW_AT_declaration set.
18666 2. Once for func() itself. This is the specification. The
18667 declaration bit below is clear as the context is the CU.
18669 We will use the cached DIE from (1) to create a new DIE with
18670 DW_AT_specification pointing to the declaration in (1).
18672 Late debug via rest_of_handle_final()
18673 -------------------------------------
18675 3. Once generating func() within the namespace. This is also the
18676 declaration, as in (1), but this time we will early exit below
18677 as we have a cached DIE and a declaration needs no additional
18678 annotations (no locations), as the source declaration line
18679 info is enough.
18681 4. Once for func() itself. As in (2), this is the specification,
18682 but this time we will re-use the cached DIE, and just annotate
18683 it with the location information that should now be available.
18685 For something without namespaces, but with abstract instances, we
18686 are also called a multiple times:
18688 class Base
18690 public:
18691 Base (); // constructor declaration (1)
18694 Base::Base () { } // constructor specification (2)
18696 Early debug
18697 -----------
18699 1. Once for the Base() constructor by virtue of it being a
18700 member of the Base class. This is done via
18701 rest_of_type_compilation.
18703 This is a declaration, so a new DIE will be created with
18704 DW_AT_declaration.
18706 2. Once for the Base() constructor definition, but this time
18707 while generating the abstract instance of the base
18708 constructor (__base_ctor) which is being generated via early
18709 debug of reachable functions.
18711 Even though we have a cached version of the declaration (1),
18712 we will create a DW_AT_specification of the declaration DIE
18713 in (1).
18715 3. Once for the __base_ctor itself, but this time, we generate
18716 an DW_AT_abstract_origin version of the DW_AT_specification in
18717 (2).
18719 Late debug via rest_of_handle_final
18720 -----------------------------------
18722 4. One final time for the __base_ctor (which will have a cached
18723 DIE with DW_AT_abstract_origin created in (3). This time,
18724 we will just annotate the location information now
18725 available.
18727 int declaration = (current_function_decl != decl
18728 || class_or_namespace_scope_p (context_die));
18730 premark_used_types (DECL_STRUCT_FUNCTION (decl));
18732 /* Now that the C++ front end lazily declares artificial member fns, we
18733 might need to retrofit the declaration into its class. */
18734 if (!declaration && !origin && !old_die
18735 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
18736 && !class_or_namespace_scope_p (context_die)
18737 && debug_info_level > DINFO_LEVEL_TERSE)
18738 old_die = force_decl_die (decl);
18740 /* An inlined instance, tag a new DIE with DW_AT_abstract_origin. */
18741 if (origin != NULL)
18743 gcc_assert (!declaration || local_scope_p (context_die));
18745 /* Fixup die_parent for the abstract instance of a nested
18746 inline function. */
18747 if (old_die && old_die->die_parent == NULL)
18748 add_child_die (context_die, old_die);
18750 if (old_die && get_AT_ref (old_die, DW_AT_abstract_origin))
18752 /* If we have a DW_AT_abstract_origin we have a working
18753 cached version. */
18754 subr_die = old_die;
18756 else
18758 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18759 add_abstract_origin_attribute (subr_die, origin);
18760 /* This is where the actual code for a cloned function is.
18761 Let's emit linkage name attribute for it. This helps
18762 debuggers to e.g, set breakpoints into
18763 constructors/destructors when the user asks "break
18764 K::K". */
18765 add_linkage_name (subr_die, decl);
18768 /* A cached copy, possibly from early dwarf generation. Reuse as
18769 much as possible. */
18770 else if (old_die)
18772 /* A declaration that has been previously dumped needs no
18773 additional information. */
18774 if (declaration)
18775 return;
18777 if (!get_AT_flag (old_die, DW_AT_declaration)
18778 /* We can have a normal definition following an inline one in the
18779 case of redefinition of GNU C extern inlines.
18780 It seems reasonable to use AT_specification in this case. */
18781 && !get_AT (old_die, DW_AT_inline))
18783 /* Detect and ignore this case, where we are trying to output
18784 something we have already output. */
18785 if (get_AT (old_die, DW_AT_low_pc)
18786 || get_AT (old_die, DW_AT_ranges))
18787 return;
18789 /* If we have no location information, this must be a
18790 partially generated DIE from early dwarf generation.
18791 Fall through and generate it. */
18794 /* If the definition comes from the same place as the declaration,
18795 maybe use the old DIE. We always want the DIE for this function
18796 that has the *_pc attributes to be under comp_unit_die so the
18797 debugger can find it. We also need to do this for abstract
18798 instances of inlines, since the spec requires the out-of-line copy
18799 to have the same parent. For local class methods, this doesn't
18800 apply; we just use the old DIE. */
18801 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18802 struct dwarf_file_data * file_index = lookup_filename (s.file);
18803 if ((is_cu_die (old_die->die_parent)
18804 /* This condition fixes the inconsistency/ICE with the
18805 following Fortran test (or some derivative thereof) while
18806 building libgfortran:
18808 module some_m
18809 contains
18810 logical function funky (FLAG)
18811 funky = .true.
18812 end function
18813 end module
18815 || (old_die->die_parent
18816 && old_die->die_parent->die_tag == DW_TAG_module)
18817 || context_die == NULL)
18818 && (DECL_ARTIFICIAL (decl)
18819 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
18820 && (get_AT_unsigned (old_die, DW_AT_decl_line)
18821 == (unsigned) s.line))))
18823 subr_die = old_die;
18825 /* Clear out the declaration attribute, but leave the
18826 parameters so they can be augmented with location
18827 information later. Unless this was a declaration, in
18828 which case, wipe out the nameless parameters and recreate
18829 them further down. */
18830 if (remove_AT (subr_die, DW_AT_declaration))
18833 remove_AT (subr_die, DW_AT_object_pointer);
18834 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
18837 /* Make a specification pointing to the previously built
18838 declaration. */
18839 else
18841 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18842 add_AT_specification (subr_die, old_die);
18843 add_pubname (decl, subr_die);
18844 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18845 add_AT_file (subr_die, DW_AT_decl_file, file_index);
18846 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18847 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
18849 /* If the prototype had an 'auto' or 'decltype(auto)' return type,
18850 emit the real type on the definition die. */
18851 if (is_cxx() && debug_info_level > DINFO_LEVEL_TERSE)
18853 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
18854 if (die == auto_die || die == decltype_auto_die)
18855 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18856 TYPE_UNQUALIFIED, context_die);
18860 /* Create a fresh DIE for anything else. */
18861 else
18863 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18865 if (TREE_PUBLIC (decl))
18866 add_AT_flag (subr_die, DW_AT_external, 1);
18868 add_name_and_src_coords_attributes (subr_die, decl);
18869 add_pubname (decl, subr_die);
18870 if (debug_info_level > DINFO_LEVEL_TERSE)
18872 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
18873 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18874 TYPE_UNQUALIFIED, context_die);
18877 add_pure_or_virtual_attribute (subr_die, decl);
18878 if (DECL_ARTIFICIAL (decl))
18879 add_AT_flag (subr_die, DW_AT_artificial, 1);
18881 if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
18882 add_AT_flag (subr_die, DW_AT_noreturn, 1);
18884 add_accessibility_attribute (subr_die, decl);
18887 /* Unless we have an existing non-declaration DIE, equate the new
18888 DIE. */
18889 if (!old_die || is_declaration_die (old_die))
18890 equate_decl_number_to_die (decl, subr_die);
18892 if (declaration)
18894 if (!old_die || !get_AT (old_die, DW_AT_inline))
18896 add_AT_flag (subr_die, DW_AT_declaration, 1);
18898 /* If this is an explicit function declaration then generate
18899 a DW_AT_explicit attribute. */
18900 if (lang_hooks.decls.function_decl_explicit_p (decl)
18901 && (dwarf_version >= 3 || !dwarf_strict))
18902 add_AT_flag (subr_die, DW_AT_explicit, 1);
18904 /* If this is a C++11 deleted special function member then generate
18905 a DW_AT_GNU_deleted attribute. */
18906 if (lang_hooks.decls.function_decl_deleted_p (decl)
18907 && (! dwarf_strict))
18908 add_AT_flag (subr_die, DW_AT_GNU_deleted, 1);
18911 /* Tag abstract instances with DW_AT_inline. */
18912 else if (DECL_ABSTRACT_P (decl))
18914 if (DECL_DECLARED_INLINE_P (decl))
18916 if (cgraph_function_possibly_inlined_p (decl))
18917 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
18918 else
18919 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
18921 else
18923 if (cgraph_function_possibly_inlined_p (decl))
18924 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
18925 else
18926 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
18929 if (DECL_DECLARED_INLINE_P (decl)
18930 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
18931 add_AT_flag (subr_die, DW_AT_artificial, 1);
18933 /* For non DECL_EXTERNALs, if range information is available, fill
18934 the DIE with it. */
18935 else if (!DECL_EXTERNAL (decl) && !early_dwarf)
18937 HOST_WIDE_INT cfa_fb_offset;
18939 struct function *fun = DECL_STRUCT_FUNCTION (decl);
18941 if (!flag_reorder_blocks_and_partition)
18943 dw_fde_ref fde = fun->fde;
18944 if (fde->dw_fde_begin)
18946 /* We have already generated the labels. */
18947 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18948 fde->dw_fde_end, false);
18950 else
18952 /* Create start/end labels and add the range. */
18953 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
18954 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
18955 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
18956 current_function_funcdef_no);
18957 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
18958 current_function_funcdef_no);
18959 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
18960 false);
18963 #if VMS_DEBUGGING_INFO
18964 /* HP OpenVMS Industry Standard 64: DWARF Extensions
18965 Section 2.3 Prologue and Epilogue Attributes:
18966 When a breakpoint is set on entry to a function, it is generally
18967 desirable for execution to be suspended, not on the very first
18968 instruction of the function, but rather at a point after the
18969 function's frame has been set up, after any language defined local
18970 declaration processing has been completed, and before execution of
18971 the first statement of the function begins. Debuggers generally
18972 cannot properly determine where this point is. Similarly for a
18973 breakpoint set on exit from a function. The prologue and epilogue
18974 attributes allow a compiler to communicate the location(s) to use. */
18977 if (fde->dw_fde_vms_end_prologue)
18978 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
18979 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
18981 if (fde->dw_fde_vms_begin_epilogue)
18982 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
18983 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
18985 #endif
18988 else
18990 /* Generate pubnames entries for the split function code ranges. */
18991 dw_fde_ref fde = fun->fde;
18993 if (fde->dw_fde_second_begin)
18995 if (dwarf_version >= 3 || !dwarf_strict)
18997 /* We should use ranges for non-contiguous code section
18998 addresses. Use the actual code range for the initial
18999 section, since the HOT/COLD labels might precede an
19000 alignment offset. */
19001 bool range_list_added = false;
19002 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
19003 fde->dw_fde_end, &range_list_added,
19004 false);
19005 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
19006 fde->dw_fde_second_end,
19007 &range_list_added, false);
19008 if (range_list_added)
19009 add_ranges (NULL);
19011 else
19013 /* There is no real support in DW2 for this .. so we make
19014 a work-around. First, emit the pub name for the segment
19015 containing the function label. Then make and emit a
19016 simplified subprogram DIE for the second segment with the
19017 name pre-fixed by __hot/cold_sect_of_. We use the same
19018 linkage name for the second die so that gdb will find both
19019 sections when given "b foo". */
19020 const char *name = NULL;
19021 tree decl_name = DECL_NAME (decl);
19022 dw_die_ref seg_die;
19024 /* Do the 'primary' section. */
19025 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
19026 fde->dw_fde_end, false);
19028 /* Build a minimal DIE for the secondary section. */
19029 seg_die = new_die (DW_TAG_subprogram,
19030 subr_die->die_parent, decl);
19032 if (TREE_PUBLIC (decl))
19033 add_AT_flag (seg_die, DW_AT_external, 1);
19035 if (decl_name != NULL
19036 && IDENTIFIER_POINTER (decl_name) != NULL)
19038 name = dwarf2_name (decl, 1);
19039 if (! DECL_ARTIFICIAL (decl))
19040 add_src_coords_attributes (seg_die, decl);
19042 add_linkage_name (seg_die, decl);
19044 gcc_assert (name != NULL);
19045 add_pure_or_virtual_attribute (seg_die, decl);
19046 if (DECL_ARTIFICIAL (decl))
19047 add_AT_flag (seg_die, DW_AT_artificial, 1);
19049 name = concat ("__second_sect_of_", name, NULL);
19050 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
19051 fde->dw_fde_second_end, false);
19052 add_name_attribute (seg_die, name);
19053 if (want_pubnames ())
19054 add_pubname_string (name, seg_die);
19057 else
19058 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
19059 false);
19062 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
19064 /* We define the "frame base" as the function's CFA. This is more
19065 convenient for several reasons: (1) It's stable across the prologue
19066 and epilogue, which makes it better than just a frame pointer,
19067 (2) With dwarf3, there exists a one-byte encoding that allows us
19068 to reference the .debug_frame data by proxy, but failing that,
19069 (3) We can at least reuse the code inspection and interpretation
19070 code that determines the CFA position at various points in the
19071 function. */
19072 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
19074 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
19075 add_AT_loc (subr_die, DW_AT_frame_base, op);
19077 else
19079 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
19080 if (list->dw_loc_next)
19081 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
19082 else
19083 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
19086 /* Compute a displacement from the "steady-state frame pointer" to
19087 the CFA. The former is what all stack slots and argument slots
19088 will reference in the rtl; the latter is what we've told the
19089 debugger about. We'll need to adjust all frame_base references
19090 by this displacement. */
19091 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
19093 if (fun->static_chain_decl)
19094 add_AT_location_description
19095 (subr_die, DW_AT_static_link,
19096 loc_list_from_tree (fun->static_chain_decl, 2, NULL));
19099 /* Generate child dies for template paramaters. */
19100 if (early_dwarf && debug_info_level > DINFO_LEVEL_TERSE)
19101 gen_generic_params_dies (decl);
19103 /* Now output descriptions of the arguments for this function. This gets
19104 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
19105 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
19106 `...' at the end of the formal parameter list. In order to find out if
19107 there was a trailing ellipsis or not, we must instead look at the type
19108 associated with the FUNCTION_DECL. This will be a node of type
19109 FUNCTION_TYPE. If the chain of type nodes hanging off of this
19110 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
19111 an ellipsis at the end. */
19113 /* In the case where we are describing a mere function declaration, all we
19114 need to do here (and all we *can* do here) is to describe the *types* of
19115 its formal parameters. */
19116 if (debug_info_level <= DINFO_LEVEL_TERSE)
19118 else if (declaration)
19119 gen_formal_types_die (decl, subr_die);
19120 else
19122 /* Generate DIEs to represent all known formal parameters. */
19123 tree parm = DECL_ARGUMENTS (decl);
19124 tree generic_decl = early_dwarf
19125 ? lang_hooks.decls.get_generic_function_decl (decl) : NULL;
19126 tree generic_decl_parm = generic_decl
19127 ? DECL_ARGUMENTS (generic_decl)
19128 : NULL;
19130 /* Now we want to walk the list of parameters of the function and
19131 emit their relevant DIEs.
19133 We consider the case of DECL being an instance of a generic function
19134 as well as it being a normal function.
19136 If DECL is an instance of a generic function we walk the
19137 parameters of the generic function declaration _and_ the parameters of
19138 DECL itself. This is useful because we want to emit specific DIEs for
19139 function parameter packs and those are declared as part of the
19140 generic function declaration. In that particular case,
19141 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
19142 That DIE has children DIEs representing the set of arguments
19143 of the pack. Note that the set of pack arguments can be empty.
19144 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
19145 children DIE.
19147 Otherwise, we just consider the parameters of DECL. */
19148 while (generic_decl_parm || parm)
19150 if (generic_decl_parm
19151 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
19152 gen_formal_parameter_pack_die (generic_decl_parm,
19153 parm, subr_die,
19154 &parm);
19155 else if (parm && !POINTER_BOUNDS_P (parm))
19157 dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
19159 if (parm == DECL_ARGUMENTS (decl)
19160 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
19161 && parm_die
19162 && (dwarf_version >= 3 || !dwarf_strict))
19163 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
19165 parm = DECL_CHAIN (parm);
19167 else if (parm)
19168 parm = DECL_CHAIN (parm);
19170 if (generic_decl_parm)
19171 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
19174 /* Decide whether we need an unspecified_parameters DIE at the end.
19175 There are 2 more cases to do this for: 1) the ansi ... declaration -
19176 this is detectable when the end of the arg list is not a
19177 void_type_node 2) an unprototyped function declaration (not a
19178 definition). This just means that we have no info about the
19179 parameters at all. */
19180 if (prototype_p (TREE_TYPE (decl)))
19182 /* This is the prototyped case, check for.... */
19183 if (stdarg_p (TREE_TYPE (decl)))
19184 gen_unspecified_parameters_die (decl, subr_die);
19186 else if (DECL_INITIAL (decl) == NULL_TREE)
19187 gen_unspecified_parameters_die (decl, subr_die);
19190 if (subr_die != old_die)
19191 /* Add the calling convention attribute if requested. */
19192 add_calling_convention_attribute (subr_die, decl);
19194 /* Output Dwarf info for all of the stuff within the body of the function
19195 (if it has one - it may be just a declaration).
19197 OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
19198 a function. This BLOCK actually represents the outermost binding contour
19199 for the function, i.e. the contour in which the function's formal
19200 parameters and labels get declared. Curiously, it appears that the front
19201 end doesn't actually put the PARM_DECL nodes for the current function onto
19202 the BLOCK_VARS list for this outer scope, but are strung off of the
19203 DECL_ARGUMENTS list for the function instead.
19205 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
19206 the LABEL_DECL nodes for the function however, and we output DWARF info
19207 for those in decls_for_scope. Just within the `outer_scope' there will be
19208 a BLOCK node representing the function's outermost pair of curly braces,
19209 and any blocks used for the base and member initializers of a C++
19210 constructor function. */
19211 tree outer_scope = DECL_INITIAL (decl);
19212 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
19214 int call_site_note_count = 0;
19215 int tail_call_site_note_count = 0;
19217 /* Emit a DW_TAG_variable DIE for a named return value. */
19218 if (DECL_NAME (DECL_RESULT (decl)))
19219 gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
19221 /* The first time through decls_for_scope we will generate the
19222 DIEs for the locals. The second time, we fill in the
19223 location info. */
19224 decls_for_scope (outer_scope, subr_die);
19226 if (call_arg_locations && !dwarf_strict)
19228 struct call_arg_loc_node *ca_loc;
19229 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
19231 dw_die_ref die = NULL;
19232 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
19233 rtx arg, next_arg;
19235 for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
19236 arg; arg = next_arg)
19238 dw_loc_descr_ref reg, val;
19239 machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
19240 dw_die_ref cdie, tdie = NULL;
19242 next_arg = XEXP (arg, 1);
19243 if (REG_P (XEXP (XEXP (arg, 0), 0))
19244 && next_arg
19245 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
19246 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
19247 && REGNO (XEXP (XEXP (arg, 0), 0))
19248 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
19249 next_arg = XEXP (next_arg, 1);
19250 if (mode == VOIDmode)
19252 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
19253 if (mode == VOIDmode)
19254 mode = GET_MODE (XEXP (arg, 0));
19256 if (mode == VOIDmode || mode == BLKmode)
19257 continue;
19258 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
19260 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
19261 tloc = XEXP (XEXP (arg, 0), 1);
19262 continue;
19264 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
19265 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
19267 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
19268 tlocc = XEXP (XEXP (arg, 0), 1);
19269 continue;
19271 reg = NULL;
19272 if (REG_P (XEXP (XEXP (arg, 0), 0)))
19273 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
19274 VAR_INIT_STATUS_INITIALIZED);
19275 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
19277 rtx mem = XEXP (XEXP (arg, 0), 0);
19278 reg = mem_loc_descriptor (XEXP (mem, 0),
19279 get_address_mode (mem),
19280 GET_MODE (mem),
19281 VAR_INIT_STATUS_INITIALIZED);
19283 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
19284 == DEBUG_PARAMETER_REF)
19286 tree tdecl
19287 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
19288 tdie = lookup_decl_die (tdecl);
19289 if (tdie == NULL)
19290 continue;
19292 else
19293 continue;
19294 if (reg == NULL
19295 && GET_CODE (XEXP (XEXP (arg, 0), 0))
19296 != DEBUG_PARAMETER_REF)
19297 continue;
19298 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
19299 VOIDmode,
19300 VAR_INIT_STATUS_INITIALIZED);
19301 if (val == NULL)
19302 continue;
19303 if (die == NULL)
19304 die = gen_call_site_die (decl, subr_die, ca_loc);
19305 cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
19306 NULL_TREE);
19307 if (reg != NULL)
19308 add_AT_loc (cdie, DW_AT_location, reg);
19309 else if (tdie != NULL)
19310 add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
19311 add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
19312 if (next_arg != XEXP (arg, 1))
19314 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
19315 if (mode == VOIDmode)
19316 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
19317 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
19318 0), 1),
19319 mode, VOIDmode,
19320 VAR_INIT_STATUS_INITIALIZED);
19321 if (val != NULL)
19322 add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
19325 if (die == NULL
19326 && (ca_loc->symbol_ref || tloc))
19327 die = gen_call_site_die (decl, subr_die, ca_loc);
19328 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
19330 dw_loc_descr_ref tval = NULL;
19332 if (tloc != NULL_RTX)
19333 tval = mem_loc_descriptor (tloc,
19334 GET_MODE (tloc) == VOIDmode
19335 ? Pmode : GET_MODE (tloc),
19336 VOIDmode,
19337 VAR_INIT_STATUS_INITIALIZED);
19338 if (tval)
19339 add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
19340 else if (tlocc != NULL_RTX)
19342 tval = mem_loc_descriptor (tlocc,
19343 GET_MODE (tlocc) == VOIDmode
19344 ? Pmode : GET_MODE (tlocc),
19345 VOIDmode,
19346 VAR_INIT_STATUS_INITIALIZED);
19347 if (tval)
19348 add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
19349 tval);
19352 if (die != NULL)
19354 call_site_note_count++;
19355 if (ca_loc->tail_call_p)
19356 tail_call_site_note_count++;
19360 call_arg_locations = NULL;
19361 call_arg_loc_last = NULL;
19362 if (tail_call_site_count >= 0
19363 && tail_call_site_count == tail_call_site_note_count
19364 && !dwarf_strict)
19366 if (call_site_count >= 0
19367 && call_site_count == call_site_note_count)
19368 add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
19369 else
19370 add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
19372 call_site_count = -1;
19373 tail_call_site_count = -1;
19377 /* Returns a hash value for X (which really is a die_struct). */
19379 hashval_t
19380 block_die_hasher::hash (die_struct *d)
19382 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
19385 /* Return nonzero if decl_id and die_parent of die_struct X is the same
19386 as decl_id and die_parent of die_struct Y. */
19388 bool
19389 block_die_hasher::equal (die_struct *x, die_struct *y)
19391 return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
19394 /* Return TRUE if DECL, which may have been previously generated as
19395 OLD_DIE, is a candidate for a DW_AT_specification. DECLARATION is
19396 true if decl (or its origin) is either an extern declaration or a
19397 class/namespace scoped declaration.
19399 The declare_in_namespace support causes us to get two DIEs for one
19400 variable, both of which are declarations. We want to avoid
19401 considering one to be a specification, so we must test for
19402 DECLARATION and DW_AT_declaration. */
19403 static inline bool
19404 decl_will_get_specification_p (dw_die_ref old_die, tree decl, bool declaration)
19406 return (old_die && TREE_STATIC (decl) && !declaration
19407 && get_AT_flag (old_die, DW_AT_declaration) == 1);
19410 /* Return true if DECL is a local static. */
19412 static inline bool
19413 local_function_static (tree decl)
19415 gcc_assert (TREE_CODE (decl) == VAR_DECL);
19416 return TREE_STATIC (decl)
19417 && DECL_CONTEXT (decl)
19418 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL;
19421 /* Generate a DIE to represent a declared data object.
19422 Either DECL or ORIGIN must be non-null. */
19424 static void
19425 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
19427 HOST_WIDE_INT off = 0;
19428 tree com_decl;
19429 tree decl_or_origin = decl ? decl : origin;
19430 tree ultimate_origin;
19431 dw_die_ref var_die;
19432 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
19433 dw_die_ref origin_die = NULL;
19434 bool declaration = (DECL_EXTERNAL (decl_or_origin)
19435 || class_or_namespace_scope_p (context_die));
19436 bool specialization_p = false;
19438 ultimate_origin = decl_ultimate_origin (decl_or_origin);
19439 if (decl || ultimate_origin)
19440 origin = ultimate_origin;
19441 com_decl = fortran_common (decl_or_origin, &off);
19443 /* Symbol in common gets emitted as a child of the common block, in the form
19444 of a data member. */
19445 if (com_decl)
19447 dw_die_ref com_die;
19448 dw_loc_list_ref loc;
19449 die_node com_die_arg;
19451 var_die = lookup_decl_die (decl_or_origin);
19452 if (var_die)
19454 if (get_AT (var_die, DW_AT_location) == NULL)
19456 loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
19457 if (loc)
19459 if (off)
19461 /* Optimize the common case. */
19462 if (single_element_loc_list_p (loc)
19463 && loc->expr->dw_loc_opc == DW_OP_addr
19464 && loc->expr->dw_loc_next == NULL
19465 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
19466 == SYMBOL_REF)
19468 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
19469 loc->expr->dw_loc_oprnd1.v.val_addr
19470 = plus_constant (GET_MODE (x), x , off);
19472 else
19473 loc_list_plus_const (loc, off);
19475 add_AT_location_description (var_die, DW_AT_location, loc);
19476 remove_AT (var_die, DW_AT_declaration);
19479 return;
19482 if (common_block_die_table == NULL)
19483 common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
19485 com_die_arg.decl_id = DECL_UID (com_decl);
19486 com_die_arg.die_parent = context_die;
19487 com_die = common_block_die_table->find (&com_die_arg);
19488 loc = loc_list_from_tree (com_decl, 2, NULL);
19489 if (com_die == NULL)
19491 const char *cnam
19492 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
19493 die_node **slot;
19495 com_die = new_die (DW_TAG_common_block, context_die, decl);
19496 add_name_and_src_coords_attributes (com_die, com_decl);
19497 if (loc)
19499 add_AT_location_description (com_die, DW_AT_location, loc);
19500 /* Avoid sharing the same loc descriptor between
19501 DW_TAG_common_block and DW_TAG_variable. */
19502 loc = loc_list_from_tree (com_decl, 2, NULL);
19504 else if (DECL_EXTERNAL (decl))
19505 add_AT_flag (com_die, DW_AT_declaration, 1);
19506 if (want_pubnames ())
19507 add_pubname_string (cnam, com_die); /* ??? needed? */
19508 com_die->decl_id = DECL_UID (com_decl);
19509 slot = common_block_die_table->find_slot (com_die, INSERT);
19510 *slot = com_die;
19512 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
19514 add_AT_location_description (com_die, DW_AT_location, loc);
19515 loc = loc_list_from_tree (com_decl, 2, NULL);
19516 remove_AT (com_die, DW_AT_declaration);
19518 var_die = new_die (DW_TAG_variable, com_die, decl);
19519 add_name_and_src_coords_attributes (var_die, decl);
19520 add_type_attribute (var_die, TREE_TYPE (decl), decl_quals (decl),
19521 context_die);
19522 add_AT_flag (var_die, DW_AT_external, 1);
19523 if (loc)
19525 if (off)
19527 /* Optimize the common case. */
19528 if (single_element_loc_list_p (loc)
19529 && loc->expr->dw_loc_opc == DW_OP_addr
19530 && loc->expr->dw_loc_next == NULL
19531 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
19533 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
19534 loc->expr->dw_loc_oprnd1.v.val_addr
19535 = plus_constant (GET_MODE (x), x, off);
19537 else
19538 loc_list_plus_const (loc, off);
19540 add_AT_location_description (var_die, DW_AT_location, loc);
19542 else if (DECL_EXTERNAL (decl))
19543 add_AT_flag (var_die, DW_AT_declaration, 1);
19544 equate_decl_number_to_die (decl, var_die);
19545 return;
19548 if (old_die)
19550 if (declaration)
19552 /* A declaration that has been previously dumped, needs no
19553 further annotations, since it doesn't need location on
19554 the second pass. */
19555 return;
19557 else if (decl_will_get_specification_p (old_die, decl, declaration)
19558 && !get_AT (old_die, DW_AT_specification))
19560 /* Fall-thru so we can make a new variable die along with a
19561 DW_AT_specification. */
19563 else if (origin && old_die->die_parent != context_die)
19565 /* If we will be creating an inlined instance, we need a
19566 new DIE that will get annotated with
19567 DW_AT_abstract_origin. Clear things so we can get a
19568 new DIE. */
19569 gcc_assert (!DECL_ABSTRACT_P (decl));
19570 old_die = NULL;
19572 else
19574 /* If a DIE was dumped early, it still needs location info.
19575 Skip to where we fill the location bits. */
19576 var_die = old_die;
19577 goto gen_variable_die_location;
19581 /* For static data members, the declaration in the class is supposed
19582 to have DW_TAG_member tag; the specification should still be
19583 DW_TAG_variable referencing the DW_TAG_member DIE. */
19584 if (declaration && class_scope_p (context_die))
19585 var_die = new_die (DW_TAG_member, context_die, decl);
19586 else
19587 var_die = new_die (DW_TAG_variable, context_die, decl);
19589 if (origin != NULL)
19590 origin_die = add_abstract_origin_attribute (var_die, origin);
19592 /* Loop unrolling can create multiple blocks that refer to the same
19593 static variable, so we must test for the DW_AT_declaration flag.
19595 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
19596 copy decls and set the DECL_ABSTRACT_P flag on them instead of
19597 sharing them.
19599 ??? Duplicated blocks have been rewritten to use .debug_ranges. */
19600 else if (decl_will_get_specification_p (old_die, decl, declaration))
19602 /* This is a definition of a C++ class level static. */
19603 add_AT_specification (var_die, old_die);
19604 specialization_p = true;
19605 if (DECL_NAME (decl))
19607 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
19608 struct dwarf_file_data * file_index = lookup_filename (s.file);
19610 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
19611 add_AT_file (var_die, DW_AT_decl_file, file_index);
19613 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
19614 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
19616 if (old_die->die_tag == DW_TAG_member)
19617 add_linkage_name (var_die, decl);
19620 else
19621 add_name_and_src_coords_attributes (var_die, decl);
19623 if ((origin == NULL && !specialization_p)
19624 || (origin != NULL
19625 && !DECL_ABSTRACT_P (decl_or_origin)
19626 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
19627 decl_function_context
19628 (decl_or_origin))))
19630 tree type = TREE_TYPE (decl_or_origin);
19632 if (decl_by_reference_p (decl_or_origin))
19633 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19634 context_die);
19635 else
19636 add_type_attribute (var_die, type, decl_quals (decl_or_origin),
19637 context_die);
19640 if (origin == NULL && !specialization_p)
19642 if (TREE_PUBLIC (decl))
19643 add_AT_flag (var_die, DW_AT_external, 1);
19645 if (DECL_ARTIFICIAL (decl))
19646 add_AT_flag (var_die, DW_AT_artificial, 1);
19648 add_accessibility_attribute (var_die, decl);
19651 if (declaration)
19652 add_AT_flag (var_die, DW_AT_declaration, 1);
19654 if (decl && (DECL_ABSTRACT_P (decl)
19655 || !old_die || is_declaration_die (old_die)))
19656 equate_decl_number_to_die (decl, var_die);
19658 gen_variable_die_location:
19659 if (! declaration
19660 && (! DECL_ABSTRACT_P (decl_or_origin)
19661 /* Local static vars are shared between all clones/inlines,
19662 so emit DW_AT_location on the abstract DIE if DECL_RTL is
19663 already set. */
19664 || (TREE_CODE (decl_or_origin) == VAR_DECL
19665 && TREE_STATIC (decl_or_origin)
19666 && DECL_RTL_SET_P (decl_or_origin)))
19667 /* When abstract origin already has DW_AT_location attribute, no need
19668 to add it again. */
19669 && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
19671 if (early_dwarf)
19672 add_pubname (decl_or_origin, var_die);
19673 else
19674 add_location_or_const_value_attribute (var_die, decl_or_origin,
19675 decl == NULL, DW_AT_location);
19677 else
19678 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
19681 /* Generate a DIE to represent a named constant. */
19683 static void
19684 gen_const_die (tree decl, dw_die_ref context_die)
19686 dw_die_ref const_die;
19687 tree type = TREE_TYPE (decl);
19689 const_die = lookup_decl_die (decl);
19690 if (const_die)
19691 return;
19693 const_die = new_die (DW_TAG_constant, context_die, decl);
19694 equate_decl_number_to_die (decl, const_die);
19695 add_name_and_src_coords_attributes (const_die, decl);
19696 add_type_attribute (const_die, type, TYPE_QUAL_CONST, context_die);
19697 if (TREE_PUBLIC (decl))
19698 add_AT_flag (const_die, DW_AT_external, 1);
19699 if (DECL_ARTIFICIAL (decl))
19700 add_AT_flag (const_die, DW_AT_artificial, 1);
19701 tree_add_const_value_attribute_for_decl (const_die, decl);
19704 /* Generate a DIE to represent a label identifier. */
19706 static void
19707 gen_label_die (tree decl, dw_die_ref context_die)
19709 tree origin = decl_ultimate_origin (decl);
19710 dw_die_ref lbl_die = lookup_decl_die (decl);
19711 rtx insn;
19712 char label[MAX_ARTIFICIAL_LABEL_BYTES];
19714 if (!lbl_die)
19716 lbl_die = new_die (DW_TAG_label, context_die, decl);
19717 equate_decl_number_to_die (decl, lbl_die);
19719 if (origin != NULL)
19720 add_abstract_origin_attribute (lbl_die, origin);
19721 else
19722 add_name_and_src_coords_attributes (lbl_die, decl);
19725 if (DECL_ABSTRACT_P (decl))
19726 equate_decl_number_to_die (decl, lbl_die);
19727 else
19729 insn = DECL_RTL_IF_SET (decl);
19731 /* Deleted labels are programmer specified labels which have been
19732 eliminated because of various optimizations. We still emit them
19733 here so that it is possible to put breakpoints on them. */
19734 if (insn
19735 && (LABEL_P (insn)
19736 || ((NOTE_P (insn)
19737 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
19739 /* When optimization is enabled (via -O) some parts of the compiler
19740 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
19741 represent source-level labels which were explicitly declared by
19742 the user. This really shouldn't be happening though, so catch
19743 it if it ever does happen. */
19744 gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
19746 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
19747 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19749 else if (insn
19750 && NOTE_P (insn)
19751 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
19752 && CODE_LABEL_NUMBER (insn) != -1)
19754 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
19755 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19760 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
19761 attributes to the DIE for a block STMT, to describe where the inlined
19762 function was called from. This is similar to add_src_coords_attributes. */
19764 static inline void
19765 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
19767 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
19769 if (dwarf_version >= 3 || !dwarf_strict)
19771 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
19772 add_AT_unsigned (die, DW_AT_call_line, s.line);
19777 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
19778 Add low_pc and high_pc attributes to the DIE for a block STMT. */
19780 static inline void
19781 add_high_low_attributes (tree stmt, dw_die_ref die)
19783 char label[MAX_ARTIFICIAL_LABEL_BYTES];
19785 if (BLOCK_FRAGMENT_CHAIN (stmt)
19786 && (dwarf_version >= 3 || !dwarf_strict))
19788 tree chain, superblock = NULL_TREE;
19789 dw_die_ref pdie;
19790 dw_attr_ref attr = NULL;
19792 if (inlined_function_outer_scope_p (stmt))
19794 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19795 BLOCK_NUMBER (stmt));
19796 add_AT_lbl_id (die, DW_AT_entry_pc, label);
19799 /* Optimize duplicate .debug_ranges lists or even tails of
19800 lists. If this BLOCK has same ranges as its supercontext,
19801 lookup DW_AT_ranges attribute in the supercontext (and
19802 recursively so), verify that the ranges_table contains the
19803 right values and use it instead of adding a new .debug_range. */
19804 for (chain = stmt, pdie = die;
19805 BLOCK_SAME_RANGE (chain);
19806 chain = BLOCK_SUPERCONTEXT (chain))
19808 dw_attr_ref new_attr;
19810 pdie = pdie->die_parent;
19811 if (pdie == NULL)
19812 break;
19813 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
19814 break;
19815 new_attr = get_AT (pdie, DW_AT_ranges);
19816 if (new_attr == NULL
19817 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
19818 break;
19819 attr = new_attr;
19820 superblock = BLOCK_SUPERCONTEXT (chain);
19822 if (attr != NULL
19823 && (ranges_table[attr->dw_attr_val.v.val_offset
19824 / 2 / DWARF2_ADDR_SIZE].num
19825 == BLOCK_NUMBER (superblock))
19826 && BLOCK_FRAGMENT_CHAIN (superblock))
19828 unsigned long off = attr->dw_attr_val.v.val_offset
19829 / 2 / DWARF2_ADDR_SIZE;
19830 unsigned long supercnt = 0, thiscnt = 0;
19831 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
19832 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
19834 ++supercnt;
19835 gcc_checking_assert (ranges_table[off + supercnt].num
19836 == BLOCK_NUMBER (chain));
19838 gcc_checking_assert (ranges_table[off + supercnt + 1].num == 0);
19839 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
19840 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
19841 ++thiscnt;
19842 gcc_assert (supercnt >= thiscnt);
19843 add_AT_range_list (die, DW_AT_ranges,
19844 ((off + supercnt - thiscnt)
19845 * 2 * DWARF2_ADDR_SIZE),
19846 false);
19847 return;
19850 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt), false);
19852 chain = BLOCK_FRAGMENT_CHAIN (stmt);
19855 add_ranges (chain);
19856 chain = BLOCK_FRAGMENT_CHAIN (chain);
19858 while (chain);
19859 add_ranges (NULL);
19861 else
19863 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
19864 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19865 BLOCK_NUMBER (stmt));
19866 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
19867 BLOCK_NUMBER (stmt));
19868 add_AT_low_high_pc (die, label, label_high, false);
19872 /* Generate a DIE for a lexical block. */
19874 static void
19875 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
19877 dw_die_ref old_die = BLOCK_DIE (stmt);
19878 dw_die_ref stmt_die = NULL;
19879 if (!old_die)
19881 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
19882 BLOCK_DIE (stmt) = stmt_die;
19885 if (BLOCK_ABSTRACT (stmt))
19887 if (old_die)
19889 #ifdef ENABLE_CHECKING
19890 /* This must have been generated early and it won't even
19891 need location information since it's a DW_AT_inline
19892 function. */
19893 for (dw_die_ref c = context_die; c; c = c->die_parent)
19894 if (c->die_tag == DW_TAG_inlined_subroutine
19895 || c->die_tag == DW_TAG_subprogram)
19897 gcc_assert (get_AT (c, DW_AT_inline));
19898 break;
19900 #endif
19901 return;
19904 else if (BLOCK_ABSTRACT_ORIGIN (stmt))
19906 /* If this is an inlined instance, create a new lexical die for
19907 anything below to attach DW_AT_abstract_origin to. */
19908 if (old_die)
19910 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
19911 BLOCK_DIE (stmt) = stmt_die;
19912 old_die = NULL;
19916 if (old_die)
19917 stmt_die = old_die;
19919 /* A non abstract block whose blocks have already been reordered
19920 should have the instruction range for this block. If so, set the
19921 high/low attributes. */
19922 if (!early_dwarf && !BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
19924 gcc_assert (stmt_die);
19925 add_high_low_attributes (stmt, stmt_die);
19928 decls_for_scope (stmt, stmt_die);
19931 /* Generate a DIE for an inlined subprogram. */
19933 static void
19934 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
19936 tree decl;
19938 /* The instance of function that is effectively being inlined shall not
19939 be abstract. */
19940 gcc_assert (! BLOCK_ABSTRACT (stmt));
19942 decl = block_ultimate_origin (stmt);
19944 /* Make sure any inlined functions are known to be inlineable. */
19945 gcc_checking_assert (DECL_ABSTRACT_P (decl)
19946 || cgraph_function_possibly_inlined_p (decl));
19948 /* Emit info for the abstract instance first, if we haven't yet. We
19949 must emit this even if the block is abstract, otherwise when we
19950 emit the block below (or elsewhere), we may end up trying to emit
19951 a die whose origin die hasn't been emitted, and crashing. */
19952 dwarf2out_abstract_function (decl);
19954 if (! BLOCK_ABSTRACT (stmt))
19956 dw_die_ref subr_die
19957 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
19959 if (call_arg_locations)
19960 BLOCK_DIE (stmt) = subr_die;
19961 add_abstract_origin_attribute (subr_die, decl);
19962 if (TREE_ASM_WRITTEN (stmt))
19963 add_high_low_attributes (stmt, subr_die);
19964 add_call_src_coords_attributes (stmt, subr_die);
19966 decls_for_scope (stmt, subr_die);
19970 /* Generate a DIE for a field in a record, or structure. */
19972 static void
19973 gen_field_die (tree decl, dw_die_ref context_die)
19975 dw_die_ref decl_die;
19977 if (TREE_TYPE (decl) == error_mark_node)
19978 return;
19980 decl_die = new_die (DW_TAG_member, context_die, decl);
19981 add_name_and_src_coords_attributes (decl_die, decl);
19982 add_type_attribute (decl_die, member_declared_type (decl),
19983 decl_quals (decl), context_die);
19985 if (DECL_BIT_FIELD_TYPE (decl))
19987 add_byte_size_attribute (decl_die, decl);
19988 add_bit_size_attribute (decl_die, decl);
19989 add_bit_offset_attribute (decl_die, decl);
19992 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
19993 add_data_member_location_attribute (decl_die, decl);
19995 if (DECL_ARTIFICIAL (decl))
19996 add_AT_flag (decl_die, DW_AT_artificial, 1);
19998 add_accessibility_attribute (decl_die, decl);
20000 /* Equate decl number to die, so that we can look up this decl later on. */
20001 equate_decl_number_to_die (decl, decl_die);
20004 #if 0
20005 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
20006 Use modified_type_die instead.
20007 We keep this code here just in case these types of DIEs may be needed to
20008 represent certain things in other languages (e.g. Pascal) someday. */
20010 static void
20011 gen_pointer_type_die (tree type, dw_die_ref context_die)
20013 dw_die_ref ptr_die
20014 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
20016 equate_type_number_to_die (type, ptr_die);
20017 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
20018 context_die);
20019 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
20022 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
20023 Use modified_type_die instead.
20024 We keep this code here just in case these types of DIEs may be needed to
20025 represent certain things in other languages (e.g. Pascal) someday. */
20027 static void
20028 gen_reference_type_die (tree type, dw_die_ref context_die)
20030 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
20032 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
20033 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
20034 else
20035 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
20037 equate_type_number_to_die (type, ref_die);
20038 add_type_attribute (ref_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
20039 context_die);
20040 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
20042 #endif
20044 /* Generate a DIE for a pointer to a member type. */
20046 static void
20047 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
20049 dw_die_ref ptr_die
20050 = new_die (DW_TAG_ptr_to_member_type,
20051 scope_die_for (type, context_die), type);
20053 equate_type_number_to_die (type, ptr_die);
20054 add_AT_die_ref (ptr_die, DW_AT_containing_type,
20055 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
20056 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
20057 context_die);
20060 typedef const char *dchar_p; /* For DEF_VEC_P. */
20062 static char *producer_string;
20064 /* Return a heap allocated producer string including command line options
20065 if -grecord-gcc-switches. */
20067 static char *
20068 gen_producer_string (void)
20070 size_t j;
20071 auto_vec<dchar_p> switches;
20072 const char *language_string = lang_hooks.name;
20073 char *producer, *tail;
20074 const char *p;
20075 size_t len = dwarf_record_gcc_switches ? 0 : 3;
20076 size_t plen = strlen (language_string) + 1 + strlen (version_string);
20078 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
20079 switch (save_decoded_options[j].opt_index)
20081 case OPT_o:
20082 case OPT_d:
20083 case OPT_dumpbase:
20084 case OPT_dumpdir:
20085 case OPT_auxbase:
20086 case OPT_auxbase_strip:
20087 case OPT_quiet:
20088 case OPT_version:
20089 case OPT_v:
20090 case OPT_w:
20091 case OPT_L:
20092 case OPT_D:
20093 case OPT_I:
20094 case OPT_U:
20095 case OPT_SPECIAL_unknown:
20096 case OPT_SPECIAL_ignore:
20097 case OPT_SPECIAL_program_name:
20098 case OPT_SPECIAL_input_file:
20099 case OPT_grecord_gcc_switches:
20100 case OPT_gno_record_gcc_switches:
20101 case OPT__output_pch_:
20102 case OPT_fdiagnostics_show_location_:
20103 case OPT_fdiagnostics_show_option:
20104 case OPT_fdiagnostics_show_caret:
20105 case OPT_fdiagnostics_color_:
20106 case OPT_fverbose_asm:
20107 case OPT____:
20108 case OPT__sysroot_:
20109 case OPT_nostdinc:
20110 case OPT_nostdinc__:
20111 case OPT_fpreprocessed:
20112 case OPT_fltrans_output_list_:
20113 case OPT_fresolution_:
20114 /* Ignore these. */
20115 continue;
20116 default:
20117 if (cl_options[save_decoded_options[j].opt_index].flags
20118 & CL_NO_DWARF_RECORD)
20119 continue;
20120 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
20121 == '-');
20122 switch (save_decoded_options[j].canonical_option[0][1])
20124 case 'M':
20125 case 'i':
20126 case 'W':
20127 continue;
20128 case 'f':
20129 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
20130 "dump", 4) == 0)
20131 continue;
20132 break;
20133 default:
20134 break;
20136 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
20137 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
20138 break;
20141 producer = XNEWVEC (char, plen + 1 + len + 1);
20142 tail = producer;
20143 sprintf (tail, "%s %s", language_string, version_string);
20144 tail += plen;
20146 FOR_EACH_VEC_ELT (switches, j, p)
20148 len = strlen (p);
20149 *tail = ' ';
20150 memcpy (tail + 1, p, len);
20151 tail += len + 1;
20154 *tail = '\0';
20155 return producer;
20158 /* Given a C and/or C++ language/version string return the "highest".
20159 C++ is assumed to be "higher" than C in this case. Used for merging
20160 LTO translation unit languages. */
20161 static const char *
20162 highest_c_language (const char *lang1, const char *lang2)
20164 if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
20165 return "GNU C++14";
20166 if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
20167 return "GNU C++11";
20168 if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
20169 return "GNU C++98";
20171 if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
20172 return "GNU C11";
20173 if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
20174 return "GNU C99";
20175 if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
20176 return "GNU C89";
20178 gcc_unreachable ();
20182 /* Generate the DIE for the compilation unit. */
20184 static dw_die_ref
20185 gen_compile_unit_die (const char *filename)
20187 dw_die_ref die;
20188 const char *language_string = lang_hooks.name;
20189 int language;
20191 die = new_die (DW_TAG_compile_unit, NULL, NULL);
20193 if (filename)
20195 add_name_attribute (die, filename);
20196 /* Don't add cwd for <built-in>. */
20197 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
20198 add_comp_dir_attribute (die);
20201 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
20203 /* If our producer is LTO try to figure out a common language to use
20204 from the global list of translation units. */
20205 if (strcmp (language_string, "GNU GIMPLE") == 0)
20207 unsigned i;
20208 tree t;
20209 const char *common_lang = NULL;
20211 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
20213 if (!TRANSLATION_UNIT_LANGUAGE (t))
20214 continue;
20215 if (!common_lang)
20216 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
20217 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
20219 else if (strncmp (common_lang, "GNU C", 5) == 0
20220 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
20221 /* Mixing C and C++ is ok, use C++ in that case. */
20222 common_lang = highest_c_language (common_lang,
20223 TRANSLATION_UNIT_LANGUAGE (t));
20224 else
20226 /* Fall back to C. */
20227 common_lang = NULL;
20228 break;
20232 if (common_lang)
20233 language_string = common_lang;
20236 language = DW_LANG_C;
20237 if (strncmp (language_string, "GNU C", 5) == 0
20238 && ISDIGIT (language_string[5]))
20240 language = DW_LANG_C89;
20241 if (dwarf_version >= 3 || !dwarf_strict)
20243 if (strcmp (language_string, "GNU C89") != 0)
20244 language = DW_LANG_C99;
20246 if (dwarf_version >= 5 /* || !dwarf_strict */)
20247 if (strcmp (language_string, "GNU C11") == 0)
20248 language = DW_LANG_C11;
20251 else if (strncmp (language_string, "GNU C++", 7) == 0)
20253 language = DW_LANG_C_plus_plus;
20254 if (dwarf_version >= 5 /* || !dwarf_strict */)
20256 if (strcmp (language_string, "GNU C++11") == 0)
20257 language = DW_LANG_C_plus_plus_11;
20258 else if (strcmp (language_string, "GNU C++14") == 0)
20259 language = DW_LANG_C_plus_plus_14;
20262 else if (strcmp (language_string, "GNU F77") == 0)
20263 language = DW_LANG_Fortran77;
20264 else if (strcmp (language_string, "GNU Pascal") == 0)
20265 language = DW_LANG_Pascal83;
20266 else if (dwarf_version >= 3 || !dwarf_strict)
20268 if (strcmp (language_string, "GNU Ada") == 0)
20269 language = DW_LANG_Ada95;
20270 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
20272 language = DW_LANG_Fortran95;
20273 if (dwarf_version >= 5 /* || !dwarf_strict */)
20275 if (strcmp (language_string, "GNU Fortran2003") == 0)
20276 language = DW_LANG_Fortran03;
20277 else if (strcmp (language_string, "GNU Fortran2008") == 0)
20278 language = DW_LANG_Fortran08;
20281 else if (strcmp (language_string, "GNU Java") == 0)
20282 language = DW_LANG_Java;
20283 else if (strcmp (language_string, "GNU Objective-C") == 0)
20284 language = DW_LANG_ObjC;
20285 else if (strcmp (language_string, "GNU Objective-C++") == 0)
20286 language = DW_LANG_ObjC_plus_plus;
20287 else if (dwarf_version >= 5 || !dwarf_strict)
20289 if (strcmp (language_string, "GNU Go") == 0)
20290 language = DW_LANG_Go;
20293 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
20294 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
20295 language = DW_LANG_Fortran90;
20297 add_AT_unsigned (die, DW_AT_language, language);
20299 switch (language)
20301 case DW_LANG_Fortran77:
20302 case DW_LANG_Fortran90:
20303 case DW_LANG_Fortran95:
20304 case DW_LANG_Fortran03:
20305 case DW_LANG_Fortran08:
20306 /* Fortran has case insensitive identifiers and the front-end
20307 lowercases everything. */
20308 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
20309 break;
20310 default:
20311 /* The default DW_ID_case_sensitive doesn't need to be specified. */
20312 break;
20314 return die;
20317 /* Generate the DIE for a base class. */
20319 static void
20320 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
20322 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
20324 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, context_die);
20325 add_data_member_location_attribute (die, binfo);
20327 if (BINFO_VIRTUAL_P (binfo))
20328 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
20330 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
20331 children, otherwise the default is DW_ACCESS_public. In DWARF2
20332 the default has always been DW_ACCESS_private. */
20333 if (access == access_public_node)
20335 if (dwarf_version == 2
20336 || context_die->die_tag == DW_TAG_class_type)
20337 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
20339 else if (access == access_protected_node)
20340 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
20341 else if (dwarf_version > 2
20342 && context_die->die_tag != DW_TAG_class_type)
20343 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
20346 /* Generate a DIE for a class member. */
20348 static void
20349 gen_member_die (tree type, dw_die_ref context_die)
20351 tree member;
20352 tree binfo = TYPE_BINFO (type);
20353 dw_die_ref child;
20355 /* If this is not an incomplete type, output descriptions of each of its
20356 members. Note that as we output the DIEs necessary to represent the
20357 members of this record or union type, we will also be trying to output
20358 DIEs to represent the *types* of those members. However the `type'
20359 function (above) will specifically avoid generating type DIEs for member
20360 types *within* the list of member DIEs for this (containing) type except
20361 for those types (of members) which are explicitly marked as also being
20362 members of this (containing) type themselves. The g++ front- end can
20363 force any given type to be treated as a member of some other (containing)
20364 type by setting the TYPE_CONTEXT of the given (member) type to point to
20365 the TREE node representing the appropriate (containing) type. */
20367 /* First output info about the base classes. */
20368 if (binfo)
20370 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
20371 int i;
20372 tree base;
20374 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
20375 gen_inheritance_die (base,
20376 (accesses ? (*accesses)[i] : access_public_node),
20377 context_die);
20380 /* Now output info about the data members and type members. */
20381 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
20383 /* If we thought we were generating minimal debug info for TYPE
20384 and then changed our minds, some of the member declarations
20385 may have already been defined. Don't define them again, but
20386 do put them in the right order. */
20388 child = lookup_decl_die (member);
20389 if (child)
20390 splice_child_die (context_die, child);
20391 else
20392 gen_decl_die (member, NULL, context_die);
20395 /* We do not keep type methods in type variants. */
20396 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
20397 /* Now output info about the function members (if any). */
20398 if (TYPE_METHODS (type) != error_mark_node)
20399 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
20401 /* Don't include clones in the member list. */
20402 if (DECL_ABSTRACT_ORIGIN (member))
20403 continue;
20404 /* Nor constructors for anonymous classes. */
20405 if (DECL_ARTIFICIAL (member)
20406 && dwarf2_name (member, 0) == NULL)
20407 continue;
20409 child = lookup_decl_die (member);
20410 if (child)
20411 splice_child_die (context_die, child);
20412 else
20413 gen_decl_die (member, NULL, context_die);
20417 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
20418 is set, we pretend that the type was never defined, so we only get the
20419 member DIEs needed by later specification DIEs. */
20421 static void
20422 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
20423 enum debug_info_usage usage)
20425 if (TREE_ASM_WRITTEN (type))
20427 /* Fill in the bound of variable-length fields in late dwarf if
20428 still incomplete. */
20429 if (!early_dwarf && variably_modified_type_p (type, NULL))
20430 for (tree member = TYPE_FIELDS (type);
20431 member;
20432 member = DECL_CHAIN (member))
20433 fill_variable_array_bounds (TREE_TYPE (member));
20434 return;
20437 dw_die_ref type_die = lookup_type_die (type);
20438 dw_die_ref scope_die = 0;
20439 int nested = 0;
20440 int complete = (TYPE_SIZE (type)
20441 && (! TYPE_STUB_DECL (type)
20442 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
20443 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
20444 complete = complete && should_emit_struct_debug (type, usage);
20446 if (type_die && ! complete)
20447 return;
20449 if (TYPE_CONTEXT (type) != NULL_TREE
20450 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
20451 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
20452 nested = 1;
20454 scope_die = scope_die_for (type, context_die);
20456 /* Generate child dies for template paramaters. */
20457 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
20458 schedule_generic_params_dies_gen (type);
20460 if (! type_die || (nested && is_cu_die (scope_die)))
20461 /* First occurrence of type or toplevel definition of nested class. */
20463 dw_die_ref old_die = type_die;
20465 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
20466 ? record_type_tag (type) : DW_TAG_union_type,
20467 scope_die, type);
20468 equate_type_number_to_die (type, type_die);
20469 if (old_die)
20470 add_AT_specification (type_die, old_die);
20471 else
20472 add_name_attribute (type_die, type_tag (type));
20474 else
20475 remove_AT (type_die, DW_AT_declaration);
20477 /* If this type has been completed, then give it a byte_size attribute and
20478 then give a list of members. */
20479 if (complete && !ns_decl)
20481 /* Prevent infinite recursion in cases where the type of some member of
20482 this type is expressed in terms of this type itself. */
20483 TREE_ASM_WRITTEN (type) = 1;
20484 add_byte_size_attribute (type_die, type);
20485 if (TYPE_STUB_DECL (type) != NULL_TREE)
20487 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
20488 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
20491 /* If the first reference to this type was as the return type of an
20492 inline function, then it may not have a parent. Fix this now. */
20493 if (type_die->die_parent == NULL)
20494 add_child_die (scope_die, type_die);
20496 push_decl_scope (type);
20497 gen_member_die (type, type_die);
20498 pop_decl_scope ();
20500 add_gnat_descriptive_type_attribute (type_die, type, context_die);
20501 if (TYPE_ARTIFICIAL (type))
20502 add_AT_flag (type_die, DW_AT_artificial, 1);
20504 /* GNU extension: Record what type our vtable lives in. */
20505 if (TYPE_VFIELD (type))
20507 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
20509 gen_type_die (vtype, context_die);
20510 add_AT_die_ref (type_die, DW_AT_containing_type,
20511 lookup_type_die (vtype));
20514 else
20516 add_AT_flag (type_die, DW_AT_declaration, 1);
20518 /* We don't need to do this for function-local types. */
20519 if (TYPE_STUB_DECL (type)
20520 && ! decl_function_context (TYPE_STUB_DECL (type)))
20521 vec_safe_push (incomplete_types, type);
20524 if (get_AT (type_die, DW_AT_name))
20525 add_pubtype (type, type_die);
20528 /* Generate a DIE for a subroutine _type_. */
20530 static void
20531 gen_subroutine_type_die (tree type, dw_die_ref context_die)
20533 tree return_type = TREE_TYPE (type);
20534 dw_die_ref subr_die
20535 = new_die (DW_TAG_subroutine_type,
20536 scope_die_for (type, context_die), type);
20538 equate_type_number_to_die (type, subr_die);
20539 add_prototyped_attribute (subr_die, type);
20540 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, context_die);
20541 gen_formal_types_die (type, subr_die);
20543 if (get_AT (subr_die, DW_AT_name))
20544 add_pubtype (type, subr_die);
20547 /* Generate a DIE for a type definition. */
20549 static void
20550 gen_typedef_die (tree decl, dw_die_ref context_die)
20552 dw_die_ref type_die;
20553 tree origin;
20555 if (TREE_ASM_WRITTEN (decl))
20557 if (DECL_ORIGINAL_TYPE (decl))
20558 fill_variable_array_bounds (DECL_ORIGINAL_TYPE (decl));
20559 return;
20562 TREE_ASM_WRITTEN (decl) = 1;
20563 type_die = new_die (DW_TAG_typedef, context_die, decl);
20564 origin = decl_ultimate_origin (decl);
20565 if (origin != NULL)
20566 add_abstract_origin_attribute (type_die, origin);
20567 else
20569 tree type;
20571 add_name_and_src_coords_attributes (type_die, decl);
20572 if (DECL_ORIGINAL_TYPE (decl))
20574 type = DECL_ORIGINAL_TYPE (decl);
20576 if (type == error_mark_node)
20577 return;
20579 gcc_assert (type != TREE_TYPE (decl));
20580 equate_type_number_to_die (TREE_TYPE (decl), type_die);
20582 else
20584 type = TREE_TYPE (decl);
20586 if (type == error_mark_node)
20587 return;
20589 if (is_naming_typedef_decl (TYPE_NAME (type)))
20591 /* Here, we are in the case of decl being a typedef naming
20592 an anonymous type, e.g:
20593 typedef struct {...} foo;
20594 In that case TREE_TYPE (decl) is not a typedef variant
20595 type and TYPE_NAME of the anonymous type is set to the
20596 TYPE_DECL of the typedef. This construct is emitted by
20597 the C++ FE.
20599 TYPE is the anonymous struct named by the typedef
20600 DECL. As we need the DW_AT_type attribute of the
20601 DW_TAG_typedef to point to the DIE of TYPE, let's
20602 generate that DIE right away. add_type_attribute
20603 called below will then pick (via lookup_type_die) that
20604 anonymous struct DIE. */
20605 if (!TREE_ASM_WRITTEN (type))
20606 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
20608 /* This is a GNU Extension. We are adding a
20609 DW_AT_linkage_name attribute to the DIE of the
20610 anonymous struct TYPE. The value of that attribute
20611 is the name of the typedef decl naming the anonymous
20612 struct. This greatly eases the work of consumers of
20613 this debug info. */
20614 add_linkage_attr (lookup_type_die (type), decl);
20618 add_type_attribute (type_die, type, decl_quals (decl), context_die);
20620 if (is_naming_typedef_decl (decl))
20621 /* We want that all subsequent calls to lookup_type_die with
20622 TYPE in argument yield the DW_TAG_typedef we have just
20623 created. */
20624 equate_type_number_to_die (type, type_die);
20626 add_accessibility_attribute (type_die, decl);
20629 if (DECL_ABSTRACT_P (decl))
20630 equate_decl_number_to_die (decl, type_die);
20632 if (get_AT (type_die, DW_AT_name))
20633 add_pubtype (decl, type_die);
20636 /* Generate a DIE for a struct, class, enum or union type. */
20638 static void
20639 gen_tagged_type_die (tree type,
20640 dw_die_ref context_die,
20641 enum debug_info_usage usage)
20643 int need_pop;
20645 if (type == NULL_TREE
20646 || !is_tagged_type (type))
20647 return;
20649 if (TREE_ASM_WRITTEN (type))
20650 need_pop = 0;
20651 /* If this is a nested type whose containing class hasn't been written
20652 out yet, writing it out will cover this one, too. This does not apply
20653 to instantiations of member class templates; they need to be added to
20654 the containing class as they are generated. FIXME: This hurts the
20655 idea of combining type decls from multiple TUs, since we can't predict
20656 what set of template instantiations we'll get. */
20657 else if (TYPE_CONTEXT (type)
20658 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
20659 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
20661 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
20663 if (TREE_ASM_WRITTEN (type))
20664 return;
20666 /* If that failed, attach ourselves to the stub. */
20667 push_decl_scope (TYPE_CONTEXT (type));
20668 context_die = lookup_type_die (TYPE_CONTEXT (type));
20669 need_pop = 1;
20671 else if (TYPE_CONTEXT (type) != NULL_TREE
20672 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
20674 /* If this type is local to a function that hasn't been written
20675 out yet, use a NULL context for now; it will be fixed up in
20676 decls_for_scope. */
20677 context_die = lookup_decl_die (TYPE_CONTEXT (type));
20678 /* A declaration DIE doesn't count; nested types need to go in the
20679 specification. */
20680 if (context_die && is_declaration_die (context_die))
20681 context_die = NULL;
20682 need_pop = 0;
20684 else
20686 context_die = declare_in_namespace (type, context_die);
20687 need_pop = 0;
20690 if (TREE_CODE (type) == ENUMERAL_TYPE)
20692 /* This might have been written out by the call to
20693 declare_in_namespace. */
20694 if (!TREE_ASM_WRITTEN (type))
20695 gen_enumeration_type_die (type, context_die);
20697 else
20698 gen_struct_or_union_type_die (type, context_die, usage);
20700 if (need_pop)
20701 pop_decl_scope ();
20703 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
20704 it up if it is ever completed. gen_*_type_die will set it for us
20705 when appropriate. */
20708 /* Generate a type description DIE. */
20710 static void
20711 gen_type_die_with_usage (tree type, dw_die_ref context_die,
20712 enum debug_info_usage usage)
20714 struct array_descr_info info;
20716 if (type == NULL_TREE || type == error_mark_node)
20717 return;
20719 #ifdef ENABLE_CHECKING
20720 if (type)
20721 verify_type (type);
20722 #endif
20724 if (TYPE_NAME (type) != NULL_TREE
20725 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
20726 && is_redundant_typedef (TYPE_NAME (type))
20727 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
20728 /* The DECL of this type is a typedef we don't want to emit debug
20729 info for but we want debug info for its underlying typedef.
20730 This can happen for e.g, the injected-class-name of a C++
20731 type. */
20732 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
20734 /* If TYPE is a typedef type variant, let's generate debug info
20735 for the parent typedef which TYPE is a type of. */
20736 if (typedef_variant_p (type))
20738 if (TREE_ASM_WRITTEN (type))
20739 return;
20741 /* Prevent broken recursion; we can't hand off to the same type. */
20742 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
20744 /* Give typedefs the right scope. */
20745 context_die = scope_die_for (type, context_die);
20747 TREE_ASM_WRITTEN (type) = 1;
20749 gen_decl_die (TYPE_NAME (type), NULL, context_die);
20750 return;
20753 /* If type is an anonymous tagged type named by a typedef, let's
20754 generate debug info for the typedef. */
20755 if (is_naming_typedef_decl (TYPE_NAME (type)))
20757 /* Use the DIE of the containing namespace as the parent DIE of
20758 the type description DIE we want to generate. */
20759 if (DECL_CONTEXT (TYPE_NAME (type))
20760 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
20761 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
20763 gen_decl_die (TYPE_NAME (type), NULL, context_die);
20764 return;
20767 /* We are going to output a DIE to represent the unqualified version
20768 of this type (i.e. without any const or volatile qualifiers) so
20769 get the main variant (i.e. the unqualified version) of this type
20770 now. (Vectors are special because the debugging info is in the
20771 cloned type itself). */
20772 if (TREE_CODE (type) != VECTOR_TYPE)
20773 type = type_main_variant (type);
20775 /* If this is an array type with hidden descriptor, handle it first. */
20776 if (!TREE_ASM_WRITTEN (type)
20777 && lang_hooks.types.get_array_descr_info)
20779 memset (&info, 0, sizeof (info));
20780 if (lang_hooks.types.get_array_descr_info (type, &info))
20782 gen_descr_array_type_die (type, &info, context_die);
20783 TREE_ASM_WRITTEN (type) = 1;
20784 return;
20788 if (TREE_ASM_WRITTEN (type))
20790 /* Variable-length types may be incomplete even if
20791 TREE_ASM_WRITTEN. For such types, fall through to
20792 gen_array_type_die() and possibly fill in
20793 DW_AT_{upper,lower}_bound attributes. */
20794 if ((TREE_CODE (type) != ARRAY_TYPE
20795 && TREE_CODE (type) != RECORD_TYPE
20796 && TREE_CODE (type) != UNION_TYPE
20797 && TREE_CODE (type) != QUAL_UNION_TYPE)
20798 || !variably_modified_type_p (type, NULL))
20799 return;
20802 switch (TREE_CODE (type))
20804 case ERROR_MARK:
20805 break;
20807 case POINTER_TYPE:
20808 case REFERENCE_TYPE:
20809 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
20810 ensures that the gen_type_die recursion will terminate even if the
20811 type is recursive. Recursive types are possible in Ada. */
20812 /* ??? We could perhaps do this for all types before the switch
20813 statement. */
20814 TREE_ASM_WRITTEN (type) = 1;
20816 /* For these types, all that is required is that we output a DIE (or a
20817 set of DIEs) to represent the "basis" type. */
20818 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20819 DINFO_USAGE_IND_USE);
20820 break;
20822 case OFFSET_TYPE:
20823 /* This code is used for C++ pointer-to-data-member types.
20824 Output a description of the relevant class type. */
20825 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
20826 DINFO_USAGE_IND_USE);
20828 /* Output a description of the type of the object pointed to. */
20829 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20830 DINFO_USAGE_IND_USE);
20832 /* Now output a DIE to represent this pointer-to-data-member type
20833 itself. */
20834 gen_ptr_to_mbr_type_die (type, context_die);
20835 break;
20837 case FUNCTION_TYPE:
20838 /* Force out return type (in case it wasn't forced out already). */
20839 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20840 DINFO_USAGE_DIR_USE);
20841 gen_subroutine_type_die (type, context_die);
20842 break;
20844 case METHOD_TYPE:
20845 /* Force out return type (in case it wasn't forced out already). */
20846 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20847 DINFO_USAGE_DIR_USE);
20848 gen_subroutine_type_die (type, context_die);
20849 break;
20851 case ARRAY_TYPE:
20852 case VECTOR_TYPE:
20853 gen_array_type_die (type, context_die);
20854 break;
20856 case ENUMERAL_TYPE:
20857 case RECORD_TYPE:
20858 case UNION_TYPE:
20859 case QUAL_UNION_TYPE:
20860 gen_tagged_type_die (type, context_die, usage);
20861 return;
20863 case VOID_TYPE:
20864 case INTEGER_TYPE:
20865 case REAL_TYPE:
20866 case FIXED_POINT_TYPE:
20867 case COMPLEX_TYPE:
20868 case BOOLEAN_TYPE:
20869 case POINTER_BOUNDS_TYPE:
20870 /* No DIEs needed for fundamental types. */
20871 break;
20873 case NULLPTR_TYPE:
20874 case LANG_TYPE:
20875 /* Just use DW_TAG_unspecified_type. */
20877 dw_die_ref type_die = lookup_type_die (type);
20878 if (type_die == NULL)
20880 tree name = TYPE_IDENTIFIER (type);
20881 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
20882 type);
20883 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
20884 equate_type_number_to_die (type, type_die);
20887 break;
20889 default:
20890 if (is_cxx_auto (type))
20892 tree name = TYPE_IDENTIFIER (type);
20893 dw_die_ref *die = (name == get_identifier ("auto")
20894 ? &auto_die : &decltype_auto_die);
20895 if (!*die)
20897 *die = new_die (DW_TAG_unspecified_type,
20898 comp_unit_die (), NULL_TREE);
20899 add_name_attribute (*die, IDENTIFIER_POINTER (name));
20901 equate_type_number_to_die (type, *die);
20902 break;
20904 gcc_unreachable ();
20907 TREE_ASM_WRITTEN (type) = 1;
20910 static void
20911 gen_type_die (tree type, dw_die_ref context_die)
20913 if (type != error_mark_node)
20915 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
20916 #ifdef ENABLE_CHECKING
20917 dw_die_ref die = lookup_type_die (type);
20918 if (die)
20919 check_die (die);
20920 #endif
20924 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
20925 things which are local to the given block. */
20927 static void
20928 gen_block_die (tree stmt, dw_die_ref context_die)
20930 int must_output_die = 0;
20931 bool inlined_func;
20933 /* Ignore blocks that are NULL. */
20934 if (stmt == NULL_TREE)
20935 return;
20937 inlined_func = inlined_function_outer_scope_p (stmt);
20939 /* If the block is one fragment of a non-contiguous block, do not
20940 process the variables, since they will have been done by the
20941 origin block. Do process subblocks. */
20942 if (BLOCK_FRAGMENT_ORIGIN (stmt))
20944 tree sub;
20946 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
20947 gen_block_die (sub, context_die);
20949 return;
20952 /* Determine if we need to output any Dwarf DIEs at all to represent this
20953 block. */
20954 if (inlined_func)
20955 /* The outer scopes for inlinings *must* always be represented. We
20956 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
20957 must_output_die = 1;
20958 else
20960 /* Determine if this block directly contains any "significant"
20961 local declarations which we will need to output DIEs for. */
20962 if (debug_info_level > DINFO_LEVEL_TERSE)
20963 /* We are not in terse mode so *any* local declaration counts
20964 as being a "significant" one. */
20965 must_output_die = ((BLOCK_VARS (stmt) != NULL
20966 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
20967 && (TREE_USED (stmt)
20968 || TREE_ASM_WRITTEN (stmt)
20969 || BLOCK_ABSTRACT (stmt)));
20970 else if ((TREE_USED (stmt)
20971 || TREE_ASM_WRITTEN (stmt)
20972 || BLOCK_ABSTRACT (stmt))
20973 && !dwarf2out_ignore_block (stmt))
20974 must_output_die = 1;
20977 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
20978 DIE for any block which contains no significant local declarations at
20979 all. Rather, in such cases we just call `decls_for_scope' so that any
20980 needed Dwarf info for any sub-blocks will get properly generated. Note
20981 that in terse mode, our definition of what constitutes a "significant"
20982 local declaration gets restricted to include only inlined function
20983 instances and local (nested) function definitions. */
20984 if (must_output_die)
20986 if (inlined_func)
20988 /* If STMT block is abstract, that means we have been called
20989 indirectly from dwarf2out_abstract_function.
20990 That function rightfully marks the descendent blocks (of
20991 the abstract function it is dealing with) as being abstract,
20992 precisely to prevent us from emitting any
20993 DW_TAG_inlined_subroutine DIE as a descendent
20994 of an abstract function instance. So in that case, we should
20995 not call gen_inlined_subroutine_die.
20997 Later though, when cgraph asks dwarf2out to emit info
20998 for the concrete instance of the function decl into which
20999 the concrete instance of STMT got inlined, the later will lead
21000 to the generation of a DW_TAG_inlined_subroutine DIE. */
21001 if (! BLOCK_ABSTRACT (stmt))
21002 gen_inlined_subroutine_die (stmt, context_die);
21004 else
21005 gen_lexical_block_die (stmt, context_die);
21007 else
21008 decls_for_scope (stmt, context_die);
21011 /* Process variable DECL (or variable with origin ORIGIN) within
21012 block STMT and add it to CONTEXT_DIE. */
21013 static void
21014 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
21016 dw_die_ref die;
21017 tree decl_or_origin = decl ? decl : origin;
21019 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
21020 die = lookup_decl_die (decl_or_origin);
21021 else if (TREE_CODE (decl_or_origin) == TYPE_DECL
21022 && TYPE_DECL_IS_STUB (decl_or_origin))
21023 die = lookup_type_die (TREE_TYPE (decl_or_origin));
21024 else
21025 die = NULL;
21027 if (die != NULL && die->die_parent == NULL)
21028 add_child_die (context_die, die);
21029 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
21031 if (early_dwarf)
21032 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
21033 stmt, context_die);
21035 else
21036 gen_decl_die (decl, origin, context_die);
21039 /* Generate all of the decls declared within a given scope and (recursively)
21040 all of its sub-blocks. */
21042 static void
21043 decls_for_scope (tree stmt, dw_die_ref context_die)
21045 tree decl;
21046 unsigned int i;
21047 tree subblocks;
21049 /* Ignore NULL blocks. */
21050 if (stmt == NULL_TREE)
21051 return;
21053 /* Output the DIEs to represent all of the data objects and typedefs
21054 declared directly within this block but not within any nested
21055 sub-blocks. Also, nested function and tag DIEs have been
21056 generated with a parent of NULL; fix that up now. We don't
21057 have to do this if we're at -g1. */
21058 if (debug_info_level > DINFO_LEVEL_TERSE)
21060 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
21061 process_scope_var (stmt, decl, NULL_TREE, context_die);
21062 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
21063 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
21064 context_die);
21067 /* Even if we're at -g1, we need to process the subblocks in order to get
21068 inlined call information. */
21070 /* Output the DIEs to represent all sub-blocks (and the items declared
21071 therein) of this block. */
21072 for (subblocks = BLOCK_SUBBLOCKS (stmt);
21073 subblocks != NULL;
21074 subblocks = BLOCK_CHAIN (subblocks))
21075 gen_block_die (subblocks, context_die);
21078 /* Is this a typedef we can avoid emitting? */
21080 static inline int
21081 is_redundant_typedef (const_tree decl)
21083 if (TYPE_DECL_IS_STUB (decl))
21084 return 1;
21086 if (DECL_ARTIFICIAL (decl)
21087 && DECL_CONTEXT (decl)
21088 && is_tagged_type (DECL_CONTEXT (decl))
21089 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
21090 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
21091 /* Also ignore the artificial member typedef for the class name. */
21092 return 1;
21094 return 0;
21097 /* Return TRUE if TYPE is a typedef that names a type for linkage
21098 purposes. This kind of typedefs is produced by the C++ FE for
21099 constructs like:
21101 typedef struct {...} foo;
21103 In that case, there is no typedef variant type produced for foo.
21104 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
21105 struct type. */
21107 static bool
21108 is_naming_typedef_decl (const_tree decl)
21110 if (decl == NULL_TREE
21111 || TREE_CODE (decl) != TYPE_DECL
21112 || !is_tagged_type (TREE_TYPE (decl))
21113 || DECL_IS_BUILTIN (decl)
21114 || is_redundant_typedef (decl)
21115 /* It looks like Ada produces TYPE_DECLs that are very similar
21116 to C++ naming typedefs but that have different
21117 semantics. Let's be specific to c++ for now. */
21118 || !is_cxx ())
21119 return FALSE;
21121 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
21122 && TYPE_NAME (TREE_TYPE (decl)) == decl
21123 && (TYPE_STUB_DECL (TREE_TYPE (decl))
21124 != TYPE_NAME (TREE_TYPE (decl))));
21127 /* Looks up the DIE for a context. */
21129 static inline dw_die_ref
21130 lookup_context_die (tree context)
21132 if (context)
21134 /* Find die that represents this context. */
21135 if (TYPE_P (context))
21137 context = TYPE_MAIN_VARIANT (context);
21138 dw_die_ref ctx = lookup_type_die (context);
21139 if (!ctx)
21140 return NULL;
21141 return strip_naming_typedef (context, ctx);
21143 else
21144 return lookup_decl_die (context);
21146 return comp_unit_die ();
21149 /* Returns the DIE for a context. */
21151 static inline dw_die_ref
21152 get_context_die (tree context)
21154 if (context)
21156 /* Find die that represents this context. */
21157 if (TYPE_P (context))
21159 context = TYPE_MAIN_VARIANT (context);
21160 return strip_naming_typedef (context, force_type_die (context));
21162 else
21163 return force_decl_die (context);
21165 return comp_unit_die ();
21168 /* Returns the DIE for decl. A DIE will always be returned. */
21170 static dw_die_ref
21171 force_decl_die (tree decl)
21173 dw_die_ref decl_die;
21174 unsigned saved_external_flag;
21175 tree save_fn = NULL_TREE;
21176 decl_die = lookup_decl_die (decl);
21177 if (!decl_die)
21179 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
21181 decl_die = lookup_decl_die (decl);
21182 if (decl_die)
21183 return decl_die;
21185 switch (TREE_CODE (decl))
21187 case FUNCTION_DECL:
21188 /* Clear current_function_decl, so that gen_subprogram_die thinks
21189 that this is a declaration. At this point, we just want to force
21190 declaration die. */
21191 save_fn = current_function_decl;
21192 current_function_decl = NULL_TREE;
21193 gen_subprogram_die (decl, context_die);
21194 current_function_decl = save_fn;
21195 break;
21197 case VAR_DECL:
21198 /* Set external flag to force declaration die. Restore it after
21199 gen_decl_die() call. */
21200 saved_external_flag = DECL_EXTERNAL (decl);
21201 DECL_EXTERNAL (decl) = 1;
21202 gen_decl_die (decl, NULL, context_die);
21203 DECL_EXTERNAL (decl) = saved_external_flag;
21204 break;
21206 case NAMESPACE_DECL:
21207 if (dwarf_version >= 3 || !dwarf_strict)
21208 dwarf2out_decl (decl);
21209 else
21210 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
21211 decl_die = comp_unit_die ();
21212 break;
21214 case TRANSLATION_UNIT_DECL:
21215 decl_die = comp_unit_die ();
21216 break;
21218 default:
21219 gcc_unreachable ();
21222 /* We should be able to find the DIE now. */
21223 if (!decl_die)
21224 decl_die = lookup_decl_die (decl);
21225 gcc_assert (decl_die);
21228 return decl_die;
21231 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
21232 always returned. */
21234 static dw_die_ref
21235 force_type_die (tree type)
21237 dw_die_ref type_die;
21239 type_die = lookup_type_die (type);
21240 if (!type_die)
21242 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
21244 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
21245 context_die);
21246 gcc_assert (type_die);
21248 return type_die;
21251 /* Force out any required namespaces to be able to output DECL,
21252 and return the new context_die for it, if it's changed. */
21254 static dw_die_ref
21255 setup_namespace_context (tree thing, dw_die_ref context_die)
21257 tree context = (DECL_P (thing)
21258 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
21259 if (context && TREE_CODE (context) == NAMESPACE_DECL)
21260 /* Force out the namespace. */
21261 context_die = force_decl_die (context);
21263 return context_die;
21266 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
21267 type) within its namespace, if appropriate.
21269 For compatibility with older debuggers, namespace DIEs only contain
21270 declarations; all definitions are emitted at CU scope, with
21271 DW_AT_specification pointing to the declaration (like with class
21272 members). */
21274 static dw_die_ref
21275 declare_in_namespace (tree thing, dw_die_ref context_die)
21277 dw_die_ref ns_context;
21279 if (debug_info_level <= DINFO_LEVEL_TERSE)
21280 return context_die;
21282 /* External declarations in the local scope only need to be emitted
21283 once, not once in the namespace and once in the scope.
21285 This avoids declaring the `extern' below in the
21286 namespace DIE as well as in the innermost scope:
21288 namespace S
21290 int i=5;
21291 int foo()
21293 int i=8;
21294 extern int i;
21295 return i;
21299 if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
21300 return context_die;
21302 /* If this decl is from an inlined function, then don't try to emit it in its
21303 namespace, as we will get confused. It would have already been emitted
21304 when the abstract instance of the inline function was emitted anyways. */
21305 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
21306 return context_die;
21308 ns_context = setup_namespace_context (thing, context_die);
21310 if (ns_context != context_die)
21312 if (is_fortran ())
21313 return ns_context;
21314 if (DECL_P (thing))
21315 gen_decl_die (thing, NULL, ns_context);
21316 else
21317 gen_type_die (thing, ns_context);
21319 return context_die;
21322 /* Generate a DIE for a namespace or namespace alias. */
21324 static void
21325 gen_namespace_die (tree decl, dw_die_ref context_die)
21327 dw_die_ref namespace_die;
21329 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
21330 they are an alias of. */
21331 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
21333 /* Output a real namespace or module. */
21334 context_die = setup_namespace_context (decl, comp_unit_die ());
21335 namespace_die = new_die (is_fortran ()
21336 ? DW_TAG_module : DW_TAG_namespace,
21337 context_die, decl);
21338 /* For Fortran modules defined in different CU don't add src coords. */
21339 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
21341 const char *name = dwarf2_name (decl, 0);
21342 if (name)
21343 add_name_attribute (namespace_die, name);
21345 else
21346 add_name_and_src_coords_attributes (namespace_die, decl);
21347 if (DECL_EXTERNAL (decl))
21348 add_AT_flag (namespace_die, DW_AT_declaration, 1);
21349 equate_decl_number_to_die (decl, namespace_die);
21351 else
21353 /* Output a namespace alias. */
21355 /* Force out the namespace we are an alias of, if necessary. */
21356 dw_die_ref origin_die
21357 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
21359 if (DECL_FILE_SCOPE_P (decl)
21360 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
21361 context_die = setup_namespace_context (decl, comp_unit_die ());
21362 /* Now create the namespace alias DIE. */
21363 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
21364 add_name_and_src_coords_attributes (namespace_die, decl);
21365 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
21366 equate_decl_number_to_die (decl, namespace_die);
21368 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
21369 if (want_pubnames ())
21370 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
21373 /* Generate Dwarf debug information for a decl described by DECL.
21374 The return value is currently only meaningful for PARM_DECLs,
21375 for all other decls it returns NULL. */
21377 static dw_die_ref
21378 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
21380 tree decl_or_origin = decl ? decl : origin;
21381 tree class_origin = NULL, ultimate_origin;
21383 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
21384 return NULL;
21386 /* Ignore pointer bounds decls. */
21387 if (DECL_P (decl_or_origin)
21388 && TREE_TYPE (decl_or_origin)
21389 && POINTER_BOUNDS_P (decl_or_origin))
21390 return NULL;
21392 switch (TREE_CODE (decl_or_origin))
21394 case ERROR_MARK:
21395 break;
21397 case CONST_DECL:
21398 if (!is_fortran () && !is_ada ())
21400 /* The individual enumerators of an enum type get output when we output
21401 the Dwarf representation of the relevant enum type itself. */
21402 break;
21405 /* Emit its type. */
21406 gen_type_die (TREE_TYPE (decl), context_die);
21408 /* And its containing namespace. */
21409 context_die = declare_in_namespace (decl, context_die);
21411 gen_const_die (decl, context_die);
21412 break;
21414 case FUNCTION_DECL:
21415 /* Don't output any DIEs to represent mere function declarations,
21416 unless they are class members or explicit block externs. */
21417 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
21418 && DECL_FILE_SCOPE_P (decl_or_origin)
21419 && (current_function_decl == NULL_TREE
21420 || DECL_ARTIFICIAL (decl_or_origin)))
21421 break;
21423 #if 0
21424 /* FIXME */
21425 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
21426 on local redeclarations of global functions. That seems broken. */
21427 if (current_function_decl != decl)
21428 /* This is only a declaration. */;
21429 #endif
21431 /* If we're emitting a clone, emit info for the abstract instance. */
21432 if (origin || DECL_ORIGIN (decl) != decl)
21433 dwarf2out_abstract_function (origin
21434 ? DECL_ORIGIN (origin)
21435 : DECL_ABSTRACT_ORIGIN (decl));
21437 /* If we're emitting an out-of-line copy of an inline function,
21438 emit info for the abstract instance and set up to refer to it. */
21439 else if (cgraph_function_possibly_inlined_p (decl)
21440 && ! DECL_ABSTRACT_P (decl)
21441 && ! class_or_namespace_scope_p (context_die)
21442 /* dwarf2out_abstract_function won't emit a die if this is just
21443 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
21444 that case, because that works only if we have a die. */
21445 && DECL_INITIAL (decl) != NULL_TREE)
21447 dwarf2out_abstract_function (decl);
21448 set_decl_origin_self (decl);
21451 /* Otherwise we're emitting the primary DIE for this decl. */
21452 else if (debug_info_level > DINFO_LEVEL_TERSE)
21454 /* Before we describe the FUNCTION_DECL itself, make sure that we
21455 have its containing type. */
21456 if (!origin)
21457 origin = decl_class_context (decl);
21458 if (origin != NULL_TREE)
21459 gen_type_die (origin, context_die);
21461 /* And its return type. */
21462 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
21464 /* And its virtual context. */
21465 if (DECL_VINDEX (decl) != NULL_TREE)
21466 gen_type_die (DECL_CONTEXT (decl), context_die);
21468 /* Make sure we have a member DIE for decl. */
21469 if (origin != NULL_TREE)
21470 gen_type_die_for_member (origin, decl, context_die);
21472 /* And its containing namespace. */
21473 context_die = declare_in_namespace (decl, context_die);
21476 /* Now output a DIE to represent the function itself. */
21477 if (decl)
21478 gen_subprogram_die (decl, context_die);
21479 break;
21481 case TYPE_DECL:
21482 /* If we are in terse mode, don't generate any DIEs to represent any
21483 actual typedefs. */
21484 if (debug_info_level <= DINFO_LEVEL_TERSE)
21485 break;
21487 /* In the special case of a TYPE_DECL node representing the declaration
21488 of some type tag, if the given TYPE_DECL is marked as having been
21489 instantiated from some other (original) TYPE_DECL node (e.g. one which
21490 was generated within the original definition of an inline function) we
21491 used to generate a special (abbreviated) DW_TAG_structure_type,
21492 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
21493 should be actually referencing those DIEs, as variable DIEs with that
21494 type would be emitted already in the abstract origin, so it was always
21495 removed during unused type prunning. Don't add anything in this
21496 case. */
21497 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
21498 break;
21500 if (is_redundant_typedef (decl))
21501 gen_type_die (TREE_TYPE (decl), context_die);
21502 else
21503 /* Output a DIE to represent the typedef itself. */
21504 gen_typedef_die (decl, context_die);
21505 break;
21507 case LABEL_DECL:
21508 if (debug_info_level >= DINFO_LEVEL_NORMAL)
21509 gen_label_die (decl, context_die);
21510 break;
21512 case VAR_DECL:
21513 case RESULT_DECL:
21514 /* If we are in terse mode, don't generate any DIEs to represent any
21515 variable declarations or definitions. */
21516 if (debug_info_level <= DINFO_LEVEL_TERSE)
21517 break;
21519 /* Output any DIEs that are needed to specify the type of this data
21520 object. */
21521 if (decl_by_reference_p (decl_or_origin))
21522 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
21523 else
21524 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
21526 /* And its containing type. */
21527 class_origin = decl_class_context (decl_or_origin);
21528 if (class_origin != NULL_TREE)
21529 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
21531 /* And its containing namespace. */
21532 context_die = declare_in_namespace (decl_or_origin, context_die);
21534 /* Now output the DIE to represent the data object itself. This gets
21535 complicated because of the possibility that the VAR_DECL really
21536 represents an inlined instance of a formal parameter for an inline
21537 function. */
21538 ultimate_origin = decl_ultimate_origin (decl_or_origin);
21539 if (ultimate_origin != NULL_TREE
21540 && TREE_CODE (ultimate_origin) == PARM_DECL)
21541 gen_formal_parameter_die (decl, origin,
21542 true /* Emit name attribute. */,
21543 context_die);
21544 else
21545 gen_variable_die (decl, origin, context_die);
21546 break;
21548 case FIELD_DECL:
21549 /* Ignore the nameless fields that are used to skip bits but handle C++
21550 anonymous unions and structs. */
21551 if (DECL_NAME (decl) != NULL_TREE
21552 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
21553 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
21555 gen_type_die (member_declared_type (decl), context_die);
21556 gen_field_die (decl, context_die);
21558 break;
21560 case PARM_DECL:
21561 if (DECL_BY_REFERENCE (decl_or_origin))
21562 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
21563 else
21564 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
21565 return gen_formal_parameter_die (decl, origin,
21566 true /* Emit name attribute. */,
21567 context_die);
21569 case NAMESPACE_DECL:
21570 case IMPORTED_DECL:
21571 if (dwarf_version >= 3 || !dwarf_strict)
21572 gen_namespace_die (decl, context_die);
21573 break;
21575 case NAMELIST_DECL:
21576 gen_namelist_decl (DECL_NAME (decl), context_die,
21577 NAMELIST_DECL_ASSOCIATED_DECL (decl));
21578 break;
21580 default:
21581 /* Probably some frontend-internal decl. Assume we don't care. */
21582 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
21583 break;
21586 return NULL;
21589 /* Output initial debug information for global DECL. Called at the
21590 end of the parsing process.
21592 This is the initial debug generation process. As such, the DIEs
21593 generated may be incomplete. A later debug generation pass
21594 (dwarf2out_late_global_decl) will augment the information generated
21595 in this pass (e.g., with complete location info). */
21597 static void
21598 dwarf2out_early_global_decl (tree decl)
21600 set_early_dwarf s;
21602 /* gen_decl_die() will set DECL_ABSTRACT because
21603 cgraph_function_possibly_inlined_p() returns true. This is in
21604 turn will cause DW_AT_inline attributes to be set.
21606 This happens because at early dwarf generation, there is no
21607 cgraph information, causing cgraph_function_possibly_inlined_p()
21608 to return true. Trick cgraph_function_possibly_inlined_p()
21609 while we generate dwarf early. */
21610 bool save = symtab->global_info_ready;
21611 symtab->global_info_ready = true;
21613 /* We don't handle TYPE_DECLs. If required, they'll be reached via
21614 other DECLs and they can point to template types or other things
21615 that dwarf2out can't handle when done via dwarf2out_decl. */
21616 if (TREE_CODE (decl) != TYPE_DECL
21617 && TREE_CODE (decl) != PARM_DECL)
21619 tree save_fndecl = current_function_decl;
21620 if (TREE_CODE (decl) == FUNCTION_DECL)
21622 /* No cfun means the symbol has no body, so there's nothing
21623 to emit. */
21624 if (!DECL_STRUCT_FUNCTION (decl))
21625 goto early_decl_exit;
21627 current_function_decl = decl;
21629 dwarf2out_decl (decl);
21630 if (TREE_CODE (decl) == FUNCTION_DECL)
21631 current_function_decl = save_fndecl;
21633 early_decl_exit:
21634 symtab->global_info_ready = save;
21637 /* Output debug information for global decl DECL. Called from
21638 toplev.c after compilation proper has finished. */
21640 static void
21641 dwarf2out_late_global_decl (tree decl)
21643 /* Output any global decls we missed or fill-in any location
21644 information we were unable to determine on the first pass.
21646 Skip over functions because they were handled by the
21647 debug_hooks->function_decl() call in rest_of_handle_final. */
21648 if ((TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
21649 && !POINTER_BOUNDS_P (decl))
21650 dwarf2out_decl (decl);
21653 /* Output debug information for type decl DECL. Called from toplev.c
21654 and from language front ends (to record built-in types). */
21655 static void
21656 dwarf2out_type_decl (tree decl, int local)
21658 if (!local)
21660 set_early_dwarf s;
21661 dwarf2out_decl (decl);
21665 /* Output debug information for imported module or decl DECL.
21666 NAME is non-NULL name in the lexical block if the decl has been renamed.
21667 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
21668 that DECL belongs to.
21669 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
21670 static void
21671 dwarf2out_imported_module_or_decl_1 (tree decl,
21672 tree name,
21673 tree lexical_block,
21674 dw_die_ref lexical_block_die)
21676 expanded_location xloc;
21677 dw_die_ref imported_die = NULL;
21678 dw_die_ref at_import_die;
21680 if (TREE_CODE (decl) == IMPORTED_DECL)
21682 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
21683 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
21684 gcc_assert (decl);
21686 else
21687 xloc = expand_location (input_location);
21689 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
21691 at_import_die = force_type_die (TREE_TYPE (decl));
21692 /* For namespace N { typedef void T; } using N::T; base_type_die
21693 returns NULL, but DW_TAG_imported_declaration requires
21694 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
21695 if (!at_import_die)
21697 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
21698 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
21699 at_import_die = lookup_type_die (TREE_TYPE (decl));
21700 gcc_assert (at_import_die);
21703 else
21705 at_import_die = lookup_decl_die (decl);
21706 if (!at_import_die)
21708 /* If we're trying to avoid duplicate debug info, we may not have
21709 emitted the member decl for this field. Emit it now. */
21710 if (TREE_CODE (decl) == FIELD_DECL)
21712 tree type = DECL_CONTEXT (decl);
21714 if (TYPE_CONTEXT (type)
21715 && TYPE_P (TYPE_CONTEXT (type))
21716 && !should_emit_struct_debug (TYPE_CONTEXT (type),
21717 DINFO_USAGE_DIR_USE))
21718 return;
21719 gen_type_die_for_member (type, decl,
21720 get_context_die (TYPE_CONTEXT (type)));
21722 if (TREE_CODE (decl) == NAMELIST_DECL)
21723 at_import_die = gen_namelist_decl (DECL_NAME (decl),
21724 get_context_die (DECL_CONTEXT (decl)),
21725 NULL_TREE);
21726 else
21727 at_import_die = force_decl_die (decl);
21731 if (TREE_CODE (decl) == NAMESPACE_DECL)
21733 if (dwarf_version >= 3 || !dwarf_strict)
21734 imported_die = new_die (DW_TAG_imported_module,
21735 lexical_block_die,
21736 lexical_block);
21737 else
21738 return;
21740 else
21741 imported_die = new_die (DW_TAG_imported_declaration,
21742 lexical_block_die,
21743 lexical_block);
21745 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
21746 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
21747 if (name)
21748 add_AT_string (imported_die, DW_AT_name,
21749 IDENTIFIER_POINTER (name));
21750 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
21753 /* Output debug information for imported module or decl DECL.
21754 NAME is non-NULL name in context if the decl has been renamed.
21755 CHILD is true if decl is one of the renamed decls as part of
21756 importing whole module. */
21758 static void
21759 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
21760 bool child)
21762 /* dw_die_ref at_import_die; */
21763 dw_die_ref scope_die;
21765 if (debug_info_level <= DINFO_LEVEL_TERSE)
21766 return;
21768 gcc_assert (decl);
21770 set_early_dwarf s;
21772 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
21773 We need decl DIE for reference and scope die. First, get DIE for the decl
21774 itself. */
21776 /* Get the scope die for decl context. Use comp_unit_die for global module
21777 or decl. If die is not found for non globals, force new die. */
21778 if (context
21779 && TYPE_P (context)
21780 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
21781 return;
21783 if (!(dwarf_version >= 3 || !dwarf_strict))
21784 return;
21786 scope_die = get_context_die (context);
21788 if (child)
21790 gcc_assert (scope_die->die_child);
21791 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
21792 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
21793 scope_die = scope_die->die_child;
21796 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
21797 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
21800 /* Output debug information for namelists. */
21802 static dw_die_ref
21803 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
21805 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
21806 tree value;
21807 unsigned i;
21809 if (debug_info_level <= DINFO_LEVEL_TERSE)
21810 return NULL;
21812 gcc_assert (scope_die != NULL);
21813 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
21814 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
21816 /* If there are no item_decls, we have a nondefining namelist, e.g.
21817 with USE association; hence, set DW_AT_declaration. */
21818 if (item_decls == NULL_TREE)
21820 add_AT_flag (nml_die, DW_AT_declaration, 1);
21821 return nml_die;
21824 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
21826 nml_item_ref_die = lookup_decl_die (value);
21827 if (!nml_item_ref_die)
21828 nml_item_ref_die = force_decl_die (value);
21830 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
21831 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
21833 return nml_die;
21837 /* Write the debugging output for DECL and return the DIE. */
21839 static void
21840 dwarf2out_decl (tree decl)
21842 dw_die_ref context_die = comp_unit_die ();
21844 switch (TREE_CODE (decl))
21846 case ERROR_MARK:
21847 return;
21849 case FUNCTION_DECL:
21850 /* What we would really like to do here is to filter out all mere
21851 file-scope declarations of file-scope functions which are never
21852 referenced later within this translation unit (and keep all of ones
21853 that *are* referenced later on) but we aren't clairvoyant, so we have
21854 no idea which functions will be referenced in the future (i.e. later
21855 on within the current translation unit). So here we just ignore all
21856 file-scope function declarations which are not also definitions. If
21857 and when the debugger needs to know something about these functions,
21858 it will have to hunt around and find the DWARF information associated
21859 with the definition of the function.
21861 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
21862 nodes represent definitions and which ones represent mere
21863 declarations. We have to check DECL_INITIAL instead. That's because
21864 the C front-end supports some weird semantics for "extern inline"
21865 function definitions. These can get inlined within the current
21866 translation unit (and thus, we need to generate Dwarf info for their
21867 abstract instances so that the Dwarf info for the concrete inlined
21868 instances can have something to refer to) but the compiler never
21869 generates any out-of-lines instances of such things (despite the fact
21870 that they *are* definitions).
21872 The important point is that the C front-end marks these "extern
21873 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
21874 them anyway. Note that the C++ front-end also plays some similar games
21875 for inline function definitions appearing within include files which
21876 also contain `#pragma interface' pragmas.
21878 If we are called from dwarf2out_abstract_function output a DIE
21879 anyway. We can end up here this way with early inlining and LTO
21880 where the inlined function is output in a different LTRANS unit
21881 or not at all. */
21882 if (DECL_INITIAL (decl) == NULL_TREE
21883 && ! DECL_ABSTRACT_P (decl))
21884 return;
21886 /* If we're a nested function, initially use a parent of NULL; if we're
21887 a plain function, this will be fixed up in decls_for_scope. If
21888 we're a method, it will be ignored, since we already have a DIE. */
21889 if (decl_function_context (decl)
21890 /* But if we're in terse mode, we don't care about scope. */
21891 && debug_info_level > DINFO_LEVEL_TERSE)
21892 context_die = NULL;
21893 break;
21895 case VAR_DECL:
21896 /* For local statics lookup proper context die. */
21897 if (local_function_static (decl))
21898 context_die = lookup_decl_die (DECL_CONTEXT (decl));
21900 /* If we are in terse mode, don't generate any DIEs to represent any
21901 variable declarations or definitions. */
21902 if (debug_info_level <= DINFO_LEVEL_TERSE)
21903 return;
21904 break;
21906 case CONST_DECL:
21907 if (debug_info_level <= DINFO_LEVEL_TERSE)
21908 return;
21909 if (!is_fortran () && !is_ada ())
21910 return;
21911 if (TREE_STATIC (decl) && decl_function_context (decl))
21912 context_die = lookup_decl_die (DECL_CONTEXT (decl));
21913 break;
21915 case NAMESPACE_DECL:
21916 case IMPORTED_DECL:
21917 if (debug_info_level <= DINFO_LEVEL_TERSE)
21918 return;
21919 if (lookup_decl_die (decl) != NULL)
21920 return;
21921 break;
21923 case TYPE_DECL:
21924 /* Don't emit stubs for types unless they are needed by other DIEs. */
21925 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
21926 return;
21928 /* Don't bother trying to generate any DIEs to represent any of the
21929 normal built-in types for the language we are compiling. */
21930 if (DECL_IS_BUILTIN (decl))
21931 return;
21933 /* If we are in terse mode, don't generate any DIEs for types. */
21934 if (debug_info_level <= DINFO_LEVEL_TERSE)
21935 return;
21937 /* If we're a function-scope tag, initially use a parent of NULL;
21938 this will be fixed up in decls_for_scope. */
21939 if (decl_function_context (decl))
21940 context_die = NULL;
21942 break;
21944 case NAMELIST_DECL:
21945 break;
21947 default:
21948 return;
21951 gen_decl_die (decl, NULL, context_die);
21953 #ifdef ENABLE_CHECKING
21954 dw_die_ref die = lookup_decl_die (decl);
21955 if (die)
21956 check_die (die);
21957 #endif
21960 /* Write the debugging output for DECL. */
21962 static void
21963 dwarf2out_function_decl (tree decl)
21965 dwarf2out_decl (decl);
21966 call_arg_locations = NULL;
21967 call_arg_loc_last = NULL;
21968 call_site_count = -1;
21969 tail_call_site_count = -1;
21970 decl_loc_table->empty ();
21971 cached_dw_loc_list_table->empty ();
21974 /* Output a marker (i.e. a label) for the beginning of the generated code for
21975 a lexical block. */
21977 static void
21978 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
21979 unsigned int blocknum)
21981 switch_to_section (current_function_section ());
21982 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
21985 /* Output a marker (i.e. a label) for the end of the generated code for a
21986 lexical block. */
21988 static void
21989 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
21991 switch_to_section (current_function_section ());
21992 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
21995 /* Returns nonzero if it is appropriate not to emit any debugging
21996 information for BLOCK, because it doesn't contain any instructions.
21998 Don't allow this for blocks with nested functions or local classes
21999 as we would end up with orphans, and in the presence of scheduling
22000 we may end up calling them anyway. */
22002 static bool
22003 dwarf2out_ignore_block (const_tree block)
22005 tree decl;
22006 unsigned int i;
22008 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
22009 if (TREE_CODE (decl) == FUNCTION_DECL
22010 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
22011 return 0;
22012 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
22014 decl = BLOCK_NONLOCALIZED_VAR (block, i);
22015 if (TREE_CODE (decl) == FUNCTION_DECL
22016 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
22017 return 0;
22020 return 1;
22023 /* Hash table routines for file_hash. */
22025 bool
22026 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
22028 return filename_cmp (p1->filename, p2) == 0;
22031 hashval_t
22032 dwarf_file_hasher::hash (dwarf_file_data *p)
22034 return htab_hash_string (p->filename);
22037 /* Lookup FILE_NAME (in the list of filenames that we know about here in
22038 dwarf2out.c) and return its "index". The index of each (known) filename is
22039 just a unique number which is associated with only that one filename. We
22040 need such numbers for the sake of generating labels (in the .debug_sfnames
22041 section) and references to those files numbers (in the .debug_srcinfo
22042 and.debug_macinfo sections). If the filename given as an argument is not
22043 found in our current list, add it to the list and assign it the next
22044 available unique index number. */
22046 static struct dwarf_file_data *
22047 lookup_filename (const char *file_name)
22049 struct dwarf_file_data * created;
22051 if (!file_name)
22052 return NULL;
22054 dwarf_file_data **slot
22055 = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
22056 INSERT);
22057 if (*slot)
22058 return *slot;
22060 created = ggc_alloc<dwarf_file_data> ();
22061 created->filename = file_name;
22062 created->emitted_number = 0;
22063 *slot = created;
22064 return created;
22067 /* If the assembler will construct the file table, then translate the compiler
22068 internal file table number into the assembler file table number, and emit
22069 a .file directive if we haven't already emitted one yet. The file table
22070 numbers are different because we prune debug info for unused variables and
22071 types, which may include filenames. */
22073 static int
22074 maybe_emit_file (struct dwarf_file_data * fd)
22076 if (! fd->emitted_number)
22078 if (last_emitted_file)
22079 fd->emitted_number = last_emitted_file->emitted_number + 1;
22080 else
22081 fd->emitted_number = 1;
22082 last_emitted_file = fd;
22084 if (DWARF2_ASM_LINE_DEBUG_INFO)
22086 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
22087 output_quoted_string (asm_out_file,
22088 remap_debug_filename (fd->filename));
22089 fputc ('\n', asm_out_file);
22093 return fd->emitted_number;
22096 /* Schedule generation of a DW_AT_const_value attribute to DIE.
22097 That generation should happen after function debug info has been
22098 generated. The value of the attribute is the constant value of ARG. */
22100 static void
22101 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
22103 die_arg_entry entry;
22105 if (!die || !arg)
22106 return;
22108 if (!tmpl_value_parm_die_table)
22109 vec_alloc (tmpl_value_parm_die_table, 32);
22111 entry.die = die;
22112 entry.arg = arg;
22113 vec_safe_push (tmpl_value_parm_die_table, entry);
22116 /* Return TRUE if T is an instance of generic type, FALSE
22117 otherwise. */
22119 static bool
22120 generic_type_p (tree t)
22122 if (t == NULL_TREE || !TYPE_P (t))
22123 return false;
22124 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
22127 /* Schedule the generation of the generic parameter dies for the
22128 instance of generic type T. The proper generation itself is later
22129 done by gen_scheduled_generic_parms_dies. */
22131 static void
22132 schedule_generic_params_dies_gen (tree t)
22134 if (!generic_type_p (t))
22135 return;
22137 if (!generic_type_instances)
22138 vec_alloc (generic_type_instances, 256);
22140 vec_safe_push (generic_type_instances, t);
22143 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
22144 by append_entry_to_tmpl_value_parm_die_table. This function must
22145 be called after function DIEs have been generated. */
22147 static void
22148 gen_remaining_tmpl_value_param_die_attribute (void)
22150 if (tmpl_value_parm_die_table)
22152 unsigned i;
22153 die_arg_entry *e;
22155 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
22156 tree_add_const_value_attribute (e->die, e->arg);
22160 /* Generate generic parameters DIEs for instances of generic types
22161 that have been previously scheduled by
22162 schedule_generic_params_dies_gen. This function must be called
22163 after all the types of the CU have been laid out. */
22165 static void
22166 gen_scheduled_generic_parms_dies (void)
22168 unsigned i;
22169 tree t;
22171 if (!generic_type_instances)
22172 return;
22174 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
22175 if (COMPLETE_TYPE_P (t))
22176 gen_generic_params_dies (t);
22180 /* Replace DW_AT_name for the decl with name. */
22182 static void
22183 dwarf2out_set_name (tree decl, tree name)
22185 dw_die_ref die;
22186 dw_attr_ref attr;
22187 const char *dname;
22189 die = TYPE_SYMTAB_DIE (decl);
22190 if (!die)
22191 return;
22193 dname = dwarf2_name (name, 0);
22194 if (!dname)
22195 return;
22197 attr = get_AT (die, DW_AT_name);
22198 if (attr)
22200 struct indirect_string_node *node;
22202 node = find_AT_string (dname);
22203 /* replace the string. */
22204 attr->dw_attr_val.v.val_str = node;
22207 else
22208 add_name_attribute (die, dname);
22211 /* True if before or during processing of the first function being emitted. */
22212 static bool in_first_function_p = true;
22213 /* True if loc_note during dwarf2out_var_location call might still be
22214 before first real instruction at address equal to .Ltext0. */
22215 static bool maybe_at_text_label_p = true;
22216 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
22217 static unsigned int first_loclabel_num_not_at_text_label;
22219 /* Called by the final INSN scan whenever we see a var location. We
22220 use it to drop labels in the right places, and throw the location in
22221 our lookup table. */
22223 static void
22224 dwarf2out_var_location (rtx_insn *loc_note)
22226 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
22227 struct var_loc_node *newloc;
22228 rtx_insn *next_real, *next_note;
22229 static const char *last_label;
22230 static const char *last_postcall_label;
22231 static bool last_in_cold_section_p;
22232 static rtx_insn *expected_next_loc_note;
22233 tree decl;
22234 bool var_loc_p;
22236 if (!NOTE_P (loc_note))
22238 if (CALL_P (loc_note))
22240 call_site_count++;
22241 if (SIBLING_CALL_P (loc_note))
22242 tail_call_site_count++;
22244 return;
22247 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
22248 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
22249 return;
22251 /* Optimize processing a large consecutive sequence of location
22252 notes so we don't spend too much time in next_real_insn. If the
22253 next insn is another location note, remember the next_real_insn
22254 calculation for next time. */
22255 next_real = cached_next_real_insn;
22256 if (next_real)
22258 if (expected_next_loc_note != loc_note)
22259 next_real = NULL;
22262 next_note = NEXT_INSN (loc_note);
22263 if (! next_note
22264 || next_note->deleted ()
22265 || ! NOTE_P (next_note)
22266 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
22267 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
22268 next_note = NULL;
22270 if (! next_real)
22271 next_real = next_real_insn (loc_note);
22273 if (next_note)
22275 expected_next_loc_note = next_note;
22276 cached_next_real_insn = next_real;
22278 else
22279 cached_next_real_insn = NULL;
22281 /* If there are no instructions which would be affected by this note,
22282 don't do anything. */
22283 if (var_loc_p
22284 && next_real == NULL_RTX
22285 && !NOTE_DURING_CALL_P (loc_note))
22286 return;
22288 if (next_real == NULL_RTX)
22289 next_real = get_last_insn ();
22291 /* If there were any real insns between note we processed last time
22292 and this note (or if it is the first note), clear
22293 last_{,postcall_}label so that they are not reused this time. */
22294 if (last_var_location_insn == NULL_RTX
22295 || last_var_location_insn != next_real
22296 || last_in_cold_section_p != in_cold_section_p)
22298 last_label = NULL;
22299 last_postcall_label = NULL;
22302 if (var_loc_p)
22304 decl = NOTE_VAR_LOCATION_DECL (loc_note);
22305 newloc = add_var_loc_to_decl (decl, loc_note,
22306 NOTE_DURING_CALL_P (loc_note)
22307 ? last_postcall_label : last_label);
22308 if (newloc == NULL)
22309 return;
22311 else
22313 decl = NULL_TREE;
22314 newloc = NULL;
22317 /* If there were no real insns between note we processed last time
22318 and this note, use the label we emitted last time. Otherwise
22319 create a new label and emit it. */
22320 if (last_label == NULL)
22322 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
22323 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
22324 loclabel_num++;
22325 last_label = ggc_strdup (loclabel);
22326 /* See if loclabel might be equal to .Ltext0. If yes,
22327 bump first_loclabel_num_not_at_text_label. */
22328 if (!have_multiple_function_sections
22329 && in_first_function_p
22330 && maybe_at_text_label_p)
22332 static rtx_insn *last_start;
22333 rtx_insn *insn;
22334 for (insn = loc_note; insn; insn = previous_insn (insn))
22335 if (insn == last_start)
22336 break;
22337 else if (!NONDEBUG_INSN_P (insn))
22338 continue;
22339 else
22341 rtx body = PATTERN (insn);
22342 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
22343 continue;
22344 /* Inline asm could occupy zero bytes. */
22345 else if (GET_CODE (body) == ASM_INPUT
22346 || asm_noperands (body) >= 0)
22347 continue;
22348 #ifdef HAVE_attr_length
22349 else if (get_attr_min_length (insn) == 0)
22350 continue;
22351 #endif
22352 else
22354 /* Assume insn has non-zero length. */
22355 maybe_at_text_label_p = false;
22356 break;
22359 if (maybe_at_text_label_p)
22361 last_start = loc_note;
22362 first_loclabel_num_not_at_text_label = loclabel_num;
22367 if (!var_loc_p)
22369 struct call_arg_loc_node *ca_loc
22370 = ggc_cleared_alloc<call_arg_loc_node> ();
22371 rtx_insn *prev = prev_real_insn (loc_note);
22372 rtx x;
22373 ca_loc->call_arg_loc_note = loc_note;
22374 ca_loc->next = NULL;
22375 ca_loc->label = last_label;
22376 gcc_assert (prev
22377 && (CALL_P (prev)
22378 || (NONJUMP_INSN_P (prev)
22379 && GET_CODE (PATTERN (prev)) == SEQUENCE
22380 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
22381 if (!CALL_P (prev))
22382 prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
22383 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
22384 x = get_call_rtx_from (PATTERN (prev));
22385 if (x)
22387 x = XEXP (XEXP (x, 0), 0);
22388 if (GET_CODE (x) == SYMBOL_REF
22389 && SYMBOL_REF_DECL (x)
22390 && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
22391 ca_loc->symbol_ref = x;
22393 ca_loc->block = insn_scope (prev);
22394 if (call_arg_locations)
22395 call_arg_loc_last->next = ca_loc;
22396 else
22397 call_arg_locations = ca_loc;
22398 call_arg_loc_last = ca_loc;
22400 else if (!NOTE_DURING_CALL_P (loc_note))
22401 newloc->label = last_label;
22402 else
22404 if (!last_postcall_label)
22406 sprintf (loclabel, "%s-1", last_label);
22407 last_postcall_label = ggc_strdup (loclabel);
22409 newloc->label = last_postcall_label;
22412 last_var_location_insn = next_real;
22413 last_in_cold_section_p = in_cold_section_p;
22416 /* Note in one location list that text section has changed. */
22419 var_location_switch_text_section_1 (var_loc_list **slot, void *)
22421 var_loc_list *list = *slot;
22422 if (list->first)
22423 list->last_before_switch
22424 = list->last->next ? list->last->next : list->last;
22425 return 1;
22428 /* Note in all location lists that text section has changed. */
22430 static void
22431 var_location_switch_text_section (void)
22433 if (decl_loc_table == NULL)
22434 return;
22436 decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
22439 /* Create a new line number table. */
22441 static dw_line_info_table *
22442 new_line_info_table (void)
22444 dw_line_info_table *table;
22446 table = ggc_cleared_alloc<dw_line_info_table_struct> ();
22447 table->file_num = 1;
22448 table->line_num = 1;
22449 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
22451 return table;
22454 /* Lookup the "current" table into which we emit line info, so
22455 that we don't have to do it for every source line. */
22457 static void
22458 set_cur_line_info_table (section *sec)
22460 dw_line_info_table *table;
22462 if (sec == text_section)
22463 table = text_section_line_info;
22464 else if (sec == cold_text_section)
22466 table = cold_text_section_line_info;
22467 if (!table)
22469 cold_text_section_line_info = table = new_line_info_table ();
22470 table->end_label = cold_end_label;
22473 else
22475 const char *end_label;
22477 if (flag_reorder_blocks_and_partition)
22479 if (in_cold_section_p)
22480 end_label = crtl->subsections.cold_section_end_label;
22481 else
22482 end_label = crtl->subsections.hot_section_end_label;
22484 else
22486 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22487 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
22488 current_function_funcdef_no);
22489 end_label = ggc_strdup (label);
22492 table = new_line_info_table ();
22493 table->end_label = end_label;
22495 vec_safe_push (separate_line_info, table);
22498 if (DWARF2_ASM_LINE_DEBUG_INFO)
22499 table->is_stmt = (cur_line_info_table
22500 ? cur_line_info_table->is_stmt
22501 : DWARF_LINE_DEFAULT_IS_STMT_START);
22502 cur_line_info_table = table;
22506 /* We need to reset the locations at the beginning of each
22507 function. We can't do this in the end_function hook, because the
22508 declarations that use the locations won't have been output when
22509 that hook is called. Also compute have_multiple_function_sections here. */
22511 static void
22512 dwarf2out_begin_function (tree fun)
22514 section *sec = function_section (fun);
22516 if (sec != text_section)
22517 have_multiple_function_sections = true;
22519 if (flag_reorder_blocks_and_partition && !cold_text_section)
22521 gcc_assert (current_function_decl == fun);
22522 cold_text_section = unlikely_text_section ();
22523 switch_to_section (cold_text_section);
22524 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
22525 switch_to_section (sec);
22528 dwarf2out_note_section_used ();
22529 call_site_count = 0;
22530 tail_call_site_count = 0;
22532 set_cur_line_info_table (sec);
22535 /* Helper function of dwarf2out_end_function, called only after emitting
22536 the very first function into assembly. Check if some .debug_loc range
22537 might end with a .LVL* label that could be equal to .Ltext0.
22538 In that case we must force using absolute addresses in .debug_loc ranges,
22539 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
22540 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
22541 list terminator.
22542 Set have_multiple_function_sections to true in that case and
22543 terminate htab traversal. */
22546 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
22548 var_loc_list *entry = *slot;
22549 struct var_loc_node *node;
22551 node = entry->first;
22552 if (node && node->next && node->next->label)
22554 unsigned int i;
22555 const char *label = node->next->label;
22556 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
22558 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
22560 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
22561 if (strcmp (label, loclabel) == 0)
22563 have_multiple_function_sections = true;
22564 return 0;
22568 return 1;
22571 /* Hook called after emitting a function into assembly.
22572 This does something only for the very first function emitted. */
22574 static void
22575 dwarf2out_end_function (unsigned int)
22577 if (in_first_function_p
22578 && !have_multiple_function_sections
22579 && first_loclabel_num_not_at_text_label
22580 && decl_loc_table)
22581 decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
22582 in_first_function_p = false;
22583 maybe_at_text_label_p = false;
22586 /* Temporary holder for dwarf2out_register_main_translation_unit. Used to let
22587 front-ends register a translation unit even before dwarf2out_init is
22588 called. */
22589 static tree main_translation_unit = NULL_TREE;
22591 /* Hook called by front-ends after they built their main translation unit.
22592 Associate comp_unit_die to UNIT. */
22594 static void
22595 dwarf2out_register_main_translation_unit (tree unit)
22597 gcc_assert (TREE_CODE (unit) == TRANSLATION_UNIT_DECL
22598 && main_translation_unit == NULL_TREE);
22599 main_translation_unit = unit;
22600 /* If dwarf2out_init has not been called yet, it will perform the association
22601 itself looking at main_translation_unit. */
22602 if (decl_die_table != NULL)
22603 equate_decl_number_to_die (unit, comp_unit_die ());
22606 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
22608 static void
22609 push_dw_line_info_entry (dw_line_info_table *table,
22610 enum dw_line_info_opcode opcode, unsigned int val)
22612 dw_line_info_entry e;
22613 e.opcode = opcode;
22614 e.val = val;
22615 vec_safe_push (table->entries, e);
22618 /* Output a label to mark the beginning of a source code line entry
22619 and record information relating to this source line, in
22620 'line_info_table' for later output of the .debug_line section. */
22621 /* ??? The discriminator parameter ought to be unsigned. */
22623 static void
22624 dwarf2out_source_line (unsigned int line, const char *filename,
22625 int discriminator, bool is_stmt)
22627 unsigned int file_num;
22628 dw_line_info_table *table;
22630 if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
22631 return;
22633 /* The discriminator column was added in dwarf4. Simplify the below
22634 by simply removing it if we're not supposed to output it. */
22635 if (dwarf_version < 4 && dwarf_strict)
22636 discriminator = 0;
22638 table = cur_line_info_table;
22639 file_num = maybe_emit_file (lookup_filename (filename));
22641 /* ??? TODO: Elide duplicate line number entries. Traditionally,
22642 the debugger has used the second (possibly duplicate) line number
22643 at the beginning of the function to mark the end of the prologue.
22644 We could eliminate any other duplicates within the function. For
22645 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
22646 that second line number entry. */
22647 /* Recall that this end-of-prologue indication is *not* the same thing
22648 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
22649 to which the hook corresponds, follows the last insn that was
22650 emitted by gen_prologue. What we need is to precede the first insn
22651 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
22652 insn that corresponds to something the user wrote. These may be
22653 very different locations once scheduling is enabled. */
22655 if (0 && file_num == table->file_num
22656 && line == table->line_num
22657 && discriminator == table->discrim_num
22658 && is_stmt == table->is_stmt)
22659 return;
22661 switch_to_section (current_function_section ());
22663 /* If requested, emit something human-readable. */
22664 if (flag_debug_asm)
22665 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
22667 if (DWARF2_ASM_LINE_DEBUG_INFO)
22669 /* Emit the .loc directive understood by GNU as. */
22670 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
22671 file_num, line, is_stmt, discriminator */
22672 fputs ("\t.loc ", asm_out_file);
22673 fprint_ul (asm_out_file, file_num);
22674 putc (' ', asm_out_file);
22675 fprint_ul (asm_out_file, line);
22676 putc (' ', asm_out_file);
22677 putc ('0', asm_out_file);
22679 if (is_stmt != table->is_stmt)
22681 fputs (" is_stmt ", asm_out_file);
22682 putc (is_stmt ? '1' : '0', asm_out_file);
22684 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
22686 gcc_assert (discriminator > 0);
22687 fputs (" discriminator ", asm_out_file);
22688 fprint_ul (asm_out_file, (unsigned long) discriminator);
22690 putc ('\n', asm_out_file);
22692 else
22694 unsigned int label_num = ++line_info_label_num;
22696 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
22698 push_dw_line_info_entry (table, LI_set_address, label_num);
22699 if (file_num != table->file_num)
22700 push_dw_line_info_entry (table, LI_set_file, file_num);
22701 if (discriminator != table->discrim_num)
22702 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
22703 if (is_stmt != table->is_stmt)
22704 push_dw_line_info_entry (table, LI_negate_stmt, 0);
22705 push_dw_line_info_entry (table, LI_set_line, line);
22708 table->file_num = file_num;
22709 table->line_num = line;
22710 table->discrim_num = discriminator;
22711 table->is_stmt = is_stmt;
22712 table->in_use = true;
22715 /* Record the beginning of a new source file. */
22717 static void
22718 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
22720 if (flag_eliminate_dwarf2_dups)
22722 /* Record the beginning of the file for break_out_includes. */
22723 dw_die_ref bincl_die;
22725 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
22726 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
22729 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22731 macinfo_entry e;
22732 e.code = DW_MACINFO_start_file;
22733 e.lineno = lineno;
22734 e.info = ggc_strdup (filename);
22735 vec_safe_push (macinfo_table, e);
22739 /* Record the end of a source file. */
22741 static void
22742 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
22744 if (flag_eliminate_dwarf2_dups)
22745 /* Record the end of the file for break_out_includes. */
22746 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
22748 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22750 macinfo_entry e;
22751 e.code = DW_MACINFO_end_file;
22752 e.lineno = lineno;
22753 e.info = NULL;
22754 vec_safe_push (macinfo_table, e);
22758 /* Called from debug_define in toplev.c. The `buffer' parameter contains
22759 the tail part of the directive line, i.e. the part which is past the
22760 initial whitespace, #, whitespace, directive-name, whitespace part. */
22762 static void
22763 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
22764 const char *buffer ATTRIBUTE_UNUSED)
22766 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22768 macinfo_entry e;
22769 /* Insert a dummy first entry to be able to optimize the whole
22770 predefined macro block using DW_MACRO_GNU_transparent_include. */
22771 if (macinfo_table->is_empty () && lineno <= 1)
22773 e.code = 0;
22774 e.lineno = 0;
22775 e.info = NULL;
22776 vec_safe_push (macinfo_table, e);
22778 e.code = DW_MACINFO_define;
22779 e.lineno = lineno;
22780 e.info = ggc_strdup (buffer);
22781 vec_safe_push (macinfo_table, e);
22785 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
22786 the tail part of the directive line, i.e. the part which is past the
22787 initial whitespace, #, whitespace, directive-name, whitespace part. */
22789 static void
22790 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
22791 const char *buffer ATTRIBUTE_UNUSED)
22793 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22795 macinfo_entry e;
22796 /* Insert a dummy first entry to be able to optimize the whole
22797 predefined macro block using DW_MACRO_GNU_transparent_include. */
22798 if (macinfo_table->is_empty () && lineno <= 1)
22800 e.code = 0;
22801 e.lineno = 0;
22802 e.info = NULL;
22803 vec_safe_push (macinfo_table, e);
22805 e.code = DW_MACINFO_undef;
22806 e.lineno = lineno;
22807 e.info = ggc_strdup (buffer);
22808 vec_safe_push (macinfo_table, e);
22812 /* Helpers to manipulate hash table of CUs. */
22814 struct macinfo_entry_hasher : nofree_ptr_hash <macinfo_entry>
22816 static inline hashval_t hash (const macinfo_entry *);
22817 static inline bool equal (const macinfo_entry *, const macinfo_entry *);
22820 inline hashval_t
22821 macinfo_entry_hasher::hash (const macinfo_entry *entry)
22823 return htab_hash_string (entry->info);
22826 inline bool
22827 macinfo_entry_hasher::equal (const macinfo_entry *entry1,
22828 const macinfo_entry *entry2)
22830 return !strcmp (entry1->info, entry2->info);
22833 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
22835 /* Output a single .debug_macinfo entry. */
22837 static void
22838 output_macinfo_op (macinfo_entry *ref)
22840 int file_num;
22841 size_t len;
22842 struct indirect_string_node *node;
22843 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22844 struct dwarf_file_data *fd;
22846 switch (ref->code)
22848 case DW_MACINFO_start_file:
22849 fd = lookup_filename (ref->info);
22850 file_num = maybe_emit_file (fd);
22851 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
22852 dw2_asm_output_data_uleb128 (ref->lineno,
22853 "Included from line number %lu",
22854 (unsigned long) ref->lineno);
22855 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
22856 break;
22857 case DW_MACINFO_end_file:
22858 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
22859 break;
22860 case DW_MACINFO_define:
22861 case DW_MACINFO_undef:
22862 len = strlen (ref->info) + 1;
22863 if (!dwarf_strict
22864 && len > DWARF_OFFSET_SIZE
22865 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
22866 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
22868 ref->code = ref->code == DW_MACINFO_define
22869 ? DW_MACRO_GNU_define_indirect
22870 : DW_MACRO_GNU_undef_indirect;
22871 output_macinfo_op (ref);
22872 return;
22874 dw2_asm_output_data (1, ref->code,
22875 ref->code == DW_MACINFO_define
22876 ? "Define macro" : "Undefine macro");
22877 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
22878 (unsigned long) ref->lineno);
22879 dw2_asm_output_nstring (ref->info, -1, "The macro");
22880 break;
22881 case DW_MACRO_GNU_define_indirect:
22882 case DW_MACRO_GNU_undef_indirect:
22883 node = find_AT_string (ref->info);
22884 gcc_assert (node
22885 && ((node->form == DW_FORM_strp)
22886 || (node->form == DW_FORM_GNU_str_index)));
22887 dw2_asm_output_data (1, ref->code,
22888 ref->code == DW_MACRO_GNU_define_indirect
22889 ? "Define macro indirect"
22890 : "Undefine macro indirect");
22891 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
22892 (unsigned long) ref->lineno);
22893 if (node->form == DW_FORM_strp)
22894 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
22895 debug_str_section, "The macro: \"%s\"",
22896 ref->info);
22897 else
22898 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
22899 ref->info);
22900 break;
22901 case DW_MACRO_GNU_transparent_include:
22902 dw2_asm_output_data (1, ref->code, "Transparent include");
22903 ASM_GENERATE_INTERNAL_LABEL (label,
22904 DEBUG_MACRO_SECTION_LABEL, ref->lineno);
22905 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
22906 break;
22907 default:
22908 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
22909 ASM_COMMENT_START, (unsigned long) ref->code);
22910 break;
22914 /* Attempt to make a sequence of define/undef macinfo ops shareable with
22915 other compilation unit .debug_macinfo sections. IDX is the first
22916 index of a define/undef, return the number of ops that should be
22917 emitted in a comdat .debug_macinfo section and emit
22918 a DW_MACRO_GNU_transparent_include entry referencing it.
22919 If the define/undef entry should be emitted normally, return 0. */
22921 static unsigned
22922 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
22923 macinfo_hash_type **macinfo_htab)
22925 macinfo_entry *first, *second, *cur, *inc;
22926 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
22927 unsigned char checksum[16];
22928 struct md5_ctx ctx;
22929 char *grp_name, *tail;
22930 const char *base;
22931 unsigned int i, count, encoded_filename_len, linebuf_len;
22932 macinfo_entry **slot;
22934 first = &(*macinfo_table)[idx];
22935 second = &(*macinfo_table)[idx + 1];
22937 /* Optimize only if there are at least two consecutive define/undef ops,
22938 and either all of them are before first DW_MACINFO_start_file
22939 with lineno {0,1} (i.e. predefined macro block), or all of them are
22940 in some included header file. */
22941 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
22942 return 0;
22943 if (vec_safe_is_empty (files))
22945 if (first->lineno > 1 || second->lineno > 1)
22946 return 0;
22948 else if (first->lineno == 0)
22949 return 0;
22951 /* Find the last define/undef entry that can be grouped together
22952 with first and at the same time compute md5 checksum of their
22953 codes, linenumbers and strings. */
22954 md5_init_ctx (&ctx);
22955 for (i = idx; macinfo_table->iterate (i, &cur); i++)
22956 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
22957 break;
22958 else if (vec_safe_is_empty (files) && cur->lineno > 1)
22959 break;
22960 else
22962 unsigned char code = cur->code;
22963 md5_process_bytes (&code, 1, &ctx);
22964 checksum_uleb128 (cur->lineno, &ctx);
22965 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
22967 md5_finish_ctx (&ctx, checksum);
22968 count = i - idx;
22970 /* From the containing include filename (if any) pick up just
22971 usable characters from its basename. */
22972 if (vec_safe_is_empty (files))
22973 base = "";
22974 else
22975 base = lbasename (files->last ().info);
22976 for (encoded_filename_len = 0, i = 0; base[i]; i++)
22977 if (ISIDNUM (base[i]) || base[i] == '.')
22978 encoded_filename_len++;
22979 /* Count . at the end. */
22980 if (encoded_filename_len)
22981 encoded_filename_len++;
22983 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
22984 linebuf_len = strlen (linebuf);
22986 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
22987 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
22988 + 16 * 2 + 1);
22989 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
22990 tail = grp_name + 4;
22991 if (encoded_filename_len)
22993 for (i = 0; base[i]; i++)
22994 if (ISIDNUM (base[i]) || base[i] == '.')
22995 *tail++ = base[i];
22996 *tail++ = '.';
22998 memcpy (tail, linebuf, linebuf_len);
22999 tail += linebuf_len;
23000 *tail++ = '.';
23001 for (i = 0; i < 16; i++)
23002 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
23004 /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
23005 in the empty vector entry before the first define/undef. */
23006 inc = &(*macinfo_table)[idx - 1];
23007 inc->code = DW_MACRO_GNU_transparent_include;
23008 inc->lineno = 0;
23009 inc->info = ggc_strdup (grp_name);
23010 if (!*macinfo_htab)
23011 *macinfo_htab = new macinfo_hash_type (10);
23012 /* Avoid emitting duplicates. */
23013 slot = (*macinfo_htab)->find_slot (inc, INSERT);
23014 if (*slot != NULL)
23016 inc->code = 0;
23017 inc->info = NULL;
23018 /* If such an entry has been used before, just emit
23019 a DW_MACRO_GNU_transparent_include op. */
23020 inc = *slot;
23021 output_macinfo_op (inc);
23022 /* And clear all macinfo_entry in the range to avoid emitting them
23023 in the second pass. */
23024 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
23026 cur->code = 0;
23027 cur->info = NULL;
23030 else
23032 *slot = inc;
23033 inc->lineno = (*macinfo_htab)->elements ();
23034 output_macinfo_op (inc);
23036 return count;
23039 /* Save any strings needed by the macinfo table in the debug str
23040 table. All strings must be collected into the table by the time
23041 index_string is called. */
23043 static void
23044 save_macinfo_strings (void)
23046 unsigned len;
23047 unsigned i;
23048 macinfo_entry *ref;
23050 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
23052 switch (ref->code)
23054 /* Match the logic in output_macinfo_op to decide on
23055 indirect strings. */
23056 case DW_MACINFO_define:
23057 case DW_MACINFO_undef:
23058 len = strlen (ref->info) + 1;
23059 if (!dwarf_strict
23060 && len > DWARF_OFFSET_SIZE
23061 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
23062 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
23063 set_indirect_string (find_AT_string (ref->info));
23064 break;
23065 case DW_MACRO_GNU_define_indirect:
23066 case DW_MACRO_GNU_undef_indirect:
23067 set_indirect_string (find_AT_string (ref->info));
23068 break;
23069 default:
23070 break;
23075 /* Output macinfo section(s). */
23077 static void
23078 output_macinfo (void)
23080 unsigned i;
23081 unsigned long length = vec_safe_length (macinfo_table);
23082 macinfo_entry *ref;
23083 vec<macinfo_entry, va_gc> *files = NULL;
23084 macinfo_hash_type *macinfo_htab = NULL;
23086 if (! length)
23087 return;
23089 /* output_macinfo* uses these interchangeably. */
23090 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
23091 && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
23092 && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
23093 && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
23095 /* For .debug_macro emit the section header. */
23096 if (!dwarf_strict)
23098 dw2_asm_output_data (2, 4, "DWARF macro version number");
23099 if (DWARF_OFFSET_SIZE == 8)
23100 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
23101 else
23102 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
23103 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
23104 (!dwarf_split_debug_info ? debug_line_section_label
23105 : debug_skeleton_line_section_label),
23106 debug_line_section, NULL);
23109 /* In the first loop, it emits the primary .debug_macinfo section
23110 and after each emitted op the macinfo_entry is cleared.
23111 If a longer range of define/undef ops can be optimized using
23112 DW_MACRO_GNU_transparent_include, the
23113 DW_MACRO_GNU_transparent_include op is emitted and kept in
23114 the vector before the first define/undef in the range and the
23115 whole range of define/undef ops is not emitted and kept. */
23116 for (i = 0; macinfo_table->iterate (i, &ref); i++)
23118 switch (ref->code)
23120 case DW_MACINFO_start_file:
23121 vec_safe_push (files, *ref);
23122 break;
23123 case DW_MACINFO_end_file:
23124 if (!vec_safe_is_empty (files))
23125 files->pop ();
23126 break;
23127 case DW_MACINFO_define:
23128 case DW_MACINFO_undef:
23129 if (!dwarf_strict
23130 && HAVE_COMDAT_GROUP
23131 && vec_safe_length (files) != 1
23132 && i > 0
23133 && i + 1 < length
23134 && (*macinfo_table)[i - 1].code == 0)
23136 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
23137 if (count)
23139 i += count - 1;
23140 continue;
23143 break;
23144 case 0:
23145 /* A dummy entry may be inserted at the beginning to be able
23146 to optimize the whole block of predefined macros. */
23147 if (i == 0)
23148 continue;
23149 default:
23150 break;
23152 output_macinfo_op (ref);
23153 ref->info = NULL;
23154 ref->code = 0;
23157 if (!macinfo_htab)
23158 return;
23160 delete macinfo_htab;
23161 macinfo_htab = NULL;
23163 /* If any DW_MACRO_GNU_transparent_include were used, on those
23164 DW_MACRO_GNU_transparent_include entries terminate the
23165 current chain and switch to a new comdat .debug_macinfo
23166 section and emit the define/undef entries within it. */
23167 for (i = 0; macinfo_table->iterate (i, &ref); i++)
23168 switch (ref->code)
23170 case 0:
23171 continue;
23172 case DW_MACRO_GNU_transparent_include:
23174 char label[MAX_ARTIFICIAL_LABEL_BYTES];
23175 tree comdat_key = get_identifier (ref->info);
23176 /* Terminate the previous .debug_macinfo section. */
23177 dw2_asm_output_data (1, 0, "End compilation unit");
23178 targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
23179 SECTION_DEBUG
23180 | SECTION_LINKONCE,
23181 comdat_key);
23182 ASM_GENERATE_INTERNAL_LABEL (label,
23183 DEBUG_MACRO_SECTION_LABEL,
23184 ref->lineno);
23185 ASM_OUTPUT_LABEL (asm_out_file, label);
23186 ref->code = 0;
23187 ref->info = NULL;
23188 dw2_asm_output_data (2, 4, "DWARF macro version number");
23189 if (DWARF_OFFSET_SIZE == 8)
23190 dw2_asm_output_data (1, 1, "Flags: 64-bit");
23191 else
23192 dw2_asm_output_data (1, 0, "Flags: 32-bit");
23194 break;
23195 case DW_MACINFO_define:
23196 case DW_MACINFO_undef:
23197 output_macinfo_op (ref);
23198 ref->code = 0;
23199 ref->info = NULL;
23200 break;
23201 default:
23202 gcc_unreachable ();
23206 /* Set up for Dwarf output at the start of compilation. */
23208 static void
23209 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
23211 /* This option is currently broken, see (PR53118 and PR46102). */
23212 if (flag_eliminate_dwarf2_dups
23213 && strstr (lang_hooks.name, "C++"))
23215 warning (0, "-feliminate-dwarf2-dups is broken for C++, ignoring");
23216 flag_eliminate_dwarf2_dups = 0;
23219 /* Allocate the file_table. */
23220 file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
23222 #ifndef DWARF2_LINENO_DEBUGGING_INFO
23223 /* Allocate the decl_die_table. */
23224 decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
23226 /* Allocate the decl_loc_table. */
23227 decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
23229 /* Allocate the cached_dw_loc_list_table. */
23230 cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
23232 /* Allocate the initial hunk of the decl_scope_table. */
23233 vec_alloc (decl_scope_table, 256);
23235 /* Allocate the initial hunk of the abbrev_die_table. */
23236 abbrev_die_table = ggc_cleared_vec_alloc<dw_die_ref>
23237 (ABBREV_DIE_TABLE_INCREMENT);
23238 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
23239 /* Zero-th entry is allocated, but unused. */
23240 abbrev_die_table_in_use = 1;
23242 /* Allocate the pubtypes and pubnames vectors. */
23243 vec_alloc (pubname_table, 32);
23244 vec_alloc (pubtype_table, 32);
23246 vec_alloc (incomplete_types, 64);
23248 vec_alloc (used_rtx_array, 32);
23250 if (!dwarf_split_debug_info)
23252 debug_info_section = get_section (DEBUG_INFO_SECTION,
23253 SECTION_DEBUG, NULL);
23254 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
23255 SECTION_DEBUG, NULL);
23256 debug_loc_section = get_section (DEBUG_LOC_SECTION,
23257 SECTION_DEBUG, NULL);
23259 else
23261 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
23262 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
23263 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
23264 SECTION_DEBUG | SECTION_EXCLUDE,
23265 NULL);
23266 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
23267 SECTION_DEBUG, NULL);
23268 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
23269 SECTION_DEBUG, NULL);
23270 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
23271 SECTION_DEBUG, NULL);
23272 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
23273 DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
23275 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
23276 the main .o, but the skeleton_line goes into the split off dwo. */
23277 debug_skeleton_line_section
23278 = get_section (DEBUG_DWO_LINE_SECTION,
23279 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
23280 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
23281 DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
23282 debug_str_offsets_section = get_section (DEBUG_STR_OFFSETS_SECTION,
23283 SECTION_DEBUG | SECTION_EXCLUDE,
23284 NULL);
23285 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
23286 DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
23287 debug_loc_section = get_section (DEBUG_DWO_LOC_SECTION,
23288 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
23289 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
23290 DEBUG_STR_DWO_SECTION_FLAGS, NULL);
23292 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
23293 SECTION_DEBUG, NULL);
23294 debug_macinfo_section = get_section (dwarf_strict
23295 ? DEBUG_MACINFO_SECTION
23296 : DEBUG_MACRO_SECTION,
23297 DEBUG_MACRO_SECTION_FLAGS, NULL);
23298 debug_line_section = get_section (DEBUG_LINE_SECTION,
23299 SECTION_DEBUG, NULL);
23300 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
23301 SECTION_DEBUG, NULL);
23302 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
23303 SECTION_DEBUG, NULL);
23304 debug_str_section = get_section (DEBUG_STR_SECTION,
23305 DEBUG_STR_SECTION_FLAGS, NULL);
23306 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
23307 SECTION_DEBUG, NULL);
23308 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
23309 SECTION_DEBUG, NULL);
23311 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
23312 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
23313 DEBUG_ABBREV_SECTION_LABEL, 0);
23314 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
23315 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
23316 COLD_TEXT_SECTION_LABEL, 0);
23317 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
23319 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
23320 DEBUG_INFO_SECTION_LABEL, 0);
23321 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
23322 DEBUG_LINE_SECTION_LABEL, 0);
23323 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
23324 DEBUG_RANGES_SECTION_LABEL, 0);
23325 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
23326 DEBUG_ADDR_SECTION_LABEL, 0);
23327 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
23328 dwarf_strict
23329 ? DEBUG_MACINFO_SECTION_LABEL
23330 : DEBUG_MACRO_SECTION_LABEL, 0);
23331 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
23333 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
23334 vec_alloc (macinfo_table, 64);
23336 switch_to_section (text_section);
23337 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
23338 #endif
23340 /* Make sure the line number table for .text always exists. */
23341 text_section_line_info = new_line_info_table ();
23342 text_section_line_info->end_label = text_end_label;
23344 #ifdef DWARF2_LINENO_DEBUGGING_INFO
23345 cur_line_info_table = text_section_line_info;
23346 #endif
23348 /* If front-ends already registered a main translation unit but we were not
23349 ready to perform the association, do this now. */
23350 if (main_translation_unit != NULL_TREE)
23351 equate_decl_number_to_die (main_translation_unit, comp_unit_die ());
23354 /* Called before compile () starts outputtting functions, variables
23355 and toplevel asms into assembly. */
23357 static void
23358 dwarf2out_assembly_start (void)
23360 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
23361 && dwarf2out_do_cfi_asm ()
23362 && (!(flag_unwind_tables || flag_exceptions)
23363 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
23364 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
23367 /* A helper function for dwarf2out_finish called through
23368 htab_traverse. Assign a string its index. All strings must be
23369 collected into the table by the time index_string is called,
23370 because the indexing code relies on htab_traverse to traverse nodes
23371 in the same order for each run. */
23374 index_string (indirect_string_node **h, unsigned int *index)
23376 indirect_string_node *node = *h;
23378 find_string_form (node);
23379 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
23381 gcc_assert (node->index == NO_INDEX_ASSIGNED);
23382 node->index = *index;
23383 *index += 1;
23385 return 1;
23388 /* A helper function for output_indirect_strings called through
23389 htab_traverse. Output the offset to a string and update the
23390 current offset. */
23393 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
23395 indirect_string_node *node = *h;
23397 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
23399 /* Assert that this node has been assigned an index. */
23400 gcc_assert (node->index != NO_INDEX_ASSIGNED
23401 && node->index != NOT_INDEXED);
23402 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
23403 "indexed string 0x%x: %s", node->index, node->str);
23404 *offset += strlen (node->str) + 1;
23406 return 1;
23409 /* A helper function for dwarf2out_finish called through
23410 htab_traverse. Output the indexed string. */
23413 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
23415 struct indirect_string_node *node = *h;
23417 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
23419 /* Assert that the strings are output in the same order as their
23420 indexes were assigned. */
23421 gcc_assert (*cur_idx == node->index);
23422 assemble_string (node->str, strlen (node->str) + 1);
23423 *cur_idx += 1;
23425 return 1;
23428 /* A helper function for dwarf2out_finish called through
23429 htab_traverse. Emit one queued .debug_str string. */
23432 output_indirect_string (indirect_string_node **h, void *)
23434 struct indirect_string_node *node = *h;
23436 node->form = find_string_form (node);
23437 if (node->form == DW_FORM_strp && node->refcount > 0)
23439 ASM_OUTPUT_LABEL (asm_out_file, node->label);
23440 assemble_string (node->str, strlen (node->str) + 1);
23443 return 1;
23446 /* Output the indexed string table. */
23448 static void
23449 output_indirect_strings (void)
23451 switch_to_section (debug_str_section);
23452 if (!dwarf_split_debug_info)
23453 debug_str_hash->traverse<void *, output_indirect_string> (NULL);
23454 else
23456 unsigned int offset = 0;
23457 unsigned int cur_idx = 0;
23459 skeleton_debug_str_hash->traverse<void *, output_indirect_string> (NULL);
23461 switch_to_section (debug_str_offsets_section);
23462 debug_str_hash->traverse_noresize
23463 <unsigned int *, output_index_string_offset> (&offset);
23464 switch_to_section (debug_str_dwo_section);
23465 debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
23466 (&cur_idx);
23470 /* Callback for htab_traverse to assign an index to an entry in the
23471 table, and to write that entry to the .debug_addr section. */
23474 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
23476 addr_table_entry *entry = *slot;
23478 if (entry->refcount == 0)
23480 gcc_assert (entry->index == NO_INDEX_ASSIGNED
23481 || entry->index == NOT_INDEXED);
23482 return 1;
23485 gcc_assert (entry->index == *cur_index);
23486 (*cur_index)++;
23488 switch (entry->kind)
23490 case ate_kind_rtx:
23491 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
23492 "0x%x", entry->index);
23493 break;
23494 case ate_kind_rtx_dtprel:
23495 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
23496 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
23497 DWARF2_ADDR_SIZE,
23498 entry->addr.rtl);
23499 fputc ('\n', asm_out_file);
23500 break;
23501 case ate_kind_label:
23502 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
23503 "0x%x", entry->index);
23504 break;
23505 default:
23506 gcc_unreachable ();
23508 return 1;
23511 /* Produce the .debug_addr section. */
23513 static void
23514 output_addr_table (void)
23516 unsigned int index = 0;
23517 if (addr_index_table == NULL || addr_index_table->size () == 0)
23518 return;
23520 switch_to_section (debug_addr_section);
23521 addr_index_table
23522 ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
23525 #if ENABLE_ASSERT_CHECKING
23526 /* Verify that all marks are clear. */
23528 static void
23529 verify_marks_clear (dw_die_ref die)
23531 dw_die_ref c;
23533 gcc_assert (! die->die_mark);
23534 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
23536 #endif /* ENABLE_ASSERT_CHECKING */
23538 /* Clear the marks for a die and its children.
23539 Be cool if the mark isn't set. */
23541 static void
23542 prune_unmark_dies (dw_die_ref die)
23544 dw_die_ref c;
23546 if (die->die_mark)
23547 die->die_mark = 0;
23548 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
23551 /* Given DIE that we're marking as used, find any other dies
23552 it references as attributes and mark them as used. */
23554 static void
23555 prune_unused_types_walk_attribs (dw_die_ref die)
23557 dw_attr_ref a;
23558 unsigned ix;
23560 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23562 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
23564 /* A reference to another DIE.
23565 Make sure that it will get emitted.
23566 If it was broken out into a comdat group, don't follow it. */
23567 if (! AT_ref (a)->comdat_type_p
23568 || a->dw_attr == DW_AT_specification)
23569 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
23571 /* Set the string's refcount to 0 so that prune_unused_types_mark
23572 accounts properly for it. */
23573 if (AT_class (a) == dw_val_class_str)
23574 a->dw_attr_val.v.val_str->refcount = 0;
23578 /* Mark the generic parameters and arguments children DIEs of DIE. */
23580 static void
23581 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
23583 dw_die_ref c;
23585 if (die == NULL || die->die_child == NULL)
23586 return;
23587 c = die->die_child;
23590 if (is_template_parameter (c))
23591 prune_unused_types_mark (c, 1);
23592 c = c->die_sib;
23593 } while (c && c != die->die_child);
23596 /* Mark DIE as being used. If DOKIDS is true, then walk down
23597 to DIE's children. */
23599 static void
23600 prune_unused_types_mark (dw_die_ref die, int dokids)
23602 dw_die_ref c;
23604 if (die->die_mark == 0)
23606 /* We haven't done this node yet. Mark it as used. */
23607 die->die_mark = 1;
23608 /* If this is the DIE of a generic type instantiation,
23609 mark the children DIEs that describe its generic parms and
23610 args. */
23611 prune_unused_types_mark_generic_parms_dies (die);
23613 /* We also have to mark its parents as used.
23614 (But we don't want to mark our parent's kids due to this,
23615 unless it is a class.) */
23616 if (die->die_parent)
23617 prune_unused_types_mark (die->die_parent,
23618 class_scope_p (die->die_parent));
23620 /* Mark any referenced nodes. */
23621 prune_unused_types_walk_attribs (die);
23623 /* If this node is a specification,
23624 also mark the definition, if it exists. */
23625 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
23626 prune_unused_types_mark (die->die_definition, 1);
23629 if (dokids && die->die_mark != 2)
23631 /* We need to walk the children, but haven't done so yet.
23632 Remember that we've walked the kids. */
23633 die->die_mark = 2;
23635 /* If this is an array type, we need to make sure our
23636 kids get marked, even if they're types. If we're
23637 breaking out types into comdat sections, do this
23638 for all type definitions. */
23639 if (die->die_tag == DW_TAG_array_type
23640 || (use_debug_types
23641 && is_type_die (die) && ! is_declaration_die (die)))
23642 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
23643 else
23644 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
23648 /* For local classes, look if any static member functions were emitted
23649 and if so, mark them. */
23651 static void
23652 prune_unused_types_walk_local_classes (dw_die_ref die)
23654 dw_die_ref c;
23656 if (die->die_mark == 2)
23657 return;
23659 switch (die->die_tag)
23661 case DW_TAG_structure_type:
23662 case DW_TAG_union_type:
23663 case DW_TAG_class_type:
23664 break;
23666 case DW_TAG_subprogram:
23667 if (!get_AT_flag (die, DW_AT_declaration)
23668 || die->die_definition != NULL)
23669 prune_unused_types_mark (die, 1);
23670 return;
23672 default:
23673 return;
23676 /* Mark children. */
23677 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
23680 /* Walk the tree DIE and mark types that we actually use. */
23682 static void
23683 prune_unused_types_walk (dw_die_ref die)
23685 dw_die_ref c;
23687 /* Don't do anything if this node is already marked and
23688 children have been marked as well. */
23689 if (die->die_mark == 2)
23690 return;
23692 switch (die->die_tag)
23694 case DW_TAG_structure_type:
23695 case DW_TAG_union_type:
23696 case DW_TAG_class_type:
23697 if (die->die_perennial_p)
23698 break;
23700 for (c = die->die_parent; c; c = c->die_parent)
23701 if (c->die_tag == DW_TAG_subprogram)
23702 break;
23704 /* Finding used static member functions inside of classes
23705 is needed just for local classes, because for other classes
23706 static member function DIEs with DW_AT_specification
23707 are emitted outside of the DW_TAG_*_type. If we ever change
23708 it, we'd need to call this even for non-local classes. */
23709 if (c)
23710 prune_unused_types_walk_local_classes (die);
23712 /* It's a type node --- don't mark it. */
23713 return;
23715 case DW_TAG_const_type:
23716 case DW_TAG_packed_type:
23717 case DW_TAG_pointer_type:
23718 case DW_TAG_reference_type:
23719 case DW_TAG_rvalue_reference_type:
23720 case DW_TAG_volatile_type:
23721 case DW_TAG_typedef:
23722 case DW_TAG_array_type:
23723 case DW_TAG_interface_type:
23724 case DW_TAG_friend:
23725 case DW_TAG_variant_part:
23726 case DW_TAG_enumeration_type:
23727 case DW_TAG_subroutine_type:
23728 case DW_TAG_string_type:
23729 case DW_TAG_set_type:
23730 case DW_TAG_subrange_type:
23731 case DW_TAG_ptr_to_member_type:
23732 case DW_TAG_file_type:
23733 if (die->die_perennial_p)
23734 break;
23736 /* It's a type node --- don't mark it. */
23737 return;
23739 default:
23740 /* Mark everything else. */
23741 break;
23744 if (die->die_mark == 0)
23746 die->die_mark = 1;
23748 /* Now, mark any dies referenced from here. */
23749 prune_unused_types_walk_attribs (die);
23752 die->die_mark = 2;
23754 /* Mark children. */
23755 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
23758 /* Increment the string counts on strings referred to from DIE's
23759 attributes. */
23761 static void
23762 prune_unused_types_update_strings (dw_die_ref die)
23764 dw_attr_ref a;
23765 unsigned ix;
23767 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23768 if (AT_class (a) == dw_val_class_str)
23770 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
23771 s->refcount++;
23772 /* Avoid unnecessarily putting strings that are used less than
23773 twice in the hash table. */
23774 if (s->refcount
23775 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
23777 indirect_string_node **slot
23778 = debug_str_hash->find_slot_with_hash (s->str,
23779 htab_hash_string (s->str),
23780 INSERT);
23781 gcc_assert (*slot == NULL);
23782 *slot = s;
23787 /* Remove from the tree DIE any dies that aren't marked. */
23789 static void
23790 prune_unused_types_prune (dw_die_ref die)
23792 dw_die_ref c;
23794 gcc_assert (die->die_mark);
23795 prune_unused_types_update_strings (die);
23797 if (! die->die_child)
23798 return;
23800 c = die->die_child;
23801 do {
23802 dw_die_ref prev = c;
23803 for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
23804 if (c == die->die_child)
23806 /* No marked children between 'prev' and the end of the list. */
23807 if (prev == c)
23808 /* No marked children at all. */
23809 die->die_child = NULL;
23810 else
23812 prev->die_sib = c->die_sib;
23813 die->die_child = prev;
23815 return;
23818 if (c != prev->die_sib)
23819 prev->die_sib = c;
23820 prune_unused_types_prune (c);
23821 } while (c != die->die_child);
23824 /* Remove dies representing declarations that we never use. */
23826 static void
23827 prune_unused_types (void)
23829 unsigned int i;
23830 limbo_die_node *node;
23831 comdat_type_node *ctnode;
23832 pubname_ref pub;
23833 dw_die_ref base_type;
23835 #if ENABLE_ASSERT_CHECKING
23836 /* All the marks should already be clear. */
23837 verify_marks_clear (comp_unit_die ());
23838 for (node = limbo_die_list; node; node = node->next)
23839 verify_marks_clear (node->die);
23840 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23841 verify_marks_clear (ctnode->root_die);
23842 #endif /* ENABLE_ASSERT_CHECKING */
23844 /* Mark types that are used in global variables. */
23845 premark_types_used_by_global_vars ();
23847 /* Set the mark on nodes that are actually used. */
23848 prune_unused_types_walk (comp_unit_die ());
23849 for (node = limbo_die_list; node; node = node->next)
23850 prune_unused_types_walk (node->die);
23851 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23853 prune_unused_types_walk (ctnode->root_die);
23854 prune_unused_types_mark (ctnode->type_die, 1);
23857 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
23858 are unusual in that they are pubnames that are the children of pubtypes.
23859 They should only be marked via their parent DW_TAG_enumeration_type die,
23860 not as roots in themselves. */
23861 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
23862 if (pub->die->die_tag != DW_TAG_enumerator)
23863 prune_unused_types_mark (pub->die, 1);
23864 for (i = 0; base_types.iterate (i, &base_type); i++)
23865 prune_unused_types_mark (base_type, 1);
23867 if (debug_str_hash)
23868 debug_str_hash->empty ();
23869 if (skeleton_debug_str_hash)
23870 skeleton_debug_str_hash->empty ();
23871 prune_unused_types_prune (comp_unit_die ());
23872 for (limbo_die_node **pnode = &limbo_die_list; *pnode; )
23874 node = *pnode;
23875 if (!node->die->die_mark)
23876 *pnode = node->next;
23877 else
23879 prune_unused_types_prune (node->die);
23880 pnode = &node->next;
23883 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23884 prune_unused_types_prune (ctnode->root_die);
23886 /* Leave the marks clear. */
23887 prune_unmark_dies (comp_unit_die ());
23888 for (node = limbo_die_list; node; node = node->next)
23889 prune_unmark_dies (node->die);
23890 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23891 prune_unmark_dies (ctnode->root_die);
23894 /* Set the parameter to true if there are any relative pathnames in
23895 the file table. */
23897 file_table_relative_p (dwarf_file_data **slot, bool *p)
23899 struct dwarf_file_data *d = *slot;
23900 if (!IS_ABSOLUTE_PATH (d->filename))
23902 *p = true;
23903 return 0;
23905 return 1;
23908 /* Helpers to manipulate hash table of comdat type units. */
23910 struct comdat_type_hasher : nofree_ptr_hash <comdat_type_node>
23912 static inline hashval_t hash (const comdat_type_node *);
23913 static inline bool equal (const comdat_type_node *, const comdat_type_node *);
23916 inline hashval_t
23917 comdat_type_hasher::hash (const comdat_type_node *type_node)
23919 hashval_t h;
23920 memcpy (&h, type_node->signature, sizeof (h));
23921 return h;
23924 inline bool
23925 comdat_type_hasher::equal (const comdat_type_node *type_node_1,
23926 const comdat_type_node *type_node_2)
23928 return (! memcmp (type_node_1->signature, type_node_2->signature,
23929 DWARF_TYPE_SIGNATURE_SIZE));
23932 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
23933 to the location it would have been added, should we know its
23934 DECL_ASSEMBLER_NAME when we added other attributes. This will
23935 probably improve compactness of debug info, removing equivalent
23936 abbrevs, and hide any differences caused by deferring the
23937 computation of the assembler name, triggered by e.g. PCH. */
23939 static inline void
23940 move_linkage_attr (dw_die_ref die)
23942 unsigned ix = vec_safe_length (die->die_attr);
23943 dw_attr_node linkage = (*die->die_attr)[ix - 1];
23945 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
23946 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
23948 while (--ix > 0)
23950 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
23952 if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
23953 break;
23956 if (ix != vec_safe_length (die->die_attr) - 1)
23958 die->die_attr->pop ();
23959 die->die_attr->quick_insert (ix, linkage);
23963 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
23964 referenced from typed stack ops and count how often they are used. */
23966 static void
23967 mark_base_types (dw_loc_descr_ref loc)
23969 dw_die_ref base_type = NULL;
23971 for (; loc; loc = loc->dw_loc_next)
23973 switch (loc->dw_loc_opc)
23975 case DW_OP_GNU_regval_type:
23976 case DW_OP_GNU_deref_type:
23977 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
23978 break;
23979 case DW_OP_GNU_convert:
23980 case DW_OP_GNU_reinterpret:
23981 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
23982 continue;
23983 /* FALLTHRU */
23984 case DW_OP_GNU_const_type:
23985 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
23986 break;
23987 case DW_OP_GNU_entry_value:
23988 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
23989 continue;
23990 default:
23991 continue;
23993 gcc_assert (base_type->die_parent == comp_unit_die ());
23994 if (base_type->die_mark)
23995 base_type->die_mark++;
23996 else
23998 base_types.safe_push (base_type);
23999 base_type->die_mark = 1;
24004 /* Comparison function for sorting marked base types. */
24006 static int
24007 base_type_cmp (const void *x, const void *y)
24009 dw_die_ref dx = *(const dw_die_ref *) x;
24010 dw_die_ref dy = *(const dw_die_ref *) y;
24011 unsigned int byte_size1, byte_size2;
24012 unsigned int encoding1, encoding2;
24013 if (dx->die_mark > dy->die_mark)
24014 return -1;
24015 if (dx->die_mark < dy->die_mark)
24016 return 1;
24017 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
24018 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
24019 if (byte_size1 < byte_size2)
24020 return 1;
24021 if (byte_size1 > byte_size2)
24022 return -1;
24023 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
24024 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
24025 if (encoding1 < encoding2)
24026 return 1;
24027 if (encoding1 > encoding2)
24028 return -1;
24029 return 0;
24032 /* Move base types marked by mark_base_types as early as possible
24033 in the CU, sorted by decreasing usage count both to make the
24034 uleb128 references as small as possible and to make sure they
24035 will have die_offset already computed by calc_die_sizes when
24036 sizes of typed stack loc ops is computed. */
24038 static void
24039 move_marked_base_types (void)
24041 unsigned int i;
24042 dw_die_ref base_type, die, c;
24044 if (base_types.is_empty ())
24045 return;
24047 /* Sort by decreasing usage count, they will be added again in that
24048 order later on. */
24049 base_types.qsort (base_type_cmp);
24050 die = comp_unit_die ();
24051 c = die->die_child;
24054 dw_die_ref prev = c;
24055 c = c->die_sib;
24056 while (c->die_mark)
24058 remove_child_with_prev (c, prev);
24059 /* As base types got marked, there must be at least
24060 one node other than DW_TAG_base_type. */
24061 gcc_assert (c != c->die_sib);
24062 c = c->die_sib;
24065 while (c != die->die_child);
24066 gcc_assert (die->die_child);
24067 c = die->die_child;
24068 for (i = 0; base_types.iterate (i, &base_type); i++)
24070 base_type->die_mark = 0;
24071 base_type->die_sib = c->die_sib;
24072 c->die_sib = base_type;
24073 c = base_type;
24077 /* Helper function for resolve_addr, attempt to resolve
24078 one CONST_STRING, return true if successful. Similarly verify that
24079 SYMBOL_REFs refer to variables emitted in the current CU. */
24081 static bool
24082 resolve_one_addr (rtx *addr)
24084 rtx rtl = *addr;
24086 if (GET_CODE (rtl) == CONST_STRING)
24088 size_t len = strlen (XSTR (rtl, 0)) + 1;
24089 tree t = build_string (len, XSTR (rtl, 0));
24090 tree tlen = size_int (len - 1);
24091 TREE_TYPE (t)
24092 = build_array_type (char_type_node, build_index_type (tlen));
24093 rtl = lookup_constant_def (t);
24094 if (!rtl || !MEM_P (rtl))
24095 return false;
24096 rtl = XEXP (rtl, 0);
24097 if (GET_CODE (rtl) == SYMBOL_REF
24098 && SYMBOL_REF_DECL (rtl)
24099 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
24100 return false;
24101 vec_safe_push (used_rtx_array, rtl);
24102 *addr = rtl;
24103 return true;
24106 if (GET_CODE (rtl) == SYMBOL_REF
24107 && SYMBOL_REF_DECL (rtl))
24109 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
24111 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
24112 return false;
24114 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
24115 return false;
24118 if (GET_CODE (rtl) == CONST)
24120 subrtx_ptr_iterator::array_type array;
24121 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
24122 if (!resolve_one_addr (*iter))
24123 return false;
24126 return true;
24129 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
24130 if possible, and create DW_TAG_dwarf_procedure that can be referenced
24131 from DW_OP_GNU_implicit_pointer if the string hasn't been seen yet. */
24133 static rtx
24134 string_cst_pool_decl (tree t)
24136 rtx rtl = output_constant_def (t, 1);
24137 unsigned char *array;
24138 dw_loc_descr_ref l;
24139 tree decl;
24140 size_t len;
24141 dw_die_ref ref;
24143 if (!rtl || !MEM_P (rtl))
24144 return NULL_RTX;
24145 rtl = XEXP (rtl, 0);
24146 if (GET_CODE (rtl) != SYMBOL_REF
24147 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
24148 return NULL_RTX;
24150 decl = SYMBOL_REF_DECL (rtl);
24151 if (!lookup_decl_die (decl))
24153 len = TREE_STRING_LENGTH (t);
24154 vec_safe_push (used_rtx_array, rtl);
24155 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
24156 array = ggc_vec_alloc<unsigned char> (len);
24157 memcpy (array, TREE_STRING_POINTER (t), len);
24158 l = new_loc_descr (DW_OP_implicit_value, len, 0);
24159 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
24160 l->dw_loc_oprnd2.v.val_vec.length = len;
24161 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
24162 l->dw_loc_oprnd2.v.val_vec.array = array;
24163 add_AT_loc (ref, DW_AT_location, l);
24164 equate_decl_number_to_die (decl, ref);
24166 return rtl;
24169 /* Helper function of resolve_addr_in_expr. LOC is
24170 a DW_OP_addr followed by DW_OP_stack_value, either at the start
24171 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
24172 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
24173 with DW_OP_GNU_implicit_pointer if possible
24174 and return true, if unsuccessful, return false. */
24176 static bool
24177 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
24179 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
24180 HOST_WIDE_INT offset = 0;
24181 dw_die_ref ref = NULL;
24182 tree decl;
24184 if (GET_CODE (rtl) == CONST
24185 && GET_CODE (XEXP (rtl, 0)) == PLUS
24186 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
24188 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
24189 rtl = XEXP (XEXP (rtl, 0), 0);
24191 if (GET_CODE (rtl) == CONST_STRING)
24193 size_t len = strlen (XSTR (rtl, 0)) + 1;
24194 tree t = build_string (len, XSTR (rtl, 0));
24195 tree tlen = size_int (len - 1);
24197 TREE_TYPE (t)
24198 = build_array_type (char_type_node, build_index_type (tlen));
24199 rtl = string_cst_pool_decl (t);
24200 if (!rtl)
24201 return false;
24203 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
24205 decl = SYMBOL_REF_DECL (rtl);
24206 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
24208 ref = lookup_decl_die (decl);
24209 if (ref && (get_AT (ref, DW_AT_location)
24210 || get_AT (ref, DW_AT_const_value)))
24212 loc->dw_loc_opc = DW_OP_GNU_implicit_pointer;
24213 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
24214 loc->dw_loc_oprnd1.val_entry = NULL;
24215 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
24216 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
24217 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
24218 loc->dw_loc_oprnd2.v.val_int = offset;
24219 return true;
24223 return false;
24226 /* Helper function for resolve_addr, handle one location
24227 expression, return false if at least one CONST_STRING or SYMBOL_REF in
24228 the location list couldn't be resolved. */
24230 static bool
24231 resolve_addr_in_expr (dw_loc_descr_ref loc)
24233 dw_loc_descr_ref keep = NULL;
24234 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
24235 switch (loc->dw_loc_opc)
24237 case DW_OP_addr:
24238 if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
24240 if ((prev == NULL
24241 || prev->dw_loc_opc == DW_OP_piece
24242 || prev->dw_loc_opc == DW_OP_bit_piece)
24243 && loc->dw_loc_next
24244 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
24245 && !dwarf_strict
24246 && optimize_one_addr_into_implicit_ptr (loc))
24247 break;
24248 return false;
24250 break;
24251 case DW_OP_GNU_addr_index:
24252 case DW_OP_GNU_const_index:
24253 if (loc->dw_loc_opc == DW_OP_GNU_addr_index
24254 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
24256 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
24257 if (!resolve_one_addr (&rtl))
24258 return false;
24259 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
24260 loc->dw_loc_oprnd1.val_entry =
24261 add_addr_table_entry (rtl, ate_kind_rtx);
24263 break;
24264 case DW_OP_const4u:
24265 case DW_OP_const8u:
24266 if (loc->dtprel
24267 && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
24268 return false;
24269 break;
24270 case DW_OP_plus_uconst:
24271 if (size_of_loc_descr (loc)
24272 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
24274 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
24276 dw_loc_descr_ref repl
24277 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
24278 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
24279 add_loc_descr (&repl, loc->dw_loc_next);
24280 *loc = *repl;
24282 break;
24283 case DW_OP_implicit_value:
24284 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
24285 && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
24286 return false;
24287 break;
24288 case DW_OP_GNU_implicit_pointer:
24289 case DW_OP_GNU_parameter_ref:
24290 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
24292 dw_die_ref ref
24293 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
24294 if (ref == NULL)
24295 return false;
24296 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
24297 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
24298 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
24300 break;
24301 case DW_OP_GNU_const_type:
24302 case DW_OP_GNU_regval_type:
24303 case DW_OP_GNU_deref_type:
24304 case DW_OP_GNU_convert:
24305 case DW_OP_GNU_reinterpret:
24306 while (loc->dw_loc_next
24307 && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
24309 dw_die_ref base1, base2;
24310 unsigned enc1, enc2, size1, size2;
24311 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
24312 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
24313 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
24314 else if (loc->dw_loc_oprnd1.val_class
24315 == dw_val_class_unsigned_const)
24316 break;
24317 else
24318 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
24319 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
24320 == dw_val_class_unsigned_const)
24321 break;
24322 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
24323 gcc_assert (base1->die_tag == DW_TAG_base_type
24324 && base2->die_tag == DW_TAG_base_type);
24325 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
24326 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
24327 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
24328 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
24329 if (size1 == size2
24330 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
24331 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
24332 && loc != keep)
24333 || enc1 == enc2))
24335 /* Optimize away next DW_OP_GNU_convert after
24336 adjusting LOC's base type die reference. */
24337 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
24338 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
24339 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
24340 else
24341 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
24342 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
24343 continue;
24345 /* Don't change integer DW_OP_GNU_convert after e.g. floating
24346 point typed stack entry. */
24347 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
24348 keep = loc->dw_loc_next;
24349 break;
24351 break;
24352 default:
24353 break;
24355 return true;
24358 /* Helper function of resolve_addr. DIE had DW_AT_location of
24359 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
24360 and DW_OP_addr couldn't be resolved. resolve_addr has already
24361 removed the DW_AT_location attribute. This function attempts to
24362 add a new DW_AT_location attribute with DW_OP_GNU_implicit_pointer
24363 to it or DW_AT_const_value attribute, if possible. */
24365 static void
24366 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
24368 if (TREE_CODE (decl) != VAR_DECL
24369 || lookup_decl_die (decl) != die
24370 || DECL_EXTERNAL (decl)
24371 || !TREE_STATIC (decl)
24372 || DECL_INITIAL (decl) == NULL_TREE
24373 || DECL_P (DECL_INITIAL (decl))
24374 || get_AT (die, DW_AT_const_value))
24375 return;
24377 tree init = DECL_INITIAL (decl);
24378 HOST_WIDE_INT offset = 0;
24379 /* For variables that have been optimized away and thus
24380 don't have a memory location, see if we can emit
24381 DW_AT_const_value instead. */
24382 if (tree_add_const_value_attribute (die, init))
24383 return;
24384 if (dwarf_strict)
24385 return;
24386 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
24387 and ADDR_EXPR refers to a decl that has DW_AT_location or
24388 DW_AT_const_value (but isn't addressable, otherwise
24389 resolving the original DW_OP_addr wouldn't fail), see if
24390 we can add DW_OP_GNU_implicit_pointer. */
24391 STRIP_NOPS (init);
24392 if (TREE_CODE (init) == POINTER_PLUS_EXPR
24393 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
24395 offset = tree_to_shwi (TREE_OPERAND (init, 1));
24396 init = TREE_OPERAND (init, 0);
24397 STRIP_NOPS (init);
24399 if (TREE_CODE (init) != ADDR_EXPR)
24400 return;
24401 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
24402 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
24403 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
24404 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
24405 && TREE_OPERAND (init, 0) != decl))
24407 dw_die_ref ref;
24408 dw_loc_descr_ref l;
24410 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
24412 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
24413 if (!rtl)
24414 return;
24415 decl = SYMBOL_REF_DECL (rtl);
24417 else
24418 decl = TREE_OPERAND (init, 0);
24419 ref = lookup_decl_die (decl);
24420 if (ref == NULL
24421 || (!get_AT (ref, DW_AT_location)
24422 && !get_AT (ref, DW_AT_const_value)))
24423 return;
24424 l = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
24425 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
24426 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
24427 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
24428 add_AT_loc (die, DW_AT_location, l);
24432 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
24433 an address in .rodata section if the string literal is emitted there,
24434 or remove the containing location list or replace DW_AT_const_value
24435 with DW_AT_location and empty location expression, if it isn't found
24436 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
24437 to something that has been emitted in the current CU. */
24439 static void
24440 resolve_addr (dw_die_ref die)
24442 dw_die_ref c;
24443 dw_attr_ref a;
24444 dw_loc_list_ref *curr, *start, loc;
24445 unsigned ix;
24447 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
24448 switch (AT_class (a))
24450 case dw_val_class_loc_list:
24451 start = curr = AT_loc_list_ptr (a);
24452 loc = *curr;
24453 gcc_assert (loc);
24454 /* The same list can be referenced more than once. See if we have
24455 already recorded the result from a previous pass. */
24456 if (loc->replaced)
24457 *curr = loc->dw_loc_next;
24458 else if (!loc->resolved_addr)
24460 /* As things stand, we do not expect or allow one die to
24461 reference a suffix of another die's location list chain.
24462 References must be identical or completely separate.
24463 There is therefore no need to cache the result of this
24464 pass on any list other than the first; doing so
24465 would lead to unnecessary writes. */
24466 while (*curr)
24468 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
24469 if (!resolve_addr_in_expr ((*curr)->expr))
24471 dw_loc_list_ref next = (*curr)->dw_loc_next;
24472 dw_loc_descr_ref l = (*curr)->expr;
24474 if (next && (*curr)->ll_symbol)
24476 gcc_assert (!next->ll_symbol);
24477 next->ll_symbol = (*curr)->ll_symbol;
24479 if (dwarf_split_debug_info)
24480 remove_loc_list_addr_table_entries (l);
24481 *curr = next;
24483 else
24485 mark_base_types ((*curr)->expr);
24486 curr = &(*curr)->dw_loc_next;
24489 if (loc == *start)
24490 loc->resolved_addr = 1;
24491 else
24493 loc->replaced = 1;
24494 loc->dw_loc_next = *start;
24497 if (!*start)
24499 remove_AT (die, a->dw_attr);
24500 ix--;
24502 break;
24503 case dw_val_class_loc:
24505 dw_loc_descr_ref l = AT_loc (a);
24506 /* For -gdwarf-2 don't attempt to optimize
24507 DW_AT_data_member_location containing
24508 DW_OP_plus_uconst - older consumers might
24509 rely on it being that op instead of a more complex,
24510 but shorter, location description. */
24511 if ((dwarf_version > 2
24512 || a->dw_attr != DW_AT_data_member_location
24513 || l == NULL
24514 || l->dw_loc_opc != DW_OP_plus_uconst
24515 || l->dw_loc_next != NULL)
24516 && !resolve_addr_in_expr (l))
24518 if (dwarf_split_debug_info)
24519 remove_loc_list_addr_table_entries (l);
24520 if (l != NULL
24521 && l->dw_loc_next == NULL
24522 && l->dw_loc_opc == DW_OP_addr
24523 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
24524 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
24525 && a->dw_attr == DW_AT_location)
24527 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
24528 remove_AT (die, a->dw_attr);
24529 ix--;
24530 optimize_location_into_implicit_ptr (die, decl);
24531 break;
24533 remove_AT (die, a->dw_attr);
24534 ix--;
24536 else
24537 mark_base_types (l);
24539 break;
24540 case dw_val_class_addr:
24541 if (a->dw_attr == DW_AT_const_value
24542 && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
24544 if (AT_index (a) != NOT_INDEXED)
24545 remove_addr_table_entry (a->dw_attr_val.val_entry);
24546 remove_AT (die, a->dw_attr);
24547 ix--;
24549 if (die->die_tag == DW_TAG_GNU_call_site
24550 && a->dw_attr == DW_AT_abstract_origin)
24552 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
24553 dw_die_ref tdie = lookup_decl_die (tdecl);
24554 dw_die_ref cdie;
24555 if (tdie == NULL
24556 && DECL_EXTERNAL (tdecl)
24557 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE
24558 && (cdie = lookup_context_die (DECL_CONTEXT (tdecl))))
24560 /* Creating a full DIE for tdecl is overly expensive and
24561 at this point even wrong when in the LTO phase
24562 as it can end up generating new type DIEs we didn't
24563 output and thus optimize_external_refs will crash. */
24564 tdie = new_die (DW_TAG_subprogram, cdie, NULL_TREE);
24565 add_AT_flag (tdie, DW_AT_external, 1);
24566 add_AT_flag (tdie, DW_AT_declaration, 1);
24567 add_linkage_attr (tdie, tdecl);
24568 add_name_and_src_coords_attributes (tdie, tdecl);
24569 equate_decl_number_to_die (tdecl, tdie);
24571 if (tdie)
24573 a->dw_attr_val.val_class = dw_val_class_die_ref;
24574 a->dw_attr_val.v.val_die_ref.die = tdie;
24575 a->dw_attr_val.v.val_die_ref.external = 0;
24577 else
24579 if (AT_index (a) != NOT_INDEXED)
24580 remove_addr_table_entry (a->dw_attr_val.val_entry);
24581 remove_AT (die, a->dw_attr);
24582 ix--;
24585 break;
24586 default:
24587 break;
24590 FOR_EACH_CHILD (die, c, resolve_addr (c));
24593 /* Helper routines for optimize_location_lists.
24594 This pass tries to share identical local lists in .debug_loc
24595 section. */
24597 /* Iteratively hash operands of LOC opcode into HSTATE. */
24599 static void
24600 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
24602 dw_val_ref val1 = &loc->dw_loc_oprnd1;
24603 dw_val_ref val2 = &loc->dw_loc_oprnd2;
24605 switch (loc->dw_loc_opc)
24607 case DW_OP_const4u:
24608 case DW_OP_const8u:
24609 if (loc->dtprel)
24610 goto hash_addr;
24611 /* FALLTHRU */
24612 case DW_OP_const1u:
24613 case DW_OP_const1s:
24614 case DW_OP_const2u:
24615 case DW_OP_const2s:
24616 case DW_OP_const4s:
24617 case DW_OP_const8s:
24618 case DW_OP_constu:
24619 case DW_OP_consts:
24620 case DW_OP_pick:
24621 case DW_OP_plus_uconst:
24622 case DW_OP_breg0:
24623 case DW_OP_breg1:
24624 case DW_OP_breg2:
24625 case DW_OP_breg3:
24626 case DW_OP_breg4:
24627 case DW_OP_breg5:
24628 case DW_OP_breg6:
24629 case DW_OP_breg7:
24630 case DW_OP_breg8:
24631 case DW_OP_breg9:
24632 case DW_OP_breg10:
24633 case DW_OP_breg11:
24634 case DW_OP_breg12:
24635 case DW_OP_breg13:
24636 case DW_OP_breg14:
24637 case DW_OP_breg15:
24638 case DW_OP_breg16:
24639 case DW_OP_breg17:
24640 case DW_OP_breg18:
24641 case DW_OP_breg19:
24642 case DW_OP_breg20:
24643 case DW_OP_breg21:
24644 case DW_OP_breg22:
24645 case DW_OP_breg23:
24646 case DW_OP_breg24:
24647 case DW_OP_breg25:
24648 case DW_OP_breg26:
24649 case DW_OP_breg27:
24650 case DW_OP_breg28:
24651 case DW_OP_breg29:
24652 case DW_OP_breg30:
24653 case DW_OP_breg31:
24654 case DW_OP_regx:
24655 case DW_OP_fbreg:
24656 case DW_OP_piece:
24657 case DW_OP_deref_size:
24658 case DW_OP_xderef_size:
24659 hstate.add_object (val1->v.val_int);
24660 break;
24661 case DW_OP_skip:
24662 case DW_OP_bra:
24664 int offset;
24666 gcc_assert (val1->val_class == dw_val_class_loc);
24667 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
24668 hstate.add_object (offset);
24670 break;
24671 case DW_OP_implicit_value:
24672 hstate.add_object (val1->v.val_unsigned);
24673 switch (val2->val_class)
24675 case dw_val_class_const:
24676 hstate.add_object (val2->v.val_int);
24677 break;
24678 case dw_val_class_vec:
24680 unsigned int elt_size = val2->v.val_vec.elt_size;
24681 unsigned int len = val2->v.val_vec.length;
24683 hstate.add_int (elt_size);
24684 hstate.add_int (len);
24685 hstate.add (val2->v.val_vec.array, len * elt_size);
24687 break;
24688 case dw_val_class_const_double:
24689 hstate.add_object (val2->v.val_double.low);
24690 hstate.add_object (val2->v.val_double.high);
24691 break;
24692 case dw_val_class_wide_int:
24693 hstate.add (val2->v.val_wide->get_val (),
24694 get_full_len (*val2->v.val_wide)
24695 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
24696 break;
24697 case dw_val_class_addr:
24698 inchash::add_rtx (val2->v.val_addr, hstate);
24699 break;
24700 default:
24701 gcc_unreachable ();
24703 break;
24704 case DW_OP_bregx:
24705 case DW_OP_bit_piece:
24706 hstate.add_object (val1->v.val_int);
24707 hstate.add_object (val2->v.val_int);
24708 break;
24709 case DW_OP_addr:
24710 hash_addr:
24711 if (loc->dtprel)
24713 unsigned char dtprel = 0xd1;
24714 hstate.add_object (dtprel);
24716 inchash::add_rtx (val1->v.val_addr, hstate);
24717 break;
24718 case DW_OP_GNU_addr_index:
24719 case DW_OP_GNU_const_index:
24721 if (loc->dtprel)
24723 unsigned char dtprel = 0xd1;
24724 hstate.add_object (dtprel);
24726 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
24728 break;
24729 case DW_OP_GNU_implicit_pointer:
24730 hstate.add_int (val2->v.val_int);
24731 break;
24732 case DW_OP_GNU_entry_value:
24733 hstate.add_object (val1->v.val_loc);
24734 break;
24735 case DW_OP_GNU_regval_type:
24736 case DW_OP_GNU_deref_type:
24738 unsigned int byte_size
24739 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
24740 unsigned int encoding
24741 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
24742 hstate.add_object (val1->v.val_int);
24743 hstate.add_object (byte_size);
24744 hstate.add_object (encoding);
24746 break;
24747 case DW_OP_GNU_convert:
24748 case DW_OP_GNU_reinterpret:
24749 if (val1->val_class == dw_val_class_unsigned_const)
24751 hstate.add_object (val1->v.val_unsigned);
24752 break;
24754 /* FALLTHRU */
24755 case DW_OP_GNU_const_type:
24757 unsigned int byte_size
24758 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
24759 unsigned int encoding
24760 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
24761 hstate.add_object (byte_size);
24762 hstate.add_object (encoding);
24763 if (loc->dw_loc_opc != DW_OP_GNU_const_type)
24764 break;
24765 hstate.add_object (val2->val_class);
24766 switch (val2->val_class)
24768 case dw_val_class_const:
24769 hstate.add_object (val2->v.val_int);
24770 break;
24771 case dw_val_class_vec:
24773 unsigned int elt_size = val2->v.val_vec.elt_size;
24774 unsigned int len = val2->v.val_vec.length;
24776 hstate.add_object (elt_size);
24777 hstate.add_object (len);
24778 hstate.add (val2->v.val_vec.array, len * elt_size);
24780 break;
24781 case dw_val_class_const_double:
24782 hstate.add_object (val2->v.val_double.low);
24783 hstate.add_object (val2->v.val_double.high);
24784 break;
24785 case dw_val_class_wide_int:
24786 hstate.add (val2->v.val_wide->get_val (),
24787 get_full_len (*val2->v.val_wide)
24788 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
24789 break;
24790 default:
24791 gcc_unreachable ();
24794 break;
24796 default:
24797 /* Other codes have no operands. */
24798 break;
24802 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
24804 static inline void
24805 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
24807 dw_loc_descr_ref l;
24808 bool sizes_computed = false;
24809 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
24810 size_of_locs (loc);
24812 for (l = loc; l != NULL; l = l->dw_loc_next)
24814 enum dwarf_location_atom opc = l->dw_loc_opc;
24815 hstate.add_object (opc);
24816 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
24818 size_of_locs (loc);
24819 sizes_computed = true;
24821 hash_loc_operands (l, hstate);
24825 /* Compute hash of the whole location list LIST_HEAD. */
24827 static inline void
24828 hash_loc_list (dw_loc_list_ref list_head)
24830 dw_loc_list_ref curr = list_head;
24831 inchash::hash hstate;
24833 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
24835 hstate.add (curr->begin, strlen (curr->begin) + 1);
24836 hstate.add (curr->end, strlen (curr->end) + 1);
24837 if (curr->section)
24838 hstate.add (curr->section, strlen (curr->section) + 1);
24839 hash_locs (curr->expr, hstate);
24841 list_head->hash = hstate.end ();
24844 /* Return true if X and Y opcodes have the same operands. */
24846 static inline bool
24847 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
24849 dw_val_ref valx1 = &x->dw_loc_oprnd1;
24850 dw_val_ref valx2 = &x->dw_loc_oprnd2;
24851 dw_val_ref valy1 = &y->dw_loc_oprnd1;
24852 dw_val_ref valy2 = &y->dw_loc_oprnd2;
24854 switch (x->dw_loc_opc)
24856 case DW_OP_const4u:
24857 case DW_OP_const8u:
24858 if (x->dtprel)
24859 goto hash_addr;
24860 /* FALLTHRU */
24861 case DW_OP_const1u:
24862 case DW_OP_const1s:
24863 case DW_OP_const2u:
24864 case DW_OP_const2s:
24865 case DW_OP_const4s:
24866 case DW_OP_const8s:
24867 case DW_OP_constu:
24868 case DW_OP_consts:
24869 case DW_OP_pick:
24870 case DW_OP_plus_uconst:
24871 case DW_OP_breg0:
24872 case DW_OP_breg1:
24873 case DW_OP_breg2:
24874 case DW_OP_breg3:
24875 case DW_OP_breg4:
24876 case DW_OP_breg5:
24877 case DW_OP_breg6:
24878 case DW_OP_breg7:
24879 case DW_OP_breg8:
24880 case DW_OP_breg9:
24881 case DW_OP_breg10:
24882 case DW_OP_breg11:
24883 case DW_OP_breg12:
24884 case DW_OP_breg13:
24885 case DW_OP_breg14:
24886 case DW_OP_breg15:
24887 case DW_OP_breg16:
24888 case DW_OP_breg17:
24889 case DW_OP_breg18:
24890 case DW_OP_breg19:
24891 case DW_OP_breg20:
24892 case DW_OP_breg21:
24893 case DW_OP_breg22:
24894 case DW_OP_breg23:
24895 case DW_OP_breg24:
24896 case DW_OP_breg25:
24897 case DW_OP_breg26:
24898 case DW_OP_breg27:
24899 case DW_OP_breg28:
24900 case DW_OP_breg29:
24901 case DW_OP_breg30:
24902 case DW_OP_breg31:
24903 case DW_OP_regx:
24904 case DW_OP_fbreg:
24905 case DW_OP_piece:
24906 case DW_OP_deref_size:
24907 case DW_OP_xderef_size:
24908 return valx1->v.val_int == valy1->v.val_int;
24909 case DW_OP_skip:
24910 case DW_OP_bra:
24911 /* If splitting debug info, the use of DW_OP_GNU_addr_index
24912 can cause irrelevant differences in dw_loc_addr. */
24913 gcc_assert (valx1->val_class == dw_val_class_loc
24914 && valy1->val_class == dw_val_class_loc
24915 && (dwarf_split_debug_info
24916 || x->dw_loc_addr == y->dw_loc_addr));
24917 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
24918 case DW_OP_implicit_value:
24919 if (valx1->v.val_unsigned != valy1->v.val_unsigned
24920 || valx2->val_class != valy2->val_class)
24921 return false;
24922 switch (valx2->val_class)
24924 case dw_val_class_const:
24925 return valx2->v.val_int == valy2->v.val_int;
24926 case dw_val_class_vec:
24927 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
24928 && valx2->v.val_vec.length == valy2->v.val_vec.length
24929 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
24930 valx2->v.val_vec.elt_size
24931 * valx2->v.val_vec.length) == 0;
24932 case dw_val_class_const_double:
24933 return valx2->v.val_double.low == valy2->v.val_double.low
24934 && valx2->v.val_double.high == valy2->v.val_double.high;
24935 case dw_val_class_wide_int:
24936 return *valx2->v.val_wide == *valy2->v.val_wide;
24937 case dw_val_class_addr:
24938 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
24939 default:
24940 gcc_unreachable ();
24942 case DW_OP_bregx:
24943 case DW_OP_bit_piece:
24944 return valx1->v.val_int == valy1->v.val_int
24945 && valx2->v.val_int == valy2->v.val_int;
24946 case DW_OP_addr:
24947 hash_addr:
24948 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
24949 case DW_OP_GNU_addr_index:
24950 case DW_OP_GNU_const_index:
24952 rtx ax1 = valx1->val_entry->addr.rtl;
24953 rtx ay1 = valy1->val_entry->addr.rtl;
24954 return rtx_equal_p (ax1, ay1);
24956 case DW_OP_GNU_implicit_pointer:
24957 return valx1->val_class == dw_val_class_die_ref
24958 && valx1->val_class == valy1->val_class
24959 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
24960 && valx2->v.val_int == valy2->v.val_int;
24961 case DW_OP_GNU_entry_value:
24962 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
24963 case DW_OP_GNU_const_type:
24964 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
24965 || valx2->val_class != valy2->val_class)
24966 return false;
24967 switch (valx2->val_class)
24969 case dw_val_class_const:
24970 return valx2->v.val_int == valy2->v.val_int;
24971 case dw_val_class_vec:
24972 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
24973 && valx2->v.val_vec.length == valy2->v.val_vec.length
24974 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
24975 valx2->v.val_vec.elt_size
24976 * valx2->v.val_vec.length) == 0;
24977 case dw_val_class_const_double:
24978 return valx2->v.val_double.low == valy2->v.val_double.low
24979 && valx2->v.val_double.high == valy2->v.val_double.high;
24980 case dw_val_class_wide_int:
24981 return *valx2->v.val_wide == *valy2->v.val_wide;
24982 default:
24983 gcc_unreachable ();
24985 case DW_OP_GNU_regval_type:
24986 case DW_OP_GNU_deref_type:
24987 return valx1->v.val_int == valy1->v.val_int
24988 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
24989 case DW_OP_GNU_convert:
24990 case DW_OP_GNU_reinterpret:
24991 if (valx1->val_class != valy1->val_class)
24992 return false;
24993 if (valx1->val_class == dw_val_class_unsigned_const)
24994 return valx1->v.val_unsigned == valy1->v.val_unsigned;
24995 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
24996 case DW_OP_GNU_parameter_ref:
24997 return valx1->val_class == dw_val_class_die_ref
24998 && valx1->val_class == valy1->val_class
24999 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
25000 default:
25001 /* Other codes have no operands. */
25002 return true;
25006 /* Return true if DWARF location expressions X and Y are the same. */
25008 static inline bool
25009 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
25011 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
25012 if (x->dw_loc_opc != y->dw_loc_opc
25013 || x->dtprel != y->dtprel
25014 || !compare_loc_operands (x, y))
25015 break;
25016 return x == NULL && y == NULL;
25019 /* Hashtable helpers. */
25021 struct loc_list_hasher : nofree_ptr_hash <dw_loc_list_struct>
25023 static inline hashval_t hash (const dw_loc_list_struct *);
25024 static inline bool equal (const dw_loc_list_struct *,
25025 const dw_loc_list_struct *);
25028 /* Return precomputed hash of location list X. */
25030 inline hashval_t
25031 loc_list_hasher::hash (const dw_loc_list_struct *x)
25033 return x->hash;
25036 /* Return true if location lists A and B are the same. */
25038 inline bool
25039 loc_list_hasher::equal (const dw_loc_list_struct *a,
25040 const dw_loc_list_struct *b)
25042 if (a == b)
25043 return 1;
25044 if (a->hash != b->hash)
25045 return 0;
25046 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
25047 if (strcmp (a->begin, b->begin) != 0
25048 || strcmp (a->end, b->end) != 0
25049 || (a->section == NULL) != (b->section == NULL)
25050 || (a->section && strcmp (a->section, b->section) != 0)
25051 || !compare_locs (a->expr, b->expr))
25052 break;
25053 return a == NULL && b == NULL;
25056 typedef hash_table<loc_list_hasher> loc_list_hash_type;
25059 /* Recursively optimize location lists referenced from DIE
25060 children and share them whenever possible. */
25062 static void
25063 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
25065 dw_die_ref c;
25066 dw_attr_ref a;
25067 unsigned ix;
25068 dw_loc_list_struct **slot;
25070 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
25071 if (AT_class (a) == dw_val_class_loc_list)
25073 dw_loc_list_ref list = AT_loc_list (a);
25074 /* TODO: perform some optimizations here, before hashing
25075 it and storing into the hash table. */
25076 hash_loc_list (list);
25077 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
25078 if (*slot == NULL)
25079 *slot = list;
25080 else
25081 a->dw_attr_val.v.val_loc_list = *slot;
25084 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
25088 /* Recursively assign each location list a unique index into the debug_addr
25089 section. */
25091 static void
25092 index_location_lists (dw_die_ref die)
25094 dw_die_ref c;
25095 dw_attr_ref a;
25096 unsigned ix;
25098 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
25099 if (AT_class (a) == dw_val_class_loc_list)
25101 dw_loc_list_ref list = AT_loc_list (a);
25102 dw_loc_list_ref curr;
25103 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
25105 /* Don't index an entry that has already been indexed
25106 or won't be output. */
25107 if (curr->begin_entry != NULL
25108 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
25109 continue;
25111 curr->begin_entry
25112 = add_addr_table_entry (xstrdup (curr->begin),
25113 ate_kind_label);
25117 FOR_EACH_CHILD (die, c, index_location_lists (c));
25120 /* Optimize location lists referenced from DIE
25121 children and share them whenever possible. */
25123 static void
25124 optimize_location_lists (dw_die_ref die)
25126 loc_list_hash_type htab (500);
25127 optimize_location_lists_1 (die, &htab);
25130 /* Output stuff that dwarf requires at the end of every file,
25131 and generate the DWARF-2 debugging info. */
25133 static void
25134 dwarf2out_finish (const char *filename)
25136 comdat_type_node *ctnode;
25137 dw_die_ref main_comp_unit_die;
25139 /* Flush out any latecomers to the limbo party. */
25140 dwarf2out_early_finish ();
25142 /* PCH might result in DW_AT_producer string being restored from the
25143 header compilation, so always fill it with empty string initially
25144 and overwrite only here. */
25145 dw_attr_ref producer = get_AT (comp_unit_die (), DW_AT_producer);
25146 producer_string = gen_producer_string ();
25147 producer->dw_attr_val.v.val_str->refcount--;
25148 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
25150 gen_scheduled_generic_parms_dies ();
25151 gen_remaining_tmpl_value_param_die_attribute ();
25153 /* Add the name for the main input file now. We delayed this from
25154 dwarf2out_init to avoid complications with PCH.
25155 For LTO produced units use a fixed artificial name to avoid
25156 leaking tempfile names into the dwarf. */
25157 if (!in_lto_p)
25158 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
25159 else
25160 add_name_attribute (comp_unit_die (), "<artificial>");
25161 if (!IS_ABSOLUTE_PATH (filename) || targetm.force_at_comp_dir)
25162 add_comp_dir_attribute (comp_unit_die ());
25163 else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
25165 bool p = false;
25166 file_table->traverse<bool *, file_table_relative_p> (&p);
25167 if (p)
25168 add_comp_dir_attribute (comp_unit_die ());
25171 #if ENABLE_ASSERT_CHECKING
25173 dw_die_ref die = comp_unit_die (), c;
25174 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
25176 #endif
25177 resolve_addr (comp_unit_die ());
25178 move_marked_base_types ();
25180 /* Walk through the list of incomplete types again, trying once more to
25181 emit full debugging info for them. */
25182 retry_incomplete_types ();
25184 if (flag_eliminate_unused_debug_types)
25185 prune_unused_types ();
25187 /* Generate separate COMDAT sections for type DIEs. */
25188 if (use_debug_types)
25190 break_out_comdat_types (comp_unit_die ());
25192 /* Each new type_unit DIE was added to the limbo die list when created.
25193 Since these have all been added to comdat_type_list, clear the
25194 limbo die list. */
25195 limbo_die_list = NULL;
25197 /* For each new comdat type unit, copy declarations for incomplete
25198 types to make the new unit self-contained (i.e., no direct
25199 references to the main compile unit). */
25200 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
25201 copy_decls_for_unworthy_types (ctnode->root_die);
25202 copy_decls_for_unworthy_types (comp_unit_die ());
25204 /* In the process of copying declarations from one unit to another,
25205 we may have left some declarations behind that are no longer
25206 referenced. Prune them. */
25207 prune_unused_types ();
25210 /* Generate separate CUs for each of the include files we've seen.
25211 They will go into limbo_die_list. */
25212 if (flag_eliminate_dwarf2_dups)
25213 break_out_includes (comp_unit_die ());
25215 /* Traverse the DIE's and add sibling attributes to those DIE's that
25216 have children. */
25217 add_sibling_attributes (comp_unit_die ());
25218 limbo_die_node *node;
25219 for (node = limbo_die_list; node; node = node->next)
25220 add_sibling_attributes (node->die);
25221 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
25222 add_sibling_attributes (ctnode->root_die);
25224 /* When splitting DWARF info, we put some attributes in the
25225 skeleton compile_unit DIE that remains in the .o, while
25226 most attributes go in the DWO compile_unit_die. */
25227 if (dwarf_split_debug_info)
25228 main_comp_unit_die = gen_compile_unit_die (NULL);
25229 else
25230 main_comp_unit_die = comp_unit_die ();
25232 /* Output a terminator label for the .text section. */
25233 switch_to_section (text_section);
25234 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
25235 if (cold_text_section)
25237 switch_to_section (cold_text_section);
25238 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
25241 /* We can only use the low/high_pc attributes if all of the code was
25242 in .text. */
25243 if (!have_multiple_function_sections
25244 || (dwarf_version < 3 && dwarf_strict))
25246 /* Don't add if the CU has no associated code. */
25247 if (text_section_used)
25248 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
25249 text_end_label, true);
25251 else
25253 unsigned fde_idx;
25254 dw_fde_ref fde;
25255 bool range_list_added = false;
25257 if (text_section_used)
25258 add_ranges_by_labels (main_comp_unit_die, text_section_label,
25259 text_end_label, &range_list_added, true);
25260 if (cold_text_section_used)
25261 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
25262 cold_end_label, &range_list_added, true);
25264 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
25266 if (DECL_IGNORED_P (fde->decl))
25267 continue;
25268 if (!fde->in_std_section)
25269 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
25270 fde->dw_fde_end, &range_list_added,
25271 true);
25272 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
25273 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
25274 fde->dw_fde_second_end, &range_list_added,
25275 true);
25278 if (range_list_added)
25280 /* We need to give .debug_loc and .debug_ranges an appropriate
25281 "base address". Use zero so that these addresses become
25282 absolute. Historically, we've emitted the unexpected
25283 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
25284 Emit both to give time for other tools to adapt. */
25285 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
25286 if (! dwarf_strict && dwarf_version < 4)
25287 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
25289 add_ranges (NULL);
25293 if (debug_info_level >= DINFO_LEVEL_TERSE)
25294 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
25295 debug_line_section_label);
25297 if (have_macinfo)
25298 add_AT_macptr (comp_unit_die (),
25299 dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
25300 macinfo_section_label);
25302 if (dwarf_split_debug_info)
25304 /* optimize_location_lists calculates the size of the lists,
25305 so index them first, and assign indices to the entries.
25306 Although optimize_location_lists will remove entries from
25307 the table, it only does so for duplicates, and therefore
25308 only reduces ref_counts to 1. */
25309 index_location_lists (comp_unit_die ());
25311 if (addr_index_table != NULL)
25313 unsigned int index = 0;
25314 addr_index_table
25315 ->traverse_noresize<unsigned int *, index_addr_table_entry>
25316 (&index);
25320 if (have_location_lists)
25321 optimize_location_lists (comp_unit_die ());
25323 save_macinfo_strings ();
25325 if (dwarf_split_debug_info)
25327 unsigned int index = 0;
25329 /* Add attributes common to skeleton compile_units and
25330 type_units. Because these attributes include strings, it
25331 must be done before freezing the string table. Top-level
25332 skeleton die attrs are added when the skeleton type unit is
25333 created, so ensure it is created by this point. */
25334 add_top_level_skeleton_die_attrs (main_comp_unit_die);
25335 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
25338 /* Output all of the compilation units. We put the main one last so that
25339 the offsets are available to output_pubnames. */
25340 for (node = limbo_die_list; node; node = node->next)
25341 output_comp_unit (node->die, 0);
25343 hash_table<comdat_type_hasher> comdat_type_table (100);
25344 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
25346 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
25348 /* Don't output duplicate types. */
25349 if (*slot != HTAB_EMPTY_ENTRY)
25350 continue;
25352 /* Add a pointer to the line table for the main compilation unit
25353 so that the debugger can make sense of DW_AT_decl_file
25354 attributes. */
25355 if (debug_info_level >= DINFO_LEVEL_TERSE)
25356 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
25357 (!dwarf_split_debug_info
25358 ? debug_line_section_label
25359 : debug_skeleton_line_section_label));
25361 output_comdat_type_unit (ctnode);
25362 *slot = ctnode;
25365 /* The AT_pubnames attribute needs to go in all skeleton dies, including
25366 both the main_cu and all skeleton TUs. Making this call unconditional
25367 would end up either adding a second copy of the AT_pubnames attribute, or
25368 requiring a special case in add_top_level_skeleton_die_attrs. */
25369 if (!dwarf_split_debug_info)
25370 add_AT_pubnames (comp_unit_die ());
25372 if (dwarf_split_debug_info)
25374 int mark;
25375 unsigned char checksum[16];
25376 struct md5_ctx ctx;
25378 /* Compute a checksum of the comp_unit to use as the dwo_id. */
25379 md5_init_ctx (&ctx);
25380 mark = 0;
25381 die_checksum (comp_unit_die (), &ctx, &mark);
25382 unmark_all_dies (comp_unit_die ());
25383 md5_finish_ctx (&ctx, checksum);
25385 /* Use the first 8 bytes of the checksum as the dwo_id,
25386 and add it to both comp-unit DIEs. */
25387 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
25388 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
25390 /* Add the base offset of the ranges table to the skeleton
25391 comp-unit DIE. */
25392 if (ranges_table_in_use)
25393 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
25394 ranges_section_label);
25396 switch_to_section (debug_addr_section);
25397 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
25398 output_addr_table ();
25401 /* Output the main compilation unit if non-empty or if .debug_macinfo
25402 or .debug_macro will be emitted. */
25403 output_comp_unit (comp_unit_die (), have_macinfo);
25405 if (dwarf_split_debug_info && info_section_emitted)
25406 output_skeleton_debug_sections (main_comp_unit_die);
25408 /* Output the abbreviation table. */
25409 if (abbrev_die_table_in_use != 1)
25411 switch_to_section (debug_abbrev_section);
25412 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
25413 output_abbrev_section ();
25416 /* Output location list section if necessary. */
25417 if (have_location_lists)
25419 /* Output the location lists info. */
25420 switch_to_section (debug_loc_section);
25421 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
25422 output_location_lists (comp_unit_die ());
25425 output_pubtables ();
25427 /* Output the address range information if a CU (.debug_info section)
25428 was emitted. We output an empty table even if we had no functions
25429 to put in it. This because the consumer has no way to tell the
25430 difference between an empty table that we omitted and failure to
25431 generate a table that would have contained data. */
25432 if (info_section_emitted)
25434 unsigned long aranges_length = size_of_aranges ();
25436 switch_to_section (debug_aranges_section);
25437 output_aranges (aranges_length);
25440 /* Output ranges section if necessary. */
25441 if (ranges_table_in_use)
25443 switch_to_section (debug_ranges_section);
25444 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
25445 output_ranges ();
25448 /* Have to end the macro section. */
25449 if (have_macinfo)
25451 switch_to_section (debug_macinfo_section);
25452 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
25453 output_macinfo ();
25454 dw2_asm_output_data (1, 0, "End compilation unit");
25457 /* Output the source line correspondence table. We must do this
25458 even if there is no line information. Otherwise, on an empty
25459 translation unit, we will generate a present, but empty,
25460 .debug_info section. IRIX 6.5 `nm' will then complain when
25461 examining the file. This is done late so that any filenames
25462 used by the debug_info section are marked as 'used'. */
25463 switch_to_section (debug_line_section);
25464 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
25465 if (! DWARF2_ASM_LINE_DEBUG_INFO)
25466 output_line_info (false);
25468 if (dwarf_split_debug_info && info_section_emitted)
25470 switch_to_section (debug_skeleton_line_section);
25471 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
25472 output_line_info (true);
25475 /* If we emitted any indirect strings, output the string table too. */
25476 if (debug_str_hash || skeleton_debug_str_hash)
25477 output_indirect_strings ();
25480 /* Perform any cleanups needed after the early debug generation pass
25481 has run. */
25483 static void
25484 dwarf2out_early_finish (void)
25486 limbo_die_node *node, *next_node;
25488 /* Add DW_AT_linkage_name for all deferred DIEs. */
25489 for (node = deferred_asm_name; node; node = node->next)
25491 tree decl = node->created_for;
25492 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
25493 /* A missing DECL_ASSEMBLER_NAME can be a constant DIE that
25494 ended up in deferred_asm_name before we knew it was
25495 constant and never written to disk. */
25496 && DECL_ASSEMBLER_NAME (decl))
25498 add_linkage_attr (node->die, decl);
25499 move_linkage_attr (node->die);
25502 deferred_asm_name = NULL;
25504 /* Traverse the limbo die list, and add parent/child links. The only
25505 dies without parents that should be here are concrete instances of
25506 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
25507 For concrete instances, we can get the parent die from the abstract
25508 instance.
25510 The point here is to flush out the limbo list so that it is empty
25511 and we don't need to stream it for LTO. */
25512 for (node = limbo_die_list; node; node = next_node)
25514 dw_die_ref die = node->die;
25515 next_node = node->next;
25517 if (die->die_parent == NULL)
25519 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
25521 if (origin && origin->die_parent)
25522 add_child_die (origin->die_parent, die);
25523 else if (is_cu_die (die))
25525 else if (seen_error ())
25526 /* It's OK to be confused by errors in the input. */
25527 add_child_die (comp_unit_die (), die);
25528 else
25530 /* In certain situations, the lexical block containing a
25531 nested function can be optimized away, which results
25532 in the nested function die being orphaned. Likewise
25533 with the return type of that nested function. Force
25534 this to be a child of the containing function.
25536 It may happen that even the containing function got fully
25537 inlined and optimized out. In that case we are lost and
25538 assign the empty child. This should not be big issue as
25539 the function is likely unreachable too. */
25540 gcc_assert (node->created_for);
25542 if (DECL_P (node->created_for))
25543 origin = get_context_die (DECL_CONTEXT (node->created_for));
25544 else if (TYPE_P (node->created_for))
25545 origin = scope_die_for (node->created_for, comp_unit_die ());
25546 else
25547 origin = comp_unit_die ();
25549 add_child_die (origin, die);
25554 limbo_die_list = NULL;
25557 /* Reset all state within dwarf2out.c so that we can rerun the compiler
25558 within the same process. For use by toplev::finalize. */
25560 void
25561 dwarf2out_c_finalize (void)
25563 last_var_location_insn = NULL;
25564 cached_next_real_insn = NULL;
25565 used_rtx_array = NULL;
25566 incomplete_types = NULL;
25567 decl_scope_table = NULL;
25568 debug_info_section = NULL;
25569 debug_skeleton_info_section = NULL;
25570 debug_abbrev_section = NULL;
25571 debug_skeleton_abbrev_section = NULL;
25572 debug_aranges_section = NULL;
25573 debug_addr_section = NULL;
25574 debug_macinfo_section = NULL;
25575 debug_line_section = NULL;
25576 debug_skeleton_line_section = NULL;
25577 debug_loc_section = NULL;
25578 debug_pubnames_section = NULL;
25579 debug_pubtypes_section = NULL;
25580 debug_str_section = NULL;
25581 debug_str_dwo_section = NULL;
25582 debug_str_offsets_section = NULL;
25583 debug_ranges_section = NULL;
25584 debug_frame_section = NULL;
25585 fde_vec = NULL;
25586 debug_str_hash = NULL;
25587 skeleton_debug_str_hash = NULL;
25588 dw2_string_counter = 0;
25589 have_multiple_function_sections = false;
25590 text_section_used = false;
25591 cold_text_section_used = false;
25592 cold_text_section = NULL;
25593 current_unit_personality = NULL;
25595 next_die_offset = 0;
25596 single_comp_unit_die = NULL;
25597 comdat_type_list = NULL;
25598 limbo_die_list = NULL;
25599 file_table = NULL;
25600 decl_die_table = NULL;
25601 common_block_die_table = NULL;
25602 decl_loc_table = NULL;
25603 call_arg_locations = NULL;
25604 call_arg_loc_last = NULL;
25605 call_site_count = -1;
25606 tail_call_site_count = -1;
25607 cached_dw_loc_list_table = NULL;
25608 abbrev_die_table = NULL;
25609 abbrev_die_table_allocated = 0;
25610 abbrev_die_table_in_use = 0;
25611 line_info_label_num = 0;
25612 cur_line_info_table = NULL;
25613 text_section_line_info = NULL;
25614 cold_text_section_line_info = NULL;
25615 separate_line_info = NULL;
25616 info_section_emitted = false;
25617 pubname_table = NULL;
25618 pubtype_table = NULL;
25619 macinfo_table = NULL;
25620 ranges_table = NULL;
25621 ranges_table_allocated = 0;
25622 ranges_table_in_use = 0;
25623 ranges_by_label = 0;
25624 ranges_by_label_allocated = 0;
25625 ranges_by_label_in_use = 0;
25626 have_location_lists = false;
25627 loclabel_num = 0;
25628 poc_label_num = 0;
25629 last_emitted_file = NULL;
25630 label_num = 0;
25631 tmpl_value_parm_die_table = NULL;
25632 generic_type_instances = NULL;
25633 frame_pointer_fb_offset = 0;
25634 frame_pointer_fb_offset_valid = false;
25635 base_types.release ();
25636 XDELETEVEC (producer_string);
25637 producer_string = NULL;
25640 #include "gt-dwarf2out.h"