Daily bump.
[official-gcc.git] / gcc / dwarf2out.c
blobc7d7181baede9a0b012ece415c96065956c60a26
1 /* Output Dwarf2 format symbol table information from GCC.
2 Copyright (C) 1992-2014 Free Software Foundation, Inc.
3 Contributed by Gary Funck (gary@intrepid.com).
4 Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
5 Extensively modified by Jason Merrill (jason@cygnus.com).
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 /* TODO: Emit .debug_line header even when there are no functions, since
24 the file numbers are used by .debug_info. Alternately, leave
25 out locations for types and decls.
26 Avoid talking about ctors and op= for PODs.
27 Factor out common prologue sequences into multiple CIEs. */
29 /* The first part of this file deals with the DWARF 2 frame unwind
30 information, which is also used by the GCC efficient exception handling
31 mechanism. The second part, controlled only by an #ifdef
32 DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
33 information. */
35 /* DWARF2 Abbreviation Glossary:
37 CFA = Canonical Frame Address
38 a fixed address on the stack which identifies a call frame.
39 We define it to be the value of SP just before the call insn.
40 The CFA register and offset, which may change during the course
41 of the function, are used to calculate its value at runtime.
43 CFI = Call Frame Instruction
44 an instruction for the DWARF2 abstract machine
46 CIE = Common Information Entry
47 information describing information common to one or more FDEs
49 DIE = Debugging Information Entry
51 FDE = Frame Description Entry
52 information describing the stack call frame, in particular,
53 how to restore registers
55 DW_CFA_... = DWARF2 CFA call frame instruction
56 DW_TAG_... = DWARF2 DIE tag */
58 #include "config.h"
59 #include "system.h"
60 #include "coretypes.h"
61 #include "tm.h"
62 #include "rtl.h"
63 #include "tree.h"
64 #include "stringpool.h"
65 #include "stor-layout.h"
66 #include "varasm.h"
67 #include "function.h"
68 #include "emit-rtl.h"
69 #include "hash-table.h"
70 #include "version.h"
71 #include "flags.h"
72 #include "hard-reg-set.h"
73 #include "regs.h"
74 #include "insn-config.h"
75 #include "reload.h"
76 #include "function.h"
77 #include "output.h"
78 #include "expr.h"
79 #include "except.h"
80 #include "dwarf2.h"
81 #include "dwarf2out.h"
82 #include "dwarf2asm.h"
83 #include "toplev.h"
84 #include "md5.h"
85 #include "tm_p.h"
86 #include "diagnostic.h"
87 #include "tree-pretty-print.h"
88 #include "debug.h"
89 #include "target.h"
90 #include "common/common-target.h"
91 #include "langhooks.h"
92 #include "cgraph.h"
93 #include "input.h"
94 #include "ira.h"
95 #include "lra.h"
96 #include "dumpfile.h"
97 #include "opts.h"
98 #include "tree-dfa.h"
99 #include "gdb/gdb-index.h"
101 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
102 static rtx last_var_location_insn;
103 static rtx cached_next_real_insn;
105 #ifdef VMS_DEBUGGING_INFO
106 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
108 /* Define this macro to be a nonzero value if the directory specifications
109 which are output in the debug info should end with a separator. */
110 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
111 /* Define this macro to evaluate to a nonzero value if GCC should refrain
112 from generating indirect strings in DWARF2 debug information, for instance
113 if your target is stuck with an old version of GDB that is unable to
114 process them properly or uses VMS Debug. */
115 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
116 #else
117 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
118 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
119 #endif
121 /* ??? Poison these here until it can be done generically. They've been
122 totally replaced in this file; make sure it stays that way. */
123 #undef DWARF2_UNWIND_INFO
124 #undef DWARF2_FRAME_INFO
125 #if (GCC_VERSION >= 3000)
126 #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
127 #endif
129 /* The size of the target's pointer type. */
130 #ifndef PTR_SIZE
131 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
132 #endif
134 /* Array of RTXes referenced by the debugging information, which therefore
135 must be kept around forever. */
136 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
138 /* A pointer to the base of a list of incomplete types which might be
139 completed at some later time. incomplete_types_list needs to be a
140 vec<tree, va_gc> *because we want to tell the garbage collector about
141 it. */
142 static GTY(()) vec<tree, va_gc> *incomplete_types;
144 /* A pointer to the base of a table of references to declaration
145 scopes. This table is a display which tracks the nesting
146 of declaration scopes at the current scope and containing
147 scopes. This table is used to find the proper place to
148 define type declaration DIE's. */
149 static GTY(()) vec<tree, va_gc> *decl_scope_table;
151 /* Pointers to various DWARF2 sections. */
152 static GTY(()) section *debug_info_section;
153 static GTY(()) section *debug_skeleton_info_section;
154 static GTY(()) section *debug_abbrev_section;
155 static GTY(()) section *debug_skeleton_abbrev_section;
156 static GTY(()) section *debug_aranges_section;
157 static GTY(()) section *debug_addr_section;
158 static GTY(()) section *debug_macinfo_section;
159 static GTY(()) section *debug_line_section;
160 static GTY(()) section *debug_skeleton_line_section;
161 static GTY(()) section *debug_loc_section;
162 static GTY(()) section *debug_pubnames_section;
163 static GTY(()) section *debug_pubtypes_section;
164 static GTY(()) section *debug_str_section;
165 static GTY(()) section *debug_str_dwo_section;
166 static GTY(()) section *debug_str_offsets_section;
167 static GTY(()) section *debug_ranges_section;
168 static GTY(()) section *debug_frame_section;
170 /* Maximum size (in bytes) of an artificially generated label. */
171 #define MAX_ARTIFICIAL_LABEL_BYTES 30
173 /* According to the (draft) DWARF 3 specification, the initial length
174 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
175 bytes are 0xffffffff, followed by the length stored in the next 8
176 bytes.
178 However, the SGI/MIPS ABI uses an initial length which is equal to
179 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
181 #ifndef DWARF_INITIAL_LENGTH_SIZE
182 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
183 #endif
185 /* Round SIZE up to the nearest BOUNDARY. */
186 #define DWARF_ROUND(SIZE,BOUNDARY) \
187 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
189 /* CIE identifier. */
190 #if HOST_BITS_PER_WIDE_INT >= 64
191 #define DWARF_CIE_ID \
192 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
193 #else
194 #define DWARF_CIE_ID DW_CIE_ID
195 #endif
198 /* A vector for a table that contains frame description
199 information for each routine. */
200 #define NOT_INDEXED (-1U)
201 #define NO_INDEX_ASSIGNED (-2U)
203 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
205 struct GTY(()) indirect_string_node {
206 const char *str;
207 unsigned int refcount;
208 enum dwarf_form form;
209 char *label;
210 unsigned int index;
213 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
215 /* With split_debug_info, both the comp_dir and dwo_name go in the
216 main object file, rather than the dwo, similar to the force_direct
217 parameter elsewhere but with additional complications:
219 1) The string is needed in both the main object file and the dwo.
220 That is, the comp_dir and dwo_name will appear in both places.
222 2) Strings can use three forms: DW_FORM_string, DW_FORM_strp or
223 DW_FORM_GNU_str_index.
225 3) GCC chooses the form to use late, depending on the size and
226 reference count.
228 Rather than forcing the all debug string handling functions and
229 callers to deal with these complications, simply use a separate,
230 special-cased string table for any attribute that should go in the
231 main object file. This limits the complexity to just the places
232 that need it. */
234 static GTY ((param_is (struct indirect_string_node)))
235 htab_t skeleton_debug_str_hash;
237 static GTY(()) int dw2_string_counter;
239 /* True if the compilation unit places functions in more than one section. */
240 static GTY(()) bool have_multiple_function_sections = false;
242 /* Whether the default text and cold text sections have been used at all. */
244 static GTY(()) bool text_section_used = false;
245 static GTY(()) bool cold_text_section_used = false;
247 /* The default cold text section. */
248 static GTY(()) section *cold_text_section;
250 /* The DIE for C++1y 'auto' in a function return type. */
251 static GTY(()) dw_die_ref auto_die;
253 /* The DIE for C++1y 'decltype(auto)' in a function return type. */
254 static GTY(()) dw_die_ref decltype_auto_die;
256 /* Forward declarations for functions defined in this file. */
258 static char *stripattributes (const char *);
259 static void output_call_frame_info (int);
260 static void dwarf2out_note_section_used (void);
262 /* Personality decl of current unit. Used only when assembler does not support
263 personality CFI. */
264 static GTY(()) rtx current_unit_personality;
266 /* Data and reference forms for relocatable data. */
267 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
268 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
270 #ifndef DEBUG_FRAME_SECTION
271 #define DEBUG_FRAME_SECTION ".debug_frame"
272 #endif
274 #ifndef FUNC_BEGIN_LABEL
275 #define FUNC_BEGIN_LABEL "LFB"
276 #endif
278 #ifndef FUNC_END_LABEL
279 #define FUNC_END_LABEL "LFE"
280 #endif
282 #ifndef PROLOGUE_END_LABEL
283 #define PROLOGUE_END_LABEL "LPE"
284 #endif
286 #ifndef EPILOGUE_BEGIN_LABEL
287 #define EPILOGUE_BEGIN_LABEL "LEB"
288 #endif
290 #ifndef FRAME_BEGIN_LABEL
291 #define FRAME_BEGIN_LABEL "Lframe"
292 #endif
293 #define CIE_AFTER_SIZE_LABEL "LSCIE"
294 #define CIE_END_LABEL "LECIE"
295 #define FDE_LABEL "LSFDE"
296 #define FDE_AFTER_SIZE_LABEL "LASFDE"
297 #define FDE_END_LABEL "LEFDE"
298 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
299 #define LINE_NUMBER_END_LABEL "LELT"
300 #define LN_PROLOG_AS_LABEL "LASLTP"
301 #define LN_PROLOG_END_LABEL "LELTP"
302 #define DIE_LABEL_PREFIX "DW"
304 /* Match the base name of a file to the base name of a compilation unit. */
306 static int
307 matches_main_base (const char *path)
309 /* Cache the last query. */
310 static const char *last_path = NULL;
311 static int last_match = 0;
312 if (path != last_path)
314 const char *base;
315 int length = base_of_path (path, &base);
316 last_path = path;
317 last_match = (length == main_input_baselength
318 && memcmp (base, main_input_basename, length) == 0);
320 return last_match;
323 #ifdef DEBUG_DEBUG_STRUCT
325 static int
326 dump_struct_debug (tree type, enum debug_info_usage usage,
327 enum debug_struct_file criterion, int generic,
328 int matches, int result)
330 /* Find the type name. */
331 tree type_decl = TYPE_STUB_DECL (type);
332 tree t = type_decl;
333 const char *name = 0;
334 if (TREE_CODE (t) == TYPE_DECL)
335 t = DECL_NAME (t);
336 if (t)
337 name = IDENTIFIER_POINTER (t);
339 fprintf (stderr, " struct %d %s %s %s %s %d %p %s\n",
340 criterion,
341 DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
342 matches ? "bas" : "hdr",
343 generic ? "gen" : "ord",
344 usage == DINFO_USAGE_DFN ? ";" :
345 usage == DINFO_USAGE_DIR_USE ? "." : "*",
346 result,
347 (void*) type_decl, name);
348 return result;
350 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
351 dump_struct_debug (type, usage, criterion, generic, matches, result)
353 #else
355 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
356 (result)
358 #endif
360 static bool
361 should_emit_struct_debug (tree type, enum debug_info_usage usage)
363 enum debug_struct_file criterion;
364 tree type_decl;
365 bool generic = lang_hooks.types.generic_p (type);
367 if (generic)
368 criterion = debug_struct_generic[usage];
369 else
370 criterion = debug_struct_ordinary[usage];
372 if (criterion == DINFO_STRUCT_FILE_NONE)
373 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
374 if (criterion == DINFO_STRUCT_FILE_ANY)
375 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
377 type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
379 if (type_decl != NULL)
381 if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
382 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
384 if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
385 return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
388 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
391 /* Return a pointer to a copy of the section string name S with all
392 attributes stripped off, and an asterisk prepended (for assemble_name). */
394 static inline char *
395 stripattributes (const char *s)
397 char *stripped = XNEWVEC (char, strlen (s) + 2);
398 char *p = stripped;
400 *p++ = '*';
402 while (*s && *s != ',')
403 *p++ = *s++;
405 *p = '\0';
406 return stripped;
409 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
410 switch to the data section instead, and write out a synthetic start label
411 for collect2 the first time around. */
413 static void
414 switch_to_eh_frame_section (bool back)
416 tree label;
418 #ifdef EH_FRAME_SECTION_NAME
419 if (eh_frame_section == 0)
421 int flags;
423 if (EH_TABLES_CAN_BE_READ_ONLY)
425 int fde_encoding;
426 int per_encoding;
427 int lsda_encoding;
429 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
430 /*global=*/0);
431 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
432 /*global=*/1);
433 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
434 /*global=*/0);
435 flags = ((! flag_pic
436 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
437 && (fde_encoding & 0x70) != DW_EH_PE_aligned
438 && (per_encoding & 0x70) != DW_EH_PE_absptr
439 && (per_encoding & 0x70) != DW_EH_PE_aligned
440 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
441 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
442 ? 0 : SECTION_WRITE);
444 else
445 flags = SECTION_WRITE;
446 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
448 #endif /* EH_FRAME_SECTION_NAME */
450 if (eh_frame_section)
451 switch_to_section (eh_frame_section);
452 else
454 /* We have no special eh_frame section. Put the information in
455 the data section and emit special labels to guide collect2. */
456 switch_to_section (data_section);
458 if (!back)
460 label = get_file_function_name ("F");
461 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
462 targetm.asm_out.globalize_label (asm_out_file,
463 IDENTIFIER_POINTER (label));
464 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
469 /* Switch [BACK] to the eh or debug frame table section, depending on
470 FOR_EH. */
472 static void
473 switch_to_frame_table_section (int for_eh, bool back)
475 if (for_eh)
476 switch_to_eh_frame_section (back);
477 else
479 if (!debug_frame_section)
480 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
481 SECTION_DEBUG, NULL);
482 switch_to_section (debug_frame_section);
486 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
488 enum dw_cfi_oprnd_type
489 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
491 switch (cfi)
493 case DW_CFA_nop:
494 case DW_CFA_GNU_window_save:
495 case DW_CFA_remember_state:
496 case DW_CFA_restore_state:
497 return dw_cfi_oprnd_unused;
499 case DW_CFA_set_loc:
500 case DW_CFA_advance_loc1:
501 case DW_CFA_advance_loc2:
502 case DW_CFA_advance_loc4:
503 case DW_CFA_MIPS_advance_loc8:
504 return dw_cfi_oprnd_addr;
506 case DW_CFA_offset:
507 case DW_CFA_offset_extended:
508 case DW_CFA_def_cfa:
509 case DW_CFA_offset_extended_sf:
510 case DW_CFA_def_cfa_sf:
511 case DW_CFA_restore:
512 case DW_CFA_restore_extended:
513 case DW_CFA_undefined:
514 case DW_CFA_same_value:
515 case DW_CFA_def_cfa_register:
516 case DW_CFA_register:
517 case DW_CFA_expression:
518 return dw_cfi_oprnd_reg_num;
520 case DW_CFA_def_cfa_offset:
521 case DW_CFA_GNU_args_size:
522 case DW_CFA_def_cfa_offset_sf:
523 return dw_cfi_oprnd_offset;
525 case DW_CFA_def_cfa_expression:
526 return dw_cfi_oprnd_loc;
528 default:
529 gcc_unreachable ();
533 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
535 enum dw_cfi_oprnd_type
536 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
538 switch (cfi)
540 case DW_CFA_def_cfa:
541 case DW_CFA_def_cfa_sf:
542 case DW_CFA_offset:
543 case DW_CFA_offset_extended_sf:
544 case DW_CFA_offset_extended:
545 return dw_cfi_oprnd_offset;
547 case DW_CFA_register:
548 return dw_cfi_oprnd_reg_num;
550 case DW_CFA_expression:
551 return dw_cfi_oprnd_loc;
553 default:
554 return dw_cfi_oprnd_unused;
558 /* Output one FDE. */
560 static void
561 output_fde (dw_fde_ref fde, bool for_eh, bool second,
562 char *section_start_label, int fde_encoding, char *augmentation,
563 bool any_lsda_needed, int lsda_encoding)
565 const char *begin, *end;
566 static unsigned int j;
567 char l1[20], l2[20];
569 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
570 /* empty */ 0);
571 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
572 for_eh + j);
573 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
574 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
575 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
576 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
577 " indicating 64-bit DWARF extension");
578 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
579 "FDE Length");
580 ASM_OUTPUT_LABEL (asm_out_file, l1);
582 if (for_eh)
583 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
584 else
585 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
586 debug_frame_section, "FDE CIE offset");
588 begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
589 end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
591 if (for_eh)
593 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
594 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
595 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
596 "FDE initial location");
597 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
598 end, begin, "FDE address range");
600 else
602 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
603 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
606 if (augmentation[0])
608 if (any_lsda_needed)
610 int size = size_of_encoded_value (lsda_encoding);
612 if (lsda_encoding == DW_EH_PE_aligned)
614 int offset = ( 4 /* Length */
615 + 4 /* CIE offset */
616 + 2 * size_of_encoded_value (fde_encoding)
617 + 1 /* Augmentation size */ );
618 int pad = -offset & (PTR_SIZE - 1);
620 size += pad;
621 gcc_assert (size_of_uleb128 (size) == 1);
624 dw2_asm_output_data_uleb128 (size, "Augmentation size");
626 if (fde->uses_eh_lsda)
628 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
629 fde->funcdef_number);
630 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
631 gen_rtx_SYMBOL_REF (Pmode, l1),
632 false,
633 "Language Specific Data Area");
635 else
637 if (lsda_encoding == DW_EH_PE_aligned)
638 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
639 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
640 "Language Specific Data Area (none)");
643 else
644 dw2_asm_output_data_uleb128 (0, "Augmentation size");
647 /* Loop through the Call Frame Instructions associated with this FDE. */
648 fde->dw_fde_current_label = begin;
650 size_t from, until, i;
652 from = 0;
653 until = vec_safe_length (fde->dw_fde_cfi);
655 if (fde->dw_fde_second_begin == NULL)
657 else if (!second)
658 until = fde->dw_fde_switch_cfi_index;
659 else
660 from = fde->dw_fde_switch_cfi_index;
662 for (i = from; i < until; i++)
663 output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
666 /* If we are to emit a ref/link from function bodies to their frame tables,
667 do it now. This is typically performed to make sure that tables
668 associated with functions are dragged with them and not discarded in
669 garbage collecting links. We need to do this on a per function basis to
670 cope with -ffunction-sections. */
672 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
673 /* Switch to the function section, emit the ref to the tables, and
674 switch *back* into the table section. */
675 switch_to_section (function_section (fde->decl));
676 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
677 switch_to_frame_table_section (for_eh, true);
678 #endif
680 /* Pad the FDE out to an address sized boundary. */
681 ASM_OUTPUT_ALIGN (asm_out_file,
682 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
683 ASM_OUTPUT_LABEL (asm_out_file, l2);
685 j += 2;
688 /* Return true if frame description entry FDE is needed for EH. */
690 static bool
691 fde_needed_for_eh_p (dw_fde_ref fde)
693 if (flag_asynchronous_unwind_tables)
694 return true;
696 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
697 return true;
699 if (fde->uses_eh_lsda)
700 return true;
702 /* If exceptions are enabled, we have collected nothrow info. */
703 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
704 return false;
706 return true;
709 /* Output the call frame information used to record information
710 that relates to calculating the frame pointer, and records the
711 location of saved registers. */
713 static void
714 output_call_frame_info (int for_eh)
716 unsigned int i;
717 dw_fde_ref fde;
718 dw_cfi_ref cfi;
719 char l1[20], l2[20], section_start_label[20];
720 bool any_lsda_needed = false;
721 char augmentation[6];
722 int augmentation_size;
723 int fde_encoding = DW_EH_PE_absptr;
724 int per_encoding = DW_EH_PE_absptr;
725 int lsda_encoding = DW_EH_PE_absptr;
726 int return_reg;
727 rtx personality = NULL;
728 int dw_cie_version;
730 /* Don't emit a CIE if there won't be any FDEs. */
731 if (!fde_vec)
732 return;
734 /* Nothing to do if the assembler's doing it all. */
735 if (dwarf2out_do_cfi_asm ())
736 return;
738 /* If we don't have any functions we'll want to unwind out of, don't emit
739 any EH unwind information. If we make FDEs linkonce, we may have to
740 emit an empty label for an FDE that wouldn't otherwise be emitted. We
741 want to avoid having an FDE kept around when the function it refers to
742 is discarded. Example where this matters: a primary function template
743 in C++ requires EH information, an explicit specialization doesn't. */
744 if (for_eh)
746 bool any_eh_needed = false;
748 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
750 if (fde->uses_eh_lsda)
751 any_eh_needed = any_lsda_needed = true;
752 else if (fde_needed_for_eh_p (fde))
753 any_eh_needed = true;
754 else if (TARGET_USES_WEAK_UNWIND_INFO)
755 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
758 if (!any_eh_needed)
759 return;
762 /* We're going to be generating comments, so turn on app. */
763 if (flag_debug_asm)
764 app_enable ();
766 /* Switch to the proper frame section, first time. */
767 switch_to_frame_table_section (for_eh, false);
769 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
770 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
772 /* Output the CIE. */
773 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
774 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
775 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
776 dw2_asm_output_data (4, 0xffffffff,
777 "Initial length escape value indicating 64-bit DWARF extension");
778 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
779 "Length of Common Information Entry");
780 ASM_OUTPUT_LABEL (asm_out_file, l1);
782 /* Now that the CIE pointer is PC-relative for EH,
783 use 0 to identify the CIE. */
784 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
785 (for_eh ? 0 : DWARF_CIE_ID),
786 "CIE Identifier Tag");
788 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
789 use CIE version 1, unless that would produce incorrect results
790 due to overflowing the return register column. */
791 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
792 dw_cie_version = 1;
793 if (return_reg >= 256 || dwarf_version > 2)
794 dw_cie_version = 3;
795 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
797 augmentation[0] = 0;
798 augmentation_size = 0;
800 personality = current_unit_personality;
801 if (for_eh)
803 char *p;
805 /* Augmentation:
806 z Indicates that a uleb128 is present to size the
807 augmentation section.
808 L Indicates the encoding (and thus presence) of
809 an LSDA pointer in the FDE augmentation.
810 R Indicates a non-default pointer encoding for
811 FDE code pointers.
812 P Indicates the presence of an encoding + language
813 personality routine in the CIE augmentation. */
815 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
816 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
817 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
819 p = augmentation + 1;
820 if (personality)
822 *p++ = 'P';
823 augmentation_size += 1 + size_of_encoded_value (per_encoding);
824 assemble_external_libcall (personality);
826 if (any_lsda_needed)
828 *p++ = 'L';
829 augmentation_size += 1;
831 if (fde_encoding != DW_EH_PE_absptr)
833 *p++ = 'R';
834 augmentation_size += 1;
836 if (p > augmentation + 1)
838 augmentation[0] = 'z';
839 *p = '\0';
842 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
843 if (personality && per_encoding == DW_EH_PE_aligned)
845 int offset = ( 4 /* Length */
846 + 4 /* CIE Id */
847 + 1 /* CIE version */
848 + strlen (augmentation) + 1 /* Augmentation */
849 + size_of_uleb128 (1) /* Code alignment */
850 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
851 + 1 /* RA column */
852 + 1 /* Augmentation size */
853 + 1 /* Personality encoding */ );
854 int pad = -offset & (PTR_SIZE - 1);
856 augmentation_size += pad;
858 /* Augmentations should be small, so there's scarce need to
859 iterate for a solution. Die if we exceed one uleb128 byte. */
860 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
864 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
865 if (dw_cie_version >= 4)
867 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
868 dw2_asm_output_data (1, 0, "CIE Segment Size");
870 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
871 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
872 "CIE Data Alignment Factor");
874 if (dw_cie_version == 1)
875 dw2_asm_output_data (1, return_reg, "CIE RA Column");
876 else
877 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
879 if (augmentation[0])
881 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
882 if (personality)
884 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
885 eh_data_format_name (per_encoding));
886 dw2_asm_output_encoded_addr_rtx (per_encoding,
887 personality,
888 true, NULL);
891 if (any_lsda_needed)
892 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
893 eh_data_format_name (lsda_encoding));
895 if (fde_encoding != DW_EH_PE_absptr)
896 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
897 eh_data_format_name (fde_encoding));
900 FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
901 output_cfi (cfi, NULL, for_eh);
903 /* Pad the CIE out to an address sized boundary. */
904 ASM_OUTPUT_ALIGN (asm_out_file,
905 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
906 ASM_OUTPUT_LABEL (asm_out_file, l2);
908 /* Loop through all of the FDE's. */
909 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
911 unsigned int k;
913 /* Don't emit EH unwind info for leaf functions that don't need it. */
914 if (for_eh && !fde_needed_for_eh_p (fde))
915 continue;
917 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
918 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
919 augmentation, any_lsda_needed, lsda_encoding);
922 if (for_eh && targetm.terminate_dw2_eh_frame_info)
923 dw2_asm_output_data (4, 0, "End of Table");
925 /* Turn off app to make assembly quicker. */
926 if (flag_debug_asm)
927 app_disable ();
930 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
932 static void
933 dwarf2out_do_cfi_startproc (bool second)
935 int enc;
936 rtx ref;
937 rtx personality = get_personality_function (current_function_decl);
939 fprintf (asm_out_file, "\t.cfi_startproc\n");
941 if (personality)
943 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
944 ref = personality;
946 /* ??? The GAS support isn't entirely consistent. We have to
947 handle indirect support ourselves, but PC-relative is done
948 in the assembler. Further, the assembler can't handle any
949 of the weirder relocation types. */
950 if (enc & DW_EH_PE_indirect)
951 ref = dw2_force_const_mem (ref, true);
953 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
954 output_addr_const (asm_out_file, ref);
955 fputc ('\n', asm_out_file);
958 if (crtl->uses_eh_lsda)
960 char lab[20];
962 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
963 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
964 current_function_funcdef_no);
965 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
966 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
968 if (enc & DW_EH_PE_indirect)
969 ref = dw2_force_const_mem (ref, true);
971 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
972 output_addr_const (asm_out_file, ref);
973 fputc ('\n', asm_out_file);
977 /* Allocate CURRENT_FDE. Immediately initialize all we can, noting that
978 this allocation may be done before pass_final. */
980 dw_fde_ref
981 dwarf2out_alloc_current_fde (void)
983 dw_fde_ref fde;
985 fde = ggc_alloc_cleared_dw_fde_node ();
986 fde->decl = current_function_decl;
987 fde->funcdef_number = current_function_funcdef_no;
988 fde->fde_index = vec_safe_length (fde_vec);
989 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
990 fde->uses_eh_lsda = crtl->uses_eh_lsda;
991 fde->nothrow = crtl->nothrow;
992 fde->drap_reg = INVALID_REGNUM;
993 fde->vdrap_reg = INVALID_REGNUM;
995 /* Record the FDE associated with this function. */
996 cfun->fde = fde;
997 vec_safe_push (fde_vec, fde);
999 return fde;
1002 /* Output a marker (i.e. a label) for the beginning of a function, before
1003 the prologue. */
1005 void
1006 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1007 const char *file ATTRIBUTE_UNUSED)
1009 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1010 char * dup_label;
1011 dw_fde_ref fde;
1012 section *fnsec;
1013 bool do_frame;
1015 current_function_func_begin_label = NULL;
1017 do_frame = dwarf2out_do_frame ();
1019 /* ??? current_function_func_begin_label is also used by except.c for
1020 call-site information. We must emit this label if it might be used. */
1021 if (!do_frame
1022 && (!flag_exceptions
1023 || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1024 return;
1026 fnsec = function_section (current_function_decl);
1027 switch_to_section (fnsec);
1028 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1029 current_function_funcdef_no);
1030 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1031 current_function_funcdef_no);
1032 dup_label = xstrdup (label);
1033 current_function_func_begin_label = dup_label;
1035 /* We can elide the fde allocation if we're not emitting debug info. */
1036 if (!do_frame)
1037 return;
1039 /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1040 emit insns as rtx but bypass the bulk of rest_of_compilation, which
1041 would include pass_dwarf2_frame. If we've not created the FDE yet,
1042 do so now. */
1043 fde = cfun->fde;
1044 if (fde == NULL)
1045 fde = dwarf2out_alloc_current_fde ();
1047 /* Initialize the bits of CURRENT_FDE that were not available earlier. */
1048 fde->dw_fde_begin = dup_label;
1049 fde->dw_fde_current_label = dup_label;
1050 fde->in_std_section = (fnsec == text_section
1051 || (cold_text_section && fnsec == cold_text_section));
1053 /* We only want to output line number information for the genuine dwarf2
1054 prologue case, not the eh frame case. */
1055 #ifdef DWARF2_DEBUGGING_INFO
1056 if (file)
1057 dwarf2out_source_line (line, file, 0, true);
1058 #endif
1060 if (dwarf2out_do_cfi_asm ())
1061 dwarf2out_do_cfi_startproc (false);
1062 else
1064 rtx personality = get_personality_function (current_function_decl);
1065 if (!current_unit_personality)
1066 current_unit_personality = personality;
1068 /* We cannot keep a current personality per function as without CFI
1069 asm, at the point where we emit the CFI data, there is no current
1070 function anymore. */
1071 if (personality && current_unit_personality != personality)
1072 sorry ("multiple EH personalities are supported only with assemblers "
1073 "supporting .cfi_personality directive");
1077 /* Output a marker (i.e. a label) for the end of the generated code
1078 for a function prologue. This gets called *after* the prologue code has
1079 been generated. */
1081 void
1082 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1083 const char *file ATTRIBUTE_UNUSED)
1085 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1087 /* Output a label to mark the endpoint of the code generated for this
1088 function. */
1089 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1090 current_function_funcdef_no);
1091 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1092 current_function_funcdef_no);
1093 cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1096 /* Output a marker (i.e. a label) for the beginning of the generated code
1097 for a function epilogue. This gets called *before* the prologue code has
1098 been generated. */
1100 void
1101 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1102 const char *file ATTRIBUTE_UNUSED)
1104 dw_fde_ref fde = cfun->fde;
1105 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1107 if (fde->dw_fde_vms_begin_epilogue)
1108 return;
1110 /* Output a label to mark the endpoint of the code generated for this
1111 function. */
1112 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1113 current_function_funcdef_no);
1114 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1115 current_function_funcdef_no);
1116 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1119 /* Output a marker (i.e. a label) for the absolute end of the generated code
1120 for a function definition. This gets called *after* the epilogue code has
1121 been generated. */
1123 void
1124 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1125 const char *file ATTRIBUTE_UNUSED)
1127 dw_fde_ref fde;
1128 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1130 last_var_location_insn = NULL_RTX;
1131 cached_next_real_insn = NULL_RTX;
1133 if (dwarf2out_do_cfi_asm ())
1134 fprintf (asm_out_file, "\t.cfi_endproc\n");
1136 /* Output a label to mark the endpoint of the code generated for this
1137 function. */
1138 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1139 current_function_funcdef_no);
1140 ASM_OUTPUT_LABEL (asm_out_file, label);
1141 fde = cfun->fde;
1142 gcc_assert (fde != NULL);
1143 if (fde->dw_fde_second_begin == NULL)
1144 fde->dw_fde_end = xstrdup (label);
1147 void
1148 dwarf2out_frame_finish (void)
1150 /* Output call frame information. */
1151 if (targetm.debug_unwind_info () == UI_DWARF2)
1152 output_call_frame_info (0);
1154 /* Output another copy for the unwinder. */
1155 if ((flag_unwind_tables || flag_exceptions)
1156 && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1157 output_call_frame_info (1);
1160 /* Note that the current function section is being used for code. */
1162 static void
1163 dwarf2out_note_section_used (void)
1165 section *sec = current_function_section ();
1166 if (sec == text_section)
1167 text_section_used = true;
1168 else if (sec == cold_text_section)
1169 cold_text_section_used = true;
1172 static void var_location_switch_text_section (void);
1173 static void set_cur_line_info_table (section *);
1175 void
1176 dwarf2out_switch_text_section (void)
1178 section *sect;
1179 dw_fde_ref fde = cfun->fde;
1181 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1183 if (!in_cold_section_p)
1185 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1186 fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1187 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1189 else
1191 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1192 fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1193 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1195 have_multiple_function_sections = true;
1197 /* There is no need to mark used sections when not debugging. */
1198 if (cold_text_section != NULL)
1199 dwarf2out_note_section_used ();
1201 if (dwarf2out_do_cfi_asm ())
1202 fprintf (asm_out_file, "\t.cfi_endproc\n");
1204 /* Now do the real section switch. */
1205 sect = current_function_section ();
1206 switch_to_section (sect);
1208 fde->second_in_std_section
1209 = (sect == text_section
1210 || (cold_text_section && sect == cold_text_section));
1212 if (dwarf2out_do_cfi_asm ())
1213 dwarf2out_do_cfi_startproc (true);
1215 var_location_switch_text_section ();
1217 if (cold_text_section != NULL)
1218 set_cur_line_info_table (sect);
1221 /* And now, the subset of the debugging information support code necessary
1222 for emitting location expressions. */
1224 /* Data about a single source file. */
1225 struct GTY(()) dwarf_file_data {
1226 const char * filename;
1227 int emitted_number;
1230 typedef struct GTY(()) deferred_locations_struct
1232 tree variable;
1233 dw_die_ref die;
1234 } deferred_locations;
1237 static GTY(()) vec<deferred_locations, va_gc> *deferred_locations_list;
1240 /* Describe an entry into the .debug_addr section. */
1242 enum ate_kind {
1243 ate_kind_rtx,
1244 ate_kind_rtx_dtprel,
1245 ate_kind_label
1248 typedef struct GTY(()) addr_table_entry_struct {
1249 enum ate_kind kind;
1250 unsigned int refcount;
1251 unsigned int index;
1252 union addr_table_entry_struct_union
1254 rtx GTY ((tag ("0"))) rtl;
1255 char * GTY ((tag ("1"))) label;
1257 GTY ((desc ("%1.kind"))) addr;
1259 addr_table_entry;
1261 /* Location lists are ranges + location descriptions for that range,
1262 so you can track variables that are in different places over
1263 their entire life. */
1264 typedef struct GTY(()) dw_loc_list_struct {
1265 dw_loc_list_ref dw_loc_next;
1266 const char *begin; /* Label and addr_entry for start of range */
1267 addr_table_entry *begin_entry;
1268 const char *end; /* Label for end of range */
1269 char *ll_symbol; /* Label for beginning of location list.
1270 Only on head of list */
1271 const char *section; /* Section this loclist is relative to */
1272 dw_loc_descr_ref expr;
1273 hashval_t hash;
1274 /* True if all addresses in this and subsequent lists are known to be
1275 resolved. */
1276 bool resolved_addr;
1277 /* True if this list has been replaced by dw_loc_next. */
1278 bool replaced;
1279 bool emitted;
1280 /* True if the range should be emitted even if begin and end
1281 are the same. */
1282 bool force;
1283 } dw_loc_list_node;
1285 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1287 /* Convert a DWARF stack opcode into its string name. */
1289 static const char *
1290 dwarf_stack_op_name (unsigned int op)
1292 const char *name = get_DW_OP_name (op);
1294 if (name != NULL)
1295 return name;
1297 return "OP_<unknown>";
1300 /* Return a pointer to a newly allocated location description. Location
1301 descriptions are simple expression terms that can be strung
1302 together to form more complicated location (address) descriptions. */
1304 static inline dw_loc_descr_ref
1305 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1306 unsigned HOST_WIDE_INT oprnd2)
1308 dw_loc_descr_ref descr = ggc_alloc_cleared_dw_loc_descr_node ();
1310 descr->dw_loc_opc = op;
1311 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1312 descr->dw_loc_oprnd1.val_entry = NULL;
1313 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1314 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1315 descr->dw_loc_oprnd2.val_entry = NULL;
1316 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1318 return descr;
1321 /* Return a pointer to a newly allocated location description for
1322 REG and OFFSET. */
1324 static inline dw_loc_descr_ref
1325 new_reg_loc_descr (unsigned int reg, unsigned HOST_WIDE_INT offset)
1327 if (reg <= 31)
1328 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1329 offset, 0);
1330 else
1331 return new_loc_descr (DW_OP_bregx, reg, offset);
1334 /* Add a location description term to a location description expression. */
1336 static inline void
1337 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1339 dw_loc_descr_ref *d;
1341 /* Find the end of the chain. */
1342 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1345 *d = descr;
1348 /* Compare two location operands for exact equality. */
1350 static bool
1351 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1353 if (a->val_class != b->val_class)
1354 return false;
1355 switch (a->val_class)
1357 case dw_val_class_none:
1358 return true;
1359 case dw_val_class_addr:
1360 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1362 case dw_val_class_offset:
1363 case dw_val_class_unsigned_const:
1364 case dw_val_class_const:
1365 case dw_val_class_range_list:
1366 case dw_val_class_lineptr:
1367 case dw_val_class_macptr:
1368 /* These are all HOST_WIDE_INT, signed or unsigned. */
1369 return a->v.val_unsigned == b->v.val_unsigned;
1371 case dw_val_class_loc:
1372 return a->v.val_loc == b->v.val_loc;
1373 case dw_val_class_loc_list:
1374 return a->v.val_loc_list == b->v.val_loc_list;
1375 case dw_val_class_die_ref:
1376 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1377 case dw_val_class_fde_ref:
1378 return a->v.val_fde_index == b->v.val_fde_index;
1379 case dw_val_class_lbl_id:
1380 case dw_val_class_high_pc:
1381 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1382 case dw_val_class_str:
1383 return a->v.val_str == b->v.val_str;
1384 case dw_val_class_flag:
1385 return a->v.val_flag == b->v.val_flag;
1386 case dw_val_class_file:
1387 return a->v.val_file == b->v.val_file;
1388 case dw_val_class_decl_ref:
1389 return a->v.val_decl_ref == b->v.val_decl_ref;
1391 case dw_val_class_const_double:
1392 return (a->v.val_double.high == b->v.val_double.high
1393 && a->v.val_double.low == b->v.val_double.low);
1395 case dw_val_class_vec:
1397 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1398 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1400 return (a_len == b_len
1401 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1404 case dw_val_class_data8:
1405 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1407 case dw_val_class_vms_delta:
1408 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1409 && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1411 gcc_unreachable ();
1414 /* Compare two location atoms for exact equality. */
1416 static bool
1417 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1419 if (a->dw_loc_opc != b->dw_loc_opc)
1420 return false;
1422 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1423 address size, but since we always allocate cleared storage it
1424 should be zero for other types of locations. */
1425 if (a->dtprel != b->dtprel)
1426 return false;
1428 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1429 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1432 /* Compare two complete location expressions for exact equality. */
1434 bool
1435 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1437 while (1)
1439 if (a == b)
1440 return true;
1441 if (a == NULL || b == NULL)
1442 return false;
1443 if (!loc_descr_equal_p_1 (a, b))
1444 return false;
1446 a = a->dw_loc_next;
1447 b = b->dw_loc_next;
1452 /* Add a constant OFFSET to a location expression. */
1454 static void
1455 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1457 dw_loc_descr_ref loc;
1458 HOST_WIDE_INT *p;
1460 gcc_assert (*list_head != NULL);
1462 if (!offset)
1463 return;
1465 /* Find the end of the chain. */
1466 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1469 p = NULL;
1470 if (loc->dw_loc_opc == DW_OP_fbreg
1471 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1472 p = &loc->dw_loc_oprnd1.v.val_int;
1473 else if (loc->dw_loc_opc == DW_OP_bregx)
1474 p = &loc->dw_loc_oprnd2.v.val_int;
1476 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1477 offset. Don't optimize if an signed integer overflow would happen. */
1478 if (p != NULL
1479 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1480 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1481 *p += offset;
1483 else if (offset > 0)
1484 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1486 else
1488 loc->dw_loc_next = int_loc_descriptor (-offset);
1489 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1493 /* Add a constant OFFSET to a location list. */
1495 static void
1496 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1498 dw_loc_list_ref d;
1499 for (d = list_head; d != NULL; d = d->dw_loc_next)
1500 loc_descr_plus_const (&d->expr, offset);
1503 #define DWARF_REF_SIZE \
1504 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1506 static unsigned long int get_base_type_offset (dw_die_ref);
1508 /* Return the size of a location descriptor. */
1510 static unsigned long
1511 size_of_loc_descr (dw_loc_descr_ref loc)
1513 unsigned long size = 1;
1515 switch (loc->dw_loc_opc)
1517 case DW_OP_addr:
1518 size += DWARF2_ADDR_SIZE;
1519 break;
1520 case DW_OP_GNU_addr_index:
1521 case DW_OP_GNU_const_index:
1522 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1523 size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1524 break;
1525 case DW_OP_const1u:
1526 case DW_OP_const1s:
1527 size += 1;
1528 break;
1529 case DW_OP_const2u:
1530 case DW_OP_const2s:
1531 size += 2;
1532 break;
1533 case DW_OP_const4u:
1534 case DW_OP_const4s:
1535 size += 4;
1536 break;
1537 case DW_OP_const8u:
1538 case DW_OP_const8s:
1539 size += 8;
1540 break;
1541 case DW_OP_constu:
1542 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1543 break;
1544 case DW_OP_consts:
1545 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1546 break;
1547 case DW_OP_pick:
1548 size += 1;
1549 break;
1550 case DW_OP_plus_uconst:
1551 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1552 break;
1553 case DW_OP_skip:
1554 case DW_OP_bra:
1555 size += 2;
1556 break;
1557 case DW_OP_breg0:
1558 case DW_OP_breg1:
1559 case DW_OP_breg2:
1560 case DW_OP_breg3:
1561 case DW_OP_breg4:
1562 case DW_OP_breg5:
1563 case DW_OP_breg6:
1564 case DW_OP_breg7:
1565 case DW_OP_breg8:
1566 case DW_OP_breg9:
1567 case DW_OP_breg10:
1568 case DW_OP_breg11:
1569 case DW_OP_breg12:
1570 case DW_OP_breg13:
1571 case DW_OP_breg14:
1572 case DW_OP_breg15:
1573 case DW_OP_breg16:
1574 case DW_OP_breg17:
1575 case DW_OP_breg18:
1576 case DW_OP_breg19:
1577 case DW_OP_breg20:
1578 case DW_OP_breg21:
1579 case DW_OP_breg22:
1580 case DW_OP_breg23:
1581 case DW_OP_breg24:
1582 case DW_OP_breg25:
1583 case DW_OP_breg26:
1584 case DW_OP_breg27:
1585 case DW_OP_breg28:
1586 case DW_OP_breg29:
1587 case DW_OP_breg30:
1588 case DW_OP_breg31:
1589 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1590 break;
1591 case DW_OP_regx:
1592 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1593 break;
1594 case DW_OP_fbreg:
1595 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1596 break;
1597 case DW_OP_bregx:
1598 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1599 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1600 break;
1601 case DW_OP_piece:
1602 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1603 break;
1604 case DW_OP_bit_piece:
1605 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1606 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1607 break;
1608 case DW_OP_deref_size:
1609 case DW_OP_xderef_size:
1610 size += 1;
1611 break;
1612 case DW_OP_call2:
1613 size += 2;
1614 break;
1615 case DW_OP_call4:
1616 size += 4;
1617 break;
1618 case DW_OP_call_ref:
1619 size += DWARF_REF_SIZE;
1620 break;
1621 case DW_OP_implicit_value:
1622 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1623 + loc->dw_loc_oprnd1.v.val_unsigned;
1624 break;
1625 case DW_OP_GNU_implicit_pointer:
1626 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1627 break;
1628 case DW_OP_GNU_entry_value:
1630 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1631 size += size_of_uleb128 (op_size) + op_size;
1632 break;
1634 case DW_OP_GNU_const_type:
1636 unsigned long o
1637 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1638 size += size_of_uleb128 (o) + 1;
1639 switch (loc->dw_loc_oprnd2.val_class)
1641 case dw_val_class_vec:
1642 size += loc->dw_loc_oprnd2.v.val_vec.length
1643 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1644 break;
1645 case dw_val_class_const:
1646 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1647 break;
1648 case dw_val_class_const_double:
1649 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1650 break;
1651 default:
1652 gcc_unreachable ();
1654 break;
1656 case DW_OP_GNU_regval_type:
1658 unsigned long o
1659 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1660 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1661 + size_of_uleb128 (o);
1663 break;
1664 case DW_OP_GNU_deref_type:
1666 unsigned long o
1667 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1668 size += 1 + size_of_uleb128 (o);
1670 break;
1671 case DW_OP_GNU_convert:
1672 case DW_OP_GNU_reinterpret:
1673 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1674 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1675 else
1677 unsigned long o
1678 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1679 size += size_of_uleb128 (o);
1681 break;
1682 case DW_OP_GNU_parameter_ref:
1683 size += 4;
1684 break;
1685 default:
1686 break;
1689 return size;
1692 /* Return the size of a series of location descriptors. */
1694 unsigned long
1695 size_of_locs (dw_loc_descr_ref loc)
1697 dw_loc_descr_ref l;
1698 unsigned long size;
1700 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1701 field, to avoid writing to a PCH file. */
1702 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1704 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1705 break;
1706 size += size_of_loc_descr (l);
1708 if (! l)
1709 return size;
1711 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1713 l->dw_loc_addr = size;
1714 size += size_of_loc_descr (l);
1717 return size;
1720 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1721 static void get_ref_die_offset_label (char *, dw_die_ref);
1722 static unsigned long int get_ref_die_offset (dw_die_ref);
1724 /* Output location description stack opcode's operands (if any).
1725 The for_eh_or_skip parameter controls whether register numbers are
1726 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1727 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1728 info). This should be suppressed for the cases that have not been converted
1729 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
1731 static void
1732 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1734 dw_val_ref val1 = &loc->dw_loc_oprnd1;
1735 dw_val_ref val2 = &loc->dw_loc_oprnd2;
1737 switch (loc->dw_loc_opc)
1739 #ifdef DWARF2_DEBUGGING_INFO
1740 case DW_OP_const2u:
1741 case DW_OP_const2s:
1742 dw2_asm_output_data (2, val1->v.val_int, NULL);
1743 break;
1744 case DW_OP_const4u:
1745 if (loc->dtprel)
1747 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1748 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1749 val1->v.val_addr);
1750 fputc ('\n', asm_out_file);
1751 break;
1753 /* FALLTHRU */
1754 case DW_OP_const4s:
1755 dw2_asm_output_data (4, val1->v.val_int, NULL);
1756 break;
1757 case DW_OP_const8u:
1758 if (loc->dtprel)
1760 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1761 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1762 val1->v.val_addr);
1763 fputc ('\n', asm_out_file);
1764 break;
1766 /* FALLTHRU */
1767 case DW_OP_const8s:
1768 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
1769 dw2_asm_output_data (8, val1->v.val_int, NULL);
1770 break;
1771 case DW_OP_skip:
1772 case DW_OP_bra:
1774 int offset;
1776 gcc_assert (val1->val_class == dw_val_class_loc);
1777 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
1779 dw2_asm_output_data (2, offset, NULL);
1781 break;
1782 case DW_OP_implicit_value:
1783 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1784 switch (val2->val_class)
1786 case dw_val_class_const:
1787 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
1788 break;
1789 case dw_val_class_vec:
1791 unsigned int elt_size = val2->v.val_vec.elt_size;
1792 unsigned int len = val2->v.val_vec.length;
1793 unsigned int i;
1794 unsigned char *p;
1796 if (elt_size > sizeof (HOST_WIDE_INT))
1798 elt_size /= 2;
1799 len *= 2;
1801 for (i = 0, p = val2->v.val_vec.array;
1802 i < len;
1803 i++, p += elt_size)
1804 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
1805 "fp or vector constant word %u", i);
1807 break;
1808 case dw_val_class_const_double:
1810 unsigned HOST_WIDE_INT first, second;
1812 if (WORDS_BIG_ENDIAN)
1814 first = val2->v.val_double.high;
1815 second = val2->v.val_double.low;
1817 else
1819 first = val2->v.val_double.low;
1820 second = val2->v.val_double.high;
1822 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1823 first, NULL);
1824 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1825 second, NULL);
1827 break;
1828 case dw_val_class_addr:
1829 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
1830 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
1831 break;
1832 default:
1833 gcc_unreachable ();
1835 break;
1836 #else
1837 case DW_OP_const2u:
1838 case DW_OP_const2s:
1839 case DW_OP_const4u:
1840 case DW_OP_const4s:
1841 case DW_OP_const8u:
1842 case DW_OP_const8s:
1843 case DW_OP_skip:
1844 case DW_OP_bra:
1845 case DW_OP_implicit_value:
1846 /* We currently don't make any attempt to make sure these are
1847 aligned properly like we do for the main unwind info, so
1848 don't support emitting things larger than a byte if we're
1849 only doing unwinding. */
1850 gcc_unreachable ();
1851 #endif
1852 case DW_OP_const1u:
1853 case DW_OP_const1s:
1854 dw2_asm_output_data (1, val1->v.val_int, NULL);
1855 break;
1856 case DW_OP_constu:
1857 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1858 break;
1859 case DW_OP_consts:
1860 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1861 break;
1862 case DW_OP_pick:
1863 dw2_asm_output_data (1, val1->v.val_int, NULL);
1864 break;
1865 case DW_OP_plus_uconst:
1866 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1867 break;
1868 case DW_OP_breg0:
1869 case DW_OP_breg1:
1870 case DW_OP_breg2:
1871 case DW_OP_breg3:
1872 case DW_OP_breg4:
1873 case DW_OP_breg5:
1874 case DW_OP_breg6:
1875 case DW_OP_breg7:
1876 case DW_OP_breg8:
1877 case DW_OP_breg9:
1878 case DW_OP_breg10:
1879 case DW_OP_breg11:
1880 case DW_OP_breg12:
1881 case DW_OP_breg13:
1882 case DW_OP_breg14:
1883 case DW_OP_breg15:
1884 case DW_OP_breg16:
1885 case DW_OP_breg17:
1886 case DW_OP_breg18:
1887 case DW_OP_breg19:
1888 case DW_OP_breg20:
1889 case DW_OP_breg21:
1890 case DW_OP_breg22:
1891 case DW_OP_breg23:
1892 case DW_OP_breg24:
1893 case DW_OP_breg25:
1894 case DW_OP_breg26:
1895 case DW_OP_breg27:
1896 case DW_OP_breg28:
1897 case DW_OP_breg29:
1898 case DW_OP_breg30:
1899 case DW_OP_breg31:
1900 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1901 break;
1902 case DW_OP_regx:
1904 unsigned r = val1->v.val_unsigned;
1905 if (for_eh_or_skip >= 0)
1906 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1907 gcc_assert (size_of_uleb128 (r)
1908 == size_of_uleb128 (val1->v.val_unsigned));
1909 dw2_asm_output_data_uleb128 (r, NULL);
1911 break;
1912 case DW_OP_fbreg:
1913 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1914 break;
1915 case DW_OP_bregx:
1917 unsigned r = val1->v.val_unsigned;
1918 if (for_eh_or_skip >= 0)
1919 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1920 gcc_assert (size_of_uleb128 (r)
1921 == size_of_uleb128 (val1->v.val_unsigned));
1922 dw2_asm_output_data_uleb128 (r, NULL);
1923 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
1925 break;
1926 case DW_OP_piece:
1927 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1928 break;
1929 case DW_OP_bit_piece:
1930 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1931 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
1932 break;
1933 case DW_OP_deref_size:
1934 case DW_OP_xderef_size:
1935 dw2_asm_output_data (1, val1->v.val_int, NULL);
1936 break;
1938 case DW_OP_addr:
1939 if (loc->dtprel)
1941 if (targetm.asm_out.output_dwarf_dtprel)
1943 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
1944 DWARF2_ADDR_SIZE,
1945 val1->v.val_addr);
1946 fputc ('\n', asm_out_file);
1948 else
1949 gcc_unreachable ();
1951 else
1953 #ifdef DWARF2_DEBUGGING_INFO
1954 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
1955 #else
1956 gcc_unreachable ();
1957 #endif
1959 break;
1961 case DW_OP_GNU_addr_index:
1962 case DW_OP_GNU_const_index:
1963 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1964 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
1965 "(index into .debug_addr)");
1966 break;
1968 case DW_OP_GNU_implicit_pointer:
1970 char label[MAX_ARTIFICIAL_LABEL_BYTES
1971 + HOST_BITS_PER_WIDE_INT / 2 + 2];
1972 gcc_assert (val1->val_class == dw_val_class_die_ref);
1973 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
1974 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
1975 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
1977 break;
1979 case DW_OP_GNU_entry_value:
1980 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
1981 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
1982 break;
1984 case DW_OP_GNU_const_type:
1986 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
1987 gcc_assert (o);
1988 dw2_asm_output_data_uleb128 (o, NULL);
1989 switch (val2->val_class)
1991 case dw_val_class_const:
1992 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
1993 dw2_asm_output_data (1, l, NULL);
1994 dw2_asm_output_data (l, val2->v.val_int, NULL);
1995 break;
1996 case dw_val_class_vec:
1998 unsigned int elt_size = val2->v.val_vec.elt_size;
1999 unsigned int len = val2->v.val_vec.length;
2000 unsigned int i;
2001 unsigned char *p;
2003 l = len * elt_size;
2004 dw2_asm_output_data (1, l, NULL);
2005 if (elt_size > sizeof (HOST_WIDE_INT))
2007 elt_size /= 2;
2008 len *= 2;
2010 for (i = 0, p = val2->v.val_vec.array;
2011 i < len;
2012 i++, p += elt_size)
2013 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2014 "fp or vector constant word %u", i);
2016 break;
2017 case dw_val_class_const_double:
2019 unsigned HOST_WIDE_INT first, second;
2020 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2022 dw2_asm_output_data (1, 2 * l, NULL);
2023 if (WORDS_BIG_ENDIAN)
2025 first = val2->v.val_double.high;
2026 second = val2->v.val_double.low;
2028 else
2030 first = val2->v.val_double.low;
2031 second = val2->v.val_double.high;
2033 dw2_asm_output_data (l, first, NULL);
2034 dw2_asm_output_data (l, second, NULL);
2036 break;
2037 default:
2038 gcc_unreachable ();
2041 break;
2042 case DW_OP_GNU_regval_type:
2044 unsigned r = val1->v.val_unsigned;
2045 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2046 gcc_assert (o);
2047 if (for_eh_or_skip >= 0)
2049 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2050 gcc_assert (size_of_uleb128 (r)
2051 == size_of_uleb128 (val1->v.val_unsigned));
2053 dw2_asm_output_data_uleb128 (r, NULL);
2054 dw2_asm_output_data_uleb128 (o, NULL);
2056 break;
2057 case DW_OP_GNU_deref_type:
2059 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2060 gcc_assert (o);
2061 dw2_asm_output_data (1, val1->v.val_int, NULL);
2062 dw2_asm_output_data_uleb128 (o, NULL);
2064 break;
2065 case DW_OP_GNU_convert:
2066 case DW_OP_GNU_reinterpret:
2067 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2068 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2069 else
2071 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2072 gcc_assert (o);
2073 dw2_asm_output_data_uleb128 (o, NULL);
2075 break;
2077 case DW_OP_GNU_parameter_ref:
2079 unsigned long o;
2080 gcc_assert (val1->val_class == dw_val_class_die_ref);
2081 o = get_ref_die_offset (val1->v.val_die_ref.die);
2082 dw2_asm_output_data (4, o, NULL);
2084 break;
2086 default:
2087 /* Other codes have no operands. */
2088 break;
2092 /* Output a sequence of location operations.
2093 The for_eh_or_skip parameter controls whether register numbers are
2094 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2095 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2096 info). This should be suppressed for the cases that have not been converted
2097 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2099 void
2100 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2102 for (; loc != NULL; loc = loc->dw_loc_next)
2104 enum dwarf_location_atom opc = loc->dw_loc_opc;
2105 /* Output the opcode. */
2106 if (for_eh_or_skip >= 0
2107 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2109 unsigned r = (opc - DW_OP_breg0);
2110 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2111 gcc_assert (r <= 31);
2112 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2114 else if (for_eh_or_skip >= 0
2115 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2117 unsigned r = (opc - DW_OP_reg0);
2118 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2119 gcc_assert (r <= 31);
2120 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2123 dw2_asm_output_data (1, opc,
2124 "%s", dwarf_stack_op_name (opc));
2126 /* Output the operand(s) (if any). */
2127 output_loc_operands (loc, for_eh_or_skip);
2131 /* Output location description stack opcode's operands (if any).
2132 The output is single bytes on a line, suitable for .cfi_escape. */
2134 static void
2135 output_loc_operands_raw (dw_loc_descr_ref loc)
2137 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2138 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2140 switch (loc->dw_loc_opc)
2142 case DW_OP_addr:
2143 case DW_OP_GNU_addr_index:
2144 case DW_OP_GNU_const_index:
2145 case DW_OP_implicit_value:
2146 /* We cannot output addresses in .cfi_escape, only bytes. */
2147 gcc_unreachable ();
2149 case DW_OP_const1u:
2150 case DW_OP_const1s:
2151 case DW_OP_pick:
2152 case DW_OP_deref_size:
2153 case DW_OP_xderef_size:
2154 fputc (',', asm_out_file);
2155 dw2_asm_output_data_raw (1, val1->v.val_int);
2156 break;
2158 case DW_OP_const2u:
2159 case DW_OP_const2s:
2160 fputc (',', asm_out_file);
2161 dw2_asm_output_data_raw (2, val1->v.val_int);
2162 break;
2164 case DW_OP_const4u:
2165 case DW_OP_const4s:
2166 fputc (',', asm_out_file);
2167 dw2_asm_output_data_raw (4, val1->v.val_int);
2168 break;
2170 case DW_OP_const8u:
2171 case DW_OP_const8s:
2172 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2173 fputc (',', asm_out_file);
2174 dw2_asm_output_data_raw (8, val1->v.val_int);
2175 break;
2177 case DW_OP_skip:
2178 case DW_OP_bra:
2180 int offset;
2182 gcc_assert (val1->val_class == dw_val_class_loc);
2183 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2185 fputc (',', asm_out_file);
2186 dw2_asm_output_data_raw (2, offset);
2188 break;
2190 case DW_OP_regx:
2192 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2193 gcc_assert (size_of_uleb128 (r)
2194 == size_of_uleb128 (val1->v.val_unsigned));
2195 fputc (',', asm_out_file);
2196 dw2_asm_output_data_uleb128_raw (r);
2198 break;
2200 case DW_OP_constu:
2201 case DW_OP_plus_uconst:
2202 case DW_OP_piece:
2203 fputc (',', asm_out_file);
2204 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2205 break;
2207 case DW_OP_bit_piece:
2208 fputc (',', asm_out_file);
2209 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2210 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2211 break;
2213 case DW_OP_consts:
2214 case DW_OP_breg0:
2215 case DW_OP_breg1:
2216 case DW_OP_breg2:
2217 case DW_OP_breg3:
2218 case DW_OP_breg4:
2219 case DW_OP_breg5:
2220 case DW_OP_breg6:
2221 case DW_OP_breg7:
2222 case DW_OP_breg8:
2223 case DW_OP_breg9:
2224 case DW_OP_breg10:
2225 case DW_OP_breg11:
2226 case DW_OP_breg12:
2227 case DW_OP_breg13:
2228 case DW_OP_breg14:
2229 case DW_OP_breg15:
2230 case DW_OP_breg16:
2231 case DW_OP_breg17:
2232 case DW_OP_breg18:
2233 case DW_OP_breg19:
2234 case DW_OP_breg20:
2235 case DW_OP_breg21:
2236 case DW_OP_breg22:
2237 case DW_OP_breg23:
2238 case DW_OP_breg24:
2239 case DW_OP_breg25:
2240 case DW_OP_breg26:
2241 case DW_OP_breg27:
2242 case DW_OP_breg28:
2243 case DW_OP_breg29:
2244 case DW_OP_breg30:
2245 case DW_OP_breg31:
2246 case DW_OP_fbreg:
2247 fputc (',', asm_out_file);
2248 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2249 break;
2251 case DW_OP_bregx:
2253 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2254 gcc_assert (size_of_uleb128 (r)
2255 == size_of_uleb128 (val1->v.val_unsigned));
2256 fputc (',', asm_out_file);
2257 dw2_asm_output_data_uleb128_raw (r);
2258 fputc (',', asm_out_file);
2259 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2261 break;
2263 case DW_OP_GNU_implicit_pointer:
2264 case DW_OP_GNU_entry_value:
2265 case DW_OP_GNU_const_type:
2266 case DW_OP_GNU_regval_type:
2267 case DW_OP_GNU_deref_type:
2268 case DW_OP_GNU_convert:
2269 case DW_OP_GNU_reinterpret:
2270 case DW_OP_GNU_parameter_ref:
2271 gcc_unreachable ();
2272 break;
2274 default:
2275 /* Other codes have no operands. */
2276 break;
2280 void
2281 output_loc_sequence_raw (dw_loc_descr_ref loc)
2283 while (1)
2285 enum dwarf_location_atom opc = loc->dw_loc_opc;
2286 /* Output the opcode. */
2287 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2289 unsigned r = (opc - DW_OP_breg0);
2290 r = DWARF2_FRAME_REG_OUT (r, 1);
2291 gcc_assert (r <= 31);
2292 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2294 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2296 unsigned r = (opc - DW_OP_reg0);
2297 r = DWARF2_FRAME_REG_OUT (r, 1);
2298 gcc_assert (r <= 31);
2299 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2301 /* Output the opcode. */
2302 fprintf (asm_out_file, "%#x", opc);
2303 output_loc_operands_raw (loc);
2305 if (!loc->dw_loc_next)
2306 break;
2307 loc = loc->dw_loc_next;
2309 fputc (',', asm_out_file);
2313 /* This function builds a dwarf location descriptor sequence from a
2314 dw_cfa_location, adding the given OFFSET to the result of the
2315 expression. */
2317 struct dw_loc_descr_node *
2318 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2320 struct dw_loc_descr_node *head, *tmp;
2322 offset += cfa->offset;
2324 if (cfa->indirect)
2326 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2327 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2328 head->dw_loc_oprnd1.val_entry = NULL;
2329 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2330 add_loc_descr (&head, tmp);
2331 if (offset != 0)
2333 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2334 add_loc_descr (&head, tmp);
2337 else
2338 head = new_reg_loc_descr (cfa->reg, offset);
2340 return head;
2343 /* This function builds a dwarf location descriptor sequence for
2344 the address at OFFSET from the CFA when stack is aligned to
2345 ALIGNMENT byte. */
2347 struct dw_loc_descr_node *
2348 build_cfa_aligned_loc (dw_cfa_location *cfa,
2349 HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2351 struct dw_loc_descr_node *head;
2352 unsigned int dwarf_fp
2353 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2355 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2356 if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2358 head = new_reg_loc_descr (dwarf_fp, 0);
2359 add_loc_descr (&head, int_loc_descriptor (alignment));
2360 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2361 loc_descr_plus_const (&head, offset);
2363 else
2364 head = new_reg_loc_descr (dwarf_fp, offset);
2365 return head;
2368 /* And now, the support for symbolic debugging information. */
2370 /* .debug_str support. */
2371 static int output_indirect_string (void **, void *);
2373 static void dwarf2out_init (const char *);
2374 static void dwarf2out_finish (const char *);
2375 static void dwarf2out_assembly_start (void);
2376 static void dwarf2out_define (unsigned int, const char *);
2377 static void dwarf2out_undef (unsigned int, const char *);
2378 static void dwarf2out_start_source_file (unsigned, const char *);
2379 static void dwarf2out_end_source_file (unsigned);
2380 static void dwarf2out_function_decl (tree);
2381 static void dwarf2out_begin_block (unsigned, unsigned);
2382 static void dwarf2out_end_block (unsigned, unsigned);
2383 static bool dwarf2out_ignore_block (const_tree);
2384 static void dwarf2out_global_decl (tree);
2385 static void dwarf2out_type_decl (tree, int);
2386 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
2387 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2388 dw_die_ref);
2389 static void dwarf2out_abstract_function (tree);
2390 static void dwarf2out_var_location (rtx);
2391 static void dwarf2out_begin_function (tree);
2392 static void dwarf2out_end_function (unsigned int);
2393 static void dwarf2out_set_name (tree, tree);
2395 /* The debug hooks structure. */
2397 const struct gcc_debug_hooks dwarf2_debug_hooks =
2399 dwarf2out_init,
2400 dwarf2out_finish,
2401 dwarf2out_assembly_start,
2402 dwarf2out_define,
2403 dwarf2out_undef,
2404 dwarf2out_start_source_file,
2405 dwarf2out_end_source_file,
2406 dwarf2out_begin_block,
2407 dwarf2out_end_block,
2408 dwarf2out_ignore_block,
2409 dwarf2out_source_line,
2410 dwarf2out_begin_prologue,
2411 #if VMS_DEBUGGING_INFO
2412 dwarf2out_vms_end_prologue,
2413 dwarf2out_vms_begin_epilogue,
2414 #else
2415 debug_nothing_int_charstar,
2416 debug_nothing_int_charstar,
2417 #endif
2418 dwarf2out_end_epilogue,
2419 dwarf2out_begin_function,
2420 dwarf2out_end_function, /* end_function */
2421 dwarf2out_function_decl, /* function_decl */
2422 dwarf2out_global_decl,
2423 dwarf2out_type_decl, /* type_decl */
2424 dwarf2out_imported_module_or_decl,
2425 debug_nothing_tree, /* deferred_inline_function */
2426 /* The DWARF 2 backend tries to reduce debugging bloat by not
2427 emitting the abstract description of inline functions until
2428 something tries to reference them. */
2429 dwarf2out_abstract_function, /* outlining_inline_function */
2430 debug_nothing_rtx, /* label */
2431 debug_nothing_int, /* handle_pch */
2432 dwarf2out_var_location,
2433 dwarf2out_switch_text_section,
2434 dwarf2out_set_name,
2435 1, /* start_end_main_source_file */
2436 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2439 /* NOTE: In the comments in this file, many references are made to
2440 "Debugging Information Entries". This term is abbreviated as `DIE'
2441 throughout the remainder of this file. */
2443 /* An internal representation of the DWARF output is built, and then
2444 walked to generate the DWARF debugging info. The walk of the internal
2445 representation is done after the entire program has been compiled.
2446 The types below are used to describe the internal representation. */
2448 /* Whether to put type DIEs into their own section .debug_types instead
2449 of making them part of the .debug_info section. Only supported for
2450 Dwarf V4 or higher and the user didn't disable them through
2451 -fno-debug-types-section. It is more efficient to put them in a
2452 separate comdat sections since the linker will then be able to
2453 remove duplicates. But not all tools support .debug_types sections
2454 yet. */
2456 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2458 /* Various DIE's use offsets relative to the beginning of the
2459 .debug_info section to refer to each other. */
2461 typedef long int dw_offset;
2463 /* Define typedefs here to avoid circular dependencies. */
2465 typedef struct dw_attr_struct *dw_attr_ref;
2466 typedef struct dw_line_info_struct *dw_line_info_ref;
2467 typedef struct pubname_struct *pubname_ref;
2468 typedef struct dw_ranges_struct *dw_ranges_ref;
2469 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
2470 typedef struct comdat_type_struct *comdat_type_node_ref;
2472 /* The entries in the line_info table more-or-less mirror the opcodes
2473 that are used in the real dwarf line table. Arrays of these entries
2474 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2475 supported. */
2477 enum dw_line_info_opcode {
2478 /* Emit DW_LNE_set_address; the operand is the label index. */
2479 LI_set_address,
2481 /* Emit a row to the matrix with the given line. This may be done
2482 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2483 special opcodes. */
2484 LI_set_line,
2486 /* Emit a DW_LNS_set_file. */
2487 LI_set_file,
2489 /* Emit a DW_LNS_set_column. */
2490 LI_set_column,
2492 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
2493 LI_negate_stmt,
2495 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
2496 LI_set_prologue_end,
2497 LI_set_epilogue_begin,
2499 /* Emit a DW_LNE_set_discriminator. */
2500 LI_set_discriminator
2503 typedef struct GTY(()) dw_line_info_struct {
2504 enum dw_line_info_opcode opcode;
2505 unsigned int val;
2506 } dw_line_info_entry;
2509 typedef struct GTY(()) dw_line_info_table_struct {
2510 /* The label that marks the end of this section. */
2511 const char *end_label;
2513 /* The values for the last row of the matrix, as collected in the table.
2514 These are used to minimize the changes to the next row. */
2515 unsigned int file_num;
2516 unsigned int line_num;
2517 unsigned int column_num;
2518 int discrim_num;
2519 bool is_stmt;
2520 bool in_use;
2522 vec<dw_line_info_entry, va_gc> *entries;
2523 } dw_line_info_table;
2525 typedef dw_line_info_table *dw_line_info_table_p;
2528 /* Each DIE attribute has a field specifying the attribute kind,
2529 a link to the next attribute in the chain, and an attribute value.
2530 Attributes are typically linked below the DIE they modify. */
2532 typedef struct GTY(()) dw_attr_struct {
2533 enum dwarf_attribute dw_attr;
2534 dw_val_node dw_attr_val;
2536 dw_attr_node;
2539 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
2540 The children of each node form a circular list linked by
2541 die_sib. die_child points to the node *before* the "first" child node. */
2543 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
2544 union die_symbol_or_type_node
2546 const char * GTY ((tag ("0"))) die_symbol;
2547 comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
2549 GTY ((desc ("%0.comdat_type_p"))) die_id;
2550 vec<dw_attr_node, va_gc> *die_attr;
2551 dw_die_ref die_parent;
2552 dw_die_ref die_child;
2553 dw_die_ref die_sib;
2554 dw_die_ref die_definition; /* ref from a specification to its definition */
2555 dw_offset die_offset;
2556 unsigned long die_abbrev;
2557 int die_mark;
2558 unsigned int decl_id;
2559 enum dwarf_tag die_tag;
2560 /* Die is used and must not be pruned as unused. */
2561 BOOL_BITFIELD die_perennial_p : 1;
2562 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2563 /* Lots of spare bits. */
2565 die_node;
2567 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
2568 #define FOR_EACH_CHILD(die, c, expr) do { \
2569 c = die->die_child; \
2570 if (c) do { \
2571 c = c->die_sib; \
2572 expr; \
2573 } while (c != die->die_child); \
2574 } while (0)
2576 /* The pubname structure */
2578 typedef struct GTY(()) pubname_struct {
2579 dw_die_ref die;
2580 const char *name;
2582 pubname_entry;
2585 struct GTY(()) dw_ranges_struct {
2586 /* If this is positive, it's a block number, otherwise it's a
2587 bitwise-negated index into dw_ranges_by_label. */
2588 int num;
2591 /* A structure to hold a macinfo entry. */
2593 typedef struct GTY(()) macinfo_struct {
2594 unsigned char code;
2595 unsigned HOST_WIDE_INT lineno;
2596 const char *info;
2598 macinfo_entry;
2601 struct GTY(()) dw_ranges_by_label_struct {
2602 const char *begin;
2603 const char *end;
2606 /* The comdat type node structure. */
2607 typedef struct GTY(()) comdat_type_struct
2609 dw_die_ref root_die;
2610 dw_die_ref type_die;
2611 dw_die_ref skeleton_die;
2612 char signature[DWARF_TYPE_SIGNATURE_SIZE];
2613 struct comdat_type_struct *next;
2615 comdat_type_node;
2617 /* The limbo die list structure. */
2618 typedef struct GTY(()) limbo_die_struct {
2619 dw_die_ref die;
2620 tree created_for;
2621 struct limbo_die_struct *next;
2623 limbo_die_node;
2625 typedef struct skeleton_chain_struct
2627 dw_die_ref old_die;
2628 dw_die_ref new_die;
2629 struct skeleton_chain_struct *parent;
2631 skeleton_chain_node;
2633 /* Define a macro which returns nonzero for a TYPE_DECL which was
2634 implicitly generated for a type.
2636 Note that, unlike the C front-end (which generates a NULL named
2637 TYPE_DECL node for each complete tagged type, each array type,
2638 and each function type node created) the C++ front-end generates
2639 a _named_ TYPE_DECL node for each tagged type node created.
2640 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
2641 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
2642 front-end, but for each type, tagged or not. */
2644 #define TYPE_DECL_IS_STUB(decl) \
2645 (DECL_NAME (decl) == NULL_TREE \
2646 || (DECL_ARTIFICIAL (decl) \
2647 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
2648 /* This is necessary for stub decls that \
2649 appear in nested inline functions. */ \
2650 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
2651 && (decl_ultimate_origin (decl) \
2652 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
2654 /* Information concerning the compilation unit's programming
2655 language, and compiler version. */
2657 /* Fixed size portion of the DWARF compilation unit header. */
2658 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
2659 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
2661 /* Fixed size portion of the DWARF comdat type unit header. */
2662 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
2663 (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
2664 + DWARF_OFFSET_SIZE)
2666 /* Fixed size portion of public names info. */
2667 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
2669 /* Fixed size portion of the address range info. */
2670 #define DWARF_ARANGES_HEADER_SIZE \
2671 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2672 DWARF2_ADDR_SIZE * 2) \
2673 - DWARF_INITIAL_LENGTH_SIZE)
2675 /* Size of padding portion in the address range info. It must be
2676 aligned to twice the pointer size. */
2677 #define DWARF_ARANGES_PAD_SIZE \
2678 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2679 DWARF2_ADDR_SIZE * 2) \
2680 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
2682 /* Use assembler line directives if available. */
2683 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
2684 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
2685 #define DWARF2_ASM_LINE_DEBUG_INFO 1
2686 #else
2687 #define DWARF2_ASM_LINE_DEBUG_INFO 0
2688 #endif
2689 #endif
2691 /* Minimum line offset in a special line info. opcode.
2692 This value was chosen to give a reasonable range of values. */
2693 #define DWARF_LINE_BASE -10
2695 /* First special line opcode - leave room for the standard opcodes. */
2696 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
2698 /* Range of line offsets in a special line info. opcode. */
2699 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
2701 /* Flag that indicates the initial value of the is_stmt_start flag.
2702 In the present implementation, we do not mark any lines as
2703 the beginning of a source statement, because that information
2704 is not made available by the GCC front-end. */
2705 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
2707 /* Maximum number of operations per instruction bundle. */
2708 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
2709 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
2710 #endif
2712 /* This location is used by calc_die_sizes() to keep track
2713 the offset of each DIE within the .debug_info section. */
2714 static unsigned long next_die_offset;
2716 /* Record the root of the DIE's built for the current compilation unit. */
2717 static GTY(()) dw_die_ref single_comp_unit_die;
2719 /* A list of type DIEs that have been separated into comdat sections. */
2720 static GTY(()) comdat_type_node *comdat_type_list;
2722 /* A list of DIEs with a NULL parent waiting to be relocated. */
2723 static GTY(()) limbo_die_node *limbo_die_list;
2725 /* A list of DIEs for which we may have to generate
2726 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
2727 static GTY(()) limbo_die_node *deferred_asm_name;
2729 /* Filenames referenced by this compilation unit. */
2730 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
2732 /* A hash table of references to DIE's that describe declarations.
2733 The key is a DECL_UID() which is a unique number identifying each decl. */
2734 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
2736 /* A hash table of references to DIE's that describe COMMON blocks.
2737 The key is DECL_UID() ^ die_parent. */
2738 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
2740 typedef struct GTY(()) die_arg_entry_struct {
2741 dw_die_ref die;
2742 tree arg;
2743 } die_arg_entry;
2746 /* Node of the variable location list. */
2747 struct GTY ((chain_next ("%h.next"))) var_loc_node {
2748 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
2749 EXPR_LIST chain. For small bitsizes, bitsize is encoded
2750 in mode of the EXPR_LIST node and first EXPR_LIST operand
2751 is either NOTE_INSN_VAR_LOCATION for a piece with a known
2752 location or NULL for padding. For larger bitsizes,
2753 mode is 0 and first operand is a CONCAT with bitsize
2754 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
2755 NULL as second operand. */
2756 rtx GTY (()) loc;
2757 const char * GTY (()) label;
2758 struct var_loc_node * GTY (()) next;
2761 /* Variable location list. */
2762 struct GTY (()) var_loc_list_def {
2763 struct var_loc_node * GTY (()) first;
2765 /* Pointer to the last but one or last element of the
2766 chained list. If the list is empty, both first and
2767 last are NULL, if the list contains just one node
2768 or the last node certainly is not redundant, it points
2769 to the last node, otherwise points to the last but one.
2770 Do not mark it for GC because it is marked through the chain. */
2771 struct var_loc_node * GTY ((skip ("%h"))) last;
2773 /* Pointer to the last element before section switch,
2774 if NULL, either sections weren't switched or first
2775 is after section switch. */
2776 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
2778 /* DECL_UID of the variable decl. */
2779 unsigned int decl_id;
2781 typedef struct var_loc_list_def var_loc_list;
2783 /* Call argument location list. */
2784 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
2785 rtx GTY (()) call_arg_loc_note;
2786 const char * GTY (()) label;
2787 tree GTY (()) block;
2788 bool tail_call_p;
2789 rtx GTY (()) symbol_ref;
2790 struct call_arg_loc_node * GTY (()) next;
2794 /* Table of decl location linked lists. */
2795 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
2797 /* Head and tail of call_arg_loc chain. */
2798 static GTY (()) struct call_arg_loc_node *call_arg_locations;
2799 static struct call_arg_loc_node *call_arg_loc_last;
2801 /* Number of call sites in the current function. */
2802 static int call_site_count = -1;
2803 /* Number of tail call sites in the current function. */
2804 static int tail_call_site_count = -1;
2806 /* Vector mapping block numbers to DW_TAG_{lexical_block,inlined_subroutine}
2807 DIEs. */
2808 static vec<dw_die_ref> block_map;
2810 /* A cached location list. */
2811 struct GTY (()) cached_dw_loc_list_def {
2812 /* The DECL_UID of the decl that this entry describes. */
2813 unsigned int decl_id;
2815 /* The cached location list. */
2816 dw_loc_list_ref loc_list;
2818 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
2820 /* Table of cached location lists. */
2821 static GTY ((param_is (cached_dw_loc_list))) htab_t cached_dw_loc_list_table;
2823 /* A pointer to the base of a list of references to DIE's that
2824 are uniquely identified by their tag, presence/absence of
2825 children DIE's, and list of attribute/value pairs. */
2826 static GTY((length ("abbrev_die_table_allocated")))
2827 dw_die_ref *abbrev_die_table;
2829 /* Number of elements currently allocated for abbrev_die_table. */
2830 static GTY(()) unsigned abbrev_die_table_allocated;
2832 /* Number of elements in type_die_table currently in use. */
2833 static GTY(()) unsigned abbrev_die_table_in_use;
2835 /* Size (in elements) of increments by which we may expand the
2836 abbrev_die_table. */
2837 #define ABBREV_DIE_TABLE_INCREMENT 256
2839 /* A global counter for generating labels for line number data. */
2840 static unsigned int line_info_label_num;
2842 /* The current table to which we should emit line number information
2843 for the current function. This will be set up at the beginning of
2844 assembly for the function. */
2845 static dw_line_info_table *cur_line_info_table;
2847 /* The two default tables of line number info. */
2848 static GTY(()) dw_line_info_table *text_section_line_info;
2849 static GTY(()) dw_line_info_table *cold_text_section_line_info;
2851 /* The set of all non-default tables of line number info. */
2852 static GTY(()) vec<dw_line_info_table_p, va_gc> *separate_line_info;
2854 /* A flag to tell pubnames/types export if there is an info section to
2855 refer to. */
2856 static bool info_section_emitted;
2858 /* A pointer to the base of a table that contains a list of publicly
2859 accessible names. */
2860 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
2862 /* A pointer to the base of a table that contains a list of publicly
2863 accessible types. */
2864 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
2866 /* A pointer to the base of a table that contains a list of macro
2867 defines/undefines (and file start/end markers). */
2868 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
2870 /* True if .debug_macinfo or .debug_macros section is going to be
2871 emitted. */
2872 #define have_macinfo \
2873 (debug_info_level >= DINFO_LEVEL_VERBOSE \
2874 && !macinfo_table->is_empty ())
2876 /* Array of dies for which we should generate .debug_ranges info. */
2877 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
2879 /* Number of elements currently allocated for ranges_table. */
2880 static GTY(()) unsigned ranges_table_allocated;
2882 /* Number of elements in ranges_table currently in use. */
2883 static GTY(()) unsigned ranges_table_in_use;
2885 /* Array of pairs of labels referenced in ranges_table. */
2886 static GTY ((length ("ranges_by_label_allocated")))
2887 dw_ranges_by_label_ref ranges_by_label;
2889 /* Number of elements currently allocated for ranges_by_label. */
2890 static GTY(()) unsigned ranges_by_label_allocated;
2892 /* Number of elements in ranges_by_label currently in use. */
2893 static GTY(()) unsigned ranges_by_label_in_use;
2895 /* Size (in elements) of increments by which we may expand the
2896 ranges_table. */
2897 #define RANGES_TABLE_INCREMENT 64
2899 /* Whether we have location lists that need outputting */
2900 static GTY(()) bool have_location_lists;
2902 /* Unique label counter. */
2903 static GTY(()) unsigned int loclabel_num;
2905 /* Unique label counter for point-of-call tables. */
2906 static GTY(()) unsigned int poc_label_num;
2908 /* Record whether the function being analyzed contains inlined functions. */
2909 static int current_function_has_inlines;
2911 /* The last file entry emitted by maybe_emit_file(). */
2912 static GTY(()) struct dwarf_file_data * last_emitted_file;
2914 /* Number of internal labels generated by gen_internal_sym(). */
2915 static GTY(()) int label_num;
2917 /* Cached result of previous call to lookup_filename. */
2918 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
2920 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
2922 /* Instances of generic types for which we need to generate debug
2923 info that describe their generic parameters and arguments. That
2924 generation needs to happen once all types are properly laid out so
2925 we do it at the end of compilation. */
2926 static GTY(()) vec<tree, va_gc> *generic_type_instances;
2928 /* Offset from the "steady-state frame pointer" to the frame base,
2929 within the current function. */
2930 static HOST_WIDE_INT frame_pointer_fb_offset;
2931 static bool frame_pointer_fb_offset_valid;
2933 static vec<dw_die_ref> base_types;
2935 /* Forward declarations for functions defined in this file. */
2937 static int is_pseudo_reg (const_rtx);
2938 static tree type_main_variant (tree);
2939 static int is_tagged_type (const_tree);
2940 static const char *dwarf_tag_name (unsigned);
2941 static const char *dwarf_attr_name (unsigned);
2942 static const char *dwarf_form_name (unsigned);
2943 static tree decl_ultimate_origin (const_tree);
2944 static tree decl_class_context (tree);
2945 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
2946 static inline enum dw_val_class AT_class (dw_attr_ref);
2947 static inline unsigned int AT_index (dw_attr_ref);
2948 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
2949 static inline unsigned AT_flag (dw_attr_ref);
2950 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
2951 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
2952 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
2953 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
2954 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
2955 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
2956 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
2957 unsigned int, unsigned char *);
2958 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
2959 static hashval_t debug_str_do_hash (const void *);
2960 static int debug_str_eq (const void *, const void *);
2961 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
2962 static inline const char *AT_string (dw_attr_ref);
2963 static enum dwarf_form AT_string_form (dw_attr_ref);
2964 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
2965 static void add_AT_specification (dw_die_ref, dw_die_ref);
2966 static inline dw_die_ref AT_ref (dw_attr_ref);
2967 static inline int AT_ref_external (dw_attr_ref);
2968 static inline void set_AT_ref_external (dw_attr_ref, int);
2969 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
2970 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
2971 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
2972 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
2973 dw_loc_list_ref);
2974 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
2975 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
2976 static void remove_addr_table_entry (addr_table_entry *);
2977 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
2978 static inline rtx AT_addr (dw_attr_ref);
2979 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
2980 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
2981 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
2982 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
2983 unsigned HOST_WIDE_INT);
2984 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
2985 unsigned long, bool);
2986 static inline const char *AT_lbl (dw_attr_ref);
2987 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
2988 static const char *get_AT_low_pc (dw_die_ref);
2989 static const char *get_AT_hi_pc (dw_die_ref);
2990 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
2991 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
2992 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
2993 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
2994 static bool is_cxx (void);
2995 static bool is_fortran (void);
2996 static bool is_ada (void);
2997 static void remove_AT (dw_die_ref, enum dwarf_attribute);
2998 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
2999 static void add_child_die (dw_die_ref, dw_die_ref);
3000 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3001 static dw_die_ref lookup_type_die (tree);
3002 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3003 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3004 static void equate_type_number_to_die (tree, dw_die_ref);
3005 static hashval_t decl_die_table_hash (const void *);
3006 static int decl_die_table_eq (const void *, const void *);
3007 static dw_die_ref lookup_decl_die (tree);
3008 static hashval_t common_block_die_table_hash (const void *);
3009 static int common_block_die_table_eq (const void *, const void *);
3010 static hashval_t decl_loc_table_hash (const void *);
3011 static int decl_loc_table_eq (const void *, const void *);
3012 static var_loc_list *lookup_decl_loc (const_tree);
3013 static void equate_decl_number_to_die (tree, dw_die_ref);
3014 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3015 static void print_spaces (FILE *);
3016 static void print_die (dw_die_ref, FILE *);
3017 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3018 static dw_die_ref pop_compile_unit (dw_die_ref);
3019 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3020 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
3021 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3022 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3023 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3024 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3025 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
3026 struct md5_ctx *, int *);
3027 struct checksum_attributes;
3028 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3029 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3030 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3031 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3032 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3033 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3034 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
3035 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3036 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3037 static void compute_section_prefix (dw_die_ref);
3038 static int is_type_die (dw_die_ref);
3039 static int is_comdat_die (dw_die_ref);
3040 static int is_symbol_die (dw_die_ref);
3041 static inline bool is_template_instantiation (dw_die_ref);
3042 static void assign_symbol_names (dw_die_ref);
3043 static void break_out_includes (dw_die_ref);
3044 static int is_declaration_die (dw_die_ref);
3045 static int should_move_die_to_comdat (dw_die_ref);
3046 static dw_die_ref clone_as_declaration (dw_die_ref);
3047 static dw_die_ref clone_die (dw_die_ref);
3048 static dw_die_ref clone_tree (dw_die_ref);
3049 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3050 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3051 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3052 static dw_die_ref generate_skeleton (dw_die_ref);
3053 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3054 dw_die_ref,
3055 dw_die_ref);
3056 static void break_out_comdat_types (dw_die_ref);
3057 static void copy_decls_for_unworthy_types (dw_die_ref);
3059 static void add_sibling_attributes (dw_die_ref);
3060 static void output_location_lists (dw_die_ref);
3061 static int constant_size (unsigned HOST_WIDE_INT);
3062 static unsigned long size_of_die (dw_die_ref);
3063 static void calc_die_sizes (dw_die_ref);
3064 static void calc_base_type_die_sizes (void);
3065 static void mark_dies (dw_die_ref);
3066 static void unmark_dies (dw_die_ref);
3067 static void unmark_all_dies (dw_die_ref);
3068 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3069 static unsigned long size_of_aranges (void);
3070 static enum dwarf_form value_format (dw_attr_ref);
3071 static void output_value_format (dw_attr_ref);
3072 static void output_abbrev_section (void);
3073 static void output_die_abbrevs (unsigned long, dw_die_ref);
3074 static void output_die_symbol (dw_die_ref);
3075 static void output_die (dw_die_ref);
3076 static void output_compilation_unit_header (void);
3077 static void output_comp_unit (dw_die_ref, int);
3078 static void output_comdat_type_unit (comdat_type_node *);
3079 static const char *dwarf2_name (tree, int);
3080 static void add_pubname (tree, dw_die_ref);
3081 static void add_enumerator_pubname (const char *, dw_die_ref);
3082 static void add_pubname_string (const char *, dw_die_ref);
3083 static void add_pubtype (tree, dw_die_ref);
3084 static void output_pubnames (vec<pubname_entry, va_gc> *);
3085 static void output_aranges (unsigned long);
3086 static unsigned int add_ranges_num (int);
3087 static unsigned int add_ranges (const_tree);
3088 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3089 bool *, bool);
3090 static void output_ranges (void);
3091 static dw_line_info_table *new_line_info_table (void);
3092 static void output_line_info (bool);
3093 static void output_file_names (void);
3094 static dw_die_ref base_type_die (tree);
3095 static int is_base_type (tree);
3096 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
3097 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
3098 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3099 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3100 static int type_is_enum (const_tree);
3101 static unsigned int dbx_reg_number (const_rtx);
3102 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3103 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3104 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3105 enum var_init_status);
3106 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3107 enum var_init_status);
3108 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3109 enum var_init_status);
3110 static int is_based_loc (const_rtx);
3111 static int resolve_one_addr (rtx *, void *);
3112 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3113 enum var_init_status);
3114 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
3115 enum var_init_status);
3116 static dw_loc_list_ref loc_list_from_tree (tree, int);
3117 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
3118 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3119 static tree field_type (const_tree);
3120 static unsigned int simple_type_align_in_bits (const_tree);
3121 static unsigned int simple_decl_align_in_bits (const_tree);
3122 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3123 static HOST_WIDE_INT field_byte_offset (const_tree);
3124 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3125 dw_loc_list_ref);
3126 static void add_data_member_location_attribute (dw_die_ref, tree);
3127 static bool add_const_value_attribute (dw_die_ref, rtx);
3128 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3129 static void insert_double (double_int, unsigned char *);
3130 static void insert_float (const_rtx, unsigned char *);
3131 static rtx rtl_for_decl_location (tree);
3132 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool,
3133 enum dwarf_attribute);
3134 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3135 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3136 static void add_name_attribute (dw_die_ref, const char *);
3137 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3138 static void add_comp_dir_attribute (dw_die_ref);
3139 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
3140 static void add_subscript_info (dw_die_ref, tree, bool);
3141 static void add_byte_size_attribute (dw_die_ref, tree);
3142 static void add_bit_offset_attribute (dw_die_ref, tree);
3143 static void add_bit_size_attribute (dw_die_ref, tree);
3144 static void add_prototyped_attribute (dw_die_ref, tree);
3145 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3146 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3147 static void add_src_coords_attributes (dw_die_ref, tree);
3148 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3149 static void push_decl_scope (tree);
3150 static void pop_decl_scope (void);
3151 static dw_die_ref scope_die_for (tree, dw_die_ref);
3152 static inline int local_scope_p (dw_die_ref);
3153 static inline int class_scope_p (dw_die_ref);
3154 static inline int class_or_namespace_scope_p (dw_die_ref);
3155 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
3156 static void add_calling_convention_attribute (dw_die_ref, tree);
3157 static const char *type_tag (const_tree);
3158 static tree member_declared_type (const_tree);
3159 #if 0
3160 static const char *decl_start_label (tree);
3161 #endif
3162 static void gen_array_type_die (tree, dw_die_ref);
3163 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3164 #if 0
3165 static void gen_entry_point_die (tree, dw_die_ref);
3166 #endif
3167 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3168 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3169 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3170 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3171 static void gen_formal_types_die (tree, dw_die_ref);
3172 static void gen_subprogram_die (tree, dw_die_ref);
3173 static void gen_variable_die (tree, tree, dw_die_ref);
3174 static void gen_const_die (tree, dw_die_ref);
3175 static void gen_label_die (tree, dw_die_ref);
3176 static void gen_lexical_block_die (tree, dw_die_ref, int);
3177 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
3178 static void gen_field_die (tree, dw_die_ref);
3179 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3180 static dw_die_ref gen_compile_unit_die (const char *);
3181 static void gen_inheritance_die (tree, tree, dw_die_ref);
3182 static void gen_member_die (tree, dw_die_ref);
3183 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3184 enum debug_info_usage);
3185 static void gen_subroutine_type_die (tree, dw_die_ref);
3186 static void gen_typedef_die (tree, dw_die_ref);
3187 static void gen_type_die (tree, dw_die_ref);
3188 static void gen_block_die (tree, dw_die_ref, int);
3189 static void decls_for_scope (tree, dw_die_ref, int);
3190 static inline int is_redundant_typedef (const_tree);
3191 static bool is_naming_typedef_decl (const_tree);
3192 static inline dw_die_ref get_context_die (tree);
3193 static void gen_namespace_die (tree, dw_die_ref);
3194 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3195 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
3196 static dw_die_ref force_decl_die (tree);
3197 static dw_die_ref force_type_die (tree);
3198 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3199 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3200 static struct dwarf_file_data * lookup_filename (const char *);
3201 static void retry_incomplete_types (void);
3202 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3203 static void gen_generic_params_dies (tree);
3204 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3205 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3206 static void splice_child_die (dw_die_ref, dw_die_ref);
3207 static int file_info_cmp (const void *, const void *);
3208 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3209 const char *, const char *);
3210 static void output_loc_list (dw_loc_list_ref);
3211 static char *gen_internal_sym (const char *);
3212 static bool want_pubnames (void);
3214 static void prune_unmark_dies (dw_die_ref);
3215 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3216 static void prune_unused_types_mark (dw_die_ref, int);
3217 static void prune_unused_types_walk (dw_die_ref);
3218 static void prune_unused_types_walk_attribs (dw_die_ref);
3219 static void prune_unused_types_prune (dw_die_ref);
3220 static void prune_unused_types (void);
3221 static int maybe_emit_file (struct dwarf_file_data *fd);
3222 static inline const char *AT_vms_delta1 (dw_attr_ref);
3223 static inline const char *AT_vms_delta2 (dw_attr_ref);
3224 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3225 const char *, const char *);
3226 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3227 static void gen_remaining_tmpl_value_param_die_attribute (void);
3228 static bool generic_type_p (tree);
3229 static void schedule_generic_params_dies_gen (tree t);
3230 static void gen_scheduled_generic_parms_dies (void);
3232 static const char *comp_dir_string (void);
3234 static hashval_t hash_loc_operands (dw_loc_descr_ref, hashval_t);
3236 /* enum for tracking thread-local variables whose address is really an offset
3237 relative to the TLS pointer, which will need link-time relocation, but will
3238 not need relocation by the DWARF consumer. */
3240 enum dtprel_bool
3242 dtprel_false = 0,
3243 dtprel_true = 1
3246 /* Return the operator to use for an address of a variable. For dtprel_true, we
3247 use DW_OP_const*. For regular variables, which need both link-time
3248 relocation and consumer-level relocation (e.g., to account for shared objects
3249 loaded at a random address), we use DW_OP_addr*. */
3251 static inline enum dwarf_location_atom
3252 dw_addr_op (enum dtprel_bool dtprel)
3254 if (dtprel == dtprel_true)
3255 return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3256 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3257 else
3258 return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3261 /* Return a pointer to a newly allocated address location description. If
3262 dwarf_split_debug_info is true, then record the address with the appropriate
3263 relocation. */
3264 static inline dw_loc_descr_ref
3265 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3267 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3269 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3270 ref->dw_loc_oprnd1.v.val_addr = addr;
3271 ref->dtprel = dtprel;
3272 if (dwarf_split_debug_info)
3273 ref->dw_loc_oprnd1.val_entry
3274 = add_addr_table_entry (addr,
3275 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3276 else
3277 ref->dw_loc_oprnd1.val_entry = NULL;
3279 return ref;
3282 /* Section names used to hold DWARF debugging information. */
3284 #ifndef DEBUG_INFO_SECTION
3285 #define DEBUG_INFO_SECTION ".debug_info"
3286 #endif
3287 #ifndef DEBUG_DWO_INFO_SECTION
3288 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3289 #endif
3290 #ifndef DEBUG_ABBREV_SECTION
3291 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3292 #endif
3293 #ifndef DEBUG_DWO_ABBREV_SECTION
3294 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3295 #endif
3296 #ifndef DEBUG_ARANGES_SECTION
3297 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3298 #endif
3299 #ifndef DEBUG_ADDR_SECTION
3300 #define DEBUG_ADDR_SECTION ".debug_addr"
3301 #endif
3302 #ifndef DEBUG_NORM_MACINFO_SECTION
3303 #define DEBUG_NORM_MACINFO_SECTION ".debug_macinfo"
3304 #endif
3305 #ifndef DEBUG_DWO_MACINFO_SECTION
3306 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
3307 #endif
3308 #ifndef DEBUG_MACINFO_SECTION
3309 #define DEBUG_MACINFO_SECTION \
3310 (!dwarf_split_debug_info \
3311 ? (DEBUG_NORM_MACINFO_SECTION) : (DEBUG_DWO_MACINFO_SECTION))
3312 #endif
3313 #ifndef DEBUG_NORM_MACRO_SECTION
3314 #define DEBUG_NORM_MACRO_SECTION ".debug_macro"
3315 #endif
3316 #ifndef DEBUG_DWO_MACRO_SECTION
3317 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
3318 #endif
3319 #ifndef DEBUG_MACRO_SECTION
3320 #define DEBUG_MACRO_SECTION \
3321 (!dwarf_split_debug_info \
3322 ? (DEBUG_NORM_MACRO_SECTION) : (DEBUG_DWO_MACRO_SECTION))
3323 #endif
3324 #ifndef DEBUG_LINE_SECTION
3325 #define DEBUG_LINE_SECTION ".debug_line"
3326 #endif
3327 #ifndef DEBUG_DWO_LINE_SECTION
3328 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3329 #endif
3330 #ifndef DEBUG_LOC_SECTION
3331 #define DEBUG_LOC_SECTION ".debug_loc"
3332 #endif
3333 #ifndef DEBUG_DWO_LOC_SECTION
3334 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
3335 #endif
3336 #ifndef DEBUG_PUBNAMES_SECTION
3337 #define DEBUG_PUBNAMES_SECTION \
3338 ((debug_generate_pub_sections == 2) \
3339 ? ".debug_gnu_pubnames" : ".debug_pubnames")
3340 #endif
3341 #ifndef DEBUG_PUBTYPES_SECTION
3342 #define DEBUG_PUBTYPES_SECTION \
3343 ((debug_generate_pub_sections == 2) \
3344 ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
3345 #endif
3346 #define DEBUG_NORM_STR_OFFSETS_SECTION ".debug_str_offsets"
3347 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3348 #ifndef DEBUG_STR_OFFSETS_SECTION
3349 #define DEBUG_STR_OFFSETS_SECTION \
3350 (!dwarf_split_debug_info \
3351 ? (DEBUG_NORM_STR_OFFSETS_SECTION) : (DEBUG_DWO_STR_OFFSETS_SECTION))
3352 #endif
3353 #ifndef DEBUG_STR_DWO_SECTION
3354 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
3355 #endif
3356 #ifndef DEBUG_STR_SECTION
3357 #define DEBUG_STR_SECTION ".debug_str"
3358 #endif
3359 #ifndef DEBUG_RANGES_SECTION
3360 #define DEBUG_RANGES_SECTION ".debug_ranges"
3361 #endif
3363 /* Standard ELF section names for compiled code and data. */
3364 #ifndef TEXT_SECTION_NAME
3365 #define TEXT_SECTION_NAME ".text"
3366 #endif
3368 /* Section flags for .debug_macinfo/.debug_macro section. */
3369 #define DEBUG_MACRO_SECTION_FLAGS \
3370 (dwarf_split_debug_info ? SECTION_DEBUG | SECTION_EXCLUDE : SECTION_DEBUG)
3372 /* Section flags for .debug_str section. */
3373 #define DEBUG_STR_SECTION_FLAGS \
3374 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
3375 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3376 : SECTION_DEBUG)
3378 /* Section flags for .debug_str.dwo section. */
3379 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
3381 /* Labels we insert at beginning sections we can reference instead of
3382 the section names themselves. */
3384 #ifndef TEXT_SECTION_LABEL
3385 #define TEXT_SECTION_LABEL "Ltext"
3386 #endif
3387 #ifndef COLD_TEXT_SECTION_LABEL
3388 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
3389 #endif
3390 #ifndef DEBUG_LINE_SECTION_LABEL
3391 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3392 #endif
3393 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3394 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
3395 #endif
3396 #ifndef DEBUG_INFO_SECTION_LABEL
3397 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3398 #endif
3399 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3400 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
3401 #endif
3402 #ifndef DEBUG_ABBREV_SECTION_LABEL
3403 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3404 #endif
3405 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3406 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3407 #endif
3408 #ifndef DEBUG_ADDR_SECTION_LABEL
3409 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
3410 #endif
3411 #ifndef DEBUG_LOC_SECTION_LABEL
3412 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3413 #endif
3414 #ifndef DEBUG_RANGES_SECTION_LABEL
3415 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3416 #endif
3417 #ifndef DEBUG_MACINFO_SECTION_LABEL
3418 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3419 #endif
3420 #ifndef DEBUG_MACRO_SECTION_LABEL
3421 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
3422 #endif
3423 #define SKELETON_COMP_DIE_ABBREV 1
3424 #define SKELETON_TYPE_DIE_ABBREV 2
3426 /* Definitions of defaults for formats and names of various special
3427 (artificial) labels which may be generated within this file (when the -g
3428 options is used and DWARF2_DEBUGGING_INFO is in effect.
3429 If necessary, these may be overridden from within the tm.h file, but
3430 typically, overriding these defaults is unnecessary. */
3432 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3433 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3434 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3435 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3436 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3437 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3438 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3439 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3440 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3441 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3442 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3443 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3444 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3445 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3447 #ifndef TEXT_END_LABEL
3448 #define TEXT_END_LABEL "Letext"
3449 #endif
3450 #ifndef COLD_END_LABEL
3451 #define COLD_END_LABEL "Letext_cold"
3452 #endif
3453 #ifndef BLOCK_BEGIN_LABEL
3454 #define BLOCK_BEGIN_LABEL "LBB"
3455 #endif
3456 #ifndef BLOCK_END_LABEL
3457 #define BLOCK_END_LABEL "LBE"
3458 #endif
3459 #ifndef LINE_CODE_LABEL
3460 #define LINE_CODE_LABEL "LM"
3461 #endif
3464 /* Return the root of the DIE's built for the current compilation unit. */
3465 static dw_die_ref
3466 comp_unit_die (void)
3468 if (!single_comp_unit_die)
3469 single_comp_unit_die = gen_compile_unit_die (NULL);
3470 return single_comp_unit_die;
3473 /* We allow a language front-end to designate a function that is to be
3474 called to "demangle" any name before it is put into a DIE. */
3476 static const char *(*demangle_name_func) (const char *);
3478 void
3479 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3481 demangle_name_func = func;
3484 /* Test if rtl node points to a pseudo register. */
3486 static inline int
3487 is_pseudo_reg (const_rtx rtl)
3489 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3490 || (GET_CODE (rtl) == SUBREG
3491 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3494 /* Return a reference to a type, with its const and volatile qualifiers
3495 removed. */
3497 static inline tree
3498 type_main_variant (tree type)
3500 type = TYPE_MAIN_VARIANT (type);
3502 /* ??? There really should be only one main variant among any group of
3503 variants of a given type (and all of the MAIN_VARIANT values for all
3504 members of the group should point to that one type) but sometimes the C
3505 front-end messes this up for array types, so we work around that bug
3506 here. */
3507 if (TREE_CODE (type) == ARRAY_TYPE)
3508 while (type != TYPE_MAIN_VARIANT (type))
3509 type = TYPE_MAIN_VARIANT (type);
3511 return type;
3514 /* Return nonzero if the given type node represents a tagged type. */
3516 static inline int
3517 is_tagged_type (const_tree type)
3519 enum tree_code code = TREE_CODE (type);
3521 return (code == RECORD_TYPE || code == UNION_TYPE
3522 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3525 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
3527 static void
3528 get_ref_die_offset_label (char *label, dw_die_ref ref)
3530 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3533 /* Return die_offset of a DIE reference to a base type. */
3535 static unsigned long int
3536 get_base_type_offset (dw_die_ref ref)
3538 if (ref->die_offset)
3539 return ref->die_offset;
3540 if (comp_unit_die ()->die_abbrev)
3542 calc_base_type_die_sizes ();
3543 gcc_assert (ref->die_offset);
3545 return ref->die_offset;
3548 /* Return die_offset of a DIE reference other than base type. */
3550 static unsigned long int
3551 get_ref_die_offset (dw_die_ref ref)
3553 gcc_assert (ref->die_offset);
3554 return ref->die_offset;
3557 /* Convert a DIE tag into its string name. */
3559 static const char *
3560 dwarf_tag_name (unsigned int tag)
3562 const char *name = get_DW_TAG_name (tag);
3564 if (name != NULL)
3565 return name;
3567 return "DW_TAG_<unknown>";
3570 /* Convert a DWARF attribute code into its string name. */
3572 static const char *
3573 dwarf_attr_name (unsigned int attr)
3575 const char *name;
3577 switch (attr)
3579 #if VMS_DEBUGGING_INFO
3580 case DW_AT_HP_prologue:
3581 return "DW_AT_HP_prologue";
3582 #else
3583 case DW_AT_MIPS_loop_unroll_factor:
3584 return "DW_AT_MIPS_loop_unroll_factor";
3585 #endif
3587 #if VMS_DEBUGGING_INFO
3588 case DW_AT_HP_epilogue:
3589 return "DW_AT_HP_epilogue";
3590 #else
3591 case DW_AT_MIPS_stride:
3592 return "DW_AT_MIPS_stride";
3593 #endif
3596 name = get_DW_AT_name (attr);
3598 if (name != NULL)
3599 return name;
3601 return "DW_AT_<unknown>";
3604 /* Convert a DWARF value form code into its string name. */
3606 static const char *
3607 dwarf_form_name (unsigned int form)
3609 const char *name = get_DW_FORM_name (form);
3611 if (name != NULL)
3612 return name;
3614 return "DW_FORM_<unknown>";
3617 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
3618 instance of an inlined instance of a decl which is local to an inline
3619 function, so we have to trace all of the way back through the origin chain
3620 to find out what sort of node actually served as the original seed for the
3621 given block. */
3623 static tree
3624 decl_ultimate_origin (const_tree decl)
3626 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
3627 return NULL_TREE;
3629 /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
3630 nodes in the function to point to themselves; ignore that if
3631 we're trying to output the abstract instance of this function. */
3632 if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
3633 return NULL_TREE;
3635 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
3636 most distant ancestor, this should never happen. */
3637 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
3639 return DECL_ABSTRACT_ORIGIN (decl);
3642 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
3643 of a virtual function may refer to a base class, so we check the 'this'
3644 parameter. */
3646 static tree
3647 decl_class_context (tree decl)
3649 tree context = NULL_TREE;
3651 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
3652 context = DECL_CONTEXT (decl);
3653 else
3654 context = TYPE_MAIN_VARIANT
3655 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
3657 if (context && !TYPE_P (context))
3658 context = NULL_TREE;
3660 return context;
3663 /* Add an attribute/value pair to a DIE. */
3665 static inline void
3666 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
3668 /* Maybe this should be an assert? */
3669 if (die == NULL)
3670 return;
3672 vec_safe_reserve (die->die_attr, 1);
3673 vec_safe_push (die->die_attr, *attr);
3676 static inline enum dw_val_class
3677 AT_class (dw_attr_ref a)
3679 return a->dw_attr_val.val_class;
3682 /* Return the index for any attribute that will be referenced with a
3683 DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index. String indices
3684 are stored in dw_attr_val.v.val_str for reference counting
3685 pruning. */
3687 static inline unsigned int
3688 AT_index (dw_attr_ref a)
3690 if (AT_class (a) == dw_val_class_str)
3691 return a->dw_attr_val.v.val_str->index;
3692 else if (a->dw_attr_val.val_entry != NULL)
3693 return a->dw_attr_val.val_entry->index;
3694 return NOT_INDEXED;
3697 /* Add a flag value attribute to a DIE. */
3699 static inline void
3700 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
3702 dw_attr_node attr;
3704 attr.dw_attr = attr_kind;
3705 attr.dw_attr_val.val_class = dw_val_class_flag;
3706 attr.dw_attr_val.val_entry = NULL;
3707 attr.dw_attr_val.v.val_flag = flag;
3708 add_dwarf_attr (die, &attr);
3711 static inline unsigned
3712 AT_flag (dw_attr_ref a)
3714 gcc_assert (a && AT_class (a) == dw_val_class_flag);
3715 return a->dw_attr_val.v.val_flag;
3718 /* Add a signed integer attribute value to a DIE. */
3720 static inline void
3721 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
3723 dw_attr_node attr;
3725 attr.dw_attr = attr_kind;
3726 attr.dw_attr_val.val_class = dw_val_class_const;
3727 attr.dw_attr_val.val_entry = NULL;
3728 attr.dw_attr_val.v.val_int = int_val;
3729 add_dwarf_attr (die, &attr);
3732 static inline HOST_WIDE_INT
3733 AT_int (dw_attr_ref a)
3735 gcc_assert (a && AT_class (a) == dw_val_class_const);
3736 return a->dw_attr_val.v.val_int;
3739 /* Add an unsigned integer attribute value to a DIE. */
3741 static inline void
3742 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
3743 unsigned HOST_WIDE_INT unsigned_val)
3745 dw_attr_node attr;
3747 attr.dw_attr = attr_kind;
3748 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
3749 attr.dw_attr_val.val_entry = NULL;
3750 attr.dw_attr_val.v.val_unsigned = unsigned_val;
3751 add_dwarf_attr (die, &attr);
3754 static inline unsigned HOST_WIDE_INT
3755 AT_unsigned (dw_attr_ref a)
3757 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
3758 return a->dw_attr_val.v.val_unsigned;
3761 /* Add an unsigned double integer attribute value to a DIE. */
3763 static inline void
3764 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
3765 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
3767 dw_attr_node attr;
3769 attr.dw_attr = attr_kind;
3770 attr.dw_attr_val.val_class = dw_val_class_const_double;
3771 attr.dw_attr_val.val_entry = NULL;
3772 attr.dw_attr_val.v.val_double.high = high;
3773 attr.dw_attr_val.v.val_double.low = low;
3774 add_dwarf_attr (die, &attr);
3777 /* Add a floating point attribute value to a DIE and return it. */
3779 static inline void
3780 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
3781 unsigned int length, unsigned int elt_size, unsigned char *array)
3783 dw_attr_node attr;
3785 attr.dw_attr = attr_kind;
3786 attr.dw_attr_val.val_class = dw_val_class_vec;
3787 attr.dw_attr_val.val_entry = NULL;
3788 attr.dw_attr_val.v.val_vec.length = length;
3789 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
3790 attr.dw_attr_val.v.val_vec.array = array;
3791 add_dwarf_attr (die, &attr);
3794 /* Add an 8-byte data attribute value to a DIE. */
3796 static inline void
3797 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
3798 unsigned char data8[8])
3800 dw_attr_node attr;
3802 attr.dw_attr = attr_kind;
3803 attr.dw_attr_val.val_class = dw_val_class_data8;
3804 attr.dw_attr_val.val_entry = NULL;
3805 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
3806 add_dwarf_attr (die, &attr);
3809 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
3810 dwarf_split_debug_info, address attributes in dies destined for the
3811 final executable have force_direct set to avoid using indexed
3812 references. */
3814 static inline void
3815 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
3816 bool force_direct)
3818 dw_attr_node attr;
3819 char * lbl_id;
3821 lbl_id = xstrdup (lbl_low);
3822 attr.dw_attr = DW_AT_low_pc;
3823 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3824 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3825 if (dwarf_split_debug_info && !force_direct)
3826 attr.dw_attr_val.val_entry
3827 = add_addr_table_entry (lbl_id, ate_kind_label);
3828 else
3829 attr.dw_attr_val.val_entry = NULL;
3830 add_dwarf_attr (die, &attr);
3832 attr.dw_attr = DW_AT_high_pc;
3833 if (dwarf_version < 4)
3834 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3835 else
3836 attr.dw_attr_val.val_class = dw_val_class_high_pc;
3837 lbl_id = xstrdup (lbl_high);
3838 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3839 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
3840 && dwarf_split_debug_info && !force_direct)
3841 attr.dw_attr_val.val_entry
3842 = add_addr_table_entry (lbl_id, ate_kind_label);
3843 else
3844 attr.dw_attr_val.val_entry = NULL;
3845 add_dwarf_attr (die, &attr);
3848 /* Hash and equality functions for debug_str_hash. */
3850 static hashval_t
3851 debug_str_do_hash (const void *x)
3853 return htab_hash_string (((const struct indirect_string_node *)x)->str);
3856 static int
3857 debug_str_eq (const void *x1, const void *x2)
3859 return strcmp ((((const struct indirect_string_node *)x1)->str),
3860 (const char *)x2) == 0;
3863 /* Add STR to the given string hash table. */
3865 static struct indirect_string_node *
3866 find_AT_string_in_table (const char *str, htab_t table)
3868 struct indirect_string_node *node;
3869 void **slot;
3871 slot = htab_find_slot_with_hash (table, str,
3872 htab_hash_string (str), INSERT);
3873 if (*slot == NULL)
3875 node = ggc_alloc_cleared_indirect_string_node ();
3876 node->str = ggc_strdup (str);
3877 *slot = node;
3879 else
3880 node = (struct indirect_string_node *) *slot;
3882 node->refcount++;
3883 return node;
3886 /* Add STR to the indirect string hash table. */
3888 static struct indirect_string_node *
3889 find_AT_string (const char *str)
3891 if (! debug_str_hash)
3892 debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
3893 debug_str_eq, NULL);
3895 return find_AT_string_in_table (str, debug_str_hash);
3898 /* Add a string attribute value to a DIE. */
3900 static inline void
3901 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
3903 dw_attr_node attr;
3904 struct indirect_string_node *node;
3906 node = find_AT_string (str);
3908 attr.dw_attr = attr_kind;
3909 attr.dw_attr_val.val_class = dw_val_class_str;
3910 attr.dw_attr_val.val_entry = NULL;
3911 attr.dw_attr_val.v.val_str = node;
3912 add_dwarf_attr (die, &attr);
3915 static inline const char *
3916 AT_string (dw_attr_ref a)
3918 gcc_assert (a && AT_class (a) == dw_val_class_str);
3919 return a->dw_attr_val.v.val_str->str;
3922 /* Call this function directly to bypass AT_string_form's logic to put
3923 the string inline in the die. */
3925 static void
3926 set_indirect_string (struct indirect_string_node *node)
3928 char label[32];
3929 /* Already indirect is a no op. */
3930 if (node->form == DW_FORM_strp || node->form == DW_FORM_GNU_str_index)
3932 gcc_assert (node->label);
3933 return;
3935 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
3936 ++dw2_string_counter;
3937 node->label = xstrdup (label);
3939 if (!dwarf_split_debug_info)
3941 node->form = DW_FORM_strp;
3942 node->index = NOT_INDEXED;
3944 else
3946 node->form = DW_FORM_GNU_str_index;
3947 node->index = NO_INDEX_ASSIGNED;
3951 /* Find out whether a string should be output inline in DIE
3952 or out-of-line in .debug_str section. */
3954 static enum dwarf_form
3955 find_string_form (struct indirect_string_node *node)
3957 unsigned int len;
3959 if (node->form)
3960 return node->form;
3962 len = strlen (node->str) + 1;
3964 /* If the string is shorter or equal to the size of the reference, it is
3965 always better to put it inline. */
3966 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
3967 return node->form = DW_FORM_string;
3969 /* If we cannot expect the linker to merge strings in .debug_str
3970 section, only put it into .debug_str if it is worth even in this
3971 single module. */
3972 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
3973 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
3974 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
3975 return node->form = DW_FORM_string;
3977 set_indirect_string (node);
3979 return node->form;
3982 /* Find out whether the string referenced from the attribute should be
3983 output inline in DIE or out-of-line in .debug_str section. */
3985 static enum dwarf_form
3986 AT_string_form (dw_attr_ref a)
3988 gcc_assert (a && AT_class (a) == dw_val_class_str);
3989 return find_string_form (a->dw_attr_val.v.val_str);
3992 /* Add a DIE reference attribute value to a DIE. */
3994 static inline void
3995 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
3997 dw_attr_node attr;
3999 #ifdef ENABLE_CHECKING
4000 gcc_assert (targ_die != NULL);
4001 #else
4002 /* With LTO we can end up trying to reference something we didn't create
4003 a DIE for. Avoid crashing later on a NULL referenced DIE. */
4004 if (targ_die == NULL)
4005 return;
4006 #endif
4008 attr.dw_attr = attr_kind;
4009 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4010 attr.dw_attr_val.val_entry = NULL;
4011 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4012 attr.dw_attr_val.v.val_die_ref.external = 0;
4013 add_dwarf_attr (die, &attr);
4016 /* Change DIE reference REF to point to NEW_DIE instead. */
4018 static inline void
4019 change_AT_die_ref (dw_attr_ref ref, dw_die_ref new_die)
4021 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4022 ref->dw_attr_val.v.val_die_ref.die = new_die;
4023 ref->dw_attr_val.v.val_die_ref.external = 0;
4026 /* Add an AT_specification attribute to a DIE, and also make the back
4027 pointer from the specification to the definition. */
4029 static inline void
4030 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4032 add_AT_die_ref (die, DW_AT_specification, targ_die);
4033 gcc_assert (!targ_die->die_definition);
4034 targ_die->die_definition = die;
4037 static inline dw_die_ref
4038 AT_ref (dw_attr_ref a)
4040 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4041 return a->dw_attr_val.v.val_die_ref.die;
4044 static inline int
4045 AT_ref_external (dw_attr_ref a)
4047 if (a && AT_class (a) == dw_val_class_die_ref)
4048 return a->dw_attr_val.v.val_die_ref.external;
4050 return 0;
4053 static inline void
4054 set_AT_ref_external (dw_attr_ref a, int i)
4056 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4057 a->dw_attr_val.v.val_die_ref.external = i;
4060 /* Add an FDE reference attribute value to a DIE. */
4062 static inline void
4063 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4065 dw_attr_node attr;
4067 attr.dw_attr = attr_kind;
4068 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4069 attr.dw_attr_val.val_entry = NULL;
4070 attr.dw_attr_val.v.val_fde_index = targ_fde;
4071 add_dwarf_attr (die, &attr);
4074 /* Add a location description attribute value to a DIE. */
4076 static inline void
4077 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4079 dw_attr_node attr;
4081 attr.dw_attr = attr_kind;
4082 attr.dw_attr_val.val_class = dw_val_class_loc;
4083 attr.dw_attr_val.val_entry = NULL;
4084 attr.dw_attr_val.v.val_loc = loc;
4085 add_dwarf_attr (die, &attr);
4088 static inline dw_loc_descr_ref
4089 AT_loc (dw_attr_ref a)
4091 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4092 return a->dw_attr_val.v.val_loc;
4095 static inline void
4096 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4098 dw_attr_node attr;
4100 attr.dw_attr = attr_kind;
4101 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4102 attr.dw_attr_val.val_entry = NULL;
4103 attr.dw_attr_val.v.val_loc_list = loc_list;
4104 add_dwarf_attr (die, &attr);
4105 have_location_lists = true;
4108 static inline dw_loc_list_ref
4109 AT_loc_list (dw_attr_ref a)
4111 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4112 return a->dw_attr_val.v.val_loc_list;
4115 static inline dw_loc_list_ref *
4116 AT_loc_list_ptr (dw_attr_ref a)
4118 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4119 return &a->dw_attr_val.v.val_loc_list;
4122 /* Table of entries into the .debug_addr section. */
4124 static GTY ((param_is (addr_table_entry))) htab_t addr_index_table;
4126 /* Hash an address_table_entry. */
4128 static hashval_t
4129 addr_table_entry_do_hash (const void *x)
4131 const addr_table_entry *a = (const addr_table_entry *) x;
4132 switch (a->kind)
4134 case ate_kind_rtx:
4135 return iterative_hash_rtx (a->addr.rtl, 0);
4136 case ate_kind_rtx_dtprel:
4137 return iterative_hash_rtx (a->addr.rtl, 1);
4138 case ate_kind_label:
4139 return htab_hash_string (a->addr.label);
4140 default:
4141 gcc_unreachable ();
4145 /* Determine equality for two address_table_entries. */
4147 static int
4148 addr_table_entry_eq (const void *x1, const void *x2)
4150 const addr_table_entry *a1 = (const addr_table_entry *) x1;
4151 const addr_table_entry *a2 = (const addr_table_entry *) x2;
4153 if (a1->kind != a2->kind)
4154 return 0;
4155 switch (a1->kind)
4157 case ate_kind_rtx:
4158 case ate_kind_rtx_dtprel:
4159 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4160 case ate_kind_label:
4161 return strcmp (a1->addr.label, a2->addr.label) == 0;
4162 default:
4163 gcc_unreachable ();
4167 /* Initialize an addr_table_entry. */
4169 void
4170 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4172 e->kind = kind;
4173 switch (kind)
4175 case ate_kind_rtx:
4176 case ate_kind_rtx_dtprel:
4177 e->addr.rtl = (rtx) addr;
4178 break;
4179 case ate_kind_label:
4180 e->addr.label = (char *) addr;
4181 break;
4183 e->refcount = 0;
4184 e->index = NO_INDEX_ASSIGNED;
4187 /* Add attr to the address table entry to the table. Defer setting an
4188 index until output time. */
4190 static addr_table_entry *
4191 add_addr_table_entry (void *addr, enum ate_kind kind)
4193 addr_table_entry *node;
4194 addr_table_entry finder;
4195 void **slot;
4197 gcc_assert (dwarf_split_debug_info);
4198 if (! addr_index_table)
4199 addr_index_table = htab_create_ggc (10, addr_table_entry_do_hash,
4200 addr_table_entry_eq, NULL);
4201 init_addr_table_entry (&finder, kind, addr);
4202 slot = htab_find_slot (addr_index_table, &finder, INSERT);
4204 if (*slot == HTAB_EMPTY_ENTRY)
4206 node = ggc_alloc_cleared_addr_table_entry ();
4207 init_addr_table_entry (node, kind, addr);
4208 *slot = node;
4210 else
4211 node = (addr_table_entry *) *slot;
4213 node->refcount++;
4214 return node;
4217 /* Remove an entry from the addr table by decrementing its refcount.
4218 Strictly, decrementing the refcount would be enough, but the
4219 assertion that the entry is actually in the table has found
4220 bugs. */
4222 static void
4223 remove_addr_table_entry (addr_table_entry *entry)
4225 gcc_assert (dwarf_split_debug_info && addr_index_table);
4226 /* After an index is assigned, the table is frozen. */
4227 gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
4228 entry->refcount--;
4231 /* Given a location list, remove all addresses it refers to from the
4232 address_table. */
4234 static void
4235 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4237 for (; descr; descr = descr->dw_loc_next)
4238 if (descr->dw_loc_oprnd1.val_entry != NULL)
4240 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4241 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4245 /* A helper function for dwarf2out_finish called through
4246 htab_traverse. Assign an addr_table_entry its index. All entries
4247 must be collected into the table when this function is called,
4248 because the indexing code relies on htab_traverse to traverse nodes
4249 in the same order for each run. */
4251 static int
4252 index_addr_table_entry (void **h, void *v)
4254 addr_table_entry *node = (addr_table_entry *) *h;
4255 unsigned int *index = (unsigned int *) v;
4257 /* Don't index unreferenced nodes. */
4258 if (node->refcount == 0)
4259 return 1;
4261 gcc_assert (node->index == NO_INDEX_ASSIGNED);
4262 node->index = *index;
4263 *index += 1;
4265 return 1;
4268 /* Add an address constant attribute value to a DIE. When using
4269 dwarf_split_debug_info, address attributes in dies destined for the
4270 final executable should be direct references--setting the parameter
4271 force_direct ensures this behavior. */
4273 static inline void
4274 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4275 bool force_direct)
4277 dw_attr_node attr;
4279 attr.dw_attr = attr_kind;
4280 attr.dw_attr_val.val_class = dw_val_class_addr;
4281 attr.dw_attr_val.v.val_addr = addr;
4282 if (dwarf_split_debug_info && !force_direct)
4283 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4284 else
4285 attr.dw_attr_val.val_entry = NULL;
4286 add_dwarf_attr (die, &attr);
4289 /* Get the RTX from to an address DIE attribute. */
4291 static inline rtx
4292 AT_addr (dw_attr_ref a)
4294 gcc_assert (a && AT_class (a) == dw_val_class_addr);
4295 return a->dw_attr_val.v.val_addr;
4298 /* Add a file attribute value to a DIE. */
4300 static inline void
4301 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4302 struct dwarf_file_data *fd)
4304 dw_attr_node attr;
4306 attr.dw_attr = attr_kind;
4307 attr.dw_attr_val.val_class = dw_val_class_file;
4308 attr.dw_attr_val.val_entry = NULL;
4309 attr.dw_attr_val.v.val_file = fd;
4310 add_dwarf_attr (die, &attr);
4313 /* Get the dwarf_file_data from a file DIE attribute. */
4315 static inline struct dwarf_file_data *
4316 AT_file (dw_attr_ref a)
4318 gcc_assert (a && AT_class (a) == dw_val_class_file);
4319 return a->dw_attr_val.v.val_file;
4322 /* Add a vms delta attribute value to a DIE. */
4324 static inline void
4325 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4326 const char *lbl1, const char *lbl2)
4328 dw_attr_node attr;
4330 attr.dw_attr = attr_kind;
4331 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4332 attr.dw_attr_val.val_entry = NULL;
4333 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4334 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4335 add_dwarf_attr (die, &attr);
4338 /* Add a label identifier attribute value to a DIE. */
4340 static inline void
4341 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4342 const char *lbl_id)
4344 dw_attr_node attr;
4346 attr.dw_attr = attr_kind;
4347 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4348 attr.dw_attr_val.val_entry = NULL;
4349 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4350 if (dwarf_split_debug_info)
4351 attr.dw_attr_val.val_entry
4352 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4353 ate_kind_label);
4354 add_dwarf_attr (die, &attr);
4357 /* Add a section offset attribute value to a DIE, an offset into the
4358 debug_line section. */
4360 static inline void
4361 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4362 const char *label)
4364 dw_attr_node attr;
4366 attr.dw_attr = attr_kind;
4367 attr.dw_attr_val.val_class = dw_val_class_lineptr;
4368 attr.dw_attr_val.val_entry = NULL;
4369 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4370 add_dwarf_attr (die, &attr);
4373 /* Add a section offset attribute value to a DIE, an offset into the
4374 debug_macinfo section. */
4376 static inline void
4377 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4378 const char *label)
4380 dw_attr_node attr;
4382 attr.dw_attr = attr_kind;
4383 attr.dw_attr_val.val_class = dw_val_class_macptr;
4384 attr.dw_attr_val.val_entry = NULL;
4385 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4386 add_dwarf_attr (die, &attr);
4389 /* Add an offset attribute value to a DIE. */
4391 static inline void
4392 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4393 unsigned HOST_WIDE_INT offset)
4395 dw_attr_node attr;
4397 attr.dw_attr = attr_kind;
4398 attr.dw_attr_val.val_class = dw_val_class_offset;
4399 attr.dw_attr_val.val_entry = NULL;
4400 attr.dw_attr_val.v.val_offset = offset;
4401 add_dwarf_attr (die, &attr);
4404 /* Add a range_list attribute value to a DIE. When using
4405 dwarf_split_debug_info, address attributes in dies destined for the
4406 final executable should be direct references--setting the parameter
4407 force_direct ensures this behavior. */
4409 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4410 #define RELOCATED_OFFSET (NULL)
4412 static void
4413 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4414 long unsigned int offset, bool force_direct)
4416 dw_attr_node attr;
4418 attr.dw_attr = attr_kind;
4419 attr.dw_attr_val.val_class = dw_val_class_range_list;
4420 /* For the range_list attribute, use val_entry to store whether the
4421 offset should follow split-debug-info or normal semantics. This
4422 value is read in output_range_list_offset. */
4423 if (dwarf_split_debug_info && !force_direct)
4424 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4425 else
4426 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4427 attr.dw_attr_val.v.val_offset = offset;
4428 add_dwarf_attr (die, &attr);
4431 /* Return the start label of a delta attribute. */
4433 static inline const char *
4434 AT_vms_delta1 (dw_attr_ref a)
4436 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4437 return a->dw_attr_val.v.val_vms_delta.lbl1;
4440 /* Return the end label of a delta attribute. */
4442 static inline const char *
4443 AT_vms_delta2 (dw_attr_ref a)
4445 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4446 return a->dw_attr_val.v.val_vms_delta.lbl2;
4449 static inline const char *
4450 AT_lbl (dw_attr_ref a)
4452 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4453 || AT_class (a) == dw_val_class_lineptr
4454 || AT_class (a) == dw_val_class_macptr
4455 || AT_class (a) == dw_val_class_high_pc));
4456 return a->dw_attr_val.v.val_lbl_id;
4459 /* Get the attribute of type attr_kind. */
4461 static dw_attr_ref
4462 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4464 dw_attr_ref a;
4465 unsigned ix;
4466 dw_die_ref spec = NULL;
4468 if (! die)
4469 return NULL;
4471 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4472 if (a->dw_attr == attr_kind)
4473 return a;
4474 else if (a->dw_attr == DW_AT_specification
4475 || a->dw_attr == DW_AT_abstract_origin)
4476 spec = AT_ref (a);
4478 if (spec)
4479 return get_AT (spec, attr_kind);
4481 return NULL;
4484 /* Returns the parent of the declaration of DIE. */
4486 static dw_die_ref
4487 get_die_parent (dw_die_ref die)
4489 dw_die_ref t;
4491 if (!die)
4492 return NULL;
4494 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4495 || (t = get_AT_ref (die, DW_AT_specification)))
4496 die = t;
4498 return die->die_parent;
4501 /* Return the "low pc" attribute value, typically associated with a subprogram
4502 DIE. Return null if the "low pc" attribute is either not present, or if it
4503 cannot be represented as an assembler label identifier. */
4505 static inline const char *
4506 get_AT_low_pc (dw_die_ref die)
4508 dw_attr_ref a = get_AT (die, DW_AT_low_pc);
4510 return a ? AT_lbl (a) : NULL;
4513 /* Return the "high pc" attribute value, typically associated with a subprogram
4514 DIE. Return null if the "high pc" attribute is either not present, or if it
4515 cannot be represented as an assembler label identifier. */
4517 static inline const char *
4518 get_AT_hi_pc (dw_die_ref die)
4520 dw_attr_ref a = get_AT (die, DW_AT_high_pc);
4522 return a ? AT_lbl (a) : NULL;
4525 /* Return the value of the string attribute designated by ATTR_KIND, or
4526 NULL if it is not present. */
4528 static inline const char *
4529 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4531 dw_attr_ref a = get_AT (die, attr_kind);
4533 return a ? AT_string (a) : NULL;
4536 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4537 if it is not present. */
4539 static inline int
4540 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4542 dw_attr_ref a = get_AT (die, attr_kind);
4544 return a ? AT_flag (a) : 0;
4547 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4548 if it is not present. */
4550 static inline unsigned
4551 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4553 dw_attr_ref a = get_AT (die, attr_kind);
4555 return a ? AT_unsigned (a) : 0;
4558 static inline dw_die_ref
4559 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4561 dw_attr_ref a = get_AT (die, attr_kind);
4563 return a ? AT_ref (a) : NULL;
4566 static inline struct dwarf_file_data *
4567 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4569 dw_attr_ref a = get_AT (die, attr_kind);
4571 return a ? AT_file (a) : NULL;
4574 /* Return TRUE if the language is C++. */
4576 static inline bool
4577 is_cxx (void)
4579 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4581 return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
4584 /* Return TRUE if the language is Java. */
4586 static inline bool
4587 is_java (void)
4589 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4591 return lang == DW_LANG_Java;
4594 /* Return TRUE if the language is Fortran. */
4596 static inline bool
4597 is_fortran (void)
4599 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4601 return (lang == DW_LANG_Fortran77
4602 || lang == DW_LANG_Fortran90
4603 || lang == DW_LANG_Fortran95);
4606 /* Return TRUE if the language is Ada. */
4608 static inline bool
4609 is_ada (void)
4611 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4613 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4616 /* Remove the specified attribute if present. */
4618 static void
4619 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4621 dw_attr_ref a;
4622 unsigned ix;
4624 if (! die)
4625 return;
4627 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4628 if (a->dw_attr == attr_kind)
4630 if (AT_class (a) == dw_val_class_str)
4631 if (a->dw_attr_val.v.val_str->refcount)
4632 a->dw_attr_val.v.val_str->refcount--;
4634 /* vec::ordered_remove should help reduce the number of abbrevs
4635 that are needed. */
4636 die->die_attr->ordered_remove (ix);
4637 return;
4641 /* Remove CHILD from its parent. PREV must have the property that
4642 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
4644 static void
4645 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4647 gcc_assert (child->die_parent == prev->die_parent);
4648 gcc_assert (prev->die_sib == child);
4649 if (prev == child)
4651 gcc_assert (child->die_parent->die_child == child);
4652 prev = NULL;
4654 else
4655 prev->die_sib = child->die_sib;
4656 if (child->die_parent->die_child == child)
4657 child->die_parent->die_child = prev;
4660 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
4661 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
4663 static void
4664 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4666 dw_die_ref parent = old_child->die_parent;
4668 gcc_assert (parent == prev->die_parent);
4669 gcc_assert (prev->die_sib == old_child);
4671 new_child->die_parent = parent;
4672 if (prev == old_child)
4674 gcc_assert (parent->die_child == old_child);
4675 new_child->die_sib = new_child;
4677 else
4679 prev->die_sib = new_child;
4680 new_child->die_sib = old_child->die_sib;
4682 if (old_child->die_parent->die_child == old_child)
4683 old_child->die_parent->die_child = new_child;
4686 /* Move all children from OLD_PARENT to NEW_PARENT. */
4688 static void
4689 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4691 dw_die_ref c;
4692 new_parent->die_child = old_parent->die_child;
4693 old_parent->die_child = NULL;
4694 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4697 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
4698 matches TAG. */
4700 static void
4701 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4703 dw_die_ref c;
4705 c = die->die_child;
4706 if (c) do {
4707 dw_die_ref prev = c;
4708 c = c->die_sib;
4709 while (c->die_tag == tag)
4711 remove_child_with_prev (c, prev);
4712 /* Might have removed every child. */
4713 if (c == c->die_sib)
4714 return;
4715 c = c->die_sib;
4717 } while (c != die->die_child);
4720 /* Add a CHILD_DIE as the last child of DIE. */
4722 static void
4723 add_child_die (dw_die_ref die, dw_die_ref child_die)
4725 /* FIXME this should probably be an assert. */
4726 if (! die || ! child_die)
4727 return;
4728 gcc_assert (die != child_die);
4730 child_die->die_parent = die;
4731 if (die->die_child)
4733 child_die->die_sib = die->die_child->die_sib;
4734 die->die_child->die_sib = child_die;
4736 else
4737 child_die->die_sib = child_die;
4738 die->die_child = child_die;
4741 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
4742 is the specification, to the end of PARENT's list of children.
4743 This is done by removing and re-adding it. */
4745 static void
4746 splice_child_die (dw_die_ref parent, dw_die_ref child)
4748 dw_die_ref p;
4750 /* We want the declaration DIE from inside the class, not the
4751 specification DIE at toplevel. */
4752 if (child->die_parent != parent)
4754 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
4756 if (tmp)
4757 child = tmp;
4760 gcc_assert (child->die_parent == parent
4761 || (child->die_parent
4762 == get_AT_ref (parent, DW_AT_specification)));
4764 for (p = child->die_parent->die_child; ; p = p->die_sib)
4765 if (p->die_sib == child)
4767 remove_child_with_prev (child, p);
4768 break;
4771 add_child_die (parent, child);
4774 /* Return a pointer to a newly created DIE node. */
4776 static inline dw_die_ref
4777 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
4779 dw_die_ref die = ggc_alloc_cleared_die_node ();
4781 die->die_tag = tag_value;
4783 if (parent_die != NULL)
4784 add_child_die (parent_die, die);
4785 else
4787 limbo_die_node *limbo_node;
4789 limbo_node = ggc_alloc_cleared_limbo_die_node ();
4790 limbo_node->die = die;
4791 limbo_node->created_for = t;
4792 limbo_node->next = limbo_die_list;
4793 limbo_die_list = limbo_node;
4796 return die;
4799 /* Return the DIE associated with the given type specifier. */
4801 static inline dw_die_ref
4802 lookup_type_die (tree type)
4804 return TYPE_SYMTAB_DIE (type);
4807 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
4808 anonymous type named by the typedef TYPE_DIE, return the DIE of the
4809 anonymous type instead the one of the naming typedef. */
4811 static inline dw_die_ref
4812 strip_naming_typedef (tree type, dw_die_ref type_die)
4814 if (type
4815 && TREE_CODE (type) == RECORD_TYPE
4816 && type_die
4817 && type_die->die_tag == DW_TAG_typedef
4818 && is_naming_typedef_decl (TYPE_NAME (type)))
4819 type_die = get_AT_ref (type_die, DW_AT_type);
4820 return type_die;
4823 /* Like lookup_type_die, but if type is an anonymous type named by a
4824 typedef[1], return the DIE of the anonymous type instead the one of
4825 the naming typedef. This is because in gen_typedef_die, we did
4826 equate the anonymous struct named by the typedef with the DIE of
4827 the naming typedef. So by default, lookup_type_die on an anonymous
4828 struct yields the DIE of the naming typedef.
4830 [1]: Read the comment of is_naming_typedef_decl to learn about what
4831 a naming typedef is. */
4833 static inline dw_die_ref
4834 lookup_type_die_strip_naming_typedef (tree type)
4836 dw_die_ref die = lookup_type_die (type);
4837 return strip_naming_typedef (type, die);
4840 /* Equate a DIE to a given type specifier. */
4842 static inline void
4843 equate_type_number_to_die (tree type, dw_die_ref type_die)
4845 TYPE_SYMTAB_DIE (type) = type_die;
4848 /* Returns a hash value for X (which really is a die_struct). */
4850 static hashval_t
4851 decl_die_table_hash (const void *x)
4853 return (hashval_t) ((const_dw_die_ref) x)->decl_id;
4856 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
4858 static int
4859 decl_die_table_eq (const void *x, const void *y)
4861 return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
4864 /* Return the DIE associated with a given declaration. */
4866 static inline dw_die_ref
4867 lookup_decl_die (tree decl)
4869 return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
4872 /* Returns a hash value for X (which really is a var_loc_list). */
4874 static hashval_t
4875 decl_loc_table_hash (const void *x)
4877 return (hashval_t) ((const var_loc_list *) x)->decl_id;
4880 /* Return nonzero if decl_id of var_loc_list X is the same as
4881 UID of decl *Y. */
4883 static int
4884 decl_loc_table_eq (const void *x, const void *y)
4886 return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
4889 /* Return the var_loc list associated with a given declaration. */
4891 static inline var_loc_list *
4892 lookup_decl_loc (const_tree decl)
4894 if (!decl_loc_table)
4895 return NULL;
4896 return (var_loc_list *)
4897 htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
4900 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
4902 static hashval_t
4903 cached_dw_loc_list_table_hash (const void *x)
4905 return (hashval_t) ((const cached_dw_loc_list *) x)->decl_id;
4908 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
4909 UID of decl *Y. */
4911 static int
4912 cached_dw_loc_list_table_eq (const void *x, const void *y)
4914 return (((const cached_dw_loc_list *) x)->decl_id
4915 == DECL_UID ((const_tree) y));
4918 /* Equate a DIE to a particular declaration. */
4920 static void
4921 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
4923 unsigned int decl_id = DECL_UID (decl);
4924 void **slot;
4926 slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
4927 *slot = decl_die;
4928 decl_die->decl_id = decl_id;
4931 /* Return how many bits covers PIECE EXPR_LIST. */
4933 static HOST_WIDE_INT
4934 decl_piece_bitsize (rtx piece)
4936 int ret = (int) GET_MODE (piece);
4937 if (ret)
4938 return ret;
4939 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
4940 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
4941 return INTVAL (XEXP (XEXP (piece, 0), 0));
4944 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
4946 static rtx *
4947 decl_piece_varloc_ptr (rtx piece)
4949 if ((int) GET_MODE (piece))
4950 return &XEXP (piece, 0);
4951 else
4952 return &XEXP (XEXP (piece, 0), 1);
4955 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
4956 Next is the chain of following piece nodes. */
4958 static rtx
4959 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
4961 if (bitsize > 0 && bitsize <= (int) MAX_MACHINE_MODE)
4962 return alloc_EXPR_LIST (bitsize, loc_note, next);
4963 else
4964 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
4965 GEN_INT (bitsize),
4966 loc_note), next);
4969 /* Return rtx that should be stored into loc field for
4970 LOC_NOTE and BITPOS/BITSIZE. */
4972 static rtx
4973 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
4974 HOST_WIDE_INT bitsize)
4976 if (bitsize != -1)
4978 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
4979 if (bitpos != 0)
4980 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
4982 return loc_note;
4985 /* This function either modifies location piece list *DEST in
4986 place (if SRC and INNER is NULL), or copies location piece list
4987 *SRC to *DEST while modifying it. Location BITPOS is modified
4988 to contain LOC_NOTE, any pieces overlapping it are removed resp.
4989 not copied and if needed some padding around it is added.
4990 When modifying in place, DEST should point to EXPR_LIST where
4991 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
4992 to the start of the whole list and INNER points to the EXPR_LIST
4993 where earlier pieces cover PIECE_BITPOS bits. */
4995 static void
4996 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
4997 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
4998 HOST_WIDE_INT bitsize, rtx loc_note)
5000 HOST_WIDE_INT diff;
5001 bool copy = inner != NULL;
5003 if (copy)
5005 /* First copy all nodes preceding the current bitpos. */
5006 while (src != inner)
5008 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5009 decl_piece_bitsize (*src), NULL_RTX);
5010 dest = &XEXP (*dest, 1);
5011 src = &XEXP (*src, 1);
5014 /* Add padding if needed. */
5015 if (bitpos != piece_bitpos)
5017 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5018 copy ? NULL_RTX : *dest);
5019 dest = &XEXP (*dest, 1);
5021 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5023 gcc_assert (!copy);
5024 /* A piece with correct bitpos and bitsize already exist,
5025 just update the location for it and return. */
5026 *decl_piece_varloc_ptr (*dest) = loc_note;
5027 return;
5029 /* Add the piece that changed. */
5030 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5031 dest = &XEXP (*dest, 1);
5032 /* Skip over pieces that overlap it. */
5033 diff = bitpos - piece_bitpos + bitsize;
5034 if (!copy)
5035 src = dest;
5036 while (diff > 0 && *src)
5038 rtx piece = *src;
5039 diff -= decl_piece_bitsize (piece);
5040 if (copy)
5041 src = &XEXP (piece, 1);
5042 else
5044 *src = XEXP (piece, 1);
5045 free_EXPR_LIST_node (piece);
5048 /* Add padding if needed. */
5049 if (diff < 0 && *src)
5051 if (!copy)
5052 dest = src;
5053 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5054 dest = &XEXP (*dest, 1);
5056 if (!copy)
5057 return;
5058 /* Finally copy all nodes following it. */
5059 while (*src)
5061 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5062 decl_piece_bitsize (*src), NULL_RTX);
5063 dest = &XEXP (*dest, 1);
5064 src = &XEXP (*src, 1);
5068 /* Add a variable location node to the linked list for DECL. */
5070 static struct var_loc_node *
5071 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5073 unsigned int decl_id;
5074 var_loc_list *temp;
5075 void **slot;
5076 struct var_loc_node *loc = NULL;
5077 HOST_WIDE_INT bitsize = -1, bitpos = -1;
5079 if (TREE_CODE (decl) == VAR_DECL
5080 && DECL_HAS_DEBUG_EXPR_P (decl))
5082 tree realdecl = DECL_DEBUG_EXPR (decl);
5083 if (handled_component_p (realdecl)
5084 || (TREE_CODE (realdecl) == MEM_REF
5085 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5087 HOST_WIDE_INT maxsize;
5088 tree innerdecl;
5089 innerdecl
5090 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
5091 if (!DECL_P (innerdecl)
5092 || DECL_IGNORED_P (innerdecl)
5093 || TREE_STATIC (innerdecl)
5094 || bitsize <= 0
5095 || bitpos + bitsize > 256
5096 || bitsize != maxsize)
5097 return NULL;
5098 decl = innerdecl;
5102 decl_id = DECL_UID (decl);
5103 slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
5104 if (*slot == NULL)
5106 temp = ggc_alloc_cleared_var_loc_list ();
5107 temp->decl_id = decl_id;
5108 *slot = temp;
5110 else
5111 temp = (var_loc_list *) *slot;
5113 /* For PARM_DECLs try to keep around the original incoming value,
5114 even if that means we'll emit a zero-range .debug_loc entry. */
5115 if (temp->last
5116 && temp->first == temp->last
5117 && TREE_CODE (decl) == PARM_DECL
5118 && NOTE_P (temp->first->loc)
5119 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5120 && DECL_INCOMING_RTL (decl)
5121 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5122 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5123 == GET_CODE (DECL_INCOMING_RTL (decl))
5124 && prev_real_insn (temp->first->loc) == NULL_RTX
5125 && (bitsize != -1
5126 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5127 NOTE_VAR_LOCATION_LOC (loc_note))
5128 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5129 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5131 loc = ggc_alloc_cleared_var_loc_node ();
5132 temp->first->next = loc;
5133 temp->last = loc;
5134 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5136 else if (temp->last)
5138 struct var_loc_node *last = temp->last, *unused = NULL;
5139 rtx *piece_loc = NULL, last_loc_note;
5140 HOST_WIDE_INT piece_bitpos = 0;
5141 if (last->next)
5143 last = last->next;
5144 gcc_assert (last->next == NULL);
5146 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5148 piece_loc = &last->loc;
5151 HOST_WIDE_INT cur_bitsize = decl_piece_bitsize (*piece_loc);
5152 if (piece_bitpos + cur_bitsize > bitpos)
5153 break;
5154 piece_bitpos += cur_bitsize;
5155 piece_loc = &XEXP (*piece_loc, 1);
5157 while (*piece_loc);
5159 /* TEMP->LAST here is either pointer to the last but one or
5160 last element in the chained list, LAST is pointer to the
5161 last element. */
5162 if (label && strcmp (last->label, label) == 0)
5164 /* For SRA optimized variables if there weren't any real
5165 insns since last note, just modify the last node. */
5166 if (piece_loc != NULL)
5168 adjust_piece_list (piece_loc, NULL, NULL,
5169 bitpos, piece_bitpos, bitsize, loc_note);
5170 return NULL;
5172 /* If the last note doesn't cover any instructions, remove it. */
5173 if (temp->last != last)
5175 temp->last->next = NULL;
5176 unused = last;
5177 last = temp->last;
5178 gcc_assert (strcmp (last->label, label) != 0);
5180 else
5182 gcc_assert (temp->first == temp->last
5183 || (temp->first->next == temp->last
5184 && TREE_CODE (decl) == PARM_DECL));
5185 memset (temp->last, '\0', sizeof (*temp->last));
5186 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5187 return temp->last;
5190 if (bitsize == -1 && NOTE_P (last->loc))
5191 last_loc_note = last->loc;
5192 else if (piece_loc != NULL
5193 && *piece_loc != NULL_RTX
5194 && piece_bitpos == bitpos
5195 && decl_piece_bitsize (*piece_loc) == bitsize)
5196 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5197 else
5198 last_loc_note = NULL_RTX;
5199 /* If the current location is the same as the end of the list,
5200 and either both or neither of the locations is uninitialized,
5201 we have nothing to do. */
5202 if (last_loc_note == NULL_RTX
5203 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5204 NOTE_VAR_LOCATION_LOC (loc_note)))
5205 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5206 != NOTE_VAR_LOCATION_STATUS (loc_note))
5207 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5208 == VAR_INIT_STATUS_UNINITIALIZED)
5209 || (NOTE_VAR_LOCATION_STATUS (loc_note)
5210 == VAR_INIT_STATUS_UNINITIALIZED))))
5212 /* Add LOC to the end of list and update LAST. If the last
5213 element of the list has been removed above, reuse its
5214 memory for the new node, otherwise allocate a new one. */
5215 if (unused)
5217 loc = unused;
5218 memset (loc, '\0', sizeof (*loc));
5220 else
5221 loc = ggc_alloc_cleared_var_loc_node ();
5222 if (bitsize == -1 || piece_loc == NULL)
5223 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5224 else
5225 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5226 bitpos, piece_bitpos, bitsize, loc_note);
5227 last->next = loc;
5228 /* Ensure TEMP->LAST will point either to the new last but one
5229 element of the chain, or to the last element in it. */
5230 if (last != temp->last)
5231 temp->last = last;
5233 else if (unused)
5234 ggc_free (unused);
5236 else
5238 loc = ggc_alloc_cleared_var_loc_node ();
5239 temp->first = loc;
5240 temp->last = loc;
5241 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5243 return loc;
5246 /* Keep track of the number of spaces used to indent the
5247 output of the debugging routines that print the structure of
5248 the DIE internal representation. */
5249 static int print_indent;
5251 /* Indent the line the number of spaces given by print_indent. */
5253 static inline void
5254 print_spaces (FILE *outfile)
5256 fprintf (outfile, "%*s", print_indent, "");
5259 /* Print a type signature in hex. */
5261 static inline void
5262 print_signature (FILE *outfile, char *sig)
5264 int i;
5266 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5267 fprintf (outfile, "%02x", sig[i] & 0xff);
5270 /* Print the information associated with a given DIE, and its children.
5271 This routine is a debugging aid only. */
5273 static void
5274 print_die (dw_die_ref die, FILE *outfile)
5276 dw_attr_ref a;
5277 dw_die_ref c;
5278 unsigned ix;
5280 print_spaces (outfile);
5281 fprintf (outfile, "DIE %4ld: %s (%p)\n",
5282 die->die_offset, dwarf_tag_name (die->die_tag),
5283 (void*) die);
5284 print_spaces (outfile);
5285 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5286 fprintf (outfile, " offset: %ld", die->die_offset);
5287 fprintf (outfile, " mark: %d\n", die->die_mark);
5289 if (die->comdat_type_p)
5291 print_spaces (outfile);
5292 fprintf (outfile, " signature: ");
5293 print_signature (outfile, die->die_id.die_type_node->signature);
5294 fprintf (outfile, "\n");
5297 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5299 print_spaces (outfile);
5300 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5302 switch (AT_class (a))
5304 case dw_val_class_addr:
5305 fprintf (outfile, "address");
5306 break;
5307 case dw_val_class_offset:
5308 fprintf (outfile, "offset");
5309 break;
5310 case dw_val_class_loc:
5311 fprintf (outfile, "location descriptor");
5312 break;
5313 case dw_val_class_loc_list:
5314 fprintf (outfile, "location list -> label:%s",
5315 AT_loc_list (a)->ll_symbol);
5316 break;
5317 case dw_val_class_range_list:
5318 fprintf (outfile, "range list");
5319 break;
5320 case dw_val_class_const:
5321 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
5322 break;
5323 case dw_val_class_unsigned_const:
5324 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
5325 break;
5326 case dw_val_class_const_double:
5327 fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
5328 HOST_WIDE_INT_PRINT_UNSIGNED")",
5329 a->dw_attr_val.v.val_double.high,
5330 a->dw_attr_val.v.val_double.low);
5331 break;
5332 case dw_val_class_vec:
5333 fprintf (outfile, "floating-point or vector constant");
5334 break;
5335 case dw_val_class_flag:
5336 fprintf (outfile, "%u", AT_flag (a));
5337 break;
5338 case dw_val_class_die_ref:
5339 if (AT_ref (a) != NULL)
5341 if (AT_ref (a)->comdat_type_p)
5343 fprintf (outfile, "die -> signature: ");
5344 print_signature (outfile,
5345 AT_ref (a)->die_id.die_type_node->signature);
5347 else if (AT_ref (a)->die_id.die_symbol)
5348 fprintf (outfile, "die -> label: %s",
5349 AT_ref (a)->die_id.die_symbol);
5350 else
5351 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
5352 fprintf (outfile, " (%p)", (void *) AT_ref (a));
5354 else
5355 fprintf (outfile, "die -> <null>");
5356 break;
5357 case dw_val_class_vms_delta:
5358 fprintf (outfile, "delta: @slotcount(%s-%s)",
5359 AT_vms_delta2 (a), AT_vms_delta1 (a));
5360 break;
5361 case dw_val_class_lbl_id:
5362 case dw_val_class_lineptr:
5363 case dw_val_class_macptr:
5364 case dw_val_class_high_pc:
5365 fprintf (outfile, "label: %s", AT_lbl (a));
5366 break;
5367 case dw_val_class_str:
5368 if (AT_string (a) != NULL)
5369 fprintf (outfile, "\"%s\"", AT_string (a));
5370 else
5371 fprintf (outfile, "<null>");
5372 break;
5373 case dw_val_class_file:
5374 fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
5375 AT_file (a)->emitted_number);
5376 break;
5377 case dw_val_class_data8:
5379 int i;
5381 for (i = 0; i < 8; i++)
5382 fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
5383 break;
5385 default:
5386 break;
5389 fprintf (outfile, "\n");
5392 if (die->die_child != NULL)
5394 print_indent += 4;
5395 FOR_EACH_CHILD (die, c, print_die (c, outfile));
5396 print_indent -= 4;
5398 if (print_indent == 0)
5399 fprintf (outfile, "\n");
5402 /* Print the information collected for a given DIE. */
5404 DEBUG_FUNCTION void
5405 debug_dwarf_die (dw_die_ref die)
5407 print_die (die, stderr);
5410 DEBUG_FUNCTION void
5411 debug (die_struct &ref)
5413 print_die (&ref, stderr);
5416 DEBUG_FUNCTION void
5417 debug (die_struct *ptr)
5419 if (ptr)
5420 debug (*ptr);
5421 else
5422 fprintf (stderr, "<nil>\n");
5426 /* Print all DWARF information collected for the compilation unit.
5427 This routine is a debugging aid only. */
5429 DEBUG_FUNCTION void
5430 debug_dwarf (void)
5432 print_indent = 0;
5433 print_die (comp_unit_die (), stderr);
5436 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
5437 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
5438 DIE that marks the start of the DIEs for this include file. */
5440 static dw_die_ref
5441 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5443 const char *filename = get_AT_string (bincl_die, DW_AT_name);
5444 dw_die_ref new_unit = gen_compile_unit_die (filename);
5446 new_unit->die_sib = old_unit;
5447 return new_unit;
5450 /* Close an include-file CU and reopen the enclosing one. */
5452 static dw_die_ref
5453 pop_compile_unit (dw_die_ref old_unit)
5455 dw_die_ref new_unit = old_unit->die_sib;
5457 old_unit->die_sib = NULL;
5458 return new_unit;
5461 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5462 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5463 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5465 /* Calculate the checksum of a location expression. */
5467 static inline void
5468 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5470 int tem;
5471 hashval_t hash = 0;
5473 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
5474 CHECKSUM (tem);
5475 hash = hash_loc_operands (loc, hash);
5476 CHECKSUM (hash);
5479 /* Calculate the checksum of an attribute. */
5481 static void
5482 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5484 dw_loc_descr_ref loc;
5485 rtx r;
5487 CHECKSUM (at->dw_attr);
5489 /* We don't care that this was compiled with a different compiler
5490 snapshot; if the output is the same, that's what matters. */
5491 if (at->dw_attr == DW_AT_producer)
5492 return;
5494 switch (AT_class (at))
5496 case dw_val_class_const:
5497 CHECKSUM (at->dw_attr_val.v.val_int);
5498 break;
5499 case dw_val_class_unsigned_const:
5500 CHECKSUM (at->dw_attr_val.v.val_unsigned);
5501 break;
5502 case dw_val_class_const_double:
5503 CHECKSUM (at->dw_attr_val.v.val_double);
5504 break;
5505 case dw_val_class_vec:
5506 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5507 (at->dw_attr_val.v.val_vec.length
5508 * at->dw_attr_val.v.val_vec.elt_size));
5509 break;
5510 case dw_val_class_flag:
5511 CHECKSUM (at->dw_attr_val.v.val_flag);
5512 break;
5513 case dw_val_class_str:
5514 CHECKSUM_STRING (AT_string (at));
5515 break;
5517 case dw_val_class_addr:
5518 r = AT_addr (at);
5519 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5520 CHECKSUM_STRING (XSTR (r, 0));
5521 break;
5523 case dw_val_class_offset:
5524 CHECKSUM (at->dw_attr_val.v.val_offset);
5525 break;
5527 case dw_val_class_loc:
5528 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5529 loc_checksum (loc, ctx);
5530 break;
5532 case dw_val_class_die_ref:
5533 die_checksum (AT_ref (at), ctx, mark);
5534 break;
5536 case dw_val_class_fde_ref:
5537 case dw_val_class_vms_delta:
5538 case dw_val_class_lbl_id:
5539 case dw_val_class_lineptr:
5540 case dw_val_class_macptr:
5541 case dw_val_class_high_pc:
5542 break;
5544 case dw_val_class_file:
5545 CHECKSUM_STRING (AT_file (at)->filename);
5546 break;
5548 case dw_val_class_data8:
5549 CHECKSUM (at->dw_attr_val.v.val_data8);
5550 break;
5552 default:
5553 break;
5557 /* Calculate the checksum of a DIE. */
5559 static void
5560 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5562 dw_die_ref c;
5563 dw_attr_ref a;
5564 unsigned ix;
5566 /* To avoid infinite recursion. */
5567 if (die->die_mark)
5569 CHECKSUM (die->die_mark);
5570 return;
5572 die->die_mark = ++(*mark);
5574 CHECKSUM (die->die_tag);
5576 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5577 attr_checksum (a, ctx, mark);
5579 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
5582 #undef CHECKSUM
5583 #undef CHECKSUM_BLOCK
5584 #undef CHECKSUM_STRING
5586 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
5587 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5588 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5589 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
5590 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
5591 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
5592 #define CHECKSUM_ATTR(FOO) \
5593 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
5595 /* Calculate the checksum of a number in signed LEB128 format. */
5597 static void
5598 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
5600 unsigned char byte;
5601 bool more;
5603 while (1)
5605 byte = (value & 0x7f);
5606 value >>= 7;
5607 more = !((value == 0 && (byte & 0x40) == 0)
5608 || (value == -1 && (byte & 0x40) != 0));
5609 if (more)
5610 byte |= 0x80;
5611 CHECKSUM (byte);
5612 if (!more)
5613 break;
5617 /* Calculate the checksum of a number in unsigned LEB128 format. */
5619 static void
5620 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
5622 while (1)
5624 unsigned char byte = (value & 0x7f);
5625 value >>= 7;
5626 if (value != 0)
5627 /* More bytes to follow. */
5628 byte |= 0x80;
5629 CHECKSUM (byte);
5630 if (value == 0)
5631 break;
5635 /* Checksum the context of the DIE. This adds the names of any
5636 surrounding namespaces or structures to the checksum. */
5638 static void
5639 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
5641 const char *name;
5642 dw_die_ref spec;
5643 int tag = die->die_tag;
5645 if (tag != DW_TAG_namespace
5646 && tag != DW_TAG_structure_type
5647 && tag != DW_TAG_class_type)
5648 return;
5650 name = get_AT_string (die, DW_AT_name);
5652 spec = get_AT_ref (die, DW_AT_specification);
5653 if (spec != NULL)
5654 die = spec;
5656 if (die->die_parent != NULL)
5657 checksum_die_context (die->die_parent, ctx);
5659 CHECKSUM_ULEB128 ('C');
5660 CHECKSUM_ULEB128 (tag);
5661 if (name != NULL)
5662 CHECKSUM_STRING (name);
5665 /* Calculate the checksum of a location expression. */
5667 static inline void
5668 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5670 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
5671 were emitted as a DW_FORM_sdata instead of a location expression. */
5672 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
5674 CHECKSUM_ULEB128 (DW_FORM_sdata);
5675 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
5676 return;
5679 /* Otherwise, just checksum the raw location expression. */
5680 while (loc != NULL)
5682 hashval_t hash = 0;
5684 CHECKSUM_ULEB128 (loc->dtprel);
5685 CHECKSUM_ULEB128 (loc->dw_loc_opc);
5686 hash = hash_loc_operands (loc, hash);
5687 CHECKSUM (hash);
5688 loc = loc->dw_loc_next;
5692 /* Calculate the checksum of an attribute. */
5694 static void
5695 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
5696 struct md5_ctx *ctx, int *mark)
5698 dw_loc_descr_ref loc;
5699 rtx r;
5701 if (AT_class (at) == dw_val_class_die_ref)
5703 dw_die_ref target_die = AT_ref (at);
5705 /* For pointer and reference types, we checksum only the (qualified)
5706 name of the target type (if there is a name). For friend entries,
5707 we checksum only the (qualified) name of the target type or function.
5708 This allows the checksum to remain the same whether the target type
5709 is complete or not. */
5710 if ((at->dw_attr == DW_AT_type
5711 && (tag == DW_TAG_pointer_type
5712 || tag == DW_TAG_reference_type
5713 || tag == DW_TAG_rvalue_reference_type
5714 || tag == DW_TAG_ptr_to_member_type))
5715 || (at->dw_attr == DW_AT_friend
5716 && tag == DW_TAG_friend))
5718 dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
5720 if (name_attr != NULL)
5722 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5724 if (decl == NULL)
5725 decl = target_die;
5726 CHECKSUM_ULEB128 ('N');
5727 CHECKSUM_ULEB128 (at->dw_attr);
5728 if (decl->die_parent != NULL)
5729 checksum_die_context (decl->die_parent, ctx);
5730 CHECKSUM_ULEB128 ('E');
5731 CHECKSUM_STRING (AT_string (name_attr));
5732 return;
5736 /* For all other references to another DIE, we check to see if the
5737 target DIE has already been visited. If it has, we emit a
5738 backward reference; if not, we descend recursively. */
5739 if (target_die->die_mark > 0)
5741 CHECKSUM_ULEB128 ('R');
5742 CHECKSUM_ULEB128 (at->dw_attr);
5743 CHECKSUM_ULEB128 (target_die->die_mark);
5745 else
5747 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5749 if (decl == NULL)
5750 decl = target_die;
5751 target_die->die_mark = ++(*mark);
5752 CHECKSUM_ULEB128 ('T');
5753 CHECKSUM_ULEB128 (at->dw_attr);
5754 if (decl->die_parent != NULL)
5755 checksum_die_context (decl->die_parent, ctx);
5756 die_checksum_ordered (target_die, ctx, mark);
5758 return;
5761 CHECKSUM_ULEB128 ('A');
5762 CHECKSUM_ULEB128 (at->dw_attr);
5764 switch (AT_class (at))
5766 case dw_val_class_const:
5767 CHECKSUM_ULEB128 (DW_FORM_sdata);
5768 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
5769 break;
5771 case dw_val_class_unsigned_const:
5772 CHECKSUM_ULEB128 (DW_FORM_sdata);
5773 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
5774 break;
5776 case dw_val_class_const_double:
5777 CHECKSUM_ULEB128 (DW_FORM_block);
5778 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
5779 CHECKSUM (at->dw_attr_val.v.val_double);
5780 break;
5782 case dw_val_class_vec:
5783 CHECKSUM_ULEB128 (DW_FORM_block);
5784 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
5785 * at->dw_attr_val.v.val_vec.elt_size);
5786 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5787 (at->dw_attr_val.v.val_vec.length
5788 * at->dw_attr_val.v.val_vec.elt_size));
5789 break;
5791 case dw_val_class_flag:
5792 CHECKSUM_ULEB128 (DW_FORM_flag);
5793 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
5794 break;
5796 case dw_val_class_str:
5797 CHECKSUM_ULEB128 (DW_FORM_string);
5798 CHECKSUM_STRING (AT_string (at));
5799 break;
5801 case dw_val_class_addr:
5802 r = AT_addr (at);
5803 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5804 CHECKSUM_ULEB128 (DW_FORM_string);
5805 CHECKSUM_STRING (XSTR (r, 0));
5806 break;
5808 case dw_val_class_offset:
5809 CHECKSUM_ULEB128 (DW_FORM_sdata);
5810 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
5811 break;
5813 case dw_val_class_loc:
5814 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5815 loc_checksum_ordered (loc, ctx);
5816 break;
5818 case dw_val_class_fde_ref:
5819 case dw_val_class_lbl_id:
5820 case dw_val_class_lineptr:
5821 case dw_val_class_macptr:
5822 case dw_val_class_high_pc:
5823 break;
5825 case dw_val_class_file:
5826 CHECKSUM_ULEB128 (DW_FORM_string);
5827 CHECKSUM_STRING (AT_file (at)->filename);
5828 break;
5830 case dw_val_class_data8:
5831 CHECKSUM (at->dw_attr_val.v.val_data8);
5832 break;
5834 default:
5835 break;
5839 struct checksum_attributes
5841 dw_attr_ref at_name;
5842 dw_attr_ref at_type;
5843 dw_attr_ref at_friend;
5844 dw_attr_ref at_accessibility;
5845 dw_attr_ref at_address_class;
5846 dw_attr_ref at_allocated;
5847 dw_attr_ref at_artificial;
5848 dw_attr_ref at_associated;
5849 dw_attr_ref at_binary_scale;
5850 dw_attr_ref at_bit_offset;
5851 dw_attr_ref at_bit_size;
5852 dw_attr_ref at_bit_stride;
5853 dw_attr_ref at_byte_size;
5854 dw_attr_ref at_byte_stride;
5855 dw_attr_ref at_const_value;
5856 dw_attr_ref at_containing_type;
5857 dw_attr_ref at_count;
5858 dw_attr_ref at_data_location;
5859 dw_attr_ref at_data_member_location;
5860 dw_attr_ref at_decimal_scale;
5861 dw_attr_ref at_decimal_sign;
5862 dw_attr_ref at_default_value;
5863 dw_attr_ref at_digit_count;
5864 dw_attr_ref at_discr;
5865 dw_attr_ref at_discr_list;
5866 dw_attr_ref at_discr_value;
5867 dw_attr_ref at_encoding;
5868 dw_attr_ref at_endianity;
5869 dw_attr_ref at_explicit;
5870 dw_attr_ref at_is_optional;
5871 dw_attr_ref at_location;
5872 dw_attr_ref at_lower_bound;
5873 dw_attr_ref at_mutable;
5874 dw_attr_ref at_ordering;
5875 dw_attr_ref at_picture_string;
5876 dw_attr_ref at_prototyped;
5877 dw_attr_ref at_small;
5878 dw_attr_ref at_segment;
5879 dw_attr_ref at_string_length;
5880 dw_attr_ref at_threads_scaled;
5881 dw_attr_ref at_upper_bound;
5882 dw_attr_ref at_use_location;
5883 dw_attr_ref at_use_UTF8;
5884 dw_attr_ref at_variable_parameter;
5885 dw_attr_ref at_virtuality;
5886 dw_attr_ref at_visibility;
5887 dw_attr_ref at_vtable_elem_location;
5890 /* Collect the attributes that we will want to use for the checksum. */
5892 static void
5893 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
5895 dw_attr_ref a;
5896 unsigned ix;
5898 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5900 switch (a->dw_attr)
5902 case DW_AT_name:
5903 attrs->at_name = a;
5904 break;
5905 case DW_AT_type:
5906 attrs->at_type = a;
5907 break;
5908 case DW_AT_friend:
5909 attrs->at_friend = a;
5910 break;
5911 case DW_AT_accessibility:
5912 attrs->at_accessibility = a;
5913 break;
5914 case DW_AT_address_class:
5915 attrs->at_address_class = a;
5916 break;
5917 case DW_AT_allocated:
5918 attrs->at_allocated = a;
5919 break;
5920 case DW_AT_artificial:
5921 attrs->at_artificial = a;
5922 break;
5923 case DW_AT_associated:
5924 attrs->at_associated = a;
5925 break;
5926 case DW_AT_binary_scale:
5927 attrs->at_binary_scale = a;
5928 break;
5929 case DW_AT_bit_offset:
5930 attrs->at_bit_offset = a;
5931 break;
5932 case DW_AT_bit_size:
5933 attrs->at_bit_size = a;
5934 break;
5935 case DW_AT_bit_stride:
5936 attrs->at_bit_stride = a;
5937 break;
5938 case DW_AT_byte_size:
5939 attrs->at_byte_size = a;
5940 break;
5941 case DW_AT_byte_stride:
5942 attrs->at_byte_stride = a;
5943 break;
5944 case DW_AT_const_value:
5945 attrs->at_const_value = a;
5946 break;
5947 case DW_AT_containing_type:
5948 attrs->at_containing_type = a;
5949 break;
5950 case DW_AT_count:
5951 attrs->at_count = a;
5952 break;
5953 case DW_AT_data_location:
5954 attrs->at_data_location = a;
5955 break;
5956 case DW_AT_data_member_location:
5957 attrs->at_data_member_location = a;
5958 break;
5959 case DW_AT_decimal_scale:
5960 attrs->at_decimal_scale = a;
5961 break;
5962 case DW_AT_decimal_sign:
5963 attrs->at_decimal_sign = a;
5964 break;
5965 case DW_AT_default_value:
5966 attrs->at_default_value = a;
5967 break;
5968 case DW_AT_digit_count:
5969 attrs->at_digit_count = a;
5970 break;
5971 case DW_AT_discr:
5972 attrs->at_discr = a;
5973 break;
5974 case DW_AT_discr_list:
5975 attrs->at_discr_list = a;
5976 break;
5977 case DW_AT_discr_value:
5978 attrs->at_discr_value = a;
5979 break;
5980 case DW_AT_encoding:
5981 attrs->at_encoding = a;
5982 break;
5983 case DW_AT_endianity:
5984 attrs->at_endianity = a;
5985 break;
5986 case DW_AT_explicit:
5987 attrs->at_explicit = a;
5988 break;
5989 case DW_AT_is_optional:
5990 attrs->at_is_optional = a;
5991 break;
5992 case DW_AT_location:
5993 attrs->at_location = a;
5994 break;
5995 case DW_AT_lower_bound:
5996 attrs->at_lower_bound = a;
5997 break;
5998 case DW_AT_mutable:
5999 attrs->at_mutable = a;
6000 break;
6001 case DW_AT_ordering:
6002 attrs->at_ordering = a;
6003 break;
6004 case DW_AT_picture_string:
6005 attrs->at_picture_string = a;
6006 break;
6007 case DW_AT_prototyped:
6008 attrs->at_prototyped = a;
6009 break;
6010 case DW_AT_small:
6011 attrs->at_small = a;
6012 break;
6013 case DW_AT_segment:
6014 attrs->at_segment = a;
6015 break;
6016 case DW_AT_string_length:
6017 attrs->at_string_length = a;
6018 break;
6019 case DW_AT_threads_scaled:
6020 attrs->at_threads_scaled = a;
6021 break;
6022 case DW_AT_upper_bound:
6023 attrs->at_upper_bound = a;
6024 break;
6025 case DW_AT_use_location:
6026 attrs->at_use_location = a;
6027 break;
6028 case DW_AT_use_UTF8:
6029 attrs->at_use_UTF8 = a;
6030 break;
6031 case DW_AT_variable_parameter:
6032 attrs->at_variable_parameter = a;
6033 break;
6034 case DW_AT_virtuality:
6035 attrs->at_virtuality = a;
6036 break;
6037 case DW_AT_visibility:
6038 attrs->at_visibility = a;
6039 break;
6040 case DW_AT_vtable_elem_location:
6041 attrs->at_vtable_elem_location = a;
6042 break;
6043 default:
6044 break;
6049 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
6051 static void
6052 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6054 dw_die_ref c;
6055 dw_die_ref decl;
6056 struct checksum_attributes attrs;
6058 CHECKSUM_ULEB128 ('D');
6059 CHECKSUM_ULEB128 (die->die_tag);
6061 memset (&attrs, 0, sizeof (attrs));
6063 decl = get_AT_ref (die, DW_AT_specification);
6064 if (decl != NULL)
6065 collect_checksum_attributes (&attrs, decl);
6066 collect_checksum_attributes (&attrs, die);
6068 CHECKSUM_ATTR (attrs.at_name);
6069 CHECKSUM_ATTR (attrs.at_accessibility);
6070 CHECKSUM_ATTR (attrs.at_address_class);
6071 CHECKSUM_ATTR (attrs.at_allocated);
6072 CHECKSUM_ATTR (attrs.at_artificial);
6073 CHECKSUM_ATTR (attrs.at_associated);
6074 CHECKSUM_ATTR (attrs.at_binary_scale);
6075 CHECKSUM_ATTR (attrs.at_bit_offset);
6076 CHECKSUM_ATTR (attrs.at_bit_size);
6077 CHECKSUM_ATTR (attrs.at_bit_stride);
6078 CHECKSUM_ATTR (attrs.at_byte_size);
6079 CHECKSUM_ATTR (attrs.at_byte_stride);
6080 CHECKSUM_ATTR (attrs.at_const_value);
6081 CHECKSUM_ATTR (attrs.at_containing_type);
6082 CHECKSUM_ATTR (attrs.at_count);
6083 CHECKSUM_ATTR (attrs.at_data_location);
6084 CHECKSUM_ATTR (attrs.at_data_member_location);
6085 CHECKSUM_ATTR (attrs.at_decimal_scale);
6086 CHECKSUM_ATTR (attrs.at_decimal_sign);
6087 CHECKSUM_ATTR (attrs.at_default_value);
6088 CHECKSUM_ATTR (attrs.at_digit_count);
6089 CHECKSUM_ATTR (attrs.at_discr);
6090 CHECKSUM_ATTR (attrs.at_discr_list);
6091 CHECKSUM_ATTR (attrs.at_discr_value);
6092 CHECKSUM_ATTR (attrs.at_encoding);
6093 CHECKSUM_ATTR (attrs.at_endianity);
6094 CHECKSUM_ATTR (attrs.at_explicit);
6095 CHECKSUM_ATTR (attrs.at_is_optional);
6096 CHECKSUM_ATTR (attrs.at_location);
6097 CHECKSUM_ATTR (attrs.at_lower_bound);
6098 CHECKSUM_ATTR (attrs.at_mutable);
6099 CHECKSUM_ATTR (attrs.at_ordering);
6100 CHECKSUM_ATTR (attrs.at_picture_string);
6101 CHECKSUM_ATTR (attrs.at_prototyped);
6102 CHECKSUM_ATTR (attrs.at_small);
6103 CHECKSUM_ATTR (attrs.at_segment);
6104 CHECKSUM_ATTR (attrs.at_string_length);
6105 CHECKSUM_ATTR (attrs.at_threads_scaled);
6106 CHECKSUM_ATTR (attrs.at_upper_bound);
6107 CHECKSUM_ATTR (attrs.at_use_location);
6108 CHECKSUM_ATTR (attrs.at_use_UTF8);
6109 CHECKSUM_ATTR (attrs.at_variable_parameter);
6110 CHECKSUM_ATTR (attrs.at_virtuality);
6111 CHECKSUM_ATTR (attrs.at_visibility);
6112 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6113 CHECKSUM_ATTR (attrs.at_type);
6114 CHECKSUM_ATTR (attrs.at_friend);
6116 /* Checksum the child DIEs. */
6117 c = die->die_child;
6118 if (c) do {
6119 dw_attr_ref name_attr;
6121 c = c->die_sib;
6122 name_attr = get_AT (c, DW_AT_name);
6123 if (is_template_instantiation (c))
6125 /* Ignore instantiations of member type and function templates. */
6127 else if (name_attr != NULL
6128 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
6130 /* Use a shallow checksum for named nested types and member
6131 functions. */
6132 CHECKSUM_ULEB128 ('S');
6133 CHECKSUM_ULEB128 (c->die_tag);
6134 CHECKSUM_STRING (AT_string (name_attr));
6136 else
6138 /* Use a deep checksum for other children. */
6139 /* Mark this DIE so it gets processed when unmarking. */
6140 if (c->die_mark == 0)
6141 c->die_mark = -1;
6142 die_checksum_ordered (c, ctx, mark);
6144 } while (c != die->die_child);
6146 CHECKSUM_ULEB128 (0);
6149 /* Add a type name and tag to a hash. */
6150 static void
6151 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
6153 CHECKSUM_ULEB128 (tag);
6154 CHECKSUM_STRING (name);
6157 #undef CHECKSUM
6158 #undef CHECKSUM_STRING
6159 #undef CHECKSUM_ATTR
6160 #undef CHECKSUM_LEB128
6161 #undef CHECKSUM_ULEB128
6163 /* Generate the type signature for DIE. This is computed by generating an
6164 MD5 checksum over the DIE's tag, its relevant attributes, and its
6165 children. Attributes that are references to other DIEs are processed
6166 by recursion, using the MARK field to prevent infinite recursion.
6167 If the DIE is nested inside a namespace or another type, we also
6168 need to include that context in the signature. The lower 64 bits
6169 of the resulting MD5 checksum comprise the signature. */
6171 static void
6172 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6174 int mark;
6175 const char *name;
6176 unsigned char checksum[16];
6177 struct md5_ctx ctx;
6178 dw_die_ref decl;
6179 dw_die_ref parent;
6181 name = get_AT_string (die, DW_AT_name);
6182 decl = get_AT_ref (die, DW_AT_specification);
6183 parent = get_die_parent (die);
6185 /* First, compute a signature for just the type name (and its surrounding
6186 context, if any. This is stored in the type unit DIE for link-time
6187 ODR (one-definition rule) checking. */
6189 if (is_cxx () && name != NULL)
6191 md5_init_ctx (&ctx);
6193 /* Checksum the names of surrounding namespaces and structures. */
6194 if (parent != NULL)
6195 checksum_die_context (parent, &ctx);
6197 /* Checksum the current DIE. */
6198 die_odr_checksum (die->die_tag, name, &ctx);
6199 md5_finish_ctx (&ctx, checksum);
6201 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6204 /* Next, compute the complete type signature. */
6206 md5_init_ctx (&ctx);
6207 mark = 1;
6208 die->die_mark = mark;
6210 /* Checksum the names of surrounding namespaces and structures. */
6211 if (parent != NULL)
6212 checksum_die_context (parent, &ctx);
6214 /* Checksum the DIE and its children. */
6215 die_checksum_ordered (die, &ctx, &mark);
6216 unmark_all_dies (die);
6217 md5_finish_ctx (&ctx, checksum);
6219 /* Store the signature in the type node and link the type DIE and the
6220 type node together. */
6221 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6222 DWARF_TYPE_SIGNATURE_SIZE);
6223 die->comdat_type_p = true;
6224 die->die_id.die_type_node = type_node;
6225 type_node->type_die = die;
6227 /* If the DIE is a specification, link its declaration to the type node
6228 as well. */
6229 if (decl != NULL)
6231 decl->comdat_type_p = true;
6232 decl->die_id.die_type_node = type_node;
6236 /* Do the location expressions look same? */
6237 static inline int
6238 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6240 return loc1->dw_loc_opc == loc2->dw_loc_opc
6241 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6242 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6245 /* Do the values look the same? */
6246 static int
6247 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6249 dw_loc_descr_ref loc1, loc2;
6250 rtx r1, r2;
6252 if (v1->val_class != v2->val_class)
6253 return 0;
6255 switch (v1->val_class)
6257 case dw_val_class_const:
6258 return v1->v.val_int == v2->v.val_int;
6259 case dw_val_class_unsigned_const:
6260 return v1->v.val_unsigned == v2->v.val_unsigned;
6261 case dw_val_class_const_double:
6262 return v1->v.val_double.high == v2->v.val_double.high
6263 && v1->v.val_double.low == v2->v.val_double.low;
6264 case dw_val_class_vec:
6265 if (v1->v.val_vec.length != v2->v.val_vec.length
6266 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6267 return 0;
6268 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6269 v1->v.val_vec.length * v1->v.val_vec.elt_size))
6270 return 0;
6271 return 1;
6272 case dw_val_class_flag:
6273 return v1->v.val_flag == v2->v.val_flag;
6274 case dw_val_class_str:
6275 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
6277 case dw_val_class_addr:
6278 r1 = v1->v.val_addr;
6279 r2 = v2->v.val_addr;
6280 if (GET_CODE (r1) != GET_CODE (r2))
6281 return 0;
6282 return !rtx_equal_p (r1, r2);
6284 case dw_val_class_offset:
6285 return v1->v.val_offset == v2->v.val_offset;
6287 case dw_val_class_loc:
6288 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6289 loc1 && loc2;
6290 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6291 if (!same_loc_p (loc1, loc2, mark))
6292 return 0;
6293 return !loc1 && !loc2;
6295 case dw_val_class_die_ref:
6296 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6298 case dw_val_class_fde_ref:
6299 case dw_val_class_vms_delta:
6300 case dw_val_class_lbl_id:
6301 case dw_val_class_lineptr:
6302 case dw_val_class_macptr:
6303 case dw_val_class_high_pc:
6304 return 1;
6306 case dw_val_class_file:
6307 return v1->v.val_file == v2->v.val_file;
6309 case dw_val_class_data8:
6310 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
6312 default:
6313 return 1;
6317 /* Do the attributes look the same? */
6319 static int
6320 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6322 if (at1->dw_attr != at2->dw_attr)
6323 return 0;
6325 /* We don't care that this was compiled with a different compiler
6326 snapshot; if the output is the same, that's what matters. */
6327 if (at1->dw_attr == DW_AT_producer)
6328 return 1;
6330 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6333 /* Do the dies look the same? */
6335 static int
6336 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6338 dw_die_ref c1, c2;
6339 dw_attr_ref a1;
6340 unsigned ix;
6342 /* To avoid infinite recursion. */
6343 if (die1->die_mark)
6344 return die1->die_mark == die2->die_mark;
6345 die1->die_mark = die2->die_mark = ++(*mark);
6347 if (die1->die_tag != die2->die_tag)
6348 return 0;
6350 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
6351 return 0;
6353 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
6354 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
6355 return 0;
6357 c1 = die1->die_child;
6358 c2 = die2->die_child;
6359 if (! c1)
6361 if (c2)
6362 return 0;
6364 else
6365 for (;;)
6367 if (!same_die_p (c1, c2, mark))
6368 return 0;
6369 c1 = c1->die_sib;
6370 c2 = c2->die_sib;
6371 if (c1 == die1->die_child)
6373 if (c2 == die2->die_child)
6374 break;
6375 else
6376 return 0;
6380 return 1;
6383 /* Do the dies look the same? Wrapper around same_die_p. */
6385 static int
6386 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6388 int mark = 0;
6389 int ret = same_die_p (die1, die2, &mark);
6391 unmark_all_dies (die1);
6392 unmark_all_dies (die2);
6394 return ret;
6397 /* The prefix to attach to symbols on DIEs in the current comdat debug
6398 info section. */
6399 static const char *comdat_symbol_id;
6401 /* The index of the current symbol within the current comdat CU. */
6402 static unsigned int comdat_symbol_number;
6404 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6405 children, and set comdat_symbol_id accordingly. */
6407 static void
6408 compute_section_prefix (dw_die_ref unit_die)
6410 const char *die_name = get_AT_string (unit_die, DW_AT_name);
6411 const char *base = die_name ? lbasename (die_name) : "anonymous";
6412 char *name = XALLOCAVEC (char, strlen (base) + 64);
6413 char *p;
6414 int i, mark;
6415 unsigned char checksum[16];
6416 struct md5_ctx ctx;
6418 /* Compute the checksum of the DIE, then append part of it as hex digits to
6419 the name filename of the unit. */
6421 md5_init_ctx (&ctx);
6422 mark = 0;
6423 die_checksum (unit_die, &ctx, &mark);
6424 unmark_all_dies (unit_die);
6425 md5_finish_ctx (&ctx, checksum);
6427 sprintf (name, "%s.", base);
6428 clean_symbol_name (name);
6430 p = name + strlen (name);
6431 for (i = 0; i < 4; i++)
6433 sprintf (p, "%.2x", checksum[i]);
6434 p += 2;
6437 comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
6438 comdat_symbol_number = 0;
6441 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
6443 static int
6444 is_type_die (dw_die_ref die)
6446 switch (die->die_tag)
6448 case DW_TAG_array_type:
6449 case DW_TAG_class_type:
6450 case DW_TAG_interface_type:
6451 case DW_TAG_enumeration_type:
6452 case DW_TAG_pointer_type:
6453 case DW_TAG_reference_type:
6454 case DW_TAG_rvalue_reference_type:
6455 case DW_TAG_string_type:
6456 case DW_TAG_structure_type:
6457 case DW_TAG_subroutine_type:
6458 case DW_TAG_union_type:
6459 case DW_TAG_ptr_to_member_type:
6460 case DW_TAG_set_type:
6461 case DW_TAG_subrange_type:
6462 case DW_TAG_base_type:
6463 case DW_TAG_const_type:
6464 case DW_TAG_file_type:
6465 case DW_TAG_packed_type:
6466 case DW_TAG_volatile_type:
6467 case DW_TAG_typedef:
6468 return 1;
6469 default:
6470 return 0;
6474 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6475 Basically, we want to choose the bits that are likely to be shared between
6476 compilations (types) and leave out the bits that are specific to individual
6477 compilations (functions). */
6479 static int
6480 is_comdat_die (dw_die_ref c)
6482 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6483 we do for stabs. The advantage is a greater likelihood of sharing between
6484 objects that don't include headers in the same order (and therefore would
6485 put the base types in a different comdat). jason 8/28/00 */
6487 if (c->die_tag == DW_TAG_base_type)
6488 return 0;
6490 if (c->die_tag == DW_TAG_pointer_type
6491 || c->die_tag == DW_TAG_reference_type
6492 || c->die_tag == DW_TAG_rvalue_reference_type
6493 || c->die_tag == DW_TAG_const_type
6494 || c->die_tag == DW_TAG_volatile_type)
6496 dw_die_ref t = get_AT_ref (c, DW_AT_type);
6498 return t ? is_comdat_die (t) : 0;
6501 return is_type_die (c);
6504 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6505 compilation unit. */
6507 static int
6508 is_symbol_die (dw_die_ref c)
6510 return (is_type_die (c)
6511 || is_declaration_die (c)
6512 || c->die_tag == DW_TAG_namespace
6513 || c->die_tag == DW_TAG_module);
6516 /* Returns true iff C is a compile-unit DIE. */
6518 static inline bool
6519 is_cu_die (dw_die_ref c)
6521 return c && c->die_tag == DW_TAG_compile_unit;
6524 /* Returns true iff C is a unit DIE of some sort. */
6526 static inline bool
6527 is_unit_die (dw_die_ref c)
6529 return c && (c->die_tag == DW_TAG_compile_unit
6530 || c->die_tag == DW_TAG_partial_unit
6531 || c->die_tag == DW_TAG_type_unit);
6534 /* Returns true iff C is a namespace DIE. */
6536 static inline bool
6537 is_namespace_die (dw_die_ref c)
6539 return c && c->die_tag == DW_TAG_namespace;
6542 /* Returns true iff C is a class or structure DIE. */
6544 static inline bool
6545 is_class_die (dw_die_ref c)
6547 return c && (c->die_tag == DW_TAG_class_type
6548 || c->die_tag == DW_TAG_structure_type);
6551 /* Return non-zero if this DIE is a template parameter. */
6553 static inline bool
6554 is_template_parameter (dw_die_ref die)
6556 switch (die->die_tag)
6558 case DW_TAG_template_type_param:
6559 case DW_TAG_template_value_param:
6560 case DW_TAG_GNU_template_template_param:
6561 case DW_TAG_GNU_template_parameter_pack:
6562 return true;
6563 default:
6564 return false;
6568 /* Return non-zero if this DIE represents a template instantiation. */
6570 static inline bool
6571 is_template_instantiation (dw_die_ref die)
6573 dw_die_ref c;
6575 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
6576 return false;
6577 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
6578 return false;
6581 static char *
6582 gen_internal_sym (const char *prefix)
6584 char buf[256];
6586 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6587 return xstrdup (buf);
6590 /* Assign symbols to all worthy DIEs under DIE. */
6592 static void
6593 assign_symbol_names (dw_die_ref die)
6595 dw_die_ref c;
6597 if (is_symbol_die (die) && !die->comdat_type_p)
6599 if (comdat_symbol_id)
6601 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
6603 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6604 comdat_symbol_id, comdat_symbol_number++);
6605 die->die_id.die_symbol = xstrdup (p);
6607 else
6608 die->die_id.die_symbol = gen_internal_sym ("LDIE");
6611 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6614 struct cu_hash_table_entry
6616 dw_die_ref cu;
6617 unsigned min_comdat_num, max_comdat_num;
6618 struct cu_hash_table_entry *next;
6621 /* Helpers to manipulate hash table of CUs. */
6623 struct cu_hash_table_entry_hasher
6625 typedef cu_hash_table_entry value_type;
6626 typedef die_struct compare_type;
6627 static inline hashval_t hash (const value_type *);
6628 static inline bool equal (const value_type *, const compare_type *);
6629 static inline void remove (value_type *);
6632 inline hashval_t
6633 cu_hash_table_entry_hasher::hash (const value_type *entry)
6635 return htab_hash_string (entry->cu->die_id.die_symbol);
6638 inline bool
6639 cu_hash_table_entry_hasher::equal (const value_type *entry1,
6640 const compare_type *entry2)
6642 return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
6645 inline void
6646 cu_hash_table_entry_hasher::remove (value_type *entry)
6648 struct cu_hash_table_entry *next;
6650 while (entry)
6652 next = entry->next;
6653 free (entry);
6654 entry = next;
6658 typedef hash_table <cu_hash_table_entry_hasher> cu_hash_type;
6660 /* Check whether we have already seen this CU and set up SYM_NUM
6661 accordingly. */
6662 static int
6663 check_duplicate_cu (dw_die_ref cu, cu_hash_type htable, unsigned int *sym_num)
6665 struct cu_hash_table_entry dummy;
6666 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6668 dummy.max_comdat_num = 0;
6670 slot = htable.find_slot_with_hash (cu,
6671 htab_hash_string (cu->die_id.die_symbol),
6672 INSERT);
6673 entry = *slot;
6675 for (; entry; last = entry, entry = entry->next)
6677 if (same_die_p_wrap (cu, entry->cu))
6678 break;
6681 if (entry)
6683 *sym_num = entry->min_comdat_num;
6684 return 1;
6687 entry = XCNEW (struct cu_hash_table_entry);
6688 entry->cu = cu;
6689 entry->min_comdat_num = *sym_num = last->max_comdat_num;
6690 entry->next = *slot;
6691 *slot = entry;
6693 return 0;
6696 /* Record SYM_NUM to record of CU in HTABLE. */
6697 static void
6698 record_comdat_symbol_number (dw_die_ref cu, cu_hash_type htable,
6699 unsigned int sym_num)
6701 struct cu_hash_table_entry **slot, *entry;
6703 slot = htable.find_slot_with_hash (cu,
6704 htab_hash_string (cu->die_id.die_symbol),
6705 NO_INSERT);
6706 entry = *slot;
6708 entry->max_comdat_num = sym_num;
6711 /* Traverse the DIE (which is always comp_unit_die), and set up
6712 additional compilation units for each of the include files we see
6713 bracketed by BINCL/EINCL. */
6715 static void
6716 break_out_includes (dw_die_ref die)
6718 dw_die_ref c;
6719 dw_die_ref unit = NULL;
6720 limbo_die_node *node, **pnode;
6721 cu_hash_type cu_hash_table;
6723 c = die->die_child;
6724 if (c) do {
6725 dw_die_ref prev = c;
6726 c = c->die_sib;
6727 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6728 || (unit && is_comdat_die (c)))
6730 dw_die_ref next = c->die_sib;
6732 /* This DIE is for a secondary CU; remove it from the main one. */
6733 remove_child_with_prev (c, prev);
6735 if (c->die_tag == DW_TAG_GNU_BINCL)
6736 unit = push_new_compile_unit (unit, c);
6737 else if (c->die_tag == DW_TAG_GNU_EINCL)
6738 unit = pop_compile_unit (unit);
6739 else
6740 add_child_die (unit, c);
6741 c = next;
6742 if (c == die->die_child)
6743 break;
6745 } while (c != die->die_child);
6747 #if 0
6748 /* We can only use this in debugging, since the frontend doesn't check
6749 to make sure that we leave every include file we enter. */
6750 gcc_assert (!unit);
6751 #endif
6753 assign_symbol_names (die);
6754 cu_hash_table.create (10);
6755 for (node = limbo_die_list, pnode = &limbo_die_list;
6756 node;
6757 node = node->next)
6759 int is_dupl;
6761 compute_section_prefix (node->die);
6762 is_dupl = check_duplicate_cu (node->die, cu_hash_table,
6763 &comdat_symbol_number);
6764 assign_symbol_names (node->die);
6765 if (is_dupl)
6766 *pnode = node->next;
6767 else
6769 pnode = &node->next;
6770 record_comdat_symbol_number (node->die, cu_hash_table,
6771 comdat_symbol_number);
6774 cu_hash_table.dispose ();
6777 /* Return non-zero if this DIE is a declaration. */
6779 static int
6780 is_declaration_die (dw_die_ref die)
6782 dw_attr_ref a;
6783 unsigned ix;
6785 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6786 if (a->dw_attr == DW_AT_declaration)
6787 return 1;
6789 return 0;
6792 /* Return non-zero if this DIE is nested inside a subprogram. */
6794 static int
6795 is_nested_in_subprogram (dw_die_ref die)
6797 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
6799 if (decl == NULL)
6800 decl = die;
6801 return local_scope_p (decl);
6804 /* Return non-zero if this DIE contains a defining declaration of a
6805 subprogram. */
6807 static int
6808 contains_subprogram_definition (dw_die_ref die)
6810 dw_die_ref c;
6812 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
6813 return 1;
6814 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
6815 return 0;
6818 /* Return non-zero if this is a type DIE that should be moved to a
6819 COMDAT .debug_types section. */
6821 static int
6822 should_move_die_to_comdat (dw_die_ref die)
6824 switch (die->die_tag)
6826 case DW_TAG_class_type:
6827 case DW_TAG_structure_type:
6828 case DW_TAG_enumeration_type:
6829 case DW_TAG_union_type:
6830 /* Don't move declarations, inlined instances, or types nested in a
6831 subprogram. */
6832 if (is_declaration_die (die)
6833 || get_AT (die, DW_AT_abstract_origin)
6834 || is_nested_in_subprogram (die))
6835 return 0;
6836 /* A type definition should never contain a subprogram definition. */
6837 gcc_assert (!contains_subprogram_definition (die));
6838 return 1;
6839 case DW_TAG_array_type:
6840 case DW_TAG_interface_type:
6841 case DW_TAG_pointer_type:
6842 case DW_TAG_reference_type:
6843 case DW_TAG_rvalue_reference_type:
6844 case DW_TAG_string_type:
6845 case DW_TAG_subroutine_type:
6846 case DW_TAG_ptr_to_member_type:
6847 case DW_TAG_set_type:
6848 case DW_TAG_subrange_type:
6849 case DW_TAG_base_type:
6850 case DW_TAG_const_type:
6851 case DW_TAG_file_type:
6852 case DW_TAG_packed_type:
6853 case DW_TAG_volatile_type:
6854 case DW_TAG_typedef:
6855 default:
6856 return 0;
6860 /* Make a clone of DIE. */
6862 static dw_die_ref
6863 clone_die (dw_die_ref die)
6865 dw_die_ref clone;
6866 dw_attr_ref a;
6867 unsigned ix;
6869 clone = ggc_alloc_cleared_die_node ();
6870 clone->die_tag = die->die_tag;
6872 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6873 add_dwarf_attr (clone, a);
6875 return clone;
6878 /* Make a clone of the tree rooted at DIE. */
6880 static dw_die_ref
6881 clone_tree (dw_die_ref die)
6883 dw_die_ref c;
6884 dw_die_ref clone = clone_die (die);
6886 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
6888 return clone;
6891 /* Make a clone of DIE as a declaration. */
6893 static dw_die_ref
6894 clone_as_declaration (dw_die_ref die)
6896 dw_die_ref clone;
6897 dw_die_ref decl;
6898 dw_attr_ref a;
6899 unsigned ix;
6901 /* If the DIE is already a declaration, just clone it. */
6902 if (is_declaration_die (die))
6903 return clone_die (die);
6905 /* If the DIE is a specification, just clone its declaration DIE. */
6906 decl = get_AT_ref (die, DW_AT_specification);
6907 if (decl != NULL)
6909 clone = clone_die (decl);
6910 if (die->comdat_type_p)
6911 add_AT_die_ref (clone, DW_AT_signature, die);
6912 return clone;
6915 clone = ggc_alloc_cleared_die_node ();
6916 clone->die_tag = die->die_tag;
6918 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6920 /* We don't want to copy over all attributes.
6921 For example we don't want DW_AT_byte_size because otherwise we will no
6922 longer have a declaration and GDB will treat it as a definition. */
6924 switch (a->dw_attr)
6926 case DW_AT_artificial:
6927 case DW_AT_containing_type:
6928 case DW_AT_external:
6929 case DW_AT_name:
6930 case DW_AT_type:
6931 case DW_AT_virtuality:
6932 case DW_AT_linkage_name:
6933 case DW_AT_MIPS_linkage_name:
6934 add_dwarf_attr (clone, a);
6935 break;
6936 case DW_AT_byte_size:
6937 default:
6938 break;
6942 if (die->comdat_type_p)
6943 add_AT_die_ref (clone, DW_AT_signature, die);
6945 add_AT_flag (clone, DW_AT_declaration, 1);
6946 return clone;
6950 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
6952 struct decl_table_entry
6954 dw_die_ref orig;
6955 dw_die_ref copy;
6958 /* Helpers to manipulate hash table of copied declarations. */
6960 /* Hashtable helpers. */
6962 struct decl_table_entry_hasher : typed_free_remove <decl_table_entry>
6964 typedef decl_table_entry value_type;
6965 typedef die_struct compare_type;
6966 static inline hashval_t hash (const value_type *);
6967 static inline bool equal (const value_type *, const compare_type *);
6970 inline hashval_t
6971 decl_table_entry_hasher::hash (const value_type *entry)
6973 return htab_hash_pointer (entry->orig);
6976 inline bool
6977 decl_table_entry_hasher::equal (const value_type *entry1,
6978 const compare_type *entry2)
6980 return entry1->orig == entry2;
6983 typedef hash_table <decl_table_entry_hasher> decl_hash_type;
6985 /* Copy DIE and its ancestors, up to, but not including, the compile unit
6986 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
6987 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
6988 to check if the ancestor has already been copied into UNIT. */
6990 static dw_die_ref
6991 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die, decl_hash_type decl_table)
6993 dw_die_ref parent = die->die_parent;
6994 dw_die_ref new_parent = unit;
6995 dw_die_ref copy;
6996 decl_table_entry **slot = NULL;
6997 struct decl_table_entry *entry = NULL;
6999 if (decl_table.is_created ())
7001 /* Check if the entry has already been copied to UNIT. */
7002 slot = decl_table.find_slot_with_hash (die, htab_hash_pointer (die),
7003 INSERT);
7004 if (*slot != HTAB_EMPTY_ENTRY)
7006 entry = *slot;
7007 return entry->copy;
7010 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
7011 entry = XCNEW (struct decl_table_entry);
7012 entry->orig = die;
7013 entry->copy = NULL;
7014 *slot = entry;
7017 if (parent != NULL)
7019 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7020 if (spec != NULL)
7021 parent = spec;
7022 if (!is_unit_die (parent))
7023 new_parent = copy_ancestor_tree (unit, parent, decl_table);
7026 copy = clone_as_declaration (die);
7027 add_child_die (new_parent, copy);
7029 if (decl_table.is_created ())
7031 /* Record the pointer to the copy. */
7032 entry->copy = copy;
7035 return copy;
7037 /* Copy the declaration context to the new type unit DIE. This includes
7038 any surrounding namespace or type declarations. If the DIE has an
7039 AT_specification attribute, it also includes attributes and children
7040 attached to the specification, and returns a pointer to the original
7041 parent of the declaration DIE. Returns NULL otherwise. */
7043 static dw_die_ref
7044 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7046 dw_die_ref decl;
7047 dw_die_ref new_decl;
7048 dw_die_ref orig_parent = NULL;
7050 decl = get_AT_ref (die, DW_AT_specification);
7051 if (decl == NULL)
7052 decl = die;
7053 else
7055 unsigned ix;
7056 dw_die_ref c;
7057 dw_attr_ref a;
7059 /* The original DIE will be changed to a declaration, and must
7060 be moved to be a child of the original declaration DIE. */
7061 orig_parent = decl->die_parent;
7063 /* Copy the type node pointer from the new DIE to the original
7064 declaration DIE so we can forward references later. */
7065 decl->comdat_type_p = true;
7066 decl->die_id.die_type_node = die->die_id.die_type_node;
7068 remove_AT (die, DW_AT_specification);
7070 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7072 if (a->dw_attr != DW_AT_name
7073 && a->dw_attr != DW_AT_declaration
7074 && a->dw_attr != DW_AT_external)
7075 add_dwarf_attr (die, a);
7078 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7081 if (decl->die_parent != NULL
7082 && !is_unit_die (decl->die_parent))
7084 new_decl = copy_ancestor_tree (unit, decl, decl_hash_type ());
7085 if (new_decl != NULL)
7087 remove_AT (new_decl, DW_AT_signature);
7088 add_AT_specification (die, new_decl);
7092 return orig_parent;
7095 /* Generate the skeleton ancestor tree for the given NODE, then clone
7096 the DIE and add the clone into the tree. */
7098 static void
7099 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7101 if (node->new_die != NULL)
7102 return;
7104 node->new_die = clone_as_declaration (node->old_die);
7106 if (node->parent != NULL)
7108 generate_skeleton_ancestor_tree (node->parent);
7109 add_child_die (node->parent->new_die, node->new_die);
7113 /* Generate a skeleton tree of DIEs containing any declarations that are
7114 found in the original tree. We traverse the tree looking for declaration
7115 DIEs, and construct the skeleton from the bottom up whenever we find one. */
7117 static void
7118 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7120 skeleton_chain_node node;
7121 dw_die_ref c;
7122 dw_die_ref first;
7123 dw_die_ref prev = NULL;
7124 dw_die_ref next = NULL;
7126 node.parent = parent;
7128 first = c = parent->old_die->die_child;
7129 if (c)
7130 next = c->die_sib;
7131 if (c) do {
7132 if (prev == NULL || prev->die_sib == c)
7133 prev = c;
7134 c = next;
7135 next = (c == first ? NULL : c->die_sib);
7136 node.old_die = c;
7137 node.new_die = NULL;
7138 if (is_declaration_die (c))
7140 if (is_template_instantiation (c))
7142 /* Instantiated templates do not need to be cloned into the
7143 type unit. Just move the DIE and its children back to
7144 the skeleton tree (in the main CU). */
7145 remove_child_with_prev (c, prev);
7146 add_child_die (parent->new_die, c);
7147 c = prev;
7149 else
7151 /* Clone the existing DIE, move the original to the skeleton
7152 tree (which is in the main CU), and put the clone, with
7153 all the original's children, where the original came from
7154 (which is about to be moved to the type unit). */
7155 dw_die_ref clone = clone_die (c);
7156 move_all_children (c, clone);
7158 replace_child (c, clone, prev);
7159 generate_skeleton_ancestor_tree (parent);
7160 add_child_die (parent->new_die, c);
7161 node.new_die = c;
7162 c = clone;
7165 generate_skeleton_bottom_up (&node);
7166 } while (next != NULL);
7169 /* Wrapper function for generate_skeleton_bottom_up. */
7171 static dw_die_ref
7172 generate_skeleton (dw_die_ref die)
7174 skeleton_chain_node node;
7176 node.old_die = die;
7177 node.new_die = NULL;
7178 node.parent = NULL;
7180 /* If this type definition is nested inside another type,
7181 and is not an instantiation of a template, always leave
7182 at least a declaration in its place. */
7183 if (die->die_parent != NULL
7184 && is_type_die (die->die_parent)
7185 && !is_template_instantiation (die))
7186 node.new_die = clone_as_declaration (die);
7188 generate_skeleton_bottom_up (&node);
7189 return node.new_die;
7192 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
7193 declaration. The original DIE is moved to a new compile unit so that
7194 existing references to it follow it to the new location. If any of the
7195 original DIE's descendants is a declaration, we need to replace the
7196 original DIE with a skeleton tree and move the declarations back into the
7197 skeleton tree. */
7199 static dw_die_ref
7200 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
7201 dw_die_ref prev)
7203 dw_die_ref skeleton, orig_parent;
7205 /* Copy the declaration context to the type unit DIE. If the returned
7206 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
7207 that DIE. */
7208 orig_parent = copy_declaration_context (unit, child);
7210 skeleton = generate_skeleton (child);
7211 if (skeleton == NULL)
7212 remove_child_with_prev (child, prev);
7213 else
7215 skeleton->comdat_type_p = true;
7216 skeleton->die_id.die_type_node = child->die_id.die_type_node;
7218 /* If the original DIE was a specification, we need to put
7219 the skeleton under the parent DIE of the declaration.
7220 This leaves the original declaration in the tree, but
7221 it will be pruned later since there are no longer any
7222 references to it. */
7223 if (orig_parent != NULL)
7225 remove_child_with_prev (child, prev);
7226 add_child_die (orig_parent, skeleton);
7228 else
7229 replace_child (child, skeleton, prev);
7232 return skeleton;
7235 /* Traverse the DIE and set up additional .debug_types sections for each
7236 type worthy of being placed in a COMDAT section. */
7238 static void
7239 break_out_comdat_types (dw_die_ref die)
7241 dw_die_ref c;
7242 dw_die_ref first;
7243 dw_die_ref prev = NULL;
7244 dw_die_ref next = NULL;
7245 dw_die_ref unit = NULL;
7247 first = c = die->die_child;
7248 if (c)
7249 next = c->die_sib;
7250 if (c) do {
7251 if (prev == NULL || prev->die_sib == c)
7252 prev = c;
7253 c = next;
7254 next = (c == first ? NULL : c->die_sib);
7255 if (should_move_die_to_comdat (c))
7257 dw_die_ref replacement;
7258 comdat_type_node_ref type_node;
7260 /* Break out nested types into their own type units. */
7261 break_out_comdat_types (c);
7263 /* Create a new type unit DIE as the root for the new tree, and
7264 add it to the list of comdat types. */
7265 unit = new_die (DW_TAG_type_unit, NULL, NULL);
7266 add_AT_unsigned (unit, DW_AT_language,
7267 get_AT_unsigned (comp_unit_die (), DW_AT_language));
7268 type_node = ggc_alloc_cleared_comdat_type_node ();
7269 type_node->root_die = unit;
7270 type_node->next = comdat_type_list;
7271 comdat_type_list = type_node;
7273 /* Generate the type signature. */
7274 generate_type_signature (c, type_node);
7276 /* Copy the declaration context, attributes, and children of the
7277 declaration into the new type unit DIE, then remove this DIE
7278 from the main CU (or replace it with a skeleton if necessary). */
7279 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
7280 type_node->skeleton_die = replacement;
7282 /* Add the DIE to the new compunit. */
7283 add_child_die (unit, c);
7285 if (replacement != NULL)
7286 c = replacement;
7288 else if (c->die_tag == DW_TAG_namespace
7289 || c->die_tag == DW_TAG_class_type
7290 || c->die_tag == DW_TAG_structure_type
7291 || c->die_tag == DW_TAG_union_type)
7293 /* Look for nested types that can be broken out. */
7294 break_out_comdat_types (c);
7296 } while (next != NULL);
7299 /* Like clone_tree, but additionally enter all the children into
7300 the hash table decl_table. */
7302 static dw_die_ref
7303 clone_tree_hash (dw_die_ref die, decl_hash_type decl_table)
7305 dw_die_ref c;
7306 dw_die_ref clone = clone_die (die);
7307 struct decl_table_entry *entry;
7308 decl_table_entry **slot = decl_table.find_slot_with_hash (die,
7309 htab_hash_pointer (die), INSERT);
7310 /* Assert that DIE isn't in the hash table yet. If it would be there
7311 before, the ancestors would be necessarily there as well, therefore
7312 clone_tree_hash wouldn't be called. */
7313 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
7314 entry = XCNEW (struct decl_table_entry);
7315 entry->orig = die;
7316 entry->copy = clone;
7317 *slot = entry;
7319 FOR_EACH_CHILD (die, c,
7320 add_child_die (clone, clone_tree_hash (c, decl_table)));
7322 return clone;
7325 /* Walk the DIE and its children, looking for references to incomplete
7326 or trivial types that are unmarked (i.e., that are not in the current
7327 type_unit). */
7329 static void
7330 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type decl_table)
7332 dw_die_ref c;
7333 dw_attr_ref a;
7334 unsigned ix;
7336 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7338 if (AT_class (a) == dw_val_class_die_ref)
7340 dw_die_ref targ = AT_ref (a);
7341 decl_table_entry **slot;
7342 struct decl_table_entry *entry;
7344 if (targ->die_mark != 0 || targ->comdat_type_p)
7345 continue;
7347 slot = decl_table.find_slot_with_hash (targ, htab_hash_pointer (targ),
7348 INSERT);
7350 if (*slot != HTAB_EMPTY_ENTRY)
7352 /* TARG has already been copied, so we just need to
7353 modify the reference to point to the copy. */
7354 entry = *slot;
7355 a->dw_attr_val.v.val_die_ref.die = entry->copy;
7357 else
7359 dw_die_ref parent = unit;
7360 dw_die_ref copy = clone_die (targ);
7362 /* Record in DECL_TABLE that TARG has been copied.
7363 Need to do this now, before the recursive call,
7364 because DECL_TABLE may be expanded and SLOT
7365 would no longer be a valid pointer. */
7366 entry = XCNEW (struct decl_table_entry);
7367 entry->orig = targ;
7368 entry->copy = copy;
7369 *slot = entry;
7371 FOR_EACH_CHILD (targ, c,
7372 add_child_die (copy,
7373 clone_tree_hash (c, decl_table)));
7375 /* Make sure the cloned tree is marked as part of the
7376 type unit. */
7377 mark_dies (copy);
7379 /* If TARG has surrounding context, copy its ancestor tree
7380 into the new type unit. */
7381 if (targ->die_parent != NULL
7382 && !is_unit_die (targ->die_parent))
7383 parent = copy_ancestor_tree (unit, targ->die_parent,
7384 decl_table);
7386 add_child_die (parent, copy);
7387 a->dw_attr_val.v.val_die_ref.die = copy;
7389 /* Make sure the newly-copied DIE is walked. If it was
7390 installed in a previously-added context, it won't
7391 get visited otherwise. */
7392 if (parent != unit)
7394 /* Find the highest point of the newly-added tree,
7395 mark each node along the way, and walk from there. */
7396 parent->die_mark = 1;
7397 while (parent->die_parent
7398 && parent->die_parent->die_mark == 0)
7400 parent = parent->die_parent;
7401 parent->die_mark = 1;
7403 copy_decls_walk (unit, parent, decl_table);
7409 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
7412 /* Copy declarations for "unworthy" types into the new comdat section.
7413 Incomplete types, modified types, and certain other types aren't broken
7414 out into comdat sections of their own, so they don't have a signature,
7415 and we need to copy the declaration into the same section so that we
7416 don't have an external reference. */
7418 static void
7419 copy_decls_for_unworthy_types (dw_die_ref unit)
7421 decl_hash_type decl_table;
7423 mark_dies (unit);
7424 decl_table.create (10);
7425 copy_decls_walk (unit, unit, decl_table);
7426 decl_table.dispose ();
7427 unmark_dies (unit);
7430 /* Traverse the DIE and add a sibling attribute if it may have the
7431 effect of speeding up access to siblings. To save some space,
7432 avoid generating sibling attributes for DIE's without children. */
7434 static void
7435 add_sibling_attributes (dw_die_ref die)
7437 dw_die_ref c;
7439 if (! die->die_child)
7440 return;
7442 if (die->die_parent && die != die->die_parent->die_child)
7443 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7445 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7448 /* Output all location lists for the DIE and its children. */
7450 static void
7451 output_location_lists (dw_die_ref die)
7453 dw_die_ref c;
7454 dw_attr_ref a;
7455 unsigned ix;
7457 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7458 if (AT_class (a) == dw_val_class_loc_list)
7459 output_loc_list (AT_loc_list (a));
7461 FOR_EACH_CHILD (die, c, output_location_lists (c));
7464 /* We want to limit the number of external references, because they are
7465 larger than local references: a relocation takes multiple words, and
7466 even a sig8 reference is always eight bytes, whereas a local reference
7467 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
7468 So if we encounter multiple external references to the same type DIE, we
7469 make a local typedef stub for it and redirect all references there.
7471 This is the element of the hash table for keeping track of these
7472 references. */
7474 struct external_ref
7476 dw_die_ref type;
7477 dw_die_ref stub;
7478 unsigned n_refs;
7481 /* Hashtable helpers. */
7483 struct external_ref_hasher : typed_free_remove <external_ref>
7485 typedef external_ref value_type;
7486 typedef external_ref compare_type;
7487 static inline hashval_t hash (const value_type *);
7488 static inline bool equal (const value_type *, const compare_type *);
7491 inline hashval_t
7492 external_ref_hasher::hash (const value_type *r)
7494 dw_die_ref die = r->type;
7495 hashval_t h = 0;
7497 /* We can't use the address of the DIE for hashing, because
7498 that will make the order of the stub DIEs non-deterministic. */
7499 if (! die->comdat_type_p)
7500 /* We have a symbol; use it to compute a hash. */
7501 h = htab_hash_string (die->die_id.die_symbol);
7502 else
7504 /* We have a type signature; use a subset of the bits as the hash.
7505 The 8-byte signature is at least as large as hashval_t. */
7506 comdat_type_node_ref type_node = die->die_id.die_type_node;
7507 memcpy (&h, type_node->signature, sizeof (h));
7509 return h;
7512 inline bool
7513 external_ref_hasher::equal (const value_type *r1, const compare_type *r2)
7515 return r1->type == r2->type;
7518 typedef hash_table <external_ref_hasher> external_ref_hash_type;
7520 /* Return a pointer to the external_ref for references to DIE. */
7522 static struct external_ref *
7523 lookup_external_ref (external_ref_hash_type map, dw_die_ref die)
7525 struct external_ref ref, *ref_p;
7526 external_ref **slot;
7528 ref.type = die;
7529 slot = map.find_slot (&ref, INSERT);
7530 if (*slot != HTAB_EMPTY_ENTRY)
7531 return *slot;
7533 ref_p = XCNEW (struct external_ref);
7534 ref_p->type = die;
7535 *slot = ref_p;
7536 return ref_p;
7539 /* Subroutine of optimize_external_refs, below.
7541 If we see a type skeleton, record it as our stub. If we see external
7542 references, remember how many we've seen. */
7544 static void
7545 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type map)
7547 dw_die_ref c;
7548 dw_attr_ref a;
7549 unsigned ix;
7550 struct external_ref *ref_p;
7552 if (is_type_die (die)
7553 && (c = get_AT_ref (die, DW_AT_signature)))
7555 /* This is a local skeleton; use it for local references. */
7556 ref_p = lookup_external_ref (map, c);
7557 ref_p->stub = die;
7560 /* Scan the DIE references, and remember any that refer to DIEs from
7561 other CUs (i.e. those which are not marked). */
7562 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7563 if (AT_class (a) == dw_val_class_die_ref
7564 && (c = AT_ref (a))->die_mark == 0
7565 && is_type_die (c))
7567 ref_p = lookup_external_ref (map, c);
7568 ref_p->n_refs++;
7571 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
7574 /* htab_traverse callback function for optimize_external_refs, below. SLOT
7575 points to an external_ref, DATA is the CU we're processing. If we don't
7576 already have a local stub, and we have multiple refs, build a stub. */
7579 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
7581 struct external_ref *ref_p = *slot;
7583 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
7585 /* We have multiple references to this type, so build a small stub.
7586 Both of these forms are a bit dodgy from the perspective of the
7587 DWARF standard, since technically they should have names. */
7588 dw_die_ref cu = data;
7589 dw_die_ref type = ref_p->type;
7590 dw_die_ref stub = NULL;
7592 if (type->comdat_type_p)
7594 /* If we refer to this type via sig8, use AT_signature. */
7595 stub = new_die (type->die_tag, cu, NULL_TREE);
7596 add_AT_die_ref (stub, DW_AT_signature, type);
7598 else
7600 /* Otherwise, use a typedef with no name. */
7601 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
7602 add_AT_die_ref (stub, DW_AT_type, type);
7605 stub->die_mark++;
7606 ref_p->stub = stub;
7608 return 1;
7611 /* DIE is a unit; look through all the DIE references to see if there are
7612 any external references to types, and if so, create local stubs for
7613 them which will be applied in build_abbrev_table. This is useful because
7614 references to local DIEs are smaller. */
7616 static external_ref_hash_type
7617 optimize_external_refs (dw_die_ref die)
7619 external_ref_hash_type map;
7620 map.create (10);
7621 optimize_external_refs_1 (die, map);
7622 map.traverse <dw_die_ref, dwarf2_build_local_stub> (die);
7623 return map;
7626 /* The format of each DIE (and its attribute value pairs) is encoded in an
7627 abbreviation table. This routine builds the abbreviation table and assigns
7628 a unique abbreviation id for each abbreviation entry. The children of each
7629 die are visited recursively. */
7631 static void
7632 build_abbrev_table (dw_die_ref die, external_ref_hash_type extern_map)
7634 unsigned long abbrev_id;
7635 unsigned int n_alloc;
7636 dw_die_ref c;
7637 dw_attr_ref a;
7638 unsigned ix;
7640 /* Scan the DIE references, and replace any that refer to
7641 DIEs from other CUs (i.e. those which are not marked) with
7642 the local stubs we built in optimize_external_refs. */
7643 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7644 if (AT_class (a) == dw_val_class_die_ref
7645 && (c = AT_ref (a))->die_mark == 0)
7647 struct external_ref *ref_p;
7648 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
7650 ref_p = lookup_external_ref (extern_map, c);
7651 if (ref_p->stub && ref_p->stub != die)
7652 change_AT_die_ref (a, ref_p->stub);
7653 else
7654 /* We aren't changing this reference, so mark it external. */
7655 set_AT_ref_external (a, 1);
7658 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7660 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7661 dw_attr_ref die_a, abbrev_a;
7662 unsigned ix;
7663 bool ok = true;
7665 if (abbrev->die_tag != die->die_tag)
7666 continue;
7667 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7668 continue;
7670 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
7671 continue;
7673 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
7675 abbrev_a = &(*abbrev->die_attr)[ix];
7676 if ((abbrev_a->dw_attr != die_a->dw_attr)
7677 || (value_format (abbrev_a) != value_format (die_a)))
7679 ok = false;
7680 break;
7683 if (ok)
7684 break;
7687 if (abbrev_id >= abbrev_die_table_in_use)
7689 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7691 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
7692 abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7693 n_alloc);
7695 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7696 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
7697 abbrev_die_table_allocated = n_alloc;
7700 ++abbrev_die_table_in_use;
7701 abbrev_die_table[abbrev_id] = die;
7704 die->die_abbrev = abbrev_id;
7705 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
7708 /* Return the power-of-two number of bytes necessary to represent VALUE. */
7710 static int
7711 constant_size (unsigned HOST_WIDE_INT value)
7713 int log;
7715 if (value == 0)
7716 log = 0;
7717 else
7718 log = floor_log2 (value);
7720 log = log / 8;
7721 log = 1 << (floor_log2 (log) + 1);
7723 return log;
7726 /* Return the size of a DIE as it is represented in the
7727 .debug_info section. */
7729 static unsigned long
7730 size_of_die (dw_die_ref die)
7732 unsigned long size = 0;
7733 dw_attr_ref a;
7734 unsigned ix;
7735 enum dwarf_form form;
7737 size += size_of_uleb128 (die->die_abbrev);
7738 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7740 switch (AT_class (a))
7742 case dw_val_class_addr:
7743 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7745 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7746 size += size_of_uleb128 (AT_index (a));
7748 else
7749 size += DWARF2_ADDR_SIZE;
7750 break;
7751 case dw_val_class_offset:
7752 size += DWARF_OFFSET_SIZE;
7753 break;
7754 case dw_val_class_loc:
7756 unsigned long lsize = size_of_locs (AT_loc (a));
7758 /* Block length. */
7759 if (dwarf_version >= 4)
7760 size += size_of_uleb128 (lsize);
7761 else
7762 size += constant_size (lsize);
7763 size += lsize;
7765 break;
7766 case dw_val_class_loc_list:
7767 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7769 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7770 size += size_of_uleb128 (AT_index (a));
7772 else
7773 size += DWARF_OFFSET_SIZE;
7774 break;
7775 case dw_val_class_range_list:
7776 size += DWARF_OFFSET_SIZE;
7777 break;
7778 case dw_val_class_const:
7779 size += size_of_sleb128 (AT_int (a));
7780 break;
7781 case dw_val_class_unsigned_const:
7783 int csize = constant_size (AT_unsigned (a));
7784 if (dwarf_version == 3
7785 && a->dw_attr == DW_AT_data_member_location
7786 && csize >= 4)
7787 size += size_of_uleb128 (AT_unsigned (a));
7788 else
7789 size += csize;
7791 break;
7792 case dw_val_class_const_double:
7793 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
7794 if (HOST_BITS_PER_WIDE_INT >= 64)
7795 size++; /* block */
7796 break;
7797 case dw_val_class_vec:
7798 size += constant_size (a->dw_attr_val.v.val_vec.length
7799 * a->dw_attr_val.v.val_vec.elt_size)
7800 + a->dw_attr_val.v.val_vec.length
7801 * a->dw_attr_val.v.val_vec.elt_size; /* block */
7802 break;
7803 case dw_val_class_flag:
7804 if (dwarf_version >= 4)
7805 /* Currently all add_AT_flag calls pass in 1 as last argument,
7806 so DW_FORM_flag_present can be used. If that ever changes,
7807 we'll need to use DW_FORM_flag and have some optimization
7808 in build_abbrev_table that will change those to
7809 DW_FORM_flag_present if it is set to 1 in all DIEs using
7810 the same abbrev entry. */
7811 gcc_assert (a->dw_attr_val.v.val_flag == 1);
7812 else
7813 size += 1;
7814 break;
7815 case dw_val_class_die_ref:
7816 if (AT_ref_external (a))
7818 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
7819 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
7820 is sized by target address length, whereas in DWARF3
7821 it's always sized as an offset. */
7822 if (use_debug_types)
7823 size += DWARF_TYPE_SIGNATURE_SIZE;
7824 else if (dwarf_version == 2)
7825 size += DWARF2_ADDR_SIZE;
7826 else
7827 size += DWARF_OFFSET_SIZE;
7829 else
7830 size += DWARF_OFFSET_SIZE;
7831 break;
7832 case dw_val_class_fde_ref:
7833 size += DWARF_OFFSET_SIZE;
7834 break;
7835 case dw_val_class_lbl_id:
7836 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7838 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7839 size += size_of_uleb128 (AT_index (a));
7841 else
7842 size += DWARF2_ADDR_SIZE;
7843 break;
7844 case dw_val_class_lineptr:
7845 case dw_val_class_macptr:
7846 size += DWARF_OFFSET_SIZE;
7847 break;
7848 case dw_val_class_str:
7849 form = AT_string_form (a);
7850 if (form == DW_FORM_strp)
7851 size += DWARF_OFFSET_SIZE;
7852 else if (form == DW_FORM_GNU_str_index)
7853 size += size_of_uleb128 (AT_index (a));
7854 else
7855 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
7856 break;
7857 case dw_val_class_file:
7858 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
7859 break;
7860 case dw_val_class_data8:
7861 size += 8;
7862 break;
7863 case dw_val_class_vms_delta:
7864 size += DWARF_OFFSET_SIZE;
7865 break;
7866 case dw_val_class_high_pc:
7867 size += DWARF2_ADDR_SIZE;
7868 break;
7869 default:
7870 gcc_unreachable ();
7874 return size;
7877 /* Size the debugging information associated with a given DIE. Visits the
7878 DIE's children recursively. Updates the global variable next_die_offset, on
7879 each time through. Uses the current value of next_die_offset to update the
7880 die_offset field in each DIE. */
7882 static void
7883 calc_die_sizes (dw_die_ref die)
7885 dw_die_ref c;
7887 gcc_assert (die->die_offset == 0
7888 || (unsigned long int) die->die_offset == next_die_offset);
7889 die->die_offset = next_die_offset;
7890 next_die_offset += size_of_die (die);
7892 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
7894 if (die->die_child != NULL)
7895 /* Count the null byte used to terminate sibling lists. */
7896 next_die_offset += 1;
7899 /* Size just the base type children at the start of the CU.
7900 This is needed because build_abbrev needs to size locs
7901 and sizing of type based stack ops needs to know die_offset
7902 values for the base types. */
7904 static void
7905 calc_base_type_die_sizes (void)
7907 unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
7908 unsigned int i;
7909 dw_die_ref base_type;
7910 #if ENABLE_ASSERT_CHECKING
7911 dw_die_ref prev = comp_unit_die ()->die_child;
7912 #endif
7914 die_offset += size_of_die (comp_unit_die ());
7915 for (i = 0; base_types.iterate (i, &base_type); i++)
7917 #if ENABLE_ASSERT_CHECKING
7918 gcc_assert (base_type->die_offset == 0
7919 && prev->die_sib == base_type
7920 && base_type->die_child == NULL
7921 && base_type->die_abbrev);
7922 prev = base_type;
7923 #endif
7924 base_type->die_offset = die_offset;
7925 die_offset += size_of_die (base_type);
7929 /* Set the marks for a die and its children. We do this so
7930 that we know whether or not a reference needs to use FORM_ref_addr; only
7931 DIEs in the same CU will be marked. We used to clear out the offset
7932 and use that as the flag, but ran into ordering problems. */
7934 static void
7935 mark_dies (dw_die_ref die)
7937 dw_die_ref c;
7939 gcc_assert (!die->die_mark);
7941 die->die_mark = 1;
7942 FOR_EACH_CHILD (die, c, mark_dies (c));
7945 /* Clear the marks for a die and its children. */
7947 static void
7948 unmark_dies (dw_die_ref die)
7950 dw_die_ref c;
7952 if (! use_debug_types)
7953 gcc_assert (die->die_mark);
7955 die->die_mark = 0;
7956 FOR_EACH_CHILD (die, c, unmark_dies (c));
7959 /* Clear the marks for a die, its children and referred dies. */
7961 static void
7962 unmark_all_dies (dw_die_ref die)
7964 dw_die_ref c;
7965 dw_attr_ref a;
7966 unsigned ix;
7968 if (!die->die_mark)
7969 return;
7970 die->die_mark = 0;
7972 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
7974 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7975 if (AT_class (a) == dw_val_class_die_ref)
7976 unmark_all_dies (AT_ref (a));
7979 /* Calculate if the entry should appear in the final output file. It may be
7980 from a pruned a type. */
7982 static bool
7983 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
7985 /* By limiting gnu pubnames to definitions only, gold can generate a
7986 gdb index without entries for declarations, which don't include
7987 enough information to be useful. */
7988 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
7989 return false;
7991 if (table == pubname_table)
7993 /* Enumerator names are part of the pubname table, but the
7994 parent DW_TAG_enumeration_type die may have been pruned.
7995 Don't output them if that is the case. */
7996 if (p->die->die_tag == DW_TAG_enumerator &&
7997 (p->die->die_parent == NULL
7998 || !p->die->die_parent->die_perennial_p))
7999 return false;
8001 /* Everything else in the pubname table is included. */
8002 return true;
8005 /* The pubtypes table shouldn't include types that have been
8006 pruned. */
8007 return (p->die->die_offset != 0
8008 || !flag_eliminate_unused_debug_types);
8011 /* Return the size of the .debug_pubnames or .debug_pubtypes table
8012 generated for the compilation unit. */
8014 static unsigned long
8015 size_of_pubnames (vec<pubname_entry, va_gc> *names)
8017 unsigned long size;
8018 unsigned i;
8019 pubname_ref p;
8020 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
8022 size = DWARF_PUBNAMES_HEADER_SIZE;
8023 FOR_EACH_VEC_ELT (*names, i, p)
8024 if (include_pubname_in_output (names, p))
8025 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
8027 size += DWARF_OFFSET_SIZE;
8028 return size;
8031 /* Return the size of the information in the .debug_aranges section. */
8033 static unsigned long
8034 size_of_aranges (void)
8036 unsigned long size;
8038 size = DWARF_ARANGES_HEADER_SIZE;
8040 /* Count the address/length pair for this compilation unit. */
8041 if (text_section_used)
8042 size += 2 * DWARF2_ADDR_SIZE;
8043 if (cold_text_section_used)
8044 size += 2 * DWARF2_ADDR_SIZE;
8045 if (have_multiple_function_sections)
8047 unsigned fde_idx;
8048 dw_fde_ref fde;
8050 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
8052 if (DECL_IGNORED_P (fde->decl))
8053 continue;
8054 if (!fde->in_std_section)
8055 size += 2 * DWARF2_ADDR_SIZE;
8056 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
8057 size += 2 * DWARF2_ADDR_SIZE;
8061 /* Count the two zero words used to terminated the address range table. */
8062 size += 2 * DWARF2_ADDR_SIZE;
8063 return size;
8066 /* Select the encoding of an attribute value. */
8068 static enum dwarf_form
8069 value_format (dw_attr_ref a)
8071 switch (AT_class (a))
8073 case dw_val_class_addr:
8074 /* Only very few attributes allow DW_FORM_addr. */
8075 switch (a->dw_attr)
8077 case DW_AT_low_pc:
8078 case DW_AT_high_pc:
8079 case DW_AT_entry_pc:
8080 case DW_AT_trampoline:
8081 return (AT_index (a) == NOT_INDEXED
8082 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8083 default:
8084 break;
8086 switch (DWARF2_ADDR_SIZE)
8088 case 1:
8089 return DW_FORM_data1;
8090 case 2:
8091 return DW_FORM_data2;
8092 case 4:
8093 return DW_FORM_data4;
8094 case 8:
8095 return DW_FORM_data8;
8096 default:
8097 gcc_unreachable ();
8099 case dw_val_class_range_list:
8100 case dw_val_class_loc_list:
8101 if (dwarf_version >= 4)
8102 return DW_FORM_sec_offset;
8103 /* FALLTHRU */
8104 case dw_val_class_vms_delta:
8105 case dw_val_class_offset:
8106 switch (DWARF_OFFSET_SIZE)
8108 case 4:
8109 return DW_FORM_data4;
8110 case 8:
8111 return DW_FORM_data8;
8112 default:
8113 gcc_unreachable ();
8115 case dw_val_class_loc:
8116 if (dwarf_version >= 4)
8117 return DW_FORM_exprloc;
8118 switch (constant_size (size_of_locs (AT_loc (a))))
8120 case 1:
8121 return DW_FORM_block1;
8122 case 2:
8123 return DW_FORM_block2;
8124 case 4:
8125 return DW_FORM_block4;
8126 default:
8127 gcc_unreachable ();
8129 case dw_val_class_const:
8130 return DW_FORM_sdata;
8131 case dw_val_class_unsigned_const:
8132 switch (constant_size (AT_unsigned (a)))
8134 case 1:
8135 return DW_FORM_data1;
8136 case 2:
8137 return DW_FORM_data2;
8138 case 4:
8139 /* In DWARF3 DW_AT_data_member_location with
8140 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
8141 constant, so we need to use DW_FORM_udata if we need
8142 a large constant. */
8143 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8144 return DW_FORM_udata;
8145 return DW_FORM_data4;
8146 case 8:
8147 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8148 return DW_FORM_udata;
8149 return DW_FORM_data8;
8150 default:
8151 gcc_unreachable ();
8153 case dw_val_class_const_double:
8154 switch (HOST_BITS_PER_WIDE_INT)
8156 case 8:
8157 return DW_FORM_data2;
8158 case 16:
8159 return DW_FORM_data4;
8160 case 32:
8161 return DW_FORM_data8;
8162 case 64:
8163 default:
8164 return DW_FORM_block1;
8166 case dw_val_class_vec:
8167 switch (constant_size (a->dw_attr_val.v.val_vec.length
8168 * a->dw_attr_val.v.val_vec.elt_size))
8170 case 1:
8171 return DW_FORM_block1;
8172 case 2:
8173 return DW_FORM_block2;
8174 case 4:
8175 return DW_FORM_block4;
8176 default:
8177 gcc_unreachable ();
8179 case dw_val_class_flag:
8180 if (dwarf_version >= 4)
8182 /* Currently all add_AT_flag calls pass in 1 as last argument,
8183 so DW_FORM_flag_present can be used. If that ever changes,
8184 we'll need to use DW_FORM_flag and have some optimization
8185 in build_abbrev_table that will change those to
8186 DW_FORM_flag_present if it is set to 1 in all DIEs using
8187 the same abbrev entry. */
8188 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8189 return DW_FORM_flag_present;
8191 return DW_FORM_flag;
8192 case dw_val_class_die_ref:
8193 if (AT_ref_external (a))
8194 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
8195 else
8196 return DW_FORM_ref;
8197 case dw_val_class_fde_ref:
8198 return DW_FORM_data;
8199 case dw_val_class_lbl_id:
8200 return (AT_index (a) == NOT_INDEXED
8201 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8202 case dw_val_class_lineptr:
8203 case dw_val_class_macptr:
8204 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
8205 case dw_val_class_str:
8206 return AT_string_form (a);
8207 case dw_val_class_file:
8208 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8210 case 1:
8211 return DW_FORM_data1;
8212 case 2:
8213 return DW_FORM_data2;
8214 case 4:
8215 return DW_FORM_data4;
8216 default:
8217 gcc_unreachable ();
8220 case dw_val_class_data8:
8221 return DW_FORM_data8;
8223 case dw_val_class_high_pc:
8224 switch (DWARF2_ADDR_SIZE)
8226 case 1:
8227 return DW_FORM_data1;
8228 case 2:
8229 return DW_FORM_data2;
8230 case 4:
8231 return DW_FORM_data4;
8232 case 8:
8233 return DW_FORM_data8;
8234 default:
8235 gcc_unreachable ();
8238 default:
8239 gcc_unreachable ();
8243 /* Output the encoding of an attribute value. */
8245 static void
8246 output_value_format (dw_attr_ref a)
8248 enum dwarf_form form = value_format (a);
8250 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8253 /* Given a die and id, produce the appropriate abbreviations. */
8255 static void
8256 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
8258 unsigned ix;
8259 dw_attr_ref a_attr;
8261 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8262 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8263 dwarf_tag_name (abbrev->die_tag));
8265 if (abbrev->die_child != NULL)
8266 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8267 else
8268 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8270 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
8272 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8273 dwarf_attr_name (a_attr->dw_attr));
8274 output_value_format (a_attr);
8277 dw2_asm_output_data (1, 0, NULL);
8278 dw2_asm_output_data (1, 0, NULL);
8282 /* Output the .debug_abbrev section which defines the DIE abbreviation
8283 table. */
8285 static void
8286 output_abbrev_section (void)
8288 unsigned long abbrev_id;
8290 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8291 output_die_abbrevs (abbrev_id, abbrev_die_table[abbrev_id]);
8293 /* Terminate the table. */
8294 dw2_asm_output_data (1, 0, NULL);
8297 /* Output a symbol we can use to refer to this DIE from another CU. */
8299 static inline void
8300 output_die_symbol (dw_die_ref die)
8302 const char *sym = die->die_id.die_symbol;
8304 gcc_assert (!die->comdat_type_p);
8306 if (sym == 0)
8307 return;
8309 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8310 /* We make these global, not weak; if the target doesn't support
8311 .linkonce, it doesn't support combining the sections, so debugging
8312 will break. */
8313 targetm.asm_out.globalize_label (asm_out_file, sym);
8315 ASM_OUTPUT_LABEL (asm_out_file, sym);
8318 /* Return a new location list, given the begin and end range, and the
8319 expression. */
8321 static inline dw_loc_list_ref
8322 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8323 const char *section)
8325 dw_loc_list_ref retlist = ggc_alloc_cleared_dw_loc_list_node ();
8327 retlist->begin = begin;
8328 retlist->begin_entry = NULL;
8329 retlist->end = end;
8330 retlist->expr = expr;
8331 retlist->section = section;
8333 return retlist;
8336 /* Generate a new internal symbol for this location list node, if it
8337 hasn't got one yet. */
8339 static inline void
8340 gen_llsym (dw_loc_list_ref list)
8342 gcc_assert (!list->ll_symbol);
8343 list->ll_symbol = gen_internal_sym ("LLST");
8346 /* Output the location list given to us. */
8348 static void
8349 output_loc_list (dw_loc_list_ref list_head)
8351 dw_loc_list_ref curr = list_head;
8353 if (list_head->emitted)
8354 return;
8355 list_head->emitted = true;
8357 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8359 /* Walk the location list, and output each range + expression. */
8360 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8362 unsigned long size;
8363 /* Don't output an entry that starts and ends at the same address. */
8364 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
8365 continue;
8366 size = size_of_locs (curr->expr);
8367 /* If the expression is too large, drop it on the floor. We could
8368 perhaps put it into DW_TAG_dwarf_procedure and refer to that
8369 in the expression, but >= 64KB expressions for a single value
8370 in a single range are unlikely very useful. */
8371 if (size > 0xffff)
8372 continue;
8373 if (dwarf_split_debug_info)
8375 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
8376 "Location list start/length entry (%s)",
8377 list_head->ll_symbol);
8378 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
8379 "Location list range start index (%s)",
8380 curr->begin);
8381 /* The length field is 4 bytes. If we ever need to support
8382 an 8-byte length, we can add a new DW_LLE code or fall back
8383 to DW_LLE_GNU_start_end_entry. */
8384 dw2_asm_output_delta (4, curr->end, curr->begin,
8385 "Location list range length (%s)",
8386 list_head->ll_symbol);
8388 else if (!have_multiple_function_sections)
8390 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8391 "Location list begin address (%s)",
8392 list_head->ll_symbol);
8393 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8394 "Location list end address (%s)",
8395 list_head->ll_symbol);
8397 else
8399 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8400 "Location list begin address (%s)",
8401 list_head->ll_symbol);
8402 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8403 "Location list end address (%s)",
8404 list_head->ll_symbol);
8407 /* Output the block length for this list of location operations. */
8408 gcc_assert (size <= 0xffff);
8409 dw2_asm_output_data (2, size, "%s", "Location expression size");
8411 output_loc_sequence (curr->expr, -1);
8414 if (dwarf_split_debug_info)
8415 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
8416 "Location list terminator (%s)",
8417 list_head->ll_symbol);
8418 else
8420 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8421 "Location list terminator begin (%s)",
8422 list_head->ll_symbol);
8423 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8424 "Location list terminator end (%s)",
8425 list_head->ll_symbol);
8429 /* Output a range_list offset into the debug_range section. Emit a
8430 relocated reference if val_entry is NULL, otherwise, emit an
8431 indirect reference. */
8433 static void
8434 output_range_list_offset (dw_attr_ref a)
8436 const char *name = dwarf_attr_name (a->dw_attr);
8438 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
8440 char *p = strchr (ranges_section_label, '\0');
8441 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX, a->dw_attr_val.v.val_offset);
8442 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8443 debug_ranges_section, "%s", name);
8444 *p = '\0';
8446 else
8447 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8448 "%s (offset from %s)", name, ranges_section_label);
8451 /* Output the offset into the debug_loc section. */
8453 static void
8454 output_loc_list_offset (dw_attr_ref a)
8456 char *sym = AT_loc_list (a)->ll_symbol;
8458 gcc_assert (sym);
8459 if (dwarf_split_debug_info)
8460 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
8461 "%s", dwarf_attr_name (a->dw_attr));
8462 else
8463 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8464 "%s", dwarf_attr_name (a->dw_attr));
8467 /* Output an attribute's index or value appropriately. */
8469 static void
8470 output_attr_index_or_value (dw_attr_ref a)
8472 const char *name = dwarf_attr_name (a->dw_attr);
8474 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8476 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
8477 return;
8479 switch (AT_class (a))
8481 case dw_val_class_addr:
8482 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8483 break;
8484 case dw_val_class_high_pc:
8485 case dw_val_class_lbl_id:
8486 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8487 break;
8488 case dw_val_class_loc_list:
8489 output_loc_list_offset (a);
8490 break;
8491 default:
8492 gcc_unreachable ();
8496 /* Output a type signature. */
8498 static inline void
8499 output_signature (const char *sig, const char *name)
8501 int i;
8503 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8504 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
8507 /* Output the DIE and its attributes. Called recursively to generate
8508 the definitions of each child DIE. */
8510 static void
8511 output_die (dw_die_ref die)
8513 dw_attr_ref a;
8514 dw_die_ref c;
8515 unsigned long size;
8516 unsigned ix;
8518 /* If someone in another CU might refer to us, set up a symbol for
8519 them to point to. */
8520 if (! die->comdat_type_p && die->die_id.die_symbol)
8521 output_die_symbol (die);
8523 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
8524 (unsigned long)die->die_offset,
8525 dwarf_tag_name (die->die_tag));
8527 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8529 const char *name = dwarf_attr_name (a->dw_attr);
8531 switch (AT_class (a))
8533 case dw_val_class_addr:
8534 output_attr_index_or_value (a);
8535 break;
8537 case dw_val_class_offset:
8538 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8539 "%s", name);
8540 break;
8542 case dw_val_class_range_list:
8543 output_range_list_offset (a);
8544 break;
8546 case dw_val_class_loc:
8547 size = size_of_locs (AT_loc (a));
8549 /* Output the block length for this list of location operations. */
8550 if (dwarf_version >= 4)
8551 dw2_asm_output_data_uleb128 (size, "%s", name);
8552 else
8553 dw2_asm_output_data (constant_size (size), size, "%s", name);
8555 output_loc_sequence (AT_loc (a), -1);
8556 break;
8558 case dw_val_class_const:
8559 /* ??? It would be slightly more efficient to use a scheme like is
8560 used for unsigned constants below, but gdb 4.x does not sign
8561 extend. Gdb 5.x does sign extend. */
8562 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8563 break;
8565 case dw_val_class_unsigned_const:
8567 int csize = constant_size (AT_unsigned (a));
8568 if (dwarf_version == 3
8569 && a->dw_attr == DW_AT_data_member_location
8570 && csize >= 4)
8571 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
8572 else
8573 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
8575 break;
8577 case dw_val_class_const_double:
8579 unsigned HOST_WIDE_INT first, second;
8581 if (HOST_BITS_PER_WIDE_INT >= 64)
8582 dw2_asm_output_data (1,
8583 HOST_BITS_PER_DOUBLE_INT
8584 / HOST_BITS_PER_CHAR,
8585 NULL);
8587 if (WORDS_BIG_ENDIAN)
8589 first = a->dw_attr_val.v.val_double.high;
8590 second = a->dw_attr_val.v.val_double.low;
8592 else
8594 first = a->dw_attr_val.v.val_double.low;
8595 second = a->dw_attr_val.v.val_double.high;
8598 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8599 first, "%s", name);
8600 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8601 second, NULL);
8603 break;
8605 case dw_val_class_vec:
8607 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8608 unsigned int len = a->dw_attr_val.v.val_vec.length;
8609 unsigned int i;
8610 unsigned char *p;
8612 dw2_asm_output_data (constant_size (len * elt_size),
8613 len * elt_size, "%s", name);
8614 if (elt_size > sizeof (HOST_WIDE_INT))
8616 elt_size /= 2;
8617 len *= 2;
8619 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8620 i < len;
8621 i++, p += elt_size)
8622 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8623 "fp or vector constant word %u", i);
8624 break;
8627 case dw_val_class_flag:
8628 if (dwarf_version >= 4)
8630 /* Currently all add_AT_flag calls pass in 1 as last argument,
8631 so DW_FORM_flag_present can be used. If that ever changes,
8632 we'll need to use DW_FORM_flag and have some optimization
8633 in build_abbrev_table that will change those to
8634 DW_FORM_flag_present if it is set to 1 in all DIEs using
8635 the same abbrev entry. */
8636 gcc_assert (AT_flag (a) == 1);
8637 if (flag_debug_asm)
8638 fprintf (asm_out_file, "\t\t\t%s %s\n",
8639 ASM_COMMENT_START, name);
8640 break;
8642 dw2_asm_output_data (1, AT_flag (a), "%s", name);
8643 break;
8645 case dw_val_class_loc_list:
8646 output_attr_index_or_value (a);
8647 break;
8649 case dw_val_class_die_ref:
8650 if (AT_ref_external (a))
8652 if (AT_ref (a)->comdat_type_p)
8654 comdat_type_node_ref type_node =
8655 AT_ref (a)->die_id.die_type_node;
8657 gcc_assert (type_node);
8658 output_signature (type_node->signature, name);
8660 else
8662 const char *sym = AT_ref (a)->die_id.die_symbol;
8663 int size;
8665 gcc_assert (sym);
8666 /* In DWARF2, DW_FORM_ref_addr is sized by target address
8667 length, whereas in DWARF3 it's always sized as an
8668 offset. */
8669 if (dwarf_version == 2)
8670 size = DWARF2_ADDR_SIZE;
8671 else
8672 size = DWARF_OFFSET_SIZE;
8673 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
8674 name);
8677 else
8679 gcc_assert (AT_ref (a)->die_offset);
8680 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8681 "%s", name);
8683 break;
8685 case dw_val_class_fde_ref:
8687 char l1[20];
8689 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
8690 a->dw_attr_val.v.val_fde_index * 2);
8691 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
8692 "%s", name);
8694 break;
8696 case dw_val_class_vms_delta:
8697 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
8698 AT_vms_delta2 (a), AT_vms_delta1 (a),
8699 "%s", name);
8700 break;
8702 case dw_val_class_lbl_id:
8703 output_attr_index_or_value (a);
8704 break;
8706 case dw_val_class_lineptr:
8707 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8708 debug_line_section, "%s", name);
8709 break;
8711 case dw_val_class_macptr:
8712 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8713 debug_macinfo_section, "%s", name);
8714 break;
8716 case dw_val_class_str:
8717 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
8718 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8719 a->dw_attr_val.v.val_str->label,
8720 debug_str_section,
8721 "%s: \"%s\"", name, AT_string (a));
8722 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
8723 dw2_asm_output_data_uleb128 (AT_index (a),
8724 "%s: \"%s\"", name, AT_string (a));
8725 else
8726 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
8727 break;
8729 case dw_val_class_file:
8731 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
8733 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
8734 a->dw_attr_val.v.val_file->filename);
8735 break;
8738 case dw_val_class_data8:
8740 int i;
8742 for (i = 0; i < 8; i++)
8743 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
8744 i == 0 ? "%s" : NULL, name);
8745 break;
8748 case dw_val_class_high_pc:
8749 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
8750 get_AT_low_pc (die), "DW_AT_high_pc");
8751 break;
8753 default:
8754 gcc_unreachable ();
8758 FOR_EACH_CHILD (die, c, output_die (c));
8760 /* Add null byte to terminate sibling list. */
8761 if (die->die_child != NULL)
8762 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
8763 (unsigned long) die->die_offset);
8766 /* Output the compilation unit that appears at the beginning of the
8767 .debug_info section, and precedes the DIE descriptions. */
8769 static void
8770 output_compilation_unit_header (void)
8772 int ver = dwarf_version;
8774 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8775 dw2_asm_output_data (4, 0xffffffff,
8776 "Initial length escape value indicating 64-bit DWARF extension");
8777 dw2_asm_output_data (DWARF_OFFSET_SIZE,
8778 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
8779 "Length of Compilation Unit Info");
8780 dw2_asm_output_data (2, ver, "DWARF version number");
8781 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
8782 debug_abbrev_section,
8783 "Offset Into Abbrev. Section");
8784 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8787 /* Output the compilation unit DIE and its children. */
8789 static void
8790 output_comp_unit (dw_die_ref die, int output_if_empty)
8792 const char *secname, *oldsym;
8793 char *tmp;
8794 external_ref_hash_type extern_map;
8796 /* Unless we are outputting main CU, we may throw away empty ones. */
8797 if (!output_if_empty && die->die_child == NULL)
8798 return;
8800 /* Even if there are no children of this DIE, we must output the information
8801 about the compilation unit. Otherwise, on an empty translation unit, we
8802 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
8803 will then complain when examining the file. First mark all the DIEs in
8804 this CU so we know which get local refs. */
8805 mark_dies (die);
8807 extern_map = optimize_external_refs (die);
8809 build_abbrev_table (die, extern_map);
8811 extern_map.dispose ();
8813 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
8814 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8815 calc_die_sizes (die);
8817 oldsym = die->die_id.die_symbol;
8818 if (oldsym)
8820 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
8822 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
8823 secname = tmp;
8824 die->die_id.die_symbol = NULL;
8825 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8827 else
8829 switch_to_section (debug_info_section);
8830 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
8831 info_section_emitted = true;
8834 /* Output debugging information. */
8835 output_compilation_unit_header ();
8836 output_die (die);
8838 /* Leave the marks on the main CU, so we can check them in
8839 output_pubnames. */
8840 if (oldsym)
8842 unmark_dies (die);
8843 die->die_id.die_symbol = oldsym;
8847 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
8848 and .debug_pubtypes. This is configured per-target, but can be
8849 overridden by the -gpubnames or -gno-pubnames options. */
8851 static inline bool
8852 want_pubnames (void)
8854 if (debug_info_level <= DINFO_LEVEL_TERSE)
8855 return false;
8856 if (debug_generate_pub_sections != -1)
8857 return debug_generate_pub_sections;
8858 return targetm.want_debug_pub_sections;
8861 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
8863 static void
8864 add_AT_pubnames (dw_die_ref die)
8866 if (want_pubnames ())
8867 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
8870 /* Add a string attribute value to a skeleton DIE. */
8872 static inline void
8873 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
8874 const char *str)
8876 dw_attr_node attr;
8877 struct indirect_string_node *node;
8879 if (! skeleton_debug_str_hash)
8880 skeleton_debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
8881 debug_str_eq, NULL);
8883 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
8884 find_string_form (node);
8885 if (node->form == DW_FORM_GNU_str_index)
8886 node->form = DW_FORM_strp;
8888 attr.dw_attr = attr_kind;
8889 attr.dw_attr_val.val_class = dw_val_class_str;
8890 attr.dw_attr_val.val_entry = NULL;
8891 attr.dw_attr_val.v.val_str = node;
8892 add_dwarf_attr (die, &attr);
8895 /* Helper function to generate top-level dies for skeleton debug_info and
8896 debug_types. */
8898 static void
8899 add_top_level_skeleton_die_attrs (dw_die_ref die)
8901 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
8902 const char *comp_dir = comp_dir_string ();
8904 add_skeleton_AT_string (die, DW_AT_GNU_dwo_name, dwo_file_name);
8905 if (comp_dir != NULL)
8906 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
8907 add_AT_pubnames (die);
8908 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
8911 /* Return the single type-unit die for skeleton type units. */
8913 static dw_die_ref
8914 get_skeleton_type_unit (void)
8916 /* For dwarf_split_debug_sections with use_type info, all type units in the
8917 skeleton sections have identical dies (but different headers). This
8918 single die will be output many times. */
8920 static dw_die_ref skeleton_type_unit = NULL;
8922 if (skeleton_type_unit == NULL)
8924 skeleton_type_unit = new_die (DW_TAG_type_unit, NULL, NULL);
8925 add_top_level_skeleton_die_attrs (skeleton_type_unit);
8926 skeleton_type_unit->die_abbrev = SKELETON_TYPE_DIE_ABBREV;
8928 return skeleton_type_unit;
8931 /* Output skeleton debug sections that point to the dwo file. */
8933 static void
8934 output_skeleton_debug_sections (dw_die_ref comp_unit)
8936 /* These attributes will be found in the full debug_info section. */
8937 remove_AT (comp_unit, DW_AT_producer);
8938 remove_AT (comp_unit, DW_AT_language);
8940 switch_to_section (debug_skeleton_info_section);
8941 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
8943 /* Produce the skeleton compilation-unit header. This one differs enough from
8944 a normal CU header that it's better not to call output_compilation_unit
8945 header. */
8946 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8947 dw2_asm_output_data (4, 0xffffffff,
8948 "Initial length escape value indicating 64-bit DWARF extension");
8950 dw2_asm_output_data (DWARF_OFFSET_SIZE,
8951 DWARF_COMPILE_UNIT_HEADER_SIZE
8952 - DWARF_INITIAL_LENGTH_SIZE
8953 + size_of_die (comp_unit),
8954 "Length of Compilation Unit Info");
8955 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
8956 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
8957 debug_abbrev_section,
8958 "Offset Into Abbrev. Section");
8959 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8961 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
8962 output_die (comp_unit);
8964 /* Build the skeleton debug_abbrev section. */
8965 switch_to_section (debug_skeleton_abbrev_section);
8966 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
8968 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
8969 if (use_debug_types)
8970 output_die_abbrevs (SKELETON_TYPE_DIE_ABBREV, get_skeleton_type_unit ());
8972 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
8975 /* Output a comdat type unit DIE and its children. */
8977 static void
8978 output_comdat_type_unit (comdat_type_node *node)
8980 const char *secname;
8981 char *tmp;
8982 int i;
8983 #if defined (OBJECT_FORMAT_ELF)
8984 tree comdat_key;
8985 #endif
8986 external_ref_hash_type extern_map;
8988 /* First mark all the DIEs in this CU so we know which get local refs. */
8989 mark_dies (node->root_die);
8991 extern_map = optimize_external_refs (node->root_die);
8993 build_abbrev_table (node->root_die, extern_map);
8995 extern_map.dispose ();
8997 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
8998 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
8999 calc_die_sizes (node->root_die);
9001 #if defined (OBJECT_FORMAT_ELF)
9002 if (!dwarf_split_debug_info)
9003 secname = ".debug_types";
9004 else
9005 secname = ".debug_types.dwo";
9007 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9008 sprintf (tmp, "wt.");
9009 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9010 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
9011 comdat_key = get_identifier (tmp);
9012 targetm.asm_out.named_section (secname,
9013 SECTION_DEBUG | SECTION_LINKONCE,
9014 comdat_key);
9015 #else
9016 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9017 sprintf (tmp, ".gnu.linkonce.wt.");
9018 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9019 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
9020 secname = tmp;
9021 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9022 #endif
9024 /* Output debugging information. */
9025 output_compilation_unit_header ();
9026 output_signature (node->signature, "Type Signature");
9027 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
9028 "Offset to Type DIE");
9029 output_die (node->root_die);
9031 unmark_dies (node->root_die);
9033 #if defined (OBJECT_FORMAT_ELF)
9034 if (dwarf_split_debug_info)
9036 /* Produce the skeleton type-unit header. */
9037 const char *secname = ".debug_types";
9039 targetm.asm_out.named_section (secname,
9040 SECTION_DEBUG | SECTION_LINKONCE,
9041 comdat_key);
9042 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9043 dw2_asm_output_data (4, 0xffffffff,
9044 "Initial length escape value indicating 64-bit DWARF extension");
9046 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9047 DWARF_COMPILE_UNIT_HEADER_SIZE
9048 - DWARF_INITIAL_LENGTH_SIZE
9049 + size_of_die (get_skeleton_type_unit ())
9050 + DWARF_TYPE_SIGNATURE_SIZE + DWARF_OFFSET_SIZE,
9051 "Length of Type Unit Info");
9052 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
9053 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
9054 debug_skeleton_abbrev_section_label,
9055 debug_abbrev_section,
9056 "Offset Into Abbrev. Section");
9057 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9058 output_signature (node->signature, "Type Signature");
9059 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, "Offset to Type DIE");
9061 output_die (get_skeleton_type_unit ());
9063 #endif
9066 /* Return the DWARF2/3 pubname associated with a decl. */
9068 static const char *
9069 dwarf2_name (tree decl, int scope)
9071 if (DECL_NAMELESS (decl))
9072 return NULL;
9073 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
9076 /* Add a new entry to .debug_pubnames if appropriate. */
9078 static void
9079 add_pubname_string (const char *str, dw_die_ref die)
9081 pubname_entry e;
9083 e.die = die;
9084 e.name = xstrdup (str);
9085 vec_safe_push (pubname_table, e);
9088 static void
9089 add_pubname (tree decl, dw_die_ref die)
9091 if (!want_pubnames ())
9092 return;
9094 /* Don't add items to the table when we expect that the consumer will have
9095 just read the enclosing die. For example, if the consumer is looking at a
9096 class_member, it will either be inside the class already, or will have just
9097 looked up the class to find the member. Either way, searching the class is
9098 faster than searching the index. */
9099 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
9100 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9102 const char *name = dwarf2_name (decl, 1);
9104 if (name)
9105 add_pubname_string (name, die);
9109 /* Add an enumerator to the pubnames section. */
9111 static void
9112 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
9114 pubname_entry e;
9116 gcc_assert (scope_name);
9117 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
9118 e.die = die;
9119 vec_safe_push (pubname_table, e);
9122 /* Add a new entry to .debug_pubtypes if appropriate. */
9124 static void
9125 add_pubtype (tree decl, dw_die_ref die)
9127 pubname_entry e;
9129 if (!want_pubnames ())
9130 return;
9132 if ((TREE_PUBLIC (decl)
9133 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9134 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
9136 tree scope = NULL;
9137 const char *scope_name = "";
9138 const char *sep = is_cxx () ? "::" : ".";
9139 const char *name;
9141 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
9142 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
9144 scope_name = lang_hooks.dwarf_name (scope, 1);
9145 if (scope_name != NULL && scope_name[0] != '\0')
9146 scope_name = concat (scope_name, sep, NULL);
9147 else
9148 scope_name = "";
9151 if (TYPE_P (decl))
9152 name = type_tag (decl);
9153 else
9154 name = lang_hooks.dwarf_name (decl, 1);
9156 /* If we don't have a name for the type, there's no point in adding
9157 it to the table. */
9158 if (name != NULL && name[0] != '\0')
9160 e.die = die;
9161 e.name = concat (scope_name, name, NULL);
9162 vec_safe_push (pubtype_table, e);
9165 /* Although it might be more consistent to add the pubinfo for the
9166 enumerators as their dies are created, they should only be added if the
9167 enum type meets the criteria above. So rather than re-check the parent
9168 enum type whenever an enumerator die is created, just output them all
9169 here. This isn't protected by the name conditional because anonymous
9170 enums don't have names. */
9171 if (die->die_tag == DW_TAG_enumeration_type)
9173 dw_die_ref c;
9175 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
9180 /* Output a single entry in the pubnames table. */
9182 static void
9183 output_pubname (dw_offset die_offset, pubname_entry *entry)
9185 dw_die_ref die = entry->die;
9186 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
9188 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
9190 if (debug_generate_pub_sections == 2)
9192 /* This logic follows gdb's method for determining the value of the flag
9193 byte. */
9194 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
9195 switch (die->die_tag)
9197 case DW_TAG_typedef:
9198 case DW_TAG_base_type:
9199 case DW_TAG_subrange_type:
9200 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9201 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9202 break;
9203 case DW_TAG_enumerator:
9204 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9205 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9206 if (!is_cxx () && !is_java ())
9207 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9208 break;
9209 case DW_TAG_subprogram:
9210 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9211 GDB_INDEX_SYMBOL_KIND_FUNCTION);
9212 if (!is_ada ())
9213 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9214 break;
9215 case DW_TAG_constant:
9216 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9217 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9218 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9219 break;
9220 case DW_TAG_variable:
9221 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9222 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9223 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9224 break;
9225 case DW_TAG_namespace:
9226 case DW_TAG_imported_declaration:
9227 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9228 break;
9229 case DW_TAG_class_type:
9230 case DW_TAG_interface_type:
9231 case DW_TAG_structure_type:
9232 case DW_TAG_union_type:
9233 case DW_TAG_enumeration_type:
9234 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9235 if (!is_cxx () && !is_java ())
9236 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9237 break;
9238 default:
9239 /* An unusual tag. Leave the flag-byte empty. */
9240 break;
9242 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
9243 "GDB-index flags");
9246 dw2_asm_output_nstring (entry->name, -1, "external name");
9250 /* Output the public names table used to speed up access to externally
9251 visible names; or the public types table used to find type definitions. */
9253 static void
9254 output_pubnames (vec<pubname_entry, va_gc> *names)
9256 unsigned i;
9257 unsigned long pubnames_length = size_of_pubnames (names);
9258 pubname_ref pub;
9260 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9261 dw2_asm_output_data (4, 0xffffffff,
9262 "Initial length escape value indicating 64-bit DWARF extension");
9263 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length, "Pub Info Length");
9265 /* Version number for pubnames/pubtypes is independent of dwarf version. */
9266 dw2_asm_output_data (2, 2, "DWARF Version");
9268 if (dwarf_split_debug_info)
9269 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9270 debug_skeleton_info_section,
9271 "Offset of Compilation Unit Info");
9272 else
9273 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9274 debug_info_section,
9275 "Offset of Compilation Unit Info");
9276 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
9277 "Compilation Unit Length");
9279 FOR_EACH_VEC_ELT (*names, i, pub)
9281 if (include_pubname_in_output (names, pub))
9283 dw_offset die_offset = pub->die->die_offset;
9285 /* We shouldn't see pubnames for DIEs outside of the main CU. */
9286 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
9287 gcc_assert (pub->die->die_mark);
9289 /* If we're putting types in their own .debug_types sections,
9290 the .debug_pubtypes table will still point to the compile
9291 unit (not the type unit), so we want to use the offset of
9292 the skeleton DIE (if there is one). */
9293 if (pub->die->comdat_type_p && names == pubtype_table)
9295 comdat_type_node_ref type_node = pub->die->die_id.die_type_node;
9297 if (type_node != NULL)
9298 die_offset = (type_node->skeleton_die != NULL
9299 ? type_node->skeleton_die->die_offset
9300 : comp_unit_die ()->die_offset);
9303 output_pubname (die_offset, pub);
9307 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
9310 /* Output public names and types tables if necessary. */
9312 static void
9313 output_pubtables (void)
9315 if (!want_pubnames () || !info_section_emitted)
9316 return;
9318 switch_to_section (debug_pubnames_section);
9319 output_pubnames (pubname_table);
9320 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
9321 It shouldn't hurt to emit it always, since pure DWARF2 consumers
9322 simply won't look for the section. */
9323 switch_to_section (debug_pubtypes_section);
9324 output_pubnames (pubtype_table);
9328 /* Output the information that goes into the .debug_aranges table.
9329 Namely, define the beginning and ending address range of the
9330 text section generated for this compilation unit. */
9332 static void
9333 output_aranges (unsigned long aranges_length)
9335 unsigned i;
9337 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9338 dw2_asm_output_data (4, 0xffffffff,
9339 "Initial length escape value indicating 64-bit DWARF extension");
9340 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
9341 "Length of Address Ranges Info");
9342 /* Version number for aranges is still 2, even in DWARF3. */
9343 dw2_asm_output_data (2, 2, "DWARF Version");
9344 if (dwarf_split_debug_info)
9345 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9346 debug_skeleton_info_section,
9347 "Offset of Compilation Unit Info");
9348 else
9349 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9350 debug_info_section,
9351 "Offset of Compilation Unit Info");
9352 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
9353 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
9355 /* We need to align to twice the pointer size here. */
9356 if (DWARF_ARANGES_PAD_SIZE)
9358 /* Pad using a 2 byte words so that padding is correct for any
9359 pointer size. */
9360 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
9361 2 * DWARF2_ADDR_SIZE);
9362 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
9363 dw2_asm_output_data (2, 0, NULL);
9366 /* It is necessary not to output these entries if the sections were
9367 not used; if the sections were not used, the length will be 0 and
9368 the address may end up as 0 if the section is discarded by ld
9369 --gc-sections, leaving an invalid (0, 0) entry that can be
9370 confused with the terminator. */
9371 if (text_section_used)
9373 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
9374 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
9375 text_section_label, "Length");
9377 if (cold_text_section_used)
9379 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
9380 "Address");
9381 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
9382 cold_text_section_label, "Length");
9385 if (have_multiple_function_sections)
9387 unsigned fde_idx;
9388 dw_fde_ref fde;
9390 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9392 if (DECL_IGNORED_P (fde->decl))
9393 continue;
9394 if (!fde->in_std_section)
9396 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
9397 "Address");
9398 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
9399 fde->dw_fde_begin, "Length");
9401 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9403 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
9404 "Address");
9405 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
9406 fde->dw_fde_second_begin, "Length");
9411 /* Output the terminator words. */
9412 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9413 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9416 /* Add a new entry to .debug_ranges. Return the offset at which it
9417 was placed. */
9419 static unsigned int
9420 add_ranges_num (int num)
9422 unsigned int in_use = ranges_table_in_use;
9424 if (in_use == ranges_table_allocated)
9426 ranges_table_allocated += RANGES_TABLE_INCREMENT;
9427 ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
9428 ranges_table_allocated);
9429 memset (ranges_table + ranges_table_in_use, 0,
9430 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
9433 ranges_table[in_use].num = num;
9434 ranges_table_in_use = in_use + 1;
9436 return in_use * 2 * DWARF2_ADDR_SIZE;
9439 /* Add a new entry to .debug_ranges corresponding to a block, or a
9440 range terminator if BLOCK is NULL. */
9442 static unsigned int
9443 add_ranges (const_tree block)
9445 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
9448 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
9449 When using dwarf_split_debug_info, address attributes in dies destined
9450 for the final executable should be direct references--setting the
9451 parameter force_direct ensures this behavior. */
9453 static void
9454 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
9455 bool *added, bool force_direct)
9457 unsigned int in_use = ranges_by_label_in_use;
9458 unsigned int offset;
9460 if (in_use == ranges_by_label_allocated)
9462 ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
9463 ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
9464 ranges_by_label,
9465 ranges_by_label_allocated);
9466 memset (ranges_by_label + ranges_by_label_in_use, 0,
9467 RANGES_TABLE_INCREMENT
9468 * sizeof (struct dw_ranges_by_label_struct));
9471 ranges_by_label[in_use].begin = begin;
9472 ranges_by_label[in_use].end = end;
9473 ranges_by_label_in_use = in_use + 1;
9475 offset = add_ranges_num (-(int)in_use - 1);
9476 if (!*added)
9478 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
9479 *added = true;
9483 static void
9484 output_ranges (void)
9486 unsigned i;
9487 static const char *const start_fmt = "Offset %#x";
9488 const char *fmt = start_fmt;
9490 for (i = 0; i < ranges_table_in_use; i++)
9492 int block_num = ranges_table[i].num;
9494 if (block_num > 0)
9496 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
9497 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
9499 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
9500 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
9502 /* If all code is in the text section, then the compilation
9503 unit base address defaults to DW_AT_low_pc, which is the
9504 base of the text section. */
9505 if (!have_multiple_function_sections)
9507 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
9508 text_section_label,
9509 fmt, i * 2 * DWARF2_ADDR_SIZE);
9510 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
9511 text_section_label, NULL);
9514 /* Otherwise, the compilation unit base address is zero,
9515 which allows us to use absolute addresses, and not worry
9516 about whether the target supports cross-section
9517 arithmetic. */
9518 else
9520 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
9521 fmt, i * 2 * DWARF2_ADDR_SIZE);
9522 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
9525 fmt = NULL;
9528 /* Negative block_num stands for an index into ranges_by_label. */
9529 else if (block_num < 0)
9531 int lab_idx = - block_num - 1;
9533 if (!have_multiple_function_sections)
9535 gcc_unreachable ();
9536 #if 0
9537 /* If we ever use add_ranges_by_labels () for a single
9538 function section, all we have to do is to take out
9539 the #if 0 above. */
9540 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9541 ranges_by_label[lab_idx].begin,
9542 text_section_label,
9543 fmt, i * 2 * DWARF2_ADDR_SIZE);
9544 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9545 ranges_by_label[lab_idx].end,
9546 text_section_label, NULL);
9547 #endif
9549 else
9551 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9552 ranges_by_label[lab_idx].begin,
9553 fmt, i * 2 * DWARF2_ADDR_SIZE);
9554 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9555 ranges_by_label[lab_idx].end,
9556 NULL);
9559 else
9561 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9562 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9563 fmt = start_fmt;
9568 /* Data structure containing information about input files. */
9569 struct file_info
9571 const char *path; /* Complete file name. */
9572 const char *fname; /* File name part. */
9573 int length; /* Length of entire string. */
9574 struct dwarf_file_data * file_idx; /* Index in input file table. */
9575 int dir_idx; /* Index in directory table. */
9578 /* Data structure containing information about directories with source
9579 files. */
9580 struct dir_info
9582 const char *path; /* Path including directory name. */
9583 int length; /* Path length. */
9584 int prefix; /* Index of directory entry which is a prefix. */
9585 int count; /* Number of files in this directory. */
9586 int dir_idx; /* Index of directory used as base. */
9589 /* Callback function for file_info comparison. We sort by looking at
9590 the directories in the path. */
9592 static int
9593 file_info_cmp (const void *p1, const void *p2)
9595 const struct file_info *const s1 = (const struct file_info *) p1;
9596 const struct file_info *const s2 = (const struct file_info *) p2;
9597 const unsigned char *cp1;
9598 const unsigned char *cp2;
9600 /* Take care of file names without directories. We need to make sure that
9601 we return consistent values to qsort since some will get confused if
9602 we return the same value when identical operands are passed in opposite
9603 orders. So if neither has a directory, return 0 and otherwise return
9604 1 or -1 depending on which one has the directory. */
9605 if ((s1->path == s1->fname || s2->path == s2->fname))
9606 return (s2->path == s2->fname) - (s1->path == s1->fname);
9608 cp1 = (const unsigned char *) s1->path;
9609 cp2 = (const unsigned char *) s2->path;
9611 while (1)
9613 ++cp1;
9614 ++cp2;
9615 /* Reached the end of the first path? If so, handle like above. */
9616 if ((cp1 == (const unsigned char *) s1->fname)
9617 || (cp2 == (const unsigned char *) s2->fname))
9618 return ((cp2 == (const unsigned char *) s2->fname)
9619 - (cp1 == (const unsigned char *) s1->fname));
9621 /* Character of current path component the same? */
9622 else if (*cp1 != *cp2)
9623 return *cp1 - *cp2;
9627 struct file_name_acquire_data
9629 struct file_info *files;
9630 int used_files;
9631 int max_files;
9634 /* Traversal function for the hash table. */
9636 static int
9637 file_name_acquire (void ** slot, void *data)
9639 struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
9640 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
9641 struct file_info *fi;
9642 const char *f;
9644 gcc_assert (fnad->max_files >= d->emitted_number);
9646 if (! d->emitted_number)
9647 return 1;
9649 gcc_assert (fnad->max_files != fnad->used_files);
9651 fi = fnad->files + fnad->used_files++;
9653 /* Skip all leading "./". */
9654 f = d->filename;
9655 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
9656 f += 2;
9658 /* Create a new array entry. */
9659 fi->path = f;
9660 fi->length = strlen (f);
9661 fi->file_idx = d;
9663 /* Search for the file name part. */
9664 f = strrchr (f, DIR_SEPARATOR);
9665 #if defined (DIR_SEPARATOR_2)
9667 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
9669 if (g != NULL)
9671 if (f == NULL || f < g)
9672 f = g;
9675 #endif
9677 fi->fname = f == NULL ? fi->path : f + 1;
9678 return 1;
9681 /* Output the directory table and the file name table. We try to minimize
9682 the total amount of memory needed. A heuristic is used to avoid large
9683 slowdowns with many input files. */
9685 static void
9686 output_file_names (void)
9688 struct file_name_acquire_data fnad;
9689 int numfiles;
9690 struct file_info *files;
9691 struct dir_info *dirs;
9692 int *saved;
9693 int *savehere;
9694 int *backmap;
9695 int ndirs;
9696 int idx_offset;
9697 int i;
9699 if (!last_emitted_file)
9701 dw2_asm_output_data (1, 0, "End directory table");
9702 dw2_asm_output_data (1, 0, "End file name table");
9703 return;
9706 numfiles = last_emitted_file->emitted_number;
9708 /* Allocate the various arrays we need. */
9709 files = XALLOCAVEC (struct file_info, numfiles);
9710 dirs = XALLOCAVEC (struct dir_info, numfiles);
9712 fnad.files = files;
9713 fnad.used_files = 0;
9714 fnad.max_files = numfiles;
9715 htab_traverse (file_table, file_name_acquire, &fnad);
9716 gcc_assert (fnad.used_files == fnad.max_files);
9718 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
9720 /* Find all the different directories used. */
9721 dirs[0].path = files[0].path;
9722 dirs[0].length = files[0].fname - files[0].path;
9723 dirs[0].prefix = -1;
9724 dirs[0].count = 1;
9725 dirs[0].dir_idx = 0;
9726 files[0].dir_idx = 0;
9727 ndirs = 1;
9729 for (i = 1; i < numfiles; i++)
9730 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
9731 && memcmp (dirs[ndirs - 1].path, files[i].path,
9732 dirs[ndirs - 1].length) == 0)
9734 /* Same directory as last entry. */
9735 files[i].dir_idx = ndirs - 1;
9736 ++dirs[ndirs - 1].count;
9738 else
9740 int j;
9742 /* This is a new directory. */
9743 dirs[ndirs].path = files[i].path;
9744 dirs[ndirs].length = files[i].fname - files[i].path;
9745 dirs[ndirs].count = 1;
9746 dirs[ndirs].dir_idx = ndirs;
9747 files[i].dir_idx = ndirs;
9749 /* Search for a prefix. */
9750 dirs[ndirs].prefix = -1;
9751 for (j = 0; j < ndirs; j++)
9752 if (dirs[j].length < dirs[ndirs].length
9753 && dirs[j].length > 1
9754 && (dirs[ndirs].prefix == -1
9755 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
9756 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
9757 dirs[ndirs].prefix = j;
9759 ++ndirs;
9762 /* Now to the actual work. We have to find a subset of the directories which
9763 allow expressing the file name using references to the directory table
9764 with the least amount of characters. We do not do an exhaustive search
9765 where we would have to check out every combination of every single
9766 possible prefix. Instead we use a heuristic which provides nearly optimal
9767 results in most cases and never is much off. */
9768 saved = XALLOCAVEC (int, ndirs);
9769 savehere = XALLOCAVEC (int, ndirs);
9771 memset (saved, '\0', ndirs * sizeof (saved[0]));
9772 for (i = 0; i < ndirs; i++)
9774 int j;
9775 int total;
9777 /* We can always save some space for the current directory. But this
9778 does not mean it will be enough to justify adding the directory. */
9779 savehere[i] = dirs[i].length;
9780 total = (savehere[i] - saved[i]) * dirs[i].count;
9782 for (j = i + 1; j < ndirs; j++)
9784 savehere[j] = 0;
9785 if (saved[j] < dirs[i].length)
9787 /* Determine whether the dirs[i] path is a prefix of the
9788 dirs[j] path. */
9789 int k;
9791 k = dirs[j].prefix;
9792 while (k != -1 && k != (int) i)
9793 k = dirs[k].prefix;
9795 if (k == (int) i)
9797 /* Yes it is. We can possibly save some memory by
9798 writing the filenames in dirs[j] relative to
9799 dirs[i]. */
9800 savehere[j] = dirs[i].length;
9801 total += (savehere[j] - saved[j]) * dirs[j].count;
9806 /* Check whether we can save enough to justify adding the dirs[i]
9807 directory. */
9808 if (total > dirs[i].length + 1)
9810 /* It's worthwhile adding. */
9811 for (j = i; j < ndirs; j++)
9812 if (savehere[j] > 0)
9814 /* Remember how much we saved for this directory so far. */
9815 saved[j] = savehere[j];
9817 /* Remember the prefix directory. */
9818 dirs[j].dir_idx = i;
9823 /* Emit the directory name table. */
9824 idx_offset = dirs[0].length > 0 ? 1 : 0;
9825 for (i = 1 - idx_offset; i < ndirs; i++)
9826 dw2_asm_output_nstring (dirs[i].path,
9827 dirs[i].length
9828 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
9829 "Directory Entry: %#x", i + idx_offset);
9831 dw2_asm_output_data (1, 0, "End directory table");
9833 /* We have to emit them in the order of emitted_number since that's
9834 used in the debug info generation. To do this efficiently we
9835 generate a back-mapping of the indices first. */
9836 backmap = XALLOCAVEC (int, numfiles);
9837 for (i = 0; i < numfiles; i++)
9838 backmap[files[i].file_idx->emitted_number - 1] = i;
9840 /* Now write all the file names. */
9841 for (i = 0; i < numfiles; i++)
9843 int file_idx = backmap[i];
9844 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
9846 #ifdef VMS_DEBUGGING_INFO
9847 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
9849 /* Setting these fields can lead to debugger miscomparisons,
9850 but VMS Debug requires them to be set correctly. */
9852 int ver;
9853 long long cdt;
9854 long siz;
9855 int maxfilelen = strlen (files[file_idx].path)
9856 + dirs[dir_idx].length
9857 + MAX_VMS_VERSION_LEN + 1;
9858 char *filebuf = XALLOCAVEC (char, maxfilelen);
9860 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
9861 snprintf (filebuf, maxfilelen, "%s;%d",
9862 files[file_idx].path + dirs[dir_idx].length, ver);
9864 dw2_asm_output_nstring
9865 (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
9867 /* Include directory index. */
9868 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9870 /* Modification time. */
9871 dw2_asm_output_data_uleb128
9872 ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
9873 ? cdt : 0,
9874 NULL);
9876 /* File length in bytes. */
9877 dw2_asm_output_data_uleb128
9878 ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
9879 ? siz : 0,
9880 NULL);
9881 #else
9882 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
9883 "File Entry: %#x", (unsigned) i + 1);
9885 /* Include directory index. */
9886 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9888 /* Modification time. */
9889 dw2_asm_output_data_uleb128 (0, NULL);
9891 /* File length in bytes. */
9892 dw2_asm_output_data_uleb128 (0, NULL);
9893 #endif /* VMS_DEBUGGING_INFO */
9896 dw2_asm_output_data (1, 0, "End file name table");
9900 /* Output one line number table into the .debug_line section. */
9902 static void
9903 output_one_line_info_table (dw_line_info_table *table)
9905 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
9906 unsigned int current_line = 1;
9907 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
9908 dw_line_info_entry *ent;
9909 size_t i;
9911 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
9913 switch (ent->opcode)
9915 case LI_set_address:
9916 /* ??? Unfortunately, we have little choice here currently, and
9917 must always use the most general form. GCC does not know the
9918 address delta itself, so we can't use DW_LNS_advance_pc. Many
9919 ports do have length attributes which will give an upper bound
9920 on the address range. We could perhaps use length attributes
9921 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
9922 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
9924 /* This can handle any delta. This takes
9925 4+DWARF2_ADDR_SIZE bytes. */
9926 dw2_asm_output_data (1, 0, "set address %s", line_label);
9927 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9928 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9929 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9930 break;
9932 case LI_set_line:
9933 if (ent->val == current_line)
9935 /* We still need to start a new row, so output a copy insn. */
9936 dw2_asm_output_data (1, DW_LNS_copy,
9937 "copy line %u", current_line);
9939 else
9941 int line_offset = ent->val - current_line;
9942 int line_delta = line_offset - DWARF_LINE_BASE;
9944 current_line = ent->val;
9945 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
9947 /* This can handle deltas from -10 to 234, using the current
9948 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
9949 This takes 1 byte. */
9950 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
9951 "line %u", current_line);
9953 else
9955 /* This can handle any delta. This takes at least 4 bytes,
9956 depending on the value being encoded. */
9957 dw2_asm_output_data (1, DW_LNS_advance_line,
9958 "advance to line %u", current_line);
9959 dw2_asm_output_data_sleb128 (line_offset, NULL);
9960 dw2_asm_output_data (1, DW_LNS_copy, NULL);
9963 break;
9965 case LI_set_file:
9966 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
9967 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
9968 break;
9970 case LI_set_column:
9971 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
9972 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
9973 break;
9975 case LI_negate_stmt:
9976 current_is_stmt = !current_is_stmt;
9977 dw2_asm_output_data (1, DW_LNS_negate_stmt,
9978 "is_stmt %d", current_is_stmt);
9979 break;
9981 case LI_set_prologue_end:
9982 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
9983 "set prologue end");
9984 break;
9986 case LI_set_epilogue_begin:
9987 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
9988 "set epilogue begin");
9989 break;
9991 case LI_set_discriminator:
9992 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
9993 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
9994 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
9995 dw2_asm_output_data_uleb128 (ent->val, NULL);
9996 break;
10000 /* Emit debug info for the address of the end of the table. */
10001 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
10002 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10003 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10004 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
10006 dw2_asm_output_data (1, 0, "end sequence");
10007 dw2_asm_output_data_uleb128 (1, NULL);
10008 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
10011 /* Output the source line number correspondence information. This
10012 information goes into the .debug_line section. */
10014 static void
10015 output_line_info (bool prologue_only)
10017 char l1[20], l2[20], p1[20], p2[20];
10018 int ver = dwarf_version;
10019 bool saw_one = false;
10020 int opc;
10022 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
10023 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
10024 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
10025 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
10027 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10028 dw2_asm_output_data (4, 0xffffffff,
10029 "Initial length escape value indicating 64-bit DWARF extension");
10030 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
10031 "Length of Source Line Info");
10032 ASM_OUTPUT_LABEL (asm_out_file, l1);
10034 dw2_asm_output_data (2, ver, "DWARF Version");
10035 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
10036 ASM_OUTPUT_LABEL (asm_out_file, p1);
10038 /* Define the architecture-dependent minimum instruction length (in bytes).
10039 In this implementation of DWARF, this field is used for information
10040 purposes only. Since GCC generates assembly language, we have no
10041 a priori knowledge of how many instruction bytes are generated for each
10042 source line, and therefore can use only the DW_LNE_set_address and
10043 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
10044 this as '1', which is "correct enough" for all architectures,
10045 and don't let the target override. */
10046 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
10048 if (ver >= 4)
10049 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
10050 "Maximum Operations Per Instruction");
10051 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
10052 "Default is_stmt_start flag");
10053 dw2_asm_output_data (1, DWARF_LINE_BASE,
10054 "Line Base Value (Special Opcodes)");
10055 dw2_asm_output_data (1, DWARF_LINE_RANGE,
10056 "Line Range Value (Special Opcodes)");
10057 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
10058 "Special Opcode Base");
10060 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
10062 int n_op_args;
10063 switch (opc)
10065 case DW_LNS_advance_pc:
10066 case DW_LNS_advance_line:
10067 case DW_LNS_set_file:
10068 case DW_LNS_set_column:
10069 case DW_LNS_fixed_advance_pc:
10070 case DW_LNS_set_isa:
10071 n_op_args = 1;
10072 break;
10073 default:
10074 n_op_args = 0;
10075 break;
10078 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
10079 opc, n_op_args);
10082 /* Write out the information about the files we use. */
10083 output_file_names ();
10084 ASM_OUTPUT_LABEL (asm_out_file, p2);
10085 if (prologue_only)
10087 /* Output the marker for the end of the line number info. */
10088 ASM_OUTPUT_LABEL (asm_out_file, l2);
10089 return;
10092 if (separate_line_info)
10094 dw_line_info_table *table;
10095 size_t i;
10097 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
10098 if (table->in_use)
10100 output_one_line_info_table (table);
10101 saw_one = true;
10104 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
10106 output_one_line_info_table (cold_text_section_line_info);
10107 saw_one = true;
10110 /* ??? Some Darwin linkers crash on a .debug_line section with no
10111 sequences. Further, merely a DW_LNE_end_sequence entry is not
10112 sufficient -- the address column must also be initialized.
10113 Make sure to output at least one set_address/end_sequence pair,
10114 choosing .text since that section is always present. */
10115 if (text_section_line_info->in_use || !saw_one)
10116 output_one_line_info_table (text_section_line_info);
10118 /* Output the marker for the end of the line number info. */
10119 ASM_OUTPUT_LABEL (asm_out_file, l2);
10122 /* Given a pointer to a tree node for some base type, return a pointer to
10123 a DIE that describes the given type.
10125 This routine must only be called for GCC type nodes that correspond to
10126 Dwarf base (fundamental) types. */
10128 static dw_die_ref
10129 base_type_die (tree type)
10131 dw_die_ref base_type_result;
10132 enum dwarf_type encoding;
10134 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
10135 return 0;
10137 /* If this is a subtype that should not be emitted as a subrange type,
10138 use the base type. See subrange_type_for_debug_p. */
10139 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
10140 type = TREE_TYPE (type);
10142 switch (TREE_CODE (type))
10144 case INTEGER_TYPE:
10145 if ((dwarf_version >= 4 || !dwarf_strict)
10146 && TYPE_NAME (type)
10147 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10148 && DECL_IS_BUILTIN (TYPE_NAME (type))
10149 && DECL_NAME (TYPE_NAME (type)))
10151 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
10152 if (strcmp (name, "char16_t") == 0
10153 || strcmp (name, "char32_t") == 0)
10155 encoding = DW_ATE_UTF;
10156 break;
10159 if (TYPE_STRING_FLAG (type))
10161 if (TYPE_UNSIGNED (type))
10162 encoding = DW_ATE_unsigned_char;
10163 else
10164 encoding = DW_ATE_signed_char;
10166 else if (TYPE_UNSIGNED (type))
10167 encoding = DW_ATE_unsigned;
10168 else
10169 encoding = DW_ATE_signed;
10170 break;
10172 case REAL_TYPE:
10173 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
10175 if (dwarf_version >= 3 || !dwarf_strict)
10176 encoding = DW_ATE_decimal_float;
10177 else
10178 encoding = DW_ATE_lo_user;
10180 else
10181 encoding = DW_ATE_float;
10182 break;
10184 case FIXED_POINT_TYPE:
10185 if (!(dwarf_version >= 3 || !dwarf_strict))
10186 encoding = DW_ATE_lo_user;
10187 else if (TYPE_UNSIGNED (type))
10188 encoding = DW_ATE_unsigned_fixed;
10189 else
10190 encoding = DW_ATE_signed_fixed;
10191 break;
10193 /* Dwarf2 doesn't know anything about complex ints, so use
10194 a user defined type for it. */
10195 case COMPLEX_TYPE:
10196 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
10197 encoding = DW_ATE_complex_float;
10198 else
10199 encoding = DW_ATE_lo_user;
10200 break;
10202 case BOOLEAN_TYPE:
10203 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
10204 encoding = DW_ATE_boolean;
10205 break;
10207 default:
10208 /* No other TREE_CODEs are Dwarf fundamental types. */
10209 gcc_unreachable ();
10212 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
10214 add_AT_unsigned (base_type_result, DW_AT_byte_size,
10215 int_size_in_bytes (type));
10216 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
10217 add_pubtype (type, base_type_result);
10219 return base_type_result;
10222 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
10223 named 'auto' in its type: return true for it, false otherwise. */
10225 static inline bool
10226 is_cxx_auto (tree type)
10228 if (is_cxx ())
10230 tree name = TYPE_NAME (type);
10231 if (TREE_CODE (name) == TYPE_DECL)
10232 name = DECL_NAME (name);
10233 if (name == get_identifier ("auto")
10234 || name == get_identifier ("decltype(auto)"))
10235 return true;
10237 return false;
10240 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
10241 given input type is a Dwarf "fundamental" type. Otherwise return null. */
10243 static inline int
10244 is_base_type (tree type)
10246 switch (TREE_CODE (type))
10248 case ERROR_MARK:
10249 case VOID_TYPE:
10250 case INTEGER_TYPE:
10251 case REAL_TYPE:
10252 case FIXED_POINT_TYPE:
10253 case COMPLEX_TYPE:
10254 case BOOLEAN_TYPE:
10255 return 1;
10257 case ARRAY_TYPE:
10258 case RECORD_TYPE:
10259 case UNION_TYPE:
10260 case QUAL_UNION_TYPE:
10261 case ENUMERAL_TYPE:
10262 case FUNCTION_TYPE:
10263 case METHOD_TYPE:
10264 case POINTER_TYPE:
10265 case REFERENCE_TYPE:
10266 case NULLPTR_TYPE:
10267 case OFFSET_TYPE:
10268 case LANG_TYPE:
10269 case VECTOR_TYPE:
10270 return 0;
10272 default:
10273 if (is_cxx_auto (type))
10274 return 0;
10275 gcc_unreachable ();
10278 return 0;
10281 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
10282 node, return the size in bits for the type if it is a constant, or else
10283 return the alignment for the type if the type's size is not constant, or
10284 else return BITS_PER_WORD if the type actually turns out to be an
10285 ERROR_MARK node. */
10287 static inline unsigned HOST_WIDE_INT
10288 simple_type_size_in_bits (const_tree type)
10290 if (TREE_CODE (type) == ERROR_MARK)
10291 return BITS_PER_WORD;
10292 else if (TYPE_SIZE (type) == NULL_TREE)
10293 return 0;
10294 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
10295 return tree_to_uhwi (TYPE_SIZE (type));
10296 else
10297 return TYPE_ALIGN (type);
10300 /* Similarly, but return a double_int instead of UHWI. */
10302 static inline double_int
10303 double_int_type_size_in_bits (const_tree type)
10305 if (TREE_CODE (type) == ERROR_MARK)
10306 return double_int::from_uhwi (BITS_PER_WORD);
10307 else if (TYPE_SIZE (type) == NULL_TREE)
10308 return double_int_zero;
10309 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
10310 return tree_to_double_int (TYPE_SIZE (type));
10311 else
10312 return double_int::from_uhwi (TYPE_ALIGN (type));
10315 /* Given a pointer to a tree node for a subrange type, return a pointer
10316 to a DIE that describes the given type. */
10318 static dw_die_ref
10319 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
10321 dw_die_ref subrange_die;
10322 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
10324 if (context_die == NULL)
10325 context_die = comp_unit_die ();
10327 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
10329 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
10331 /* The size of the subrange type and its base type do not match,
10332 so we need to generate a size attribute for the subrange type. */
10333 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
10336 if (low)
10337 add_bound_info (subrange_die, DW_AT_lower_bound, low);
10338 if (high)
10339 add_bound_info (subrange_die, DW_AT_upper_bound, high);
10341 return subrange_die;
10344 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
10345 entry that chains various modifiers in front of the given type. */
10347 static dw_die_ref
10348 modified_type_die (tree type, int is_const_type, int is_volatile_type,
10349 dw_die_ref context_die)
10351 enum tree_code code = TREE_CODE (type);
10352 dw_die_ref mod_type_die;
10353 dw_die_ref sub_die = NULL;
10354 tree item_type = NULL;
10355 tree qualified_type;
10356 tree name, low, high;
10357 dw_die_ref mod_scope;
10359 if (code == ERROR_MARK)
10360 return NULL;
10362 /* See if we already have the appropriately qualified variant of
10363 this type. */
10364 qualified_type
10365 = get_qualified_type (type,
10366 ((is_const_type ? TYPE_QUAL_CONST : 0)
10367 | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
10369 if (qualified_type == sizetype
10370 && TYPE_NAME (qualified_type)
10371 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
10373 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
10375 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
10376 && TYPE_PRECISION (t)
10377 == TYPE_PRECISION (qualified_type)
10378 && TYPE_UNSIGNED (t)
10379 == TYPE_UNSIGNED (qualified_type));
10380 qualified_type = t;
10383 /* If we do, then we can just use its DIE, if it exists. */
10384 if (qualified_type)
10386 mod_type_die = lookup_type_die (qualified_type);
10387 if (mod_type_die)
10388 return mod_type_die;
10391 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
10393 /* Handle C typedef types. */
10394 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
10395 && !DECL_ARTIFICIAL (name))
10397 tree dtype = TREE_TYPE (name);
10399 if (qualified_type == dtype)
10401 /* For a named type, use the typedef. */
10402 gen_type_die (qualified_type, context_die);
10403 return lookup_type_die (qualified_type);
10405 else if (is_const_type < TYPE_READONLY (dtype)
10406 || is_volatile_type < TYPE_VOLATILE (dtype)
10407 || (is_const_type <= TYPE_READONLY (dtype)
10408 && is_volatile_type <= TYPE_VOLATILE (dtype)
10409 && DECL_ORIGINAL_TYPE (name) != type))
10410 /* cv-unqualified version of named type. Just use the unnamed
10411 type to which it refers. */
10412 return modified_type_die (DECL_ORIGINAL_TYPE (name),
10413 is_const_type, is_volatile_type,
10414 context_die);
10415 /* Else cv-qualified version of named type; fall through. */
10418 mod_scope = scope_die_for (type, context_die);
10420 if (is_const_type
10421 /* If both is_const_type and is_volatile_type, prefer the path
10422 which leads to a qualified type. */
10423 && (!is_volatile_type
10424 || get_qualified_type (type, TYPE_QUAL_CONST) == NULL_TREE
10425 || get_qualified_type (type, TYPE_QUAL_VOLATILE) != NULL_TREE))
10427 mod_type_die = new_die (DW_TAG_const_type, mod_scope, type);
10428 sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
10430 else if (is_volatile_type)
10432 mod_type_die = new_die (DW_TAG_volatile_type, mod_scope, type);
10433 sub_die = modified_type_die (type, is_const_type, 0, context_die);
10435 else if (code == POINTER_TYPE)
10437 mod_type_die = new_die (DW_TAG_pointer_type, mod_scope, type);
10438 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10439 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10440 item_type = TREE_TYPE (type);
10441 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10442 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10443 TYPE_ADDR_SPACE (item_type));
10445 else if (code == REFERENCE_TYPE)
10447 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
10448 mod_type_die = new_die (DW_TAG_rvalue_reference_type, mod_scope,
10449 type);
10450 else
10451 mod_type_die = new_die (DW_TAG_reference_type, mod_scope, type);
10452 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10453 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10454 item_type = TREE_TYPE (type);
10455 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10456 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10457 TYPE_ADDR_SPACE (item_type));
10459 else if (code == INTEGER_TYPE
10460 && TREE_TYPE (type) != NULL_TREE
10461 && subrange_type_for_debug_p (type, &low, &high))
10463 mod_type_die = subrange_type_die (type, low, high, context_die);
10464 item_type = TREE_TYPE (type);
10466 else if (is_base_type (type))
10467 mod_type_die = base_type_die (type);
10468 else
10470 gen_type_die (type, context_die);
10472 /* We have to get the type_main_variant here (and pass that to the
10473 `lookup_type_die' routine) because the ..._TYPE node we have
10474 might simply be a *copy* of some original type node (where the
10475 copy was created to help us keep track of typedef names) and
10476 that copy might have a different TYPE_UID from the original
10477 ..._TYPE node. */
10478 if (TREE_CODE (type) != VECTOR_TYPE)
10479 return lookup_type_die (type_main_variant (type));
10480 else
10481 /* Vectors have the debugging information in the type,
10482 not the main variant. */
10483 return lookup_type_die (type);
10486 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
10487 don't output a DW_TAG_typedef, since there isn't one in the
10488 user's program; just attach a DW_AT_name to the type.
10489 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
10490 if the base type already has the same name. */
10491 if (name
10492 && ((TREE_CODE (name) != TYPE_DECL
10493 && (qualified_type == TYPE_MAIN_VARIANT (type)
10494 || (!is_const_type && !is_volatile_type)))
10495 || (TREE_CODE (name) == TYPE_DECL
10496 && TREE_TYPE (name) == qualified_type
10497 && DECL_NAME (name))))
10499 if (TREE_CODE (name) == TYPE_DECL)
10500 /* Could just call add_name_and_src_coords_attributes here,
10501 but since this is a builtin type it doesn't have any
10502 useful source coordinates anyway. */
10503 name = DECL_NAME (name);
10504 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
10506 /* This probably indicates a bug. */
10507 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
10509 name = TYPE_NAME (type);
10510 if (name
10511 && TREE_CODE (name) == TYPE_DECL)
10512 name = DECL_NAME (name);
10513 add_name_attribute (mod_type_die,
10514 name ? IDENTIFIER_POINTER (name) : "__unknown__");
10517 if (qualified_type)
10518 equate_type_number_to_die (qualified_type, mod_type_die);
10520 if (item_type)
10521 /* We must do this after the equate_type_number_to_die call, in case
10522 this is a recursive type. This ensures that the modified_type_die
10523 recursion will terminate even if the type is recursive. Recursive
10524 types are possible in Ada. */
10525 sub_die = modified_type_die (item_type,
10526 TYPE_READONLY (item_type),
10527 TYPE_VOLATILE (item_type),
10528 context_die);
10530 if (sub_die != NULL)
10531 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
10533 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
10534 if (TYPE_ARTIFICIAL (type))
10535 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
10537 return mod_type_die;
10540 /* Generate DIEs for the generic parameters of T.
10541 T must be either a generic type or a generic function.
10542 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
10544 static void
10545 gen_generic_params_dies (tree t)
10547 tree parms, args;
10548 int parms_num, i;
10549 dw_die_ref die = NULL;
10550 int non_default;
10552 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
10553 return;
10555 if (TYPE_P (t))
10556 die = lookup_type_die (t);
10557 else if (DECL_P (t))
10558 die = lookup_decl_die (t);
10560 gcc_assert (die);
10562 parms = lang_hooks.get_innermost_generic_parms (t);
10563 if (!parms)
10564 /* T has no generic parameter. It means T is neither a generic type
10565 or function. End of story. */
10566 return;
10568 parms_num = TREE_VEC_LENGTH (parms);
10569 args = lang_hooks.get_innermost_generic_args (t);
10570 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
10571 non_default = int_cst_value (TREE_CHAIN (args));
10572 else
10573 non_default = TREE_VEC_LENGTH (args);
10574 for (i = 0; i < parms_num; i++)
10576 tree parm, arg, arg_pack_elems;
10577 dw_die_ref parm_die;
10579 parm = TREE_VEC_ELT (parms, i);
10580 arg = TREE_VEC_ELT (args, i);
10581 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
10582 gcc_assert (parm && TREE_VALUE (parm) && arg);
10584 if (parm && TREE_VALUE (parm) && arg)
10586 /* If PARM represents a template parameter pack,
10587 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
10588 by DW_TAG_template_*_parameter DIEs for the argument
10589 pack elements of ARG. Note that ARG would then be
10590 an argument pack. */
10591 if (arg_pack_elems)
10592 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
10593 arg_pack_elems,
10594 die);
10595 else
10596 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
10597 true /* emit name */, die);
10598 if (i >= non_default)
10599 add_AT_flag (parm_die, DW_AT_default_value, 1);
10604 /* Create and return a DIE for PARM which should be
10605 the representation of a generic type parameter.
10606 For instance, in the C++ front end, PARM would be a template parameter.
10607 ARG is the argument to PARM.
10608 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
10609 name of the PARM.
10610 PARENT_DIE is the parent DIE which the new created DIE should be added to,
10611 as a child node. */
10613 static dw_die_ref
10614 generic_parameter_die (tree parm, tree arg,
10615 bool emit_name_p,
10616 dw_die_ref parent_die)
10618 dw_die_ref tmpl_die = NULL;
10619 const char *name = NULL;
10621 if (!parm || !DECL_NAME (parm) || !arg)
10622 return NULL;
10624 /* We support non-type generic parameters and arguments,
10625 type generic parameters and arguments, as well as
10626 generic generic parameters (a.k.a. template template parameters in C++)
10627 and arguments. */
10628 if (TREE_CODE (parm) == PARM_DECL)
10629 /* PARM is a nontype generic parameter */
10630 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
10631 else if (TREE_CODE (parm) == TYPE_DECL)
10632 /* PARM is a type generic parameter. */
10633 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
10634 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10635 /* PARM is a generic generic parameter.
10636 Its DIE is a GNU extension. It shall have a
10637 DW_AT_name attribute to represent the name of the template template
10638 parameter, and a DW_AT_GNU_template_name attribute to represent the
10639 name of the template template argument. */
10640 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
10641 parent_die, parm);
10642 else
10643 gcc_unreachable ();
10645 if (tmpl_die)
10647 tree tmpl_type;
10649 /* If PARM is a generic parameter pack, it means we are
10650 emitting debug info for a template argument pack element.
10651 In other terms, ARG is a template argument pack element.
10652 In that case, we don't emit any DW_AT_name attribute for
10653 the die. */
10654 if (emit_name_p)
10656 name = IDENTIFIER_POINTER (DECL_NAME (parm));
10657 gcc_assert (name);
10658 add_AT_string (tmpl_die, DW_AT_name, name);
10661 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10663 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
10664 TMPL_DIE should have a child DW_AT_type attribute that is set
10665 to the type of the argument to PARM, which is ARG.
10666 If PARM is a type generic parameter, TMPL_DIE should have a
10667 child DW_AT_type that is set to ARG. */
10668 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
10669 add_type_attribute (tmpl_die, tmpl_type, 0,
10670 TREE_THIS_VOLATILE (tmpl_type),
10671 parent_die);
10673 else
10675 /* So TMPL_DIE is a DIE representing a
10676 a generic generic template parameter, a.k.a template template
10677 parameter in C++ and arg is a template. */
10679 /* The DW_AT_GNU_template_name attribute of the DIE must be set
10680 to the name of the argument. */
10681 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
10682 if (name)
10683 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
10686 if (TREE_CODE (parm) == PARM_DECL)
10687 /* So PARM is a non-type generic parameter.
10688 DWARF3 5.6.8 says we must set a DW_AT_const_value child
10689 attribute of TMPL_DIE which value represents the value
10690 of ARG.
10691 We must be careful here:
10692 The value of ARG might reference some function decls.
10693 We might currently be emitting debug info for a generic
10694 type and types are emitted before function decls, we don't
10695 know if the function decls referenced by ARG will actually be
10696 emitted after cgraph computations.
10697 So must defer the generation of the DW_AT_const_value to
10698 after cgraph is ready. */
10699 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
10702 return tmpl_die;
10705 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
10706 PARM_PACK must be a template parameter pack. The returned DIE
10707 will be child DIE of PARENT_DIE. */
10709 static dw_die_ref
10710 template_parameter_pack_die (tree parm_pack,
10711 tree parm_pack_args,
10712 dw_die_ref parent_die)
10714 dw_die_ref die;
10715 int j;
10717 gcc_assert (parent_die && parm_pack);
10719 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
10720 add_name_and_src_coords_attributes (die, parm_pack);
10721 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
10722 generic_parameter_die (parm_pack,
10723 TREE_VEC_ELT (parm_pack_args, j),
10724 false /* Don't emit DW_AT_name */,
10725 die);
10726 return die;
10729 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
10730 an enumerated type. */
10732 static inline int
10733 type_is_enum (const_tree type)
10735 return TREE_CODE (type) == ENUMERAL_TYPE;
10738 /* Return the DBX register number described by a given RTL node. */
10740 static unsigned int
10741 dbx_reg_number (const_rtx rtl)
10743 unsigned regno = REGNO (rtl);
10745 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
10747 #ifdef LEAF_REG_REMAP
10748 if (crtl->uses_only_leaf_regs)
10750 int leaf_reg = LEAF_REG_REMAP (regno);
10751 if (leaf_reg != -1)
10752 regno = (unsigned) leaf_reg;
10754 #endif
10756 regno = DBX_REGISTER_NUMBER (regno);
10757 gcc_assert (regno != INVALID_REGNUM);
10758 return regno;
10761 /* Optionally add a DW_OP_piece term to a location description expression.
10762 DW_OP_piece is only added if the location description expression already
10763 doesn't end with DW_OP_piece. */
10765 static void
10766 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
10768 dw_loc_descr_ref loc;
10770 if (*list_head != NULL)
10772 /* Find the end of the chain. */
10773 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
10776 if (loc->dw_loc_opc != DW_OP_piece)
10777 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
10781 /* Return a location descriptor that designates a machine register or
10782 zero if there is none. */
10784 static dw_loc_descr_ref
10785 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
10787 rtx regs;
10789 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
10790 return 0;
10792 /* We only use "frame base" when we're sure we're talking about the
10793 post-prologue local stack frame. We do this by *not* running
10794 register elimination until this point, and recognizing the special
10795 argument pointer and soft frame pointer rtx's.
10796 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
10797 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
10798 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
10800 dw_loc_descr_ref result = NULL;
10802 if (dwarf_version >= 4 || !dwarf_strict)
10804 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
10805 initialized);
10806 if (result)
10807 add_loc_descr (&result,
10808 new_loc_descr (DW_OP_stack_value, 0, 0));
10810 return result;
10813 regs = targetm.dwarf_register_span (rtl);
10815 if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
10816 return multiple_reg_loc_descriptor (rtl, regs, initialized);
10817 else
10819 unsigned int dbx_regnum = dbx_reg_number (rtl);
10820 if (dbx_regnum == IGNORED_DWARF_REGNUM)
10821 return 0;
10822 return one_reg_loc_descriptor (dbx_regnum, initialized);
10826 /* Return a location descriptor that designates a machine register for
10827 a given hard register number. */
10829 static dw_loc_descr_ref
10830 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
10832 dw_loc_descr_ref reg_loc_descr;
10834 if (regno <= 31)
10835 reg_loc_descr
10836 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
10837 else
10838 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
10840 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10841 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10843 return reg_loc_descr;
10846 /* Given an RTL of a register, return a location descriptor that
10847 designates a value that spans more than one register. */
10849 static dw_loc_descr_ref
10850 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
10851 enum var_init_status initialized)
10853 int size, i;
10854 dw_loc_descr_ref loc_result = NULL;
10856 /* Simple, contiguous registers. */
10857 if (regs == NULL_RTX)
10859 unsigned reg = REGNO (rtl);
10860 int nregs;
10862 #ifdef LEAF_REG_REMAP
10863 if (crtl->uses_only_leaf_regs)
10865 int leaf_reg = LEAF_REG_REMAP (reg);
10866 if (leaf_reg != -1)
10867 reg = (unsigned) leaf_reg;
10869 #endif
10871 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
10872 nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
10874 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
10876 loc_result = NULL;
10877 while (nregs--)
10879 dw_loc_descr_ref t;
10881 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
10882 VAR_INIT_STATUS_INITIALIZED);
10883 add_loc_descr (&loc_result, t);
10884 add_loc_descr_op_piece (&loc_result, size);
10885 ++reg;
10887 return loc_result;
10890 /* Now onto stupid register sets in non contiguous locations. */
10892 gcc_assert (GET_CODE (regs) == PARALLEL);
10894 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10895 loc_result = NULL;
10897 for (i = 0; i < XVECLEN (regs, 0); ++i)
10899 dw_loc_descr_ref t;
10901 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
10902 VAR_INIT_STATUS_INITIALIZED);
10903 add_loc_descr (&loc_result, t);
10904 add_loc_descr_op_piece (&loc_result, size);
10907 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10908 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10909 return loc_result;
10912 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
10914 /* Return a location descriptor that designates a constant i,
10915 as a compound operation from constant (i >> shift), constant shift
10916 and DW_OP_shl. */
10918 static dw_loc_descr_ref
10919 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
10921 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
10922 add_loc_descr (&ret, int_loc_descriptor (shift));
10923 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
10924 return ret;
10927 /* Return a location descriptor that designates a constant. */
10929 static dw_loc_descr_ref
10930 int_loc_descriptor (HOST_WIDE_INT i)
10932 enum dwarf_location_atom op;
10934 /* Pick the smallest representation of a constant, rather than just
10935 defaulting to the LEB encoding. */
10936 if (i >= 0)
10938 int clz = clz_hwi (i);
10939 int ctz = ctz_hwi (i);
10940 if (i <= 31)
10941 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
10942 else if (i <= 0xff)
10943 op = DW_OP_const1u;
10944 else if (i <= 0xffff)
10945 op = DW_OP_const2u;
10946 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
10947 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
10948 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
10949 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
10950 while DW_OP_const4u is 5 bytes. */
10951 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
10952 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10953 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
10954 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
10955 while DW_OP_const4u is 5 bytes. */
10956 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
10957 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
10958 op = DW_OP_const4u;
10959 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10960 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
10961 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
10962 while DW_OP_constu of constant >= 0x100000000 takes at least
10963 6 bytes. */
10964 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
10965 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
10966 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
10967 >= HOST_BITS_PER_WIDE_INT)
10968 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
10969 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
10970 while DW_OP_constu takes in this case at least 6 bytes. */
10971 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
10972 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
10973 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
10974 && size_of_uleb128 (i) > 6)
10975 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
10976 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
10977 else
10978 op = DW_OP_constu;
10980 else
10982 if (i >= -0x80)
10983 op = DW_OP_const1s;
10984 else if (i >= -0x8000)
10985 op = DW_OP_const2s;
10986 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
10988 if (size_of_int_loc_descriptor (i) < 5)
10990 dw_loc_descr_ref ret = int_loc_descriptor (-i);
10991 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
10992 return ret;
10994 op = DW_OP_const4s;
10996 else
10998 if (size_of_int_loc_descriptor (i)
10999 < (unsigned long) 1 + size_of_sleb128 (i))
11001 dw_loc_descr_ref ret = int_loc_descriptor (-i);
11002 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11003 return ret;
11005 op = DW_OP_consts;
11009 return new_loc_descr (op, i, 0);
11012 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
11013 without actually allocating it. */
11015 static unsigned long
11016 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11018 return size_of_int_loc_descriptor (i >> shift)
11019 + size_of_int_loc_descriptor (shift)
11020 + 1;
11023 /* Return size_of_locs (int_loc_descriptor (i)) without
11024 actually allocating it. */
11026 static unsigned long
11027 size_of_int_loc_descriptor (HOST_WIDE_INT i)
11029 unsigned long s;
11031 if (i >= 0)
11033 int clz, ctz;
11034 if (i <= 31)
11035 return 1;
11036 else if (i <= 0xff)
11037 return 2;
11038 else if (i <= 0xffff)
11039 return 3;
11040 clz = clz_hwi (i);
11041 ctz = ctz_hwi (i);
11042 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11043 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11044 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11045 - clz - 5);
11046 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11047 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11048 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11049 - clz - 8);
11050 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11051 return 5;
11052 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
11053 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11054 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11055 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11056 - clz - 8);
11057 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11058 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
11059 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11060 - clz - 16);
11061 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11062 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11063 && s > 6)
11064 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11065 - clz - 32);
11066 else
11067 return 1 + s;
11069 else
11071 if (i >= -0x80)
11072 return 2;
11073 else if (i >= -0x8000)
11074 return 3;
11075 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11077 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11079 s = size_of_int_loc_descriptor (-i) + 1;
11080 if (s < 5)
11081 return s;
11083 return 5;
11085 else
11087 unsigned long r = 1 + size_of_sleb128 (i);
11088 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11090 s = size_of_int_loc_descriptor (-i) + 1;
11091 if (s < r)
11092 return s;
11094 return r;
11099 /* Return loc description representing "address" of integer value.
11100 This can appear only as toplevel expression. */
11102 static dw_loc_descr_ref
11103 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
11105 int litsize;
11106 dw_loc_descr_ref loc_result = NULL;
11108 if (!(dwarf_version >= 4 || !dwarf_strict))
11109 return NULL;
11111 litsize = size_of_int_loc_descriptor (i);
11112 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
11113 is more compact. For DW_OP_stack_value we need:
11114 litsize + 1 (DW_OP_stack_value)
11115 and for DW_OP_implicit_value:
11116 1 (DW_OP_implicit_value) + 1 (length) + size. */
11117 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
11119 loc_result = int_loc_descriptor (i);
11120 add_loc_descr (&loc_result,
11121 new_loc_descr (DW_OP_stack_value, 0, 0));
11122 return loc_result;
11125 loc_result = new_loc_descr (DW_OP_implicit_value,
11126 size, 0);
11127 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
11128 loc_result->dw_loc_oprnd2.v.val_int = i;
11129 return loc_result;
11132 /* Return a location descriptor that designates a base+offset location. */
11134 static dw_loc_descr_ref
11135 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
11136 enum var_init_status initialized)
11138 unsigned int regno;
11139 dw_loc_descr_ref result;
11140 dw_fde_ref fde = cfun->fde;
11142 /* We only use "frame base" when we're sure we're talking about the
11143 post-prologue local stack frame. We do this by *not* running
11144 register elimination until this point, and recognizing the special
11145 argument pointer and soft frame pointer rtx's. */
11146 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
11148 rtx elim = (ira_use_lra_p
11149 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
11150 : eliminate_regs (reg, VOIDmode, NULL_RTX));
11152 if (elim != reg)
11154 if (GET_CODE (elim) == PLUS)
11156 offset += INTVAL (XEXP (elim, 1));
11157 elim = XEXP (elim, 0);
11159 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
11160 && (elim == hard_frame_pointer_rtx
11161 || elim == stack_pointer_rtx))
11162 || elim == (frame_pointer_needed
11163 ? hard_frame_pointer_rtx
11164 : stack_pointer_rtx));
11166 /* If drap register is used to align stack, use frame
11167 pointer + offset to access stack variables. If stack
11168 is aligned without drap, use stack pointer + offset to
11169 access stack variables. */
11170 if (crtl->stack_realign_tried
11171 && reg == frame_pointer_rtx)
11173 int base_reg
11174 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
11175 ? HARD_FRAME_POINTER_REGNUM
11176 : REGNO (elim));
11177 return new_reg_loc_descr (base_reg, offset);
11180 gcc_assert (frame_pointer_fb_offset_valid);
11181 offset += frame_pointer_fb_offset;
11182 return new_loc_descr (DW_OP_fbreg, offset, 0);
11186 regno = REGNO (reg);
11187 #ifdef LEAF_REG_REMAP
11188 if (crtl->uses_only_leaf_regs)
11190 int leaf_reg = LEAF_REG_REMAP (regno);
11191 if (leaf_reg != -1)
11192 regno = (unsigned) leaf_reg;
11194 #endif
11195 regno = DWARF_FRAME_REGNUM (regno);
11197 if (!optimize && fde
11198 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
11200 /* Use cfa+offset to represent the location of arguments passed
11201 on the stack when drap is used to align stack.
11202 Only do this when not optimizing, for optimized code var-tracking
11203 is supposed to track where the arguments live and the register
11204 used as vdrap or drap in some spot might be used for something
11205 else in other part of the routine. */
11206 return new_loc_descr (DW_OP_fbreg, offset, 0);
11209 if (regno <= 31)
11210 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
11211 offset, 0);
11212 else
11213 result = new_loc_descr (DW_OP_bregx, regno, offset);
11215 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11216 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11218 return result;
11221 /* Return true if this RTL expression describes a base+offset calculation. */
11223 static inline int
11224 is_based_loc (const_rtx rtl)
11226 return (GET_CODE (rtl) == PLUS
11227 && ((REG_P (XEXP (rtl, 0))
11228 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
11229 && CONST_INT_P (XEXP (rtl, 1)))));
11232 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
11233 failed. */
11235 static dw_loc_descr_ref
11236 tls_mem_loc_descriptor (rtx mem)
11238 tree base;
11239 dw_loc_descr_ref loc_result;
11241 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
11242 return NULL;
11244 base = get_base_address (MEM_EXPR (mem));
11245 if (base == NULL
11246 || TREE_CODE (base) != VAR_DECL
11247 || !DECL_THREAD_LOCAL_P (base))
11248 return NULL;
11250 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
11251 if (loc_result == NULL)
11252 return NULL;
11254 if (MEM_OFFSET (mem))
11255 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
11257 return loc_result;
11260 /* Output debug info about reason why we failed to expand expression as dwarf
11261 expression. */
11263 static void
11264 expansion_failed (tree expr, rtx rtl, char const *reason)
11266 if (dump_file && (dump_flags & TDF_DETAILS))
11268 fprintf (dump_file, "Failed to expand as dwarf: ");
11269 if (expr)
11270 print_generic_expr (dump_file, expr, dump_flags);
11271 if (rtl)
11273 fprintf (dump_file, "\n");
11274 print_rtl (dump_file, rtl);
11276 fprintf (dump_file, "\nReason: %s\n", reason);
11280 /* Helper function for const_ok_for_output, called either directly
11281 or via for_each_rtx. */
11283 static int
11284 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
11286 rtx rtl = *rtlp;
11288 if (GET_CODE (rtl) == UNSPEC)
11290 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
11291 we can't express it in the debug info. */
11292 #ifdef ENABLE_CHECKING
11293 /* Don't complain about TLS UNSPECs, those are just too hard to
11294 delegitimize. Note this could be a non-decl SYMBOL_REF such as
11295 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
11296 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
11297 if (XVECLEN (rtl, 0) == 0
11298 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
11299 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE)
11300 inform (current_function_decl
11301 ? DECL_SOURCE_LOCATION (current_function_decl)
11302 : UNKNOWN_LOCATION,
11303 #if NUM_UNSPEC_VALUES > 0
11304 "non-delegitimized UNSPEC %s (%d) found in variable location",
11305 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
11306 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
11307 XINT (rtl, 1));
11308 #else
11309 "non-delegitimized UNSPEC %d found in variable location",
11310 XINT (rtl, 1));
11311 #endif
11312 #endif
11313 expansion_failed (NULL_TREE, rtl,
11314 "UNSPEC hasn't been delegitimized.\n");
11315 return 1;
11318 if (targetm.const_not_ok_for_debug_p (rtl))
11320 expansion_failed (NULL_TREE, rtl,
11321 "Expression rejected for debug by the backend.\n");
11322 return 1;
11325 /* FIXME: Refer to PR60655. It is possible for simplification
11326 of rtl expressions in var tracking to produce such expressions.
11327 We should really identify / validate expressions
11328 enclosed in CONST that can be handled by assemblers on various
11329 targets and only handle legitimate cases here. */
11330 if (GET_CODE (rtl) != SYMBOL_REF)
11332 if (GET_CODE (rtl) == NOT)
11333 return 1;
11335 return 0;
11338 if (CONSTANT_POOL_ADDRESS_P (rtl))
11340 bool marked;
11341 get_pool_constant_mark (rtl, &marked);
11342 /* If all references to this pool constant were optimized away,
11343 it was not output and thus we can't represent it. */
11344 if (!marked)
11346 expansion_failed (NULL_TREE, rtl,
11347 "Constant was removed from constant pool.\n");
11348 return 1;
11352 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11353 return 1;
11355 /* Avoid references to external symbols in debug info, on several targets
11356 the linker might even refuse to link when linking a shared library,
11357 and in many other cases the relocations for .debug_info/.debug_loc are
11358 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
11359 to be defined within the same shared library or executable are fine. */
11360 if (SYMBOL_REF_EXTERNAL_P (rtl))
11362 tree decl = SYMBOL_REF_DECL (rtl);
11364 if (decl == NULL || !targetm.binds_local_p (decl))
11366 expansion_failed (NULL_TREE, rtl,
11367 "Symbol not defined in current TU.\n");
11368 return 1;
11372 return 0;
11375 /* Return true if constant RTL can be emitted in DW_OP_addr or
11376 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
11377 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
11379 static bool
11380 const_ok_for_output (rtx rtl)
11382 if (GET_CODE (rtl) == SYMBOL_REF)
11383 return const_ok_for_output_1 (&rtl, NULL) == 0;
11385 if (GET_CODE (rtl) == CONST)
11386 return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
11388 return true;
11391 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
11392 if possible, NULL otherwise. */
11394 static dw_die_ref
11395 base_type_for_mode (enum machine_mode mode, bool unsignedp)
11397 dw_die_ref type_die;
11398 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11400 if (type == NULL)
11401 return NULL;
11402 switch (TREE_CODE (type))
11404 case INTEGER_TYPE:
11405 case REAL_TYPE:
11406 break;
11407 default:
11408 return NULL;
11410 type_die = lookup_type_die (type);
11411 if (!type_die)
11412 type_die = modified_type_die (type, false, false, comp_unit_die ());
11413 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
11414 return NULL;
11415 return type_die;
11418 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
11419 type matching MODE, or, if MODE is narrower than or as wide as
11420 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
11421 possible. */
11423 static dw_loc_descr_ref
11424 convert_descriptor_to_mode (enum machine_mode mode, dw_loc_descr_ref op)
11426 enum machine_mode outer_mode = mode;
11427 dw_die_ref type_die;
11428 dw_loc_descr_ref cvt;
11430 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11432 add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
11433 return op;
11435 type_die = base_type_for_mode (outer_mode, 1);
11436 if (type_die == NULL)
11437 return NULL;
11438 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11439 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11440 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11441 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11442 add_loc_descr (&op, cvt);
11443 return op;
11446 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
11448 static dw_loc_descr_ref
11449 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
11450 dw_loc_descr_ref op1)
11452 dw_loc_descr_ref ret = op0;
11453 add_loc_descr (&ret, op1);
11454 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11455 if (STORE_FLAG_VALUE != 1)
11457 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
11458 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
11460 return ret;
11463 /* Return location descriptor for signed comparison OP RTL. */
11465 static dw_loc_descr_ref
11466 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11467 enum machine_mode mem_mode)
11469 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11470 dw_loc_descr_ref op0, op1;
11471 int shift;
11473 if (op_mode == VOIDmode)
11474 op_mode = GET_MODE (XEXP (rtl, 1));
11475 if (op_mode == VOIDmode)
11476 return NULL;
11478 if (dwarf_strict
11479 && (GET_MODE_CLASS (op_mode) != MODE_INT
11480 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
11481 return NULL;
11483 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11484 VAR_INIT_STATUS_INITIALIZED);
11485 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11486 VAR_INIT_STATUS_INITIALIZED);
11488 if (op0 == NULL || op1 == NULL)
11489 return NULL;
11491 if (GET_MODE_CLASS (op_mode) != MODE_INT
11492 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11493 return compare_loc_descriptor (op, op0, op1);
11495 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11497 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
11498 dw_loc_descr_ref cvt;
11500 if (type_die == NULL)
11501 return NULL;
11502 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11503 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11504 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11505 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11506 add_loc_descr (&op0, cvt);
11507 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11508 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11509 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11510 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11511 add_loc_descr (&op1, cvt);
11512 return compare_loc_descriptor (op, op0, op1);
11515 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
11516 /* For eq/ne, if the operands are known to be zero-extended,
11517 there is no need to do the fancy shifting up. */
11518 if (op == DW_OP_eq || op == DW_OP_ne)
11520 dw_loc_descr_ref last0, last1;
11521 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11523 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11525 /* deref_size zero extends, and for constants we can check
11526 whether they are zero extended or not. */
11527 if (((last0->dw_loc_opc == DW_OP_deref_size
11528 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11529 || (CONST_INT_P (XEXP (rtl, 0))
11530 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
11531 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
11532 && ((last1->dw_loc_opc == DW_OP_deref_size
11533 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11534 || (CONST_INT_P (XEXP (rtl, 1))
11535 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
11536 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
11537 return compare_loc_descriptor (op, op0, op1);
11539 /* EQ/NE comparison against constant in narrower type than
11540 DWARF2_ADDR_SIZE can be performed either as
11541 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
11542 DW_OP_{eq,ne}
11544 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
11545 DW_OP_{eq,ne}. Pick whatever is shorter. */
11546 if (CONST_INT_P (XEXP (rtl, 1))
11547 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
11548 && (size_of_int_loc_descriptor (shift) + 1
11549 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
11550 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
11551 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11552 & GET_MODE_MASK (op_mode))))
11554 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
11555 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11556 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11557 & GET_MODE_MASK (op_mode));
11558 return compare_loc_descriptor (op, op0, op1);
11561 add_loc_descr (&op0, int_loc_descriptor (shift));
11562 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11563 if (CONST_INT_P (XEXP (rtl, 1)))
11564 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
11565 else
11567 add_loc_descr (&op1, int_loc_descriptor (shift));
11568 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11570 return compare_loc_descriptor (op, op0, op1);
11573 /* Return location descriptor for unsigned comparison OP RTL. */
11575 static dw_loc_descr_ref
11576 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11577 enum machine_mode mem_mode)
11579 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11580 dw_loc_descr_ref op0, op1;
11582 if (op_mode == VOIDmode)
11583 op_mode = GET_MODE (XEXP (rtl, 1));
11584 if (op_mode == VOIDmode)
11585 return NULL;
11586 if (GET_MODE_CLASS (op_mode) != MODE_INT)
11587 return NULL;
11589 if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11590 return NULL;
11592 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11593 VAR_INIT_STATUS_INITIALIZED);
11594 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11595 VAR_INIT_STATUS_INITIALIZED);
11597 if (op0 == NULL || op1 == NULL)
11598 return NULL;
11600 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
11602 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
11603 dw_loc_descr_ref last0, last1;
11604 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11606 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11608 if (CONST_INT_P (XEXP (rtl, 0)))
11609 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
11610 /* deref_size zero extends, so no need to mask it again. */
11611 else if (last0->dw_loc_opc != DW_OP_deref_size
11612 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11614 add_loc_descr (&op0, int_loc_descriptor (mask));
11615 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11617 if (CONST_INT_P (XEXP (rtl, 1)))
11618 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
11619 /* deref_size zero extends, so no need to mask it again. */
11620 else if (last1->dw_loc_opc != DW_OP_deref_size
11621 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11623 add_loc_descr (&op1, int_loc_descriptor (mask));
11624 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11627 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11629 HOST_WIDE_INT bias = 1;
11630 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11631 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11632 if (CONST_INT_P (XEXP (rtl, 1)))
11633 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
11634 + INTVAL (XEXP (rtl, 1)));
11635 else
11636 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
11637 bias, 0));
11639 return compare_loc_descriptor (op, op0, op1);
11642 /* Return location descriptor for {U,S}{MIN,MAX}. */
11644 static dw_loc_descr_ref
11645 minmax_loc_descriptor (rtx rtl, enum machine_mode mode,
11646 enum machine_mode mem_mode)
11648 enum dwarf_location_atom op;
11649 dw_loc_descr_ref op0, op1, ret;
11650 dw_loc_descr_ref bra_node, drop_node;
11652 if (dwarf_strict
11653 && (GET_MODE_CLASS (mode) != MODE_INT
11654 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
11655 return NULL;
11657 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11658 VAR_INIT_STATUS_INITIALIZED);
11659 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11660 VAR_INIT_STATUS_INITIALIZED);
11662 if (op0 == NULL || op1 == NULL)
11663 return NULL;
11665 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
11666 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
11667 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
11668 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
11670 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11672 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11673 add_loc_descr (&op0, int_loc_descriptor (mask));
11674 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11675 add_loc_descr (&op1, int_loc_descriptor (mask));
11676 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11678 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
11680 HOST_WIDE_INT bias = 1;
11681 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11682 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11683 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11686 else if (GET_MODE_CLASS (mode) == MODE_INT
11687 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11689 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
11690 add_loc_descr (&op0, int_loc_descriptor (shift));
11691 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11692 add_loc_descr (&op1, int_loc_descriptor (shift));
11693 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11695 else if (GET_MODE_CLASS (mode) == MODE_INT
11696 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11698 dw_die_ref type_die = base_type_for_mode (mode, 0);
11699 dw_loc_descr_ref cvt;
11700 if (type_die == NULL)
11701 return NULL;
11702 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11703 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11704 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11705 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11706 add_loc_descr (&op0, cvt);
11707 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11708 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11709 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11710 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11711 add_loc_descr (&op1, cvt);
11714 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
11715 op = DW_OP_lt;
11716 else
11717 op = DW_OP_gt;
11718 ret = op0;
11719 add_loc_descr (&ret, op1);
11720 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11721 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
11722 add_loc_descr (&ret, bra_node);
11723 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11724 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
11725 add_loc_descr (&ret, drop_node);
11726 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
11727 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
11728 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
11729 && GET_MODE_CLASS (mode) == MODE_INT
11730 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11731 ret = convert_descriptor_to_mode (mode, ret);
11732 return ret;
11735 /* Helper function for mem_loc_descriptor. Perform OP binary op,
11736 but after converting arguments to type_die, afterwards
11737 convert back to unsigned. */
11739 static dw_loc_descr_ref
11740 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
11741 enum machine_mode mode, enum machine_mode mem_mode)
11743 dw_loc_descr_ref cvt, op0, op1;
11745 if (type_die == NULL)
11746 return NULL;
11747 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11748 VAR_INIT_STATUS_INITIALIZED);
11749 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11750 VAR_INIT_STATUS_INITIALIZED);
11751 if (op0 == NULL || op1 == NULL)
11752 return NULL;
11753 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11754 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11755 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11756 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11757 add_loc_descr (&op0, cvt);
11758 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11759 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11760 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11761 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11762 add_loc_descr (&op1, cvt);
11763 add_loc_descr (&op0, op1);
11764 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
11765 return convert_descriptor_to_mode (mode, op0);
11768 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
11769 const0 is DW_OP_lit0 or corresponding typed constant,
11770 const1 is DW_OP_lit1 or corresponding typed constant
11771 and constMSB is constant with just the MSB bit set
11772 for the mode):
11773 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11774 L1: const0 DW_OP_swap
11775 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
11776 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11777 L3: DW_OP_drop
11778 L4: DW_OP_nop
11780 CTZ is similar:
11781 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11782 L1: const0 DW_OP_swap
11783 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11784 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11785 L3: DW_OP_drop
11786 L4: DW_OP_nop
11788 FFS is similar:
11789 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
11790 L1: const1 DW_OP_swap
11791 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11792 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11793 L3: DW_OP_drop
11794 L4: DW_OP_nop */
11796 static dw_loc_descr_ref
11797 clz_loc_descriptor (rtx rtl, enum machine_mode mode,
11798 enum machine_mode mem_mode)
11800 dw_loc_descr_ref op0, ret, tmp;
11801 HOST_WIDE_INT valv;
11802 dw_loc_descr_ref l1jump, l1label;
11803 dw_loc_descr_ref l2jump, l2label;
11804 dw_loc_descr_ref l3jump, l3label;
11805 dw_loc_descr_ref l4jump, l4label;
11806 rtx msb;
11808 if (GET_MODE_CLASS (mode) != MODE_INT
11809 || GET_MODE (XEXP (rtl, 0)) != mode
11810 || (GET_CODE (rtl) == CLZ
11811 && GET_MODE_BITSIZE (mode) > HOST_BITS_PER_DOUBLE_INT))
11812 return NULL;
11814 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11815 VAR_INIT_STATUS_INITIALIZED);
11816 if (op0 == NULL)
11817 return NULL;
11818 ret = op0;
11819 if (GET_CODE (rtl) == CLZ)
11821 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11822 valv = GET_MODE_BITSIZE (mode);
11824 else if (GET_CODE (rtl) == FFS)
11825 valv = 0;
11826 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11827 valv = GET_MODE_BITSIZE (mode);
11828 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11829 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
11830 add_loc_descr (&ret, l1jump);
11831 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
11832 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
11833 VAR_INIT_STATUS_INITIALIZED);
11834 if (tmp == NULL)
11835 return NULL;
11836 add_loc_descr (&ret, tmp);
11837 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
11838 add_loc_descr (&ret, l4jump);
11839 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
11840 ? const1_rtx : const0_rtx,
11841 mode, mem_mode,
11842 VAR_INIT_STATUS_INITIALIZED);
11843 if (l1label == NULL)
11844 return NULL;
11845 add_loc_descr (&ret, l1label);
11846 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11847 l2label = new_loc_descr (DW_OP_dup, 0, 0);
11848 add_loc_descr (&ret, l2label);
11849 if (GET_CODE (rtl) != CLZ)
11850 msb = const1_rtx;
11851 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11852 msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
11853 << (GET_MODE_BITSIZE (mode) - 1));
11854 else
11855 msb = immed_double_const (0, (unsigned HOST_WIDE_INT) 1
11856 << (GET_MODE_BITSIZE (mode)
11857 - HOST_BITS_PER_WIDE_INT - 1), mode);
11858 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
11859 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
11860 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
11861 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
11862 else
11863 tmp = mem_loc_descriptor (msb, mode, mem_mode,
11864 VAR_INIT_STATUS_INITIALIZED);
11865 if (tmp == NULL)
11866 return NULL;
11867 add_loc_descr (&ret, tmp);
11868 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11869 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
11870 add_loc_descr (&ret, l3jump);
11871 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11872 VAR_INIT_STATUS_INITIALIZED);
11873 if (tmp == NULL)
11874 return NULL;
11875 add_loc_descr (&ret, tmp);
11876 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
11877 ? DW_OP_shl : DW_OP_shr, 0, 0));
11878 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11879 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
11880 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11881 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
11882 add_loc_descr (&ret, l2jump);
11883 l3label = new_loc_descr (DW_OP_drop, 0, 0);
11884 add_loc_descr (&ret, l3label);
11885 l4label = new_loc_descr (DW_OP_nop, 0, 0);
11886 add_loc_descr (&ret, l4label);
11887 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11888 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11889 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11890 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11891 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11892 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
11893 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11894 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
11895 return ret;
11898 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
11899 const1 is DW_OP_lit1 or corresponding typed constant):
11900 const0 DW_OP_swap
11901 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
11902 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
11903 L2: DW_OP_drop
11905 PARITY is similar:
11906 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
11907 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
11908 L2: DW_OP_drop */
11910 static dw_loc_descr_ref
11911 popcount_loc_descriptor (rtx rtl, enum machine_mode mode,
11912 enum machine_mode mem_mode)
11914 dw_loc_descr_ref op0, ret, tmp;
11915 dw_loc_descr_ref l1jump, l1label;
11916 dw_loc_descr_ref l2jump, l2label;
11918 if (GET_MODE_CLASS (mode) != MODE_INT
11919 || GET_MODE (XEXP (rtl, 0)) != mode)
11920 return NULL;
11922 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11923 VAR_INIT_STATUS_INITIALIZED);
11924 if (op0 == NULL)
11925 return NULL;
11926 ret = op0;
11927 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11928 VAR_INIT_STATUS_INITIALIZED);
11929 if (tmp == NULL)
11930 return NULL;
11931 add_loc_descr (&ret, tmp);
11932 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11933 l1label = new_loc_descr (DW_OP_dup, 0, 0);
11934 add_loc_descr (&ret, l1label);
11935 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
11936 add_loc_descr (&ret, l2jump);
11937 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11938 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
11939 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11940 VAR_INIT_STATUS_INITIALIZED);
11941 if (tmp == NULL)
11942 return NULL;
11943 add_loc_descr (&ret, tmp);
11944 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11945 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
11946 ? DW_OP_plus : DW_OP_xor, 0, 0));
11947 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11948 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11949 VAR_INIT_STATUS_INITIALIZED);
11950 add_loc_descr (&ret, tmp);
11951 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11952 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
11953 add_loc_descr (&ret, l1jump);
11954 l2label = new_loc_descr (DW_OP_drop, 0, 0);
11955 add_loc_descr (&ret, l2label);
11956 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11957 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11958 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11959 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11960 return ret;
11963 /* BSWAP (constS is initial shift count, either 56 or 24):
11964 constS const0
11965 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
11966 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
11967 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
11968 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
11969 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
11971 static dw_loc_descr_ref
11972 bswap_loc_descriptor (rtx rtl, enum machine_mode mode,
11973 enum machine_mode mem_mode)
11975 dw_loc_descr_ref op0, ret, tmp;
11976 dw_loc_descr_ref l1jump, l1label;
11977 dw_loc_descr_ref l2jump, l2label;
11979 if (GET_MODE_CLASS (mode) != MODE_INT
11980 || BITS_PER_UNIT != 8
11981 || (GET_MODE_BITSIZE (mode) != 32
11982 && GET_MODE_BITSIZE (mode) != 64))
11983 return NULL;
11985 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11986 VAR_INIT_STATUS_INITIALIZED);
11987 if (op0 == NULL)
11988 return NULL;
11990 ret = op0;
11991 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
11992 mode, mem_mode,
11993 VAR_INIT_STATUS_INITIALIZED);
11994 if (tmp == NULL)
11995 return NULL;
11996 add_loc_descr (&ret, tmp);
11997 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11998 VAR_INIT_STATUS_INITIALIZED);
11999 if (tmp == NULL)
12000 return NULL;
12001 add_loc_descr (&ret, tmp);
12002 l1label = new_loc_descr (DW_OP_pick, 2, 0);
12003 add_loc_descr (&ret, l1label);
12004 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12005 mode, mem_mode,
12006 VAR_INIT_STATUS_INITIALIZED);
12007 add_loc_descr (&ret, tmp);
12008 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
12009 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12010 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12011 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
12012 VAR_INIT_STATUS_INITIALIZED);
12013 if (tmp == NULL)
12014 return NULL;
12015 add_loc_descr (&ret, tmp);
12016 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12017 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
12018 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12019 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12020 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12021 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12022 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12023 VAR_INIT_STATUS_INITIALIZED);
12024 add_loc_descr (&ret, tmp);
12025 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
12026 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12027 add_loc_descr (&ret, l2jump);
12028 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
12029 VAR_INIT_STATUS_INITIALIZED);
12030 add_loc_descr (&ret, tmp);
12031 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12032 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12033 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12034 add_loc_descr (&ret, l1jump);
12035 l2label = new_loc_descr (DW_OP_drop, 0, 0);
12036 add_loc_descr (&ret, l2label);
12037 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12038 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12039 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12040 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12041 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12042 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12043 return ret;
12046 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
12047 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12048 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
12049 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
12051 ROTATERT is similar:
12052 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
12053 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12054 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
12056 static dw_loc_descr_ref
12057 rotate_loc_descriptor (rtx rtl, enum machine_mode mode,
12058 enum machine_mode mem_mode)
12060 rtx rtlop1 = XEXP (rtl, 1);
12061 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
12062 int i;
12064 if (GET_MODE_CLASS (mode) != MODE_INT)
12065 return NULL;
12067 if (GET_MODE (rtlop1) != VOIDmode
12068 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
12069 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12070 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12071 VAR_INIT_STATUS_INITIALIZED);
12072 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12073 VAR_INIT_STATUS_INITIALIZED);
12074 if (op0 == NULL || op1 == NULL)
12075 return NULL;
12076 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12077 for (i = 0; i < 2; i++)
12079 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
12080 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
12081 mode, mem_mode,
12082 VAR_INIT_STATUS_INITIALIZED);
12083 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12084 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12085 ? DW_OP_const4u
12086 : HOST_BITS_PER_WIDE_INT == 64
12087 ? DW_OP_const8u : DW_OP_constu,
12088 GET_MODE_MASK (mode), 0);
12089 else
12090 mask[i] = NULL;
12091 if (mask[i] == NULL)
12092 return NULL;
12093 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
12095 ret = op0;
12096 add_loc_descr (&ret, op1);
12097 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12098 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12099 if (GET_CODE (rtl) == ROTATERT)
12101 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12102 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12103 GET_MODE_BITSIZE (mode), 0));
12105 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12106 if (mask[0] != NULL)
12107 add_loc_descr (&ret, mask[0]);
12108 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12109 if (mask[1] != NULL)
12111 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12112 add_loc_descr (&ret, mask[1]);
12113 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12115 if (GET_CODE (rtl) == ROTATE)
12117 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12118 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12119 GET_MODE_BITSIZE (mode), 0));
12121 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12122 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12123 return ret;
12126 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
12127 for DEBUG_PARAMETER_REF RTL. */
12129 static dw_loc_descr_ref
12130 parameter_ref_descriptor (rtx rtl)
12132 dw_loc_descr_ref ret;
12133 dw_die_ref ref;
12135 if (dwarf_strict)
12136 return NULL;
12137 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
12138 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
12139 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
12140 if (ref)
12142 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12143 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
12144 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
12146 else
12148 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
12149 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
12151 return ret;
12154 /* The following routine converts the RTL for a variable or parameter
12155 (resident in memory) into an equivalent Dwarf representation of a
12156 mechanism for getting the address of that same variable onto the top of a
12157 hypothetical "address evaluation" stack.
12159 When creating memory location descriptors, we are effectively transforming
12160 the RTL for a memory-resident object into its Dwarf postfix expression
12161 equivalent. This routine recursively descends an RTL tree, turning
12162 it into Dwarf postfix code as it goes.
12164 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
12166 MEM_MODE is the mode of the memory reference, needed to handle some
12167 autoincrement addressing modes.
12169 Return 0 if we can't represent the location. */
12171 dw_loc_descr_ref
12172 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
12173 enum machine_mode mem_mode,
12174 enum var_init_status initialized)
12176 dw_loc_descr_ref mem_loc_result = NULL;
12177 enum dwarf_location_atom op;
12178 dw_loc_descr_ref op0, op1;
12179 rtx inner = NULL_RTX;
12181 if (mode == VOIDmode)
12182 mode = GET_MODE (rtl);
12184 /* Note that for a dynamically sized array, the location we will generate a
12185 description of here will be the lowest numbered location which is
12186 actually within the array. That's *not* necessarily the same as the
12187 zeroth element of the array. */
12189 rtl = targetm.delegitimize_address (rtl);
12191 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
12192 return NULL;
12194 switch (GET_CODE (rtl))
12196 case POST_INC:
12197 case POST_DEC:
12198 case POST_MODIFY:
12199 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
12201 case SUBREG:
12202 /* The case of a subreg may arise when we have a local (register)
12203 variable or a formal (register) parameter which doesn't quite fill
12204 up an entire register. For now, just assume that it is
12205 legitimate to make the Dwarf info refer to the whole register which
12206 contains the given subreg. */
12207 if (!subreg_lowpart_p (rtl))
12208 break;
12209 inner = SUBREG_REG (rtl);
12210 case TRUNCATE:
12211 if (inner == NULL_RTX)
12212 inner = XEXP (rtl, 0);
12213 if (GET_MODE_CLASS (mode) == MODE_INT
12214 && GET_MODE_CLASS (GET_MODE (inner)) == MODE_INT
12215 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12216 #ifdef POINTERS_EXTEND_UNSIGNED
12217 || (mode == Pmode && mem_mode != VOIDmode)
12218 #endif
12220 && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
12222 mem_loc_result = mem_loc_descriptor (inner,
12223 GET_MODE (inner),
12224 mem_mode, initialized);
12225 break;
12227 if (dwarf_strict)
12228 break;
12229 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
12230 break;
12231 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
12232 && (GET_MODE_CLASS (mode) != MODE_INT
12233 || GET_MODE_CLASS (GET_MODE (inner)) != MODE_INT))
12234 break;
12235 else
12237 dw_die_ref type_die;
12238 dw_loc_descr_ref cvt;
12240 mem_loc_result = mem_loc_descriptor (inner,
12241 GET_MODE (inner),
12242 mem_mode, initialized);
12243 if (mem_loc_result == NULL)
12244 break;
12245 type_die = base_type_for_mode (mode,
12246 GET_MODE_CLASS (mode) == MODE_INT);
12247 if (type_die == NULL)
12249 mem_loc_result = NULL;
12250 break;
12252 if (GET_MODE_SIZE (mode)
12253 != GET_MODE_SIZE (GET_MODE (inner)))
12254 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12255 else
12256 cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
12257 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12258 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12259 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12260 add_loc_descr (&mem_loc_result, cvt);
12262 break;
12264 case REG:
12265 if (GET_MODE_CLASS (mode) != MODE_INT
12266 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12267 && rtl != arg_pointer_rtx
12268 && rtl != frame_pointer_rtx
12269 #ifdef POINTERS_EXTEND_UNSIGNED
12270 && (mode != Pmode || mem_mode == VOIDmode)
12271 #endif
12274 dw_die_ref type_die;
12275 unsigned int dbx_regnum;
12277 if (dwarf_strict)
12278 break;
12279 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
12280 break;
12281 type_die = base_type_for_mode (mode,
12282 GET_MODE_CLASS (mode) == MODE_INT);
12283 if (type_die == NULL)
12284 break;
12286 dbx_regnum = dbx_reg_number (rtl);
12287 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12288 break;
12289 mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
12290 dbx_regnum, 0);
12291 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12292 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12293 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
12294 break;
12296 /* Whenever a register number forms a part of the description of the
12297 method for calculating the (dynamic) address of a memory resident
12298 object, DWARF rules require the register number be referred to as
12299 a "base register". This distinction is not based in any way upon
12300 what category of register the hardware believes the given register
12301 belongs to. This is strictly DWARF terminology we're dealing with
12302 here. Note that in cases where the location of a memory-resident
12303 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
12304 OP_CONST (0)) the actual DWARF location descriptor that we generate
12305 may just be OP_BASEREG (basereg). This may look deceptively like
12306 the object in question was allocated to a register (rather than in
12307 memory) so DWARF consumers need to be aware of the subtle
12308 distinction between OP_REG and OP_BASEREG. */
12309 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
12310 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
12311 else if (stack_realign_drap
12312 && crtl->drap_reg
12313 && crtl->args.internal_arg_pointer == rtl
12314 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
12316 /* If RTL is internal_arg_pointer, which has been optimized
12317 out, use DRAP instead. */
12318 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
12319 VAR_INIT_STATUS_INITIALIZED);
12321 break;
12323 case SIGN_EXTEND:
12324 case ZERO_EXTEND:
12325 if (GET_MODE_CLASS (mode) != MODE_INT)
12326 break;
12327 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12328 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12329 if (op0 == 0)
12330 break;
12331 else if (GET_CODE (rtl) == ZERO_EXTEND
12332 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12333 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12334 < HOST_BITS_PER_WIDE_INT
12335 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
12336 to expand zero extend as two shifts instead of
12337 masking. */
12338 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
12340 enum machine_mode imode = GET_MODE (XEXP (rtl, 0));
12341 mem_loc_result = op0;
12342 add_loc_descr (&mem_loc_result,
12343 int_loc_descriptor (GET_MODE_MASK (imode)));
12344 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
12346 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12348 int shift = DWARF2_ADDR_SIZE
12349 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
12350 shift *= BITS_PER_UNIT;
12351 if (GET_CODE (rtl) == SIGN_EXTEND)
12352 op = DW_OP_shra;
12353 else
12354 op = DW_OP_shr;
12355 mem_loc_result = op0;
12356 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12357 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12358 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12359 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12361 else if (!dwarf_strict)
12363 dw_die_ref type_die1, type_die2;
12364 dw_loc_descr_ref cvt;
12366 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12367 GET_CODE (rtl) == ZERO_EXTEND);
12368 if (type_die1 == NULL)
12369 break;
12370 type_die2 = base_type_for_mode (mode, 1);
12371 if (type_die2 == NULL)
12372 break;
12373 mem_loc_result = op0;
12374 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12375 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12376 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
12377 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12378 add_loc_descr (&mem_loc_result, cvt);
12379 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12380 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12381 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
12382 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12383 add_loc_descr (&mem_loc_result, cvt);
12385 break;
12387 case MEM:
12389 rtx new_rtl = avoid_constant_pool_reference (rtl);
12390 if (new_rtl != rtl)
12392 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
12393 initialized);
12394 if (mem_loc_result != NULL)
12395 return mem_loc_result;
12398 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
12399 get_address_mode (rtl), mode,
12400 VAR_INIT_STATUS_INITIALIZED);
12401 if (mem_loc_result == NULL)
12402 mem_loc_result = tls_mem_loc_descriptor (rtl);
12403 if (mem_loc_result != NULL)
12405 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12406 || GET_MODE_CLASS (mode) != MODE_INT)
12408 dw_die_ref type_die;
12409 dw_loc_descr_ref deref;
12411 if (dwarf_strict)
12412 return NULL;
12413 type_die
12414 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
12415 if (type_die == NULL)
12416 return NULL;
12417 deref = new_loc_descr (DW_OP_GNU_deref_type,
12418 GET_MODE_SIZE (mode), 0);
12419 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12420 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12421 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
12422 add_loc_descr (&mem_loc_result, deref);
12424 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12425 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
12426 else
12427 add_loc_descr (&mem_loc_result,
12428 new_loc_descr (DW_OP_deref_size,
12429 GET_MODE_SIZE (mode), 0));
12431 break;
12433 case LO_SUM:
12434 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
12436 case LABEL_REF:
12437 /* Some ports can transform a symbol ref into a label ref, because
12438 the symbol ref is too far away and has to be dumped into a constant
12439 pool. */
12440 case CONST:
12441 case SYMBOL_REF:
12442 if (GET_MODE_CLASS (mode) != MODE_INT
12443 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12444 #ifdef POINTERS_EXTEND_UNSIGNED
12445 && (mode != Pmode || mem_mode == VOIDmode)
12446 #endif
12448 break;
12449 if (GET_CODE (rtl) == SYMBOL_REF
12450 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12452 dw_loc_descr_ref temp;
12454 /* If this is not defined, we have no way to emit the data. */
12455 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
12456 break;
12458 temp = new_addr_loc_descr (rtl, dtprel_true);
12460 mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
12461 add_loc_descr (&mem_loc_result, temp);
12463 break;
12466 if (!const_ok_for_output (rtl))
12467 break;
12469 symref:
12470 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
12471 vec_safe_push (used_rtx_array, rtl);
12472 break;
12474 case CONCAT:
12475 case CONCATN:
12476 case VAR_LOCATION:
12477 case DEBUG_IMPLICIT_PTR:
12478 expansion_failed (NULL_TREE, rtl,
12479 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
12480 return 0;
12482 case ENTRY_VALUE:
12483 if (dwarf_strict)
12484 return NULL;
12485 if (REG_P (ENTRY_VALUE_EXP (rtl)))
12487 if (GET_MODE_CLASS (mode) != MODE_INT
12488 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12489 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12490 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12491 else
12493 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
12494 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12495 return NULL;
12496 op0 = one_reg_loc_descriptor (dbx_regnum,
12497 VAR_INIT_STATUS_INITIALIZED);
12500 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
12501 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
12503 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12504 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12505 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
12506 return NULL;
12508 else
12509 gcc_unreachable ();
12510 if (op0 == NULL)
12511 return NULL;
12512 mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
12513 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
12514 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
12515 break;
12517 case DEBUG_PARAMETER_REF:
12518 mem_loc_result = parameter_ref_descriptor (rtl);
12519 break;
12521 case PRE_MODIFY:
12522 /* Extract the PLUS expression nested inside and fall into
12523 PLUS code below. */
12524 rtl = XEXP (rtl, 1);
12525 goto plus;
12527 case PRE_INC:
12528 case PRE_DEC:
12529 /* Turn these into a PLUS expression and fall into the PLUS code
12530 below. */
12531 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
12532 gen_int_mode (GET_CODE (rtl) == PRE_INC
12533 ? GET_MODE_UNIT_SIZE (mem_mode)
12534 : -GET_MODE_UNIT_SIZE (mem_mode),
12535 mode));
12537 /* ... fall through ... */
12539 case PLUS:
12540 plus:
12541 if (is_based_loc (rtl)
12542 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12543 || XEXP (rtl, 0) == arg_pointer_rtx
12544 || XEXP (rtl, 0) == frame_pointer_rtx)
12545 && GET_MODE_CLASS (mode) == MODE_INT)
12546 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
12547 INTVAL (XEXP (rtl, 1)),
12548 VAR_INIT_STATUS_INITIALIZED);
12549 else
12551 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12552 VAR_INIT_STATUS_INITIALIZED);
12553 if (mem_loc_result == 0)
12554 break;
12556 if (CONST_INT_P (XEXP (rtl, 1))
12557 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12558 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
12559 else
12561 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12562 VAR_INIT_STATUS_INITIALIZED);
12563 if (op1 == 0)
12564 return NULL;
12565 add_loc_descr (&mem_loc_result, op1);
12566 add_loc_descr (&mem_loc_result,
12567 new_loc_descr (DW_OP_plus, 0, 0));
12570 break;
12572 /* If a pseudo-reg is optimized away, it is possible for it to
12573 be replaced with a MEM containing a multiply or shift. */
12574 case MINUS:
12575 op = DW_OP_minus;
12576 goto do_binop;
12578 case MULT:
12579 op = DW_OP_mul;
12580 goto do_binop;
12582 case DIV:
12583 if (!dwarf_strict
12584 && GET_MODE_CLASS (mode) == MODE_INT
12585 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12587 mem_loc_result = typed_binop (DW_OP_div, rtl,
12588 base_type_for_mode (mode, 0),
12589 mode, mem_mode);
12590 break;
12592 op = DW_OP_div;
12593 goto do_binop;
12595 case UMOD:
12596 op = DW_OP_mod;
12597 goto do_binop;
12599 case ASHIFT:
12600 op = DW_OP_shl;
12601 goto do_shift;
12603 case ASHIFTRT:
12604 op = DW_OP_shra;
12605 goto do_shift;
12607 case LSHIFTRT:
12608 op = DW_OP_shr;
12609 goto do_shift;
12611 do_shift:
12612 if (GET_MODE_CLASS (mode) != MODE_INT)
12613 break;
12614 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12615 VAR_INIT_STATUS_INITIALIZED);
12617 rtx rtlop1 = XEXP (rtl, 1);
12618 if (GET_MODE (rtlop1) != VOIDmode
12619 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
12620 < GET_MODE_BITSIZE (mode))
12621 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12622 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12623 VAR_INIT_STATUS_INITIALIZED);
12626 if (op0 == 0 || op1 == 0)
12627 break;
12629 mem_loc_result = op0;
12630 add_loc_descr (&mem_loc_result, op1);
12631 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12632 break;
12634 case AND:
12635 op = DW_OP_and;
12636 goto do_binop;
12638 case IOR:
12639 op = DW_OP_or;
12640 goto do_binop;
12642 case XOR:
12643 op = DW_OP_xor;
12644 goto do_binop;
12646 do_binop:
12647 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12648 VAR_INIT_STATUS_INITIALIZED);
12649 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12650 VAR_INIT_STATUS_INITIALIZED);
12652 if (op0 == 0 || op1 == 0)
12653 break;
12655 mem_loc_result = op0;
12656 add_loc_descr (&mem_loc_result, op1);
12657 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12658 break;
12660 case MOD:
12661 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
12663 mem_loc_result = typed_binop (DW_OP_mod, rtl,
12664 base_type_for_mode (mode, 0),
12665 mode, mem_mode);
12666 break;
12669 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12670 VAR_INIT_STATUS_INITIALIZED);
12671 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12672 VAR_INIT_STATUS_INITIALIZED);
12674 if (op0 == 0 || op1 == 0)
12675 break;
12677 mem_loc_result = op0;
12678 add_loc_descr (&mem_loc_result, op1);
12679 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12680 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12681 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
12682 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
12683 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
12684 break;
12686 case UDIV:
12687 if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
12689 if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
12691 op = DW_OP_div;
12692 goto do_binop;
12694 mem_loc_result = typed_binop (DW_OP_div, rtl,
12695 base_type_for_mode (mode, 1),
12696 mode, mem_mode);
12698 break;
12700 case NOT:
12701 op = DW_OP_not;
12702 goto do_unop;
12704 case ABS:
12705 op = DW_OP_abs;
12706 goto do_unop;
12708 case NEG:
12709 op = DW_OP_neg;
12710 goto do_unop;
12712 do_unop:
12713 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12714 VAR_INIT_STATUS_INITIALIZED);
12716 if (op0 == 0)
12717 break;
12719 mem_loc_result = op0;
12720 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12721 break;
12723 case CONST_INT:
12724 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12725 #ifdef POINTERS_EXTEND_UNSIGNED
12726 || (mode == Pmode
12727 && mem_mode != VOIDmode
12728 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
12729 #endif
12732 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12733 break;
12735 if (!dwarf_strict
12736 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
12737 || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
12739 dw_die_ref type_die = base_type_for_mode (mode, 1);
12740 enum machine_mode amode;
12741 if (type_die == NULL)
12742 return NULL;
12743 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
12744 MODE_INT, 0);
12745 if (INTVAL (rtl) >= 0
12746 && amode != BLKmode
12747 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
12748 /* const DW_OP_GNU_convert <XXX> vs.
12749 DW_OP_GNU_const_type <XXX, 1, const>. */
12750 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
12751 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
12753 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12754 op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12755 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12756 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12757 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
12758 add_loc_descr (&mem_loc_result, op0);
12759 return mem_loc_result;
12761 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
12762 INTVAL (rtl));
12763 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12764 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12765 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12766 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12767 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
12768 else
12770 mem_loc_result->dw_loc_oprnd2.val_class
12771 = dw_val_class_const_double;
12772 mem_loc_result->dw_loc_oprnd2.v.val_double
12773 = double_int::from_shwi (INTVAL (rtl));
12776 break;
12778 case CONST_DOUBLE:
12779 if (!dwarf_strict)
12781 dw_die_ref type_die;
12783 /* Note that a CONST_DOUBLE rtx could represent either an integer
12784 or a floating-point constant. A CONST_DOUBLE is used whenever
12785 the constant requires more than one word in order to be
12786 adequately represented. We output CONST_DOUBLEs as blocks. */
12787 if (mode == VOIDmode
12788 || (GET_MODE (rtl) == VOIDmode
12789 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
12790 break;
12791 type_die = base_type_for_mode (mode,
12792 GET_MODE_CLASS (mode) == MODE_INT);
12793 if (type_die == NULL)
12794 return NULL;
12795 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
12796 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12797 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12798 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12799 if (SCALAR_FLOAT_MODE_P (mode))
12801 unsigned int length = GET_MODE_SIZE (mode);
12802 unsigned char *array
12803 = (unsigned char*) ggc_alloc_atomic (length);
12805 insert_float (rtl, array);
12806 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
12807 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
12808 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
12809 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
12811 else
12813 mem_loc_result->dw_loc_oprnd2.val_class
12814 = dw_val_class_const_double;
12815 mem_loc_result->dw_loc_oprnd2.v.val_double
12816 = rtx_to_double_int (rtl);
12819 break;
12821 case EQ:
12822 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
12823 break;
12825 case GE:
12826 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
12827 break;
12829 case GT:
12830 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
12831 break;
12833 case LE:
12834 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
12835 break;
12837 case LT:
12838 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
12839 break;
12841 case NE:
12842 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
12843 break;
12845 case GEU:
12846 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
12847 break;
12849 case GTU:
12850 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
12851 break;
12853 case LEU:
12854 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
12855 break;
12857 case LTU:
12858 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
12859 break;
12861 case UMIN:
12862 case UMAX:
12863 if (GET_MODE_CLASS (mode) != MODE_INT)
12864 break;
12865 /* FALLTHRU */
12866 case SMIN:
12867 case SMAX:
12868 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
12869 break;
12871 case ZERO_EXTRACT:
12872 case SIGN_EXTRACT:
12873 if (CONST_INT_P (XEXP (rtl, 1))
12874 && CONST_INT_P (XEXP (rtl, 2))
12875 && ((unsigned) INTVAL (XEXP (rtl, 1))
12876 + (unsigned) INTVAL (XEXP (rtl, 2))
12877 <= GET_MODE_BITSIZE (mode))
12878 && GET_MODE_CLASS (mode) == MODE_INT
12879 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12880 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
12882 int shift, size;
12883 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12884 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12885 if (op0 == 0)
12886 break;
12887 if (GET_CODE (rtl) == SIGN_EXTRACT)
12888 op = DW_OP_shra;
12889 else
12890 op = DW_OP_shr;
12891 mem_loc_result = op0;
12892 size = INTVAL (XEXP (rtl, 1));
12893 shift = INTVAL (XEXP (rtl, 2));
12894 if (BITS_BIG_ENDIAN)
12895 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12896 - shift - size;
12897 if (shift + size != (int) DWARF2_ADDR_SIZE)
12899 add_loc_descr (&mem_loc_result,
12900 int_loc_descriptor (DWARF2_ADDR_SIZE
12901 - shift - size));
12902 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12904 if (size != (int) DWARF2_ADDR_SIZE)
12906 add_loc_descr (&mem_loc_result,
12907 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
12908 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12911 break;
12913 case IF_THEN_ELSE:
12915 dw_loc_descr_ref op2, bra_node, drop_node;
12916 op0 = mem_loc_descriptor (XEXP (rtl, 0),
12917 GET_MODE (XEXP (rtl, 0)) == VOIDmode
12918 ? word_mode : GET_MODE (XEXP (rtl, 0)),
12919 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12920 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12921 VAR_INIT_STATUS_INITIALIZED);
12922 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
12923 VAR_INIT_STATUS_INITIALIZED);
12924 if (op0 == NULL || op1 == NULL || op2 == NULL)
12925 break;
12927 mem_loc_result = op1;
12928 add_loc_descr (&mem_loc_result, op2);
12929 add_loc_descr (&mem_loc_result, op0);
12930 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12931 add_loc_descr (&mem_loc_result, bra_node);
12932 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
12933 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
12934 add_loc_descr (&mem_loc_result, drop_node);
12935 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12936 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
12938 break;
12940 case FLOAT_EXTEND:
12941 case FLOAT_TRUNCATE:
12942 case FLOAT:
12943 case UNSIGNED_FLOAT:
12944 case FIX:
12945 case UNSIGNED_FIX:
12946 if (!dwarf_strict)
12948 dw_die_ref type_die;
12949 dw_loc_descr_ref cvt;
12951 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12952 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12953 if (op0 == NULL)
12954 break;
12955 if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
12956 && (GET_CODE (rtl) == FLOAT
12957 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
12958 <= DWARF2_ADDR_SIZE))
12960 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12961 GET_CODE (rtl) == UNSIGNED_FLOAT);
12962 if (type_die == NULL)
12963 break;
12964 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12965 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12966 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12967 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12968 add_loc_descr (&op0, cvt);
12970 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
12971 if (type_die == NULL)
12972 break;
12973 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12974 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12975 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12976 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12977 add_loc_descr (&op0, cvt);
12978 if (GET_MODE_CLASS (mode) == MODE_INT
12979 && (GET_CODE (rtl) == FIX
12980 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
12982 op0 = convert_descriptor_to_mode (mode, op0);
12983 if (op0 == NULL)
12984 break;
12986 mem_loc_result = op0;
12988 break;
12990 case CLZ:
12991 case CTZ:
12992 case FFS:
12993 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
12994 break;
12996 case POPCOUNT:
12997 case PARITY:
12998 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
12999 break;
13001 case BSWAP:
13002 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
13003 break;
13005 case ROTATE:
13006 case ROTATERT:
13007 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
13008 break;
13010 case COMPARE:
13011 /* In theory, we could implement the above. */
13012 /* DWARF cannot represent the unsigned compare operations
13013 natively. */
13014 case SS_MULT:
13015 case US_MULT:
13016 case SS_DIV:
13017 case US_DIV:
13018 case SS_PLUS:
13019 case US_PLUS:
13020 case SS_MINUS:
13021 case US_MINUS:
13022 case SS_NEG:
13023 case US_NEG:
13024 case SS_ABS:
13025 case SS_ASHIFT:
13026 case US_ASHIFT:
13027 case SS_TRUNCATE:
13028 case US_TRUNCATE:
13029 case UNORDERED:
13030 case ORDERED:
13031 case UNEQ:
13032 case UNGE:
13033 case UNGT:
13034 case UNLE:
13035 case UNLT:
13036 case LTGT:
13037 case FRACT_CONVERT:
13038 case UNSIGNED_FRACT_CONVERT:
13039 case SAT_FRACT:
13040 case UNSIGNED_SAT_FRACT:
13041 case SQRT:
13042 case ASM_OPERANDS:
13043 case VEC_MERGE:
13044 case VEC_SELECT:
13045 case VEC_CONCAT:
13046 case VEC_DUPLICATE:
13047 case UNSPEC:
13048 case HIGH:
13049 case FMA:
13050 case STRICT_LOW_PART:
13051 case CONST_VECTOR:
13052 case CONST_FIXED:
13053 case CLRSB:
13054 case CLOBBER:
13055 /* If delegitimize_address couldn't do anything with the UNSPEC, we
13056 can't express it in the debug info. This can happen e.g. with some
13057 TLS UNSPECs. */
13058 break;
13060 case CONST_STRING:
13061 resolve_one_addr (&rtl, NULL);
13062 goto symref;
13064 default:
13065 #ifdef ENABLE_CHECKING
13066 print_rtl (stderr, rtl);
13067 gcc_unreachable ();
13068 #else
13069 break;
13070 #endif
13073 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13074 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13076 return mem_loc_result;
13079 /* Return a descriptor that describes the concatenation of two locations.
13080 This is typically a complex variable. */
13082 static dw_loc_descr_ref
13083 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
13085 dw_loc_descr_ref cc_loc_result = NULL;
13086 dw_loc_descr_ref x0_ref
13087 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13088 dw_loc_descr_ref x1_ref
13089 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13091 if (x0_ref == 0 || x1_ref == 0)
13092 return 0;
13094 cc_loc_result = x0_ref;
13095 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
13097 add_loc_descr (&cc_loc_result, x1_ref);
13098 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
13100 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13101 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13103 return cc_loc_result;
13106 /* Return a descriptor that describes the concatenation of N
13107 locations. */
13109 static dw_loc_descr_ref
13110 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
13112 unsigned int i;
13113 dw_loc_descr_ref cc_loc_result = NULL;
13114 unsigned int n = XVECLEN (concatn, 0);
13116 for (i = 0; i < n; ++i)
13118 dw_loc_descr_ref ref;
13119 rtx x = XVECEXP (concatn, 0, i);
13121 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13122 if (ref == NULL)
13123 return NULL;
13125 add_loc_descr (&cc_loc_result, ref);
13126 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
13129 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13130 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13132 return cc_loc_result;
13135 /* Helper function for loc_descriptor. Return DW_OP_GNU_implicit_pointer
13136 for DEBUG_IMPLICIT_PTR RTL. */
13138 static dw_loc_descr_ref
13139 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
13141 dw_loc_descr_ref ret;
13142 dw_die_ref ref;
13144 if (dwarf_strict)
13145 return NULL;
13146 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
13147 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
13148 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
13149 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
13150 ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
13151 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
13152 if (ref)
13154 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13155 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
13156 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
13158 else
13160 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
13161 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
13163 return ret;
13166 /* Output a proper Dwarf location descriptor for a variable or parameter
13167 which is either allocated in a register or in a memory location. For a
13168 register, we just generate an OP_REG and the register number. For a
13169 memory location we provide a Dwarf postfix expression describing how to
13170 generate the (dynamic) address of the object onto the address stack.
13172 MODE is mode of the decl if this loc_descriptor is going to be used in
13173 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
13174 allowed, VOIDmode otherwise.
13176 If we don't know how to describe it, return 0. */
13178 static dw_loc_descr_ref
13179 loc_descriptor (rtx rtl, enum machine_mode mode,
13180 enum var_init_status initialized)
13182 dw_loc_descr_ref loc_result = NULL;
13184 switch (GET_CODE (rtl))
13186 case SUBREG:
13187 /* The case of a subreg may arise when we have a local (register)
13188 variable or a formal (register) parameter which doesn't quite fill
13189 up an entire register. For now, just assume that it is
13190 legitimate to make the Dwarf info refer to the whole register which
13191 contains the given subreg. */
13192 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
13193 loc_result = loc_descriptor (SUBREG_REG (rtl),
13194 GET_MODE (SUBREG_REG (rtl)), initialized);
13195 else
13196 goto do_default;
13197 break;
13199 case REG:
13200 loc_result = reg_loc_descriptor (rtl, initialized);
13201 break;
13203 case MEM:
13204 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13205 GET_MODE (rtl), initialized);
13206 if (loc_result == NULL)
13207 loc_result = tls_mem_loc_descriptor (rtl);
13208 if (loc_result == NULL)
13210 rtx new_rtl = avoid_constant_pool_reference (rtl);
13211 if (new_rtl != rtl)
13212 loc_result = loc_descriptor (new_rtl, mode, initialized);
13214 break;
13216 case CONCAT:
13217 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
13218 initialized);
13219 break;
13221 case CONCATN:
13222 loc_result = concatn_loc_descriptor (rtl, initialized);
13223 break;
13225 case VAR_LOCATION:
13226 /* Single part. */
13227 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
13229 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
13230 if (GET_CODE (loc) == EXPR_LIST)
13231 loc = XEXP (loc, 0);
13232 loc_result = loc_descriptor (loc, mode, initialized);
13233 break;
13236 rtl = XEXP (rtl, 1);
13237 /* FALLTHRU */
13239 case PARALLEL:
13241 rtvec par_elems = XVEC (rtl, 0);
13242 int num_elem = GET_NUM_ELEM (par_elems);
13243 enum machine_mode mode;
13244 int i;
13246 /* Create the first one, so we have something to add to. */
13247 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
13248 VOIDmode, initialized);
13249 if (loc_result == NULL)
13250 return NULL;
13251 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
13252 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13253 for (i = 1; i < num_elem; i++)
13255 dw_loc_descr_ref temp;
13257 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
13258 VOIDmode, initialized);
13259 if (temp == NULL)
13260 return NULL;
13261 add_loc_descr (&loc_result, temp);
13262 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
13263 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13266 break;
13268 case CONST_INT:
13269 if (mode != VOIDmode && mode != BLKmode)
13270 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
13271 INTVAL (rtl));
13272 break;
13274 case CONST_DOUBLE:
13275 if (mode == VOIDmode)
13276 mode = GET_MODE (rtl);
13278 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13280 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13282 /* Note that a CONST_DOUBLE rtx could represent either an integer
13283 or a floating-point constant. A CONST_DOUBLE is used whenever
13284 the constant requires more than one word in order to be
13285 adequately represented. We output CONST_DOUBLEs as blocks. */
13286 loc_result = new_loc_descr (DW_OP_implicit_value,
13287 GET_MODE_SIZE (mode), 0);
13288 if (SCALAR_FLOAT_MODE_P (mode))
13290 unsigned int length = GET_MODE_SIZE (mode);
13291 unsigned char *array
13292 = (unsigned char*) ggc_alloc_atomic (length);
13294 insert_float (rtl, array);
13295 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13296 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13297 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13298 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13300 else
13302 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
13303 loc_result->dw_loc_oprnd2.v.val_double
13304 = rtx_to_double_int (rtl);
13307 break;
13309 case CONST_VECTOR:
13310 if (mode == VOIDmode)
13311 mode = GET_MODE (rtl);
13313 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13315 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
13316 unsigned int length = CONST_VECTOR_NUNITS (rtl);
13317 unsigned char *array = (unsigned char *)
13318 ggc_alloc_atomic (length * elt_size);
13319 unsigned int i;
13320 unsigned char *p;
13322 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13323 switch (GET_MODE_CLASS (mode))
13325 case MODE_VECTOR_INT:
13326 for (i = 0, p = array; i < length; i++, p += elt_size)
13328 rtx elt = CONST_VECTOR_ELT (rtl, i);
13329 double_int val = rtx_to_double_int (elt);
13331 if (elt_size <= sizeof (HOST_WIDE_INT))
13332 insert_int (val.to_shwi (), elt_size, p);
13333 else
13335 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
13336 insert_double (val, p);
13339 break;
13341 case MODE_VECTOR_FLOAT:
13342 for (i = 0, p = array; i < length; i++, p += elt_size)
13344 rtx elt = CONST_VECTOR_ELT (rtl, i);
13345 insert_float (elt, p);
13347 break;
13349 default:
13350 gcc_unreachable ();
13353 loc_result = new_loc_descr (DW_OP_implicit_value,
13354 length * elt_size, 0);
13355 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13356 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
13357 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
13358 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13360 break;
13362 case CONST:
13363 if (mode == VOIDmode
13364 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
13365 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
13366 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
13368 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
13369 break;
13371 /* FALLTHROUGH */
13372 case SYMBOL_REF:
13373 if (!const_ok_for_output (rtl))
13374 break;
13375 case LABEL_REF:
13376 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
13377 && (dwarf_version >= 4 || !dwarf_strict))
13379 loc_result = new_addr_loc_descr (rtl, dtprel_false);
13380 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
13381 vec_safe_push (used_rtx_array, rtl);
13383 break;
13385 case DEBUG_IMPLICIT_PTR:
13386 loc_result = implicit_ptr_descriptor (rtl, 0);
13387 break;
13389 case PLUS:
13390 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
13391 && CONST_INT_P (XEXP (rtl, 1)))
13393 loc_result
13394 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
13395 break;
13397 /* FALLTHRU */
13398 do_default:
13399 default:
13400 if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
13401 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
13402 && dwarf_version >= 4)
13403 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
13405 /* Value expression. */
13406 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
13407 if (loc_result)
13408 add_loc_descr (&loc_result,
13409 new_loc_descr (DW_OP_stack_value, 0, 0));
13411 break;
13414 return loc_result;
13417 /* We need to figure out what section we should use as the base for the
13418 address ranges where a given location is valid.
13419 1. If this particular DECL has a section associated with it, use that.
13420 2. If this function has a section associated with it, use that.
13421 3. Otherwise, use the text section.
13422 XXX: If you split a variable across multiple sections, we won't notice. */
13424 static const char *
13425 secname_for_decl (const_tree decl)
13427 const char *secname;
13429 if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
13431 tree sectree = DECL_SECTION_NAME (decl);
13432 secname = TREE_STRING_POINTER (sectree);
13434 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
13436 tree sectree = DECL_SECTION_NAME (current_function_decl);
13437 secname = TREE_STRING_POINTER (sectree);
13439 else if (cfun && in_cold_section_p)
13440 secname = crtl->subsections.cold_section_label;
13441 else
13442 secname = text_section_label;
13444 return secname;
13447 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
13449 static bool
13450 decl_by_reference_p (tree decl)
13452 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
13453 || TREE_CODE (decl) == VAR_DECL)
13454 && DECL_BY_REFERENCE (decl));
13457 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13458 for VARLOC. */
13460 static dw_loc_descr_ref
13461 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
13462 enum var_init_status initialized)
13464 int have_address = 0;
13465 dw_loc_descr_ref descr;
13466 enum machine_mode mode;
13468 if (want_address != 2)
13470 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
13471 /* Single part. */
13472 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13474 varloc = PAT_VAR_LOCATION_LOC (varloc);
13475 if (GET_CODE (varloc) == EXPR_LIST)
13476 varloc = XEXP (varloc, 0);
13477 mode = GET_MODE (varloc);
13478 if (MEM_P (varloc))
13480 rtx addr = XEXP (varloc, 0);
13481 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
13482 mode, initialized);
13483 if (descr)
13484 have_address = 1;
13485 else
13487 rtx x = avoid_constant_pool_reference (varloc);
13488 if (x != varloc)
13489 descr = mem_loc_descriptor (x, mode, VOIDmode,
13490 initialized);
13493 else
13494 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
13496 else
13497 return 0;
13499 else
13501 if (GET_CODE (varloc) == VAR_LOCATION)
13502 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
13503 else
13504 mode = DECL_MODE (loc);
13505 descr = loc_descriptor (varloc, mode, initialized);
13506 have_address = 1;
13509 if (!descr)
13510 return 0;
13512 if (want_address == 2 && !have_address
13513 && (dwarf_version >= 4 || !dwarf_strict))
13515 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
13517 expansion_failed (loc, NULL_RTX,
13518 "DWARF address size mismatch");
13519 return 0;
13521 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
13522 have_address = 1;
13524 /* Show if we can't fill the request for an address. */
13525 if (want_address && !have_address)
13527 expansion_failed (loc, NULL_RTX,
13528 "Want address and only have value");
13529 return 0;
13532 /* If we've got an address and don't want one, dereference. */
13533 if (!want_address && have_address)
13535 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
13536 enum dwarf_location_atom op;
13538 if (size > DWARF2_ADDR_SIZE || size == -1)
13540 expansion_failed (loc, NULL_RTX,
13541 "DWARF address size mismatch");
13542 return 0;
13544 else if (size == DWARF2_ADDR_SIZE)
13545 op = DW_OP_deref;
13546 else
13547 op = DW_OP_deref_size;
13549 add_loc_descr (&descr, new_loc_descr (op, size, 0));
13552 return descr;
13555 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
13556 if it is not possible. */
13558 static dw_loc_descr_ref
13559 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
13561 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
13562 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
13563 else if (dwarf_version >= 3 || !dwarf_strict)
13564 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
13565 else
13566 return NULL;
13569 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13570 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
13572 static dw_loc_descr_ref
13573 dw_sra_loc_expr (tree decl, rtx loc)
13575 rtx p;
13576 unsigned HOST_WIDE_INT padsize = 0;
13577 dw_loc_descr_ref descr, *descr_tail;
13578 unsigned HOST_WIDE_INT decl_size;
13579 rtx varloc;
13580 enum var_init_status initialized;
13582 if (DECL_SIZE (decl) == NULL
13583 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
13584 return NULL;
13586 decl_size = tree_to_uhwi (DECL_SIZE (decl));
13587 descr = NULL;
13588 descr_tail = &descr;
13590 for (p = loc; p; p = XEXP (p, 1))
13592 unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
13593 rtx loc_note = *decl_piece_varloc_ptr (p);
13594 dw_loc_descr_ref cur_descr;
13595 dw_loc_descr_ref *tail, last = NULL;
13596 unsigned HOST_WIDE_INT opsize = 0;
13598 if (loc_note == NULL_RTX
13599 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
13601 padsize += bitsize;
13602 continue;
13604 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
13605 varloc = NOTE_VAR_LOCATION (loc_note);
13606 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
13607 if (cur_descr == NULL)
13609 padsize += bitsize;
13610 continue;
13613 /* Check that cur_descr either doesn't use
13614 DW_OP_*piece operations, or their sum is equal
13615 to bitsize. Otherwise we can't embed it. */
13616 for (tail = &cur_descr; *tail != NULL;
13617 tail = &(*tail)->dw_loc_next)
13618 if ((*tail)->dw_loc_opc == DW_OP_piece)
13620 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
13621 * BITS_PER_UNIT;
13622 last = *tail;
13624 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
13626 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
13627 last = *tail;
13630 if (last != NULL && opsize != bitsize)
13632 padsize += bitsize;
13633 /* Discard the current piece of the descriptor and release any
13634 addr_table entries it uses. */
13635 remove_loc_list_addr_table_entries (cur_descr);
13636 continue;
13639 /* If there is a hole, add DW_OP_*piece after empty DWARF
13640 expression, which means that those bits are optimized out. */
13641 if (padsize)
13643 if (padsize > decl_size)
13645 remove_loc_list_addr_table_entries (cur_descr);
13646 goto discard_descr;
13648 decl_size -= padsize;
13649 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
13650 if (*descr_tail == NULL)
13652 remove_loc_list_addr_table_entries (cur_descr);
13653 goto discard_descr;
13655 descr_tail = &(*descr_tail)->dw_loc_next;
13656 padsize = 0;
13658 *descr_tail = cur_descr;
13659 descr_tail = tail;
13660 if (bitsize > decl_size)
13661 goto discard_descr;
13662 decl_size -= bitsize;
13663 if (last == NULL)
13665 HOST_WIDE_INT offset = 0;
13666 if (GET_CODE (varloc) == VAR_LOCATION
13667 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13669 varloc = PAT_VAR_LOCATION_LOC (varloc);
13670 if (GET_CODE (varloc) == EXPR_LIST)
13671 varloc = XEXP (varloc, 0);
13675 if (GET_CODE (varloc) == CONST
13676 || GET_CODE (varloc) == SIGN_EXTEND
13677 || GET_CODE (varloc) == ZERO_EXTEND)
13678 varloc = XEXP (varloc, 0);
13679 else if (GET_CODE (varloc) == SUBREG)
13680 varloc = SUBREG_REG (varloc);
13681 else
13682 break;
13684 while (1);
13685 /* DW_OP_bit_size offset should be zero for register
13686 or implicit location descriptions and empty location
13687 descriptions, but for memory addresses needs big endian
13688 adjustment. */
13689 if (MEM_P (varloc))
13691 unsigned HOST_WIDE_INT memsize
13692 = MEM_SIZE (varloc) * BITS_PER_UNIT;
13693 if (memsize != bitsize)
13695 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
13696 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
13697 goto discard_descr;
13698 if (memsize < bitsize)
13699 goto discard_descr;
13700 if (BITS_BIG_ENDIAN)
13701 offset = memsize - bitsize;
13705 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
13706 if (*descr_tail == NULL)
13707 goto discard_descr;
13708 descr_tail = &(*descr_tail)->dw_loc_next;
13712 /* If there were any non-empty expressions, add padding till the end of
13713 the decl. */
13714 if (descr != NULL && decl_size != 0)
13716 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
13717 if (*descr_tail == NULL)
13718 goto discard_descr;
13720 return descr;
13722 discard_descr:
13723 /* Discard the descriptor and release any addr_table entries it uses. */
13724 remove_loc_list_addr_table_entries (descr);
13725 return NULL;
13728 /* Return the dwarf representation of the location list LOC_LIST of
13729 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
13730 function. */
13732 static dw_loc_list_ref
13733 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
13735 const char *endname, *secname;
13736 rtx varloc;
13737 enum var_init_status initialized;
13738 struct var_loc_node *node;
13739 dw_loc_descr_ref descr;
13740 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
13741 dw_loc_list_ref list = NULL;
13742 dw_loc_list_ref *listp = &list;
13744 /* Now that we know what section we are using for a base,
13745 actually construct the list of locations.
13746 The first location information is what is passed to the
13747 function that creates the location list, and the remaining
13748 locations just get added on to that list.
13749 Note that we only know the start address for a location
13750 (IE location changes), so to build the range, we use
13751 the range [current location start, next location start].
13752 This means we have to special case the last node, and generate
13753 a range of [last location start, end of function label]. */
13755 secname = secname_for_decl (decl);
13757 for (node = loc_list->first; node; node = node->next)
13758 if (GET_CODE (node->loc) == EXPR_LIST
13759 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
13761 if (GET_CODE (node->loc) == EXPR_LIST)
13763 /* This requires DW_OP_{,bit_}piece, which is not usable
13764 inside DWARF expressions. */
13765 if (want_address != 2)
13766 continue;
13767 descr = dw_sra_loc_expr (decl, node->loc);
13768 if (descr == NULL)
13769 continue;
13771 else
13773 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13774 varloc = NOTE_VAR_LOCATION (node->loc);
13775 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
13777 if (descr)
13779 bool range_across_switch = false;
13780 /* If section switch happens in between node->label
13781 and node->next->label (or end of function) and
13782 we can't emit it as a single entry list,
13783 emit two ranges, first one ending at the end
13784 of first partition and second one starting at the
13785 beginning of second partition. */
13786 if (node == loc_list->last_before_switch
13787 && (node != loc_list->first || loc_list->first->next)
13788 && current_function_decl)
13790 endname = cfun->fde->dw_fde_end;
13791 range_across_switch = true;
13793 /* The variable has a location between NODE->LABEL and
13794 NODE->NEXT->LABEL. */
13795 else if (node->next)
13796 endname = node->next->label;
13797 /* If the variable has a location at the last label
13798 it keeps its location until the end of function. */
13799 else if (!current_function_decl)
13800 endname = text_end_label;
13801 else
13803 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
13804 current_function_funcdef_no);
13805 endname = ggc_strdup (label_id);
13808 *listp = new_loc_list (descr, node->label, endname, secname);
13809 if (TREE_CODE (decl) == PARM_DECL
13810 && node == loc_list->first
13811 && NOTE_P (node->loc)
13812 && strcmp (node->label, endname) == 0)
13813 (*listp)->force = true;
13814 listp = &(*listp)->dw_loc_next;
13816 if (range_across_switch)
13818 if (GET_CODE (node->loc) == EXPR_LIST)
13819 descr = dw_sra_loc_expr (decl, node->loc);
13820 else
13822 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13823 varloc = NOTE_VAR_LOCATION (node->loc);
13824 descr = dw_loc_list_1 (decl, varloc, want_address,
13825 initialized);
13827 gcc_assert (descr);
13828 /* The variable has a location between NODE->LABEL and
13829 NODE->NEXT->LABEL. */
13830 if (node->next)
13831 endname = node->next->label;
13832 else
13833 endname = cfun->fde->dw_fde_second_end;
13834 *listp = new_loc_list (descr,
13835 cfun->fde->dw_fde_second_begin,
13836 endname, secname);
13837 listp = &(*listp)->dw_loc_next;
13842 /* Try to avoid the overhead of a location list emitting a location
13843 expression instead, but only if we didn't have more than one
13844 location entry in the first place. If some entries were not
13845 representable, we don't want to pretend a single entry that was
13846 applies to the entire scope in which the variable is
13847 available. */
13848 if (list && loc_list->first->next)
13849 gen_llsym (list);
13851 return list;
13854 /* Return if the loc_list has only single element and thus can be represented
13855 as location description. */
13857 static bool
13858 single_element_loc_list_p (dw_loc_list_ref list)
13860 gcc_assert (!list->dw_loc_next || list->ll_symbol);
13861 return !list->ll_symbol;
13864 /* To each location in list LIST add loc descr REF. */
13866 static void
13867 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
13869 dw_loc_descr_ref copy;
13870 add_loc_descr (&list->expr, ref);
13871 list = list->dw_loc_next;
13872 while (list)
13874 copy = ggc_alloc_dw_loc_descr_node ();
13875 memcpy (copy, ref, sizeof (dw_loc_descr_node));
13876 add_loc_descr (&list->expr, copy);
13877 while (copy->dw_loc_next)
13879 dw_loc_descr_ref new_copy = ggc_alloc_dw_loc_descr_node ();
13880 memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
13881 copy->dw_loc_next = new_copy;
13882 copy = new_copy;
13884 list = list->dw_loc_next;
13888 /* Given two lists RET and LIST
13889 produce location list that is result of adding expression in LIST
13890 to expression in RET on each position in program.
13891 Might be destructive on both RET and LIST.
13893 TODO: We handle only simple cases of RET or LIST having at most one
13894 element. General case would inolve sorting the lists in program order
13895 and merging them that will need some additional work.
13896 Adding that will improve quality of debug info especially for SRA-ed
13897 structures. */
13899 static void
13900 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
13902 if (!list)
13903 return;
13904 if (!*ret)
13906 *ret = list;
13907 return;
13909 if (!list->dw_loc_next)
13911 add_loc_descr_to_each (*ret, list->expr);
13912 return;
13914 if (!(*ret)->dw_loc_next)
13916 add_loc_descr_to_each (list, (*ret)->expr);
13917 *ret = list;
13918 return;
13920 expansion_failed (NULL_TREE, NULL_RTX,
13921 "Don't know how to merge two non-trivial"
13922 " location lists.\n");
13923 *ret = NULL;
13924 return;
13927 /* LOC is constant expression. Try a luck, look it up in constant
13928 pool and return its loc_descr of its address. */
13930 static dw_loc_descr_ref
13931 cst_pool_loc_descr (tree loc)
13933 /* Get an RTL for this, if something has been emitted. */
13934 rtx rtl = lookup_constant_def (loc);
13936 if (!rtl || !MEM_P (rtl))
13938 gcc_assert (!rtl);
13939 return 0;
13941 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
13943 /* TODO: We might get more coverage if we was actually delaying expansion
13944 of all expressions till end of compilation when constant pools are fully
13945 populated. */
13946 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
13948 expansion_failed (loc, NULL_RTX,
13949 "CST value in contant pool but not marked.");
13950 return 0;
13952 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13953 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
13956 /* Return dw_loc_list representing address of addr_expr LOC
13957 by looking for inner INDIRECT_REF expression and turning
13958 it into simple arithmetics. */
13960 static dw_loc_list_ref
13961 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
13963 tree obj, offset;
13964 HOST_WIDE_INT bitsize, bitpos, bytepos;
13965 enum machine_mode mode;
13966 int unsignedp, volatilep = 0;
13967 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
13969 obj = get_inner_reference (TREE_OPERAND (loc, 0),
13970 &bitsize, &bitpos, &offset, &mode,
13971 &unsignedp, &volatilep, false);
13972 STRIP_NOPS (obj);
13973 if (bitpos % BITS_PER_UNIT)
13975 expansion_failed (loc, NULL_RTX, "bitfield access");
13976 return 0;
13978 if (!INDIRECT_REF_P (obj))
13980 expansion_failed (obj,
13981 NULL_RTX, "no indirect ref in inner refrence");
13982 return 0;
13984 if (!offset && !bitpos)
13985 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
13986 else if (toplev
13987 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
13988 && (dwarf_version >= 4 || !dwarf_strict))
13990 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
13991 if (!list_ret)
13992 return 0;
13993 if (offset)
13995 /* Variable offset. */
13996 list_ret1 = loc_list_from_tree (offset, 0);
13997 if (list_ret1 == 0)
13998 return 0;
13999 add_loc_list (&list_ret, list_ret1);
14000 if (!list_ret)
14001 return 0;
14002 add_loc_descr_to_each (list_ret,
14003 new_loc_descr (DW_OP_plus, 0, 0));
14005 bytepos = bitpos / BITS_PER_UNIT;
14006 if (bytepos > 0)
14007 add_loc_descr_to_each (list_ret,
14008 new_loc_descr (DW_OP_plus_uconst,
14009 bytepos, 0));
14010 else if (bytepos < 0)
14011 loc_list_plus_const (list_ret, bytepos);
14012 add_loc_descr_to_each (list_ret,
14013 new_loc_descr (DW_OP_stack_value, 0, 0));
14015 return list_ret;
14019 /* Generate Dwarf location list representing LOC.
14020 If WANT_ADDRESS is false, expression computing LOC will be computed
14021 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
14022 if WANT_ADDRESS is 2, expression computing address useable in location
14023 will be returned (i.e. DW_OP_reg can be used
14024 to refer to register values). */
14026 static dw_loc_list_ref
14027 loc_list_from_tree (tree loc, int want_address)
14029 dw_loc_descr_ref ret = NULL, ret1 = NULL;
14030 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14031 int have_address = 0;
14032 enum dwarf_location_atom op;
14034 /* ??? Most of the time we do not take proper care for sign/zero
14035 extending the values properly. Hopefully this won't be a real
14036 problem... */
14038 switch (TREE_CODE (loc))
14040 case ERROR_MARK:
14041 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
14042 return 0;
14044 case PLACEHOLDER_EXPR:
14045 /* This case involves extracting fields from an object to determine the
14046 position of other fields. We don't try to encode this here. The
14047 only user of this is Ada, which encodes the needed information using
14048 the names of types. */
14049 expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
14050 return 0;
14052 case CALL_EXPR:
14053 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
14054 /* There are no opcodes for these operations. */
14055 return 0;
14057 case PREINCREMENT_EXPR:
14058 case PREDECREMENT_EXPR:
14059 case POSTINCREMENT_EXPR:
14060 case POSTDECREMENT_EXPR:
14061 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
14062 /* There are no opcodes for these operations. */
14063 return 0;
14065 case ADDR_EXPR:
14066 /* If we already want an address, see if there is INDIRECT_REF inside
14067 e.g. for &this->field. */
14068 if (want_address)
14070 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
14071 (loc, want_address == 2);
14072 if (list_ret)
14073 have_address = 1;
14074 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
14075 && (ret = cst_pool_loc_descr (loc)))
14076 have_address = 1;
14078 /* Otherwise, process the argument and look for the address. */
14079 if (!list_ret && !ret)
14080 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
14081 else
14083 if (want_address)
14084 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
14085 return NULL;
14087 break;
14089 case VAR_DECL:
14090 if (DECL_THREAD_LOCAL_P (loc))
14092 rtx rtl;
14093 enum dwarf_location_atom tls_op;
14094 enum dtprel_bool dtprel = dtprel_false;
14096 if (targetm.have_tls)
14098 /* If this is not defined, we have no way to emit the
14099 data. */
14100 if (!targetm.asm_out.output_dwarf_dtprel)
14101 return 0;
14103 /* The way DW_OP_GNU_push_tls_address is specified, we
14104 can only look up addresses of objects in the current
14105 module. We used DW_OP_addr as first op, but that's
14106 wrong, because DW_OP_addr is relocated by the debug
14107 info consumer, while DW_OP_GNU_push_tls_address
14108 operand shouldn't be. */
14109 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
14110 return 0;
14111 dtprel = dtprel_true;
14112 tls_op = DW_OP_GNU_push_tls_address;
14114 else
14116 if (!targetm.emutls.debug_form_tls_address
14117 || !(dwarf_version >= 3 || !dwarf_strict))
14118 return 0;
14119 /* We stuffed the control variable into the DECL_VALUE_EXPR
14120 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
14121 no longer appear in gimple code. We used the control
14122 variable in specific so that we could pick it up here. */
14123 loc = DECL_VALUE_EXPR (loc);
14124 tls_op = DW_OP_form_tls_address;
14127 rtl = rtl_for_decl_location (loc);
14128 if (rtl == NULL_RTX)
14129 return 0;
14131 if (!MEM_P (rtl))
14132 return 0;
14133 rtl = XEXP (rtl, 0);
14134 if (! CONSTANT_P (rtl))
14135 return 0;
14137 ret = new_addr_loc_descr (rtl, dtprel);
14138 ret1 = new_loc_descr (tls_op, 0, 0);
14139 add_loc_descr (&ret, ret1);
14141 have_address = 1;
14142 break;
14144 /* FALLTHRU */
14146 case PARM_DECL:
14147 case RESULT_DECL:
14148 if (DECL_HAS_VALUE_EXPR_P (loc))
14149 return loc_list_from_tree (DECL_VALUE_EXPR (loc),
14150 want_address);
14151 /* FALLTHRU */
14153 case FUNCTION_DECL:
14155 rtx rtl;
14156 var_loc_list *loc_list = lookup_decl_loc (loc);
14158 if (loc_list && loc_list->first)
14160 list_ret = dw_loc_list (loc_list, loc, want_address);
14161 have_address = want_address != 0;
14162 break;
14164 rtl = rtl_for_decl_location (loc);
14165 if (rtl == NULL_RTX)
14167 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
14168 return 0;
14170 else if (CONST_INT_P (rtl))
14172 HOST_WIDE_INT val = INTVAL (rtl);
14173 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14174 val &= GET_MODE_MASK (DECL_MODE (loc));
14175 ret = int_loc_descriptor (val);
14177 else if (GET_CODE (rtl) == CONST_STRING)
14179 expansion_failed (loc, NULL_RTX, "CONST_STRING");
14180 return 0;
14182 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
14183 ret = new_addr_loc_descr (rtl, dtprel_false);
14184 else
14186 enum machine_mode mode, mem_mode;
14188 /* Certain constructs can only be represented at top-level. */
14189 if (want_address == 2)
14191 ret = loc_descriptor (rtl, VOIDmode,
14192 VAR_INIT_STATUS_INITIALIZED);
14193 have_address = 1;
14195 else
14197 mode = GET_MODE (rtl);
14198 mem_mode = VOIDmode;
14199 if (MEM_P (rtl))
14201 mem_mode = mode;
14202 mode = get_address_mode (rtl);
14203 rtl = XEXP (rtl, 0);
14204 have_address = 1;
14206 ret = mem_loc_descriptor (rtl, mode, mem_mode,
14207 VAR_INIT_STATUS_INITIALIZED);
14209 if (!ret)
14210 expansion_failed (loc, rtl,
14211 "failed to produce loc descriptor for rtl");
14214 break;
14216 case MEM_REF:
14217 /* ??? FIXME. */
14218 if (!integer_zerop (TREE_OPERAND (loc, 1)))
14219 return 0;
14220 /* Fallthru. */
14221 case INDIRECT_REF:
14222 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14223 have_address = 1;
14224 break;
14226 case TARGET_MEM_REF:
14227 case SSA_NAME:
14228 return NULL;
14230 case COMPOUND_EXPR:
14231 return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
14233 CASE_CONVERT:
14234 case VIEW_CONVERT_EXPR:
14235 case SAVE_EXPR:
14236 case MODIFY_EXPR:
14237 return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
14239 case COMPONENT_REF:
14240 case BIT_FIELD_REF:
14241 case ARRAY_REF:
14242 case ARRAY_RANGE_REF:
14243 case REALPART_EXPR:
14244 case IMAGPART_EXPR:
14246 tree obj, offset;
14247 HOST_WIDE_INT bitsize, bitpos, bytepos;
14248 enum machine_mode mode;
14249 int unsignedp, volatilep = 0;
14251 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
14252 &unsignedp, &volatilep, false);
14254 gcc_assert (obj != loc);
14256 list_ret = loc_list_from_tree (obj,
14257 want_address == 2
14258 && !bitpos && !offset ? 2 : 1);
14259 /* TODO: We can extract value of the small expression via shifting even
14260 for nonzero bitpos. */
14261 if (list_ret == 0)
14262 return 0;
14263 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
14265 expansion_failed (loc, NULL_RTX,
14266 "bitfield access");
14267 return 0;
14270 if (offset != NULL_TREE)
14272 /* Variable offset. */
14273 list_ret1 = loc_list_from_tree (offset, 0);
14274 if (list_ret1 == 0)
14275 return 0;
14276 add_loc_list (&list_ret, list_ret1);
14277 if (!list_ret)
14278 return 0;
14279 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
14282 bytepos = bitpos / BITS_PER_UNIT;
14283 if (bytepos > 0)
14284 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
14285 else if (bytepos < 0)
14286 loc_list_plus_const (list_ret, bytepos);
14288 have_address = 1;
14289 break;
14292 case INTEGER_CST:
14293 if ((want_address || !tree_fits_shwi_p (loc))
14294 && (ret = cst_pool_loc_descr (loc)))
14295 have_address = 1;
14296 else if (want_address == 2
14297 && tree_fits_shwi_p (loc)
14298 && (ret = address_of_int_loc_descriptor
14299 (int_size_in_bytes (TREE_TYPE (loc)),
14300 tree_to_shwi (loc))))
14301 have_address = 1;
14302 else if (tree_fits_shwi_p (loc))
14303 ret = int_loc_descriptor (tree_to_shwi (loc));
14304 else
14306 expansion_failed (loc, NULL_RTX,
14307 "Integer operand is not host integer");
14308 return 0;
14310 break;
14312 case CONSTRUCTOR:
14313 case REAL_CST:
14314 case STRING_CST:
14315 case COMPLEX_CST:
14316 if ((ret = cst_pool_loc_descr (loc)))
14317 have_address = 1;
14318 else
14319 /* We can construct small constants here using int_loc_descriptor. */
14320 expansion_failed (loc, NULL_RTX,
14321 "constructor or constant not in constant pool");
14322 break;
14324 case TRUTH_AND_EXPR:
14325 case TRUTH_ANDIF_EXPR:
14326 case BIT_AND_EXPR:
14327 op = DW_OP_and;
14328 goto do_binop;
14330 case TRUTH_XOR_EXPR:
14331 case BIT_XOR_EXPR:
14332 op = DW_OP_xor;
14333 goto do_binop;
14335 case TRUTH_OR_EXPR:
14336 case TRUTH_ORIF_EXPR:
14337 case BIT_IOR_EXPR:
14338 op = DW_OP_or;
14339 goto do_binop;
14341 case FLOOR_DIV_EXPR:
14342 case CEIL_DIV_EXPR:
14343 case ROUND_DIV_EXPR:
14344 case TRUNC_DIV_EXPR:
14345 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14346 return 0;
14347 op = DW_OP_div;
14348 goto do_binop;
14350 case MINUS_EXPR:
14351 op = DW_OP_minus;
14352 goto do_binop;
14354 case FLOOR_MOD_EXPR:
14355 case CEIL_MOD_EXPR:
14356 case ROUND_MOD_EXPR:
14357 case TRUNC_MOD_EXPR:
14358 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14360 op = DW_OP_mod;
14361 goto do_binop;
14363 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14364 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14365 if (list_ret == 0 || list_ret1 == 0)
14366 return 0;
14368 add_loc_list (&list_ret, list_ret1);
14369 if (list_ret == 0)
14370 return 0;
14371 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14372 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14373 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
14374 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
14375 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
14376 break;
14378 case MULT_EXPR:
14379 op = DW_OP_mul;
14380 goto do_binop;
14382 case LSHIFT_EXPR:
14383 op = DW_OP_shl;
14384 goto do_binop;
14386 case RSHIFT_EXPR:
14387 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
14388 goto do_binop;
14390 case POINTER_PLUS_EXPR:
14391 case PLUS_EXPR:
14392 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
14394 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14395 if (list_ret == 0)
14396 return 0;
14398 loc_list_plus_const (list_ret, tree_to_shwi (TREE_OPERAND (loc, 1)));
14399 break;
14402 op = DW_OP_plus;
14403 goto do_binop;
14405 case LE_EXPR:
14406 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14407 return 0;
14409 op = DW_OP_le;
14410 goto do_binop;
14412 case GE_EXPR:
14413 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14414 return 0;
14416 op = DW_OP_ge;
14417 goto do_binop;
14419 case LT_EXPR:
14420 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14421 return 0;
14423 op = DW_OP_lt;
14424 goto do_binop;
14426 case GT_EXPR:
14427 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14428 return 0;
14430 op = DW_OP_gt;
14431 goto do_binop;
14433 case EQ_EXPR:
14434 op = DW_OP_eq;
14435 goto do_binop;
14437 case NE_EXPR:
14438 op = DW_OP_ne;
14439 goto do_binop;
14441 do_binop:
14442 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14443 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14444 if (list_ret == 0 || list_ret1 == 0)
14445 return 0;
14447 add_loc_list (&list_ret, list_ret1);
14448 if (list_ret == 0)
14449 return 0;
14450 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14451 break;
14453 case TRUTH_NOT_EXPR:
14454 case BIT_NOT_EXPR:
14455 op = DW_OP_not;
14456 goto do_unop;
14458 case ABS_EXPR:
14459 op = DW_OP_abs;
14460 goto do_unop;
14462 case NEGATE_EXPR:
14463 op = DW_OP_neg;
14464 goto do_unop;
14466 do_unop:
14467 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14468 if (list_ret == 0)
14469 return 0;
14471 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14472 break;
14474 case MIN_EXPR:
14475 case MAX_EXPR:
14477 const enum tree_code code =
14478 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
14480 loc = build3 (COND_EXPR, TREE_TYPE (loc),
14481 build2 (code, integer_type_node,
14482 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
14483 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
14486 /* ... fall through ... */
14488 case COND_EXPR:
14490 dw_loc_descr_ref lhs
14491 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
14492 dw_loc_list_ref rhs
14493 = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
14494 dw_loc_descr_ref bra_node, jump_node, tmp;
14496 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14497 if (list_ret == 0 || lhs == 0 || rhs == 0)
14498 return 0;
14500 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14501 add_loc_descr_to_each (list_ret, bra_node);
14503 add_loc_list (&list_ret, rhs);
14504 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
14505 add_loc_descr_to_each (list_ret, jump_node);
14507 add_loc_descr_to_each (list_ret, lhs);
14508 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14509 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
14511 /* ??? Need a node to point the skip at. Use a nop. */
14512 tmp = new_loc_descr (DW_OP_nop, 0, 0);
14513 add_loc_descr_to_each (list_ret, tmp);
14514 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14515 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
14517 break;
14519 case FIX_TRUNC_EXPR:
14520 return 0;
14522 default:
14523 /* Leave front-end specific codes as simply unknown. This comes
14524 up, for instance, with the C STMT_EXPR. */
14525 if ((unsigned int) TREE_CODE (loc)
14526 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
14528 expansion_failed (loc, NULL_RTX,
14529 "language specific tree node");
14530 return 0;
14533 #ifdef ENABLE_CHECKING
14534 /* Otherwise this is a generic code; we should just lists all of
14535 these explicitly. We forgot one. */
14536 gcc_unreachable ();
14537 #else
14538 /* In a release build, we want to degrade gracefully: better to
14539 generate incomplete debugging information than to crash. */
14540 return NULL;
14541 #endif
14544 if (!ret && !list_ret)
14545 return 0;
14547 if (want_address == 2 && !have_address
14548 && (dwarf_version >= 4 || !dwarf_strict))
14550 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14552 expansion_failed (loc, NULL_RTX,
14553 "DWARF address size mismatch");
14554 return 0;
14556 if (ret)
14557 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
14558 else
14559 add_loc_descr_to_each (list_ret,
14560 new_loc_descr (DW_OP_stack_value, 0, 0));
14561 have_address = 1;
14563 /* Show if we can't fill the request for an address. */
14564 if (want_address && !have_address)
14566 expansion_failed (loc, NULL_RTX,
14567 "Want address and only have value");
14568 return 0;
14571 gcc_assert (!ret || !list_ret);
14573 /* If we've got an address and don't want one, dereference. */
14574 if (!want_address && have_address)
14576 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14578 if (size > DWARF2_ADDR_SIZE || size == -1)
14580 expansion_failed (loc, NULL_RTX,
14581 "DWARF address size mismatch");
14582 return 0;
14584 else if (size == DWARF2_ADDR_SIZE)
14585 op = DW_OP_deref;
14586 else
14587 op = DW_OP_deref_size;
14589 if (ret)
14590 add_loc_descr (&ret, new_loc_descr (op, size, 0));
14591 else
14592 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
14594 if (ret)
14595 list_ret = new_loc_list (ret, NULL, NULL, NULL);
14597 return list_ret;
14600 /* Same as above but return only single location expression. */
14601 static dw_loc_descr_ref
14602 loc_descriptor_from_tree (tree loc, int want_address)
14604 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
14605 if (!ret)
14606 return NULL;
14607 if (ret->dw_loc_next)
14609 expansion_failed (loc, NULL_RTX,
14610 "Location list where only loc descriptor needed");
14611 return NULL;
14613 return ret->expr;
14616 /* Given a value, round it up to the lowest multiple of `boundary'
14617 which is not less than the value itself. */
14619 static inline HOST_WIDE_INT
14620 ceiling (HOST_WIDE_INT value, unsigned int boundary)
14622 return (((value + boundary - 1) / boundary) * boundary);
14625 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
14626 pointer to the declared type for the relevant field variable, or return
14627 `integer_type_node' if the given node turns out to be an
14628 ERROR_MARK node. */
14630 static inline tree
14631 field_type (const_tree decl)
14633 tree type;
14635 if (TREE_CODE (decl) == ERROR_MARK)
14636 return integer_type_node;
14638 type = DECL_BIT_FIELD_TYPE (decl);
14639 if (type == NULL_TREE)
14640 type = TREE_TYPE (decl);
14642 return type;
14645 /* Given a pointer to a tree node, return the alignment in bits for
14646 it, or else return BITS_PER_WORD if the node actually turns out to
14647 be an ERROR_MARK node. */
14649 static inline unsigned
14650 simple_type_align_in_bits (const_tree type)
14652 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
14655 static inline unsigned
14656 simple_decl_align_in_bits (const_tree decl)
14658 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
14661 /* Return the result of rounding T up to ALIGN. */
14663 static inline double_int
14664 round_up_to_align (double_int t, unsigned int align)
14666 double_int alignd = double_int::from_uhwi (align);
14667 t += alignd;
14668 t += double_int_minus_one;
14669 t = t.div (alignd, true, TRUNC_DIV_EXPR);
14670 t *= alignd;
14671 return t;
14674 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
14675 lowest addressed byte of the "containing object" for the given FIELD_DECL,
14676 or return 0 if we are unable to determine what that offset is, either
14677 because the argument turns out to be a pointer to an ERROR_MARK node, or
14678 because the offset is actually variable. (We can't handle the latter case
14679 just yet). */
14681 static HOST_WIDE_INT
14682 field_byte_offset (const_tree decl)
14684 double_int object_offset_in_bits;
14685 double_int object_offset_in_bytes;
14686 double_int bitpos_int;
14688 if (TREE_CODE (decl) == ERROR_MARK)
14689 return 0;
14691 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
14693 /* We cannot yet cope with fields whose positions are variable, so
14694 for now, when we see such things, we simply return 0. Someday, we may
14695 be able to handle such cases, but it will be damn difficult. */
14696 if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
14697 return 0;
14699 bitpos_int = tree_to_double_int (bit_position (decl));
14701 #ifdef PCC_BITFIELD_TYPE_MATTERS
14702 if (PCC_BITFIELD_TYPE_MATTERS)
14704 tree type;
14705 tree field_size_tree;
14706 double_int deepest_bitpos;
14707 double_int field_size_in_bits;
14708 unsigned int type_align_in_bits;
14709 unsigned int decl_align_in_bits;
14710 double_int type_size_in_bits;
14712 type = field_type (decl);
14713 type_size_in_bits = double_int_type_size_in_bits (type);
14714 type_align_in_bits = simple_type_align_in_bits (type);
14716 field_size_tree = DECL_SIZE (decl);
14718 /* The size could be unspecified if there was an error, or for
14719 a flexible array member. */
14720 if (!field_size_tree)
14721 field_size_tree = bitsize_zero_node;
14723 /* If the size of the field is not constant, use the type size. */
14724 if (TREE_CODE (field_size_tree) == INTEGER_CST)
14725 field_size_in_bits = tree_to_double_int (field_size_tree);
14726 else
14727 field_size_in_bits = type_size_in_bits;
14729 decl_align_in_bits = simple_decl_align_in_bits (decl);
14731 /* The GCC front-end doesn't make any attempt to keep track of the
14732 starting bit offset (relative to the start of the containing
14733 structure type) of the hypothetical "containing object" for a
14734 bit-field. Thus, when computing the byte offset value for the
14735 start of the "containing object" of a bit-field, we must deduce
14736 this information on our own. This can be rather tricky to do in
14737 some cases. For example, handling the following structure type
14738 definition when compiling for an i386/i486 target (which only
14739 aligns long long's to 32-bit boundaries) can be very tricky:
14741 struct S { int field1; long long field2:31; };
14743 Fortunately, there is a simple rule-of-thumb which can be used
14744 in such cases. When compiling for an i386/i486, GCC will
14745 allocate 8 bytes for the structure shown above. It decides to
14746 do this based upon one simple rule for bit-field allocation.
14747 GCC allocates each "containing object" for each bit-field at
14748 the first (i.e. lowest addressed) legitimate alignment boundary
14749 (based upon the required minimum alignment for the declared
14750 type of the field) which it can possibly use, subject to the
14751 condition that there is still enough available space remaining
14752 in the containing object (when allocated at the selected point)
14753 to fully accommodate all of the bits of the bit-field itself.
14755 This simple rule makes it obvious why GCC allocates 8 bytes for
14756 each object of the structure type shown above. When looking
14757 for a place to allocate the "containing object" for `field2',
14758 the compiler simply tries to allocate a 64-bit "containing
14759 object" at each successive 32-bit boundary (starting at zero)
14760 until it finds a place to allocate that 64- bit field such that
14761 at least 31 contiguous (and previously unallocated) bits remain
14762 within that selected 64 bit field. (As it turns out, for the
14763 example above, the compiler finds it is OK to allocate the
14764 "containing object" 64-bit field at bit-offset zero within the
14765 structure type.)
14767 Here we attempt to work backwards from the limited set of facts
14768 we're given, and we try to deduce from those facts, where GCC
14769 must have believed that the containing object started (within
14770 the structure type). The value we deduce is then used (by the
14771 callers of this routine) to generate DW_AT_location and
14772 DW_AT_bit_offset attributes for fields (both bit-fields and, in
14773 the case of DW_AT_location, regular fields as well). */
14775 /* Figure out the bit-distance from the start of the structure to
14776 the "deepest" bit of the bit-field. */
14777 deepest_bitpos = bitpos_int + field_size_in_bits;
14779 /* This is the tricky part. Use some fancy footwork to deduce
14780 where the lowest addressed bit of the containing object must
14781 be. */
14782 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
14784 /* Round up to type_align by default. This works best for
14785 bitfields. */
14786 object_offset_in_bits
14787 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
14789 if (object_offset_in_bits.ugt (bitpos_int))
14791 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
14793 /* Round up to decl_align instead. */
14794 object_offset_in_bits
14795 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
14798 else
14799 #endif /* PCC_BITFIELD_TYPE_MATTERS */
14800 object_offset_in_bits = bitpos_int;
14802 object_offset_in_bytes
14803 = object_offset_in_bits.div (double_int::from_uhwi (BITS_PER_UNIT),
14804 true, TRUNC_DIV_EXPR);
14805 return object_offset_in_bytes.to_shwi ();
14808 /* The following routines define various Dwarf attributes and any data
14809 associated with them. */
14811 /* Add a location description attribute value to a DIE.
14813 This emits location attributes suitable for whole variables and
14814 whole parameters. Note that the location attributes for struct fields are
14815 generated by the routine `data_member_location_attribute' below. */
14817 static inline void
14818 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
14819 dw_loc_list_ref descr)
14821 if (descr == 0)
14822 return;
14823 if (single_element_loc_list_p (descr))
14824 add_AT_loc (die, attr_kind, descr->expr);
14825 else
14826 add_AT_loc_list (die, attr_kind, descr);
14829 /* Add DW_AT_accessibility attribute to DIE if needed. */
14831 static void
14832 add_accessibility_attribute (dw_die_ref die, tree decl)
14834 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
14835 children, otherwise the default is DW_ACCESS_public. In DWARF2
14836 the default has always been DW_ACCESS_public. */
14837 if (TREE_PROTECTED (decl))
14838 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
14839 else if (TREE_PRIVATE (decl))
14841 if (dwarf_version == 2
14842 || die->die_parent == NULL
14843 || die->die_parent->die_tag != DW_TAG_class_type)
14844 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
14846 else if (dwarf_version > 2
14847 && die->die_parent
14848 && die->die_parent->die_tag == DW_TAG_class_type)
14849 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
14852 /* Attach the specialized form of location attribute used for data members of
14853 struct and union types. In the special case of a FIELD_DECL node which
14854 represents a bit-field, the "offset" part of this special location
14855 descriptor must indicate the distance in bytes from the lowest-addressed
14856 byte of the containing struct or union type to the lowest-addressed byte of
14857 the "containing object" for the bit-field. (See the `field_byte_offset'
14858 function above).
14860 For any given bit-field, the "containing object" is a hypothetical object
14861 (of some integral or enum type) within which the given bit-field lives. The
14862 type of this hypothetical "containing object" is always the same as the
14863 declared type of the individual bit-field itself (for GCC anyway... the
14864 DWARF spec doesn't actually mandate this). Note that it is the size (in
14865 bytes) of the hypothetical "containing object" which will be given in the
14866 DW_AT_byte_size attribute for this bit-field. (See the
14867 `byte_size_attribute' function below.) It is also used when calculating the
14868 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
14869 function below.) */
14871 static void
14872 add_data_member_location_attribute (dw_die_ref die, tree decl)
14874 HOST_WIDE_INT offset;
14875 dw_loc_descr_ref loc_descr = 0;
14877 if (TREE_CODE (decl) == TREE_BINFO)
14879 /* We're working on the TAG_inheritance for a base class. */
14880 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
14882 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
14883 aren't at a fixed offset from all (sub)objects of the same
14884 type. We need to extract the appropriate offset from our
14885 vtable. The following dwarf expression means
14887 BaseAddr = ObAddr + *((*ObAddr) - Offset)
14889 This is specific to the V3 ABI, of course. */
14891 dw_loc_descr_ref tmp;
14893 /* Make a copy of the object address. */
14894 tmp = new_loc_descr (DW_OP_dup, 0, 0);
14895 add_loc_descr (&loc_descr, tmp);
14897 /* Extract the vtable address. */
14898 tmp = new_loc_descr (DW_OP_deref, 0, 0);
14899 add_loc_descr (&loc_descr, tmp);
14901 /* Calculate the address of the offset. */
14902 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
14903 gcc_assert (offset < 0);
14905 tmp = int_loc_descriptor (-offset);
14906 add_loc_descr (&loc_descr, tmp);
14907 tmp = new_loc_descr (DW_OP_minus, 0, 0);
14908 add_loc_descr (&loc_descr, tmp);
14910 /* Extract the offset. */
14911 tmp = new_loc_descr (DW_OP_deref, 0, 0);
14912 add_loc_descr (&loc_descr, tmp);
14914 /* Add it to the object address. */
14915 tmp = new_loc_descr (DW_OP_plus, 0, 0);
14916 add_loc_descr (&loc_descr, tmp);
14918 else
14919 offset = tree_to_shwi (BINFO_OFFSET (decl));
14921 else
14922 offset = field_byte_offset (decl);
14924 if (! loc_descr)
14926 if (dwarf_version > 2)
14928 /* Don't need to output a location expression, just the constant. */
14929 if (offset < 0)
14930 add_AT_int (die, DW_AT_data_member_location, offset);
14931 else
14932 add_AT_unsigned (die, DW_AT_data_member_location, offset);
14933 return;
14935 else
14937 enum dwarf_location_atom op;
14939 /* The DWARF2 standard says that we should assume that the structure
14940 address is already on the stack, so we can specify a structure
14941 field address by using DW_OP_plus_uconst. */
14942 op = DW_OP_plus_uconst;
14943 loc_descr = new_loc_descr (op, offset, 0);
14947 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
14950 /* Writes integer values to dw_vec_const array. */
14952 static void
14953 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
14955 while (size != 0)
14957 *dest++ = val & 0xff;
14958 val >>= 8;
14959 --size;
14963 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
14965 static HOST_WIDE_INT
14966 extract_int (const unsigned char *src, unsigned int size)
14968 HOST_WIDE_INT val = 0;
14970 src += size;
14971 while (size != 0)
14973 val <<= 8;
14974 val |= *--src & 0xff;
14975 --size;
14977 return val;
14980 /* Writes double_int values to dw_vec_const array. */
14982 static void
14983 insert_double (double_int val, unsigned char *dest)
14985 unsigned char *p0 = dest;
14986 unsigned char *p1 = dest + sizeof (HOST_WIDE_INT);
14988 if (WORDS_BIG_ENDIAN)
14990 p0 = p1;
14991 p1 = dest;
14994 insert_int ((HOST_WIDE_INT) val.low, sizeof (HOST_WIDE_INT), p0);
14995 insert_int ((HOST_WIDE_INT) val.high, sizeof (HOST_WIDE_INT), p1);
14998 /* Writes floating point values to dw_vec_const array. */
15000 static void
15001 insert_float (const_rtx rtl, unsigned char *array)
15003 REAL_VALUE_TYPE rv;
15004 long val[4];
15005 int i;
15007 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
15008 real_to_target (val, &rv, GET_MODE (rtl));
15010 /* real_to_target puts 32-bit pieces in each long. Pack them. */
15011 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
15013 insert_int (val[i], 4, array);
15014 array += 4;
15018 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
15019 does not have a "location" either in memory or in a register. These
15020 things can arise in GNU C when a constant is passed as an actual parameter
15021 to an inlined function. They can also arise in C++ where declared
15022 constants do not necessarily get memory "homes". */
15024 static bool
15025 add_const_value_attribute (dw_die_ref die, rtx rtl)
15027 switch (GET_CODE (rtl))
15029 case CONST_INT:
15031 HOST_WIDE_INT val = INTVAL (rtl);
15033 if (val < 0)
15034 add_AT_int (die, DW_AT_const_value, val);
15035 else
15036 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
15038 return true;
15040 case CONST_DOUBLE:
15041 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
15042 floating-point constant. A CONST_DOUBLE is used whenever the
15043 constant requires more than one word in order to be adequately
15044 represented. */
15046 enum machine_mode mode = GET_MODE (rtl);
15048 if (SCALAR_FLOAT_MODE_P (mode))
15050 unsigned int length = GET_MODE_SIZE (mode);
15051 unsigned char *array = (unsigned char *) ggc_alloc_atomic (length);
15053 insert_float (rtl, array);
15054 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
15056 else
15057 add_AT_double (die, DW_AT_const_value,
15058 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
15060 return true;
15062 case CONST_VECTOR:
15064 enum machine_mode mode = GET_MODE (rtl);
15065 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
15066 unsigned int length = CONST_VECTOR_NUNITS (rtl);
15067 unsigned char *array = (unsigned char *) ggc_alloc_atomic
15068 (length * elt_size);
15069 unsigned int i;
15070 unsigned char *p;
15072 switch (GET_MODE_CLASS (mode))
15074 case MODE_VECTOR_INT:
15075 for (i = 0, p = array; i < length; i++, p += elt_size)
15077 rtx elt = CONST_VECTOR_ELT (rtl, i);
15078 double_int val = rtx_to_double_int (elt);
15080 if (elt_size <= sizeof (HOST_WIDE_INT))
15081 insert_int (val.to_shwi (), elt_size, p);
15082 else
15084 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
15085 insert_double (val, p);
15088 break;
15090 case MODE_VECTOR_FLOAT:
15091 for (i = 0, p = array; i < length; i++, p += elt_size)
15093 rtx elt = CONST_VECTOR_ELT (rtl, i);
15094 insert_float (elt, p);
15096 break;
15098 default:
15099 gcc_unreachable ();
15102 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
15104 return true;
15106 case CONST_STRING:
15107 if (dwarf_version >= 4 || !dwarf_strict)
15109 dw_loc_descr_ref loc_result;
15110 resolve_one_addr (&rtl, NULL);
15111 rtl_addr:
15112 loc_result = new_addr_loc_descr (rtl, dtprel_false);
15113 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
15114 add_AT_loc (die, DW_AT_location, loc_result);
15115 vec_safe_push (used_rtx_array, rtl);
15116 return true;
15118 return false;
15120 case CONST:
15121 if (CONSTANT_P (XEXP (rtl, 0)))
15122 return add_const_value_attribute (die, XEXP (rtl, 0));
15123 /* FALLTHROUGH */
15124 case SYMBOL_REF:
15125 if (!const_ok_for_output (rtl))
15126 return false;
15127 case LABEL_REF:
15128 if (dwarf_version >= 4 || !dwarf_strict)
15129 goto rtl_addr;
15130 return false;
15132 case PLUS:
15133 /* In cases where an inlined instance of an inline function is passed
15134 the address of an `auto' variable (which is local to the caller) we
15135 can get a situation where the DECL_RTL of the artificial local
15136 variable (for the inlining) which acts as a stand-in for the
15137 corresponding formal parameter (of the inline function) will look
15138 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
15139 exactly a compile-time constant expression, but it isn't the address
15140 of the (artificial) local variable either. Rather, it represents the
15141 *value* which the artificial local variable always has during its
15142 lifetime. We currently have no way to represent such quasi-constant
15143 values in Dwarf, so for now we just punt and generate nothing. */
15144 return false;
15146 case HIGH:
15147 case CONST_FIXED:
15148 return false;
15150 case MEM:
15151 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
15152 && MEM_READONLY_P (rtl)
15153 && GET_MODE (rtl) == BLKmode)
15155 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
15156 return true;
15158 return false;
15160 default:
15161 /* No other kinds of rtx should be possible here. */
15162 gcc_unreachable ();
15164 return false;
15167 /* Determine whether the evaluation of EXPR references any variables
15168 or functions which aren't otherwise used (and therefore may not be
15169 output). */
15170 static tree
15171 reference_to_unused (tree * tp, int * walk_subtrees,
15172 void * data ATTRIBUTE_UNUSED)
15174 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
15175 *walk_subtrees = 0;
15177 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
15178 && ! TREE_ASM_WRITTEN (*tp))
15179 return *tp;
15180 /* ??? The C++ FE emits debug information for using decls, so
15181 putting gcc_unreachable here falls over. See PR31899. For now
15182 be conservative. */
15183 else if (!cgraph_global_info_ready
15184 && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
15185 return *tp;
15186 else if (TREE_CODE (*tp) == VAR_DECL)
15188 varpool_node *node = varpool_get_node (*tp);
15189 if (!node || !node->definition)
15190 return *tp;
15192 else if (TREE_CODE (*tp) == FUNCTION_DECL
15193 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
15195 /* The call graph machinery must have finished analyzing,
15196 optimizing and gimplifying the CU by now.
15197 So if *TP has no call graph node associated
15198 to it, it means *TP will not be emitted. */
15199 if (!cgraph_get_node (*tp))
15200 return *tp;
15202 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
15203 return *tp;
15205 return NULL_TREE;
15208 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
15209 for use in a later add_const_value_attribute call. */
15211 static rtx
15212 rtl_for_decl_init (tree init, tree type)
15214 rtx rtl = NULL_RTX;
15216 STRIP_NOPS (init);
15218 /* If a variable is initialized with a string constant without embedded
15219 zeros, build CONST_STRING. */
15220 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
15222 tree enttype = TREE_TYPE (type);
15223 tree domain = TYPE_DOMAIN (type);
15224 enum machine_mode mode = TYPE_MODE (enttype);
15226 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
15227 && domain
15228 && integer_zerop (TYPE_MIN_VALUE (domain))
15229 && compare_tree_int (TYPE_MAX_VALUE (domain),
15230 TREE_STRING_LENGTH (init) - 1) == 0
15231 && ((size_t) TREE_STRING_LENGTH (init)
15232 == strlen (TREE_STRING_POINTER (init)) + 1))
15234 rtl = gen_rtx_CONST_STRING (VOIDmode,
15235 ggc_strdup (TREE_STRING_POINTER (init)));
15236 rtl = gen_rtx_MEM (BLKmode, rtl);
15237 MEM_READONLY_P (rtl) = 1;
15240 /* Other aggregates, and complex values, could be represented using
15241 CONCAT: FIXME! */
15242 else if (AGGREGATE_TYPE_P (type)
15243 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
15244 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
15245 || TREE_CODE (type) == COMPLEX_TYPE)
15247 /* Vectors only work if their mode is supported by the target.
15248 FIXME: generic vectors ought to work too. */
15249 else if (TREE_CODE (type) == VECTOR_TYPE
15250 && !VECTOR_MODE_P (TYPE_MODE (type)))
15252 /* If the initializer is something that we know will expand into an
15253 immediate RTL constant, expand it now. We must be careful not to
15254 reference variables which won't be output. */
15255 else if (initializer_constant_valid_p (init, type)
15256 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
15258 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
15259 possible. */
15260 if (TREE_CODE (type) == VECTOR_TYPE)
15261 switch (TREE_CODE (init))
15263 case VECTOR_CST:
15264 break;
15265 case CONSTRUCTOR:
15266 if (TREE_CONSTANT (init))
15268 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
15269 bool constant_p = true;
15270 tree value;
15271 unsigned HOST_WIDE_INT ix;
15273 /* Even when ctor is constant, it might contain non-*_CST
15274 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
15275 belong into VECTOR_CST nodes. */
15276 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
15277 if (!CONSTANT_CLASS_P (value))
15279 constant_p = false;
15280 break;
15283 if (constant_p)
15285 init = build_vector_from_ctor (type, elts);
15286 break;
15289 /* FALLTHRU */
15291 default:
15292 return NULL;
15295 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
15297 /* If expand_expr returns a MEM, it wasn't immediate. */
15298 gcc_assert (!rtl || !MEM_P (rtl));
15301 return rtl;
15304 /* Generate RTL for the variable DECL to represent its location. */
15306 static rtx
15307 rtl_for_decl_location (tree decl)
15309 rtx rtl;
15311 /* Here we have to decide where we are going to say the parameter "lives"
15312 (as far as the debugger is concerned). We only have a couple of
15313 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
15315 DECL_RTL normally indicates where the parameter lives during most of the
15316 activation of the function. If optimization is enabled however, this
15317 could be either NULL or else a pseudo-reg. Both of those cases indicate
15318 that the parameter doesn't really live anywhere (as far as the code
15319 generation parts of GCC are concerned) during most of the function's
15320 activation. That will happen (for example) if the parameter is never
15321 referenced within the function.
15323 We could just generate a location descriptor here for all non-NULL
15324 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
15325 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
15326 where DECL_RTL is NULL or is a pseudo-reg.
15328 Note however that we can only get away with using DECL_INCOMING_RTL as
15329 a backup substitute for DECL_RTL in certain limited cases. In cases
15330 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
15331 we can be sure that the parameter was passed using the same type as it is
15332 declared to have within the function, and that its DECL_INCOMING_RTL
15333 points us to a place where a value of that type is passed.
15335 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
15336 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
15337 because in these cases DECL_INCOMING_RTL points us to a value of some
15338 type which is *different* from the type of the parameter itself. Thus,
15339 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
15340 such cases, the debugger would end up (for example) trying to fetch a
15341 `float' from a place which actually contains the first part of a
15342 `double'. That would lead to really incorrect and confusing
15343 output at debug-time.
15345 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
15346 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
15347 are a couple of exceptions however. On little-endian machines we can
15348 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
15349 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
15350 an integral type that is smaller than TREE_TYPE (decl). These cases arise
15351 when (on a little-endian machine) a non-prototyped function has a
15352 parameter declared to be of type `short' or `char'. In such cases,
15353 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
15354 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
15355 passed `int' value. If the debugger then uses that address to fetch
15356 a `short' or a `char' (on a little-endian machine) the result will be
15357 the correct data, so we allow for such exceptional cases below.
15359 Note that our goal here is to describe the place where the given formal
15360 parameter lives during most of the function's activation (i.e. between the
15361 end of the prologue and the start of the epilogue). We'll do that as best
15362 as we can. Note however that if the given formal parameter is modified
15363 sometime during the execution of the function, then a stack backtrace (at
15364 debug-time) will show the function as having been called with the *new*
15365 value rather than the value which was originally passed in. This happens
15366 rarely enough that it is not a major problem, but it *is* a problem, and
15367 I'd like to fix it.
15369 A future version of dwarf2out.c may generate two additional attributes for
15370 any given DW_TAG_formal_parameter DIE which will describe the "passed
15371 type" and the "passed location" for the given formal parameter in addition
15372 to the attributes we now generate to indicate the "declared type" and the
15373 "active location" for each parameter. This additional set of attributes
15374 could be used by debuggers for stack backtraces. Separately, note that
15375 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
15376 This happens (for example) for inlined-instances of inline function formal
15377 parameters which are never referenced. This really shouldn't be
15378 happening. All PARM_DECL nodes should get valid non-NULL
15379 DECL_INCOMING_RTL values. FIXME. */
15381 /* Use DECL_RTL as the "location" unless we find something better. */
15382 rtl = DECL_RTL_IF_SET (decl);
15384 /* When generating abstract instances, ignore everything except
15385 constants, symbols living in memory, and symbols living in
15386 fixed registers. */
15387 if (! reload_completed)
15389 if (rtl
15390 && (CONSTANT_P (rtl)
15391 || (MEM_P (rtl)
15392 && CONSTANT_P (XEXP (rtl, 0)))
15393 || (REG_P (rtl)
15394 && TREE_CODE (decl) == VAR_DECL
15395 && TREE_STATIC (decl))))
15397 rtl = targetm.delegitimize_address (rtl);
15398 return rtl;
15400 rtl = NULL_RTX;
15402 else if (TREE_CODE (decl) == PARM_DECL)
15404 if (rtl == NULL_RTX
15405 || is_pseudo_reg (rtl)
15406 || (MEM_P (rtl)
15407 && is_pseudo_reg (XEXP (rtl, 0))
15408 && DECL_INCOMING_RTL (decl)
15409 && MEM_P (DECL_INCOMING_RTL (decl))
15410 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
15412 tree declared_type = TREE_TYPE (decl);
15413 tree passed_type = DECL_ARG_TYPE (decl);
15414 enum machine_mode dmode = TYPE_MODE (declared_type);
15415 enum machine_mode pmode = TYPE_MODE (passed_type);
15417 /* This decl represents a formal parameter which was optimized out.
15418 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
15419 all cases where (rtl == NULL_RTX) just below. */
15420 if (dmode == pmode)
15421 rtl = DECL_INCOMING_RTL (decl);
15422 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
15423 && SCALAR_INT_MODE_P (dmode)
15424 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
15425 && DECL_INCOMING_RTL (decl))
15427 rtx inc = DECL_INCOMING_RTL (decl);
15428 if (REG_P (inc))
15429 rtl = inc;
15430 else if (MEM_P (inc))
15432 if (BYTES_BIG_ENDIAN)
15433 rtl = adjust_address_nv (inc, dmode,
15434 GET_MODE_SIZE (pmode)
15435 - GET_MODE_SIZE (dmode));
15436 else
15437 rtl = inc;
15442 /* If the parm was passed in registers, but lives on the stack, then
15443 make a big endian correction if the mode of the type of the
15444 parameter is not the same as the mode of the rtl. */
15445 /* ??? This is the same series of checks that are made in dbxout.c before
15446 we reach the big endian correction code there. It isn't clear if all
15447 of these checks are necessary here, but keeping them all is the safe
15448 thing to do. */
15449 else if (MEM_P (rtl)
15450 && XEXP (rtl, 0) != const0_rtx
15451 && ! CONSTANT_P (XEXP (rtl, 0))
15452 /* Not passed in memory. */
15453 && !MEM_P (DECL_INCOMING_RTL (decl))
15454 /* Not passed by invisible reference. */
15455 && (!REG_P (XEXP (rtl, 0))
15456 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
15457 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
15458 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
15459 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
15460 #endif
15462 /* Big endian correction check. */
15463 && BYTES_BIG_ENDIAN
15464 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
15465 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
15466 < UNITS_PER_WORD))
15468 enum machine_mode addr_mode = get_address_mode (rtl);
15469 int offset = (UNITS_PER_WORD
15470 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
15472 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15473 plus_constant (addr_mode, XEXP (rtl, 0), offset));
15476 else if (TREE_CODE (decl) == VAR_DECL
15477 && rtl
15478 && MEM_P (rtl)
15479 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
15480 && BYTES_BIG_ENDIAN)
15482 enum machine_mode addr_mode = get_address_mode (rtl);
15483 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
15484 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
15486 /* If a variable is declared "register" yet is smaller than
15487 a register, then if we store the variable to memory, it
15488 looks like we're storing a register-sized value, when in
15489 fact we are not. We need to adjust the offset of the
15490 storage location to reflect the actual value's bytes,
15491 else gdb will not be able to display it. */
15492 if (rsize > dsize)
15493 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15494 plus_constant (addr_mode, XEXP (rtl, 0),
15495 rsize - dsize));
15498 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
15499 and will have been substituted directly into all expressions that use it.
15500 C does not have such a concept, but C++ and other languages do. */
15501 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
15502 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
15504 if (rtl)
15505 rtl = targetm.delegitimize_address (rtl);
15507 /* If we don't look past the constant pool, we risk emitting a
15508 reference to a constant pool entry that isn't referenced from
15509 code, and thus is not emitted. */
15510 if (rtl)
15511 rtl = avoid_constant_pool_reference (rtl);
15513 /* Try harder to get a rtl. If this symbol ends up not being emitted
15514 in the current CU, resolve_addr will remove the expression referencing
15515 it. */
15516 if (rtl == NULL_RTX
15517 && TREE_CODE (decl) == VAR_DECL
15518 && !DECL_EXTERNAL (decl)
15519 && TREE_STATIC (decl)
15520 && DECL_NAME (decl)
15521 && !DECL_HARD_REGISTER (decl)
15522 && DECL_MODE (decl) != VOIDmode)
15524 rtl = make_decl_rtl_for_debug (decl);
15525 if (!MEM_P (rtl)
15526 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
15527 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
15528 rtl = NULL_RTX;
15531 return rtl;
15534 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
15535 returned. If so, the decl for the COMMON block is returned, and the
15536 value is the offset into the common block for the symbol. */
15538 static tree
15539 fortran_common (tree decl, HOST_WIDE_INT *value)
15541 tree val_expr, cvar;
15542 enum machine_mode mode;
15543 HOST_WIDE_INT bitsize, bitpos;
15544 tree offset;
15545 int unsignedp, volatilep = 0;
15547 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
15548 it does not have a value (the offset into the common area), or if it
15549 is thread local (as opposed to global) then it isn't common, and shouldn't
15550 be handled as such. */
15551 if (TREE_CODE (decl) != VAR_DECL
15552 || !TREE_STATIC (decl)
15553 || !DECL_HAS_VALUE_EXPR_P (decl)
15554 || !is_fortran ())
15555 return NULL_TREE;
15557 val_expr = DECL_VALUE_EXPR (decl);
15558 if (TREE_CODE (val_expr) != COMPONENT_REF)
15559 return NULL_TREE;
15561 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
15562 &mode, &unsignedp, &volatilep, true);
15564 if (cvar == NULL_TREE
15565 || TREE_CODE (cvar) != VAR_DECL
15566 || DECL_ARTIFICIAL (cvar)
15567 || !TREE_PUBLIC (cvar))
15568 return NULL_TREE;
15570 *value = 0;
15571 if (offset != NULL)
15573 if (!tree_fits_shwi_p (offset))
15574 return NULL_TREE;
15575 *value = tree_to_shwi (offset);
15577 if (bitpos != 0)
15578 *value += bitpos / BITS_PER_UNIT;
15580 return cvar;
15583 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
15584 data attribute for a variable or a parameter. We generate the
15585 DW_AT_const_value attribute only in those cases where the given variable
15586 or parameter does not have a true "location" either in memory or in a
15587 register. This can happen (for example) when a constant is passed as an
15588 actual argument in a call to an inline function. (It's possible that
15589 these things can crop up in other ways also.) Note that one type of
15590 constant value which can be passed into an inlined function is a constant
15591 pointer. This can happen for example if an actual argument in an inlined
15592 function call evaluates to a compile-time constant address.
15594 CACHE_P is true if it is worth caching the location list for DECL,
15595 so that future calls can reuse it rather than regenerate it from scratch.
15596 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
15597 since we will need to refer to them each time the function is inlined. */
15599 static bool
15600 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
15601 enum dwarf_attribute attr)
15603 rtx rtl;
15604 dw_loc_list_ref list;
15605 var_loc_list *loc_list;
15606 cached_dw_loc_list *cache;
15607 void **slot;
15609 if (TREE_CODE (decl) == ERROR_MARK)
15610 return false;
15612 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
15613 || TREE_CODE (decl) == RESULT_DECL);
15615 /* Try to get some constant RTL for this decl, and use that as the value of
15616 the location. */
15618 rtl = rtl_for_decl_location (decl);
15619 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15620 && add_const_value_attribute (die, rtl))
15621 return true;
15623 /* See if we have single element location list that is equivalent to
15624 a constant value. That way we are better to use add_const_value_attribute
15625 rather than expanding constant value equivalent. */
15626 loc_list = lookup_decl_loc (decl);
15627 if (loc_list
15628 && loc_list->first
15629 && loc_list->first->next == NULL
15630 && NOTE_P (loc_list->first->loc)
15631 && NOTE_VAR_LOCATION (loc_list->first->loc)
15632 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
15634 struct var_loc_node *node;
15636 node = loc_list->first;
15637 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
15638 if (GET_CODE (rtl) == EXPR_LIST)
15639 rtl = XEXP (rtl, 0);
15640 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15641 && add_const_value_attribute (die, rtl))
15642 return true;
15644 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
15645 list several times. See if we've already cached the contents. */
15646 list = NULL;
15647 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
15648 cache_p = false;
15649 if (cache_p)
15651 cache = (cached_dw_loc_list *)
15652 htab_find_with_hash (cached_dw_loc_list_table, decl, DECL_UID (decl));
15653 if (cache)
15654 list = cache->loc_list;
15656 if (list == NULL)
15658 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
15659 /* It is usually worth caching this result if the decl is from
15660 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
15661 if (cache_p && list && list->dw_loc_next)
15663 slot = htab_find_slot_with_hash (cached_dw_loc_list_table, decl,
15664 DECL_UID (decl), INSERT);
15665 cache = ggc_alloc_cleared_cached_dw_loc_list ();
15666 cache->decl_id = DECL_UID (decl);
15667 cache->loc_list = list;
15668 *slot = cache;
15671 if (list)
15673 add_AT_location_description (die, attr, list);
15674 return true;
15676 /* None of that worked, so it must not really have a location;
15677 try adding a constant value attribute from the DECL_INITIAL. */
15678 return tree_add_const_value_attribute_for_decl (die, decl);
15681 /* Add VARIABLE and DIE into deferred locations list. */
15683 static void
15684 defer_location (tree variable, dw_die_ref die)
15686 deferred_locations entry;
15687 entry.variable = variable;
15688 entry.die = die;
15689 vec_safe_push (deferred_locations_list, entry);
15692 /* Helper function for tree_add_const_value_attribute. Natively encode
15693 initializer INIT into an array. Return true if successful. */
15695 static bool
15696 native_encode_initializer (tree init, unsigned char *array, int size)
15698 tree type;
15700 if (init == NULL_TREE)
15701 return false;
15703 STRIP_NOPS (init);
15704 switch (TREE_CODE (init))
15706 case STRING_CST:
15707 type = TREE_TYPE (init);
15708 if (TREE_CODE (type) == ARRAY_TYPE)
15710 tree enttype = TREE_TYPE (type);
15711 enum machine_mode mode = TYPE_MODE (enttype);
15713 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
15714 return false;
15715 if (int_size_in_bytes (type) != size)
15716 return false;
15717 if (size > TREE_STRING_LENGTH (init))
15719 memcpy (array, TREE_STRING_POINTER (init),
15720 TREE_STRING_LENGTH (init));
15721 memset (array + TREE_STRING_LENGTH (init),
15722 '\0', size - TREE_STRING_LENGTH (init));
15724 else
15725 memcpy (array, TREE_STRING_POINTER (init), size);
15726 return true;
15728 return false;
15729 case CONSTRUCTOR:
15730 type = TREE_TYPE (init);
15731 if (int_size_in_bytes (type) != size)
15732 return false;
15733 if (TREE_CODE (type) == ARRAY_TYPE)
15735 HOST_WIDE_INT min_index;
15736 unsigned HOST_WIDE_INT cnt;
15737 int curpos = 0, fieldsize;
15738 constructor_elt *ce;
15740 if (TYPE_DOMAIN (type) == NULL_TREE
15741 || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
15742 return false;
15744 fieldsize = int_size_in_bytes (TREE_TYPE (type));
15745 if (fieldsize <= 0)
15746 return false;
15748 min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
15749 memset (array, '\0', size);
15750 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
15752 tree val = ce->value;
15753 tree index = ce->index;
15754 int pos = curpos;
15755 if (index && TREE_CODE (index) == RANGE_EXPR)
15756 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
15757 * fieldsize;
15758 else if (index)
15759 pos = (tree_to_shwi (index) - min_index) * fieldsize;
15761 if (val)
15763 STRIP_NOPS (val);
15764 if (!native_encode_initializer (val, array + pos, fieldsize))
15765 return false;
15767 curpos = pos + fieldsize;
15768 if (index && TREE_CODE (index) == RANGE_EXPR)
15770 int count = tree_to_shwi (TREE_OPERAND (index, 1))
15771 - tree_to_shwi (TREE_OPERAND (index, 0));
15772 while (count-- > 0)
15774 if (val)
15775 memcpy (array + curpos, array + pos, fieldsize);
15776 curpos += fieldsize;
15779 gcc_assert (curpos <= size);
15781 return true;
15783 else if (TREE_CODE (type) == RECORD_TYPE
15784 || TREE_CODE (type) == UNION_TYPE)
15786 tree field = NULL_TREE;
15787 unsigned HOST_WIDE_INT cnt;
15788 constructor_elt *ce;
15790 if (int_size_in_bytes (type) != size)
15791 return false;
15793 if (TREE_CODE (type) == RECORD_TYPE)
15794 field = TYPE_FIELDS (type);
15796 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
15798 tree val = ce->value;
15799 int pos, fieldsize;
15801 if (ce->index != 0)
15802 field = ce->index;
15804 if (val)
15805 STRIP_NOPS (val);
15807 if (field == NULL_TREE || DECL_BIT_FIELD (field))
15808 return false;
15810 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
15811 && TYPE_DOMAIN (TREE_TYPE (field))
15812 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
15813 return false;
15814 else if (DECL_SIZE_UNIT (field) == NULL_TREE
15815 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
15816 return false;
15817 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
15818 pos = int_byte_position (field);
15819 gcc_assert (pos + fieldsize <= size);
15820 if (val
15821 && !native_encode_initializer (val, array + pos, fieldsize))
15822 return false;
15824 return true;
15826 return false;
15827 case VIEW_CONVERT_EXPR:
15828 case NON_LVALUE_EXPR:
15829 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
15830 default:
15831 return native_encode_expr (init, array, size) == size;
15835 /* Attach a DW_AT_const_value attribute to DIE. The value of the
15836 attribute is the const value T. */
15838 static bool
15839 tree_add_const_value_attribute (dw_die_ref die, tree t)
15841 tree init;
15842 tree type = TREE_TYPE (t);
15843 rtx rtl;
15845 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
15846 return false;
15848 init = t;
15849 gcc_assert (!DECL_P (init));
15851 rtl = rtl_for_decl_init (init, type);
15852 if (rtl)
15853 return add_const_value_attribute (die, rtl);
15854 /* If the host and target are sane, try harder. */
15855 else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
15856 && initializer_constant_valid_p (init, type))
15858 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
15859 if (size > 0 && (int) size == size)
15861 unsigned char *array = (unsigned char *)
15862 ggc_alloc_cleared_atomic (size);
15864 if (native_encode_initializer (init, array, size))
15866 add_AT_vec (die, DW_AT_const_value, size, 1, array);
15867 return true;
15869 ggc_free (array);
15872 return false;
15875 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
15876 attribute is the const value of T, where T is an integral constant
15877 variable with static storage duration
15878 (so it can't be a PARM_DECL or a RESULT_DECL). */
15880 static bool
15881 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
15884 if (!decl
15885 || (TREE_CODE (decl) != VAR_DECL
15886 && TREE_CODE (decl) != CONST_DECL)
15887 || (TREE_CODE (decl) == VAR_DECL
15888 && !TREE_STATIC (decl)))
15889 return false;
15891 if (TREE_READONLY (decl)
15892 && ! TREE_THIS_VOLATILE (decl)
15893 && DECL_INITIAL (decl))
15894 /* OK */;
15895 else
15896 return false;
15898 /* Don't add DW_AT_const_value if abstract origin already has one. */
15899 if (get_AT (var_die, DW_AT_const_value))
15900 return false;
15902 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
15905 /* Convert the CFI instructions for the current function into a
15906 location list. This is used for DW_AT_frame_base when we targeting
15907 a dwarf2 consumer that does not support the dwarf3
15908 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
15909 expressions. */
15911 static dw_loc_list_ref
15912 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
15914 int ix;
15915 dw_fde_ref fde;
15916 dw_loc_list_ref list, *list_tail;
15917 dw_cfi_ref cfi;
15918 dw_cfa_location last_cfa, next_cfa;
15919 const char *start_label, *last_label, *section;
15920 dw_cfa_location remember;
15922 fde = cfun->fde;
15923 gcc_assert (fde != NULL);
15925 section = secname_for_decl (current_function_decl);
15926 list_tail = &list;
15927 list = NULL;
15929 memset (&next_cfa, 0, sizeof (next_cfa));
15930 next_cfa.reg = INVALID_REGNUM;
15931 remember = next_cfa;
15933 start_label = fde->dw_fde_begin;
15935 /* ??? Bald assumption that the CIE opcode list does not contain
15936 advance opcodes. */
15937 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
15938 lookup_cfa_1 (cfi, &next_cfa, &remember);
15940 last_cfa = next_cfa;
15941 last_label = start_label;
15943 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
15945 /* If the first partition contained no CFI adjustments, the
15946 CIE opcodes apply to the whole first partition. */
15947 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15948 fde->dw_fde_begin, fde->dw_fde_end, section);
15949 list_tail =&(*list_tail)->dw_loc_next;
15950 start_label = last_label = fde->dw_fde_second_begin;
15953 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
15955 switch (cfi->dw_cfi_opc)
15957 case DW_CFA_set_loc:
15958 case DW_CFA_advance_loc1:
15959 case DW_CFA_advance_loc2:
15960 case DW_CFA_advance_loc4:
15961 if (!cfa_equal_p (&last_cfa, &next_cfa))
15963 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15964 start_label, last_label, section);
15966 list_tail = &(*list_tail)->dw_loc_next;
15967 last_cfa = next_cfa;
15968 start_label = last_label;
15970 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
15971 break;
15973 case DW_CFA_advance_loc:
15974 /* The encoding is complex enough that we should never emit this. */
15975 gcc_unreachable ();
15977 default:
15978 lookup_cfa_1 (cfi, &next_cfa, &remember);
15979 break;
15981 if (ix + 1 == fde->dw_fde_switch_cfi_index)
15983 if (!cfa_equal_p (&last_cfa, &next_cfa))
15985 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15986 start_label, last_label, section);
15988 list_tail = &(*list_tail)->dw_loc_next;
15989 last_cfa = next_cfa;
15990 start_label = last_label;
15992 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15993 start_label, fde->dw_fde_end, section);
15994 list_tail = &(*list_tail)->dw_loc_next;
15995 start_label = last_label = fde->dw_fde_second_begin;
15999 if (!cfa_equal_p (&last_cfa, &next_cfa))
16001 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16002 start_label, last_label, section);
16003 list_tail = &(*list_tail)->dw_loc_next;
16004 start_label = last_label;
16007 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
16008 start_label,
16009 fde->dw_fde_second_begin
16010 ? fde->dw_fde_second_end : fde->dw_fde_end,
16011 section);
16013 if (list && list->dw_loc_next)
16014 gen_llsym (list);
16016 return list;
16019 /* Compute a displacement from the "steady-state frame pointer" to the
16020 frame base (often the same as the CFA), and store it in
16021 frame_pointer_fb_offset. OFFSET is added to the displacement
16022 before the latter is negated. */
16024 static void
16025 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
16027 rtx reg, elim;
16029 #ifdef FRAME_POINTER_CFA_OFFSET
16030 reg = frame_pointer_rtx;
16031 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
16032 #else
16033 reg = arg_pointer_rtx;
16034 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
16035 #endif
16037 elim = (ira_use_lra_p
16038 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
16039 : eliminate_regs (reg, VOIDmode, NULL_RTX));
16040 if (GET_CODE (elim) == PLUS)
16042 offset += INTVAL (XEXP (elim, 1));
16043 elim = XEXP (elim, 0);
16046 frame_pointer_fb_offset = -offset;
16048 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
16049 in which to eliminate. This is because it's stack pointer isn't
16050 directly accessible as a register within the ISA. To work around
16051 this, assume that while we cannot provide a proper value for
16052 frame_pointer_fb_offset, we won't need one either. */
16053 frame_pointer_fb_offset_valid
16054 = ((SUPPORTS_STACK_ALIGNMENT
16055 && (elim == hard_frame_pointer_rtx
16056 || elim == stack_pointer_rtx))
16057 || elim == (frame_pointer_needed
16058 ? hard_frame_pointer_rtx
16059 : stack_pointer_rtx));
16062 /* Generate a DW_AT_name attribute given some string value to be included as
16063 the value of the attribute. */
16065 static void
16066 add_name_attribute (dw_die_ref die, const char *name_string)
16068 if (name_string != NULL && *name_string != 0)
16070 if (demangle_name_func)
16071 name_string = (*demangle_name_func) (name_string);
16073 add_AT_string (die, DW_AT_name, name_string);
16077 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
16078 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
16079 of TYPE accordingly.
16081 ??? This is a temporary measure until after we're able to generate
16082 regular DWARF for the complex Ada type system. */
16084 static void
16085 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
16086 dw_die_ref context_die)
16088 tree dtype;
16089 dw_die_ref dtype_die;
16091 if (!lang_hooks.types.descriptive_type)
16092 return;
16094 dtype = lang_hooks.types.descriptive_type (type);
16095 if (!dtype)
16096 return;
16098 dtype_die = lookup_type_die (dtype);
16099 if (!dtype_die)
16101 gen_type_die (dtype, context_die);
16102 dtype_die = lookup_type_die (dtype);
16103 gcc_assert (dtype_die);
16106 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
16109 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
16111 static const char *
16112 comp_dir_string (void)
16114 const char *wd;
16115 char *wd1;
16116 static const char *cached_wd = NULL;
16118 if (cached_wd != NULL)
16119 return cached_wd;
16121 wd = get_src_pwd ();
16122 if (wd == NULL)
16123 return NULL;
16125 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
16127 int wdlen;
16129 wdlen = strlen (wd);
16130 wd1 = (char *) ggc_alloc_atomic (wdlen + 2);
16131 strcpy (wd1, wd);
16132 wd1 [wdlen] = DIR_SEPARATOR;
16133 wd1 [wdlen + 1] = 0;
16134 wd = wd1;
16137 cached_wd = remap_debug_filename (wd);
16138 return cached_wd;
16141 /* Generate a DW_AT_comp_dir attribute for DIE. */
16143 static void
16144 add_comp_dir_attribute (dw_die_ref die)
16146 const char * wd = comp_dir_string ();
16147 if (wd != NULL)
16148 add_AT_string (die, DW_AT_comp_dir, wd);
16151 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
16152 default. */
16154 static int
16155 lower_bound_default (void)
16157 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
16159 case DW_LANG_C:
16160 case DW_LANG_C89:
16161 case DW_LANG_C99:
16162 case DW_LANG_C_plus_plus:
16163 case DW_LANG_ObjC:
16164 case DW_LANG_ObjC_plus_plus:
16165 case DW_LANG_Java:
16166 return 0;
16167 case DW_LANG_Fortran77:
16168 case DW_LANG_Fortran90:
16169 case DW_LANG_Fortran95:
16170 return 1;
16171 case DW_LANG_UPC:
16172 case DW_LANG_D:
16173 case DW_LANG_Python:
16174 return dwarf_version >= 4 ? 0 : -1;
16175 case DW_LANG_Ada95:
16176 case DW_LANG_Ada83:
16177 case DW_LANG_Cobol74:
16178 case DW_LANG_Cobol85:
16179 case DW_LANG_Pascal83:
16180 case DW_LANG_Modula2:
16181 case DW_LANG_PLI:
16182 return dwarf_version >= 4 ? 1 : -1;
16183 default:
16184 return -1;
16188 /* Given a tree node describing an array bound (either lower or upper) output
16189 a representation for that bound. */
16191 static void
16192 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
16194 switch (TREE_CODE (bound))
16196 case ERROR_MARK:
16197 return;
16199 /* All fixed-bounds are represented by INTEGER_CST nodes. */
16200 case INTEGER_CST:
16202 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
16203 int dflt;
16205 /* Use the default if possible. */
16206 if (bound_attr == DW_AT_lower_bound
16207 && tree_fits_shwi_p (bound)
16208 && (dflt = lower_bound_default ()) != -1
16209 && tree_to_shwi (bound) == dflt)
16212 /* Otherwise represent the bound as an unsigned value with the
16213 precision of its type. The precision and signedness of the
16214 type will be necessary to re-interpret it unambiguously. */
16215 else if (prec < HOST_BITS_PER_WIDE_INT)
16217 unsigned HOST_WIDE_INT mask
16218 = ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
16219 add_AT_unsigned (subrange_die, bound_attr,
16220 TREE_INT_CST_LOW (bound) & mask);
16222 else if (prec == HOST_BITS_PER_WIDE_INT
16223 || TREE_INT_CST_HIGH (bound) == 0)
16224 add_AT_unsigned (subrange_die, bound_attr,
16225 TREE_INT_CST_LOW (bound));
16226 else
16227 add_AT_double (subrange_die, bound_attr, TREE_INT_CST_HIGH (bound),
16228 TREE_INT_CST_LOW (bound));
16230 break;
16232 CASE_CONVERT:
16233 case VIEW_CONVERT_EXPR:
16234 add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
16235 break;
16237 case SAVE_EXPR:
16238 break;
16240 case VAR_DECL:
16241 case PARM_DECL:
16242 case RESULT_DECL:
16244 dw_die_ref decl_die = lookup_decl_die (bound);
16246 /* ??? Can this happen, or should the variable have been bound
16247 first? Probably it can, since I imagine that we try to create
16248 the types of parameters in the order in which they exist in
16249 the list, and won't have created a forward reference to a
16250 later parameter. */
16251 if (decl_die != NULL)
16253 add_AT_die_ref (subrange_die, bound_attr, decl_die);
16254 break;
16257 /* FALLTHRU */
16259 default:
16261 /* Otherwise try to create a stack operation procedure to
16262 evaluate the value of the array bound. */
16264 dw_die_ref ctx, decl_die;
16265 dw_loc_list_ref list;
16267 list = loc_list_from_tree (bound, 2);
16268 if (list == NULL || single_element_loc_list_p (list))
16270 /* If DW_AT_*bound is not a reference nor constant, it is
16271 a DWARF expression rather than location description.
16272 For that loc_list_from_tree (bound, 0) is needed.
16273 If that fails to give a single element list,
16274 fall back to outputting this as a reference anyway. */
16275 dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
16276 if (list2 && single_element_loc_list_p (list2))
16278 add_AT_loc (subrange_die, bound_attr, list2->expr);
16279 break;
16282 if (list == NULL)
16283 break;
16285 if (current_function_decl == 0)
16286 ctx = comp_unit_die ();
16287 else
16288 ctx = lookup_decl_die (current_function_decl);
16290 decl_die = new_die (DW_TAG_variable, ctx, bound);
16291 add_AT_flag (decl_die, DW_AT_artificial, 1);
16292 add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
16293 add_AT_location_description (decl_die, DW_AT_location, list);
16294 add_AT_die_ref (subrange_die, bound_attr, decl_die);
16295 break;
16300 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
16301 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
16302 Note that the block of subscript information for an array type also
16303 includes information about the element type of the given array type. */
16305 static void
16306 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
16308 unsigned dimension_number;
16309 tree lower, upper;
16310 dw_die_ref subrange_die;
16312 for (dimension_number = 0;
16313 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
16314 type = TREE_TYPE (type), dimension_number++)
16316 tree domain = TYPE_DOMAIN (type);
16318 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
16319 break;
16321 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
16322 and (in GNU C only) variable bounds. Handle all three forms
16323 here. */
16324 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
16325 if (domain)
16327 /* We have an array type with specified bounds. */
16328 lower = TYPE_MIN_VALUE (domain);
16329 upper = TYPE_MAX_VALUE (domain);
16331 /* Define the index type. */
16332 if (TREE_TYPE (domain))
16334 /* ??? This is probably an Ada unnamed subrange type. Ignore the
16335 TREE_TYPE field. We can't emit debug info for this
16336 because it is an unnamed integral type. */
16337 if (TREE_CODE (domain) == INTEGER_TYPE
16338 && TYPE_NAME (domain) == NULL_TREE
16339 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
16340 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
16342 else
16343 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
16344 type_die);
16347 /* ??? If upper is NULL, the array has unspecified length,
16348 but it does have a lower bound. This happens with Fortran
16349 dimension arr(N:*)
16350 Since the debugger is definitely going to need to know N
16351 to produce useful results, go ahead and output the lower
16352 bound solo, and hope the debugger can cope. */
16354 add_bound_info (subrange_die, DW_AT_lower_bound, lower);
16355 if (upper)
16356 add_bound_info (subrange_die, DW_AT_upper_bound, upper);
16359 /* Otherwise we have an array type with an unspecified length. The
16360 DWARF-2 spec does not say how to handle this; let's just leave out the
16361 bounds. */
16365 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
16367 static void
16368 add_byte_size_attribute (dw_die_ref die, tree tree_node)
16370 dw_die_ref decl_die;
16371 HOST_WIDE_INT size;
16373 switch (TREE_CODE (tree_node))
16375 case ERROR_MARK:
16376 size = 0;
16377 break;
16378 case ENUMERAL_TYPE:
16379 case RECORD_TYPE:
16380 case UNION_TYPE:
16381 case QUAL_UNION_TYPE:
16382 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
16383 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
16385 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
16386 return;
16388 size = int_size_in_bytes (tree_node);
16389 break;
16390 case FIELD_DECL:
16391 /* For a data member of a struct or union, the DW_AT_byte_size is
16392 generally given as the number of bytes normally allocated for an
16393 object of the *declared* type of the member itself. This is true
16394 even for bit-fields. */
16395 size = int_size_in_bytes (field_type (tree_node));
16396 break;
16397 default:
16398 gcc_unreachable ();
16401 /* Note that `size' might be -1 when we get to this point. If it is, that
16402 indicates that the byte size of the entity in question is variable. We
16403 have no good way of expressing this fact in Dwarf at the present time,
16404 when location description was not used by the caller code instead. */
16405 if (size >= 0)
16406 add_AT_unsigned (die, DW_AT_byte_size, size);
16409 /* For a FIELD_DECL node which represents a bit-field, output an attribute
16410 which specifies the distance in bits from the highest order bit of the
16411 "containing object" for the bit-field to the highest order bit of the
16412 bit-field itself.
16414 For any given bit-field, the "containing object" is a hypothetical object
16415 (of some integral or enum type) within which the given bit-field lives. The
16416 type of this hypothetical "containing object" is always the same as the
16417 declared type of the individual bit-field itself. The determination of the
16418 exact location of the "containing object" for a bit-field is rather
16419 complicated. It's handled by the `field_byte_offset' function (above).
16421 Note that it is the size (in bytes) of the hypothetical "containing object"
16422 which will be given in the DW_AT_byte_size attribute for this bit-field.
16423 (See `byte_size_attribute' above). */
16425 static inline void
16426 add_bit_offset_attribute (dw_die_ref die, tree decl)
16428 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
16429 tree type = DECL_BIT_FIELD_TYPE (decl);
16430 HOST_WIDE_INT bitpos_int;
16431 HOST_WIDE_INT highest_order_object_bit_offset;
16432 HOST_WIDE_INT highest_order_field_bit_offset;
16433 HOST_WIDE_INT bit_offset;
16435 /* Must be a field and a bit field. */
16436 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
16438 /* We can't yet handle bit-fields whose offsets are variable, so if we
16439 encounter such things, just return without generating any attribute
16440 whatsoever. Likewise for variable or too large size. */
16441 if (! tree_fits_shwi_p (bit_position (decl))
16442 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
16443 return;
16445 bitpos_int = int_bit_position (decl);
16447 /* Note that the bit offset is always the distance (in bits) from the
16448 highest-order bit of the "containing object" to the highest-order bit of
16449 the bit-field itself. Since the "high-order end" of any object or field
16450 is different on big-endian and little-endian machines, the computation
16451 below must take account of these differences. */
16452 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
16453 highest_order_field_bit_offset = bitpos_int;
16455 if (! BYTES_BIG_ENDIAN)
16457 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
16458 highest_order_object_bit_offset += simple_type_size_in_bits (type);
16461 bit_offset
16462 = (! BYTES_BIG_ENDIAN
16463 ? highest_order_object_bit_offset - highest_order_field_bit_offset
16464 : highest_order_field_bit_offset - highest_order_object_bit_offset);
16466 if (bit_offset < 0)
16467 add_AT_int (die, DW_AT_bit_offset, bit_offset);
16468 else
16469 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
16472 /* For a FIELD_DECL node which represents a bit field, output an attribute
16473 which specifies the length in bits of the given field. */
16475 static inline void
16476 add_bit_size_attribute (dw_die_ref die, tree decl)
16478 /* Must be a field and a bit field. */
16479 gcc_assert (TREE_CODE (decl) == FIELD_DECL
16480 && DECL_BIT_FIELD_TYPE (decl));
16482 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
16483 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
16486 /* If the compiled language is ANSI C, then add a 'prototyped'
16487 attribute, if arg types are given for the parameters of a function. */
16489 static inline void
16490 add_prototyped_attribute (dw_die_ref die, tree func_type)
16492 if (get_AT_unsigned (comp_unit_die (), DW_AT_language) == DW_LANG_C89
16493 && prototype_p (func_type))
16494 add_AT_flag (die, DW_AT_prototyped, 1);
16497 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
16498 by looking in either the type declaration or object declaration
16499 equate table. */
16501 static inline dw_die_ref
16502 add_abstract_origin_attribute (dw_die_ref die, tree origin)
16504 dw_die_ref origin_die = NULL;
16506 if (TREE_CODE (origin) != FUNCTION_DECL)
16508 /* We may have gotten separated from the block for the inlined
16509 function, if we're in an exception handler or some such; make
16510 sure that the abstract function has been written out.
16512 Doing this for nested functions is wrong, however; functions are
16513 distinct units, and our context might not even be inline. */
16514 tree fn = origin;
16516 if (TYPE_P (fn))
16517 fn = TYPE_STUB_DECL (fn);
16519 fn = decl_function_context (fn);
16520 if (fn)
16521 dwarf2out_abstract_function (fn);
16524 if (DECL_P (origin))
16525 origin_die = lookup_decl_die (origin);
16526 else if (TYPE_P (origin))
16527 origin_die = lookup_type_die (origin);
16529 /* XXX: Functions that are never lowered don't always have correct block
16530 trees (in the case of java, they simply have no block tree, in some other
16531 languages). For these functions, there is nothing we can really do to
16532 output correct debug info for inlined functions in all cases. Rather
16533 than die, we'll just produce deficient debug info now, in that we will
16534 have variables without a proper abstract origin. In the future, when all
16535 functions are lowered, we should re-add a gcc_assert (origin_die)
16536 here. */
16538 if (origin_die)
16539 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
16540 return origin_die;
16543 /* We do not currently support the pure_virtual attribute. */
16545 static inline void
16546 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
16548 if (DECL_VINDEX (func_decl))
16550 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
16552 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
16553 add_AT_loc (die, DW_AT_vtable_elem_location,
16554 new_loc_descr (DW_OP_constu,
16555 tree_to_shwi (DECL_VINDEX (func_decl)),
16556 0));
16558 /* GNU extension: Record what type this method came from originally. */
16559 if (debug_info_level > DINFO_LEVEL_TERSE
16560 && DECL_CONTEXT (func_decl))
16561 add_AT_die_ref (die, DW_AT_containing_type,
16562 lookup_type_die (DECL_CONTEXT (func_decl)));
16566 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
16567 given decl. This used to be a vendor extension until after DWARF 4
16568 standardized it. */
16570 static void
16571 add_linkage_attr (dw_die_ref die, tree decl)
16573 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
16575 /* Mimic what assemble_name_raw does with a leading '*'. */
16576 if (name[0] == '*')
16577 name = &name[1];
16579 if (dwarf_version >= 4)
16580 add_AT_string (die, DW_AT_linkage_name, name);
16581 else
16582 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
16585 /* Add source coordinate attributes for the given decl. */
16587 static void
16588 add_src_coords_attributes (dw_die_ref die, tree decl)
16590 expanded_location s;
16592 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
16593 return;
16594 s = expand_location (DECL_SOURCE_LOCATION (decl));
16595 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
16596 add_AT_unsigned (die, DW_AT_decl_line, s.line);
16599 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
16601 static void
16602 add_linkage_name (dw_die_ref die, tree decl)
16604 if (debug_info_level > DINFO_LEVEL_TERSE
16605 && (TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
16606 && TREE_PUBLIC (decl)
16607 && !DECL_ABSTRACT (decl)
16608 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
16609 && die->die_tag != DW_TAG_member)
16611 /* Defer until we have an assembler name set. */
16612 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
16614 limbo_die_node *asm_name;
16616 asm_name = ggc_alloc_cleared_limbo_die_node ();
16617 asm_name->die = die;
16618 asm_name->created_for = decl;
16619 asm_name->next = deferred_asm_name;
16620 deferred_asm_name = asm_name;
16622 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
16623 add_linkage_attr (die, decl);
16627 /* Add a DW_AT_name attribute and source coordinate attribute for the
16628 given decl, but only if it actually has a name. */
16630 static void
16631 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
16633 tree decl_name;
16635 decl_name = DECL_NAME (decl);
16636 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
16638 const char *name = dwarf2_name (decl, 0);
16639 if (name)
16640 add_name_attribute (die, name);
16641 if (! DECL_ARTIFICIAL (decl))
16642 add_src_coords_attributes (die, decl);
16644 add_linkage_name (die, decl);
16647 #ifdef VMS_DEBUGGING_INFO
16648 /* Get the function's name, as described by its RTL. This may be different
16649 from the DECL_NAME name used in the source file. */
16650 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
16652 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
16653 XEXP (DECL_RTL (decl), 0), false);
16654 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
16656 #endif /* VMS_DEBUGGING_INFO */
16659 #ifdef VMS_DEBUGGING_INFO
16660 /* Output the debug main pointer die for VMS */
16662 void
16663 dwarf2out_vms_debug_main_pointer (void)
16665 char label[MAX_ARTIFICIAL_LABEL_BYTES];
16666 dw_die_ref die;
16668 /* Allocate the VMS debug main subprogram die. */
16669 die = ggc_alloc_cleared_die_node ();
16670 die->die_tag = DW_TAG_subprogram;
16671 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
16672 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
16673 current_function_funcdef_no);
16674 add_AT_lbl_id (die, DW_AT_entry_pc, label);
16676 /* Make it the first child of comp_unit_die (). */
16677 die->die_parent = comp_unit_die ();
16678 if (comp_unit_die ()->die_child)
16680 die->die_sib = comp_unit_die ()->die_child->die_sib;
16681 comp_unit_die ()->die_child->die_sib = die;
16683 else
16685 die->die_sib = die;
16686 comp_unit_die ()->die_child = die;
16689 #endif /* VMS_DEBUGGING_INFO */
16691 /* Push a new declaration scope. */
16693 static void
16694 push_decl_scope (tree scope)
16696 vec_safe_push (decl_scope_table, scope);
16699 /* Pop a declaration scope. */
16701 static inline void
16702 pop_decl_scope (void)
16704 decl_scope_table->pop ();
16707 /* walk_tree helper function for uses_local_type, below. */
16709 static tree
16710 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
16712 if (!TYPE_P (*tp))
16713 *walk_subtrees = 0;
16714 else
16716 tree name = TYPE_NAME (*tp);
16717 if (name && DECL_P (name) && decl_function_context (name))
16718 return *tp;
16720 return NULL_TREE;
16723 /* If TYPE involves a function-local type (including a local typedef to a
16724 non-local type), returns that type; otherwise returns NULL_TREE. */
16726 static tree
16727 uses_local_type (tree type)
16729 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
16730 return used;
16733 /* Return the DIE for the scope that immediately contains this type.
16734 Non-named types that do not involve a function-local type get global
16735 scope. Named types nested in namespaces or other types get their
16736 containing scope. All other types (i.e. function-local named types) get
16737 the current active scope. */
16739 static dw_die_ref
16740 scope_die_for (tree t, dw_die_ref context_die)
16742 dw_die_ref scope_die = NULL;
16743 tree containing_scope;
16745 /* Non-types always go in the current scope. */
16746 gcc_assert (TYPE_P (t));
16748 /* Use the scope of the typedef, rather than the scope of the type
16749 it refers to. */
16750 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
16751 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
16752 else
16753 containing_scope = TYPE_CONTEXT (t);
16755 /* Use the containing namespace if there is one. */
16756 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
16758 if (context_die == lookup_decl_die (containing_scope))
16759 /* OK */;
16760 else if (debug_info_level > DINFO_LEVEL_TERSE)
16761 context_die = get_context_die (containing_scope);
16762 else
16763 containing_scope = NULL_TREE;
16766 /* Ignore function type "scopes" from the C frontend. They mean that
16767 a tagged type is local to a parmlist of a function declarator, but
16768 that isn't useful to DWARF. */
16769 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
16770 containing_scope = NULL_TREE;
16772 if (SCOPE_FILE_SCOPE_P (containing_scope))
16774 /* If T uses a local type keep it local as well, to avoid references
16775 to function-local DIEs from outside the function. */
16776 if (current_function_decl && uses_local_type (t))
16777 scope_die = context_die;
16778 else
16779 scope_die = comp_unit_die ();
16781 else if (TYPE_P (containing_scope))
16783 /* For types, we can just look up the appropriate DIE. */
16784 if (debug_info_level > DINFO_LEVEL_TERSE)
16785 scope_die = get_context_die (containing_scope);
16786 else
16788 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
16789 if (scope_die == NULL)
16790 scope_die = comp_unit_die ();
16793 else
16794 scope_die = context_die;
16796 return scope_die;
16799 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
16801 static inline int
16802 local_scope_p (dw_die_ref context_die)
16804 for (; context_die; context_die = context_die->die_parent)
16805 if (context_die->die_tag == DW_TAG_inlined_subroutine
16806 || context_die->die_tag == DW_TAG_subprogram)
16807 return 1;
16809 return 0;
16812 /* Returns nonzero if CONTEXT_DIE is a class. */
16814 static inline int
16815 class_scope_p (dw_die_ref context_die)
16817 return (context_die
16818 && (context_die->die_tag == DW_TAG_structure_type
16819 || context_die->die_tag == DW_TAG_class_type
16820 || context_die->die_tag == DW_TAG_interface_type
16821 || context_die->die_tag == DW_TAG_union_type));
16824 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
16825 whether or not to treat a DIE in this context as a declaration. */
16827 static inline int
16828 class_or_namespace_scope_p (dw_die_ref context_die)
16830 return (class_scope_p (context_die)
16831 || (context_die && context_die->die_tag == DW_TAG_namespace));
16834 /* Many forms of DIEs require a "type description" attribute. This
16835 routine locates the proper "type descriptor" die for the type given
16836 by 'type', and adds a DW_AT_type attribute below the given die. */
16838 static void
16839 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
16840 int decl_volatile, dw_die_ref context_die)
16842 enum tree_code code = TREE_CODE (type);
16843 dw_die_ref type_die = NULL;
16845 /* ??? If this type is an unnamed subrange type of an integral, floating-point
16846 or fixed-point type, use the inner type. This is because we have no
16847 support for unnamed types in base_type_die. This can happen if this is
16848 an Ada subrange type. Correct solution is emit a subrange type die. */
16849 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
16850 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
16851 type = TREE_TYPE (type), code = TREE_CODE (type);
16853 if (code == ERROR_MARK
16854 /* Handle a special case. For functions whose return type is void, we
16855 generate *no* type attribute. (Note that no object may have type
16856 `void', so this only applies to function return types). */
16857 || code == VOID_TYPE)
16858 return;
16860 type_die = modified_type_die (type,
16861 decl_const || TYPE_READONLY (type),
16862 decl_volatile || TYPE_VOLATILE (type),
16863 context_die);
16865 if (type_die != NULL)
16866 add_AT_die_ref (object_die, DW_AT_type, type_die);
16869 /* Given an object die, add the calling convention attribute for the
16870 function call type. */
16871 static void
16872 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
16874 enum dwarf_calling_convention value = DW_CC_normal;
16876 value = ((enum dwarf_calling_convention)
16877 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
16879 if (is_fortran ()
16880 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
16882 /* DWARF 2 doesn't provide a way to identify a program's source-level
16883 entry point. DW_AT_calling_convention attributes are only meant
16884 to describe functions' calling conventions. However, lacking a
16885 better way to signal the Fortran main program, we used this for
16886 a long time, following existing custom. Now, DWARF 4 has
16887 DW_AT_main_subprogram, which we add below, but some tools still
16888 rely on the old way, which we thus keep. */
16889 value = DW_CC_program;
16891 if (dwarf_version >= 4 || !dwarf_strict)
16892 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
16895 /* Only add the attribute if the backend requests it, and
16896 is not DW_CC_normal. */
16897 if (value && (value != DW_CC_normal))
16898 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
16901 /* Given a tree pointer to a struct, class, union, or enum type node, return
16902 a pointer to the (string) tag name for the given type, or zero if the type
16903 was declared without a tag. */
16905 static const char *
16906 type_tag (const_tree type)
16908 const char *name = 0;
16910 if (TYPE_NAME (type) != 0)
16912 tree t = 0;
16914 /* Find the IDENTIFIER_NODE for the type name. */
16915 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
16916 && !TYPE_NAMELESS (type))
16917 t = TYPE_NAME (type);
16919 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
16920 a TYPE_DECL node, regardless of whether or not a `typedef' was
16921 involved. */
16922 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
16923 && ! DECL_IGNORED_P (TYPE_NAME (type)))
16925 /* We want to be extra verbose. Don't call dwarf_name if
16926 DECL_NAME isn't set. The default hook for decl_printable_name
16927 doesn't like that, and in this context it's correct to return
16928 0, instead of "<anonymous>" or the like. */
16929 if (DECL_NAME (TYPE_NAME (type))
16930 && !DECL_NAMELESS (TYPE_NAME (type)))
16931 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
16934 /* Now get the name as a string, or invent one. */
16935 if (!name && t != 0)
16936 name = IDENTIFIER_POINTER (t);
16939 return (name == 0 || *name == '\0') ? 0 : name;
16942 /* Return the type associated with a data member, make a special check
16943 for bit field types. */
16945 static inline tree
16946 member_declared_type (const_tree member)
16948 return (DECL_BIT_FIELD_TYPE (member)
16949 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
16952 /* Get the decl's label, as described by its RTL. This may be different
16953 from the DECL_NAME name used in the source file. */
16955 #if 0
16956 static const char *
16957 decl_start_label (tree decl)
16959 rtx x;
16960 const char *fnname;
16962 x = DECL_RTL (decl);
16963 gcc_assert (MEM_P (x));
16965 x = XEXP (x, 0);
16966 gcc_assert (GET_CODE (x) == SYMBOL_REF);
16968 fnname = XSTR (x, 0);
16969 return fnname;
16971 #endif
16973 /* These routines generate the internal representation of the DIE's for
16974 the compilation unit. Debugging information is collected by walking
16975 the declaration trees passed in from dwarf2out_decl(). */
16977 static void
16978 gen_array_type_die (tree type, dw_die_ref context_die)
16980 dw_die_ref scope_die = scope_die_for (type, context_die);
16981 dw_die_ref array_die;
16983 /* GNU compilers represent multidimensional array types as sequences of one
16984 dimensional array types whose element types are themselves array types.
16985 We sometimes squish that down to a single array_type DIE with multiple
16986 subscripts in the Dwarf debugging info. The draft Dwarf specification
16987 say that we are allowed to do this kind of compression in C, because
16988 there is no difference between an array of arrays and a multidimensional
16989 array. We don't do this for Ada to remain as close as possible to the
16990 actual representation, which is especially important against the language
16991 flexibilty wrt arrays of variable size. */
16993 bool collapse_nested_arrays = !is_ada ();
16994 tree element_type;
16996 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
16997 DW_TAG_string_type doesn't have DW_AT_type attribute). */
16998 if (TYPE_STRING_FLAG (type)
16999 && TREE_CODE (type) == ARRAY_TYPE
17000 && is_fortran ()
17001 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
17003 HOST_WIDE_INT size;
17005 array_die = new_die (DW_TAG_string_type, scope_die, type);
17006 add_name_attribute (array_die, type_tag (type));
17007 equate_type_number_to_die (type, array_die);
17008 size = int_size_in_bytes (type);
17009 if (size >= 0)
17010 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17011 else if (TYPE_DOMAIN (type) != NULL_TREE
17012 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
17013 && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
17015 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
17016 dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
17018 size = int_size_in_bytes (TREE_TYPE (szdecl));
17019 if (loc && size > 0)
17021 add_AT_location_description (array_die, DW_AT_string_length, loc);
17022 if (size != DWARF2_ADDR_SIZE)
17023 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17026 return;
17029 array_die = new_die (DW_TAG_array_type, scope_die, type);
17030 add_name_attribute (array_die, type_tag (type));
17031 equate_type_number_to_die (type, array_die);
17033 if (TREE_CODE (type) == VECTOR_TYPE)
17034 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
17036 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
17037 if (is_fortran ()
17038 && TREE_CODE (type) == ARRAY_TYPE
17039 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
17040 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
17041 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17043 #if 0
17044 /* We default the array ordering. SDB will probably do
17045 the right things even if DW_AT_ordering is not present. It's not even
17046 an issue until we start to get into multidimensional arrays anyway. If
17047 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
17048 then we'll have to put the DW_AT_ordering attribute back in. (But if
17049 and when we find out that we need to put these in, we will only do so
17050 for multidimensional arrays. */
17051 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17052 #endif
17054 if (TREE_CODE (type) == VECTOR_TYPE)
17056 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
17057 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
17058 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
17059 add_bound_info (subrange_die, DW_AT_upper_bound,
17060 size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
17062 else
17063 add_subscript_info (array_die, type, collapse_nested_arrays);
17065 /* Add representation of the type of the elements of this array type and
17066 emit the corresponding DIE if we haven't done it already. */
17067 element_type = TREE_TYPE (type);
17068 if (collapse_nested_arrays)
17069 while (TREE_CODE (element_type) == ARRAY_TYPE)
17071 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
17072 break;
17073 element_type = TREE_TYPE (element_type);
17076 add_type_attribute (array_die, element_type, 0, 0, context_die);
17078 add_gnat_descriptive_type_attribute (array_die, type, context_die);
17079 if (TYPE_ARTIFICIAL (type))
17080 add_AT_flag (array_die, DW_AT_artificial, 1);
17082 if (get_AT (array_die, DW_AT_name))
17083 add_pubtype (type, array_die);
17086 static dw_loc_descr_ref
17087 descr_info_loc (tree val, tree base_decl)
17089 HOST_WIDE_INT size;
17090 dw_loc_descr_ref loc, loc2;
17091 enum dwarf_location_atom op;
17093 if (val == base_decl)
17094 return new_loc_descr (DW_OP_push_object_address, 0, 0);
17096 switch (TREE_CODE (val))
17098 CASE_CONVERT:
17099 return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17100 case VAR_DECL:
17101 return loc_descriptor_from_tree (val, 0);
17102 case INTEGER_CST:
17103 if (tree_fits_shwi_p (val))
17104 return int_loc_descriptor (tree_to_shwi (val));
17105 break;
17106 case INDIRECT_REF:
17107 size = int_size_in_bytes (TREE_TYPE (val));
17108 if (size < 0)
17109 break;
17110 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17111 if (!loc)
17112 break;
17113 if (size == DWARF2_ADDR_SIZE)
17114 add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
17115 else
17116 add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
17117 return loc;
17118 case POINTER_PLUS_EXPR:
17119 case PLUS_EXPR:
17120 if (tree_fits_uhwi_p (TREE_OPERAND (val, 1))
17121 && tree_to_uhwi (TREE_OPERAND (val, 1)) < 16384)
17123 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17124 if (!loc)
17125 break;
17126 loc_descr_plus_const (&loc, tree_to_shwi (TREE_OPERAND (val, 1)));
17128 else
17130 op = DW_OP_plus;
17131 do_binop:
17132 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17133 if (!loc)
17134 break;
17135 loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
17136 if (!loc2)
17137 break;
17138 add_loc_descr (&loc, loc2);
17139 add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
17141 return loc;
17142 case MINUS_EXPR:
17143 op = DW_OP_minus;
17144 goto do_binop;
17145 case MULT_EXPR:
17146 op = DW_OP_mul;
17147 goto do_binop;
17148 case EQ_EXPR:
17149 op = DW_OP_eq;
17150 goto do_binop;
17151 case NE_EXPR:
17152 op = DW_OP_ne;
17153 goto do_binop;
17154 default:
17155 break;
17157 return NULL;
17160 static void
17161 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
17162 tree val, tree base_decl)
17164 dw_loc_descr_ref loc;
17166 if (tree_fits_shwi_p (val))
17168 add_AT_unsigned (die, attr, tree_to_shwi (val));
17169 return;
17172 loc = descr_info_loc (val, base_decl);
17173 if (!loc)
17174 return;
17176 add_AT_loc (die, attr, loc);
17179 /* This routine generates DIE for array with hidden descriptor, details
17180 are filled into *info by a langhook. */
17182 static void
17183 gen_descr_array_type_die (tree type, struct array_descr_info *info,
17184 dw_die_ref context_die)
17186 dw_die_ref scope_die = scope_die_for (type, context_die);
17187 dw_die_ref array_die;
17188 int dim;
17190 array_die = new_die (DW_TAG_array_type, scope_die, type);
17191 add_name_attribute (array_die, type_tag (type));
17192 equate_type_number_to_die (type, array_die);
17194 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
17195 if (is_fortran ()
17196 && info->ndimensions >= 2)
17197 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17199 if (info->data_location)
17200 add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
17201 info->base_decl);
17202 if (info->associated)
17203 add_descr_info_field (array_die, DW_AT_associated, info->associated,
17204 info->base_decl);
17205 if (info->allocated)
17206 add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
17207 info->base_decl);
17209 for (dim = 0; dim < info->ndimensions; dim++)
17211 dw_die_ref subrange_die
17212 = new_die (DW_TAG_subrange_type, array_die, NULL);
17214 if (info->dimen[dim].lower_bound)
17216 /* If it is the default value, omit it. */
17217 int dflt;
17219 if (tree_fits_shwi_p (info->dimen[dim].lower_bound)
17220 && (dflt = lower_bound_default ()) != -1
17221 && tree_to_shwi (info->dimen[dim].lower_bound) == dflt)
17223 else
17224 add_descr_info_field (subrange_die, DW_AT_lower_bound,
17225 info->dimen[dim].lower_bound,
17226 info->base_decl);
17228 if (info->dimen[dim].upper_bound)
17229 add_descr_info_field (subrange_die, DW_AT_upper_bound,
17230 info->dimen[dim].upper_bound,
17231 info->base_decl);
17232 if (info->dimen[dim].stride)
17233 add_descr_info_field (subrange_die, DW_AT_byte_stride,
17234 info->dimen[dim].stride,
17235 info->base_decl);
17238 gen_type_die (info->element_type, context_die);
17239 add_type_attribute (array_die, info->element_type, 0, 0, context_die);
17241 if (get_AT (array_die, DW_AT_name))
17242 add_pubtype (type, array_die);
17245 #if 0
17246 static void
17247 gen_entry_point_die (tree decl, dw_die_ref context_die)
17249 tree origin = decl_ultimate_origin (decl);
17250 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
17252 if (origin != NULL)
17253 add_abstract_origin_attribute (decl_die, origin);
17254 else
17256 add_name_and_src_coords_attributes (decl_die, decl);
17257 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
17258 0, 0, context_die);
17261 if (DECL_ABSTRACT (decl))
17262 equate_decl_number_to_die (decl, decl_die);
17263 else
17264 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
17266 #endif
17268 /* Walk through the list of incomplete types again, trying once more to
17269 emit full debugging info for them. */
17271 static void
17272 retry_incomplete_types (void)
17274 int i;
17276 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
17277 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
17278 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
17281 /* Determine what tag to use for a record type. */
17283 static enum dwarf_tag
17284 record_type_tag (tree type)
17286 if (! lang_hooks.types.classify_record)
17287 return DW_TAG_structure_type;
17289 switch (lang_hooks.types.classify_record (type))
17291 case RECORD_IS_STRUCT:
17292 return DW_TAG_structure_type;
17294 case RECORD_IS_CLASS:
17295 return DW_TAG_class_type;
17297 case RECORD_IS_INTERFACE:
17298 if (dwarf_version >= 3 || !dwarf_strict)
17299 return DW_TAG_interface_type;
17300 return DW_TAG_structure_type;
17302 default:
17303 gcc_unreachable ();
17307 /* Generate a DIE to represent an enumeration type. Note that these DIEs
17308 include all of the information about the enumeration values also. Each
17309 enumerated type name/value is listed as a child of the enumerated type
17310 DIE. */
17312 static dw_die_ref
17313 gen_enumeration_type_die (tree type, dw_die_ref context_die)
17315 dw_die_ref type_die = lookup_type_die (type);
17317 if (type_die == NULL)
17319 type_die = new_die (DW_TAG_enumeration_type,
17320 scope_die_for (type, context_die), type);
17321 equate_type_number_to_die (type, type_die);
17322 add_name_attribute (type_die, type_tag (type));
17323 if (dwarf_version >= 4 || !dwarf_strict)
17325 if (ENUM_IS_SCOPED (type))
17326 add_AT_flag (type_die, DW_AT_enum_class, 1);
17327 if (ENUM_IS_OPAQUE (type))
17328 add_AT_flag (type_die, DW_AT_declaration, 1);
17331 else if (! TYPE_SIZE (type))
17332 return type_die;
17333 else
17334 remove_AT (type_die, DW_AT_declaration);
17336 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
17337 given enum type is incomplete, do not generate the DW_AT_byte_size
17338 attribute or the DW_AT_element_list attribute. */
17339 if (TYPE_SIZE (type))
17341 tree link;
17343 TREE_ASM_WRITTEN (type) = 1;
17344 add_byte_size_attribute (type_die, type);
17345 if (TYPE_STUB_DECL (type) != NULL_TREE)
17347 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
17348 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
17351 /* If the first reference to this type was as the return type of an
17352 inline function, then it may not have a parent. Fix this now. */
17353 if (type_die->die_parent == NULL)
17354 add_child_die (scope_die_for (type, context_die), type_die);
17356 for (link = TYPE_VALUES (type);
17357 link != NULL; link = TREE_CHAIN (link))
17359 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
17360 tree value = TREE_VALUE (link);
17362 add_name_attribute (enum_die,
17363 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
17365 if (TREE_CODE (value) == CONST_DECL)
17366 value = DECL_INITIAL (value);
17368 if (simple_type_size_in_bits (TREE_TYPE (value))
17369 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
17370 /* DWARF2 does not provide a way of indicating whether or
17371 not enumeration constants are signed or unsigned. GDB
17372 always assumes the values are signed, so we output all
17373 values as if they were signed. That means that
17374 enumeration constants with very large unsigned values
17375 will appear to have negative values in the debugger.
17377 TODO: the above comment is wrong, DWARF2 does provide
17378 DW_FORM_sdata/DW_FORM_udata to represent signed/unsigned data.
17379 This should be re-worked to use correct signed/unsigned
17380 int/double tags for all cases, instead of always treating as
17381 signed. */
17382 add_AT_int (enum_die, DW_AT_const_value, TREE_INT_CST_LOW (value));
17383 else
17384 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
17385 that here. */
17386 add_AT_double (enum_die, DW_AT_const_value,
17387 TREE_INT_CST_HIGH (value), TREE_INT_CST_LOW (value));
17390 add_gnat_descriptive_type_attribute (type_die, type, context_die);
17391 if (TYPE_ARTIFICIAL (type))
17392 add_AT_flag (type_die, DW_AT_artificial, 1);
17394 else
17395 add_AT_flag (type_die, DW_AT_declaration, 1);
17397 add_pubtype (type, type_die);
17399 return type_die;
17402 /* Generate a DIE to represent either a real live formal parameter decl or to
17403 represent just the type of some formal parameter position in some function
17404 type.
17406 Note that this routine is a bit unusual because its argument may be a
17407 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
17408 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
17409 node. If it's the former then this function is being called to output a
17410 DIE to represent a formal parameter object (or some inlining thereof). If
17411 it's the latter, then this function is only being called to output a
17412 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
17413 argument type of some subprogram type.
17414 If EMIT_NAME_P is true, name and source coordinate attributes
17415 are emitted. */
17417 static dw_die_ref
17418 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
17419 dw_die_ref context_die)
17421 tree node_or_origin = node ? node : origin;
17422 tree ultimate_origin;
17423 dw_die_ref parm_die
17424 = new_die (DW_TAG_formal_parameter, context_die, node);
17426 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
17428 case tcc_declaration:
17429 ultimate_origin = decl_ultimate_origin (node_or_origin);
17430 if (node || ultimate_origin)
17431 origin = ultimate_origin;
17432 if (origin != NULL)
17433 add_abstract_origin_attribute (parm_die, origin);
17434 else if (emit_name_p)
17435 add_name_and_src_coords_attributes (parm_die, node);
17436 if (origin == NULL
17437 || (! DECL_ABSTRACT (node_or_origin)
17438 && variably_modified_type_p (TREE_TYPE (node_or_origin),
17439 decl_function_context
17440 (node_or_origin))))
17442 tree type = TREE_TYPE (node_or_origin);
17443 if (decl_by_reference_p (node_or_origin))
17444 add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
17445 context_die);
17446 else
17447 add_type_attribute (parm_die, type,
17448 TREE_READONLY (node_or_origin),
17449 TREE_THIS_VOLATILE (node_or_origin),
17450 context_die);
17452 if (origin == NULL && DECL_ARTIFICIAL (node))
17453 add_AT_flag (parm_die, DW_AT_artificial, 1);
17455 if (node && node != origin)
17456 equate_decl_number_to_die (node, parm_die);
17457 if (! DECL_ABSTRACT (node_or_origin))
17458 add_location_or_const_value_attribute (parm_die, node_or_origin,
17459 node == NULL, DW_AT_location);
17461 break;
17463 case tcc_type:
17464 /* We were called with some kind of a ..._TYPE node. */
17465 add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
17466 break;
17468 default:
17469 gcc_unreachable ();
17472 return parm_die;
17475 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
17476 children DW_TAG_formal_parameter DIEs representing the arguments of the
17477 parameter pack.
17479 PARM_PACK must be a function parameter pack.
17480 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
17481 must point to the subsequent arguments of the function PACK_ARG belongs to.
17482 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
17483 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
17484 following the last one for which a DIE was generated. */
17486 static dw_die_ref
17487 gen_formal_parameter_pack_die (tree parm_pack,
17488 tree pack_arg,
17489 dw_die_ref subr_die,
17490 tree *next_arg)
17492 tree arg;
17493 dw_die_ref parm_pack_die;
17495 gcc_assert (parm_pack
17496 && lang_hooks.function_parameter_pack_p (parm_pack)
17497 && subr_die);
17499 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
17500 add_src_coords_attributes (parm_pack_die, parm_pack);
17502 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
17504 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
17505 parm_pack))
17506 break;
17507 gen_formal_parameter_die (arg, NULL,
17508 false /* Don't emit name attribute. */,
17509 parm_pack_die);
17511 if (next_arg)
17512 *next_arg = arg;
17513 return parm_pack_die;
17516 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
17517 at the end of an (ANSI prototyped) formal parameters list. */
17519 static void
17520 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
17522 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
17525 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
17526 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
17527 parameters as specified in some function type specification (except for
17528 those which appear as part of a function *definition*). */
17530 static void
17531 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
17533 tree link;
17534 tree formal_type = NULL;
17535 tree first_parm_type;
17536 tree arg;
17538 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
17540 arg = DECL_ARGUMENTS (function_or_method_type);
17541 function_or_method_type = TREE_TYPE (function_or_method_type);
17543 else
17544 arg = NULL_TREE;
17546 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
17548 /* Make our first pass over the list of formal parameter types and output a
17549 DW_TAG_formal_parameter DIE for each one. */
17550 for (link = first_parm_type; link; )
17552 dw_die_ref parm_die;
17554 formal_type = TREE_VALUE (link);
17555 if (formal_type == void_type_node)
17556 break;
17558 /* Output a (nameless) DIE to represent the formal parameter itself. */
17559 parm_die = gen_formal_parameter_die (formal_type, NULL,
17560 true /* Emit name attribute. */,
17561 context_die);
17562 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
17563 && link == first_parm_type)
17565 add_AT_flag (parm_die, DW_AT_artificial, 1);
17566 if (dwarf_version >= 3 || !dwarf_strict)
17567 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
17569 else if (arg && DECL_ARTIFICIAL (arg))
17570 add_AT_flag (parm_die, DW_AT_artificial, 1);
17572 link = TREE_CHAIN (link);
17573 if (arg)
17574 arg = DECL_CHAIN (arg);
17577 /* If this function type has an ellipsis, add a
17578 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
17579 if (formal_type != void_type_node)
17580 gen_unspecified_parameters_die (function_or_method_type, context_die);
17582 /* Make our second (and final) pass over the list of formal parameter types
17583 and output DIEs to represent those types (as necessary). */
17584 for (link = TYPE_ARG_TYPES (function_or_method_type);
17585 link && TREE_VALUE (link);
17586 link = TREE_CHAIN (link))
17587 gen_type_die (TREE_VALUE (link), context_die);
17590 /* We want to generate the DIE for TYPE so that we can generate the
17591 die for MEMBER, which has been defined; we will need to refer back
17592 to the member declaration nested within TYPE. If we're trying to
17593 generate minimal debug info for TYPE, processing TYPE won't do the
17594 trick; we need to attach the member declaration by hand. */
17596 static void
17597 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
17599 gen_type_die (type, context_die);
17601 /* If we're trying to avoid duplicate debug info, we may not have
17602 emitted the member decl for this function. Emit it now. */
17603 if (TYPE_STUB_DECL (type)
17604 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
17605 && ! lookup_decl_die (member))
17607 dw_die_ref type_die;
17608 gcc_assert (!decl_ultimate_origin (member));
17610 push_decl_scope (type);
17611 type_die = lookup_type_die_strip_naming_typedef (type);
17612 if (TREE_CODE (member) == FUNCTION_DECL)
17613 gen_subprogram_die (member, type_die);
17614 else if (TREE_CODE (member) == FIELD_DECL)
17616 /* Ignore the nameless fields that are used to skip bits but handle
17617 C++ anonymous unions and structs. */
17618 if (DECL_NAME (member) != NULL_TREE
17619 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
17620 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
17622 gen_type_die (member_declared_type (member), type_die);
17623 gen_field_die (member, type_die);
17626 else
17627 gen_variable_die (member, NULL_TREE, type_die);
17629 pop_decl_scope ();
17633 /* Forward declare these functions, because they are mutually recursive
17634 with their set_block_* pairing functions. */
17635 static void set_decl_origin_self (tree);
17636 static void set_decl_abstract_flags (tree, int);
17638 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
17639 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
17640 that it points to the node itself, thus indicating that the node is its
17641 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
17642 the given node is NULL, recursively descend the decl/block tree which
17643 it is the root of, and for each other ..._DECL or BLOCK node contained
17644 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
17645 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
17646 values to point to themselves. */
17648 static void
17649 set_block_origin_self (tree stmt)
17651 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
17653 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
17656 tree local_decl;
17658 for (local_decl = BLOCK_VARS (stmt);
17659 local_decl != NULL_TREE;
17660 local_decl = DECL_CHAIN (local_decl))
17661 if (! DECL_EXTERNAL (local_decl))
17662 set_decl_origin_self (local_decl); /* Potential recursion. */
17666 tree subblock;
17668 for (subblock = BLOCK_SUBBLOCKS (stmt);
17669 subblock != NULL_TREE;
17670 subblock = BLOCK_CHAIN (subblock))
17671 set_block_origin_self (subblock); /* Recurse. */
17676 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
17677 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
17678 node to so that it points to the node itself, thus indicating that the
17679 node represents its own (abstract) origin. Additionally, if the
17680 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
17681 the decl/block tree of which the given node is the root of, and for
17682 each other ..._DECL or BLOCK node contained therein whose
17683 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
17684 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
17685 point to themselves. */
17687 static void
17688 set_decl_origin_self (tree decl)
17690 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
17692 DECL_ABSTRACT_ORIGIN (decl) = decl;
17693 if (TREE_CODE (decl) == FUNCTION_DECL)
17695 tree arg;
17697 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
17698 DECL_ABSTRACT_ORIGIN (arg) = arg;
17699 if (DECL_INITIAL (decl) != NULL_TREE
17700 && DECL_INITIAL (decl) != error_mark_node)
17701 set_block_origin_self (DECL_INITIAL (decl));
17706 /* Given a pointer to some BLOCK node, and a boolean value to set the
17707 "abstract" flags to, set that value into the BLOCK_ABSTRACT flag for
17708 the given block, and for all local decls and all local sub-blocks
17709 (recursively) which are contained therein. */
17711 static void
17712 set_block_abstract_flags (tree stmt, int setting)
17714 tree local_decl;
17715 tree subblock;
17716 unsigned int i;
17718 BLOCK_ABSTRACT (stmt) = setting;
17720 for (local_decl = BLOCK_VARS (stmt);
17721 local_decl != NULL_TREE;
17722 local_decl = DECL_CHAIN (local_decl))
17723 if (! DECL_EXTERNAL (local_decl))
17724 set_decl_abstract_flags (local_decl, setting);
17726 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
17728 local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
17729 if ((TREE_CODE (local_decl) == VAR_DECL && !TREE_STATIC (local_decl))
17730 || TREE_CODE (local_decl) == PARM_DECL)
17731 set_decl_abstract_flags (local_decl, setting);
17734 for (subblock = BLOCK_SUBBLOCKS (stmt);
17735 subblock != NULL_TREE;
17736 subblock = BLOCK_CHAIN (subblock))
17737 set_block_abstract_flags (subblock, setting);
17740 /* Given a pointer to some ..._DECL node, and a boolean value to set the
17741 "abstract" flags to, set that value into the DECL_ABSTRACT flag for the
17742 given decl, and (in the case where the decl is a FUNCTION_DECL) also
17743 set the abstract flags for all of the parameters, local vars, local
17744 blocks and sub-blocks (recursively) to the same setting. */
17746 static void
17747 set_decl_abstract_flags (tree decl, int setting)
17749 DECL_ABSTRACT (decl) = setting;
17750 if (TREE_CODE (decl) == FUNCTION_DECL)
17752 tree arg;
17754 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
17755 DECL_ABSTRACT (arg) = setting;
17756 if (DECL_INITIAL (decl) != NULL_TREE
17757 && DECL_INITIAL (decl) != error_mark_node)
17758 set_block_abstract_flags (DECL_INITIAL (decl), setting);
17762 /* Generate the DWARF2 info for the "abstract" instance of a function which we
17763 may later generate inlined and/or out-of-line instances of. */
17765 static void
17766 dwarf2out_abstract_function (tree decl)
17768 dw_die_ref old_die;
17769 tree save_fn;
17770 tree context;
17771 int was_abstract;
17772 htab_t old_decl_loc_table;
17773 htab_t old_cached_dw_loc_list_table;
17774 int old_call_site_count, old_tail_call_site_count;
17775 struct call_arg_loc_node *old_call_arg_locations;
17777 /* Make sure we have the actual abstract inline, not a clone. */
17778 decl = DECL_ORIGIN (decl);
17780 old_die = lookup_decl_die (decl);
17781 if (old_die && get_AT (old_die, DW_AT_inline))
17782 /* We've already generated the abstract instance. */
17783 return;
17785 /* We can be called while recursively when seeing block defining inlined subroutine
17786 DIE. Be sure to not clobber the outer location table nor use it or we would
17787 get locations in abstract instantces. */
17788 old_decl_loc_table = decl_loc_table;
17789 decl_loc_table = NULL;
17790 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
17791 cached_dw_loc_list_table = NULL;
17792 old_call_arg_locations = call_arg_locations;
17793 call_arg_locations = NULL;
17794 old_call_site_count = call_site_count;
17795 call_site_count = -1;
17796 old_tail_call_site_count = tail_call_site_count;
17797 tail_call_site_count = -1;
17799 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
17800 we don't get confused by DECL_ABSTRACT. */
17801 if (debug_info_level > DINFO_LEVEL_TERSE)
17803 context = decl_class_context (decl);
17804 if (context)
17805 gen_type_die_for_member
17806 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
17809 /* Pretend we've just finished compiling this function. */
17810 save_fn = current_function_decl;
17811 current_function_decl = decl;
17813 was_abstract = DECL_ABSTRACT (decl);
17814 set_decl_abstract_flags (decl, 1);
17815 dwarf2out_decl (decl);
17816 if (! was_abstract)
17817 set_decl_abstract_flags (decl, 0);
17819 current_function_decl = save_fn;
17820 decl_loc_table = old_decl_loc_table;
17821 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
17822 call_arg_locations = old_call_arg_locations;
17823 call_site_count = old_call_site_count;
17824 tail_call_site_count = old_tail_call_site_count;
17827 /* Helper function of premark_used_types() which gets called through
17828 htab_traverse.
17830 Marks the DIE of a given type in *SLOT as perennial, so it never gets
17831 marked as unused by prune_unused_types. */
17833 static int
17834 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
17836 tree type;
17837 dw_die_ref die;
17839 type = (tree) *slot;
17840 die = lookup_type_die (type);
17841 if (die != NULL)
17842 die->die_perennial_p = 1;
17843 return 1;
17846 /* Helper function of premark_types_used_by_global_vars which gets called
17847 through htab_traverse.
17849 Marks the DIE of a given type in *SLOT as perennial, so it never gets
17850 marked as unused by prune_unused_types. The DIE of the type is marked
17851 only if the global variable using the type will actually be emitted. */
17853 static int
17854 premark_types_used_by_global_vars_helper (void **slot,
17855 void *data ATTRIBUTE_UNUSED)
17857 struct types_used_by_vars_entry *entry;
17858 dw_die_ref die;
17860 entry = (struct types_used_by_vars_entry *) *slot;
17861 gcc_assert (entry->type != NULL
17862 && entry->var_decl != NULL);
17863 die = lookup_type_die (entry->type);
17864 if (die)
17866 /* Ask cgraph if the global variable really is to be emitted.
17867 If yes, then we'll keep the DIE of ENTRY->TYPE. */
17868 varpool_node *node = varpool_get_node (entry->var_decl);
17869 if (node && node->definition)
17871 die->die_perennial_p = 1;
17872 /* Keep the parent DIEs as well. */
17873 while ((die = die->die_parent) && die->die_perennial_p == 0)
17874 die->die_perennial_p = 1;
17877 return 1;
17880 /* Mark all members of used_types_hash as perennial. */
17882 static void
17883 premark_used_types (struct function *fun)
17885 if (fun && fun->used_types_hash)
17886 htab_traverse (fun->used_types_hash, premark_used_types_helper, NULL);
17889 /* Mark all members of types_used_by_vars_entry as perennial. */
17891 static void
17892 premark_types_used_by_global_vars (void)
17894 if (types_used_by_vars_hash)
17895 htab_traverse (types_used_by_vars_hash,
17896 premark_types_used_by_global_vars_helper, NULL);
17899 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
17900 for CA_LOC call arg loc node. */
17902 static dw_die_ref
17903 gen_call_site_die (tree decl, dw_die_ref subr_die,
17904 struct call_arg_loc_node *ca_loc)
17906 dw_die_ref stmt_die = NULL, die;
17907 tree block = ca_loc->block;
17909 while (block
17910 && block != DECL_INITIAL (decl)
17911 && TREE_CODE (block) == BLOCK)
17913 if (block_map.length () > BLOCK_NUMBER (block))
17914 stmt_die = block_map[BLOCK_NUMBER (block)];
17915 if (stmt_die)
17916 break;
17917 block = BLOCK_SUPERCONTEXT (block);
17919 if (stmt_die == NULL)
17920 stmt_die = subr_die;
17921 die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
17922 add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
17923 if (ca_loc->tail_call_p)
17924 add_AT_flag (die, DW_AT_GNU_tail_call, 1);
17925 if (ca_loc->symbol_ref)
17927 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
17928 if (tdie)
17929 add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
17930 else
17931 add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref, false);
17933 return die;
17936 /* Generate a DIE to represent a declared function (either file-scope or
17937 block-local). */
17939 static void
17940 gen_subprogram_die (tree decl, dw_die_ref context_die)
17942 tree origin = decl_ultimate_origin (decl);
17943 dw_die_ref subr_die;
17944 tree outer_scope;
17945 dw_die_ref old_die = lookup_decl_die (decl);
17946 int declaration = (current_function_decl != decl
17947 || class_or_namespace_scope_p (context_die));
17949 premark_used_types (DECL_STRUCT_FUNCTION (decl));
17951 /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
17952 started to generate the abstract instance of an inline, decided to output
17953 its containing class, and proceeded to emit the declaration of the inline
17954 from the member list for the class. If so, DECLARATION takes priority;
17955 we'll get back to the abstract instance when done with the class. */
17957 /* The class-scope declaration DIE must be the primary DIE. */
17958 if (origin && declaration && class_or_namespace_scope_p (context_die))
17960 origin = NULL;
17961 gcc_assert (!old_die);
17964 /* Now that the C++ front end lazily declares artificial member fns, we
17965 might need to retrofit the declaration into its class. */
17966 if (!declaration && !origin && !old_die
17967 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
17968 && !class_or_namespace_scope_p (context_die)
17969 && debug_info_level > DINFO_LEVEL_TERSE)
17970 old_die = force_decl_die (decl);
17972 if (origin != NULL)
17974 gcc_assert (!declaration || local_scope_p (context_die));
17976 /* Fixup die_parent for the abstract instance of a nested
17977 inline function. */
17978 if (old_die && old_die->die_parent == NULL)
17979 add_child_die (context_die, old_die);
17981 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17982 add_abstract_origin_attribute (subr_die, origin);
17983 /* This is where the actual code for a cloned function is.
17984 Let's emit linkage name attribute for it. This helps
17985 debuggers to e.g, set breakpoints into
17986 constructors/destructors when the user asks "break
17987 K::K". */
17988 add_linkage_name (subr_die, decl);
17990 else if (old_die)
17992 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
17993 struct dwarf_file_data * file_index = lookup_filename (s.file);
17995 if (!get_AT_flag (old_die, DW_AT_declaration)
17996 /* We can have a normal definition following an inline one in the
17997 case of redefinition of GNU C extern inlines.
17998 It seems reasonable to use AT_specification in this case. */
17999 && !get_AT (old_die, DW_AT_inline))
18001 /* Detect and ignore this case, where we are trying to output
18002 something we have already output. */
18003 return;
18006 /* If the definition comes from the same place as the declaration,
18007 maybe use the old DIE. We always want the DIE for this function
18008 that has the *_pc attributes to be under comp_unit_die so the
18009 debugger can find it. We also need to do this for abstract
18010 instances of inlines, since the spec requires the out-of-line copy
18011 to have the same parent. For local class methods, this doesn't
18012 apply; we just use the old DIE. */
18013 if ((is_cu_die (old_die->die_parent) || context_die == NULL)
18014 && (DECL_ARTIFICIAL (decl)
18015 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
18016 && (get_AT_unsigned (old_die, DW_AT_decl_line)
18017 == (unsigned) s.line))))
18019 subr_die = old_die;
18021 /* Clear out the declaration attribute and the formal parameters.
18022 Do not remove all children, because it is possible that this
18023 declaration die was forced using force_decl_die(). In such
18024 cases die that forced declaration die (e.g. TAG_imported_module)
18025 is one of the children that we do not want to remove. */
18026 remove_AT (subr_die, DW_AT_declaration);
18027 remove_AT (subr_die, DW_AT_object_pointer);
18028 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
18030 else
18032 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18033 add_AT_specification (subr_die, old_die);
18034 add_pubname (decl, subr_die);
18035 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18036 add_AT_file (subr_die, DW_AT_decl_file, file_index);
18037 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18038 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
18040 /* If the prototype had an 'auto' or 'decltype(auto)' return type,
18041 emit the real type on the definition die. */
18042 if (is_cxx() && debug_info_level > DINFO_LEVEL_TERSE)
18044 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
18045 if (die == auto_die || die == decltype_auto_die)
18046 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18047 0, 0, context_die);
18051 else
18053 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18055 if (TREE_PUBLIC (decl))
18056 add_AT_flag (subr_die, DW_AT_external, 1);
18058 add_name_and_src_coords_attributes (subr_die, decl);
18059 add_pubname (decl, subr_die);
18060 if (debug_info_level > DINFO_LEVEL_TERSE)
18062 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
18063 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18064 0, 0, context_die);
18067 add_pure_or_virtual_attribute (subr_die, decl);
18068 if (DECL_ARTIFICIAL (decl))
18069 add_AT_flag (subr_die, DW_AT_artificial, 1);
18071 add_accessibility_attribute (subr_die, decl);
18074 if (declaration)
18076 if (!old_die || !get_AT (old_die, DW_AT_inline))
18078 add_AT_flag (subr_die, DW_AT_declaration, 1);
18080 /* If this is an explicit function declaration then generate
18081 a DW_AT_explicit attribute. */
18082 if (lang_hooks.decls.function_decl_explicit_p (decl)
18083 && (dwarf_version >= 3 || !dwarf_strict))
18084 add_AT_flag (subr_die, DW_AT_explicit, 1);
18086 /* The first time we see a member function, it is in the context of
18087 the class to which it belongs. We make sure of this by emitting
18088 the class first. The next time is the definition, which is
18089 handled above. The two may come from the same source text.
18091 Note that force_decl_die() forces function declaration die. It is
18092 later reused to represent definition. */
18093 equate_decl_number_to_die (decl, subr_die);
18096 else if (DECL_ABSTRACT (decl))
18098 if (DECL_DECLARED_INLINE_P (decl))
18100 if (cgraph_function_possibly_inlined_p (decl))
18101 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
18102 else
18103 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
18105 else
18107 if (cgraph_function_possibly_inlined_p (decl))
18108 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
18109 else
18110 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
18113 if (DECL_DECLARED_INLINE_P (decl)
18114 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
18115 add_AT_flag (subr_die, DW_AT_artificial, 1);
18117 equate_decl_number_to_die (decl, subr_die);
18119 else if (!DECL_EXTERNAL (decl))
18121 HOST_WIDE_INT cfa_fb_offset;
18122 struct function *fun = DECL_STRUCT_FUNCTION (decl);
18124 if (!old_die || !get_AT (old_die, DW_AT_inline))
18125 equate_decl_number_to_die (decl, subr_die);
18127 gcc_checking_assert (fun);
18128 if (!flag_reorder_blocks_and_partition)
18130 dw_fde_ref fde = fun->fde;
18131 if (fde->dw_fde_begin)
18133 /* We have already generated the labels. */
18134 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18135 fde->dw_fde_end, false);
18137 else
18139 /* Create start/end labels and add the range. */
18140 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
18141 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
18142 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
18143 current_function_funcdef_no);
18144 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
18145 current_function_funcdef_no);
18146 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
18147 false);
18150 #if VMS_DEBUGGING_INFO
18151 /* HP OpenVMS Industry Standard 64: DWARF Extensions
18152 Section 2.3 Prologue and Epilogue Attributes:
18153 When a breakpoint is set on entry to a function, it is generally
18154 desirable for execution to be suspended, not on the very first
18155 instruction of the function, but rather at a point after the
18156 function's frame has been set up, after any language defined local
18157 declaration processing has been completed, and before execution of
18158 the first statement of the function begins. Debuggers generally
18159 cannot properly determine where this point is. Similarly for a
18160 breakpoint set on exit from a function. The prologue and epilogue
18161 attributes allow a compiler to communicate the location(s) to use. */
18164 if (fde->dw_fde_vms_end_prologue)
18165 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
18166 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
18168 if (fde->dw_fde_vms_begin_epilogue)
18169 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
18170 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
18172 #endif
18175 else
18177 /* Generate pubnames entries for the split function code ranges. */
18178 dw_fde_ref fde = fun->fde;
18180 if (fde->dw_fde_second_begin)
18182 if (dwarf_version >= 3 || !dwarf_strict)
18184 /* We should use ranges for non-contiguous code section
18185 addresses. Use the actual code range for the initial
18186 section, since the HOT/COLD labels might precede an
18187 alignment offset. */
18188 bool range_list_added = false;
18189 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
18190 fde->dw_fde_end, &range_list_added,
18191 false);
18192 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
18193 fde->dw_fde_second_end,
18194 &range_list_added, false);
18195 if (range_list_added)
18196 add_ranges (NULL);
18198 else
18200 /* There is no real support in DW2 for this .. so we make
18201 a work-around. First, emit the pub name for the segment
18202 containing the function label. Then make and emit a
18203 simplified subprogram DIE for the second segment with the
18204 name pre-fixed by __hot/cold_sect_of_. We use the same
18205 linkage name for the second die so that gdb will find both
18206 sections when given "b foo". */
18207 const char *name = NULL;
18208 tree decl_name = DECL_NAME (decl);
18209 dw_die_ref seg_die;
18211 /* Do the 'primary' section. */
18212 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18213 fde->dw_fde_end, false);
18215 /* Build a minimal DIE for the secondary section. */
18216 seg_die = new_die (DW_TAG_subprogram,
18217 subr_die->die_parent, decl);
18219 if (TREE_PUBLIC (decl))
18220 add_AT_flag (seg_die, DW_AT_external, 1);
18222 if (decl_name != NULL
18223 && IDENTIFIER_POINTER (decl_name) != NULL)
18225 name = dwarf2_name (decl, 1);
18226 if (! DECL_ARTIFICIAL (decl))
18227 add_src_coords_attributes (seg_die, decl);
18229 add_linkage_name (seg_die, decl);
18231 gcc_assert (name != NULL);
18232 add_pure_or_virtual_attribute (seg_die, decl);
18233 if (DECL_ARTIFICIAL (decl))
18234 add_AT_flag (seg_die, DW_AT_artificial, 1);
18236 name = concat ("__second_sect_of_", name, NULL);
18237 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
18238 fde->dw_fde_second_end, false);
18239 add_name_attribute (seg_die, name);
18240 if (want_pubnames ())
18241 add_pubname_string (name, seg_die);
18244 else
18245 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
18246 false);
18249 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
18251 /* We define the "frame base" as the function's CFA. This is more
18252 convenient for several reasons: (1) It's stable across the prologue
18253 and epilogue, which makes it better than just a frame pointer,
18254 (2) With dwarf3, there exists a one-byte encoding that allows us
18255 to reference the .debug_frame data by proxy, but failing that,
18256 (3) We can at least reuse the code inspection and interpretation
18257 code that determines the CFA position at various points in the
18258 function. */
18259 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
18261 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
18262 add_AT_loc (subr_die, DW_AT_frame_base, op);
18264 else
18266 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
18267 if (list->dw_loc_next)
18268 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
18269 else
18270 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
18273 /* Compute a displacement from the "steady-state frame pointer" to
18274 the CFA. The former is what all stack slots and argument slots
18275 will reference in the rtl; the latter is what we've told the
18276 debugger about. We'll need to adjust all frame_base references
18277 by this displacement. */
18278 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
18280 if (fun->static_chain_decl)
18281 add_AT_location_description (subr_die, DW_AT_static_link,
18282 loc_list_from_tree (fun->static_chain_decl, 2));
18285 /* Generate child dies for template paramaters. */
18286 if (debug_info_level > DINFO_LEVEL_TERSE)
18287 gen_generic_params_dies (decl);
18289 /* Now output descriptions of the arguments for this function. This gets
18290 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
18291 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
18292 `...' at the end of the formal parameter list. In order to find out if
18293 there was a trailing ellipsis or not, we must instead look at the type
18294 associated with the FUNCTION_DECL. This will be a node of type
18295 FUNCTION_TYPE. If the chain of type nodes hanging off of this
18296 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
18297 an ellipsis at the end. */
18299 /* In the case where we are describing a mere function declaration, all we
18300 need to do here (and all we *can* do here) is to describe the *types* of
18301 its formal parameters. */
18302 if (debug_info_level <= DINFO_LEVEL_TERSE)
18304 else if (declaration)
18305 gen_formal_types_die (decl, subr_die);
18306 else
18308 /* Generate DIEs to represent all known formal parameters. */
18309 tree parm = DECL_ARGUMENTS (decl);
18310 tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
18311 tree generic_decl_parm = generic_decl
18312 ? DECL_ARGUMENTS (generic_decl)
18313 : NULL;
18315 /* Now we want to walk the list of parameters of the function and
18316 emit their relevant DIEs.
18318 We consider the case of DECL being an instance of a generic function
18319 as well as it being a normal function.
18321 If DECL is an instance of a generic function we walk the
18322 parameters of the generic function declaration _and_ the parameters of
18323 DECL itself. This is useful because we want to emit specific DIEs for
18324 function parameter packs and those are declared as part of the
18325 generic function declaration. In that particular case,
18326 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
18327 That DIE has children DIEs representing the set of arguments
18328 of the pack. Note that the set of pack arguments can be empty.
18329 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
18330 children DIE.
18332 Otherwise, we just consider the parameters of DECL. */
18333 while (generic_decl_parm || parm)
18335 if (generic_decl_parm
18336 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
18337 gen_formal_parameter_pack_die (generic_decl_parm,
18338 parm, subr_die,
18339 &parm);
18340 else if (parm)
18342 dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
18344 if (parm == DECL_ARGUMENTS (decl)
18345 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
18346 && parm_die
18347 && (dwarf_version >= 3 || !dwarf_strict))
18348 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
18350 parm = DECL_CHAIN (parm);
18353 if (generic_decl_parm)
18354 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
18357 /* Decide whether we need an unspecified_parameters DIE at the end.
18358 There are 2 more cases to do this for: 1) the ansi ... declaration -
18359 this is detectable when the end of the arg list is not a
18360 void_type_node 2) an unprototyped function declaration (not a
18361 definition). This just means that we have no info about the
18362 parameters at all. */
18363 if (prototype_p (TREE_TYPE (decl)))
18365 /* This is the prototyped case, check for.... */
18366 if (stdarg_p (TREE_TYPE (decl)))
18367 gen_unspecified_parameters_die (decl, subr_die);
18369 else if (DECL_INITIAL (decl) == NULL_TREE)
18370 gen_unspecified_parameters_die (decl, subr_die);
18373 /* Output Dwarf info for all of the stuff within the body of the function
18374 (if it has one - it may be just a declaration). */
18375 outer_scope = DECL_INITIAL (decl);
18377 /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
18378 a function. This BLOCK actually represents the outermost binding contour
18379 for the function, i.e. the contour in which the function's formal
18380 parameters and labels get declared. Curiously, it appears that the front
18381 end doesn't actually put the PARM_DECL nodes for the current function onto
18382 the BLOCK_VARS list for this outer scope, but are strung off of the
18383 DECL_ARGUMENTS list for the function instead.
18385 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
18386 the LABEL_DECL nodes for the function however, and we output DWARF info
18387 for those in decls_for_scope. Just within the `outer_scope' there will be
18388 a BLOCK node representing the function's outermost pair of curly braces,
18389 and any blocks used for the base and member initializers of a C++
18390 constructor function. */
18391 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
18393 int call_site_note_count = 0;
18394 int tail_call_site_note_count = 0;
18396 /* Emit a DW_TAG_variable DIE for a named return value. */
18397 if (DECL_NAME (DECL_RESULT (decl)))
18398 gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
18400 current_function_has_inlines = 0;
18401 decls_for_scope (outer_scope, subr_die, 0);
18403 if (call_arg_locations && !dwarf_strict)
18405 struct call_arg_loc_node *ca_loc;
18406 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
18408 dw_die_ref die = NULL;
18409 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
18410 rtx arg, next_arg;
18412 for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
18413 arg; arg = next_arg)
18415 dw_loc_descr_ref reg, val;
18416 enum machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
18417 dw_die_ref cdie, tdie = NULL;
18419 next_arg = XEXP (arg, 1);
18420 if (REG_P (XEXP (XEXP (arg, 0), 0))
18421 && next_arg
18422 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
18423 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
18424 && REGNO (XEXP (XEXP (arg, 0), 0))
18425 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
18426 next_arg = XEXP (next_arg, 1);
18427 if (mode == VOIDmode)
18429 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
18430 if (mode == VOIDmode)
18431 mode = GET_MODE (XEXP (arg, 0));
18433 if (mode == VOIDmode || mode == BLKmode)
18434 continue;
18435 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
18437 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18438 tloc = XEXP (XEXP (arg, 0), 1);
18439 continue;
18441 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
18442 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
18444 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18445 tlocc = XEXP (XEXP (arg, 0), 1);
18446 continue;
18448 reg = NULL;
18449 if (REG_P (XEXP (XEXP (arg, 0), 0)))
18450 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
18451 VAR_INIT_STATUS_INITIALIZED);
18452 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
18454 rtx mem = XEXP (XEXP (arg, 0), 0);
18455 reg = mem_loc_descriptor (XEXP (mem, 0),
18456 get_address_mode (mem),
18457 GET_MODE (mem),
18458 VAR_INIT_STATUS_INITIALIZED);
18460 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
18461 == DEBUG_PARAMETER_REF)
18463 tree tdecl
18464 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
18465 tdie = lookup_decl_die (tdecl);
18466 if (tdie == NULL)
18467 continue;
18469 else
18470 continue;
18471 if (reg == NULL
18472 && GET_CODE (XEXP (XEXP (arg, 0), 0))
18473 != DEBUG_PARAMETER_REF)
18474 continue;
18475 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
18476 VOIDmode,
18477 VAR_INIT_STATUS_INITIALIZED);
18478 if (val == NULL)
18479 continue;
18480 if (die == NULL)
18481 die = gen_call_site_die (decl, subr_die, ca_loc);
18482 cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
18483 NULL_TREE);
18484 if (reg != NULL)
18485 add_AT_loc (cdie, DW_AT_location, reg);
18486 else if (tdie != NULL)
18487 add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
18488 add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
18489 if (next_arg != XEXP (arg, 1))
18491 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
18492 if (mode == VOIDmode)
18493 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
18494 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
18495 0), 1),
18496 mode, VOIDmode,
18497 VAR_INIT_STATUS_INITIALIZED);
18498 if (val != NULL)
18499 add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
18502 if (die == NULL
18503 && (ca_loc->symbol_ref || tloc))
18504 die = gen_call_site_die (decl, subr_die, ca_loc);
18505 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
18507 dw_loc_descr_ref tval = NULL;
18509 if (tloc != NULL_RTX)
18510 tval = mem_loc_descriptor (tloc,
18511 GET_MODE (tloc) == VOIDmode
18512 ? Pmode : GET_MODE (tloc),
18513 VOIDmode,
18514 VAR_INIT_STATUS_INITIALIZED);
18515 if (tval)
18516 add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
18517 else if (tlocc != NULL_RTX)
18519 tval = mem_loc_descriptor (tlocc,
18520 GET_MODE (tlocc) == VOIDmode
18521 ? Pmode : GET_MODE (tlocc),
18522 VOIDmode,
18523 VAR_INIT_STATUS_INITIALIZED);
18524 if (tval)
18525 add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
18526 tval);
18529 if (die != NULL)
18531 call_site_note_count++;
18532 if (ca_loc->tail_call_p)
18533 tail_call_site_note_count++;
18537 call_arg_locations = NULL;
18538 call_arg_loc_last = NULL;
18539 if (tail_call_site_count >= 0
18540 && tail_call_site_count == tail_call_site_note_count
18541 && !dwarf_strict)
18543 if (call_site_count >= 0
18544 && call_site_count == call_site_note_count)
18545 add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
18546 else
18547 add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
18549 call_site_count = -1;
18550 tail_call_site_count = -1;
18553 if (subr_die != old_die)
18554 /* Add the calling convention attribute if requested. */
18555 add_calling_convention_attribute (subr_die, decl);
18558 /* Returns a hash value for X (which really is a die_struct). */
18560 static hashval_t
18561 common_block_die_table_hash (const void *x)
18563 const_dw_die_ref d = (const_dw_die_ref) x;
18564 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
18567 /* Return nonzero if decl_id and die_parent of die_struct X is the same
18568 as decl_id and die_parent of die_struct Y. */
18570 static int
18571 common_block_die_table_eq (const void *x, const void *y)
18573 const_dw_die_ref d = (const_dw_die_ref) x;
18574 const_dw_die_ref e = (const_dw_die_ref) y;
18575 return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
18578 /* Generate a DIE to represent a declared data object.
18579 Either DECL or ORIGIN must be non-null. */
18581 static void
18582 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
18584 HOST_WIDE_INT off = 0;
18585 tree com_decl;
18586 tree decl_or_origin = decl ? decl : origin;
18587 tree ultimate_origin;
18588 dw_die_ref var_die;
18589 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
18590 dw_die_ref origin_die;
18591 bool declaration = (DECL_EXTERNAL (decl_or_origin)
18592 || class_or_namespace_scope_p (context_die));
18593 bool specialization_p = false;
18595 ultimate_origin = decl_ultimate_origin (decl_or_origin);
18596 if (decl || ultimate_origin)
18597 origin = ultimate_origin;
18598 com_decl = fortran_common (decl_or_origin, &off);
18600 /* Symbol in common gets emitted as a child of the common block, in the form
18601 of a data member. */
18602 if (com_decl)
18604 dw_die_ref com_die;
18605 dw_loc_list_ref loc;
18606 die_node com_die_arg;
18608 var_die = lookup_decl_die (decl_or_origin);
18609 if (var_die)
18611 if (get_AT (var_die, DW_AT_location) == NULL)
18613 loc = loc_list_from_tree (com_decl, off ? 1 : 2);
18614 if (loc)
18616 if (off)
18618 /* Optimize the common case. */
18619 if (single_element_loc_list_p (loc)
18620 && loc->expr->dw_loc_opc == DW_OP_addr
18621 && loc->expr->dw_loc_next == NULL
18622 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
18623 == SYMBOL_REF)
18625 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
18626 loc->expr->dw_loc_oprnd1.v.val_addr
18627 = plus_constant (GET_MODE (x), x , off);
18629 else
18630 loc_list_plus_const (loc, off);
18632 add_AT_location_description (var_die, DW_AT_location, loc);
18633 remove_AT (var_die, DW_AT_declaration);
18636 return;
18639 if (common_block_die_table == NULL)
18640 common_block_die_table
18641 = htab_create_ggc (10, common_block_die_table_hash,
18642 common_block_die_table_eq, NULL);
18644 com_die_arg.decl_id = DECL_UID (com_decl);
18645 com_die_arg.die_parent = context_die;
18646 com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
18647 loc = loc_list_from_tree (com_decl, 2);
18648 if (com_die == NULL)
18650 const char *cnam
18651 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
18652 void **slot;
18654 com_die = new_die (DW_TAG_common_block, context_die, decl);
18655 add_name_and_src_coords_attributes (com_die, com_decl);
18656 if (loc)
18658 add_AT_location_description (com_die, DW_AT_location, loc);
18659 /* Avoid sharing the same loc descriptor between
18660 DW_TAG_common_block and DW_TAG_variable. */
18661 loc = loc_list_from_tree (com_decl, 2);
18663 else if (DECL_EXTERNAL (decl))
18664 add_AT_flag (com_die, DW_AT_declaration, 1);
18665 if (want_pubnames ())
18666 add_pubname_string (cnam, com_die); /* ??? needed? */
18667 com_die->decl_id = DECL_UID (com_decl);
18668 slot = htab_find_slot (common_block_die_table, com_die, INSERT);
18669 *slot = (void *) com_die;
18671 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
18673 add_AT_location_description (com_die, DW_AT_location, loc);
18674 loc = loc_list_from_tree (com_decl, 2);
18675 remove_AT (com_die, DW_AT_declaration);
18677 var_die = new_die (DW_TAG_variable, com_die, decl);
18678 add_name_and_src_coords_attributes (var_die, decl);
18679 add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
18680 TREE_THIS_VOLATILE (decl), context_die);
18681 add_AT_flag (var_die, DW_AT_external, 1);
18682 if (loc)
18684 if (off)
18686 /* Optimize the common case. */
18687 if (single_element_loc_list_p (loc)
18688 && loc->expr->dw_loc_opc == DW_OP_addr
18689 && loc->expr->dw_loc_next == NULL
18690 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
18692 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
18693 loc->expr->dw_loc_oprnd1.v.val_addr
18694 = plus_constant (GET_MODE (x), x, off);
18696 else
18697 loc_list_plus_const (loc, off);
18699 add_AT_location_description (var_die, DW_AT_location, loc);
18701 else if (DECL_EXTERNAL (decl))
18702 add_AT_flag (var_die, DW_AT_declaration, 1);
18703 equate_decl_number_to_die (decl, var_die);
18704 return;
18707 /* If the compiler emitted a definition for the DECL declaration
18708 and if we already emitted a DIE for it, don't emit a second
18709 DIE for it again. Allow re-declarations of DECLs that are
18710 inside functions, though. */
18711 if (old_die && declaration && !local_scope_p (context_die))
18712 return;
18714 /* For static data members, the declaration in the class is supposed
18715 to have DW_TAG_member tag; the specification should still be
18716 DW_TAG_variable referencing the DW_TAG_member DIE. */
18717 if (declaration && class_scope_p (context_die))
18718 var_die = new_die (DW_TAG_member, context_die, decl);
18719 else
18720 var_die = new_die (DW_TAG_variable, context_die, decl);
18722 origin_die = NULL;
18723 if (origin != NULL)
18724 origin_die = add_abstract_origin_attribute (var_die, origin);
18726 /* Loop unrolling can create multiple blocks that refer to the same
18727 static variable, so we must test for the DW_AT_declaration flag.
18729 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
18730 copy decls and set the DECL_ABSTRACT flag on them instead of
18731 sharing them.
18733 ??? Duplicated blocks have been rewritten to use .debug_ranges.
18735 ??? The declare_in_namespace support causes us to get two DIEs for one
18736 variable, both of which are declarations. We want to avoid considering
18737 one to be a specification, so we must test that this DIE is not a
18738 declaration. */
18739 else if (old_die && TREE_STATIC (decl) && ! declaration
18740 && get_AT_flag (old_die, DW_AT_declaration) == 1)
18742 /* This is a definition of a C++ class level static. */
18743 add_AT_specification (var_die, old_die);
18744 specialization_p = true;
18745 if (DECL_NAME (decl))
18747 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18748 struct dwarf_file_data * file_index = lookup_filename (s.file);
18750 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18751 add_AT_file (var_die, DW_AT_decl_file, file_index);
18753 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18754 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
18756 if (old_die->die_tag == DW_TAG_member)
18757 add_linkage_name (var_die, decl);
18760 else
18761 add_name_and_src_coords_attributes (var_die, decl);
18763 if ((origin == NULL && !specialization_p)
18764 || (origin != NULL
18765 && !DECL_ABSTRACT (decl_or_origin)
18766 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
18767 decl_function_context
18768 (decl_or_origin))))
18770 tree type = TREE_TYPE (decl_or_origin);
18772 if (decl_by_reference_p (decl_or_origin))
18773 add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
18774 else
18775 add_type_attribute (var_die, type, TREE_READONLY (decl_or_origin),
18776 TREE_THIS_VOLATILE (decl_or_origin), context_die);
18779 if (origin == NULL && !specialization_p)
18781 if (TREE_PUBLIC (decl))
18782 add_AT_flag (var_die, DW_AT_external, 1);
18784 if (DECL_ARTIFICIAL (decl))
18785 add_AT_flag (var_die, DW_AT_artificial, 1);
18787 add_accessibility_attribute (var_die, decl);
18790 if (declaration)
18791 add_AT_flag (var_die, DW_AT_declaration, 1);
18793 if (decl && (DECL_ABSTRACT (decl) || declaration || old_die == NULL))
18794 equate_decl_number_to_die (decl, var_die);
18796 if (! declaration
18797 && (! DECL_ABSTRACT (decl_or_origin)
18798 /* Local static vars are shared between all clones/inlines,
18799 so emit DW_AT_location on the abstract DIE if DECL_RTL is
18800 already set. */
18801 || (TREE_CODE (decl_or_origin) == VAR_DECL
18802 && TREE_STATIC (decl_or_origin)
18803 && DECL_RTL_SET_P (decl_or_origin)))
18804 /* When abstract origin already has DW_AT_location attribute, no need
18805 to add it again. */
18806 && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
18808 if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
18809 && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
18810 defer_location (decl_or_origin, var_die);
18811 else
18812 add_location_or_const_value_attribute (var_die, decl_or_origin,
18813 decl == NULL, DW_AT_location);
18814 add_pubname (decl_or_origin, var_die);
18816 else
18817 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
18820 /* Generate a DIE to represent a named constant. */
18822 static void
18823 gen_const_die (tree decl, dw_die_ref context_die)
18825 dw_die_ref const_die;
18826 tree type = TREE_TYPE (decl);
18828 const_die = new_die (DW_TAG_constant, context_die, decl);
18829 add_name_and_src_coords_attributes (const_die, decl);
18830 add_type_attribute (const_die, type, 1, 0, context_die);
18831 if (TREE_PUBLIC (decl))
18832 add_AT_flag (const_die, DW_AT_external, 1);
18833 if (DECL_ARTIFICIAL (decl))
18834 add_AT_flag (const_die, DW_AT_artificial, 1);
18835 tree_add_const_value_attribute_for_decl (const_die, decl);
18838 /* Generate a DIE to represent a label identifier. */
18840 static void
18841 gen_label_die (tree decl, dw_die_ref context_die)
18843 tree origin = decl_ultimate_origin (decl);
18844 dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
18845 rtx insn;
18846 char label[MAX_ARTIFICIAL_LABEL_BYTES];
18848 if (origin != NULL)
18849 add_abstract_origin_attribute (lbl_die, origin);
18850 else
18851 add_name_and_src_coords_attributes (lbl_die, decl);
18853 if (DECL_ABSTRACT (decl))
18854 equate_decl_number_to_die (decl, lbl_die);
18855 else
18857 insn = DECL_RTL_IF_SET (decl);
18859 /* Deleted labels are programmer specified labels which have been
18860 eliminated because of various optimizations. We still emit them
18861 here so that it is possible to put breakpoints on them. */
18862 if (insn
18863 && (LABEL_P (insn)
18864 || ((NOTE_P (insn)
18865 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
18867 /* When optimization is enabled (via -O) some parts of the compiler
18868 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
18869 represent source-level labels which were explicitly declared by
18870 the user. This really shouldn't be happening though, so catch
18871 it if it ever does happen. */
18872 gcc_assert (!INSN_DELETED_P (insn));
18874 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
18875 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
18877 else if (insn
18878 && NOTE_P (insn)
18879 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
18880 && CODE_LABEL_NUMBER (insn) != -1)
18882 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
18883 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
18888 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
18889 attributes to the DIE for a block STMT, to describe where the inlined
18890 function was called from. This is similar to add_src_coords_attributes. */
18892 static inline void
18893 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
18895 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
18897 if (dwarf_version >= 3 || !dwarf_strict)
18899 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
18900 add_AT_unsigned (die, DW_AT_call_line, s.line);
18905 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
18906 Add low_pc and high_pc attributes to the DIE for a block STMT. */
18908 static inline void
18909 add_high_low_attributes (tree stmt, dw_die_ref die)
18911 char label[MAX_ARTIFICIAL_LABEL_BYTES];
18913 if (BLOCK_FRAGMENT_CHAIN (stmt)
18914 && (dwarf_version >= 3 || !dwarf_strict))
18916 tree chain, superblock = NULL_TREE;
18917 dw_die_ref pdie;
18918 dw_attr_ref attr = NULL;
18920 if (inlined_function_outer_scope_p (stmt))
18922 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
18923 BLOCK_NUMBER (stmt));
18924 add_AT_lbl_id (die, DW_AT_entry_pc, label);
18927 /* Optimize duplicate .debug_ranges lists or even tails of
18928 lists. If this BLOCK has same ranges as its supercontext,
18929 lookup DW_AT_ranges attribute in the supercontext (and
18930 recursively so), verify that the ranges_table contains the
18931 right values and use it instead of adding a new .debug_range. */
18932 for (chain = stmt, pdie = die;
18933 BLOCK_SAME_RANGE (chain);
18934 chain = BLOCK_SUPERCONTEXT (chain))
18936 dw_attr_ref new_attr;
18938 pdie = pdie->die_parent;
18939 if (pdie == NULL)
18940 break;
18941 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
18942 break;
18943 new_attr = get_AT (pdie, DW_AT_ranges);
18944 if (new_attr == NULL
18945 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
18946 break;
18947 attr = new_attr;
18948 superblock = BLOCK_SUPERCONTEXT (chain);
18950 if (attr != NULL
18951 && (ranges_table[attr->dw_attr_val.v.val_offset
18952 / 2 / DWARF2_ADDR_SIZE].num
18953 == BLOCK_NUMBER (superblock))
18954 && BLOCK_FRAGMENT_CHAIN (superblock))
18956 unsigned long off = attr->dw_attr_val.v.val_offset
18957 / 2 / DWARF2_ADDR_SIZE;
18958 unsigned long supercnt = 0, thiscnt = 0;
18959 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
18960 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
18962 ++supercnt;
18963 gcc_checking_assert (ranges_table[off + supercnt].num
18964 == BLOCK_NUMBER (chain));
18966 gcc_checking_assert (ranges_table[off + supercnt + 1].num == 0);
18967 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
18968 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
18969 ++thiscnt;
18970 gcc_assert (supercnt >= thiscnt);
18971 add_AT_range_list (die, DW_AT_ranges,
18972 ((off + supercnt - thiscnt)
18973 * 2 * DWARF2_ADDR_SIZE),
18974 false);
18975 return;
18978 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt), false);
18980 chain = BLOCK_FRAGMENT_CHAIN (stmt);
18983 add_ranges (chain);
18984 chain = BLOCK_FRAGMENT_CHAIN (chain);
18986 while (chain);
18987 add_ranges (NULL);
18989 else
18991 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
18992 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
18993 BLOCK_NUMBER (stmt));
18994 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
18995 BLOCK_NUMBER (stmt));
18996 add_AT_low_high_pc (die, label, label_high, false);
19000 /* Generate a DIE for a lexical block. */
19002 static void
19003 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
19005 dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
19007 if (call_arg_locations)
19009 if (block_map.length () <= BLOCK_NUMBER (stmt))
19010 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
19011 block_map[BLOCK_NUMBER (stmt)] = stmt_die;
19014 if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
19015 add_high_low_attributes (stmt, stmt_die);
19017 decls_for_scope (stmt, stmt_die, depth);
19020 /* Generate a DIE for an inlined subprogram. */
19022 static void
19023 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
19025 tree decl;
19027 /* The instance of function that is effectively being inlined shall not
19028 be abstract. */
19029 gcc_assert (! BLOCK_ABSTRACT (stmt));
19031 decl = block_ultimate_origin (stmt);
19033 /* Emit info for the abstract instance first, if we haven't yet. We
19034 must emit this even if the block is abstract, otherwise when we
19035 emit the block below (or elsewhere), we may end up trying to emit
19036 a die whose origin die hasn't been emitted, and crashing. */
19037 dwarf2out_abstract_function (decl);
19039 if (! BLOCK_ABSTRACT (stmt))
19041 dw_die_ref subr_die
19042 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
19044 if (call_arg_locations)
19046 if (block_map.length () <= BLOCK_NUMBER (stmt))
19047 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
19048 block_map[BLOCK_NUMBER (stmt)] = subr_die;
19050 add_abstract_origin_attribute (subr_die, decl);
19051 if (TREE_ASM_WRITTEN (stmt))
19052 add_high_low_attributes (stmt, subr_die);
19053 add_call_src_coords_attributes (stmt, subr_die);
19055 decls_for_scope (stmt, subr_die, depth);
19056 current_function_has_inlines = 1;
19060 /* Generate a DIE for a field in a record, or structure. */
19062 static void
19063 gen_field_die (tree decl, dw_die_ref context_die)
19065 dw_die_ref decl_die;
19067 if (TREE_TYPE (decl) == error_mark_node)
19068 return;
19070 decl_die = new_die (DW_TAG_member, context_die, decl);
19071 add_name_and_src_coords_attributes (decl_die, decl);
19072 add_type_attribute (decl_die, member_declared_type (decl),
19073 TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
19074 context_die);
19076 if (DECL_BIT_FIELD_TYPE (decl))
19078 add_byte_size_attribute (decl_die, decl);
19079 add_bit_size_attribute (decl_die, decl);
19080 add_bit_offset_attribute (decl_die, decl);
19083 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
19084 add_data_member_location_attribute (decl_die, decl);
19086 if (DECL_ARTIFICIAL (decl))
19087 add_AT_flag (decl_die, DW_AT_artificial, 1);
19089 add_accessibility_attribute (decl_die, decl);
19091 /* Equate decl number to die, so that we can look up this decl later on. */
19092 equate_decl_number_to_die (decl, decl_die);
19095 #if 0
19096 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19097 Use modified_type_die instead.
19098 We keep this code here just in case these types of DIEs may be needed to
19099 represent certain things in other languages (e.g. Pascal) someday. */
19101 static void
19102 gen_pointer_type_die (tree type, dw_die_ref context_die)
19104 dw_die_ref ptr_die
19105 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
19107 equate_type_number_to_die (type, ptr_die);
19108 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
19109 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19112 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19113 Use modified_type_die instead.
19114 We keep this code here just in case these types of DIEs may be needed to
19115 represent certain things in other languages (e.g. Pascal) someday. */
19117 static void
19118 gen_reference_type_die (tree type, dw_die_ref context_die)
19120 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
19122 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
19123 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
19124 else
19125 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
19127 equate_type_number_to_die (type, ref_die);
19128 add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
19129 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19131 #endif
19133 /* Generate a DIE for a pointer to a member type. */
19135 static void
19136 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
19138 dw_die_ref ptr_die
19139 = new_die (DW_TAG_ptr_to_member_type,
19140 scope_die_for (type, context_die), type);
19142 equate_type_number_to_die (type, ptr_die);
19143 add_AT_die_ref (ptr_die, DW_AT_containing_type,
19144 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
19145 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
19148 typedef const char *dchar_p; /* For DEF_VEC_P. */
19150 static char *producer_string;
19152 /* Return a heap allocated producer string including command line options
19153 if -grecord-gcc-switches. */
19155 static char *
19156 gen_producer_string (void)
19158 size_t j;
19159 auto_vec<dchar_p> switches;
19160 const char *language_string = lang_hooks.name;
19161 char *producer, *tail;
19162 const char *p;
19163 size_t len = dwarf_record_gcc_switches ? 0 : 3;
19164 size_t plen = strlen (language_string) + 1 + strlen (version_string);
19166 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
19167 switch (save_decoded_options[j].opt_index)
19169 case OPT_o:
19170 case OPT_d:
19171 case OPT_dumpbase:
19172 case OPT_dumpdir:
19173 case OPT_auxbase:
19174 case OPT_auxbase_strip:
19175 case OPT_quiet:
19176 case OPT_version:
19177 case OPT_v:
19178 case OPT_w:
19179 case OPT_L:
19180 case OPT_D:
19181 case OPT_I:
19182 case OPT_U:
19183 case OPT_SPECIAL_unknown:
19184 case OPT_SPECIAL_ignore:
19185 case OPT_SPECIAL_program_name:
19186 case OPT_SPECIAL_input_file:
19187 case OPT_grecord_gcc_switches:
19188 case OPT_gno_record_gcc_switches:
19189 case OPT__output_pch_:
19190 case OPT_fdiagnostics_show_location_:
19191 case OPT_fdiagnostics_show_option:
19192 case OPT_fdiagnostics_show_caret:
19193 case OPT_fdiagnostics_color_:
19194 case OPT_fverbose_asm:
19195 case OPT____:
19196 case OPT__sysroot_:
19197 case OPT_nostdinc:
19198 case OPT_nostdinc__:
19199 case OPT_fpreprocessed:
19200 case OPT_fltrans_output_list_:
19201 case OPT_fresolution_:
19202 /* Ignore these. */
19203 continue;
19204 default:
19205 if (cl_options[save_decoded_options[j].opt_index].flags
19206 & CL_NO_DWARF_RECORD)
19207 continue;
19208 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
19209 == '-');
19210 switch (save_decoded_options[j].canonical_option[0][1])
19212 case 'M':
19213 case 'i':
19214 case 'W':
19215 continue;
19216 case 'f':
19217 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
19218 "dump", 4) == 0)
19219 continue;
19220 break;
19221 default:
19222 break;
19224 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
19225 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
19226 break;
19229 producer = XNEWVEC (char, plen + 1 + len + 1);
19230 tail = producer;
19231 sprintf (tail, "%s %s", language_string, version_string);
19232 tail += plen;
19234 FOR_EACH_VEC_ELT (switches, j, p)
19236 len = strlen (p);
19237 *tail = ' ';
19238 memcpy (tail + 1, p, len);
19239 tail += len + 1;
19242 *tail = '\0';
19243 return producer;
19246 /* Generate the DIE for the compilation unit. */
19248 static dw_die_ref
19249 gen_compile_unit_die (const char *filename)
19251 dw_die_ref die;
19252 const char *language_string = lang_hooks.name;
19253 int language;
19255 die = new_die (DW_TAG_compile_unit, NULL, NULL);
19257 if (filename)
19259 add_name_attribute (die, filename);
19260 /* Don't add cwd for <built-in>. */
19261 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
19262 add_comp_dir_attribute (die);
19265 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
19267 /* If our producer is LTO try to figure out a common language to use
19268 from the global list of translation units. */
19269 if (strcmp (language_string, "GNU GIMPLE") == 0)
19271 unsigned i;
19272 tree t;
19273 const char *common_lang = NULL;
19275 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
19277 if (!TRANSLATION_UNIT_LANGUAGE (t))
19278 continue;
19279 if (!common_lang)
19280 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
19281 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
19283 else if (strncmp (common_lang, "GNU C", 5) == 0
19284 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
19285 /* Mixing C and C++ is ok, use C++ in that case. */
19286 common_lang = "GNU C++";
19287 else
19289 /* Fall back to C. */
19290 common_lang = NULL;
19291 break;
19295 if (common_lang)
19296 language_string = common_lang;
19299 language = DW_LANG_C89;
19300 if (strcmp (language_string, "GNU C++") == 0)
19301 language = DW_LANG_C_plus_plus;
19302 else if (strcmp (language_string, "GNU F77") == 0)
19303 language = DW_LANG_Fortran77;
19304 else if (strcmp (language_string, "GNU Pascal") == 0)
19305 language = DW_LANG_Pascal83;
19306 else if (dwarf_version >= 3 || !dwarf_strict)
19308 if (strcmp (language_string, "GNU Ada") == 0)
19309 language = DW_LANG_Ada95;
19310 else if (strcmp (language_string, "GNU Fortran") == 0)
19311 language = DW_LANG_Fortran95;
19312 else if (strcmp (language_string, "GNU Java") == 0)
19313 language = DW_LANG_Java;
19314 else if (strcmp (language_string, "GNU Objective-C") == 0)
19315 language = DW_LANG_ObjC;
19316 else if (strcmp (language_string, "GNU Objective-C++") == 0)
19317 language = DW_LANG_ObjC_plus_plus;
19318 else if (dwarf_version >= 5 || !dwarf_strict)
19320 if (strcmp (language_string, "GNU Go") == 0)
19321 language = DW_LANG_Go;
19324 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
19325 else if (strcmp (language_string, "GNU Fortran") == 0)
19326 language = DW_LANG_Fortran90;
19328 add_AT_unsigned (die, DW_AT_language, language);
19330 switch (language)
19332 case DW_LANG_Fortran77:
19333 case DW_LANG_Fortran90:
19334 case DW_LANG_Fortran95:
19335 /* Fortran has case insensitive identifiers and the front-end
19336 lowercases everything. */
19337 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
19338 break;
19339 default:
19340 /* The default DW_ID_case_sensitive doesn't need to be specified. */
19341 break;
19343 return die;
19346 /* Generate the DIE for a base class. */
19348 static void
19349 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
19351 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
19353 add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
19354 add_data_member_location_attribute (die, binfo);
19356 if (BINFO_VIRTUAL_P (binfo))
19357 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
19359 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
19360 children, otherwise the default is DW_ACCESS_public. In DWARF2
19361 the default has always been DW_ACCESS_private. */
19362 if (access == access_public_node)
19364 if (dwarf_version == 2
19365 || context_die->die_tag == DW_TAG_class_type)
19366 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
19368 else if (access == access_protected_node)
19369 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
19370 else if (dwarf_version > 2
19371 && context_die->die_tag != DW_TAG_class_type)
19372 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
19375 /* Generate a DIE for a class member. */
19377 static void
19378 gen_member_die (tree type, dw_die_ref context_die)
19380 tree member;
19381 tree binfo = TYPE_BINFO (type);
19382 dw_die_ref child;
19384 /* If this is not an incomplete type, output descriptions of each of its
19385 members. Note that as we output the DIEs necessary to represent the
19386 members of this record or union type, we will also be trying to output
19387 DIEs to represent the *types* of those members. However the `type'
19388 function (above) will specifically avoid generating type DIEs for member
19389 types *within* the list of member DIEs for this (containing) type except
19390 for those types (of members) which are explicitly marked as also being
19391 members of this (containing) type themselves. The g++ front- end can
19392 force any given type to be treated as a member of some other (containing)
19393 type by setting the TYPE_CONTEXT of the given (member) type to point to
19394 the TREE node representing the appropriate (containing) type. */
19396 /* First output info about the base classes. */
19397 if (binfo)
19399 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
19400 int i;
19401 tree base;
19403 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
19404 gen_inheritance_die (base,
19405 (accesses ? (*accesses)[i] : access_public_node),
19406 context_die);
19409 /* Now output info about the data members and type members. */
19410 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
19412 /* If we thought we were generating minimal debug info for TYPE
19413 and then changed our minds, some of the member declarations
19414 may have already been defined. Don't define them again, but
19415 do put them in the right order. */
19417 child = lookup_decl_die (member);
19418 if (child)
19419 splice_child_die (context_die, child);
19420 else
19421 gen_decl_die (member, NULL, context_die);
19424 /* Now output info about the function members (if any). */
19425 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
19427 /* Don't include clones in the member list. */
19428 if (DECL_ABSTRACT_ORIGIN (member))
19429 continue;
19431 child = lookup_decl_die (member);
19432 if (child)
19433 splice_child_die (context_die, child);
19434 else
19435 gen_decl_die (member, NULL, context_die);
19439 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
19440 is set, we pretend that the type was never defined, so we only get the
19441 member DIEs needed by later specification DIEs. */
19443 static void
19444 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
19445 enum debug_info_usage usage)
19447 dw_die_ref type_die = lookup_type_die (type);
19448 dw_die_ref scope_die = 0;
19449 int nested = 0;
19450 int complete = (TYPE_SIZE (type)
19451 && (! TYPE_STUB_DECL (type)
19452 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
19453 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
19454 complete = complete && should_emit_struct_debug (type, usage);
19456 if (type_die && ! complete)
19457 return;
19459 if (TYPE_CONTEXT (type) != NULL_TREE
19460 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19461 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
19462 nested = 1;
19464 scope_die = scope_die_for (type, context_die);
19466 /* Generate child dies for template paramaters. */
19467 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
19468 schedule_generic_params_dies_gen (type);
19470 if (! type_die || (nested && is_cu_die (scope_die)))
19471 /* First occurrence of type or toplevel definition of nested class. */
19473 dw_die_ref old_die = type_die;
19475 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
19476 ? record_type_tag (type) : DW_TAG_union_type,
19477 scope_die, type);
19478 equate_type_number_to_die (type, type_die);
19479 if (old_die)
19480 add_AT_specification (type_die, old_die);
19481 else
19482 add_name_attribute (type_die, type_tag (type));
19484 else
19485 remove_AT (type_die, DW_AT_declaration);
19487 /* If this type has been completed, then give it a byte_size attribute and
19488 then give a list of members. */
19489 if (complete && !ns_decl)
19491 /* Prevent infinite recursion in cases where the type of some member of
19492 this type is expressed in terms of this type itself. */
19493 TREE_ASM_WRITTEN (type) = 1;
19494 add_byte_size_attribute (type_die, type);
19495 if (TYPE_STUB_DECL (type) != NULL_TREE)
19497 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
19498 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
19501 /* If the first reference to this type was as the return type of an
19502 inline function, then it may not have a parent. Fix this now. */
19503 if (type_die->die_parent == NULL)
19504 add_child_die (scope_die, type_die);
19506 push_decl_scope (type);
19507 gen_member_die (type, type_die);
19508 pop_decl_scope ();
19510 add_gnat_descriptive_type_attribute (type_die, type, context_die);
19511 if (TYPE_ARTIFICIAL (type))
19512 add_AT_flag (type_die, DW_AT_artificial, 1);
19514 /* GNU extension: Record what type our vtable lives in. */
19515 if (TYPE_VFIELD (type))
19517 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
19519 gen_type_die (vtype, context_die);
19520 add_AT_die_ref (type_die, DW_AT_containing_type,
19521 lookup_type_die (vtype));
19524 else
19526 add_AT_flag (type_die, DW_AT_declaration, 1);
19528 /* We don't need to do this for function-local types. */
19529 if (TYPE_STUB_DECL (type)
19530 && ! decl_function_context (TYPE_STUB_DECL (type)))
19531 vec_safe_push (incomplete_types, type);
19534 if (get_AT (type_die, DW_AT_name))
19535 add_pubtype (type, type_die);
19538 /* Generate a DIE for a subroutine _type_. */
19540 static void
19541 gen_subroutine_type_die (tree type, dw_die_ref context_die)
19543 tree return_type = TREE_TYPE (type);
19544 dw_die_ref subr_die
19545 = new_die (DW_TAG_subroutine_type,
19546 scope_die_for (type, context_die), type);
19548 equate_type_number_to_die (type, subr_die);
19549 add_prototyped_attribute (subr_die, type);
19550 add_type_attribute (subr_die, return_type, 0, 0, context_die);
19551 gen_formal_types_die (type, subr_die);
19553 if (get_AT (subr_die, DW_AT_name))
19554 add_pubtype (type, subr_die);
19557 /* Generate a DIE for a type definition. */
19559 static void
19560 gen_typedef_die (tree decl, dw_die_ref context_die)
19562 dw_die_ref type_die;
19563 tree origin;
19565 if (TREE_ASM_WRITTEN (decl))
19566 return;
19568 TREE_ASM_WRITTEN (decl) = 1;
19569 type_die = new_die (DW_TAG_typedef, context_die, decl);
19570 origin = decl_ultimate_origin (decl);
19571 if (origin != NULL)
19572 add_abstract_origin_attribute (type_die, origin);
19573 else
19575 tree type;
19577 add_name_and_src_coords_attributes (type_die, decl);
19578 if (DECL_ORIGINAL_TYPE (decl))
19580 type = DECL_ORIGINAL_TYPE (decl);
19582 gcc_assert (type != TREE_TYPE (decl));
19583 equate_type_number_to_die (TREE_TYPE (decl), type_die);
19585 else
19587 type = TREE_TYPE (decl);
19589 if (is_naming_typedef_decl (TYPE_NAME (type)))
19591 /* Here, we are in the case of decl being a typedef naming
19592 an anonymous type, e.g:
19593 typedef struct {...} foo;
19594 In that case TREE_TYPE (decl) is not a typedef variant
19595 type and TYPE_NAME of the anonymous type is set to the
19596 TYPE_DECL of the typedef. This construct is emitted by
19597 the C++ FE.
19599 TYPE is the anonymous struct named by the typedef
19600 DECL. As we need the DW_AT_type attribute of the
19601 DW_TAG_typedef to point to the DIE of TYPE, let's
19602 generate that DIE right away. add_type_attribute
19603 called below will then pick (via lookup_type_die) that
19604 anonymous struct DIE. */
19605 if (!TREE_ASM_WRITTEN (type))
19606 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
19608 /* This is a GNU Extension. We are adding a
19609 DW_AT_linkage_name attribute to the DIE of the
19610 anonymous struct TYPE. The value of that attribute
19611 is the name of the typedef decl naming the anonymous
19612 struct. This greatly eases the work of consumers of
19613 this debug info. */
19614 add_linkage_attr (lookup_type_die (type), decl);
19618 add_type_attribute (type_die, type, TREE_READONLY (decl),
19619 TREE_THIS_VOLATILE (decl), context_die);
19621 if (is_naming_typedef_decl (decl))
19622 /* We want that all subsequent calls to lookup_type_die with
19623 TYPE in argument yield the DW_TAG_typedef we have just
19624 created. */
19625 equate_type_number_to_die (type, type_die);
19627 add_accessibility_attribute (type_die, decl);
19630 if (DECL_ABSTRACT (decl))
19631 equate_decl_number_to_die (decl, type_die);
19633 if (get_AT (type_die, DW_AT_name))
19634 add_pubtype (decl, type_die);
19637 /* Generate a DIE for a struct, class, enum or union type. */
19639 static void
19640 gen_tagged_type_die (tree type,
19641 dw_die_ref context_die,
19642 enum debug_info_usage usage)
19644 int need_pop;
19646 if (type == NULL_TREE
19647 || !is_tagged_type (type))
19648 return;
19650 /* If this is a nested type whose containing class hasn't been written
19651 out yet, writing it out will cover this one, too. This does not apply
19652 to instantiations of member class templates; they need to be added to
19653 the containing class as they are generated. FIXME: This hurts the
19654 idea of combining type decls from multiple TUs, since we can't predict
19655 what set of template instantiations we'll get. */
19656 if (TYPE_CONTEXT (type)
19657 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19658 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
19660 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
19662 if (TREE_ASM_WRITTEN (type))
19663 return;
19665 /* If that failed, attach ourselves to the stub. */
19666 push_decl_scope (TYPE_CONTEXT (type));
19667 context_die = lookup_type_die (TYPE_CONTEXT (type));
19668 need_pop = 1;
19670 else if (TYPE_CONTEXT (type) != NULL_TREE
19671 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
19673 /* If this type is local to a function that hasn't been written
19674 out yet, use a NULL context for now; it will be fixed up in
19675 decls_for_scope. */
19676 context_die = lookup_decl_die (TYPE_CONTEXT (type));
19677 /* A declaration DIE doesn't count; nested types need to go in the
19678 specification. */
19679 if (context_die && is_declaration_die (context_die))
19680 context_die = NULL;
19681 need_pop = 0;
19683 else
19685 context_die = declare_in_namespace (type, context_die);
19686 need_pop = 0;
19689 if (TREE_CODE (type) == ENUMERAL_TYPE)
19691 /* This might have been written out by the call to
19692 declare_in_namespace. */
19693 if (!TREE_ASM_WRITTEN (type))
19694 gen_enumeration_type_die (type, context_die);
19696 else
19697 gen_struct_or_union_type_die (type, context_die, usage);
19699 if (need_pop)
19700 pop_decl_scope ();
19702 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
19703 it up if it is ever completed. gen_*_type_die will set it for us
19704 when appropriate. */
19707 /* Generate a type description DIE. */
19709 static void
19710 gen_type_die_with_usage (tree type, dw_die_ref context_die,
19711 enum debug_info_usage usage)
19713 struct array_descr_info info;
19715 if (type == NULL_TREE || type == error_mark_node)
19716 return;
19718 if (TYPE_NAME (type) != NULL_TREE
19719 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
19720 && is_redundant_typedef (TYPE_NAME (type))
19721 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
19722 /* The DECL of this type is a typedef we don't want to emit debug
19723 info for but we want debug info for its underlying typedef.
19724 This can happen for e.g, the injected-class-name of a C++
19725 type. */
19726 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
19728 /* If TYPE is a typedef type variant, let's generate debug info
19729 for the parent typedef which TYPE is a type of. */
19730 if (typedef_variant_p (type))
19732 if (TREE_ASM_WRITTEN (type))
19733 return;
19735 /* Prevent broken recursion; we can't hand off to the same type. */
19736 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
19738 /* Give typedefs the right scope. */
19739 context_die = scope_die_for (type, context_die);
19741 TREE_ASM_WRITTEN (type) = 1;
19743 gen_decl_die (TYPE_NAME (type), NULL, context_die);
19744 return;
19747 /* If type is an anonymous tagged type named by a typedef, let's
19748 generate debug info for the typedef. */
19749 if (is_naming_typedef_decl (TYPE_NAME (type)))
19751 /* Use the DIE of the containing namespace as the parent DIE of
19752 the type description DIE we want to generate. */
19753 if (DECL_CONTEXT (TYPE_NAME (type))
19754 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
19755 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
19757 gen_decl_die (TYPE_NAME (type), NULL, context_die);
19758 return;
19761 /* If this is an array type with hidden descriptor, handle it first. */
19762 if (!TREE_ASM_WRITTEN (type)
19763 && lang_hooks.types.get_array_descr_info
19764 && lang_hooks.types.get_array_descr_info (type, &info)
19765 && (dwarf_version >= 3 || !dwarf_strict))
19767 gen_descr_array_type_die (type, &info, context_die);
19768 TREE_ASM_WRITTEN (type) = 1;
19769 return;
19772 /* We are going to output a DIE to represent the unqualified version
19773 of this type (i.e. without any const or volatile qualifiers) so
19774 get the main variant (i.e. the unqualified version) of this type
19775 now. (Vectors are special because the debugging info is in the
19776 cloned type itself). */
19777 if (TREE_CODE (type) != VECTOR_TYPE)
19778 type = type_main_variant (type);
19780 if (TREE_ASM_WRITTEN (type))
19781 return;
19783 switch (TREE_CODE (type))
19785 case ERROR_MARK:
19786 break;
19788 case POINTER_TYPE:
19789 case REFERENCE_TYPE:
19790 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
19791 ensures that the gen_type_die recursion will terminate even if the
19792 type is recursive. Recursive types are possible in Ada. */
19793 /* ??? We could perhaps do this for all types before the switch
19794 statement. */
19795 TREE_ASM_WRITTEN (type) = 1;
19797 /* For these types, all that is required is that we output a DIE (or a
19798 set of DIEs) to represent the "basis" type. */
19799 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19800 DINFO_USAGE_IND_USE);
19801 break;
19803 case OFFSET_TYPE:
19804 /* This code is used for C++ pointer-to-data-member types.
19805 Output a description of the relevant class type. */
19806 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
19807 DINFO_USAGE_IND_USE);
19809 /* Output a description of the type of the object pointed to. */
19810 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19811 DINFO_USAGE_IND_USE);
19813 /* Now output a DIE to represent this pointer-to-data-member type
19814 itself. */
19815 gen_ptr_to_mbr_type_die (type, context_die);
19816 break;
19818 case FUNCTION_TYPE:
19819 /* Force out return type (in case it wasn't forced out already). */
19820 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19821 DINFO_USAGE_DIR_USE);
19822 gen_subroutine_type_die (type, context_die);
19823 break;
19825 case METHOD_TYPE:
19826 /* Force out return type (in case it wasn't forced out already). */
19827 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19828 DINFO_USAGE_DIR_USE);
19829 gen_subroutine_type_die (type, context_die);
19830 break;
19832 case ARRAY_TYPE:
19833 gen_array_type_die (type, context_die);
19834 break;
19836 case VECTOR_TYPE:
19837 gen_array_type_die (type, context_die);
19838 break;
19840 case ENUMERAL_TYPE:
19841 case RECORD_TYPE:
19842 case UNION_TYPE:
19843 case QUAL_UNION_TYPE:
19844 gen_tagged_type_die (type, context_die, usage);
19845 return;
19847 case VOID_TYPE:
19848 case INTEGER_TYPE:
19849 case REAL_TYPE:
19850 case FIXED_POINT_TYPE:
19851 case COMPLEX_TYPE:
19852 case BOOLEAN_TYPE:
19853 /* No DIEs needed for fundamental types. */
19854 break;
19856 case NULLPTR_TYPE:
19857 case LANG_TYPE:
19858 /* Just use DW_TAG_unspecified_type. */
19860 dw_die_ref type_die = lookup_type_die (type);
19861 if (type_die == NULL)
19863 tree name = TYPE_NAME (type);
19864 if (TREE_CODE (name) == TYPE_DECL)
19865 name = DECL_NAME (name);
19866 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (), type);
19867 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
19868 equate_type_number_to_die (type, type_die);
19871 break;
19873 default:
19874 if (is_cxx_auto (type))
19876 tree name = TYPE_NAME (type);
19877 if (TREE_CODE (name) == TYPE_DECL)
19878 name = DECL_NAME (name);
19879 dw_die_ref *die = (name == get_identifier ("auto")
19880 ? &auto_die : &decltype_auto_die);
19881 if (!*die)
19883 *die = new_die (DW_TAG_unspecified_type,
19884 comp_unit_die (), NULL_TREE);
19885 add_name_attribute (*die, IDENTIFIER_POINTER (name));
19887 equate_type_number_to_die (type, *die);
19888 break;
19890 gcc_unreachable ();
19893 TREE_ASM_WRITTEN (type) = 1;
19896 static void
19897 gen_type_die (tree type, dw_die_ref context_die)
19899 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
19902 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
19903 things which are local to the given block. */
19905 static void
19906 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
19908 int must_output_die = 0;
19909 bool inlined_func;
19911 /* Ignore blocks that are NULL. */
19912 if (stmt == NULL_TREE)
19913 return;
19915 inlined_func = inlined_function_outer_scope_p (stmt);
19917 /* If the block is one fragment of a non-contiguous block, do not
19918 process the variables, since they will have been done by the
19919 origin block. Do process subblocks. */
19920 if (BLOCK_FRAGMENT_ORIGIN (stmt))
19922 tree sub;
19924 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
19925 gen_block_die (sub, context_die, depth + 1);
19927 return;
19930 /* Determine if we need to output any Dwarf DIEs at all to represent this
19931 block. */
19932 if (inlined_func)
19933 /* The outer scopes for inlinings *must* always be represented. We
19934 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
19935 must_output_die = 1;
19936 else
19938 /* Determine if this block directly contains any "significant"
19939 local declarations which we will need to output DIEs for. */
19940 if (debug_info_level > DINFO_LEVEL_TERSE)
19941 /* We are not in terse mode so *any* local declaration counts
19942 as being a "significant" one. */
19943 must_output_die = ((BLOCK_VARS (stmt) != NULL
19944 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
19945 && (TREE_USED (stmt)
19946 || TREE_ASM_WRITTEN (stmt)
19947 || BLOCK_ABSTRACT (stmt)));
19948 else if ((TREE_USED (stmt)
19949 || TREE_ASM_WRITTEN (stmt)
19950 || BLOCK_ABSTRACT (stmt))
19951 && !dwarf2out_ignore_block (stmt))
19952 must_output_die = 1;
19955 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
19956 DIE for any block which contains no significant local declarations at
19957 all. Rather, in such cases we just call `decls_for_scope' so that any
19958 needed Dwarf info for any sub-blocks will get properly generated. Note
19959 that in terse mode, our definition of what constitutes a "significant"
19960 local declaration gets restricted to include only inlined function
19961 instances and local (nested) function definitions. */
19962 if (must_output_die)
19964 if (inlined_func)
19966 /* If STMT block is abstract, that means we have been called
19967 indirectly from dwarf2out_abstract_function.
19968 That function rightfully marks the descendent blocks (of
19969 the abstract function it is dealing with) as being abstract,
19970 precisely to prevent us from emitting any
19971 DW_TAG_inlined_subroutine DIE as a descendent
19972 of an abstract function instance. So in that case, we should
19973 not call gen_inlined_subroutine_die.
19975 Later though, when cgraph asks dwarf2out to emit info
19976 for the concrete instance of the function decl into which
19977 the concrete instance of STMT got inlined, the later will lead
19978 to the generation of a DW_TAG_inlined_subroutine DIE. */
19979 if (! BLOCK_ABSTRACT (stmt))
19980 gen_inlined_subroutine_die (stmt, context_die, depth);
19982 else
19983 gen_lexical_block_die (stmt, context_die, depth);
19985 else
19986 decls_for_scope (stmt, context_die, depth);
19989 /* Process variable DECL (or variable with origin ORIGIN) within
19990 block STMT and add it to CONTEXT_DIE. */
19991 static void
19992 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
19994 dw_die_ref die;
19995 tree decl_or_origin = decl ? decl : origin;
19997 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
19998 die = lookup_decl_die (decl_or_origin);
19999 else if (TREE_CODE (decl_or_origin) == TYPE_DECL
20000 && TYPE_DECL_IS_STUB (decl_or_origin))
20001 die = lookup_type_die (TREE_TYPE (decl_or_origin));
20002 else
20003 die = NULL;
20005 if (die != NULL && die->die_parent == NULL)
20006 add_child_die (context_die, die);
20007 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
20008 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
20009 stmt, context_die);
20010 else
20011 gen_decl_die (decl, origin, context_die);
20014 /* Generate all of the decls declared within a given scope and (recursively)
20015 all of its sub-blocks. */
20017 static void
20018 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
20020 tree decl;
20021 unsigned int i;
20022 tree subblocks;
20024 /* Ignore NULL blocks. */
20025 if (stmt == NULL_TREE)
20026 return;
20028 /* Output the DIEs to represent all of the data objects and typedefs
20029 declared directly within this block but not within any nested
20030 sub-blocks. Also, nested function and tag DIEs have been
20031 generated with a parent of NULL; fix that up now. We don't
20032 have to do this if we're at -g1. */
20033 if (debug_info_level > DINFO_LEVEL_TERSE)
20035 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
20036 process_scope_var (stmt, decl, NULL_TREE, context_die);
20037 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
20038 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
20039 context_die);
20042 /* Even if we're at -g1, we need to process the subblocks in order to get
20043 inlined call information. */
20045 /* Output the DIEs to represent all sub-blocks (and the items declared
20046 therein) of this block. */
20047 for (subblocks = BLOCK_SUBBLOCKS (stmt);
20048 subblocks != NULL;
20049 subblocks = BLOCK_CHAIN (subblocks))
20050 gen_block_die (subblocks, context_die, depth + 1);
20053 /* Is this a typedef we can avoid emitting? */
20055 static inline int
20056 is_redundant_typedef (const_tree decl)
20058 if (TYPE_DECL_IS_STUB (decl))
20059 return 1;
20061 if (DECL_ARTIFICIAL (decl)
20062 && DECL_CONTEXT (decl)
20063 && is_tagged_type (DECL_CONTEXT (decl))
20064 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
20065 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
20066 /* Also ignore the artificial member typedef for the class name. */
20067 return 1;
20069 return 0;
20072 /* Return TRUE if TYPE is a typedef that names a type for linkage
20073 purposes. This kind of typedefs is produced by the C++ FE for
20074 constructs like:
20076 typedef struct {...} foo;
20078 In that case, there is no typedef variant type produced for foo.
20079 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
20080 struct type. */
20082 static bool
20083 is_naming_typedef_decl (const_tree decl)
20085 if (decl == NULL_TREE
20086 || TREE_CODE (decl) != TYPE_DECL
20087 || !is_tagged_type (TREE_TYPE (decl))
20088 || DECL_IS_BUILTIN (decl)
20089 || is_redundant_typedef (decl)
20090 /* It looks like Ada produces TYPE_DECLs that are very similar
20091 to C++ naming typedefs but that have different
20092 semantics. Let's be specific to c++ for now. */
20093 || !is_cxx ())
20094 return FALSE;
20096 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
20097 && TYPE_NAME (TREE_TYPE (decl)) == decl
20098 && (TYPE_STUB_DECL (TREE_TYPE (decl))
20099 != TYPE_NAME (TREE_TYPE (decl))));
20102 /* Returns the DIE for a context. */
20104 static inline dw_die_ref
20105 get_context_die (tree context)
20107 if (context)
20109 /* Find die that represents this context. */
20110 if (TYPE_P (context))
20112 context = TYPE_MAIN_VARIANT (context);
20113 return strip_naming_typedef (context, force_type_die (context));
20115 else
20116 return force_decl_die (context);
20118 return comp_unit_die ();
20121 /* Returns the DIE for decl. A DIE will always be returned. */
20123 static dw_die_ref
20124 force_decl_die (tree decl)
20126 dw_die_ref decl_die;
20127 unsigned saved_external_flag;
20128 tree save_fn = NULL_TREE;
20129 decl_die = lookup_decl_die (decl);
20130 if (!decl_die)
20132 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
20134 decl_die = lookup_decl_die (decl);
20135 if (decl_die)
20136 return decl_die;
20138 switch (TREE_CODE (decl))
20140 case FUNCTION_DECL:
20141 /* Clear current_function_decl, so that gen_subprogram_die thinks
20142 that this is a declaration. At this point, we just want to force
20143 declaration die. */
20144 save_fn = current_function_decl;
20145 current_function_decl = NULL_TREE;
20146 gen_subprogram_die (decl, context_die);
20147 current_function_decl = save_fn;
20148 break;
20150 case VAR_DECL:
20151 /* Set external flag to force declaration die. Restore it after
20152 gen_decl_die() call. */
20153 saved_external_flag = DECL_EXTERNAL (decl);
20154 DECL_EXTERNAL (decl) = 1;
20155 gen_decl_die (decl, NULL, context_die);
20156 DECL_EXTERNAL (decl) = saved_external_flag;
20157 break;
20159 case NAMESPACE_DECL:
20160 if (dwarf_version >= 3 || !dwarf_strict)
20161 dwarf2out_decl (decl);
20162 else
20163 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
20164 decl_die = comp_unit_die ();
20165 break;
20167 case TRANSLATION_UNIT_DECL:
20168 decl_die = comp_unit_die ();
20169 break;
20171 default:
20172 gcc_unreachable ();
20175 /* We should be able to find the DIE now. */
20176 if (!decl_die)
20177 decl_die = lookup_decl_die (decl);
20178 gcc_assert (decl_die);
20181 return decl_die;
20184 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
20185 always returned. */
20187 static dw_die_ref
20188 force_type_die (tree type)
20190 dw_die_ref type_die;
20192 type_die = lookup_type_die (type);
20193 if (!type_die)
20195 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
20197 type_die = modified_type_die (type, TYPE_READONLY (type),
20198 TYPE_VOLATILE (type), context_die);
20199 gcc_assert (type_die);
20201 return type_die;
20204 /* Force out any required namespaces to be able to output DECL,
20205 and return the new context_die for it, if it's changed. */
20207 static dw_die_ref
20208 setup_namespace_context (tree thing, dw_die_ref context_die)
20210 tree context = (DECL_P (thing)
20211 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
20212 if (context && TREE_CODE (context) == NAMESPACE_DECL)
20213 /* Force out the namespace. */
20214 context_die = force_decl_die (context);
20216 return context_die;
20219 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
20220 type) within its namespace, if appropriate.
20222 For compatibility with older debuggers, namespace DIEs only contain
20223 declarations; all definitions are emitted at CU scope. */
20225 static dw_die_ref
20226 declare_in_namespace (tree thing, dw_die_ref context_die)
20228 dw_die_ref ns_context;
20230 if (debug_info_level <= DINFO_LEVEL_TERSE)
20231 return context_die;
20233 /* If this decl is from an inlined function, then don't try to emit it in its
20234 namespace, as we will get confused. It would have already been emitted
20235 when the abstract instance of the inline function was emitted anyways. */
20236 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
20237 return context_die;
20239 ns_context = setup_namespace_context (thing, context_die);
20241 if (ns_context != context_die)
20243 if (is_fortran ())
20244 return ns_context;
20245 if (DECL_P (thing))
20246 gen_decl_die (thing, NULL, ns_context);
20247 else
20248 gen_type_die (thing, ns_context);
20250 return context_die;
20253 /* Generate a DIE for a namespace or namespace alias. */
20255 static void
20256 gen_namespace_die (tree decl, dw_die_ref context_die)
20258 dw_die_ref namespace_die;
20260 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
20261 they are an alias of. */
20262 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
20264 /* Output a real namespace or module. */
20265 context_die = setup_namespace_context (decl, comp_unit_die ());
20266 namespace_die = new_die (is_fortran ()
20267 ? DW_TAG_module : DW_TAG_namespace,
20268 context_die, decl);
20269 /* For Fortran modules defined in different CU don't add src coords. */
20270 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
20272 const char *name = dwarf2_name (decl, 0);
20273 if (name)
20274 add_name_attribute (namespace_die, name);
20276 else
20277 add_name_and_src_coords_attributes (namespace_die, decl);
20278 if (DECL_EXTERNAL (decl))
20279 add_AT_flag (namespace_die, DW_AT_declaration, 1);
20280 equate_decl_number_to_die (decl, namespace_die);
20282 else
20284 /* Output a namespace alias. */
20286 /* Force out the namespace we are an alias of, if necessary. */
20287 dw_die_ref origin_die
20288 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
20290 if (DECL_FILE_SCOPE_P (decl)
20291 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
20292 context_die = setup_namespace_context (decl, comp_unit_die ());
20293 /* Now create the namespace alias DIE. */
20294 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
20295 add_name_and_src_coords_attributes (namespace_die, decl);
20296 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
20297 equate_decl_number_to_die (decl, namespace_die);
20299 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
20300 if (want_pubnames ())
20301 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
20304 /* Generate Dwarf debug information for a decl described by DECL.
20305 The return value is currently only meaningful for PARM_DECLs,
20306 for all other decls it returns NULL. */
20308 static dw_die_ref
20309 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
20311 tree decl_or_origin = decl ? decl : origin;
20312 tree class_origin = NULL, ultimate_origin;
20314 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
20315 return NULL;
20317 switch (TREE_CODE (decl_or_origin))
20319 case ERROR_MARK:
20320 break;
20322 case CONST_DECL:
20323 if (!is_fortran () && !is_ada ())
20325 /* The individual enumerators of an enum type get output when we output
20326 the Dwarf representation of the relevant enum type itself. */
20327 break;
20330 /* Emit its type. */
20331 gen_type_die (TREE_TYPE (decl), context_die);
20333 /* And its containing namespace. */
20334 context_die = declare_in_namespace (decl, context_die);
20336 gen_const_die (decl, context_die);
20337 break;
20339 case FUNCTION_DECL:
20340 /* Don't output any DIEs to represent mere function declarations,
20341 unless they are class members or explicit block externs. */
20342 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
20343 && DECL_FILE_SCOPE_P (decl_or_origin)
20344 && (current_function_decl == NULL_TREE
20345 || DECL_ARTIFICIAL (decl_or_origin)))
20346 break;
20348 #if 0
20349 /* FIXME */
20350 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
20351 on local redeclarations of global functions. That seems broken. */
20352 if (current_function_decl != decl)
20353 /* This is only a declaration. */;
20354 #endif
20356 /* If we're emitting a clone, emit info for the abstract instance. */
20357 if (origin || DECL_ORIGIN (decl) != decl)
20358 dwarf2out_abstract_function (origin
20359 ? DECL_ORIGIN (origin)
20360 : DECL_ABSTRACT_ORIGIN (decl));
20362 /* If we're emitting an out-of-line copy of an inline function,
20363 emit info for the abstract instance and set up to refer to it. */
20364 else if (cgraph_function_possibly_inlined_p (decl)
20365 && ! DECL_ABSTRACT (decl)
20366 && ! class_or_namespace_scope_p (context_die)
20367 /* dwarf2out_abstract_function won't emit a die if this is just
20368 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
20369 that case, because that works only if we have a die. */
20370 && DECL_INITIAL (decl) != NULL_TREE)
20372 dwarf2out_abstract_function (decl);
20373 set_decl_origin_self (decl);
20376 /* Otherwise we're emitting the primary DIE for this decl. */
20377 else if (debug_info_level > DINFO_LEVEL_TERSE)
20379 /* Before we describe the FUNCTION_DECL itself, make sure that we
20380 have its containing type. */
20381 if (!origin)
20382 origin = decl_class_context (decl);
20383 if (origin != NULL_TREE)
20384 gen_type_die (origin, context_die);
20386 /* And its return type. */
20387 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
20389 /* And its virtual context. */
20390 if (DECL_VINDEX (decl) != NULL_TREE)
20391 gen_type_die (DECL_CONTEXT (decl), context_die);
20393 /* Make sure we have a member DIE for decl. */
20394 if (origin != NULL_TREE)
20395 gen_type_die_for_member (origin, decl, context_die);
20397 /* And its containing namespace. */
20398 context_die = declare_in_namespace (decl, context_die);
20401 /* Now output a DIE to represent the function itself. */
20402 if (decl)
20403 gen_subprogram_die (decl, context_die);
20404 break;
20406 case TYPE_DECL:
20407 /* If we are in terse mode, don't generate any DIEs to represent any
20408 actual typedefs. */
20409 if (debug_info_level <= DINFO_LEVEL_TERSE)
20410 break;
20412 /* In the special case of a TYPE_DECL node representing the declaration
20413 of some type tag, if the given TYPE_DECL is marked as having been
20414 instantiated from some other (original) TYPE_DECL node (e.g. one which
20415 was generated within the original definition of an inline function) we
20416 used to generate a special (abbreviated) DW_TAG_structure_type,
20417 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
20418 should be actually referencing those DIEs, as variable DIEs with that
20419 type would be emitted already in the abstract origin, so it was always
20420 removed during unused type prunning. Don't add anything in this
20421 case. */
20422 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
20423 break;
20425 if (is_redundant_typedef (decl))
20426 gen_type_die (TREE_TYPE (decl), context_die);
20427 else
20428 /* Output a DIE to represent the typedef itself. */
20429 gen_typedef_die (decl, context_die);
20430 break;
20432 case LABEL_DECL:
20433 if (debug_info_level >= DINFO_LEVEL_NORMAL)
20434 gen_label_die (decl, context_die);
20435 break;
20437 case VAR_DECL:
20438 case RESULT_DECL:
20439 /* If we are in terse mode, don't generate any DIEs to represent any
20440 variable declarations or definitions. */
20441 if (debug_info_level <= DINFO_LEVEL_TERSE)
20442 break;
20444 /* Output any DIEs that are needed to specify the type of this data
20445 object. */
20446 if (decl_by_reference_p (decl_or_origin))
20447 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20448 else
20449 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20451 /* And its containing type. */
20452 class_origin = decl_class_context (decl_or_origin);
20453 if (class_origin != NULL_TREE)
20454 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
20456 /* And its containing namespace. */
20457 context_die = declare_in_namespace (decl_or_origin, context_die);
20459 /* Now output the DIE to represent the data object itself. This gets
20460 complicated because of the possibility that the VAR_DECL really
20461 represents an inlined instance of a formal parameter for an inline
20462 function. */
20463 ultimate_origin = decl_ultimate_origin (decl_or_origin);
20464 if (ultimate_origin != NULL_TREE
20465 && TREE_CODE (ultimate_origin) == PARM_DECL)
20466 gen_formal_parameter_die (decl, origin,
20467 true /* Emit name attribute. */,
20468 context_die);
20469 else
20470 gen_variable_die (decl, origin, context_die);
20471 break;
20473 case FIELD_DECL:
20474 /* Ignore the nameless fields that are used to skip bits but handle C++
20475 anonymous unions and structs. */
20476 if (DECL_NAME (decl) != NULL_TREE
20477 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
20478 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
20480 gen_type_die (member_declared_type (decl), context_die);
20481 gen_field_die (decl, context_die);
20483 break;
20485 case PARM_DECL:
20486 if (DECL_BY_REFERENCE (decl_or_origin))
20487 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20488 else
20489 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20490 return gen_formal_parameter_die (decl, origin,
20491 true /* Emit name attribute. */,
20492 context_die);
20494 case NAMESPACE_DECL:
20495 case IMPORTED_DECL:
20496 if (dwarf_version >= 3 || !dwarf_strict)
20497 gen_namespace_die (decl, context_die);
20498 break;
20500 case NAMELIST_DECL:
20501 gen_namelist_decl (DECL_NAME (decl), context_die,
20502 NAMELIST_DECL_ASSOCIATED_DECL (decl));
20503 break;
20505 default:
20506 /* Probably some frontend-internal decl. Assume we don't care. */
20507 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
20508 break;
20511 return NULL;
20514 /* Output debug information for global decl DECL. Called from toplev.c after
20515 compilation proper has finished. */
20517 static void
20518 dwarf2out_global_decl (tree decl)
20520 /* Output DWARF2 information for file-scope tentative data object
20521 declarations, file-scope (extern) function declarations (which
20522 had no corresponding body) and file-scope tagged type declarations
20523 and definitions which have not yet been forced out. */
20524 if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
20525 dwarf2out_decl (decl);
20528 /* Output debug information for type decl DECL. Called from toplev.c
20529 and from language front ends (to record built-in types). */
20530 static void
20531 dwarf2out_type_decl (tree decl, int local)
20533 if (!local)
20534 dwarf2out_decl (decl);
20537 /* Output debug information for imported module or decl DECL.
20538 NAME is non-NULL name in the lexical block if the decl has been renamed.
20539 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
20540 that DECL belongs to.
20541 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
20542 static void
20543 dwarf2out_imported_module_or_decl_1 (tree decl,
20544 tree name,
20545 tree lexical_block,
20546 dw_die_ref lexical_block_die)
20548 expanded_location xloc;
20549 dw_die_ref imported_die = NULL;
20550 dw_die_ref at_import_die;
20552 if (TREE_CODE (decl) == IMPORTED_DECL)
20554 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
20555 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
20556 gcc_assert (decl);
20558 else
20559 xloc = expand_location (input_location);
20561 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
20563 at_import_die = force_type_die (TREE_TYPE (decl));
20564 /* For namespace N { typedef void T; } using N::T; base_type_die
20565 returns NULL, but DW_TAG_imported_declaration requires
20566 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
20567 if (!at_import_die)
20569 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
20570 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
20571 at_import_die = lookup_type_die (TREE_TYPE (decl));
20572 gcc_assert (at_import_die);
20575 else
20577 at_import_die = lookup_decl_die (decl);
20578 if (!at_import_die)
20580 /* If we're trying to avoid duplicate debug info, we may not have
20581 emitted the member decl for this field. Emit it now. */
20582 if (TREE_CODE (decl) == FIELD_DECL)
20584 tree type = DECL_CONTEXT (decl);
20586 if (TYPE_CONTEXT (type)
20587 && TYPE_P (TYPE_CONTEXT (type))
20588 && !should_emit_struct_debug (TYPE_CONTEXT (type),
20589 DINFO_USAGE_DIR_USE))
20590 return;
20591 gen_type_die_for_member (type, decl,
20592 get_context_die (TYPE_CONTEXT (type)));
20594 if (TREE_CODE (decl) == NAMELIST_DECL)
20595 at_import_die = gen_namelist_decl (DECL_NAME (decl),
20596 get_context_die (DECL_CONTEXT (decl)),
20597 NULL_TREE);
20598 else
20599 at_import_die = force_decl_die (decl);
20603 if (TREE_CODE (decl) == NAMESPACE_DECL)
20605 if (dwarf_version >= 3 || !dwarf_strict)
20606 imported_die = new_die (DW_TAG_imported_module,
20607 lexical_block_die,
20608 lexical_block);
20609 else
20610 return;
20612 else
20613 imported_die = new_die (DW_TAG_imported_declaration,
20614 lexical_block_die,
20615 lexical_block);
20617 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
20618 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
20619 if (name)
20620 add_AT_string (imported_die, DW_AT_name,
20621 IDENTIFIER_POINTER (name));
20622 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
20625 /* Output debug information for imported module or decl DECL.
20626 NAME is non-NULL name in context if the decl has been renamed.
20627 CHILD is true if decl is one of the renamed decls as part of
20628 importing whole module. */
20630 static void
20631 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
20632 bool child)
20634 /* dw_die_ref at_import_die; */
20635 dw_die_ref scope_die;
20637 if (debug_info_level <= DINFO_LEVEL_TERSE)
20638 return;
20640 gcc_assert (decl);
20642 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
20643 We need decl DIE for reference and scope die. First, get DIE for the decl
20644 itself. */
20646 /* Get the scope die for decl context. Use comp_unit_die for global module
20647 or decl. If die is not found for non globals, force new die. */
20648 if (context
20649 && TYPE_P (context)
20650 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
20651 return;
20653 if (!(dwarf_version >= 3 || !dwarf_strict))
20654 return;
20656 scope_die = get_context_die (context);
20658 if (child)
20660 gcc_assert (scope_die->die_child);
20661 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
20662 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
20663 scope_die = scope_die->die_child;
20666 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
20667 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
20671 /* Output debug information for namelists. */
20673 static dw_die_ref
20674 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
20676 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
20677 tree value;
20678 unsigned i;
20680 if (debug_info_level <= DINFO_LEVEL_TERSE)
20681 return NULL;
20683 gcc_assert (scope_die != NULL);
20684 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
20685 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
20687 /* If there are no item_decls, we have a nondefining namelist, e.g.
20688 with USE association; hence, set DW_AT_declaration. */
20689 if (item_decls == NULL_TREE)
20691 add_AT_flag (nml_die, DW_AT_declaration, 1);
20692 return nml_die;
20695 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
20697 nml_item_ref_die = lookup_decl_die (value);
20698 if (!nml_item_ref_die)
20699 nml_item_ref_die = force_decl_die (value);
20701 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
20702 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
20704 return nml_die;
20708 /* Write the debugging output for DECL. */
20710 void
20711 dwarf2out_decl (tree decl)
20713 dw_die_ref context_die = comp_unit_die ();
20715 switch (TREE_CODE (decl))
20717 case ERROR_MARK:
20718 return;
20720 case FUNCTION_DECL:
20721 /* What we would really like to do here is to filter out all mere
20722 file-scope declarations of file-scope functions which are never
20723 referenced later within this translation unit (and keep all of ones
20724 that *are* referenced later on) but we aren't clairvoyant, so we have
20725 no idea which functions will be referenced in the future (i.e. later
20726 on within the current translation unit). So here we just ignore all
20727 file-scope function declarations which are not also definitions. If
20728 and when the debugger needs to know something about these functions,
20729 it will have to hunt around and find the DWARF information associated
20730 with the definition of the function.
20732 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
20733 nodes represent definitions and which ones represent mere
20734 declarations. We have to check DECL_INITIAL instead. That's because
20735 the C front-end supports some weird semantics for "extern inline"
20736 function definitions. These can get inlined within the current
20737 translation unit (and thus, we need to generate Dwarf info for their
20738 abstract instances so that the Dwarf info for the concrete inlined
20739 instances can have something to refer to) but the compiler never
20740 generates any out-of-lines instances of such things (despite the fact
20741 that they *are* definitions).
20743 The important point is that the C front-end marks these "extern
20744 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
20745 them anyway. Note that the C++ front-end also plays some similar games
20746 for inline function definitions appearing within include files which
20747 also contain `#pragma interface' pragmas.
20749 If we are called from dwarf2out_abstract_function output a DIE
20750 anyway. We can end up here this way with early inlining and LTO
20751 where the inlined function is output in a different LTRANS unit
20752 or not at all. */
20753 if (DECL_INITIAL (decl) == NULL_TREE
20754 && ! DECL_ABSTRACT (decl))
20755 return;
20757 /* If we're a nested function, initially use a parent of NULL; if we're
20758 a plain function, this will be fixed up in decls_for_scope. If
20759 we're a method, it will be ignored, since we already have a DIE. */
20760 if (decl_function_context (decl)
20761 /* But if we're in terse mode, we don't care about scope. */
20762 && debug_info_level > DINFO_LEVEL_TERSE)
20763 context_die = NULL;
20764 break;
20766 case VAR_DECL:
20767 /* Ignore this VAR_DECL if it refers to a file-scope extern data object
20768 declaration and if the declaration was never even referenced from
20769 within this entire compilation unit. We suppress these DIEs in
20770 order to save space in the .debug section (by eliminating entries
20771 which are probably useless). Note that we must not suppress
20772 block-local extern declarations (whether used or not) because that
20773 would screw-up the debugger's name lookup mechanism and cause it to
20774 miss things which really ought to be in scope at a given point. */
20775 if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
20776 return;
20778 /* For local statics lookup proper context die. */
20779 if (TREE_STATIC (decl)
20780 && DECL_CONTEXT (decl)
20781 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
20782 context_die = lookup_decl_die (DECL_CONTEXT (decl));
20784 /* If we are in terse mode, don't generate any DIEs to represent any
20785 variable declarations or definitions. */
20786 if (debug_info_level <= DINFO_LEVEL_TERSE)
20787 return;
20788 break;
20790 case CONST_DECL:
20791 if (debug_info_level <= DINFO_LEVEL_TERSE)
20792 return;
20793 if (!is_fortran () && !is_ada ())
20794 return;
20795 if (TREE_STATIC (decl) && decl_function_context (decl))
20796 context_die = lookup_decl_die (DECL_CONTEXT (decl));
20797 break;
20799 case NAMESPACE_DECL:
20800 case IMPORTED_DECL:
20801 if (debug_info_level <= DINFO_LEVEL_TERSE)
20802 return;
20803 if (lookup_decl_die (decl) != NULL)
20804 return;
20805 break;
20807 case TYPE_DECL:
20808 /* Don't emit stubs for types unless they are needed by other DIEs. */
20809 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
20810 return;
20812 /* Don't bother trying to generate any DIEs to represent any of the
20813 normal built-in types for the language we are compiling. */
20814 if (DECL_IS_BUILTIN (decl))
20815 return;
20817 /* If we are in terse mode, don't generate any DIEs for types. */
20818 if (debug_info_level <= DINFO_LEVEL_TERSE)
20819 return;
20821 /* If we're a function-scope tag, initially use a parent of NULL;
20822 this will be fixed up in decls_for_scope. */
20823 if (decl_function_context (decl))
20824 context_die = NULL;
20826 break;
20828 case NAMELIST_DECL:
20829 break;
20831 default:
20832 return;
20835 gen_decl_die (decl, NULL, context_die);
20838 /* Write the debugging output for DECL. */
20840 static void
20841 dwarf2out_function_decl (tree decl)
20843 dwarf2out_decl (decl);
20844 call_arg_locations = NULL;
20845 call_arg_loc_last = NULL;
20846 call_site_count = -1;
20847 tail_call_site_count = -1;
20848 block_map.release ();
20849 htab_empty (decl_loc_table);
20850 htab_empty (cached_dw_loc_list_table);
20853 /* Output a marker (i.e. a label) for the beginning of the generated code for
20854 a lexical block. */
20856 static void
20857 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
20858 unsigned int blocknum)
20860 switch_to_section (current_function_section ());
20861 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
20864 /* Output a marker (i.e. a label) for the end of the generated code for a
20865 lexical block. */
20867 static void
20868 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
20870 switch_to_section (current_function_section ());
20871 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
20874 /* Returns nonzero if it is appropriate not to emit any debugging
20875 information for BLOCK, because it doesn't contain any instructions.
20877 Don't allow this for blocks with nested functions or local classes
20878 as we would end up with orphans, and in the presence of scheduling
20879 we may end up calling them anyway. */
20881 static bool
20882 dwarf2out_ignore_block (const_tree block)
20884 tree decl;
20885 unsigned int i;
20887 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
20888 if (TREE_CODE (decl) == FUNCTION_DECL
20889 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
20890 return 0;
20891 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
20893 decl = BLOCK_NONLOCALIZED_VAR (block, i);
20894 if (TREE_CODE (decl) == FUNCTION_DECL
20895 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
20896 return 0;
20899 return 1;
20902 /* Hash table routines for file_hash. */
20904 static int
20905 file_table_eq (const void *p1_p, const void *p2_p)
20907 const struct dwarf_file_data *const p1 =
20908 (const struct dwarf_file_data *) p1_p;
20909 const char *const p2 = (const char *) p2_p;
20910 return filename_cmp (p1->filename, p2) == 0;
20913 static hashval_t
20914 file_table_hash (const void *p_p)
20916 const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
20917 return htab_hash_string (p->filename);
20920 /* Lookup FILE_NAME (in the list of filenames that we know about here in
20921 dwarf2out.c) and return its "index". The index of each (known) filename is
20922 just a unique number which is associated with only that one filename. We
20923 need such numbers for the sake of generating labels (in the .debug_sfnames
20924 section) and references to those files numbers (in the .debug_srcinfo
20925 and.debug_macinfo sections). If the filename given as an argument is not
20926 found in our current list, add it to the list and assign it the next
20927 available unique index number. In order to speed up searches, we remember
20928 the index of the filename was looked up last. This handles the majority of
20929 all searches. */
20931 static struct dwarf_file_data *
20932 lookup_filename (const char *file_name)
20934 void ** slot;
20935 struct dwarf_file_data * created;
20937 /* Check to see if the file name that was searched on the previous
20938 call matches this file name. If so, return the index. */
20939 if (file_table_last_lookup
20940 && (file_name == file_table_last_lookup->filename
20941 || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
20942 return file_table_last_lookup;
20944 /* Didn't match the previous lookup, search the table. */
20945 slot = htab_find_slot_with_hash (file_table, file_name,
20946 htab_hash_string (file_name), INSERT);
20947 if (*slot)
20948 return (struct dwarf_file_data *) *slot;
20950 created = ggc_alloc_dwarf_file_data ();
20951 created->filename = file_name;
20952 created->emitted_number = 0;
20953 *slot = created;
20954 return created;
20957 /* If the assembler will construct the file table, then translate the compiler
20958 internal file table number into the assembler file table number, and emit
20959 a .file directive if we haven't already emitted one yet. The file table
20960 numbers are different because we prune debug info for unused variables and
20961 types, which may include filenames. */
20963 static int
20964 maybe_emit_file (struct dwarf_file_data * fd)
20966 if (! fd->emitted_number)
20968 if (last_emitted_file)
20969 fd->emitted_number = last_emitted_file->emitted_number + 1;
20970 else
20971 fd->emitted_number = 1;
20972 last_emitted_file = fd;
20974 if (DWARF2_ASM_LINE_DEBUG_INFO)
20976 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
20977 output_quoted_string (asm_out_file,
20978 remap_debug_filename (fd->filename));
20979 fputc ('\n', asm_out_file);
20983 return fd->emitted_number;
20986 /* Schedule generation of a DW_AT_const_value attribute to DIE.
20987 That generation should happen after function debug info has been
20988 generated. The value of the attribute is the constant value of ARG. */
20990 static void
20991 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
20993 die_arg_entry entry;
20995 if (!die || !arg)
20996 return;
20998 if (!tmpl_value_parm_die_table)
20999 vec_alloc (tmpl_value_parm_die_table, 32);
21001 entry.die = die;
21002 entry.arg = arg;
21003 vec_safe_push (tmpl_value_parm_die_table, entry);
21006 /* Return TRUE if T is an instance of generic type, FALSE
21007 otherwise. */
21009 static bool
21010 generic_type_p (tree t)
21012 if (t == NULL_TREE || !TYPE_P (t))
21013 return false;
21014 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
21017 /* Schedule the generation of the generic parameter dies for the
21018 instance of generic type T. The proper generation itself is later
21019 done by gen_scheduled_generic_parms_dies. */
21021 static void
21022 schedule_generic_params_dies_gen (tree t)
21024 if (!generic_type_p (t))
21025 return;
21027 if (!generic_type_instances)
21028 vec_alloc (generic_type_instances, 256);
21030 vec_safe_push (generic_type_instances, t);
21033 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
21034 by append_entry_to_tmpl_value_parm_die_table. This function must
21035 be called after function DIEs have been generated. */
21037 static void
21038 gen_remaining_tmpl_value_param_die_attribute (void)
21040 if (tmpl_value_parm_die_table)
21042 unsigned i;
21043 die_arg_entry *e;
21045 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
21046 tree_add_const_value_attribute (e->die, e->arg);
21050 /* Generate generic parameters DIEs for instances of generic types
21051 that have been previously scheduled by
21052 schedule_generic_params_dies_gen. This function must be called
21053 after all the types of the CU have been laid out. */
21055 static void
21056 gen_scheduled_generic_parms_dies (void)
21058 unsigned i;
21059 tree t;
21061 if (!generic_type_instances)
21062 return;
21064 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
21065 if (COMPLETE_TYPE_P (t))
21066 gen_generic_params_dies (t);
21070 /* Replace DW_AT_name for the decl with name. */
21072 static void
21073 dwarf2out_set_name (tree decl, tree name)
21075 dw_die_ref die;
21076 dw_attr_ref attr;
21077 const char *dname;
21079 die = TYPE_SYMTAB_DIE (decl);
21080 if (!die)
21081 return;
21083 dname = dwarf2_name (name, 0);
21084 if (!dname)
21085 return;
21087 attr = get_AT (die, DW_AT_name);
21088 if (attr)
21090 struct indirect_string_node *node;
21092 node = find_AT_string (dname);
21093 /* replace the string. */
21094 attr->dw_attr_val.v.val_str = node;
21097 else
21098 add_name_attribute (die, dname);
21101 /* True if before or during processing of the first function being emitted. */
21102 static bool in_first_function_p = true;
21103 /* True if loc_note during dwarf2out_var_location call might still be
21104 before first real instruction at address equal to .Ltext0. */
21105 static bool maybe_at_text_label_p = true;
21106 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
21107 static unsigned int first_loclabel_num_not_at_text_label;
21109 /* Called by the final INSN scan whenever we see a var location. We
21110 use it to drop labels in the right places, and throw the location in
21111 our lookup table. */
21113 static void
21114 dwarf2out_var_location (rtx loc_note)
21116 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
21117 struct var_loc_node *newloc;
21118 rtx next_real, next_note;
21119 static const char *last_label;
21120 static const char *last_postcall_label;
21121 static bool last_in_cold_section_p;
21122 static rtx expected_next_loc_note;
21123 tree decl;
21124 bool var_loc_p;
21126 if (!NOTE_P (loc_note))
21128 if (CALL_P (loc_note))
21130 call_site_count++;
21131 if (SIBLING_CALL_P (loc_note))
21132 tail_call_site_count++;
21134 return;
21137 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
21138 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
21139 return;
21141 /* Optimize processing a large consecutive sequence of location
21142 notes so we don't spend too much time in next_real_insn. If the
21143 next insn is another location note, remember the next_real_insn
21144 calculation for next time. */
21145 next_real = cached_next_real_insn;
21146 if (next_real)
21148 if (expected_next_loc_note != loc_note)
21149 next_real = NULL_RTX;
21152 next_note = NEXT_INSN (loc_note);
21153 if (! next_note
21154 || INSN_DELETED_P (next_note)
21155 || ! NOTE_P (next_note)
21156 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
21157 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
21158 next_note = NULL_RTX;
21160 if (! next_real)
21161 next_real = next_real_insn (loc_note);
21163 if (next_note)
21165 expected_next_loc_note = next_note;
21166 cached_next_real_insn = next_real;
21168 else
21169 cached_next_real_insn = NULL_RTX;
21171 /* If there are no instructions which would be affected by this note,
21172 don't do anything. */
21173 if (var_loc_p
21174 && next_real == NULL_RTX
21175 && !NOTE_DURING_CALL_P (loc_note))
21176 return;
21178 if (next_real == NULL_RTX)
21179 next_real = get_last_insn ();
21181 /* If there were any real insns between note we processed last time
21182 and this note (or if it is the first note), clear
21183 last_{,postcall_}label so that they are not reused this time. */
21184 if (last_var_location_insn == NULL_RTX
21185 || last_var_location_insn != next_real
21186 || last_in_cold_section_p != in_cold_section_p)
21188 last_label = NULL;
21189 last_postcall_label = NULL;
21192 if (var_loc_p)
21194 decl = NOTE_VAR_LOCATION_DECL (loc_note);
21195 newloc = add_var_loc_to_decl (decl, loc_note,
21196 NOTE_DURING_CALL_P (loc_note)
21197 ? last_postcall_label : last_label);
21198 if (newloc == NULL)
21199 return;
21201 else
21203 decl = NULL_TREE;
21204 newloc = NULL;
21207 /* If there were no real insns between note we processed last time
21208 and this note, use the label we emitted last time. Otherwise
21209 create a new label and emit it. */
21210 if (last_label == NULL)
21212 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
21213 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
21214 loclabel_num++;
21215 last_label = ggc_strdup (loclabel);
21216 /* See if loclabel might be equal to .Ltext0. If yes,
21217 bump first_loclabel_num_not_at_text_label. */
21218 if (!have_multiple_function_sections
21219 && in_first_function_p
21220 && maybe_at_text_label_p)
21222 static rtx last_start;
21223 rtx insn;
21224 for (insn = loc_note; insn; insn = previous_insn (insn))
21225 if (insn == last_start)
21226 break;
21227 else if (!NONDEBUG_INSN_P (insn))
21228 continue;
21229 else
21231 rtx body = PATTERN (insn);
21232 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
21233 continue;
21234 /* Inline asm could occupy zero bytes. */
21235 else if (GET_CODE (body) == ASM_INPUT
21236 || asm_noperands (body) >= 0)
21237 continue;
21238 #ifdef HAVE_attr_length
21239 else if (get_attr_min_length (insn) == 0)
21240 continue;
21241 #endif
21242 else
21244 /* Assume insn has non-zero length. */
21245 maybe_at_text_label_p = false;
21246 break;
21249 if (maybe_at_text_label_p)
21251 last_start = loc_note;
21252 first_loclabel_num_not_at_text_label = loclabel_num;
21257 if (!var_loc_p)
21259 struct call_arg_loc_node *ca_loc
21260 = ggc_alloc_cleared_call_arg_loc_node ();
21261 rtx prev = prev_real_insn (loc_note), x;
21262 ca_loc->call_arg_loc_note = loc_note;
21263 ca_loc->next = NULL;
21264 ca_loc->label = last_label;
21265 gcc_assert (prev
21266 && (CALL_P (prev)
21267 || (NONJUMP_INSN_P (prev)
21268 && GET_CODE (PATTERN (prev)) == SEQUENCE
21269 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
21270 if (!CALL_P (prev))
21271 prev = XVECEXP (PATTERN (prev), 0, 0);
21272 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
21273 x = get_call_rtx_from (PATTERN (prev));
21274 if (x)
21276 x = XEXP (XEXP (x, 0), 0);
21277 if (GET_CODE (x) == SYMBOL_REF
21278 && SYMBOL_REF_DECL (x)
21279 && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
21280 ca_loc->symbol_ref = x;
21282 ca_loc->block = insn_scope (prev);
21283 if (call_arg_locations)
21284 call_arg_loc_last->next = ca_loc;
21285 else
21286 call_arg_locations = ca_loc;
21287 call_arg_loc_last = ca_loc;
21289 else if (!NOTE_DURING_CALL_P (loc_note))
21290 newloc->label = last_label;
21291 else
21293 if (!last_postcall_label)
21295 sprintf (loclabel, "%s-1", last_label);
21296 last_postcall_label = ggc_strdup (loclabel);
21298 newloc->label = last_postcall_label;
21301 last_var_location_insn = next_real;
21302 last_in_cold_section_p = in_cold_section_p;
21305 /* Note in one location list that text section has changed. */
21307 static int
21308 var_location_switch_text_section_1 (void **slot, void *data ATTRIBUTE_UNUSED)
21310 var_loc_list *list = (var_loc_list *) *slot;
21311 if (list->first)
21312 list->last_before_switch
21313 = list->last->next ? list->last->next : list->last;
21314 return 1;
21317 /* Note in all location lists that text section has changed. */
21319 static void
21320 var_location_switch_text_section (void)
21322 if (decl_loc_table == NULL)
21323 return;
21325 htab_traverse (decl_loc_table, var_location_switch_text_section_1, NULL);
21328 /* Create a new line number table. */
21330 static dw_line_info_table *
21331 new_line_info_table (void)
21333 dw_line_info_table *table;
21335 table = ggc_alloc_cleared_dw_line_info_table_struct ();
21336 table->file_num = 1;
21337 table->line_num = 1;
21338 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
21340 return table;
21343 /* Lookup the "current" table into which we emit line info, so
21344 that we don't have to do it for every source line. */
21346 static void
21347 set_cur_line_info_table (section *sec)
21349 dw_line_info_table *table;
21351 if (sec == text_section)
21352 table = text_section_line_info;
21353 else if (sec == cold_text_section)
21355 table = cold_text_section_line_info;
21356 if (!table)
21358 cold_text_section_line_info = table = new_line_info_table ();
21359 table->end_label = cold_end_label;
21362 else
21364 const char *end_label;
21366 if (flag_reorder_blocks_and_partition)
21368 if (in_cold_section_p)
21369 end_label = crtl->subsections.cold_section_end_label;
21370 else
21371 end_label = crtl->subsections.hot_section_end_label;
21373 else
21375 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21376 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
21377 current_function_funcdef_no);
21378 end_label = ggc_strdup (label);
21381 table = new_line_info_table ();
21382 table->end_label = end_label;
21384 vec_safe_push (separate_line_info, table);
21387 if (DWARF2_ASM_LINE_DEBUG_INFO)
21388 table->is_stmt = (cur_line_info_table
21389 ? cur_line_info_table->is_stmt
21390 : DWARF_LINE_DEFAULT_IS_STMT_START);
21391 cur_line_info_table = table;
21395 /* We need to reset the locations at the beginning of each
21396 function. We can't do this in the end_function hook, because the
21397 declarations that use the locations won't have been output when
21398 that hook is called. Also compute have_multiple_function_sections here. */
21400 static void
21401 dwarf2out_begin_function (tree fun)
21403 section *sec = function_section (fun);
21405 if (sec != text_section)
21406 have_multiple_function_sections = true;
21408 if (flag_reorder_blocks_and_partition && !cold_text_section)
21410 gcc_assert (current_function_decl == fun);
21411 cold_text_section = unlikely_text_section ();
21412 switch_to_section (cold_text_section);
21413 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
21414 switch_to_section (sec);
21417 dwarf2out_note_section_used ();
21418 call_site_count = 0;
21419 tail_call_site_count = 0;
21421 set_cur_line_info_table (sec);
21424 /* Helper function of dwarf2out_end_function, called only after emitting
21425 the very first function into assembly. Check if some .debug_loc range
21426 might end with a .LVL* label that could be equal to .Ltext0.
21427 In that case we must force using absolute addresses in .debug_loc ranges,
21428 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
21429 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
21430 list terminator.
21431 Set have_multiple_function_sections to true in that case and
21432 terminate htab traversal. */
21434 static int
21435 find_empty_loc_ranges_at_text_label (void **slot, void *)
21437 var_loc_list *entry;
21438 struct var_loc_node *node;
21440 entry = (var_loc_list *) *slot;
21441 node = entry->first;
21442 if (node && node->next && node->next->label)
21444 unsigned int i;
21445 const char *label = node->next->label;
21446 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
21448 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
21450 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
21451 if (strcmp (label, loclabel) == 0)
21453 have_multiple_function_sections = true;
21454 return 0;
21458 return 1;
21461 /* Hook called after emitting a function into assembly.
21462 This does something only for the very first function emitted. */
21464 static void
21465 dwarf2out_end_function (unsigned int)
21467 if (in_first_function_p
21468 && !have_multiple_function_sections
21469 && first_loclabel_num_not_at_text_label
21470 && decl_loc_table)
21471 htab_traverse (decl_loc_table, find_empty_loc_ranges_at_text_label,
21472 NULL);
21473 in_first_function_p = false;
21474 maybe_at_text_label_p = false;
21477 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
21479 static void
21480 push_dw_line_info_entry (dw_line_info_table *table,
21481 enum dw_line_info_opcode opcode, unsigned int val)
21483 dw_line_info_entry e;
21484 e.opcode = opcode;
21485 e.val = val;
21486 vec_safe_push (table->entries, e);
21489 /* Output a label to mark the beginning of a source code line entry
21490 and record information relating to this source line, in
21491 'line_info_table' for later output of the .debug_line section. */
21492 /* ??? The discriminator parameter ought to be unsigned. */
21494 static void
21495 dwarf2out_source_line (unsigned int line, const char *filename,
21496 int discriminator, bool is_stmt)
21498 unsigned int file_num;
21499 dw_line_info_table *table;
21501 if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
21502 return;
21504 /* The discriminator column was added in dwarf4. Simplify the below
21505 by simply removing it if we're not supposed to output it. */
21506 if (dwarf_version < 4 && dwarf_strict)
21507 discriminator = 0;
21509 table = cur_line_info_table;
21510 file_num = maybe_emit_file (lookup_filename (filename));
21512 /* ??? TODO: Elide duplicate line number entries. Traditionally,
21513 the debugger has used the second (possibly duplicate) line number
21514 at the beginning of the function to mark the end of the prologue.
21515 We could eliminate any other duplicates within the function. For
21516 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
21517 that second line number entry. */
21518 /* Recall that this end-of-prologue indication is *not* the same thing
21519 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
21520 to which the hook corresponds, follows the last insn that was
21521 emitted by gen_prologue. What we need is to precede the first insn
21522 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
21523 insn that corresponds to something the user wrote. These may be
21524 very different locations once scheduling is enabled. */
21526 if (0 && file_num == table->file_num
21527 && line == table->line_num
21528 && discriminator == table->discrim_num
21529 && is_stmt == table->is_stmt)
21530 return;
21532 switch_to_section (current_function_section ());
21534 /* If requested, emit something human-readable. */
21535 if (flag_debug_asm)
21536 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
21538 if (DWARF2_ASM_LINE_DEBUG_INFO)
21540 /* Emit the .loc directive understood by GNU as. */
21541 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
21542 file_num, line, is_stmt, discriminator */
21543 fputs ("\t.loc ", asm_out_file);
21544 fprint_ul (asm_out_file, file_num);
21545 putc (' ', asm_out_file);
21546 fprint_ul (asm_out_file, line);
21547 putc (' ', asm_out_file);
21548 putc ('0', asm_out_file);
21550 if (is_stmt != table->is_stmt)
21552 fputs (" is_stmt ", asm_out_file);
21553 putc (is_stmt ? '1' : '0', asm_out_file);
21555 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
21557 gcc_assert (discriminator > 0);
21558 fputs (" discriminator ", asm_out_file);
21559 fprint_ul (asm_out_file, (unsigned long) discriminator);
21561 putc ('\n', asm_out_file);
21563 else
21565 unsigned int label_num = ++line_info_label_num;
21567 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
21569 push_dw_line_info_entry (table, LI_set_address, label_num);
21570 if (file_num != table->file_num)
21571 push_dw_line_info_entry (table, LI_set_file, file_num);
21572 if (discriminator != table->discrim_num)
21573 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
21574 if (is_stmt != table->is_stmt)
21575 push_dw_line_info_entry (table, LI_negate_stmt, 0);
21576 push_dw_line_info_entry (table, LI_set_line, line);
21579 table->file_num = file_num;
21580 table->line_num = line;
21581 table->discrim_num = discriminator;
21582 table->is_stmt = is_stmt;
21583 table->in_use = true;
21586 /* Record the beginning of a new source file. */
21588 static void
21589 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
21591 if (flag_eliminate_dwarf2_dups)
21593 /* Record the beginning of the file for break_out_includes. */
21594 dw_die_ref bincl_die;
21596 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
21597 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
21600 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21602 macinfo_entry e;
21603 e.code = DW_MACINFO_start_file;
21604 e.lineno = lineno;
21605 e.info = ggc_strdup (filename);
21606 vec_safe_push (macinfo_table, e);
21610 /* Record the end of a source file. */
21612 static void
21613 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
21615 if (flag_eliminate_dwarf2_dups)
21616 /* Record the end of the file for break_out_includes. */
21617 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
21619 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21621 macinfo_entry e;
21622 e.code = DW_MACINFO_end_file;
21623 e.lineno = lineno;
21624 e.info = NULL;
21625 vec_safe_push (macinfo_table, e);
21629 /* Called from debug_define in toplev.c. The `buffer' parameter contains
21630 the tail part of the directive line, i.e. the part which is past the
21631 initial whitespace, #, whitespace, directive-name, whitespace part. */
21633 static void
21634 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
21635 const char *buffer ATTRIBUTE_UNUSED)
21637 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21639 macinfo_entry e;
21640 /* Insert a dummy first entry to be able to optimize the whole
21641 predefined macro block using DW_MACRO_GNU_transparent_include. */
21642 if (macinfo_table->is_empty () && lineno <= 1)
21644 e.code = 0;
21645 e.lineno = 0;
21646 e.info = NULL;
21647 vec_safe_push (macinfo_table, e);
21649 e.code = DW_MACINFO_define;
21650 e.lineno = lineno;
21651 e.info = ggc_strdup (buffer);
21652 vec_safe_push (macinfo_table, e);
21656 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
21657 the tail part of the directive line, i.e. the part which is past the
21658 initial whitespace, #, whitespace, directive-name, whitespace part. */
21660 static void
21661 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
21662 const char *buffer ATTRIBUTE_UNUSED)
21664 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21666 macinfo_entry e;
21667 /* Insert a dummy first entry to be able to optimize the whole
21668 predefined macro block using DW_MACRO_GNU_transparent_include. */
21669 if (macinfo_table->is_empty () && lineno <= 1)
21671 e.code = 0;
21672 e.lineno = 0;
21673 e.info = NULL;
21674 vec_safe_push (macinfo_table, e);
21676 e.code = DW_MACINFO_undef;
21677 e.lineno = lineno;
21678 e.info = ggc_strdup (buffer);
21679 vec_safe_push (macinfo_table, e);
21683 /* Helpers to manipulate hash table of CUs. */
21685 struct macinfo_entry_hasher : typed_noop_remove <macinfo_entry>
21687 typedef macinfo_entry value_type;
21688 typedef macinfo_entry compare_type;
21689 static inline hashval_t hash (const value_type *);
21690 static inline bool equal (const value_type *, const compare_type *);
21693 inline hashval_t
21694 macinfo_entry_hasher::hash (const value_type *entry)
21696 return htab_hash_string (entry->info);
21699 inline bool
21700 macinfo_entry_hasher::equal (const value_type *entry1,
21701 const compare_type *entry2)
21703 return !strcmp (entry1->info, entry2->info);
21706 typedef hash_table <macinfo_entry_hasher> macinfo_hash_type;
21708 /* Output a single .debug_macinfo entry. */
21710 static void
21711 output_macinfo_op (macinfo_entry *ref)
21713 int file_num;
21714 size_t len;
21715 struct indirect_string_node *node;
21716 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21717 struct dwarf_file_data *fd;
21719 switch (ref->code)
21721 case DW_MACINFO_start_file:
21722 fd = lookup_filename (ref->info);
21723 file_num = maybe_emit_file (fd);
21724 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
21725 dw2_asm_output_data_uleb128 (ref->lineno,
21726 "Included from line number %lu",
21727 (unsigned long) ref->lineno);
21728 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
21729 break;
21730 case DW_MACINFO_end_file:
21731 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
21732 break;
21733 case DW_MACINFO_define:
21734 case DW_MACINFO_undef:
21735 len = strlen (ref->info) + 1;
21736 if (!dwarf_strict
21737 && len > DWARF_OFFSET_SIZE
21738 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
21739 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
21741 ref->code = ref->code == DW_MACINFO_define
21742 ? DW_MACRO_GNU_define_indirect
21743 : DW_MACRO_GNU_undef_indirect;
21744 output_macinfo_op (ref);
21745 return;
21747 dw2_asm_output_data (1, ref->code,
21748 ref->code == DW_MACINFO_define
21749 ? "Define macro" : "Undefine macro");
21750 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
21751 (unsigned long) ref->lineno);
21752 dw2_asm_output_nstring (ref->info, -1, "The macro");
21753 break;
21754 case DW_MACRO_GNU_define_indirect:
21755 case DW_MACRO_GNU_undef_indirect:
21756 node = find_AT_string (ref->info);
21757 gcc_assert (node
21758 && ((node->form == DW_FORM_strp)
21759 || (node->form == DW_FORM_GNU_str_index)));
21760 dw2_asm_output_data (1, ref->code,
21761 ref->code == DW_MACRO_GNU_define_indirect
21762 ? "Define macro indirect"
21763 : "Undefine macro indirect");
21764 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
21765 (unsigned long) ref->lineno);
21766 if (node->form == DW_FORM_strp)
21767 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
21768 debug_str_section, "The macro: \"%s\"",
21769 ref->info);
21770 else
21771 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
21772 ref->info);
21773 break;
21774 case DW_MACRO_GNU_transparent_include:
21775 dw2_asm_output_data (1, ref->code, "Transparent include");
21776 ASM_GENERATE_INTERNAL_LABEL (label,
21777 DEBUG_MACRO_SECTION_LABEL, ref->lineno);
21778 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
21779 break;
21780 default:
21781 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
21782 ASM_COMMENT_START, (unsigned long) ref->code);
21783 break;
21787 /* Attempt to make a sequence of define/undef macinfo ops shareable with
21788 other compilation unit .debug_macinfo sections. IDX is the first
21789 index of a define/undef, return the number of ops that should be
21790 emitted in a comdat .debug_macinfo section and emit
21791 a DW_MACRO_GNU_transparent_include entry referencing it.
21792 If the define/undef entry should be emitted normally, return 0. */
21794 static unsigned
21795 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
21796 macinfo_hash_type *macinfo_htab)
21798 macinfo_entry *first, *second, *cur, *inc;
21799 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
21800 unsigned char checksum[16];
21801 struct md5_ctx ctx;
21802 char *grp_name, *tail;
21803 const char *base;
21804 unsigned int i, count, encoded_filename_len, linebuf_len;
21805 macinfo_entry **slot;
21807 first = &(*macinfo_table)[idx];
21808 second = &(*macinfo_table)[idx + 1];
21810 /* Optimize only if there are at least two consecutive define/undef ops,
21811 and either all of them are before first DW_MACINFO_start_file
21812 with lineno {0,1} (i.e. predefined macro block), or all of them are
21813 in some included header file. */
21814 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
21815 return 0;
21816 if (vec_safe_is_empty (files))
21818 if (first->lineno > 1 || second->lineno > 1)
21819 return 0;
21821 else if (first->lineno == 0)
21822 return 0;
21824 /* Find the last define/undef entry that can be grouped together
21825 with first and at the same time compute md5 checksum of their
21826 codes, linenumbers and strings. */
21827 md5_init_ctx (&ctx);
21828 for (i = idx; macinfo_table->iterate (i, &cur); i++)
21829 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
21830 break;
21831 else if (vec_safe_is_empty (files) && cur->lineno > 1)
21832 break;
21833 else
21835 unsigned char code = cur->code;
21836 md5_process_bytes (&code, 1, &ctx);
21837 checksum_uleb128 (cur->lineno, &ctx);
21838 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
21840 md5_finish_ctx (&ctx, checksum);
21841 count = i - idx;
21843 /* From the containing include filename (if any) pick up just
21844 usable characters from its basename. */
21845 if (vec_safe_is_empty (files))
21846 base = "";
21847 else
21848 base = lbasename (files->last ().info);
21849 for (encoded_filename_len = 0, i = 0; base[i]; i++)
21850 if (ISIDNUM (base[i]) || base[i] == '.')
21851 encoded_filename_len++;
21852 /* Count . at the end. */
21853 if (encoded_filename_len)
21854 encoded_filename_len++;
21856 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
21857 linebuf_len = strlen (linebuf);
21859 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
21860 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
21861 + 16 * 2 + 1);
21862 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
21863 tail = grp_name + 4;
21864 if (encoded_filename_len)
21866 for (i = 0; base[i]; i++)
21867 if (ISIDNUM (base[i]) || base[i] == '.')
21868 *tail++ = base[i];
21869 *tail++ = '.';
21871 memcpy (tail, linebuf, linebuf_len);
21872 tail += linebuf_len;
21873 *tail++ = '.';
21874 for (i = 0; i < 16; i++)
21875 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
21877 /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
21878 in the empty vector entry before the first define/undef. */
21879 inc = &(*macinfo_table)[idx - 1];
21880 inc->code = DW_MACRO_GNU_transparent_include;
21881 inc->lineno = 0;
21882 inc->info = ggc_strdup (grp_name);
21883 if (!macinfo_htab->is_created ())
21884 macinfo_htab->create (10);
21885 /* Avoid emitting duplicates. */
21886 slot = macinfo_htab->find_slot (inc, INSERT);
21887 if (*slot != NULL)
21889 inc->code = 0;
21890 inc->info = NULL;
21891 /* If such an entry has been used before, just emit
21892 a DW_MACRO_GNU_transparent_include op. */
21893 inc = *slot;
21894 output_macinfo_op (inc);
21895 /* And clear all macinfo_entry in the range to avoid emitting them
21896 in the second pass. */
21897 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
21899 cur->code = 0;
21900 cur->info = NULL;
21903 else
21905 *slot = inc;
21906 inc->lineno = macinfo_htab->elements ();
21907 output_macinfo_op (inc);
21909 return count;
21912 /* Save any strings needed by the macinfo table in the debug str
21913 table. All strings must be collected into the table by the time
21914 index_string is called. */
21916 static void
21917 save_macinfo_strings (void)
21919 unsigned len;
21920 unsigned i;
21921 macinfo_entry *ref;
21923 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
21925 switch (ref->code)
21927 /* Match the logic in output_macinfo_op to decide on
21928 indirect strings. */
21929 case DW_MACINFO_define:
21930 case DW_MACINFO_undef:
21931 len = strlen (ref->info) + 1;
21932 if (!dwarf_strict
21933 && len > DWARF_OFFSET_SIZE
21934 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
21935 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
21936 set_indirect_string (find_AT_string (ref->info));
21937 break;
21938 case DW_MACRO_GNU_define_indirect:
21939 case DW_MACRO_GNU_undef_indirect:
21940 set_indirect_string (find_AT_string (ref->info));
21941 break;
21942 default:
21943 break;
21948 /* Output macinfo section(s). */
21950 static void
21951 output_macinfo (void)
21953 unsigned i;
21954 unsigned long length = vec_safe_length (macinfo_table);
21955 macinfo_entry *ref;
21956 vec<macinfo_entry, va_gc> *files = NULL;
21957 macinfo_hash_type macinfo_htab;
21959 if (! length)
21960 return;
21962 /* output_macinfo* uses these interchangeably. */
21963 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
21964 && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
21965 && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
21966 && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
21968 /* For .debug_macro emit the section header. */
21969 if (!dwarf_strict)
21971 dw2_asm_output_data (2, 4, "DWARF macro version number");
21972 if (DWARF_OFFSET_SIZE == 8)
21973 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
21974 else
21975 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
21976 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
21977 (!dwarf_split_debug_info ? debug_line_section_label
21978 : debug_skeleton_line_section_label),
21979 debug_line_section, NULL);
21982 /* In the first loop, it emits the primary .debug_macinfo section
21983 and after each emitted op the macinfo_entry is cleared.
21984 If a longer range of define/undef ops can be optimized using
21985 DW_MACRO_GNU_transparent_include, the
21986 DW_MACRO_GNU_transparent_include op is emitted and kept in
21987 the vector before the first define/undef in the range and the
21988 whole range of define/undef ops is not emitted and kept. */
21989 for (i = 0; macinfo_table->iterate (i, &ref); i++)
21991 switch (ref->code)
21993 case DW_MACINFO_start_file:
21994 vec_safe_push (files, *ref);
21995 break;
21996 case DW_MACINFO_end_file:
21997 if (!vec_safe_is_empty (files))
21998 files->pop ();
21999 break;
22000 case DW_MACINFO_define:
22001 case DW_MACINFO_undef:
22002 if (!dwarf_strict
22003 && HAVE_COMDAT_GROUP
22004 && vec_safe_length (files) != 1
22005 && i > 0
22006 && i + 1 < length
22007 && (*macinfo_table)[i - 1].code == 0)
22009 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
22010 if (count)
22012 i += count - 1;
22013 continue;
22016 break;
22017 case 0:
22018 /* A dummy entry may be inserted at the beginning to be able
22019 to optimize the whole block of predefined macros. */
22020 if (i == 0)
22021 continue;
22022 default:
22023 break;
22025 output_macinfo_op (ref);
22026 ref->info = NULL;
22027 ref->code = 0;
22030 if (!macinfo_htab.is_created ())
22031 return;
22033 macinfo_htab.dispose ();
22035 /* If any DW_MACRO_GNU_transparent_include were used, on those
22036 DW_MACRO_GNU_transparent_include entries terminate the
22037 current chain and switch to a new comdat .debug_macinfo
22038 section and emit the define/undef entries within it. */
22039 for (i = 0; macinfo_table->iterate (i, &ref); i++)
22040 switch (ref->code)
22042 case 0:
22043 continue;
22044 case DW_MACRO_GNU_transparent_include:
22046 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22047 tree comdat_key = get_identifier (ref->info);
22048 /* Terminate the previous .debug_macinfo section. */
22049 dw2_asm_output_data (1, 0, "End compilation unit");
22050 targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
22051 SECTION_DEBUG
22052 | SECTION_LINKONCE,
22053 comdat_key);
22054 ASM_GENERATE_INTERNAL_LABEL (label,
22055 DEBUG_MACRO_SECTION_LABEL,
22056 ref->lineno);
22057 ASM_OUTPUT_LABEL (asm_out_file, label);
22058 ref->code = 0;
22059 ref->info = NULL;
22060 dw2_asm_output_data (2, 4, "DWARF macro version number");
22061 if (DWARF_OFFSET_SIZE == 8)
22062 dw2_asm_output_data (1, 1, "Flags: 64-bit");
22063 else
22064 dw2_asm_output_data (1, 0, "Flags: 32-bit");
22066 break;
22067 case DW_MACINFO_define:
22068 case DW_MACINFO_undef:
22069 output_macinfo_op (ref);
22070 ref->code = 0;
22071 ref->info = NULL;
22072 break;
22073 default:
22074 gcc_unreachable ();
22078 /* Set up for Dwarf output at the start of compilation. */
22080 static void
22081 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
22083 /* Allocate the file_table. */
22084 file_table = htab_create_ggc (50, file_table_hash,
22085 file_table_eq, NULL);
22087 /* Allocate the decl_die_table. */
22088 decl_die_table = htab_create_ggc (10, decl_die_table_hash,
22089 decl_die_table_eq, NULL);
22091 /* Allocate the decl_loc_table. */
22092 decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
22093 decl_loc_table_eq, NULL);
22095 /* Allocate the cached_dw_loc_list_table. */
22096 cached_dw_loc_list_table
22097 = htab_create_ggc (10, cached_dw_loc_list_table_hash,
22098 cached_dw_loc_list_table_eq, NULL);
22100 /* Allocate the initial hunk of the decl_scope_table. */
22101 vec_alloc (decl_scope_table, 256);
22103 /* Allocate the initial hunk of the abbrev_die_table. */
22104 abbrev_die_table = ggc_alloc_cleared_vec_dw_die_ref
22105 (ABBREV_DIE_TABLE_INCREMENT);
22106 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
22107 /* Zero-th entry is allocated, but unused. */
22108 abbrev_die_table_in_use = 1;
22110 /* Allocate the pubtypes and pubnames vectors. */
22111 vec_alloc (pubname_table, 32);
22112 vec_alloc (pubtype_table, 32);
22114 vec_alloc (incomplete_types, 64);
22116 vec_alloc (used_rtx_array, 32);
22118 if (!dwarf_split_debug_info)
22120 debug_info_section = get_section (DEBUG_INFO_SECTION,
22121 SECTION_DEBUG, NULL);
22122 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22123 SECTION_DEBUG, NULL);
22124 debug_loc_section = get_section (DEBUG_LOC_SECTION,
22125 SECTION_DEBUG, NULL);
22127 else
22129 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
22130 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22131 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
22132 SECTION_DEBUG | SECTION_EXCLUDE,
22133 NULL);
22134 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
22135 SECTION_DEBUG, NULL);
22136 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
22137 SECTION_DEBUG, NULL);
22138 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22139 SECTION_DEBUG, NULL);
22140 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
22141 DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
22143 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
22144 the main .o, but the skeleton_line goes into the split off dwo. */
22145 debug_skeleton_line_section
22146 = get_section (DEBUG_DWO_LINE_SECTION,
22147 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22148 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
22149 DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
22150 debug_str_offsets_section = get_section (DEBUG_STR_OFFSETS_SECTION,
22151 SECTION_DEBUG | SECTION_EXCLUDE,
22152 NULL);
22153 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
22154 DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
22155 debug_loc_section = get_section (DEBUG_DWO_LOC_SECTION,
22156 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22157 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
22158 DEBUG_STR_DWO_SECTION_FLAGS, NULL);
22160 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
22161 SECTION_DEBUG, NULL);
22162 debug_macinfo_section = get_section (dwarf_strict
22163 ? DEBUG_MACINFO_SECTION
22164 : DEBUG_MACRO_SECTION,
22165 DEBUG_MACRO_SECTION_FLAGS, NULL);
22166 debug_line_section = get_section (DEBUG_LINE_SECTION,
22167 SECTION_DEBUG, NULL);
22168 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
22169 SECTION_DEBUG, NULL);
22170 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
22171 SECTION_DEBUG, NULL);
22172 debug_str_section = get_section (DEBUG_STR_SECTION,
22173 DEBUG_STR_SECTION_FLAGS, NULL);
22174 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
22175 SECTION_DEBUG, NULL);
22176 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
22177 SECTION_DEBUG, NULL);
22179 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
22180 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
22181 DEBUG_ABBREV_SECTION_LABEL, 0);
22182 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
22183 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
22184 COLD_TEXT_SECTION_LABEL, 0);
22185 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
22187 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
22188 DEBUG_INFO_SECTION_LABEL, 0);
22189 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
22190 DEBUG_LINE_SECTION_LABEL, 0);
22191 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
22192 DEBUG_RANGES_SECTION_LABEL, 0);
22193 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
22194 DEBUG_ADDR_SECTION_LABEL, 0);
22195 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
22196 dwarf_strict
22197 ? DEBUG_MACINFO_SECTION_LABEL
22198 : DEBUG_MACRO_SECTION_LABEL, 0);
22199 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
22201 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22202 vec_alloc (macinfo_table, 64);
22204 switch_to_section (text_section);
22205 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
22207 /* Make sure the line number table for .text always exists. */
22208 text_section_line_info = new_line_info_table ();
22209 text_section_line_info->end_label = text_end_label;
22212 /* Called before compile () starts outputtting functions, variables
22213 and toplevel asms into assembly. */
22215 static void
22216 dwarf2out_assembly_start (void)
22218 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
22219 && dwarf2out_do_cfi_asm ()
22220 && (!(flag_unwind_tables || flag_exceptions)
22221 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
22222 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
22225 /* A helper function for dwarf2out_finish called through
22226 htab_traverse. Assign a string its index. All strings must be
22227 collected into the table by the time index_string is called,
22228 because the indexing code relies on htab_traverse to traverse nodes
22229 in the same order for each run. */
22231 static int
22232 index_string (void **h, void *v)
22234 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22235 unsigned int *index = (unsigned int *) v;
22237 find_string_form (node);
22238 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22240 gcc_assert (node->index == NO_INDEX_ASSIGNED);
22241 node->index = *index;
22242 *index += 1;
22244 return 1;
22247 /* A helper function for output_indirect_strings called through
22248 htab_traverse. Output the offset to a string and update the
22249 current offset. */
22251 static int
22252 output_index_string_offset (void **h, void *v)
22254 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22255 unsigned int *offset = (unsigned int *) v;
22257 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22259 /* Assert that this node has been assigned an index. */
22260 gcc_assert (node->index != NO_INDEX_ASSIGNED
22261 && node->index != NOT_INDEXED);
22262 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
22263 "indexed string 0x%x: %s", node->index, node->str);
22264 *offset += strlen (node->str) + 1;
22266 return 1;
22269 /* A helper function for dwarf2out_finish called through
22270 htab_traverse. Output the indexed string. */
22272 static int
22273 output_index_string (void **h, void *v)
22275 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22276 unsigned int *cur_idx = (unsigned int *) v;
22278 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22280 /* Assert that the strings are output in the same order as their
22281 indexes were assigned. */
22282 gcc_assert (*cur_idx == node->index);
22283 assemble_string (node->str, strlen (node->str) + 1);
22284 *cur_idx += 1;
22286 return 1;
22289 /* A helper function for dwarf2out_finish called through
22290 htab_traverse. Emit one queued .debug_str string. */
22292 static int
22293 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
22295 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22297 node->form = find_string_form (node);
22298 if (node->form == DW_FORM_strp && node->refcount > 0)
22300 ASM_OUTPUT_LABEL (asm_out_file, node->label);
22301 assemble_string (node->str, strlen (node->str) + 1);
22304 return 1;
22307 /* Output the indexed string table. */
22309 static void
22310 output_indirect_strings (void)
22312 switch_to_section (debug_str_section);
22313 if (!dwarf_split_debug_info)
22314 htab_traverse (debug_str_hash, output_indirect_string, NULL);
22315 else
22317 unsigned int offset = 0;
22318 unsigned int cur_idx = 0;
22320 htab_traverse (skeleton_debug_str_hash, output_indirect_string, NULL);
22322 switch_to_section (debug_str_offsets_section);
22323 htab_traverse_noresize (debug_str_hash,
22324 output_index_string_offset,
22325 &offset);
22326 switch_to_section (debug_str_dwo_section);
22327 htab_traverse_noresize (debug_str_hash,
22328 output_index_string,
22329 &cur_idx);
22333 /* Callback for htab_traverse to assign an index to an entry in the
22334 table, and to write that entry to the .debug_addr section. */
22336 static int
22337 output_addr_table_entry (void **slot, void *data)
22339 addr_table_entry *entry = (addr_table_entry *) *slot;
22340 unsigned int *cur_index = (unsigned int *)data;
22342 if (entry->refcount == 0)
22344 gcc_assert (entry->index == NO_INDEX_ASSIGNED
22345 || entry->index == NOT_INDEXED);
22346 return 1;
22349 gcc_assert (entry->index == *cur_index);
22350 (*cur_index)++;
22352 switch (entry->kind)
22354 case ate_kind_rtx:
22355 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
22356 "0x%x", entry->index);
22357 break;
22358 case ate_kind_rtx_dtprel:
22359 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
22360 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
22361 DWARF2_ADDR_SIZE,
22362 entry->addr.rtl);
22363 fputc ('\n', asm_out_file);
22364 break;
22365 case ate_kind_label:
22366 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
22367 "0x%x", entry->index);
22368 break;
22369 default:
22370 gcc_unreachable ();
22372 return 1;
22375 /* Produce the .debug_addr section. */
22377 static void
22378 output_addr_table (void)
22380 unsigned int index = 0;
22381 if (addr_index_table == NULL || htab_size (addr_index_table) == 0)
22382 return;
22384 switch_to_section (debug_addr_section);
22385 htab_traverse_noresize (addr_index_table, output_addr_table_entry, &index);
22388 #if ENABLE_ASSERT_CHECKING
22389 /* Verify that all marks are clear. */
22391 static void
22392 verify_marks_clear (dw_die_ref die)
22394 dw_die_ref c;
22396 gcc_assert (! die->die_mark);
22397 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
22399 #endif /* ENABLE_ASSERT_CHECKING */
22401 /* Clear the marks for a die and its children.
22402 Be cool if the mark isn't set. */
22404 static void
22405 prune_unmark_dies (dw_die_ref die)
22407 dw_die_ref c;
22409 if (die->die_mark)
22410 die->die_mark = 0;
22411 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
22414 /* Given DIE that we're marking as used, find any other dies
22415 it references as attributes and mark them as used. */
22417 static void
22418 prune_unused_types_walk_attribs (dw_die_ref die)
22420 dw_attr_ref a;
22421 unsigned ix;
22423 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22425 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
22427 /* A reference to another DIE.
22428 Make sure that it will get emitted.
22429 If it was broken out into a comdat group, don't follow it. */
22430 if (! AT_ref (a)->comdat_type_p
22431 || a->dw_attr == DW_AT_specification)
22432 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
22434 /* Set the string's refcount to 0 so that prune_unused_types_mark
22435 accounts properly for it. */
22436 if (AT_class (a) == dw_val_class_str)
22437 a->dw_attr_val.v.val_str->refcount = 0;
22441 /* Mark the generic parameters and arguments children DIEs of DIE. */
22443 static void
22444 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
22446 dw_die_ref c;
22448 if (die == NULL || die->die_child == NULL)
22449 return;
22450 c = die->die_child;
22453 if (is_template_parameter (c))
22454 prune_unused_types_mark (c, 1);
22455 c = c->die_sib;
22456 } while (c && c != die->die_child);
22459 /* Mark DIE as being used. If DOKIDS is true, then walk down
22460 to DIE's children. */
22462 static void
22463 prune_unused_types_mark (dw_die_ref die, int dokids)
22465 dw_die_ref c;
22467 if (die->die_mark == 0)
22469 /* We haven't done this node yet. Mark it as used. */
22470 die->die_mark = 1;
22471 /* If this is the DIE of a generic type instantiation,
22472 mark the children DIEs that describe its generic parms and
22473 args. */
22474 prune_unused_types_mark_generic_parms_dies (die);
22476 /* We also have to mark its parents as used.
22477 (But we don't want to mark our parent's kids due to this,
22478 unless it is a class.) */
22479 if (die->die_parent)
22480 prune_unused_types_mark (die->die_parent,
22481 class_scope_p (die->die_parent));
22483 /* Mark any referenced nodes. */
22484 prune_unused_types_walk_attribs (die);
22486 /* If this node is a specification,
22487 also mark the definition, if it exists. */
22488 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
22489 prune_unused_types_mark (die->die_definition, 1);
22492 if (dokids && die->die_mark != 2)
22494 /* We need to walk the children, but haven't done so yet.
22495 Remember that we've walked the kids. */
22496 die->die_mark = 2;
22498 /* If this is an array type, we need to make sure our
22499 kids get marked, even if they're types. If we're
22500 breaking out types into comdat sections, do this
22501 for all type definitions. */
22502 if (die->die_tag == DW_TAG_array_type
22503 || (use_debug_types
22504 && is_type_die (die) && ! is_declaration_die (die)))
22505 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
22506 else
22507 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22511 /* For local classes, look if any static member functions were emitted
22512 and if so, mark them. */
22514 static void
22515 prune_unused_types_walk_local_classes (dw_die_ref die)
22517 dw_die_ref c;
22519 if (die->die_mark == 2)
22520 return;
22522 switch (die->die_tag)
22524 case DW_TAG_structure_type:
22525 case DW_TAG_union_type:
22526 case DW_TAG_class_type:
22527 break;
22529 case DW_TAG_subprogram:
22530 if (!get_AT_flag (die, DW_AT_declaration)
22531 || die->die_definition != NULL)
22532 prune_unused_types_mark (die, 1);
22533 return;
22535 default:
22536 return;
22539 /* Mark children. */
22540 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
22543 /* Walk the tree DIE and mark types that we actually use. */
22545 static void
22546 prune_unused_types_walk (dw_die_ref die)
22548 dw_die_ref c;
22550 /* Don't do anything if this node is already marked and
22551 children have been marked as well. */
22552 if (die->die_mark == 2)
22553 return;
22555 switch (die->die_tag)
22557 case DW_TAG_structure_type:
22558 case DW_TAG_union_type:
22559 case DW_TAG_class_type:
22560 if (die->die_perennial_p)
22561 break;
22563 for (c = die->die_parent; c; c = c->die_parent)
22564 if (c->die_tag == DW_TAG_subprogram)
22565 break;
22567 /* Finding used static member functions inside of classes
22568 is needed just for local classes, because for other classes
22569 static member function DIEs with DW_AT_specification
22570 are emitted outside of the DW_TAG_*_type. If we ever change
22571 it, we'd need to call this even for non-local classes. */
22572 if (c)
22573 prune_unused_types_walk_local_classes (die);
22575 /* It's a type node --- don't mark it. */
22576 return;
22578 case DW_TAG_const_type:
22579 case DW_TAG_packed_type:
22580 case DW_TAG_pointer_type:
22581 case DW_TAG_reference_type:
22582 case DW_TAG_rvalue_reference_type:
22583 case DW_TAG_volatile_type:
22584 case DW_TAG_typedef:
22585 case DW_TAG_array_type:
22586 case DW_TAG_interface_type:
22587 case DW_TAG_friend:
22588 case DW_TAG_variant_part:
22589 case DW_TAG_enumeration_type:
22590 case DW_TAG_subroutine_type:
22591 case DW_TAG_string_type:
22592 case DW_TAG_set_type:
22593 case DW_TAG_subrange_type:
22594 case DW_TAG_ptr_to_member_type:
22595 case DW_TAG_file_type:
22596 if (die->die_perennial_p)
22597 break;
22599 /* It's a type node --- don't mark it. */
22600 return;
22602 default:
22603 /* Mark everything else. */
22604 break;
22607 if (die->die_mark == 0)
22609 die->die_mark = 1;
22611 /* Now, mark any dies referenced from here. */
22612 prune_unused_types_walk_attribs (die);
22615 die->die_mark = 2;
22617 /* Mark children. */
22618 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22621 /* Increment the string counts on strings referred to from DIE's
22622 attributes. */
22624 static void
22625 prune_unused_types_update_strings (dw_die_ref die)
22627 dw_attr_ref a;
22628 unsigned ix;
22630 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22631 if (AT_class (a) == dw_val_class_str)
22633 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
22634 s->refcount++;
22635 /* Avoid unnecessarily putting strings that are used less than
22636 twice in the hash table. */
22637 if (s->refcount
22638 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
22640 void ** slot;
22641 slot = htab_find_slot_with_hash (debug_str_hash, s->str,
22642 htab_hash_string (s->str),
22643 INSERT);
22644 gcc_assert (*slot == NULL);
22645 *slot = s;
22650 /* Remove from the tree DIE any dies that aren't marked. */
22652 static void
22653 prune_unused_types_prune (dw_die_ref die)
22655 dw_die_ref c;
22657 gcc_assert (die->die_mark);
22658 prune_unused_types_update_strings (die);
22660 if (! die->die_child)
22661 return;
22663 c = die->die_child;
22664 do {
22665 dw_die_ref prev = c;
22666 for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
22667 if (c == die->die_child)
22669 /* No marked children between 'prev' and the end of the list. */
22670 if (prev == c)
22671 /* No marked children at all. */
22672 die->die_child = NULL;
22673 else
22675 prev->die_sib = c->die_sib;
22676 die->die_child = prev;
22678 return;
22681 if (c != prev->die_sib)
22682 prev->die_sib = c;
22683 prune_unused_types_prune (c);
22684 } while (c != die->die_child);
22687 /* Remove dies representing declarations that we never use. */
22689 static void
22690 prune_unused_types (void)
22692 unsigned int i;
22693 limbo_die_node *node;
22694 comdat_type_node *ctnode;
22695 pubname_ref pub;
22696 dw_die_ref base_type;
22698 #if ENABLE_ASSERT_CHECKING
22699 /* All the marks should already be clear. */
22700 verify_marks_clear (comp_unit_die ());
22701 for (node = limbo_die_list; node; node = node->next)
22702 verify_marks_clear (node->die);
22703 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22704 verify_marks_clear (ctnode->root_die);
22705 #endif /* ENABLE_ASSERT_CHECKING */
22707 /* Mark types that are used in global variables. */
22708 premark_types_used_by_global_vars ();
22710 /* Set the mark on nodes that are actually used. */
22711 prune_unused_types_walk (comp_unit_die ());
22712 for (node = limbo_die_list; node; node = node->next)
22713 prune_unused_types_walk (node->die);
22714 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22716 prune_unused_types_walk (ctnode->root_die);
22717 prune_unused_types_mark (ctnode->type_die, 1);
22720 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
22721 are unusual in that they are pubnames that are the children of pubtypes.
22722 They should only be marked via their parent DW_TAG_enumeration_type die,
22723 not as roots in themselves. */
22724 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
22725 if (pub->die->die_tag != DW_TAG_enumerator)
22726 prune_unused_types_mark (pub->die, 1);
22727 for (i = 0; base_types.iterate (i, &base_type); i++)
22728 prune_unused_types_mark (base_type, 1);
22730 if (debug_str_hash)
22731 htab_empty (debug_str_hash);
22732 if (skeleton_debug_str_hash)
22733 htab_empty (skeleton_debug_str_hash);
22734 prune_unused_types_prune (comp_unit_die ());
22735 for (node = limbo_die_list; node; node = node->next)
22736 prune_unused_types_prune (node->die);
22737 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22738 prune_unused_types_prune (ctnode->root_die);
22740 /* Leave the marks clear. */
22741 prune_unmark_dies (comp_unit_die ());
22742 for (node = limbo_die_list; node; node = node->next)
22743 prune_unmark_dies (node->die);
22744 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22745 prune_unmark_dies (ctnode->root_die);
22748 /* Set the parameter to true if there are any relative pathnames in
22749 the file table. */
22750 static int
22751 file_table_relative_p (void ** slot, void *param)
22753 bool *p = (bool *) param;
22754 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
22755 if (!IS_ABSOLUTE_PATH (d->filename))
22757 *p = true;
22758 return 0;
22760 return 1;
22763 /* Helpers to manipulate hash table of comdat type units. */
22765 struct comdat_type_hasher : typed_noop_remove <comdat_type_node>
22767 typedef comdat_type_node value_type;
22768 typedef comdat_type_node compare_type;
22769 static inline hashval_t hash (const value_type *);
22770 static inline bool equal (const value_type *, const compare_type *);
22773 inline hashval_t
22774 comdat_type_hasher::hash (const value_type *type_node)
22776 hashval_t h;
22777 memcpy (&h, type_node->signature, sizeof (h));
22778 return h;
22781 inline bool
22782 comdat_type_hasher::equal (const value_type *type_node_1,
22783 const compare_type *type_node_2)
22785 return (! memcmp (type_node_1->signature, type_node_2->signature,
22786 DWARF_TYPE_SIGNATURE_SIZE));
22789 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
22790 to the location it would have been added, should we know its
22791 DECL_ASSEMBLER_NAME when we added other attributes. This will
22792 probably improve compactness of debug info, removing equivalent
22793 abbrevs, and hide any differences caused by deferring the
22794 computation of the assembler name, triggered by e.g. PCH. */
22796 static inline void
22797 move_linkage_attr (dw_die_ref die)
22799 unsigned ix = vec_safe_length (die->die_attr);
22800 dw_attr_node linkage = (*die->die_attr)[ix - 1];
22802 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
22803 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
22805 while (--ix > 0)
22807 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
22809 if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
22810 break;
22813 if (ix != vec_safe_length (die->die_attr) - 1)
22815 die->die_attr->pop ();
22816 die->die_attr->quick_insert (ix, linkage);
22820 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
22821 referenced from typed stack ops and count how often they are used. */
22823 static void
22824 mark_base_types (dw_loc_descr_ref loc)
22826 dw_die_ref base_type = NULL;
22828 for (; loc; loc = loc->dw_loc_next)
22830 switch (loc->dw_loc_opc)
22832 case DW_OP_GNU_regval_type:
22833 case DW_OP_GNU_deref_type:
22834 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
22835 break;
22836 case DW_OP_GNU_convert:
22837 case DW_OP_GNU_reinterpret:
22838 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
22839 continue;
22840 /* FALLTHRU */
22841 case DW_OP_GNU_const_type:
22842 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
22843 break;
22844 case DW_OP_GNU_entry_value:
22845 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
22846 continue;
22847 default:
22848 continue;
22850 gcc_assert (base_type->die_parent == comp_unit_die ());
22851 if (base_type->die_mark)
22852 base_type->die_mark++;
22853 else
22855 base_types.safe_push (base_type);
22856 base_type->die_mark = 1;
22861 /* Comparison function for sorting marked base types. */
22863 static int
22864 base_type_cmp (const void *x, const void *y)
22866 dw_die_ref dx = *(const dw_die_ref *) x;
22867 dw_die_ref dy = *(const dw_die_ref *) y;
22868 unsigned int byte_size1, byte_size2;
22869 unsigned int encoding1, encoding2;
22870 if (dx->die_mark > dy->die_mark)
22871 return -1;
22872 if (dx->die_mark < dy->die_mark)
22873 return 1;
22874 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
22875 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
22876 if (byte_size1 < byte_size2)
22877 return 1;
22878 if (byte_size1 > byte_size2)
22879 return -1;
22880 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
22881 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
22882 if (encoding1 < encoding2)
22883 return 1;
22884 if (encoding1 > encoding2)
22885 return -1;
22886 return 0;
22889 /* Move base types marked by mark_base_types as early as possible
22890 in the CU, sorted by decreasing usage count both to make the
22891 uleb128 references as small as possible and to make sure they
22892 will have die_offset already computed by calc_die_sizes when
22893 sizes of typed stack loc ops is computed. */
22895 static void
22896 move_marked_base_types (void)
22898 unsigned int i;
22899 dw_die_ref base_type, die, c;
22901 if (base_types.is_empty ())
22902 return;
22904 /* Sort by decreasing usage count, they will be added again in that
22905 order later on. */
22906 base_types.qsort (base_type_cmp);
22907 die = comp_unit_die ();
22908 c = die->die_child;
22911 dw_die_ref prev = c;
22912 c = c->die_sib;
22913 while (c->die_mark)
22915 remove_child_with_prev (c, prev);
22916 /* As base types got marked, there must be at least
22917 one node other than DW_TAG_base_type. */
22918 gcc_assert (c != c->die_sib);
22919 c = c->die_sib;
22922 while (c != die->die_child);
22923 gcc_assert (die->die_child);
22924 c = die->die_child;
22925 for (i = 0; base_types.iterate (i, &base_type); i++)
22927 base_type->die_mark = 0;
22928 base_type->die_sib = c->die_sib;
22929 c->die_sib = base_type;
22930 c = base_type;
22934 /* Helper function for resolve_addr, attempt to resolve
22935 one CONST_STRING, return non-zero if not successful. Similarly verify that
22936 SYMBOL_REFs refer to variables emitted in the current CU. */
22938 static int
22939 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
22941 rtx rtl = *addr;
22943 if (GET_CODE (rtl) == CONST_STRING)
22945 size_t len = strlen (XSTR (rtl, 0)) + 1;
22946 tree t = build_string (len, XSTR (rtl, 0));
22947 tree tlen = size_int (len - 1);
22948 TREE_TYPE (t)
22949 = build_array_type (char_type_node, build_index_type (tlen));
22950 rtl = lookup_constant_def (t);
22951 if (!rtl || !MEM_P (rtl))
22952 return 1;
22953 rtl = XEXP (rtl, 0);
22954 if (GET_CODE (rtl) == SYMBOL_REF
22955 && SYMBOL_REF_DECL (rtl)
22956 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
22957 return 1;
22958 vec_safe_push (used_rtx_array, rtl);
22959 *addr = rtl;
22960 return 0;
22963 if (GET_CODE (rtl) == SYMBOL_REF
22964 && SYMBOL_REF_DECL (rtl))
22966 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
22968 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
22969 return 1;
22971 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
22972 return 1;
22975 if (GET_CODE (rtl) == CONST
22976 && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
22977 return 1;
22979 return 0;
22982 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
22983 if possible, and create DW_TAG_dwarf_procedure that can be referenced
22984 from DW_OP_GNU_implicit_pointer if the string hasn't been seen yet. */
22986 static rtx
22987 string_cst_pool_decl (tree t)
22989 rtx rtl = output_constant_def (t, 1);
22990 unsigned char *array;
22991 dw_loc_descr_ref l;
22992 tree decl;
22993 size_t len;
22994 dw_die_ref ref;
22996 if (!rtl || !MEM_P (rtl))
22997 return NULL_RTX;
22998 rtl = XEXP (rtl, 0);
22999 if (GET_CODE (rtl) != SYMBOL_REF
23000 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
23001 return NULL_RTX;
23003 decl = SYMBOL_REF_DECL (rtl);
23004 if (!lookup_decl_die (decl))
23006 len = TREE_STRING_LENGTH (t);
23007 vec_safe_push (used_rtx_array, rtl);
23008 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
23009 array = (unsigned char *) ggc_alloc_atomic (len);
23010 memcpy (array, TREE_STRING_POINTER (t), len);
23011 l = new_loc_descr (DW_OP_implicit_value, len, 0);
23012 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
23013 l->dw_loc_oprnd2.v.val_vec.length = len;
23014 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
23015 l->dw_loc_oprnd2.v.val_vec.array = array;
23016 add_AT_loc (ref, DW_AT_location, l);
23017 equate_decl_number_to_die (decl, ref);
23019 return rtl;
23022 /* Helper function of resolve_addr_in_expr. LOC is
23023 a DW_OP_addr followed by DW_OP_stack_value, either at the start
23024 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
23025 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
23026 with DW_OP_GNU_implicit_pointer if possible
23027 and return true, if unsuccessful, return false. */
23029 static bool
23030 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
23032 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
23033 HOST_WIDE_INT offset = 0;
23034 dw_die_ref ref = NULL;
23035 tree decl;
23037 if (GET_CODE (rtl) == CONST
23038 && GET_CODE (XEXP (rtl, 0)) == PLUS
23039 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
23041 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
23042 rtl = XEXP (XEXP (rtl, 0), 0);
23044 if (GET_CODE (rtl) == CONST_STRING)
23046 size_t len = strlen (XSTR (rtl, 0)) + 1;
23047 tree t = build_string (len, XSTR (rtl, 0));
23048 tree tlen = size_int (len - 1);
23050 TREE_TYPE (t)
23051 = build_array_type (char_type_node, build_index_type (tlen));
23052 rtl = string_cst_pool_decl (t);
23053 if (!rtl)
23054 return false;
23056 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
23058 decl = SYMBOL_REF_DECL (rtl);
23059 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
23061 ref = lookup_decl_die (decl);
23062 if (ref && (get_AT (ref, DW_AT_location)
23063 || get_AT (ref, DW_AT_const_value)))
23065 loc->dw_loc_opc = DW_OP_GNU_implicit_pointer;
23066 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23067 loc->dw_loc_oprnd1.val_entry = NULL;
23068 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
23069 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
23070 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
23071 loc->dw_loc_oprnd2.v.val_int = offset;
23072 return true;
23076 return false;
23079 /* Helper function for resolve_addr, handle one location
23080 expression, return false if at least one CONST_STRING or SYMBOL_REF in
23081 the location list couldn't be resolved. */
23083 static bool
23084 resolve_addr_in_expr (dw_loc_descr_ref loc)
23086 dw_loc_descr_ref keep = NULL;
23087 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
23088 switch (loc->dw_loc_opc)
23090 case DW_OP_addr:
23091 if (resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
23093 if ((prev == NULL
23094 || prev->dw_loc_opc == DW_OP_piece
23095 || prev->dw_loc_opc == DW_OP_bit_piece)
23096 && loc->dw_loc_next
23097 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
23098 && !dwarf_strict
23099 && optimize_one_addr_into_implicit_ptr (loc))
23100 break;
23101 return false;
23103 break;
23104 case DW_OP_GNU_addr_index:
23105 case DW_OP_GNU_const_index:
23106 if (loc->dw_loc_opc == DW_OP_GNU_addr_index
23107 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
23109 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
23110 if (resolve_one_addr (&rtl, NULL))
23111 return false;
23112 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
23113 loc->dw_loc_oprnd1.val_entry =
23114 add_addr_table_entry (rtl, ate_kind_rtx);
23116 break;
23117 case DW_OP_const4u:
23118 case DW_OP_const8u:
23119 if (loc->dtprel
23120 && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
23121 return false;
23122 break;
23123 case DW_OP_plus_uconst:
23124 if (size_of_loc_descr (loc)
23125 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
23127 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
23129 dw_loc_descr_ref repl
23130 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
23131 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
23132 add_loc_descr (&repl, loc->dw_loc_next);
23133 *loc = *repl;
23135 break;
23136 case DW_OP_implicit_value:
23137 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
23138 && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL))
23139 return false;
23140 break;
23141 case DW_OP_GNU_implicit_pointer:
23142 case DW_OP_GNU_parameter_ref:
23143 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
23145 dw_die_ref ref
23146 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
23147 if (ref == NULL)
23148 return false;
23149 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23150 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
23151 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
23153 break;
23154 case DW_OP_GNU_const_type:
23155 case DW_OP_GNU_regval_type:
23156 case DW_OP_GNU_deref_type:
23157 case DW_OP_GNU_convert:
23158 case DW_OP_GNU_reinterpret:
23159 while (loc->dw_loc_next
23160 && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
23162 dw_die_ref base1, base2;
23163 unsigned enc1, enc2, size1, size2;
23164 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
23165 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
23166 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
23167 else if (loc->dw_loc_oprnd1.val_class
23168 == dw_val_class_unsigned_const)
23169 break;
23170 else
23171 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
23172 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
23173 == dw_val_class_unsigned_const)
23174 break;
23175 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
23176 gcc_assert (base1->die_tag == DW_TAG_base_type
23177 && base2->die_tag == DW_TAG_base_type);
23178 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
23179 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
23180 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
23181 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
23182 if (size1 == size2
23183 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
23184 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
23185 && loc != keep)
23186 || enc1 == enc2))
23188 /* Optimize away next DW_OP_GNU_convert after
23189 adjusting LOC's base type die reference. */
23190 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
23191 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
23192 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
23193 else
23194 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
23195 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
23196 continue;
23198 /* Don't change integer DW_OP_GNU_convert after e.g. floating
23199 point typed stack entry. */
23200 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
23201 keep = loc->dw_loc_next;
23202 break;
23204 break;
23205 default:
23206 break;
23208 return true;
23211 /* Helper function of resolve_addr. DIE had DW_AT_location of
23212 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
23213 and DW_OP_addr couldn't be resolved. resolve_addr has already
23214 removed the DW_AT_location attribute. This function attempts to
23215 add a new DW_AT_location attribute with DW_OP_GNU_implicit_pointer
23216 to it or DW_AT_const_value attribute, if possible. */
23218 static void
23219 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
23221 if (TREE_CODE (decl) != VAR_DECL
23222 || lookup_decl_die (decl) != die
23223 || DECL_EXTERNAL (decl)
23224 || !TREE_STATIC (decl)
23225 || DECL_INITIAL (decl) == NULL_TREE
23226 || DECL_P (DECL_INITIAL (decl))
23227 || get_AT (die, DW_AT_const_value))
23228 return;
23230 tree init = DECL_INITIAL (decl);
23231 HOST_WIDE_INT offset = 0;
23232 /* For variables that have been optimized away and thus
23233 don't have a memory location, see if we can emit
23234 DW_AT_const_value instead. */
23235 if (tree_add_const_value_attribute (die, init))
23236 return;
23237 if (dwarf_strict)
23238 return;
23239 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
23240 and ADDR_EXPR refers to a decl that has DW_AT_location or
23241 DW_AT_const_value (but isn't addressable, otherwise
23242 resolving the original DW_OP_addr wouldn't fail), see if
23243 we can add DW_OP_GNU_implicit_pointer. */
23244 STRIP_NOPS (init);
23245 if (TREE_CODE (init) == POINTER_PLUS_EXPR
23246 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
23248 offset = tree_to_shwi (TREE_OPERAND (init, 1));
23249 init = TREE_OPERAND (init, 0);
23250 STRIP_NOPS (init);
23252 if (TREE_CODE (init) != ADDR_EXPR)
23253 return;
23254 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
23255 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
23256 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
23257 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
23258 && TREE_OPERAND (init, 0) != decl))
23260 dw_die_ref ref;
23261 dw_loc_descr_ref l;
23263 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
23265 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
23266 if (!rtl)
23267 return;
23268 decl = SYMBOL_REF_DECL (rtl);
23270 else
23271 decl = TREE_OPERAND (init, 0);
23272 ref = lookup_decl_die (decl);
23273 if (ref == NULL
23274 || (!get_AT (ref, DW_AT_location)
23275 && !get_AT (ref, DW_AT_const_value)))
23276 return;
23277 l = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
23278 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23279 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
23280 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
23281 add_AT_loc (die, DW_AT_location, l);
23285 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
23286 an address in .rodata section if the string literal is emitted there,
23287 or remove the containing location list or replace DW_AT_const_value
23288 with DW_AT_location and empty location expression, if it isn't found
23289 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
23290 to something that has been emitted in the current CU. */
23292 static void
23293 resolve_addr (dw_die_ref die)
23295 dw_die_ref c;
23296 dw_attr_ref a;
23297 dw_loc_list_ref *curr, *start, loc;
23298 unsigned ix;
23300 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23301 switch (AT_class (a))
23303 case dw_val_class_loc_list:
23304 start = curr = AT_loc_list_ptr (a);
23305 loc = *curr;
23306 gcc_assert (loc);
23307 /* The same list can be referenced more than once. See if we have
23308 already recorded the result from a previous pass. */
23309 if (loc->replaced)
23310 *curr = loc->dw_loc_next;
23311 else if (!loc->resolved_addr)
23313 /* As things stand, we do not expect or allow one die to
23314 reference a suffix of another die's location list chain.
23315 References must be identical or completely separate.
23316 There is therefore no need to cache the result of this
23317 pass on any list other than the first; doing so
23318 would lead to unnecessary writes. */
23319 while (*curr)
23321 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
23322 if (!resolve_addr_in_expr ((*curr)->expr))
23324 dw_loc_list_ref next = (*curr)->dw_loc_next;
23325 dw_loc_descr_ref l = (*curr)->expr;
23327 if (next && (*curr)->ll_symbol)
23329 gcc_assert (!next->ll_symbol);
23330 next->ll_symbol = (*curr)->ll_symbol;
23332 if (dwarf_split_debug_info)
23333 remove_loc_list_addr_table_entries (l);
23334 *curr = next;
23336 else
23338 mark_base_types ((*curr)->expr);
23339 curr = &(*curr)->dw_loc_next;
23342 if (loc == *start)
23343 loc->resolved_addr = 1;
23344 else
23346 loc->replaced = 1;
23347 loc->dw_loc_next = *start;
23350 if (!*start)
23352 remove_AT (die, a->dw_attr);
23353 ix--;
23355 break;
23356 case dw_val_class_loc:
23358 dw_loc_descr_ref l = AT_loc (a);
23359 /* For -gdwarf-2 don't attempt to optimize
23360 DW_AT_data_member_location containing
23361 DW_OP_plus_uconst - older consumers might
23362 rely on it being that op instead of a more complex,
23363 but shorter, location description. */
23364 if ((dwarf_version > 2
23365 || a->dw_attr != DW_AT_data_member_location
23366 || l == NULL
23367 || l->dw_loc_opc != DW_OP_plus_uconst
23368 || l->dw_loc_next != NULL)
23369 && !resolve_addr_in_expr (l))
23371 if (dwarf_split_debug_info)
23372 remove_loc_list_addr_table_entries (l);
23373 if (l != NULL
23374 && l->dw_loc_next == NULL
23375 && l->dw_loc_opc == DW_OP_addr
23376 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
23377 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
23378 && a->dw_attr == DW_AT_location)
23380 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
23381 remove_AT (die, a->dw_attr);
23382 ix--;
23383 optimize_location_into_implicit_ptr (die, decl);
23384 break;
23386 remove_AT (die, a->dw_attr);
23387 ix--;
23389 else
23390 mark_base_types (l);
23392 break;
23393 case dw_val_class_addr:
23394 if (a->dw_attr == DW_AT_const_value
23395 && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
23397 if (AT_index (a) != NOT_INDEXED)
23398 remove_addr_table_entry (a->dw_attr_val.val_entry);
23399 remove_AT (die, a->dw_attr);
23400 ix--;
23402 if (die->die_tag == DW_TAG_GNU_call_site
23403 && a->dw_attr == DW_AT_abstract_origin)
23405 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
23406 dw_die_ref tdie = lookup_decl_die (tdecl);
23407 if (tdie == NULL
23408 && DECL_EXTERNAL (tdecl)
23409 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE)
23411 force_decl_die (tdecl);
23412 tdie = lookup_decl_die (tdecl);
23414 if (tdie)
23416 a->dw_attr_val.val_class = dw_val_class_die_ref;
23417 a->dw_attr_val.v.val_die_ref.die = tdie;
23418 a->dw_attr_val.v.val_die_ref.external = 0;
23420 else
23422 if (AT_index (a) != NOT_INDEXED)
23423 remove_addr_table_entry (a->dw_attr_val.val_entry);
23424 remove_AT (die, a->dw_attr);
23425 ix--;
23428 break;
23429 default:
23430 break;
23433 FOR_EACH_CHILD (die, c, resolve_addr (c));
23436 /* Helper routines for optimize_location_lists.
23437 This pass tries to share identical local lists in .debug_loc
23438 section. */
23440 /* Iteratively hash operands of LOC opcode. */
23442 static hashval_t
23443 hash_loc_operands (dw_loc_descr_ref loc, hashval_t hash)
23445 dw_val_ref val1 = &loc->dw_loc_oprnd1;
23446 dw_val_ref val2 = &loc->dw_loc_oprnd2;
23448 switch (loc->dw_loc_opc)
23450 case DW_OP_const4u:
23451 case DW_OP_const8u:
23452 if (loc->dtprel)
23453 goto hash_addr;
23454 /* FALLTHRU */
23455 case DW_OP_const1u:
23456 case DW_OP_const1s:
23457 case DW_OP_const2u:
23458 case DW_OP_const2s:
23459 case DW_OP_const4s:
23460 case DW_OP_const8s:
23461 case DW_OP_constu:
23462 case DW_OP_consts:
23463 case DW_OP_pick:
23464 case DW_OP_plus_uconst:
23465 case DW_OP_breg0:
23466 case DW_OP_breg1:
23467 case DW_OP_breg2:
23468 case DW_OP_breg3:
23469 case DW_OP_breg4:
23470 case DW_OP_breg5:
23471 case DW_OP_breg6:
23472 case DW_OP_breg7:
23473 case DW_OP_breg8:
23474 case DW_OP_breg9:
23475 case DW_OP_breg10:
23476 case DW_OP_breg11:
23477 case DW_OP_breg12:
23478 case DW_OP_breg13:
23479 case DW_OP_breg14:
23480 case DW_OP_breg15:
23481 case DW_OP_breg16:
23482 case DW_OP_breg17:
23483 case DW_OP_breg18:
23484 case DW_OP_breg19:
23485 case DW_OP_breg20:
23486 case DW_OP_breg21:
23487 case DW_OP_breg22:
23488 case DW_OP_breg23:
23489 case DW_OP_breg24:
23490 case DW_OP_breg25:
23491 case DW_OP_breg26:
23492 case DW_OP_breg27:
23493 case DW_OP_breg28:
23494 case DW_OP_breg29:
23495 case DW_OP_breg30:
23496 case DW_OP_breg31:
23497 case DW_OP_regx:
23498 case DW_OP_fbreg:
23499 case DW_OP_piece:
23500 case DW_OP_deref_size:
23501 case DW_OP_xderef_size:
23502 hash = iterative_hash_object (val1->v.val_int, hash);
23503 break;
23504 case DW_OP_skip:
23505 case DW_OP_bra:
23507 int offset;
23509 gcc_assert (val1->val_class == dw_val_class_loc);
23510 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
23511 hash = iterative_hash_object (offset, hash);
23513 break;
23514 case DW_OP_implicit_value:
23515 hash = iterative_hash_object (val1->v.val_unsigned, hash);
23516 switch (val2->val_class)
23518 case dw_val_class_const:
23519 hash = iterative_hash_object (val2->v.val_int, hash);
23520 break;
23521 case dw_val_class_vec:
23523 unsigned int elt_size = val2->v.val_vec.elt_size;
23524 unsigned int len = val2->v.val_vec.length;
23526 hash = iterative_hash_object (elt_size, hash);
23527 hash = iterative_hash_object (len, hash);
23528 hash = iterative_hash (val2->v.val_vec.array,
23529 len * elt_size, hash);
23531 break;
23532 case dw_val_class_const_double:
23533 hash = iterative_hash_object (val2->v.val_double.low, hash);
23534 hash = iterative_hash_object (val2->v.val_double.high, hash);
23535 break;
23536 case dw_val_class_addr:
23537 hash = iterative_hash_rtx (val2->v.val_addr, hash);
23538 break;
23539 default:
23540 gcc_unreachable ();
23542 break;
23543 case DW_OP_bregx:
23544 case DW_OP_bit_piece:
23545 hash = iterative_hash_object (val1->v.val_int, hash);
23546 hash = iterative_hash_object (val2->v.val_int, hash);
23547 break;
23548 case DW_OP_addr:
23549 hash_addr:
23550 if (loc->dtprel)
23552 unsigned char dtprel = 0xd1;
23553 hash = iterative_hash_object (dtprel, hash);
23555 hash = iterative_hash_rtx (val1->v.val_addr, hash);
23556 break;
23557 case DW_OP_GNU_addr_index:
23558 case DW_OP_GNU_const_index:
23560 if (loc->dtprel)
23562 unsigned char dtprel = 0xd1;
23563 hash = iterative_hash_object (dtprel, hash);
23565 hash = iterative_hash_rtx (val1->val_entry->addr.rtl, hash);
23567 break;
23568 case DW_OP_GNU_implicit_pointer:
23569 hash = iterative_hash_object (val2->v.val_int, hash);
23570 break;
23571 case DW_OP_GNU_entry_value:
23572 hash = hash_loc_operands (val1->v.val_loc, hash);
23573 break;
23574 case DW_OP_GNU_regval_type:
23575 case DW_OP_GNU_deref_type:
23577 unsigned int byte_size
23578 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
23579 unsigned int encoding
23580 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
23581 hash = iterative_hash_object (val1->v.val_int, hash);
23582 hash = iterative_hash_object (byte_size, hash);
23583 hash = iterative_hash_object (encoding, hash);
23585 break;
23586 case DW_OP_GNU_convert:
23587 case DW_OP_GNU_reinterpret:
23588 if (val1->val_class == dw_val_class_unsigned_const)
23590 hash = iterative_hash_object (val1->v.val_unsigned, hash);
23591 break;
23593 /* FALLTHRU */
23594 case DW_OP_GNU_const_type:
23596 unsigned int byte_size
23597 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
23598 unsigned int encoding
23599 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
23600 hash = iterative_hash_object (byte_size, hash);
23601 hash = iterative_hash_object (encoding, hash);
23602 if (loc->dw_loc_opc != DW_OP_GNU_const_type)
23603 break;
23604 hash = iterative_hash_object (val2->val_class, hash);
23605 switch (val2->val_class)
23607 case dw_val_class_const:
23608 hash = iterative_hash_object (val2->v.val_int, hash);
23609 break;
23610 case dw_val_class_vec:
23612 unsigned int elt_size = val2->v.val_vec.elt_size;
23613 unsigned int len = val2->v.val_vec.length;
23615 hash = iterative_hash_object (elt_size, hash);
23616 hash = iterative_hash_object (len, hash);
23617 hash = iterative_hash (val2->v.val_vec.array,
23618 len * elt_size, hash);
23620 break;
23621 case dw_val_class_const_double:
23622 hash = iterative_hash_object (val2->v.val_double.low, hash);
23623 hash = iterative_hash_object (val2->v.val_double.high, hash);
23624 break;
23625 default:
23626 gcc_unreachable ();
23629 break;
23631 default:
23632 /* Other codes have no operands. */
23633 break;
23635 return hash;
23638 /* Iteratively hash the whole DWARF location expression LOC. */
23640 static inline hashval_t
23641 hash_locs (dw_loc_descr_ref loc, hashval_t hash)
23643 dw_loc_descr_ref l;
23644 bool sizes_computed = false;
23645 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
23646 size_of_locs (loc);
23648 for (l = loc; l != NULL; l = l->dw_loc_next)
23650 enum dwarf_location_atom opc = l->dw_loc_opc;
23651 hash = iterative_hash_object (opc, hash);
23652 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
23654 size_of_locs (loc);
23655 sizes_computed = true;
23657 hash = hash_loc_operands (l, hash);
23659 return hash;
23662 /* Compute hash of the whole location list LIST_HEAD. */
23664 static inline void
23665 hash_loc_list (dw_loc_list_ref list_head)
23667 dw_loc_list_ref curr = list_head;
23668 hashval_t hash = 0;
23670 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
23672 hash = iterative_hash (curr->begin, strlen (curr->begin) + 1, hash);
23673 hash = iterative_hash (curr->end, strlen (curr->end) + 1, hash);
23674 if (curr->section)
23675 hash = iterative_hash (curr->section, strlen (curr->section) + 1,
23676 hash);
23677 hash = hash_locs (curr->expr, hash);
23679 list_head->hash = hash;
23682 /* Return true if X and Y opcodes have the same operands. */
23684 static inline bool
23685 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
23687 dw_val_ref valx1 = &x->dw_loc_oprnd1;
23688 dw_val_ref valx2 = &x->dw_loc_oprnd2;
23689 dw_val_ref valy1 = &y->dw_loc_oprnd1;
23690 dw_val_ref valy2 = &y->dw_loc_oprnd2;
23692 switch (x->dw_loc_opc)
23694 case DW_OP_const4u:
23695 case DW_OP_const8u:
23696 if (x->dtprel)
23697 goto hash_addr;
23698 /* FALLTHRU */
23699 case DW_OP_const1u:
23700 case DW_OP_const1s:
23701 case DW_OP_const2u:
23702 case DW_OP_const2s:
23703 case DW_OP_const4s:
23704 case DW_OP_const8s:
23705 case DW_OP_constu:
23706 case DW_OP_consts:
23707 case DW_OP_pick:
23708 case DW_OP_plus_uconst:
23709 case DW_OP_breg0:
23710 case DW_OP_breg1:
23711 case DW_OP_breg2:
23712 case DW_OP_breg3:
23713 case DW_OP_breg4:
23714 case DW_OP_breg5:
23715 case DW_OP_breg6:
23716 case DW_OP_breg7:
23717 case DW_OP_breg8:
23718 case DW_OP_breg9:
23719 case DW_OP_breg10:
23720 case DW_OP_breg11:
23721 case DW_OP_breg12:
23722 case DW_OP_breg13:
23723 case DW_OP_breg14:
23724 case DW_OP_breg15:
23725 case DW_OP_breg16:
23726 case DW_OP_breg17:
23727 case DW_OP_breg18:
23728 case DW_OP_breg19:
23729 case DW_OP_breg20:
23730 case DW_OP_breg21:
23731 case DW_OP_breg22:
23732 case DW_OP_breg23:
23733 case DW_OP_breg24:
23734 case DW_OP_breg25:
23735 case DW_OP_breg26:
23736 case DW_OP_breg27:
23737 case DW_OP_breg28:
23738 case DW_OP_breg29:
23739 case DW_OP_breg30:
23740 case DW_OP_breg31:
23741 case DW_OP_regx:
23742 case DW_OP_fbreg:
23743 case DW_OP_piece:
23744 case DW_OP_deref_size:
23745 case DW_OP_xderef_size:
23746 return valx1->v.val_int == valy1->v.val_int;
23747 case DW_OP_skip:
23748 case DW_OP_bra:
23749 /* If splitting debug info, the use of DW_OP_GNU_addr_index
23750 can cause irrelevant differences in dw_loc_addr. */
23751 gcc_assert (valx1->val_class == dw_val_class_loc
23752 && valy1->val_class == dw_val_class_loc
23753 && (dwarf_split_debug_info
23754 || x->dw_loc_addr == y->dw_loc_addr));
23755 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
23756 case DW_OP_implicit_value:
23757 if (valx1->v.val_unsigned != valy1->v.val_unsigned
23758 || valx2->val_class != valy2->val_class)
23759 return false;
23760 switch (valx2->val_class)
23762 case dw_val_class_const:
23763 return valx2->v.val_int == valy2->v.val_int;
23764 case dw_val_class_vec:
23765 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
23766 && valx2->v.val_vec.length == valy2->v.val_vec.length
23767 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
23768 valx2->v.val_vec.elt_size
23769 * valx2->v.val_vec.length) == 0;
23770 case dw_val_class_const_double:
23771 return valx2->v.val_double.low == valy2->v.val_double.low
23772 && valx2->v.val_double.high == valy2->v.val_double.high;
23773 case dw_val_class_addr:
23774 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
23775 default:
23776 gcc_unreachable ();
23778 case DW_OP_bregx:
23779 case DW_OP_bit_piece:
23780 return valx1->v.val_int == valy1->v.val_int
23781 && valx2->v.val_int == valy2->v.val_int;
23782 case DW_OP_addr:
23783 hash_addr:
23784 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
23785 case DW_OP_GNU_addr_index:
23786 case DW_OP_GNU_const_index:
23788 rtx ax1 = valx1->val_entry->addr.rtl;
23789 rtx ay1 = valy1->val_entry->addr.rtl;
23790 return rtx_equal_p (ax1, ay1);
23792 case DW_OP_GNU_implicit_pointer:
23793 return valx1->val_class == dw_val_class_die_ref
23794 && valx1->val_class == valy1->val_class
23795 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
23796 && valx2->v.val_int == valy2->v.val_int;
23797 case DW_OP_GNU_entry_value:
23798 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
23799 case DW_OP_GNU_const_type:
23800 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
23801 || valx2->val_class != valy2->val_class)
23802 return false;
23803 switch (valx2->val_class)
23805 case dw_val_class_const:
23806 return valx2->v.val_int == valy2->v.val_int;
23807 case dw_val_class_vec:
23808 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
23809 && valx2->v.val_vec.length == valy2->v.val_vec.length
23810 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
23811 valx2->v.val_vec.elt_size
23812 * valx2->v.val_vec.length) == 0;
23813 case dw_val_class_const_double:
23814 return valx2->v.val_double.low == valy2->v.val_double.low
23815 && valx2->v.val_double.high == valy2->v.val_double.high;
23816 default:
23817 gcc_unreachable ();
23819 case DW_OP_GNU_regval_type:
23820 case DW_OP_GNU_deref_type:
23821 return valx1->v.val_int == valy1->v.val_int
23822 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
23823 case DW_OP_GNU_convert:
23824 case DW_OP_GNU_reinterpret:
23825 if (valx1->val_class != valy1->val_class)
23826 return false;
23827 if (valx1->val_class == dw_val_class_unsigned_const)
23828 return valx1->v.val_unsigned == valy1->v.val_unsigned;
23829 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
23830 case DW_OP_GNU_parameter_ref:
23831 return valx1->val_class == dw_val_class_die_ref
23832 && valx1->val_class == valy1->val_class
23833 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
23834 default:
23835 /* Other codes have no operands. */
23836 return true;
23840 /* Return true if DWARF location expressions X and Y are the same. */
23842 static inline bool
23843 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
23845 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
23846 if (x->dw_loc_opc != y->dw_loc_opc
23847 || x->dtprel != y->dtprel
23848 || !compare_loc_operands (x, y))
23849 break;
23850 return x == NULL && y == NULL;
23853 /* Hashtable helpers. */
23855 struct loc_list_hasher : typed_noop_remove <dw_loc_list_struct>
23857 typedef dw_loc_list_struct value_type;
23858 typedef dw_loc_list_struct compare_type;
23859 static inline hashval_t hash (const value_type *);
23860 static inline bool equal (const value_type *, const compare_type *);
23863 /* Return precomputed hash of location list X. */
23865 inline hashval_t
23866 loc_list_hasher::hash (const value_type *x)
23868 return x->hash;
23871 /* Return true if location lists A and B are the same. */
23873 inline bool
23874 loc_list_hasher::equal (const value_type *a, const compare_type *b)
23876 if (a == b)
23877 return 1;
23878 if (a->hash != b->hash)
23879 return 0;
23880 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
23881 if (strcmp (a->begin, b->begin) != 0
23882 || strcmp (a->end, b->end) != 0
23883 || (a->section == NULL) != (b->section == NULL)
23884 || (a->section && strcmp (a->section, b->section) != 0)
23885 || !compare_locs (a->expr, b->expr))
23886 break;
23887 return a == NULL && b == NULL;
23890 typedef hash_table <loc_list_hasher> loc_list_hash_type;
23893 /* Recursively optimize location lists referenced from DIE
23894 children and share them whenever possible. */
23896 static void
23897 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type htab)
23899 dw_die_ref c;
23900 dw_attr_ref a;
23901 unsigned ix;
23902 dw_loc_list_struct **slot;
23904 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23905 if (AT_class (a) == dw_val_class_loc_list)
23907 dw_loc_list_ref list = AT_loc_list (a);
23908 /* TODO: perform some optimizations here, before hashing
23909 it and storing into the hash table. */
23910 hash_loc_list (list);
23911 slot = htab.find_slot_with_hash (list, list->hash, INSERT);
23912 if (*slot == NULL)
23913 *slot = list;
23914 else
23915 a->dw_attr_val.v.val_loc_list = *slot;
23918 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
23922 /* Recursively assign each location list a unique index into the debug_addr
23923 section. */
23925 static void
23926 index_location_lists (dw_die_ref die)
23928 dw_die_ref c;
23929 dw_attr_ref a;
23930 unsigned ix;
23932 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23933 if (AT_class (a) == dw_val_class_loc_list)
23935 dw_loc_list_ref list = AT_loc_list (a);
23936 dw_loc_list_ref curr;
23937 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
23939 /* Don't index an entry that has already been indexed
23940 or won't be output. */
23941 if (curr->begin_entry != NULL
23942 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
23943 continue;
23945 curr->begin_entry
23946 = add_addr_table_entry (xstrdup (curr->begin),
23947 ate_kind_label);
23951 FOR_EACH_CHILD (die, c, index_location_lists (c));
23954 /* Optimize location lists referenced from DIE
23955 children and share them whenever possible. */
23957 static void
23958 optimize_location_lists (dw_die_ref die)
23960 loc_list_hash_type htab;
23961 htab.create (500);
23962 optimize_location_lists_1 (die, htab);
23963 htab.dispose ();
23966 /* Output stuff that dwarf requires at the end of every file,
23967 and generate the DWARF-2 debugging info. */
23969 static void
23970 dwarf2out_finish (const char *filename)
23972 limbo_die_node *node, *next_node;
23973 comdat_type_node *ctnode;
23974 hash_table <comdat_type_hasher> comdat_type_table;
23975 unsigned int i;
23976 dw_die_ref main_comp_unit_die;
23978 /* PCH might result in DW_AT_producer string being restored from the
23979 header compilation, so always fill it with empty string initially
23980 and overwrite only here. */
23981 dw_attr_ref producer = get_AT (comp_unit_die (), DW_AT_producer);
23982 producer_string = gen_producer_string ();
23983 producer->dw_attr_val.v.val_str->refcount--;
23984 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
23986 gen_scheduled_generic_parms_dies ();
23987 gen_remaining_tmpl_value_param_die_attribute ();
23989 /* Add the name for the main input file now. We delayed this from
23990 dwarf2out_init to avoid complications with PCH.
23991 For LTO produced units use a fixed artificial name to avoid
23992 leaking tempfile names into the dwarf. */
23993 if (!in_lto_p)
23994 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
23995 else
23996 add_name_attribute (comp_unit_die (), "<artificial>");
23997 if (!IS_ABSOLUTE_PATH (filename) || targetm.force_at_comp_dir)
23998 add_comp_dir_attribute (comp_unit_die ());
23999 else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
24001 bool p = false;
24002 htab_traverse (file_table, file_table_relative_p, &p);
24003 if (p)
24004 add_comp_dir_attribute (comp_unit_die ());
24007 if (deferred_locations_list)
24008 for (i = 0; i < deferred_locations_list->length (); i++)
24010 add_location_or_const_value_attribute (
24011 (*deferred_locations_list)[i].die,
24012 (*deferred_locations_list)[i].variable,
24013 false,
24014 DW_AT_location);
24017 /* Traverse the limbo die list, and add parent/child links. The only
24018 dies without parents that should be here are concrete instances of
24019 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
24020 For concrete instances, we can get the parent die from the abstract
24021 instance. */
24022 for (node = limbo_die_list; node; node = next_node)
24024 dw_die_ref die = node->die;
24025 next_node = node->next;
24027 if (die->die_parent == NULL)
24029 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
24031 if (origin && origin->die_parent)
24032 add_child_die (origin->die_parent, die);
24033 else if (is_cu_die (die))
24035 else if (seen_error ())
24036 /* It's OK to be confused by errors in the input. */
24037 add_child_die (comp_unit_die (), die);
24038 else
24040 /* In certain situations, the lexical block containing a
24041 nested function can be optimized away, which results
24042 in the nested function die being orphaned. Likewise
24043 with the return type of that nested function. Force
24044 this to be a child of the containing function.
24046 It may happen that even the containing function got fully
24047 inlined and optimized out. In that case we are lost and
24048 assign the empty child. This should not be big issue as
24049 the function is likely unreachable too. */
24050 gcc_assert (node->created_for);
24052 if (DECL_P (node->created_for))
24053 origin = get_context_die (DECL_CONTEXT (node->created_for));
24054 else if (TYPE_P (node->created_for))
24055 origin = scope_die_for (node->created_for, comp_unit_die ());
24056 else
24057 origin = comp_unit_die ();
24059 add_child_die (origin, die);
24064 limbo_die_list = NULL;
24066 #if ENABLE_ASSERT_CHECKING
24068 dw_die_ref die = comp_unit_die (), c;
24069 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
24071 #endif
24072 resolve_addr (comp_unit_die ());
24073 move_marked_base_types ();
24075 for (node = deferred_asm_name; node; node = node->next)
24077 tree decl = node->created_for;
24078 /* When generating LTO bytecode we can not generate new assembler
24079 names at this point and all important decls got theirs via
24080 free-lang-data. */
24081 if ((!flag_generate_lto || DECL_ASSEMBLER_NAME_SET_P (decl))
24082 && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
24084 add_linkage_attr (node->die, decl);
24085 move_linkage_attr (node->die);
24089 deferred_asm_name = NULL;
24091 /* Walk through the list of incomplete types again, trying once more to
24092 emit full debugging info for them. */
24093 retry_incomplete_types ();
24095 if (flag_eliminate_unused_debug_types)
24096 prune_unused_types ();
24098 /* Generate separate COMDAT sections for type DIEs. */
24099 if (use_debug_types)
24101 break_out_comdat_types (comp_unit_die ());
24103 /* Each new type_unit DIE was added to the limbo die list when created.
24104 Since these have all been added to comdat_type_list, clear the
24105 limbo die list. */
24106 limbo_die_list = NULL;
24108 /* For each new comdat type unit, copy declarations for incomplete
24109 types to make the new unit self-contained (i.e., no direct
24110 references to the main compile unit). */
24111 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24112 copy_decls_for_unworthy_types (ctnode->root_die);
24113 copy_decls_for_unworthy_types (comp_unit_die ());
24115 /* In the process of copying declarations from one unit to another,
24116 we may have left some declarations behind that are no longer
24117 referenced. Prune them. */
24118 prune_unused_types ();
24121 /* Generate separate CUs for each of the include files we've seen.
24122 They will go into limbo_die_list. */
24123 if (flag_eliminate_dwarf2_dups)
24124 break_out_includes (comp_unit_die ());
24126 /* Traverse the DIE's and add add sibling attributes to those DIE's
24127 that have children. */
24128 add_sibling_attributes (comp_unit_die ());
24129 for (node = limbo_die_list; node; node = node->next)
24130 add_sibling_attributes (node->die);
24131 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24132 add_sibling_attributes (ctnode->root_die);
24134 /* When splitting DWARF info, we put some attributes in the
24135 skeleton compile_unit DIE that remains in the .o, while
24136 most attributes go in the DWO compile_unit_die. */
24137 if (dwarf_split_debug_info)
24138 main_comp_unit_die = gen_compile_unit_die (NULL);
24139 else
24140 main_comp_unit_die = comp_unit_die ();
24142 /* Output a terminator label for the .text section. */
24143 switch_to_section (text_section);
24144 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
24145 if (cold_text_section)
24147 switch_to_section (cold_text_section);
24148 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
24151 /* We can only use the low/high_pc attributes if all of the code was
24152 in .text. */
24153 if (!have_multiple_function_sections
24154 || (dwarf_version < 3 && dwarf_strict))
24156 /* Don't add if the CU has no associated code. */
24157 if (text_section_used)
24158 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
24159 text_end_label, true);
24161 else
24163 unsigned fde_idx;
24164 dw_fde_ref fde;
24165 bool range_list_added = false;
24167 if (text_section_used)
24168 add_ranges_by_labels (main_comp_unit_die, text_section_label,
24169 text_end_label, &range_list_added, true);
24170 if (cold_text_section_used)
24171 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
24172 cold_end_label, &range_list_added, true);
24174 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
24176 if (DECL_IGNORED_P (fde->decl))
24177 continue;
24178 if (!fde->in_std_section)
24179 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
24180 fde->dw_fde_end, &range_list_added,
24181 true);
24182 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
24183 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
24184 fde->dw_fde_second_end, &range_list_added,
24185 true);
24188 if (range_list_added)
24190 /* We need to give .debug_loc and .debug_ranges an appropriate
24191 "base address". Use zero so that these addresses become
24192 absolute. Historically, we've emitted the unexpected
24193 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
24194 Emit both to give time for other tools to adapt. */
24195 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
24196 if (! dwarf_strict && dwarf_version < 4)
24197 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
24199 add_ranges (NULL);
24203 if (debug_info_level >= DINFO_LEVEL_TERSE)
24204 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
24205 debug_line_section_label);
24207 if (have_macinfo)
24208 add_AT_macptr (comp_unit_die (),
24209 dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
24210 macinfo_section_label);
24212 if (dwarf_split_debug_info)
24214 /* optimize_location_lists calculates the size of the lists,
24215 so index them first, and assign indices to the entries.
24216 Although optimize_location_lists will remove entries from
24217 the table, it only does so for duplicates, and therefore
24218 only reduces ref_counts to 1. */
24219 index_location_lists (comp_unit_die ());
24221 if (addr_index_table != NULL)
24223 unsigned int index = 0;
24224 htab_traverse_noresize (addr_index_table,
24225 index_addr_table_entry, &index);
24229 if (have_location_lists)
24230 optimize_location_lists (comp_unit_die ());
24232 save_macinfo_strings ();
24234 if (dwarf_split_debug_info)
24236 unsigned int index = 0;
24238 /* Add attributes common to skeleton compile_units and
24239 type_units. Because these attributes include strings, it
24240 must be done before freezing the string table. Top-level
24241 skeleton die attrs are added when the skeleton type unit is
24242 created, so ensure it is created by this point. */
24243 add_top_level_skeleton_die_attrs (main_comp_unit_die);
24244 (void) get_skeleton_type_unit ();
24245 htab_traverse_noresize (debug_str_hash, index_string, &index);
24248 /* Output all of the compilation units. We put the main one last so that
24249 the offsets are available to output_pubnames. */
24250 for (node = limbo_die_list; node; node = node->next)
24251 output_comp_unit (node->die, 0);
24253 comdat_type_table.create (100);
24254 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24256 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
24258 /* Don't output duplicate types. */
24259 if (*slot != HTAB_EMPTY_ENTRY)
24260 continue;
24262 /* Add a pointer to the line table for the main compilation unit
24263 so that the debugger can make sense of DW_AT_decl_file
24264 attributes. */
24265 if (debug_info_level >= DINFO_LEVEL_TERSE)
24266 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
24267 (!dwarf_split_debug_info
24268 ? debug_line_section_label
24269 : debug_skeleton_line_section_label));
24271 output_comdat_type_unit (ctnode);
24272 *slot = ctnode;
24274 comdat_type_table.dispose ();
24276 /* The AT_pubnames attribute needs to go in all skeleton dies, including
24277 both the main_cu and all skeleton TUs. Making this call unconditional
24278 would end up either adding a second copy of the AT_pubnames attribute, or
24279 requiring a special case in add_top_level_skeleton_die_attrs. */
24280 if (!dwarf_split_debug_info)
24281 add_AT_pubnames (comp_unit_die ());
24283 if (dwarf_split_debug_info)
24285 int mark;
24286 unsigned char checksum[16];
24287 struct md5_ctx ctx;
24289 /* Compute a checksum of the comp_unit to use as the dwo_id. */
24290 md5_init_ctx (&ctx);
24291 mark = 0;
24292 die_checksum (comp_unit_die (), &ctx, &mark);
24293 unmark_all_dies (comp_unit_die ());
24294 md5_finish_ctx (&ctx, checksum);
24296 /* Use the first 8 bytes of the checksum as the dwo_id,
24297 and add it to both comp-unit DIEs. */
24298 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
24299 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
24301 /* Add the base offset of the ranges table to the skeleton
24302 comp-unit DIE. */
24303 if (ranges_table_in_use)
24304 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
24305 ranges_section_label);
24307 switch_to_section (debug_addr_section);
24308 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
24309 output_addr_table ();
24312 /* Output the main compilation unit if non-empty or if .debug_macinfo
24313 or .debug_macro will be emitted. */
24314 output_comp_unit (comp_unit_die (), have_macinfo);
24316 if (dwarf_split_debug_info && info_section_emitted)
24317 output_skeleton_debug_sections (main_comp_unit_die);
24319 /* Output the abbreviation table. */
24320 if (abbrev_die_table_in_use != 1)
24322 switch_to_section (debug_abbrev_section);
24323 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
24324 output_abbrev_section ();
24327 /* Output location list section if necessary. */
24328 if (have_location_lists)
24330 /* Output the location lists info. */
24331 switch_to_section (debug_loc_section);
24332 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
24333 output_location_lists (comp_unit_die ());
24336 output_pubtables ();
24338 /* Output the address range information if a CU (.debug_info section)
24339 was emitted. We output an empty table even if we had no functions
24340 to put in it. This because the consumer has no way to tell the
24341 difference between an empty table that we omitted and failure to
24342 generate a table that would have contained data. */
24343 if (info_section_emitted)
24345 unsigned long aranges_length = size_of_aranges ();
24347 switch_to_section (debug_aranges_section);
24348 output_aranges (aranges_length);
24351 /* Output ranges section if necessary. */
24352 if (ranges_table_in_use)
24354 switch_to_section (debug_ranges_section);
24355 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
24356 output_ranges ();
24359 /* Have to end the macro section. */
24360 if (have_macinfo)
24362 switch_to_section (debug_macinfo_section);
24363 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
24364 output_macinfo ();
24365 dw2_asm_output_data (1, 0, "End compilation unit");
24368 /* Output the source line correspondence table. We must do this
24369 even if there is no line information. Otherwise, on an empty
24370 translation unit, we will generate a present, but empty,
24371 .debug_info section. IRIX 6.5 `nm' will then complain when
24372 examining the file. This is done late so that any filenames
24373 used by the debug_info section are marked as 'used'. */
24374 switch_to_section (debug_line_section);
24375 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
24376 if (! DWARF2_ASM_LINE_DEBUG_INFO)
24377 output_line_info (false);
24379 if (dwarf_split_debug_info && info_section_emitted)
24381 switch_to_section (debug_skeleton_line_section);
24382 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
24383 output_line_info (true);
24386 /* If we emitted any indirect strings, output the string table too. */
24387 if (debug_str_hash || skeleton_debug_str_hash)
24388 output_indirect_strings ();
24391 #include "gt-dwarf2out.h"