PR rtl-optimization/57003
[official-gcc.git] / gcc / dwarf2out.c
blob66cdc961cb31cdda2711544983a6e6ccd4c70c2b
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 "rtlhash.h"
75 #include "insn-config.h"
76 #include "reload.h"
77 #include "function.h"
78 #include "output.h"
79 #include "expr.h"
80 #include "except.h"
81 #include "dwarf2.h"
82 #include "dwarf2out.h"
83 #include "dwarf2asm.h"
84 #include "toplev.h"
85 #include "md5.h"
86 #include "tm_p.h"
87 #include "diagnostic.h"
88 #include "tree-pretty-print.h"
89 #include "debug.h"
90 #include "target.h"
91 #include "common/common-target.h"
92 #include "langhooks.h"
93 #include "cgraph.h"
94 #include "input.h"
95 #include "ira.h"
96 #include "lra.h"
97 #include "dumpfile.h"
98 #include "opts.h"
99 #include "tree-dfa.h"
100 #include "gdb/gdb-index.h"
101 #include "rtl-iter.h"
103 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
104 static rtx_insn *last_var_location_insn;
105 static rtx_insn *cached_next_real_insn;
106 static void dwarf2out_decl (tree);
108 #ifdef VMS_DEBUGGING_INFO
109 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
111 /* Define this macro to be a nonzero value if the directory specifications
112 which are output in the debug info should end with a separator. */
113 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
114 /* Define this macro to evaluate to a nonzero value if GCC should refrain
115 from generating indirect strings in DWARF2 debug information, for instance
116 if your target is stuck with an old version of GDB that is unable to
117 process them properly or uses VMS Debug. */
118 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
119 #else
120 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
121 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
122 #endif
124 /* ??? Poison these here until it can be done generically. They've been
125 totally replaced in this file; make sure it stays that way. */
126 #undef DWARF2_UNWIND_INFO
127 #undef DWARF2_FRAME_INFO
128 #if (GCC_VERSION >= 3000)
129 #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
130 #endif
132 /* The size of the target's pointer type. */
133 #ifndef PTR_SIZE
134 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
135 #endif
137 /* Array of RTXes referenced by the debugging information, which therefore
138 must be kept around forever. */
139 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
141 /* A pointer to the base of a list of incomplete types which might be
142 completed at some later time. incomplete_types_list needs to be a
143 vec<tree, va_gc> *because we want to tell the garbage collector about
144 it. */
145 static GTY(()) vec<tree, va_gc> *incomplete_types;
147 /* A pointer to the base of a table of references to declaration
148 scopes. This table is a display which tracks the nesting
149 of declaration scopes at the current scope and containing
150 scopes. This table is used to find the proper place to
151 define type declaration DIE's. */
152 static GTY(()) vec<tree, va_gc> *decl_scope_table;
154 /* Pointers to various DWARF2 sections. */
155 static GTY(()) section *debug_info_section;
156 static GTY(()) section *debug_skeleton_info_section;
157 static GTY(()) section *debug_abbrev_section;
158 static GTY(()) section *debug_skeleton_abbrev_section;
159 static GTY(()) section *debug_aranges_section;
160 static GTY(()) section *debug_addr_section;
161 static GTY(()) section *debug_macinfo_section;
162 static GTY(()) section *debug_line_section;
163 static GTY(()) section *debug_skeleton_line_section;
164 static GTY(()) section *debug_loc_section;
165 static GTY(()) section *debug_pubnames_section;
166 static GTY(()) section *debug_pubtypes_section;
167 static GTY(()) section *debug_str_section;
168 static GTY(()) section *debug_str_dwo_section;
169 static GTY(()) section *debug_str_offsets_section;
170 static GTY(()) section *debug_ranges_section;
171 static GTY(()) section *debug_frame_section;
173 /* Maximum size (in bytes) of an artificially generated label. */
174 #define MAX_ARTIFICIAL_LABEL_BYTES 30
176 /* According to the (draft) DWARF 3 specification, the initial length
177 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
178 bytes are 0xffffffff, followed by the length stored in the next 8
179 bytes.
181 However, the SGI/MIPS ABI uses an initial length which is equal to
182 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
184 #ifndef DWARF_INITIAL_LENGTH_SIZE
185 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
186 #endif
188 /* Round SIZE up to the nearest BOUNDARY. */
189 #define DWARF_ROUND(SIZE,BOUNDARY) \
190 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
192 /* CIE identifier. */
193 #if HOST_BITS_PER_WIDE_INT >= 64
194 #define DWARF_CIE_ID \
195 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
196 #else
197 #define DWARF_CIE_ID DW_CIE_ID
198 #endif
201 /* A vector for a table that contains frame description
202 information for each routine. */
203 #define NOT_INDEXED (-1U)
204 #define NO_INDEX_ASSIGNED (-2U)
206 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
208 struct GTY(()) indirect_string_node {
209 const char *str;
210 unsigned int refcount;
211 enum dwarf_form form;
212 char *label;
213 unsigned int index;
216 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
218 /* With split_debug_info, both the comp_dir and dwo_name go in the
219 main object file, rather than the dwo, similar to the force_direct
220 parameter elsewhere but with additional complications:
222 1) The string is needed in both the main object file and the dwo.
223 That is, the comp_dir and dwo_name will appear in both places.
225 2) Strings can use three forms: DW_FORM_string, DW_FORM_strp or
226 DW_FORM_GNU_str_index.
228 3) GCC chooses the form to use late, depending on the size and
229 reference count.
231 Rather than forcing the all debug string handling functions and
232 callers to deal with these complications, simply use a separate,
233 special-cased string table for any attribute that should go in the
234 main object file. This limits the complexity to just the places
235 that need it. */
237 static GTY ((param_is (struct indirect_string_node)))
238 htab_t skeleton_debug_str_hash;
240 static GTY(()) int dw2_string_counter;
242 /* True if the compilation unit places functions in more than one section. */
243 static GTY(()) bool have_multiple_function_sections = false;
245 /* Whether the default text and cold text sections have been used at all. */
247 static GTY(()) bool text_section_used = false;
248 static GTY(()) bool cold_text_section_used = false;
250 /* The default cold text section. */
251 static GTY(()) section *cold_text_section;
253 /* The DIE for C++14 'auto' in a function return type. */
254 static GTY(()) dw_die_ref auto_die;
256 /* The DIE for C++14 'decltype(auto)' in a function return type. */
257 static GTY(()) dw_die_ref decltype_auto_die;
259 /* Forward declarations for functions defined in this file. */
261 static char *stripattributes (const char *);
262 static void output_call_frame_info (int);
263 static void dwarf2out_note_section_used (void);
265 /* Personality decl of current unit. Used only when assembler does not support
266 personality CFI. */
267 static GTY(()) rtx current_unit_personality;
269 /* Data and reference forms for relocatable data. */
270 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
271 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
273 #ifndef DEBUG_FRAME_SECTION
274 #define DEBUG_FRAME_SECTION ".debug_frame"
275 #endif
277 #ifndef FUNC_BEGIN_LABEL
278 #define FUNC_BEGIN_LABEL "LFB"
279 #endif
281 #ifndef FUNC_END_LABEL
282 #define FUNC_END_LABEL "LFE"
283 #endif
285 #ifndef PROLOGUE_END_LABEL
286 #define PROLOGUE_END_LABEL "LPE"
287 #endif
289 #ifndef EPILOGUE_BEGIN_LABEL
290 #define EPILOGUE_BEGIN_LABEL "LEB"
291 #endif
293 #ifndef FRAME_BEGIN_LABEL
294 #define FRAME_BEGIN_LABEL "Lframe"
295 #endif
296 #define CIE_AFTER_SIZE_LABEL "LSCIE"
297 #define CIE_END_LABEL "LECIE"
298 #define FDE_LABEL "LSFDE"
299 #define FDE_AFTER_SIZE_LABEL "LASFDE"
300 #define FDE_END_LABEL "LEFDE"
301 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
302 #define LINE_NUMBER_END_LABEL "LELT"
303 #define LN_PROLOG_AS_LABEL "LASLTP"
304 #define LN_PROLOG_END_LABEL "LELTP"
305 #define DIE_LABEL_PREFIX "DW"
307 /* Match the base name of a file to the base name of a compilation unit. */
309 static int
310 matches_main_base (const char *path)
312 /* Cache the last query. */
313 static const char *last_path = NULL;
314 static int last_match = 0;
315 if (path != last_path)
317 const char *base;
318 int length = base_of_path (path, &base);
319 last_path = path;
320 last_match = (length == main_input_baselength
321 && memcmp (base, main_input_basename, length) == 0);
323 return last_match;
326 #ifdef DEBUG_DEBUG_STRUCT
328 static int
329 dump_struct_debug (tree type, enum debug_info_usage usage,
330 enum debug_struct_file criterion, int generic,
331 int matches, int result)
333 /* Find the type name. */
334 tree type_decl = TYPE_STUB_DECL (type);
335 tree t = type_decl;
336 const char *name = 0;
337 if (TREE_CODE (t) == TYPE_DECL)
338 t = DECL_NAME (t);
339 if (t)
340 name = IDENTIFIER_POINTER (t);
342 fprintf (stderr, " struct %d %s %s %s %s %d %p %s\n",
343 criterion,
344 DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
345 matches ? "bas" : "hdr",
346 generic ? "gen" : "ord",
347 usage == DINFO_USAGE_DFN ? ";" :
348 usage == DINFO_USAGE_DIR_USE ? "." : "*",
349 result,
350 (void*) type_decl, name);
351 return result;
353 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
354 dump_struct_debug (type, usage, criterion, generic, matches, result)
356 #else
358 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
359 (result)
361 #endif
363 /* Get the number of HOST_WIDE_INTs needed to represent the precision
364 of the number. */
366 static unsigned int
367 get_full_len (const wide_int &op)
369 return ((op.get_precision () + HOST_BITS_PER_WIDE_INT - 1)
370 / HOST_BITS_PER_WIDE_INT);
373 static bool
374 should_emit_struct_debug (tree type, enum debug_info_usage usage)
376 enum debug_struct_file criterion;
377 tree type_decl;
378 bool generic = lang_hooks.types.generic_p (type);
380 if (generic)
381 criterion = debug_struct_generic[usage];
382 else
383 criterion = debug_struct_ordinary[usage];
385 if (criterion == DINFO_STRUCT_FILE_NONE)
386 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
387 if (criterion == DINFO_STRUCT_FILE_ANY)
388 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
390 type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
392 if (type_decl != NULL)
394 if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
395 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
397 if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
398 return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
401 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
404 /* Return a pointer to a copy of the section string name S with all
405 attributes stripped off, and an asterisk prepended (for assemble_name). */
407 static inline char *
408 stripattributes (const char *s)
410 char *stripped = XNEWVEC (char, strlen (s) + 2);
411 char *p = stripped;
413 *p++ = '*';
415 while (*s && *s != ',')
416 *p++ = *s++;
418 *p = '\0';
419 return stripped;
422 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
423 switch to the data section instead, and write out a synthetic start label
424 for collect2 the first time around. */
426 static void
427 switch_to_eh_frame_section (bool back)
429 tree label;
431 #ifdef EH_FRAME_SECTION_NAME
432 if (eh_frame_section == 0)
434 int flags;
436 if (EH_TABLES_CAN_BE_READ_ONLY)
438 int fde_encoding;
439 int per_encoding;
440 int lsda_encoding;
442 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
443 /*global=*/0);
444 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
445 /*global=*/1);
446 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
447 /*global=*/0);
448 flags = ((! flag_pic
449 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
450 && (fde_encoding & 0x70) != DW_EH_PE_aligned
451 && (per_encoding & 0x70) != DW_EH_PE_absptr
452 && (per_encoding & 0x70) != DW_EH_PE_aligned
453 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
454 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
455 ? 0 : SECTION_WRITE);
457 else
458 flags = SECTION_WRITE;
459 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
461 #endif /* EH_FRAME_SECTION_NAME */
463 if (eh_frame_section)
464 switch_to_section (eh_frame_section);
465 else
467 /* We have no special eh_frame section. Put the information in
468 the data section and emit special labels to guide collect2. */
469 switch_to_section (data_section);
471 if (!back)
473 label = get_file_function_name ("F");
474 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
475 targetm.asm_out.globalize_label (asm_out_file,
476 IDENTIFIER_POINTER (label));
477 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
482 /* Switch [BACK] to the eh or debug frame table section, depending on
483 FOR_EH. */
485 static void
486 switch_to_frame_table_section (int for_eh, bool back)
488 if (for_eh)
489 switch_to_eh_frame_section (back);
490 else
492 if (!debug_frame_section)
493 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
494 SECTION_DEBUG, NULL);
495 switch_to_section (debug_frame_section);
499 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
501 enum dw_cfi_oprnd_type
502 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
504 switch (cfi)
506 case DW_CFA_nop:
507 case DW_CFA_GNU_window_save:
508 case DW_CFA_remember_state:
509 case DW_CFA_restore_state:
510 return dw_cfi_oprnd_unused;
512 case DW_CFA_set_loc:
513 case DW_CFA_advance_loc1:
514 case DW_CFA_advance_loc2:
515 case DW_CFA_advance_loc4:
516 case DW_CFA_MIPS_advance_loc8:
517 return dw_cfi_oprnd_addr;
519 case DW_CFA_offset:
520 case DW_CFA_offset_extended:
521 case DW_CFA_def_cfa:
522 case DW_CFA_offset_extended_sf:
523 case DW_CFA_def_cfa_sf:
524 case DW_CFA_restore:
525 case DW_CFA_restore_extended:
526 case DW_CFA_undefined:
527 case DW_CFA_same_value:
528 case DW_CFA_def_cfa_register:
529 case DW_CFA_register:
530 case DW_CFA_expression:
531 return dw_cfi_oprnd_reg_num;
533 case DW_CFA_def_cfa_offset:
534 case DW_CFA_GNU_args_size:
535 case DW_CFA_def_cfa_offset_sf:
536 return dw_cfi_oprnd_offset;
538 case DW_CFA_def_cfa_expression:
539 return dw_cfi_oprnd_loc;
541 default:
542 gcc_unreachable ();
546 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
548 enum dw_cfi_oprnd_type
549 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
551 switch (cfi)
553 case DW_CFA_def_cfa:
554 case DW_CFA_def_cfa_sf:
555 case DW_CFA_offset:
556 case DW_CFA_offset_extended_sf:
557 case DW_CFA_offset_extended:
558 return dw_cfi_oprnd_offset;
560 case DW_CFA_register:
561 return dw_cfi_oprnd_reg_num;
563 case DW_CFA_expression:
564 return dw_cfi_oprnd_loc;
566 default:
567 return dw_cfi_oprnd_unused;
571 /* Output one FDE. */
573 static void
574 output_fde (dw_fde_ref fde, bool for_eh, bool second,
575 char *section_start_label, int fde_encoding, char *augmentation,
576 bool any_lsda_needed, int lsda_encoding)
578 const char *begin, *end;
579 static unsigned int j;
580 char l1[20], l2[20];
582 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
583 /* empty */ 0);
584 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
585 for_eh + j);
586 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
587 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
588 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
589 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
590 " indicating 64-bit DWARF extension");
591 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
592 "FDE Length");
593 ASM_OUTPUT_LABEL (asm_out_file, l1);
595 if (for_eh)
596 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
597 else
598 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
599 debug_frame_section, "FDE CIE offset");
601 begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
602 end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
604 if (for_eh)
606 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
607 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
608 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
609 "FDE initial location");
610 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
611 end, begin, "FDE address range");
613 else
615 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
616 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
619 if (augmentation[0])
621 if (any_lsda_needed)
623 int size = size_of_encoded_value (lsda_encoding);
625 if (lsda_encoding == DW_EH_PE_aligned)
627 int offset = ( 4 /* Length */
628 + 4 /* CIE offset */
629 + 2 * size_of_encoded_value (fde_encoding)
630 + 1 /* Augmentation size */ );
631 int pad = -offset & (PTR_SIZE - 1);
633 size += pad;
634 gcc_assert (size_of_uleb128 (size) == 1);
637 dw2_asm_output_data_uleb128 (size, "Augmentation size");
639 if (fde->uses_eh_lsda)
641 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
642 fde->funcdef_number);
643 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
644 gen_rtx_SYMBOL_REF (Pmode, l1),
645 false,
646 "Language Specific Data Area");
648 else
650 if (lsda_encoding == DW_EH_PE_aligned)
651 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
652 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
653 "Language Specific Data Area (none)");
656 else
657 dw2_asm_output_data_uleb128 (0, "Augmentation size");
660 /* Loop through the Call Frame Instructions associated with this FDE. */
661 fde->dw_fde_current_label = begin;
663 size_t from, until, i;
665 from = 0;
666 until = vec_safe_length (fde->dw_fde_cfi);
668 if (fde->dw_fde_second_begin == NULL)
670 else if (!second)
671 until = fde->dw_fde_switch_cfi_index;
672 else
673 from = fde->dw_fde_switch_cfi_index;
675 for (i = from; i < until; i++)
676 output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
679 /* If we are to emit a ref/link from function bodies to their frame tables,
680 do it now. This is typically performed to make sure that tables
681 associated with functions are dragged with them and not discarded in
682 garbage collecting links. We need to do this on a per function basis to
683 cope with -ffunction-sections. */
685 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
686 /* Switch to the function section, emit the ref to the tables, and
687 switch *back* into the table section. */
688 switch_to_section (function_section (fde->decl));
689 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
690 switch_to_frame_table_section (for_eh, true);
691 #endif
693 /* Pad the FDE out to an address sized boundary. */
694 ASM_OUTPUT_ALIGN (asm_out_file,
695 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
696 ASM_OUTPUT_LABEL (asm_out_file, l2);
698 j += 2;
701 /* Return true if frame description entry FDE is needed for EH. */
703 static bool
704 fde_needed_for_eh_p (dw_fde_ref fde)
706 if (flag_asynchronous_unwind_tables)
707 return true;
709 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
710 return true;
712 if (fde->uses_eh_lsda)
713 return true;
715 /* If exceptions are enabled, we have collected nothrow info. */
716 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
717 return false;
719 return true;
722 /* Output the call frame information used to record information
723 that relates to calculating the frame pointer, and records the
724 location of saved registers. */
726 static void
727 output_call_frame_info (int for_eh)
729 unsigned int i;
730 dw_fde_ref fde;
731 dw_cfi_ref cfi;
732 char l1[20], l2[20], section_start_label[20];
733 bool any_lsda_needed = false;
734 char augmentation[6];
735 int augmentation_size;
736 int fde_encoding = DW_EH_PE_absptr;
737 int per_encoding = DW_EH_PE_absptr;
738 int lsda_encoding = DW_EH_PE_absptr;
739 int return_reg;
740 rtx personality = NULL;
741 int dw_cie_version;
743 /* Don't emit a CIE if there won't be any FDEs. */
744 if (!fde_vec)
745 return;
747 /* Nothing to do if the assembler's doing it all. */
748 if (dwarf2out_do_cfi_asm ())
749 return;
751 /* If we don't have any functions we'll want to unwind out of, don't emit
752 any EH unwind information. If we make FDEs linkonce, we may have to
753 emit an empty label for an FDE that wouldn't otherwise be emitted. We
754 want to avoid having an FDE kept around when the function it refers to
755 is discarded. Example where this matters: a primary function template
756 in C++ requires EH information, an explicit specialization doesn't. */
757 if (for_eh)
759 bool any_eh_needed = false;
761 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
763 if (fde->uses_eh_lsda)
764 any_eh_needed = any_lsda_needed = true;
765 else if (fde_needed_for_eh_p (fde))
766 any_eh_needed = true;
767 else if (TARGET_USES_WEAK_UNWIND_INFO)
768 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
771 if (!any_eh_needed)
772 return;
775 /* We're going to be generating comments, so turn on app. */
776 if (flag_debug_asm)
777 app_enable ();
779 /* Switch to the proper frame section, first time. */
780 switch_to_frame_table_section (for_eh, false);
782 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
783 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
785 /* Output the CIE. */
786 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
787 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
788 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
789 dw2_asm_output_data (4, 0xffffffff,
790 "Initial length escape value indicating 64-bit DWARF extension");
791 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
792 "Length of Common Information Entry");
793 ASM_OUTPUT_LABEL (asm_out_file, l1);
795 /* Now that the CIE pointer is PC-relative for EH,
796 use 0 to identify the CIE. */
797 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
798 (for_eh ? 0 : DWARF_CIE_ID),
799 "CIE Identifier Tag");
801 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
802 use CIE version 1, unless that would produce incorrect results
803 due to overflowing the return register column. */
804 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
805 dw_cie_version = 1;
806 if (return_reg >= 256 || dwarf_version > 2)
807 dw_cie_version = 3;
808 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
810 augmentation[0] = 0;
811 augmentation_size = 0;
813 personality = current_unit_personality;
814 if (for_eh)
816 char *p;
818 /* Augmentation:
819 z Indicates that a uleb128 is present to size the
820 augmentation section.
821 L Indicates the encoding (and thus presence) of
822 an LSDA pointer in the FDE augmentation.
823 R Indicates a non-default pointer encoding for
824 FDE code pointers.
825 P Indicates the presence of an encoding + language
826 personality routine in the CIE augmentation. */
828 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
829 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
830 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
832 p = augmentation + 1;
833 if (personality)
835 *p++ = 'P';
836 augmentation_size += 1 + size_of_encoded_value (per_encoding);
837 assemble_external_libcall (personality);
839 if (any_lsda_needed)
841 *p++ = 'L';
842 augmentation_size += 1;
844 if (fde_encoding != DW_EH_PE_absptr)
846 *p++ = 'R';
847 augmentation_size += 1;
849 if (p > augmentation + 1)
851 augmentation[0] = 'z';
852 *p = '\0';
855 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
856 if (personality && per_encoding == DW_EH_PE_aligned)
858 int offset = ( 4 /* Length */
859 + 4 /* CIE Id */
860 + 1 /* CIE version */
861 + strlen (augmentation) + 1 /* Augmentation */
862 + size_of_uleb128 (1) /* Code alignment */
863 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
864 + 1 /* RA column */
865 + 1 /* Augmentation size */
866 + 1 /* Personality encoding */ );
867 int pad = -offset & (PTR_SIZE - 1);
869 augmentation_size += pad;
871 /* Augmentations should be small, so there's scarce need to
872 iterate for a solution. Die if we exceed one uleb128 byte. */
873 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
877 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
878 if (dw_cie_version >= 4)
880 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
881 dw2_asm_output_data (1, 0, "CIE Segment Size");
883 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
884 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
885 "CIE Data Alignment Factor");
887 if (dw_cie_version == 1)
888 dw2_asm_output_data (1, return_reg, "CIE RA Column");
889 else
890 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
892 if (augmentation[0])
894 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
895 if (personality)
897 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
898 eh_data_format_name (per_encoding));
899 dw2_asm_output_encoded_addr_rtx (per_encoding,
900 personality,
901 true, NULL);
904 if (any_lsda_needed)
905 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
906 eh_data_format_name (lsda_encoding));
908 if (fde_encoding != DW_EH_PE_absptr)
909 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
910 eh_data_format_name (fde_encoding));
913 FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
914 output_cfi (cfi, NULL, for_eh);
916 /* Pad the CIE out to an address sized boundary. */
917 ASM_OUTPUT_ALIGN (asm_out_file,
918 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
919 ASM_OUTPUT_LABEL (asm_out_file, l2);
921 /* Loop through all of the FDE's. */
922 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
924 unsigned int k;
926 /* Don't emit EH unwind info for leaf functions that don't need it. */
927 if (for_eh && !fde_needed_for_eh_p (fde))
928 continue;
930 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
931 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
932 augmentation, any_lsda_needed, lsda_encoding);
935 if (for_eh && targetm.terminate_dw2_eh_frame_info)
936 dw2_asm_output_data (4, 0, "End of Table");
938 /* Turn off app to make assembly quicker. */
939 if (flag_debug_asm)
940 app_disable ();
943 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
945 static void
946 dwarf2out_do_cfi_startproc (bool second)
948 int enc;
949 rtx ref;
950 rtx personality = get_personality_function (current_function_decl);
952 fprintf (asm_out_file, "\t.cfi_startproc\n");
954 if (personality)
956 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
957 ref = personality;
959 /* ??? The GAS support isn't entirely consistent. We have to
960 handle indirect support ourselves, but PC-relative is done
961 in the assembler. Further, the assembler can't handle any
962 of the weirder relocation types. */
963 if (enc & DW_EH_PE_indirect)
964 ref = dw2_force_const_mem (ref, true);
966 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
967 output_addr_const (asm_out_file, ref);
968 fputc ('\n', asm_out_file);
971 if (crtl->uses_eh_lsda)
973 char lab[20];
975 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
976 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
977 current_function_funcdef_no);
978 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
979 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
981 if (enc & DW_EH_PE_indirect)
982 ref = dw2_force_const_mem (ref, true);
984 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
985 output_addr_const (asm_out_file, ref);
986 fputc ('\n', asm_out_file);
990 /* Allocate CURRENT_FDE. Immediately initialize all we can, noting that
991 this allocation may be done before pass_final. */
993 dw_fde_ref
994 dwarf2out_alloc_current_fde (void)
996 dw_fde_ref fde;
998 fde = ggc_cleared_alloc<dw_fde_node> ();
999 fde->decl = current_function_decl;
1000 fde->funcdef_number = current_function_funcdef_no;
1001 fde->fde_index = vec_safe_length (fde_vec);
1002 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
1003 fde->uses_eh_lsda = crtl->uses_eh_lsda;
1004 fde->nothrow = crtl->nothrow;
1005 fde->drap_reg = INVALID_REGNUM;
1006 fde->vdrap_reg = INVALID_REGNUM;
1008 /* Record the FDE associated with this function. */
1009 cfun->fde = fde;
1010 vec_safe_push (fde_vec, fde);
1012 return fde;
1015 /* Output a marker (i.e. a label) for the beginning of a function, before
1016 the prologue. */
1018 void
1019 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1020 const char *file ATTRIBUTE_UNUSED)
1022 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1023 char * dup_label;
1024 dw_fde_ref fde;
1025 section *fnsec;
1026 bool do_frame;
1028 current_function_func_begin_label = NULL;
1030 do_frame = dwarf2out_do_frame ();
1032 /* ??? current_function_func_begin_label is also used by except.c for
1033 call-site information. We must emit this label if it might be used. */
1034 if (!do_frame
1035 && (!flag_exceptions
1036 || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1037 return;
1039 fnsec = function_section (current_function_decl);
1040 switch_to_section (fnsec);
1041 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1042 current_function_funcdef_no);
1043 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1044 current_function_funcdef_no);
1045 dup_label = xstrdup (label);
1046 current_function_func_begin_label = dup_label;
1048 /* We can elide the fde allocation if we're not emitting debug info. */
1049 if (!do_frame)
1050 return;
1052 /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1053 emit insns as rtx but bypass the bulk of rest_of_compilation, which
1054 would include pass_dwarf2_frame. If we've not created the FDE yet,
1055 do so now. */
1056 fde = cfun->fde;
1057 if (fde == NULL)
1058 fde = dwarf2out_alloc_current_fde ();
1060 /* Initialize the bits of CURRENT_FDE that were not available earlier. */
1061 fde->dw_fde_begin = dup_label;
1062 fde->dw_fde_current_label = dup_label;
1063 fde->in_std_section = (fnsec == text_section
1064 || (cold_text_section && fnsec == cold_text_section));
1066 /* We only want to output line number information for the genuine dwarf2
1067 prologue case, not the eh frame case. */
1068 #ifdef DWARF2_DEBUGGING_INFO
1069 if (file)
1070 dwarf2out_source_line (line, file, 0, true);
1071 #endif
1073 if (dwarf2out_do_cfi_asm ())
1074 dwarf2out_do_cfi_startproc (false);
1075 else
1077 rtx personality = get_personality_function (current_function_decl);
1078 if (!current_unit_personality)
1079 current_unit_personality = personality;
1081 /* We cannot keep a current personality per function as without CFI
1082 asm, at the point where we emit the CFI data, there is no current
1083 function anymore. */
1084 if (personality && current_unit_personality != personality)
1085 sorry ("multiple EH personalities are supported only with assemblers "
1086 "supporting .cfi_personality directive");
1090 /* Output a marker (i.e. a label) for the end of the generated code
1091 for a function prologue. This gets called *after* the prologue code has
1092 been generated. */
1094 void
1095 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1096 const char *file ATTRIBUTE_UNUSED)
1098 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1100 /* Output a label to mark the endpoint of the code generated for this
1101 function. */
1102 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1103 current_function_funcdef_no);
1104 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1105 current_function_funcdef_no);
1106 cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1109 /* Output a marker (i.e. a label) for the beginning of the generated code
1110 for a function epilogue. This gets called *before* the prologue code has
1111 been generated. */
1113 void
1114 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1115 const char *file ATTRIBUTE_UNUSED)
1117 dw_fde_ref fde = cfun->fde;
1118 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1120 if (fde->dw_fde_vms_begin_epilogue)
1121 return;
1123 /* Output a label to mark the endpoint of the code generated for this
1124 function. */
1125 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1126 current_function_funcdef_no);
1127 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1128 current_function_funcdef_no);
1129 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1132 /* Output a marker (i.e. a label) for the absolute end of the generated code
1133 for a function definition. This gets called *after* the epilogue code has
1134 been generated. */
1136 void
1137 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1138 const char *file ATTRIBUTE_UNUSED)
1140 dw_fde_ref fde;
1141 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1143 last_var_location_insn = NULL;
1144 cached_next_real_insn = NULL;
1146 if (dwarf2out_do_cfi_asm ())
1147 fprintf (asm_out_file, "\t.cfi_endproc\n");
1149 /* Output a label to mark the endpoint of the code generated for this
1150 function. */
1151 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1152 current_function_funcdef_no);
1153 ASM_OUTPUT_LABEL (asm_out_file, label);
1154 fde = cfun->fde;
1155 gcc_assert (fde != NULL);
1156 if (fde->dw_fde_second_begin == NULL)
1157 fde->dw_fde_end = xstrdup (label);
1160 void
1161 dwarf2out_frame_finish (void)
1163 /* Output call frame information. */
1164 if (targetm.debug_unwind_info () == UI_DWARF2)
1165 output_call_frame_info (0);
1167 /* Output another copy for the unwinder. */
1168 if ((flag_unwind_tables || flag_exceptions)
1169 && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1170 output_call_frame_info (1);
1173 /* Note that the current function section is being used for code. */
1175 static void
1176 dwarf2out_note_section_used (void)
1178 section *sec = current_function_section ();
1179 if (sec == text_section)
1180 text_section_used = true;
1181 else if (sec == cold_text_section)
1182 cold_text_section_used = true;
1185 static void var_location_switch_text_section (void);
1186 static void set_cur_line_info_table (section *);
1188 void
1189 dwarf2out_switch_text_section (void)
1191 section *sect;
1192 dw_fde_ref fde = cfun->fde;
1194 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1196 if (!in_cold_section_p)
1198 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1199 fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1200 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1202 else
1204 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1205 fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1206 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1208 have_multiple_function_sections = true;
1210 /* There is no need to mark used sections when not debugging. */
1211 if (cold_text_section != NULL)
1212 dwarf2out_note_section_used ();
1214 if (dwarf2out_do_cfi_asm ())
1215 fprintf (asm_out_file, "\t.cfi_endproc\n");
1217 /* Now do the real section switch. */
1218 sect = current_function_section ();
1219 switch_to_section (sect);
1221 fde->second_in_std_section
1222 = (sect == text_section
1223 || (cold_text_section && sect == cold_text_section));
1225 if (dwarf2out_do_cfi_asm ())
1226 dwarf2out_do_cfi_startproc (true);
1228 var_location_switch_text_section ();
1230 if (cold_text_section != NULL)
1231 set_cur_line_info_table (sect);
1234 /* And now, the subset of the debugging information support code necessary
1235 for emitting location expressions. */
1237 /* Data about a single source file. */
1238 struct GTY(()) dwarf_file_data {
1239 const char * filename;
1240 int emitted_number;
1243 typedef struct GTY(()) deferred_locations_struct
1245 tree variable;
1246 dw_die_ref die;
1247 } deferred_locations;
1250 static GTY(()) vec<deferred_locations, va_gc> *deferred_locations_list;
1253 /* Describe an entry into the .debug_addr section. */
1255 enum ate_kind {
1256 ate_kind_rtx,
1257 ate_kind_rtx_dtprel,
1258 ate_kind_label
1261 typedef struct GTY(()) addr_table_entry_struct {
1262 enum ate_kind kind;
1263 unsigned int refcount;
1264 unsigned int index;
1265 union addr_table_entry_struct_union
1267 rtx GTY ((tag ("0"))) rtl;
1268 char * GTY ((tag ("1"))) label;
1270 GTY ((desc ("%1.kind"))) addr;
1272 addr_table_entry;
1274 /* Location lists are ranges + location descriptions for that range,
1275 so you can track variables that are in different places over
1276 their entire life. */
1277 typedef struct GTY(()) dw_loc_list_struct {
1278 dw_loc_list_ref dw_loc_next;
1279 const char *begin; /* Label and addr_entry for start of range */
1280 addr_table_entry *begin_entry;
1281 const char *end; /* Label for end of range */
1282 char *ll_symbol; /* Label for beginning of location list.
1283 Only on head of list */
1284 const char *section; /* Section this loclist is relative to */
1285 dw_loc_descr_ref expr;
1286 hashval_t hash;
1287 /* True if all addresses in this and subsequent lists are known to be
1288 resolved. */
1289 bool resolved_addr;
1290 /* True if this list has been replaced by dw_loc_next. */
1291 bool replaced;
1292 bool emitted;
1293 /* True if the range should be emitted even if begin and end
1294 are the same. */
1295 bool force;
1296 } dw_loc_list_node;
1298 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1300 /* Convert a DWARF stack opcode into its string name. */
1302 static const char *
1303 dwarf_stack_op_name (unsigned int op)
1305 const char *name = get_DW_OP_name (op);
1307 if (name != NULL)
1308 return name;
1310 return "OP_<unknown>";
1313 /* Return a pointer to a newly allocated location description. Location
1314 descriptions are simple expression terms that can be strung
1315 together to form more complicated location (address) descriptions. */
1317 static inline dw_loc_descr_ref
1318 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1319 unsigned HOST_WIDE_INT oprnd2)
1321 dw_loc_descr_ref descr = ggc_cleared_alloc<dw_loc_descr_node> ();
1323 descr->dw_loc_opc = op;
1324 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1325 descr->dw_loc_oprnd1.val_entry = NULL;
1326 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1327 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1328 descr->dw_loc_oprnd2.val_entry = NULL;
1329 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1331 return descr;
1334 /* Return a pointer to a newly allocated location description for
1335 REG and OFFSET. */
1337 static inline dw_loc_descr_ref
1338 new_reg_loc_descr (unsigned int reg, unsigned HOST_WIDE_INT offset)
1340 if (reg <= 31)
1341 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1342 offset, 0);
1343 else
1344 return new_loc_descr (DW_OP_bregx, reg, offset);
1347 /* Add a location description term to a location description expression. */
1349 static inline void
1350 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1352 dw_loc_descr_ref *d;
1354 /* Find the end of the chain. */
1355 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1358 *d = descr;
1361 /* Compare two location operands for exact equality. */
1363 static bool
1364 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1366 if (a->val_class != b->val_class)
1367 return false;
1368 switch (a->val_class)
1370 case dw_val_class_none:
1371 return true;
1372 case dw_val_class_addr:
1373 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1375 case dw_val_class_offset:
1376 case dw_val_class_unsigned_const:
1377 case dw_val_class_const:
1378 case dw_val_class_range_list:
1379 case dw_val_class_lineptr:
1380 case dw_val_class_macptr:
1381 /* These are all HOST_WIDE_INT, signed or unsigned. */
1382 return a->v.val_unsigned == b->v.val_unsigned;
1384 case dw_val_class_loc:
1385 return a->v.val_loc == b->v.val_loc;
1386 case dw_val_class_loc_list:
1387 return a->v.val_loc_list == b->v.val_loc_list;
1388 case dw_val_class_die_ref:
1389 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1390 case dw_val_class_fde_ref:
1391 return a->v.val_fde_index == b->v.val_fde_index;
1392 case dw_val_class_lbl_id:
1393 case dw_val_class_high_pc:
1394 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1395 case dw_val_class_str:
1396 return a->v.val_str == b->v.val_str;
1397 case dw_val_class_flag:
1398 return a->v.val_flag == b->v.val_flag;
1399 case dw_val_class_file:
1400 return a->v.val_file == b->v.val_file;
1401 case dw_val_class_decl_ref:
1402 return a->v.val_decl_ref == b->v.val_decl_ref;
1404 case dw_val_class_const_double:
1405 return (a->v.val_double.high == b->v.val_double.high
1406 && a->v.val_double.low == b->v.val_double.low);
1408 case dw_val_class_wide_int:
1409 return *a->v.val_wide == *b->v.val_wide;
1411 case dw_val_class_vec:
1413 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1414 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1416 return (a_len == b_len
1417 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1420 case dw_val_class_data8:
1421 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1423 case dw_val_class_vms_delta:
1424 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1425 && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1427 gcc_unreachable ();
1430 /* Compare two location atoms for exact equality. */
1432 static bool
1433 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1435 if (a->dw_loc_opc != b->dw_loc_opc)
1436 return false;
1438 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1439 address size, but since we always allocate cleared storage it
1440 should be zero for other types of locations. */
1441 if (a->dtprel != b->dtprel)
1442 return false;
1444 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1445 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1448 /* Compare two complete location expressions for exact equality. */
1450 bool
1451 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1453 while (1)
1455 if (a == b)
1456 return true;
1457 if (a == NULL || b == NULL)
1458 return false;
1459 if (!loc_descr_equal_p_1 (a, b))
1460 return false;
1462 a = a->dw_loc_next;
1463 b = b->dw_loc_next;
1468 /* Add a constant OFFSET to a location expression. */
1470 static void
1471 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1473 dw_loc_descr_ref loc;
1474 HOST_WIDE_INT *p;
1476 gcc_assert (*list_head != NULL);
1478 if (!offset)
1479 return;
1481 /* Find the end of the chain. */
1482 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1485 p = NULL;
1486 if (loc->dw_loc_opc == DW_OP_fbreg
1487 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1488 p = &loc->dw_loc_oprnd1.v.val_int;
1489 else if (loc->dw_loc_opc == DW_OP_bregx)
1490 p = &loc->dw_loc_oprnd2.v.val_int;
1492 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1493 offset. Don't optimize if an signed integer overflow would happen. */
1494 if (p != NULL
1495 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1496 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1497 *p += offset;
1499 else if (offset > 0)
1500 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1502 else
1504 loc->dw_loc_next = int_loc_descriptor (-offset);
1505 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1509 /* Add a constant OFFSET to a location list. */
1511 static void
1512 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1514 dw_loc_list_ref d;
1515 for (d = list_head; d != NULL; d = d->dw_loc_next)
1516 loc_descr_plus_const (&d->expr, offset);
1519 #define DWARF_REF_SIZE \
1520 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1522 static unsigned long int get_base_type_offset (dw_die_ref);
1524 /* Return the size of a location descriptor. */
1526 static unsigned long
1527 size_of_loc_descr (dw_loc_descr_ref loc)
1529 unsigned long size = 1;
1531 switch (loc->dw_loc_opc)
1533 case DW_OP_addr:
1534 size += DWARF2_ADDR_SIZE;
1535 break;
1536 case DW_OP_GNU_addr_index:
1537 case DW_OP_GNU_const_index:
1538 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1539 size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1540 break;
1541 case DW_OP_const1u:
1542 case DW_OP_const1s:
1543 size += 1;
1544 break;
1545 case DW_OP_const2u:
1546 case DW_OP_const2s:
1547 size += 2;
1548 break;
1549 case DW_OP_const4u:
1550 case DW_OP_const4s:
1551 size += 4;
1552 break;
1553 case DW_OP_const8u:
1554 case DW_OP_const8s:
1555 size += 8;
1556 break;
1557 case DW_OP_constu:
1558 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1559 break;
1560 case DW_OP_consts:
1561 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1562 break;
1563 case DW_OP_pick:
1564 size += 1;
1565 break;
1566 case DW_OP_plus_uconst:
1567 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1568 break;
1569 case DW_OP_skip:
1570 case DW_OP_bra:
1571 size += 2;
1572 break;
1573 case DW_OP_breg0:
1574 case DW_OP_breg1:
1575 case DW_OP_breg2:
1576 case DW_OP_breg3:
1577 case DW_OP_breg4:
1578 case DW_OP_breg5:
1579 case DW_OP_breg6:
1580 case DW_OP_breg7:
1581 case DW_OP_breg8:
1582 case DW_OP_breg9:
1583 case DW_OP_breg10:
1584 case DW_OP_breg11:
1585 case DW_OP_breg12:
1586 case DW_OP_breg13:
1587 case DW_OP_breg14:
1588 case DW_OP_breg15:
1589 case DW_OP_breg16:
1590 case DW_OP_breg17:
1591 case DW_OP_breg18:
1592 case DW_OP_breg19:
1593 case DW_OP_breg20:
1594 case DW_OP_breg21:
1595 case DW_OP_breg22:
1596 case DW_OP_breg23:
1597 case DW_OP_breg24:
1598 case DW_OP_breg25:
1599 case DW_OP_breg26:
1600 case DW_OP_breg27:
1601 case DW_OP_breg28:
1602 case DW_OP_breg29:
1603 case DW_OP_breg30:
1604 case DW_OP_breg31:
1605 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1606 break;
1607 case DW_OP_regx:
1608 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1609 break;
1610 case DW_OP_fbreg:
1611 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1612 break;
1613 case DW_OP_bregx:
1614 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1615 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1616 break;
1617 case DW_OP_piece:
1618 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1619 break;
1620 case DW_OP_bit_piece:
1621 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1622 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1623 break;
1624 case DW_OP_deref_size:
1625 case DW_OP_xderef_size:
1626 size += 1;
1627 break;
1628 case DW_OP_call2:
1629 size += 2;
1630 break;
1631 case DW_OP_call4:
1632 size += 4;
1633 break;
1634 case DW_OP_call_ref:
1635 size += DWARF_REF_SIZE;
1636 break;
1637 case DW_OP_implicit_value:
1638 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1639 + loc->dw_loc_oprnd1.v.val_unsigned;
1640 break;
1641 case DW_OP_GNU_implicit_pointer:
1642 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1643 break;
1644 case DW_OP_GNU_entry_value:
1646 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1647 size += size_of_uleb128 (op_size) + op_size;
1648 break;
1650 case DW_OP_GNU_const_type:
1652 unsigned long o
1653 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1654 size += size_of_uleb128 (o) + 1;
1655 switch (loc->dw_loc_oprnd2.val_class)
1657 case dw_val_class_vec:
1658 size += loc->dw_loc_oprnd2.v.val_vec.length
1659 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1660 break;
1661 case dw_val_class_const:
1662 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1663 break;
1664 case dw_val_class_const_double:
1665 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1666 break;
1667 case dw_val_class_wide_int:
1668 size += (get_full_len (*loc->dw_loc_oprnd2.v.val_wide)
1669 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
1670 break;
1671 default:
1672 gcc_unreachable ();
1674 break;
1676 case DW_OP_GNU_regval_type:
1678 unsigned long o
1679 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1680 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1681 + size_of_uleb128 (o);
1683 break;
1684 case DW_OP_GNU_deref_type:
1686 unsigned long o
1687 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1688 size += 1 + size_of_uleb128 (o);
1690 break;
1691 case DW_OP_GNU_convert:
1692 case DW_OP_GNU_reinterpret:
1693 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1694 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1695 else
1697 unsigned long o
1698 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1699 size += size_of_uleb128 (o);
1701 break;
1702 case DW_OP_GNU_parameter_ref:
1703 size += 4;
1704 break;
1705 default:
1706 break;
1709 return size;
1712 /* Return the size of a series of location descriptors. */
1714 unsigned long
1715 size_of_locs (dw_loc_descr_ref loc)
1717 dw_loc_descr_ref l;
1718 unsigned long size;
1720 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1721 field, to avoid writing to a PCH file. */
1722 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1724 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1725 break;
1726 size += size_of_loc_descr (l);
1728 if (! l)
1729 return size;
1731 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1733 l->dw_loc_addr = size;
1734 size += size_of_loc_descr (l);
1737 return size;
1740 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1741 static void get_ref_die_offset_label (char *, dw_die_ref);
1742 static unsigned long int get_ref_die_offset (dw_die_ref);
1744 /* Output location description stack opcode's operands (if any).
1745 The for_eh_or_skip parameter controls whether register numbers are
1746 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1747 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1748 info). This should be suppressed for the cases that have not been converted
1749 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
1751 static void
1752 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1754 dw_val_ref val1 = &loc->dw_loc_oprnd1;
1755 dw_val_ref val2 = &loc->dw_loc_oprnd2;
1757 switch (loc->dw_loc_opc)
1759 #ifdef DWARF2_DEBUGGING_INFO
1760 case DW_OP_const2u:
1761 case DW_OP_const2s:
1762 dw2_asm_output_data (2, val1->v.val_int, NULL);
1763 break;
1764 case DW_OP_const4u:
1765 if (loc->dtprel)
1767 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1768 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1769 val1->v.val_addr);
1770 fputc ('\n', asm_out_file);
1771 break;
1773 /* FALLTHRU */
1774 case DW_OP_const4s:
1775 dw2_asm_output_data (4, val1->v.val_int, NULL);
1776 break;
1777 case DW_OP_const8u:
1778 if (loc->dtprel)
1780 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1781 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1782 val1->v.val_addr);
1783 fputc ('\n', asm_out_file);
1784 break;
1786 /* FALLTHRU */
1787 case DW_OP_const8s:
1788 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
1789 dw2_asm_output_data (8, val1->v.val_int, NULL);
1790 break;
1791 case DW_OP_skip:
1792 case DW_OP_bra:
1794 int offset;
1796 gcc_assert (val1->val_class == dw_val_class_loc);
1797 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
1799 dw2_asm_output_data (2, offset, NULL);
1801 break;
1802 case DW_OP_implicit_value:
1803 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1804 switch (val2->val_class)
1806 case dw_val_class_const:
1807 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
1808 break;
1809 case dw_val_class_vec:
1811 unsigned int elt_size = val2->v.val_vec.elt_size;
1812 unsigned int len = val2->v.val_vec.length;
1813 unsigned int i;
1814 unsigned char *p;
1816 if (elt_size > sizeof (HOST_WIDE_INT))
1818 elt_size /= 2;
1819 len *= 2;
1821 for (i = 0, p = val2->v.val_vec.array;
1822 i < len;
1823 i++, p += elt_size)
1824 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
1825 "fp or vector constant word %u", i);
1827 break;
1828 case dw_val_class_const_double:
1830 unsigned HOST_WIDE_INT first, second;
1832 if (WORDS_BIG_ENDIAN)
1834 first = val2->v.val_double.high;
1835 second = val2->v.val_double.low;
1837 else
1839 first = val2->v.val_double.low;
1840 second = val2->v.val_double.high;
1842 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1843 first, NULL);
1844 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1845 second, NULL);
1847 break;
1848 case dw_val_class_wide_int:
1850 int i;
1851 int len = get_full_len (*val2->v.val_wide);
1852 if (WORDS_BIG_ENDIAN)
1853 for (i = len - 1; i >= 0; --i)
1854 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1855 val2->v.val_wide->elt (i), NULL);
1856 else
1857 for (i = 0; i < len; ++i)
1858 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1859 val2->v.val_wide->elt (i), NULL);
1861 break;
1862 case dw_val_class_addr:
1863 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
1864 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
1865 break;
1866 default:
1867 gcc_unreachable ();
1869 break;
1870 #else
1871 case DW_OP_const2u:
1872 case DW_OP_const2s:
1873 case DW_OP_const4u:
1874 case DW_OP_const4s:
1875 case DW_OP_const8u:
1876 case DW_OP_const8s:
1877 case DW_OP_skip:
1878 case DW_OP_bra:
1879 case DW_OP_implicit_value:
1880 /* We currently don't make any attempt to make sure these are
1881 aligned properly like we do for the main unwind info, so
1882 don't support emitting things larger than a byte if we're
1883 only doing unwinding. */
1884 gcc_unreachable ();
1885 #endif
1886 case DW_OP_const1u:
1887 case DW_OP_const1s:
1888 dw2_asm_output_data (1, val1->v.val_int, NULL);
1889 break;
1890 case DW_OP_constu:
1891 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1892 break;
1893 case DW_OP_consts:
1894 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1895 break;
1896 case DW_OP_pick:
1897 dw2_asm_output_data (1, val1->v.val_int, NULL);
1898 break;
1899 case DW_OP_plus_uconst:
1900 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1901 break;
1902 case DW_OP_breg0:
1903 case DW_OP_breg1:
1904 case DW_OP_breg2:
1905 case DW_OP_breg3:
1906 case DW_OP_breg4:
1907 case DW_OP_breg5:
1908 case DW_OP_breg6:
1909 case DW_OP_breg7:
1910 case DW_OP_breg8:
1911 case DW_OP_breg9:
1912 case DW_OP_breg10:
1913 case DW_OP_breg11:
1914 case DW_OP_breg12:
1915 case DW_OP_breg13:
1916 case DW_OP_breg14:
1917 case DW_OP_breg15:
1918 case DW_OP_breg16:
1919 case DW_OP_breg17:
1920 case DW_OP_breg18:
1921 case DW_OP_breg19:
1922 case DW_OP_breg20:
1923 case DW_OP_breg21:
1924 case DW_OP_breg22:
1925 case DW_OP_breg23:
1926 case DW_OP_breg24:
1927 case DW_OP_breg25:
1928 case DW_OP_breg26:
1929 case DW_OP_breg27:
1930 case DW_OP_breg28:
1931 case DW_OP_breg29:
1932 case DW_OP_breg30:
1933 case DW_OP_breg31:
1934 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1935 break;
1936 case DW_OP_regx:
1938 unsigned r = val1->v.val_unsigned;
1939 if (for_eh_or_skip >= 0)
1940 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1941 gcc_assert (size_of_uleb128 (r)
1942 == size_of_uleb128 (val1->v.val_unsigned));
1943 dw2_asm_output_data_uleb128 (r, NULL);
1945 break;
1946 case DW_OP_fbreg:
1947 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1948 break;
1949 case DW_OP_bregx:
1951 unsigned r = val1->v.val_unsigned;
1952 if (for_eh_or_skip >= 0)
1953 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1954 gcc_assert (size_of_uleb128 (r)
1955 == size_of_uleb128 (val1->v.val_unsigned));
1956 dw2_asm_output_data_uleb128 (r, NULL);
1957 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
1959 break;
1960 case DW_OP_piece:
1961 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1962 break;
1963 case DW_OP_bit_piece:
1964 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1965 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
1966 break;
1967 case DW_OP_deref_size:
1968 case DW_OP_xderef_size:
1969 dw2_asm_output_data (1, val1->v.val_int, NULL);
1970 break;
1972 case DW_OP_addr:
1973 if (loc->dtprel)
1975 if (targetm.asm_out.output_dwarf_dtprel)
1977 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
1978 DWARF2_ADDR_SIZE,
1979 val1->v.val_addr);
1980 fputc ('\n', asm_out_file);
1982 else
1983 gcc_unreachable ();
1985 else
1987 #ifdef DWARF2_DEBUGGING_INFO
1988 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
1989 #else
1990 gcc_unreachable ();
1991 #endif
1993 break;
1995 case DW_OP_GNU_addr_index:
1996 case DW_OP_GNU_const_index:
1997 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1998 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
1999 "(index into .debug_addr)");
2000 break;
2002 case DW_OP_GNU_implicit_pointer:
2004 char label[MAX_ARTIFICIAL_LABEL_BYTES
2005 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2006 gcc_assert (val1->val_class == dw_val_class_die_ref);
2007 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2008 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2009 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2011 break;
2013 case DW_OP_GNU_entry_value:
2014 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2015 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2016 break;
2018 case DW_OP_GNU_const_type:
2020 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2021 gcc_assert (o);
2022 dw2_asm_output_data_uleb128 (o, NULL);
2023 switch (val2->val_class)
2025 case dw_val_class_const:
2026 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2027 dw2_asm_output_data (1, l, NULL);
2028 dw2_asm_output_data (l, val2->v.val_int, NULL);
2029 break;
2030 case dw_val_class_vec:
2032 unsigned int elt_size = val2->v.val_vec.elt_size;
2033 unsigned int len = val2->v.val_vec.length;
2034 unsigned int i;
2035 unsigned char *p;
2037 l = len * elt_size;
2038 dw2_asm_output_data (1, l, NULL);
2039 if (elt_size > sizeof (HOST_WIDE_INT))
2041 elt_size /= 2;
2042 len *= 2;
2044 for (i = 0, p = val2->v.val_vec.array;
2045 i < len;
2046 i++, p += elt_size)
2047 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2048 "fp or vector constant word %u", i);
2050 break;
2051 case dw_val_class_const_double:
2053 unsigned HOST_WIDE_INT first, second;
2054 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2056 dw2_asm_output_data (1, 2 * l, NULL);
2057 if (WORDS_BIG_ENDIAN)
2059 first = val2->v.val_double.high;
2060 second = val2->v.val_double.low;
2062 else
2064 first = val2->v.val_double.low;
2065 second = val2->v.val_double.high;
2067 dw2_asm_output_data (l, first, NULL);
2068 dw2_asm_output_data (l, second, NULL);
2070 break;
2071 case dw_val_class_wide_int:
2073 int i;
2074 int len = get_full_len (*val2->v.val_wide);
2075 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2077 dw2_asm_output_data (1, len * l, NULL);
2078 if (WORDS_BIG_ENDIAN)
2079 for (i = len - 1; i >= 0; --i)
2080 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2081 else
2082 for (i = 0; i < len; ++i)
2083 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2085 break;
2086 default:
2087 gcc_unreachable ();
2090 break;
2091 case DW_OP_GNU_regval_type:
2093 unsigned r = val1->v.val_unsigned;
2094 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2095 gcc_assert (o);
2096 if (for_eh_or_skip >= 0)
2098 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2099 gcc_assert (size_of_uleb128 (r)
2100 == size_of_uleb128 (val1->v.val_unsigned));
2102 dw2_asm_output_data_uleb128 (r, NULL);
2103 dw2_asm_output_data_uleb128 (o, NULL);
2105 break;
2106 case DW_OP_GNU_deref_type:
2108 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2109 gcc_assert (o);
2110 dw2_asm_output_data (1, val1->v.val_int, NULL);
2111 dw2_asm_output_data_uleb128 (o, NULL);
2113 break;
2114 case DW_OP_GNU_convert:
2115 case DW_OP_GNU_reinterpret:
2116 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2117 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2118 else
2120 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2121 gcc_assert (o);
2122 dw2_asm_output_data_uleb128 (o, NULL);
2124 break;
2126 case DW_OP_GNU_parameter_ref:
2128 unsigned long o;
2129 gcc_assert (val1->val_class == dw_val_class_die_ref);
2130 o = get_ref_die_offset (val1->v.val_die_ref.die);
2131 dw2_asm_output_data (4, o, NULL);
2133 break;
2135 default:
2136 /* Other codes have no operands. */
2137 break;
2141 /* Output a sequence of location operations.
2142 The for_eh_or_skip parameter controls whether register numbers are
2143 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2144 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2145 info). This should be suppressed for the cases that have not been converted
2146 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2148 void
2149 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2151 for (; loc != NULL; loc = loc->dw_loc_next)
2153 enum dwarf_location_atom opc = loc->dw_loc_opc;
2154 /* Output the opcode. */
2155 if (for_eh_or_skip >= 0
2156 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2158 unsigned r = (opc - DW_OP_breg0);
2159 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2160 gcc_assert (r <= 31);
2161 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2163 else if (for_eh_or_skip >= 0
2164 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2166 unsigned r = (opc - DW_OP_reg0);
2167 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2168 gcc_assert (r <= 31);
2169 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2172 dw2_asm_output_data (1, opc,
2173 "%s", dwarf_stack_op_name (opc));
2175 /* Output the operand(s) (if any). */
2176 output_loc_operands (loc, for_eh_or_skip);
2180 /* Output location description stack opcode's operands (if any).
2181 The output is single bytes on a line, suitable for .cfi_escape. */
2183 static void
2184 output_loc_operands_raw (dw_loc_descr_ref loc)
2186 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2187 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2189 switch (loc->dw_loc_opc)
2191 case DW_OP_addr:
2192 case DW_OP_GNU_addr_index:
2193 case DW_OP_GNU_const_index:
2194 case DW_OP_implicit_value:
2195 /* We cannot output addresses in .cfi_escape, only bytes. */
2196 gcc_unreachable ();
2198 case DW_OP_const1u:
2199 case DW_OP_const1s:
2200 case DW_OP_pick:
2201 case DW_OP_deref_size:
2202 case DW_OP_xderef_size:
2203 fputc (',', asm_out_file);
2204 dw2_asm_output_data_raw (1, val1->v.val_int);
2205 break;
2207 case DW_OP_const2u:
2208 case DW_OP_const2s:
2209 fputc (',', asm_out_file);
2210 dw2_asm_output_data_raw (2, val1->v.val_int);
2211 break;
2213 case DW_OP_const4u:
2214 case DW_OP_const4s:
2215 fputc (',', asm_out_file);
2216 dw2_asm_output_data_raw (4, val1->v.val_int);
2217 break;
2219 case DW_OP_const8u:
2220 case DW_OP_const8s:
2221 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2222 fputc (',', asm_out_file);
2223 dw2_asm_output_data_raw (8, val1->v.val_int);
2224 break;
2226 case DW_OP_skip:
2227 case DW_OP_bra:
2229 int offset;
2231 gcc_assert (val1->val_class == dw_val_class_loc);
2232 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2234 fputc (',', asm_out_file);
2235 dw2_asm_output_data_raw (2, offset);
2237 break;
2239 case DW_OP_regx:
2241 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2242 gcc_assert (size_of_uleb128 (r)
2243 == size_of_uleb128 (val1->v.val_unsigned));
2244 fputc (',', asm_out_file);
2245 dw2_asm_output_data_uleb128_raw (r);
2247 break;
2249 case DW_OP_constu:
2250 case DW_OP_plus_uconst:
2251 case DW_OP_piece:
2252 fputc (',', asm_out_file);
2253 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2254 break;
2256 case DW_OP_bit_piece:
2257 fputc (',', asm_out_file);
2258 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2259 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2260 break;
2262 case DW_OP_consts:
2263 case DW_OP_breg0:
2264 case DW_OP_breg1:
2265 case DW_OP_breg2:
2266 case DW_OP_breg3:
2267 case DW_OP_breg4:
2268 case DW_OP_breg5:
2269 case DW_OP_breg6:
2270 case DW_OP_breg7:
2271 case DW_OP_breg8:
2272 case DW_OP_breg9:
2273 case DW_OP_breg10:
2274 case DW_OP_breg11:
2275 case DW_OP_breg12:
2276 case DW_OP_breg13:
2277 case DW_OP_breg14:
2278 case DW_OP_breg15:
2279 case DW_OP_breg16:
2280 case DW_OP_breg17:
2281 case DW_OP_breg18:
2282 case DW_OP_breg19:
2283 case DW_OP_breg20:
2284 case DW_OP_breg21:
2285 case DW_OP_breg22:
2286 case DW_OP_breg23:
2287 case DW_OP_breg24:
2288 case DW_OP_breg25:
2289 case DW_OP_breg26:
2290 case DW_OP_breg27:
2291 case DW_OP_breg28:
2292 case DW_OP_breg29:
2293 case DW_OP_breg30:
2294 case DW_OP_breg31:
2295 case DW_OP_fbreg:
2296 fputc (',', asm_out_file);
2297 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2298 break;
2300 case DW_OP_bregx:
2302 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2303 gcc_assert (size_of_uleb128 (r)
2304 == size_of_uleb128 (val1->v.val_unsigned));
2305 fputc (',', asm_out_file);
2306 dw2_asm_output_data_uleb128_raw (r);
2307 fputc (',', asm_out_file);
2308 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2310 break;
2312 case DW_OP_GNU_implicit_pointer:
2313 case DW_OP_GNU_entry_value:
2314 case DW_OP_GNU_const_type:
2315 case DW_OP_GNU_regval_type:
2316 case DW_OP_GNU_deref_type:
2317 case DW_OP_GNU_convert:
2318 case DW_OP_GNU_reinterpret:
2319 case DW_OP_GNU_parameter_ref:
2320 gcc_unreachable ();
2321 break;
2323 default:
2324 /* Other codes have no operands. */
2325 break;
2329 void
2330 output_loc_sequence_raw (dw_loc_descr_ref loc)
2332 while (1)
2334 enum dwarf_location_atom opc = loc->dw_loc_opc;
2335 /* Output the opcode. */
2336 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2338 unsigned r = (opc - DW_OP_breg0);
2339 r = DWARF2_FRAME_REG_OUT (r, 1);
2340 gcc_assert (r <= 31);
2341 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2343 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2345 unsigned r = (opc - DW_OP_reg0);
2346 r = DWARF2_FRAME_REG_OUT (r, 1);
2347 gcc_assert (r <= 31);
2348 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2350 /* Output the opcode. */
2351 fprintf (asm_out_file, "%#x", opc);
2352 output_loc_operands_raw (loc);
2354 if (!loc->dw_loc_next)
2355 break;
2356 loc = loc->dw_loc_next;
2358 fputc (',', asm_out_file);
2362 /* This function builds a dwarf location descriptor sequence from a
2363 dw_cfa_location, adding the given OFFSET to the result of the
2364 expression. */
2366 struct dw_loc_descr_node *
2367 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2369 struct dw_loc_descr_node *head, *tmp;
2371 offset += cfa->offset;
2373 if (cfa->indirect)
2375 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2376 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2377 head->dw_loc_oprnd1.val_entry = NULL;
2378 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2379 add_loc_descr (&head, tmp);
2380 if (offset != 0)
2382 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2383 add_loc_descr (&head, tmp);
2386 else
2387 head = new_reg_loc_descr (cfa->reg, offset);
2389 return head;
2392 /* This function builds a dwarf location descriptor sequence for
2393 the address at OFFSET from the CFA when stack is aligned to
2394 ALIGNMENT byte. */
2396 struct dw_loc_descr_node *
2397 build_cfa_aligned_loc (dw_cfa_location *cfa,
2398 HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2400 struct dw_loc_descr_node *head;
2401 unsigned int dwarf_fp
2402 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2404 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2405 if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2407 head = new_reg_loc_descr (dwarf_fp, 0);
2408 add_loc_descr (&head, int_loc_descriptor (alignment));
2409 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2410 loc_descr_plus_const (&head, offset);
2412 else
2413 head = new_reg_loc_descr (dwarf_fp, offset);
2414 return head;
2417 /* And now, the support for symbolic debugging information. */
2419 /* .debug_str support. */
2420 static int output_indirect_string (void **, void *);
2422 static void dwarf2out_init (const char *);
2423 static void dwarf2out_finish (const char *);
2424 static void dwarf2out_assembly_start (void);
2425 static void dwarf2out_define (unsigned int, const char *);
2426 static void dwarf2out_undef (unsigned int, const char *);
2427 static void dwarf2out_start_source_file (unsigned, const char *);
2428 static void dwarf2out_end_source_file (unsigned);
2429 static void dwarf2out_function_decl (tree);
2430 static void dwarf2out_begin_block (unsigned, unsigned);
2431 static void dwarf2out_end_block (unsigned, unsigned);
2432 static bool dwarf2out_ignore_block (const_tree);
2433 static void dwarf2out_global_decl (tree);
2434 static void dwarf2out_type_decl (tree, int);
2435 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
2436 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2437 dw_die_ref);
2438 static void dwarf2out_abstract_function (tree);
2439 static void dwarf2out_var_location (rtx_insn *);
2440 static void dwarf2out_begin_function (tree);
2441 static void dwarf2out_end_function (unsigned int);
2442 static void dwarf2out_set_name (tree, tree);
2444 /* The debug hooks structure. */
2446 const struct gcc_debug_hooks dwarf2_debug_hooks =
2448 dwarf2out_init,
2449 dwarf2out_finish,
2450 dwarf2out_assembly_start,
2451 dwarf2out_define,
2452 dwarf2out_undef,
2453 dwarf2out_start_source_file,
2454 dwarf2out_end_source_file,
2455 dwarf2out_begin_block,
2456 dwarf2out_end_block,
2457 dwarf2out_ignore_block,
2458 dwarf2out_source_line,
2459 dwarf2out_begin_prologue,
2460 #if VMS_DEBUGGING_INFO
2461 dwarf2out_vms_end_prologue,
2462 dwarf2out_vms_begin_epilogue,
2463 #else
2464 debug_nothing_int_charstar,
2465 debug_nothing_int_charstar,
2466 #endif
2467 dwarf2out_end_epilogue,
2468 dwarf2out_begin_function,
2469 dwarf2out_end_function, /* end_function */
2470 dwarf2out_function_decl, /* function_decl */
2471 dwarf2out_global_decl,
2472 dwarf2out_type_decl, /* type_decl */
2473 dwarf2out_imported_module_or_decl,
2474 debug_nothing_tree, /* deferred_inline_function */
2475 /* The DWARF 2 backend tries to reduce debugging bloat by not
2476 emitting the abstract description of inline functions until
2477 something tries to reference them. */
2478 dwarf2out_abstract_function, /* outlining_inline_function */
2479 debug_nothing_rtx_code_label, /* label */
2480 debug_nothing_int, /* handle_pch */
2481 dwarf2out_var_location,
2482 dwarf2out_switch_text_section,
2483 dwarf2out_set_name,
2484 1, /* start_end_main_source_file */
2485 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2488 /* NOTE: In the comments in this file, many references are made to
2489 "Debugging Information Entries". This term is abbreviated as `DIE'
2490 throughout the remainder of this file. */
2492 /* An internal representation of the DWARF output is built, and then
2493 walked to generate the DWARF debugging info. The walk of the internal
2494 representation is done after the entire program has been compiled.
2495 The types below are used to describe the internal representation. */
2497 /* Whether to put type DIEs into their own section .debug_types instead
2498 of making them part of the .debug_info section. Only supported for
2499 Dwarf V4 or higher and the user didn't disable them through
2500 -fno-debug-types-section. It is more efficient to put them in a
2501 separate comdat sections since the linker will then be able to
2502 remove duplicates. But not all tools support .debug_types sections
2503 yet. */
2505 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2507 /* Various DIE's use offsets relative to the beginning of the
2508 .debug_info section to refer to each other. */
2510 typedef long int dw_offset;
2512 /* Define typedefs here to avoid circular dependencies. */
2514 typedef struct dw_attr_struct *dw_attr_ref;
2515 typedef struct dw_line_info_struct *dw_line_info_ref;
2516 typedef struct pubname_struct *pubname_ref;
2517 typedef struct dw_ranges_struct *dw_ranges_ref;
2518 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
2519 typedef struct comdat_type_struct *comdat_type_node_ref;
2521 /* The entries in the line_info table more-or-less mirror the opcodes
2522 that are used in the real dwarf line table. Arrays of these entries
2523 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2524 supported. */
2526 enum dw_line_info_opcode {
2527 /* Emit DW_LNE_set_address; the operand is the label index. */
2528 LI_set_address,
2530 /* Emit a row to the matrix with the given line. This may be done
2531 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2532 special opcodes. */
2533 LI_set_line,
2535 /* Emit a DW_LNS_set_file. */
2536 LI_set_file,
2538 /* Emit a DW_LNS_set_column. */
2539 LI_set_column,
2541 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
2542 LI_negate_stmt,
2544 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
2545 LI_set_prologue_end,
2546 LI_set_epilogue_begin,
2548 /* Emit a DW_LNE_set_discriminator. */
2549 LI_set_discriminator
2552 typedef struct GTY(()) dw_line_info_struct {
2553 enum dw_line_info_opcode opcode;
2554 unsigned int val;
2555 } dw_line_info_entry;
2558 typedef struct GTY(()) dw_line_info_table_struct {
2559 /* The label that marks the end of this section. */
2560 const char *end_label;
2562 /* The values for the last row of the matrix, as collected in the table.
2563 These are used to minimize the changes to the next row. */
2564 unsigned int file_num;
2565 unsigned int line_num;
2566 unsigned int column_num;
2567 int discrim_num;
2568 bool is_stmt;
2569 bool in_use;
2571 vec<dw_line_info_entry, va_gc> *entries;
2572 } dw_line_info_table;
2574 typedef dw_line_info_table *dw_line_info_table_p;
2577 /* Each DIE attribute has a field specifying the attribute kind,
2578 a link to the next attribute in the chain, and an attribute value.
2579 Attributes are typically linked below the DIE they modify. */
2581 typedef struct GTY(()) dw_attr_struct {
2582 enum dwarf_attribute dw_attr;
2583 dw_val_node dw_attr_val;
2585 dw_attr_node;
2588 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
2589 The children of each node form a circular list linked by
2590 die_sib. die_child points to the node *before* the "first" child node. */
2592 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
2593 union die_symbol_or_type_node
2595 const char * GTY ((tag ("0"))) die_symbol;
2596 comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
2598 GTY ((desc ("%0.comdat_type_p"))) die_id;
2599 vec<dw_attr_node, va_gc> *die_attr;
2600 dw_die_ref die_parent;
2601 dw_die_ref die_child;
2602 dw_die_ref die_sib;
2603 dw_die_ref die_definition; /* ref from a specification to its definition */
2604 dw_offset die_offset;
2605 unsigned long die_abbrev;
2606 int die_mark;
2607 unsigned int decl_id;
2608 enum dwarf_tag die_tag;
2609 /* Die is used and must not be pruned as unused. */
2610 BOOL_BITFIELD die_perennial_p : 1;
2611 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2612 /* Lots of spare bits. */
2614 die_node;
2616 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
2617 #define FOR_EACH_CHILD(die, c, expr) do { \
2618 c = die->die_child; \
2619 if (c) do { \
2620 c = c->die_sib; \
2621 expr; \
2622 } while (c != die->die_child); \
2623 } while (0)
2625 /* The pubname structure */
2627 typedef struct GTY(()) pubname_struct {
2628 dw_die_ref die;
2629 const char *name;
2631 pubname_entry;
2634 struct GTY(()) dw_ranges_struct {
2635 /* If this is positive, it's a block number, otherwise it's a
2636 bitwise-negated index into dw_ranges_by_label. */
2637 int num;
2640 /* A structure to hold a macinfo entry. */
2642 typedef struct GTY(()) macinfo_struct {
2643 unsigned char code;
2644 unsigned HOST_WIDE_INT lineno;
2645 const char *info;
2647 macinfo_entry;
2650 struct GTY(()) dw_ranges_by_label_struct {
2651 const char *begin;
2652 const char *end;
2655 /* The comdat type node structure. */
2656 typedef struct GTY(()) comdat_type_struct
2658 dw_die_ref root_die;
2659 dw_die_ref type_die;
2660 dw_die_ref skeleton_die;
2661 char signature[DWARF_TYPE_SIGNATURE_SIZE];
2662 struct comdat_type_struct *next;
2664 comdat_type_node;
2666 /* The limbo die list structure. */
2667 typedef struct GTY(()) limbo_die_struct {
2668 dw_die_ref die;
2669 tree created_for;
2670 struct limbo_die_struct *next;
2672 limbo_die_node;
2674 typedef struct skeleton_chain_struct
2676 dw_die_ref old_die;
2677 dw_die_ref new_die;
2678 struct skeleton_chain_struct *parent;
2680 skeleton_chain_node;
2682 /* Define a macro which returns nonzero for a TYPE_DECL which was
2683 implicitly generated for a type.
2685 Note that, unlike the C front-end (which generates a NULL named
2686 TYPE_DECL node for each complete tagged type, each array type,
2687 and each function type node created) the C++ front-end generates
2688 a _named_ TYPE_DECL node for each tagged type node created.
2689 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
2690 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
2691 front-end, but for each type, tagged or not. */
2693 #define TYPE_DECL_IS_STUB(decl) \
2694 (DECL_NAME (decl) == NULL_TREE \
2695 || (DECL_ARTIFICIAL (decl) \
2696 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
2697 /* This is necessary for stub decls that \
2698 appear in nested inline functions. */ \
2699 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
2700 && (decl_ultimate_origin (decl) \
2701 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
2703 /* Information concerning the compilation unit's programming
2704 language, and compiler version. */
2706 /* Fixed size portion of the DWARF compilation unit header. */
2707 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
2708 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
2710 /* Fixed size portion of the DWARF comdat type unit header. */
2711 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
2712 (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
2713 + DWARF_OFFSET_SIZE)
2715 /* Fixed size portion of public names info. */
2716 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
2718 /* Fixed size portion of the address range info. */
2719 #define DWARF_ARANGES_HEADER_SIZE \
2720 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2721 DWARF2_ADDR_SIZE * 2) \
2722 - DWARF_INITIAL_LENGTH_SIZE)
2724 /* Size of padding portion in the address range info. It must be
2725 aligned to twice the pointer size. */
2726 #define DWARF_ARANGES_PAD_SIZE \
2727 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2728 DWARF2_ADDR_SIZE * 2) \
2729 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
2731 /* Use assembler line directives if available. */
2732 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
2733 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
2734 #define DWARF2_ASM_LINE_DEBUG_INFO 1
2735 #else
2736 #define DWARF2_ASM_LINE_DEBUG_INFO 0
2737 #endif
2738 #endif
2740 /* Minimum line offset in a special line info. opcode.
2741 This value was chosen to give a reasonable range of values. */
2742 #define DWARF_LINE_BASE -10
2744 /* First special line opcode - leave room for the standard opcodes. */
2745 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
2747 /* Range of line offsets in a special line info. opcode. */
2748 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
2750 /* Flag that indicates the initial value of the is_stmt_start flag.
2751 In the present implementation, we do not mark any lines as
2752 the beginning of a source statement, because that information
2753 is not made available by the GCC front-end. */
2754 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
2756 /* Maximum number of operations per instruction bundle. */
2757 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
2758 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
2759 #endif
2761 /* This location is used by calc_die_sizes() to keep track
2762 the offset of each DIE within the .debug_info section. */
2763 static unsigned long next_die_offset;
2765 /* Record the root of the DIE's built for the current compilation unit. */
2766 static GTY(()) dw_die_ref single_comp_unit_die;
2768 /* A list of type DIEs that have been separated into comdat sections. */
2769 static GTY(()) comdat_type_node *comdat_type_list;
2771 /* A list of DIEs with a NULL parent waiting to be relocated. */
2772 static GTY(()) limbo_die_node *limbo_die_list;
2774 /* A list of DIEs for which we may have to generate
2775 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
2776 static GTY(()) limbo_die_node *deferred_asm_name;
2778 /* Filenames referenced by this compilation unit. */
2779 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
2781 /* A hash table of references to DIE's that describe declarations.
2782 The key is a DECL_UID() which is a unique number identifying each decl. */
2783 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
2785 /* A hash table of references to DIE's that describe COMMON blocks.
2786 The key is DECL_UID() ^ die_parent. */
2787 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
2789 typedef struct GTY(()) die_arg_entry_struct {
2790 dw_die_ref die;
2791 tree arg;
2792 } die_arg_entry;
2795 /* Node of the variable location list. */
2796 struct GTY ((chain_next ("%h.next"))) var_loc_node {
2797 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
2798 EXPR_LIST chain. For small bitsizes, bitsize is encoded
2799 in mode of the EXPR_LIST node and first EXPR_LIST operand
2800 is either NOTE_INSN_VAR_LOCATION for a piece with a known
2801 location or NULL for padding. For larger bitsizes,
2802 mode is 0 and first operand is a CONCAT with bitsize
2803 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
2804 NULL as second operand. */
2805 rtx GTY (()) loc;
2806 const char * GTY (()) label;
2807 struct var_loc_node * GTY (()) next;
2810 /* Variable location list. */
2811 struct GTY (()) var_loc_list_def {
2812 struct var_loc_node * GTY (()) first;
2814 /* Pointer to the last but one or last element of the
2815 chained list. If the list is empty, both first and
2816 last are NULL, if the list contains just one node
2817 or the last node certainly is not redundant, it points
2818 to the last node, otherwise points to the last but one.
2819 Do not mark it for GC because it is marked through the chain. */
2820 struct var_loc_node * GTY ((skip ("%h"))) last;
2822 /* Pointer to the last element before section switch,
2823 if NULL, either sections weren't switched or first
2824 is after section switch. */
2825 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
2827 /* DECL_UID of the variable decl. */
2828 unsigned int decl_id;
2830 typedef struct var_loc_list_def var_loc_list;
2832 /* Call argument location list. */
2833 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
2834 rtx GTY (()) call_arg_loc_note;
2835 const char * GTY (()) label;
2836 tree GTY (()) block;
2837 bool tail_call_p;
2838 rtx GTY (()) symbol_ref;
2839 struct call_arg_loc_node * GTY (()) next;
2843 /* Table of decl location linked lists. */
2844 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
2846 /* Head and tail of call_arg_loc chain. */
2847 static GTY (()) struct call_arg_loc_node *call_arg_locations;
2848 static struct call_arg_loc_node *call_arg_loc_last;
2850 /* Number of call sites in the current function. */
2851 static int call_site_count = -1;
2852 /* Number of tail call sites in the current function. */
2853 static int tail_call_site_count = -1;
2855 /* Vector mapping block numbers to DW_TAG_{lexical_block,inlined_subroutine}
2856 DIEs. */
2857 static vec<dw_die_ref> block_map;
2859 /* A cached location list. */
2860 struct GTY (()) cached_dw_loc_list_def {
2861 /* The DECL_UID of the decl that this entry describes. */
2862 unsigned int decl_id;
2864 /* The cached location list. */
2865 dw_loc_list_ref loc_list;
2867 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
2869 /* Table of cached location lists. */
2870 static GTY ((param_is (cached_dw_loc_list))) htab_t cached_dw_loc_list_table;
2872 /* A pointer to the base of a list of references to DIE's that
2873 are uniquely identified by their tag, presence/absence of
2874 children DIE's, and list of attribute/value pairs. */
2875 static GTY((length ("abbrev_die_table_allocated")))
2876 dw_die_ref *abbrev_die_table;
2878 /* Number of elements currently allocated for abbrev_die_table. */
2879 static GTY(()) unsigned abbrev_die_table_allocated;
2881 /* Number of elements in type_die_table currently in use. */
2882 static GTY(()) unsigned abbrev_die_table_in_use;
2884 /* Size (in elements) of increments by which we may expand the
2885 abbrev_die_table. */
2886 #define ABBREV_DIE_TABLE_INCREMENT 256
2888 /* A global counter for generating labels for line number data. */
2889 static unsigned int line_info_label_num;
2891 /* The current table to which we should emit line number information
2892 for the current function. This will be set up at the beginning of
2893 assembly for the function. */
2894 static dw_line_info_table *cur_line_info_table;
2896 /* The two default tables of line number info. */
2897 static GTY(()) dw_line_info_table *text_section_line_info;
2898 static GTY(()) dw_line_info_table *cold_text_section_line_info;
2900 /* The set of all non-default tables of line number info. */
2901 static GTY(()) vec<dw_line_info_table_p, va_gc> *separate_line_info;
2903 /* A flag to tell pubnames/types export if there is an info section to
2904 refer to. */
2905 static bool info_section_emitted;
2907 /* A pointer to the base of a table that contains a list of publicly
2908 accessible names. */
2909 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
2911 /* A pointer to the base of a table that contains a list of publicly
2912 accessible types. */
2913 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
2915 /* A pointer to the base of a table that contains a list of macro
2916 defines/undefines (and file start/end markers). */
2917 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
2919 /* True if .debug_macinfo or .debug_macros section is going to be
2920 emitted. */
2921 #define have_macinfo \
2922 (debug_info_level >= DINFO_LEVEL_VERBOSE \
2923 && !macinfo_table->is_empty ())
2925 /* Array of dies for which we should generate .debug_ranges info. */
2926 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
2928 /* Number of elements currently allocated for ranges_table. */
2929 static GTY(()) unsigned ranges_table_allocated;
2931 /* Number of elements in ranges_table currently in use. */
2932 static GTY(()) unsigned ranges_table_in_use;
2934 /* Array of pairs of labels referenced in ranges_table. */
2935 static GTY ((length ("ranges_by_label_allocated")))
2936 dw_ranges_by_label_ref ranges_by_label;
2938 /* Number of elements currently allocated for ranges_by_label. */
2939 static GTY(()) unsigned ranges_by_label_allocated;
2941 /* Number of elements in ranges_by_label currently in use. */
2942 static GTY(()) unsigned ranges_by_label_in_use;
2944 /* Size (in elements) of increments by which we may expand the
2945 ranges_table. */
2946 #define RANGES_TABLE_INCREMENT 64
2948 /* Whether we have location lists that need outputting */
2949 static GTY(()) bool have_location_lists;
2951 /* Unique label counter. */
2952 static GTY(()) unsigned int loclabel_num;
2954 /* Unique label counter for point-of-call tables. */
2955 static GTY(()) unsigned int poc_label_num;
2957 /* The last file entry emitted by maybe_emit_file(). */
2958 static GTY(()) struct dwarf_file_data * last_emitted_file;
2960 /* Number of internal labels generated by gen_internal_sym(). */
2961 static GTY(()) int label_num;
2963 /* Cached result of previous call to lookup_filename. */
2964 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
2966 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
2968 /* Instances of generic types for which we need to generate debug
2969 info that describe their generic parameters and arguments. That
2970 generation needs to happen once all types are properly laid out so
2971 we do it at the end of compilation. */
2972 static GTY(()) vec<tree, va_gc> *generic_type_instances;
2974 /* Offset from the "steady-state frame pointer" to the frame base,
2975 within the current function. */
2976 static HOST_WIDE_INT frame_pointer_fb_offset;
2977 static bool frame_pointer_fb_offset_valid;
2979 static vec<dw_die_ref> base_types;
2981 /* Forward declarations for functions defined in this file. */
2983 static int is_pseudo_reg (const_rtx);
2984 static tree type_main_variant (tree);
2985 static int is_tagged_type (const_tree);
2986 static const char *dwarf_tag_name (unsigned);
2987 static const char *dwarf_attr_name (unsigned);
2988 static const char *dwarf_form_name (unsigned);
2989 static tree decl_ultimate_origin (const_tree);
2990 static tree decl_class_context (tree);
2991 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
2992 static inline enum dw_val_class AT_class (dw_attr_ref);
2993 static inline unsigned int AT_index (dw_attr_ref);
2994 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
2995 static inline unsigned AT_flag (dw_attr_ref);
2996 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
2997 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
2998 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
2999 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
3000 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3001 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3002 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3003 unsigned int, unsigned char *);
3004 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3005 static hashval_t debug_str_do_hash (const void *);
3006 static int debug_str_eq (const void *, const void *);
3007 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3008 static inline const char *AT_string (dw_attr_ref);
3009 static enum dwarf_form AT_string_form (dw_attr_ref);
3010 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3011 static void add_AT_specification (dw_die_ref, dw_die_ref);
3012 static inline dw_die_ref AT_ref (dw_attr_ref);
3013 static inline int AT_ref_external (dw_attr_ref);
3014 static inline void set_AT_ref_external (dw_attr_ref, int);
3015 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3016 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3017 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
3018 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3019 dw_loc_list_ref);
3020 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
3021 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3022 static void remove_addr_table_entry (addr_table_entry *);
3023 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3024 static inline rtx AT_addr (dw_attr_ref);
3025 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3026 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3027 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3028 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3029 unsigned HOST_WIDE_INT);
3030 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3031 unsigned long, bool);
3032 static inline const char *AT_lbl (dw_attr_ref);
3033 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
3034 static const char *get_AT_low_pc (dw_die_ref);
3035 static const char *get_AT_hi_pc (dw_die_ref);
3036 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3037 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3038 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3039 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3040 static bool is_cxx (void);
3041 static bool is_fortran (void);
3042 static bool is_ada (void);
3043 static void remove_AT (dw_die_ref, enum dwarf_attribute);
3044 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3045 static void add_child_die (dw_die_ref, dw_die_ref);
3046 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3047 static dw_die_ref lookup_type_die (tree);
3048 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3049 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3050 static void equate_type_number_to_die (tree, dw_die_ref);
3051 static hashval_t decl_die_table_hash (const void *);
3052 static int decl_die_table_eq (const void *, const void *);
3053 static dw_die_ref lookup_decl_die (tree);
3054 static hashval_t common_block_die_table_hash (const void *);
3055 static int common_block_die_table_eq (const void *, const void *);
3056 static hashval_t decl_loc_table_hash (const void *);
3057 static int decl_loc_table_eq (const void *, const void *);
3058 static var_loc_list *lookup_decl_loc (const_tree);
3059 static void equate_decl_number_to_die (tree, dw_die_ref);
3060 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3061 static void print_spaces (FILE *);
3062 static void print_die (dw_die_ref, FILE *);
3063 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3064 static dw_die_ref pop_compile_unit (dw_die_ref);
3065 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3066 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
3067 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3068 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3069 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3070 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3071 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
3072 struct md5_ctx *, int *);
3073 struct checksum_attributes;
3074 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3075 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3076 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3077 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3078 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3079 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3080 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
3081 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3082 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3083 static void compute_section_prefix (dw_die_ref);
3084 static int is_type_die (dw_die_ref);
3085 static int is_comdat_die (dw_die_ref);
3086 static int is_symbol_die (dw_die_ref);
3087 static inline bool is_template_instantiation (dw_die_ref);
3088 static void assign_symbol_names (dw_die_ref);
3089 static void break_out_includes (dw_die_ref);
3090 static int is_declaration_die (dw_die_ref);
3091 static int should_move_die_to_comdat (dw_die_ref);
3092 static dw_die_ref clone_as_declaration (dw_die_ref);
3093 static dw_die_ref clone_die (dw_die_ref);
3094 static dw_die_ref clone_tree (dw_die_ref);
3095 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3096 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3097 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3098 static dw_die_ref generate_skeleton (dw_die_ref);
3099 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3100 dw_die_ref,
3101 dw_die_ref);
3102 static void break_out_comdat_types (dw_die_ref);
3103 static void copy_decls_for_unworthy_types (dw_die_ref);
3105 static void add_sibling_attributes (dw_die_ref);
3106 static void output_location_lists (dw_die_ref);
3107 static int constant_size (unsigned HOST_WIDE_INT);
3108 static unsigned long size_of_die (dw_die_ref);
3109 static void calc_die_sizes (dw_die_ref);
3110 static void calc_base_type_die_sizes (void);
3111 static void mark_dies (dw_die_ref);
3112 static void unmark_dies (dw_die_ref);
3113 static void unmark_all_dies (dw_die_ref);
3114 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3115 static unsigned long size_of_aranges (void);
3116 static enum dwarf_form value_format (dw_attr_ref);
3117 static void output_value_format (dw_attr_ref);
3118 static void output_abbrev_section (void);
3119 static void output_die_abbrevs (unsigned long, dw_die_ref);
3120 static void output_die_symbol (dw_die_ref);
3121 static void output_die (dw_die_ref);
3122 static void output_compilation_unit_header (void);
3123 static void output_comp_unit (dw_die_ref, int);
3124 static void output_comdat_type_unit (comdat_type_node *);
3125 static const char *dwarf2_name (tree, int);
3126 static void add_pubname (tree, dw_die_ref);
3127 static void add_enumerator_pubname (const char *, dw_die_ref);
3128 static void add_pubname_string (const char *, dw_die_ref);
3129 static void add_pubtype (tree, dw_die_ref);
3130 static void output_pubnames (vec<pubname_entry, va_gc> *);
3131 static void output_aranges (unsigned long);
3132 static unsigned int add_ranges_num (int);
3133 static unsigned int add_ranges (const_tree);
3134 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3135 bool *, bool);
3136 static void output_ranges (void);
3137 static dw_line_info_table *new_line_info_table (void);
3138 static void output_line_info (bool);
3139 static void output_file_names (void);
3140 static dw_die_ref base_type_die (tree);
3141 static int is_base_type (tree);
3142 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
3143 static int decl_quals (const_tree);
3144 static dw_die_ref modified_type_die (tree, int, dw_die_ref);
3145 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3146 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3147 static int type_is_enum (const_tree);
3148 static unsigned int dbx_reg_number (const_rtx);
3149 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3150 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3151 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3152 enum var_init_status);
3153 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3154 enum var_init_status);
3155 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3156 enum var_init_status);
3157 static int is_based_loc (const_rtx);
3158 static bool resolve_one_addr (rtx *);
3159 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3160 enum var_init_status);
3161 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
3162 enum var_init_status);
3163 static dw_loc_list_ref loc_list_from_tree (tree, int);
3164 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
3165 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3166 static tree field_type (const_tree);
3167 static unsigned int simple_type_align_in_bits (const_tree);
3168 static unsigned int simple_decl_align_in_bits (const_tree);
3169 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3170 static HOST_WIDE_INT field_byte_offset (const_tree);
3171 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3172 dw_loc_list_ref);
3173 static void add_data_member_location_attribute (dw_die_ref, tree);
3174 static bool add_const_value_attribute (dw_die_ref, rtx);
3175 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3176 static void insert_wide_int (const wide_int &, unsigned char *, int);
3177 static void insert_float (const_rtx, unsigned char *);
3178 static rtx rtl_for_decl_location (tree);
3179 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool,
3180 enum dwarf_attribute);
3181 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3182 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3183 static void add_name_attribute (dw_die_ref, const char *);
3184 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3185 static void add_comp_dir_attribute (dw_die_ref);
3186 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
3187 static void add_subscript_info (dw_die_ref, tree, bool);
3188 static void add_byte_size_attribute (dw_die_ref, tree);
3189 static void add_bit_offset_attribute (dw_die_ref, tree);
3190 static void add_bit_size_attribute (dw_die_ref, tree);
3191 static void add_prototyped_attribute (dw_die_ref, tree);
3192 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3193 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3194 static void add_src_coords_attributes (dw_die_ref, tree);
3195 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3196 static void push_decl_scope (tree);
3197 static void pop_decl_scope (void);
3198 static dw_die_ref scope_die_for (tree, dw_die_ref);
3199 static inline int local_scope_p (dw_die_ref);
3200 static inline int class_scope_p (dw_die_ref);
3201 static inline int class_or_namespace_scope_p (dw_die_ref);
3202 static void add_type_attribute (dw_die_ref, tree, int, dw_die_ref);
3203 static void add_calling_convention_attribute (dw_die_ref, tree);
3204 static const char *type_tag (const_tree);
3205 static tree member_declared_type (const_tree);
3206 #if 0
3207 static const char *decl_start_label (tree);
3208 #endif
3209 static void gen_array_type_die (tree, dw_die_ref);
3210 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3211 #if 0
3212 static void gen_entry_point_die (tree, dw_die_ref);
3213 #endif
3214 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3215 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3216 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3217 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3218 static void gen_formal_types_die (tree, dw_die_ref);
3219 static void gen_subprogram_die (tree, dw_die_ref);
3220 static void gen_variable_die (tree, tree, dw_die_ref);
3221 static void gen_const_die (tree, dw_die_ref);
3222 static void gen_label_die (tree, dw_die_ref);
3223 static void gen_lexical_block_die (tree, dw_die_ref, int);
3224 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
3225 static void gen_field_die (tree, dw_die_ref);
3226 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3227 static dw_die_ref gen_compile_unit_die (const char *);
3228 static void gen_inheritance_die (tree, tree, dw_die_ref);
3229 static void gen_member_die (tree, dw_die_ref);
3230 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3231 enum debug_info_usage);
3232 static void gen_subroutine_type_die (tree, dw_die_ref);
3233 static void gen_typedef_die (tree, dw_die_ref);
3234 static void gen_type_die (tree, dw_die_ref);
3235 static void gen_block_die (tree, dw_die_ref, int);
3236 static void decls_for_scope (tree, dw_die_ref, int);
3237 static inline int is_redundant_typedef (const_tree);
3238 static bool is_naming_typedef_decl (const_tree);
3239 static inline dw_die_ref get_context_die (tree);
3240 static void gen_namespace_die (tree, dw_die_ref);
3241 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3242 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
3243 static dw_die_ref force_decl_die (tree);
3244 static dw_die_ref force_type_die (tree);
3245 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3246 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3247 static struct dwarf_file_data * lookup_filename (const char *);
3248 static void retry_incomplete_types (void);
3249 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3250 static void gen_generic_params_dies (tree);
3251 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3252 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3253 static void splice_child_die (dw_die_ref, dw_die_ref);
3254 static int file_info_cmp (const void *, const void *);
3255 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3256 const char *, const char *);
3257 static void output_loc_list (dw_loc_list_ref);
3258 static char *gen_internal_sym (const char *);
3259 static bool want_pubnames (void);
3261 static void prune_unmark_dies (dw_die_ref);
3262 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3263 static void prune_unused_types_mark (dw_die_ref, int);
3264 static void prune_unused_types_walk (dw_die_ref);
3265 static void prune_unused_types_walk_attribs (dw_die_ref);
3266 static void prune_unused_types_prune (dw_die_ref);
3267 static void prune_unused_types (void);
3268 static int maybe_emit_file (struct dwarf_file_data *fd);
3269 static inline const char *AT_vms_delta1 (dw_attr_ref);
3270 static inline const char *AT_vms_delta2 (dw_attr_ref);
3271 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3272 const char *, const char *);
3273 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3274 static void gen_remaining_tmpl_value_param_die_attribute (void);
3275 static bool generic_type_p (tree);
3276 static void schedule_generic_params_dies_gen (tree t);
3277 static void gen_scheduled_generic_parms_dies (void);
3279 static const char *comp_dir_string (void);
3281 static void hash_loc_operands (dw_loc_descr_ref, inchash::hash &);
3283 /* enum for tracking thread-local variables whose address is really an offset
3284 relative to the TLS pointer, which will need link-time relocation, but will
3285 not need relocation by the DWARF consumer. */
3287 enum dtprel_bool
3289 dtprel_false = 0,
3290 dtprel_true = 1
3293 /* Return the operator to use for an address of a variable. For dtprel_true, we
3294 use DW_OP_const*. For regular variables, which need both link-time
3295 relocation and consumer-level relocation (e.g., to account for shared objects
3296 loaded at a random address), we use DW_OP_addr*. */
3298 static inline enum dwarf_location_atom
3299 dw_addr_op (enum dtprel_bool dtprel)
3301 if (dtprel == dtprel_true)
3302 return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3303 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3304 else
3305 return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3308 /* Return a pointer to a newly allocated address location description. If
3309 dwarf_split_debug_info is true, then record the address with the appropriate
3310 relocation. */
3311 static inline dw_loc_descr_ref
3312 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3314 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3316 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3317 ref->dw_loc_oprnd1.v.val_addr = addr;
3318 ref->dtprel = dtprel;
3319 if (dwarf_split_debug_info)
3320 ref->dw_loc_oprnd1.val_entry
3321 = add_addr_table_entry (addr,
3322 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3323 else
3324 ref->dw_loc_oprnd1.val_entry = NULL;
3326 return ref;
3329 /* Section names used to hold DWARF debugging information. */
3331 #ifndef DEBUG_INFO_SECTION
3332 #define DEBUG_INFO_SECTION ".debug_info"
3333 #endif
3334 #ifndef DEBUG_DWO_INFO_SECTION
3335 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3336 #endif
3337 #ifndef DEBUG_ABBREV_SECTION
3338 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3339 #endif
3340 #ifndef DEBUG_DWO_ABBREV_SECTION
3341 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3342 #endif
3343 #ifndef DEBUG_ARANGES_SECTION
3344 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3345 #endif
3346 #ifndef DEBUG_ADDR_SECTION
3347 #define DEBUG_ADDR_SECTION ".debug_addr"
3348 #endif
3349 #ifndef DEBUG_NORM_MACINFO_SECTION
3350 #define DEBUG_NORM_MACINFO_SECTION ".debug_macinfo"
3351 #endif
3352 #ifndef DEBUG_DWO_MACINFO_SECTION
3353 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
3354 #endif
3355 #ifndef DEBUG_MACINFO_SECTION
3356 #define DEBUG_MACINFO_SECTION \
3357 (!dwarf_split_debug_info \
3358 ? (DEBUG_NORM_MACINFO_SECTION) : (DEBUG_DWO_MACINFO_SECTION))
3359 #endif
3360 #ifndef DEBUG_NORM_MACRO_SECTION
3361 #define DEBUG_NORM_MACRO_SECTION ".debug_macro"
3362 #endif
3363 #ifndef DEBUG_DWO_MACRO_SECTION
3364 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
3365 #endif
3366 #ifndef DEBUG_MACRO_SECTION
3367 #define DEBUG_MACRO_SECTION \
3368 (!dwarf_split_debug_info \
3369 ? (DEBUG_NORM_MACRO_SECTION) : (DEBUG_DWO_MACRO_SECTION))
3370 #endif
3371 #ifndef DEBUG_LINE_SECTION
3372 #define DEBUG_LINE_SECTION ".debug_line"
3373 #endif
3374 #ifndef DEBUG_DWO_LINE_SECTION
3375 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3376 #endif
3377 #ifndef DEBUG_LOC_SECTION
3378 #define DEBUG_LOC_SECTION ".debug_loc"
3379 #endif
3380 #ifndef DEBUG_DWO_LOC_SECTION
3381 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
3382 #endif
3383 #ifndef DEBUG_PUBNAMES_SECTION
3384 #define DEBUG_PUBNAMES_SECTION \
3385 ((debug_generate_pub_sections == 2) \
3386 ? ".debug_gnu_pubnames" : ".debug_pubnames")
3387 #endif
3388 #ifndef DEBUG_PUBTYPES_SECTION
3389 #define DEBUG_PUBTYPES_SECTION \
3390 ((debug_generate_pub_sections == 2) \
3391 ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
3392 #endif
3393 #define DEBUG_NORM_STR_OFFSETS_SECTION ".debug_str_offsets"
3394 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3395 #ifndef DEBUG_STR_OFFSETS_SECTION
3396 #define DEBUG_STR_OFFSETS_SECTION \
3397 (!dwarf_split_debug_info \
3398 ? (DEBUG_NORM_STR_OFFSETS_SECTION) : (DEBUG_DWO_STR_OFFSETS_SECTION))
3399 #endif
3400 #ifndef DEBUG_STR_DWO_SECTION
3401 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
3402 #endif
3403 #ifndef DEBUG_STR_SECTION
3404 #define DEBUG_STR_SECTION ".debug_str"
3405 #endif
3406 #ifndef DEBUG_RANGES_SECTION
3407 #define DEBUG_RANGES_SECTION ".debug_ranges"
3408 #endif
3410 /* Standard ELF section names for compiled code and data. */
3411 #ifndef TEXT_SECTION_NAME
3412 #define TEXT_SECTION_NAME ".text"
3413 #endif
3415 /* Section flags for .debug_macinfo/.debug_macro section. */
3416 #define DEBUG_MACRO_SECTION_FLAGS \
3417 (dwarf_split_debug_info ? SECTION_DEBUG | SECTION_EXCLUDE : SECTION_DEBUG)
3419 /* Section flags for .debug_str section. */
3420 #define DEBUG_STR_SECTION_FLAGS \
3421 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
3422 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3423 : SECTION_DEBUG)
3425 /* Section flags for .debug_str.dwo section. */
3426 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
3428 /* Labels we insert at beginning sections we can reference instead of
3429 the section names themselves. */
3431 #ifndef TEXT_SECTION_LABEL
3432 #define TEXT_SECTION_LABEL "Ltext"
3433 #endif
3434 #ifndef COLD_TEXT_SECTION_LABEL
3435 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
3436 #endif
3437 #ifndef DEBUG_LINE_SECTION_LABEL
3438 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3439 #endif
3440 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3441 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
3442 #endif
3443 #ifndef DEBUG_INFO_SECTION_LABEL
3444 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3445 #endif
3446 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3447 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
3448 #endif
3449 #ifndef DEBUG_ABBREV_SECTION_LABEL
3450 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3451 #endif
3452 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3453 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3454 #endif
3455 #ifndef DEBUG_ADDR_SECTION_LABEL
3456 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
3457 #endif
3458 #ifndef DEBUG_LOC_SECTION_LABEL
3459 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3460 #endif
3461 #ifndef DEBUG_RANGES_SECTION_LABEL
3462 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3463 #endif
3464 #ifndef DEBUG_MACINFO_SECTION_LABEL
3465 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3466 #endif
3467 #ifndef DEBUG_MACRO_SECTION_LABEL
3468 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
3469 #endif
3470 #define SKELETON_COMP_DIE_ABBREV 1
3471 #define SKELETON_TYPE_DIE_ABBREV 2
3473 /* Definitions of defaults for formats and names of various special
3474 (artificial) labels which may be generated within this file (when the -g
3475 options is used and DWARF2_DEBUGGING_INFO is in effect.
3476 If necessary, these may be overridden from within the tm.h file, but
3477 typically, overriding these defaults is unnecessary. */
3479 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3480 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3481 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3482 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3483 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3484 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3485 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3486 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3487 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3488 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3489 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3490 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3491 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3492 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3494 #ifndef TEXT_END_LABEL
3495 #define TEXT_END_LABEL "Letext"
3496 #endif
3497 #ifndef COLD_END_LABEL
3498 #define COLD_END_LABEL "Letext_cold"
3499 #endif
3500 #ifndef BLOCK_BEGIN_LABEL
3501 #define BLOCK_BEGIN_LABEL "LBB"
3502 #endif
3503 #ifndef BLOCK_END_LABEL
3504 #define BLOCK_END_LABEL "LBE"
3505 #endif
3506 #ifndef LINE_CODE_LABEL
3507 #define LINE_CODE_LABEL "LM"
3508 #endif
3511 /* Return the root of the DIE's built for the current compilation unit. */
3512 static dw_die_ref
3513 comp_unit_die (void)
3515 if (!single_comp_unit_die)
3516 single_comp_unit_die = gen_compile_unit_die (NULL);
3517 return single_comp_unit_die;
3520 /* We allow a language front-end to designate a function that is to be
3521 called to "demangle" any name before it is put into a DIE. */
3523 static const char *(*demangle_name_func) (const char *);
3525 void
3526 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3528 demangle_name_func = func;
3531 /* Test if rtl node points to a pseudo register. */
3533 static inline int
3534 is_pseudo_reg (const_rtx rtl)
3536 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3537 || (GET_CODE (rtl) == SUBREG
3538 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3541 /* Return a reference to a type, with its const and volatile qualifiers
3542 removed. */
3544 static inline tree
3545 type_main_variant (tree type)
3547 type = TYPE_MAIN_VARIANT (type);
3549 /* ??? There really should be only one main variant among any group of
3550 variants of a given type (and all of the MAIN_VARIANT values for all
3551 members of the group should point to that one type) but sometimes the C
3552 front-end messes this up for array types, so we work around that bug
3553 here. */
3554 if (TREE_CODE (type) == ARRAY_TYPE)
3555 while (type != TYPE_MAIN_VARIANT (type))
3556 type = TYPE_MAIN_VARIANT (type);
3558 return type;
3561 /* Return nonzero if the given type node represents a tagged type. */
3563 static inline int
3564 is_tagged_type (const_tree type)
3566 enum tree_code code = TREE_CODE (type);
3568 return (code == RECORD_TYPE || code == UNION_TYPE
3569 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3572 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
3574 static void
3575 get_ref_die_offset_label (char *label, dw_die_ref ref)
3577 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3580 /* Return die_offset of a DIE reference to a base type. */
3582 static unsigned long int
3583 get_base_type_offset (dw_die_ref ref)
3585 if (ref->die_offset)
3586 return ref->die_offset;
3587 if (comp_unit_die ()->die_abbrev)
3589 calc_base_type_die_sizes ();
3590 gcc_assert (ref->die_offset);
3592 return ref->die_offset;
3595 /* Return die_offset of a DIE reference other than base type. */
3597 static unsigned long int
3598 get_ref_die_offset (dw_die_ref ref)
3600 gcc_assert (ref->die_offset);
3601 return ref->die_offset;
3604 /* Convert a DIE tag into its string name. */
3606 static const char *
3607 dwarf_tag_name (unsigned int tag)
3609 const char *name = get_DW_TAG_name (tag);
3611 if (name != NULL)
3612 return name;
3614 return "DW_TAG_<unknown>";
3617 /* Convert a DWARF attribute code into its string name. */
3619 static const char *
3620 dwarf_attr_name (unsigned int attr)
3622 const char *name;
3624 switch (attr)
3626 #if VMS_DEBUGGING_INFO
3627 case DW_AT_HP_prologue:
3628 return "DW_AT_HP_prologue";
3629 #else
3630 case DW_AT_MIPS_loop_unroll_factor:
3631 return "DW_AT_MIPS_loop_unroll_factor";
3632 #endif
3634 #if VMS_DEBUGGING_INFO
3635 case DW_AT_HP_epilogue:
3636 return "DW_AT_HP_epilogue";
3637 #else
3638 case DW_AT_MIPS_stride:
3639 return "DW_AT_MIPS_stride";
3640 #endif
3643 name = get_DW_AT_name (attr);
3645 if (name != NULL)
3646 return name;
3648 return "DW_AT_<unknown>";
3651 /* Convert a DWARF value form code into its string name. */
3653 static const char *
3654 dwarf_form_name (unsigned int form)
3656 const char *name = get_DW_FORM_name (form);
3658 if (name != NULL)
3659 return name;
3661 return "DW_FORM_<unknown>";
3664 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
3665 instance of an inlined instance of a decl which is local to an inline
3666 function, so we have to trace all of the way back through the origin chain
3667 to find out what sort of node actually served as the original seed for the
3668 given block. */
3670 static tree
3671 decl_ultimate_origin (const_tree decl)
3673 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
3674 return NULL_TREE;
3676 /* DECL_ABSTRACT_ORIGIN can point to itself; ignore that if
3677 we're trying to output the abstract instance of this function. */
3678 if (DECL_ABSTRACT_P (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
3679 return NULL_TREE;
3681 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
3682 most distant ancestor, this should never happen. */
3683 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
3685 return DECL_ABSTRACT_ORIGIN (decl);
3688 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
3689 of a virtual function may refer to a base class, so we check the 'this'
3690 parameter. */
3692 static tree
3693 decl_class_context (tree decl)
3695 tree context = NULL_TREE;
3697 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
3698 context = DECL_CONTEXT (decl);
3699 else
3700 context = TYPE_MAIN_VARIANT
3701 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
3703 if (context && !TYPE_P (context))
3704 context = NULL_TREE;
3706 return context;
3709 /* Add an attribute/value pair to a DIE. */
3711 static inline void
3712 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
3714 /* Maybe this should be an assert? */
3715 if (die == NULL)
3716 return;
3718 vec_safe_reserve (die->die_attr, 1);
3719 vec_safe_push (die->die_attr, *attr);
3722 static inline enum dw_val_class
3723 AT_class (dw_attr_ref a)
3725 return a->dw_attr_val.val_class;
3728 /* Return the index for any attribute that will be referenced with a
3729 DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index. String indices
3730 are stored in dw_attr_val.v.val_str for reference counting
3731 pruning. */
3733 static inline unsigned int
3734 AT_index (dw_attr_ref a)
3736 if (AT_class (a) == dw_val_class_str)
3737 return a->dw_attr_val.v.val_str->index;
3738 else if (a->dw_attr_val.val_entry != NULL)
3739 return a->dw_attr_val.val_entry->index;
3740 return NOT_INDEXED;
3743 /* Add a flag value attribute to a DIE. */
3745 static inline void
3746 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
3748 dw_attr_node attr;
3750 attr.dw_attr = attr_kind;
3751 attr.dw_attr_val.val_class = dw_val_class_flag;
3752 attr.dw_attr_val.val_entry = NULL;
3753 attr.dw_attr_val.v.val_flag = flag;
3754 add_dwarf_attr (die, &attr);
3757 static inline unsigned
3758 AT_flag (dw_attr_ref a)
3760 gcc_assert (a && AT_class (a) == dw_val_class_flag);
3761 return a->dw_attr_val.v.val_flag;
3764 /* Add a signed integer attribute value to a DIE. */
3766 static inline void
3767 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
3769 dw_attr_node attr;
3771 attr.dw_attr = attr_kind;
3772 attr.dw_attr_val.val_class = dw_val_class_const;
3773 attr.dw_attr_val.val_entry = NULL;
3774 attr.dw_attr_val.v.val_int = int_val;
3775 add_dwarf_attr (die, &attr);
3778 static inline HOST_WIDE_INT
3779 AT_int (dw_attr_ref a)
3781 gcc_assert (a && AT_class (a) == dw_val_class_const);
3782 return a->dw_attr_val.v.val_int;
3785 /* Add an unsigned integer attribute value to a DIE. */
3787 static inline void
3788 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
3789 unsigned HOST_WIDE_INT unsigned_val)
3791 dw_attr_node attr;
3793 attr.dw_attr = attr_kind;
3794 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
3795 attr.dw_attr_val.val_entry = NULL;
3796 attr.dw_attr_val.v.val_unsigned = unsigned_val;
3797 add_dwarf_attr (die, &attr);
3800 static inline unsigned HOST_WIDE_INT
3801 AT_unsigned (dw_attr_ref a)
3803 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
3804 return a->dw_attr_val.v.val_unsigned;
3807 /* Add an unsigned wide integer attribute value to a DIE. */
3809 static inline void
3810 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
3811 const wide_int& w)
3813 dw_attr_node attr;
3815 attr.dw_attr = attr_kind;
3816 attr.dw_attr_val.val_class = dw_val_class_wide_int;
3817 attr.dw_attr_val.v.val_wide = ggc_cleared_alloc<wide_int> ();
3818 *attr.dw_attr_val.v.val_wide = w;
3819 add_dwarf_attr (die, &attr);
3822 /* Add an unsigned double integer attribute value to a DIE. */
3824 static inline void
3825 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
3826 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
3828 dw_attr_node attr;
3830 attr.dw_attr = attr_kind;
3831 attr.dw_attr_val.val_class = dw_val_class_const_double;
3832 attr.dw_attr_val.val_entry = NULL;
3833 attr.dw_attr_val.v.val_double.high = high;
3834 attr.dw_attr_val.v.val_double.low = low;
3835 add_dwarf_attr (die, &attr);
3838 /* Add a floating point attribute value to a DIE and return it. */
3840 static inline void
3841 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
3842 unsigned int length, unsigned int elt_size, unsigned char *array)
3844 dw_attr_node attr;
3846 attr.dw_attr = attr_kind;
3847 attr.dw_attr_val.val_class = dw_val_class_vec;
3848 attr.dw_attr_val.val_entry = NULL;
3849 attr.dw_attr_val.v.val_vec.length = length;
3850 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
3851 attr.dw_attr_val.v.val_vec.array = array;
3852 add_dwarf_attr (die, &attr);
3855 /* Add an 8-byte data attribute value to a DIE. */
3857 static inline void
3858 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
3859 unsigned char data8[8])
3861 dw_attr_node attr;
3863 attr.dw_attr = attr_kind;
3864 attr.dw_attr_val.val_class = dw_val_class_data8;
3865 attr.dw_attr_val.val_entry = NULL;
3866 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
3867 add_dwarf_attr (die, &attr);
3870 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
3871 dwarf_split_debug_info, address attributes in dies destined for the
3872 final executable have force_direct set to avoid using indexed
3873 references. */
3875 static inline void
3876 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
3877 bool force_direct)
3879 dw_attr_node attr;
3880 char * lbl_id;
3882 lbl_id = xstrdup (lbl_low);
3883 attr.dw_attr = DW_AT_low_pc;
3884 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3885 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3886 if (dwarf_split_debug_info && !force_direct)
3887 attr.dw_attr_val.val_entry
3888 = add_addr_table_entry (lbl_id, ate_kind_label);
3889 else
3890 attr.dw_attr_val.val_entry = NULL;
3891 add_dwarf_attr (die, &attr);
3893 attr.dw_attr = DW_AT_high_pc;
3894 if (dwarf_version < 4)
3895 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3896 else
3897 attr.dw_attr_val.val_class = dw_val_class_high_pc;
3898 lbl_id = xstrdup (lbl_high);
3899 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3900 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
3901 && dwarf_split_debug_info && !force_direct)
3902 attr.dw_attr_val.val_entry
3903 = add_addr_table_entry (lbl_id, ate_kind_label);
3904 else
3905 attr.dw_attr_val.val_entry = NULL;
3906 add_dwarf_attr (die, &attr);
3909 /* Hash and equality functions for debug_str_hash. */
3911 static hashval_t
3912 debug_str_do_hash (const void *x)
3914 return htab_hash_string (((const struct indirect_string_node *)x)->str);
3917 static int
3918 debug_str_eq (const void *x1, const void *x2)
3920 return strcmp ((((const struct indirect_string_node *)x1)->str),
3921 (const char *)x2) == 0;
3924 /* Add STR to the given string hash table. */
3926 static struct indirect_string_node *
3927 find_AT_string_in_table (const char *str, htab_t table)
3929 struct indirect_string_node *node;
3930 void **slot;
3932 slot = htab_find_slot_with_hash (table, str,
3933 htab_hash_string (str), INSERT);
3934 if (*slot == NULL)
3936 node = ggc_cleared_alloc<indirect_string_node> ();
3937 node->str = ggc_strdup (str);
3938 *slot = node;
3940 else
3941 node = (struct indirect_string_node *) *slot;
3943 node->refcount++;
3944 return node;
3947 /* Add STR to the indirect string hash table. */
3949 static struct indirect_string_node *
3950 find_AT_string (const char *str)
3952 if (! debug_str_hash)
3953 debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
3954 debug_str_eq, NULL);
3956 return find_AT_string_in_table (str, debug_str_hash);
3959 /* Add a string attribute value to a DIE. */
3961 static inline void
3962 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
3964 dw_attr_node attr;
3965 struct indirect_string_node *node;
3967 node = find_AT_string (str);
3969 attr.dw_attr = attr_kind;
3970 attr.dw_attr_val.val_class = dw_val_class_str;
3971 attr.dw_attr_val.val_entry = NULL;
3972 attr.dw_attr_val.v.val_str = node;
3973 add_dwarf_attr (die, &attr);
3976 static inline const char *
3977 AT_string (dw_attr_ref a)
3979 gcc_assert (a && AT_class (a) == dw_val_class_str);
3980 return a->dw_attr_val.v.val_str->str;
3983 /* Call this function directly to bypass AT_string_form's logic to put
3984 the string inline in the die. */
3986 static void
3987 set_indirect_string (struct indirect_string_node *node)
3989 char label[32];
3990 /* Already indirect is a no op. */
3991 if (node->form == DW_FORM_strp || node->form == DW_FORM_GNU_str_index)
3993 gcc_assert (node->label);
3994 return;
3996 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
3997 ++dw2_string_counter;
3998 node->label = xstrdup (label);
4000 if (!dwarf_split_debug_info)
4002 node->form = DW_FORM_strp;
4003 node->index = NOT_INDEXED;
4005 else
4007 node->form = DW_FORM_GNU_str_index;
4008 node->index = NO_INDEX_ASSIGNED;
4012 /* Find out whether a string should be output inline in DIE
4013 or out-of-line in .debug_str section. */
4015 static enum dwarf_form
4016 find_string_form (struct indirect_string_node *node)
4018 unsigned int len;
4020 if (node->form)
4021 return node->form;
4023 len = strlen (node->str) + 1;
4025 /* If the string is shorter or equal to the size of the reference, it is
4026 always better to put it inline. */
4027 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4028 return node->form = DW_FORM_string;
4030 /* If we cannot expect the linker to merge strings in .debug_str
4031 section, only put it into .debug_str if it is worth even in this
4032 single module. */
4033 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4034 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4035 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
4036 return node->form = DW_FORM_string;
4038 set_indirect_string (node);
4040 return node->form;
4043 /* Find out whether the string referenced from the attribute should be
4044 output inline in DIE or out-of-line in .debug_str section. */
4046 static enum dwarf_form
4047 AT_string_form (dw_attr_ref a)
4049 gcc_assert (a && AT_class (a) == dw_val_class_str);
4050 return find_string_form (a->dw_attr_val.v.val_str);
4053 /* Add a DIE reference attribute value to a DIE. */
4055 static inline void
4056 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4058 dw_attr_node attr;
4060 #ifdef ENABLE_CHECKING
4061 gcc_assert (targ_die != NULL);
4062 #else
4063 /* With LTO we can end up trying to reference something we didn't create
4064 a DIE for. Avoid crashing later on a NULL referenced DIE. */
4065 if (targ_die == NULL)
4066 return;
4067 #endif
4069 attr.dw_attr = attr_kind;
4070 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4071 attr.dw_attr_val.val_entry = NULL;
4072 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4073 attr.dw_attr_val.v.val_die_ref.external = 0;
4074 add_dwarf_attr (die, &attr);
4077 /* Change DIE reference REF to point to NEW_DIE instead. */
4079 static inline void
4080 change_AT_die_ref (dw_attr_ref ref, dw_die_ref new_die)
4082 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4083 ref->dw_attr_val.v.val_die_ref.die = new_die;
4084 ref->dw_attr_val.v.val_die_ref.external = 0;
4087 /* Add an AT_specification attribute to a DIE, and also make the back
4088 pointer from the specification to the definition. */
4090 static inline void
4091 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4093 add_AT_die_ref (die, DW_AT_specification, targ_die);
4094 gcc_assert (!targ_die->die_definition);
4095 targ_die->die_definition = die;
4098 static inline dw_die_ref
4099 AT_ref (dw_attr_ref a)
4101 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4102 return a->dw_attr_val.v.val_die_ref.die;
4105 static inline int
4106 AT_ref_external (dw_attr_ref a)
4108 if (a && AT_class (a) == dw_val_class_die_ref)
4109 return a->dw_attr_val.v.val_die_ref.external;
4111 return 0;
4114 static inline void
4115 set_AT_ref_external (dw_attr_ref a, int i)
4117 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4118 a->dw_attr_val.v.val_die_ref.external = i;
4121 /* Add an FDE reference attribute value to a DIE. */
4123 static inline void
4124 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4126 dw_attr_node attr;
4128 attr.dw_attr = attr_kind;
4129 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4130 attr.dw_attr_val.val_entry = NULL;
4131 attr.dw_attr_val.v.val_fde_index = targ_fde;
4132 add_dwarf_attr (die, &attr);
4135 /* Add a location description attribute value to a DIE. */
4137 static inline void
4138 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4140 dw_attr_node attr;
4142 attr.dw_attr = attr_kind;
4143 attr.dw_attr_val.val_class = dw_val_class_loc;
4144 attr.dw_attr_val.val_entry = NULL;
4145 attr.dw_attr_val.v.val_loc = loc;
4146 add_dwarf_attr (die, &attr);
4149 static inline dw_loc_descr_ref
4150 AT_loc (dw_attr_ref a)
4152 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4153 return a->dw_attr_val.v.val_loc;
4156 static inline void
4157 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4159 dw_attr_node attr;
4161 attr.dw_attr = attr_kind;
4162 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4163 attr.dw_attr_val.val_entry = NULL;
4164 attr.dw_attr_val.v.val_loc_list = loc_list;
4165 add_dwarf_attr (die, &attr);
4166 have_location_lists = true;
4169 static inline dw_loc_list_ref
4170 AT_loc_list (dw_attr_ref a)
4172 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4173 return a->dw_attr_val.v.val_loc_list;
4176 static inline dw_loc_list_ref *
4177 AT_loc_list_ptr (dw_attr_ref a)
4179 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4180 return &a->dw_attr_val.v.val_loc_list;
4183 /* Table of entries into the .debug_addr section. */
4185 static GTY ((param_is (addr_table_entry))) htab_t addr_index_table;
4187 /* Hash an address_table_entry. */
4189 static hashval_t
4190 addr_table_entry_do_hash (const void *x)
4192 const addr_table_entry *a = (const addr_table_entry *) x;
4193 inchash::hash hstate;
4194 switch (a->kind)
4196 case ate_kind_rtx:
4197 hstate.add_int (0);
4198 break;
4199 case ate_kind_rtx_dtprel:
4200 hstate.add_int (1);
4201 break;
4202 case ate_kind_label:
4203 return htab_hash_string (a->addr.label);
4204 default:
4205 gcc_unreachable ();
4207 inchash::add_rtx (a->addr.rtl, hstate);
4208 return hstate.end ();
4211 /* Determine equality for two address_table_entries. */
4213 static int
4214 addr_table_entry_eq (const void *x1, const void *x2)
4216 const addr_table_entry *a1 = (const addr_table_entry *) x1;
4217 const addr_table_entry *a2 = (const addr_table_entry *) x2;
4219 if (a1->kind != a2->kind)
4220 return 0;
4221 switch (a1->kind)
4223 case ate_kind_rtx:
4224 case ate_kind_rtx_dtprel:
4225 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4226 case ate_kind_label:
4227 return strcmp (a1->addr.label, a2->addr.label) == 0;
4228 default:
4229 gcc_unreachable ();
4233 /* Initialize an addr_table_entry. */
4235 void
4236 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4238 e->kind = kind;
4239 switch (kind)
4241 case ate_kind_rtx:
4242 case ate_kind_rtx_dtprel:
4243 e->addr.rtl = (rtx) addr;
4244 break;
4245 case ate_kind_label:
4246 e->addr.label = (char *) addr;
4247 break;
4249 e->refcount = 0;
4250 e->index = NO_INDEX_ASSIGNED;
4253 /* Add attr to the address table entry to the table. Defer setting an
4254 index until output time. */
4256 static addr_table_entry *
4257 add_addr_table_entry (void *addr, enum ate_kind kind)
4259 addr_table_entry *node;
4260 addr_table_entry finder;
4261 void **slot;
4263 gcc_assert (dwarf_split_debug_info);
4264 if (! addr_index_table)
4265 addr_index_table = htab_create_ggc (10, addr_table_entry_do_hash,
4266 addr_table_entry_eq, NULL);
4267 init_addr_table_entry (&finder, kind, addr);
4268 slot = htab_find_slot (addr_index_table, &finder, INSERT);
4270 if (*slot == HTAB_EMPTY_ENTRY)
4272 node = ggc_cleared_alloc<addr_table_entry> ();
4273 init_addr_table_entry (node, kind, addr);
4274 *slot = node;
4276 else
4277 node = (addr_table_entry *) *slot;
4279 node->refcount++;
4280 return node;
4283 /* Remove an entry from the addr table by decrementing its refcount.
4284 Strictly, decrementing the refcount would be enough, but the
4285 assertion that the entry is actually in the table has found
4286 bugs. */
4288 static void
4289 remove_addr_table_entry (addr_table_entry *entry)
4291 gcc_assert (dwarf_split_debug_info && addr_index_table);
4292 /* After an index is assigned, the table is frozen. */
4293 gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
4294 entry->refcount--;
4297 /* Given a location list, remove all addresses it refers to from the
4298 address_table. */
4300 static void
4301 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4303 for (; descr; descr = descr->dw_loc_next)
4304 if (descr->dw_loc_oprnd1.val_entry != NULL)
4306 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4307 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4311 /* A helper function for dwarf2out_finish called through
4312 htab_traverse. Assign an addr_table_entry its index. All entries
4313 must be collected into the table when this function is called,
4314 because the indexing code relies on htab_traverse to traverse nodes
4315 in the same order for each run. */
4317 static int
4318 index_addr_table_entry (void **h, void *v)
4320 addr_table_entry *node = (addr_table_entry *) *h;
4321 unsigned int *index = (unsigned int *) v;
4323 /* Don't index unreferenced nodes. */
4324 if (node->refcount == 0)
4325 return 1;
4327 gcc_assert (node->index == NO_INDEX_ASSIGNED);
4328 node->index = *index;
4329 *index += 1;
4331 return 1;
4334 /* Add an address constant attribute value to a DIE. When using
4335 dwarf_split_debug_info, address attributes in dies destined for the
4336 final executable should be direct references--setting the parameter
4337 force_direct ensures this behavior. */
4339 static inline void
4340 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4341 bool force_direct)
4343 dw_attr_node attr;
4345 attr.dw_attr = attr_kind;
4346 attr.dw_attr_val.val_class = dw_val_class_addr;
4347 attr.dw_attr_val.v.val_addr = addr;
4348 if (dwarf_split_debug_info && !force_direct)
4349 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4350 else
4351 attr.dw_attr_val.val_entry = NULL;
4352 add_dwarf_attr (die, &attr);
4355 /* Get the RTX from to an address DIE attribute. */
4357 static inline rtx
4358 AT_addr (dw_attr_ref a)
4360 gcc_assert (a && AT_class (a) == dw_val_class_addr);
4361 return a->dw_attr_val.v.val_addr;
4364 /* Add a file attribute value to a DIE. */
4366 static inline void
4367 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4368 struct dwarf_file_data *fd)
4370 dw_attr_node attr;
4372 attr.dw_attr = attr_kind;
4373 attr.dw_attr_val.val_class = dw_val_class_file;
4374 attr.dw_attr_val.val_entry = NULL;
4375 attr.dw_attr_val.v.val_file = fd;
4376 add_dwarf_attr (die, &attr);
4379 /* Get the dwarf_file_data from a file DIE attribute. */
4381 static inline struct dwarf_file_data *
4382 AT_file (dw_attr_ref a)
4384 gcc_assert (a && AT_class (a) == dw_val_class_file);
4385 return a->dw_attr_val.v.val_file;
4388 /* Add a vms delta attribute value to a DIE. */
4390 static inline void
4391 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4392 const char *lbl1, const char *lbl2)
4394 dw_attr_node attr;
4396 attr.dw_attr = attr_kind;
4397 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4398 attr.dw_attr_val.val_entry = NULL;
4399 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4400 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4401 add_dwarf_attr (die, &attr);
4404 /* Add a label identifier attribute value to a DIE. */
4406 static inline void
4407 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4408 const char *lbl_id)
4410 dw_attr_node attr;
4412 attr.dw_attr = attr_kind;
4413 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4414 attr.dw_attr_val.val_entry = NULL;
4415 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4416 if (dwarf_split_debug_info)
4417 attr.dw_attr_val.val_entry
4418 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4419 ate_kind_label);
4420 add_dwarf_attr (die, &attr);
4423 /* Add a section offset attribute value to a DIE, an offset into the
4424 debug_line section. */
4426 static inline void
4427 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4428 const char *label)
4430 dw_attr_node attr;
4432 attr.dw_attr = attr_kind;
4433 attr.dw_attr_val.val_class = dw_val_class_lineptr;
4434 attr.dw_attr_val.val_entry = NULL;
4435 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4436 add_dwarf_attr (die, &attr);
4439 /* Add a section offset attribute value to a DIE, an offset into the
4440 debug_macinfo section. */
4442 static inline void
4443 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4444 const char *label)
4446 dw_attr_node attr;
4448 attr.dw_attr = attr_kind;
4449 attr.dw_attr_val.val_class = dw_val_class_macptr;
4450 attr.dw_attr_val.val_entry = NULL;
4451 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4452 add_dwarf_attr (die, &attr);
4455 /* Add an offset attribute value to a DIE. */
4457 static inline void
4458 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4459 unsigned HOST_WIDE_INT offset)
4461 dw_attr_node attr;
4463 attr.dw_attr = attr_kind;
4464 attr.dw_attr_val.val_class = dw_val_class_offset;
4465 attr.dw_attr_val.val_entry = NULL;
4466 attr.dw_attr_val.v.val_offset = offset;
4467 add_dwarf_attr (die, &attr);
4470 /* Add a range_list attribute value to a DIE. When using
4471 dwarf_split_debug_info, address attributes in dies destined for the
4472 final executable should be direct references--setting the parameter
4473 force_direct ensures this behavior. */
4475 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4476 #define RELOCATED_OFFSET (NULL)
4478 static void
4479 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4480 long unsigned int offset, bool force_direct)
4482 dw_attr_node attr;
4484 attr.dw_attr = attr_kind;
4485 attr.dw_attr_val.val_class = dw_val_class_range_list;
4486 /* For the range_list attribute, use val_entry to store whether the
4487 offset should follow split-debug-info or normal semantics. This
4488 value is read in output_range_list_offset. */
4489 if (dwarf_split_debug_info && !force_direct)
4490 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4491 else
4492 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4493 attr.dw_attr_val.v.val_offset = offset;
4494 add_dwarf_attr (die, &attr);
4497 /* Return the start label of a delta attribute. */
4499 static inline const char *
4500 AT_vms_delta1 (dw_attr_ref a)
4502 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4503 return a->dw_attr_val.v.val_vms_delta.lbl1;
4506 /* Return the end label of a delta attribute. */
4508 static inline const char *
4509 AT_vms_delta2 (dw_attr_ref a)
4511 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4512 return a->dw_attr_val.v.val_vms_delta.lbl2;
4515 static inline const char *
4516 AT_lbl (dw_attr_ref a)
4518 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4519 || AT_class (a) == dw_val_class_lineptr
4520 || AT_class (a) == dw_val_class_macptr
4521 || AT_class (a) == dw_val_class_high_pc));
4522 return a->dw_attr_val.v.val_lbl_id;
4525 /* Get the attribute of type attr_kind. */
4527 static dw_attr_ref
4528 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4530 dw_attr_ref a;
4531 unsigned ix;
4532 dw_die_ref spec = NULL;
4534 if (! die)
4535 return NULL;
4537 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4538 if (a->dw_attr == attr_kind)
4539 return a;
4540 else if (a->dw_attr == DW_AT_specification
4541 || a->dw_attr == DW_AT_abstract_origin)
4542 spec = AT_ref (a);
4544 if (spec)
4545 return get_AT (spec, attr_kind);
4547 return NULL;
4550 /* Returns the parent of the declaration of DIE. */
4552 static dw_die_ref
4553 get_die_parent (dw_die_ref die)
4555 dw_die_ref t;
4557 if (!die)
4558 return NULL;
4560 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4561 || (t = get_AT_ref (die, DW_AT_specification)))
4562 die = t;
4564 return die->die_parent;
4567 /* Return the "low pc" attribute value, typically associated with a subprogram
4568 DIE. Return null if the "low pc" attribute is either not present, or if it
4569 cannot be represented as an assembler label identifier. */
4571 static inline const char *
4572 get_AT_low_pc (dw_die_ref die)
4574 dw_attr_ref a = get_AT (die, DW_AT_low_pc);
4576 return a ? AT_lbl (a) : NULL;
4579 /* Return the "high pc" attribute value, typically associated with a subprogram
4580 DIE. Return null if the "high pc" attribute is either not present, or if it
4581 cannot be represented as an assembler label identifier. */
4583 static inline const char *
4584 get_AT_hi_pc (dw_die_ref die)
4586 dw_attr_ref a = get_AT (die, DW_AT_high_pc);
4588 return a ? AT_lbl (a) : NULL;
4591 /* Return the value of the string attribute designated by ATTR_KIND, or
4592 NULL if it is not present. */
4594 static inline const char *
4595 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4597 dw_attr_ref a = get_AT (die, attr_kind);
4599 return a ? AT_string (a) : NULL;
4602 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4603 if it is not present. */
4605 static inline int
4606 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4608 dw_attr_ref a = get_AT (die, attr_kind);
4610 return a ? AT_flag (a) : 0;
4613 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4614 if it is not present. */
4616 static inline unsigned
4617 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4619 dw_attr_ref a = get_AT (die, attr_kind);
4621 return a ? AT_unsigned (a) : 0;
4624 static inline dw_die_ref
4625 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4627 dw_attr_ref a = get_AT (die, attr_kind);
4629 return a ? AT_ref (a) : NULL;
4632 static inline struct dwarf_file_data *
4633 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4635 dw_attr_ref a = get_AT (die, attr_kind);
4637 return a ? AT_file (a) : NULL;
4640 /* Return TRUE if the language is C++. */
4642 static inline bool
4643 is_cxx (void)
4645 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4647 return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
4650 /* Return TRUE if the language is Java. */
4652 static inline bool
4653 is_java (void)
4655 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4657 return lang == DW_LANG_Java;
4660 /* Return TRUE if the language is Fortran. */
4662 static inline bool
4663 is_fortran (void)
4665 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4667 return (lang == DW_LANG_Fortran77
4668 || lang == DW_LANG_Fortran90
4669 || lang == DW_LANG_Fortran95);
4672 /* Return TRUE if the language is Ada. */
4674 static inline bool
4675 is_ada (void)
4677 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4679 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4682 /* Remove the specified attribute if present. */
4684 static void
4685 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4687 dw_attr_ref a;
4688 unsigned ix;
4690 if (! die)
4691 return;
4693 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4694 if (a->dw_attr == attr_kind)
4696 if (AT_class (a) == dw_val_class_str)
4697 if (a->dw_attr_val.v.val_str->refcount)
4698 a->dw_attr_val.v.val_str->refcount--;
4700 /* vec::ordered_remove should help reduce the number of abbrevs
4701 that are needed. */
4702 die->die_attr->ordered_remove (ix);
4703 return;
4707 /* Remove CHILD from its parent. PREV must have the property that
4708 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
4710 static void
4711 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4713 gcc_assert (child->die_parent == prev->die_parent);
4714 gcc_assert (prev->die_sib == child);
4715 if (prev == child)
4717 gcc_assert (child->die_parent->die_child == child);
4718 prev = NULL;
4720 else
4721 prev->die_sib = child->die_sib;
4722 if (child->die_parent->die_child == child)
4723 child->die_parent->die_child = prev;
4726 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
4727 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
4729 static void
4730 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4732 dw_die_ref parent = old_child->die_parent;
4734 gcc_assert (parent == prev->die_parent);
4735 gcc_assert (prev->die_sib == old_child);
4737 new_child->die_parent = parent;
4738 if (prev == old_child)
4740 gcc_assert (parent->die_child == old_child);
4741 new_child->die_sib = new_child;
4743 else
4745 prev->die_sib = new_child;
4746 new_child->die_sib = old_child->die_sib;
4748 if (old_child->die_parent->die_child == old_child)
4749 old_child->die_parent->die_child = new_child;
4752 /* Move all children from OLD_PARENT to NEW_PARENT. */
4754 static void
4755 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4757 dw_die_ref c;
4758 new_parent->die_child = old_parent->die_child;
4759 old_parent->die_child = NULL;
4760 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4763 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
4764 matches TAG. */
4766 static void
4767 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4769 dw_die_ref c;
4771 c = die->die_child;
4772 if (c) do {
4773 dw_die_ref prev = c;
4774 c = c->die_sib;
4775 while (c->die_tag == tag)
4777 remove_child_with_prev (c, prev);
4778 /* Might have removed every child. */
4779 if (c == c->die_sib)
4780 return;
4781 c = c->die_sib;
4783 } while (c != die->die_child);
4786 /* Add a CHILD_DIE as the last child of DIE. */
4788 static void
4789 add_child_die (dw_die_ref die, dw_die_ref child_die)
4791 /* FIXME this should probably be an assert. */
4792 if (! die || ! child_die)
4793 return;
4794 gcc_assert (die != child_die);
4796 child_die->die_parent = die;
4797 if (die->die_child)
4799 child_die->die_sib = die->die_child->die_sib;
4800 die->die_child->die_sib = child_die;
4802 else
4803 child_die->die_sib = child_die;
4804 die->die_child = child_die;
4807 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
4808 is the specification, to the end of PARENT's list of children.
4809 This is done by removing and re-adding it. */
4811 static void
4812 splice_child_die (dw_die_ref parent, dw_die_ref child)
4814 dw_die_ref p;
4816 /* We want the declaration DIE from inside the class, not the
4817 specification DIE at toplevel. */
4818 if (child->die_parent != parent)
4820 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
4822 if (tmp)
4823 child = tmp;
4826 gcc_assert (child->die_parent == parent
4827 || (child->die_parent
4828 == get_AT_ref (parent, DW_AT_specification)));
4830 for (p = child->die_parent->die_child; ; p = p->die_sib)
4831 if (p->die_sib == child)
4833 remove_child_with_prev (child, p);
4834 break;
4837 add_child_die (parent, child);
4840 /* Return a pointer to a newly created DIE node. */
4842 static inline dw_die_ref
4843 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
4845 dw_die_ref die = ggc_cleared_alloc<die_node> ();
4847 die->die_tag = tag_value;
4849 if (parent_die != NULL)
4850 add_child_die (parent_die, die);
4851 else
4853 limbo_die_node *limbo_node;
4855 limbo_node = ggc_cleared_alloc<limbo_die_node> ();
4856 limbo_node->die = die;
4857 limbo_node->created_for = t;
4858 limbo_node->next = limbo_die_list;
4859 limbo_die_list = limbo_node;
4862 return die;
4865 /* Return the DIE associated with the given type specifier. */
4867 static inline dw_die_ref
4868 lookup_type_die (tree type)
4870 return TYPE_SYMTAB_DIE (type);
4873 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
4874 anonymous type named by the typedef TYPE_DIE, return the DIE of the
4875 anonymous type instead the one of the naming typedef. */
4877 static inline dw_die_ref
4878 strip_naming_typedef (tree type, dw_die_ref type_die)
4880 if (type
4881 && TREE_CODE (type) == RECORD_TYPE
4882 && type_die
4883 && type_die->die_tag == DW_TAG_typedef
4884 && is_naming_typedef_decl (TYPE_NAME (type)))
4885 type_die = get_AT_ref (type_die, DW_AT_type);
4886 return type_die;
4889 /* Like lookup_type_die, but if type is an anonymous type named by a
4890 typedef[1], return the DIE of the anonymous type instead the one of
4891 the naming typedef. This is because in gen_typedef_die, we did
4892 equate the anonymous struct named by the typedef with the DIE of
4893 the naming typedef. So by default, lookup_type_die on an anonymous
4894 struct yields the DIE of the naming typedef.
4896 [1]: Read the comment of is_naming_typedef_decl to learn about what
4897 a naming typedef is. */
4899 static inline dw_die_ref
4900 lookup_type_die_strip_naming_typedef (tree type)
4902 dw_die_ref die = lookup_type_die (type);
4903 return strip_naming_typedef (type, die);
4906 /* Equate a DIE to a given type specifier. */
4908 static inline void
4909 equate_type_number_to_die (tree type, dw_die_ref type_die)
4911 TYPE_SYMTAB_DIE (type) = type_die;
4914 /* Returns a hash value for X (which really is a die_struct). */
4916 static hashval_t
4917 decl_die_table_hash (const void *x)
4919 return (hashval_t) ((const_dw_die_ref) x)->decl_id;
4922 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
4924 static int
4925 decl_die_table_eq (const void *x, const void *y)
4927 return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
4930 /* Return the DIE associated with a given declaration. */
4932 static inline dw_die_ref
4933 lookup_decl_die (tree decl)
4935 return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
4938 /* Returns a hash value for X (which really is a var_loc_list). */
4940 static hashval_t
4941 decl_loc_table_hash (const void *x)
4943 return (hashval_t) ((const var_loc_list *) x)->decl_id;
4946 /* Return nonzero if decl_id of var_loc_list X is the same as
4947 UID of decl *Y. */
4949 static int
4950 decl_loc_table_eq (const void *x, const void *y)
4952 return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
4955 /* Return the var_loc list associated with a given declaration. */
4957 static inline var_loc_list *
4958 lookup_decl_loc (const_tree decl)
4960 if (!decl_loc_table)
4961 return NULL;
4962 return (var_loc_list *)
4963 htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
4966 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
4968 static hashval_t
4969 cached_dw_loc_list_table_hash (const void *x)
4971 return (hashval_t) ((const cached_dw_loc_list *) x)->decl_id;
4974 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
4975 UID of decl *Y. */
4977 static int
4978 cached_dw_loc_list_table_eq (const void *x, const void *y)
4980 return (((const cached_dw_loc_list *) x)->decl_id
4981 == DECL_UID ((const_tree) y));
4984 /* Equate a DIE to a particular declaration. */
4986 static void
4987 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
4989 unsigned int decl_id = DECL_UID (decl);
4990 void **slot;
4992 slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
4993 *slot = decl_die;
4994 decl_die->decl_id = decl_id;
4997 /* Return how many bits covers PIECE EXPR_LIST. */
4999 static int
5000 decl_piece_bitsize (rtx piece)
5002 int ret = (int) GET_MODE (piece);
5003 if (ret)
5004 return ret;
5005 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
5006 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
5007 return INTVAL (XEXP (XEXP (piece, 0), 0));
5010 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
5012 static rtx *
5013 decl_piece_varloc_ptr (rtx piece)
5015 if ((int) GET_MODE (piece))
5016 return &XEXP (piece, 0);
5017 else
5018 return &XEXP (XEXP (piece, 0), 1);
5021 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
5022 Next is the chain of following piece nodes. */
5024 static rtx_expr_list *
5025 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
5027 if (bitsize <= (int) MAX_MACHINE_MODE)
5028 return alloc_EXPR_LIST (bitsize, loc_note, next);
5029 else
5030 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
5031 GEN_INT (bitsize),
5032 loc_note), next);
5035 /* Return rtx that should be stored into loc field for
5036 LOC_NOTE and BITPOS/BITSIZE. */
5038 static rtx
5039 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
5040 HOST_WIDE_INT bitsize)
5042 if (bitsize != -1)
5044 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
5045 if (bitpos != 0)
5046 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
5048 return loc_note;
5051 /* This function either modifies location piece list *DEST in
5052 place (if SRC and INNER is NULL), or copies location piece list
5053 *SRC to *DEST while modifying it. Location BITPOS is modified
5054 to contain LOC_NOTE, any pieces overlapping it are removed resp.
5055 not copied and if needed some padding around it is added.
5056 When modifying in place, DEST should point to EXPR_LIST where
5057 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
5058 to the start of the whole list and INNER points to the EXPR_LIST
5059 where earlier pieces cover PIECE_BITPOS bits. */
5061 static void
5062 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5063 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5064 HOST_WIDE_INT bitsize, rtx loc_note)
5066 int diff;
5067 bool copy = inner != NULL;
5069 if (copy)
5071 /* First copy all nodes preceding the current bitpos. */
5072 while (src != inner)
5074 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5075 decl_piece_bitsize (*src), NULL_RTX);
5076 dest = &XEXP (*dest, 1);
5077 src = &XEXP (*src, 1);
5080 /* Add padding if needed. */
5081 if (bitpos != piece_bitpos)
5083 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5084 copy ? NULL_RTX : *dest);
5085 dest = &XEXP (*dest, 1);
5087 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5089 gcc_assert (!copy);
5090 /* A piece with correct bitpos and bitsize already exist,
5091 just update the location for it and return. */
5092 *decl_piece_varloc_ptr (*dest) = loc_note;
5093 return;
5095 /* Add the piece that changed. */
5096 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5097 dest = &XEXP (*dest, 1);
5098 /* Skip over pieces that overlap it. */
5099 diff = bitpos - piece_bitpos + bitsize;
5100 if (!copy)
5101 src = dest;
5102 while (diff > 0 && *src)
5104 rtx piece = *src;
5105 diff -= decl_piece_bitsize (piece);
5106 if (copy)
5107 src = &XEXP (piece, 1);
5108 else
5110 *src = XEXP (piece, 1);
5111 free_EXPR_LIST_node (piece);
5114 /* Add padding if needed. */
5115 if (diff < 0 && *src)
5117 if (!copy)
5118 dest = src;
5119 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5120 dest = &XEXP (*dest, 1);
5122 if (!copy)
5123 return;
5124 /* Finally copy all nodes following it. */
5125 while (*src)
5127 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5128 decl_piece_bitsize (*src), NULL_RTX);
5129 dest = &XEXP (*dest, 1);
5130 src = &XEXP (*src, 1);
5134 /* Add a variable location node to the linked list for DECL. */
5136 static struct var_loc_node *
5137 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5139 unsigned int decl_id;
5140 var_loc_list *temp;
5141 void **slot;
5142 struct var_loc_node *loc = NULL;
5143 HOST_WIDE_INT bitsize = -1, bitpos = -1;
5145 if (TREE_CODE (decl) == VAR_DECL
5146 && DECL_HAS_DEBUG_EXPR_P (decl))
5148 tree realdecl = DECL_DEBUG_EXPR (decl);
5149 if (handled_component_p (realdecl)
5150 || (TREE_CODE (realdecl) == MEM_REF
5151 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5153 HOST_WIDE_INT maxsize;
5154 tree innerdecl;
5155 innerdecl
5156 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
5157 if (!DECL_P (innerdecl)
5158 || DECL_IGNORED_P (innerdecl)
5159 || TREE_STATIC (innerdecl)
5160 || bitsize <= 0
5161 || bitpos + bitsize > 256
5162 || bitsize != maxsize)
5163 return NULL;
5164 decl = innerdecl;
5168 decl_id = DECL_UID (decl);
5169 slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
5170 if (*slot == NULL)
5172 temp = ggc_cleared_alloc<var_loc_list> ();
5173 temp->decl_id = decl_id;
5174 *slot = temp;
5176 else
5177 temp = (var_loc_list *) *slot;
5179 /* For PARM_DECLs try to keep around the original incoming value,
5180 even if that means we'll emit a zero-range .debug_loc entry. */
5181 if (temp->last
5182 && temp->first == temp->last
5183 && TREE_CODE (decl) == PARM_DECL
5184 && NOTE_P (temp->first->loc)
5185 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5186 && DECL_INCOMING_RTL (decl)
5187 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5188 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5189 == GET_CODE (DECL_INCOMING_RTL (decl))
5190 && prev_real_insn (temp->first->loc) == NULL_RTX
5191 && (bitsize != -1
5192 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5193 NOTE_VAR_LOCATION_LOC (loc_note))
5194 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5195 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5197 loc = ggc_cleared_alloc<var_loc_node> ();
5198 temp->first->next = loc;
5199 temp->last = loc;
5200 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5202 else if (temp->last)
5204 struct var_loc_node *last = temp->last, *unused = NULL;
5205 rtx *piece_loc = NULL, last_loc_note;
5206 int piece_bitpos = 0;
5207 if (last->next)
5209 last = last->next;
5210 gcc_assert (last->next == NULL);
5212 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5214 piece_loc = &last->loc;
5217 int cur_bitsize = decl_piece_bitsize (*piece_loc);
5218 if (piece_bitpos + cur_bitsize > bitpos)
5219 break;
5220 piece_bitpos += cur_bitsize;
5221 piece_loc = &XEXP (*piece_loc, 1);
5223 while (*piece_loc);
5225 /* TEMP->LAST here is either pointer to the last but one or
5226 last element in the chained list, LAST is pointer to the
5227 last element. */
5228 if (label && strcmp (last->label, label) == 0)
5230 /* For SRA optimized variables if there weren't any real
5231 insns since last note, just modify the last node. */
5232 if (piece_loc != NULL)
5234 adjust_piece_list (piece_loc, NULL, NULL,
5235 bitpos, piece_bitpos, bitsize, loc_note);
5236 return NULL;
5238 /* If the last note doesn't cover any instructions, remove it. */
5239 if (temp->last != last)
5241 temp->last->next = NULL;
5242 unused = last;
5243 last = temp->last;
5244 gcc_assert (strcmp (last->label, label) != 0);
5246 else
5248 gcc_assert (temp->first == temp->last
5249 || (temp->first->next == temp->last
5250 && TREE_CODE (decl) == PARM_DECL));
5251 memset (temp->last, '\0', sizeof (*temp->last));
5252 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5253 return temp->last;
5256 if (bitsize == -1 && NOTE_P (last->loc))
5257 last_loc_note = last->loc;
5258 else if (piece_loc != NULL
5259 && *piece_loc != NULL_RTX
5260 && piece_bitpos == bitpos
5261 && decl_piece_bitsize (*piece_loc) == bitsize)
5262 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5263 else
5264 last_loc_note = NULL_RTX;
5265 /* If the current location is the same as the end of the list,
5266 and either both or neither of the locations is uninitialized,
5267 we have nothing to do. */
5268 if (last_loc_note == NULL_RTX
5269 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5270 NOTE_VAR_LOCATION_LOC (loc_note)))
5271 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5272 != NOTE_VAR_LOCATION_STATUS (loc_note))
5273 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5274 == VAR_INIT_STATUS_UNINITIALIZED)
5275 || (NOTE_VAR_LOCATION_STATUS (loc_note)
5276 == VAR_INIT_STATUS_UNINITIALIZED))))
5278 /* Add LOC to the end of list and update LAST. If the last
5279 element of the list has been removed above, reuse its
5280 memory for the new node, otherwise allocate a new one. */
5281 if (unused)
5283 loc = unused;
5284 memset (loc, '\0', sizeof (*loc));
5286 else
5287 loc = ggc_cleared_alloc<var_loc_node> ();
5288 if (bitsize == -1 || piece_loc == NULL)
5289 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5290 else
5291 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5292 bitpos, piece_bitpos, bitsize, loc_note);
5293 last->next = loc;
5294 /* Ensure TEMP->LAST will point either to the new last but one
5295 element of the chain, or to the last element in it. */
5296 if (last != temp->last)
5297 temp->last = last;
5299 else if (unused)
5300 ggc_free (unused);
5302 else
5304 loc = ggc_cleared_alloc<var_loc_node> ();
5305 temp->first = loc;
5306 temp->last = loc;
5307 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5309 return loc;
5312 /* Keep track of the number of spaces used to indent the
5313 output of the debugging routines that print the structure of
5314 the DIE internal representation. */
5315 static int print_indent;
5317 /* Indent the line the number of spaces given by print_indent. */
5319 static inline void
5320 print_spaces (FILE *outfile)
5322 fprintf (outfile, "%*s", print_indent, "");
5325 /* Print a type signature in hex. */
5327 static inline void
5328 print_signature (FILE *outfile, char *sig)
5330 int i;
5332 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5333 fprintf (outfile, "%02x", sig[i] & 0xff);
5336 /* Print the information associated with a given DIE, and its children.
5337 This routine is a debugging aid only. */
5339 static void
5340 print_die (dw_die_ref die, FILE *outfile)
5342 dw_attr_ref a;
5343 dw_die_ref c;
5344 unsigned ix;
5346 print_spaces (outfile);
5347 fprintf (outfile, "DIE %4ld: %s (%p)\n",
5348 die->die_offset, dwarf_tag_name (die->die_tag),
5349 (void*) die);
5350 print_spaces (outfile);
5351 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5352 fprintf (outfile, " offset: %ld", die->die_offset);
5353 fprintf (outfile, " mark: %d\n", die->die_mark);
5355 if (die->comdat_type_p)
5357 print_spaces (outfile);
5358 fprintf (outfile, " signature: ");
5359 print_signature (outfile, die->die_id.die_type_node->signature);
5360 fprintf (outfile, "\n");
5363 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5365 print_spaces (outfile);
5366 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5368 switch (AT_class (a))
5370 case dw_val_class_addr:
5371 fprintf (outfile, "address");
5372 break;
5373 case dw_val_class_offset:
5374 fprintf (outfile, "offset");
5375 break;
5376 case dw_val_class_loc:
5377 fprintf (outfile, "location descriptor");
5378 break;
5379 case dw_val_class_loc_list:
5380 fprintf (outfile, "location list -> label:%s",
5381 AT_loc_list (a)->ll_symbol);
5382 break;
5383 case dw_val_class_range_list:
5384 fprintf (outfile, "range list");
5385 break;
5386 case dw_val_class_const:
5387 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
5388 break;
5389 case dw_val_class_unsigned_const:
5390 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
5391 break;
5392 case dw_val_class_const_double:
5393 fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
5394 HOST_WIDE_INT_PRINT_UNSIGNED")",
5395 a->dw_attr_val.v.val_double.high,
5396 a->dw_attr_val.v.val_double.low);
5397 break;
5398 case dw_val_class_wide_int:
5400 int i = a->dw_attr_val.v.val_wide->get_len ();
5401 fprintf (outfile, "constant (");
5402 gcc_assert (i > 0);
5403 if (a->dw_attr_val.v.val_wide->elt (i - 1) == 0)
5404 fprintf (outfile, "0x");
5405 fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
5406 a->dw_attr_val.v.val_wide->elt (--i));
5407 while (--i >= 0)
5408 fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
5409 a->dw_attr_val.v.val_wide->elt (i));
5410 fprintf (outfile, ")");
5411 break;
5413 case dw_val_class_vec:
5414 fprintf (outfile, "floating-point or vector constant");
5415 break;
5416 case dw_val_class_flag:
5417 fprintf (outfile, "%u", AT_flag (a));
5418 break;
5419 case dw_val_class_die_ref:
5420 if (AT_ref (a) != NULL)
5422 if (AT_ref (a)->comdat_type_p)
5424 fprintf (outfile, "die -> signature: ");
5425 print_signature (outfile,
5426 AT_ref (a)->die_id.die_type_node->signature);
5428 else if (AT_ref (a)->die_id.die_symbol)
5429 fprintf (outfile, "die -> label: %s",
5430 AT_ref (a)->die_id.die_symbol);
5431 else
5432 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
5433 fprintf (outfile, " (%p)", (void *) AT_ref (a));
5435 else
5436 fprintf (outfile, "die -> <null>");
5437 break;
5438 case dw_val_class_vms_delta:
5439 fprintf (outfile, "delta: @slotcount(%s-%s)",
5440 AT_vms_delta2 (a), AT_vms_delta1 (a));
5441 break;
5442 case dw_val_class_lbl_id:
5443 case dw_val_class_lineptr:
5444 case dw_val_class_macptr:
5445 case dw_val_class_high_pc:
5446 fprintf (outfile, "label: %s", AT_lbl (a));
5447 break;
5448 case dw_val_class_str:
5449 if (AT_string (a) != NULL)
5450 fprintf (outfile, "\"%s\"", AT_string (a));
5451 else
5452 fprintf (outfile, "<null>");
5453 break;
5454 case dw_val_class_file:
5455 fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
5456 AT_file (a)->emitted_number);
5457 break;
5458 case dw_val_class_data8:
5460 int i;
5462 for (i = 0; i < 8; i++)
5463 fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
5464 break;
5466 default:
5467 break;
5470 fprintf (outfile, "\n");
5473 if (die->die_child != NULL)
5475 print_indent += 4;
5476 FOR_EACH_CHILD (die, c, print_die (c, outfile));
5477 print_indent -= 4;
5479 if (print_indent == 0)
5480 fprintf (outfile, "\n");
5483 /* Print the information collected for a given DIE. */
5485 DEBUG_FUNCTION void
5486 debug_dwarf_die (dw_die_ref die)
5488 print_die (die, stderr);
5491 DEBUG_FUNCTION void
5492 debug (die_struct &ref)
5494 print_die (&ref, stderr);
5497 DEBUG_FUNCTION void
5498 debug (die_struct *ptr)
5500 if (ptr)
5501 debug (*ptr);
5502 else
5503 fprintf (stderr, "<nil>\n");
5507 /* Print all DWARF information collected for the compilation unit.
5508 This routine is a debugging aid only. */
5510 DEBUG_FUNCTION void
5511 debug_dwarf (void)
5513 print_indent = 0;
5514 print_die (comp_unit_die (), stderr);
5517 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
5518 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
5519 DIE that marks the start of the DIEs for this include file. */
5521 static dw_die_ref
5522 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5524 const char *filename = get_AT_string (bincl_die, DW_AT_name);
5525 dw_die_ref new_unit = gen_compile_unit_die (filename);
5527 new_unit->die_sib = old_unit;
5528 return new_unit;
5531 /* Close an include-file CU and reopen the enclosing one. */
5533 static dw_die_ref
5534 pop_compile_unit (dw_die_ref old_unit)
5536 dw_die_ref new_unit = old_unit->die_sib;
5538 old_unit->die_sib = NULL;
5539 return new_unit;
5542 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5543 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5544 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5546 /* Calculate the checksum of a location expression. */
5548 static inline void
5549 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5551 int tem;
5552 inchash::hash hstate;
5553 hashval_t hash;
5555 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
5556 CHECKSUM (tem);
5557 hash_loc_operands (loc, hstate);
5558 hash = hstate.end();
5559 CHECKSUM (hash);
5562 /* Calculate the checksum of an attribute. */
5564 static void
5565 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5567 dw_loc_descr_ref loc;
5568 rtx r;
5570 CHECKSUM (at->dw_attr);
5572 /* We don't care that this was compiled with a different compiler
5573 snapshot; if the output is the same, that's what matters. */
5574 if (at->dw_attr == DW_AT_producer)
5575 return;
5577 switch (AT_class (at))
5579 case dw_val_class_const:
5580 CHECKSUM (at->dw_attr_val.v.val_int);
5581 break;
5582 case dw_val_class_unsigned_const:
5583 CHECKSUM (at->dw_attr_val.v.val_unsigned);
5584 break;
5585 case dw_val_class_const_double:
5586 CHECKSUM (at->dw_attr_val.v.val_double);
5587 break;
5588 case dw_val_class_wide_int:
5589 CHECKSUM (*at->dw_attr_val.v.val_wide);
5590 break;
5591 case dw_val_class_vec:
5592 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5593 (at->dw_attr_val.v.val_vec.length
5594 * at->dw_attr_val.v.val_vec.elt_size));
5595 break;
5596 case dw_val_class_flag:
5597 CHECKSUM (at->dw_attr_val.v.val_flag);
5598 break;
5599 case dw_val_class_str:
5600 CHECKSUM_STRING (AT_string (at));
5601 break;
5603 case dw_val_class_addr:
5604 r = AT_addr (at);
5605 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5606 CHECKSUM_STRING (XSTR (r, 0));
5607 break;
5609 case dw_val_class_offset:
5610 CHECKSUM (at->dw_attr_val.v.val_offset);
5611 break;
5613 case dw_val_class_loc:
5614 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5615 loc_checksum (loc, ctx);
5616 break;
5618 case dw_val_class_die_ref:
5619 die_checksum (AT_ref (at), ctx, mark);
5620 break;
5622 case dw_val_class_fde_ref:
5623 case dw_val_class_vms_delta:
5624 case dw_val_class_lbl_id:
5625 case dw_val_class_lineptr:
5626 case dw_val_class_macptr:
5627 case dw_val_class_high_pc:
5628 break;
5630 case dw_val_class_file:
5631 CHECKSUM_STRING (AT_file (at)->filename);
5632 break;
5634 case dw_val_class_data8:
5635 CHECKSUM (at->dw_attr_val.v.val_data8);
5636 break;
5638 default:
5639 break;
5643 /* Calculate the checksum of a DIE. */
5645 static void
5646 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5648 dw_die_ref c;
5649 dw_attr_ref a;
5650 unsigned ix;
5652 /* To avoid infinite recursion. */
5653 if (die->die_mark)
5655 CHECKSUM (die->die_mark);
5656 return;
5658 die->die_mark = ++(*mark);
5660 CHECKSUM (die->die_tag);
5662 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5663 attr_checksum (a, ctx, mark);
5665 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
5668 #undef CHECKSUM
5669 #undef CHECKSUM_BLOCK
5670 #undef CHECKSUM_STRING
5672 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
5673 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5674 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5675 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
5676 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
5677 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
5678 #define CHECKSUM_ATTR(FOO) \
5679 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
5681 /* Calculate the checksum of a number in signed LEB128 format. */
5683 static void
5684 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
5686 unsigned char byte;
5687 bool more;
5689 while (1)
5691 byte = (value & 0x7f);
5692 value >>= 7;
5693 more = !((value == 0 && (byte & 0x40) == 0)
5694 || (value == -1 && (byte & 0x40) != 0));
5695 if (more)
5696 byte |= 0x80;
5697 CHECKSUM (byte);
5698 if (!more)
5699 break;
5703 /* Calculate the checksum of a number in unsigned LEB128 format. */
5705 static void
5706 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
5708 while (1)
5710 unsigned char byte = (value & 0x7f);
5711 value >>= 7;
5712 if (value != 0)
5713 /* More bytes to follow. */
5714 byte |= 0x80;
5715 CHECKSUM (byte);
5716 if (value == 0)
5717 break;
5721 /* Checksum the context of the DIE. This adds the names of any
5722 surrounding namespaces or structures to the checksum. */
5724 static void
5725 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
5727 const char *name;
5728 dw_die_ref spec;
5729 int tag = die->die_tag;
5731 if (tag != DW_TAG_namespace
5732 && tag != DW_TAG_structure_type
5733 && tag != DW_TAG_class_type)
5734 return;
5736 name = get_AT_string (die, DW_AT_name);
5738 spec = get_AT_ref (die, DW_AT_specification);
5739 if (spec != NULL)
5740 die = spec;
5742 if (die->die_parent != NULL)
5743 checksum_die_context (die->die_parent, ctx);
5745 CHECKSUM_ULEB128 ('C');
5746 CHECKSUM_ULEB128 (tag);
5747 if (name != NULL)
5748 CHECKSUM_STRING (name);
5751 /* Calculate the checksum of a location expression. */
5753 static inline void
5754 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5756 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
5757 were emitted as a DW_FORM_sdata instead of a location expression. */
5758 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
5760 CHECKSUM_ULEB128 (DW_FORM_sdata);
5761 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
5762 return;
5765 /* Otherwise, just checksum the raw location expression. */
5766 while (loc != NULL)
5768 inchash::hash hstate;
5769 hashval_t hash;
5771 CHECKSUM_ULEB128 (loc->dtprel);
5772 CHECKSUM_ULEB128 (loc->dw_loc_opc);
5773 hash_loc_operands (loc, hstate);
5774 hash = hstate.end ();
5775 CHECKSUM (hash);
5776 loc = loc->dw_loc_next;
5780 /* Calculate the checksum of an attribute. */
5782 static void
5783 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
5784 struct md5_ctx *ctx, int *mark)
5786 dw_loc_descr_ref loc;
5787 rtx r;
5789 if (AT_class (at) == dw_val_class_die_ref)
5791 dw_die_ref target_die = AT_ref (at);
5793 /* For pointer and reference types, we checksum only the (qualified)
5794 name of the target type (if there is a name). For friend entries,
5795 we checksum only the (qualified) name of the target type or function.
5796 This allows the checksum to remain the same whether the target type
5797 is complete or not. */
5798 if ((at->dw_attr == DW_AT_type
5799 && (tag == DW_TAG_pointer_type
5800 || tag == DW_TAG_reference_type
5801 || tag == DW_TAG_rvalue_reference_type
5802 || tag == DW_TAG_ptr_to_member_type))
5803 || (at->dw_attr == DW_AT_friend
5804 && tag == DW_TAG_friend))
5806 dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
5808 if (name_attr != NULL)
5810 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5812 if (decl == NULL)
5813 decl = target_die;
5814 CHECKSUM_ULEB128 ('N');
5815 CHECKSUM_ULEB128 (at->dw_attr);
5816 if (decl->die_parent != NULL)
5817 checksum_die_context (decl->die_parent, ctx);
5818 CHECKSUM_ULEB128 ('E');
5819 CHECKSUM_STRING (AT_string (name_attr));
5820 return;
5824 /* For all other references to another DIE, we check to see if the
5825 target DIE has already been visited. If it has, we emit a
5826 backward reference; if not, we descend recursively. */
5827 if (target_die->die_mark > 0)
5829 CHECKSUM_ULEB128 ('R');
5830 CHECKSUM_ULEB128 (at->dw_attr);
5831 CHECKSUM_ULEB128 (target_die->die_mark);
5833 else
5835 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5837 if (decl == NULL)
5838 decl = target_die;
5839 target_die->die_mark = ++(*mark);
5840 CHECKSUM_ULEB128 ('T');
5841 CHECKSUM_ULEB128 (at->dw_attr);
5842 if (decl->die_parent != NULL)
5843 checksum_die_context (decl->die_parent, ctx);
5844 die_checksum_ordered (target_die, ctx, mark);
5846 return;
5849 CHECKSUM_ULEB128 ('A');
5850 CHECKSUM_ULEB128 (at->dw_attr);
5852 switch (AT_class (at))
5854 case dw_val_class_const:
5855 CHECKSUM_ULEB128 (DW_FORM_sdata);
5856 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
5857 break;
5859 case dw_val_class_unsigned_const:
5860 CHECKSUM_ULEB128 (DW_FORM_sdata);
5861 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
5862 break;
5864 case dw_val_class_const_double:
5865 CHECKSUM_ULEB128 (DW_FORM_block);
5866 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
5867 CHECKSUM (at->dw_attr_val.v.val_double);
5868 break;
5870 case dw_val_class_wide_int:
5871 CHECKSUM_ULEB128 (DW_FORM_block);
5872 CHECKSUM_ULEB128 (sizeof (*at->dw_attr_val.v.val_wide));
5873 CHECKSUM (*at->dw_attr_val.v.val_wide);
5874 break;
5876 case dw_val_class_vec:
5877 CHECKSUM_ULEB128 (DW_FORM_block);
5878 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
5879 * at->dw_attr_val.v.val_vec.elt_size);
5880 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5881 (at->dw_attr_val.v.val_vec.length
5882 * at->dw_attr_val.v.val_vec.elt_size));
5883 break;
5885 case dw_val_class_flag:
5886 CHECKSUM_ULEB128 (DW_FORM_flag);
5887 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
5888 break;
5890 case dw_val_class_str:
5891 CHECKSUM_ULEB128 (DW_FORM_string);
5892 CHECKSUM_STRING (AT_string (at));
5893 break;
5895 case dw_val_class_addr:
5896 r = AT_addr (at);
5897 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5898 CHECKSUM_ULEB128 (DW_FORM_string);
5899 CHECKSUM_STRING (XSTR (r, 0));
5900 break;
5902 case dw_val_class_offset:
5903 CHECKSUM_ULEB128 (DW_FORM_sdata);
5904 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
5905 break;
5907 case dw_val_class_loc:
5908 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5909 loc_checksum_ordered (loc, ctx);
5910 break;
5912 case dw_val_class_fde_ref:
5913 case dw_val_class_lbl_id:
5914 case dw_val_class_lineptr:
5915 case dw_val_class_macptr:
5916 case dw_val_class_high_pc:
5917 break;
5919 case dw_val_class_file:
5920 CHECKSUM_ULEB128 (DW_FORM_string);
5921 CHECKSUM_STRING (AT_file (at)->filename);
5922 break;
5924 case dw_val_class_data8:
5925 CHECKSUM (at->dw_attr_val.v.val_data8);
5926 break;
5928 default:
5929 break;
5933 struct checksum_attributes
5935 dw_attr_ref at_name;
5936 dw_attr_ref at_type;
5937 dw_attr_ref at_friend;
5938 dw_attr_ref at_accessibility;
5939 dw_attr_ref at_address_class;
5940 dw_attr_ref at_allocated;
5941 dw_attr_ref at_artificial;
5942 dw_attr_ref at_associated;
5943 dw_attr_ref at_binary_scale;
5944 dw_attr_ref at_bit_offset;
5945 dw_attr_ref at_bit_size;
5946 dw_attr_ref at_bit_stride;
5947 dw_attr_ref at_byte_size;
5948 dw_attr_ref at_byte_stride;
5949 dw_attr_ref at_const_value;
5950 dw_attr_ref at_containing_type;
5951 dw_attr_ref at_count;
5952 dw_attr_ref at_data_location;
5953 dw_attr_ref at_data_member_location;
5954 dw_attr_ref at_decimal_scale;
5955 dw_attr_ref at_decimal_sign;
5956 dw_attr_ref at_default_value;
5957 dw_attr_ref at_digit_count;
5958 dw_attr_ref at_discr;
5959 dw_attr_ref at_discr_list;
5960 dw_attr_ref at_discr_value;
5961 dw_attr_ref at_encoding;
5962 dw_attr_ref at_endianity;
5963 dw_attr_ref at_explicit;
5964 dw_attr_ref at_is_optional;
5965 dw_attr_ref at_location;
5966 dw_attr_ref at_lower_bound;
5967 dw_attr_ref at_mutable;
5968 dw_attr_ref at_ordering;
5969 dw_attr_ref at_picture_string;
5970 dw_attr_ref at_prototyped;
5971 dw_attr_ref at_small;
5972 dw_attr_ref at_segment;
5973 dw_attr_ref at_string_length;
5974 dw_attr_ref at_threads_scaled;
5975 dw_attr_ref at_upper_bound;
5976 dw_attr_ref at_use_location;
5977 dw_attr_ref at_use_UTF8;
5978 dw_attr_ref at_variable_parameter;
5979 dw_attr_ref at_virtuality;
5980 dw_attr_ref at_visibility;
5981 dw_attr_ref at_vtable_elem_location;
5984 /* Collect the attributes that we will want to use for the checksum. */
5986 static void
5987 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
5989 dw_attr_ref a;
5990 unsigned ix;
5992 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5994 switch (a->dw_attr)
5996 case DW_AT_name:
5997 attrs->at_name = a;
5998 break;
5999 case DW_AT_type:
6000 attrs->at_type = a;
6001 break;
6002 case DW_AT_friend:
6003 attrs->at_friend = a;
6004 break;
6005 case DW_AT_accessibility:
6006 attrs->at_accessibility = a;
6007 break;
6008 case DW_AT_address_class:
6009 attrs->at_address_class = a;
6010 break;
6011 case DW_AT_allocated:
6012 attrs->at_allocated = a;
6013 break;
6014 case DW_AT_artificial:
6015 attrs->at_artificial = a;
6016 break;
6017 case DW_AT_associated:
6018 attrs->at_associated = a;
6019 break;
6020 case DW_AT_binary_scale:
6021 attrs->at_binary_scale = a;
6022 break;
6023 case DW_AT_bit_offset:
6024 attrs->at_bit_offset = a;
6025 break;
6026 case DW_AT_bit_size:
6027 attrs->at_bit_size = a;
6028 break;
6029 case DW_AT_bit_stride:
6030 attrs->at_bit_stride = a;
6031 break;
6032 case DW_AT_byte_size:
6033 attrs->at_byte_size = a;
6034 break;
6035 case DW_AT_byte_stride:
6036 attrs->at_byte_stride = a;
6037 break;
6038 case DW_AT_const_value:
6039 attrs->at_const_value = a;
6040 break;
6041 case DW_AT_containing_type:
6042 attrs->at_containing_type = a;
6043 break;
6044 case DW_AT_count:
6045 attrs->at_count = a;
6046 break;
6047 case DW_AT_data_location:
6048 attrs->at_data_location = a;
6049 break;
6050 case DW_AT_data_member_location:
6051 attrs->at_data_member_location = a;
6052 break;
6053 case DW_AT_decimal_scale:
6054 attrs->at_decimal_scale = a;
6055 break;
6056 case DW_AT_decimal_sign:
6057 attrs->at_decimal_sign = a;
6058 break;
6059 case DW_AT_default_value:
6060 attrs->at_default_value = a;
6061 break;
6062 case DW_AT_digit_count:
6063 attrs->at_digit_count = a;
6064 break;
6065 case DW_AT_discr:
6066 attrs->at_discr = a;
6067 break;
6068 case DW_AT_discr_list:
6069 attrs->at_discr_list = a;
6070 break;
6071 case DW_AT_discr_value:
6072 attrs->at_discr_value = a;
6073 break;
6074 case DW_AT_encoding:
6075 attrs->at_encoding = a;
6076 break;
6077 case DW_AT_endianity:
6078 attrs->at_endianity = a;
6079 break;
6080 case DW_AT_explicit:
6081 attrs->at_explicit = a;
6082 break;
6083 case DW_AT_is_optional:
6084 attrs->at_is_optional = a;
6085 break;
6086 case DW_AT_location:
6087 attrs->at_location = a;
6088 break;
6089 case DW_AT_lower_bound:
6090 attrs->at_lower_bound = a;
6091 break;
6092 case DW_AT_mutable:
6093 attrs->at_mutable = a;
6094 break;
6095 case DW_AT_ordering:
6096 attrs->at_ordering = a;
6097 break;
6098 case DW_AT_picture_string:
6099 attrs->at_picture_string = a;
6100 break;
6101 case DW_AT_prototyped:
6102 attrs->at_prototyped = a;
6103 break;
6104 case DW_AT_small:
6105 attrs->at_small = a;
6106 break;
6107 case DW_AT_segment:
6108 attrs->at_segment = a;
6109 break;
6110 case DW_AT_string_length:
6111 attrs->at_string_length = a;
6112 break;
6113 case DW_AT_threads_scaled:
6114 attrs->at_threads_scaled = a;
6115 break;
6116 case DW_AT_upper_bound:
6117 attrs->at_upper_bound = a;
6118 break;
6119 case DW_AT_use_location:
6120 attrs->at_use_location = a;
6121 break;
6122 case DW_AT_use_UTF8:
6123 attrs->at_use_UTF8 = a;
6124 break;
6125 case DW_AT_variable_parameter:
6126 attrs->at_variable_parameter = a;
6127 break;
6128 case DW_AT_virtuality:
6129 attrs->at_virtuality = a;
6130 break;
6131 case DW_AT_visibility:
6132 attrs->at_visibility = a;
6133 break;
6134 case DW_AT_vtable_elem_location:
6135 attrs->at_vtable_elem_location = a;
6136 break;
6137 default:
6138 break;
6143 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
6145 static void
6146 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6148 dw_die_ref c;
6149 dw_die_ref decl;
6150 struct checksum_attributes attrs;
6152 CHECKSUM_ULEB128 ('D');
6153 CHECKSUM_ULEB128 (die->die_tag);
6155 memset (&attrs, 0, sizeof (attrs));
6157 decl = get_AT_ref (die, DW_AT_specification);
6158 if (decl != NULL)
6159 collect_checksum_attributes (&attrs, decl);
6160 collect_checksum_attributes (&attrs, die);
6162 CHECKSUM_ATTR (attrs.at_name);
6163 CHECKSUM_ATTR (attrs.at_accessibility);
6164 CHECKSUM_ATTR (attrs.at_address_class);
6165 CHECKSUM_ATTR (attrs.at_allocated);
6166 CHECKSUM_ATTR (attrs.at_artificial);
6167 CHECKSUM_ATTR (attrs.at_associated);
6168 CHECKSUM_ATTR (attrs.at_binary_scale);
6169 CHECKSUM_ATTR (attrs.at_bit_offset);
6170 CHECKSUM_ATTR (attrs.at_bit_size);
6171 CHECKSUM_ATTR (attrs.at_bit_stride);
6172 CHECKSUM_ATTR (attrs.at_byte_size);
6173 CHECKSUM_ATTR (attrs.at_byte_stride);
6174 CHECKSUM_ATTR (attrs.at_const_value);
6175 CHECKSUM_ATTR (attrs.at_containing_type);
6176 CHECKSUM_ATTR (attrs.at_count);
6177 CHECKSUM_ATTR (attrs.at_data_location);
6178 CHECKSUM_ATTR (attrs.at_data_member_location);
6179 CHECKSUM_ATTR (attrs.at_decimal_scale);
6180 CHECKSUM_ATTR (attrs.at_decimal_sign);
6181 CHECKSUM_ATTR (attrs.at_default_value);
6182 CHECKSUM_ATTR (attrs.at_digit_count);
6183 CHECKSUM_ATTR (attrs.at_discr);
6184 CHECKSUM_ATTR (attrs.at_discr_list);
6185 CHECKSUM_ATTR (attrs.at_discr_value);
6186 CHECKSUM_ATTR (attrs.at_encoding);
6187 CHECKSUM_ATTR (attrs.at_endianity);
6188 CHECKSUM_ATTR (attrs.at_explicit);
6189 CHECKSUM_ATTR (attrs.at_is_optional);
6190 CHECKSUM_ATTR (attrs.at_location);
6191 CHECKSUM_ATTR (attrs.at_lower_bound);
6192 CHECKSUM_ATTR (attrs.at_mutable);
6193 CHECKSUM_ATTR (attrs.at_ordering);
6194 CHECKSUM_ATTR (attrs.at_picture_string);
6195 CHECKSUM_ATTR (attrs.at_prototyped);
6196 CHECKSUM_ATTR (attrs.at_small);
6197 CHECKSUM_ATTR (attrs.at_segment);
6198 CHECKSUM_ATTR (attrs.at_string_length);
6199 CHECKSUM_ATTR (attrs.at_threads_scaled);
6200 CHECKSUM_ATTR (attrs.at_upper_bound);
6201 CHECKSUM_ATTR (attrs.at_use_location);
6202 CHECKSUM_ATTR (attrs.at_use_UTF8);
6203 CHECKSUM_ATTR (attrs.at_variable_parameter);
6204 CHECKSUM_ATTR (attrs.at_virtuality);
6205 CHECKSUM_ATTR (attrs.at_visibility);
6206 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6207 CHECKSUM_ATTR (attrs.at_type);
6208 CHECKSUM_ATTR (attrs.at_friend);
6210 /* Checksum the child DIEs. */
6211 c = die->die_child;
6212 if (c) do {
6213 dw_attr_ref name_attr;
6215 c = c->die_sib;
6216 name_attr = get_AT (c, DW_AT_name);
6217 if (is_template_instantiation (c))
6219 /* Ignore instantiations of member type and function templates. */
6221 else if (name_attr != NULL
6222 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
6224 /* Use a shallow checksum for named nested types and member
6225 functions. */
6226 CHECKSUM_ULEB128 ('S');
6227 CHECKSUM_ULEB128 (c->die_tag);
6228 CHECKSUM_STRING (AT_string (name_attr));
6230 else
6232 /* Use a deep checksum for other children. */
6233 /* Mark this DIE so it gets processed when unmarking. */
6234 if (c->die_mark == 0)
6235 c->die_mark = -1;
6236 die_checksum_ordered (c, ctx, mark);
6238 } while (c != die->die_child);
6240 CHECKSUM_ULEB128 (0);
6243 /* Add a type name and tag to a hash. */
6244 static void
6245 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
6247 CHECKSUM_ULEB128 (tag);
6248 CHECKSUM_STRING (name);
6251 #undef CHECKSUM
6252 #undef CHECKSUM_STRING
6253 #undef CHECKSUM_ATTR
6254 #undef CHECKSUM_LEB128
6255 #undef CHECKSUM_ULEB128
6257 /* Generate the type signature for DIE. This is computed by generating an
6258 MD5 checksum over the DIE's tag, its relevant attributes, and its
6259 children. Attributes that are references to other DIEs are processed
6260 by recursion, using the MARK field to prevent infinite recursion.
6261 If the DIE is nested inside a namespace or another type, we also
6262 need to include that context in the signature. The lower 64 bits
6263 of the resulting MD5 checksum comprise the signature. */
6265 static void
6266 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6268 int mark;
6269 const char *name;
6270 unsigned char checksum[16];
6271 struct md5_ctx ctx;
6272 dw_die_ref decl;
6273 dw_die_ref parent;
6275 name = get_AT_string (die, DW_AT_name);
6276 decl = get_AT_ref (die, DW_AT_specification);
6277 parent = get_die_parent (die);
6279 /* First, compute a signature for just the type name (and its surrounding
6280 context, if any. This is stored in the type unit DIE for link-time
6281 ODR (one-definition rule) checking. */
6283 if (is_cxx () && name != NULL)
6285 md5_init_ctx (&ctx);
6287 /* Checksum the names of surrounding namespaces and structures. */
6288 if (parent != NULL)
6289 checksum_die_context (parent, &ctx);
6291 /* Checksum the current DIE. */
6292 die_odr_checksum (die->die_tag, name, &ctx);
6293 md5_finish_ctx (&ctx, checksum);
6295 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6298 /* Next, compute the complete type signature. */
6300 md5_init_ctx (&ctx);
6301 mark = 1;
6302 die->die_mark = mark;
6304 /* Checksum the names of surrounding namespaces and structures. */
6305 if (parent != NULL)
6306 checksum_die_context (parent, &ctx);
6308 /* Checksum the DIE and its children. */
6309 die_checksum_ordered (die, &ctx, &mark);
6310 unmark_all_dies (die);
6311 md5_finish_ctx (&ctx, checksum);
6313 /* Store the signature in the type node and link the type DIE and the
6314 type node together. */
6315 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6316 DWARF_TYPE_SIGNATURE_SIZE);
6317 die->comdat_type_p = true;
6318 die->die_id.die_type_node = type_node;
6319 type_node->type_die = die;
6321 /* If the DIE is a specification, link its declaration to the type node
6322 as well. */
6323 if (decl != NULL)
6325 decl->comdat_type_p = true;
6326 decl->die_id.die_type_node = type_node;
6330 /* Do the location expressions look same? */
6331 static inline int
6332 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6334 return loc1->dw_loc_opc == loc2->dw_loc_opc
6335 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6336 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6339 /* Do the values look the same? */
6340 static int
6341 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6343 dw_loc_descr_ref loc1, loc2;
6344 rtx r1, r2;
6346 if (v1->val_class != v2->val_class)
6347 return 0;
6349 switch (v1->val_class)
6351 case dw_val_class_const:
6352 return v1->v.val_int == v2->v.val_int;
6353 case dw_val_class_unsigned_const:
6354 return v1->v.val_unsigned == v2->v.val_unsigned;
6355 case dw_val_class_const_double:
6356 return v1->v.val_double.high == v2->v.val_double.high
6357 && v1->v.val_double.low == v2->v.val_double.low;
6358 case dw_val_class_wide_int:
6359 return *v1->v.val_wide == *v2->v.val_wide;
6360 case dw_val_class_vec:
6361 if (v1->v.val_vec.length != v2->v.val_vec.length
6362 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6363 return 0;
6364 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6365 v1->v.val_vec.length * v1->v.val_vec.elt_size))
6366 return 0;
6367 return 1;
6368 case dw_val_class_flag:
6369 return v1->v.val_flag == v2->v.val_flag;
6370 case dw_val_class_str:
6371 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
6373 case dw_val_class_addr:
6374 r1 = v1->v.val_addr;
6375 r2 = v2->v.val_addr;
6376 if (GET_CODE (r1) != GET_CODE (r2))
6377 return 0;
6378 return !rtx_equal_p (r1, r2);
6380 case dw_val_class_offset:
6381 return v1->v.val_offset == v2->v.val_offset;
6383 case dw_val_class_loc:
6384 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6385 loc1 && loc2;
6386 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6387 if (!same_loc_p (loc1, loc2, mark))
6388 return 0;
6389 return !loc1 && !loc2;
6391 case dw_val_class_die_ref:
6392 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6394 case dw_val_class_fde_ref:
6395 case dw_val_class_vms_delta:
6396 case dw_val_class_lbl_id:
6397 case dw_val_class_lineptr:
6398 case dw_val_class_macptr:
6399 case dw_val_class_high_pc:
6400 return 1;
6402 case dw_val_class_file:
6403 return v1->v.val_file == v2->v.val_file;
6405 case dw_val_class_data8:
6406 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
6408 default:
6409 return 1;
6413 /* Do the attributes look the same? */
6415 static int
6416 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6418 if (at1->dw_attr != at2->dw_attr)
6419 return 0;
6421 /* We don't care that this was compiled with a different compiler
6422 snapshot; if the output is the same, that's what matters. */
6423 if (at1->dw_attr == DW_AT_producer)
6424 return 1;
6426 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6429 /* Do the dies look the same? */
6431 static int
6432 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6434 dw_die_ref c1, c2;
6435 dw_attr_ref a1;
6436 unsigned ix;
6438 /* To avoid infinite recursion. */
6439 if (die1->die_mark)
6440 return die1->die_mark == die2->die_mark;
6441 die1->die_mark = die2->die_mark = ++(*mark);
6443 if (die1->die_tag != die2->die_tag)
6444 return 0;
6446 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
6447 return 0;
6449 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
6450 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
6451 return 0;
6453 c1 = die1->die_child;
6454 c2 = die2->die_child;
6455 if (! c1)
6457 if (c2)
6458 return 0;
6460 else
6461 for (;;)
6463 if (!same_die_p (c1, c2, mark))
6464 return 0;
6465 c1 = c1->die_sib;
6466 c2 = c2->die_sib;
6467 if (c1 == die1->die_child)
6469 if (c2 == die2->die_child)
6470 break;
6471 else
6472 return 0;
6476 return 1;
6479 /* Do the dies look the same? Wrapper around same_die_p. */
6481 static int
6482 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6484 int mark = 0;
6485 int ret = same_die_p (die1, die2, &mark);
6487 unmark_all_dies (die1);
6488 unmark_all_dies (die2);
6490 return ret;
6493 /* The prefix to attach to symbols on DIEs in the current comdat debug
6494 info section. */
6495 static const char *comdat_symbol_id;
6497 /* The index of the current symbol within the current comdat CU. */
6498 static unsigned int comdat_symbol_number;
6500 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6501 children, and set comdat_symbol_id accordingly. */
6503 static void
6504 compute_section_prefix (dw_die_ref unit_die)
6506 const char *die_name = get_AT_string (unit_die, DW_AT_name);
6507 const char *base = die_name ? lbasename (die_name) : "anonymous";
6508 char *name = XALLOCAVEC (char, strlen (base) + 64);
6509 char *p;
6510 int i, mark;
6511 unsigned char checksum[16];
6512 struct md5_ctx ctx;
6514 /* Compute the checksum of the DIE, then append part of it as hex digits to
6515 the name filename of the unit. */
6517 md5_init_ctx (&ctx);
6518 mark = 0;
6519 die_checksum (unit_die, &ctx, &mark);
6520 unmark_all_dies (unit_die);
6521 md5_finish_ctx (&ctx, checksum);
6523 sprintf (name, "%s.", base);
6524 clean_symbol_name (name);
6526 p = name + strlen (name);
6527 for (i = 0; i < 4; i++)
6529 sprintf (p, "%.2x", checksum[i]);
6530 p += 2;
6533 comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
6534 comdat_symbol_number = 0;
6537 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
6539 static int
6540 is_type_die (dw_die_ref die)
6542 switch (die->die_tag)
6544 case DW_TAG_array_type:
6545 case DW_TAG_class_type:
6546 case DW_TAG_interface_type:
6547 case DW_TAG_enumeration_type:
6548 case DW_TAG_pointer_type:
6549 case DW_TAG_reference_type:
6550 case DW_TAG_rvalue_reference_type:
6551 case DW_TAG_string_type:
6552 case DW_TAG_structure_type:
6553 case DW_TAG_subroutine_type:
6554 case DW_TAG_union_type:
6555 case DW_TAG_ptr_to_member_type:
6556 case DW_TAG_set_type:
6557 case DW_TAG_subrange_type:
6558 case DW_TAG_base_type:
6559 case DW_TAG_const_type:
6560 case DW_TAG_file_type:
6561 case DW_TAG_packed_type:
6562 case DW_TAG_volatile_type:
6563 case DW_TAG_typedef:
6564 return 1;
6565 default:
6566 return 0;
6570 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6571 Basically, we want to choose the bits that are likely to be shared between
6572 compilations (types) and leave out the bits that are specific to individual
6573 compilations (functions). */
6575 static int
6576 is_comdat_die (dw_die_ref c)
6578 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6579 we do for stabs. The advantage is a greater likelihood of sharing between
6580 objects that don't include headers in the same order (and therefore would
6581 put the base types in a different comdat). jason 8/28/00 */
6583 if (c->die_tag == DW_TAG_base_type)
6584 return 0;
6586 if (c->die_tag == DW_TAG_pointer_type
6587 || c->die_tag == DW_TAG_reference_type
6588 || c->die_tag == DW_TAG_rvalue_reference_type
6589 || c->die_tag == DW_TAG_const_type
6590 || c->die_tag == DW_TAG_volatile_type)
6592 dw_die_ref t = get_AT_ref (c, DW_AT_type);
6594 return t ? is_comdat_die (t) : 0;
6597 return is_type_die (c);
6600 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6601 compilation unit. */
6603 static int
6604 is_symbol_die (dw_die_ref c)
6606 return (is_type_die (c)
6607 || is_declaration_die (c)
6608 || c->die_tag == DW_TAG_namespace
6609 || c->die_tag == DW_TAG_module);
6612 /* Returns true iff C is a compile-unit DIE. */
6614 static inline bool
6615 is_cu_die (dw_die_ref c)
6617 return c && c->die_tag == DW_TAG_compile_unit;
6620 /* Returns true iff C is a unit DIE of some sort. */
6622 static inline bool
6623 is_unit_die (dw_die_ref c)
6625 return c && (c->die_tag == DW_TAG_compile_unit
6626 || c->die_tag == DW_TAG_partial_unit
6627 || c->die_tag == DW_TAG_type_unit);
6630 /* Returns true iff C is a namespace DIE. */
6632 static inline bool
6633 is_namespace_die (dw_die_ref c)
6635 return c && c->die_tag == DW_TAG_namespace;
6638 /* Returns true iff C is a class or structure DIE. */
6640 static inline bool
6641 is_class_die (dw_die_ref c)
6643 return c && (c->die_tag == DW_TAG_class_type
6644 || c->die_tag == DW_TAG_structure_type);
6647 /* Return non-zero if this DIE is a template parameter. */
6649 static inline bool
6650 is_template_parameter (dw_die_ref die)
6652 switch (die->die_tag)
6654 case DW_TAG_template_type_param:
6655 case DW_TAG_template_value_param:
6656 case DW_TAG_GNU_template_template_param:
6657 case DW_TAG_GNU_template_parameter_pack:
6658 return true;
6659 default:
6660 return false;
6664 /* Return non-zero if this DIE represents a template instantiation. */
6666 static inline bool
6667 is_template_instantiation (dw_die_ref die)
6669 dw_die_ref c;
6671 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
6672 return false;
6673 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
6674 return false;
6677 static char *
6678 gen_internal_sym (const char *prefix)
6680 char buf[256];
6682 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6683 return xstrdup (buf);
6686 /* Assign symbols to all worthy DIEs under DIE. */
6688 static void
6689 assign_symbol_names (dw_die_ref die)
6691 dw_die_ref c;
6693 if (is_symbol_die (die) && !die->comdat_type_p)
6695 if (comdat_symbol_id)
6697 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
6699 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6700 comdat_symbol_id, comdat_symbol_number++);
6701 die->die_id.die_symbol = xstrdup (p);
6703 else
6704 die->die_id.die_symbol = gen_internal_sym ("LDIE");
6707 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6710 struct cu_hash_table_entry
6712 dw_die_ref cu;
6713 unsigned min_comdat_num, max_comdat_num;
6714 struct cu_hash_table_entry *next;
6717 /* Helpers to manipulate hash table of CUs. */
6719 struct cu_hash_table_entry_hasher
6721 typedef cu_hash_table_entry value_type;
6722 typedef die_struct compare_type;
6723 static inline hashval_t hash (const value_type *);
6724 static inline bool equal (const value_type *, const compare_type *);
6725 static inline void remove (value_type *);
6728 inline hashval_t
6729 cu_hash_table_entry_hasher::hash (const value_type *entry)
6731 return htab_hash_string (entry->cu->die_id.die_symbol);
6734 inline bool
6735 cu_hash_table_entry_hasher::equal (const value_type *entry1,
6736 const compare_type *entry2)
6738 return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
6741 inline void
6742 cu_hash_table_entry_hasher::remove (value_type *entry)
6744 struct cu_hash_table_entry *next;
6746 while (entry)
6748 next = entry->next;
6749 free (entry);
6750 entry = next;
6754 typedef hash_table<cu_hash_table_entry_hasher> cu_hash_type;
6756 /* Check whether we have already seen this CU and set up SYM_NUM
6757 accordingly. */
6758 static int
6759 check_duplicate_cu (dw_die_ref cu, cu_hash_type *htable, unsigned int *sym_num)
6761 struct cu_hash_table_entry dummy;
6762 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6764 dummy.max_comdat_num = 0;
6766 slot = htable->find_slot_with_hash (cu,
6767 htab_hash_string (cu->die_id.die_symbol),
6768 INSERT);
6769 entry = *slot;
6771 for (; entry; last = entry, entry = entry->next)
6773 if (same_die_p_wrap (cu, entry->cu))
6774 break;
6777 if (entry)
6779 *sym_num = entry->min_comdat_num;
6780 return 1;
6783 entry = XCNEW (struct cu_hash_table_entry);
6784 entry->cu = cu;
6785 entry->min_comdat_num = *sym_num = last->max_comdat_num;
6786 entry->next = *slot;
6787 *slot = entry;
6789 return 0;
6792 /* Record SYM_NUM to record of CU in HTABLE. */
6793 static void
6794 record_comdat_symbol_number (dw_die_ref cu, cu_hash_type *htable,
6795 unsigned int sym_num)
6797 struct cu_hash_table_entry **slot, *entry;
6799 slot = htable->find_slot_with_hash (cu,
6800 htab_hash_string (cu->die_id.die_symbol),
6801 NO_INSERT);
6802 entry = *slot;
6804 entry->max_comdat_num = sym_num;
6807 /* Traverse the DIE (which is always comp_unit_die), and set up
6808 additional compilation units for each of the include files we see
6809 bracketed by BINCL/EINCL. */
6811 static void
6812 break_out_includes (dw_die_ref die)
6814 dw_die_ref c;
6815 dw_die_ref unit = NULL;
6816 limbo_die_node *node, **pnode;
6818 c = die->die_child;
6819 if (c) do {
6820 dw_die_ref prev = c;
6821 c = c->die_sib;
6822 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6823 || (unit && is_comdat_die (c)))
6825 dw_die_ref next = c->die_sib;
6827 /* This DIE is for a secondary CU; remove it from the main one. */
6828 remove_child_with_prev (c, prev);
6830 if (c->die_tag == DW_TAG_GNU_BINCL)
6831 unit = push_new_compile_unit (unit, c);
6832 else if (c->die_tag == DW_TAG_GNU_EINCL)
6833 unit = pop_compile_unit (unit);
6834 else
6835 add_child_die (unit, c);
6836 c = next;
6837 if (c == die->die_child)
6838 break;
6840 } while (c != die->die_child);
6842 #if 0
6843 /* We can only use this in debugging, since the frontend doesn't check
6844 to make sure that we leave every include file we enter. */
6845 gcc_assert (!unit);
6846 #endif
6848 assign_symbol_names (die);
6849 cu_hash_type cu_hash_table (10);
6850 for (node = limbo_die_list, pnode = &limbo_die_list;
6851 node;
6852 node = node->next)
6854 int is_dupl;
6856 compute_section_prefix (node->die);
6857 is_dupl = check_duplicate_cu (node->die, &cu_hash_table,
6858 &comdat_symbol_number);
6859 assign_symbol_names (node->die);
6860 if (is_dupl)
6861 *pnode = node->next;
6862 else
6864 pnode = &node->next;
6865 record_comdat_symbol_number (node->die, &cu_hash_table,
6866 comdat_symbol_number);
6871 /* Return non-zero if this DIE is a declaration. */
6873 static int
6874 is_declaration_die (dw_die_ref die)
6876 dw_attr_ref a;
6877 unsigned ix;
6879 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6880 if (a->dw_attr == DW_AT_declaration)
6881 return 1;
6883 return 0;
6886 /* Return non-zero if this DIE is nested inside a subprogram. */
6888 static int
6889 is_nested_in_subprogram (dw_die_ref die)
6891 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
6893 if (decl == NULL)
6894 decl = die;
6895 return local_scope_p (decl);
6898 /* Return non-zero if this DIE contains a defining declaration of a
6899 subprogram. */
6901 static int
6902 contains_subprogram_definition (dw_die_ref die)
6904 dw_die_ref c;
6906 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
6907 return 1;
6908 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
6909 return 0;
6912 /* Return non-zero if this is a type DIE that should be moved to a
6913 COMDAT .debug_types section. */
6915 static int
6916 should_move_die_to_comdat (dw_die_ref die)
6918 switch (die->die_tag)
6920 case DW_TAG_class_type:
6921 case DW_TAG_structure_type:
6922 case DW_TAG_enumeration_type:
6923 case DW_TAG_union_type:
6924 /* Don't move declarations, inlined instances, types nested in a
6925 subprogram, or types that contain subprogram definitions. */
6926 if (is_declaration_die (die)
6927 || get_AT (die, DW_AT_abstract_origin)
6928 || is_nested_in_subprogram (die)
6929 || contains_subprogram_definition (die))
6930 return 0;
6931 return 1;
6932 case DW_TAG_array_type:
6933 case DW_TAG_interface_type:
6934 case DW_TAG_pointer_type:
6935 case DW_TAG_reference_type:
6936 case DW_TAG_rvalue_reference_type:
6937 case DW_TAG_string_type:
6938 case DW_TAG_subroutine_type:
6939 case DW_TAG_ptr_to_member_type:
6940 case DW_TAG_set_type:
6941 case DW_TAG_subrange_type:
6942 case DW_TAG_base_type:
6943 case DW_TAG_const_type:
6944 case DW_TAG_file_type:
6945 case DW_TAG_packed_type:
6946 case DW_TAG_volatile_type:
6947 case DW_TAG_typedef:
6948 default:
6949 return 0;
6953 /* Make a clone of DIE. */
6955 static dw_die_ref
6956 clone_die (dw_die_ref die)
6958 dw_die_ref clone;
6959 dw_attr_ref a;
6960 unsigned ix;
6962 clone = ggc_cleared_alloc<die_node> ();
6963 clone->die_tag = die->die_tag;
6965 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6966 add_dwarf_attr (clone, a);
6968 return clone;
6971 /* Make a clone of the tree rooted at DIE. */
6973 static dw_die_ref
6974 clone_tree (dw_die_ref die)
6976 dw_die_ref c;
6977 dw_die_ref clone = clone_die (die);
6979 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
6981 return clone;
6984 /* Make a clone of DIE as a declaration. */
6986 static dw_die_ref
6987 clone_as_declaration (dw_die_ref die)
6989 dw_die_ref clone;
6990 dw_die_ref decl;
6991 dw_attr_ref a;
6992 unsigned ix;
6994 /* If the DIE is already a declaration, just clone it. */
6995 if (is_declaration_die (die))
6996 return clone_die (die);
6998 /* If the DIE is a specification, just clone its declaration DIE. */
6999 decl = get_AT_ref (die, DW_AT_specification);
7000 if (decl != NULL)
7002 clone = clone_die (decl);
7003 if (die->comdat_type_p)
7004 add_AT_die_ref (clone, DW_AT_signature, die);
7005 return clone;
7008 clone = ggc_cleared_alloc<die_node> ();
7009 clone->die_tag = die->die_tag;
7011 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7013 /* We don't want to copy over all attributes.
7014 For example we don't want DW_AT_byte_size because otherwise we will no
7015 longer have a declaration and GDB will treat it as a definition. */
7017 switch (a->dw_attr)
7019 case DW_AT_abstract_origin:
7020 case DW_AT_artificial:
7021 case DW_AT_containing_type:
7022 case DW_AT_external:
7023 case DW_AT_name:
7024 case DW_AT_type:
7025 case DW_AT_virtuality:
7026 case DW_AT_linkage_name:
7027 case DW_AT_MIPS_linkage_name:
7028 add_dwarf_attr (clone, a);
7029 break;
7030 case DW_AT_byte_size:
7031 default:
7032 break;
7036 if (die->comdat_type_p)
7037 add_AT_die_ref (clone, DW_AT_signature, die);
7039 add_AT_flag (clone, DW_AT_declaration, 1);
7040 return clone;
7044 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
7046 struct decl_table_entry
7048 dw_die_ref orig;
7049 dw_die_ref copy;
7052 /* Helpers to manipulate hash table of copied declarations. */
7054 /* Hashtable helpers. */
7056 struct decl_table_entry_hasher : typed_free_remove <decl_table_entry>
7058 typedef decl_table_entry value_type;
7059 typedef die_struct compare_type;
7060 static inline hashval_t hash (const value_type *);
7061 static inline bool equal (const value_type *, const compare_type *);
7064 inline hashval_t
7065 decl_table_entry_hasher::hash (const value_type *entry)
7067 return htab_hash_pointer (entry->orig);
7070 inline bool
7071 decl_table_entry_hasher::equal (const value_type *entry1,
7072 const compare_type *entry2)
7074 return entry1->orig == entry2;
7077 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
7079 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7080 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
7081 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
7082 to check if the ancestor has already been copied into UNIT. */
7084 static dw_die_ref
7085 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
7086 decl_hash_type *decl_table)
7088 dw_die_ref parent = die->die_parent;
7089 dw_die_ref new_parent = unit;
7090 dw_die_ref copy;
7091 decl_table_entry **slot = NULL;
7092 struct decl_table_entry *entry = NULL;
7094 if (decl_table)
7096 /* Check if the entry has already been copied to UNIT. */
7097 slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
7098 INSERT);
7099 if (*slot != HTAB_EMPTY_ENTRY)
7101 entry = *slot;
7102 return entry->copy;
7105 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
7106 entry = XCNEW (struct decl_table_entry);
7107 entry->orig = die;
7108 entry->copy = NULL;
7109 *slot = entry;
7112 if (parent != NULL)
7114 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7115 if (spec != NULL)
7116 parent = spec;
7117 if (!is_unit_die (parent))
7118 new_parent = copy_ancestor_tree (unit, parent, decl_table);
7121 copy = clone_as_declaration (die);
7122 add_child_die (new_parent, copy);
7124 if (decl_table)
7126 /* Record the pointer to the copy. */
7127 entry->copy = copy;
7130 return copy;
7132 /* Copy the declaration context to the new type unit DIE. This includes
7133 any surrounding namespace or type declarations. If the DIE has an
7134 AT_specification attribute, it also includes attributes and children
7135 attached to the specification, and returns a pointer to the original
7136 parent of the declaration DIE. Returns NULL otherwise. */
7138 static dw_die_ref
7139 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7141 dw_die_ref decl;
7142 dw_die_ref new_decl;
7143 dw_die_ref orig_parent = NULL;
7145 decl = get_AT_ref (die, DW_AT_specification);
7146 if (decl == NULL)
7147 decl = die;
7148 else
7150 unsigned ix;
7151 dw_die_ref c;
7152 dw_attr_ref a;
7154 /* The original DIE will be changed to a declaration, and must
7155 be moved to be a child of the original declaration DIE. */
7156 orig_parent = decl->die_parent;
7158 /* Copy the type node pointer from the new DIE to the original
7159 declaration DIE so we can forward references later. */
7160 decl->comdat_type_p = true;
7161 decl->die_id.die_type_node = die->die_id.die_type_node;
7163 remove_AT (die, DW_AT_specification);
7165 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7167 if (a->dw_attr != DW_AT_name
7168 && a->dw_attr != DW_AT_declaration
7169 && a->dw_attr != DW_AT_external)
7170 add_dwarf_attr (die, a);
7173 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7176 if (decl->die_parent != NULL
7177 && !is_unit_die (decl->die_parent))
7179 new_decl = copy_ancestor_tree (unit, decl, NULL);
7180 if (new_decl != NULL)
7182 remove_AT (new_decl, DW_AT_signature);
7183 add_AT_specification (die, new_decl);
7187 return orig_parent;
7190 /* Generate the skeleton ancestor tree for the given NODE, then clone
7191 the DIE and add the clone into the tree. */
7193 static void
7194 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7196 if (node->new_die != NULL)
7197 return;
7199 node->new_die = clone_as_declaration (node->old_die);
7201 if (node->parent != NULL)
7203 generate_skeleton_ancestor_tree (node->parent);
7204 add_child_die (node->parent->new_die, node->new_die);
7208 /* Generate a skeleton tree of DIEs containing any declarations that are
7209 found in the original tree. We traverse the tree looking for declaration
7210 DIEs, and construct the skeleton from the bottom up whenever we find one. */
7212 static void
7213 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7215 skeleton_chain_node node;
7216 dw_die_ref c;
7217 dw_die_ref first;
7218 dw_die_ref prev = NULL;
7219 dw_die_ref next = NULL;
7221 node.parent = parent;
7223 first = c = parent->old_die->die_child;
7224 if (c)
7225 next = c->die_sib;
7226 if (c) do {
7227 if (prev == NULL || prev->die_sib == c)
7228 prev = c;
7229 c = next;
7230 next = (c == first ? NULL : c->die_sib);
7231 node.old_die = c;
7232 node.new_die = NULL;
7233 if (is_declaration_die (c))
7235 if (is_template_instantiation (c))
7237 /* Instantiated templates do not need to be cloned into the
7238 type unit. Just move the DIE and its children back to
7239 the skeleton tree (in the main CU). */
7240 remove_child_with_prev (c, prev);
7241 add_child_die (parent->new_die, c);
7242 c = prev;
7244 else
7246 /* Clone the existing DIE, move the original to the skeleton
7247 tree (which is in the main CU), and put the clone, with
7248 all the original's children, where the original came from
7249 (which is about to be moved to the type unit). */
7250 dw_die_ref clone = clone_die (c);
7251 move_all_children (c, clone);
7253 /* If the original has a DW_AT_object_pointer attribute,
7254 it would now point to a child DIE just moved to the
7255 cloned tree, so we need to remove that attribute from
7256 the original. */
7257 remove_AT (c, DW_AT_object_pointer);
7259 replace_child (c, clone, prev);
7260 generate_skeleton_ancestor_tree (parent);
7261 add_child_die (parent->new_die, c);
7262 node.new_die = c;
7263 c = clone;
7266 generate_skeleton_bottom_up (&node);
7267 } while (next != NULL);
7270 /* Wrapper function for generate_skeleton_bottom_up. */
7272 static dw_die_ref
7273 generate_skeleton (dw_die_ref die)
7275 skeleton_chain_node node;
7277 node.old_die = die;
7278 node.new_die = NULL;
7279 node.parent = NULL;
7281 /* If this type definition is nested inside another type,
7282 and is not an instantiation of a template, always leave
7283 at least a declaration in its place. */
7284 if (die->die_parent != NULL
7285 && is_type_die (die->die_parent)
7286 && !is_template_instantiation (die))
7287 node.new_die = clone_as_declaration (die);
7289 generate_skeleton_bottom_up (&node);
7290 return node.new_die;
7293 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
7294 declaration. The original DIE is moved to a new compile unit so that
7295 existing references to it follow it to the new location. If any of the
7296 original DIE's descendants is a declaration, we need to replace the
7297 original DIE with a skeleton tree and move the declarations back into the
7298 skeleton tree. */
7300 static dw_die_ref
7301 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
7302 dw_die_ref prev)
7304 dw_die_ref skeleton, orig_parent;
7306 /* Copy the declaration context to the type unit DIE. If the returned
7307 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
7308 that DIE. */
7309 orig_parent = copy_declaration_context (unit, child);
7311 skeleton = generate_skeleton (child);
7312 if (skeleton == NULL)
7313 remove_child_with_prev (child, prev);
7314 else
7316 skeleton->comdat_type_p = true;
7317 skeleton->die_id.die_type_node = child->die_id.die_type_node;
7319 /* If the original DIE was a specification, we need to put
7320 the skeleton under the parent DIE of the declaration.
7321 This leaves the original declaration in the tree, but
7322 it will be pruned later since there are no longer any
7323 references to it. */
7324 if (orig_parent != NULL)
7326 remove_child_with_prev (child, prev);
7327 add_child_die (orig_parent, skeleton);
7329 else
7330 replace_child (child, skeleton, prev);
7333 return skeleton;
7336 /* Traverse the DIE and set up additional .debug_types sections for each
7337 type worthy of being placed in a COMDAT section. */
7339 static void
7340 break_out_comdat_types (dw_die_ref die)
7342 dw_die_ref c;
7343 dw_die_ref first;
7344 dw_die_ref prev = NULL;
7345 dw_die_ref next = NULL;
7346 dw_die_ref unit = NULL;
7348 first = c = die->die_child;
7349 if (c)
7350 next = c->die_sib;
7351 if (c) do {
7352 if (prev == NULL || prev->die_sib == c)
7353 prev = c;
7354 c = next;
7355 next = (c == first ? NULL : c->die_sib);
7356 if (should_move_die_to_comdat (c))
7358 dw_die_ref replacement;
7359 comdat_type_node_ref type_node;
7361 /* Break out nested types into their own type units. */
7362 break_out_comdat_types (c);
7364 /* Create a new type unit DIE as the root for the new tree, and
7365 add it to the list of comdat types. */
7366 unit = new_die (DW_TAG_type_unit, NULL, NULL);
7367 add_AT_unsigned (unit, DW_AT_language,
7368 get_AT_unsigned (comp_unit_die (), DW_AT_language));
7369 type_node = ggc_cleared_alloc<comdat_type_node> ();
7370 type_node->root_die = unit;
7371 type_node->next = comdat_type_list;
7372 comdat_type_list = type_node;
7374 /* Generate the type signature. */
7375 generate_type_signature (c, type_node);
7377 /* Copy the declaration context, attributes, and children of the
7378 declaration into the new type unit DIE, then remove this DIE
7379 from the main CU (or replace it with a skeleton if necessary). */
7380 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
7381 type_node->skeleton_die = replacement;
7383 /* Add the DIE to the new compunit. */
7384 add_child_die (unit, c);
7386 if (replacement != NULL)
7387 c = replacement;
7389 else if (c->die_tag == DW_TAG_namespace
7390 || c->die_tag == DW_TAG_class_type
7391 || c->die_tag == DW_TAG_structure_type
7392 || c->die_tag == DW_TAG_union_type)
7394 /* Look for nested types that can be broken out. */
7395 break_out_comdat_types (c);
7397 } while (next != NULL);
7400 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
7401 Enter all the cloned children into the hash table decl_table. */
7403 static dw_die_ref
7404 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
7406 dw_die_ref c;
7407 dw_die_ref clone;
7408 struct decl_table_entry *entry;
7409 decl_table_entry **slot;
7411 if (die->die_tag == DW_TAG_subprogram)
7412 clone = clone_as_declaration (die);
7413 else
7414 clone = clone_die (die);
7416 slot = decl_table->find_slot_with_hash (die,
7417 htab_hash_pointer (die), INSERT);
7419 /* Assert that DIE isn't in the hash table yet. If it would be there
7420 before, the ancestors would be necessarily there as well, therefore
7421 clone_tree_partial wouldn't be called. */
7422 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
7424 entry = XCNEW (struct decl_table_entry);
7425 entry->orig = die;
7426 entry->copy = clone;
7427 *slot = entry;
7429 if (die->die_tag != DW_TAG_subprogram)
7430 FOR_EACH_CHILD (die, c,
7431 add_child_die (clone, clone_tree_partial (c, decl_table)));
7433 return clone;
7436 /* Walk the DIE and its children, looking for references to incomplete
7437 or trivial types that are unmarked (i.e., that are not in the current
7438 type_unit). */
7440 static void
7441 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
7443 dw_die_ref c;
7444 dw_attr_ref a;
7445 unsigned ix;
7447 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7449 if (AT_class (a) == dw_val_class_die_ref)
7451 dw_die_ref targ = AT_ref (a);
7452 decl_table_entry **slot;
7453 struct decl_table_entry *entry;
7455 if (targ->die_mark != 0 || targ->comdat_type_p)
7456 continue;
7458 slot = decl_table->find_slot_with_hash (targ,
7459 htab_hash_pointer (targ),
7460 INSERT);
7462 if (*slot != HTAB_EMPTY_ENTRY)
7464 /* TARG has already been copied, so we just need to
7465 modify the reference to point to the copy. */
7466 entry = *slot;
7467 a->dw_attr_val.v.val_die_ref.die = entry->copy;
7469 else
7471 dw_die_ref parent = unit;
7472 dw_die_ref copy = clone_die (targ);
7474 /* Record in DECL_TABLE that TARG has been copied.
7475 Need to do this now, before the recursive call,
7476 because DECL_TABLE may be expanded and SLOT
7477 would no longer be a valid pointer. */
7478 entry = XCNEW (struct decl_table_entry);
7479 entry->orig = targ;
7480 entry->copy = copy;
7481 *slot = entry;
7483 /* If TARG is not a declaration DIE, we need to copy its
7484 children. */
7485 if (!is_declaration_die (targ))
7487 FOR_EACH_CHILD (
7488 targ, c,
7489 add_child_die (copy,
7490 clone_tree_partial (c, decl_table)));
7493 /* Make sure the cloned tree is marked as part of the
7494 type unit. */
7495 mark_dies (copy);
7497 /* If TARG has surrounding context, copy its ancestor tree
7498 into the new type unit. */
7499 if (targ->die_parent != NULL
7500 && !is_unit_die (targ->die_parent))
7501 parent = copy_ancestor_tree (unit, targ->die_parent,
7502 decl_table);
7504 add_child_die (parent, copy);
7505 a->dw_attr_val.v.val_die_ref.die = copy;
7507 /* Make sure the newly-copied DIE is walked. If it was
7508 installed in a previously-added context, it won't
7509 get visited otherwise. */
7510 if (parent != unit)
7512 /* Find the highest point of the newly-added tree,
7513 mark each node along the way, and walk from there. */
7514 parent->die_mark = 1;
7515 while (parent->die_parent
7516 && parent->die_parent->die_mark == 0)
7518 parent = parent->die_parent;
7519 parent->die_mark = 1;
7521 copy_decls_walk (unit, parent, decl_table);
7527 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
7530 /* Copy declarations for "unworthy" types into the new comdat section.
7531 Incomplete types, modified types, and certain other types aren't broken
7532 out into comdat sections of their own, so they don't have a signature,
7533 and we need to copy the declaration into the same section so that we
7534 don't have an external reference. */
7536 static void
7537 copy_decls_for_unworthy_types (dw_die_ref unit)
7539 mark_dies (unit);
7540 decl_hash_type decl_table (10);
7541 copy_decls_walk (unit, unit, &decl_table);
7542 unmark_dies (unit);
7545 /* Traverse the DIE and add a sibling attribute if it may have the
7546 effect of speeding up access to siblings. To save some space,
7547 avoid generating sibling attributes for DIE's without children. */
7549 static void
7550 add_sibling_attributes (dw_die_ref die)
7552 dw_die_ref c;
7554 if (! die->die_child)
7555 return;
7557 if (die->die_parent && die != die->die_parent->die_child)
7558 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7560 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7563 /* Output all location lists for the DIE and its children. */
7565 static void
7566 output_location_lists (dw_die_ref die)
7568 dw_die_ref c;
7569 dw_attr_ref a;
7570 unsigned ix;
7572 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7573 if (AT_class (a) == dw_val_class_loc_list)
7574 output_loc_list (AT_loc_list (a));
7576 FOR_EACH_CHILD (die, c, output_location_lists (c));
7579 /* We want to limit the number of external references, because they are
7580 larger than local references: a relocation takes multiple words, and
7581 even a sig8 reference is always eight bytes, whereas a local reference
7582 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
7583 So if we encounter multiple external references to the same type DIE, we
7584 make a local typedef stub for it and redirect all references there.
7586 This is the element of the hash table for keeping track of these
7587 references. */
7589 struct external_ref
7591 dw_die_ref type;
7592 dw_die_ref stub;
7593 unsigned n_refs;
7596 /* Hashtable helpers. */
7598 struct external_ref_hasher : typed_free_remove <external_ref>
7600 typedef external_ref value_type;
7601 typedef external_ref compare_type;
7602 static inline hashval_t hash (const value_type *);
7603 static inline bool equal (const value_type *, const compare_type *);
7606 inline hashval_t
7607 external_ref_hasher::hash (const value_type *r)
7609 dw_die_ref die = r->type;
7610 hashval_t h = 0;
7612 /* We can't use the address of the DIE for hashing, because
7613 that will make the order of the stub DIEs non-deterministic. */
7614 if (! die->comdat_type_p)
7615 /* We have a symbol; use it to compute a hash. */
7616 h = htab_hash_string (die->die_id.die_symbol);
7617 else
7619 /* We have a type signature; use a subset of the bits as the hash.
7620 The 8-byte signature is at least as large as hashval_t. */
7621 comdat_type_node_ref type_node = die->die_id.die_type_node;
7622 memcpy (&h, type_node->signature, sizeof (h));
7624 return h;
7627 inline bool
7628 external_ref_hasher::equal (const value_type *r1, const compare_type *r2)
7630 return r1->type == r2->type;
7633 typedef hash_table<external_ref_hasher> external_ref_hash_type;
7635 /* Return a pointer to the external_ref for references to DIE. */
7637 static struct external_ref *
7638 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
7640 struct external_ref ref, *ref_p;
7641 external_ref **slot;
7643 ref.type = die;
7644 slot = map->find_slot (&ref, INSERT);
7645 if (*slot != HTAB_EMPTY_ENTRY)
7646 return *slot;
7648 ref_p = XCNEW (struct external_ref);
7649 ref_p->type = die;
7650 *slot = ref_p;
7651 return ref_p;
7654 /* Subroutine of optimize_external_refs, below.
7656 If we see a type skeleton, record it as our stub. If we see external
7657 references, remember how many we've seen. */
7659 static void
7660 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
7662 dw_die_ref c;
7663 dw_attr_ref a;
7664 unsigned ix;
7665 struct external_ref *ref_p;
7667 if (is_type_die (die)
7668 && (c = get_AT_ref (die, DW_AT_signature)))
7670 /* This is a local skeleton; use it for local references. */
7671 ref_p = lookup_external_ref (map, c);
7672 ref_p->stub = die;
7675 /* Scan the DIE references, and remember any that refer to DIEs from
7676 other CUs (i.e. those which are not marked). */
7677 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7678 if (AT_class (a) == dw_val_class_die_ref
7679 && (c = AT_ref (a))->die_mark == 0
7680 && is_type_die (c))
7682 ref_p = lookup_external_ref (map, c);
7683 ref_p->n_refs++;
7686 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
7689 /* htab_traverse callback function for optimize_external_refs, below. SLOT
7690 points to an external_ref, DATA is the CU we're processing. If we don't
7691 already have a local stub, and we have multiple refs, build a stub. */
7694 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
7696 struct external_ref *ref_p = *slot;
7698 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
7700 /* We have multiple references to this type, so build a small stub.
7701 Both of these forms are a bit dodgy from the perspective of the
7702 DWARF standard, since technically they should have names. */
7703 dw_die_ref cu = data;
7704 dw_die_ref type = ref_p->type;
7705 dw_die_ref stub = NULL;
7707 if (type->comdat_type_p)
7709 /* If we refer to this type via sig8, use AT_signature. */
7710 stub = new_die (type->die_tag, cu, NULL_TREE);
7711 add_AT_die_ref (stub, DW_AT_signature, type);
7713 else
7715 /* Otherwise, use a typedef with no name. */
7716 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
7717 add_AT_die_ref (stub, DW_AT_type, type);
7720 stub->die_mark++;
7721 ref_p->stub = stub;
7723 return 1;
7726 /* DIE is a unit; look through all the DIE references to see if there are
7727 any external references to types, and if so, create local stubs for
7728 them which will be applied in build_abbrev_table. This is useful because
7729 references to local DIEs are smaller. */
7731 static external_ref_hash_type *
7732 optimize_external_refs (dw_die_ref die)
7734 external_ref_hash_type *map = new external_ref_hash_type (10);
7735 optimize_external_refs_1 (die, map);
7736 map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
7737 return map;
7740 /* The format of each DIE (and its attribute value pairs) is encoded in an
7741 abbreviation table. This routine builds the abbreviation table and assigns
7742 a unique abbreviation id for each abbreviation entry. The children of each
7743 die are visited recursively. */
7745 static void
7746 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
7748 unsigned long abbrev_id;
7749 unsigned int n_alloc;
7750 dw_die_ref c;
7751 dw_attr_ref a;
7752 unsigned ix;
7754 /* Scan the DIE references, and replace any that refer to
7755 DIEs from other CUs (i.e. those which are not marked) with
7756 the local stubs we built in optimize_external_refs. */
7757 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7758 if (AT_class (a) == dw_val_class_die_ref
7759 && (c = AT_ref (a))->die_mark == 0)
7761 struct external_ref *ref_p;
7762 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
7764 ref_p = lookup_external_ref (extern_map, c);
7765 if (ref_p->stub && ref_p->stub != die)
7766 change_AT_die_ref (a, ref_p->stub);
7767 else
7768 /* We aren't changing this reference, so mark it external. */
7769 set_AT_ref_external (a, 1);
7772 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7774 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7775 dw_attr_ref die_a, abbrev_a;
7776 unsigned ix;
7777 bool ok = true;
7779 if (abbrev->die_tag != die->die_tag)
7780 continue;
7781 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7782 continue;
7784 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
7785 continue;
7787 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
7789 abbrev_a = &(*abbrev->die_attr)[ix];
7790 if ((abbrev_a->dw_attr != die_a->dw_attr)
7791 || (value_format (abbrev_a) != value_format (die_a)))
7793 ok = false;
7794 break;
7797 if (ok)
7798 break;
7801 if (abbrev_id >= abbrev_die_table_in_use)
7803 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7805 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
7806 abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7807 n_alloc);
7809 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7810 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
7811 abbrev_die_table_allocated = n_alloc;
7814 ++abbrev_die_table_in_use;
7815 abbrev_die_table[abbrev_id] = die;
7818 die->die_abbrev = abbrev_id;
7819 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
7822 /* Return the power-of-two number of bytes necessary to represent VALUE. */
7824 static int
7825 constant_size (unsigned HOST_WIDE_INT value)
7827 int log;
7829 if (value == 0)
7830 log = 0;
7831 else
7832 log = floor_log2 (value);
7834 log = log / 8;
7835 log = 1 << (floor_log2 (log) + 1);
7837 return log;
7840 /* Return the size of a DIE as it is represented in the
7841 .debug_info section. */
7843 static unsigned long
7844 size_of_die (dw_die_ref die)
7846 unsigned long size = 0;
7847 dw_attr_ref a;
7848 unsigned ix;
7849 enum dwarf_form form;
7851 size += size_of_uleb128 (die->die_abbrev);
7852 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7854 switch (AT_class (a))
7856 case dw_val_class_addr:
7857 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7859 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7860 size += size_of_uleb128 (AT_index (a));
7862 else
7863 size += DWARF2_ADDR_SIZE;
7864 break;
7865 case dw_val_class_offset:
7866 size += DWARF_OFFSET_SIZE;
7867 break;
7868 case dw_val_class_loc:
7870 unsigned long lsize = size_of_locs (AT_loc (a));
7872 /* Block length. */
7873 if (dwarf_version >= 4)
7874 size += size_of_uleb128 (lsize);
7875 else
7876 size += constant_size (lsize);
7877 size += lsize;
7879 break;
7880 case dw_val_class_loc_list:
7881 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7883 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7884 size += size_of_uleb128 (AT_index (a));
7886 else
7887 size += DWARF_OFFSET_SIZE;
7888 break;
7889 case dw_val_class_range_list:
7890 size += DWARF_OFFSET_SIZE;
7891 break;
7892 case dw_val_class_const:
7893 size += size_of_sleb128 (AT_int (a));
7894 break;
7895 case dw_val_class_unsigned_const:
7897 int csize = constant_size (AT_unsigned (a));
7898 if (dwarf_version == 3
7899 && a->dw_attr == DW_AT_data_member_location
7900 && csize >= 4)
7901 size += size_of_uleb128 (AT_unsigned (a));
7902 else
7903 size += csize;
7905 break;
7906 case dw_val_class_const_double:
7907 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
7908 if (HOST_BITS_PER_WIDE_INT >= 64)
7909 size++; /* block */
7910 break;
7911 case dw_val_class_wide_int:
7912 size += (get_full_len (*a->dw_attr_val.v.val_wide)
7913 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
7914 if (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT
7915 > 64)
7916 size++; /* block */
7917 break;
7918 case dw_val_class_vec:
7919 size += constant_size (a->dw_attr_val.v.val_vec.length
7920 * a->dw_attr_val.v.val_vec.elt_size)
7921 + a->dw_attr_val.v.val_vec.length
7922 * a->dw_attr_val.v.val_vec.elt_size; /* block */
7923 break;
7924 case dw_val_class_flag:
7925 if (dwarf_version >= 4)
7926 /* Currently all add_AT_flag calls pass in 1 as last argument,
7927 so DW_FORM_flag_present can be used. If that ever changes,
7928 we'll need to use DW_FORM_flag and have some optimization
7929 in build_abbrev_table that will change those to
7930 DW_FORM_flag_present if it is set to 1 in all DIEs using
7931 the same abbrev entry. */
7932 gcc_assert (a->dw_attr_val.v.val_flag == 1);
7933 else
7934 size += 1;
7935 break;
7936 case dw_val_class_die_ref:
7937 if (AT_ref_external (a))
7939 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
7940 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
7941 is sized by target address length, whereas in DWARF3
7942 it's always sized as an offset. */
7943 if (use_debug_types)
7944 size += DWARF_TYPE_SIGNATURE_SIZE;
7945 else if (dwarf_version == 2)
7946 size += DWARF2_ADDR_SIZE;
7947 else
7948 size += DWARF_OFFSET_SIZE;
7950 else
7951 size += DWARF_OFFSET_SIZE;
7952 break;
7953 case dw_val_class_fde_ref:
7954 size += DWARF_OFFSET_SIZE;
7955 break;
7956 case dw_val_class_lbl_id:
7957 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7959 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7960 size += size_of_uleb128 (AT_index (a));
7962 else
7963 size += DWARF2_ADDR_SIZE;
7964 break;
7965 case dw_val_class_lineptr:
7966 case dw_val_class_macptr:
7967 size += DWARF_OFFSET_SIZE;
7968 break;
7969 case dw_val_class_str:
7970 form = AT_string_form (a);
7971 if (form == DW_FORM_strp)
7972 size += DWARF_OFFSET_SIZE;
7973 else if (form == DW_FORM_GNU_str_index)
7974 size += size_of_uleb128 (AT_index (a));
7975 else
7976 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
7977 break;
7978 case dw_val_class_file:
7979 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
7980 break;
7981 case dw_val_class_data8:
7982 size += 8;
7983 break;
7984 case dw_val_class_vms_delta:
7985 size += DWARF_OFFSET_SIZE;
7986 break;
7987 case dw_val_class_high_pc:
7988 size += DWARF2_ADDR_SIZE;
7989 break;
7990 default:
7991 gcc_unreachable ();
7995 return size;
7998 /* Size the debugging information associated with a given DIE. Visits the
7999 DIE's children recursively. Updates the global variable next_die_offset, on
8000 each time through. Uses the current value of next_die_offset to update the
8001 die_offset field in each DIE. */
8003 static void
8004 calc_die_sizes (dw_die_ref die)
8006 dw_die_ref c;
8008 gcc_assert (die->die_offset == 0
8009 || (unsigned long int) die->die_offset == next_die_offset);
8010 die->die_offset = next_die_offset;
8011 next_die_offset += size_of_die (die);
8013 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
8015 if (die->die_child != NULL)
8016 /* Count the null byte used to terminate sibling lists. */
8017 next_die_offset += 1;
8020 /* Size just the base type children at the start of the CU.
8021 This is needed because build_abbrev needs to size locs
8022 and sizing of type based stack ops needs to know die_offset
8023 values for the base types. */
8025 static void
8026 calc_base_type_die_sizes (void)
8028 unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8029 unsigned int i;
8030 dw_die_ref base_type;
8031 #if ENABLE_ASSERT_CHECKING
8032 dw_die_ref prev = comp_unit_die ()->die_child;
8033 #endif
8035 die_offset += size_of_die (comp_unit_die ());
8036 for (i = 0; base_types.iterate (i, &base_type); i++)
8038 #if ENABLE_ASSERT_CHECKING
8039 gcc_assert (base_type->die_offset == 0
8040 && prev->die_sib == base_type
8041 && base_type->die_child == NULL
8042 && base_type->die_abbrev);
8043 prev = base_type;
8044 #endif
8045 base_type->die_offset = die_offset;
8046 die_offset += size_of_die (base_type);
8050 /* Set the marks for a die and its children. We do this so
8051 that we know whether or not a reference needs to use FORM_ref_addr; only
8052 DIEs in the same CU will be marked. We used to clear out the offset
8053 and use that as the flag, but ran into ordering problems. */
8055 static void
8056 mark_dies (dw_die_ref die)
8058 dw_die_ref c;
8060 gcc_assert (!die->die_mark);
8062 die->die_mark = 1;
8063 FOR_EACH_CHILD (die, c, mark_dies (c));
8066 /* Clear the marks for a die and its children. */
8068 static void
8069 unmark_dies (dw_die_ref die)
8071 dw_die_ref c;
8073 if (! use_debug_types)
8074 gcc_assert (die->die_mark);
8076 die->die_mark = 0;
8077 FOR_EACH_CHILD (die, c, unmark_dies (c));
8080 /* Clear the marks for a die, its children and referred dies. */
8082 static void
8083 unmark_all_dies (dw_die_ref die)
8085 dw_die_ref c;
8086 dw_attr_ref a;
8087 unsigned ix;
8089 if (!die->die_mark)
8090 return;
8091 die->die_mark = 0;
8093 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
8095 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8096 if (AT_class (a) == dw_val_class_die_ref)
8097 unmark_all_dies (AT_ref (a));
8100 /* Calculate if the entry should appear in the final output file. It may be
8101 from a pruned a type. */
8103 static bool
8104 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
8106 /* By limiting gnu pubnames to definitions only, gold can generate a
8107 gdb index without entries for declarations, which don't include
8108 enough information to be useful. */
8109 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
8110 return false;
8112 if (table == pubname_table)
8114 /* Enumerator names are part of the pubname table, but the
8115 parent DW_TAG_enumeration_type die may have been pruned.
8116 Don't output them if that is the case. */
8117 if (p->die->die_tag == DW_TAG_enumerator &&
8118 (p->die->die_parent == NULL
8119 || !p->die->die_parent->die_perennial_p))
8120 return false;
8122 /* Everything else in the pubname table is included. */
8123 return true;
8126 /* The pubtypes table shouldn't include types that have been
8127 pruned. */
8128 return (p->die->die_offset != 0
8129 || !flag_eliminate_unused_debug_types);
8132 /* Return the size of the .debug_pubnames or .debug_pubtypes table
8133 generated for the compilation unit. */
8135 static unsigned long
8136 size_of_pubnames (vec<pubname_entry, va_gc> *names)
8138 unsigned long size;
8139 unsigned i;
8140 pubname_ref p;
8141 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
8143 size = DWARF_PUBNAMES_HEADER_SIZE;
8144 FOR_EACH_VEC_ELT (*names, i, p)
8145 if (include_pubname_in_output (names, p))
8146 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
8148 size += DWARF_OFFSET_SIZE;
8149 return size;
8152 /* Return the size of the information in the .debug_aranges section. */
8154 static unsigned long
8155 size_of_aranges (void)
8157 unsigned long size;
8159 size = DWARF_ARANGES_HEADER_SIZE;
8161 /* Count the address/length pair for this compilation unit. */
8162 if (text_section_used)
8163 size += 2 * DWARF2_ADDR_SIZE;
8164 if (cold_text_section_used)
8165 size += 2 * DWARF2_ADDR_SIZE;
8166 if (have_multiple_function_sections)
8168 unsigned fde_idx;
8169 dw_fde_ref fde;
8171 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
8173 if (DECL_IGNORED_P (fde->decl))
8174 continue;
8175 if (!fde->in_std_section)
8176 size += 2 * DWARF2_ADDR_SIZE;
8177 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
8178 size += 2 * DWARF2_ADDR_SIZE;
8182 /* Count the two zero words used to terminated the address range table. */
8183 size += 2 * DWARF2_ADDR_SIZE;
8184 return size;
8187 /* Select the encoding of an attribute value. */
8189 static enum dwarf_form
8190 value_format (dw_attr_ref a)
8192 switch (AT_class (a))
8194 case dw_val_class_addr:
8195 /* Only very few attributes allow DW_FORM_addr. */
8196 switch (a->dw_attr)
8198 case DW_AT_low_pc:
8199 case DW_AT_high_pc:
8200 case DW_AT_entry_pc:
8201 case DW_AT_trampoline:
8202 return (AT_index (a) == NOT_INDEXED
8203 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8204 default:
8205 break;
8207 switch (DWARF2_ADDR_SIZE)
8209 case 1:
8210 return DW_FORM_data1;
8211 case 2:
8212 return DW_FORM_data2;
8213 case 4:
8214 return DW_FORM_data4;
8215 case 8:
8216 return DW_FORM_data8;
8217 default:
8218 gcc_unreachable ();
8220 case dw_val_class_range_list:
8221 case dw_val_class_loc_list:
8222 if (dwarf_version >= 4)
8223 return DW_FORM_sec_offset;
8224 /* FALLTHRU */
8225 case dw_val_class_vms_delta:
8226 case dw_val_class_offset:
8227 switch (DWARF_OFFSET_SIZE)
8229 case 4:
8230 return DW_FORM_data4;
8231 case 8:
8232 return DW_FORM_data8;
8233 default:
8234 gcc_unreachable ();
8236 case dw_val_class_loc:
8237 if (dwarf_version >= 4)
8238 return DW_FORM_exprloc;
8239 switch (constant_size (size_of_locs (AT_loc (a))))
8241 case 1:
8242 return DW_FORM_block1;
8243 case 2:
8244 return DW_FORM_block2;
8245 case 4:
8246 return DW_FORM_block4;
8247 default:
8248 gcc_unreachable ();
8250 case dw_val_class_const:
8251 return DW_FORM_sdata;
8252 case dw_val_class_unsigned_const:
8253 switch (constant_size (AT_unsigned (a)))
8255 case 1:
8256 return DW_FORM_data1;
8257 case 2:
8258 return DW_FORM_data2;
8259 case 4:
8260 /* In DWARF3 DW_AT_data_member_location with
8261 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
8262 constant, so we need to use DW_FORM_udata if we need
8263 a large constant. */
8264 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8265 return DW_FORM_udata;
8266 return DW_FORM_data4;
8267 case 8:
8268 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8269 return DW_FORM_udata;
8270 return DW_FORM_data8;
8271 default:
8272 gcc_unreachable ();
8274 case dw_val_class_const_double:
8275 switch (HOST_BITS_PER_WIDE_INT)
8277 case 8:
8278 return DW_FORM_data2;
8279 case 16:
8280 return DW_FORM_data4;
8281 case 32:
8282 return DW_FORM_data8;
8283 case 64:
8284 default:
8285 return DW_FORM_block1;
8287 case dw_val_class_wide_int:
8288 switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
8290 case 8:
8291 return DW_FORM_data1;
8292 case 16:
8293 return DW_FORM_data2;
8294 case 32:
8295 return DW_FORM_data4;
8296 case 64:
8297 return DW_FORM_data8;
8298 default:
8299 return DW_FORM_block1;
8301 case dw_val_class_vec:
8302 switch (constant_size (a->dw_attr_val.v.val_vec.length
8303 * a->dw_attr_val.v.val_vec.elt_size))
8305 case 1:
8306 return DW_FORM_block1;
8307 case 2:
8308 return DW_FORM_block2;
8309 case 4:
8310 return DW_FORM_block4;
8311 default:
8312 gcc_unreachable ();
8314 case dw_val_class_flag:
8315 if (dwarf_version >= 4)
8317 /* Currently all add_AT_flag calls pass in 1 as last argument,
8318 so DW_FORM_flag_present can be used. If that ever changes,
8319 we'll need to use DW_FORM_flag and have some optimization
8320 in build_abbrev_table that will change those to
8321 DW_FORM_flag_present if it is set to 1 in all DIEs using
8322 the same abbrev entry. */
8323 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8324 return DW_FORM_flag_present;
8326 return DW_FORM_flag;
8327 case dw_val_class_die_ref:
8328 if (AT_ref_external (a))
8329 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
8330 else
8331 return DW_FORM_ref;
8332 case dw_val_class_fde_ref:
8333 return DW_FORM_data;
8334 case dw_val_class_lbl_id:
8335 return (AT_index (a) == NOT_INDEXED
8336 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8337 case dw_val_class_lineptr:
8338 case dw_val_class_macptr:
8339 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
8340 case dw_val_class_str:
8341 return AT_string_form (a);
8342 case dw_val_class_file:
8343 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8345 case 1:
8346 return DW_FORM_data1;
8347 case 2:
8348 return DW_FORM_data2;
8349 case 4:
8350 return DW_FORM_data4;
8351 default:
8352 gcc_unreachable ();
8355 case dw_val_class_data8:
8356 return DW_FORM_data8;
8358 case dw_val_class_high_pc:
8359 switch (DWARF2_ADDR_SIZE)
8361 case 1:
8362 return DW_FORM_data1;
8363 case 2:
8364 return DW_FORM_data2;
8365 case 4:
8366 return DW_FORM_data4;
8367 case 8:
8368 return DW_FORM_data8;
8369 default:
8370 gcc_unreachable ();
8373 default:
8374 gcc_unreachable ();
8378 /* Output the encoding of an attribute value. */
8380 static void
8381 output_value_format (dw_attr_ref a)
8383 enum dwarf_form form = value_format (a);
8385 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8388 /* Given a die and id, produce the appropriate abbreviations. */
8390 static void
8391 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
8393 unsigned ix;
8394 dw_attr_ref a_attr;
8396 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8397 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8398 dwarf_tag_name (abbrev->die_tag));
8400 if (abbrev->die_child != NULL)
8401 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8402 else
8403 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8405 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
8407 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8408 dwarf_attr_name (a_attr->dw_attr));
8409 output_value_format (a_attr);
8412 dw2_asm_output_data (1, 0, NULL);
8413 dw2_asm_output_data (1, 0, NULL);
8417 /* Output the .debug_abbrev section which defines the DIE abbreviation
8418 table. */
8420 static void
8421 output_abbrev_section (void)
8423 unsigned long abbrev_id;
8425 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8426 output_die_abbrevs (abbrev_id, abbrev_die_table[abbrev_id]);
8428 /* Terminate the table. */
8429 dw2_asm_output_data (1, 0, NULL);
8432 /* Output a symbol we can use to refer to this DIE from another CU. */
8434 static inline void
8435 output_die_symbol (dw_die_ref die)
8437 const char *sym = die->die_id.die_symbol;
8439 gcc_assert (!die->comdat_type_p);
8441 if (sym == 0)
8442 return;
8444 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8445 /* We make these global, not weak; if the target doesn't support
8446 .linkonce, it doesn't support combining the sections, so debugging
8447 will break. */
8448 targetm.asm_out.globalize_label (asm_out_file, sym);
8450 ASM_OUTPUT_LABEL (asm_out_file, sym);
8453 /* Return a new location list, given the begin and end range, and the
8454 expression. */
8456 static inline dw_loc_list_ref
8457 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8458 const char *section)
8460 dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
8462 retlist->begin = begin;
8463 retlist->begin_entry = NULL;
8464 retlist->end = end;
8465 retlist->expr = expr;
8466 retlist->section = section;
8468 return retlist;
8471 /* Generate a new internal symbol for this location list node, if it
8472 hasn't got one yet. */
8474 static inline void
8475 gen_llsym (dw_loc_list_ref list)
8477 gcc_assert (!list->ll_symbol);
8478 list->ll_symbol = gen_internal_sym ("LLST");
8481 /* Output the location list given to us. */
8483 static void
8484 output_loc_list (dw_loc_list_ref list_head)
8486 dw_loc_list_ref curr = list_head;
8488 if (list_head->emitted)
8489 return;
8490 list_head->emitted = true;
8492 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8494 /* Walk the location list, and output each range + expression. */
8495 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8497 unsigned long size;
8498 /* Don't output an entry that starts and ends at the same address. */
8499 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
8500 continue;
8501 size = size_of_locs (curr->expr);
8502 /* If the expression is too large, drop it on the floor. We could
8503 perhaps put it into DW_TAG_dwarf_procedure and refer to that
8504 in the expression, but >= 64KB expressions for a single value
8505 in a single range are unlikely very useful. */
8506 if (size > 0xffff)
8507 continue;
8508 if (dwarf_split_debug_info)
8510 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
8511 "Location list start/length entry (%s)",
8512 list_head->ll_symbol);
8513 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
8514 "Location list range start index (%s)",
8515 curr->begin);
8516 /* The length field is 4 bytes. If we ever need to support
8517 an 8-byte length, we can add a new DW_LLE code or fall back
8518 to DW_LLE_GNU_start_end_entry. */
8519 dw2_asm_output_delta (4, curr->end, curr->begin,
8520 "Location list range length (%s)",
8521 list_head->ll_symbol);
8523 else if (!have_multiple_function_sections)
8525 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8526 "Location list begin address (%s)",
8527 list_head->ll_symbol);
8528 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8529 "Location list end address (%s)",
8530 list_head->ll_symbol);
8532 else
8534 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8535 "Location list begin address (%s)",
8536 list_head->ll_symbol);
8537 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8538 "Location list end address (%s)",
8539 list_head->ll_symbol);
8542 /* Output the block length for this list of location operations. */
8543 gcc_assert (size <= 0xffff);
8544 dw2_asm_output_data (2, size, "%s", "Location expression size");
8546 output_loc_sequence (curr->expr, -1);
8549 if (dwarf_split_debug_info)
8550 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
8551 "Location list terminator (%s)",
8552 list_head->ll_symbol);
8553 else
8555 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8556 "Location list terminator begin (%s)",
8557 list_head->ll_symbol);
8558 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8559 "Location list terminator end (%s)",
8560 list_head->ll_symbol);
8564 /* Output a range_list offset into the debug_range section. Emit a
8565 relocated reference if val_entry is NULL, otherwise, emit an
8566 indirect reference. */
8568 static void
8569 output_range_list_offset (dw_attr_ref a)
8571 const char *name = dwarf_attr_name (a->dw_attr);
8573 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
8575 char *p = strchr (ranges_section_label, '\0');
8576 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX, a->dw_attr_val.v.val_offset);
8577 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8578 debug_ranges_section, "%s", name);
8579 *p = '\0';
8581 else
8582 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8583 "%s (offset from %s)", name, ranges_section_label);
8586 /* Output the offset into the debug_loc section. */
8588 static void
8589 output_loc_list_offset (dw_attr_ref a)
8591 char *sym = AT_loc_list (a)->ll_symbol;
8593 gcc_assert (sym);
8594 if (dwarf_split_debug_info)
8595 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
8596 "%s", dwarf_attr_name (a->dw_attr));
8597 else
8598 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8599 "%s", dwarf_attr_name (a->dw_attr));
8602 /* Output an attribute's index or value appropriately. */
8604 static void
8605 output_attr_index_or_value (dw_attr_ref a)
8607 const char *name = dwarf_attr_name (a->dw_attr);
8609 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8611 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
8612 return;
8614 switch (AT_class (a))
8616 case dw_val_class_addr:
8617 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8618 break;
8619 case dw_val_class_high_pc:
8620 case dw_val_class_lbl_id:
8621 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8622 break;
8623 case dw_val_class_loc_list:
8624 output_loc_list_offset (a);
8625 break;
8626 default:
8627 gcc_unreachable ();
8631 /* Output a type signature. */
8633 static inline void
8634 output_signature (const char *sig, const char *name)
8636 int i;
8638 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8639 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
8642 /* Output the DIE and its attributes. Called recursively to generate
8643 the definitions of each child DIE. */
8645 static void
8646 output_die (dw_die_ref die)
8648 dw_attr_ref a;
8649 dw_die_ref c;
8650 unsigned long size;
8651 unsigned ix;
8653 /* If someone in another CU might refer to us, set up a symbol for
8654 them to point to. */
8655 if (! die->comdat_type_p && die->die_id.die_symbol)
8656 output_die_symbol (die);
8658 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
8659 (unsigned long)die->die_offset,
8660 dwarf_tag_name (die->die_tag));
8662 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8664 const char *name = dwarf_attr_name (a->dw_attr);
8666 switch (AT_class (a))
8668 case dw_val_class_addr:
8669 output_attr_index_or_value (a);
8670 break;
8672 case dw_val_class_offset:
8673 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8674 "%s", name);
8675 break;
8677 case dw_val_class_range_list:
8678 output_range_list_offset (a);
8679 break;
8681 case dw_val_class_loc:
8682 size = size_of_locs (AT_loc (a));
8684 /* Output the block length for this list of location operations. */
8685 if (dwarf_version >= 4)
8686 dw2_asm_output_data_uleb128 (size, "%s", name);
8687 else
8688 dw2_asm_output_data (constant_size (size), size, "%s", name);
8690 output_loc_sequence (AT_loc (a), -1);
8691 break;
8693 case dw_val_class_const:
8694 /* ??? It would be slightly more efficient to use a scheme like is
8695 used for unsigned constants below, but gdb 4.x does not sign
8696 extend. Gdb 5.x does sign extend. */
8697 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8698 break;
8700 case dw_val_class_unsigned_const:
8702 int csize = constant_size (AT_unsigned (a));
8703 if (dwarf_version == 3
8704 && a->dw_attr == DW_AT_data_member_location
8705 && csize >= 4)
8706 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
8707 else
8708 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
8710 break;
8712 case dw_val_class_const_double:
8714 unsigned HOST_WIDE_INT first, second;
8716 if (HOST_BITS_PER_WIDE_INT >= 64)
8717 dw2_asm_output_data (1,
8718 HOST_BITS_PER_DOUBLE_INT
8719 / HOST_BITS_PER_CHAR,
8720 NULL);
8722 if (WORDS_BIG_ENDIAN)
8724 first = a->dw_attr_val.v.val_double.high;
8725 second = a->dw_attr_val.v.val_double.low;
8727 else
8729 first = a->dw_attr_val.v.val_double.low;
8730 second = a->dw_attr_val.v.val_double.high;
8733 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8734 first, "%s", name);
8735 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8736 second, NULL);
8738 break;
8740 case dw_val_class_wide_int:
8742 int i;
8743 int len = get_full_len (*a->dw_attr_val.v.val_wide);
8744 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
8745 if (len * HOST_BITS_PER_WIDE_INT > 64)
8746 dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide) * l,
8747 NULL);
8749 if (WORDS_BIG_ENDIAN)
8750 for (i = len - 1; i >= 0; --i)
8752 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
8753 name);
8754 name = NULL;
8756 else
8757 for (i = 0; i < len; ++i)
8759 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
8760 name);
8761 name = NULL;
8764 break;
8766 case dw_val_class_vec:
8768 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8769 unsigned int len = a->dw_attr_val.v.val_vec.length;
8770 unsigned int i;
8771 unsigned char *p;
8773 dw2_asm_output_data (constant_size (len * elt_size),
8774 len * elt_size, "%s", name);
8775 if (elt_size > sizeof (HOST_WIDE_INT))
8777 elt_size /= 2;
8778 len *= 2;
8780 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8781 i < len;
8782 i++, p += elt_size)
8783 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8784 "fp or vector constant word %u", i);
8785 break;
8788 case dw_val_class_flag:
8789 if (dwarf_version >= 4)
8791 /* Currently all add_AT_flag calls pass in 1 as last argument,
8792 so DW_FORM_flag_present can be used. If that ever changes,
8793 we'll need to use DW_FORM_flag and have some optimization
8794 in build_abbrev_table that will change those to
8795 DW_FORM_flag_present if it is set to 1 in all DIEs using
8796 the same abbrev entry. */
8797 gcc_assert (AT_flag (a) == 1);
8798 if (flag_debug_asm)
8799 fprintf (asm_out_file, "\t\t\t%s %s\n",
8800 ASM_COMMENT_START, name);
8801 break;
8803 dw2_asm_output_data (1, AT_flag (a), "%s", name);
8804 break;
8806 case dw_val_class_loc_list:
8807 output_attr_index_or_value (a);
8808 break;
8810 case dw_val_class_die_ref:
8811 if (AT_ref_external (a))
8813 if (AT_ref (a)->comdat_type_p)
8815 comdat_type_node_ref type_node =
8816 AT_ref (a)->die_id.die_type_node;
8818 gcc_assert (type_node);
8819 output_signature (type_node->signature, name);
8821 else
8823 const char *sym = AT_ref (a)->die_id.die_symbol;
8824 int size;
8826 gcc_assert (sym);
8827 /* In DWARF2, DW_FORM_ref_addr is sized by target address
8828 length, whereas in DWARF3 it's always sized as an
8829 offset. */
8830 if (dwarf_version == 2)
8831 size = DWARF2_ADDR_SIZE;
8832 else
8833 size = DWARF_OFFSET_SIZE;
8834 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
8835 name);
8838 else
8840 gcc_assert (AT_ref (a)->die_offset);
8841 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8842 "%s", name);
8844 break;
8846 case dw_val_class_fde_ref:
8848 char l1[20];
8850 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
8851 a->dw_attr_val.v.val_fde_index * 2);
8852 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
8853 "%s", name);
8855 break;
8857 case dw_val_class_vms_delta:
8858 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
8859 AT_vms_delta2 (a), AT_vms_delta1 (a),
8860 "%s", name);
8861 break;
8863 case dw_val_class_lbl_id:
8864 output_attr_index_or_value (a);
8865 break;
8867 case dw_val_class_lineptr:
8868 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8869 debug_line_section, "%s", name);
8870 break;
8872 case dw_val_class_macptr:
8873 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8874 debug_macinfo_section, "%s", name);
8875 break;
8877 case dw_val_class_str:
8878 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
8879 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8880 a->dw_attr_val.v.val_str->label,
8881 debug_str_section,
8882 "%s: \"%s\"", name, AT_string (a));
8883 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
8884 dw2_asm_output_data_uleb128 (AT_index (a),
8885 "%s: \"%s\"", name, AT_string (a));
8886 else
8887 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
8888 break;
8890 case dw_val_class_file:
8892 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
8894 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
8895 a->dw_attr_val.v.val_file->filename);
8896 break;
8899 case dw_val_class_data8:
8901 int i;
8903 for (i = 0; i < 8; i++)
8904 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
8905 i == 0 ? "%s" : NULL, name);
8906 break;
8909 case dw_val_class_high_pc:
8910 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
8911 get_AT_low_pc (die), "DW_AT_high_pc");
8912 break;
8914 default:
8915 gcc_unreachable ();
8919 FOR_EACH_CHILD (die, c, output_die (c));
8921 /* Add null byte to terminate sibling list. */
8922 if (die->die_child != NULL)
8923 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
8924 (unsigned long) die->die_offset);
8927 /* Output the compilation unit that appears at the beginning of the
8928 .debug_info section, and precedes the DIE descriptions. */
8930 static void
8931 output_compilation_unit_header (void)
8933 int ver = dwarf_version;
8935 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8936 dw2_asm_output_data (4, 0xffffffff,
8937 "Initial length escape value indicating 64-bit DWARF extension");
8938 dw2_asm_output_data (DWARF_OFFSET_SIZE,
8939 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
8940 "Length of Compilation Unit Info");
8941 dw2_asm_output_data (2, ver, "DWARF version number");
8942 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
8943 debug_abbrev_section,
8944 "Offset Into Abbrev. Section");
8945 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8948 /* Output the compilation unit DIE and its children. */
8950 static void
8951 output_comp_unit (dw_die_ref die, int output_if_empty)
8953 const char *secname, *oldsym;
8954 char *tmp;
8956 /* Unless we are outputting main CU, we may throw away empty ones. */
8957 if (!output_if_empty && die->die_child == NULL)
8958 return;
8960 /* Even if there are no children of this DIE, we must output the information
8961 about the compilation unit. Otherwise, on an empty translation unit, we
8962 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
8963 will then complain when examining the file. First mark all the DIEs in
8964 this CU so we know which get local refs. */
8965 mark_dies (die);
8967 external_ref_hash_type *extern_map = optimize_external_refs (die);
8969 build_abbrev_table (die, extern_map);
8971 delete extern_map;
8973 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
8974 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8975 calc_die_sizes (die);
8977 oldsym = die->die_id.die_symbol;
8978 if (oldsym)
8980 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
8982 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
8983 secname = tmp;
8984 die->die_id.die_symbol = NULL;
8985 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8987 else
8989 switch_to_section (debug_info_section);
8990 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
8991 info_section_emitted = true;
8994 /* Output debugging information. */
8995 output_compilation_unit_header ();
8996 output_die (die);
8998 /* Leave the marks on the main CU, so we can check them in
8999 output_pubnames. */
9000 if (oldsym)
9002 unmark_dies (die);
9003 die->die_id.die_symbol = oldsym;
9007 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
9008 and .debug_pubtypes. This is configured per-target, but can be
9009 overridden by the -gpubnames or -gno-pubnames options. */
9011 static inline bool
9012 want_pubnames (void)
9014 if (debug_info_level <= DINFO_LEVEL_TERSE)
9015 return false;
9016 if (debug_generate_pub_sections != -1)
9017 return debug_generate_pub_sections;
9018 return targetm.want_debug_pub_sections;
9021 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
9023 static void
9024 add_AT_pubnames (dw_die_ref die)
9026 if (want_pubnames ())
9027 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
9030 /* Add a string attribute value to a skeleton DIE. */
9032 static inline void
9033 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
9034 const char *str)
9036 dw_attr_node attr;
9037 struct indirect_string_node *node;
9039 if (! skeleton_debug_str_hash)
9040 skeleton_debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
9041 debug_str_eq, NULL);
9043 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
9044 find_string_form (node);
9045 if (node->form == DW_FORM_GNU_str_index)
9046 node->form = DW_FORM_strp;
9048 attr.dw_attr = attr_kind;
9049 attr.dw_attr_val.val_class = dw_val_class_str;
9050 attr.dw_attr_val.val_entry = NULL;
9051 attr.dw_attr_val.v.val_str = node;
9052 add_dwarf_attr (die, &attr);
9055 /* Helper function to generate top-level dies for skeleton debug_info and
9056 debug_types. */
9058 static void
9059 add_top_level_skeleton_die_attrs (dw_die_ref die)
9061 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
9062 const char *comp_dir = comp_dir_string ();
9064 add_skeleton_AT_string (die, DW_AT_GNU_dwo_name, dwo_file_name);
9065 if (comp_dir != NULL)
9066 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
9067 add_AT_pubnames (die);
9068 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
9071 /* Output skeleton debug sections that point to the dwo file. */
9073 static void
9074 output_skeleton_debug_sections (dw_die_ref comp_unit)
9076 /* These attributes will be found in the full debug_info section. */
9077 remove_AT (comp_unit, DW_AT_producer);
9078 remove_AT (comp_unit, DW_AT_language);
9080 switch_to_section (debug_skeleton_info_section);
9081 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
9083 /* Produce the skeleton compilation-unit header. This one differs enough from
9084 a normal CU header that it's better not to call output_compilation_unit
9085 header. */
9086 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9087 dw2_asm_output_data (4, 0xffffffff,
9088 "Initial length escape value indicating 64-bit DWARF extension");
9090 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9091 DWARF_COMPILE_UNIT_HEADER_SIZE
9092 - DWARF_INITIAL_LENGTH_SIZE
9093 + size_of_die (comp_unit),
9094 "Length of Compilation Unit Info");
9095 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
9096 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
9097 debug_abbrev_section,
9098 "Offset Into Abbrev. Section");
9099 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9101 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
9102 output_die (comp_unit);
9104 /* Build the skeleton debug_abbrev section. */
9105 switch_to_section (debug_skeleton_abbrev_section);
9106 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
9108 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
9110 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
9113 /* Output a comdat type unit DIE and its children. */
9115 static void
9116 output_comdat_type_unit (comdat_type_node *node)
9118 const char *secname;
9119 char *tmp;
9120 int i;
9121 #if defined (OBJECT_FORMAT_ELF)
9122 tree comdat_key;
9123 #endif
9125 /* First mark all the DIEs in this CU so we know which get local refs. */
9126 mark_dies (node->root_die);
9128 external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
9130 build_abbrev_table (node->root_die, extern_map);
9132 delete extern_map;
9133 extern_map = NULL;
9135 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
9136 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
9137 calc_die_sizes (node->root_die);
9139 #if defined (OBJECT_FORMAT_ELF)
9140 if (!dwarf_split_debug_info)
9141 secname = ".debug_types";
9142 else
9143 secname = ".debug_types.dwo";
9145 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9146 sprintf (tmp, "wt.");
9147 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9148 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
9149 comdat_key = get_identifier (tmp);
9150 targetm.asm_out.named_section (secname,
9151 SECTION_DEBUG | SECTION_LINKONCE,
9152 comdat_key);
9153 #else
9154 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9155 sprintf (tmp, ".gnu.linkonce.wt.");
9156 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9157 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
9158 secname = tmp;
9159 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9160 #endif
9162 /* Output debugging information. */
9163 output_compilation_unit_header ();
9164 output_signature (node->signature, "Type Signature");
9165 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
9166 "Offset to Type DIE");
9167 output_die (node->root_die);
9169 unmark_dies (node->root_die);
9172 /* Return the DWARF2/3 pubname associated with a decl. */
9174 static const char *
9175 dwarf2_name (tree decl, int scope)
9177 if (DECL_NAMELESS (decl))
9178 return NULL;
9179 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
9182 /* Add a new entry to .debug_pubnames if appropriate. */
9184 static void
9185 add_pubname_string (const char *str, dw_die_ref die)
9187 pubname_entry e;
9189 e.die = die;
9190 e.name = xstrdup (str);
9191 vec_safe_push (pubname_table, e);
9194 static void
9195 add_pubname (tree decl, dw_die_ref die)
9197 if (!want_pubnames ())
9198 return;
9200 /* Don't add items to the table when we expect that the consumer will have
9201 just read the enclosing die. For example, if the consumer is looking at a
9202 class_member, it will either be inside the class already, or will have just
9203 looked up the class to find the member. Either way, searching the class is
9204 faster than searching the index. */
9205 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
9206 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9208 const char *name = dwarf2_name (decl, 1);
9210 if (name)
9211 add_pubname_string (name, die);
9215 /* Add an enumerator to the pubnames section. */
9217 static void
9218 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
9220 pubname_entry e;
9222 gcc_assert (scope_name);
9223 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
9224 e.die = die;
9225 vec_safe_push (pubname_table, e);
9228 /* Add a new entry to .debug_pubtypes if appropriate. */
9230 static void
9231 add_pubtype (tree decl, dw_die_ref die)
9233 pubname_entry e;
9235 if (!want_pubnames ())
9236 return;
9238 if ((TREE_PUBLIC (decl)
9239 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9240 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
9242 tree scope = NULL;
9243 const char *scope_name = "";
9244 const char *sep = is_cxx () ? "::" : ".";
9245 const char *name;
9247 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
9248 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
9250 scope_name = lang_hooks.dwarf_name (scope, 1);
9251 if (scope_name != NULL && scope_name[0] != '\0')
9252 scope_name = concat (scope_name, sep, NULL);
9253 else
9254 scope_name = "";
9257 if (TYPE_P (decl))
9258 name = type_tag (decl);
9259 else
9260 name = lang_hooks.dwarf_name (decl, 1);
9262 /* If we don't have a name for the type, there's no point in adding
9263 it to the table. */
9264 if (name != NULL && name[0] != '\0')
9266 e.die = die;
9267 e.name = concat (scope_name, name, NULL);
9268 vec_safe_push (pubtype_table, e);
9271 /* Although it might be more consistent to add the pubinfo for the
9272 enumerators as their dies are created, they should only be added if the
9273 enum type meets the criteria above. So rather than re-check the parent
9274 enum type whenever an enumerator die is created, just output them all
9275 here. This isn't protected by the name conditional because anonymous
9276 enums don't have names. */
9277 if (die->die_tag == DW_TAG_enumeration_type)
9279 dw_die_ref c;
9281 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
9286 /* Output a single entry in the pubnames table. */
9288 static void
9289 output_pubname (dw_offset die_offset, pubname_entry *entry)
9291 dw_die_ref die = entry->die;
9292 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
9294 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
9296 if (debug_generate_pub_sections == 2)
9298 /* This logic follows gdb's method for determining the value of the flag
9299 byte. */
9300 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
9301 switch (die->die_tag)
9303 case DW_TAG_typedef:
9304 case DW_TAG_base_type:
9305 case DW_TAG_subrange_type:
9306 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9307 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9308 break;
9309 case DW_TAG_enumerator:
9310 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9311 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9312 if (!is_cxx () && !is_java ())
9313 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9314 break;
9315 case DW_TAG_subprogram:
9316 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9317 GDB_INDEX_SYMBOL_KIND_FUNCTION);
9318 if (!is_ada ())
9319 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9320 break;
9321 case DW_TAG_constant:
9322 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9323 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9324 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9325 break;
9326 case DW_TAG_variable:
9327 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9328 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9329 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9330 break;
9331 case DW_TAG_namespace:
9332 case DW_TAG_imported_declaration:
9333 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9334 break;
9335 case DW_TAG_class_type:
9336 case DW_TAG_interface_type:
9337 case DW_TAG_structure_type:
9338 case DW_TAG_union_type:
9339 case DW_TAG_enumeration_type:
9340 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9341 if (!is_cxx () && !is_java ())
9342 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9343 break;
9344 default:
9345 /* An unusual tag. Leave the flag-byte empty. */
9346 break;
9348 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
9349 "GDB-index flags");
9352 dw2_asm_output_nstring (entry->name, -1, "external name");
9356 /* Output the public names table used to speed up access to externally
9357 visible names; or the public types table used to find type definitions. */
9359 static void
9360 output_pubnames (vec<pubname_entry, va_gc> *names)
9362 unsigned i;
9363 unsigned long pubnames_length = size_of_pubnames (names);
9364 pubname_ref pub;
9366 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9367 dw2_asm_output_data (4, 0xffffffff,
9368 "Initial length escape value indicating 64-bit DWARF extension");
9369 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length, "Pub Info Length");
9371 /* Version number for pubnames/pubtypes is independent of dwarf version. */
9372 dw2_asm_output_data (2, 2, "DWARF Version");
9374 if (dwarf_split_debug_info)
9375 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9376 debug_skeleton_info_section,
9377 "Offset of Compilation Unit Info");
9378 else
9379 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9380 debug_info_section,
9381 "Offset of Compilation Unit Info");
9382 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
9383 "Compilation Unit Length");
9385 FOR_EACH_VEC_ELT (*names, i, pub)
9387 if (include_pubname_in_output (names, pub))
9389 dw_offset die_offset = pub->die->die_offset;
9391 /* We shouldn't see pubnames for DIEs outside of the main CU. */
9392 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
9393 gcc_assert (pub->die->die_mark);
9395 /* If we're putting types in their own .debug_types sections,
9396 the .debug_pubtypes table will still point to the compile
9397 unit (not the type unit), so we want to use the offset of
9398 the skeleton DIE (if there is one). */
9399 if (pub->die->comdat_type_p && names == pubtype_table)
9401 comdat_type_node_ref type_node = pub->die->die_id.die_type_node;
9403 if (type_node != NULL)
9404 die_offset = (type_node->skeleton_die != NULL
9405 ? type_node->skeleton_die->die_offset
9406 : comp_unit_die ()->die_offset);
9409 output_pubname (die_offset, pub);
9413 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
9416 /* Output public names and types tables if necessary. */
9418 static void
9419 output_pubtables (void)
9421 if (!want_pubnames () || !info_section_emitted)
9422 return;
9424 switch_to_section (debug_pubnames_section);
9425 output_pubnames (pubname_table);
9426 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
9427 It shouldn't hurt to emit it always, since pure DWARF2 consumers
9428 simply won't look for the section. */
9429 switch_to_section (debug_pubtypes_section);
9430 output_pubnames (pubtype_table);
9434 /* Output the information that goes into the .debug_aranges table.
9435 Namely, define the beginning and ending address range of the
9436 text section generated for this compilation unit. */
9438 static void
9439 output_aranges (unsigned long aranges_length)
9441 unsigned i;
9443 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9444 dw2_asm_output_data (4, 0xffffffff,
9445 "Initial length escape value indicating 64-bit DWARF extension");
9446 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
9447 "Length of Address Ranges Info");
9448 /* Version number for aranges is still 2, even in DWARF3. */
9449 dw2_asm_output_data (2, 2, "DWARF Version");
9450 if (dwarf_split_debug_info)
9451 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9452 debug_skeleton_info_section,
9453 "Offset of Compilation Unit Info");
9454 else
9455 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9456 debug_info_section,
9457 "Offset of Compilation Unit Info");
9458 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
9459 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
9461 /* We need to align to twice the pointer size here. */
9462 if (DWARF_ARANGES_PAD_SIZE)
9464 /* Pad using a 2 byte words so that padding is correct for any
9465 pointer size. */
9466 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
9467 2 * DWARF2_ADDR_SIZE);
9468 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
9469 dw2_asm_output_data (2, 0, NULL);
9472 /* It is necessary not to output these entries if the sections were
9473 not used; if the sections were not used, the length will be 0 and
9474 the address may end up as 0 if the section is discarded by ld
9475 --gc-sections, leaving an invalid (0, 0) entry that can be
9476 confused with the terminator. */
9477 if (text_section_used)
9479 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
9480 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
9481 text_section_label, "Length");
9483 if (cold_text_section_used)
9485 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
9486 "Address");
9487 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
9488 cold_text_section_label, "Length");
9491 if (have_multiple_function_sections)
9493 unsigned fde_idx;
9494 dw_fde_ref fde;
9496 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9498 if (DECL_IGNORED_P (fde->decl))
9499 continue;
9500 if (!fde->in_std_section)
9502 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
9503 "Address");
9504 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
9505 fde->dw_fde_begin, "Length");
9507 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9509 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
9510 "Address");
9511 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
9512 fde->dw_fde_second_begin, "Length");
9517 /* Output the terminator words. */
9518 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9519 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9522 /* Add a new entry to .debug_ranges. Return the offset at which it
9523 was placed. */
9525 static unsigned int
9526 add_ranges_num (int num)
9528 unsigned int in_use = ranges_table_in_use;
9530 if (in_use == ranges_table_allocated)
9532 ranges_table_allocated += RANGES_TABLE_INCREMENT;
9533 ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
9534 ranges_table_allocated);
9535 memset (ranges_table + ranges_table_in_use, 0,
9536 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
9539 ranges_table[in_use].num = num;
9540 ranges_table_in_use = in_use + 1;
9542 return in_use * 2 * DWARF2_ADDR_SIZE;
9545 /* Add a new entry to .debug_ranges corresponding to a block, or a
9546 range terminator if BLOCK is NULL. */
9548 static unsigned int
9549 add_ranges (const_tree block)
9551 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
9554 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
9555 When using dwarf_split_debug_info, address attributes in dies destined
9556 for the final executable should be direct references--setting the
9557 parameter force_direct ensures this behavior. */
9559 static void
9560 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
9561 bool *added, bool force_direct)
9563 unsigned int in_use = ranges_by_label_in_use;
9564 unsigned int offset;
9566 if (in_use == ranges_by_label_allocated)
9568 ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
9569 ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
9570 ranges_by_label,
9571 ranges_by_label_allocated);
9572 memset (ranges_by_label + ranges_by_label_in_use, 0,
9573 RANGES_TABLE_INCREMENT
9574 * sizeof (struct dw_ranges_by_label_struct));
9577 ranges_by_label[in_use].begin = begin;
9578 ranges_by_label[in_use].end = end;
9579 ranges_by_label_in_use = in_use + 1;
9581 offset = add_ranges_num (-(int)in_use - 1);
9582 if (!*added)
9584 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
9585 *added = true;
9589 static void
9590 output_ranges (void)
9592 unsigned i;
9593 static const char *const start_fmt = "Offset %#x";
9594 const char *fmt = start_fmt;
9596 for (i = 0; i < ranges_table_in_use; i++)
9598 int block_num = ranges_table[i].num;
9600 if (block_num > 0)
9602 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
9603 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
9605 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
9606 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
9608 /* If all code is in the text section, then the compilation
9609 unit base address defaults to DW_AT_low_pc, which is the
9610 base of the text section. */
9611 if (!have_multiple_function_sections)
9613 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
9614 text_section_label,
9615 fmt, i * 2 * DWARF2_ADDR_SIZE);
9616 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
9617 text_section_label, NULL);
9620 /* Otherwise, the compilation unit base address is zero,
9621 which allows us to use absolute addresses, and not worry
9622 about whether the target supports cross-section
9623 arithmetic. */
9624 else
9626 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
9627 fmt, i * 2 * DWARF2_ADDR_SIZE);
9628 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
9631 fmt = NULL;
9634 /* Negative block_num stands for an index into ranges_by_label. */
9635 else if (block_num < 0)
9637 int lab_idx = - block_num - 1;
9639 if (!have_multiple_function_sections)
9641 gcc_unreachable ();
9642 #if 0
9643 /* If we ever use add_ranges_by_labels () for a single
9644 function section, all we have to do is to take out
9645 the #if 0 above. */
9646 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9647 ranges_by_label[lab_idx].begin,
9648 text_section_label,
9649 fmt, i * 2 * DWARF2_ADDR_SIZE);
9650 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9651 ranges_by_label[lab_idx].end,
9652 text_section_label, NULL);
9653 #endif
9655 else
9657 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9658 ranges_by_label[lab_idx].begin,
9659 fmt, i * 2 * DWARF2_ADDR_SIZE);
9660 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9661 ranges_by_label[lab_idx].end,
9662 NULL);
9665 else
9667 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9668 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9669 fmt = start_fmt;
9674 /* Data structure containing information about input files. */
9675 struct file_info
9677 const char *path; /* Complete file name. */
9678 const char *fname; /* File name part. */
9679 int length; /* Length of entire string. */
9680 struct dwarf_file_data * file_idx; /* Index in input file table. */
9681 int dir_idx; /* Index in directory table. */
9684 /* Data structure containing information about directories with source
9685 files. */
9686 struct dir_info
9688 const char *path; /* Path including directory name. */
9689 int length; /* Path length. */
9690 int prefix; /* Index of directory entry which is a prefix. */
9691 int count; /* Number of files in this directory. */
9692 int dir_idx; /* Index of directory used as base. */
9695 /* Callback function for file_info comparison. We sort by looking at
9696 the directories in the path. */
9698 static int
9699 file_info_cmp (const void *p1, const void *p2)
9701 const struct file_info *const s1 = (const struct file_info *) p1;
9702 const struct file_info *const s2 = (const struct file_info *) p2;
9703 const unsigned char *cp1;
9704 const unsigned char *cp2;
9706 /* Take care of file names without directories. We need to make sure that
9707 we return consistent values to qsort since some will get confused if
9708 we return the same value when identical operands are passed in opposite
9709 orders. So if neither has a directory, return 0 and otherwise return
9710 1 or -1 depending on which one has the directory. */
9711 if ((s1->path == s1->fname || s2->path == s2->fname))
9712 return (s2->path == s2->fname) - (s1->path == s1->fname);
9714 cp1 = (const unsigned char *) s1->path;
9715 cp2 = (const unsigned char *) s2->path;
9717 while (1)
9719 ++cp1;
9720 ++cp2;
9721 /* Reached the end of the first path? If so, handle like above. */
9722 if ((cp1 == (const unsigned char *) s1->fname)
9723 || (cp2 == (const unsigned char *) s2->fname))
9724 return ((cp2 == (const unsigned char *) s2->fname)
9725 - (cp1 == (const unsigned char *) s1->fname));
9727 /* Character of current path component the same? */
9728 else if (*cp1 != *cp2)
9729 return *cp1 - *cp2;
9733 struct file_name_acquire_data
9735 struct file_info *files;
9736 int used_files;
9737 int max_files;
9740 /* Traversal function for the hash table. */
9742 static int
9743 file_name_acquire (void ** slot, void *data)
9745 struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
9746 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
9747 struct file_info *fi;
9748 const char *f;
9750 gcc_assert (fnad->max_files >= d->emitted_number);
9752 if (! d->emitted_number)
9753 return 1;
9755 gcc_assert (fnad->max_files != fnad->used_files);
9757 fi = fnad->files + fnad->used_files++;
9759 /* Skip all leading "./". */
9760 f = d->filename;
9761 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
9762 f += 2;
9764 /* Create a new array entry. */
9765 fi->path = f;
9766 fi->length = strlen (f);
9767 fi->file_idx = d;
9769 /* Search for the file name part. */
9770 f = strrchr (f, DIR_SEPARATOR);
9771 #if defined (DIR_SEPARATOR_2)
9773 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
9775 if (g != NULL)
9777 if (f == NULL || f < g)
9778 f = g;
9781 #endif
9783 fi->fname = f == NULL ? fi->path : f + 1;
9784 return 1;
9787 /* Output the directory table and the file name table. We try to minimize
9788 the total amount of memory needed. A heuristic is used to avoid large
9789 slowdowns with many input files. */
9791 static void
9792 output_file_names (void)
9794 struct file_name_acquire_data fnad;
9795 int numfiles;
9796 struct file_info *files;
9797 struct dir_info *dirs;
9798 int *saved;
9799 int *savehere;
9800 int *backmap;
9801 int ndirs;
9802 int idx_offset;
9803 int i;
9805 if (!last_emitted_file)
9807 dw2_asm_output_data (1, 0, "End directory table");
9808 dw2_asm_output_data (1, 0, "End file name table");
9809 return;
9812 numfiles = last_emitted_file->emitted_number;
9814 /* Allocate the various arrays we need. */
9815 files = XALLOCAVEC (struct file_info, numfiles);
9816 dirs = XALLOCAVEC (struct dir_info, numfiles);
9818 fnad.files = files;
9819 fnad.used_files = 0;
9820 fnad.max_files = numfiles;
9821 htab_traverse (file_table, file_name_acquire, &fnad);
9822 gcc_assert (fnad.used_files == fnad.max_files);
9824 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
9826 /* Find all the different directories used. */
9827 dirs[0].path = files[0].path;
9828 dirs[0].length = files[0].fname - files[0].path;
9829 dirs[0].prefix = -1;
9830 dirs[0].count = 1;
9831 dirs[0].dir_idx = 0;
9832 files[0].dir_idx = 0;
9833 ndirs = 1;
9835 for (i = 1; i < numfiles; i++)
9836 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
9837 && memcmp (dirs[ndirs - 1].path, files[i].path,
9838 dirs[ndirs - 1].length) == 0)
9840 /* Same directory as last entry. */
9841 files[i].dir_idx = ndirs - 1;
9842 ++dirs[ndirs - 1].count;
9844 else
9846 int j;
9848 /* This is a new directory. */
9849 dirs[ndirs].path = files[i].path;
9850 dirs[ndirs].length = files[i].fname - files[i].path;
9851 dirs[ndirs].count = 1;
9852 dirs[ndirs].dir_idx = ndirs;
9853 files[i].dir_idx = ndirs;
9855 /* Search for a prefix. */
9856 dirs[ndirs].prefix = -1;
9857 for (j = 0; j < ndirs; j++)
9858 if (dirs[j].length < dirs[ndirs].length
9859 && dirs[j].length > 1
9860 && (dirs[ndirs].prefix == -1
9861 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
9862 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
9863 dirs[ndirs].prefix = j;
9865 ++ndirs;
9868 /* Now to the actual work. We have to find a subset of the directories which
9869 allow expressing the file name using references to the directory table
9870 with the least amount of characters. We do not do an exhaustive search
9871 where we would have to check out every combination of every single
9872 possible prefix. Instead we use a heuristic which provides nearly optimal
9873 results in most cases and never is much off. */
9874 saved = XALLOCAVEC (int, ndirs);
9875 savehere = XALLOCAVEC (int, ndirs);
9877 memset (saved, '\0', ndirs * sizeof (saved[0]));
9878 for (i = 0; i < ndirs; i++)
9880 int j;
9881 int total;
9883 /* We can always save some space for the current directory. But this
9884 does not mean it will be enough to justify adding the directory. */
9885 savehere[i] = dirs[i].length;
9886 total = (savehere[i] - saved[i]) * dirs[i].count;
9888 for (j = i + 1; j < ndirs; j++)
9890 savehere[j] = 0;
9891 if (saved[j] < dirs[i].length)
9893 /* Determine whether the dirs[i] path is a prefix of the
9894 dirs[j] path. */
9895 int k;
9897 k = dirs[j].prefix;
9898 while (k != -1 && k != (int) i)
9899 k = dirs[k].prefix;
9901 if (k == (int) i)
9903 /* Yes it is. We can possibly save some memory by
9904 writing the filenames in dirs[j] relative to
9905 dirs[i]. */
9906 savehere[j] = dirs[i].length;
9907 total += (savehere[j] - saved[j]) * dirs[j].count;
9912 /* Check whether we can save enough to justify adding the dirs[i]
9913 directory. */
9914 if (total > dirs[i].length + 1)
9916 /* It's worthwhile adding. */
9917 for (j = i; j < ndirs; j++)
9918 if (savehere[j] > 0)
9920 /* Remember how much we saved for this directory so far. */
9921 saved[j] = savehere[j];
9923 /* Remember the prefix directory. */
9924 dirs[j].dir_idx = i;
9929 /* Emit the directory name table. */
9930 idx_offset = dirs[0].length > 0 ? 1 : 0;
9931 for (i = 1 - idx_offset; i < ndirs; i++)
9932 dw2_asm_output_nstring (dirs[i].path,
9933 dirs[i].length
9934 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
9935 "Directory Entry: %#x", i + idx_offset);
9937 dw2_asm_output_data (1, 0, "End directory table");
9939 /* We have to emit them in the order of emitted_number since that's
9940 used in the debug info generation. To do this efficiently we
9941 generate a back-mapping of the indices first. */
9942 backmap = XALLOCAVEC (int, numfiles);
9943 for (i = 0; i < numfiles; i++)
9944 backmap[files[i].file_idx->emitted_number - 1] = i;
9946 /* Now write all the file names. */
9947 for (i = 0; i < numfiles; i++)
9949 int file_idx = backmap[i];
9950 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
9952 #ifdef VMS_DEBUGGING_INFO
9953 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
9955 /* Setting these fields can lead to debugger miscomparisons,
9956 but VMS Debug requires them to be set correctly. */
9958 int ver;
9959 long long cdt;
9960 long siz;
9961 int maxfilelen = strlen (files[file_idx].path)
9962 + dirs[dir_idx].length
9963 + MAX_VMS_VERSION_LEN + 1;
9964 char *filebuf = XALLOCAVEC (char, maxfilelen);
9966 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
9967 snprintf (filebuf, maxfilelen, "%s;%d",
9968 files[file_idx].path + dirs[dir_idx].length, ver);
9970 dw2_asm_output_nstring
9971 (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
9973 /* Include directory index. */
9974 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9976 /* Modification time. */
9977 dw2_asm_output_data_uleb128
9978 ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
9979 ? cdt : 0,
9980 NULL);
9982 /* File length in bytes. */
9983 dw2_asm_output_data_uleb128
9984 ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
9985 ? siz : 0,
9986 NULL);
9987 #else
9988 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
9989 "File Entry: %#x", (unsigned) i + 1);
9991 /* Include directory index. */
9992 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9994 /* Modification time. */
9995 dw2_asm_output_data_uleb128 (0, NULL);
9997 /* File length in bytes. */
9998 dw2_asm_output_data_uleb128 (0, NULL);
9999 #endif /* VMS_DEBUGGING_INFO */
10002 dw2_asm_output_data (1, 0, "End file name table");
10006 /* Output one line number table into the .debug_line section. */
10008 static void
10009 output_one_line_info_table (dw_line_info_table *table)
10011 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
10012 unsigned int current_line = 1;
10013 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
10014 dw_line_info_entry *ent;
10015 size_t i;
10017 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
10019 switch (ent->opcode)
10021 case LI_set_address:
10022 /* ??? Unfortunately, we have little choice here currently, and
10023 must always use the most general form. GCC does not know the
10024 address delta itself, so we can't use DW_LNS_advance_pc. Many
10025 ports do have length attributes which will give an upper bound
10026 on the address range. We could perhaps use length attributes
10027 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
10028 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
10030 /* This can handle any delta. This takes
10031 4+DWARF2_ADDR_SIZE bytes. */
10032 dw2_asm_output_data (1, 0, "set address %s", line_label);
10033 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10034 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10035 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
10036 break;
10038 case LI_set_line:
10039 if (ent->val == current_line)
10041 /* We still need to start a new row, so output a copy insn. */
10042 dw2_asm_output_data (1, DW_LNS_copy,
10043 "copy line %u", current_line);
10045 else
10047 int line_offset = ent->val - current_line;
10048 int line_delta = line_offset - DWARF_LINE_BASE;
10050 current_line = ent->val;
10051 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
10053 /* This can handle deltas from -10 to 234, using the current
10054 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
10055 This takes 1 byte. */
10056 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
10057 "line %u", current_line);
10059 else
10061 /* This can handle any delta. This takes at least 4 bytes,
10062 depending on the value being encoded. */
10063 dw2_asm_output_data (1, DW_LNS_advance_line,
10064 "advance to line %u", current_line);
10065 dw2_asm_output_data_sleb128 (line_offset, NULL);
10066 dw2_asm_output_data (1, DW_LNS_copy, NULL);
10069 break;
10071 case LI_set_file:
10072 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
10073 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10074 break;
10076 case LI_set_column:
10077 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
10078 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10079 break;
10081 case LI_negate_stmt:
10082 current_is_stmt = !current_is_stmt;
10083 dw2_asm_output_data (1, DW_LNS_negate_stmt,
10084 "is_stmt %d", current_is_stmt);
10085 break;
10087 case LI_set_prologue_end:
10088 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
10089 "set prologue end");
10090 break;
10092 case LI_set_epilogue_begin:
10093 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
10094 "set epilogue begin");
10095 break;
10097 case LI_set_discriminator:
10098 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
10099 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
10100 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
10101 dw2_asm_output_data_uleb128 (ent->val, NULL);
10102 break;
10106 /* Emit debug info for the address of the end of the table. */
10107 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
10108 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10109 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10110 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
10112 dw2_asm_output_data (1, 0, "end sequence");
10113 dw2_asm_output_data_uleb128 (1, NULL);
10114 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
10117 /* Output the source line number correspondence information. This
10118 information goes into the .debug_line section. */
10120 static void
10121 output_line_info (bool prologue_only)
10123 char l1[20], l2[20], p1[20], p2[20];
10124 int ver = dwarf_version;
10125 bool saw_one = false;
10126 int opc;
10128 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
10129 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
10130 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
10131 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
10133 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10134 dw2_asm_output_data (4, 0xffffffff,
10135 "Initial length escape value indicating 64-bit DWARF extension");
10136 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
10137 "Length of Source Line Info");
10138 ASM_OUTPUT_LABEL (asm_out_file, l1);
10140 dw2_asm_output_data (2, ver, "DWARF Version");
10141 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
10142 ASM_OUTPUT_LABEL (asm_out_file, p1);
10144 /* Define the architecture-dependent minimum instruction length (in bytes).
10145 In this implementation of DWARF, this field is used for information
10146 purposes only. Since GCC generates assembly language, we have no
10147 a priori knowledge of how many instruction bytes are generated for each
10148 source line, and therefore can use only the DW_LNE_set_address and
10149 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
10150 this as '1', which is "correct enough" for all architectures,
10151 and don't let the target override. */
10152 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
10154 if (ver >= 4)
10155 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
10156 "Maximum Operations Per Instruction");
10157 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
10158 "Default is_stmt_start flag");
10159 dw2_asm_output_data (1, DWARF_LINE_BASE,
10160 "Line Base Value (Special Opcodes)");
10161 dw2_asm_output_data (1, DWARF_LINE_RANGE,
10162 "Line Range Value (Special Opcodes)");
10163 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
10164 "Special Opcode Base");
10166 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
10168 int n_op_args;
10169 switch (opc)
10171 case DW_LNS_advance_pc:
10172 case DW_LNS_advance_line:
10173 case DW_LNS_set_file:
10174 case DW_LNS_set_column:
10175 case DW_LNS_fixed_advance_pc:
10176 case DW_LNS_set_isa:
10177 n_op_args = 1;
10178 break;
10179 default:
10180 n_op_args = 0;
10181 break;
10184 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
10185 opc, n_op_args);
10188 /* Write out the information about the files we use. */
10189 output_file_names ();
10190 ASM_OUTPUT_LABEL (asm_out_file, p2);
10191 if (prologue_only)
10193 /* Output the marker for the end of the line number info. */
10194 ASM_OUTPUT_LABEL (asm_out_file, l2);
10195 return;
10198 if (separate_line_info)
10200 dw_line_info_table *table;
10201 size_t i;
10203 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
10204 if (table->in_use)
10206 output_one_line_info_table (table);
10207 saw_one = true;
10210 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
10212 output_one_line_info_table (cold_text_section_line_info);
10213 saw_one = true;
10216 /* ??? Some Darwin linkers crash on a .debug_line section with no
10217 sequences. Further, merely a DW_LNE_end_sequence entry is not
10218 sufficient -- the address column must also be initialized.
10219 Make sure to output at least one set_address/end_sequence pair,
10220 choosing .text since that section is always present. */
10221 if (text_section_line_info->in_use || !saw_one)
10222 output_one_line_info_table (text_section_line_info);
10224 /* Output the marker for the end of the line number info. */
10225 ASM_OUTPUT_LABEL (asm_out_file, l2);
10228 /* Given a pointer to a tree node for some base type, return a pointer to
10229 a DIE that describes the given type.
10231 This routine must only be called for GCC type nodes that correspond to
10232 Dwarf base (fundamental) types. */
10234 static dw_die_ref
10235 base_type_die (tree type)
10237 dw_die_ref base_type_result;
10238 enum dwarf_type encoding;
10240 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
10241 return 0;
10243 /* If this is a subtype that should not be emitted as a subrange type,
10244 use the base type. See subrange_type_for_debug_p. */
10245 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
10246 type = TREE_TYPE (type);
10248 switch (TREE_CODE (type))
10250 case INTEGER_TYPE:
10251 if ((dwarf_version >= 4 || !dwarf_strict)
10252 && TYPE_NAME (type)
10253 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10254 && DECL_IS_BUILTIN (TYPE_NAME (type))
10255 && DECL_NAME (TYPE_NAME (type)))
10257 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
10258 if (strcmp (name, "char16_t") == 0
10259 || strcmp (name, "char32_t") == 0)
10261 encoding = DW_ATE_UTF;
10262 break;
10265 if (TYPE_STRING_FLAG (type))
10267 if (TYPE_UNSIGNED (type))
10268 encoding = DW_ATE_unsigned_char;
10269 else
10270 encoding = DW_ATE_signed_char;
10272 else if (TYPE_UNSIGNED (type))
10273 encoding = DW_ATE_unsigned;
10274 else
10275 encoding = DW_ATE_signed;
10276 break;
10278 case REAL_TYPE:
10279 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
10281 if (dwarf_version >= 3 || !dwarf_strict)
10282 encoding = DW_ATE_decimal_float;
10283 else
10284 encoding = DW_ATE_lo_user;
10286 else
10287 encoding = DW_ATE_float;
10288 break;
10290 case FIXED_POINT_TYPE:
10291 if (!(dwarf_version >= 3 || !dwarf_strict))
10292 encoding = DW_ATE_lo_user;
10293 else if (TYPE_UNSIGNED (type))
10294 encoding = DW_ATE_unsigned_fixed;
10295 else
10296 encoding = DW_ATE_signed_fixed;
10297 break;
10299 /* Dwarf2 doesn't know anything about complex ints, so use
10300 a user defined type for it. */
10301 case COMPLEX_TYPE:
10302 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
10303 encoding = DW_ATE_complex_float;
10304 else
10305 encoding = DW_ATE_lo_user;
10306 break;
10308 case BOOLEAN_TYPE:
10309 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
10310 encoding = DW_ATE_boolean;
10311 break;
10313 default:
10314 /* No other TREE_CODEs are Dwarf fundamental types. */
10315 gcc_unreachable ();
10318 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
10320 add_AT_unsigned (base_type_result, DW_AT_byte_size,
10321 int_size_in_bytes (type));
10322 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
10323 add_pubtype (type, base_type_result);
10325 return base_type_result;
10328 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
10329 named 'auto' in its type: return true for it, false otherwise. */
10331 static inline bool
10332 is_cxx_auto (tree type)
10334 if (is_cxx ())
10336 tree name = TYPE_IDENTIFIER (type);
10337 if (name == get_identifier ("auto")
10338 || name == get_identifier ("decltype(auto)"))
10339 return true;
10341 return false;
10344 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
10345 given input type is a Dwarf "fundamental" type. Otherwise return null. */
10347 static inline int
10348 is_base_type (tree type)
10350 switch (TREE_CODE (type))
10352 case ERROR_MARK:
10353 case VOID_TYPE:
10354 case INTEGER_TYPE:
10355 case REAL_TYPE:
10356 case FIXED_POINT_TYPE:
10357 case COMPLEX_TYPE:
10358 case BOOLEAN_TYPE:
10359 return 1;
10361 case ARRAY_TYPE:
10362 case RECORD_TYPE:
10363 case UNION_TYPE:
10364 case QUAL_UNION_TYPE:
10365 case ENUMERAL_TYPE:
10366 case FUNCTION_TYPE:
10367 case METHOD_TYPE:
10368 case POINTER_TYPE:
10369 case REFERENCE_TYPE:
10370 case NULLPTR_TYPE:
10371 case OFFSET_TYPE:
10372 case LANG_TYPE:
10373 case VECTOR_TYPE:
10374 return 0;
10376 default:
10377 if (is_cxx_auto (type))
10378 return 0;
10379 gcc_unreachable ();
10382 return 0;
10385 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
10386 node, return the size in bits for the type if it is a constant, or else
10387 return the alignment for the type if the type's size is not constant, or
10388 else return BITS_PER_WORD if the type actually turns out to be an
10389 ERROR_MARK node. */
10391 static inline unsigned HOST_WIDE_INT
10392 simple_type_size_in_bits (const_tree type)
10394 if (TREE_CODE (type) == ERROR_MARK)
10395 return BITS_PER_WORD;
10396 else if (TYPE_SIZE (type) == NULL_TREE)
10397 return 0;
10398 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
10399 return tree_to_uhwi (TYPE_SIZE (type));
10400 else
10401 return TYPE_ALIGN (type);
10404 /* Similarly, but return an offset_int instead of UHWI. */
10406 static inline offset_int
10407 offset_int_type_size_in_bits (const_tree type)
10409 if (TREE_CODE (type) == ERROR_MARK)
10410 return BITS_PER_WORD;
10411 else if (TYPE_SIZE (type) == NULL_TREE)
10412 return 0;
10413 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
10414 return wi::to_offset (TYPE_SIZE (type));
10415 else
10416 return TYPE_ALIGN (type);
10419 /* Given a pointer to a tree node for a subrange type, return a pointer
10420 to a DIE that describes the given type. */
10422 static dw_die_ref
10423 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
10425 dw_die_ref subrange_die;
10426 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
10428 if (context_die == NULL)
10429 context_die = comp_unit_die ();
10431 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
10433 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
10435 /* The size of the subrange type and its base type do not match,
10436 so we need to generate a size attribute for the subrange type. */
10437 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
10440 if (low)
10441 add_bound_info (subrange_die, DW_AT_lower_bound, low);
10442 if (high)
10443 add_bound_info (subrange_die, DW_AT_upper_bound, high);
10445 return subrange_die;
10448 /* Returns the (const and/or volatile) cv_qualifiers associated with
10449 the decl node. This will normally be augmented with the
10450 cv_qualifiers of the underlying type in add_type_attribute. */
10452 static int
10453 decl_quals (const_tree decl)
10455 return ((TREE_READONLY (decl)
10456 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
10457 | (TREE_THIS_VOLATILE (decl)
10458 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
10461 /* Determine the TYPE whose qualifiers match the largest strict subset
10462 of the given TYPE_QUALS, and return its qualifiers. Ignore all
10463 qualifiers outside QUAL_MASK. */
10465 static int
10466 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
10468 tree t;
10469 int best_rank = 0, best_qual = 0, max_rank;
10471 type_quals &= qual_mask;
10472 max_rank = popcount_hwi (type_quals) - 1;
10474 for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
10475 t = TYPE_NEXT_VARIANT (t))
10477 int q = TYPE_QUALS (t) & qual_mask;
10479 if ((q & type_quals) == q && q != type_quals
10480 && check_base_type (t, type))
10482 int rank = popcount_hwi (q);
10484 if (rank > best_rank)
10486 best_rank = rank;
10487 best_qual = q;
10492 return best_qual;
10495 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
10496 entry that chains various modifiers in front of the given type. */
10498 static dw_die_ref
10499 modified_type_die (tree type, int cv_quals, dw_die_ref context_die)
10501 enum tree_code code = TREE_CODE (type);
10502 dw_die_ref mod_type_die;
10503 dw_die_ref sub_die = NULL;
10504 tree item_type = NULL;
10505 tree qualified_type;
10506 tree name, low, high;
10507 dw_die_ref mod_scope;
10508 /* Only these cv-qualifiers are currently handled. */
10509 const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
10510 | TYPE_QUAL_RESTRICT);
10512 if (code == ERROR_MARK)
10513 return NULL;
10515 cv_quals &= cv_qual_mask;
10517 /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
10518 tag modifier (and not an attribute) old consumers won't be able
10519 to handle it. */
10520 if (dwarf_version < 3)
10521 cv_quals &= ~TYPE_QUAL_RESTRICT;
10523 /* See if we already have the appropriately qualified variant of
10524 this type. */
10525 qualified_type = get_qualified_type (type, cv_quals);
10527 if (qualified_type == sizetype
10528 && TYPE_NAME (qualified_type)
10529 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
10531 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
10533 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
10534 && TYPE_PRECISION (t)
10535 == TYPE_PRECISION (qualified_type)
10536 && TYPE_UNSIGNED (t)
10537 == TYPE_UNSIGNED (qualified_type));
10538 qualified_type = t;
10541 /* If we do, then we can just use its DIE, if it exists. */
10542 if (qualified_type)
10544 mod_type_die = lookup_type_die (qualified_type);
10545 if (mod_type_die)
10546 return mod_type_die;
10549 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
10551 /* Handle C typedef types. */
10552 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
10553 && !DECL_ARTIFICIAL (name))
10555 tree dtype = TREE_TYPE (name);
10557 if (qualified_type == dtype)
10559 /* For a named type, use the typedef. */
10560 gen_type_die (qualified_type, context_die);
10561 return lookup_type_die (qualified_type);
10563 else
10565 int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
10566 dquals &= cv_qual_mask;
10567 if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
10568 || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
10569 /* cv-unqualified version of named type. Just use
10570 the unnamed type to which it refers. */
10571 return modified_type_die (DECL_ORIGINAL_TYPE (name),
10572 cv_quals, context_die);
10573 /* Else cv-qualified version of named type; fall through. */
10577 mod_scope = scope_die_for (type, context_die);
10579 if (cv_quals)
10581 struct qual_info { int q; enum dwarf_tag t; };
10582 static const struct qual_info qual_info[] =
10584 { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
10585 { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
10586 { TYPE_QUAL_CONST, DW_TAG_const_type },
10588 int sub_quals;
10589 unsigned i;
10591 /* Determine a lesser qualified type that most closely matches
10592 this one. Then generate DW_TAG_* entries for the remaining
10593 qualifiers. */
10594 sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
10595 cv_qual_mask);
10596 mod_type_die = modified_type_die (type, sub_quals, context_die);
10598 for (i = 0; i < sizeof (qual_info) / sizeof (qual_info[0]); i++)
10599 if (qual_info[i].q & cv_quals & ~sub_quals)
10601 dw_die_ref d = new_die (qual_info[i].t, mod_scope, type);
10602 if (mod_type_die)
10603 add_AT_die_ref (d, DW_AT_type, mod_type_die);
10604 mod_type_die = d;
10607 else if (code == POINTER_TYPE)
10609 mod_type_die = new_die (DW_TAG_pointer_type, mod_scope, type);
10610 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10611 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10612 item_type = TREE_TYPE (type);
10613 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10614 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10615 TYPE_ADDR_SPACE (item_type));
10617 else if (code == REFERENCE_TYPE)
10619 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
10620 mod_type_die = new_die (DW_TAG_rvalue_reference_type, mod_scope,
10621 type);
10622 else
10623 mod_type_die = new_die (DW_TAG_reference_type, mod_scope, type);
10624 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10625 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10626 item_type = TREE_TYPE (type);
10627 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10628 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10629 TYPE_ADDR_SPACE (item_type));
10631 else if (code == INTEGER_TYPE
10632 && TREE_TYPE (type) != NULL_TREE
10633 && subrange_type_for_debug_p (type, &low, &high))
10635 mod_type_die = subrange_type_die (type, low, high, context_die);
10636 item_type = TREE_TYPE (type);
10638 else if (is_base_type (type))
10639 mod_type_die = base_type_die (type);
10640 else
10642 gen_type_die (type, context_die);
10644 /* We have to get the type_main_variant here (and pass that to the
10645 `lookup_type_die' routine) because the ..._TYPE node we have
10646 might simply be a *copy* of some original type node (where the
10647 copy was created to help us keep track of typedef names) and
10648 that copy might have a different TYPE_UID from the original
10649 ..._TYPE node. */
10650 if (TREE_CODE (type) != VECTOR_TYPE)
10651 return lookup_type_die (type_main_variant (type));
10652 else
10653 /* Vectors have the debugging information in the type,
10654 not the main variant. */
10655 return lookup_type_die (type);
10658 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
10659 don't output a DW_TAG_typedef, since there isn't one in the
10660 user's program; just attach a DW_AT_name to the type.
10661 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
10662 if the base type already has the same name. */
10663 if (name
10664 && ((TREE_CODE (name) != TYPE_DECL
10665 && (qualified_type == TYPE_MAIN_VARIANT (type)
10666 || (cv_quals == TYPE_UNQUALIFIED)))
10667 || (TREE_CODE (name) == TYPE_DECL
10668 && TREE_TYPE (name) == qualified_type
10669 && DECL_NAME (name))))
10671 if (TREE_CODE (name) == TYPE_DECL)
10672 /* Could just call add_name_and_src_coords_attributes here,
10673 but since this is a builtin type it doesn't have any
10674 useful source coordinates anyway. */
10675 name = DECL_NAME (name);
10676 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
10678 /* This probably indicates a bug. */
10679 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
10681 name = TYPE_IDENTIFIER (type);
10682 add_name_attribute (mod_type_die,
10683 name ? IDENTIFIER_POINTER (name) : "__unknown__");
10686 if (qualified_type)
10687 equate_type_number_to_die (qualified_type, mod_type_die);
10689 if (item_type)
10690 /* We must do this after the equate_type_number_to_die call, in case
10691 this is a recursive type. This ensures that the modified_type_die
10692 recursion will terminate even if the type is recursive. Recursive
10693 types are possible in Ada. */
10694 sub_die = modified_type_die (item_type,
10695 TYPE_QUALS_NO_ADDR_SPACE (item_type),
10696 context_die);
10698 if (sub_die != NULL)
10699 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
10701 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
10702 if (TYPE_ARTIFICIAL (type))
10703 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
10705 return mod_type_die;
10708 /* Generate DIEs for the generic parameters of T.
10709 T must be either a generic type or a generic function.
10710 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
10712 static void
10713 gen_generic_params_dies (tree t)
10715 tree parms, args;
10716 int parms_num, i;
10717 dw_die_ref die = NULL;
10718 int non_default;
10720 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
10721 return;
10723 if (TYPE_P (t))
10724 die = lookup_type_die (t);
10725 else if (DECL_P (t))
10726 die = lookup_decl_die (t);
10728 gcc_assert (die);
10730 parms = lang_hooks.get_innermost_generic_parms (t);
10731 if (!parms)
10732 /* T has no generic parameter. It means T is neither a generic type
10733 or function. End of story. */
10734 return;
10736 parms_num = TREE_VEC_LENGTH (parms);
10737 args = lang_hooks.get_innermost_generic_args (t);
10738 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
10739 non_default = int_cst_value (TREE_CHAIN (args));
10740 else
10741 non_default = TREE_VEC_LENGTH (args);
10742 for (i = 0; i < parms_num; i++)
10744 tree parm, arg, arg_pack_elems;
10745 dw_die_ref parm_die;
10747 parm = TREE_VEC_ELT (parms, i);
10748 arg = TREE_VEC_ELT (args, i);
10749 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
10750 gcc_assert (parm && TREE_VALUE (parm) && arg);
10752 if (parm && TREE_VALUE (parm) && arg)
10754 /* If PARM represents a template parameter pack,
10755 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
10756 by DW_TAG_template_*_parameter DIEs for the argument
10757 pack elements of ARG. Note that ARG would then be
10758 an argument pack. */
10759 if (arg_pack_elems)
10760 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
10761 arg_pack_elems,
10762 die);
10763 else
10764 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
10765 true /* emit name */, die);
10766 if (i >= non_default)
10767 add_AT_flag (parm_die, DW_AT_default_value, 1);
10772 /* Create and return a DIE for PARM which should be
10773 the representation of a generic type parameter.
10774 For instance, in the C++ front end, PARM would be a template parameter.
10775 ARG is the argument to PARM.
10776 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
10777 name of the PARM.
10778 PARENT_DIE is the parent DIE which the new created DIE should be added to,
10779 as a child node. */
10781 static dw_die_ref
10782 generic_parameter_die (tree parm, tree arg,
10783 bool emit_name_p,
10784 dw_die_ref parent_die)
10786 dw_die_ref tmpl_die = NULL;
10787 const char *name = NULL;
10789 if (!parm || !DECL_NAME (parm) || !arg)
10790 return NULL;
10792 /* We support non-type generic parameters and arguments,
10793 type generic parameters and arguments, as well as
10794 generic generic parameters (a.k.a. template template parameters in C++)
10795 and arguments. */
10796 if (TREE_CODE (parm) == PARM_DECL)
10797 /* PARM is a nontype generic parameter */
10798 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
10799 else if (TREE_CODE (parm) == TYPE_DECL)
10800 /* PARM is a type generic parameter. */
10801 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
10802 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10803 /* PARM is a generic generic parameter.
10804 Its DIE is a GNU extension. It shall have a
10805 DW_AT_name attribute to represent the name of the template template
10806 parameter, and a DW_AT_GNU_template_name attribute to represent the
10807 name of the template template argument. */
10808 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
10809 parent_die, parm);
10810 else
10811 gcc_unreachable ();
10813 if (tmpl_die)
10815 tree tmpl_type;
10817 /* If PARM is a generic parameter pack, it means we are
10818 emitting debug info for a template argument pack element.
10819 In other terms, ARG is a template argument pack element.
10820 In that case, we don't emit any DW_AT_name attribute for
10821 the die. */
10822 if (emit_name_p)
10824 name = IDENTIFIER_POINTER (DECL_NAME (parm));
10825 gcc_assert (name);
10826 add_AT_string (tmpl_die, DW_AT_name, name);
10829 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10831 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
10832 TMPL_DIE should have a child DW_AT_type attribute that is set
10833 to the type of the argument to PARM, which is ARG.
10834 If PARM is a type generic parameter, TMPL_DIE should have a
10835 child DW_AT_type that is set to ARG. */
10836 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
10837 add_type_attribute (tmpl_die, tmpl_type,
10838 (TREE_THIS_VOLATILE (tmpl_type)
10839 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
10840 parent_die);
10842 else
10844 /* So TMPL_DIE is a DIE representing a
10845 a generic generic template parameter, a.k.a template template
10846 parameter in C++ and arg is a template. */
10848 /* The DW_AT_GNU_template_name attribute of the DIE must be set
10849 to the name of the argument. */
10850 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
10851 if (name)
10852 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
10855 if (TREE_CODE (parm) == PARM_DECL)
10856 /* So PARM is a non-type generic parameter.
10857 DWARF3 5.6.8 says we must set a DW_AT_const_value child
10858 attribute of TMPL_DIE which value represents the value
10859 of ARG.
10860 We must be careful here:
10861 The value of ARG might reference some function decls.
10862 We might currently be emitting debug info for a generic
10863 type and types are emitted before function decls, we don't
10864 know if the function decls referenced by ARG will actually be
10865 emitted after cgraph computations.
10866 So must defer the generation of the DW_AT_const_value to
10867 after cgraph is ready. */
10868 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
10871 return tmpl_die;
10874 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
10875 PARM_PACK must be a template parameter pack. The returned DIE
10876 will be child DIE of PARENT_DIE. */
10878 static dw_die_ref
10879 template_parameter_pack_die (tree parm_pack,
10880 tree parm_pack_args,
10881 dw_die_ref parent_die)
10883 dw_die_ref die;
10884 int j;
10886 gcc_assert (parent_die && parm_pack);
10888 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
10889 add_name_and_src_coords_attributes (die, parm_pack);
10890 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
10891 generic_parameter_die (parm_pack,
10892 TREE_VEC_ELT (parm_pack_args, j),
10893 false /* Don't emit DW_AT_name */,
10894 die);
10895 return die;
10898 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
10899 an enumerated type. */
10901 static inline int
10902 type_is_enum (const_tree type)
10904 return TREE_CODE (type) == ENUMERAL_TYPE;
10907 /* Return the DBX register number described by a given RTL node. */
10909 static unsigned int
10910 dbx_reg_number (const_rtx rtl)
10912 unsigned regno = REGNO (rtl);
10914 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
10916 #ifdef LEAF_REG_REMAP
10917 if (crtl->uses_only_leaf_regs)
10919 int leaf_reg = LEAF_REG_REMAP (regno);
10920 if (leaf_reg != -1)
10921 regno = (unsigned) leaf_reg;
10923 #endif
10925 regno = DBX_REGISTER_NUMBER (regno);
10926 gcc_assert (regno != INVALID_REGNUM);
10927 return regno;
10930 /* Optionally add a DW_OP_piece term to a location description expression.
10931 DW_OP_piece is only added if the location description expression already
10932 doesn't end with DW_OP_piece. */
10934 static void
10935 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
10937 dw_loc_descr_ref loc;
10939 if (*list_head != NULL)
10941 /* Find the end of the chain. */
10942 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
10945 if (loc->dw_loc_opc != DW_OP_piece)
10946 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
10950 /* Return a location descriptor that designates a machine register or
10951 zero if there is none. */
10953 static dw_loc_descr_ref
10954 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
10956 rtx regs;
10958 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
10959 return 0;
10961 /* We only use "frame base" when we're sure we're talking about the
10962 post-prologue local stack frame. We do this by *not* running
10963 register elimination until this point, and recognizing the special
10964 argument pointer and soft frame pointer rtx's.
10965 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
10966 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
10967 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
10969 dw_loc_descr_ref result = NULL;
10971 if (dwarf_version >= 4 || !dwarf_strict)
10973 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
10974 initialized);
10975 if (result)
10976 add_loc_descr (&result,
10977 new_loc_descr (DW_OP_stack_value, 0, 0));
10979 return result;
10982 regs = targetm.dwarf_register_span (rtl);
10984 if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
10985 return multiple_reg_loc_descriptor (rtl, regs, initialized);
10986 else
10988 unsigned int dbx_regnum = dbx_reg_number (rtl);
10989 if (dbx_regnum == IGNORED_DWARF_REGNUM)
10990 return 0;
10991 return one_reg_loc_descriptor (dbx_regnum, initialized);
10995 /* Return a location descriptor that designates a machine register for
10996 a given hard register number. */
10998 static dw_loc_descr_ref
10999 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
11001 dw_loc_descr_ref reg_loc_descr;
11003 if (regno <= 31)
11004 reg_loc_descr
11005 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
11006 else
11007 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
11009 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11010 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11012 return reg_loc_descr;
11015 /* Given an RTL of a register, return a location descriptor that
11016 designates a value that spans more than one register. */
11018 static dw_loc_descr_ref
11019 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
11020 enum var_init_status initialized)
11022 int size, i;
11023 dw_loc_descr_ref loc_result = NULL;
11025 /* Simple, contiguous registers. */
11026 if (regs == NULL_RTX)
11028 unsigned reg = REGNO (rtl);
11029 int nregs;
11031 #ifdef LEAF_REG_REMAP
11032 if (crtl->uses_only_leaf_regs)
11034 int leaf_reg = LEAF_REG_REMAP (reg);
11035 if (leaf_reg != -1)
11036 reg = (unsigned) leaf_reg;
11038 #endif
11040 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
11041 nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
11043 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
11045 loc_result = NULL;
11046 while (nregs--)
11048 dw_loc_descr_ref t;
11050 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
11051 VAR_INIT_STATUS_INITIALIZED);
11052 add_loc_descr (&loc_result, t);
11053 add_loc_descr_op_piece (&loc_result, size);
11054 ++reg;
11056 return loc_result;
11059 /* Now onto stupid register sets in non contiguous locations. */
11061 gcc_assert (GET_CODE (regs) == PARALLEL);
11063 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
11064 loc_result = NULL;
11066 for (i = 0; i < XVECLEN (regs, 0); ++i)
11068 dw_loc_descr_ref t;
11070 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
11071 VAR_INIT_STATUS_INITIALIZED);
11072 add_loc_descr (&loc_result, t);
11073 add_loc_descr_op_piece (&loc_result, size);
11076 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
11077 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11078 return loc_result;
11081 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
11083 /* Return a location descriptor that designates a constant i,
11084 as a compound operation from constant (i >> shift), constant shift
11085 and DW_OP_shl. */
11087 static dw_loc_descr_ref
11088 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11090 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
11091 add_loc_descr (&ret, int_loc_descriptor (shift));
11092 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11093 return ret;
11096 /* Return a location descriptor that designates a constant. */
11098 static dw_loc_descr_ref
11099 int_loc_descriptor (HOST_WIDE_INT i)
11101 enum dwarf_location_atom op;
11103 /* Pick the smallest representation of a constant, rather than just
11104 defaulting to the LEB encoding. */
11105 if (i >= 0)
11107 int clz = clz_hwi (i);
11108 int ctz = ctz_hwi (i);
11109 if (i <= 31)
11110 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
11111 else if (i <= 0xff)
11112 op = DW_OP_const1u;
11113 else if (i <= 0xffff)
11114 op = DW_OP_const2u;
11115 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11116 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11117 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
11118 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
11119 while DW_OP_const4u is 5 bytes. */
11120 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
11121 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11122 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11123 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
11124 while DW_OP_const4u is 5 bytes. */
11125 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11126 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11127 op = DW_OP_const4u;
11128 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11129 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11130 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
11131 while DW_OP_constu of constant >= 0x100000000 takes at least
11132 6 bytes. */
11133 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11134 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11135 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
11136 >= HOST_BITS_PER_WIDE_INT)
11137 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
11138 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
11139 while DW_OP_constu takes in this case at least 6 bytes. */
11140 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
11141 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11142 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11143 && size_of_uleb128 (i) > 6)
11144 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
11145 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
11146 else
11147 op = DW_OP_constu;
11149 else
11151 if (i >= -0x80)
11152 op = DW_OP_const1s;
11153 else if (i >= -0x8000)
11154 op = DW_OP_const2s;
11155 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11157 if (size_of_int_loc_descriptor (i) < 5)
11159 dw_loc_descr_ref ret = int_loc_descriptor (-i);
11160 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11161 return ret;
11163 op = DW_OP_const4s;
11165 else
11167 if (size_of_int_loc_descriptor (i)
11168 < (unsigned long) 1 + size_of_sleb128 (i))
11170 dw_loc_descr_ref ret = int_loc_descriptor (-i);
11171 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11172 return ret;
11174 op = DW_OP_consts;
11178 return new_loc_descr (op, i, 0);
11181 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
11182 without actually allocating it. */
11184 static unsigned long
11185 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11187 return size_of_int_loc_descriptor (i >> shift)
11188 + size_of_int_loc_descriptor (shift)
11189 + 1;
11192 /* Return size_of_locs (int_loc_descriptor (i)) without
11193 actually allocating it. */
11195 static unsigned long
11196 size_of_int_loc_descriptor (HOST_WIDE_INT i)
11198 unsigned long s;
11200 if (i >= 0)
11202 int clz, ctz;
11203 if (i <= 31)
11204 return 1;
11205 else if (i <= 0xff)
11206 return 2;
11207 else if (i <= 0xffff)
11208 return 3;
11209 clz = clz_hwi (i);
11210 ctz = ctz_hwi (i);
11211 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11212 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11213 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11214 - clz - 5);
11215 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11216 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11217 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11218 - clz - 8);
11219 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11220 return 5;
11221 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
11222 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11223 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11224 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11225 - clz - 8);
11226 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11227 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
11228 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11229 - clz - 16);
11230 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11231 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11232 && s > 6)
11233 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11234 - clz - 32);
11235 else
11236 return 1 + s;
11238 else
11240 if (i >= -0x80)
11241 return 2;
11242 else if (i >= -0x8000)
11243 return 3;
11244 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11246 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11248 s = size_of_int_loc_descriptor (-i) + 1;
11249 if (s < 5)
11250 return s;
11252 return 5;
11254 else
11256 unsigned long r = 1 + size_of_sleb128 (i);
11257 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11259 s = size_of_int_loc_descriptor (-i) + 1;
11260 if (s < r)
11261 return s;
11263 return r;
11268 /* Return loc description representing "address" of integer value.
11269 This can appear only as toplevel expression. */
11271 static dw_loc_descr_ref
11272 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
11274 int litsize;
11275 dw_loc_descr_ref loc_result = NULL;
11277 if (!(dwarf_version >= 4 || !dwarf_strict))
11278 return NULL;
11280 litsize = size_of_int_loc_descriptor (i);
11281 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
11282 is more compact. For DW_OP_stack_value we need:
11283 litsize + 1 (DW_OP_stack_value)
11284 and for DW_OP_implicit_value:
11285 1 (DW_OP_implicit_value) + 1 (length) + size. */
11286 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
11288 loc_result = int_loc_descriptor (i);
11289 add_loc_descr (&loc_result,
11290 new_loc_descr (DW_OP_stack_value, 0, 0));
11291 return loc_result;
11294 loc_result = new_loc_descr (DW_OP_implicit_value,
11295 size, 0);
11296 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
11297 loc_result->dw_loc_oprnd2.v.val_int = i;
11298 return loc_result;
11301 /* Return a location descriptor that designates a base+offset location. */
11303 static dw_loc_descr_ref
11304 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
11305 enum var_init_status initialized)
11307 unsigned int regno;
11308 dw_loc_descr_ref result;
11309 dw_fde_ref fde = cfun->fde;
11311 /* We only use "frame base" when we're sure we're talking about the
11312 post-prologue local stack frame. We do this by *not* running
11313 register elimination until this point, and recognizing the special
11314 argument pointer and soft frame pointer rtx's. */
11315 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
11317 rtx elim = (ira_use_lra_p
11318 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
11319 : eliminate_regs (reg, VOIDmode, NULL_RTX));
11321 if (elim != reg)
11323 if (GET_CODE (elim) == PLUS)
11325 offset += INTVAL (XEXP (elim, 1));
11326 elim = XEXP (elim, 0);
11328 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
11329 && (elim == hard_frame_pointer_rtx
11330 || elim == stack_pointer_rtx))
11331 || elim == (frame_pointer_needed
11332 ? hard_frame_pointer_rtx
11333 : stack_pointer_rtx));
11335 /* If drap register is used to align stack, use frame
11336 pointer + offset to access stack variables. If stack
11337 is aligned without drap, use stack pointer + offset to
11338 access stack variables. */
11339 if (crtl->stack_realign_tried
11340 && reg == frame_pointer_rtx)
11342 int base_reg
11343 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
11344 ? HARD_FRAME_POINTER_REGNUM
11345 : REGNO (elim));
11346 return new_reg_loc_descr (base_reg, offset);
11349 gcc_assert (frame_pointer_fb_offset_valid);
11350 offset += frame_pointer_fb_offset;
11351 return new_loc_descr (DW_OP_fbreg, offset, 0);
11355 regno = REGNO (reg);
11356 #ifdef LEAF_REG_REMAP
11357 if (crtl->uses_only_leaf_regs)
11359 int leaf_reg = LEAF_REG_REMAP (regno);
11360 if (leaf_reg != -1)
11361 regno = (unsigned) leaf_reg;
11363 #endif
11364 regno = DWARF_FRAME_REGNUM (regno);
11366 if (!optimize && fde
11367 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
11369 /* Use cfa+offset to represent the location of arguments passed
11370 on the stack when drap is used to align stack.
11371 Only do this when not optimizing, for optimized code var-tracking
11372 is supposed to track where the arguments live and the register
11373 used as vdrap or drap in some spot might be used for something
11374 else in other part of the routine. */
11375 return new_loc_descr (DW_OP_fbreg, offset, 0);
11378 if (regno <= 31)
11379 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
11380 offset, 0);
11381 else
11382 result = new_loc_descr (DW_OP_bregx, regno, offset);
11384 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11385 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11387 return result;
11390 /* Return true if this RTL expression describes a base+offset calculation. */
11392 static inline int
11393 is_based_loc (const_rtx rtl)
11395 return (GET_CODE (rtl) == PLUS
11396 && ((REG_P (XEXP (rtl, 0))
11397 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
11398 && CONST_INT_P (XEXP (rtl, 1)))));
11401 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
11402 failed. */
11404 static dw_loc_descr_ref
11405 tls_mem_loc_descriptor (rtx mem)
11407 tree base;
11408 dw_loc_descr_ref loc_result;
11410 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
11411 return NULL;
11413 base = get_base_address (MEM_EXPR (mem));
11414 if (base == NULL
11415 || TREE_CODE (base) != VAR_DECL
11416 || !DECL_THREAD_LOCAL_P (base))
11417 return NULL;
11419 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
11420 if (loc_result == NULL)
11421 return NULL;
11423 if (MEM_OFFSET (mem))
11424 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
11426 return loc_result;
11429 /* Output debug info about reason why we failed to expand expression as dwarf
11430 expression. */
11432 static void
11433 expansion_failed (tree expr, rtx rtl, char const *reason)
11435 if (dump_file && (dump_flags & TDF_DETAILS))
11437 fprintf (dump_file, "Failed to expand as dwarf: ");
11438 if (expr)
11439 print_generic_expr (dump_file, expr, dump_flags);
11440 if (rtl)
11442 fprintf (dump_file, "\n");
11443 print_rtl (dump_file, rtl);
11445 fprintf (dump_file, "\nReason: %s\n", reason);
11449 /* Helper function for const_ok_for_output. */
11451 static bool
11452 const_ok_for_output_1 (rtx rtl)
11454 if (GET_CODE (rtl) == UNSPEC)
11456 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
11457 we can't express it in the debug info. */
11458 #ifdef ENABLE_CHECKING
11459 /* Don't complain about TLS UNSPECs, those are just too hard to
11460 delegitimize. Note this could be a non-decl SYMBOL_REF such as
11461 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
11462 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
11463 if (XVECLEN (rtl, 0) == 0
11464 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
11465 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE)
11466 inform (current_function_decl
11467 ? DECL_SOURCE_LOCATION (current_function_decl)
11468 : UNKNOWN_LOCATION,
11469 #if NUM_UNSPEC_VALUES > 0
11470 "non-delegitimized UNSPEC %s (%d) found in variable location",
11471 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
11472 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
11473 XINT (rtl, 1));
11474 #else
11475 "non-delegitimized UNSPEC %d found in variable location",
11476 XINT (rtl, 1));
11477 #endif
11478 #endif
11479 expansion_failed (NULL_TREE, rtl,
11480 "UNSPEC hasn't been delegitimized.\n");
11481 return false;
11484 if (targetm.const_not_ok_for_debug_p (rtl))
11486 expansion_failed (NULL_TREE, rtl,
11487 "Expression rejected for debug by the backend.\n");
11488 return false;
11491 /* FIXME: Refer to PR60655. It is possible for simplification
11492 of rtl expressions in var tracking to produce such expressions.
11493 We should really identify / validate expressions
11494 enclosed in CONST that can be handled by assemblers on various
11495 targets and only handle legitimate cases here. */
11496 if (GET_CODE (rtl) != SYMBOL_REF)
11498 if (GET_CODE (rtl) == NOT)
11499 return false;
11500 return true;
11503 if (CONSTANT_POOL_ADDRESS_P (rtl))
11505 bool marked;
11506 get_pool_constant_mark (rtl, &marked);
11507 /* If all references to this pool constant were optimized away,
11508 it was not output and thus we can't represent it. */
11509 if (!marked)
11511 expansion_failed (NULL_TREE, rtl,
11512 "Constant was removed from constant pool.\n");
11513 return false;
11517 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11518 return false;
11520 /* Avoid references to external symbols in debug info, on several targets
11521 the linker might even refuse to link when linking a shared library,
11522 and in many other cases the relocations for .debug_info/.debug_loc are
11523 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
11524 to be defined within the same shared library or executable are fine. */
11525 if (SYMBOL_REF_EXTERNAL_P (rtl))
11527 tree decl = SYMBOL_REF_DECL (rtl);
11529 if (decl == NULL || !targetm.binds_local_p (decl))
11531 expansion_failed (NULL_TREE, rtl,
11532 "Symbol not defined in current TU.\n");
11533 return false;
11537 return true;
11540 /* Return true if constant RTL can be emitted in DW_OP_addr or
11541 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
11542 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
11544 static bool
11545 const_ok_for_output (rtx rtl)
11547 if (GET_CODE (rtl) == SYMBOL_REF)
11548 return const_ok_for_output_1 (rtl);
11550 if (GET_CODE (rtl) == CONST)
11552 subrtx_var_iterator::array_type array;
11553 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
11554 if (!const_ok_for_output_1 (*iter))
11555 return false;
11556 return true;
11559 return true;
11562 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
11563 if possible, NULL otherwise. */
11565 static dw_die_ref
11566 base_type_for_mode (enum machine_mode mode, bool unsignedp)
11568 dw_die_ref type_die;
11569 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11571 if (type == NULL)
11572 return NULL;
11573 switch (TREE_CODE (type))
11575 case INTEGER_TYPE:
11576 case REAL_TYPE:
11577 break;
11578 default:
11579 return NULL;
11581 type_die = lookup_type_die (type);
11582 if (!type_die)
11583 type_die = modified_type_die (type, TYPE_UNQUALIFIED, comp_unit_die ());
11584 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
11585 return NULL;
11586 return type_die;
11589 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
11590 type matching MODE, or, if MODE is narrower than or as wide as
11591 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
11592 possible. */
11594 static dw_loc_descr_ref
11595 convert_descriptor_to_mode (enum machine_mode mode, dw_loc_descr_ref op)
11597 enum machine_mode outer_mode = mode;
11598 dw_die_ref type_die;
11599 dw_loc_descr_ref cvt;
11601 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11603 add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
11604 return op;
11606 type_die = base_type_for_mode (outer_mode, 1);
11607 if (type_die == NULL)
11608 return NULL;
11609 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11610 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11611 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11612 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11613 add_loc_descr (&op, cvt);
11614 return op;
11617 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
11619 static dw_loc_descr_ref
11620 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
11621 dw_loc_descr_ref op1)
11623 dw_loc_descr_ref ret = op0;
11624 add_loc_descr (&ret, op1);
11625 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11626 if (STORE_FLAG_VALUE != 1)
11628 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
11629 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
11631 return ret;
11634 /* Return location descriptor for signed comparison OP RTL. */
11636 static dw_loc_descr_ref
11637 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11638 enum machine_mode mem_mode)
11640 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11641 dw_loc_descr_ref op0, op1;
11642 int shift;
11644 if (op_mode == VOIDmode)
11645 op_mode = GET_MODE (XEXP (rtl, 1));
11646 if (op_mode == VOIDmode)
11647 return NULL;
11649 if (dwarf_strict
11650 && (GET_MODE_CLASS (op_mode) != MODE_INT
11651 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
11652 return NULL;
11654 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11655 VAR_INIT_STATUS_INITIALIZED);
11656 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11657 VAR_INIT_STATUS_INITIALIZED);
11659 if (op0 == NULL || op1 == NULL)
11660 return NULL;
11662 if (GET_MODE_CLASS (op_mode) != MODE_INT
11663 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11664 return compare_loc_descriptor (op, op0, op1);
11666 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11668 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
11669 dw_loc_descr_ref cvt;
11671 if (type_die == NULL)
11672 return NULL;
11673 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11674 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11675 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11676 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11677 add_loc_descr (&op0, cvt);
11678 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11679 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11680 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11681 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11682 add_loc_descr (&op1, cvt);
11683 return compare_loc_descriptor (op, op0, op1);
11686 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
11687 /* For eq/ne, if the operands are known to be zero-extended,
11688 there is no need to do the fancy shifting up. */
11689 if (op == DW_OP_eq || op == DW_OP_ne)
11691 dw_loc_descr_ref last0, last1;
11692 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11694 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11696 /* deref_size zero extends, and for constants we can check
11697 whether they are zero extended or not. */
11698 if (((last0->dw_loc_opc == DW_OP_deref_size
11699 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11700 || (CONST_INT_P (XEXP (rtl, 0))
11701 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
11702 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
11703 && ((last1->dw_loc_opc == DW_OP_deref_size
11704 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11705 || (CONST_INT_P (XEXP (rtl, 1))
11706 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
11707 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
11708 return compare_loc_descriptor (op, op0, op1);
11710 /* EQ/NE comparison against constant in narrower type than
11711 DWARF2_ADDR_SIZE can be performed either as
11712 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
11713 DW_OP_{eq,ne}
11715 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
11716 DW_OP_{eq,ne}. Pick whatever is shorter. */
11717 if (CONST_INT_P (XEXP (rtl, 1))
11718 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
11719 && (size_of_int_loc_descriptor (shift) + 1
11720 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
11721 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
11722 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11723 & GET_MODE_MASK (op_mode))))
11725 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
11726 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11727 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11728 & GET_MODE_MASK (op_mode));
11729 return compare_loc_descriptor (op, op0, op1);
11732 add_loc_descr (&op0, int_loc_descriptor (shift));
11733 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11734 if (CONST_INT_P (XEXP (rtl, 1)))
11735 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
11736 else
11738 add_loc_descr (&op1, int_loc_descriptor (shift));
11739 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11741 return compare_loc_descriptor (op, op0, op1);
11744 /* Return location descriptor for unsigned comparison OP RTL. */
11746 static dw_loc_descr_ref
11747 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11748 enum machine_mode mem_mode)
11750 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11751 dw_loc_descr_ref op0, op1;
11753 if (op_mode == VOIDmode)
11754 op_mode = GET_MODE (XEXP (rtl, 1));
11755 if (op_mode == VOIDmode)
11756 return NULL;
11757 if (GET_MODE_CLASS (op_mode) != MODE_INT)
11758 return NULL;
11760 if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11761 return NULL;
11763 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11764 VAR_INIT_STATUS_INITIALIZED);
11765 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11766 VAR_INIT_STATUS_INITIALIZED);
11768 if (op0 == NULL || op1 == NULL)
11769 return NULL;
11771 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
11773 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
11774 dw_loc_descr_ref last0, last1;
11775 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11777 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11779 if (CONST_INT_P (XEXP (rtl, 0)))
11780 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
11781 /* deref_size zero extends, so no need to mask it again. */
11782 else if (last0->dw_loc_opc != DW_OP_deref_size
11783 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11785 add_loc_descr (&op0, int_loc_descriptor (mask));
11786 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11788 if (CONST_INT_P (XEXP (rtl, 1)))
11789 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
11790 /* deref_size zero extends, so no need to mask it again. */
11791 else if (last1->dw_loc_opc != DW_OP_deref_size
11792 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11794 add_loc_descr (&op1, int_loc_descriptor (mask));
11795 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11798 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11800 HOST_WIDE_INT bias = 1;
11801 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11802 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11803 if (CONST_INT_P (XEXP (rtl, 1)))
11804 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
11805 + INTVAL (XEXP (rtl, 1)));
11806 else
11807 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
11808 bias, 0));
11810 return compare_loc_descriptor (op, op0, op1);
11813 /* Return location descriptor for {U,S}{MIN,MAX}. */
11815 static dw_loc_descr_ref
11816 minmax_loc_descriptor (rtx rtl, enum machine_mode mode,
11817 enum machine_mode mem_mode)
11819 enum dwarf_location_atom op;
11820 dw_loc_descr_ref op0, op1, ret;
11821 dw_loc_descr_ref bra_node, drop_node;
11823 if (dwarf_strict
11824 && (GET_MODE_CLASS (mode) != MODE_INT
11825 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
11826 return NULL;
11828 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11829 VAR_INIT_STATUS_INITIALIZED);
11830 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11831 VAR_INIT_STATUS_INITIALIZED);
11833 if (op0 == NULL || op1 == NULL)
11834 return NULL;
11836 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
11837 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
11838 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
11839 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
11841 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11843 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11844 add_loc_descr (&op0, int_loc_descriptor (mask));
11845 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11846 add_loc_descr (&op1, int_loc_descriptor (mask));
11847 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11849 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
11851 HOST_WIDE_INT bias = 1;
11852 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11853 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11854 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11857 else if (GET_MODE_CLASS (mode) == MODE_INT
11858 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11860 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
11861 add_loc_descr (&op0, int_loc_descriptor (shift));
11862 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11863 add_loc_descr (&op1, int_loc_descriptor (shift));
11864 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11866 else if (GET_MODE_CLASS (mode) == MODE_INT
11867 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11869 dw_die_ref type_die = base_type_for_mode (mode, 0);
11870 dw_loc_descr_ref cvt;
11871 if (type_die == NULL)
11872 return NULL;
11873 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11874 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11875 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11876 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11877 add_loc_descr (&op0, cvt);
11878 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11879 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11880 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11881 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11882 add_loc_descr (&op1, cvt);
11885 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
11886 op = DW_OP_lt;
11887 else
11888 op = DW_OP_gt;
11889 ret = op0;
11890 add_loc_descr (&ret, op1);
11891 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11892 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
11893 add_loc_descr (&ret, bra_node);
11894 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11895 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
11896 add_loc_descr (&ret, drop_node);
11897 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
11898 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
11899 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
11900 && GET_MODE_CLASS (mode) == MODE_INT
11901 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11902 ret = convert_descriptor_to_mode (mode, ret);
11903 return ret;
11906 /* Helper function for mem_loc_descriptor. Perform OP binary op,
11907 but after converting arguments to type_die, afterwards
11908 convert back to unsigned. */
11910 static dw_loc_descr_ref
11911 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
11912 enum machine_mode mode, enum machine_mode mem_mode)
11914 dw_loc_descr_ref cvt, op0, op1;
11916 if (type_die == NULL)
11917 return NULL;
11918 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11919 VAR_INIT_STATUS_INITIALIZED);
11920 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11921 VAR_INIT_STATUS_INITIALIZED);
11922 if (op0 == NULL || op1 == NULL)
11923 return NULL;
11924 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11925 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11926 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11927 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11928 add_loc_descr (&op0, cvt);
11929 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11930 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11931 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11932 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11933 add_loc_descr (&op1, cvt);
11934 add_loc_descr (&op0, op1);
11935 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
11936 return convert_descriptor_to_mode (mode, op0);
11939 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
11940 const0 is DW_OP_lit0 or corresponding typed constant,
11941 const1 is DW_OP_lit1 or corresponding typed constant
11942 and constMSB is constant with just the MSB bit set
11943 for the mode):
11944 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11945 L1: const0 DW_OP_swap
11946 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
11947 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11948 L3: DW_OP_drop
11949 L4: DW_OP_nop
11951 CTZ is similar:
11952 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11953 L1: const0 DW_OP_swap
11954 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11955 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11956 L3: DW_OP_drop
11957 L4: DW_OP_nop
11959 FFS is similar:
11960 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
11961 L1: const1 DW_OP_swap
11962 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11963 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11964 L3: DW_OP_drop
11965 L4: DW_OP_nop */
11967 static dw_loc_descr_ref
11968 clz_loc_descriptor (rtx rtl, enum machine_mode mode,
11969 enum machine_mode mem_mode)
11971 dw_loc_descr_ref op0, ret, tmp;
11972 HOST_WIDE_INT valv;
11973 dw_loc_descr_ref l1jump, l1label;
11974 dw_loc_descr_ref l2jump, l2label;
11975 dw_loc_descr_ref l3jump, l3label;
11976 dw_loc_descr_ref l4jump, l4label;
11977 rtx msb;
11979 if (GET_MODE_CLASS (mode) != MODE_INT
11980 || GET_MODE (XEXP (rtl, 0)) != mode)
11981 return NULL;
11983 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11984 VAR_INIT_STATUS_INITIALIZED);
11985 if (op0 == NULL)
11986 return NULL;
11987 ret = op0;
11988 if (GET_CODE (rtl) == CLZ)
11990 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11991 valv = GET_MODE_BITSIZE (mode);
11993 else if (GET_CODE (rtl) == FFS)
11994 valv = 0;
11995 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11996 valv = GET_MODE_BITSIZE (mode);
11997 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11998 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
11999 add_loc_descr (&ret, l1jump);
12000 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12001 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
12002 VAR_INIT_STATUS_INITIALIZED);
12003 if (tmp == NULL)
12004 return NULL;
12005 add_loc_descr (&ret, tmp);
12006 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
12007 add_loc_descr (&ret, l4jump);
12008 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
12009 ? const1_rtx : const0_rtx,
12010 mode, mem_mode,
12011 VAR_INIT_STATUS_INITIALIZED);
12012 if (l1label == NULL)
12013 return NULL;
12014 add_loc_descr (&ret, l1label);
12015 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12016 l2label = new_loc_descr (DW_OP_dup, 0, 0);
12017 add_loc_descr (&ret, l2label);
12018 if (GET_CODE (rtl) != CLZ)
12019 msb = const1_rtx;
12020 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
12021 msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
12022 << (GET_MODE_BITSIZE (mode) - 1));
12023 else
12024 msb = immed_wide_int_const
12025 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
12026 GET_MODE_PRECISION (mode)), mode);
12027 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
12028 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12029 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
12030 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
12031 else
12032 tmp = mem_loc_descriptor (msb, mode, mem_mode,
12033 VAR_INIT_STATUS_INITIALIZED);
12034 if (tmp == NULL)
12035 return NULL;
12036 add_loc_descr (&ret, tmp);
12037 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12038 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
12039 add_loc_descr (&ret, l3jump);
12040 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12041 VAR_INIT_STATUS_INITIALIZED);
12042 if (tmp == NULL)
12043 return NULL;
12044 add_loc_descr (&ret, tmp);
12045 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
12046 ? DW_OP_shl : DW_OP_shr, 0, 0));
12047 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12048 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
12049 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12050 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
12051 add_loc_descr (&ret, l2jump);
12052 l3label = new_loc_descr (DW_OP_drop, 0, 0);
12053 add_loc_descr (&ret, l3label);
12054 l4label = new_loc_descr (DW_OP_nop, 0, 0);
12055 add_loc_descr (&ret, l4label);
12056 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12057 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12058 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12059 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12060 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12061 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
12062 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12063 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
12064 return ret;
12067 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
12068 const1 is DW_OP_lit1 or corresponding typed constant):
12069 const0 DW_OP_swap
12070 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
12071 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
12072 L2: DW_OP_drop
12074 PARITY is similar:
12075 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
12076 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
12077 L2: DW_OP_drop */
12079 static dw_loc_descr_ref
12080 popcount_loc_descriptor (rtx rtl, enum machine_mode mode,
12081 enum machine_mode mem_mode)
12083 dw_loc_descr_ref op0, ret, tmp;
12084 dw_loc_descr_ref l1jump, l1label;
12085 dw_loc_descr_ref l2jump, l2label;
12087 if (GET_MODE_CLASS (mode) != MODE_INT
12088 || GET_MODE (XEXP (rtl, 0)) != mode)
12089 return NULL;
12091 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12092 VAR_INIT_STATUS_INITIALIZED);
12093 if (op0 == NULL)
12094 return NULL;
12095 ret = op0;
12096 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12097 VAR_INIT_STATUS_INITIALIZED);
12098 if (tmp == NULL)
12099 return NULL;
12100 add_loc_descr (&ret, tmp);
12101 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12102 l1label = new_loc_descr (DW_OP_dup, 0, 0);
12103 add_loc_descr (&ret, l1label);
12104 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12105 add_loc_descr (&ret, l2jump);
12106 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12107 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12108 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12109 VAR_INIT_STATUS_INITIALIZED);
12110 if (tmp == NULL)
12111 return NULL;
12112 add_loc_descr (&ret, tmp);
12113 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12114 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
12115 ? DW_OP_plus : DW_OP_xor, 0, 0));
12116 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12117 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12118 VAR_INIT_STATUS_INITIALIZED);
12119 add_loc_descr (&ret, tmp);
12120 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12121 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12122 add_loc_descr (&ret, l1jump);
12123 l2label = new_loc_descr (DW_OP_drop, 0, 0);
12124 add_loc_descr (&ret, l2label);
12125 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12126 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12127 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12128 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12129 return ret;
12132 /* BSWAP (constS is initial shift count, either 56 or 24):
12133 constS const0
12134 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
12135 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
12136 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
12137 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
12138 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
12140 static dw_loc_descr_ref
12141 bswap_loc_descriptor (rtx rtl, enum machine_mode mode,
12142 enum machine_mode mem_mode)
12144 dw_loc_descr_ref op0, ret, tmp;
12145 dw_loc_descr_ref l1jump, l1label;
12146 dw_loc_descr_ref l2jump, l2label;
12148 if (GET_MODE_CLASS (mode) != MODE_INT
12149 || BITS_PER_UNIT != 8
12150 || (GET_MODE_BITSIZE (mode) != 32
12151 && GET_MODE_BITSIZE (mode) != 64))
12152 return NULL;
12154 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12155 VAR_INIT_STATUS_INITIALIZED);
12156 if (op0 == NULL)
12157 return NULL;
12159 ret = op0;
12160 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12161 mode, mem_mode,
12162 VAR_INIT_STATUS_INITIALIZED);
12163 if (tmp == NULL)
12164 return NULL;
12165 add_loc_descr (&ret, tmp);
12166 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12167 VAR_INIT_STATUS_INITIALIZED);
12168 if (tmp == NULL)
12169 return NULL;
12170 add_loc_descr (&ret, tmp);
12171 l1label = new_loc_descr (DW_OP_pick, 2, 0);
12172 add_loc_descr (&ret, l1label);
12173 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12174 mode, mem_mode,
12175 VAR_INIT_STATUS_INITIALIZED);
12176 add_loc_descr (&ret, tmp);
12177 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
12178 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12179 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12180 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
12181 VAR_INIT_STATUS_INITIALIZED);
12182 if (tmp == NULL)
12183 return NULL;
12184 add_loc_descr (&ret, tmp);
12185 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12186 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
12187 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12188 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12189 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12190 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12191 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12192 VAR_INIT_STATUS_INITIALIZED);
12193 add_loc_descr (&ret, tmp);
12194 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
12195 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12196 add_loc_descr (&ret, l2jump);
12197 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
12198 VAR_INIT_STATUS_INITIALIZED);
12199 add_loc_descr (&ret, tmp);
12200 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12201 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12202 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12203 add_loc_descr (&ret, l1jump);
12204 l2label = new_loc_descr (DW_OP_drop, 0, 0);
12205 add_loc_descr (&ret, l2label);
12206 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12207 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12208 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12209 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12210 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12211 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12212 return ret;
12215 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
12216 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12217 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
12218 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
12220 ROTATERT is similar:
12221 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
12222 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12223 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
12225 static dw_loc_descr_ref
12226 rotate_loc_descriptor (rtx rtl, enum machine_mode mode,
12227 enum machine_mode mem_mode)
12229 rtx rtlop1 = XEXP (rtl, 1);
12230 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
12231 int i;
12233 if (GET_MODE_CLASS (mode) != MODE_INT)
12234 return NULL;
12236 if (GET_MODE (rtlop1) != VOIDmode
12237 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
12238 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12239 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12240 VAR_INIT_STATUS_INITIALIZED);
12241 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12242 VAR_INIT_STATUS_INITIALIZED);
12243 if (op0 == NULL || op1 == NULL)
12244 return NULL;
12245 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12246 for (i = 0; i < 2; i++)
12248 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
12249 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
12250 mode, mem_mode,
12251 VAR_INIT_STATUS_INITIALIZED);
12252 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12253 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12254 ? DW_OP_const4u
12255 : HOST_BITS_PER_WIDE_INT == 64
12256 ? DW_OP_const8u : DW_OP_constu,
12257 GET_MODE_MASK (mode), 0);
12258 else
12259 mask[i] = NULL;
12260 if (mask[i] == NULL)
12261 return NULL;
12262 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
12264 ret = op0;
12265 add_loc_descr (&ret, op1);
12266 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12267 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12268 if (GET_CODE (rtl) == ROTATERT)
12270 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12271 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12272 GET_MODE_BITSIZE (mode), 0));
12274 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12275 if (mask[0] != NULL)
12276 add_loc_descr (&ret, mask[0]);
12277 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12278 if (mask[1] != NULL)
12280 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12281 add_loc_descr (&ret, mask[1]);
12282 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12284 if (GET_CODE (rtl) == ROTATE)
12286 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12287 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12288 GET_MODE_BITSIZE (mode), 0));
12290 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12291 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12292 return ret;
12295 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
12296 for DEBUG_PARAMETER_REF RTL. */
12298 static dw_loc_descr_ref
12299 parameter_ref_descriptor (rtx rtl)
12301 dw_loc_descr_ref ret;
12302 dw_die_ref ref;
12304 if (dwarf_strict)
12305 return NULL;
12306 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
12307 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
12308 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
12309 if (ref)
12311 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12312 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
12313 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
12315 else
12317 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
12318 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
12320 return ret;
12323 /* The following routine converts the RTL for a variable or parameter
12324 (resident in memory) into an equivalent Dwarf representation of a
12325 mechanism for getting the address of that same variable onto the top of a
12326 hypothetical "address evaluation" stack.
12328 When creating memory location descriptors, we are effectively transforming
12329 the RTL for a memory-resident object into its Dwarf postfix expression
12330 equivalent. This routine recursively descends an RTL tree, turning
12331 it into Dwarf postfix code as it goes.
12333 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
12335 MEM_MODE is the mode of the memory reference, needed to handle some
12336 autoincrement addressing modes.
12338 Return 0 if we can't represent the location. */
12340 dw_loc_descr_ref
12341 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
12342 enum machine_mode mem_mode,
12343 enum var_init_status initialized)
12345 dw_loc_descr_ref mem_loc_result = NULL;
12346 enum dwarf_location_atom op;
12347 dw_loc_descr_ref op0, op1;
12348 rtx inner = NULL_RTX;
12350 if (mode == VOIDmode)
12351 mode = GET_MODE (rtl);
12353 /* Note that for a dynamically sized array, the location we will generate a
12354 description of here will be the lowest numbered location which is
12355 actually within the array. That's *not* necessarily the same as the
12356 zeroth element of the array. */
12358 rtl = targetm.delegitimize_address (rtl);
12360 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
12361 return NULL;
12363 switch (GET_CODE (rtl))
12365 case POST_INC:
12366 case POST_DEC:
12367 case POST_MODIFY:
12368 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
12370 case SUBREG:
12371 /* The case of a subreg may arise when we have a local (register)
12372 variable or a formal (register) parameter which doesn't quite fill
12373 up an entire register. For now, just assume that it is
12374 legitimate to make the Dwarf info refer to the whole register which
12375 contains the given subreg. */
12376 if (!subreg_lowpart_p (rtl))
12377 break;
12378 inner = SUBREG_REG (rtl);
12379 case TRUNCATE:
12380 if (inner == NULL_RTX)
12381 inner = XEXP (rtl, 0);
12382 if (GET_MODE_CLASS (mode) == MODE_INT
12383 && GET_MODE_CLASS (GET_MODE (inner)) == MODE_INT
12384 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12385 #ifdef POINTERS_EXTEND_UNSIGNED
12386 || (mode == Pmode && mem_mode != VOIDmode)
12387 #endif
12389 && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
12391 mem_loc_result = mem_loc_descriptor (inner,
12392 GET_MODE (inner),
12393 mem_mode, initialized);
12394 break;
12396 if (dwarf_strict)
12397 break;
12398 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
12399 break;
12400 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
12401 && (GET_MODE_CLASS (mode) != MODE_INT
12402 || GET_MODE_CLASS (GET_MODE (inner)) != MODE_INT))
12403 break;
12404 else
12406 dw_die_ref type_die;
12407 dw_loc_descr_ref cvt;
12409 mem_loc_result = mem_loc_descriptor (inner,
12410 GET_MODE (inner),
12411 mem_mode, initialized);
12412 if (mem_loc_result == NULL)
12413 break;
12414 type_die = base_type_for_mode (mode,
12415 GET_MODE_CLASS (mode) == MODE_INT);
12416 if (type_die == NULL)
12418 mem_loc_result = NULL;
12419 break;
12421 if (GET_MODE_SIZE (mode)
12422 != GET_MODE_SIZE (GET_MODE (inner)))
12423 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12424 else
12425 cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
12426 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12427 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12428 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12429 add_loc_descr (&mem_loc_result, cvt);
12431 break;
12433 case REG:
12434 if (GET_MODE_CLASS (mode) != MODE_INT
12435 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12436 && rtl != arg_pointer_rtx
12437 && rtl != frame_pointer_rtx
12438 #ifdef POINTERS_EXTEND_UNSIGNED
12439 && (mode != Pmode || mem_mode == VOIDmode)
12440 #endif
12443 dw_die_ref type_die;
12444 unsigned int dbx_regnum;
12446 if (dwarf_strict)
12447 break;
12448 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
12449 break;
12450 type_die = base_type_for_mode (mode,
12451 GET_MODE_CLASS (mode) == MODE_INT);
12452 if (type_die == NULL)
12453 break;
12455 dbx_regnum = dbx_reg_number (rtl);
12456 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12457 break;
12458 mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
12459 dbx_regnum, 0);
12460 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12461 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12462 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
12463 break;
12465 /* Whenever a register number forms a part of the description of the
12466 method for calculating the (dynamic) address of a memory resident
12467 object, DWARF rules require the register number be referred to as
12468 a "base register". This distinction is not based in any way upon
12469 what category of register the hardware believes the given register
12470 belongs to. This is strictly DWARF terminology we're dealing with
12471 here. Note that in cases where the location of a memory-resident
12472 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
12473 OP_CONST (0)) the actual DWARF location descriptor that we generate
12474 may just be OP_BASEREG (basereg). This may look deceptively like
12475 the object in question was allocated to a register (rather than in
12476 memory) so DWARF consumers need to be aware of the subtle
12477 distinction between OP_REG and OP_BASEREG. */
12478 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
12479 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
12480 else if (stack_realign_drap
12481 && crtl->drap_reg
12482 && crtl->args.internal_arg_pointer == rtl
12483 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
12485 /* If RTL is internal_arg_pointer, which has been optimized
12486 out, use DRAP instead. */
12487 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
12488 VAR_INIT_STATUS_INITIALIZED);
12490 break;
12492 case SIGN_EXTEND:
12493 case ZERO_EXTEND:
12494 if (GET_MODE_CLASS (mode) != MODE_INT)
12495 break;
12496 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12497 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12498 if (op0 == 0)
12499 break;
12500 else if (GET_CODE (rtl) == ZERO_EXTEND
12501 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12502 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12503 < HOST_BITS_PER_WIDE_INT
12504 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
12505 to expand zero extend as two shifts instead of
12506 masking. */
12507 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
12509 enum machine_mode imode = GET_MODE (XEXP (rtl, 0));
12510 mem_loc_result = op0;
12511 add_loc_descr (&mem_loc_result,
12512 int_loc_descriptor (GET_MODE_MASK (imode)));
12513 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
12515 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12517 int shift = DWARF2_ADDR_SIZE
12518 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
12519 shift *= BITS_PER_UNIT;
12520 if (GET_CODE (rtl) == SIGN_EXTEND)
12521 op = DW_OP_shra;
12522 else
12523 op = DW_OP_shr;
12524 mem_loc_result = op0;
12525 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12526 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12527 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12528 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12530 else if (!dwarf_strict)
12532 dw_die_ref type_die1, type_die2;
12533 dw_loc_descr_ref cvt;
12535 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12536 GET_CODE (rtl) == ZERO_EXTEND);
12537 if (type_die1 == NULL)
12538 break;
12539 type_die2 = base_type_for_mode (mode, 1);
12540 if (type_die2 == NULL)
12541 break;
12542 mem_loc_result = op0;
12543 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12544 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12545 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
12546 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12547 add_loc_descr (&mem_loc_result, cvt);
12548 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12549 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12550 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
12551 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12552 add_loc_descr (&mem_loc_result, cvt);
12554 break;
12556 case MEM:
12558 rtx new_rtl = avoid_constant_pool_reference (rtl);
12559 if (new_rtl != rtl)
12561 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
12562 initialized);
12563 if (mem_loc_result != NULL)
12564 return mem_loc_result;
12567 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
12568 get_address_mode (rtl), mode,
12569 VAR_INIT_STATUS_INITIALIZED);
12570 if (mem_loc_result == NULL)
12571 mem_loc_result = tls_mem_loc_descriptor (rtl);
12572 if (mem_loc_result != NULL)
12574 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12575 || GET_MODE_CLASS (mode) != MODE_INT)
12577 dw_die_ref type_die;
12578 dw_loc_descr_ref deref;
12580 if (dwarf_strict)
12581 return NULL;
12582 type_die
12583 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
12584 if (type_die == NULL)
12585 return NULL;
12586 deref = new_loc_descr (DW_OP_GNU_deref_type,
12587 GET_MODE_SIZE (mode), 0);
12588 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12589 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12590 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
12591 add_loc_descr (&mem_loc_result, deref);
12593 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12594 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
12595 else
12596 add_loc_descr (&mem_loc_result,
12597 new_loc_descr (DW_OP_deref_size,
12598 GET_MODE_SIZE (mode), 0));
12600 break;
12602 case LO_SUM:
12603 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
12605 case LABEL_REF:
12606 /* Some ports can transform a symbol ref into a label ref, because
12607 the symbol ref is too far away and has to be dumped into a constant
12608 pool. */
12609 case CONST:
12610 case SYMBOL_REF:
12611 if ((GET_MODE_CLASS (mode) != MODE_INT
12612 && GET_MODE_CLASS (mode) != MODE_PARTIAL_INT)
12613 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12614 #ifdef POINTERS_EXTEND_UNSIGNED
12615 && (mode != Pmode || mem_mode == VOIDmode)
12616 #endif
12618 break;
12619 if (GET_CODE (rtl) == SYMBOL_REF
12620 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12622 dw_loc_descr_ref temp;
12624 /* If this is not defined, we have no way to emit the data. */
12625 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
12626 break;
12628 temp = new_addr_loc_descr (rtl, dtprel_true);
12630 mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
12631 add_loc_descr (&mem_loc_result, temp);
12633 break;
12636 if (!const_ok_for_output (rtl))
12637 break;
12639 symref:
12640 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
12641 vec_safe_push (used_rtx_array, rtl);
12642 break;
12644 case CONCAT:
12645 case CONCATN:
12646 case VAR_LOCATION:
12647 case DEBUG_IMPLICIT_PTR:
12648 expansion_failed (NULL_TREE, rtl,
12649 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
12650 return 0;
12652 case ENTRY_VALUE:
12653 if (dwarf_strict)
12654 return NULL;
12655 if (REG_P (ENTRY_VALUE_EXP (rtl)))
12657 if (GET_MODE_CLASS (mode) != MODE_INT
12658 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12659 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12660 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12661 else
12663 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
12664 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12665 return NULL;
12666 op0 = one_reg_loc_descriptor (dbx_regnum,
12667 VAR_INIT_STATUS_INITIALIZED);
12670 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
12671 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
12673 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12674 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12675 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
12676 return NULL;
12678 else
12679 gcc_unreachable ();
12680 if (op0 == NULL)
12681 return NULL;
12682 mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
12683 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
12684 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
12685 break;
12687 case DEBUG_PARAMETER_REF:
12688 mem_loc_result = parameter_ref_descriptor (rtl);
12689 break;
12691 case PRE_MODIFY:
12692 /* Extract the PLUS expression nested inside and fall into
12693 PLUS code below. */
12694 rtl = XEXP (rtl, 1);
12695 goto plus;
12697 case PRE_INC:
12698 case PRE_DEC:
12699 /* Turn these into a PLUS expression and fall into the PLUS code
12700 below. */
12701 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
12702 gen_int_mode (GET_CODE (rtl) == PRE_INC
12703 ? GET_MODE_UNIT_SIZE (mem_mode)
12704 : -GET_MODE_UNIT_SIZE (mem_mode),
12705 mode));
12707 /* ... fall through ... */
12709 case PLUS:
12710 plus:
12711 if (is_based_loc (rtl)
12712 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12713 || XEXP (rtl, 0) == arg_pointer_rtx
12714 || XEXP (rtl, 0) == frame_pointer_rtx)
12715 && GET_MODE_CLASS (mode) == MODE_INT)
12716 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
12717 INTVAL (XEXP (rtl, 1)),
12718 VAR_INIT_STATUS_INITIALIZED);
12719 else
12721 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12722 VAR_INIT_STATUS_INITIALIZED);
12723 if (mem_loc_result == 0)
12724 break;
12726 if (CONST_INT_P (XEXP (rtl, 1))
12727 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12728 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
12729 else
12731 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12732 VAR_INIT_STATUS_INITIALIZED);
12733 if (op1 == 0)
12734 return NULL;
12735 add_loc_descr (&mem_loc_result, op1);
12736 add_loc_descr (&mem_loc_result,
12737 new_loc_descr (DW_OP_plus, 0, 0));
12740 break;
12742 /* If a pseudo-reg is optimized away, it is possible for it to
12743 be replaced with a MEM containing a multiply or shift. */
12744 case MINUS:
12745 op = DW_OP_minus;
12746 goto do_binop;
12748 case MULT:
12749 op = DW_OP_mul;
12750 goto do_binop;
12752 case DIV:
12753 if (!dwarf_strict
12754 && GET_MODE_CLASS (mode) == MODE_INT
12755 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12757 mem_loc_result = typed_binop (DW_OP_div, rtl,
12758 base_type_for_mode (mode, 0),
12759 mode, mem_mode);
12760 break;
12762 op = DW_OP_div;
12763 goto do_binop;
12765 case UMOD:
12766 op = DW_OP_mod;
12767 goto do_binop;
12769 case ASHIFT:
12770 op = DW_OP_shl;
12771 goto do_shift;
12773 case ASHIFTRT:
12774 op = DW_OP_shra;
12775 goto do_shift;
12777 case LSHIFTRT:
12778 op = DW_OP_shr;
12779 goto do_shift;
12781 do_shift:
12782 if (GET_MODE_CLASS (mode) != MODE_INT)
12783 break;
12784 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12785 VAR_INIT_STATUS_INITIALIZED);
12787 rtx rtlop1 = XEXP (rtl, 1);
12788 if (GET_MODE (rtlop1) != VOIDmode
12789 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
12790 < GET_MODE_BITSIZE (mode))
12791 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12792 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12793 VAR_INIT_STATUS_INITIALIZED);
12796 if (op0 == 0 || op1 == 0)
12797 break;
12799 mem_loc_result = op0;
12800 add_loc_descr (&mem_loc_result, op1);
12801 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12802 break;
12804 case AND:
12805 op = DW_OP_and;
12806 goto do_binop;
12808 case IOR:
12809 op = DW_OP_or;
12810 goto do_binop;
12812 case XOR:
12813 op = DW_OP_xor;
12814 goto do_binop;
12816 do_binop:
12817 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12818 VAR_INIT_STATUS_INITIALIZED);
12819 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12820 VAR_INIT_STATUS_INITIALIZED);
12822 if (op0 == 0 || op1 == 0)
12823 break;
12825 mem_loc_result = op0;
12826 add_loc_descr (&mem_loc_result, op1);
12827 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12828 break;
12830 case MOD:
12831 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
12833 mem_loc_result = typed_binop (DW_OP_mod, rtl,
12834 base_type_for_mode (mode, 0),
12835 mode, mem_mode);
12836 break;
12839 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12840 VAR_INIT_STATUS_INITIALIZED);
12841 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12842 VAR_INIT_STATUS_INITIALIZED);
12844 if (op0 == 0 || op1 == 0)
12845 break;
12847 mem_loc_result = op0;
12848 add_loc_descr (&mem_loc_result, op1);
12849 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12850 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12851 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
12852 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
12853 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
12854 break;
12856 case UDIV:
12857 if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
12859 if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
12861 op = DW_OP_div;
12862 goto do_binop;
12864 mem_loc_result = typed_binop (DW_OP_div, rtl,
12865 base_type_for_mode (mode, 1),
12866 mode, mem_mode);
12868 break;
12870 case NOT:
12871 op = DW_OP_not;
12872 goto do_unop;
12874 case ABS:
12875 op = DW_OP_abs;
12876 goto do_unop;
12878 case NEG:
12879 op = DW_OP_neg;
12880 goto do_unop;
12882 do_unop:
12883 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12884 VAR_INIT_STATUS_INITIALIZED);
12886 if (op0 == 0)
12887 break;
12889 mem_loc_result = op0;
12890 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12891 break;
12893 case CONST_INT:
12894 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12895 #ifdef POINTERS_EXTEND_UNSIGNED
12896 || (mode == Pmode
12897 && mem_mode != VOIDmode
12898 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
12899 #endif
12902 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12903 break;
12905 if (!dwarf_strict
12906 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
12907 || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
12909 dw_die_ref type_die = base_type_for_mode (mode, 1);
12910 enum machine_mode amode;
12911 if (type_die == NULL)
12912 return NULL;
12913 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
12914 MODE_INT, 0);
12915 if (INTVAL (rtl) >= 0
12916 && amode != BLKmode
12917 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
12918 /* const DW_OP_GNU_convert <XXX> vs.
12919 DW_OP_GNU_const_type <XXX, 1, const>. */
12920 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
12921 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
12923 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12924 op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12925 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12926 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12927 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
12928 add_loc_descr (&mem_loc_result, op0);
12929 return mem_loc_result;
12931 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
12932 INTVAL (rtl));
12933 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12934 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12935 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12936 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12937 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
12938 else
12940 mem_loc_result->dw_loc_oprnd2.val_class
12941 = dw_val_class_const_double;
12942 mem_loc_result->dw_loc_oprnd2.v.val_double
12943 = double_int::from_shwi (INTVAL (rtl));
12946 break;
12948 case CONST_DOUBLE:
12949 if (!dwarf_strict)
12951 dw_die_ref type_die;
12953 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
12954 CONST_DOUBLE rtx could represent either a large integer
12955 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
12956 the value is always a floating point constant.
12958 When it is an integer, a CONST_DOUBLE is used whenever
12959 the constant requires 2 HWIs to be adequately represented.
12960 We output CONST_DOUBLEs as blocks. */
12961 if (mode == VOIDmode
12962 || (GET_MODE (rtl) == VOIDmode
12963 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
12964 break;
12965 type_die = base_type_for_mode (mode,
12966 GET_MODE_CLASS (mode) == MODE_INT);
12967 if (type_die == NULL)
12968 return NULL;
12969 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
12970 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12971 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12972 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12973 #if TARGET_SUPPORTS_WIDE_INT == 0
12974 if (!SCALAR_FLOAT_MODE_P (mode))
12976 mem_loc_result->dw_loc_oprnd2.val_class
12977 = dw_val_class_const_double;
12978 mem_loc_result->dw_loc_oprnd2.v.val_double
12979 = rtx_to_double_int (rtl);
12981 else
12982 #endif
12984 unsigned int length = GET_MODE_SIZE (mode);
12985 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
12987 insert_float (rtl, array);
12988 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
12989 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
12990 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
12991 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
12994 break;
12996 case CONST_WIDE_INT:
12997 if (!dwarf_strict)
12999 dw_die_ref type_die;
13001 type_die = base_type_for_mode (mode,
13002 GET_MODE_CLASS (mode) == MODE_INT);
13003 if (type_die == NULL)
13004 return NULL;
13005 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
13006 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13007 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13008 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13009 mem_loc_result->dw_loc_oprnd2.val_class
13010 = dw_val_class_wide_int;
13011 mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_cleared_alloc<wide_int> ();
13012 *mem_loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
13014 break;
13016 case EQ:
13017 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
13018 break;
13020 case GE:
13021 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
13022 break;
13024 case GT:
13025 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
13026 break;
13028 case LE:
13029 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
13030 break;
13032 case LT:
13033 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
13034 break;
13036 case NE:
13037 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
13038 break;
13040 case GEU:
13041 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
13042 break;
13044 case GTU:
13045 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
13046 break;
13048 case LEU:
13049 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
13050 break;
13052 case LTU:
13053 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
13054 break;
13056 case UMIN:
13057 case UMAX:
13058 if (GET_MODE_CLASS (mode) != MODE_INT)
13059 break;
13060 /* FALLTHRU */
13061 case SMIN:
13062 case SMAX:
13063 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
13064 break;
13066 case ZERO_EXTRACT:
13067 case SIGN_EXTRACT:
13068 if (CONST_INT_P (XEXP (rtl, 1))
13069 && CONST_INT_P (XEXP (rtl, 2))
13070 && ((unsigned) INTVAL (XEXP (rtl, 1))
13071 + (unsigned) INTVAL (XEXP (rtl, 2))
13072 <= GET_MODE_BITSIZE (mode))
13073 && GET_MODE_CLASS (mode) == MODE_INT
13074 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13075 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
13077 int shift, size;
13078 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13079 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13080 if (op0 == 0)
13081 break;
13082 if (GET_CODE (rtl) == SIGN_EXTRACT)
13083 op = DW_OP_shra;
13084 else
13085 op = DW_OP_shr;
13086 mem_loc_result = op0;
13087 size = INTVAL (XEXP (rtl, 1));
13088 shift = INTVAL (XEXP (rtl, 2));
13089 if (BITS_BIG_ENDIAN)
13090 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
13091 - shift - size;
13092 if (shift + size != (int) DWARF2_ADDR_SIZE)
13094 add_loc_descr (&mem_loc_result,
13095 int_loc_descriptor (DWARF2_ADDR_SIZE
13096 - shift - size));
13097 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13099 if (size != (int) DWARF2_ADDR_SIZE)
13101 add_loc_descr (&mem_loc_result,
13102 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
13103 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13106 break;
13108 case IF_THEN_ELSE:
13110 dw_loc_descr_ref op2, bra_node, drop_node;
13111 op0 = mem_loc_descriptor (XEXP (rtl, 0),
13112 GET_MODE (XEXP (rtl, 0)) == VOIDmode
13113 ? word_mode : GET_MODE (XEXP (rtl, 0)),
13114 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13115 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13116 VAR_INIT_STATUS_INITIALIZED);
13117 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
13118 VAR_INIT_STATUS_INITIALIZED);
13119 if (op0 == NULL || op1 == NULL || op2 == NULL)
13120 break;
13122 mem_loc_result = op1;
13123 add_loc_descr (&mem_loc_result, op2);
13124 add_loc_descr (&mem_loc_result, op0);
13125 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13126 add_loc_descr (&mem_loc_result, bra_node);
13127 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
13128 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
13129 add_loc_descr (&mem_loc_result, drop_node);
13130 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13131 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
13133 break;
13135 case FLOAT_EXTEND:
13136 case FLOAT_TRUNCATE:
13137 case FLOAT:
13138 case UNSIGNED_FLOAT:
13139 case FIX:
13140 case UNSIGNED_FIX:
13141 if (!dwarf_strict)
13143 dw_die_ref type_die;
13144 dw_loc_descr_ref cvt;
13146 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13147 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13148 if (op0 == NULL)
13149 break;
13150 if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
13151 && (GET_CODE (rtl) == FLOAT
13152 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
13153 <= DWARF2_ADDR_SIZE))
13155 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
13156 GET_CODE (rtl) == UNSIGNED_FLOAT);
13157 if (type_die == NULL)
13158 break;
13159 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13160 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13161 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13162 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13163 add_loc_descr (&op0, cvt);
13165 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
13166 if (type_die == NULL)
13167 break;
13168 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13169 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13170 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13171 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13172 add_loc_descr (&op0, cvt);
13173 if (GET_MODE_CLASS (mode) == MODE_INT
13174 && (GET_CODE (rtl) == FIX
13175 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
13177 op0 = convert_descriptor_to_mode (mode, op0);
13178 if (op0 == NULL)
13179 break;
13181 mem_loc_result = op0;
13183 break;
13185 case CLZ:
13186 case CTZ:
13187 case FFS:
13188 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
13189 break;
13191 case POPCOUNT:
13192 case PARITY:
13193 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
13194 break;
13196 case BSWAP:
13197 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
13198 break;
13200 case ROTATE:
13201 case ROTATERT:
13202 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
13203 break;
13205 case COMPARE:
13206 /* In theory, we could implement the above. */
13207 /* DWARF cannot represent the unsigned compare operations
13208 natively. */
13209 case SS_MULT:
13210 case US_MULT:
13211 case SS_DIV:
13212 case US_DIV:
13213 case SS_PLUS:
13214 case US_PLUS:
13215 case SS_MINUS:
13216 case US_MINUS:
13217 case SS_NEG:
13218 case US_NEG:
13219 case SS_ABS:
13220 case SS_ASHIFT:
13221 case US_ASHIFT:
13222 case SS_TRUNCATE:
13223 case US_TRUNCATE:
13224 case UNORDERED:
13225 case ORDERED:
13226 case UNEQ:
13227 case UNGE:
13228 case UNGT:
13229 case UNLE:
13230 case UNLT:
13231 case LTGT:
13232 case FRACT_CONVERT:
13233 case UNSIGNED_FRACT_CONVERT:
13234 case SAT_FRACT:
13235 case UNSIGNED_SAT_FRACT:
13236 case SQRT:
13237 case ASM_OPERANDS:
13238 case VEC_MERGE:
13239 case VEC_SELECT:
13240 case VEC_CONCAT:
13241 case VEC_DUPLICATE:
13242 case UNSPEC:
13243 case HIGH:
13244 case FMA:
13245 case STRICT_LOW_PART:
13246 case CONST_VECTOR:
13247 case CONST_FIXED:
13248 case CLRSB:
13249 case CLOBBER:
13250 /* If delegitimize_address couldn't do anything with the UNSPEC, we
13251 can't express it in the debug info. This can happen e.g. with some
13252 TLS UNSPECs. */
13253 break;
13255 case CONST_STRING:
13256 resolve_one_addr (&rtl);
13257 goto symref;
13259 default:
13260 #ifdef ENABLE_CHECKING
13261 print_rtl (stderr, rtl);
13262 gcc_unreachable ();
13263 #else
13264 break;
13265 #endif
13268 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13269 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13271 return mem_loc_result;
13274 /* Return a descriptor that describes the concatenation of two locations.
13275 This is typically a complex variable. */
13277 static dw_loc_descr_ref
13278 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
13280 dw_loc_descr_ref cc_loc_result = NULL;
13281 dw_loc_descr_ref x0_ref
13282 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13283 dw_loc_descr_ref x1_ref
13284 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13286 if (x0_ref == 0 || x1_ref == 0)
13287 return 0;
13289 cc_loc_result = x0_ref;
13290 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
13292 add_loc_descr (&cc_loc_result, x1_ref);
13293 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
13295 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13296 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13298 return cc_loc_result;
13301 /* Return a descriptor that describes the concatenation of N
13302 locations. */
13304 static dw_loc_descr_ref
13305 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
13307 unsigned int i;
13308 dw_loc_descr_ref cc_loc_result = NULL;
13309 unsigned int n = XVECLEN (concatn, 0);
13311 for (i = 0; i < n; ++i)
13313 dw_loc_descr_ref ref;
13314 rtx x = XVECEXP (concatn, 0, i);
13316 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13317 if (ref == NULL)
13318 return NULL;
13320 add_loc_descr (&cc_loc_result, ref);
13321 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
13324 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13325 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13327 return cc_loc_result;
13330 /* Helper function for loc_descriptor. Return DW_OP_GNU_implicit_pointer
13331 for DEBUG_IMPLICIT_PTR RTL. */
13333 static dw_loc_descr_ref
13334 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
13336 dw_loc_descr_ref ret;
13337 dw_die_ref ref;
13339 if (dwarf_strict)
13340 return NULL;
13341 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
13342 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
13343 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
13344 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
13345 ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
13346 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
13347 if (ref)
13349 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13350 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
13351 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
13353 else
13355 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
13356 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
13358 return ret;
13361 /* Output a proper Dwarf location descriptor for a variable or parameter
13362 which is either allocated in a register or in a memory location. For a
13363 register, we just generate an OP_REG and the register number. For a
13364 memory location we provide a Dwarf postfix expression describing how to
13365 generate the (dynamic) address of the object onto the address stack.
13367 MODE is mode of the decl if this loc_descriptor is going to be used in
13368 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
13369 allowed, VOIDmode otherwise.
13371 If we don't know how to describe it, return 0. */
13373 static dw_loc_descr_ref
13374 loc_descriptor (rtx rtl, enum machine_mode mode,
13375 enum var_init_status initialized)
13377 dw_loc_descr_ref loc_result = NULL;
13379 switch (GET_CODE (rtl))
13381 case SUBREG:
13382 /* The case of a subreg may arise when we have a local (register)
13383 variable or a formal (register) parameter which doesn't quite fill
13384 up an entire register. For now, just assume that it is
13385 legitimate to make the Dwarf info refer to the whole register which
13386 contains the given subreg. */
13387 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
13388 loc_result = loc_descriptor (SUBREG_REG (rtl),
13389 GET_MODE (SUBREG_REG (rtl)), initialized);
13390 else
13391 goto do_default;
13392 break;
13394 case REG:
13395 loc_result = reg_loc_descriptor (rtl, initialized);
13396 break;
13398 case MEM:
13399 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13400 GET_MODE (rtl), initialized);
13401 if (loc_result == NULL)
13402 loc_result = tls_mem_loc_descriptor (rtl);
13403 if (loc_result == NULL)
13405 rtx new_rtl = avoid_constant_pool_reference (rtl);
13406 if (new_rtl != rtl)
13407 loc_result = loc_descriptor (new_rtl, mode, initialized);
13409 break;
13411 case CONCAT:
13412 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
13413 initialized);
13414 break;
13416 case CONCATN:
13417 loc_result = concatn_loc_descriptor (rtl, initialized);
13418 break;
13420 case VAR_LOCATION:
13421 /* Single part. */
13422 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
13424 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
13425 if (GET_CODE (loc) == EXPR_LIST)
13426 loc = XEXP (loc, 0);
13427 loc_result = loc_descriptor (loc, mode, initialized);
13428 break;
13431 rtl = XEXP (rtl, 1);
13432 /* FALLTHRU */
13434 case PARALLEL:
13436 rtvec par_elems = XVEC (rtl, 0);
13437 int num_elem = GET_NUM_ELEM (par_elems);
13438 enum machine_mode mode;
13439 int i;
13441 /* Create the first one, so we have something to add to. */
13442 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
13443 VOIDmode, initialized);
13444 if (loc_result == NULL)
13445 return NULL;
13446 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
13447 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13448 for (i = 1; i < num_elem; i++)
13450 dw_loc_descr_ref temp;
13452 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
13453 VOIDmode, initialized);
13454 if (temp == NULL)
13455 return NULL;
13456 add_loc_descr (&loc_result, temp);
13457 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
13458 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13461 break;
13463 case CONST_INT:
13464 if (mode != VOIDmode && mode != BLKmode)
13465 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
13466 INTVAL (rtl));
13467 break;
13469 case CONST_DOUBLE:
13470 if (mode == VOIDmode)
13471 mode = GET_MODE (rtl);
13473 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13475 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13477 /* Note that a CONST_DOUBLE rtx could represent either an integer
13478 or a floating-point constant. A CONST_DOUBLE is used whenever
13479 the constant requires more than one word in order to be
13480 adequately represented. We output CONST_DOUBLEs as blocks. */
13481 loc_result = new_loc_descr (DW_OP_implicit_value,
13482 GET_MODE_SIZE (mode), 0);
13483 #if TARGET_SUPPORTS_WIDE_INT == 0
13484 if (!SCALAR_FLOAT_MODE_P (mode))
13486 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
13487 loc_result->dw_loc_oprnd2.v.val_double
13488 = rtx_to_double_int (rtl);
13490 else
13491 #endif
13493 unsigned int length = GET_MODE_SIZE (mode);
13494 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
13496 insert_float (rtl, array);
13497 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13498 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13499 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13500 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13503 break;
13505 case CONST_WIDE_INT:
13506 if (mode == VOIDmode)
13507 mode = GET_MODE (rtl);
13509 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13511 loc_result = new_loc_descr (DW_OP_implicit_value,
13512 GET_MODE_SIZE (mode), 0);
13513 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
13514 loc_result->dw_loc_oprnd2.v.val_wide = ggc_cleared_alloc<wide_int> ();
13515 *loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
13517 break;
13519 case CONST_VECTOR:
13520 if (mode == VOIDmode)
13521 mode = GET_MODE (rtl);
13523 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13525 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
13526 unsigned int length = CONST_VECTOR_NUNITS (rtl);
13527 unsigned char *array
13528 = ggc_vec_alloc<unsigned char> (length * elt_size);
13529 unsigned int i;
13530 unsigned char *p;
13531 enum machine_mode imode = GET_MODE_INNER (mode);
13533 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13534 switch (GET_MODE_CLASS (mode))
13536 case MODE_VECTOR_INT:
13537 for (i = 0, p = array; i < length; i++, p += elt_size)
13539 rtx elt = CONST_VECTOR_ELT (rtl, i);
13540 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
13542 break;
13544 case MODE_VECTOR_FLOAT:
13545 for (i = 0, p = array; i < length; i++, p += elt_size)
13547 rtx elt = CONST_VECTOR_ELT (rtl, i);
13548 insert_float (elt, p);
13550 break;
13552 default:
13553 gcc_unreachable ();
13556 loc_result = new_loc_descr (DW_OP_implicit_value,
13557 length * elt_size, 0);
13558 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13559 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
13560 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
13561 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13563 break;
13565 case CONST:
13566 if (mode == VOIDmode
13567 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
13568 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
13569 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
13571 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
13572 break;
13574 /* FALLTHROUGH */
13575 case SYMBOL_REF:
13576 if (!const_ok_for_output (rtl))
13577 break;
13578 case LABEL_REF:
13579 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
13580 && (dwarf_version >= 4 || !dwarf_strict))
13582 loc_result = new_addr_loc_descr (rtl, dtprel_false);
13583 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
13584 vec_safe_push (used_rtx_array, rtl);
13586 break;
13588 case DEBUG_IMPLICIT_PTR:
13589 loc_result = implicit_ptr_descriptor (rtl, 0);
13590 break;
13592 case PLUS:
13593 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
13594 && CONST_INT_P (XEXP (rtl, 1)))
13596 loc_result
13597 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
13598 break;
13600 /* FALLTHRU */
13601 do_default:
13602 default:
13603 if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
13604 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
13605 && dwarf_version >= 4)
13606 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
13608 /* Value expression. */
13609 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
13610 if (loc_result)
13611 add_loc_descr (&loc_result,
13612 new_loc_descr (DW_OP_stack_value, 0, 0));
13614 break;
13617 return loc_result;
13620 /* We need to figure out what section we should use as the base for the
13621 address ranges where a given location is valid.
13622 1. If this particular DECL has a section associated with it, use that.
13623 2. If this function has a section associated with it, use that.
13624 3. Otherwise, use the text section.
13625 XXX: If you split a variable across multiple sections, we won't notice. */
13627 static const char *
13628 secname_for_decl (const_tree decl)
13630 const char *secname;
13632 if (VAR_OR_FUNCTION_DECL_P (decl)
13633 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
13634 && DECL_SECTION_NAME (decl))
13635 secname = DECL_SECTION_NAME (decl);
13636 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
13637 secname = DECL_SECTION_NAME (current_function_decl);
13638 else if (cfun && in_cold_section_p)
13639 secname = crtl->subsections.cold_section_label;
13640 else
13641 secname = text_section_label;
13643 return secname;
13646 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
13648 static bool
13649 decl_by_reference_p (tree decl)
13651 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
13652 || TREE_CODE (decl) == VAR_DECL)
13653 && DECL_BY_REFERENCE (decl));
13656 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13657 for VARLOC. */
13659 static dw_loc_descr_ref
13660 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
13661 enum var_init_status initialized)
13663 int have_address = 0;
13664 dw_loc_descr_ref descr;
13665 enum machine_mode mode;
13667 if (want_address != 2)
13669 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
13670 /* Single part. */
13671 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13673 varloc = PAT_VAR_LOCATION_LOC (varloc);
13674 if (GET_CODE (varloc) == EXPR_LIST)
13675 varloc = XEXP (varloc, 0);
13676 mode = GET_MODE (varloc);
13677 if (MEM_P (varloc))
13679 rtx addr = XEXP (varloc, 0);
13680 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
13681 mode, initialized);
13682 if (descr)
13683 have_address = 1;
13684 else
13686 rtx x = avoid_constant_pool_reference (varloc);
13687 if (x != varloc)
13688 descr = mem_loc_descriptor (x, mode, VOIDmode,
13689 initialized);
13692 else
13693 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
13695 else
13696 return 0;
13698 else
13700 if (GET_CODE (varloc) == VAR_LOCATION)
13701 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
13702 else
13703 mode = DECL_MODE (loc);
13704 descr = loc_descriptor (varloc, mode, initialized);
13705 have_address = 1;
13708 if (!descr)
13709 return 0;
13711 if (want_address == 2 && !have_address
13712 && (dwarf_version >= 4 || !dwarf_strict))
13714 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
13716 expansion_failed (loc, NULL_RTX,
13717 "DWARF address size mismatch");
13718 return 0;
13720 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
13721 have_address = 1;
13723 /* Show if we can't fill the request for an address. */
13724 if (want_address && !have_address)
13726 expansion_failed (loc, NULL_RTX,
13727 "Want address and only have value");
13728 return 0;
13731 /* If we've got an address and don't want one, dereference. */
13732 if (!want_address && have_address)
13734 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
13735 enum dwarf_location_atom op;
13737 if (size > DWARF2_ADDR_SIZE || size == -1)
13739 expansion_failed (loc, NULL_RTX,
13740 "DWARF address size mismatch");
13741 return 0;
13743 else if (size == DWARF2_ADDR_SIZE)
13744 op = DW_OP_deref;
13745 else
13746 op = DW_OP_deref_size;
13748 add_loc_descr (&descr, new_loc_descr (op, size, 0));
13751 return descr;
13754 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
13755 if it is not possible. */
13757 static dw_loc_descr_ref
13758 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
13760 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
13761 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
13762 else if (dwarf_version >= 3 || !dwarf_strict)
13763 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
13764 else
13765 return NULL;
13768 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13769 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
13771 static dw_loc_descr_ref
13772 dw_sra_loc_expr (tree decl, rtx loc)
13774 rtx p;
13775 unsigned int padsize = 0;
13776 dw_loc_descr_ref descr, *descr_tail;
13777 unsigned HOST_WIDE_INT decl_size;
13778 rtx varloc;
13779 enum var_init_status initialized;
13781 if (DECL_SIZE (decl) == NULL
13782 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
13783 return NULL;
13785 decl_size = tree_to_uhwi (DECL_SIZE (decl));
13786 descr = NULL;
13787 descr_tail = &descr;
13789 for (p = loc; p; p = XEXP (p, 1))
13791 unsigned int bitsize = decl_piece_bitsize (p);
13792 rtx loc_note = *decl_piece_varloc_ptr (p);
13793 dw_loc_descr_ref cur_descr;
13794 dw_loc_descr_ref *tail, last = NULL;
13795 unsigned int opsize = 0;
13797 if (loc_note == NULL_RTX
13798 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
13800 padsize += bitsize;
13801 continue;
13803 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
13804 varloc = NOTE_VAR_LOCATION (loc_note);
13805 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
13806 if (cur_descr == NULL)
13808 padsize += bitsize;
13809 continue;
13812 /* Check that cur_descr either doesn't use
13813 DW_OP_*piece operations, or their sum is equal
13814 to bitsize. Otherwise we can't embed it. */
13815 for (tail = &cur_descr; *tail != NULL;
13816 tail = &(*tail)->dw_loc_next)
13817 if ((*tail)->dw_loc_opc == DW_OP_piece)
13819 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
13820 * BITS_PER_UNIT;
13821 last = *tail;
13823 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
13825 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
13826 last = *tail;
13829 if (last != NULL && opsize != bitsize)
13831 padsize += bitsize;
13832 /* Discard the current piece of the descriptor and release any
13833 addr_table entries it uses. */
13834 remove_loc_list_addr_table_entries (cur_descr);
13835 continue;
13838 /* If there is a hole, add DW_OP_*piece after empty DWARF
13839 expression, which means that those bits are optimized out. */
13840 if (padsize)
13842 if (padsize > decl_size)
13844 remove_loc_list_addr_table_entries (cur_descr);
13845 goto discard_descr;
13847 decl_size -= padsize;
13848 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
13849 if (*descr_tail == NULL)
13851 remove_loc_list_addr_table_entries (cur_descr);
13852 goto discard_descr;
13854 descr_tail = &(*descr_tail)->dw_loc_next;
13855 padsize = 0;
13857 *descr_tail = cur_descr;
13858 descr_tail = tail;
13859 if (bitsize > decl_size)
13860 goto discard_descr;
13861 decl_size -= bitsize;
13862 if (last == NULL)
13864 HOST_WIDE_INT offset = 0;
13865 if (GET_CODE (varloc) == VAR_LOCATION
13866 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13868 varloc = PAT_VAR_LOCATION_LOC (varloc);
13869 if (GET_CODE (varloc) == EXPR_LIST)
13870 varloc = XEXP (varloc, 0);
13874 if (GET_CODE (varloc) == CONST
13875 || GET_CODE (varloc) == SIGN_EXTEND
13876 || GET_CODE (varloc) == ZERO_EXTEND)
13877 varloc = XEXP (varloc, 0);
13878 else if (GET_CODE (varloc) == SUBREG)
13879 varloc = SUBREG_REG (varloc);
13880 else
13881 break;
13883 while (1);
13884 /* DW_OP_bit_size offset should be zero for register
13885 or implicit location descriptions and empty location
13886 descriptions, but for memory addresses needs big endian
13887 adjustment. */
13888 if (MEM_P (varloc))
13890 unsigned HOST_WIDE_INT memsize
13891 = MEM_SIZE (varloc) * BITS_PER_UNIT;
13892 if (memsize != bitsize)
13894 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
13895 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
13896 goto discard_descr;
13897 if (memsize < bitsize)
13898 goto discard_descr;
13899 if (BITS_BIG_ENDIAN)
13900 offset = memsize - bitsize;
13904 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
13905 if (*descr_tail == NULL)
13906 goto discard_descr;
13907 descr_tail = &(*descr_tail)->dw_loc_next;
13911 /* If there were any non-empty expressions, add padding till the end of
13912 the decl. */
13913 if (descr != NULL && decl_size != 0)
13915 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
13916 if (*descr_tail == NULL)
13917 goto discard_descr;
13919 return descr;
13921 discard_descr:
13922 /* Discard the descriptor and release any addr_table entries it uses. */
13923 remove_loc_list_addr_table_entries (descr);
13924 return NULL;
13927 /* Return the dwarf representation of the location list LOC_LIST of
13928 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
13929 function. */
13931 static dw_loc_list_ref
13932 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
13934 const char *endname, *secname;
13935 rtx varloc;
13936 enum var_init_status initialized;
13937 struct var_loc_node *node;
13938 dw_loc_descr_ref descr;
13939 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
13940 dw_loc_list_ref list = NULL;
13941 dw_loc_list_ref *listp = &list;
13943 /* Now that we know what section we are using for a base,
13944 actually construct the list of locations.
13945 The first location information is what is passed to the
13946 function that creates the location list, and the remaining
13947 locations just get added on to that list.
13948 Note that we only know the start address for a location
13949 (IE location changes), so to build the range, we use
13950 the range [current location start, next location start].
13951 This means we have to special case the last node, and generate
13952 a range of [last location start, end of function label]. */
13954 secname = secname_for_decl (decl);
13956 for (node = loc_list->first; node; node = node->next)
13957 if (GET_CODE (node->loc) == EXPR_LIST
13958 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
13960 if (GET_CODE (node->loc) == EXPR_LIST)
13962 /* This requires DW_OP_{,bit_}piece, which is not usable
13963 inside DWARF expressions. */
13964 if (want_address != 2)
13965 continue;
13966 descr = dw_sra_loc_expr (decl, node->loc);
13967 if (descr == NULL)
13968 continue;
13970 else
13972 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13973 varloc = NOTE_VAR_LOCATION (node->loc);
13974 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
13976 if (descr)
13978 bool range_across_switch = false;
13979 /* If section switch happens in between node->label
13980 and node->next->label (or end of function) and
13981 we can't emit it as a single entry list,
13982 emit two ranges, first one ending at the end
13983 of first partition and second one starting at the
13984 beginning of second partition. */
13985 if (node == loc_list->last_before_switch
13986 && (node != loc_list->first || loc_list->first->next)
13987 && current_function_decl)
13989 endname = cfun->fde->dw_fde_end;
13990 range_across_switch = true;
13992 /* The variable has a location between NODE->LABEL and
13993 NODE->NEXT->LABEL. */
13994 else if (node->next)
13995 endname = node->next->label;
13996 /* If the variable has a location at the last label
13997 it keeps its location until the end of function. */
13998 else if (!current_function_decl)
13999 endname = text_end_label;
14000 else
14002 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
14003 current_function_funcdef_no);
14004 endname = ggc_strdup (label_id);
14007 *listp = new_loc_list (descr, node->label, endname, secname);
14008 if (TREE_CODE (decl) == PARM_DECL
14009 && node == loc_list->first
14010 && NOTE_P (node->loc)
14011 && strcmp (node->label, endname) == 0)
14012 (*listp)->force = true;
14013 listp = &(*listp)->dw_loc_next;
14015 if (range_across_switch)
14017 if (GET_CODE (node->loc) == EXPR_LIST)
14018 descr = dw_sra_loc_expr (decl, node->loc);
14019 else
14021 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14022 varloc = NOTE_VAR_LOCATION (node->loc);
14023 descr = dw_loc_list_1 (decl, varloc, want_address,
14024 initialized);
14026 gcc_assert (descr);
14027 /* The variable has a location between NODE->LABEL and
14028 NODE->NEXT->LABEL. */
14029 if (node->next)
14030 endname = node->next->label;
14031 else
14032 endname = cfun->fde->dw_fde_second_end;
14033 *listp = new_loc_list (descr,
14034 cfun->fde->dw_fde_second_begin,
14035 endname, secname);
14036 listp = &(*listp)->dw_loc_next;
14041 /* Try to avoid the overhead of a location list emitting a location
14042 expression instead, but only if we didn't have more than one
14043 location entry in the first place. If some entries were not
14044 representable, we don't want to pretend a single entry that was
14045 applies to the entire scope in which the variable is
14046 available. */
14047 if (list && loc_list->first->next)
14048 gen_llsym (list);
14050 return list;
14053 /* Return if the loc_list has only single element and thus can be represented
14054 as location description. */
14056 static bool
14057 single_element_loc_list_p (dw_loc_list_ref list)
14059 gcc_assert (!list->dw_loc_next || list->ll_symbol);
14060 return !list->ll_symbol;
14063 /* To each location in list LIST add loc descr REF. */
14065 static void
14066 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
14068 dw_loc_descr_ref copy;
14069 add_loc_descr (&list->expr, ref);
14070 list = list->dw_loc_next;
14071 while (list)
14073 copy = ggc_alloc<dw_loc_descr_node> ();
14074 memcpy (copy, ref, sizeof (dw_loc_descr_node));
14075 add_loc_descr (&list->expr, copy);
14076 while (copy->dw_loc_next)
14078 dw_loc_descr_ref new_copy = ggc_alloc<dw_loc_descr_node> ();
14079 memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
14080 copy->dw_loc_next = new_copy;
14081 copy = new_copy;
14083 list = list->dw_loc_next;
14087 /* Given two lists RET and LIST
14088 produce location list that is result of adding expression in LIST
14089 to expression in RET on each position in program.
14090 Might be destructive on both RET and LIST.
14092 TODO: We handle only simple cases of RET or LIST having at most one
14093 element. General case would inolve sorting the lists in program order
14094 and merging them that will need some additional work.
14095 Adding that will improve quality of debug info especially for SRA-ed
14096 structures. */
14098 static void
14099 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
14101 if (!list)
14102 return;
14103 if (!*ret)
14105 *ret = list;
14106 return;
14108 if (!list->dw_loc_next)
14110 add_loc_descr_to_each (*ret, list->expr);
14111 return;
14113 if (!(*ret)->dw_loc_next)
14115 add_loc_descr_to_each (list, (*ret)->expr);
14116 *ret = list;
14117 return;
14119 expansion_failed (NULL_TREE, NULL_RTX,
14120 "Don't know how to merge two non-trivial"
14121 " location lists.\n");
14122 *ret = NULL;
14123 return;
14126 /* LOC is constant expression. Try a luck, look it up in constant
14127 pool and return its loc_descr of its address. */
14129 static dw_loc_descr_ref
14130 cst_pool_loc_descr (tree loc)
14132 /* Get an RTL for this, if something has been emitted. */
14133 rtx rtl = lookup_constant_def (loc);
14135 if (!rtl || !MEM_P (rtl))
14137 gcc_assert (!rtl);
14138 return 0;
14140 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
14142 /* TODO: We might get more coverage if we was actually delaying expansion
14143 of all expressions till end of compilation when constant pools are fully
14144 populated. */
14145 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
14147 expansion_failed (loc, NULL_RTX,
14148 "CST value in contant pool but not marked.");
14149 return 0;
14151 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
14152 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
14155 /* Return dw_loc_list representing address of addr_expr LOC
14156 by looking for inner INDIRECT_REF expression and turning
14157 it into simple arithmetics. */
14159 static dw_loc_list_ref
14160 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
14162 tree obj, offset;
14163 HOST_WIDE_INT bitsize, bitpos, bytepos;
14164 enum machine_mode mode;
14165 int unsignedp, volatilep = 0;
14166 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14168 obj = get_inner_reference (TREE_OPERAND (loc, 0),
14169 &bitsize, &bitpos, &offset, &mode,
14170 &unsignedp, &volatilep, false);
14171 STRIP_NOPS (obj);
14172 if (bitpos % BITS_PER_UNIT)
14174 expansion_failed (loc, NULL_RTX, "bitfield access");
14175 return 0;
14177 if (!INDIRECT_REF_P (obj))
14179 expansion_failed (obj,
14180 NULL_RTX, "no indirect ref in inner refrence");
14181 return 0;
14183 if (!offset && !bitpos)
14184 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
14185 else if (toplev
14186 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
14187 && (dwarf_version >= 4 || !dwarf_strict))
14189 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
14190 if (!list_ret)
14191 return 0;
14192 if (offset)
14194 /* Variable offset. */
14195 list_ret1 = loc_list_from_tree (offset, 0);
14196 if (list_ret1 == 0)
14197 return 0;
14198 add_loc_list (&list_ret, list_ret1);
14199 if (!list_ret)
14200 return 0;
14201 add_loc_descr_to_each (list_ret,
14202 new_loc_descr (DW_OP_plus, 0, 0));
14204 bytepos = bitpos / BITS_PER_UNIT;
14205 if (bytepos > 0)
14206 add_loc_descr_to_each (list_ret,
14207 new_loc_descr (DW_OP_plus_uconst,
14208 bytepos, 0));
14209 else if (bytepos < 0)
14210 loc_list_plus_const (list_ret, bytepos);
14211 add_loc_descr_to_each (list_ret,
14212 new_loc_descr (DW_OP_stack_value, 0, 0));
14214 return list_ret;
14218 /* Generate Dwarf location list representing LOC.
14219 If WANT_ADDRESS is false, expression computing LOC will be computed
14220 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
14221 if WANT_ADDRESS is 2, expression computing address useable in location
14222 will be returned (i.e. DW_OP_reg can be used
14223 to refer to register values). */
14225 static dw_loc_list_ref
14226 loc_list_from_tree (tree loc, int want_address)
14228 dw_loc_descr_ref ret = NULL, ret1 = NULL;
14229 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14230 int have_address = 0;
14231 enum dwarf_location_atom op;
14233 /* ??? Most of the time we do not take proper care for sign/zero
14234 extending the values properly. Hopefully this won't be a real
14235 problem... */
14237 switch (TREE_CODE (loc))
14239 case ERROR_MARK:
14240 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
14241 return 0;
14243 case PLACEHOLDER_EXPR:
14244 /* This case involves extracting fields from an object to determine the
14245 position of other fields. We don't try to encode this here. The
14246 only user of this is Ada, which encodes the needed information using
14247 the names of types. */
14248 expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
14249 return 0;
14251 case CALL_EXPR:
14252 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
14253 /* There are no opcodes for these operations. */
14254 return 0;
14256 case PREINCREMENT_EXPR:
14257 case PREDECREMENT_EXPR:
14258 case POSTINCREMENT_EXPR:
14259 case POSTDECREMENT_EXPR:
14260 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
14261 /* There are no opcodes for these operations. */
14262 return 0;
14264 case ADDR_EXPR:
14265 /* If we already want an address, see if there is INDIRECT_REF inside
14266 e.g. for &this->field. */
14267 if (want_address)
14269 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
14270 (loc, want_address == 2);
14271 if (list_ret)
14272 have_address = 1;
14273 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
14274 && (ret = cst_pool_loc_descr (loc)))
14275 have_address = 1;
14277 /* Otherwise, process the argument and look for the address. */
14278 if (!list_ret && !ret)
14279 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
14280 else
14282 if (want_address)
14283 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
14284 return NULL;
14286 break;
14288 case VAR_DECL:
14289 if (DECL_THREAD_LOCAL_P (loc))
14291 rtx rtl;
14292 enum dwarf_location_atom tls_op;
14293 enum dtprel_bool dtprel = dtprel_false;
14295 if (targetm.have_tls)
14297 /* If this is not defined, we have no way to emit the
14298 data. */
14299 if (!targetm.asm_out.output_dwarf_dtprel)
14300 return 0;
14302 /* The way DW_OP_GNU_push_tls_address is specified, we
14303 can only look up addresses of objects in the current
14304 module. We used DW_OP_addr as first op, but that's
14305 wrong, because DW_OP_addr is relocated by the debug
14306 info consumer, while DW_OP_GNU_push_tls_address
14307 operand shouldn't be. */
14308 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
14309 return 0;
14310 dtprel = dtprel_true;
14311 tls_op = DW_OP_GNU_push_tls_address;
14313 else
14315 if (!targetm.emutls.debug_form_tls_address
14316 || !(dwarf_version >= 3 || !dwarf_strict))
14317 return 0;
14318 /* We stuffed the control variable into the DECL_VALUE_EXPR
14319 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
14320 no longer appear in gimple code. We used the control
14321 variable in specific so that we could pick it up here. */
14322 loc = DECL_VALUE_EXPR (loc);
14323 tls_op = DW_OP_form_tls_address;
14326 rtl = rtl_for_decl_location (loc);
14327 if (rtl == NULL_RTX)
14328 return 0;
14330 if (!MEM_P (rtl))
14331 return 0;
14332 rtl = XEXP (rtl, 0);
14333 if (! CONSTANT_P (rtl))
14334 return 0;
14336 ret = new_addr_loc_descr (rtl, dtprel);
14337 ret1 = new_loc_descr (tls_op, 0, 0);
14338 add_loc_descr (&ret, ret1);
14340 have_address = 1;
14341 break;
14343 /* FALLTHRU */
14345 case PARM_DECL:
14346 case RESULT_DECL:
14347 if (DECL_HAS_VALUE_EXPR_P (loc))
14348 return loc_list_from_tree (DECL_VALUE_EXPR (loc),
14349 want_address);
14350 /* FALLTHRU */
14352 case FUNCTION_DECL:
14354 rtx rtl;
14355 var_loc_list *loc_list = lookup_decl_loc (loc);
14357 if (loc_list && loc_list->first)
14359 list_ret = dw_loc_list (loc_list, loc, want_address);
14360 have_address = want_address != 0;
14361 break;
14363 rtl = rtl_for_decl_location (loc);
14364 if (rtl == NULL_RTX)
14366 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
14367 return 0;
14369 else if (CONST_INT_P (rtl))
14371 HOST_WIDE_INT val = INTVAL (rtl);
14372 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14373 val &= GET_MODE_MASK (DECL_MODE (loc));
14374 ret = int_loc_descriptor (val);
14376 else if (GET_CODE (rtl) == CONST_STRING)
14378 expansion_failed (loc, NULL_RTX, "CONST_STRING");
14379 return 0;
14381 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
14382 ret = new_addr_loc_descr (rtl, dtprel_false);
14383 else
14385 enum machine_mode mode, mem_mode;
14387 /* Certain constructs can only be represented at top-level. */
14388 if (want_address == 2)
14390 ret = loc_descriptor (rtl, VOIDmode,
14391 VAR_INIT_STATUS_INITIALIZED);
14392 have_address = 1;
14394 else
14396 mode = GET_MODE (rtl);
14397 mem_mode = VOIDmode;
14398 if (MEM_P (rtl))
14400 mem_mode = mode;
14401 mode = get_address_mode (rtl);
14402 rtl = XEXP (rtl, 0);
14403 have_address = 1;
14405 ret = mem_loc_descriptor (rtl, mode, mem_mode,
14406 VAR_INIT_STATUS_INITIALIZED);
14408 if (!ret)
14409 expansion_failed (loc, rtl,
14410 "failed to produce loc descriptor for rtl");
14413 break;
14415 case MEM_REF:
14416 if (!integer_zerop (TREE_OPERAND (loc, 1)))
14418 have_address = 1;
14419 goto do_plus;
14421 /* Fallthru. */
14422 case INDIRECT_REF:
14423 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14424 have_address = 1;
14425 break;
14427 case TARGET_MEM_REF:
14428 case SSA_NAME:
14429 return NULL;
14431 case COMPOUND_EXPR:
14432 return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
14434 CASE_CONVERT:
14435 case VIEW_CONVERT_EXPR:
14436 case SAVE_EXPR:
14437 case MODIFY_EXPR:
14438 return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
14440 case COMPONENT_REF:
14441 case BIT_FIELD_REF:
14442 case ARRAY_REF:
14443 case ARRAY_RANGE_REF:
14444 case REALPART_EXPR:
14445 case IMAGPART_EXPR:
14447 tree obj, offset;
14448 HOST_WIDE_INT bitsize, bitpos, bytepos;
14449 enum machine_mode mode;
14450 int unsignedp, volatilep = 0;
14452 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
14453 &unsignedp, &volatilep, false);
14455 gcc_assert (obj != loc);
14457 list_ret = loc_list_from_tree (obj,
14458 want_address == 2
14459 && !bitpos && !offset ? 2 : 1);
14460 /* TODO: We can extract value of the small expression via shifting even
14461 for nonzero bitpos. */
14462 if (list_ret == 0)
14463 return 0;
14464 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
14466 expansion_failed (loc, NULL_RTX,
14467 "bitfield access");
14468 return 0;
14471 if (offset != NULL_TREE)
14473 /* Variable offset. */
14474 list_ret1 = loc_list_from_tree (offset, 0);
14475 if (list_ret1 == 0)
14476 return 0;
14477 add_loc_list (&list_ret, list_ret1);
14478 if (!list_ret)
14479 return 0;
14480 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
14483 bytepos = bitpos / BITS_PER_UNIT;
14484 if (bytepos > 0)
14485 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
14486 else if (bytepos < 0)
14487 loc_list_plus_const (list_ret, bytepos);
14489 have_address = 1;
14490 break;
14493 case INTEGER_CST:
14494 if ((want_address || !tree_fits_shwi_p (loc))
14495 && (ret = cst_pool_loc_descr (loc)))
14496 have_address = 1;
14497 else if (want_address == 2
14498 && tree_fits_shwi_p (loc)
14499 && (ret = address_of_int_loc_descriptor
14500 (int_size_in_bytes (TREE_TYPE (loc)),
14501 tree_to_shwi (loc))))
14502 have_address = 1;
14503 else if (tree_fits_shwi_p (loc))
14504 ret = int_loc_descriptor (tree_to_shwi (loc));
14505 else
14507 expansion_failed (loc, NULL_RTX,
14508 "Integer operand is not host integer");
14509 return 0;
14511 break;
14513 case CONSTRUCTOR:
14514 case REAL_CST:
14515 case STRING_CST:
14516 case COMPLEX_CST:
14517 if ((ret = cst_pool_loc_descr (loc)))
14518 have_address = 1;
14519 else
14520 /* We can construct small constants here using int_loc_descriptor. */
14521 expansion_failed (loc, NULL_RTX,
14522 "constructor or constant not in constant pool");
14523 break;
14525 case TRUTH_AND_EXPR:
14526 case TRUTH_ANDIF_EXPR:
14527 case BIT_AND_EXPR:
14528 op = DW_OP_and;
14529 goto do_binop;
14531 case TRUTH_XOR_EXPR:
14532 case BIT_XOR_EXPR:
14533 op = DW_OP_xor;
14534 goto do_binop;
14536 case TRUTH_OR_EXPR:
14537 case TRUTH_ORIF_EXPR:
14538 case BIT_IOR_EXPR:
14539 op = DW_OP_or;
14540 goto do_binop;
14542 case FLOOR_DIV_EXPR:
14543 case CEIL_DIV_EXPR:
14544 case ROUND_DIV_EXPR:
14545 case TRUNC_DIV_EXPR:
14546 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14547 return 0;
14548 op = DW_OP_div;
14549 goto do_binop;
14551 case MINUS_EXPR:
14552 op = DW_OP_minus;
14553 goto do_binop;
14555 case FLOOR_MOD_EXPR:
14556 case CEIL_MOD_EXPR:
14557 case ROUND_MOD_EXPR:
14558 case TRUNC_MOD_EXPR:
14559 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14561 op = DW_OP_mod;
14562 goto do_binop;
14564 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14565 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14566 if (list_ret == 0 || list_ret1 == 0)
14567 return 0;
14569 add_loc_list (&list_ret, list_ret1);
14570 if (list_ret == 0)
14571 return 0;
14572 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14573 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14574 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
14575 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
14576 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
14577 break;
14579 case MULT_EXPR:
14580 op = DW_OP_mul;
14581 goto do_binop;
14583 case LSHIFT_EXPR:
14584 op = DW_OP_shl;
14585 goto do_binop;
14587 case RSHIFT_EXPR:
14588 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
14589 goto do_binop;
14591 case POINTER_PLUS_EXPR:
14592 case PLUS_EXPR:
14593 do_plus:
14594 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
14596 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14597 if (list_ret == 0)
14598 return 0;
14600 loc_list_plus_const (list_ret, tree_to_shwi (TREE_OPERAND (loc, 1)));
14601 break;
14604 op = DW_OP_plus;
14605 goto do_binop;
14607 case LE_EXPR:
14608 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14609 return 0;
14611 op = DW_OP_le;
14612 goto do_binop;
14614 case GE_EXPR:
14615 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14616 return 0;
14618 op = DW_OP_ge;
14619 goto do_binop;
14621 case LT_EXPR:
14622 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14623 return 0;
14625 op = DW_OP_lt;
14626 goto do_binop;
14628 case GT_EXPR:
14629 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14630 return 0;
14632 op = DW_OP_gt;
14633 goto do_binop;
14635 case EQ_EXPR:
14636 op = DW_OP_eq;
14637 goto do_binop;
14639 case NE_EXPR:
14640 op = DW_OP_ne;
14641 goto do_binop;
14643 do_binop:
14644 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14645 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14646 if (list_ret == 0 || list_ret1 == 0)
14647 return 0;
14649 add_loc_list (&list_ret, list_ret1);
14650 if (list_ret == 0)
14651 return 0;
14652 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14653 break;
14655 case TRUTH_NOT_EXPR:
14656 case BIT_NOT_EXPR:
14657 op = DW_OP_not;
14658 goto do_unop;
14660 case ABS_EXPR:
14661 op = DW_OP_abs;
14662 goto do_unop;
14664 case NEGATE_EXPR:
14665 op = DW_OP_neg;
14666 goto do_unop;
14668 do_unop:
14669 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14670 if (list_ret == 0)
14671 return 0;
14673 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14674 break;
14676 case MIN_EXPR:
14677 case MAX_EXPR:
14679 const enum tree_code code =
14680 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
14682 loc = build3 (COND_EXPR, TREE_TYPE (loc),
14683 build2 (code, integer_type_node,
14684 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
14685 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
14688 /* ... fall through ... */
14690 case COND_EXPR:
14692 dw_loc_descr_ref lhs
14693 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
14694 dw_loc_list_ref rhs
14695 = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
14696 dw_loc_descr_ref bra_node, jump_node, tmp;
14698 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14699 if (list_ret == 0 || lhs == 0 || rhs == 0)
14700 return 0;
14702 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14703 add_loc_descr_to_each (list_ret, bra_node);
14705 add_loc_list (&list_ret, rhs);
14706 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
14707 add_loc_descr_to_each (list_ret, jump_node);
14709 add_loc_descr_to_each (list_ret, lhs);
14710 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14711 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
14713 /* ??? Need a node to point the skip at. Use a nop. */
14714 tmp = new_loc_descr (DW_OP_nop, 0, 0);
14715 add_loc_descr_to_each (list_ret, tmp);
14716 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14717 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
14719 break;
14721 case FIX_TRUNC_EXPR:
14722 return 0;
14724 default:
14725 /* Leave front-end specific codes as simply unknown. This comes
14726 up, for instance, with the C STMT_EXPR. */
14727 if ((unsigned int) TREE_CODE (loc)
14728 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
14730 expansion_failed (loc, NULL_RTX,
14731 "language specific tree node");
14732 return 0;
14735 #ifdef ENABLE_CHECKING
14736 /* Otherwise this is a generic code; we should just lists all of
14737 these explicitly. We forgot one. */
14738 gcc_unreachable ();
14739 #else
14740 /* In a release build, we want to degrade gracefully: better to
14741 generate incomplete debugging information than to crash. */
14742 return NULL;
14743 #endif
14746 if (!ret && !list_ret)
14747 return 0;
14749 if (want_address == 2 && !have_address
14750 && (dwarf_version >= 4 || !dwarf_strict))
14752 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14754 expansion_failed (loc, NULL_RTX,
14755 "DWARF address size mismatch");
14756 return 0;
14758 if (ret)
14759 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
14760 else
14761 add_loc_descr_to_each (list_ret,
14762 new_loc_descr (DW_OP_stack_value, 0, 0));
14763 have_address = 1;
14765 /* Show if we can't fill the request for an address. */
14766 if (want_address && !have_address)
14768 expansion_failed (loc, NULL_RTX,
14769 "Want address and only have value");
14770 return 0;
14773 gcc_assert (!ret || !list_ret);
14775 /* If we've got an address and don't want one, dereference. */
14776 if (!want_address && have_address)
14778 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14780 if (size > DWARF2_ADDR_SIZE || size == -1)
14782 expansion_failed (loc, NULL_RTX,
14783 "DWARF address size mismatch");
14784 return 0;
14786 else if (size == DWARF2_ADDR_SIZE)
14787 op = DW_OP_deref;
14788 else
14789 op = DW_OP_deref_size;
14791 if (ret)
14792 add_loc_descr (&ret, new_loc_descr (op, size, 0));
14793 else
14794 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
14796 if (ret)
14797 list_ret = new_loc_list (ret, NULL, NULL, NULL);
14799 return list_ret;
14802 /* Same as above but return only single location expression. */
14803 static dw_loc_descr_ref
14804 loc_descriptor_from_tree (tree loc, int want_address)
14806 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
14807 if (!ret)
14808 return NULL;
14809 if (ret->dw_loc_next)
14811 expansion_failed (loc, NULL_RTX,
14812 "Location list where only loc descriptor needed");
14813 return NULL;
14815 return ret->expr;
14818 /* Given a value, round it up to the lowest multiple of `boundary'
14819 which is not less than the value itself. */
14821 static inline HOST_WIDE_INT
14822 ceiling (HOST_WIDE_INT value, unsigned int boundary)
14824 return (((value + boundary - 1) / boundary) * boundary);
14827 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
14828 pointer to the declared type for the relevant field variable, or return
14829 `integer_type_node' if the given node turns out to be an
14830 ERROR_MARK node. */
14832 static inline tree
14833 field_type (const_tree decl)
14835 tree type;
14837 if (TREE_CODE (decl) == ERROR_MARK)
14838 return integer_type_node;
14840 type = DECL_BIT_FIELD_TYPE (decl);
14841 if (type == NULL_TREE)
14842 type = TREE_TYPE (decl);
14844 return type;
14847 /* Given a pointer to a tree node, return the alignment in bits for
14848 it, or else return BITS_PER_WORD if the node actually turns out to
14849 be an ERROR_MARK node. */
14851 static inline unsigned
14852 simple_type_align_in_bits (const_tree type)
14854 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
14857 static inline unsigned
14858 simple_decl_align_in_bits (const_tree decl)
14860 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
14863 /* Return the result of rounding T up to ALIGN. */
14865 static inline offset_int
14866 round_up_to_align (const offset_int &t, unsigned int align)
14868 return wi::udiv_trunc (t + align - 1, align) * align;
14871 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
14872 lowest addressed byte of the "containing object" for the given FIELD_DECL,
14873 or return 0 if we are unable to determine what that offset is, either
14874 because the argument turns out to be a pointer to an ERROR_MARK node, or
14875 because the offset is actually variable. (We can't handle the latter case
14876 just yet). */
14878 static HOST_WIDE_INT
14879 field_byte_offset (const_tree decl)
14881 offset_int object_offset_in_bits;
14882 offset_int object_offset_in_bytes;
14883 offset_int bitpos_int;
14885 if (TREE_CODE (decl) == ERROR_MARK)
14886 return 0;
14888 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
14890 /* We cannot yet cope with fields whose positions are variable, so
14891 for now, when we see such things, we simply return 0. Someday, we may
14892 be able to handle such cases, but it will be damn difficult. */
14893 if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
14894 return 0;
14896 bitpos_int = wi::to_offset (bit_position (decl));
14898 #ifdef PCC_BITFIELD_TYPE_MATTERS
14899 if (PCC_BITFIELD_TYPE_MATTERS)
14901 tree type;
14902 tree field_size_tree;
14903 offset_int deepest_bitpos;
14904 offset_int field_size_in_bits;
14905 unsigned int type_align_in_bits;
14906 unsigned int decl_align_in_bits;
14907 offset_int type_size_in_bits;
14909 type = field_type (decl);
14910 type_size_in_bits = offset_int_type_size_in_bits (type);
14911 type_align_in_bits = simple_type_align_in_bits (type);
14913 field_size_tree = DECL_SIZE (decl);
14915 /* The size could be unspecified if there was an error, or for
14916 a flexible array member. */
14917 if (!field_size_tree)
14918 field_size_tree = bitsize_zero_node;
14920 /* If the size of the field is not constant, use the type size. */
14921 if (TREE_CODE (field_size_tree) == INTEGER_CST)
14922 field_size_in_bits = wi::to_offset (field_size_tree);
14923 else
14924 field_size_in_bits = type_size_in_bits;
14926 decl_align_in_bits = simple_decl_align_in_bits (decl);
14928 /* The GCC front-end doesn't make any attempt to keep track of the
14929 starting bit offset (relative to the start of the containing
14930 structure type) of the hypothetical "containing object" for a
14931 bit-field. Thus, when computing the byte offset value for the
14932 start of the "containing object" of a bit-field, we must deduce
14933 this information on our own. This can be rather tricky to do in
14934 some cases. For example, handling the following structure type
14935 definition when compiling for an i386/i486 target (which only
14936 aligns long long's to 32-bit boundaries) can be very tricky:
14938 struct S { int field1; long long field2:31; };
14940 Fortunately, there is a simple rule-of-thumb which can be used
14941 in such cases. When compiling for an i386/i486, GCC will
14942 allocate 8 bytes for the structure shown above. It decides to
14943 do this based upon one simple rule for bit-field allocation.
14944 GCC allocates each "containing object" for each bit-field at
14945 the first (i.e. lowest addressed) legitimate alignment boundary
14946 (based upon the required minimum alignment for the declared
14947 type of the field) which it can possibly use, subject to the
14948 condition that there is still enough available space remaining
14949 in the containing object (when allocated at the selected point)
14950 to fully accommodate all of the bits of the bit-field itself.
14952 This simple rule makes it obvious why GCC allocates 8 bytes for
14953 each object of the structure type shown above. When looking
14954 for a place to allocate the "containing object" for `field2',
14955 the compiler simply tries to allocate a 64-bit "containing
14956 object" at each successive 32-bit boundary (starting at zero)
14957 until it finds a place to allocate that 64- bit field such that
14958 at least 31 contiguous (and previously unallocated) bits remain
14959 within that selected 64 bit field. (As it turns out, for the
14960 example above, the compiler finds it is OK to allocate the
14961 "containing object" 64-bit field at bit-offset zero within the
14962 structure type.)
14964 Here we attempt to work backwards from the limited set of facts
14965 we're given, and we try to deduce from those facts, where GCC
14966 must have believed that the containing object started (within
14967 the structure type). The value we deduce is then used (by the
14968 callers of this routine) to generate DW_AT_location and
14969 DW_AT_bit_offset attributes for fields (both bit-fields and, in
14970 the case of DW_AT_location, regular fields as well). */
14972 /* Figure out the bit-distance from the start of the structure to
14973 the "deepest" bit of the bit-field. */
14974 deepest_bitpos = bitpos_int + field_size_in_bits;
14976 /* This is the tricky part. Use some fancy footwork to deduce
14977 where the lowest addressed bit of the containing object must
14978 be. */
14979 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
14981 /* Round up to type_align by default. This works best for
14982 bitfields. */
14983 object_offset_in_bits
14984 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
14986 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
14988 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
14990 /* Round up to decl_align instead. */
14991 object_offset_in_bits
14992 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
14995 else
14996 #endif /* PCC_BITFIELD_TYPE_MATTERS */
14997 object_offset_in_bits = bitpos_int;
14999 object_offset_in_bytes
15000 = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
15001 return object_offset_in_bytes.to_shwi ();
15004 /* The following routines define various Dwarf attributes and any data
15005 associated with them. */
15007 /* Add a location description attribute value to a DIE.
15009 This emits location attributes suitable for whole variables and
15010 whole parameters. Note that the location attributes for struct fields are
15011 generated by the routine `data_member_location_attribute' below. */
15013 static inline void
15014 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
15015 dw_loc_list_ref descr)
15017 if (descr == 0)
15018 return;
15019 if (single_element_loc_list_p (descr))
15020 add_AT_loc (die, attr_kind, descr->expr);
15021 else
15022 add_AT_loc_list (die, attr_kind, descr);
15025 /* Add DW_AT_accessibility attribute to DIE if needed. */
15027 static void
15028 add_accessibility_attribute (dw_die_ref die, tree decl)
15030 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
15031 children, otherwise the default is DW_ACCESS_public. In DWARF2
15032 the default has always been DW_ACCESS_public. */
15033 if (TREE_PROTECTED (decl))
15034 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
15035 else if (TREE_PRIVATE (decl))
15037 if (dwarf_version == 2
15038 || die->die_parent == NULL
15039 || die->die_parent->die_tag != DW_TAG_class_type)
15040 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
15042 else if (dwarf_version > 2
15043 && die->die_parent
15044 && die->die_parent->die_tag == DW_TAG_class_type)
15045 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
15048 /* Attach the specialized form of location attribute used for data members of
15049 struct and union types. In the special case of a FIELD_DECL node which
15050 represents a bit-field, the "offset" part of this special location
15051 descriptor must indicate the distance in bytes from the lowest-addressed
15052 byte of the containing struct or union type to the lowest-addressed byte of
15053 the "containing object" for the bit-field. (See the `field_byte_offset'
15054 function above).
15056 For any given bit-field, the "containing object" is a hypothetical object
15057 (of some integral or enum type) within which the given bit-field lives. The
15058 type of this hypothetical "containing object" is always the same as the
15059 declared type of the individual bit-field itself (for GCC anyway... the
15060 DWARF spec doesn't actually mandate this). Note that it is the size (in
15061 bytes) of the hypothetical "containing object" which will be given in the
15062 DW_AT_byte_size attribute for this bit-field. (See the
15063 `byte_size_attribute' function below.) It is also used when calculating the
15064 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
15065 function below.) */
15067 static void
15068 add_data_member_location_attribute (dw_die_ref die, tree decl)
15070 HOST_WIDE_INT offset;
15071 dw_loc_descr_ref loc_descr = 0;
15073 if (TREE_CODE (decl) == TREE_BINFO)
15075 /* We're working on the TAG_inheritance for a base class. */
15076 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
15078 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
15079 aren't at a fixed offset from all (sub)objects of the same
15080 type. We need to extract the appropriate offset from our
15081 vtable. The following dwarf expression means
15083 BaseAddr = ObAddr + *((*ObAddr) - Offset)
15085 This is specific to the V3 ABI, of course. */
15087 dw_loc_descr_ref tmp;
15089 /* Make a copy of the object address. */
15090 tmp = new_loc_descr (DW_OP_dup, 0, 0);
15091 add_loc_descr (&loc_descr, tmp);
15093 /* Extract the vtable address. */
15094 tmp = new_loc_descr (DW_OP_deref, 0, 0);
15095 add_loc_descr (&loc_descr, tmp);
15097 /* Calculate the address of the offset. */
15098 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
15099 gcc_assert (offset < 0);
15101 tmp = int_loc_descriptor (-offset);
15102 add_loc_descr (&loc_descr, tmp);
15103 tmp = new_loc_descr (DW_OP_minus, 0, 0);
15104 add_loc_descr (&loc_descr, tmp);
15106 /* Extract the offset. */
15107 tmp = new_loc_descr (DW_OP_deref, 0, 0);
15108 add_loc_descr (&loc_descr, tmp);
15110 /* Add it to the object address. */
15111 tmp = new_loc_descr (DW_OP_plus, 0, 0);
15112 add_loc_descr (&loc_descr, tmp);
15114 else
15115 offset = tree_to_shwi (BINFO_OFFSET (decl));
15117 else
15118 offset = field_byte_offset (decl);
15120 if (! loc_descr)
15122 if (dwarf_version > 2)
15124 /* Don't need to output a location expression, just the constant. */
15125 if (offset < 0)
15126 add_AT_int (die, DW_AT_data_member_location, offset);
15127 else
15128 add_AT_unsigned (die, DW_AT_data_member_location, offset);
15129 return;
15131 else
15133 enum dwarf_location_atom op;
15135 /* The DWARF2 standard says that we should assume that the structure
15136 address is already on the stack, so we can specify a structure
15137 field address by using DW_OP_plus_uconst. */
15138 op = DW_OP_plus_uconst;
15139 loc_descr = new_loc_descr (op, offset, 0);
15143 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
15146 /* Writes integer values to dw_vec_const array. */
15148 static void
15149 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
15151 while (size != 0)
15153 *dest++ = val & 0xff;
15154 val >>= 8;
15155 --size;
15159 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
15161 static HOST_WIDE_INT
15162 extract_int (const unsigned char *src, unsigned int size)
15164 HOST_WIDE_INT val = 0;
15166 src += size;
15167 while (size != 0)
15169 val <<= 8;
15170 val |= *--src & 0xff;
15171 --size;
15173 return val;
15176 /* Writes wide_int values to dw_vec_const array. */
15178 static void
15179 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
15181 int i;
15183 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
15185 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
15186 return;
15189 /* We'd have to extend this code to support odd sizes. */
15190 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
15192 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
15194 if (WORDS_BIG_ENDIAN)
15195 for (i = n - 1; i >= 0; i--)
15197 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
15198 dest += sizeof (HOST_WIDE_INT);
15200 else
15201 for (i = 0; i < n; i++)
15203 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
15204 dest += sizeof (HOST_WIDE_INT);
15208 /* Writes floating point values to dw_vec_const array. */
15210 static void
15211 insert_float (const_rtx rtl, unsigned char *array)
15213 REAL_VALUE_TYPE rv;
15214 long val[4];
15215 int i;
15217 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
15218 real_to_target (val, &rv, GET_MODE (rtl));
15220 /* real_to_target puts 32-bit pieces in each long. Pack them. */
15221 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
15223 insert_int (val[i], 4, array);
15224 array += 4;
15228 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
15229 does not have a "location" either in memory or in a register. These
15230 things can arise in GNU C when a constant is passed as an actual parameter
15231 to an inlined function. They can also arise in C++ where declared
15232 constants do not necessarily get memory "homes". */
15234 static bool
15235 add_const_value_attribute (dw_die_ref die, rtx rtl)
15237 switch (GET_CODE (rtl))
15239 case CONST_INT:
15241 HOST_WIDE_INT val = INTVAL (rtl);
15243 if (val < 0)
15244 add_AT_int (die, DW_AT_const_value, val);
15245 else
15246 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
15248 return true;
15250 case CONST_WIDE_INT:
15251 add_AT_wide (die, DW_AT_const_value,
15252 std::make_pair (rtl, GET_MODE (rtl)));
15253 return true;
15255 case CONST_DOUBLE:
15256 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
15257 floating-point constant. A CONST_DOUBLE is used whenever the
15258 constant requires more than one word in order to be adequately
15259 represented. */
15261 enum machine_mode mode = GET_MODE (rtl);
15263 if (TARGET_SUPPORTS_WIDE_INT == 0 && !SCALAR_FLOAT_MODE_P (mode))
15264 add_AT_double (die, DW_AT_const_value,
15265 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
15266 else
15268 unsigned int length = GET_MODE_SIZE (mode);
15269 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15271 insert_float (rtl, array);
15272 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
15275 return true;
15277 case CONST_VECTOR:
15279 enum machine_mode mode = GET_MODE (rtl);
15280 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
15281 unsigned int length = CONST_VECTOR_NUNITS (rtl);
15282 unsigned char *array
15283 = ggc_vec_alloc<unsigned char> (length * elt_size);
15284 unsigned int i;
15285 unsigned char *p;
15286 enum machine_mode imode = GET_MODE_INNER (mode);
15288 switch (GET_MODE_CLASS (mode))
15290 case MODE_VECTOR_INT:
15291 for (i = 0, p = array; i < length; i++, p += elt_size)
15293 rtx elt = CONST_VECTOR_ELT (rtl, i);
15294 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
15296 break;
15298 case MODE_VECTOR_FLOAT:
15299 for (i = 0, p = array; i < length; i++, p += elt_size)
15301 rtx elt = CONST_VECTOR_ELT (rtl, i);
15302 insert_float (elt, p);
15304 break;
15306 default:
15307 gcc_unreachable ();
15310 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
15312 return true;
15314 case CONST_STRING:
15315 if (dwarf_version >= 4 || !dwarf_strict)
15317 dw_loc_descr_ref loc_result;
15318 resolve_one_addr (&rtl);
15319 rtl_addr:
15320 loc_result = new_addr_loc_descr (rtl, dtprel_false);
15321 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
15322 add_AT_loc (die, DW_AT_location, loc_result);
15323 vec_safe_push (used_rtx_array, rtl);
15324 return true;
15326 return false;
15328 case CONST:
15329 if (CONSTANT_P (XEXP (rtl, 0)))
15330 return add_const_value_attribute (die, XEXP (rtl, 0));
15331 /* FALLTHROUGH */
15332 case SYMBOL_REF:
15333 if (!const_ok_for_output (rtl))
15334 return false;
15335 case LABEL_REF:
15336 if (dwarf_version >= 4 || !dwarf_strict)
15337 goto rtl_addr;
15338 return false;
15340 case PLUS:
15341 /* In cases where an inlined instance of an inline function is passed
15342 the address of an `auto' variable (which is local to the caller) we
15343 can get a situation where the DECL_RTL of the artificial local
15344 variable (for the inlining) which acts as a stand-in for the
15345 corresponding formal parameter (of the inline function) will look
15346 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
15347 exactly a compile-time constant expression, but it isn't the address
15348 of the (artificial) local variable either. Rather, it represents the
15349 *value* which the artificial local variable always has during its
15350 lifetime. We currently have no way to represent such quasi-constant
15351 values in Dwarf, so for now we just punt and generate nothing. */
15352 return false;
15354 case HIGH:
15355 case CONST_FIXED:
15356 return false;
15358 case MEM:
15359 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
15360 && MEM_READONLY_P (rtl)
15361 && GET_MODE (rtl) == BLKmode)
15363 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
15364 return true;
15366 return false;
15368 default:
15369 /* No other kinds of rtx should be possible here. */
15370 gcc_unreachable ();
15372 return false;
15375 /* Determine whether the evaluation of EXPR references any variables
15376 or functions which aren't otherwise used (and therefore may not be
15377 output). */
15378 static tree
15379 reference_to_unused (tree * tp, int * walk_subtrees,
15380 void * data ATTRIBUTE_UNUSED)
15382 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
15383 *walk_subtrees = 0;
15385 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
15386 && ! TREE_ASM_WRITTEN (*tp))
15387 return *tp;
15388 /* ??? The C++ FE emits debug information for using decls, so
15389 putting gcc_unreachable here falls over. See PR31899. For now
15390 be conservative. */
15391 else if (!symtab->global_info_ready
15392 && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
15393 return *tp;
15394 else if (TREE_CODE (*tp) == VAR_DECL)
15396 varpool_node *node = varpool_node::get (*tp);
15397 if (!node || !node->definition)
15398 return *tp;
15400 else if (TREE_CODE (*tp) == FUNCTION_DECL
15401 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
15403 /* The call graph machinery must have finished analyzing,
15404 optimizing and gimplifying the CU by now.
15405 So if *TP has no call graph node associated
15406 to it, it means *TP will not be emitted. */
15407 if (!cgraph_node::get (*tp))
15408 return *tp;
15410 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
15411 return *tp;
15413 return NULL_TREE;
15416 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
15417 for use in a later add_const_value_attribute call. */
15419 static rtx
15420 rtl_for_decl_init (tree init, tree type)
15422 rtx rtl = NULL_RTX;
15424 STRIP_NOPS (init);
15426 /* If a variable is initialized with a string constant without embedded
15427 zeros, build CONST_STRING. */
15428 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
15430 tree enttype = TREE_TYPE (type);
15431 tree domain = TYPE_DOMAIN (type);
15432 enum machine_mode mode = TYPE_MODE (enttype);
15434 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
15435 && domain
15436 && integer_zerop (TYPE_MIN_VALUE (domain))
15437 && compare_tree_int (TYPE_MAX_VALUE (domain),
15438 TREE_STRING_LENGTH (init) - 1) == 0
15439 && ((size_t) TREE_STRING_LENGTH (init)
15440 == strlen (TREE_STRING_POINTER (init)) + 1))
15442 rtl = gen_rtx_CONST_STRING (VOIDmode,
15443 ggc_strdup (TREE_STRING_POINTER (init)));
15444 rtl = gen_rtx_MEM (BLKmode, rtl);
15445 MEM_READONLY_P (rtl) = 1;
15448 /* Other aggregates, and complex values, could be represented using
15449 CONCAT: FIXME! */
15450 else if (AGGREGATE_TYPE_P (type)
15451 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
15452 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
15453 || TREE_CODE (type) == COMPLEX_TYPE)
15455 /* Vectors only work if their mode is supported by the target.
15456 FIXME: generic vectors ought to work too. */
15457 else if (TREE_CODE (type) == VECTOR_TYPE
15458 && !VECTOR_MODE_P (TYPE_MODE (type)))
15460 /* If the initializer is something that we know will expand into an
15461 immediate RTL constant, expand it now. We must be careful not to
15462 reference variables which won't be output. */
15463 else if (initializer_constant_valid_p (init, type)
15464 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
15466 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
15467 possible. */
15468 if (TREE_CODE (type) == VECTOR_TYPE)
15469 switch (TREE_CODE (init))
15471 case VECTOR_CST:
15472 break;
15473 case CONSTRUCTOR:
15474 if (TREE_CONSTANT (init))
15476 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
15477 bool constant_p = true;
15478 tree value;
15479 unsigned HOST_WIDE_INT ix;
15481 /* Even when ctor is constant, it might contain non-*_CST
15482 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
15483 belong into VECTOR_CST nodes. */
15484 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
15485 if (!CONSTANT_CLASS_P (value))
15487 constant_p = false;
15488 break;
15491 if (constant_p)
15493 init = build_vector_from_ctor (type, elts);
15494 break;
15497 /* FALLTHRU */
15499 default:
15500 return NULL;
15503 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
15505 /* If expand_expr returns a MEM, it wasn't immediate. */
15506 gcc_assert (!rtl || !MEM_P (rtl));
15509 return rtl;
15512 /* Generate RTL for the variable DECL to represent its location. */
15514 static rtx
15515 rtl_for_decl_location (tree decl)
15517 rtx rtl;
15519 /* Here we have to decide where we are going to say the parameter "lives"
15520 (as far as the debugger is concerned). We only have a couple of
15521 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
15523 DECL_RTL normally indicates where the parameter lives during most of the
15524 activation of the function. If optimization is enabled however, this
15525 could be either NULL or else a pseudo-reg. Both of those cases indicate
15526 that the parameter doesn't really live anywhere (as far as the code
15527 generation parts of GCC are concerned) during most of the function's
15528 activation. That will happen (for example) if the parameter is never
15529 referenced within the function.
15531 We could just generate a location descriptor here for all non-NULL
15532 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
15533 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
15534 where DECL_RTL is NULL or is a pseudo-reg.
15536 Note however that we can only get away with using DECL_INCOMING_RTL as
15537 a backup substitute for DECL_RTL in certain limited cases. In cases
15538 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
15539 we can be sure that the parameter was passed using the same type as it is
15540 declared to have within the function, and that its DECL_INCOMING_RTL
15541 points us to a place where a value of that type is passed.
15543 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
15544 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
15545 because in these cases DECL_INCOMING_RTL points us to a value of some
15546 type which is *different* from the type of the parameter itself. Thus,
15547 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
15548 such cases, the debugger would end up (for example) trying to fetch a
15549 `float' from a place which actually contains the first part of a
15550 `double'. That would lead to really incorrect and confusing
15551 output at debug-time.
15553 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
15554 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
15555 are a couple of exceptions however. On little-endian machines we can
15556 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
15557 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
15558 an integral type that is smaller than TREE_TYPE (decl). These cases arise
15559 when (on a little-endian machine) a non-prototyped function has a
15560 parameter declared to be of type `short' or `char'. In such cases,
15561 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
15562 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
15563 passed `int' value. If the debugger then uses that address to fetch
15564 a `short' or a `char' (on a little-endian machine) the result will be
15565 the correct data, so we allow for such exceptional cases below.
15567 Note that our goal here is to describe the place where the given formal
15568 parameter lives during most of the function's activation (i.e. between the
15569 end of the prologue and the start of the epilogue). We'll do that as best
15570 as we can. Note however that if the given formal parameter is modified
15571 sometime during the execution of the function, then a stack backtrace (at
15572 debug-time) will show the function as having been called with the *new*
15573 value rather than the value which was originally passed in. This happens
15574 rarely enough that it is not a major problem, but it *is* a problem, and
15575 I'd like to fix it.
15577 A future version of dwarf2out.c may generate two additional attributes for
15578 any given DW_TAG_formal_parameter DIE which will describe the "passed
15579 type" and the "passed location" for the given formal parameter in addition
15580 to the attributes we now generate to indicate the "declared type" and the
15581 "active location" for each parameter. This additional set of attributes
15582 could be used by debuggers for stack backtraces. Separately, note that
15583 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
15584 This happens (for example) for inlined-instances of inline function formal
15585 parameters which are never referenced. This really shouldn't be
15586 happening. All PARM_DECL nodes should get valid non-NULL
15587 DECL_INCOMING_RTL values. FIXME. */
15589 /* Use DECL_RTL as the "location" unless we find something better. */
15590 rtl = DECL_RTL_IF_SET (decl);
15592 /* When generating abstract instances, ignore everything except
15593 constants, symbols living in memory, and symbols living in
15594 fixed registers. */
15595 if (! reload_completed)
15597 if (rtl
15598 && (CONSTANT_P (rtl)
15599 || (MEM_P (rtl)
15600 && CONSTANT_P (XEXP (rtl, 0)))
15601 || (REG_P (rtl)
15602 && TREE_CODE (decl) == VAR_DECL
15603 && TREE_STATIC (decl))))
15605 rtl = targetm.delegitimize_address (rtl);
15606 return rtl;
15608 rtl = NULL_RTX;
15610 else if (TREE_CODE (decl) == PARM_DECL)
15612 if (rtl == NULL_RTX
15613 || is_pseudo_reg (rtl)
15614 || (MEM_P (rtl)
15615 && is_pseudo_reg (XEXP (rtl, 0))
15616 && DECL_INCOMING_RTL (decl)
15617 && MEM_P (DECL_INCOMING_RTL (decl))
15618 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
15620 tree declared_type = TREE_TYPE (decl);
15621 tree passed_type = DECL_ARG_TYPE (decl);
15622 enum machine_mode dmode = TYPE_MODE (declared_type);
15623 enum machine_mode pmode = TYPE_MODE (passed_type);
15625 /* This decl represents a formal parameter which was optimized out.
15626 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
15627 all cases where (rtl == NULL_RTX) just below. */
15628 if (dmode == pmode)
15629 rtl = DECL_INCOMING_RTL (decl);
15630 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
15631 && SCALAR_INT_MODE_P (dmode)
15632 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
15633 && DECL_INCOMING_RTL (decl))
15635 rtx inc = DECL_INCOMING_RTL (decl);
15636 if (REG_P (inc))
15637 rtl = inc;
15638 else if (MEM_P (inc))
15640 if (BYTES_BIG_ENDIAN)
15641 rtl = adjust_address_nv (inc, dmode,
15642 GET_MODE_SIZE (pmode)
15643 - GET_MODE_SIZE (dmode));
15644 else
15645 rtl = inc;
15650 /* If the parm was passed in registers, but lives on the stack, then
15651 make a big endian correction if the mode of the type of the
15652 parameter is not the same as the mode of the rtl. */
15653 /* ??? This is the same series of checks that are made in dbxout.c before
15654 we reach the big endian correction code there. It isn't clear if all
15655 of these checks are necessary here, but keeping them all is the safe
15656 thing to do. */
15657 else if (MEM_P (rtl)
15658 && XEXP (rtl, 0) != const0_rtx
15659 && ! CONSTANT_P (XEXP (rtl, 0))
15660 /* Not passed in memory. */
15661 && !MEM_P (DECL_INCOMING_RTL (decl))
15662 /* Not passed by invisible reference. */
15663 && (!REG_P (XEXP (rtl, 0))
15664 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
15665 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
15666 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
15667 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
15668 #endif
15670 /* Big endian correction check. */
15671 && BYTES_BIG_ENDIAN
15672 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
15673 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
15674 < UNITS_PER_WORD))
15676 enum machine_mode addr_mode = get_address_mode (rtl);
15677 int offset = (UNITS_PER_WORD
15678 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
15680 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15681 plus_constant (addr_mode, XEXP (rtl, 0), offset));
15684 else if (TREE_CODE (decl) == VAR_DECL
15685 && rtl
15686 && MEM_P (rtl)
15687 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
15688 && BYTES_BIG_ENDIAN)
15690 enum machine_mode addr_mode = get_address_mode (rtl);
15691 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
15692 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
15694 /* If a variable is declared "register" yet is smaller than
15695 a register, then if we store the variable to memory, it
15696 looks like we're storing a register-sized value, when in
15697 fact we are not. We need to adjust the offset of the
15698 storage location to reflect the actual value's bytes,
15699 else gdb will not be able to display it. */
15700 if (rsize > dsize)
15701 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15702 plus_constant (addr_mode, XEXP (rtl, 0),
15703 rsize - dsize));
15706 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
15707 and will have been substituted directly into all expressions that use it.
15708 C does not have such a concept, but C++ and other languages do. */
15709 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
15710 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
15712 if (rtl)
15713 rtl = targetm.delegitimize_address (rtl);
15715 /* If we don't look past the constant pool, we risk emitting a
15716 reference to a constant pool entry that isn't referenced from
15717 code, and thus is not emitted. */
15718 if (rtl)
15719 rtl = avoid_constant_pool_reference (rtl);
15721 /* Try harder to get a rtl. If this symbol ends up not being emitted
15722 in the current CU, resolve_addr will remove the expression referencing
15723 it. */
15724 if (rtl == NULL_RTX
15725 && TREE_CODE (decl) == VAR_DECL
15726 && !DECL_EXTERNAL (decl)
15727 && TREE_STATIC (decl)
15728 && DECL_NAME (decl)
15729 && !DECL_HARD_REGISTER (decl)
15730 && DECL_MODE (decl) != VOIDmode)
15732 rtl = make_decl_rtl_for_debug (decl);
15733 if (!MEM_P (rtl)
15734 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
15735 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
15736 rtl = NULL_RTX;
15739 return rtl;
15742 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
15743 returned. If so, the decl for the COMMON block is returned, and the
15744 value is the offset into the common block for the symbol. */
15746 static tree
15747 fortran_common (tree decl, HOST_WIDE_INT *value)
15749 tree val_expr, cvar;
15750 enum machine_mode mode;
15751 HOST_WIDE_INT bitsize, bitpos;
15752 tree offset;
15753 int unsignedp, volatilep = 0;
15755 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
15756 it does not have a value (the offset into the common area), or if it
15757 is thread local (as opposed to global) then it isn't common, and shouldn't
15758 be handled as such. */
15759 if (TREE_CODE (decl) != VAR_DECL
15760 || !TREE_STATIC (decl)
15761 || !DECL_HAS_VALUE_EXPR_P (decl)
15762 || !is_fortran ())
15763 return NULL_TREE;
15765 val_expr = DECL_VALUE_EXPR (decl);
15766 if (TREE_CODE (val_expr) != COMPONENT_REF)
15767 return NULL_TREE;
15769 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
15770 &mode, &unsignedp, &volatilep, true);
15772 if (cvar == NULL_TREE
15773 || TREE_CODE (cvar) != VAR_DECL
15774 || DECL_ARTIFICIAL (cvar)
15775 || !TREE_PUBLIC (cvar))
15776 return NULL_TREE;
15778 *value = 0;
15779 if (offset != NULL)
15781 if (!tree_fits_shwi_p (offset))
15782 return NULL_TREE;
15783 *value = tree_to_shwi (offset);
15785 if (bitpos != 0)
15786 *value += bitpos / BITS_PER_UNIT;
15788 return cvar;
15791 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
15792 data attribute for a variable or a parameter. We generate the
15793 DW_AT_const_value attribute only in those cases where the given variable
15794 or parameter does not have a true "location" either in memory or in a
15795 register. This can happen (for example) when a constant is passed as an
15796 actual argument in a call to an inline function. (It's possible that
15797 these things can crop up in other ways also.) Note that one type of
15798 constant value which can be passed into an inlined function is a constant
15799 pointer. This can happen for example if an actual argument in an inlined
15800 function call evaluates to a compile-time constant address.
15802 CACHE_P is true if it is worth caching the location list for DECL,
15803 so that future calls can reuse it rather than regenerate it from scratch.
15804 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
15805 since we will need to refer to them each time the function is inlined. */
15807 static bool
15808 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
15809 enum dwarf_attribute attr)
15811 rtx rtl;
15812 dw_loc_list_ref list;
15813 var_loc_list *loc_list;
15814 cached_dw_loc_list *cache;
15815 void **slot;
15817 if (TREE_CODE (decl) == ERROR_MARK)
15818 return false;
15820 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
15821 || TREE_CODE (decl) == RESULT_DECL);
15823 /* Try to get some constant RTL for this decl, and use that as the value of
15824 the location. */
15826 rtl = rtl_for_decl_location (decl);
15827 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15828 && add_const_value_attribute (die, rtl))
15829 return true;
15831 /* See if we have single element location list that is equivalent to
15832 a constant value. That way we are better to use add_const_value_attribute
15833 rather than expanding constant value equivalent. */
15834 loc_list = lookup_decl_loc (decl);
15835 if (loc_list
15836 && loc_list->first
15837 && loc_list->first->next == NULL
15838 && NOTE_P (loc_list->first->loc)
15839 && NOTE_VAR_LOCATION (loc_list->first->loc)
15840 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
15842 struct var_loc_node *node;
15844 node = loc_list->first;
15845 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
15846 if (GET_CODE (rtl) == EXPR_LIST)
15847 rtl = XEXP (rtl, 0);
15848 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15849 && add_const_value_attribute (die, rtl))
15850 return true;
15852 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
15853 list several times. See if we've already cached the contents. */
15854 list = NULL;
15855 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
15856 cache_p = false;
15857 if (cache_p)
15859 cache = (cached_dw_loc_list *)
15860 htab_find_with_hash (cached_dw_loc_list_table, decl, DECL_UID (decl));
15861 if (cache)
15862 list = cache->loc_list;
15864 if (list == NULL)
15866 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
15867 /* It is usually worth caching this result if the decl is from
15868 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
15869 if (cache_p && list && list->dw_loc_next)
15871 slot = htab_find_slot_with_hash (cached_dw_loc_list_table, decl,
15872 DECL_UID (decl), INSERT);
15873 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
15874 cache->decl_id = DECL_UID (decl);
15875 cache->loc_list = list;
15876 *slot = cache;
15879 if (list)
15881 add_AT_location_description (die, attr, list);
15882 return true;
15884 /* None of that worked, so it must not really have a location;
15885 try adding a constant value attribute from the DECL_INITIAL. */
15886 return tree_add_const_value_attribute_for_decl (die, decl);
15889 /* Add VARIABLE and DIE into deferred locations list. */
15891 static void
15892 defer_location (tree variable, dw_die_ref die)
15894 deferred_locations entry;
15895 entry.variable = variable;
15896 entry.die = die;
15897 vec_safe_push (deferred_locations_list, entry);
15900 /* Helper function for tree_add_const_value_attribute. Natively encode
15901 initializer INIT into an array. Return true if successful. */
15903 static bool
15904 native_encode_initializer (tree init, unsigned char *array, int size)
15906 tree type;
15908 if (init == NULL_TREE)
15909 return false;
15911 STRIP_NOPS (init);
15912 switch (TREE_CODE (init))
15914 case STRING_CST:
15915 type = TREE_TYPE (init);
15916 if (TREE_CODE (type) == ARRAY_TYPE)
15918 tree enttype = TREE_TYPE (type);
15919 enum machine_mode mode = TYPE_MODE (enttype);
15921 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
15922 return false;
15923 if (int_size_in_bytes (type) != size)
15924 return false;
15925 if (size > TREE_STRING_LENGTH (init))
15927 memcpy (array, TREE_STRING_POINTER (init),
15928 TREE_STRING_LENGTH (init));
15929 memset (array + TREE_STRING_LENGTH (init),
15930 '\0', size - TREE_STRING_LENGTH (init));
15932 else
15933 memcpy (array, TREE_STRING_POINTER (init), size);
15934 return true;
15936 return false;
15937 case CONSTRUCTOR:
15938 type = TREE_TYPE (init);
15939 if (int_size_in_bytes (type) != size)
15940 return false;
15941 if (TREE_CODE (type) == ARRAY_TYPE)
15943 HOST_WIDE_INT min_index;
15944 unsigned HOST_WIDE_INT cnt;
15945 int curpos = 0, fieldsize;
15946 constructor_elt *ce;
15948 if (TYPE_DOMAIN (type) == NULL_TREE
15949 || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
15950 return false;
15952 fieldsize = int_size_in_bytes (TREE_TYPE (type));
15953 if (fieldsize <= 0)
15954 return false;
15956 min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
15957 memset (array, '\0', size);
15958 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
15960 tree val = ce->value;
15961 tree index = ce->index;
15962 int pos = curpos;
15963 if (index && TREE_CODE (index) == RANGE_EXPR)
15964 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
15965 * fieldsize;
15966 else if (index)
15967 pos = (tree_to_shwi (index) - min_index) * fieldsize;
15969 if (val)
15971 STRIP_NOPS (val);
15972 if (!native_encode_initializer (val, array + pos, fieldsize))
15973 return false;
15975 curpos = pos + fieldsize;
15976 if (index && TREE_CODE (index) == RANGE_EXPR)
15978 int count = tree_to_shwi (TREE_OPERAND (index, 1))
15979 - tree_to_shwi (TREE_OPERAND (index, 0));
15980 while (count-- > 0)
15982 if (val)
15983 memcpy (array + curpos, array + pos, fieldsize);
15984 curpos += fieldsize;
15987 gcc_assert (curpos <= size);
15989 return true;
15991 else if (TREE_CODE (type) == RECORD_TYPE
15992 || TREE_CODE (type) == UNION_TYPE)
15994 tree field = NULL_TREE;
15995 unsigned HOST_WIDE_INT cnt;
15996 constructor_elt *ce;
15998 if (int_size_in_bytes (type) != size)
15999 return false;
16001 if (TREE_CODE (type) == RECORD_TYPE)
16002 field = TYPE_FIELDS (type);
16004 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
16006 tree val = ce->value;
16007 int pos, fieldsize;
16009 if (ce->index != 0)
16010 field = ce->index;
16012 if (val)
16013 STRIP_NOPS (val);
16015 if (field == NULL_TREE || DECL_BIT_FIELD (field))
16016 return false;
16018 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
16019 && TYPE_DOMAIN (TREE_TYPE (field))
16020 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
16021 return false;
16022 else if (DECL_SIZE_UNIT (field) == NULL_TREE
16023 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
16024 return false;
16025 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
16026 pos = int_byte_position (field);
16027 gcc_assert (pos + fieldsize <= size);
16028 if (val
16029 && !native_encode_initializer (val, array + pos, fieldsize))
16030 return false;
16032 return true;
16034 return false;
16035 case VIEW_CONVERT_EXPR:
16036 case NON_LVALUE_EXPR:
16037 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
16038 default:
16039 return native_encode_expr (init, array, size) == size;
16043 /* Attach a DW_AT_const_value attribute to DIE. The value of the
16044 attribute is the const value T. */
16046 static bool
16047 tree_add_const_value_attribute (dw_die_ref die, tree t)
16049 tree init;
16050 tree type = TREE_TYPE (t);
16051 rtx rtl;
16053 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
16054 return false;
16056 init = t;
16057 gcc_assert (!DECL_P (init));
16059 rtl = rtl_for_decl_init (init, type);
16060 if (rtl)
16061 return add_const_value_attribute (die, rtl);
16062 /* If the host and target are sane, try harder. */
16063 else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
16064 && initializer_constant_valid_p (init, type))
16066 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
16067 if (size > 0 && (int) size == size)
16069 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
16071 if (native_encode_initializer (init, array, size))
16073 add_AT_vec (die, DW_AT_const_value, size, 1, array);
16074 return true;
16076 ggc_free (array);
16079 return false;
16082 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
16083 attribute is the const value of T, where T is an integral constant
16084 variable with static storage duration
16085 (so it can't be a PARM_DECL or a RESULT_DECL). */
16087 static bool
16088 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
16091 if (!decl
16092 || (TREE_CODE (decl) != VAR_DECL
16093 && TREE_CODE (decl) != CONST_DECL)
16094 || (TREE_CODE (decl) == VAR_DECL
16095 && !TREE_STATIC (decl)))
16096 return false;
16098 if (TREE_READONLY (decl)
16099 && ! TREE_THIS_VOLATILE (decl)
16100 && DECL_INITIAL (decl))
16101 /* OK */;
16102 else
16103 return false;
16105 /* Don't add DW_AT_const_value if abstract origin already has one. */
16106 if (get_AT (var_die, DW_AT_const_value))
16107 return false;
16109 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
16112 /* Convert the CFI instructions for the current function into a
16113 location list. This is used for DW_AT_frame_base when we targeting
16114 a dwarf2 consumer that does not support the dwarf3
16115 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
16116 expressions. */
16118 static dw_loc_list_ref
16119 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
16121 int ix;
16122 dw_fde_ref fde;
16123 dw_loc_list_ref list, *list_tail;
16124 dw_cfi_ref cfi;
16125 dw_cfa_location last_cfa, next_cfa;
16126 const char *start_label, *last_label, *section;
16127 dw_cfa_location remember;
16129 fde = cfun->fde;
16130 gcc_assert (fde != NULL);
16132 section = secname_for_decl (current_function_decl);
16133 list_tail = &list;
16134 list = NULL;
16136 memset (&next_cfa, 0, sizeof (next_cfa));
16137 next_cfa.reg = INVALID_REGNUM;
16138 remember = next_cfa;
16140 start_label = fde->dw_fde_begin;
16142 /* ??? Bald assumption that the CIE opcode list does not contain
16143 advance opcodes. */
16144 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
16145 lookup_cfa_1 (cfi, &next_cfa, &remember);
16147 last_cfa = next_cfa;
16148 last_label = start_label;
16150 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
16152 /* If the first partition contained no CFI adjustments, the
16153 CIE opcodes apply to the whole first partition. */
16154 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16155 fde->dw_fde_begin, fde->dw_fde_end, section);
16156 list_tail =&(*list_tail)->dw_loc_next;
16157 start_label = last_label = fde->dw_fde_second_begin;
16160 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
16162 switch (cfi->dw_cfi_opc)
16164 case DW_CFA_set_loc:
16165 case DW_CFA_advance_loc1:
16166 case DW_CFA_advance_loc2:
16167 case DW_CFA_advance_loc4:
16168 if (!cfa_equal_p (&last_cfa, &next_cfa))
16170 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16171 start_label, last_label, section);
16173 list_tail = &(*list_tail)->dw_loc_next;
16174 last_cfa = next_cfa;
16175 start_label = last_label;
16177 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
16178 break;
16180 case DW_CFA_advance_loc:
16181 /* The encoding is complex enough that we should never emit this. */
16182 gcc_unreachable ();
16184 default:
16185 lookup_cfa_1 (cfi, &next_cfa, &remember);
16186 break;
16188 if (ix + 1 == fde->dw_fde_switch_cfi_index)
16190 if (!cfa_equal_p (&last_cfa, &next_cfa))
16192 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16193 start_label, last_label, section);
16195 list_tail = &(*list_tail)->dw_loc_next;
16196 last_cfa = next_cfa;
16197 start_label = last_label;
16199 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16200 start_label, fde->dw_fde_end, section);
16201 list_tail = &(*list_tail)->dw_loc_next;
16202 start_label = last_label = fde->dw_fde_second_begin;
16206 if (!cfa_equal_p (&last_cfa, &next_cfa))
16208 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16209 start_label, last_label, section);
16210 list_tail = &(*list_tail)->dw_loc_next;
16211 start_label = last_label;
16214 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
16215 start_label,
16216 fde->dw_fde_second_begin
16217 ? fde->dw_fde_second_end : fde->dw_fde_end,
16218 section);
16220 if (list && list->dw_loc_next)
16221 gen_llsym (list);
16223 return list;
16226 /* Compute a displacement from the "steady-state frame pointer" to the
16227 frame base (often the same as the CFA), and store it in
16228 frame_pointer_fb_offset. OFFSET is added to the displacement
16229 before the latter is negated. */
16231 static void
16232 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
16234 rtx reg, elim;
16236 #ifdef FRAME_POINTER_CFA_OFFSET
16237 reg = frame_pointer_rtx;
16238 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
16239 #else
16240 reg = arg_pointer_rtx;
16241 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
16242 #endif
16244 elim = (ira_use_lra_p
16245 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
16246 : eliminate_regs (reg, VOIDmode, NULL_RTX));
16247 if (GET_CODE (elim) == PLUS)
16249 offset += INTVAL (XEXP (elim, 1));
16250 elim = XEXP (elim, 0);
16253 frame_pointer_fb_offset = -offset;
16255 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
16256 in which to eliminate. This is because it's stack pointer isn't
16257 directly accessible as a register within the ISA. To work around
16258 this, assume that while we cannot provide a proper value for
16259 frame_pointer_fb_offset, we won't need one either. */
16260 frame_pointer_fb_offset_valid
16261 = ((SUPPORTS_STACK_ALIGNMENT
16262 && (elim == hard_frame_pointer_rtx
16263 || elim == stack_pointer_rtx))
16264 || elim == (frame_pointer_needed
16265 ? hard_frame_pointer_rtx
16266 : stack_pointer_rtx));
16269 /* Generate a DW_AT_name attribute given some string value to be included as
16270 the value of the attribute. */
16272 static void
16273 add_name_attribute (dw_die_ref die, const char *name_string)
16275 if (name_string != NULL && *name_string != 0)
16277 if (demangle_name_func)
16278 name_string = (*demangle_name_func) (name_string);
16280 add_AT_string (die, DW_AT_name, name_string);
16284 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
16285 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
16286 of TYPE accordingly.
16288 ??? This is a temporary measure until after we're able to generate
16289 regular DWARF for the complex Ada type system. */
16291 static void
16292 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
16293 dw_die_ref context_die)
16295 tree dtype;
16296 dw_die_ref dtype_die;
16298 if (!lang_hooks.types.descriptive_type)
16299 return;
16301 dtype = lang_hooks.types.descriptive_type (type);
16302 if (!dtype)
16303 return;
16305 dtype_die = lookup_type_die (dtype);
16306 if (!dtype_die)
16308 gen_type_die (dtype, context_die);
16309 dtype_die = lookup_type_die (dtype);
16310 gcc_assert (dtype_die);
16313 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
16316 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
16318 static const char *
16319 comp_dir_string (void)
16321 const char *wd;
16322 char *wd1;
16323 static const char *cached_wd = NULL;
16325 if (cached_wd != NULL)
16326 return cached_wd;
16328 wd = get_src_pwd ();
16329 if (wd == NULL)
16330 return NULL;
16332 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
16334 int wdlen;
16336 wdlen = strlen (wd);
16337 wd1 = ggc_vec_alloc<char> (wdlen + 2);
16338 strcpy (wd1, wd);
16339 wd1 [wdlen] = DIR_SEPARATOR;
16340 wd1 [wdlen + 1] = 0;
16341 wd = wd1;
16344 cached_wd = remap_debug_filename (wd);
16345 return cached_wd;
16348 /* Generate a DW_AT_comp_dir attribute for DIE. */
16350 static void
16351 add_comp_dir_attribute (dw_die_ref die)
16353 const char * wd = comp_dir_string ();
16354 if (wd != NULL)
16355 add_AT_string (die, DW_AT_comp_dir, wd);
16358 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
16359 default. */
16361 static int
16362 lower_bound_default (void)
16364 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
16366 case DW_LANG_C:
16367 case DW_LANG_C89:
16368 case DW_LANG_C99:
16369 case DW_LANG_C_plus_plus:
16370 case DW_LANG_ObjC:
16371 case DW_LANG_ObjC_plus_plus:
16372 case DW_LANG_Java:
16373 return 0;
16374 case DW_LANG_Fortran77:
16375 case DW_LANG_Fortran90:
16376 case DW_LANG_Fortran95:
16377 return 1;
16378 case DW_LANG_UPC:
16379 case DW_LANG_D:
16380 case DW_LANG_Python:
16381 return dwarf_version >= 4 ? 0 : -1;
16382 case DW_LANG_Ada95:
16383 case DW_LANG_Ada83:
16384 case DW_LANG_Cobol74:
16385 case DW_LANG_Cobol85:
16386 case DW_LANG_Pascal83:
16387 case DW_LANG_Modula2:
16388 case DW_LANG_PLI:
16389 return dwarf_version >= 4 ? 1 : -1;
16390 default:
16391 return -1;
16395 /* Given a tree node describing an array bound (either lower or upper) output
16396 a representation for that bound. */
16398 static void
16399 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
16401 switch (TREE_CODE (bound))
16403 case ERROR_MARK:
16404 return;
16406 /* All fixed-bounds are represented by INTEGER_CST nodes. */
16407 case INTEGER_CST:
16409 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
16410 int dflt;
16412 /* Use the default if possible. */
16413 if (bound_attr == DW_AT_lower_bound
16414 && tree_fits_shwi_p (bound)
16415 && (dflt = lower_bound_default ()) != -1
16416 && tree_to_shwi (bound) == dflt)
16419 /* If HOST_WIDE_INT is big enough then represent the bound as
16420 a constant value. We need to choose a form based on
16421 whether the type is signed or unsigned. We cannot just
16422 call add_AT_unsigned if the value itself is positive
16423 (add_AT_unsigned might add the unsigned value encoded as
16424 DW_FORM_data[1248]). Some DWARF consumers will lookup the
16425 bounds type and then sign extend any unsigned values found
16426 for signed types. This is needed only for
16427 DW_AT_{lower,upper}_bound, since for most other attributes,
16428 consumers will treat DW_FORM_data[1248] as unsigned values,
16429 regardless of the underlying type. */
16430 else if (prec <= HOST_BITS_PER_WIDE_INT
16431 || tree_fits_uhwi_p (bound))
16433 if (TYPE_UNSIGNED (TREE_TYPE (bound)))
16434 add_AT_unsigned (subrange_die, bound_attr,
16435 TREE_INT_CST_LOW (bound));
16436 else
16437 add_AT_int (subrange_die, bound_attr, TREE_INT_CST_LOW (bound));
16439 else
16440 /* Otherwise represent the bound as an unsigned value with
16441 the precision of its type. The precision and signedness
16442 of the type will be necessary to re-interpret it
16443 unambiguously. */
16444 add_AT_wide (subrange_die, bound_attr, bound);
16446 break;
16448 CASE_CONVERT:
16449 case VIEW_CONVERT_EXPR:
16450 add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
16451 break;
16453 case SAVE_EXPR:
16454 break;
16456 case VAR_DECL:
16457 case PARM_DECL:
16458 case RESULT_DECL:
16460 dw_die_ref decl_die = lookup_decl_die (bound);
16462 /* ??? Can this happen, or should the variable have been bound
16463 first? Probably it can, since I imagine that we try to create
16464 the types of parameters in the order in which they exist in
16465 the list, and won't have created a forward reference to a
16466 later parameter. */
16467 if (decl_die != NULL)
16469 add_AT_die_ref (subrange_die, bound_attr, decl_die);
16470 break;
16473 /* FALLTHRU */
16475 default:
16477 /* Otherwise try to create a stack operation procedure to
16478 evaluate the value of the array bound. */
16480 dw_die_ref ctx, decl_die;
16481 dw_loc_list_ref list;
16483 list = loc_list_from_tree (bound, 2);
16484 if (list == NULL || single_element_loc_list_p (list))
16486 /* If DW_AT_*bound is not a reference nor constant, it is
16487 a DWARF expression rather than location description.
16488 For that loc_list_from_tree (bound, 0) is needed.
16489 If that fails to give a single element list,
16490 fall back to outputting this as a reference anyway. */
16491 dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
16492 if (list2 && single_element_loc_list_p (list2))
16494 add_AT_loc (subrange_die, bound_attr, list2->expr);
16495 break;
16498 if (list == NULL)
16499 break;
16501 if (current_function_decl == 0)
16502 ctx = comp_unit_die ();
16503 else
16504 ctx = lookup_decl_die (current_function_decl);
16506 decl_die = new_die (DW_TAG_variable, ctx, bound);
16507 add_AT_flag (decl_die, DW_AT_artificial, 1);
16508 add_type_attribute (decl_die, TREE_TYPE (bound), TYPE_QUAL_CONST, ctx);
16509 add_AT_location_description (decl_die, DW_AT_location, list);
16510 add_AT_die_ref (subrange_die, bound_attr, decl_die);
16511 break;
16516 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
16517 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
16518 Note that the block of subscript information for an array type also
16519 includes information about the element type of the given array type. */
16521 static void
16522 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
16524 unsigned dimension_number;
16525 tree lower, upper;
16526 dw_die_ref subrange_die;
16528 for (dimension_number = 0;
16529 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
16530 type = TREE_TYPE (type), dimension_number++)
16532 tree domain = TYPE_DOMAIN (type);
16534 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
16535 break;
16537 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
16538 and (in GNU C only) variable bounds. Handle all three forms
16539 here. */
16540 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
16541 if (domain)
16543 /* We have an array type with specified bounds. */
16544 lower = TYPE_MIN_VALUE (domain);
16545 upper = TYPE_MAX_VALUE (domain);
16547 /* Define the index type. */
16548 if (TREE_TYPE (domain))
16550 /* ??? This is probably an Ada unnamed subrange type. Ignore the
16551 TREE_TYPE field. We can't emit debug info for this
16552 because it is an unnamed integral type. */
16553 if (TREE_CODE (domain) == INTEGER_TYPE
16554 && TYPE_NAME (domain) == NULL_TREE
16555 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
16556 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
16558 else
16559 add_type_attribute (subrange_die, TREE_TYPE (domain),
16560 TYPE_UNQUALIFIED, type_die);
16563 /* ??? If upper is NULL, the array has unspecified length,
16564 but it does have a lower bound. This happens with Fortran
16565 dimension arr(N:*)
16566 Since the debugger is definitely going to need to know N
16567 to produce useful results, go ahead and output the lower
16568 bound solo, and hope the debugger can cope. */
16570 add_bound_info (subrange_die, DW_AT_lower_bound, lower);
16571 if (upper)
16572 add_bound_info (subrange_die, DW_AT_upper_bound, upper);
16575 /* Otherwise we have an array type with an unspecified length. The
16576 DWARF-2 spec does not say how to handle this; let's just leave out the
16577 bounds. */
16581 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
16583 static void
16584 add_byte_size_attribute (dw_die_ref die, tree tree_node)
16586 dw_die_ref decl_die;
16587 HOST_WIDE_INT size;
16589 switch (TREE_CODE (tree_node))
16591 case ERROR_MARK:
16592 size = 0;
16593 break;
16594 case ENUMERAL_TYPE:
16595 case RECORD_TYPE:
16596 case UNION_TYPE:
16597 case QUAL_UNION_TYPE:
16598 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
16599 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
16601 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
16602 return;
16604 size = int_size_in_bytes (tree_node);
16605 break;
16606 case FIELD_DECL:
16607 /* For a data member of a struct or union, the DW_AT_byte_size is
16608 generally given as the number of bytes normally allocated for an
16609 object of the *declared* type of the member itself. This is true
16610 even for bit-fields. */
16611 size = int_size_in_bytes (field_type (tree_node));
16612 break;
16613 default:
16614 gcc_unreachable ();
16617 /* Note that `size' might be -1 when we get to this point. If it is, that
16618 indicates that the byte size of the entity in question is variable. We
16619 have no good way of expressing this fact in Dwarf at the present time,
16620 when location description was not used by the caller code instead. */
16621 if (size >= 0)
16622 add_AT_unsigned (die, DW_AT_byte_size, size);
16625 /* For a FIELD_DECL node which represents a bit-field, output an attribute
16626 which specifies the distance in bits from the highest order bit of the
16627 "containing object" for the bit-field to the highest order bit of the
16628 bit-field itself.
16630 For any given bit-field, the "containing object" is a hypothetical object
16631 (of some integral or enum type) within which the given bit-field lives. The
16632 type of this hypothetical "containing object" is always the same as the
16633 declared type of the individual bit-field itself. The determination of the
16634 exact location of the "containing object" for a bit-field is rather
16635 complicated. It's handled by the `field_byte_offset' function (above).
16637 Note that it is the size (in bytes) of the hypothetical "containing object"
16638 which will be given in the DW_AT_byte_size attribute for this bit-field.
16639 (See `byte_size_attribute' above). */
16641 static inline void
16642 add_bit_offset_attribute (dw_die_ref die, tree decl)
16644 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
16645 tree type = DECL_BIT_FIELD_TYPE (decl);
16646 HOST_WIDE_INT bitpos_int;
16647 HOST_WIDE_INT highest_order_object_bit_offset;
16648 HOST_WIDE_INT highest_order_field_bit_offset;
16649 HOST_WIDE_INT bit_offset;
16651 /* Must be a field and a bit field. */
16652 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
16654 /* We can't yet handle bit-fields whose offsets are variable, so if we
16655 encounter such things, just return without generating any attribute
16656 whatsoever. Likewise for variable or too large size. */
16657 if (! tree_fits_shwi_p (bit_position (decl))
16658 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
16659 return;
16661 bitpos_int = int_bit_position (decl);
16663 /* Note that the bit offset is always the distance (in bits) from the
16664 highest-order bit of the "containing object" to the highest-order bit of
16665 the bit-field itself. Since the "high-order end" of any object or field
16666 is different on big-endian and little-endian machines, the computation
16667 below must take account of these differences. */
16668 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
16669 highest_order_field_bit_offset = bitpos_int;
16671 if (! BYTES_BIG_ENDIAN)
16673 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
16674 highest_order_object_bit_offset += simple_type_size_in_bits (type);
16677 bit_offset
16678 = (! BYTES_BIG_ENDIAN
16679 ? highest_order_object_bit_offset - highest_order_field_bit_offset
16680 : highest_order_field_bit_offset - highest_order_object_bit_offset);
16682 if (bit_offset < 0)
16683 add_AT_int (die, DW_AT_bit_offset, bit_offset);
16684 else
16685 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
16688 /* For a FIELD_DECL node which represents a bit field, output an attribute
16689 which specifies the length in bits of the given field. */
16691 static inline void
16692 add_bit_size_attribute (dw_die_ref die, tree decl)
16694 /* Must be a field and a bit field. */
16695 gcc_assert (TREE_CODE (decl) == FIELD_DECL
16696 && DECL_BIT_FIELD_TYPE (decl));
16698 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
16699 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
16702 /* If the compiled language is ANSI C, then add a 'prototyped'
16703 attribute, if arg types are given for the parameters of a function. */
16705 static inline void
16706 add_prototyped_attribute (dw_die_ref die, tree func_type)
16708 if (get_AT_unsigned (comp_unit_die (), DW_AT_language) == DW_LANG_C89
16709 && prototype_p (func_type))
16710 add_AT_flag (die, DW_AT_prototyped, 1);
16713 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
16714 by looking in either the type declaration or object declaration
16715 equate table. */
16717 static inline dw_die_ref
16718 add_abstract_origin_attribute (dw_die_ref die, tree origin)
16720 dw_die_ref origin_die = NULL;
16722 if (TREE_CODE (origin) != FUNCTION_DECL)
16724 /* We may have gotten separated from the block for the inlined
16725 function, if we're in an exception handler or some such; make
16726 sure that the abstract function has been written out.
16728 Doing this for nested functions is wrong, however; functions are
16729 distinct units, and our context might not even be inline. */
16730 tree fn = origin;
16732 if (TYPE_P (fn))
16733 fn = TYPE_STUB_DECL (fn);
16735 fn = decl_function_context (fn);
16736 if (fn)
16737 dwarf2out_abstract_function (fn);
16740 if (DECL_P (origin))
16741 origin_die = lookup_decl_die (origin);
16742 else if (TYPE_P (origin))
16743 origin_die = lookup_type_die (origin);
16745 /* XXX: Functions that are never lowered don't always have correct block
16746 trees (in the case of java, they simply have no block tree, in some other
16747 languages). For these functions, there is nothing we can really do to
16748 output correct debug info for inlined functions in all cases. Rather
16749 than die, we'll just produce deficient debug info now, in that we will
16750 have variables without a proper abstract origin. In the future, when all
16751 functions are lowered, we should re-add a gcc_assert (origin_die)
16752 here. */
16754 if (origin_die)
16755 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
16756 return origin_die;
16759 /* We do not currently support the pure_virtual attribute. */
16761 static inline void
16762 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
16764 if (DECL_VINDEX (func_decl))
16766 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
16768 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
16769 add_AT_loc (die, DW_AT_vtable_elem_location,
16770 new_loc_descr (DW_OP_constu,
16771 tree_to_shwi (DECL_VINDEX (func_decl)),
16772 0));
16774 /* GNU extension: Record what type this method came from originally. */
16775 if (debug_info_level > DINFO_LEVEL_TERSE
16776 && DECL_CONTEXT (func_decl))
16777 add_AT_die_ref (die, DW_AT_containing_type,
16778 lookup_type_die (DECL_CONTEXT (func_decl)));
16782 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
16783 given decl. This used to be a vendor extension until after DWARF 4
16784 standardized it. */
16786 static void
16787 add_linkage_attr (dw_die_ref die, tree decl)
16789 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
16791 /* Mimic what assemble_name_raw does with a leading '*'. */
16792 if (name[0] == '*')
16793 name = &name[1];
16795 if (dwarf_version >= 4)
16796 add_AT_string (die, DW_AT_linkage_name, name);
16797 else
16798 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
16801 /* Add source coordinate attributes for the given decl. */
16803 static void
16804 add_src_coords_attributes (dw_die_ref die, tree decl)
16806 expanded_location s;
16808 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
16809 return;
16810 s = expand_location (DECL_SOURCE_LOCATION (decl));
16811 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
16812 add_AT_unsigned (die, DW_AT_decl_line, s.line);
16815 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
16817 static void
16818 add_linkage_name (dw_die_ref die, tree decl)
16820 if (debug_info_level > DINFO_LEVEL_NONE
16821 && (TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
16822 && TREE_PUBLIC (decl)
16823 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
16824 && die->die_tag != DW_TAG_member)
16826 /* Defer until we have an assembler name set. */
16827 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
16829 limbo_die_node *asm_name;
16831 asm_name = ggc_cleared_alloc<limbo_die_node> ();
16832 asm_name->die = die;
16833 asm_name->created_for = decl;
16834 asm_name->next = deferred_asm_name;
16835 deferred_asm_name = asm_name;
16837 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
16838 add_linkage_attr (die, decl);
16842 /* Add a DW_AT_name attribute and source coordinate attribute for the
16843 given decl, but only if it actually has a name. */
16845 static void
16846 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
16848 tree decl_name;
16850 decl_name = DECL_NAME (decl);
16851 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
16853 const char *name = dwarf2_name (decl, 0);
16854 if (name)
16855 add_name_attribute (die, name);
16856 if (! DECL_ARTIFICIAL (decl))
16857 add_src_coords_attributes (die, decl);
16859 add_linkage_name (die, decl);
16862 #ifdef VMS_DEBUGGING_INFO
16863 /* Get the function's name, as described by its RTL. This may be different
16864 from the DECL_NAME name used in the source file. */
16865 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
16867 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
16868 XEXP (DECL_RTL (decl), 0), false);
16869 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
16871 #endif /* VMS_DEBUGGING_INFO */
16874 #ifdef VMS_DEBUGGING_INFO
16875 /* Output the debug main pointer die for VMS */
16877 void
16878 dwarf2out_vms_debug_main_pointer (void)
16880 char label[MAX_ARTIFICIAL_LABEL_BYTES];
16881 dw_die_ref die;
16883 /* Allocate the VMS debug main subprogram die. */
16884 die = ggc_cleared_alloc<die_node> ();
16885 die->die_tag = DW_TAG_subprogram;
16886 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
16887 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
16888 current_function_funcdef_no);
16889 add_AT_lbl_id (die, DW_AT_entry_pc, label);
16891 /* Make it the first child of comp_unit_die (). */
16892 die->die_parent = comp_unit_die ();
16893 if (comp_unit_die ()->die_child)
16895 die->die_sib = comp_unit_die ()->die_child->die_sib;
16896 comp_unit_die ()->die_child->die_sib = die;
16898 else
16900 die->die_sib = die;
16901 comp_unit_die ()->die_child = die;
16904 #endif /* VMS_DEBUGGING_INFO */
16906 /* Push a new declaration scope. */
16908 static void
16909 push_decl_scope (tree scope)
16911 vec_safe_push (decl_scope_table, scope);
16914 /* Pop a declaration scope. */
16916 static inline void
16917 pop_decl_scope (void)
16919 decl_scope_table->pop ();
16922 /* walk_tree helper function for uses_local_type, below. */
16924 static tree
16925 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
16927 if (!TYPE_P (*tp))
16928 *walk_subtrees = 0;
16929 else
16931 tree name = TYPE_NAME (*tp);
16932 if (name && DECL_P (name) && decl_function_context (name))
16933 return *tp;
16935 return NULL_TREE;
16938 /* If TYPE involves a function-local type (including a local typedef to a
16939 non-local type), returns that type; otherwise returns NULL_TREE. */
16941 static tree
16942 uses_local_type (tree type)
16944 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
16945 return used;
16948 /* Return the DIE for the scope that immediately contains this type.
16949 Non-named types that do not involve a function-local type get global
16950 scope. Named types nested in namespaces or other types get their
16951 containing scope. All other types (i.e. function-local named types) get
16952 the current active scope. */
16954 static dw_die_ref
16955 scope_die_for (tree t, dw_die_ref context_die)
16957 dw_die_ref scope_die = NULL;
16958 tree containing_scope;
16960 /* Non-types always go in the current scope. */
16961 gcc_assert (TYPE_P (t));
16963 /* Use the scope of the typedef, rather than the scope of the type
16964 it refers to. */
16965 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
16966 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
16967 else
16968 containing_scope = TYPE_CONTEXT (t);
16970 /* Use the containing namespace if there is one. */
16971 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
16973 if (context_die == lookup_decl_die (containing_scope))
16974 /* OK */;
16975 else if (debug_info_level > DINFO_LEVEL_TERSE)
16976 context_die = get_context_die (containing_scope);
16977 else
16978 containing_scope = NULL_TREE;
16981 /* Ignore function type "scopes" from the C frontend. They mean that
16982 a tagged type is local to a parmlist of a function declarator, but
16983 that isn't useful to DWARF. */
16984 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
16985 containing_scope = NULL_TREE;
16987 if (SCOPE_FILE_SCOPE_P (containing_scope))
16989 /* If T uses a local type keep it local as well, to avoid references
16990 to function-local DIEs from outside the function. */
16991 if (current_function_decl && uses_local_type (t))
16992 scope_die = context_die;
16993 else
16994 scope_die = comp_unit_die ();
16996 else if (TYPE_P (containing_scope))
16998 /* For types, we can just look up the appropriate DIE. */
16999 if (debug_info_level > DINFO_LEVEL_TERSE)
17000 scope_die = get_context_die (containing_scope);
17001 else
17003 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
17004 if (scope_die == NULL)
17005 scope_die = comp_unit_die ();
17008 else
17009 scope_die = context_die;
17011 return scope_die;
17014 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
17016 static inline int
17017 local_scope_p (dw_die_ref context_die)
17019 for (; context_die; context_die = context_die->die_parent)
17020 if (context_die->die_tag == DW_TAG_inlined_subroutine
17021 || context_die->die_tag == DW_TAG_subprogram)
17022 return 1;
17024 return 0;
17027 /* Returns nonzero if CONTEXT_DIE is a class. */
17029 static inline int
17030 class_scope_p (dw_die_ref context_die)
17032 return (context_die
17033 && (context_die->die_tag == DW_TAG_structure_type
17034 || context_die->die_tag == DW_TAG_class_type
17035 || context_die->die_tag == DW_TAG_interface_type
17036 || context_die->die_tag == DW_TAG_union_type));
17039 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
17040 whether or not to treat a DIE in this context as a declaration. */
17042 static inline int
17043 class_or_namespace_scope_p (dw_die_ref context_die)
17045 return (class_scope_p (context_die)
17046 || (context_die && context_die->die_tag == DW_TAG_namespace));
17049 /* Many forms of DIEs require a "type description" attribute. This
17050 routine locates the proper "type descriptor" die for the type given
17051 by 'type' plus any additional qualifiers given by 'cv_quals', and
17052 adds a DW_AT_type attribute below the given die. */
17054 static void
17055 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
17056 dw_die_ref context_die)
17058 enum tree_code code = TREE_CODE (type);
17059 dw_die_ref type_die = NULL;
17061 /* ??? If this type is an unnamed subrange type of an integral, floating-point
17062 or fixed-point type, use the inner type. This is because we have no
17063 support for unnamed types in base_type_die. This can happen if this is
17064 an Ada subrange type. Correct solution is emit a subrange type die. */
17065 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
17066 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
17067 type = TREE_TYPE (type), code = TREE_CODE (type);
17069 if (code == ERROR_MARK
17070 /* Handle a special case. For functions whose return type is void, we
17071 generate *no* type attribute. (Note that no object may have type
17072 `void', so this only applies to function return types). */
17073 || code == VOID_TYPE)
17074 return;
17076 type_die = modified_type_die (type,
17077 cv_quals | TYPE_QUALS_NO_ADDR_SPACE (type),
17078 context_die);
17080 if (type_die != NULL)
17081 add_AT_die_ref (object_die, DW_AT_type, type_die);
17084 /* Given an object die, add the calling convention attribute for the
17085 function call type. */
17086 static void
17087 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
17089 enum dwarf_calling_convention value = DW_CC_normal;
17091 value = ((enum dwarf_calling_convention)
17092 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
17094 if (is_fortran ()
17095 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
17097 /* DWARF 2 doesn't provide a way to identify a program's source-level
17098 entry point. DW_AT_calling_convention attributes are only meant
17099 to describe functions' calling conventions. However, lacking a
17100 better way to signal the Fortran main program, we used this for
17101 a long time, following existing custom. Now, DWARF 4 has
17102 DW_AT_main_subprogram, which we add below, but some tools still
17103 rely on the old way, which we thus keep. */
17104 value = DW_CC_program;
17106 if (dwarf_version >= 4 || !dwarf_strict)
17107 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
17110 /* Only add the attribute if the backend requests it, and
17111 is not DW_CC_normal. */
17112 if (value && (value != DW_CC_normal))
17113 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
17116 /* Given a tree pointer to a struct, class, union, or enum type node, return
17117 a pointer to the (string) tag name for the given type, or zero if the type
17118 was declared without a tag. */
17120 static const char *
17121 type_tag (const_tree type)
17123 const char *name = 0;
17125 if (TYPE_NAME (type) != 0)
17127 tree t = 0;
17129 /* Find the IDENTIFIER_NODE for the type name. */
17130 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
17131 && !TYPE_NAMELESS (type))
17132 t = TYPE_NAME (type);
17134 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
17135 a TYPE_DECL node, regardless of whether or not a `typedef' was
17136 involved. */
17137 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
17138 && ! DECL_IGNORED_P (TYPE_NAME (type)))
17140 /* We want to be extra verbose. Don't call dwarf_name if
17141 DECL_NAME isn't set. The default hook for decl_printable_name
17142 doesn't like that, and in this context it's correct to return
17143 0, instead of "<anonymous>" or the like. */
17144 if (DECL_NAME (TYPE_NAME (type))
17145 && !DECL_NAMELESS (TYPE_NAME (type)))
17146 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
17149 /* Now get the name as a string, or invent one. */
17150 if (!name && t != 0)
17151 name = IDENTIFIER_POINTER (t);
17154 return (name == 0 || *name == '\0') ? 0 : name;
17157 /* Return the type associated with a data member, make a special check
17158 for bit field types. */
17160 static inline tree
17161 member_declared_type (const_tree member)
17163 return (DECL_BIT_FIELD_TYPE (member)
17164 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
17167 /* Get the decl's label, as described by its RTL. This may be different
17168 from the DECL_NAME name used in the source file. */
17170 #if 0
17171 static const char *
17172 decl_start_label (tree decl)
17174 rtx x;
17175 const char *fnname;
17177 x = DECL_RTL (decl);
17178 gcc_assert (MEM_P (x));
17180 x = XEXP (x, 0);
17181 gcc_assert (GET_CODE (x) == SYMBOL_REF);
17183 fnname = XSTR (x, 0);
17184 return fnname;
17186 #endif
17188 /* These routines generate the internal representation of the DIE's for
17189 the compilation unit. Debugging information is collected by walking
17190 the declaration trees passed in from dwarf2out_decl(). */
17192 static void
17193 gen_array_type_die (tree type, dw_die_ref context_die)
17195 dw_die_ref scope_die = scope_die_for (type, context_die);
17196 dw_die_ref array_die;
17198 /* GNU compilers represent multidimensional array types as sequences of one
17199 dimensional array types whose element types are themselves array types.
17200 We sometimes squish that down to a single array_type DIE with multiple
17201 subscripts in the Dwarf debugging info. The draft Dwarf specification
17202 say that we are allowed to do this kind of compression in C, because
17203 there is no difference between an array of arrays and a multidimensional
17204 array. We don't do this for Ada to remain as close as possible to the
17205 actual representation, which is especially important against the language
17206 flexibilty wrt arrays of variable size. */
17208 bool collapse_nested_arrays = !is_ada ();
17209 tree element_type;
17211 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
17212 DW_TAG_string_type doesn't have DW_AT_type attribute). */
17213 if (TYPE_STRING_FLAG (type)
17214 && TREE_CODE (type) == ARRAY_TYPE
17215 && is_fortran ()
17216 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
17218 HOST_WIDE_INT size;
17220 array_die = new_die (DW_TAG_string_type, scope_die, type);
17221 add_name_attribute (array_die, type_tag (type));
17222 equate_type_number_to_die (type, array_die);
17223 size = int_size_in_bytes (type);
17224 if (size >= 0)
17225 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17226 else if (TYPE_DOMAIN (type) != NULL_TREE
17227 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
17228 && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
17230 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
17231 dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
17233 size = int_size_in_bytes (TREE_TYPE (szdecl));
17234 if (loc && size > 0)
17236 add_AT_location_description (array_die, DW_AT_string_length, loc);
17237 if (size != DWARF2_ADDR_SIZE)
17238 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17241 return;
17244 array_die = new_die (DW_TAG_array_type, scope_die, type);
17245 add_name_attribute (array_die, type_tag (type));
17246 equate_type_number_to_die (type, array_die);
17248 if (TREE_CODE (type) == VECTOR_TYPE)
17249 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
17251 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
17252 if (is_fortran ()
17253 && TREE_CODE (type) == ARRAY_TYPE
17254 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
17255 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
17256 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17258 #if 0
17259 /* We default the array ordering. SDB will probably do
17260 the right things even if DW_AT_ordering is not present. It's not even
17261 an issue until we start to get into multidimensional arrays anyway. If
17262 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
17263 then we'll have to put the DW_AT_ordering attribute back in. (But if
17264 and when we find out that we need to put these in, we will only do so
17265 for multidimensional arrays. */
17266 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17267 #endif
17269 if (TREE_CODE (type) == VECTOR_TYPE)
17271 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
17272 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
17273 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
17274 add_bound_info (subrange_die, DW_AT_upper_bound,
17275 size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
17277 else
17278 add_subscript_info (array_die, type, collapse_nested_arrays);
17280 /* Add representation of the type of the elements of this array type and
17281 emit the corresponding DIE if we haven't done it already. */
17282 element_type = TREE_TYPE (type);
17283 if (collapse_nested_arrays)
17284 while (TREE_CODE (element_type) == ARRAY_TYPE)
17286 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
17287 break;
17288 element_type = TREE_TYPE (element_type);
17291 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED, context_die);
17293 add_gnat_descriptive_type_attribute (array_die, type, context_die);
17294 if (TYPE_ARTIFICIAL (type))
17295 add_AT_flag (array_die, DW_AT_artificial, 1);
17297 if (get_AT (array_die, DW_AT_name))
17298 add_pubtype (type, array_die);
17301 static dw_loc_descr_ref
17302 descr_info_loc (tree val, tree base_decl)
17304 HOST_WIDE_INT size;
17305 dw_loc_descr_ref loc, loc2;
17306 enum dwarf_location_atom op;
17308 if (val == base_decl)
17309 return new_loc_descr (DW_OP_push_object_address, 0, 0);
17311 switch (TREE_CODE (val))
17313 CASE_CONVERT:
17314 return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17315 case VAR_DECL:
17316 return loc_descriptor_from_tree (val, 0);
17317 case INTEGER_CST:
17318 if (tree_fits_shwi_p (val))
17319 return int_loc_descriptor (tree_to_shwi (val));
17320 break;
17321 case INDIRECT_REF:
17322 size = int_size_in_bytes (TREE_TYPE (val));
17323 if (size < 0)
17324 break;
17325 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17326 if (!loc)
17327 break;
17328 if (size == DWARF2_ADDR_SIZE)
17329 add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
17330 else
17331 add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
17332 return loc;
17333 case POINTER_PLUS_EXPR:
17334 case PLUS_EXPR:
17335 if (tree_fits_uhwi_p (TREE_OPERAND (val, 1))
17336 && tree_to_uhwi (TREE_OPERAND (val, 1)) < 16384)
17338 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17339 if (!loc)
17340 break;
17341 loc_descr_plus_const (&loc, tree_to_shwi (TREE_OPERAND (val, 1)));
17343 else
17345 op = DW_OP_plus;
17346 do_binop:
17347 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17348 if (!loc)
17349 break;
17350 loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
17351 if (!loc2)
17352 break;
17353 add_loc_descr (&loc, loc2);
17354 add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
17356 return loc;
17357 case MINUS_EXPR:
17358 op = DW_OP_minus;
17359 goto do_binop;
17360 case MULT_EXPR:
17361 op = DW_OP_mul;
17362 goto do_binop;
17363 case EQ_EXPR:
17364 op = DW_OP_eq;
17365 goto do_binop;
17366 case NE_EXPR:
17367 op = DW_OP_ne;
17368 goto do_binop;
17369 default:
17370 break;
17372 return NULL;
17375 static void
17376 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
17377 tree val, tree base_decl)
17379 dw_loc_descr_ref loc;
17381 if (tree_fits_shwi_p (val))
17383 add_AT_unsigned (die, attr, tree_to_shwi (val));
17384 return;
17387 loc = descr_info_loc (val, base_decl);
17388 if (!loc)
17389 return;
17391 add_AT_loc (die, attr, loc);
17394 /* This routine generates DIE for array with hidden descriptor, details
17395 are filled into *info by a langhook. */
17397 static void
17398 gen_descr_array_type_die (tree type, struct array_descr_info *info,
17399 dw_die_ref context_die)
17401 dw_die_ref scope_die = scope_die_for (type, context_die);
17402 dw_die_ref array_die;
17403 int dim;
17405 array_die = new_die (DW_TAG_array_type, scope_die, type);
17406 add_name_attribute (array_die, type_tag (type));
17407 equate_type_number_to_die (type, array_die);
17409 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
17410 if (is_fortran ()
17411 && info->ndimensions >= 2)
17412 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17414 if (info->data_location)
17415 add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
17416 info->base_decl);
17417 if (info->associated)
17418 add_descr_info_field (array_die, DW_AT_associated, info->associated,
17419 info->base_decl);
17420 if (info->allocated)
17421 add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
17422 info->base_decl);
17424 for (dim = 0; dim < info->ndimensions; dim++)
17426 dw_die_ref subrange_die
17427 = new_die (DW_TAG_subrange_type, array_die, NULL);
17429 if (info->dimen[dim].lower_bound)
17431 /* If it is the default value, omit it. */
17432 int dflt;
17434 if (tree_fits_shwi_p (info->dimen[dim].lower_bound)
17435 && (dflt = lower_bound_default ()) != -1
17436 && tree_to_shwi (info->dimen[dim].lower_bound) == dflt)
17438 else
17439 add_descr_info_field (subrange_die, DW_AT_lower_bound,
17440 info->dimen[dim].lower_bound,
17441 info->base_decl);
17443 if (info->dimen[dim].upper_bound)
17444 add_descr_info_field (subrange_die, DW_AT_upper_bound,
17445 info->dimen[dim].upper_bound,
17446 info->base_decl);
17447 if (info->dimen[dim].stride)
17448 add_descr_info_field (subrange_die, DW_AT_byte_stride,
17449 info->dimen[dim].stride,
17450 info->base_decl);
17453 gen_type_die (info->element_type, context_die);
17454 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
17455 context_die);
17457 if (get_AT (array_die, DW_AT_name))
17458 add_pubtype (type, array_die);
17461 #if 0
17462 static void
17463 gen_entry_point_die (tree decl, dw_die_ref context_die)
17465 tree origin = decl_ultimate_origin (decl);
17466 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
17468 if (origin != NULL)
17469 add_abstract_origin_attribute (decl_die, origin);
17470 else
17472 add_name_and_src_coords_attributes (decl_die, decl);
17473 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
17474 TYPE_UNQUALIFIED, context_die);
17477 if (DECL_ABSTRACT_P (decl))
17478 equate_decl_number_to_die (decl, decl_die);
17479 else
17480 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
17482 #endif
17484 /* Walk through the list of incomplete types again, trying once more to
17485 emit full debugging info for them. */
17487 static void
17488 retry_incomplete_types (void)
17490 int i;
17492 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
17493 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
17494 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
17497 /* Determine what tag to use for a record type. */
17499 static enum dwarf_tag
17500 record_type_tag (tree type)
17502 if (! lang_hooks.types.classify_record)
17503 return DW_TAG_structure_type;
17505 switch (lang_hooks.types.classify_record (type))
17507 case RECORD_IS_STRUCT:
17508 return DW_TAG_structure_type;
17510 case RECORD_IS_CLASS:
17511 return DW_TAG_class_type;
17513 case RECORD_IS_INTERFACE:
17514 if (dwarf_version >= 3 || !dwarf_strict)
17515 return DW_TAG_interface_type;
17516 return DW_TAG_structure_type;
17518 default:
17519 gcc_unreachable ();
17523 /* Generate a DIE to represent an enumeration type. Note that these DIEs
17524 include all of the information about the enumeration values also. Each
17525 enumerated type name/value is listed as a child of the enumerated type
17526 DIE. */
17528 static dw_die_ref
17529 gen_enumeration_type_die (tree type, dw_die_ref context_die)
17531 dw_die_ref type_die = lookup_type_die (type);
17533 if (type_die == NULL)
17535 type_die = new_die (DW_TAG_enumeration_type,
17536 scope_die_for (type, context_die), type);
17537 equate_type_number_to_die (type, type_die);
17538 add_name_attribute (type_die, type_tag (type));
17539 if (dwarf_version >= 4 || !dwarf_strict)
17541 if (ENUM_IS_SCOPED (type))
17542 add_AT_flag (type_die, DW_AT_enum_class, 1);
17543 if (ENUM_IS_OPAQUE (type))
17544 add_AT_flag (type_die, DW_AT_declaration, 1);
17547 else if (! TYPE_SIZE (type))
17548 return type_die;
17549 else
17550 remove_AT (type_die, DW_AT_declaration);
17552 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
17553 given enum type is incomplete, do not generate the DW_AT_byte_size
17554 attribute or the DW_AT_element_list attribute. */
17555 if (TYPE_SIZE (type))
17557 tree link;
17559 TREE_ASM_WRITTEN (type) = 1;
17560 add_byte_size_attribute (type_die, type);
17561 if (dwarf_version >= 3 || !dwarf_strict)
17563 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
17564 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED,
17565 context_die);
17567 if (TYPE_STUB_DECL (type) != NULL_TREE)
17569 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
17570 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
17573 /* If the first reference to this type was as the return type of an
17574 inline function, then it may not have a parent. Fix this now. */
17575 if (type_die->die_parent == NULL)
17576 add_child_die (scope_die_for (type, context_die), type_die);
17578 for (link = TYPE_VALUES (type);
17579 link != NULL; link = TREE_CHAIN (link))
17581 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
17582 tree value = TREE_VALUE (link);
17584 add_name_attribute (enum_die,
17585 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
17587 if (TREE_CODE (value) == CONST_DECL)
17588 value = DECL_INITIAL (value);
17590 if (simple_type_size_in_bits (TREE_TYPE (value))
17591 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
17593 /* For constant forms created by add_AT_unsigned DWARF
17594 consumers (GDB, elfutils, etc.) always zero extend
17595 the value. Only when the actual value is negative
17596 do we need to use add_AT_int to generate a constant
17597 form that can represent negative values. */
17598 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
17599 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
17600 add_AT_unsigned (enum_die, DW_AT_const_value,
17601 (unsigned HOST_WIDE_INT) val);
17602 else
17603 add_AT_int (enum_die, DW_AT_const_value, val);
17605 else
17606 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
17607 that here. TODO: This should be re-worked to use correct
17608 signed/unsigned double tags for all cases. */
17609 add_AT_wide (enum_die, DW_AT_const_value, value);
17612 add_gnat_descriptive_type_attribute (type_die, type, context_die);
17613 if (TYPE_ARTIFICIAL (type))
17614 add_AT_flag (type_die, DW_AT_artificial, 1);
17616 else
17617 add_AT_flag (type_die, DW_AT_declaration, 1);
17619 add_pubtype (type, type_die);
17621 return type_die;
17624 /* Generate a DIE to represent either a real live formal parameter decl or to
17625 represent just the type of some formal parameter position in some function
17626 type.
17628 Note that this routine is a bit unusual because its argument may be a
17629 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
17630 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
17631 node. If it's the former then this function is being called to output a
17632 DIE to represent a formal parameter object (or some inlining thereof). If
17633 it's the latter, then this function is only being called to output a
17634 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
17635 argument type of some subprogram type.
17636 If EMIT_NAME_P is true, name and source coordinate attributes
17637 are emitted. */
17639 static dw_die_ref
17640 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
17641 dw_die_ref context_die)
17643 tree node_or_origin = node ? node : origin;
17644 tree ultimate_origin;
17645 dw_die_ref parm_die
17646 = new_die (DW_TAG_formal_parameter, context_die, node);
17648 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
17650 case tcc_declaration:
17651 ultimate_origin = decl_ultimate_origin (node_or_origin);
17652 if (node || ultimate_origin)
17653 origin = ultimate_origin;
17654 if (origin != NULL)
17655 add_abstract_origin_attribute (parm_die, origin);
17656 else if (emit_name_p)
17657 add_name_and_src_coords_attributes (parm_die, node);
17658 if (origin == NULL
17659 || (! DECL_ABSTRACT_P (node_or_origin)
17660 && variably_modified_type_p (TREE_TYPE (node_or_origin),
17661 decl_function_context
17662 (node_or_origin))))
17664 tree type = TREE_TYPE (node_or_origin);
17665 if (decl_by_reference_p (node_or_origin))
17666 add_type_attribute (parm_die, TREE_TYPE (type),
17667 TYPE_UNQUALIFIED, context_die);
17668 else
17669 add_type_attribute (parm_die, type,
17670 decl_quals (node_or_origin),
17671 context_die);
17673 if (origin == NULL && DECL_ARTIFICIAL (node))
17674 add_AT_flag (parm_die, DW_AT_artificial, 1);
17676 if (node && node != origin)
17677 equate_decl_number_to_die (node, parm_die);
17678 if (! DECL_ABSTRACT_P (node_or_origin))
17679 add_location_or_const_value_attribute (parm_die, node_or_origin,
17680 node == NULL, DW_AT_location);
17682 break;
17684 case tcc_type:
17685 /* We were called with some kind of a ..._TYPE node. */
17686 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED,
17687 context_die);
17688 break;
17690 default:
17691 gcc_unreachable ();
17694 return parm_die;
17697 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
17698 children DW_TAG_formal_parameter DIEs representing the arguments of the
17699 parameter pack.
17701 PARM_PACK must be a function parameter pack.
17702 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
17703 must point to the subsequent arguments of the function PACK_ARG belongs to.
17704 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
17705 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
17706 following the last one for which a DIE was generated. */
17708 static dw_die_ref
17709 gen_formal_parameter_pack_die (tree parm_pack,
17710 tree pack_arg,
17711 dw_die_ref subr_die,
17712 tree *next_arg)
17714 tree arg;
17715 dw_die_ref parm_pack_die;
17717 gcc_assert (parm_pack
17718 && lang_hooks.function_parameter_pack_p (parm_pack)
17719 && subr_die);
17721 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
17722 add_src_coords_attributes (parm_pack_die, parm_pack);
17724 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
17726 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
17727 parm_pack))
17728 break;
17729 gen_formal_parameter_die (arg, NULL,
17730 false /* Don't emit name attribute. */,
17731 parm_pack_die);
17733 if (next_arg)
17734 *next_arg = arg;
17735 return parm_pack_die;
17738 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
17739 at the end of an (ANSI prototyped) formal parameters list. */
17741 static void
17742 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
17744 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
17747 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
17748 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
17749 parameters as specified in some function type specification (except for
17750 those which appear as part of a function *definition*). */
17752 static void
17753 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
17755 tree link;
17756 tree formal_type = NULL;
17757 tree first_parm_type;
17758 tree arg;
17760 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
17762 arg = DECL_ARGUMENTS (function_or_method_type);
17763 function_or_method_type = TREE_TYPE (function_or_method_type);
17765 else
17766 arg = NULL_TREE;
17768 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
17770 /* Make our first pass over the list of formal parameter types and output a
17771 DW_TAG_formal_parameter DIE for each one. */
17772 for (link = first_parm_type; link; )
17774 dw_die_ref parm_die;
17776 formal_type = TREE_VALUE (link);
17777 if (formal_type == void_type_node)
17778 break;
17780 /* Output a (nameless) DIE to represent the formal parameter itself. */
17781 parm_die = gen_formal_parameter_die (formal_type, NULL,
17782 true /* Emit name attribute. */,
17783 context_die);
17784 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
17785 && link == first_parm_type)
17787 add_AT_flag (parm_die, DW_AT_artificial, 1);
17788 if (dwarf_version >= 3 || !dwarf_strict)
17789 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
17791 else if (arg && DECL_ARTIFICIAL (arg))
17792 add_AT_flag (parm_die, DW_AT_artificial, 1);
17794 link = TREE_CHAIN (link);
17795 if (arg)
17796 arg = DECL_CHAIN (arg);
17799 /* If this function type has an ellipsis, add a
17800 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
17801 if (formal_type != void_type_node)
17802 gen_unspecified_parameters_die (function_or_method_type, context_die);
17804 /* Make our second (and final) pass over the list of formal parameter types
17805 and output DIEs to represent those types (as necessary). */
17806 for (link = TYPE_ARG_TYPES (function_or_method_type);
17807 link && TREE_VALUE (link);
17808 link = TREE_CHAIN (link))
17809 gen_type_die (TREE_VALUE (link), context_die);
17812 /* We want to generate the DIE for TYPE so that we can generate the
17813 die for MEMBER, which has been defined; we will need to refer back
17814 to the member declaration nested within TYPE. If we're trying to
17815 generate minimal debug info for TYPE, processing TYPE won't do the
17816 trick; we need to attach the member declaration by hand. */
17818 static void
17819 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
17821 gen_type_die (type, context_die);
17823 /* If we're trying to avoid duplicate debug info, we may not have
17824 emitted the member decl for this function. Emit it now. */
17825 if (TYPE_STUB_DECL (type)
17826 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
17827 && ! lookup_decl_die (member))
17829 dw_die_ref type_die;
17830 gcc_assert (!decl_ultimate_origin (member));
17832 push_decl_scope (type);
17833 type_die = lookup_type_die_strip_naming_typedef (type);
17834 if (TREE_CODE (member) == FUNCTION_DECL)
17835 gen_subprogram_die (member, type_die);
17836 else if (TREE_CODE (member) == FIELD_DECL)
17838 /* Ignore the nameless fields that are used to skip bits but handle
17839 C++ anonymous unions and structs. */
17840 if (DECL_NAME (member) != NULL_TREE
17841 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
17842 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
17844 gen_type_die (member_declared_type (member), type_die);
17845 gen_field_die (member, type_die);
17848 else
17849 gen_variable_die (member, NULL_TREE, type_die);
17851 pop_decl_scope ();
17855 /* Forward declare these functions, because they are mutually recursive
17856 with their set_block_* pairing functions. */
17857 static void set_decl_origin_self (tree);
17858 static void set_decl_abstract_flags (tree, int);
17860 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
17861 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
17862 that it points to the node itself, thus indicating that the node is its
17863 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
17864 the given node is NULL, recursively descend the decl/block tree which
17865 it is the root of, and for each other ..._DECL or BLOCK node contained
17866 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
17867 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
17868 values to point to themselves. */
17870 static void
17871 set_block_origin_self (tree stmt)
17873 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
17875 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
17878 tree local_decl;
17880 for (local_decl = BLOCK_VARS (stmt);
17881 local_decl != NULL_TREE;
17882 local_decl = DECL_CHAIN (local_decl))
17883 if (! DECL_EXTERNAL (local_decl))
17884 set_decl_origin_self (local_decl); /* Potential recursion. */
17888 tree subblock;
17890 for (subblock = BLOCK_SUBBLOCKS (stmt);
17891 subblock != NULL_TREE;
17892 subblock = BLOCK_CHAIN (subblock))
17893 set_block_origin_self (subblock); /* Recurse. */
17898 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
17899 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
17900 node to so that it points to the node itself, thus indicating that the
17901 node represents its own (abstract) origin. Additionally, if the
17902 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
17903 the decl/block tree of which the given node is the root of, and for
17904 each other ..._DECL or BLOCK node contained therein whose
17905 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
17906 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
17907 point to themselves. */
17909 static void
17910 set_decl_origin_self (tree decl)
17912 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
17914 DECL_ABSTRACT_ORIGIN (decl) = decl;
17915 if (TREE_CODE (decl) == FUNCTION_DECL)
17917 tree arg;
17919 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
17920 DECL_ABSTRACT_ORIGIN (arg) = arg;
17921 if (DECL_INITIAL (decl) != NULL_TREE
17922 && DECL_INITIAL (decl) != error_mark_node)
17923 set_block_origin_self (DECL_INITIAL (decl));
17928 /* Given a pointer to some BLOCK node, and a boolean value to set the
17929 "abstract" flags to, set that value into the BLOCK_ABSTRACT flag for
17930 the given block, and for all local decls and all local sub-blocks
17931 (recursively) which are contained therein. */
17933 static void
17934 set_block_abstract_flags (tree stmt, int setting)
17936 tree local_decl;
17937 tree subblock;
17938 unsigned int i;
17940 BLOCK_ABSTRACT (stmt) = setting;
17942 for (local_decl = BLOCK_VARS (stmt);
17943 local_decl != NULL_TREE;
17944 local_decl = DECL_CHAIN (local_decl))
17945 if (! DECL_EXTERNAL (local_decl))
17946 set_decl_abstract_flags (local_decl, setting);
17948 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
17950 local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
17951 if ((TREE_CODE (local_decl) == VAR_DECL && !TREE_STATIC (local_decl))
17952 || TREE_CODE (local_decl) == PARM_DECL)
17953 set_decl_abstract_flags (local_decl, setting);
17956 for (subblock = BLOCK_SUBBLOCKS (stmt);
17957 subblock != NULL_TREE;
17958 subblock = BLOCK_CHAIN (subblock))
17959 set_block_abstract_flags (subblock, setting);
17962 /* Given a pointer to some ..._DECL node, and a boolean value to set the
17963 "abstract" flags to, set that value into the DECL_ABSTRACT_P flag for the
17964 given decl, and (in the case where the decl is a FUNCTION_DECL) also
17965 set the abstract flags for all of the parameters, local vars, local
17966 blocks and sub-blocks (recursively) to the same setting. */
17968 static void
17969 set_decl_abstract_flags (tree decl, int setting)
17971 DECL_ABSTRACT_P (decl) = setting;
17972 if (TREE_CODE (decl) == FUNCTION_DECL)
17974 tree arg;
17976 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
17977 DECL_ABSTRACT_P (arg) = setting;
17978 if (DECL_INITIAL (decl) != NULL_TREE
17979 && DECL_INITIAL (decl) != error_mark_node)
17980 set_block_abstract_flags (DECL_INITIAL (decl), setting);
17984 /* Generate the DWARF2 info for the "abstract" instance of a function which we
17985 may later generate inlined and/or out-of-line instances of. */
17987 static void
17988 dwarf2out_abstract_function (tree decl)
17990 dw_die_ref old_die;
17991 tree save_fn;
17992 tree context;
17993 int was_abstract;
17994 htab_t old_decl_loc_table;
17995 htab_t old_cached_dw_loc_list_table;
17996 int old_call_site_count, old_tail_call_site_count;
17997 struct call_arg_loc_node *old_call_arg_locations;
17999 /* Make sure we have the actual abstract inline, not a clone. */
18000 decl = DECL_ORIGIN (decl);
18002 old_die = lookup_decl_die (decl);
18003 if (old_die && get_AT (old_die, DW_AT_inline))
18004 /* We've already generated the abstract instance. */
18005 return;
18007 /* We can be called while recursively when seeing block defining inlined subroutine
18008 DIE. Be sure to not clobber the outer location table nor use it or we would
18009 get locations in abstract instantces. */
18010 old_decl_loc_table = decl_loc_table;
18011 decl_loc_table = NULL;
18012 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
18013 cached_dw_loc_list_table = NULL;
18014 old_call_arg_locations = call_arg_locations;
18015 call_arg_locations = NULL;
18016 old_call_site_count = call_site_count;
18017 call_site_count = -1;
18018 old_tail_call_site_count = tail_call_site_count;
18019 tail_call_site_count = -1;
18021 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
18022 we don't get confused by DECL_ABSTRACT_P. */
18023 if (debug_info_level > DINFO_LEVEL_TERSE)
18025 context = decl_class_context (decl);
18026 if (context)
18027 gen_type_die_for_member
18028 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
18031 /* Pretend we've just finished compiling this function. */
18032 save_fn = current_function_decl;
18033 current_function_decl = decl;
18035 was_abstract = DECL_ABSTRACT_P (decl);
18036 set_decl_abstract_flags (decl, 1);
18037 dwarf2out_decl (decl);
18038 if (! was_abstract)
18039 set_decl_abstract_flags (decl, 0);
18041 current_function_decl = save_fn;
18042 decl_loc_table = old_decl_loc_table;
18043 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
18044 call_arg_locations = old_call_arg_locations;
18045 call_site_count = old_call_site_count;
18046 tail_call_site_count = old_tail_call_site_count;
18049 /* Helper function of premark_used_types() which gets called through
18050 htab_traverse.
18052 Marks the DIE of a given type in *SLOT as perennial, so it never gets
18053 marked as unused by prune_unused_types. */
18055 bool
18056 premark_used_types_helper (tree const &type, void *)
18058 dw_die_ref die;
18060 die = lookup_type_die (type);
18061 if (die != NULL)
18062 die->die_perennial_p = 1;
18063 return true;
18066 /* Helper function of premark_types_used_by_global_vars which gets called
18067 through htab_traverse.
18069 Marks the DIE of a given type in *SLOT as perennial, so it never gets
18070 marked as unused by prune_unused_types. The DIE of the type is marked
18071 only if the global variable using the type will actually be emitted. */
18073 static int
18074 premark_types_used_by_global_vars_helper (void **slot,
18075 void *data ATTRIBUTE_UNUSED)
18077 struct types_used_by_vars_entry *entry;
18078 dw_die_ref die;
18080 entry = (struct types_used_by_vars_entry *) *slot;
18081 gcc_assert (entry->type != NULL
18082 && entry->var_decl != NULL);
18083 die = lookup_type_die (entry->type);
18084 if (die)
18086 /* Ask cgraph if the global variable really is to be emitted.
18087 If yes, then we'll keep the DIE of ENTRY->TYPE. */
18088 varpool_node *node = varpool_node::get (entry->var_decl);
18089 if (node && node->definition)
18091 die->die_perennial_p = 1;
18092 /* Keep the parent DIEs as well. */
18093 while ((die = die->die_parent) && die->die_perennial_p == 0)
18094 die->die_perennial_p = 1;
18097 return 1;
18100 /* Mark all members of used_types_hash as perennial. */
18102 static void
18103 premark_used_types (struct function *fun)
18105 if (fun && fun->used_types_hash)
18106 fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
18109 /* Mark all members of types_used_by_vars_entry as perennial. */
18111 static void
18112 premark_types_used_by_global_vars (void)
18114 if (types_used_by_vars_hash)
18115 htab_traverse (types_used_by_vars_hash,
18116 premark_types_used_by_global_vars_helper, NULL);
18119 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
18120 for CA_LOC call arg loc node. */
18122 static dw_die_ref
18123 gen_call_site_die (tree decl, dw_die_ref subr_die,
18124 struct call_arg_loc_node *ca_loc)
18126 dw_die_ref stmt_die = NULL, die;
18127 tree block = ca_loc->block;
18129 while (block
18130 && block != DECL_INITIAL (decl)
18131 && TREE_CODE (block) == BLOCK)
18133 if (block_map.length () > BLOCK_NUMBER (block))
18134 stmt_die = block_map[BLOCK_NUMBER (block)];
18135 if (stmt_die)
18136 break;
18137 block = BLOCK_SUPERCONTEXT (block);
18139 if (stmt_die == NULL)
18140 stmt_die = subr_die;
18141 die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
18142 add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
18143 if (ca_loc->tail_call_p)
18144 add_AT_flag (die, DW_AT_GNU_tail_call, 1);
18145 if (ca_loc->symbol_ref)
18147 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
18148 if (tdie)
18149 add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
18150 else
18151 add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref, false);
18153 return die;
18156 /* Generate a DIE to represent a declared function (either file-scope or
18157 block-local). */
18159 static void
18160 gen_subprogram_die (tree decl, dw_die_ref context_die)
18162 tree origin = decl_ultimate_origin (decl);
18163 dw_die_ref subr_die;
18164 tree outer_scope;
18165 dw_die_ref old_die = lookup_decl_die (decl);
18166 int declaration = (current_function_decl != decl
18167 || class_or_namespace_scope_p (context_die));
18169 premark_used_types (DECL_STRUCT_FUNCTION (decl));
18171 /* It is possible to have both DECL_ABSTRACT_P and DECLARATION be true if we
18172 started to generate the abstract instance of an inline, decided to output
18173 its containing class, and proceeded to emit the declaration of the inline
18174 from the member list for the class. If so, DECLARATION takes priority;
18175 we'll get back to the abstract instance when done with the class. */
18177 /* The class-scope declaration DIE must be the primary DIE. */
18178 if (origin && declaration && class_or_namespace_scope_p (context_die))
18180 origin = NULL;
18181 gcc_assert (!old_die);
18184 /* Now that the C++ front end lazily declares artificial member fns, we
18185 might need to retrofit the declaration into its class. */
18186 if (!declaration && !origin && !old_die
18187 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
18188 && !class_or_namespace_scope_p (context_die)
18189 && debug_info_level > DINFO_LEVEL_TERSE)
18190 old_die = force_decl_die (decl);
18192 if (origin != NULL)
18194 gcc_assert (!declaration || local_scope_p (context_die));
18196 /* Fixup die_parent for the abstract instance of a nested
18197 inline function. */
18198 if (old_die && old_die->die_parent == NULL)
18199 add_child_die (context_die, old_die);
18201 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18202 add_abstract_origin_attribute (subr_die, origin);
18203 /* This is where the actual code for a cloned function is.
18204 Let's emit linkage name attribute for it. This helps
18205 debuggers to e.g, set breakpoints into
18206 constructors/destructors when the user asks "break
18207 K::K". */
18208 add_linkage_name (subr_die, decl);
18210 else if (old_die)
18212 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18213 struct dwarf_file_data * file_index = lookup_filename (s.file);
18215 if (!get_AT_flag (old_die, DW_AT_declaration)
18216 /* We can have a normal definition following an inline one in the
18217 case of redefinition of GNU C extern inlines.
18218 It seems reasonable to use AT_specification in this case. */
18219 && !get_AT (old_die, DW_AT_inline))
18221 /* Detect and ignore this case, where we are trying to output
18222 something we have already output. */
18223 return;
18226 /* If the definition comes from the same place as the declaration,
18227 maybe use the old DIE. We always want the DIE for this function
18228 that has the *_pc attributes to be under comp_unit_die so the
18229 debugger can find it. We also need to do this for abstract
18230 instances of inlines, since the spec requires the out-of-line copy
18231 to have the same parent. For local class methods, this doesn't
18232 apply; we just use the old DIE. */
18233 if ((is_cu_die (old_die->die_parent) || context_die == NULL)
18234 && (DECL_ARTIFICIAL (decl)
18235 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
18236 && (get_AT_unsigned (old_die, DW_AT_decl_line)
18237 == (unsigned) s.line))))
18239 subr_die = old_die;
18241 /* Clear out the declaration attribute and the formal parameters.
18242 Do not remove all children, because it is possible that this
18243 declaration die was forced using force_decl_die(). In such
18244 cases die that forced declaration die (e.g. TAG_imported_module)
18245 is one of the children that we do not want to remove. */
18246 remove_AT (subr_die, DW_AT_declaration);
18247 remove_AT (subr_die, DW_AT_object_pointer);
18248 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
18250 else
18252 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18253 add_AT_specification (subr_die, old_die);
18254 add_pubname (decl, subr_die);
18255 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18256 add_AT_file (subr_die, DW_AT_decl_file, file_index);
18257 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18258 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
18260 /* If the prototype had an 'auto' or 'decltype(auto)' return type,
18261 emit the real type on the definition die. */
18262 if (is_cxx() && debug_info_level > DINFO_LEVEL_TERSE)
18264 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
18265 if (die == auto_die || die == decltype_auto_die)
18266 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18267 TYPE_UNQUALIFIED, context_die);
18271 else
18273 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18275 if (TREE_PUBLIC (decl))
18276 add_AT_flag (subr_die, DW_AT_external, 1);
18278 add_name_and_src_coords_attributes (subr_die, decl);
18279 add_pubname (decl, subr_die);
18280 if (debug_info_level > DINFO_LEVEL_TERSE)
18282 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
18283 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18284 TYPE_UNQUALIFIED, context_die);
18287 add_pure_or_virtual_attribute (subr_die, decl);
18288 if (DECL_ARTIFICIAL (decl))
18289 add_AT_flag (subr_die, DW_AT_artificial, 1);
18291 add_accessibility_attribute (subr_die, decl);
18294 if (declaration)
18296 if (!old_die || !get_AT (old_die, DW_AT_inline))
18298 add_AT_flag (subr_die, DW_AT_declaration, 1);
18300 /* If this is an explicit function declaration then generate
18301 a DW_AT_explicit attribute. */
18302 if (lang_hooks.decls.function_decl_explicit_p (decl)
18303 && (dwarf_version >= 3 || !dwarf_strict))
18304 add_AT_flag (subr_die, DW_AT_explicit, 1);
18306 /* If this is a C++11 deleted special function member then generate
18307 a DW_AT_GNU_deleted attribute. */
18308 if (lang_hooks.decls.function_decl_deleted_p (decl)
18309 && (! dwarf_strict))
18310 add_AT_flag (subr_die, DW_AT_GNU_deleted, 1);
18312 /* The first time we see a member function, it is in the context of
18313 the class to which it belongs. We make sure of this by emitting
18314 the class first. The next time is the definition, which is
18315 handled above. The two may come from the same source text.
18317 Note that force_decl_die() forces function declaration die. It is
18318 later reused to represent definition. */
18319 equate_decl_number_to_die (decl, subr_die);
18322 else if (DECL_ABSTRACT_P (decl))
18324 if (DECL_DECLARED_INLINE_P (decl))
18326 if (cgraph_function_possibly_inlined_p (decl))
18327 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
18328 else
18329 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
18331 else
18333 if (cgraph_function_possibly_inlined_p (decl))
18334 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
18335 else
18336 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
18339 if (DECL_DECLARED_INLINE_P (decl)
18340 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
18341 add_AT_flag (subr_die, DW_AT_artificial, 1);
18343 equate_decl_number_to_die (decl, subr_die);
18345 else if (!DECL_EXTERNAL (decl))
18347 HOST_WIDE_INT cfa_fb_offset;
18348 struct function *fun = DECL_STRUCT_FUNCTION (decl);
18350 if (!old_die || !get_AT (old_die, DW_AT_inline))
18351 equate_decl_number_to_die (decl, subr_die);
18353 gcc_checking_assert (fun);
18354 if (!flag_reorder_blocks_and_partition)
18356 dw_fde_ref fde = fun->fde;
18357 if (fde->dw_fde_begin)
18359 /* We have already generated the labels. */
18360 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18361 fde->dw_fde_end, false);
18363 else
18365 /* Create start/end labels and add the range. */
18366 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
18367 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
18368 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
18369 current_function_funcdef_no);
18370 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
18371 current_function_funcdef_no);
18372 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
18373 false);
18376 #if VMS_DEBUGGING_INFO
18377 /* HP OpenVMS Industry Standard 64: DWARF Extensions
18378 Section 2.3 Prologue and Epilogue Attributes:
18379 When a breakpoint is set on entry to a function, it is generally
18380 desirable for execution to be suspended, not on the very first
18381 instruction of the function, but rather at a point after the
18382 function's frame has been set up, after any language defined local
18383 declaration processing has been completed, and before execution of
18384 the first statement of the function begins. Debuggers generally
18385 cannot properly determine where this point is. Similarly for a
18386 breakpoint set on exit from a function. The prologue and epilogue
18387 attributes allow a compiler to communicate the location(s) to use. */
18390 if (fde->dw_fde_vms_end_prologue)
18391 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
18392 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
18394 if (fde->dw_fde_vms_begin_epilogue)
18395 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
18396 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
18398 #endif
18401 else
18403 /* Generate pubnames entries for the split function code ranges. */
18404 dw_fde_ref fde = fun->fde;
18406 if (fde->dw_fde_second_begin)
18408 if (dwarf_version >= 3 || !dwarf_strict)
18410 /* We should use ranges for non-contiguous code section
18411 addresses. Use the actual code range for the initial
18412 section, since the HOT/COLD labels might precede an
18413 alignment offset. */
18414 bool range_list_added = false;
18415 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
18416 fde->dw_fde_end, &range_list_added,
18417 false);
18418 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
18419 fde->dw_fde_second_end,
18420 &range_list_added, false);
18421 if (range_list_added)
18422 add_ranges (NULL);
18424 else
18426 /* There is no real support in DW2 for this .. so we make
18427 a work-around. First, emit the pub name for the segment
18428 containing the function label. Then make and emit a
18429 simplified subprogram DIE for the second segment with the
18430 name pre-fixed by __hot/cold_sect_of_. We use the same
18431 linkage name for the second die so that gdb will find both
18432 sections when given "b foo". */
18433 const char *name = NULL;
18434 tree decl_name = DECL_NAME (decl);
18435 dw_die_ref seg_die;
18437 /* Do the 'primary' section. */
18438 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18439 fde->dw_fde_end, false);
18441 /* Build a minimal DIE for the secondary section. */
18442 seg_die = new_die (DW_TAG_subprogram,
18443 subr_die->die_parent, decl);
18445 if (TREE_PUBLIC (decl))
18446 add_AT_flag (seg_die, DW_AT_external, 1);
18448 if (decl_name != NULL
18449 && IDENTIFIER_POINTER (decl_name) != NULL)
18451 name = dwarf2_name (decl, 1);
18452 if (! DECL_ARTIFICIAL (decl))
18453 add_src_coords_attributes (seg_die, decl);
18455 add_linkage_name (seg_die, decl);
18457 gcc_assert (name != NULL);
18458 add_pure_or_virtual_attribute (seg_die, decl);
18459 if (DECL_ARTIFICIAL (decl))
18460 add_AT_flag (seg_die, DW_AT_artificial, 1);
18462 name = concat ("__second_sect_of_", name, NULL);
18463 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
18464 fde->dw_fde_second_end, false);
18465 add_name_attribute (seg_die, name);
18466 if (want_pubnames ())
18467 add_pubname_string (name, seg_die);
18470 else
18471 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
18472 false);
18475 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
18477 /* We define the "frame base" as the function's CFA. This is more
18478 convenient for several reasons: (1) It's stable across the prologue
18479 and epilogue, which makes it better than just a frame pointer,
18480 (2) With dwarf3, there exists a one-byte encoding that allows us
18481 to reference the .debug_frame data by proxy, but failing that,
18482 (3) We can at least reuse the code inspection and interpretation
18483 code that determines the CFA position at various points in the
18484 function. */
18485 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
18487 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
18488 add_AT_loc (subr_die, DW_AT_frame_base, op);
18490 else
18492 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
18493 if (list->dw_loc_next)
18494 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
18495 else
18496 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
18499 /* Compute a displacement from the "steady-state frame pointer" to
18500 the CFA. The former is what all stack slots and argument slots
18501 will reference in the rtl; the latter is what we've told the
18502 debugger about. We'll need to adjust all frame_base references
18503 by this displacement. */
18504 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
18506 if (fun->static_chain_decl)
18507 add_AT_location_description (subr_die, DW_AT_static_link,
18508 loc_list_from_tree (fun->static_chain_decl, 2));
18511 /* Generate child dies for template paramaters. */
18512 if (debug_info_level > DINFO_LEVEL_TERSE)
18513 gen_generic_params_dies (decl);
18515 /* Now output descriptions of the arguments for this function. This gets
18516 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
18517 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
18518 `...' at the end of the formal parameter list. In order to find out if
18519 there was a trailing ellipsis or not, we must instead look at the type
18520 associated with the FUNCTION_DECL. This will be a node of type
18521 FUNCTION_TYPE. If the chain of type nodes hanging off of this
18522 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
18523 an ellipsis at the end. */
18525 /* In the case where we are describing a mere function declaration, all we
18526 need to do here (and all we *can* do here) is to describe the *types* of
18527 its formal parameters. */
18528 if (debug_info_level <= DINFO_LEVEL_TERSE)
18530 else if (declaration)
18531 gen_formal_types_die (decl, subr_die);
18532 else
18534 /* Generate DIEs to represent all known formal parameters. */
18535 tree parm = DECL_ARGUMENTS (decl);
18536 tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
18537 tree generic_decl_parm = generic_decl
18538 ? DECL_ARGUMENTS (generic_decl)
18539 : NULL;
18541 /* Now we want to walk the list of parameters of the function and
18542 emit their relevant DIEs.
18544 We consider the case of DECL being an instance of a generic function
18545 as well as it being a normal function.
18547 If DECL is an instance of a generic function we walk the
18548 parameters of the generic function declaration _and_ the parameters of
18549 DECL itself. This is useful because we want to emit specific DIEs for
18550 function parameter packs and those are declared as part of the
18551 generic function declaration. In that particular case,
18552 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
18553 That DIE has children DIEs representing the set of arguments
18554 of the pack. Note that the set of pack arguments can be empty.
18555 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
18556 children DIE.
18558 Otherwise, we just consider the parameters of DECL. */
18559 while (generic_decl_parm || parm)
18561 if (generic_decl_parm
18562 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
18563 gen_formal_parameter_pack_die (generic_decl_parm,
18564 parm, subr_die,
18565 &parm);
18566 else if (parm)
18568 dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
18570 if (parm == DECL_ARGUMENTS (decl)
18571 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
18572 && parm_die
18573 && (dwarf_version >= 3 || !dwarf_strict))
18574 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
18576 parm = DECL_CHAIN (parm);
18579 if (generic_decl_parm)
18580 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
18583 /* Decide whether we need an unspecified_parameters DIE at the end.
18584 There are 2 more cases to do this for: 1) the ansi ... declaration -
18585 this is detectable when the end of the arg list is not a
18586 void_type_node 2) an unprototyped function declaration (not a
18587 definition). This just means that we have no info about the
18588 parameters at all. */
18589 if (prototype_p (TREE_TYPE (decl)))
18591 /* This is the prototyped case, check for.... */
18592 if (stdarg_p (TREE_TYPE (decl)))
18593 gen_unspecified_parameters_die (decl, subr_die);
18595 else if (DECL_INITIAL (decl) == NULL_TREE)
18596 gen_unspecified_parameters_die (decl, subr_die);
18599 /* Output Dwarf info for all of the stuff within the body of the function
18600 (if it has one - it may be just a declaration). */
18601 outer_scope = DECL_INITIAL (decl);
18603 /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
18604 a function. This BLOCK actually represents the outermost binding contour
18605 for the function, i.e. the contour in which the function's formal
18606 parameters and labels get declared. Curiously, it appears that the front
18607 end doesn't actually put the PARM_DECL nodes for the current function onto
18608 the BLOCK_VARS list for this outer scope, but are strung off of the
18609 DECL_ARGUMENTS list for the function instead.
18611 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
18612 the LABEL_DECL nodes for the function however, and we output DWARF info
18613 for those in decls_for_scope. Just within the `outer_scope' there will be
18614 a BLOCK node representing the function's outermost pair of curly braces,
18615 and any blocks used for the base and member initializers of a C++
18616 constructor function. */
18617 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
18619 int call_site_note_count = 0;
18620 int tail_call_site_note_count = 0;
18622 /* Emit a DW_TAG_variable DIE for a named return value. */
18623 if (DECL_NAME (DECL_RESULT (decl)))
18624 gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
18626 decls_for_scope (outer_scope, subr_die, 0);
18628 if (call_arg_locations && !dwarf_strict)
18630 struct call_arg_loc_node *ca_loc;
18631 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
18633 dw_die_ref die = NULL;
18634 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
18635 rtx arg, next_arg;
18637 for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
18638 arg; arg = next_arg)
18640 dw_loc_descr_ref reg, val;
18641 enum machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
18642 dw_die_ref cdie, tdie = NULL;
18644 next_arg = XEXP (arg, 1);
18645 if (REG_P (XEXP (XEXP (arg, 0), 0))
18646 && next_arg
18647 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
18648 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
18649 && REGNO (XEXP (XEXP (arg, 0), 0))
18650 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
18651 next_arg = XEXP (next_arg, 1);
18652 if (mode == VOIDmode)
18654 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
18655 if (mode == VOIDmode)
18656 mode = GET_MODE (XEXP (arg, 0));
18658 if (mode == VOIDmode || mode == BLKmode)
18659 continue;
18660 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
18662 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18663 tloc = XEXP (XEXP (arg, 0), 1);
18664 continue;
18666 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
18667 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
18669 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18670 tlocc = XEXP (XEXP (arg, 0), 1);
18671 continue;
18673 reg = NULL;
18674 if (REG_P (XEXP (XEXP (arg, 0), 0)))
18675 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
18676 VAR_INIT_STATUS_INITIALIZED);
18677 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
18679 rtx mem = XEXP (XEXP (arg, 0), 0);
18680 reg = mem_loc_descriptor (XEXP (mem, 0),
18681 get_address_mode (mem),
18682 GET_MODE (mem),
18683 VAR_INIT_STATUS_INITIALIZED);
18685 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
18686 == DEBUG_PARAMETER_REF)
18688 tree tdecl
18689 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
18690 tdie = lookup_decl_die (tdecl);
18691 if (tdie == NULL)
18692 continue;
18694 else
18695 continue;
18696 if (reg == NULL
18697 && GET_CODE (XEXP (XEXP (arg, 0), 0))
18698 != DEBUG_PARAMETER_REF)
18699 continue;
18700 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
18701 VOIDmode,
18702 VAR_INIT_STATUS_INITIALIZED);
18703 if (val == NULL)
18704 continue;
18705 if (die == NULL)
18706 die = gen_call_site_die (decl, subr_die, ca_loc);
18707 cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
18708 NULL_TREE);
18709 if (reg != NULL)
18710 add_AT_loc (cdie, DW_AT_location, reg);
18711 else if (tdie != NULL)
18712 add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
18713 add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
18714 if (next_arg != XEXP (arg, 1))
18716 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
18717 if (mode == VOIDmode)
18718 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
18719 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
18720 0), 1),
18721 mode, VOIDmode,
18722 VAR_INIT_STATUS_INITIALIZED);
18723 if (val != NULL)
18724 add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
18727 if (die == NULL
18728 && (ca_loc->symbol_ref || tloc))
18729 die = gen_call_site_die (decl, subr_die, ca_loc);
18730 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
18732 dw_loc_descr_ref tval = NULL;
18734 if (tloc != NULL_RTX)
18735 tval = mem_loc_descriptor (tloc,
18736 GET_MODE (tloc) == VOIDmode
18737 ? Pmode : GET_MODE (tloc),
18738 VOIDmode,
18739 VAR_INIT_STATUS_INITIALIZED);
18740 if (tval)
18741 add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
18742 else if (tlocc != NULL_RTX)
18744 tval = mem_loc_descriptor (tlocc,
18745 GET_MODE (tlocc) == VOIDmode
18746 ? Pmode : GET_MODE (tlocc),
18747 VOIDmode,
18748 VAR_INIT_STATUS_INITIALIZED);
18749 if (tval)
18750 add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
18751 tval);
18754 if (die != NULL)
18756 call_site_note_count++;
18757 if (ca_loc->tail_call_p)
18758 tail_call_site_note_count++;
18762 call_arg_locations = NULL;
18763 call_arg_loc_last = NULL;
18764 if (tail_call_site_count >= 0
18765 && tail_call_site_count == tail_call_site_note_count
18766 && !dwarf_strict)
18768 if (call_site_count >= 0
18769 && call_site_count == call_site_note_count)
18770 add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
18771 else
18772 add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
18774 call_site_count = -1;
18775 tail_call_site_count = -1;
18778 if (subr_die != old_die)
18779 /* Add the calling convention attribute if requested. */
18780 add_calling_convention_attribute (subr_die, decl);
18783 /* Returns a hash value for X (which really is a die_struct). */
18785 static hashval_t
18786 common_block_die_table_hash (const void *x)
18788 const_dw_die_ref d = (const_dw_die_ref) x;
18789 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
18792 /* Return nonzero if decl_id and die_parent of die_struct X is the same
18793 as decl_id and die_parent of die_struct Y. */
18795 static int
18796 common_block_die_table_eq (const void *x, const void *y)
18798 const_dw_die_ref d = (const_dw_die_ref) x;
18799 const_dw_die_ref e = (const_dw_die_ref) y;
18800 return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
18803 /* Generate a DIE to represent a declared data object.
18804 Either DECL or ORIGIN must be non-null. */
18806 static void
18807 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
18809 HOST_WIDE_INT off = 0;
18810 tree com_decl;
18811 tree decl_or_origin = decl ? decl : origin;
18812 tree ultimate_origin;
18813 dw_die_ref var_die;
18814 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
18815 dw_die_ref origin_die;
18816 bool declaration = (DECL_EXTERNAL (decl_or_origin)
18817 || class_or_namespace_scope_p (context_die));
18818 bool specialization_p = false;
18820 ultimate_origin = decl_ultimate_origin (decl_or_origin);
18821 if (decl || ultimate_origin)
18822 origin = ultimate_origin;
18823 com_decl = fortran_common (decl_or_origin, &off);
18825 /* Symbol in common gets emitted as a child of the common block, in the form
18826 of a data member. */
18827 if (com_decl)
18829 dw_die_ref com_die;
18830 dw_loc_list_ref loc;
18831 die_node com_die_arg;
18833 var_die = lookup_decl_die (decl_or_origin);
18834 if (var_die)
18836 if (get_AT (var_die, DW_AT_location) == NULL)
18838 loc = loc_list_from_tree (com_decl, off ? 1 : 2);
18839 if (loc)
18841 if (off)
18843 /* Optimize the common case. */
18844 if (single_element_loc_list_p (loc)
18845 && loc->expr->dw_loc_opc == DW_OP_addr
18846 && loc->expr->dw_loc_next == NULL
18847 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
18848 == SYMBOL_REF)
18850 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
18851 loc->expr->dw_loc_oprnd1.v.val_addr
18852 = plus_constant (GET_MODE (x), x , off);
18854 else
18855 loc_list_plus_const (loc, off);
18857 add_AT_location_description (var_die, DW_AT_location, loc);
18858 remove_AT (var_die, DW_AT_declaration);
18861 return;
18864 if (common_block_die_table == NULL)
18865 common_block_die_table
18866 = htab_create_ggc (10, common_block_die_table_hash,
18867 common_block_die_table_eq, NULL);
18869 com_die_arg.decl_id = DECL_UID (com_decl);
18870 com_die_arg.die_parent = context_die;
18871 com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
18872 loc = loc_list_from_tree (com_decl, 2);
18873 if (com_die == NULL)
18875 const char *cnam
18876 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
18877 void **slot;
18879 com_die = new_die (DW_TAG_common_block, context_die, decl);
18880 add_name_and_src_coords_attributes (com_die, com_decl);
18881 if (loc)
18883 add_AT_location_description (com_die, DW_AT_location, loc);
18884 /* Avoid sharing the same loc descriptor between
18885 DW_TAG_common_block and DW_TAG_variable. */
18886 loc = loc_list_from_tree (com_decl, 2);
18888 else if (DECL_EXTERNAL (decl))
18889 add_AT_flag (com_die, DW_AT_declaration, 1);
18890 if (want_pubnames ())
18891 add_pubname_string (cnam, com_die); /* ??? needed? */
18892 com_die->decl_id = DECL_UID (com_decl);
18893 slot = htab_find_slot (common_block_die_table, com_die, INSERT);
18894 *slot = (void *) com_die;
18896 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
18898 add_AT_location_description (com_die, DW_AT_location, loc);
18899 loc = loc_list_from_tree (com_decl, 2);
18900 remove_AT (com_die, DW_AT_declaration);
18902 var_die = new_die (DW_TAG_variable, com_die, decl);
18903 add_name_and_src_coords_attributes (var_die, decl);
18904 add_type_attribute (var_die, TREE_TYPE (decl), decl_quals (decl),
18905 context_die);
18906 add_AT_flag (var_die, DW_AT_external, 1);
18907 if (loc)
18909 if (off)
18911 /* Optimize the common case. */
18912 if (single_element_loc_list_p (loc)
18913 && loc->expr->dw_loc_opc == DW_OP_addr
18914 && loc->expr->dw_loc_next == NULL
18915 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
18917 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
18918 loc->expr->dw_loc_oprnd1.v.val_addr
18919 = plus_constant (GET_MODE (x), x, off);
18921 else
18922 loc_list_plus_const (loc, off);
18924 add_AT_location_description (var_die, DW_AT_location, loc);
18926 else if (DECL_EXTERNAL (decl))
18927 add_AT_flag (var_die, DW_AT_declaration, 1);
18928 equate_decl_number_to_die (decl, var_die);
18929 return;
18932 /* If the compiler emitted a definition for the DECL declaration
18933 and if we already emitted a DIE for it, don't emit a second
18934 DIE for it again. Allow re-declarations of DECLs that are
18935 inside functions, though. */
18936 if (old_die && declaration && !local_scope_p (context_die))
18937 return;
18939 /* For static data members, the declaration in the class is supposed
18940 to have DW_TAG_member tag; the specification should still be
18941 DW_TAG_variable referencing the DW_TAG_member DIE. */
18942 if (declaration && class_scope_p (context_die))
18943 var_die = new_die (DW_TAG_member, context_die, decl);
18944 else
18945 var_die = new_die (DW_TAG_variable, context_die, decl);
18947 origin_die = NULL;
18948 if (origin != NULL)
18949 origin_die = add_abstract_origin_attribute (var_die, origin);
18951 /* Loop unrolling can create multiple blocks that refer to the same
18952 static variable, so we must test for the DW_AT_declaration flag.
18954 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
18955 copy decls and set the DECL_ABSTRACT_P flag on them instead of
18956 sharing them.
18958 ??? Duplicated blocks have been rewritten to use .debug_ranges.
18960 ??? The declare_in_namespace support causes us to get two DIEs for one
18961 variable, both of which are declarations. We want to avoid considering
18962 one to be a specification, so we must test that this DIE is not a
18963 declaration. */
18964 else if (old_die && TREE_STATIC (decl) && ! declaration
18965 && get_AT_flag (old_die, DW_AT_declaration) == 1)
18967 /* This is a definition of a C++ class level static. */
18968 add_AT_specification (var_die, old_die);
18969 specialization_p = true;
18970 if (DECL_NAME (decl))
18972 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18973 struct dwarf_file_data * file_index = lookup_filename (s.file);
18975 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18976 add_AT_file (var_die, DW_AT_decl_file, file_index);
18978 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18979 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
18981 if (old_die->die_tag == DW_TAG_member)
18982 add_linkage_name (var_die, decl);
18985 else
18986 add_name_and_src_coords_attributes (var_die, decl);
18988 if ((origin == NULL && !specialization_p)
18989 || (origin != NULL
18990 && !DECL_ABSTRACT_P (decl_or_origin)
18991 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
18992 decl_function_context
18993 (decl_or_origin))))
18995 tree type = TREE_TYPE (decl_or_origin);
18997 if (decl_by_reference_p (decl_or_origin))
18998 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
18999 context_die);
19000 else
19001 add_type_attribute (var_die, type, decl_quals (decl_or_origin),
19002 context_die);
19005 if (origin == NULL && !specialization_p)
19007 if (TREE_PUBLIC (decl))
19008 add_AT_flag (var_die, DW_AT_external, 1);
19010 if (DECL_ARTIFICIAL (decl))
19011 add_AT_flag (var_die, DW_AT_artificial, 1);
19013 add_accessibility_attribute (var_die, decl);
19016 if (declaration)
19017 add_AT_flag (var_die, DW_AT_declaration, 1);
19019 if (decl && (DECL_ABSTRACT_P (decl) || declaration || old_die == NULL))
19020 equate_decl_number_to_die (decl, var_die);
19022 if (! declaration
19023 && (! DECL_ABSTRACT_P (decl_or_origin)
19024 /* Local static vars are shared between all clones/inlines,
19025 so emit DW_AT_location on the abstract DIE if DECL_RTL is
19026 already set. */
19027 || (TREE_CODE (decl_or_origin) == VAR_DECL
19028 && TREE_STATIC (decl_or_origin)
19029 && DECL_RTL_SET_P (decl_or_origin)))
19030 /* When abstract origin already has DW_AT_location attribute, no need
19031 to add it again. */
19032 && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
19034 if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
19035 && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
19036 defer_location (decl_or_origin, var_die);
19037 else
19038 add_location_or_const_value_attribute (var_die, decl_or_origin,
19039 decl == NULL, DW_AT_location);
19040 add_pubname (decl_or_origin, var_die);
19042 else
19043 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
19046 /* Generate a DIE to represent a named constant. */
19048 static void
19049 gen_const_die (tree decl, dw_die_ref context_die)
19051 dw_die_ref const_die;
19052 tree type = TREE_TYPE (decl);
19054 const_die = new_die (DW_TAG_constant, context_die, decl);
19055 add_name_and_src_coords_attributes (const_die, decl);
19056 add_type_attribute (const_die, type, TYPE_QUAL_CONST, context_die);
19057 if (TREE_PUBLIC (decl))
19058 add_AT_flag (const_die, DW_AT_external, 1);
19059 if (DECL_ARTIFICIAL (decl))
19060 add_AT_flag (const_die, DW_AT_artificial, 1);
19061 tree_add_const_value_attribute_for_decl (const_die, decl);
19064 /* Generate a DIE to represent a label identifier. */
19066 static void
19067 gen_label_die (tree decl, dw_die_ref context_die)
19069 tree origin = decl_ultimate_origin (decl);
19070 dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
19071 rtx insn;
19072 char label[MAX_ARTIFICIAL_LABEL_BYTES];
19074 if (origin != NULL)
19075 add_abstract_origin_attribute (lbl_die, origin);
19076 else
19077 add_name_and_src_coords_attributes (lbl_die, decl);
19079 if (DECL_ABSTRACT_P (decl))
19080 equate_decl_number_to_die (decl, lbl_die);
19081 else
19083 insn = DECL_RTL_IF_SET (decl);
19085 /* Deleted labels are programmer specified labels which have been
19086 eliminated because of various optimizations. We still emit them
19087 here so that it is possible to put breakpoints on them. */
19088 if (insn
19089 && (LABEL_P (insn)
19090 || ((NOTE_P (insn)
19091 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
19093 /* When optimization is enabled (via -O) some parts of the compiler
19094 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
19095 represent source-level labels which were explicitly declared by
19096 the user. This really shouldn't be happening though, so catch
19097 it if it ever does happen. */
19098 gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
19100 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
19101 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19103 else if (insn
19104 && NOTE_P (insn)
19105 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
19106 && CODE_LABEL_NUMBER (insn) != -1)
19108 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
19109 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19114 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
19115 attributes to the DIE for a block STMT, to describe where the inlined
19116 function was called from. This is similar to add_src_coords_attributes. */
19118 static inline void
19119 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
19121 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
19123 if (dwarf_version >= 3 || !dwarf_strict)
19125 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
19126 add_AT_unsigned (die, DW_AT_call_line, s.line);
19131 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
19132 Add low_pc and high_pc attributes to the DIE for a block STMT. */
19134 static inline void
19135 add_high_low_attributes (tree stmt, dw_die_ref die)
19137 char label[MAX_ARTIFICIAL_LABEL_BYTES];
19139 if (BLOCK_FRAGMENT_CHAIN (stmt)
19140 && (dwarf_version >= 3 || !dwarf_strict))
19142 tree chain, superblock = NULL_TREE;
19143 dw_die_ref pdie;
19144 dw_attr_ref attr = NULL;
19146 if (inlined_function_outer_scope_p (stmt))
19148 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19149 BLOCK_NUMBER (stmt));
19150 add_AT_lbl_id (die, DW_AT_entry_pc, label);
19153 /* Optimize duplicate .debug_ranges lists or even tails of
19154 lists. If this BLOCK has same ranges as its supercontext,
19155 lookup DW_AT_ranges attribute in the supercontext (and
19156 recursively so), verify that the ranges_table contains the
19157 right values and use it instead of adding a new .debug_range. */
19158 for (chain = stmt, pdie = die;
19159 BLOCK_SAME_RANGE (chain);
19160 chain = BLOCK_SUPERCONTEXT (chain))
19162 dw_attr_ref new_attr;
19164 pdie = pdie->die_parent;
19165 if (pdie == NULL)
19166 break;
19167 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
19168 break;
19169 new_attr = get_AT (pdie, DW_AT_ranges);
19170 if (new_attr == NULL
19171 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
19172 break;
19173 attr = new_attr;
19174 superblock = BLOCK_SUPERCONTEXT (chain);
19176 if (attr != NULL
19177 && (ranges_table[attr->dw_attr_val.v.val_offset
19178 / 2 / DWARF2_ADDR_SIZE].num
19179 == BLOCK_NUMBER (superblock))
19180 && BLOCK_FRAGMENT_CHAIN (superblock))
19182 unsigned long off = attr->dw_attr_val.v.val_offset
19183 / 2 / DWARF2_ADDR_SIZE;
19184 unsigned long supercnt = 0, thiscnt = 0;
19185 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
19186 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
19188 ++supercnt;
19189 gcc_checking_assert (ranges_table[off + supercnt].num
19190 == BLOCK_NUMBER (chain));
19192 gcc_checking_assert (ranges_table[off + supercnt + 1].num == 0);
19193 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
19194 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
19195 ++thiscnt;
19196 gcc_assert (supercnt >= thiscnt);
19197 add_AT_range_list (die, DW_AT_ranges,
19198 ((off + supercnt - thiscnt)
19199 * 2 * DWARF2_ADDR_SIZE),
19200 false);
19201 return;
19204 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt), false);
19206 chain = BLOCK_FRAGMENT_CHAIN (stmt);
19209 add_ranges (chain);
19210 chain = BLOCK_FRAGMENT_CHAIN (chain);
19212 while (chain);
19213 add_ranges (NULL);
19215 else
19217 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
19218 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19219 BLOCK_NUMBER (stmt));
19220 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
19221 BLOCK_NUMBER (stmt));
19222 add_AT_low_high_pc (die, label, label_high, false);
19226 /* Generate a DIE for a lexical block. */
19228 static void
19229 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
19231 dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
19233 if (call_arg_locations)
19235 if (block_map.length () <= BLOCK_NUMBER (stmt))
19236 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
19237 block_map[BLOCK_NUMBER (stmt)] = stmt_die;
19240 if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
19241 add_high_low_attributes (stmt, stmt_die);
19243 decls_for_scope (stmt, stmt_die, depth);
19246 /* Generate a DIE for an inlined subprogram. */
19248 static void
19249 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
19251 tree decl;
19253 /* The instance of function that is effectively being inlined shall not
19254 be abstract. */
19255 gcc_assert (! BLOCK_ABSTRACT (stmt));
19257 decl = block_ultimate_origin (stmt);
19259 /* Emit info for the abstract instance first, if we haven't yet. We
19260 must emit this even if the block is abstract, otherwise when we
19261 emit the block below (or elsewhere), we may end up trying to emit
19262 a die whose origin die hasn't been emitted, and crashing. */
19263 dwarf2out_abstract_function (decl);
19265 if (! BLOCK_ABSTRACT (stmt))
19267 dw_die_ref subr_die
19268 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
19270 if (call_arg_locations)
19272 if (block_map.length () <= BLOCK_NUMBER (stmt))
19273 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
19274 block_map[BLOCK_NUMBER (stmt)] = subr_die;
19276 add_abstract_origin_attribute (subr_die, decl);
19277 if (TREE_ASM_WRITTEN (stmt))
19278 add_high_low_attributes (stmt, subr_die);
19279 add_call_src_coords_attributes (stmt, subr_die);
19281 decls_for_scope (stmt, subr_die, depth);
19285 /* Generate a DIE for a field in a record, or structure. */
19287 static void
19288 gen_field_die (tree decl, dw_die_ref context_die)
19290 dw_die_ref decl_die;
19292 if (TREE_TYPE (decl) == error_mark_node)
19293 return;
19295 decl_die = new_die (DW_TAG_member, context_die, decl);
19296 add_name_and_src_coords_attributes (decl_die, decl);
19297 add_type_attribute (decl_die, member_declared_type (decl),
19298 decl_quals (decl), context_die);
19300 if (DECL_BIT_FIELD_TYPE (decl))
19302 add_byte_size_attribute (decl_die, decl);
19303 add_bit_size_attribute (decl_die, decl);
19304 add_bit_offset_attribute (decl_die, decl);
19307 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
19308 add_data_member_location_attribute (decl_die, decl);
19310 if (DECL_ARTIFICIAL (decl))
19311 add_AT_flag (decl_die, DW_AT_artificial, 1);
19313 add_accessibility_attribute (decl_die, decl);
19315 /* Equate decl number to die, so that we can look up this decl later on. */
19316 equate_decl_number_to_die (decl, decl_die);
19319 #if 0
19320 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19321 Use modified_type_die instead.
19322 We keep this code here just in case these types of DIEs may be needed to
19323 represent certain things in other languages (e.g. Pascal) someday. */
19325 static void
19326 gen_pointer_type_die (tree type, dw_die_ref context_die)
19328 dw_die_ref ptr_die
19329 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
19331 equate_type_number_to_die (type, ptr_die);
19332 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19333 context_die);
19334 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19337 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19338 Use modified_type_die instead.
19339 We keep this code here just in case these types of DIEs may be needed to
19340 represent certain things in other languages (e.g. Pascal) someday. */
19342 static void
19343 gen_reference_type_die (tree type, dw_die_ref context_die)
19345 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
19347 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
19348 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
19349 else
19350 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
19352 equate_type_number_to_die (type, ref_die);
19353 add_type_attribute (ref_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19354 context_die);
19355 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19357 #endif
19359 /* Generate a DIE for a pointer to a member type. */
19361 static void
19362 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
19364 dw_die_ref ptr_die
19365 = new_die (DW_TAG_ptr_to_member_type,
19366 scope_die_for (type, context_die), type);
19368 equate_type_number_to_die (type, ptr_die);
19369 add_AT_die_ref (ptr_die, DW_AT_containing_type,
19370 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
19371 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19372 context_die);
19375 typedef const char *dchar_p; /* For DEF_VEC_P. */
19377 static char *producer_string;
19379 /* Return a heap allocated producer string including command line options
19380 if -grecord-gcc-switches. */
19382 static char *
19383 gen_producer_string (void)
19385 size_t j;
19386 auto_vec<dchar_p> switches;
19387 const char *language_string = lang_hooks.name;
19388 char *producer, *tail;
19389 const char *p;
19390 size_t len = dwarf_record_gcc_switches ? 0 : 3;
19391 size_t plen = strlen (language_string) + 1 + strlen (version_string);
19393 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
19394 switch (save_decoded_options[j].opt_index)
19396 case OPT_o:
19397 case OPT_d:
19398 case OPT_dumpbase:
19399 case OPT_dumpdir:
19400 case OPT_auxbase:
19401 case OPT_auxbase_strip:
19402 case OPT_quiet:
19403 case OPT_version:
19404 case OPT_v:
19405 case OPT_w:
19406 case OPT_L:
19407 case OPT_D:
19408 case OPT_I:
19409 case OPT_U:
19410 case OPT_SPECIAL_unknown:
19411 case OPT_SPECIAL_ignore:
19412 case OPT_SPECIAL_program_name:
19413 case OPT_SPECIAL_input_file:
19414 case OPT_grecord_gcc_switches:
19415 case OPT_gno_record_gcc_switches:
19416 case OPT__output_pch_:
19417 case OPT_fdiagnostics_show_location_:
19418 case OPT_fdiagnostics_show_option:
19419 case OPT_fdiagnostics_show_caret:
19420 case OPT_fdiagnostics_color_:
19421 case OPT_fverbose_asm:
19422 case OPT____:
19423 case OPT__sysroot_:
19424 case OPT_nostdinc:
19425 case OPT_nostdinc__:
19426 /* Ignore these. */
19427 continue;
19428 default:
19429 if (cl_options[save_decoded_options[j].opt_index].flags
19430 & CL_NO_DWARF_RECORD)
19431 continue;
19432 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
19433 == '-');
19434 switch (save_decoded_options[j].canonical_option[0][1])
19436 case 'M':
19437 case 'i':
19438 case 'W':
19439 continue;
19440 case 'f':
19441 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
19442 "dump", 4) == 0)
19443 continue;
19444 break;
19445 default:
19446 break;
19448 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
19449 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
19450 break;
19453 producer = XNEWVEC (char, plen + 1 + len + 1);
19454 tail = producer;
19455 sprintf (tail, "%s %s", language_string, version_string);
19456 tail += plen;
19458 FOR_EACH_VEC_ELT (switches, j, p)
19460 len = strlen (p);
19461 *tail = ' ';
19462 memcpy (tail + 1, p, len);
19463 tail += len + 1;
19466 *tail = '\0';
19467 return producer;
19470 /* Generate the DIE for the compilation unit. */
19472 static dw_die_ref
19473 gen_compile_unit_die (const char *filename)
19475 dw_die_ref die;
19476 const char *language_string = lang_hooks.name;
19477 int language;
19479 die = new_die (DW_TAG_compile_unit, NULL, NULL);
19481 if (filename)
19483 add_name_attribute (die, filename);
19484 /* Don't add cwd for <built-in>. */
19485 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
19486 add_comp_dir_attribute (die);
19489 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
19491 /* If our producer is LTO try to figure out a common language to use
19492 from the global list of translation units. */
19493 if (strcmp (language_string, "GNU GIMPLE") == 0)
19495 unsigned i;
19496 tree t;
19497 const char *common_lang = NULL;
19499 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
19501 if (!TRANSLATION_UNIT_LANGUAGE (t))
19502 continue;
19503 if (!common_lang)
19504 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
19505 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
19507 else if (strncmp (common_lang, "GNU C", 5) == 0
19508 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
19509 /* Mixing C and C++ is ok, use C++ in that case. */
19510 common_lang = "GNU C++";
19511 else
19513 /* Fall back to C. */
19514 common_lang = NULL;
19515 break;
19519 if (common_lang)
19520 language_string = common_lang;
19523 language = DW_LANG_C89;
19524 if (strcmp (language_string, "GNU C++") == 0)
19525 language = DW_LANG_C_plus_plus;
19526 else if (strcmp (language_string, "GNU F77") == 0)
19527 language = DW_LANG_Fortran77;
19528 else if (strcmp (language_string, "GNU Pascal") == 0)
19529 language = DW_LANG_Pascal83;
19530 else if (dwarf_version >= 3 || !dwarf_strict)
19532 if (strcmp (language_string, "GNU Ada") == 0)
19533 language = DW_LANG_Ada95;
19534 else if (strcmp (language_string, "GNU Fortran") == 0)
19535 language = DW_LANG_Fortran95;
19536 else if (strcmp (language_string, "GNU Java") == 0)
19537 language = DW_LANG_Java;
19538 else if (strcmp (language_string, "GNU Objective-C") == 0)
19539 language = DW_LANG_ObjC;
19540 else if (strcmp (language_string, "GNU Objective-C++") == 0)
19541 language = DW_LANG_ObjC_plus_plus;
19542 else if (dwarf_version >= 5 || !dwarf_strict)
19544 if (strcmp (language_string, "GNU Go") == 0)
19545 language = DW_LANG_Go;
19548 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
19549 else if (strcmp (language_string, "GNU Fortran") == 0)
19550 language = DW_LANG_Fortran90;
19552 add_AT_unsigned (die, DW_AT_language, language);
19554 switch (language)
19556 case DW_LANG_Fortran77:
19557 case DW_LANG_Fortran90:
19558 case DW_LANG_Fortran95:
19559 /* Fortran has case insensitive identifiers and the front-end
19560 lowercases everything. */
19561 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
19562 break;
19563 default:
19564 /* The default DW_ID_case_sensitive doesn't need to be specified. */
19565 break;
19567 return die;
19570 /* Generate the DIE for a base class. */
19572 static void
19573 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
19575 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
19577 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, context_die);
19578 add_data_member_location_attribute (die, binfo);
19580 if (BINFO_VIRTUAL_P (binfo))
19581 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
19583 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
19584 children, otherwise the default is DW_ACCESS_public. In DWARF2
19585 the default has always been DW_ACCESS_private. */
19586 if (access == access_public_node)
19588 if (dwarf_version == 2
19589 || context_die->die_tag == DW_TAG_class_type)
19590 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
19592 else if (access == access_protected_node)
19593 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
19594 else if (dwarf_version > 2
19595 && context_die->die_tag != DW_TAG_class_type)
19596 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
19599 /* Generate a DIE for a class member. */
19601 static void
19602 gen_member_die (tree type, dw_die_ref context_die)
19604 tree member;
19605 tree binfo = TYPE_BINFO (type);
19606 dw_die_ref child;
19608 /* If this is not an incomplete type, output descriptions of each of its
19609 members. Note that as we output the DIEs necessary to represent the
19610 members of this record or union type, we will also be trying to output
19611 DIEs to represent the *types* of those members. However the `type'
19612 function (above) will specifically avoid generating type DIEs for member
19613 types *within* the list of member DIEs for this (containing) type except
19614 for those types (of members) which are explicitly marked as also being
19615 members of this (containing) type themselves. The g++ front- end can
19616 force any given type to be treated as a member of some other (containing)
19617 type by setting the TYPE_CONTEXT of the given (member) type to point to
19618 the TREE node representing the appropriate (containing) type. */
19620 /* First output info about the base classes. */
19621 if (binfo)
19623 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
19624 int i;
19625 tree base;
19627 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
19628 gen_inheritance_die (base,
19629 (accesses ? (*accesses)[i] : access_public_node),
19630 context_die);
19633 /* Now output info about the data members and type members. */
19634 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
19636 /* If we thought we were generating minimal debug info for TYPE
19637 and then changed our minds, some of the member declarations
19638 may have already been defined. Don't define them again, but
19639 do put them in the right order. */
19641 child = lookup_decl_die (member);
19642 if (child)
19643 splice_child_die (context_die, child);
19644 else
19645 gen_decl_die (member, NULL, context_die);
19648 /* Now output info about the function members (if any). */
19649 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
19651 /* Don't include clones in the member list. */
19652 if (DECL_ABSTRACT_ORIGIN (member))
19653 continue;
19655 child = lookup_decl_die (member);
19656 if (child)
19657 splice_child_die (context_die, child);
19658 else
19659 gen_decl_die (member, NULL, context_die);
19663 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
19664 is set, we pretend that the type was never defined, so we only get the
19665 member DIEs needed by later specification DIEs. */
19667 static void
19668 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
19669 enum debug_info_usage usage)
19671 dw_die_ref type_die = lookup_type_die (type);
19672 dw_die_ref scope_die = 0;
19673 int nested = 0;
19674 int complete = (TYPE_SIZE (type)
19675 && (! TYPE_STUB_DECL (type)
19676 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
19677 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
19678 complete = complete && should_emit_struct_debug (type, usage);
19680 if (type_die && ! complete)
19681 return;
19683 if (TYPE_CONTEXT (type) != NULL_TREE
19684 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19685 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
19686 nested = 1;
19688 scope_die = scope_die_for (type, context_die);
19690 /* Generate child dies for template paramaters. */
19691 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
19692 schedule_generic_params_dies_gen (type);
19694 if (! type_die || (nested && is_cu_die (scope_die)))
19695 /* First occurrence of type or toplevel definition of nested class. */
19697 dw_die_ref old_die = type_die;
19699 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
19700 ? record_type_tag (type) : DW_TAG_union_type,
19701 scope_die, type);
19702 equate_type_number_to_die (type, type_die);
19703 if (old_die)
19704 add_AT_specification (type_die, old_die);
19705 else
19706 add_name_attribute (type_die, type_tag (type));
19708 else
19709 remove_AT (type_die, DW_AT_declaration);
19711 /* If this type has been completed, then give it a byte_size attribute and
19712 then give a list of members. */
19713 if (complete && !ns_decl)
19715 /* Prevent infinite recursion in cases where the type of some member of
19716 this type is expressed in terms of this type itself. */
19717 TREE_ASM_WRITTEN (type) = 1;
19718 add_byte_size_attribute (type_die, type);
19719 if (TYPE_STUB_DECL (type) != NULL_TREE)
19721 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
19722 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
19725 /* If the first reference to this type was as the return type of an
19726 inline function, then it may not have a parent. Fix this now. */
19727 if (type_die->die_parent == NULL)
19728 add_child_die (scope_die, type_die);
19730 push_decl_scope (type);
19731 gen_member_die (type, type_die);
19732 pop_decl_scope ();
19734 add_gnat_descriptive_type_attribute (type_die, type, context_die);
19735 if (TYPE_ARTIFICIAL (type))
19736 add_AT_flag (type_die, DW_AT_artificial, 1);
19738 /* GNU extension: Record what type our vtable lives in. */
19739 if (TYPE_VFIELD (type))
19741 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
19743 gen_type_die (vtype, context_die);
19744 add_AT_die_ref (type_die, DW_AT_containing_type,
19745 lookup_type_die (vtype));
19748 else
19750 add_AT_flag (type_die, DW_AT_declaration, 1);
19752 /* We don't need to do this for function-local types. */
19753 if (TYPE_STUB_DECL (type)
19754 && ! decl_function_context (TYPE_STUB_DECL (type)))
19755 vec_safe_push (incomplete_types, type);
19758 if (get_AT (type_die, DW_AT_name))
19759 add_pubtype (type, type_die);
19762 /* Generate a DIE for a subroutine _type_. */
19764 static void
19765 gen_subroutine_type_die (tree type, dw_die_ref context_die)
19767 tree return_type = TREE_TYPE (type);
19768 dw_die_ref subr_die
19769 = new_die (DW_TAG_subroutine_type,
19770 scope_die_for (type, context_die), type);
19772 equate_type_number_to_die (type, subr_die);
19773 add_prototyped_attribute (subr_die, type);
19774 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, context_die);
19775 gen_formal_types_die (type, subr_die);
19777 if (get_AT (subr_die, DW_AT_name))
19778 add_pubtype (type, subr_die);
19781 /* Generate a DIE for a type definition. */
19783 static void
19784 gen_typedef_die (tree decl, dw_die_ref context_die)
19786 dw_die_ref type_die;
19787 tree origin;
19789 if (TREE_ASM_WRITTEN (decl))
19790 return;
19792 TREE_ASM_WRITTEN (decl) = 1;
19793 type_die = new_die (DW_TAG_typedef, context_die, decl);
19794 origin = decl_ultimate_origin (decl);
19795 if (origin != NULL)
19796 add_abstract_origin_attribute (type_die, origin);
19797 else
19799 tree type;
19801 add_name_and_src_coords_attributes (type_die, decl);
19802 if (DECL_ORIGINAL_TYPE (decl))
19804 type = DECL_ORIGINAL_TYPE (decl);
19806 gcc_assert (type != TREE_TYPE (decl));
19807 equate_type_number_to_die (TREE_TYPE (decl), type_die);
19809 else
19811 type = TREE_TYPE (decl);
19813 if (is_naming_typedef_decl (TYPE_NAME (type)))
19815 /* Here, we are in the case of decl being a typedef naming
19816 an anonymous type, e.g:
19817 typedef struct {...} foo;
19818 In that case TREE_TYPE (decl) is not a typedef variant
19819 type and TYPE_NAME of the anonymous type is set to the
19820 TYPE_DECL of the typedef. This construct is emitted by
19821 the C++ FE.
19823 TYPE is the anonymous struct named by the typedef
19824 DECL. As we need the DW_AT_type attribute of the
19825 DW_TAG_typedef to point to the DIE of TYPE, let's
19826 generate that DIE right away. add_type_attribute
19827 called below will then pick (via lookup_type_die) that
19828 anonymous struct DIE. */
19829 if (!TREE_ASM_WRITTEN (type))
19830 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
19832 /* This is a GNU Extension. We are adding a
19833 DW_AT_linkage_name attribute to the DIE of the
19834 anonymous struct TYPE. The value of that attribute
19835 is the name of the typedef decl naming the anonymous
19836 struct. This greatly eases the work of consumers of
19837 this debug info. */
19838 add_linkage_attr (lookup_type_die (type), decl);
19842 add_type_attribute (type_die, type, decl_quals (decl), context_die);
19844 if (is_naming_typedef_decl (decl))
19845 /* We want that all subsequent calls to lookup_type_die with
19846 TYPE in argument yield the DW_TAG_typedef we have just
19847 created. */
19848 equate_type_number_to_die (type, type_die);
19850 add_accessibility_attribute (type_die, decl);
19853 if (DECL_ABSTRACT_P (decl))
19854 equate_decl_number_to_die (decl, type_die);
19856 if (get_AT (type_die, DW_AT_name))
19857 add_pubtype (decl, type_die);
19860 /* Generate a DIE for a struct, class, enum or union type. */
19862 static void
19863 gen_tagged_type_die (tree type,
19864 dw_die_ref context_die,
19865 enum debug_info_usage usage)
19867 int need_pop;
19869 if (type == NULL_TREE
19870 || !is_tagged_type (type))
19871 return;
19873 /* If this is a nested type whose containing class hasn't been written
19874 out yet, writing it out will cover this one, too. This does not apply
19875 to instantiations of member class templates; they need to be added to
19876 the containing class as they are generated. FIXME: This hurts the
19877 idea of combining type decls from multiple TUs, since we can't predict
19878 what set of template instantiations we'll get. */
19879 if (TYPE_CONTEXT (type)
19880 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19881 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
19883 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
19885 if (TREE_ASM_WRITTEN (type))
19886 return;
19888 /* If that failed, attach ourselves to the stub. */
19889 push_decl_scope (TYPE_CONTEXT (type));
19890 context_die = lookup_type_die (TYPE_CONTEXT (type));
19891 need_pop = 1;
19893 else if (TYPE_CONTEXT (type) != NULL_TREE
19894 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
19896 /* If this type is local to a function that hasn't been written
19897 out yet, use a NULL context for now; it will be fixed up in
19898 decls_for_scope. */
19899 context_die = lookup_decl_die (TYPE_CONTEXT (type));
19900 /* A declaration DIE doesn't count; nested types need to go in the
19901 specification. */
19902 if (context_die && is_declaration_die (context_die))
19903 context_die = NULL;
19904 need_pop = 0;
19906 else
19908 context_die = declare_in_namespace (type, context_die);
19909 need_pop = 0;
19912 if (TREE_CODE (type) == ENUMERAL_TYPE)
19914 /* This might have been written out by the call to
19915 declare_in_namespace. */
19916 if (!TREE_ASM_WRITTEN (type))
19917 gen_enumeration_type_die (type, context_die);
19919 else
19920 gen_struct_or_union_type_die (type, context_die, usage);
19922 if (need_pop)
19923 pop_decl_scope ();
19925 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
19926 it up if it is ever completed. gen_*_type_die will set it for us
19927 when appropriate. */
19930 /* Generate a type description DIE. */
19932 static void
19933 gen_type_die_with_usage (tree type, dw_die_ref context_die,
19934 enum debug_info_usage usage)
19936 struct array_descr_info info;
19938 if (type == NULL_TREE || type == error_mark_node)
19939 return;
19941 if (TYPE_NAME (type) != NULL_TREE
19942 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
19943 && is_redundant_typedef (TYPE_NAME (type))
19944 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
19945 /* The DECL of this type is a typedef we don't want to emit debug
19946 info for but we want debug info for its underlying typedef.
19947 This can happen for e.g, the injected-class-name of a C++
19948 type. */
19949 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
19951 /* If TYPE is a typedef type variant, let's generate debug info
19952 for the parent typedef which TYPE is a type of. */
19953 if (typedef_variant_p (type))
19955 if (TREE_ASM_WRITTEN (type))
19956 return;
19958 /* Prevent broken recursion; we can't hand off to the same type. */
19959 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
19961 /* Give typedefs the right scope. */
19962 context_die = scope_die_for (type, context_die);
19964 TREE_ASM_WRITTEN (type) = 1;
19966 gen_decl_die (TYPE_NAME (type), NULL, context_die);
19967 return;
19970 /* If type is an anonymous tagged type named by a typedef, let's
19971 generate debug info for the typedef. */
19972 if (is_naming_typedef_decl (TYPE_NAME (type)))
19974 /* Use the DIE of the containing namespace as the parent DIE of
19975 the type description DIE we want to generate. */
19976 if (DECL_CONTEXT (TYPE_NAME (type))
19977 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
19978 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
19980 gen_decl_die (TYPE_NAME (type), NULL, context_die);
19981 return;
19984 /* If this is an array type with hidden descriptor, handle it first. */
19985 if (!TREE_ASM_WRITTEN (type)
19986 && lang_hooks.types.get_array_descr_info
19987 && lang_hooks.types.get_array_descr_info (type, &info)
19988 && (dwarf_version >= 3 || !dwarf_strict))
19990 gen_descr_array_type_die (type, &info, context_die);
19991 TREE_ASM_WRITTEN (type) = 1;
19992 return;
19995 /* We are going to output a DIE to represent the unqualified version
19996 of this type (i.e. without any const or volatile qualifiers) so
19997 get the main variant (i.e. the unqualified version) of this type
19998 now. (Vectors are special because the debugging info is in the
19999 cloned type itself). */
20000 if (TREE_CODE (type) != VECTOR_TYPE)
20001 type = type_main_variant (type);
20003 if (TREE_ASM_WRITTEN (type))
20004 return;
20006 switch (TREE_CODE (type))
20008 case ERROR_MARK:
20009 break;
20011 case POINTER_TYPE:
20012 case REFERENCE_TYPE:
20013 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
20014 ensures that the gen_type_die recursion will terminate even if the
20015 type is recursive. Recursive types are possible in Ada. */
20016 /* ??? We could perhaps do this for all types before the switch
20017 statement. */
20018 TREE_ASM_WRITTEN (type) = 1;
20020 /* For these types, all that is required is that we output a DIE (or a
20021 set of DIEs) to represent the "basis" type. */
20022 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20023 DINFO_USAGE_IND_USE);
20024 break;
20026 case OFFSET_TYPE:
20027 /* This code is used for C++ pointer-to-data-member types.
20028 Output a description of the relevant class type. */
20029 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
20030 DINFO_USAGE_IND_USE);
20032 /* Output a description of the type of the object pointed to. */
20033 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20034 DINFO_USAGE_IND_USE);
20036 /* Now output a DIE to represent this pointer-to-data-member type
20037 itself. */
20038 gen_ptr_to_mbr_type_die (type, context_die);
20039 break;
20041 case FUNCTION_TYPE:
20042 /* Force out return type (in case it wasn't forced out already). */
20043 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20044 DINFO_USAGE_DIR_USE);
20045 gen_subroutine_type_die (type, context_die);
20046 break;
20048 case METHOD_TYPE:
20049 /* Force out return type (in case it wasn't forced out already). */
20050 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20051 DINFO_USAGE_DIR_USE);
20052 gen_subroutine_type_die (type, context_die);
20053 break;
20055 case ARRAY_TYPE:
20056 gen_array_type_die (type, context_die);
20057 break;
20059 case VECTOR_TYPE:
20060 gen_array_type_die (type, context_die);
20061 break;
20063 case ENUMERAL_TYPE:
20064 case RECORD_TYPE:
20065 case UNION_TYPE:
20066 case QUAL_UNION_TYPE:
20067 gen_tagged_type_die (type, context_die, usage);
20068 return;
20070 case VOID_TYPE:
20071 case INTEGER_TYPE:
20072 case REAL_TYPE:
20073 case FIXED_POINT_TYPE:
20074 case COMPLEX_TYPE:
20075 case BOOLEAN_TYPE:
20076 /* No DIEs needed for fundamental types. */
20077 break;
20079 case NULLPTR_TYPE:
20080 case LANG_TYPE:
20081 /* Just use DW_TAG_unspecified_type. */
20083 dw_die_ref type_die = lookup_type_die (type);
20084 if (type_die == NULL)
20086 tree name = TYPE_IDENTIFIER (type);
20087 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
20088 type);
20089 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
20090 equate_type_number_to_die (type, type_die);
20093 break;
20095 default:
20096 if (is_cxx_auto (type))
20098 tree name = TYPE_IDENTIFIER (type);
20099 dw_die_ref *die = (name == get_identifier ("auto")
20100 ? &auto_die : &decltype_auto_die);
20101 if (!*die)
20103 *die = new_die (DW_TAG_unspecified_type,
20104 comp_unit_die (), NULL_TREE);
20105 add_name_attribute (*die, IDENTIFIER_POINTER (name));
20107 equate_type_number_to_die (type, *die);
20108 break;
20110 gcc_unreachable ();
20113 TREE_ASM_WRITTEN (type) = 1;
20116 static void
20117 gen_type_die (tree type, dw_die_ref context_die)
20119 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
20122 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
20123 things which are local to the given block. */
20125 static void
20126 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
20128 int must_output_die = 0;
20129 bool inlined_func;
20131 /* Ignore blocks that are NULL. */
20132 if (stmt == NULL_TREE)
20133 return;
20135 inlined_func = inlined_function_outer_scope_p (stmt);
20137 /* If the block is one fragment of a non-contiguous block, do not
20138 process the variables, since they will have been done by the
20139 origin block. Do process subblocks. */
20140 if (BLOCK_FRAGMENT_ORIGIN (stmt))
20142 tree sub;
20144 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
20145 gen_block_die (sub, context_die, depth + 1);
20147 return;
20150 /* Determine if we need to output any Dwarf DIEs at all to represent this
20151 block. */
20152 if (inlined_func)
20153 /* The outer scopes for inlinings *must* always be represented. We
20154 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
20155 must_output_die = 1;
20156 else
20158 /* Determine if this block directly contains any "significant"
20159 local declarations which we will need to output DIEs for. */
20160 if (debug_info_level > DINFO_LEVEL_TERSE)
20161 /* We are not in terse mode so *any* local declaration counts
20162 as being a "significant" one. */
20163 must_output_die = ((BLOCK_VARS (stmt) != NULL
20164 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
20165 && (TREE_USED (stmt)
20166 || TREE_ASM_WRITTEN (stmt)
20167 || BLOCK_ABSTRACT (stmt)));
20168 else if ((TREE_USED (stmt)
20169 || TREE_ASM_WRITTEN (stmt)
20170 || BLOCK_ABSTRACT (stmt))
20171 && !dwarf2out_ignore_block (stmt))
20172 must_output_die = 1;
20175 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
20176 DIE for any block which contains no significant local declarations at
20177 all. Rather, in such cases we just call `decls_for_scope' so that any
20178 needed Dwarf info for any sub-blocks will get properly generated. Note
20179 that in terse mode, our definition of what constitutes a "significant"
20180 local declaration gets restricted to include only inlined function
20181 instances and local (nested) function definitions. */
20182 if (must_output_die)
20184 if (inlined_func)
20186 /* If STMT block is abstract, that means we have been called
20187 indirectly from dwarf2out_abstract_function.
20188 That function rightfully marks the descendent blocks (of
20189 the abstract function it is dealing with) as being abstract,
20190 precisely to prevent us from emitting any
20191 DW_TAG_inlined_subroutine DIE as a descendent
20192 of an abstract function instance. So in that case, we should
20193 not call gen_inlined_subroutine_die.
20195 Later though, when cgraph asks dwarf2out to emit info
20196 for the concrete instance of the function decl into which
20197 the concrete instance of STMT got inlined, the later will lead
20198 to the generation of a DW_TAG_inlined_subroutine DIE. */
20199 if (! BLOCK_ABSTRACT (stmt))
20200 gen_inlined_subroutine_die (stmt, context_die, depth);
20202 else
20203 gen_lexical_block_die (stmt, context_die, depth);
20205 else
20206 decls_for_scope (stmt, context_die, depth);
20209 /* Process variable DECL (or variable with origin ORIGIN) within
20210 block STMT and add it to CONTEXT_DIE. */
20211 static void
20212 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
20214 dw_die_ref die;
20215 tree decl_or_origin = decl ? decl : origin;
20217 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
20218 die = lookup_decl_die (decl_or_origin);
20219 else if (TREE_CODE (decl_or_origin) == TYPE_DECL
20220 && TYPE_DECL_IS_STUB (decl_or_origin))
20221 die = lookup_type_die (TREE_TYPE (decl_or_origin));
20222 else
20223 die = NULL;
20225 if (die != NULL && die->die_parent == NULL)
20226 add_child_die (context_die, die);
20227 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
20228 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
20229 stmt, context_die);
20230 else
20231 gen_decl_die (decl, origin, context_die);
20234 /* Generate all of the decls declared within a given scope and (recursively)
20235 all of its sub-blocks. */
20237 static void
20238 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
20240 tree decl;
20241 unsigned int i;
20242 tree subblocks;
20244 /* Ignore NULL blocks. */
20245 if (stmt == NULL_TREE)
20246 return;
20248 /* Output the DIEs to represent all of the data objects and typedefs
20249 declared directly within this block but not within any nested
20250 sub-blocks. Also, nested function and tag DIEs have been
20251 generated with a parent of NULL; fix that up now. We don't
20252 have to do this if we're at -g1. */
20253 if (debug_info_level > DINFO_LEVEL_TERSE)
20255 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
20256 process_scope_var (stmt, decl, NULL_TREE, context_die);
20257 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
20258 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
20259 context_die);
20262 /* Even if we're at -g1, we need to process the subblocks in order to get
20263 inlined call information. */
20265 /* Output the DIEs to represent all sub-blocks (and the items declared
20266 therein) of this block. */
20267 for (subblocks = BLOCK_SUBBLOCKS (stmt);
20268 subblocks != NULL;
20269 subblocks = BLOCK_CHAIN (subblocks))
20270 gen_block_die (subblocks, context_die, depth + 1);
20273 /* Is this a typedef we can avoid emitting? */
20275 static inline int
20276 is_redundant_typedef (const_tree decl)
20278 if (TYPE_DECL_IS_STUB (decl))
20279 return 1;
20281 if (DECL_ARTIFICIAL (decl)
20282 && DECL_CONTEXT (decl)
20283 && is_tagged_type (DECL_CONTEXT (decl))
20284 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
20285 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
20286 /* Also ignore the artificial member typedef for the class name. */
20287 return 1;
20289 return 0;
20292 /* Return TRUE if TYPE is a typedef that names a type for linkage
20293 purposes. This kind of typedefs is produced by the C++ FE for
20294 constructs like:
20296 typedef struct {...} foo;
20298 In that case, there is no typedef variant type produced for foo.
20299 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
20300 struct type. */
20302 static bool
20303 is_naming_typedef_decl (const_tree decl)
20305 if (decl == NULL_TREE
20306 || TREE_CODE (decl) != TYPE_DECL
20307 || !is_tagged_type (TREE_TYPE (decl))
20308 || DECL_IS_BUILTIN (decl)
20309 || is_redundant_typedef (decl)
20310 /* It looks like Ada produces TYPE_DECLs that are very similar
20311 to C++ naming typedefs but that have different
20312 semantics. Let's be specific to c++ for now. */
20313 || !is_cxx ())
20314 return FALSE;
20316 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
20317 && TYPE_NAME (TREE_TYPE (decl)) == decl
20318 && (TYPE_STUB_DECL (TREE_TYPE (decl))
20319 != TYPE_NAME (TREE_TYPE (decl))));
20322 /* Returns the DIE for a context. */
20324 static inline dw_die_ref
20325 get_context_die (tree context)
20327 if (context)
20329 /* Find die that represents this context. */
20330 if (TYPE_P (context))
20332 context = TYPE_MAIN_VARIANT (context);
20333 return strip_naming_typedef (context, force_type_die (context));
20335 else
20336 return force_decl_die (context);
20338 return comp_unit_die ();
20341 /* Returns the DIE for decl. A DIE will always be returned. */
20343 static dw_die_ref
20344 force_decl_die (tree decl)
20346 dw_die_ref decl_die;
20347 unsigned saved_external_flag;
20348 tree save_fn = NULL_TREE;
20349 decl_die = lookup_decl_die (decl);
20350 if (!decl_die)
20352 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
20354 decl_die = lookup_decl_die (decl);
20355 if (decl_die)
20356 return decl_die;
20358 switch (TREE_CODE (decl))
20360 case FUNCTION_DECL:
20361 /* Clear current_function_decl, so that gen_subprogram_die thinks
20362 that this is a declaration. At this point, we just want to force
20363 declaration die. */
20364 save_fn = current_function_decl;
20365 current_function_decl = NULL_TREE;
20366 gen_subprogram_die (decl, context_die);
20367 current_function_decl = save_fn;
20368 break;
20370 case VAR_DECL:
20371 /* Set external flag to force declaration die. Restore it after
20372 gen_decl_die() call. */
20373 saved_external_flag = DECL_EXTERNAL (decl);
20374 DECL_EXTERNAL (decl) = 1;
20375 gen_decl_die (decl, NULL, context_die);
20376 DECL_EXTERNAL (decl) = saved_external_flag;
20377 break;
20379 case NAMESPACE_DECL:
20380 if (dwarf_version >= 3 || !dwarf_strict)
20381 dwarf2out_decl (decl);
20382 else
20383 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
20384 decl_die = comp_unit_die ();
20385 break;
20387 case TRANSLATION_UNIT_DECL:
20388 decl_die = comp_unit_die ();
20389 break;
20391 default:
20392 gcc_unreachable ();
20395 /* We should be able to find the DIE now. */
20396 if (!decl_die)
20397 decl_die = lookup_decl_die (decl);
20398 gcc_assert (decl_die);
20401 return decl_die;
20404 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
20405 always returned. */
20407 static dw_die_ref
20408 force_type_die (tree type)
20410 dw_die_ref type_die;
20412 type_die = lookup_type_die (type);
20413 if (!type_die)
20415 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
20417 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
20418 context_die);
20419 gcc_assert (type_die);
20421 return type_die;
20424 /* Force out any required namespaces to be able to output DECL,
20425 and return the new context_die for it, if it's changed. */
20427 static dw_die_ref
20428 setup_namespace_context (tree thing, dw_die_ref context_die)
20430 tree context = (DECL_P (thing)
20431 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
20432 if (context && TREE_CODE (context) == NAMESPACE_DECL)
20433 /* Force out the namespace. */
20434 context_die = force_decl_die (context);
20436 return context_die;
20439 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
20440 type) within its namespace, if appropriate.
20442 For compatibility with older debuggers, namespace DIEs only contain
20443 declarations; all definitions are emitted at CU scope. */
20445 static dw_die_ref
20446 declare_in_namespace (tree thing, dw_die_ref context_die)
20448 dw_die_ref ns_context;
20450 if (debug_info_level <= DINFO_LEVEL_TERSE)
20451 return context_die;
20453 /* If this decl is from an inlined function, then don't try to emit it in its
20454 namespace, as we will get confused. It would have already been emitted
20455 when the abstract instance of the inline function was emitted anyways. */
20456 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
20457 return context_die;
20459 ns_context = setup_namespace_context (thing, context_die);
20461 if (ns_context != context_die)
20463 if (is_fortran ())
20464 return ns_context;
20465 if (DECL_P (thing))
20466 gen_decl_die (thing, NULL, ns_context);
20467 else
20468 gen_type_die (thing, ns_context);
20470 return context_die;
20473 /* Generate a DIE for a namespace or namespace alias. */
20475 static void
20476 gen_namespace_die (tree decl, dw_die_ref context_die)
20478 dw_die_ref namespace_die;
20480 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
20481 they are an alias of. */
20482 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
20484 /* Output a real namespace or module. */
20485 context_die = setup_namespace_context (decl, comp_unit_die ());
20486 namespace_die = new_die (is_fortran ()
20487 ? DW_TAG_module : DW_TAG_namespace,
20488 context_die, decl);
20489 /* For Fortran modules defined in different CU don't add src coords. */
20490 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
20492 const char *name = dwarf2_name (decl, 0);
20493 if (name)
20494 add_name_attribute (namespace_die, name);
20496 else
20497 add_name_and_src_coords_attributes (namespace_die, decl);
20498 if (DECL_EXTERNAL (decl))
20499 add_AT_flag (namespace_die, DW_AT_declaration, 1);
20500 equate_decl_number_to_die (decl, namespace_die);
20502 else
20504 /* Output a namespace alias. */
20506 /* Force out the namespace we are an alias of, if necessary. */
20507 dw_die_ref origin_die
20508 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
20510 if (DECL_FILE_SCOPE_P (decl)
20511 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
20512 context_die = setup_namespace_context (decl, comp_unit_die ());
20513 /* Now create the namespace alias DIE. */
20514 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
20515 add_name_and_src_coords_attributes (namespace_die, decl);
20516 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
20517 equate_decl_number_to_die (decl, namespace_die);
20519 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
20520 if (want_pubnames ())
20521 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
20524 /* Generate Dwarf debug information for a decl described by DECL.
20525 The return value is currently only meaningful for PARM_DECLs,
20526 for all other decls it returns NULL. */
20528 static dw_die_ref
20529 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
20531 tree decl_or_origin = decl ? decl : origin;
20532 tree class_origin = NULL, ultimate_origin;
20534 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
20535 return NULL;
20537 switch (TREE_CODE (decl_or_origin))
20539 case ERROR_MARK:
20540 break;
20542 case CONST_DECL:
20543 if (!is_fortran () && !is_ada ())
20545 /* The individual enumerators of an enum type get output when we output
20546 the Dwarf representation of the relevant enum type itself. */
20547 break;
20550 /* Emit its type. */
20551 gen_type_die (TREE_TYPE (decl), context_die);
20553 /* And its containing namespace. */
20554 context_die = declare_in_namespace (decl, context_die);
20556 gen_const_die (decl, context_die);
20557 break;
20559 case FUNCTION_DECL:
20560 /* Don't output any DIEs to represent mere function declarations,
20561 unless they are class members or explicit block externs. */
20562 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
20563 && DECL_FILE_SCOPE_P (decl_or_origin)
20564 && (current_function_decl == NULL_TREE
20565 || DECL_ARTIFICIAL (decl_or_origin)))
20566 break;
20568 #if 0
20569 /* FIXME */
20570 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
20571 on local redeclarations of global functions. That seems broken. */
20572 if (current_function_decl != decl)
20573 /* This is only a declaration. */;
20574 #endif
20576 /* If we're emitting a clone, emit info for the abstract instance. */
20577 if (origin || DECL_ORIGIN (decl) != decl)
20578 dwarf2out_abstract_function (origin
20579 ? DECL_ORIGIN (origin)
20580 : DECL_ABSTRACT_ORIGIN (decl));
20582 /* If we're emitting an out-of-line copy of an inline function,
20583 emit info for the abstract instance and set up to refer to it. */
20584 else if (cgraph_function_possibly_inlined_p (decl)
20585 && ! DECL_ABSTRACT_P (decl)
20586 && ! class_or_namespace_scope_p (context_die)
20587 /* dwarf2out_abstract_function won't emit a die if this is just
20588 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
20589 that case, because that works only if we have a die. */
20590 && DECL_INITIAL (decl) != NULL_TREE)
20592 dwarf2out_abstract_function (decl);
20593 set_decl_origin_self (decl);
20596 /* Otherwise we're emitting the primary DIE for this decl. */
20597 else if (debug_info_level > DINFO_LEVEL_TERSE)
20599 /* Before we describe the FUNCTION_DECL itself, make sure that we
20600 have its containing type. */
20601 if (!origin)
20602 origin = decl_class_context (decl);
20603 if (origin != NULL_TREE)
20604 gen_type_die (origin, context_die);
20606 /* And its return type. */
20607 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
20609 /* And its virtual context. */
20610 if (DECL_VINDEX (decl) != NULL_TREE)
20611 gen_type_die (DECL_CONTEXT (decl), context_die);
20613 /* Make sure we have a member DIE for decl. */
20614 if (origin != NULL_TREE)
20615 gen_type_die_for_member (origin, decl, context_die);
20617 /* And its containing namespace. */
20618 context_die = declare_in_namespace (decl, context_die);
20621 /* Now output a DIE to represent the function itself. */
20622 if (decl)
20623 gen_subprogram_die (decl, context_die);
20624 break;
20626 case TYPE_DECL:
20627 /* If we are in terse mode, don't generate any DIEs to represent any
20628 actual typedefs. */
20629 if (debug_info_level <= DINFO_LEVEL_TERSE)
20630 break;
20632 /* In the special case of a TYPE_DECL node representing the declaration
20633 of some type tag, if the given TYPE_DECL is marked as having been
20634 instantiated from some other (original) TYPE_DECL node (e.g. one which
20635 was generated within the original definition of an inline function) we
20636 used to generate a special (abbreviated) DW_TAG_structure_type,
20637 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
20638 should be actually referencing those DIEs, as variable DIEs with that
20639 type would be emitted already in the abstract origin, so it was always
20640 removed during unused type prunning. Don't add anything in this
20641 case. */
20642 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
20643 break;
20645 if (is_redundant_typedef (decl))
20646 gen_type_die (TREE_TYPE (decl), context_die);
20647 else
20648 /* Output a DIE to represent the typedef itself. */
20649 gen_typedef_die (decl, context_die);
20650 break;
20652 case LABEL_DECL:
20653 if (debug_info_level >= DINFO_LEVEL_NORMAL)
20654 gen_label_die (decl, context_die);
20655 break;
20657 case VAR_DECL:
20658 case RESULT_DECL:
20659 /* If we are in terse mode, don't generate any DIEs to represent any
20660 variable declarations or definitions. */
20661 if (debug_info_level <= DINFO_LEVEL_TERSE)
20662 break;
20664 /* Output any DIEs that are needed to specify the type of this data
20665 object. */
20666 if (decl_by_reference_p (decl_or_origin))
20667 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20668 else
20669 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20671 /* And its containing type. */
20672 class_origin = decl_class_context (decl_or_origin);
20673 if (class_origin != NULL_TREE)
20674 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
20676 /* And its containing namespace. */
20677 context_die = declare_in_namespace (decl_or_origin, context_die);
20679 /* Now output the DIE to represent the data object itself. This gets
20680 complicated because of the possibility that the VAR_DECL really
20681 represents an inlined instance of a formal parameter for an inline
20682 function. */
20683 ultimate_origin = decl_ultimate_origin (decl_or_origin);
20684 if (ultimate_origin != NULL_TREE
20685 && TREE_CODE (ultimate_origin) == PARM_DECL)
20686 gen_formal_parameter_die (decl, origin,
20687 true /* Emit name attribute. */,
20688 context_die);
20689 else
20690 gen_variable_die (decl, origin, context_die);
20691 break;
20693 case FIELD_DECL:
20694 /* Ignore the nameless fields that are used to skip bits but handle C++
20695 anonymous unions and structs. */
20696 if (DECL_NAME (decl) != NULL_TREE
20697 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
20698 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
20700 gen_type_die (member_declared_type (decl), context_die);
20701 gen_field_die (decl, context_die);
20703 break;
20705 case PARM_DECL:
20706 if (DECL_BY_REFERENCE (decl_or_origin))
20707 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20708 else
20709 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20710 return gen_formal_parameter_die (decl, origin,
20711 true /* Emit name attribute. */,
20712 context_die);
20714 case NAMESPACE_DECL:
20715 case IMPORTED_DECL:
20716 if (dwarf_version >= 3 || !dwarf_strict)
20717 gen_namespace_die (decl, context_die);
20718 break;
20720 case NAMELIST_DECL:
20721 gen_namelist_decl (DECL_NAME (decl), context_die,
20722 NAMELIST_DECL_ASSOCIATED_DECL (decl));
20723 break;
20725 default:
20726 /* Probably some frontend-internal decl. Assume we don't care. */
20727 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
20728 break;
20731 return NULL;
20734 /* Output debug information for global decl DECL. Called from toplev.c after
20735 compilation proper has finished. */
20737 static void
20738 dwarf2out_global_decl (tree decl)
20740 /* Output DWARF2 information for file-scope tentative data object
20741 declarations, file-scope (extern) function declarations (which
20742 had no corresponding body) and file-scope tagged type declarations
20743 and definitions which have not yet been forced out. */
20744 if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
20745 dwarf2out_decl (decl);
20748 /* Output debug information for type decl DECL. Called from toplev.c
20749 and from language front ends (to record built-in types). */
20750 static void
20751 dwarf2out_type_decl (tree decl, int local)
20753 if (!local)
20754 dwarf2out_decl (decl);
20757 /* Output debug information for imported module or decl DECL.
20758 NAME is non-NULL name in the lexical block if the decl has been renamed.
20759 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
20760 that DECL belongs to.
20761 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
20762 static void
20763 dwarf2out_imported_module_or_decl_1 (tree decl,
20764 tree name,
20765 tree lexical_block,
20766 dw_die_ref lexical_block_die)
20768 expanded_location xloc;
20769 dw_die_ref imported_die = NULL;
20770 dw_die_ref at_import_die;
20772 if (TREE_CODE (decl) == IMPORTED_DECL)
20774 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
20775 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
20776 gcc_assert (decl);
20778 else
20779 xloc = expand_location (input_location);
20781 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
20783 at_import_die = force_type_die (TREE_TYPE (decl));
20784 /* For namespace N { typedef void T; } using N::T; base_type_die
20785 returns NULL, but DW_TAG_imported_declaration requires
20786 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
20787 if (!at_import_die)
20789 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
20790 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
20791 at_import_die = lookup_type_die (TREE_TYPE (decl));
20792 gcc_assert (at_import_die);
20795 else
20797 at_import_die = lookup_decl_die (decl);
20798 if (!at_import_die)
20800 /* If we're trying to avoid duplicate debug info, we may not have
20801 emitted the member decl for this field. Emit it now. */
20802 if (TREE_CODE (decl) == FIELD_DECL)
20804 tree type = DECL_CONTEXT (decl);
20806 if (TYPE_CONTEXT (type)
20807 && TYPE_P (TYPE_CONTEXT (type))
20808 && !should_emit_struct_debug (TYPE_CONTEXT (type),
20809 DINFO_USAGE_DIR_USE))
20810 return;
20811 gen_type_die_for_member (type, decl,
20812 get_context_die (TYPE_CONTEXT (type)));
20814 if (TREE_CODE (decl) == NAMELIST_DECL)
20815 at_import_die = gen_namelist_decl (DECL_NAME (decl),
20816 get_context_die (DECL_CONTEXT (decl)),
20817 NULL_TREE);
20818 else
20819 at_import_die = force_decl_die (decl);
20823 if (TREE_CODE (decl) == NAMESPACE_DECL)
20825 if (dwarf_version >= 3 || !dwarf_strict)
20826 imported_die = new_die (DW_TAG_imported_module,
20827 lexical_block_die,
20828 lexical_block);
20829 else
20830 return;
20832 else
20833 imported_die = new_die (DW_TAG_imported_declaration,
20834 lexical_block_die,
20835 lexical_block);
20837 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
20838 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
20839 if (name)
20840 add_AT_string (imported_die, DW_AT_name,
20841 IDENTIFIER_POINTER (name));
20842 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
20845 /* Output debug information for imported module or decl DECL.
20846 NAME is non-NULL name in context if the decl has been renamed.
20847 CHILD is true if decl is one of the renamed decls as part of
20848 importing whole module. */
20850 static void
20851 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
20852 bool child)
20854 /* dw_die_ref at_import_die; */
20855 dw_die_ref scope_die;
20857 if (debug_info_level <= DINFO_LEVEL_TERSE)
20858 return;
20860 gcc_assert (decl);
20862 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
20863 We need decl DIE for reference and scope die. First, get DIE for the decl
20864 itself. */
20866 /* Get the scope die for decl context. Use comp_unit_die for global module
20867 or decl. If die is not found for non globals, force new die. */
20868 if (context
20869 && TYPE_P (context)
20870 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
20871 return;
20873 if (!(dwarf_version >= 3 || !dwarf_strict))
20874 return;
20876 scope_die = get_context_die (context);
20878 if (child)
20880 gcc_assert (scope_die->die_child);
20881 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
20882 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
20883 scope_die = scope_die->die_child;
20886 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
20887 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
20891 /* Output debug information for namelists. */
20893 static dw_die_ref
20894 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
20896 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
20897 tree value;
20898 unsigned i;
20900 if (debug_info_level <= DINFO_LEVEL_TERSE)
20901 return NULL;
20903 gcc_assert (scope_die != NULL);
20904 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
20905 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
20907 /* If there are no item_decls, we have a nondefining namelist, e.g.
20908 with USE association; hence, set DW_AT_declaration. */
20909 if (item_decls == NULL_TREE)
20911 add_AT_flag (nml_die, DW_AT_declaration, 1);
20912 return nml_die;
20915 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
20917 nml_item_ref_die = lookup_decl_die (value);
20918 if (!nml_item_ref_die)
20919 nml_item_ref_die = force_decl_die (value);
20921 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
20922 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
20924 return nml_die;
20928 /* Write the debugging output for DECL. */
20930 static void
20931 dwarf2out_decl (tree decl)
20933 dw_die_ref context_die = comp_unit_die ();
20935 switch (TREE_CODE (decl))
20937 case ERROR_MARK:
20938 return;
20940 case FUNCTION_DECL:
20941 /* What we would really like to do here is to filter out all mere
20942 file-scope declarations of file-scope functions which are never
20943 referenced later within this translation unit (and keep all of ones
20944 that *are* referenced later on) but we aren't clairvoyant, so we have
20945 no idea which functions will be referenced in the future (i.e. later
20946 on within the current translation unit). So here we just ignore all
20947 file-scope function declarations which are not also definitions. If
20948 and when the debugger needs to know something about these functions,
20949 it will have to hunt around and find the DWARF information associated
20950 with the definition of the function.
20952 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
20953 nodes represent definitions and which ones represent mere
20954 declarations. We have to check DECL_INITIAL instead. That's because
20955 the C front-end supports some weird semantics for "extern inline"
20956 function definitions. These can get inlined within the current
20957 translation unit (and thus, we need to generate Dwarf info for their
20958 abstract instances so that the Dwarf info for the concrete inlined
20959 instances can have something to refer to) but the compiler never
20960 generates any out-of-lines instances of such things (despite the fact
20961 that they *are* definitions).
20963 The important point is that the C front-end marks these "extern
20964 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
20965 them anyway. Note that the C++ front-end also plays some similar games
20966 for inline function definitions appearing within include files which
20967 also contain `#pragma interface' pragmas.
20969 If we are called from dwarf2out_abstract_function output a DIE
20970 anyway. We can end up here this way with early inlining and LTO
20971 where the inlined function is output in a different LTRANS unit
20972 or not at all. */
20973 if (DECL_INITIAL (decl) == NULL_TREE
20974 && ! DECL_ABSTRACT_P (decl))
20975 return;
20977 /* If we're a nested function, initially use a parent of NULL; if we're
20978 a plain function, this will be fixed up in decls_for_scope. If
20979 we're a method, it will be ignored, since we already have a DIE. */
20980 if (decl_function_context (decl)
20981 /* But if we're in terse mode, we don't care about scope. */
20982 && debug_info_level > DINFO_LEVEL_TERSE)
20983 context_die = NULL;
20984 break;
20986 case VAR_DECL:
20987 /* Ignore this VAR_DECL if it refers to a file-scope extern data object
20988 declaration and if the declaration was never even referenced from
20989 within this entire compilation unit. We suppress these DIEs in
20990 order to save space in the .debug section (by eliminating entries
20991 which are probably useless). Note that we must not suppress
20992 block-local extern declarations (whether used or not) because that
20993 would screw-up the debugger's name lookup mechanism and cause it to
20994 miss things which really ought to be in scope at a given point. */
20995 if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
20996 return;
20998 /* For local statics lookup proper context die. */
20999 if (TREE_STATIC (decl)
21000 && DECL_CONTEXT (decl)
21001 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
21002 context_die = lookup_decl_die (DECL_CONTEXT (decl));
21004 /* If we are in terse mode, don't generate any DIEs to represent any
21005 variable declarations or definitions. */
21006 if (debug_info_level <= DINFO_LEVEL_TERSE)
21007 return;
21008 break;
21010 case CONST_DECL:
21011 if (debug_info_level <= DINFO_LEVEL_TERSE)
21012 return;
21013 if (!is_fortran () && !is_ada ())
21014 return;
21015 if (TREE_STATIC (decl) && decl_function_context (decl))
21016 context_die = lookup_decl_die (DECL_CONTEXT (decl));
21017 break;
21019 case NAMESPACE_DECL:
21020 case IMPORTED_DECL:
21021 if (debug_info_level <= DINFO_LEVEL_TERSE)
21022 return;
21023 if (lookup_decl_die (decl) != NULL)
21024 return;
21025 break;
21027 case TYPE_DECL:
21028 /* Don't emit stubs for types unless they are needed by other DIEs. */
21029 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
21030 return;
21032 /* Don't bother trying to generate any DIEs to represent any of the
21033 normal built-in types for the language we are compiling. */
21034 if (DECL_IS_BUILTIN (decl))
21035 return;
21037 /* If we are in terse mode, don't generate any DIEs for types. */
21038 if (debug_info_level <= DINFO_LEVEL_TERSE)
21039 return;
21041 /* If we're a function-scope tag, initially use a parent of NULL;
21042 this will be fixed up in decls_for_scope. */
21043 if (decl_function_context (decl))
21044 context_die = NULL;
21046 break;
21048 case NAMELIST_DECL:
21049 break;
21051 default:
21052 return;
21055 gen_decl_die (decl, NULL, context_die);
21058 /* Write the debugging output for DECL. */
21060 static void
21061 dwarf2out_function_decl (tree decl)
21063 dwarf2out_decl (decl);
21064 call_arg_locations = NULL;
21065 call_arg_loc_last = NULL;
21066 call_site_count = -1;
21067 tail_call_site_count = -1;
21068 block_map.release ();
21069 htab_empty (decl_loc_table);
21070 htab_empty (cached_dw_loc_list_table);
21073 /* Output a marker (i.e. a label) for the beginning of the generated code for
21074 a lexical block. */
21076 static void
21077 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
21078 unsigned int blocknum)
21080 switch_to_section (current_function_section ());
21081 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
21084 /* Output a marker (i.e. a label) for the end of the generated code for a
21085 lexical block. */
21087 static void
21088 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
21090 switch_to_section (current_function_section ());
21091 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
21094 /* Returns nonzero if it is appropriate not to emit any debugging
21095 information for BLOCK, because it doesn't contain any instructions.
21097 Don't allow this for blocks with nested functions or local classes
21098 as we would end up with orphans, and in the presence of scheduling
21099 we may end up calling them anyway. */
21101 static bool
21102 dwarf2out_ignore_block (const_tree block)
21104 tree decl;
21105 unsigned int i;
21107 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
21108 if (TREE_CODE (decl) == FUNCTION_DECL
21109 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21110 return 0;
21111 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
21113 decl = BLOCK_NONLOCALIZED_VAR (block, i);
21114 if (TREE_CODE (decl) == FUNCTION_DECL
21115 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21116 return 0;
21119 return 1;
21122 /* Hash table routines for file_hash. */
21124 static int
21125 file_table_eq (const void *p1_p, const void *p2_p)
21127 const struct dwarf_file_data *const p1 =
21128 (const struct dwarf_file_data *) p1_p;
21129 const char *const p2 = (const char *) p2_p;
21130 return filename_cmp (p1->filename, p2) == 0;
21133 static hashval_t
21134 file_table_hash (const void *p_p)
21136 const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
21137 return htab_hash_string (p->filename);
21140 /* Lookup FILE_NAME (in the list of filenames that we know about here in
21141 dwarf2out.c) and return its "index". The index of each (known) filename is
21142 just a unique number which is associated with only that one filename. We
21143 need such numbers for the sake of generating labels (in the .debug_sfnames
21144 section) and references to those files numbers (in the .debug_srcinfo
21145 and.debug_macinfo sections). If the filename given as an argument is not
21146 found in our current list, add it to the list and assign it the next
21147 available unique index number. In order to speed up searches, we remember
21148 the index of the filename was looked up last. This handles the majority of
21149 all searches. */
21151 static struct dwarf_file_data *
21152 lookup_filename (const char *file_name)
21154 void ** slot;
21155 struct dwarf_file_data * created;
21157 /* Check to see if the file name that was searched on the previous
21158 call matches this file name. If so, return the index. */
21159 if (file_table_last_lookup
21160 && (file_name == file_table_last_lookup->filename
21161 || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
21162 return file_table_last_lookup;
21164 /* Didn't match the previous lookup, search the table. */
21165 slot = htab_find_slot_with_hash (file_table, file_name,
21166 htab_hash_string (file_name), INSERT);
21167 if (*slot)
21168 return (struct dwarf_file_data *) *slot;
21170 created = ggc_alloc<dwarf_file_data> ();
21171 created->filename = file_name;
21172 created->emitted_number = 0;
21173 *slot = created;
21174 return created;
21177 /* If the assembler will construct the file table, then translate the compiler
21178 internal file table number into the assembler file table number, and emit
21179 a .file directive if we haven't already emitted one yet. The file table
21180 numbers are different because we prune debug info for unused variables and
21181 types, which may include filenames. */
21183 static int
21184 maybe_emit_file (struct dwarf_file_data * fd)
21186 if (! fd->emitted_number)
21188 if (last_emitted_file)
21189 fd->emitted_number = last_emitted_file->emitted_number + 1;
21190 else
21191 fd->emitted_number = 1;
21192 last_emitted_file = fd;
21194 if (DWARF2_ASM_LINE_DEBUG_INFO)
21196 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
21197 output_quoted_string (asm_out_file,
21198 remap_debug_filename (fd->filename));
21199 fputc ('\n', asm_out_file);
21203 return fd->emitted_number;
21206 /* Schedule generation of a DW_AT_const_value attribute to DIE.
21207 That generation should happen after function debug info has been
21208 generated. The value of the attribute is the constant value of ARG. */
21210 static void
21211 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
21213 die_arg_entry entry;
21215 if (!die || !arg)
21216 return;
21218 if (!tmpl_value_parm_die_table)
21219 vec_alloc (tmpl_value_parm_die_table, 32);
21221 entry.die = die;
21222 entry.arg = arg;
21223 vec_safe_push (tmpl_value_parm_die_table, entry);
21226 /* Return TRUE if T is an instance of generic type, FALSE
21227 otherwise. */
21229 static bool
21230 generic_type_p (tree t)
21232 if (t == NULL_TREE || !TYPE_P (t))
21233 return false;
21234 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
21237 /* Schedule the generation of the generic parameter dies for the
21238 instance of generic type T. The proper generation itself is later
21239 done by gen_scheduled_generic_parms_dies. */
21241 static void
21242 schedule_generic_params_dies_gen (tree t)
21244 if (!generic_type_p (t))
21245 return;
21247 if (!generic_type_instances)
21248 vec_alloc (generic_type_instances, 256);
21250 vec_safe_push (generic_type_instances, t);
21253 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
21254 by append_entry_to_tmpl_value_parm_die_table. This function must
21255 be called after function DIEs have been generated. */
21257 static void
21258 gen_remaining_tmpl_value_param_die_attribute (void)
21260 if (tmpl_value_parm_die_table)
21262 unsigned i;
21263 die_arg_entry *e;
21265 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
21266 tree_add_const_value_attribute (e->die, e->arg);
21270 /* Generate generic parameters DIEs for instances of generic types
21271 that have been previously scheduled by
21272 schedule_generic_params_dies_gen. This function must be called
21273 after all the types of the CU have been laid out. */
21275 static void
21276 gen_scheduled_generic_parms_dies (void)
21278 unsigned i;
21279 tree t;
21281 if (!generic_type_instances)
21282 return;
21284 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
21285 if (COMPLETE_TYPE_P (t))
21286 gen_generic_params_dies (t);
21290 /* Replace DW_AT_name for the decl with name. */
21292 static void
21293 dwarf2out_set_name (tree decl, tree name)
21295 dw_die_ref die;
21296 dw_attr_ref attr;
21297 const char *dname;
21299 die = TYPE_SYMTAB_DIE (decl);
21300 if (!die)
21301 return;
21303 dname = dwarf2_name (name, 0);
21304 if (!dname)
21305 return;
21307 attr = get_AT (die, DW_AT_name);
21308 if (attr)
21310 struct indirect_string_node *node;
21312 node = find_AT_string (dname);
21313 /* replace the string. */
21314 attr->dw_attr_val.v.val_str = node;
21317 else
21318 add_name_attribute (die, dname);
21321 /* True if before or during processing of the first function being emitted. */
21322 static bool in_first_function_p = true;
21323 /* True if loc_note during dwarf2out_var_location call might still be
21324 before first real instruction at address equal to .Ltext0. */
21325 static bool maybe_at_text_label_p = true;
21326 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
21327 static unsigned int first_loclabel_num_not_at_text_label;
21329 /* Called by the final INSN scan whenever we see a var location. We
21330 use it to drop labels in the right places, and throw the location in
21331 our lookup table. */
21333 static void
21334 dwarf2out_var_location (rtx_insn *loc_note)
21336 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
21337 struct var_loc_node *newloc;
21338 rtx_insn *next_real, *next_note;
21339 static const char *last_label;
21340 static const char *last_postcall_label;
21341 static bool last_in_cold_section_p;
21342 static rtx_insn *expected_next_loc_note;
21343 tree decl;
21344 bool var_loc_p;
21346 if (!NOTE_P (loc_note))
21348 if (CALL_P (loc_note))
21350 call_site_count++;
21351 if (SIBLING_CALL_P (loc_note))
21352 tail_call_site_count++;
21354 return;
21357 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
21358 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
21359 return;
21361 /* Optimize processing a large consecutive sequence of location
21362 notes so we don't spend too much time in next_real_insn. If the
21363 next insn is another location note, remember the next_real_insn
21364 calculation for next time. */
21365 next_real = cached_next_real_insn;
21366 if (next_real)
21368 if (expected_next_loc_note != loc_note)
21369 next_real = NULL;
21372 next_note = NEXT_INSN (loc_note);
21373 if (! next_note
21374 || next_note->deleted ()
21375 || ! NOTE_P (next_note)
21376 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
21377 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
21378 next_note = NULL;
21380 if (! next_real)
21381 next_real = next_real_insn (loc_note);
21383 if (next_note)
21385 expected_next_loc_note = next_note;
21386 cached_next_real_insn = next_real;
21388 else
21389 cached_next_real_insn = NULL;
21391 /* If there are no instructions which would be affected by this note,
21392 don't do anything. */
21393 if (var_loc_p
21394 && next_real == NULL_RTX
21395 && !NOTE_DURING_CALL_P (loc_note))
21396 return;
21398 if (next_real == NULL_RTX)
21399 next_real = get_last_insn ();
21401 /* If there were any real insns between note we processed last time
21402 and this note (or if it is the first note), clear
21403 last_{,postcall_}label so that they are not reused this time. */
21404 if (last_var_location_insn == NULL_RTX
21405 || last_var_location_insn != next_real
21406 || last_in_cold_section_p != in_cold_section_p)
21408 last_label = NULL;
21409 last_postcall_label = NULL;
21412 if (var_loc_p)
21414 decl = NOTE_VAR_LOCATION_DECL (loc_note);
21415 newloc = add_var_loc_to_decl (decl, loc_note,
21416 NOTE_DURING_CALL_P (loc_note)
21417 ? last_postcall_label : last_label);
21418 if (newloc == NULL)
21419 return;
21421 else
21423 decl = NULL_TREE;
21424 newloc = NULL;
21427 /* If there were no real insns between note we processed last time
21428 and this note, use the label we emitted last time. Otherwise
21429 create a new label and emit it. */
21430 if (last_label == NULL)
21432 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
21433 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
21434 loclabel_num++;
21435 last_label = ggc_strdup (loclabel);
21436 /* See if loclabel might be equal to .Ltext0. If yes,
21437 bump first_loclabel_num_not_at_text_label. */
21438 if (!have_multiple_function_sections
21439 && in_first_function_p
21440 && maybe_at_text_label_p)
21442 static rtx_insn *last_start;
21443 rtx_insn *insn;
21444 for (insn = loc_note; insn; insn = previous_insn (insn))
21445 if (insn == last_start)
21446 break;
21447 else if (!NONDEBUG_INSN_P (insn))
21448 continue;
21449 else
21451 rtx body = PATTERN (insn);
21452 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
21453 continue;
21454 /* Inline asm could occupy zero bytes. */
21455 else if (GET_CODE (body) == ASM_INPUT
21456 || asm_noperands (body) >= 0)
21457 continue;
21458 #ifdef HAVE_attr_length
21459 else if (get_attr_min_length (insn) == 0)
21460 continue;
21461 #endif
21462 else
21464 /* Assume insn has non-zero length. */
21465 maybe_at_text_label_p = false;
21466 break;
21469 if (maybe_at_text_label_p)
21471 last_start = loc_note;
21472 first_loclabel_num_not_at_text_label = loclabel_num;
21477 if (!var_loc_p)
21479 struct call_arg_loc_node *ca_loc
21480 = ggc_cleared_alloc<call_arg_loc_node> ();
21481 rtx_insn *prev = prev_real_insn (loc_note);
21482 rtx x;
21483 ca_loc->call_arg_loc_note = loc_note;
21484 ca_loc->next = NULL;
21485 ca_loc->label = last_label;
21486 gcc_assert (prev
21487 && (CALL_P (prev)
21488 || (NONJUMP_INSN_P (prev)
21489 && GET_CODE (PATTERN (prev)) == SEQUENCE
21490 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
21491 if (!CALL_P (prev))
21492 prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
21493 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
21494 x = get_call_rtx_from (PATTERN (prev));
21495 if (x)
21497 x = XEXP (XEXP (x, 0), 0);
21498 if (GET_CODE (x) == SYMBOL_REF
21499 && SYMBOL_REF_DECL (x)
21500 && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
21501 ca_loc->symbol_ref = x;
21503 ca_loc->block = insn_scope (prev);
21504 if (call_arg_locations)
21505 call_arg_loc_last->next = ca_loc;
21506 else
21507 call_arg_locations = ca_loc;
21508 call_arg_loc_last = ca_loc;
21510 else if (!NOTE_DURING_CALL_P (loc_note))
21511 newloc->label = last_label;
21512 else
21514 if (!last_postcall_label)
21516 sprintf (loclabel, "%s-1", last_label);
21517 last_postcall_label = ggc_strdup (loclabel);
21519 newloc->label = last_postcall_label;
21522 last_var_location_insn = next_real;
21523 last_in_cold_section_p = in_cold_section_p;
21526 /* Note in one location list that text section has changed. */
21528 static int
21529 var_location_switch_text_section_1 (void **slot, void *data ATTRIBUTE_UNUSED)
21531 var_loc_list *list = (var_loc_list *) *slot;
21532 if (list->first)
21533 list->last_before_switch
21534 = list->last->next ? list->last->next : list->last;
21535 return 1;
21538 /* Note in all location lists that text section has changed. */
21540 static void
21541 var_location_switch_text_section (void)
21543 if (decl_loc_table == NULL)
21544 return;
21546 htab_traverse (decl_loc_table, var_location_switch_text_section_1, NULL);
21549 /* Create a new line number table. */
21551 static dw_line_info_table *
21552 new_line_info_table (void)
21554 dw_line_info_table *table;
21556 table = ggc_cleared_alloc<dw_line_info_table_struct> ();
21557 table->file_num = 1;
21558 table->line_num = 1;
21559 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
21561 return table;
21564 /* Lookup the "current" table into which we emit line info, so
21565 that we don't have to do it for every source line. */
21567 static void
21568 set_cur_line_info_table (section *sec)
21570 dw_line_info_table *table;
21572 if (sec == text_section)
21573 table = text_section_line_info;
21574 else if (sec == cold_text_section)
21576 table = cold_text_section_line_info;
21577 if (!table)
21579 cold_text_section_line_info = table = new_line_info_table ();
21580 table->end_label = cold_end_label;
21583 else
21585 const char *end_label;
21587 if (flag_reorder_blocks_and_partition)
21589 if (in_cold_section_p)
21590 end_label = crtl->subsections.cold_section_end_label;
21591 else
21592 end_label = crtl->subsections.hot_section_end_label;
21594 else
21596 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21597 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
21598 current_function_funcdef_no);
21599 end_label = ggc_strdup (label);
21602 table = new_line_info_table ();
21603 table->end_label = end_label;
21605 vec_safe_push (separate_line_info, table);
21608 if (DWARF2_ASM_LINE_DEBUG_INFO)
21609 table->is_stmt = (cur_line_info_table
21610 ? cur_line_info_table->is_stmt
21611 : DWARF_LINE_DEFAULT_IS_STMT_START);
21612 cur_line_info_table = table;
21616 /* We need to reset the locations at the beginning of each
21617 function. We can't do this in the end_function hook, because the
21618 declarations that use the locations won't have been output when
21619 that hook is called. Also compute have_multiple_function_sections here. */
21621 static void
21622 dwarf2out_begin_function (tree fun)
21624 section *sec = function_section (fun);
21626 if (sec != text_section)
21627 have_multiple_function_sections = true;
21629 if (flag_reorder_blocks_and_partition && !cold_text_section)
21631 gcc_assert (current_function_decl == fun);
21632 cold_text_section = unlikely_text_section ();
21633 switch_to_section (cold_text_section);
21634 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
21635 switch_to_section (sec);
21638 dwarf2out_note_section_used ();
21639 call_site_count = 0;
21640 tail_call_site_count = 0;
21642 set_cur_line_info_table (sec);
21645 /* Helper function of dwarf2out_end_function, called only after emitting
21646 the very first function into assembly. Check if some .debug_loc range
21647 might end with a .LVL* label that could be equal to .Ltext0.
21648 In that case we must force using absolute addresses in .debug_loc ranges,
21649 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
21650 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
21651 list terminator.
21652 Set have_multiple_function_sections to true in that case and
21653 terminate htab traversal. */
21655 static int
21656 find_empty_loc_ranges_at_text_label (void **slot, void *)
21658 var_loc_list *entry;
21659 struct var_loc_node *node;
21661 entry = (var_loc_list *) *slot;
21662 node = entry->first;
21663 if (node && node->next && node->next->label)
21665 unsigned int i;
21666 const char *label = node->next->label;
21667 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
21669 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
21671 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
21672 if (strcmp (label, loclabel) == 0)
21674 have_multiple_function_sections = true;
21675 return 0;
21679 return 1;
21682 /* Hook called after emitting a function into assembly.
21683 This does something only for the very first function emitted. */
21685 static void
21686 dwarf2out_end_function (unsigned int)
21688 if (in_first_function_p
21689 && !have_multiple_function_sections
21690 && first_loclabel_num_not_at_text_label
21691 && decl_loc_table)
21692 htab_traverse (decl_loc_table, find_empty_loc_ranges_at_text_label,
21693 NULL);
21694 in_first_function_p = false;
21695 maybe_at_text_label_p = false;
21698 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
21700 static void
21701 push_dw_line_info_entry (dw_line_info_table *table,
21702 enum dw_line_info_opcode opcode, unsigned int val)
21704 dw_line_info_entry e;
21705 e.opcode = opcode;
21706 e.val = val;
21707 vec_safe_push (table->entries, e);
21710 /* Output a label to mark the beginning of a source code line entry
21711 and record information relating to this source line, in
21712 'line_info_table' for later output of the .debug_line section. */
21713 /* ??? The discriminator parameter ought to be unsigned. */
21715 static void
21716 dwarf2out_source_line (unsigned int line, const char *filename,
21717 int discriminator, bool is_stmt)
21719 unsigned int file_num;
21720 dw_line_info_table *table;
21722 if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
21723 return;
21725 /* The discriminator column was added in dwarf4. Simplify the below
21726 by simply removing it if we're not supposed to output it. */
21727 if (dwarf_version < 4 && dwarf_strict)
21728 discriminator = 0;
21730 table = cur_line_info_table;
21731 file_num = maybe_emit_file (lookup_filename (filename));
21733 /* ??? TODO: Elide duplicate line number entries. Traditionally,
21734 the debugger has used the second (possibly duplicate) line number
21735 at the beginning of the function to mark the end of the prologue.
21736 We could eliminate any other duplicates within the function. For
21737 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
21738 that second line number entry. */
21739 /* Recall that this end-of-prologue indication is *not* the same thing
21740 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
21741 to which the hook corresponds, follows the last insn that was
21742 emitted by gen_prologue. What we need is to precede the first insn
21743 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
21744 insn that corresponds to something the user wrote. These may be
21745 very different locations once scheduling is enabled. */
21747 if (0 && file_num == table->file_num
21748 && line == table->line_num
21749 && discriminator == table->discrim_num
21750 && is_stmt == table->is_stmt)
21751 return;
21753 switch_to_section (current_function_section ());
21755 /* If requested, emit something human-readable. */
21756 if (flag_debug_asm)
21757 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
21759 if (DWARF2_ASM_LINE_DEBUG_INFO)
21761 /* Emit the .loc directive understood by GNU as. */
21762 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
21763 file_num, line, is_stmt, discriminator */
21764 fputs ("\t.loc ", asm_out_file);
21765 fprint_ul (asm_out_file, file_num);
21766 putc (' ', asm_out_file);
21767 fprint_ul (asm_out_file, line);
21768 putc (' ', asm_out_file);
21769 putc ('0', asm_out_file);
21771 if (is_stmt != table->is_stmt)
21773 fputs (" is_stmt ", asm_out_file);
21774 putc (is_stmt ? '1' : '0', asm_out_file);
21776 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
21778 gcc_assert (discriminator > 0);
21779 fputs (" discriminator ", asm_out_file);
21780 fprint_ul (asm_out_file, (unsigned long) discriminator);
21782 putc ('\n', asm_out_file);
21784 else
21786 unsigned int label_num = ++line_info_label_num;
21788 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
21790 push_dw_line_info_entry (table, LI_set_address, label_num);
21791 if (file_num != table->file_num)
21792 push_dw_line_info_entry (table, LI_set_file, file_num);
21793 if (discriminator != table->discrim_num)
21794 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
21795 if (is_stmt != table->is_stmt)
21796 push_dw_line_info_entry (table, LI_negate_stmt, 0);
21797 push_dw_line_info_entry (table, LI_set_line, line);
21800 table->file_num = file_num;
21801 table->line_num = line;
21802 table->discrim_num = discriminator;
21803 table->is_stmt = is_stmt;
21804 table->in_use = true;
21807 /* Record the beginning of a new source file. */
21809 static void
21810 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
21812 if (flag_eliminate_dwarf2_dups)
21814 /* Record the beginning of the file for break_out_includes. */
21815 dw_die_ref bincl_die;
21817 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
21818 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
21821 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21823 macinfo_entry e;
21824 e.code = DW_MACINFO_start_file;
21825 e.lineno = lineno;
21826 e.info = ggc_strdup (filename);
21827 vec_safe_push (macinfo_table, e);
21831 /* Record the end of a source file. */
21833 static void
21834 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
21836 if (flag_eliminate_dwarf2_dups)
21837 /* Record the end of the file for break_out_includes. */
21838 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
21840 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21842 macinfo_entry e;
21843 e.code = DW_MACINFO_end_file;
21844 e.lineno = lineno;
21845 e.info = NULL;
21846 vec_safe_push (macinfo_table, e);
21850 /* Called from debug_define in toplev.c. The `buffer' parameter contains
21851 the tail part of the directive line, i.e. the part which is past the
21852 initial whitespace, #, whitespace, directive-name, whitespace part. */
21854 static void
21855 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
21856 const char *buffer ATTRIBUTE_UNUSED)
21858 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21860 macinfo_entry e;
21861 /* Insert a dummy first entry to be able to optimize the whole
21862 predefined macro block using DW_MACRO_GNU_transparent_include. */
21863 if (macinfo_table->is_empty () && lineno <= 1)
21865 e.code = 0;
21866 e.lineno = 0;
21867 e.info = NULL;
21868 vec_safe_push (macinfo_table, e);
21870 e.code = DW_MACINFO_define;
21871 e.lineno = lineno;
21872 e.info = ggc_strdup (buffer);
21873 vec_safe_push (macinfo_table, e);
21877 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
21878 the tail part of the directive line, i.e. the part which is past the
21879 initial whitespace, #, whitespace, directive-name, whitespace part. */
21881 static void
21882 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
21883 const char *buffer ATTRIBUTE_UNUSED)
21885 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21887 macinfo_entry e;
21888 /* Insert a dummy first entry to be able to optimize the whole
21889 predefined macro block using DW_MACRO_GNU_transparent_include. */
21890 if (macinfo_table->is_empty () && lineno <= 1)
21892 e.code = 0;
21893 e.lineno = 0;
21894 e.info = NULL;
21895 vec_safe_push (macinfo_table, e);
21897 e.code = DW_MACINFO_undef;
21898 e.lineno = lineno;
21899 e.info = ggc_strdup (buffer);
21900 vec_safe_push (macinfo_table, e);
21904 /* Helpers to manipulate hash table of CUs. */
21906 struct macinfo_entry_hasher : typed_noop_remove <macinfo_entry>
21908 typedef macinfo_entry value_type;
21909 typedef macinfo_entry compare_type;
21910 static inline hashval_t hash (const value_type *);
21911 static inline bool equal (const value_type *, const compare_type *);
21914 inline hashval_t
21915 macinfo_entry_hasher::hash (const value_type *entry)
21917 return htab_hash_string (entry->info);
21920 inline bool
21921 macinfo_entry_hasher::equal (const value_type *entry1,
21922 const compare_type *entry2)
21924 return !strcmp (entry1->info, entry2->info);
21927 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
21929 /* Output a single .debug_macinfo entry. */
21931 static void
21932 output_macinfo_op (macinfo_entry *ref)
21934 int file_num;
21935 size_t len;
21936 struct indirect_string_node *node;
21937 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21938 struct dwarf_file_data *fd;
21940 switch (ref->code)
21942 case DW_MACINFO_start_file:
21943 fd = lookup_filename (ref->info);
21944 file_num = maybe_emit_file (fd);
21945 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
21946 dw2_asm_output_data_uleb128 (ref->lineno,
21947 "Included from line number %lu",
21948 (unsigned long) ref->lineno);
21949 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
21950 break;
21951 case DW_MACINFO_end_file:
21952 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
21953 break;
21954 case DW_MACINFO_define:
21955 case DW_MACINFO_undef:
21956 len = strlen (ref->info) + 1;
21957 if (!dwarf_strict
21958 && len > DWARF_OFFSET_SIZE
21959 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
21960 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
21962 ref->code = ref->code == DW_MACINFO_define
21963 ? DW_MACRO_GNU_define_indirect
21964 : DW_MACRO_GNU_undef_indirect;
21965 output_macinfo_op (ref);
21966 return;
21968 dw2_asm_output_data (1, ref->code,
21969 ref->code == DW_MACINFO_define
21970 ? "Define macro" : "Undefine macro");
21971 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
21972 (unsigned long) ref->lineno);
21973 dw2_asm_output_nstring (ref->info, -1, "The macro");
21974 break;
21975 case DW_MACRO_GNU_define_indirect:
21976 case DW_MACRO_GNU_undef_indirect:
21977 node = find_AT_string (ref->info);
21978 gcc_assert (node
21979 && ((node->form == DW_FORM_strp)
21980 || (node->form == DW_FORM_GNU_str_index)));
21981 dw2_asm_output_data (1, ref->code,
21982 ref->code == DW_MACRO_GNU_define_indirect
21983 ? "Define macro indirect"
21984 : "Undefine macro indirect");
21985 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
21986 (unsigned long) ref->lineno);
21987 if (node->form == DW_FORM_strp)
21988 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
21989 debug_str_section, "The macro: \"%s\"",
21990 ref->info);
21991 else
21992 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
21993 ref->info);
21994 break;
21995 case DW_MACRO_GNU_transparent_include:
21996 dw2_asm_output_data (1, ref->code, "Transparent include");
21997 ASM_GENERATE_INTERNAL_LABEL (label,
21998 DEBUG_MACRO_SECTION_LABEL, ref->lineno);
21999 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
22000 break;
22001 default:
22002 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
22003 ASM_COMMENT_START, (unsigned long) ref->code);
22004 break;
22008 /* Attempt to make a sequence of define/undef macinfo ops shareable with
22009 other compilation unit .debug_macinfo sections. IDX is the first
22010 index of a define/undef, return the number of ops that should be
22011 emitted in a comdat .debug_macinfo section and emit
22012 a DW_MACRO_GNU_transparent_include entry referencing it.
22013 If the define/undef entry should be emitted normally, return 0. */
22015 static unsigned
22016 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
22017 macinfo_hash_type **macinfo_htab)
22019 macinfo_entry *first, *second, *cur, *inc;
22020 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
22021 unsigned char checksum[16];
22022 struct md5_ctx ctx;
22023 char *grp_name, *tail;
22024 const char *base;
22025 unsigned int i, count, encoded_filename_len, linebuf_len;
22026 macinfo_entry **slot;
22028 first = &(*macinfo_table)[idx];
22029 second = &(*macinfo_table)[idx + 1];
22031 /* Optimize only if there are at least two consecutive define/undef ops,
22032 and either all of them are before first DW_MACINFO_start_file
22033 with lineno {0,1} (i.e. predefined macro block), or all of them are
22034 in some included header file. */
22035 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
22036 return 0;
22037 if (vec_safe_is_empty (files))
22039 if (first->lineno > 1 || second->lineno > 1)
22040 return 0;
22042 else if (first->lineno == 0)
22043 return 0;
22045 /* Find the last define/undef entry that can be grouped together
22046 with first and at the same time compute md5 checksum of their
22047 codes, linenumbers and strings. */
22048 md5_init_ctx (&ctx);
22049 for (i = idx; macinfo_table->iterate (i, &cur); i++)
22050 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
22051 break;
22052 else if (vec_safe_is_empty (files) && cur->lineno > 1)
22053 break;
22054 else
22056 unsigned char code = cur->code;
22057 md5_process_bytes (&code, 1, &ctx);
22058 checksum_uleb128 (cur->lineno, &ctx);
22059 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
22061 md5_finish_ctx (&ctx, checksum);
22062 count = i - idx;
22064 /* From the containing include filename (if any) pick up just
22065 usable characters from its basename. */
22066 if (vec_safe_is_empty (files))
22067 base = "";
22068 else
22069 base = lbasename (files->last ().info);
22070 for (encoded_filename_len = 0, i = 0; base[i]; i++)
22071 if (ISIDNUM (base[i]) || base[i] == '.')
22072 encoded_filename_len++;
22073 /* Count . at the end. */
22074 if (encoded_filename_len)
22075 encoded_filename_len++;
22077 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
22078 linebuf_len = strlen (linebuf);
22080 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
22081 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
22082 + 16 * 2 + 1);
22083 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
22084 tail = grp_name + 4;
22085 if (encoded_filename_len)
22087 for (i = 0; base[i]; i++)
22088 if (ISIDNUM (base[i]) || base[i] == '.')
22089 *tail++ = base[i];
22090 *tail++ = '.';
22092 memcpy (tail, linebuf, linebuf_len);
22093 tail += linebuf_len;
22094 *tail++ = '.';
22095 for (i = 0; i < 16; i++)
22096 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
22098 /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
22099 in the empty vector entry before the first define/undef. */
22100 inc = &(*macinfo_table)[idx - 1];
22101 inc->code = DW_MACRO_GNU_transparent_include;
22102 inc->lineno = 0;
22103 inc->info = ggc_strdup (grp_name);
22104 if (!*macinfo_htab)
22105 *macinfo_htab = new macinfo_hash_type (10);
22106 /* Avoid emitting duplicates. */
22107 slot = (*macinfo_htab)->find_slot (inc, INSERT);
22108 if (*slot != NULL)
22110 inc->code = 0;
22111 inc->info = NULL;
22112 /* If such an entry has been used before, just emit
22113 a DW_MACRO_GNU_transparent_include op. */
22114 inc = *slot;
22115 output_macinfo_op (inc);
22116 /* And clear all macinfo_entry in the range to avoid emitting them
22117 in the second pass. */
22118 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
22120 cur->code = 0;
22121 cur->info = NULL;
22124 else
22126 *slot = inc;
22127 inc->lineno = (*macinfo_htab)->elements ();
22128 output_macinfo_op (inc);
22130 return count;
22133 /* Save any strings needed by the macinfo table in the debug str
22134 table. All strings must be collected into the table by the time
22135 index_string is called. */
22137 static void
22138 save_macinfo_strings (void)
22140 unsigned len;
22141 unsigned i;
22142 macinfo_entry *ref;
22144 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
22146 switch (ref->code)
22148 /* Match the logic in output_macinfo_op to decide on
22149 indirect strings. */
22150 case DW_MACINFO_define:
22151 case DW_MACINFO_undef:
22152 len = strlen (ref->info) + 1;
22153 if (!dwarf_strict
22154 && len > DWARF_OFFSET_SIZE
22155 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
22156 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
22157 set_indirect_string (find_AT_string (ref->info));
22158 break;
22159 case DW_MACRO_GNU_define_indirect:
22160 case DW_MACRO_GNU_undef_indirect:
22161 set_indirect_string (find_AT_string (ref->info));
22162 break;
22163 default:
22164 break;
22169 /* Output macinfo section(s). */
22171 static void
22172 output_macinfo (void)
22174 unsigned i;
22175 unsigned long length = vec_safe_length (macinfo_table);
22176 macinfo_entry *ref;
22177 vec<macinfo_entry, va_gc> *files = NULL;
22178 macinfo_hash_type *macinfo_htab = NULL;
22180 if (! length)
22181 return;
22183 /* output_macinfo* uses these interchangeably. */
22184 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
22185 && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
22186 && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
22187 && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
22189 /* For .debug_macro emit the section header. */
22190 if (!dwarf_strict)
22192 dw2_asm_output_data (2, 4, "DWARF macro version number");
22193 if (DWARF_OFFSET_SIZE == 8)
22194 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
22195 else
22196 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
22197 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
22198 (!dwarf_split_debug_info ? debug_line_section_label
22199 : debug_skeleton_line_section_label),
22200 debug_line_section, NULL);
22203 /* In the first loop, it emits the primary .debug_macinfo section
22204 and after each emitted op the macinfo_entry is cleared.
22205 If a longer range of define/undef ops can be optimized using
22206 DW_MACRO_GNU_transparent_include, the
22207 DW_MACRO_GNU_transparent_include op is emitted and kept in
22208 the vector before the first define/undef in the range and the
22209 whole range of define/undef ops is not emitted and kept. */
22210 for (i = 0; macinfo_table->iterate (i, &ref); i++)
22212 switch (ref->code)
22214 case DW_MACINFO_start_file:
22215 vec_safe_push (files, *ref);
22216 break;
22217 case DW_MACINFO_end_file:
22218 if (!vec_safe_is_empty (files))
22219 files->pop ();
22220 break;
22221 case DW_MACINFO_define:
22222 case DW_MACINFO_undef:
22223 if (!dwarf_strict
22224 && HAVE_COMDAT_GROUP
22225 && vec_safe_length (files) != 1
22226 && i > 0
22227 && i + 1 < length
22228 && (*macinfo_table)[i - 1].code == 0)
22230 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
22231 if (count)
22233 i += count - 1;
22234 continue;
22237 break;
22238 case 0:
22239 /* A dummy entry may be inserted at the beginning to be able
22240 to optimize the whole block of predefined macros. */
22241 if (i == 0)
22242 continue;
22243 default:
22244 break;
22246 output_macinfo_op (ref);
22247 ref->info = NULL;
22248 ref->code = 0;
22251 if (!macinfo_htab)
22252 return;
22254 delete macinfo_htab;
22255 macinfo_htab = NULL;
22257 /* If any DW_MACRO_GNU_transparent_include were used, on those
22258 DW_MACRO_GNU_transparent_include entries terminate the
22259 current chain and switch to a new comdat .debug_macinfo
22260 section and emit the define/undef entries within it. */
22261 for (i = 0; macinfo_table->iterate (i, &ref); i++)
22262 switch (ref->code)
22264 case 0:
22265 continue;
22266 case DW_MACRO_GNU_transparent_include:
22268 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22269 tree comdat_key = get_identifier (ref->info);
22270 /* Terminate the previous .debug_macinfo section. */
22271 dw2_asm_output_data (1, 0, "End compilation unit");
22272 targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
22273 SECTION_DEBUG
22274 | SECTION_LINKONCE,
22275 comdat_key);
22276 ASM_GENERATE_INTERNAL_LABEL (label,
22277 DEBUG_MACRO_SECTION_LABEL,
22278 ref->lineno);
22279 ASM_OUTPUT_LABEL (asm_out_file, label);
22280 ref->code = 0;
22281 ref->info = NULL;
22282 dw2_asm_output_data (2, 4, "DWARF macro version number");
22283 if (DWARF_OFFSET_SIZE == 8)
22284 dw2_asm_output_data (1, 1, "Flags: 64-bit");
22285 else
22286 dw2_asm_output_data (1, 0, "Flags: 32-bit");
22288 break;
22289 case DW_MACINFO_define:
22290 case DW_MACINFO_undef:
22291 output_macinfo_op (ref);
22292 ref->code = 0;
22293 ref->info = NULL;
22294 break;
22295 default:
22296 gcc_unreachable ();
22300 /* Set up for Dwarf output at the start of compilation. */
22302 static void
22303 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
22305 /* Allocate the file_table. */
22306 file_table = htab_create_ggc (50, file_table_hash,
22307 file_table_eq, NULL);
22309 /* Allocate the decl_die_table. */
22310 decl_die_table = htab_create_ggc (10, decl_die_table_hash,
22311 decl_die_table_eq, NULL);
22313 /* Allocate the decl_loc_table. */
22314 decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
22315 decl_loc_table_eq, NULL);
22317 /* Allocate the cached_dw_loc_list_table. */
22318 cached_dw_loc_list_table
22319 = htab_create_ggc (10, cached_dw_loc_list_table_hash,
22320 cached_dw_loc_list_table_eq, NULL);
22322 /* Allocate the initial hunk of the decl_scope_table. */
22323 vec_alloc (decl_scope_table, 256);
22325 /* Allocate the initial hunk of the abbrev_die_table. */
22326 abbrev_die_table = ggc_cleared_vec_alloc<dw_die_ref>
22327 (ABBREV_DIE_TABLE_INCREMENT);
22328 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
22329 /* Zero-th entry is allocated, but unused. */
22330 abbrev_die_table_in_use = 1;
22332 /* Allocate the pubtypes and pubnames vectors. */
22333 vec_alloc (pubname_table, 32);
22334 vec_alloc (pubtype_table, 32);
22336 vec_alloc (incomplete_types, 64);
22338 vec_alloc (used_rtx_array, 32);
22340 if (!dwarf_split_debug_info)
22342 debug_info_section = get_section (DEBUG_INFO_SECTION,
22343 SECTION_DEBUG, NULL);
22344 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22345 SECTION_DEBUG, NULL);
22346 debug_loc_section = get_section (DEBUG_LOC_SECTION,
22347 SECTION_DEBUG, NULL);
22349 else
22351 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
22352 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22353 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
22354 SECTION_DEBUG | SECTION_EXCLUDE,
22355 NULL);
22356 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
22357 SECTION_DEBUG, NULL);
22358 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
22359 SECTION_DEBUG, NULL);
22360 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22361 SECTION_DEBUG, NULL);
22362 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
22363 DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
22365 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
22366 the main .o, but the skeleton_line goes into the split off dwo. */
22367 debug_skeleton_line_section
22368 = get_section (DEBUG_DWO_LINE_SECTION,
22369 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22370 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
22371 DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
22372 debug_str_offsets_section = get_section (DEBUG_STR_OFFSETS_SECTION,
22373 SECTION_DEBUG | SECTION_EXCLUDE,
22374 NULL);
22375 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
22376 DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
22377 debug_loc_section = get_section (DEBUG_DWO_LOC_SECTION,
22378 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22379 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
22380 DEBUG_STR_DWO_SECTION_FLAGS, NULL);
22382 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
22383 SECTION_DEBUG, NULL);
22384 debug_macinfo_section = get_section (dwarf_strict
22385 ? DEBUG_MACINFO_SECTION
22386 : DEBUG_MACRO_SECTION,
22387 DEBUG_MACRO_SECTION_FLAGS, NULL);
22388 debug_line_section = get_section (DEBUG_LINE_SECTION,
22389 SECTION_DEBUG, NULL);
22390 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
22391 SECTION_DEBUG, NULL);
22392 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
22393 SECTION_DEBUG, NULL);
22394 debug_str_section = get_section (DEBUG_STR_SECTION,
22395 DEBUG_STR_SECTION_FLAGS, NULL);
22396 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
22397 SECTION_DEBUG, NULL);
22398 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
22399 SECTION_DEBUG, NULL);
22401 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
22402 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
22403 DEBUG_ABBREV_SECTION_LABEL, 0);
22404 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
22405 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
22406 COLD_TEXT_SECTION_LABEL, 0);
22407 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
22409 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
22410 DEBUG_INFO_SECTION_LABEL, 0);
22411 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
22412 DEBUG_LINE_SECTION_LABEL, 0);
22413 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
22414 DEBUG_RANGES_SECTION_LABEL, 0);
22415 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
22416 DEBUG_ADDR_SECTION_LABEL, 0);
22417 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
22418 dwarf_strict
22419 ? DEBUG_MACINFO_SECTION_LABEL
22420 : DEBUG_MACRO_SECTION_LABEL, 0);
22421 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
22423 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22424 vec_alloc (macinfo_table, 64);
22426 switch_to_section (text_section);
22427 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
22429 /* Make sure the line number table for .text always exists. */
22430 text_section_line_info = new_line_info_table ();
22431 text_section_line_info->end_label = text_end_label;
22434 /* Called before compile () starts outputtting functions, variables
22435 and toplevel asms into assembly. */
22437 static void
22438 dwarf2out_assembly_start (void)
22440 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
22441 && dwarf2out_do_cfi_asm ()
22442 && (!(flag_unwind_tables || flag_exceptions)
22443 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
22444 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
22447 /* A helper function for dwarf2out_finish called through
22448 htab_traverse. Assign a string its index. All strings must be
22449 collected into the table by the time index_string is called,
22450 because the indexing code relies on htab_traverse to traverse nodes
22451 in the same order for each run. */
22453 static int
22454 index_string (void **h, void *v)
22456 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22457 unsigned int *index = (unsigned int *) v;
22459 find_string_form (node);
22460 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22462 gcc_assert (node->index == NO_INDEX_ASSIGNED);
22463 node->index = *index;
22464 *index += 1;
22466 return 1;
22469 /* A helper function for output_indirect_strings called through
22470 htab_traverse. Output the offset to a string and update the
22471 current offset. */
22473 static int
22474 output_index_string_offset (void **h, void *v)
22476 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22477 unsigned int *offset = (unsigned int *) v;
22479 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22481 /* Assert that this node has been assigned an index. */
22482 gcc_assert (node->index != NO_INDEX_ASSIGNED
22483 && node->index != NOT_INDEXED);
22484 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
22485 "indexed string 0x%x: %s", node->index, node->str);
22486 *offset += strlen (node->str) + 1;
22488 return 1;
22491 /* A helper function for dwarf2out_finish called through
22492 htab_traverse. Output the indexed string. */
22494 static int
22495 output_index_string (void **h, void *v)
22497 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22498 unsigned int *cur_idx = (unsigned int *) v;
22500 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22502 /* Assert that the strings are output in the same order as their
22503 indexes were assigned. */
22504 gcc_assert (*cur_idx == node->index);
22505 assemble_string (node->str, strlen (node->str) + 1);
22506 *cur_idx += 1;
22508 return 1;
22511 /* A helper function for dwarf2out_finish called through
22512 htab_traverse. Emit one queued .debug_str string. */
22514 static int
22515 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
22517 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22519 node->form = find_string_form (node);
22520 if (node->form == DW_FORM_strp && node->refcount > 0)
22522 ASM_OUTPUT_LABEL (asm_out_file, node->label);
22523 assemble_string (node->str, strlen (node->str) + 1);
22526 return 1;
22529 /* Output the indexed string table. */
22531 static void
22532 output_indirect_strings (void)
22534 switch_to_section (debug_str_section);
22535 if (!dwarf_split_debug_info)
22536 htab_traverse (debug_str_hash, output_indirect_string, NULL);
22537 else
22539 unsigned int offset = 0;
22540 unsigned int cur_idx = 0;
22542 htab_traverse (skeleton_debug_str_hash, output_indirect_string, NULL);
22544 switch_to_section (debug_str_offsets_section);
22545 htab_traverse_noresize (debug_str_hash,
22546 output_index_string_offset,
22547 &offset);
22548 switch_to_section (debug_str_dwo_section);
22549 htab_traverse_noresize (debug_str_hash,
22550 output_index_string,
22551 &cur_idx);
22555 /* Callback for htab_traverse to assign an index to an entry in the
22556 table, and to write that entry to the .debug_addr section. */
22558 static int
22559 output_addr_table_entry (void **slot, void *data)
22561 addr_table_entry *entry = (addr_table_entry *) *slot;
22562 unsigned int *cur_index = (unsigned int *)data;
22564 if (entry->refcount == 0)
22566 gcc_assert (entry->index == NO_INDEX_ASSIGNED
22567 || entry->index == NOT_INDEXED);
22568 return 1;
22571 gcc_assert (entry->index == *cur_index);
22572 (*cur_index)++;
22574 switch (entry->kind)
22576 case ate_kind_rtx:
22577 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
22578 "0x%x", entry->index);
22579 break;
22580 case ate_kind_rtx_dtprel:
22581 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
22582 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
22583 DWARF2_ADDR_SIZE,
22584 entry->addr.rtl);
22585 fputc ('\n', asm_out_file);
22586 break;
22587 case ate_kind_label:
22588 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
22589 "0x%x", entry->index);
22590 break;
22591 default:
22592 gcc_unreachable ();
22594 return 1;
22597 /* Produce the .debug_addr section. */
22599 static void
22600 output_addr_table (void)
22602 unsigned int index = 0;
22603 if (addr_index_table == NULL || htab_size (addr_index_table) == 0)
22604 return;
22606 switch_to_section (debug_addr_section);
22607 htab_traverse_noresize (addr_index_table, output_addr_table_entry, &index);
22610 #if ENABLE_ASSERT_CHECKING
22611 /* Verify that all marks are clear. */
22613 static void
22614 verify_marks_clear (dw_die_ref die)
22616 dw_die_ref c;
22618 gcc_assert (! die->die_mark);
22619 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
22621 #endif /* ENABLE_ASSERT_CHECKING */
22623 /* Clear the marks for a die and its children.
22624 Be cool if the mark isn't set. */
22626 static void
22627 prune_unmark_dies (dw_die_ref die)
22629 dw_die_ref c;
22631 if (die->die_mark)
22632 die->die_mark = 0;
22633 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
22636 /* Given DIE that we're marking as used, find any other dies
22637 it references as attributes and mark them as used. */
22639 static void
22640 prune_unused_types_walk_attribs (dw_die_ref die)
22642 dw_attr_ref a;
22643 unsigned ix;
22645 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22647 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
22649 /* A reference to another DIE.
22650 Make sure that it will get emitted.
22651 If it was broken out into a comdat group, don't follow it. */
22652 if (! AT_ref (a)->comdat_type_p
22653 || a->dw_attr == DW_AT_specification)
22654 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
22656 /* Set the string's refcount to 0 so that prune_unused_types_mark
22657 accounts properly for it. */
22658 if (AT_class (a) == dw_val_class_str)
22659 a->dw_attr_val.v.val_str->refcount = 0;
22663 /* Mark the generic parameters and arguments children DIEs of DIE. */
22665 static void
22666 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
22668 dw_die_ref c;
22670 if (die == NULL || die->die_child == NULL)
22671 return;
22672 c = die->die_child;
22675 if (is_template_parameter (c))
22676 prune_unused_types_mark (c, 1);
22677 c = c->die_sib;
22678 } while (c && c != die->die_child);
22681 /* Mark DIE as being used. If DOKIDS is true, then walk down
22682 to DIE's children. */
22684 static void
22685 prune_unused_types_mark (dw_die_ref die, int dokids)
22687 dw_die_ref c;
22689 if (die->die_mark == 0)
22691 /* We haven't done this node yet. Mark it as used. */
22692 die->die_mark = 1;
22693 /* If this is the DIE of a generic type instantiation,
22694 mark the children DIEs that describe its generic parms and
22695 args. */
22696 prune_unused_types_mark_generic_parms_dies (die);
22698 /* We also have to mark its parents as used.
22699 (But we don't want to mark our parent's kids due to this,
22700 unless it is a class.) */
22701 if (die->die_parent)
22702 prune_unused_types_mark (die->die_parent,
22703 class_scope_p (die->die_parent));
22705 /* Mark any referenced nodes. */
22706 prune_unused_types_walk_attribs (die);
22708 /* If this node is a specification,
22709 also mark the definition, if it exists. */
22710 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
22711 prune_unused_types_mark (die->die_definition, 1);
22714 if (dokids && die->die_mark != 2)
22716 /* We need to walk the children, but haven't done so yet.
22717 Remember that we've walked the kids. */
22718 die->die_mark = 2;
22720 /* If this is an array type, we need to make sure our
22721 kids get marked, even if they're types. If we're
22722 breaking out types into comdat sections, do this
22723 for all type definitions. */
22724 if (die->die_tag == DW_TAG_array_type
22725 || (use_debug_types
22726 && is_type_die (die) && ! is_declaration_die (die)))
22727 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
22728 else
22729 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22733 /* For local classes, look if any static member functions were emitted
22734 and if so, mark them. */
22736 static void
22737 prune_unused_types_walk_local_classes (dw_die_ref die)
22739 dw_die_ref c;
22741 if (die->die_mark == 2)
22742 return;
22744 switch (die->die_tag)
22746 case DW_TAG_structure_type:
22747 case DW_TAG_union_type:
22748 case DW_TAG_class_type:
22749 break;
22751 case DW_TAG_subprogram:
22752 if (!get_AT_flag (die, DW_AT_declaration)
22753 || die->die_definition != NULL)
22754 prune_unused_types_mark (die, 1);
22755 return;
22757 default:
22758 return;
22761 /* Mark children. */
22762 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
22765 /* Walk the tree DIE and mark types that we actually use. */
22767 static void
22768 prune_unused_types_walk (dw_die_ref die)
22770 dw_die_ref c;
22772 /* Don't do anything if this node is already marked and
22773 children have been marked as well. */
22774 if (die->die_mark == 2)
22775 return;
22777 switch (die->die_tag)
22779 case DW_TAG_structure_type:
22780 case DW_TAG_union_type:
22781 case DW_TAG_class_type:
22782 if (die->die_perennial_p)
22783 break;
22785 for (c = die->die_parent; c; c = c->die_parent)
22786 if (c->die_tag == DW_TAG_subprogram)
22787 break;
22789 /* Finding used static member functions inside of classes
22790 is needed just for local classes, because for other classes
22791 static member function DIEs with DW_AT_specification
22792 are emitted outside of the DW_TAG_*_type. If we ever change
22793 it, we'd need to call this even for non-local classes. */
22794 if (c)
22795 prune_unused_types_walk_local_classes (die);
22797 /* It's a type node --- don't mark it. */
22798 return;
22800 case DW_TAG_const_type:
22801 case DW_TAG_packed_type:
22802 case DW_TAG_pointer_type:
22803 case DW_TAG_reference_type:
22804 case DW_TAG_rvalue_reference_type:
22805 case DW_TAG_volatile_type:
22806 case DW_TAG_typedef:
22807 case DW_TAG_array_type:
22808 case DW_TAG_interface_type:
22809 case DW_TAG_friend:
22810 case DW_TAG_variant_part:
22811 case DW_TAG_enumeration_type:
22812 case DW_TAG_subroutine_type:
22813 case DW_TAG_string_type:
22814 case DW_TAG_set_type:
22815 case DW_TAG_subrange_type:
22816 case DW_TAG_ptr_to_member_type:
22817 case DW_TAG_file_type:
22818 if (die->die_perennial_p)
22819 break;
22821 /* It's a type node --- don't mark it. */
22822 return;
22824 default:
22825 /* Mark everything else. */
22826 break;
22829 if (die->die_mark == 0)
22831 die->die_mark = 1;
22833 /* Now, mark any dies referenced from here. */
22834 prune_unused_types_walk_attribs (die);
22837 die->die_mark = 2;
22839 /* Mark children. */
22840 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22843 /* Increment the string counts on strings referred to from DIE's
22844 attributes. */
22846 static void
22847 prune_unused_types_update_strings (dw_die_ref die)
22849 dw_attr_ref a;
22850 unsigned ix;
22852 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22853 if (AT_class (a) == dw_val_class_str)
22855 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
22856 s->refcount++;
22857 /* Avoid unnecessarily putting strings that are used less than
22858 twice in the hash table. */
22859 if (s->refcount
22860 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
22862 void ** slot;
22863 slot = htab_find_slot_with_hash (debug_str_hash, s->str,
22864 htab_hash_string (s->str),
22865 INSERT);
22866 gcc_assert (*slot == NULL);
22867 *slot = s;
22872 /* Remove from the tree DIE any dies that aren't marked. */
22874 static void
22875 prune_unused_types_prune (dw_die_ref die)
22877 dw_die_ref c;
22879 gcc_assert (die->die_mark);
22880 prune_unused_types_update_strings (die);
22882 if (! die->die_child)
22883 return;
22885 c = die->die_child;
22886 do {
22887 dw_die_ref prev = c;
22888 for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
22889 if (c == die->die_child)
22891 /* No marked children between 'prev' and the end of the list. */
22892 if (prev == c)
22893 /* No marked children at all. */
22894 die->die_child = NULL;
22895 else
22897 prev->die_sib = c->die_sib;
22898 die->die_child = prev;
22900 return;
22903 if (c != prev->die_sib)
22904 prev->die_sib = c;
22905 prune_unused_types_prune (c);
22906 } while (c != die->die_child);
22909 /* Remove dies representing declarations that we never use. */
22911 static void
22912 prune_unused_types (void)
22914 unsigned int i;
22915 limbo_die_node *node;
22916 comdat_type_node *ctnode;
22917 pubname_ref pub;
22918 dw_die_ref base_type;
22920 #if ENABLE_ASSERT_CHECKING
22921 /* All the marks should already be clear. */
22922 verify_marks_clear (comp_unit_die ());
22923 for (node = limbo_die_list; node; node = node->next)
22924 verify_marks_clear (node->die);
22925 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22926 verify_marks_clear (ctnode->root_die);
22927 #endif /* ENABLE_ASSERT_CHECKING */
22929 /* Mark types that are used in global variables. */
22930 premark_types_used_by_global_vars ();
22932 /* Set the mark on nodes that are actually used. */
22933 prune_unused_types_walk (comp_unit_die ());
22934 for (node = limbo_die_list; node; node = node->next)
22935 prune_unused_types_walk (node->die);
22936 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22938 prune_unused_types_walk (ctnode->root_die);
22939 prune_unused_types_mark (ctnode->type_die, 1);
22942 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
22943 are unusual in that they are pubnames that are the children of pubtypes.
22944 They should only be marked via their parent DW_TAG_enumeration_type die,
22945 not as roots in themselves. */
22946 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
22947 if (pub->die->die_tag != DW_TAG_enumerator)
22948 prune_unused_types_mark (pub->die, 1);
22949 for (i = 0; base_types.iterate (i, &base_type); i++)
22950 prune_unused_types_mark (base_type, 1);
22952 if (debug_str_hash)
22953 htab_empty (debug_str_hash);
22954 if (skeleton_debug_str_hash)
22955 htab_empty (skeleton_debug_str_hash);
22956 prune_unused_types_prune (comp_unit_die ());
22957 for (node = limbo_die_list; node; node = node->next)
22958 prune_unused_types_prune (node->die);
22959 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22960 prune_unused_types_prune (ctnode->root_die);
22962 /* Leave the marks clear. */
22963 prune_unmark_dies (comp_unit_die ());
22964 for (node = limbo_die_list; node; node = node->next)
22965 prune_unmark_dies (node->die);
22966 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22967 prune_unmark_dies (ctnode->root_die);
22970 /* Set the parameter to true if there are any relative pathnames in
22971 the file table. */
22972 static int
22973 file_table_relative_p (void ** slot, void *param)
22975 bool *p = (bool *) param;
22976 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
22977 if (!IS_ABSOLUTE_PATH (d->filename))
22979 *p = true;
22980 return 0;
22982 return 1;
22985 /* Helpers to manipulate hash table of comdat type units. */
22987 struct comdat_type_hasher : typed_noop_remove <comdat_type_node>
22989 typedef comdat_type_node value_type;
22990 typedef comdat_type_node compare_type;
22991 static inline hashval_t hash (const value_type *);
22992 static inline bool equal (const value_type *, const compare_type *);
22995 inline hashval_t
22996 comdat_type_hasher::hash (const value_type *type_node)
22998 hashval_t h;
22999 memcpy (&h, type_node->signature, sizeof (h));
23000 return h;
23003 inline bool
23004 comdat_type_hasher::equal (const value_type *type_node_1,
23005 const compare_type *type_node_2)
23007 return (! memcmp (type_node_1->signature, type_node_2->signature,
23008 DWARF_TYPE_SIGNATURE_SIZE));
23011 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
23012 to the location it would have been added, should we know its
23013 DECL_ASSEMBLER_NAME when we added other attributes. This will
23014 probably improve compactness of debug info, removing equivalent
23015 abbrevs, and hide any differences caused by deferring the
23016 computation of the assembler name, triggered by e.g. PCH. */
23018 static inline void
23019 move_linkage_attr (dw_die_ref die)
23021 unsigned ix = vec_safe_length (die->die_attr);
23022 dw_attr_node linkage = (*die->die_attr)[ix - 1];
23024 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
23025 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
23027 while (--ix > 0)
23029 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
23031 if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
23032 break;
23035 if (ix != vec_safe_length (die->die_attr) - 1)
23037 die->die_attr->pop ();
23038 die->die_attr->quick_insert (ix, linkage);
23042 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
23043 referenced from typed stack ops and count how often they are used. */
23045 static void
23046 mark_base_types (dw_loc_descr_ref loc)
23048 dw_die_ref base_type = NULL;
23050 for (; loc; loc = loc->dw_loc_next)
23052 switch (loc->dw_loc_opc)
23054 case DW_OP_GNU_regval_type:
23055 case DW_OP_GNU_deref_type:
23056 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
23057 break;
23058 case DW_OP_GNU_convert:
23059 case DW_OP_GNU_reinterpret:
23060 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
23061 continue;
23062 /* FALLTHRU */
23063 case DW_OP_GNU_const_type:
23064 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
23065 break;
23066 case DW_OP_GNU_entry_value:
23067 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
23068 continue;
23069 default:
23070 continue;
23072 gcc_assert (base_type->die_parent == comp_unit_die ());
23073 if (base_type->die_mark)
23074 base_type->die_mark++;
23075 else
23077 base_types.safe_push (base_type);
23078 base_type->die_mark = 1;
23083 /* Comparison function for sorting marked base types. */
23085 static int
23086 base_type_cmp (const void *x, const void *y)
23088 dw_die_ref dx = *(const dw_die_ref *) x;
23089 dw_die_ref dy = *(const dw_die_ref *) y;
23090 unsigned int byte_size1, byte_size2;
23091 unsigned int encoding1, encoding2;
23092 if (dx->die_mark > dy->die_mark)
23093 return -1;
23094 if (dx->die_mark < dy->die_mark)
23095 return 1;
23096 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
23097 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
23098 if (byte_size1 < byte_size2)
23099 return 1;
23100 if (byte_size1 > byte_size2)
23101 return -1;
23102 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
23103 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
23104 if (encoding1 < encoding2)
23105 return 1;
23106 if (encoding1 > encoding2)
23107 return -1;
23108 return 0;
23111 /* Move base types marked by mark_base_types as early as possible
23112 in the CU, sorted by decreasing usage count both to make the
23113 uleb128 references as small as possible and to make sure they
23114 will have die_offset already computed by calc_die_sizes when
23115 sizes of typed stack loc ops is computed. */
23117 static void
23118 move_marked_base_types (void)
23120 unsigned int i;
23121 dw_die_ref base_type, die, c;
23123 if (base_types.is_empty ())
23124 return;
23126 /* Sort by decreasing usage count, they will be added again in that
23127 order later on. */
23128 base_types.qsort (base_type_cmp);
23129 die = comp_unit_die ();
23130 c = die->die_child;
23133 dw_die_ref prev = c;
23134 c = c->die_sib;
23135 while (c->die_mark)
23137 remove_child_with_prev (c, prev);
23138 /* As base types got marked, there must be at least
23139 one node other than DW_TAG_base_type. */
23140 gcc_assert (c != c->die_sib);
23141 c = c->die_sib;
23144 while (c != die->die_child);
23145 gcc_assert (die->die_child);
23146 c = die->die_child;
23147 for (i = 0; base_types.iterate (i, &base_type); i++)
23149 base_type->die_mark = 0;
23150 base_type->die_sib = c->die_sib;
23151 c->die_sib = base_type;
23152 c = base_type;
23156 /* Helper function for resolve_addr, attempt to resolve
23157 one CONST_STRING, return true if successful. Similarly verify that
23158 SYMBOL_REFs refer to variables emitted in the current CU. */
23160 static bool
23161 resolve_one_addr (rtx *addr)
23163 rtx rtl = *addr;
23165 if (GET_CODE (rtl) == CONST_STRING)
23167 size_t len = strlen (XSTR (rtl, 0)) + 1;
23168 tree t = build_string (len, XSTR (rtl, 0));
23169 tree tlen = size_int (len - 1);
23170 TREE_TYPE (t)
23171 = build_array_type (char_type_node, build_index_type (tlen));
23172 rtl = lookup_constant_def (t);
23173 if (!rtl || !MEM_P (rtl))
23174 return false;
23175 rtl = XEXP (rtl, 0);
23176 if (GET_CODE (rtl) == SYMBOL_REF
23177 && SYMBOL_REF_DECL (rtl)
23178 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
23179 return false;
23180 vec_safe_push (used_rtx_array, rtl);
23181 *addr = rtl;
23182 return true;
23185 if (GET_CODE (rtl) == SYMBOL_REF
23186 && SYMBOL_REF_DECL (rtl))
23188 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
23190 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
23191 return false;
23193 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
23194 return false;
23197 if (GET_CODE (rtl) == CONST)
23199 subrtx_ptr_iterator::array_type array;
23200 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
23201 if (!resolve_one_addr (*iter))
23202 return false;
23205 return true;
23208 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
23209 if possible, and create DW_TAG_dwarf_procedure that can be referenced
23210 from DW_OP_GNU_implicit_pointer if the string hasn't been seen yet. */
23212 static rtx
23213 string_cst_pool_decl (tree t)
23215 rtx rtl = output_constant_def (t, 1);
23216 unsigned char *array;
23217 dw_loc_descr_ref l;
23218 tree decl;
23219 size_t len;
23220 dw_die_ref ref;
23222 if (!rtl || !MEM_P (rtl))
23223 return NULL_RTX;
23224 rtl = XEXP (rtl, 0);
23225 if (GET_CODE (rtl) != SYMBOL_REF
23226 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
23227 return NULL_RTX;
23229 decl = SYMBOL_REF_DECL (rtl);
23230 if (!lookup_decl_die (decl))
23232 len = TREE_STRING_LENGTH (t);
23233 vec_safe_push (used_rtx_array, rtl);
23234 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
23235 array = ggc_vec_alloc<unsigned char> (len);
23236 memcpy (array, TREE_STRING_POINTER (t), len);
23237 l = new_loc_descr (DW_OP_implicit_value, len, 0);
23238 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
23239 l->dw_loc_oprnd2.v.val_vec.length = len;
23240 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
23241 l->dw_loc_oprnd2.v.val_vec.array = array;
23242 add_AT_loc (ref, DW_AT_location, l);
23243 equate_decl_number_to_die (decl, ref);
23245 return rtl;
23248 /* Helper function of resolve_addr_in_expr. LOC is
23249 a DW_OP_addr followed by DW_OP_stack_value, either at the start
23250 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
23251 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
23252 with DW_OP_GNU_implicit_pointer if possible
23253 and return true, if unsuccessful, return false. */
23255 static bool
23256 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
23258 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
23259 HOST_WIDE_INT offset = 0;
23260 dw_die_ref ref = NULL;
23261 tree decl;
23263 if (GET_CODE (rtl) == CONST
23264 && GET_CODE (XEXP (rtl, 0)) == PLUS
23265 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
23267 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
23268 rtl = XEXP (XEXP (rtl, 0), 0);
23270 if (GET_CODE (rtl) == CONST_STRING)
23272 size_t len = strlen (XSTR (rtl, 0)) + 1;
23273 tree t = build_string (len, XSTR (rtl, 0));
23274 tree tlen = size_int (len - 1);
23276 TREE_TYPE (t)
23277 = build_array_type (char_type_node, build_index_type (tlen));
23278 rtl = string_cst_pool_decl (t);
23279 if (!rtl)
23280 return false;
23282 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
23284 decl = SYMBOL_REF_DECL (rtl);
23285 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
23287 ref = lookup_decl_die (decl);
23288 if (ref && (get_AT (ref, DW_AT_location)
23289 || get_AT (ref, DW_AT_const_value)))
23291 loc->dw_loc_opc = DW_OP_GNU_implicit_pointer;
23292 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23293 loc->dw_loc_oprnd1.val_entry = NULL;
23294 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
23295 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
23296 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
23297 loc->dw_loc_oprnd2.v.val_int = offset;
23298 return true;
23302 return false;
23305 /* Helper function for resolve_addr, handle one location
23306 expression, return false if at least one CONST_STRING or SYMBOL_REF in
23307 the location list couldn't be resolved. */
23309 static bool
23310 resolve_addr_in_expr (dw_loc_descr_ref loc)
23312 dw_loc_descr_ref keep = NULL;
23313 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
23314 switch (loc->dw_loc_opc)
23316 case DW_OP_addr:
23317 if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
23319 if ((prev == NULL
23320 || prev->dw_loc_opc == DW_OP_piece
23321 || prev->dw_loc_opc == DW_OP_bit_piece)
23322 && loc->dw_loc_next
23323 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
23324 && !dwarf_strict
23325 && optimize_one_addr_into_implicit_ptr (loc))
23326 break;
23327 return false;
23329 break;
23330 case DW_OP_GNU_addr_index:
23331 case DW_OP_GNU_const_index:
23332 if (loc->dw_loc_opc == DW_OP_GNU_addr_index
23333 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
23335 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
23336 if (!resolve_one_addr (&rtl))
23337 return false;
23338 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
23339 loc->dw_loc_oprnd1.val_entry =
23340 add_addr_table_entry (rtl, ate_kind_rtx);
23342 break;
23343 case DW_OP_const4u:
23344 case DW_OP_const8u:
23345 if (loc->dtprel
23346 && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
23347 return false;
23348 break;
23349 case DW_OP_plus_uconst:
23350 if (size_of_loc_descr (loc)
23351 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
23353 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
23355 dw_loc_descr_ref repl
23356 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
23357 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
23358 add_loc_descr (&repl, loc->dw_loc_next);
23359 *loc = *repl;
23361 break;
23362 case DW_OP_implicit_value:
23363 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
23364 && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
23365 return false;
23366 break;
23367 case DW_OP_GNU_implicit_pointer:
23368 case DW_OP_GNU_parameter_ref:
23369 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
23371 dw_die_ref ref
23372 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
23373 if (ref == NULL)
23374 return false;
23375 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23376 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
23377 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
23379 break;
23380 case DW_OP_GNU_const_type:
23381 case DW_OP_GNU_regval_type:
23382 case DW_OP_GNU_deref_type:
23383 case DW_OP_GNU_convert:
23384 case DW_OP_GNU_reinterpret:
23385 while (loc->dw_loc_next
23386 && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
23388 dw_die_ref base1, base2;
23389 unsigned enc1, enc2, size1, size2;
23390 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
23391 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
23392 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
23393 else if (loc->dw_loc_oprnd1.val_class
23394 == dw_val_class_unsigned_const)
23395 break;
23396 else
23397 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
23398 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
23399 == dw_val_class_unsigned_const)
23400 break;
23401 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
23402 gcc_assert (base1->die_tag == DW_TAG_base_type
23403 && base2->die_tag == DW_TAG_base_type);
23404 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
23405 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
23406 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
23407 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
23408 if (size1 == size2
23409 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
23410 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
23411 && loc != keep)
23412 || enc1 == enc2))
23414 /* Optimize away next DW_OP_GNU_convert after
23415 adjusting LOC's base type die reference. */
23416 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
23417 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
23418 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
23419 else
23420 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
23421 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
23422 continue;
23424 /* Don't change integer DW_OP_GNU_convert after e.g. floating
23425 point typed stack entry. */
23426 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
23427 keep = loc->dw_loc_next;
23428 break;
23430 break;
23431 default:
23432 break;
23434 return true;
23437 /* Helper function of resolve_addr. DIE had DW_AT_location of
23438 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
23439 and DW_OP_addr couldn't be resolved. resolve_addr has already
23440 removed the DW_AT_location attribute. This function attempts to
23441 add a new DW_AT_location attribute with DW_OP_GNU_implicit_pointer
23442 to it or DW_AT_const_value attribute, if possible. */
23444 static void
23445 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
23447 if (TREE_CODE (decl) != VAR_DECL
23448 || lookup_decl_die (decl) != die
23449 || DECL_EXTERNAL (decl)
23450 || !TREE_STATIC (decl)
23451 || DECL_INITIAL (decl) == NULL_TREE
23452 || DECL_P (DECL_INITIAL (decl))
23453 || get_AT (die, DW_AT_const_value))
23454 return;
23456 tree init = DECL_INITIAL (decl);
23457 HOST_WIDE_INT offset = 0;
23458 /* For variables that have been optimized away and thus
23459 don't have a memory location, see if we can emit
23460 DW_AT_const_value instead. */
23461 if (tree_add_const_value_attribute (die, init))
23462 return;
23463 if (dwarf_strict)
23464 return;
23465 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
23466 and ADDR_EXPR refers to a decl that has DW_AT_location or
23467 DW_AT_const_value (but isn't addressable, otherwise
23468 resolving the original DW_OP_addr wouldn't fail), see if
23469 we can add DW_OP_GNU_implicit_pointer. */
23470 STRIP_NOPS (init);
23471 if (TREE_CODE (init) == POINTER_PLUS_EXPR
23472 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
23474 offset = tree_to_shwi (TREE_OPERAND (init, 1));
23475 init = TREE_OPERAND (init, 0);
23476 STRIP_NOPS (init);
23478 if (TREE_CODE (init) != ADDR_EXPR)
23479 return;
23480 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
23481 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
23482 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
23483 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
23484 && TREE_OPERAND (init, 0) != decl))
23486 dw_die_ref ref;
23487 dw_loc_descr_ref l;
23489 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
23491 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
23492 if (!rtl)
23493 return;
23494 decl = SYMBOL_REF_DECL (rtl);
23496 else
23497 decl = TREE_OPERAND (init, 0);
23498 ref = lookup_decl_die (decl);
23499 if (ref == NULL
23500 || (!get_AT (ref, DW_AT_location)
23501 && !get_AT (ref, DW_AT_const_value)))
23502 return;
23503 l = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
23504 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23505 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
23506 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
23507 add_AT_loc (die, DW_AT_location, l);
23511 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
23512 an address in .rodata section if the string literal is emitted there,
23513 or remove the containing location list or replace DW_AT_const_value
23514 with DW_AT_location and empty location expression, if it isn't found
23515 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
23516 to something that has been emitted in the current CU. */
23518 static void
23519 resolve_addr (dw_die_ref die)
23521 dw_die_ref c;
23522 dw_attr_ref a;
23523 dw_loc_list_ref *curr, *start, loc;
23524 unsigned ix;
23526 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23527 switch (AT_class (a))
23529 case dw_val_class_loc_list:
23530 start = curr = AT_loc_list_ptr (a);
23531 loc = *curr;
23532 gcc_assert (loc);
23533 /* The same list can be referenced more than once. See if we have
23534 already recorded the result from a previous pass. */
23535 if (loc->replaced)
23536 *curr = loc->dw_loc_next;
23537 else if (!loc->resolved_addr)
23539 /* As things stand, we do not expect or allow one die to
23540 reference a suffix of another die's location list chain.
23541 References must be identical or completely separate.
23542 There is therefore no need to cache the result of this
23543 pass on any list other than the first; doing so
23544 would lead to unnecessary writes. */
23545 while (*curr)
23547 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
23548 if (!resolve_addr_in_expr ((*curr)->expr))
23550 dw_loc_list_ref next = (*curr)->dw_loc_next;
23551 dw_loc_descr_ref l = (*curr)->expr;
23553 if (next && (*curr)->ll_symbol)
23555 gcc_assert (!next->ll_symbol);
23556 next->ll_symbol = (*curr)->ll_symbol;
23558 if (dwarf_split_debug_info)
23559 remove_loc_list_addr_table_entries (l);
23560 *curr = next;
23562 else
23564 mark_base_types ((*curr)->expr);
23565 curr = &(*curr)->dw_loc_next;
23568 if (loc == *start)
23569 loc->resolved_addr = 1;
23570 else
23572 loc->replaced = 1;
23573 loc->dw_loc_next = *start;
23576 if (!*start)
23578 remove_AT (die, a->dw_attr);
23579 ix--;
23581 break;
23582 case dw_val_class_loc:
23584 dw_loc_descr_ref l = AT_loc (a);
23585 /* For -gdwarf-2 don't attempt to optimize
23586 DW_AT_data_member_location containing
23587 DW_OP_plus_uconst - older consumers might
23588 rely on it being that op instead of a more complex,
23589 but shorter, location description. */
23590 if ((dwarf_version > 2
23591 || a->dw_attr != DW_AT_data_member_location
23592 || l == NULL
23593 || l->dw_loc_opc != DW_OP_plus_uconst
23594 || l->dw_loc_next != NULL)
23595 && !resolve_addr_in_expr (l))
23597 if (dwarf_split_debug_info)
23598 remove_loc_list_addr_table_entries (l);
23599 if (l != NULL
23600 && l->dw_loc_next == NULL
23601 && l->dw_loc_opc == DW_OP_addr
23602 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
23603 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
23604 && a->dw_attr == DW_AT_location)
23606 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
23607 remove_AT (die, a->dw_attr);
23608 ix--;
23609 optimize_location_into_implicit_ptr (die, decl);
23610 break;
23612 remove_AT (die, a->dw_attr);
23613 ix--;
23615 else
23616 mark_base_types (l);
23618 break;
23619 case dw_val_class_addr:
23620 if (a->dw_attr == DW_AT_const_value
23621 && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
23623 if (AT_index (a) != NOT_INDEXED)
23624 remove_addr_table_entry (a->dw_attr_val.val_entry);
23625 remove_AT (die, a->dw_attr);
23626 ix--;
23628 if (die->die_tag == DW_TAG_GNU_call_site
23629 && a->dw_attr == DW_AT_abstract_origin)
23631 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
23632 dw_die_ref tdie = lookup_decl_die (tdecl);
23633 if (tdie == NULL
23634 && DECL_EXTERNAL (tdecl)
23635 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE)
23637 force_decl_die (tdecl);
23638 tdie = lookup_decl_die (tdecl);
23640 if (tdie)
23642 a->dw_attr_val.val_class = dw_val_class_die_ref;
23643 a->dw_attr_val.v.val_die_ref.die = tdie;
23644 a->dw_attr_val.v.val_die_ref.external = 0;
23646 else
23648 if (AT_index (a) != NOT_INDEXED)
23649 remove_addr_table_entry (a->dw_attr_val.val_entry);
23650 remove_AT (die, a->dw_attr);
23651 ix--;
23654 break;
23655 default:
23656 break;
23659 FOR_EACH_CHILD (die, c, resolve_addr (c));
23662 /* Helper routines for optimize_location_lists.
23663 This pass tries to share identical local lists in .debug_loc
23664 section. */
23666 /* Iteratively hash operands of LOC opcode into HSTATE. */
23668 static void
23669 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
23671 dw_val_ref val1 = &loc->dw_loc_oprnd1;
23672 dw_val_ref val2 = &loc->dw_loc_oprnd2;
23674 switch (loc->dw_loc_opc)
23676 case DW_OP_const4u:
23677 case DW_OP_const8u:
23678 if (loc->dtprel)
23679 goto hash_addr;
23680 /* FALLTHRU */
23681 case DW_OP_const1u:
23682 case DW_OP_const1s:
23683 case DW_OP_const2u:
23684 case DW_OP_const2s:
23685 case DW_OP_const4s:
23686 case DW_OP_const8s:
23687 case DW_OP_constu:
23688 case DW_OP_consts:
23689 case DW_OP_pick:
23690 case DW_OP_plus_uconst:
23691 case DW_OP_breg0:
23692 case DW_OP_breg1:
23693 case DW_OP_breg2:
23694 case DW_OP_breg3:
23695 case DW_OP_breg4:
23696 case DW_OP_breg5:
23697 case DW_OP_breg6:
23698 case DW_OP_breg7:
23699 case DW_OP_breg8:
23700 case DW_OP_breg9:
23701 case DW_OP_breg10:
23702 case DW_OP_breg11:
23703 case DW_OP_breg12:
23704 case DW_OP_breg13:
23705 case DW_OP_breg14:
23706 case DW_OP_breg15:
23707 case DW_OP_breg16:
23708 case DW_OP_breg17:
23709 case DW_OP_breg18:
23710 case DW_OP_breg19:
23711 case DW_OP_breg20:
23712 case DW_OP_breg21:
23713 case DW_OP_breg22:
23714 case DW_OP_breg23:
23715 case DW_OP_breg24:
23716 case DW_OP_breg25:
23717 case DW_OP_breg26:
23718 case DW_OP_breg27:
23719 case DW_OP_breg28:
23720 case DW_OP_breg29:
23721 case DW_OP_breg30:
23722 case DW_OP_breg31:
23723 case DW_OP_regx:
23724 case DW_OP_fbreg:
23725 case DW_OP_piece:
23726 case DW_OP_deref_size:
23727 case DW_OP_xderef_size:
23728 hstate.add_object (val1->v.val_int);
23729 break;
23730 case DW_OP_skip:
23731 case DW_OP_bra:
23733 int offset;
23735 gcc_assert (val1->val_class == dw_val_class_loc);
23736 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
23737 hstate.add_object (offset);
23739 break;
23740 case DW_OP_implicit_value:
23741 hstate.add_object (val1->v.val_unsigned);
23742 switch (val2->val_class)
23744 case dw_val_class_const:
23745 hstate.add_object (val2->v.val_int);
23746 break;
23747 case dw_val_class_vec:
23749 unsigned int elt_size = val2->v.val_vec.elt_size;
23750 unsigned int len = val2->v.val_vec.length;
23752 hstate.add_int (elt_size);
23753 hstate.add_int (len);
23754 hstate.add (val2->v.val_vec.array, len * elt_size);
23756 break;
23757 case dw_val_class_const_double:
23758 hstate.add_object (val2->v.val_double.low);
23759 hstate.add_object (val2->v.val_double.high);
23760 break;
23761 case dw_val_class_wide_int:
23762 hstate.add_object (*val2->v.val_wide);
23763 break;
23764 case dw_val_class_addr:
23765 inchash::add_rtx (val2->v.val_addr, hstate);
23766 break;
23767 default:
23768 gcc_unreachable ();
23770 break;
23771 case DW_OP_bregx:
23772 case DW_OP_bit_piece:
23773 hstate.add_object (val1->v.val_int);
23774 hstate.add_object (val2->v.val_int);
23775 break;
23776 case DW_OP_addr:
23777 hash_addr:
23778 if (loc->dtprel)
23780 unsigned char dtprel = 0xd1;
23781 hstate.add_object (dtprel);
23783 inchash::add_rtx (val1->v.val_addr, hstate);
23784 break;
23785 case DW_OP_GNU_addr_index:
23786 case DW_OP_GNU_const_index:
23788 if (loc->dtprel)
23790 unsigned char dtprel = 0xd1;
23791 hstate.add_object (dtprel);
23793 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
23795 break;
23796 case DW_OP_GNU_implicit_pointer:
23797 hstate.add_int (val2->v.val_int);
23798 break;
23799 case DW_OP_GNU_entry_value:
23800 hstate.add_object (val1->v.val_loc);
23801 break;
23802 case DW_OP_GNU_regval_type:
23803 case DW_OP_GNU_deref_type:
23805 unsigned int byte_size
23806 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
23807 unsigned int encoding
23808 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
23809 hstate.add_object (val1->v.val_int);
23810 hstate.add_object (byte_size);
23811 hstate.add_object (encoding);
23813 break;
23814 case DW_OP_GNU_convert:
23815 case DW_OP_GNU_reinterpret:
23816 if (val1->val_class == dw_val_class_unsigned_const)
23818 hstate.add_object (val1->v.val_unsigned);
23819 break;
23821 /* FALLTHRU */
23822 case DW_OP_GNU_const_type:
23824 unsigned int byte_size
23825 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
23826 unsigned int encoding
23827 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
23828 hstate.add_object (byte_size);
23829 hstate.add_object (encoding);
23830 if (loc->dw_loc_opc != DW_OP_GNU_const_type)
23831 break;
23832 hstate.add_object (val2->val_class);
23833 switch (val2->val_class)
23835 case dw_val_class_const:
23836 hstate.add_object (val2->v.val_int);
23837 break;
23838 case dw_val_class_vec:
23840 unsigned int elt_size = val2->v.val_vec.elt_size;
23841 unsigned int len = val2->v.val_vec.length;
23843 hstate.add_object (elt_size);
23844 hstate.add_object (len);
23845 hstate.add (val2->v.val_vec.array, len * elt_size);
23847 break;
23848 case dw_val_class_const_double:
23849 hstate.add_object (val2->v.val_double.low);
23850 hstate.add_object (val2->v.val_double.high);
23851 break;
23852 case dw_val_class_wide_int:
23853 hstate.add_object (*val2->v.val_wide);
23854 break;
23855 default:
23856 gcc_unreachable ();
23859 break;
23861 default:
23862 /* Other codes have no operands. */
23863 break;
23867 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
23869 static inline void
23870 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
23872 dw_loc_descr_ref l;
23873 bool sizes_computed = false;
23874 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
23875 size_of_locs (loc);
23877 for (l = loc; l != NULL; l = l->dw_loc_next)
23879 enum dwarf_location_atom opc = l->dw_loc_opc;
23880 hstate.add_object (opc);
23881 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
23883 size_of_locs (loc);
23884 sizes_computed = true;
23886 hash_loc_operands (l, hstate);
23890 /* Compute hash of the whole location list LIST_HEAD. */
23892 static inline void
23893 hash_loc_list (dw_loc_list_ref list_head)
23895 dw_loc_list_ref curr = list_head;
23896 inchash::hash hstate;
23898 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
23900 hstate.add (curr->begin, strlen (curr->begin) + 1);
23901 hstate.add (curr->end, strlen (curr->end) + 1);
23902 if (curr->section)
23903 hstate.add (curr->section, strlen (curr->section) + 1);
23904 hash_locs (curr->expr, hstate);
23906 list_head->hash = hstate.end ();
23909 /* Return true if X and Y opcodes have the same operands. */
23911 static inline bool
23912 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
23914 dw_val_ref valx1 = &x->dw_loc_oprnd1;
23915 dw_val_ref valx2 = &x->dw_loc_oprnd2;
23916 dw_val_ref valy1 = &y->dw_loc_oprnd1;
23917 dw_val_ref valy2 = &y->dw_loc_oprnd2;
23919 switch (x->dw_loc_opc)
23921 case DW_OP_const4u:
23922 case DW_OP_const8u:
23923 if (x->dtprel)
23924 goto hash_addr;
23925 /* FALLTHRU */
23926 case DW_OP_const1u:
23927 case DW_OP_const1s:
23928 case DW_OP_const2u:
23929 case DW_OP_const2s:
23930 case DW_OP_const4s:
23931 case DW_OP_const8s:
23932 case DW_OP_constu:
23933 case DW_OP_consts:
23934 case DW_OP_pick:
23935 case DW_OP_plus_uconst:
23936 case DW_OP_breg0:
23937 case DW_OP_breg1:
23938 case DW_OP_breg2:
23939 case DW_OP_breg3:
23940 case DW_OP_breg4:
23941 case DW_OP_breg5:
23942 case DW_OP_breg6:
23943 case DW_OP_breg7:
23944 case DW_OP_breg8:
23945 case DW_OP_breg9:
23946 case DW_OP_breg10:
23947 case DW_OP_breg11:
23948 case DW_OP_breg12:
23949 case DW_OP_breg13:
23950 case DW_OP_breg14:
23951 case DW_OP_breg15:
23952 case DW_OP_breg16:
23953 case DW_OP_breg17:
23954 case DW_OP_breg18:
23955 case DW_OP_breg19:
23956 case DW_OP_breg20:
23957 case DW_OP_breg21:
23958 case DW_OP_breg22:
23959 case DW_OP_breg23:
23960 case DW_OP_breg24:
23961 case DW_OP_breg25:
23962 case DW_OP_breg26:
23963 case DW_OP_breg27:
23964 case DW_OP_breg28:
23965 case DW_OP_breg29:
23966 case DW_OP_breg30:
23967 case DW_OP_breg31:
23968 case DW_OP_regx:
23969 case DW_OP_fbreg:
23970 case DW_OP_piece:
23971 case DW_OP_deref_size:
23972 case DW_OP_xderef_size:
23973 return valx1->v.val_int == valy1->v.val_int;
23974 case DW_OP_skip:
23975 case DW_OP_bra:
23976 /* If splitting debug info, the use of DW_OP_GNU_addr_index
23977 can cause irrelevant differences in dw_loc_addr. */
23978 gcc_assert (valx1->val_class == dw_val_class_loc
23979 && valy1->val_class == dw_val_class_loc
23980 && (dwarf_split_debug_info
23981 || x->dw_loc_addr == y->dw_loc_addr));
23982 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
23983 case DW_OP_implicit_value:
23984 if (valx1->v.val_unsigned != valy1->v.val_unsigned
23985 || valx2->val_class != valy2->val_class)
23986 return false;
23987 switch (valx2->val_class)
23989 case dw_val_class_const:
23990 return valx2->v.val_int == valy2->v.val_int;
23991 case dw_val_class_vec:
23992 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
23993 && valx2->v.val_vec.length == valy2->v.val_vec.length
23994 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
23995 valx2->v.val_vec.elt_size
23996 * valx2->v.val_vec.length) == 0;
23997 case dw_val_class_const_double:
23998 return valx2->v.val_double.low == valy2->v.val_double.low
23999 && valx2->v.val_double.high == valy2->v.val_double.high;
24000 case dw_val_class_wide_int:
24001 return *valx2->v.val_wide == *valy2->v.val_wide;
24002 case dw_val_class_addr:
24003 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
24004 default:
24005 gcc_unreachable ();
24007 case DW_OP_bregx:
24008 case DW_OP_bit_piece:
24009 return valx1->v.val_int == valy1->v.val_int
24010 && valx2->v.val_int == valy2->v.val_int;
24011 case DW_OP_addr:
24012 hash_addr:
24013 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
24014 case DW_OP_GNU_addr_index:
24015 case DW_OP_GNU_const_index:
24017 rtx ax1 = valx1->val_entry->addr.rtl;
24018 rtx ay1 = valy1->val_entry->addr.rtl;
24019 return rtx_equal_p (ax1, ay1);
24021 case DW_OP_GNU_implicit_pointer:
24022 return valx1->val_class == dw_val_class_die_ref
24023 && valx1->val_class == valy1->val_class
24024 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
24025 && valx2->v.val_int == valy2->v.val_int;
24026 case DW_OP_GNU_entry_value:
24027 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
24028 case DW_OP_GNU_const_type:
24029 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
24030 || valx2->val_class != valy2->val_class)
24031 return false;
24032 switch (valx2->val_class)
24034 case dw_val_class_const:
24035 return valx2->v.val_int == valy2->v.val_int;
24036 case dw_val_class_vec:
24037 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
24038 && valx2->v.val_vec.length == valy2->v.val_vec.length
24039 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
24040 valx2->v.val_vec.elt_size
24041 * valx2->v.val_vec.length) == 0;
24042 case dw_val_class_const_double:
24043 return valx2->v.val_double.low == valy2->v.val_double.low
24044 && valx2->v.val_double.high == valy2->v.val_double.high;
24045 case dw_val_class_wide_int:
24046 return *valx2->v.val_wide == *valy2->v.val_wide;
24047 default:
24048 gcc_unreachable ();
24050 case DW_OP_GNU_regval_type:
24051 case DW_OP_GNU_deref_type:
24052 return valx1->v.val_int == valy1->v.val_int
24053 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
24054 case DW_OP_GNU_convert:
24055 case DW_OP_GNU_reinterpret:
24056 if (valx1->val_class != valy1->val_class)
24057 return false;
24058 if (valx1->val_class == dw_val_class_unsigned_const)
24059 return valx1->v.val_unsigned == valy1->v.val_unsigned;
24060 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
24061 case DW_OP_GNU_parameter_ref:
24062 return valx1->val_class == dw_val_class_die_ref
24063 && valx1->val_class == valy1->val_class
24064 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
24065 default:
24066 /* Other codes have no operands. */
24067 return true;
24071 /* Return true if DWARF location expressions X and Y are the same. */
24073 static inline bool
24074 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
24076 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
24077 if (x->dw_loc_opc != y->dw_loc_opc
24078 || x->dtprel != y->dtprel
24079 || !compare_loc_operands (x, y))
24080 break;
24081 return x == NULL && y == NULL;
24084 /* Hashtable helpers. */
24086 struct loc_list_hasher : typed_noop_remove <dw_loc_list_struct>
24088 typedef dw_loc_list_struct value_type;
24089 typedef dw_loc_list_struct compare_type;
24090 static inline hashval_t hash (const value_type *);
24091 static inline bool equal (const value_type *, const compare_type *);
24094 /* Return precomputed hash of location list X. */
24096 inline hashval_t
24097 loc_list_hasher::hash (const value_type *x)
24099 return x->hash;
24102 /* Return true if location lists A and B are the same. */
24104 inline bool
24105 loc_list_hasher::equal (const value_type *a, const compare_type *b)
24107 if (a == b)
24108 return 1;
24109 if (a->hash != b->hash)
24110 return 0;
24111 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
24112 if (strcmp (a->begin, b->begin) != 0
24113 || strcmp (a->end, b->end) != 0
24114 || (a->section == NULL) != (b->section == NULL)
24115 || (a->section && strcmp (a->section, b->section) != 0)
24116 || !compare_locs (a->expr, b->expr))
24117 break;
24118 return a == NULL && b == NULL;
24121 typedef hash_table<loc_list_hasher> loc_list_hash_type;
24124 /* Recursively optimize location lists referenced from DIE
24125 children and share them whenever possible. */
24127 static void
24128 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
24130 dw_die_ref c;
24131 dw_attr_ref a;
24132 unsigned ix;
24133 dw_loc_list_struct **slot;
24135 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
24136 if (AT_class (a) == dw_val_class_loc_list)
24138 dw_loc_list_ref list = AT_loc_list (a);
24139 /* TODO: perform some optimizations here, before hashing
24140 it and storing into the hash table. */
24141 hash_loc_list (list);
24142 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
24143 if (*slot == NULL)
24144 *slot = list;
24145 else
24146 a->dw_attr_val.v.val_loc_list = *slot;
24149 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
24153 /* Recursively assign each location list a unique index into the debug_addr
24154 section. */
24156 static void
24157 index_location_lists (dw_die_ref die)
24159 dw_die_ref c;
24160 dw_attr_ref a;
24161 unsigned ix;
24163 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
24164 if (AT_class (a) == dw_val_class_loc_list)
24166 dw_loc_list_ref list = AT_loc_list (a);
24167 dw_loc_list_ref curr;
24168 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
24170 /* Don't index an entry that has already been indexed
24171 or won't be output. */
24172 if (curr->begin_entry != NULL
24173 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
24174 continue;
24176 curr->begin_entry
24177 = add_addr_table_entry (xstrdup (curr->begin),
24178 ate_kind_label);
24182 FOR_EACH_CHILD (die, c, index_location_lists (c));
24185 /* Optimize location lists referenced from DIE
24186 children and share them whenever possible. */
24188 static void
24189 optimize_location_lists (dw_die_ref die)
24191 loc_list_hash_type htab (500);
24192 optimize_location_lists_1 (die, &htab);
24195 /* Output stuff that dwarf requires at the end of every file,
24196 and generate the DWARF-2 debugging info. */
24198 static void
24199 dwarf2out_finish (const char *filename)
24201 limbo_die_node *node, *next_node;
24202 comdat_type_node *ctnode;
24203 unsigned int i;
24204 dw_die_ref main_comp_unit_die;
24206 /* PCH might result in DW_AT_producer string being restored from the
24207 header compilation, so always fill it with empty string initially
24208 and overwrite only here. */
24209 dw_attr_ref producer = get_AT (comp_unit_die (), DW_AT_producer);
24210 producer_string = gen_producer_string ();
24211 producer->dw_attr_val.v.val_str->refcount--;
24212 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
24214 gen_scheduled_generic_parms_dies ();
24215 gen_remaining_tmpl_value_param_die_attribute ();
24217 /* Add the name for the main input file now. We delayed this from
24218 dwarf2out_init to avoid complications with PCH. */
24219 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
24220 if (!IS_ABSOLUTE_PATH (filename) || targetm.force_at_comp_dir)
24221 add_comp_dir_attribute (comp_unit_die ());
24222 else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
24224 bool p = false;
24225 htab_traverse (file_table, file_table_relative_p, &p);
24226 if (p)
24227 add_comp_dir_attribute (comp_unit_die ());
24230 if (deferred_locations_list)
24231 for (i = 0; i < deferred_locations_list->length (); i++)
24233 add_location_or_const_value_attribute (
24234 (*deferred_locations_list)[i].die,
24235 (*deferred_locations_list)[i].variable,
24236 false,
24237 DW_AT_location);
24240 /* Traverse the limbo die list, and add parent/child links. The only
24241 dies without parents that should be here are concrete instances of
24242 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
24243 For concrete instances, we can get the parent die from the abstract
24244 instance. */
24245 for (node = limbo_die_list; node; node = next_node)
24247 dw_die_ref die = node->die;
24248 next_node = node->next;
24250 if (die->die_parent == NULL)
24252 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
24254 if (origin && origin->die_parent)
24255 add_child_die (origin->die_parent, die);
24256 else if (is_cu_die (die))
24258 else if (seen_error ())
24259 /* It's OK to be confused by errors in the input. */
24260 add_child_die (comp_unit_die (), die);
24261 else
24263 /* In certain situations, the lexical block containing a
24264 nested function can be optimized away, which results
24265 in the nested function die being orphaned. Likewise
24266 with the return type of that nested function. Force
24267 this to be a child of the containing function.
24269 It may happen that even the containing function got fully
24270 inlined and optimized out. In that case we are lost and
24271 assign the empty child. This should not be big issue as
24272 the function is likely unreachable too. */
24273 gcc_assert (node->created_for);
24275 if (DECL_P (node->created_for))
24276 origin = get_context_die (DECL_CONTEXT (node->created_for));
24277 else if (TYPE_P (node->created_for))
24278 origin = scope_die_for (node->created_for, comp_unit_die ());
24279 else
24280 origin = comp_unit_die ();
24282 add_child_die (origin, die);
24287 limbo_die_list = NULL;
24289 #if ENABLE_ASSERT_CHECKING
24291 dw_die_ref die = comp_unit_die (), c;
24292 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
24294 #endif
24295 resolve_addr (comp_unit_die ());
24296 move_marked_base_types ();
24298 for (node = deferred_asm_name; node; node = node->next)
24300 tree decl = node->created_for;
24301 /* When generating LTO bytecode we can not generate new assembler
24302 names at this point and all important decls got theirs via
24303 free-lang-data. */
24304 if ((!flag_generate_lto || DECL_ASSEMBLER_NAME_SET_P (decl))
24305 && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
24307 add_linkage_attr (node->die, decl);
24308 move_linkage_attr (node->die);
24312 deferred_asm_name = NULL;
24314 /* Walk through the list of incomplete types again, trying once more to
24315 emit full debugging info for them. */
24316 retry_incomplete_types ();
24318 if (flag_eliminate_unused_debug_types)
24319 prune_unused_types ();
24321 /* Generate separate COMDAT sections for type DIEs. */
24322 if (use_debug_types)
24324 break_out_comdat_types (comp_unit_die ());
24326 /* Each new type_unit DIE was added to the limbo die list when created.
24327 Since these have all been added to comdat_type_list, clear the
24328 limbo die list. */
24329 limbo_die_list = NULL;
24331 /* For each new comdat type unit, copy declarations for incomplete
24332 types to make the new unit self-contained (i.e., no direct
24333 references to the main compile unit). */
24334 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24335 copy_decls_for_unworthy_types (ctnode->root_die);
24336 copy_decls_for_unworthy_types (comp_unit_die ());
24338 /* In the process of copying declarations from one unit to another,
24339 we may have left some declarations behind that are no longer
24340 referenced. Prune them. */
24341 prune_unused_types ();
24344 /* Generate separate CUs for each of the include files we've seen.
24345 They will go into limbo_die_list. */
24346 if (flag_eliminate_dwarf2_dups)
24347 break_out_includes (comp_unit_die ());
24349 /* Traverse the DIE's and add add sibling attributes to those DIE's
24350 that have children. */
24351 add_sibling_attributes (comp_unit_die ());
24352 for (node = limbo_die_list; node; node = node->next)
24353 add_sibling_attributes (node->die);
24354 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24355 add_sibling_attributes (ctnode->root_die);
24357 /* When splitting DWARF info, we put some attributes in the
24358 skeleton compile_unit DIE that remains in the .o, while
24359 most attributes go in the DWO compile_unit_die. */
24360 if (dwarf_split_debug_info)
24361 main_comp_unit_die = gen_compile_unit_die (NULL);
24362 else
24363 main_comp_unit_die = comp_unit_die ();
24365 /* Output a terminator label for the .text section. */
24366 switch_to_section (text_section);
24367 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
24368 if (cold_text_section)
24370 switch_to_section (cold_text_section);
24371 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
24374 /* We can only use the low/high_pc attributes if all of the code was
24375 in .text. */
24376 if (!have_multiple_function_sections
24377 || (dwarf_version < 3 && dwarf_strict))
24379 /* Don't add if the CU has no associated code. */
24380 if (text_section_used)
24381 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
24382 text_end_label, true);
24384 else
24386 unsigned fde_idx;
24387 dw_fde_ref fde;
24388 bool range_list_added = false;
24390 if (text_section_used)
24391 add_ranges_by_labels (main_comp_unit_die, text_section_label,
24392 text_end_label, &range_list_added, true);
24393 if (cold_text_section_used)
24394 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
24395 cold_end_label, &range_list_added, true);
24397 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
24399 if (DECL_IGNORED_P (fde->decl))
24400 continue;
24401 if (!fde->in_std_section)
24402 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
24403 fde->dw_fde_end, &range_list_added,
24404 true);
24405 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
24406 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
24407 fde->dw_fde_second_end, &range_list_added,
24408 true);
24411 if (range_list_added)
24413 /* We need to give .debug_loc and .debug_ranges an appropriate
24414 "base address". Use zero so that these addresses become
24415 absolute. Historically, we've emitted the unexpected
24416 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
24417 Emit both to give time for other tools to adapt. */
24418 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
24419 if (! dwarf_strict && dwarf_version < 4)
24420 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
24422 add_ranges (NULL);
24426 if (debug_info_level >= DINFO_LEVEL_TERSE)
24427 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
24428 debug_line_section_label);
24430 if (have_macinfo)
24431 add_AT_macptr (comp_unit_die (),
24432 dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
24433 macinfo_section_label);
24435 if (dwarf_split_debug_info)
24437 /* optimize_location_lists calculates the size of the lists,
24438 so index them first, and assign indices to the entries.
24439 Although optimize_location_lists will remove entries from
24440 the table, it only does so for duplicates, and therefore
24441 only reduces ref_counts to 1. */
24442 index_location_lists (comp_unit_die ());
24444 if (addr_index_table != NULL)
24446 unsigned int index = 0;
24447 htab_traverse_noresize (addr_index_table,
24448 index_addr_table_entry, &index);
24452 if (have_location_lists)
24453 optimize_location_lists (comp_unit_die ());
24455 save_macinfo_strings ();
24457 if (dwarf_split_debug_info)
24459 unsigned int index = 0;
24461 /* Add attributes common to skeleton compile_units and
24462 type_units. Because these attributes include strings, it
24463 must be done before freezing the string table. Top-level
24464 skeleton die attrs are added when the skeleton type unit is
24465 created, so ensure it is created by this point. */
24466 add_top_level_skeleton_die_attrs (main_comp_unit_die);
24467 htab_traverse_noresize (debug_str_hash, index_string, &index);
24470 /* Output all of the compilation units. We put the main one last so that
24471 the offsets are available to output_pubnames. */
24472 for (node = limbo_die_list; node; node = node->next)
24473 output_comp_unit (node->die, 0);
24475 hash_table<comdat_type_hasher> comdat_type_table (100);
24476 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24478 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
24480 /* Don't output duplicate types. */
24481 if (*slot != HTAB_EMPTY_ENTRY)
24482 continue;
24484 /* Add a pointer to the line table for the main compilation unit
24485 so that the debugger can make sense of DW_AT_decl_file
24486 attributes. */
24487 if (debug_info_level >= DINFO_LEVEL_TERSE)
24488 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
24489 (!dwarf_split_debug_info
24490 ? debug_line_section_label
24491 : debug_skeleton_line_section_label));
24493 output_comdat_type_unit (ctnode);
24494 *slot = ctnode;
24497 /* The AT_pubnames attribute needs to go in all skeleton dies, including
24498 both the main_cu and all skeleton TUs. Making this call unconditional
24499 would end up either adding a second copy of the AT_pubnames attribute, or
24500 requiring a special case in add_top_level_skeleton_die_attrs. */
24501 if (!dwarf_split_debug_info)
24502 add_AT_pubnames (comp_unit_die ());
24504 if (dwarf_split_debug_info)
24506 int mark;
24507 unsigned char checksum[16];
24508 struct md5_ctx ctx;
24510 /* Compute a checksum of the comp_unit to use as the dwo_id. */
24511 md5_init_ctx (&ctx);
24512 mark = 0;
24513 die_checksum (comp_unit_die (), &ctx, &mark);
24514 unmark_all_dies (comp_unit_die ());
24515 md5_finish_ctx (&ctx, checksum);
24517 /* Use the first 8 bytes of the checksum as the dwo_id,
24518 and add it to both comp-unit DIEs. */
24519 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
24520 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
24522 /* Add the base offset of the ranges table to the skeleton
24523 comp-unit DIE. */
24524 if (ranges_table_in_use)
24525 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
24526 ranges_section_label);
24528 switch_to_section (debug_addr_section);
24529 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
24530 output_addr_table ();
24533 /* Output the main compilation unit if non-empty or if .debug_macinfo
24534 or .debug_macro will be emitted. */
24535 output_comp_unit (comp_unit_die (), have_macinfo);
24537 if (dwarf_split_debug_info && info_section_emitted)
24538 output_skeleton_debug_sections (main_comp_unit_die);
24540 /* Output the abbreviation table. */
24541 if (abbrev_die_table_in_use != 1)
24543 switch_to_section (debug_abbrev_section);
24544 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
24545 output_abbrev_section ();
24548 /* Output location list section if necessary. */
24549 if (have_location_lists)
24551 /* Output the location lists info. */
24552 switch_to_section (debug_loc_section);
24553 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
24554 output_location_lists (comp_unit_die ());
24557 output_pubtables ();
24559 /* Output the address range information if a CU (.debug_info section)
24560 was emitted. We output an empty table even if we had no functions
24561 to put in it. This because the consumer has no way to tell the
24562 difference between an empty table that we omitted and failure to
24563 generate a table that would have contained data. */
24564 if (info_section_emitted)
24566 unsigned long aranges_length = size_of_aranges ();
24568 switch_to_section (debug_aranges_section);
24569 output_aranges (aranges_length);
24572 /* Output ranges section if necessary. */
24573 if (ranges_table_in_use)
24575 switch_to_section (debug_ranges_section);
24576 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
24577 output_ranges ();
24580 /* Have to end the macro section. */
24581 if (have_macinfo)
24583 switch_to_section (debug_macinfo_section);
24584 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
24585 output_macinfo ();
24586 dw2_asm_output_data (1, 0, "End compilation unit");
24589 /* Output the source line correspondence table. We must do this
24590 even if there is no line information. Otherwise, on an empty
24591 translation unit, we will generate a present, but empty,
24592 .debug_info section. IRIX 6.5 `nm' will then complain when
24593 examining the file. This is done late so that any filenames
24594 used by the debug_info section are marked as 'used'. */
24595 switch_to_section (debug_line_section);
24596 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
24597 if (! DWARF2_ASM_LINE_DEBUG_INFO)
24598 output_line_info (false);
24600 if (dwarf_split_debug_info && info_section_emitted)
24602 switch_to_section (debug_skeleton_line_section);
24603 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
24604 output_line_info (true);
24607 /* If we emitted any indirect strings, output the string table too. */
24608 if (debug_str_hash || skeleton_debug_str_hash)
24609 output_indirect_strings ();
24612 #include "gt-dwarf2out.h"