[AArch64] Wire up vqdmullh_laneq_s16 and vqdmullh_laneq_s32
[official-gcc.git] / gcc / dwarf2out.c
blobb5fcfa430992c7ac0a05179a0aa84eebc6e9ea20
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 /* Record whether the function being analyzed contains inlined functions. */
2958 static int current_function_has_inlines;
2960 /* The last file entry emitted by maybe_emit_file(). */
2961 static GTY(()) struct dwarf_file_data * last_emitted_file;
2963 /* Number of internal labels generated by gen_internal_sym(). */
2964 static GTY(()) int label_num;
2966 /* Cached result of previous call to lookup_filename. */
2967 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
2969 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
2971 /* Instances of generic types for which we need to generate debug
2972 info that describe their generic parameters and arguments. That
2973 generation needs to happen once all types are properly laid out so
2974 we do it at the end of compilation. */
2975 static GTY(()) vec<tree, va_gc> *generic_type_instances;
2977 /* Offset from the "steady-state frame pointer" to the frame base,
2978 within the current function. */
2979 static HOST_WIDE_INT frame_pointer_fb_offset;
2980 static bool frame_pointer_fb_offset_valid;
2982 static vec<dw_die_ref> base_types;
2984 /* Forward declarations for functions defined in this file. */
2986 static int is_pseudo_reg (const_rtx);
2987 static tree type_main_variant (tree);
2988 static int is_tagged_type (const_tree);
2989 static const char *dwarf_tag_name (unsigned);
2990 static const char *dwarf_attr_name (unsigned);
2991 static const char *dwarf_form_name (unsigned);
2992 static tree decl_ultimate_origin (const_tree);
2993 static tree decl_class_context (tree);
2994 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
2995 static inline enum dw_val_class AT_class (dw_attr_ref);
2996 static inline unsigned int AT_index (dw_attr_ref);
2997 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
2998 static inline unsigned AT_flag (dw_attr_ref);
2999 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3000 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
3001 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3002 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
3003 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3004 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3005 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3006 unsigned int, unsigned char *);
3007 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3008 static hashval_t debug_str_do_hash (const void *);
3009 static int debug_str_eq (const void *, const void *);
3010 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3011 static inline const char *AT_string (dw_attr_ref);
3012 static enum dwarf_form AT_string_form (dw_attr_ref);
3013 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3014 static void add_AT_specification (dw_die_ref, dw_die_ref);
3015 static inline dw_die_ref AT_ref (dw_attr_ref);
3016 static inline int AT_ref_external (dw_attr_ref);
3017 static inline void set_AT_ref_external (dw_attr_ref, int);
3018 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3019 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3020 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
3021 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3022 dw_loc_list_ref);
3023 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
3024 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3025 static void remove_addr_table_entry (addr_table_entry *);
3026 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3027 static inline rtx AT_addr (dw_attr_ref);
3028 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3029 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3030 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3031 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3032 unsigned HOST_WIDE_INT);
3033 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3034 unsigned long, bool);
3035 static inline const char *AT_lbl (dw_attr_ref);
3036 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
3037 static const char *get_AT_low_pc (dw_die_ref);
3038 static const char *get_AT_hi_pc (dw_die_ref);
3039 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3040 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3041 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3042 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3043 static bool is_cxx (void);
3044 static bool is_fortran (void);
3045 static bool is_ada (void);
3046 static void remove_AT (dw_die_ref, enum dwarf_attribute);
3047 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3048 static void add_child_die (dw_die_ref, dw_die_ref);
3049 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3050 static dw_die_ref lookup_type_die (tree);
3051 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3052 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3053 static void equate_type_number_to_die (tree, dw_die_ref);
3054 static hashval_t decl_die_table_hash (const void *);
3055 static int decl_die_table_eq (const void *, const void *);
3056 static dw_die_ref lookup_decl_die (tree);
3057 static hashval_t common_block_die_table_hash (const void *);
3058 static int common_block_die_table_eq (const void *, const void *);
3059 static hashval_t decl_loc_table_hash (const void *);
3060 static int decl_loc_table_eq (const void *, const void *);
3061 static var_loc_list *lookup_decl_loc (const_tree);
3062 static void equate_decl_number_to_die (tree, dw_die_ref);
3063 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3064 static void print_spaces (FILE *);
3065 static void print_die (dw_die_ref, FILE *);
3066 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3067 static dw_die_ref pop_compile_unit (dw_die_ref);
3068 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3069 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
3070 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3071 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3072 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3073 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3074 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
3075 struct md5_ctx *, int *);
3076 struct checksum_attributes;
3077 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3078 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3079 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3080 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3081 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3082 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3083 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
3084 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3085 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3086 static void compute_section_prefix (dw_die_ref);
3087 static int is_type_die (dw_die_ref);
3088 static int is_comdat_die (dw_die_ref);
3089 static int is_symbol_die (dw_die_ref);
3090 static inline bool is_template_instantiation (dw_die_ref);
3091 static void assign_symbol_names (dw_die_ref);
3092 static void break_out_includes (dw_die_ref);
3093 static int is_declaration_die (dw_die_ref);
3094 static int should_move_die_to_comdat (dw_die_ref);
3095 static dw_die_ref clone_as_declaration (dw_die_ref);
3096 static dw_die_ref clone_die (dw_die_ref);
3097 static dw_die_ref clone_tree (dw_die_ref);
3098 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3099 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3100 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3101 static dw_die_ref generate_skeleton (dw_die_ref);
3102 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3103 dw_die_ref,
3104 dw_die_ref);
3105 static void break_out_comdat_types (dw_die_ref);
3106 static void copy_decls_for_unworthy_types (dw_die_ref);
3108 static void add_sibling_attributes (dw_die_ref);
3109 static void output_location_lists (dw_die_ref);
3110 static int constant_size (unsigned HOST_WIDE_INT);
3111 static unsigned long size_of_die (dw_die_ref);
3112 static void calc_die_sizes (dw_die_ref);
3113 static void calc_base_type_die_sizes (void);
3114 static void mark_dies (dw_die_ref);
3115 static void unmark_dies (dw_die_ref);
3116 static void unmark_all_dies (dw_die_ref);
3117 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3118 static unsigned long size_of_aranges (void);
3119 static enum dwarf_form value_format (dw_attr_ref);
3120 static void output_value_format (dw_attr_ref);
3121 static void output_abbrev_section (void);
3122 static void output_die_abbrevs (unsigned long, dw_die_ref);
3123 static void output_die_symbol (dw_die_ref);
3124 static void output_die (dw_die_ref);
3125 static void output_compilation_unit_header (void);
3126 static void output_comp_unit (dw_die_ref, int);
3127 static void output_comdat_type_unit (comdat_type_node *);
3128 static const char *dwarf2_name (tree, int);
3129 static void add_pubname (tree, dw_die_ref);
3130 static void add_enumerator_pubname (const char *, dw_die_ref);
3131 static void add_pubname_string (const char *, dw_die_ref);
3132 static void add_pubtype (tree, dw_die_ref);
3133 static void output_pubnames (vec<pubname_entry, va_gc> *);
3134 static void output_aranges (unsigned long);
3135 static unsigned int add_ranges_num (int);
3136 static unsigned int add_ranges (const_tree);
3137 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3138 bool *, bool);
3139 static void output_ranges (void);
3140 static dw_line_info_table *new_line_info_table (void);
3141 static void output_line_info (bool);
3142 static void output_file_names (void);
3143 static dw_die_ref base_type_die (tree);
3144 static int is_base_type (tree);
3145 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
3146 static int decl_quals (const_tree);
3147 static dw_die_ref modified_type_die (tree, int, dw_die_ref);
3148 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3149 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3150 static int type_is_enum (const_tree);
3151 static unsigned int dbx_reg_number (const_rtx);
3152 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3153 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3154 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3155 enum var_init_status);
3156 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3157 enum var_init_status);
3158 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3159 enum var_init_status);
3160 static int is_based_loc (const_rtx);
3161 static bool resolve_one_addr (rtx *);
3162 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3163 enum var_init_status);
3164 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
3165 enum var_init_status);
3166 static dw_loc_list_ref loc_list_from_tree (tree, int);
3167 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
3168 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3169 static tree field_type (const_tree);
3170 static unsigned int simple_type_align_in_bits (const_tree);
3171 static unsigned int simple_decl_align_in_bits (const_tree);
3172 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3173 static HOST_WIDE_INT field_byte_offset (const_tree);
3174 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3175 dw_loc_list_ref);
3176 static void add_data_member_location_attribute (dw_die_ref, tree);
3177 static bool add_const_value_attribute (dw_die_ref, rtx);
3178 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3179 static void insert_wide_int (const wide_int &, unsigned char *, int);
3180 static void insert_float (const_rtx, unsigned char *);
3181 static rtx rtl_for_decl_location (tree);
3182 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool,
3183 enum dwarf_attribute);
3184 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3185 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3186 static void add_name_attribute (dw_die_ref, const char *);
3187 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3188 static void add_comp_dir_attribute (dw_die_ref);
3189 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
3190 static void add_subscript_info (dw_die_ref, tree, bool);
3191 static void add_byte_size_attribute (dw_die_ref, tree);
3192 static void add_bit_offset_attribute (dw_die_ref, tree);
3193 static void add_bit_size_attribute (dw_die_ref, tree);
3194 static void add_prototyped_attribute (dw_die_ref, tree);
3195 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3196 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3197 static void add_src_coords_attributes (dw_die_ref, tree);
3198 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3199 static void push_decl_scope (tree);
3200 static void pop_decl_scope (void);
3201 static dw_die_ref scope_die_for (tree, dw_die_ref);
3202 static inline int local_scope_p (dw_die_ref);
3203 static inline int class_scope_p (dw_die_ref);
3204 static inline int class_or_namespace_scope_p (dw_die_ref);
3205 static void add_type_attribute (dw_die_ref, tree, int, dw_die_ref);
3206 static void add_calling_convention_attribute (dw_die_ref, tree);
3207 static const char *type_tag (const_tree);
3208 static tree member_declared_type (const_tree);
3209 #if 0
3210 static const char *decl_start_label (tree);
3211 #endif
3212 static void gen_array_type_die (tree, dw_die_ref);
3213 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3214 #if 0
3215 static void gen_entry_point_die (tree, dw_die_ref);
3216 #endif
3217 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3218 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3219 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3220 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3221 static void gen_formal_types_die (tree, dw_die_ref);
3222 static void gen_subprogram_die (tree, dw_die_ref);
3223 static void gen_variable_die (tree, tree, dw_die_ref);
3224 static void gen_const_die (tree, dw_die_ref);
3225 static void gen_label_die (tree, dw_die_ref);
3226 static void gen_lexical_block_die (tree, dw_die_ref, int);
3227 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
3228 static void gen_field_die (tree, dw_die_ref);
3229 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3230 static dw_die_ref gen_compile_unit_die (const char *);
3231 static void gen_inheritance_die (tree, tree, dw_die_ref);
3232 static void gen_member_die (tree, dw_die_ref);
3233 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3234 enum debug_info_usage);
3235 static void gen_subroutine_type_die (tree, dw_die_ref);
3236 static void gen_typedef_die (tree, dw_die_ref);
3237 static void gen_type_die (tree, dw_die_ref);
3238 static void gen_block_die (tree, dw_die_ref, int);
3239 static void decls_for_scope (tree, dw_die_ref, int);
3240 static inline int is_redundant_typedef (const_tree);
3241 static bool is_naming_typedef_decl (const_tree);
3242 static inline dw_die_ref get_context_die (tree);
3243 static void gen_namespace_die (tree, dw_die_ref);
3244 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3245 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
3246 static dw_die_ref force_decl_die (tree);
3247 static dw_die_ref force_type_die (tree);
3248 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3249 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3250 static struct dwarf_file_data * lookup_filename (const char *);
3251 static void retry_incomplete_types (void);
3252 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3253 static void gen_generic_params_dies (tree);
3254 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3255 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3256 static void splice_child_die (dw_die_ref, dw_die_ref);
3257 static int file_info_cmp (const void *, const void *);
3258 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3259 const char *, const char *);
3260 static void output_loc_list (dw_loc_list_ref);
3261 static char *gen_internal_sym (const char *);
3262 static bool want_pubnames (void);
3264 static void prune_unmark_dies (dw_die_ref);
3265 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3266 static void prune_unused_types_mark (dw_die_ref, int);
3267 static void prune_unused_types_walk (dw_die_ref);
3268 static void prune_unused_types_walk_attribs (dw_die_ref);
3269 static void prune_unused_types_prune (dw_die_ref);
3270 static void prune_unused_types (void);
3271 static int maybe_emit_file (struct dwarf_file_data *fd);
3272 static inline const char *AT_vms_delta1 (dw_attr_ref);
3273 static inline const char *AT_vms_delta2 (dw_attr_ref);
3274 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3275 const char *, const char *);
3276 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3277 static void gen_remaining_tmpl_value_param_die_attribute (void);
3278 static bool generic_type_p (tree);
3279 static void schedule_generic_params_dies_gen (tree t);
3280 static void gen_scheduled_generic_parms_dies (void);
3282 static const char *comp_dir_string (void);
3284 static void hash_loc_operands (dw_loc_descr_ref, inchash::hash &);
3286 /* enum for tracking thread-local variables whose address is really an offset
3287 relative to the TLS pointer, which will need link-time relocation, but will
3288 not need relocation by the DWARF consumer. */
3290 enum dtprel_bool
3292 dtprel_false = 0,
3293 dtprel_true = 1
3296 /* Return the operator to use for an address of a variable. For dtprel_true, we
3297 use DW_OP_const*. For regular variables, which need both link-time
3298 relocation and consumer-level relocation (e.g., to account for shared objects
3299 loaded at a random address), we use DW_OP_addr*. */
3301 static inline enum dwarf_location_atom
3302 dw_addr_op (enum dtprel_bool dtprel)
3304 if (dtprel == dtprel_true)
3305 return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3306 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3307 else
3308 return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3311 /* Return a pointer to a newly allocated address location description. If
3312 dwarf_split_debug_info is true, then record the address with the appropriate
3313 relocation. */
3314 static inline dw_loc_descr_ref
3315 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3317 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3319 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3320 ref->dw_loc_oprnd1.v.val_addr = addr;
3321 ref->dtprel = dtprel;
3322 if (dwarf_split_debug_info)
3323 ref->dw_loc_oprnd1.val_entry
3324 = add_addr_table_entry (addr,
3325 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3326 else
3327 ref->dw_loc_oprnd1.val_entry = NULL;
3329 return ref;
3332 /* Section names used to hold DWARF debugging information. */
3334 #ifndef DEBUG_INFO_SECTION
3335 #define DEBUG_INFO_SECTION ".debug_info"
3336 #endif
3337 #ifndef DEBUG_DWO_INFO_SECTION
3338 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3339 #endif
3340 #ifndef DEBUG_ABBREV_SECTION
3341 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3342 #endif
3343 #ifndef DEBUG_DWO_ABBREV_SECTION
3344 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3345 #endif
3346 #ifndef DEBUG_ARANGES_SECTION
3347 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3348 #endif
3349 #ifndef DEBUG_ADDR_SECTION
3350 #define DEBUG_ADDR_SECTION ".debug_addr"
3351 #endif
3352 #ifndef DEBUG_NORM_MACINFO_SECTION
3353 #define DEBUG_NORM_MACINFO_SECTION ".debug_macinfo"
3354 #endif
3355 #ifndef DEBUG_DWO_MACINFO_SECTION
3356 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
3357 #endif
3358 #ifndef DEBUG_MACINFO_SECTION
3359 #define DEBUG_MACINFO_SECTION \
3360 (!dwarf_split_debug_info \
3361 ? (DEBUG_NORM_MACINFO_SECTION) : (DEBUG_DWO_MACINFO_SECTION))
3362 #endif
3363 #ifndef DEBUG_NORM_MACRO_SECTION
3364 #define DEBUG_NORM_MACRO_SECTION ".debug_macro"
3365 #endif
3366 #ifndef DEBUG_DWO_MACRO_SECTION
3367 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
3368 #endif
3369 #ifndef DEBUG_MACRO_SECTION
3370 #define DEBUG_MACRO_SECTION \
3371 (!dwarf_split_debug_info \
3372 ? (DEBUG_NORM_MACRO_SECTION) : (DEBUG_DWO_MACRO_SECTION))
3373 #endif
3374 #ifndef DEBUG_LINE_SECTION
3375 #define DEBUG_LINE_SECTION ".debug_line"
3376 #endif
3377 #ifndef DEBUG_DWO_LINE_SECTION
3378 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3379 #endif
3380 #ifndef DEBUG_LOC_SECTION
3381 #define DEBUG_LOC_SECTION ".debug_loc"
3382 #endif
3383 #ifndef DEBUG_DWO_LOC_SECTION
3384 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
3385 #endif
3386 #ifndef DEBUG_PUBNAMES_SECTION
3387 #define DEBUG_PUBNAMES_SECTION \
3388 ((debug_generate_pub_sections == 2) \
3389 ? ".debug_gnu_pubnames" : ".debug_pubnames")
3390 #endif
3391 #ifndef DEBUG_PUBTYPES_SECTION
3392 #define DEBUG_PUBTYPES_SECTION \
3393 ((debug_generate_pub_sections == 2) \
3394 ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
3395 #endif
3396 #define DEBUG_NORM_STR_OFFSETS_SECTION ".debug_str_offsets"
3397 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3398 #ifndef DEBUG_STR_OFFSETS_SECTION
3399 #define DEBUG_STR_OFFSETS_SECTION \
3400 (!dwarf_split_debug_info \
3401 ? (DEBUG_NORM_STR_OFFSETS_SECTION) : (DEBUG_DWO_STR_OFFSETS_SECTION))
3402 #endif
3403 #ifndef DEBUG_STR_DWO_SECTION
3404 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
3405 #endif
3406 #ifndef DEBUG_STR_SECTION
3407 #define DEBUG_STR_SECTION ".debug_str"
3408 #endif
3409 #ifndef DEBUG_RANGES_SECTION
3410 #define DEBUG_RANGES_SECTION ".debug_ranges"
3411 #endif
3413 /* Standard ELF section names for compiled code and data. */
3414 #ifndef TEXT_SECTION_NAME
3415 #define TEXT_SECTION_NAME ".text"
3416 #endif
3418 /* Section flags for .debug_macinfo/.debug_macro section. */
3419 #define DEBUG_MACRO_SECTION_FLAGS \
3420 (dwarf_split_debug_info ? SECTION_DEBUG | SECTION_EXCLUDE : SECTION_DEBUG)
3422 /* Section flags for .debug_str section. */
3423 #define DEBUG_STR_SECTION_FLAGS \
3424 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
3425 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3426 : SECTION_DEBUG)
3428 /* Section flags for .debug_str.dwo section. */
3429 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
3431 /* Labels we insert at beginning sections we can reference instead of
3432 the section names themselves. */
3434 #ifndef TEXT_SECTION_LABEL
3435 #define TEXT_SECTION_LABEL "Ltext"
3436 #endif
3437 #ifndef COLD_TEXT_SECTION_LABEL
3438 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
3439 #endif
3440 #ifndef DEBUG_LINE_SECTION_LABEL
3441 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3442 #endif
3443 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3444 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
3445 #endif
3446 #ifndef DEBUG_INFO_SECTION_LABEL
3447 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3448 #endif
3449 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3450 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
3451 #endif
3452 #ifndef DEBUG_ABBREV_SECTION_LABEL
3453 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3454 #endif
3455 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3456 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3457 #endif
3458 #ifndef DEBUG_ADDR_SECTION_LABEL
3459 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
3460 #endif
3461 #ifndef DEBUG_LOC_SECTION_LABEL
3462 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3463 #endif
3464 #ifndef DEBUG_RANGES_SECTION_LABEL
3465 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3466 #endif
3467 #ifndef DEBUG_MACINFO_SECTION_LABEL
3468 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3469 #endif
3470 #ifndef DEBUG_MACRO_SECTION_LABEL
3471 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
3472 #endif
3473 #define SKELETON_COMP_DIE_ABBREV 1
3474 #define SKELETON_TYPE_DIE_ABBREV 2
3476 /* Definitions of defaults for formats and names of various special
3477 (artificial) labels which may be generated within this file (when the -g
3478 options is used and DWARF2_DEBUGGING_INFO is in effect.
3479 If necessary, these may be overridden from within the tm.h file, but
3480 typically, overriding these defaults is unnecessary. */
3482 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3483 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3484 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3485 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3486 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3487 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3488 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3489 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3490 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3491 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3492 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3493 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3494 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3495 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3497 #ifndef TEXT_END_LABEL
3498 #define TEXT_END_LABEL "Letext"
3499 #endif
3500 #ifndef COLD_END_LABEL
3501 #define COLD_END_LABEL "Letext_cold"
3502 #endif
3503 #ifndef BLOCK_BEGIN_LABEL
3504 #define BLOCK_BEGIN_LABEL "LBB"
3505 #endif
3506 #ifndef BLOCK_END_LABEL
3507 #define BLOCK_END_LABEL "LBE"
3508 #endif
3509 #ifndef LINE_CODE_LABEL
3510 #define LINE_CODE_LABEL "LM"
3511 #endif
3514 /* Return the root of the DIE's built for the current compilation unit. */
3515 static dw_die_ref
3516 comp_unit_die (void)
3518 if (!single_comp_unit_die)
3519 single_comp_unit_die = gen_compile_unit_die (NULL);
3520 return single_comp_unit_die;
3523 /* We allow a language front-end to designate a function that is to be
3524 called to "demangle" any name before it is put into a DIE. */
3526 static const char *(*demangle_name_func) (const char *);
3528 void
3529 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3531 demangle_name_func = func;
3534 /* Test if rtl node points to a pseudo register. */
3536 static inline int
3537 is_pseudo_reg (const_rtx rtl)
3539 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3540 || (GET_CODE (rtl) == SUBREG
3541 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3544 /* Return a reference to a type, with its const and volatile qualifiers
3545 removed. */
3547 static inline tree
3548 type_main_variant (tree type)
3550 type = TYPE_MAIN_VARIANT (type);
3552 /* ??? There really should be only one main variant among any group of
3553 variants of a given type (and all of the MAIN_VARIANT values for all
3554 members of the group should point to that one type) but sometimes the C
3555 front-end messes this up for array types, so we work around that bug
3556 here. */
3557 if (TREE_CODE (type) == ARRAY_TYPE)
3558 while (type != TYPE_MAIN_VARIANT (type))
3559 type = TYPE_MAIN_VARIANT (type);
3561 return type;
3564 /* Return nonzero if the given type node represents a tagged type. */
3566 static inline int
3567 is_tagged_type (const_tree type)
3569 enum tree_code code = TREE_CODE (type);
3571 return (code == RECORD_TYPE || code == UNION_TYPE
3572 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3575 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
3577 static void
3578 get_ref_die_offset_label (char *label, dw_die_ref ref)
3580 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3583 /* Return die_offset of a DIE reference to a base type. */
3585 static unsigned long int
3586 get_base_type_offset (dw_die_ref ref)
3588 if (ref->die_offset)
3589 return ref->die_offset;
3590 if (comp_unit_die ()->die_abbrev)
3592 calc_base_type_die_sizes ();
3593 gcc_assert (ref->die_offset);
3595 return ref->die_offset;
3598 /* Return die_offset of a DIE reference other than base type. */
3600 static unsigned long int
3601 get_ref_die_offset (dw_die_ref ref)
3603 gcc_assert (ref->die_offset);
3604 return ref->die_offset;
3607 /* Convert a DIE tag into its string name. */
3609 static const char *
3610 dwarf_tag_name (unsigned int tag)
3612 const char *name = get_DW_TAG_name (tag);
3614 if (name != NULL)
3615 return name;
3617 return "DW_TAG_<unknown>";
3620 /* Convert a DWARF attribute code into its string name. */
3622 static const char *
3623 dwarf_attr_name (unsigned int attr)
3625 const char *name;
3627 switch (attr)
3629 #if VMS_DEBUGGING_INFO
3630 case DW_AT_HP_prologue:
3631 return "DW_AT_HP_prologue";
3632 #else
3633 case DW_AT_MIPS_loop_unroll_factor:
3634 return "DW_AT_MIPS_loop_unroll_factor";
3635 #endif
3637 #if VMS_DEBUGGING_INFO
3638 case DW_AT_HP_epilogue:
3639 return "DW_AT_HP_epilogue";
3640 #else
3641 case DW_AT_MIPS_stride:
3642 return "DW_AT_MIPS_stride";
3643 #endif
3646 name = get_DW_AT_name (attr);
3648 if (name != NULL)
3649 return name;
3651 return "DW_AT_<unknown>";
3654 /* Convert a DWARF value form code into its string name. */
3656 static const char *
3657 dwarf_form_name (unsigned int form)
3659 const char *name = get_DW_FORM_name (form);
3661 if (name != NULL)
3662 return name;
3664 return "DW_FORM_<unknown>";
3667 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
3668 instance of an inlined instance of a decl which is local to an inline
3669 function, so we have to trace all of the way back through the origin chain
3670 to find out what sort of node actually served as the original seed for the
3671 given block. */
3673 static tree
3674 decl_ultimate_origin (const_tree decl)
3676 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
3677 return NULL_TREE;
3679 /* DECL_ABSTRACT_ORIGIN can point to itself; ignore that if
3680 we're trying to output the abstract instance of this function. */
3681 if (DECL_ABSTRACT_P (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
3682 return NULL_TREE;
3684 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
3685 most distant ancestor, this should never happen. */
3686 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
3688 return DECL_ABSTRACT_ORIGIN (decl);
3691 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
3692 of a virtual function may refer to a base class, so we check the 'this'
3693 parameter. */
3695 static tree
3696 decl_class_context (tree decl)
3698 tree context = NULL_TREE;
3700 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
3701 context = DECL_CONTEXT (decl);
3702 else
3703 context = TYPE_MAIN_VARIANT
3704 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
3706 if (context && !TYPE_P (context))
3707 context = NULL_TREE;
3709 return context;
3712 /* Add an attribute/value pair to a DIE. */
3714 static inline void
3715 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
3717 /* Maybe this should be an assert? */
3718 if (die == NULL)
3719 return;
3721 vec_safe_reserve (die->die_attr, 1);
3722 vec_safe_push (die->die_attr, *attr);
3725 static inline enum dw_val_class
3726 AT_class (dw_attr_ref a)
3728 return a->dw_attr_val.val_class;
3731 /* Return the index for any attribute that will be referenced with a
3732 DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index. String indices
3733 are stored in dw_attr_val.v.val_str for reference counting
3734 pruning. */
3736 static inline unsigned int
3737 AT_index (dw_attr_ref a)
3739 if (AT_class (a) == dw_val_class_str)
3740 return a->dw_attr_val.v.val_str->index;
3741 else if (a->dw_attr_val.val_entry != NULL)
3742 return a->dw_attr_val.val_entry->index;
3743 return NOT_INDEXED;
3746 /* Add a flag value attribute to a DIE. */
3748 static inline void
3749 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
3751 dw_attr_node attr;
3753 attr.dw_attr = attr_kind;
3754 attr.dw_attr_val.val_class = dw_val_class_flag;
3755 attr.dw_attr_val.val_entry = NULL;
3756 attr.dw_attr_val.v.val_flag = flag;
3757 add_dwarf_attr (die, &attr);
3760 static inline unsigned
3761 AT_flag (dw_attr_ref a)
3763 gcc_assert (a && AT_class (a) == dw_val_class_flag);
3764 return a->dw_attr_val.v.val_flag;
3767 /* Add a signed integer attribute value to a DIE. */
3769 static inline void
3770 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
3772 dw_attr_node attr;
3774 attr.dw_attr = attr_kind;
3775 attr.dw_attr_val.val_class = dw_val_class_const;
3776 attr.dw_attr_val.val_entry = NULL;
3777 attr.dw_attr_val.v.val_int = int_val;
3778 add_dwarf_attr (die, &attr);
3781 static inline HOST_WIDE_INT
3782 AT_int (dw_attr_ref a)
3784 gcc_assert (a && AT_class (a) == dw_val_class_const);
3785 return a->dw_attr_val.v.val_int;
3788 /* Add an unsigned integer attribute value to a DIE. */
3790 static inline void
3791 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
3792 unsigned HOST_WIDE_INT unsigned_val)
3794 dw_attr_node attr;
3796 attr.dw_attr = attr_kind;
3797 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
3798 attr.dw_attr_val.val_entry = NULL;
3799 attr.dw_attr_val.v.val_unsigned = unsigned_val;
3800 add_dwarf_attr (die, &attr);
3803 static inline unsigned HOST_WIDE_INT
3804 AT_unsigned (dw_attr_ref a)
3806 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
3807 return a->dw_attr_val.v.val_unsigned;
3810 /* Add an unsigned wide integer attribute value to a DIE. */
3812 static inline void
3813 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
3814 const wide_int& w)
3816 dw_attr_node attr;
3818 attr.dw_attr = attr_kind;
3819 attr.dw_attr_val.val_class = dw_val_class_wide_int;
3820 attr.dw_attr_val.v.val_wide = ggc_cleared_alloc<wide_int> ();
3821 *attr.dw_attr_val.v.val_wide = w;
3822 add_dwarf_attr (die, &attr);
3825 /* Add an unsigned double integer attribute value to a DIE. */
3827 static inline void
3828 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
3829 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
3831 dw_attr_node attr;
3833 attr.dw_attr = attr_kind;
3834 attr.dw_attr_val.val_class = dw_val_class_const_double;
3835 attr.dw_attr_val.val_entry = NULL;
3836 attr.dw_attr_val.v.val_double.high = high;
3837 attr.dw_attr_val.v.val_double.low = low;
3838 add_dwarf_attr (die, &attr);
3841 /* Add a floating point attribute value to a DIE and return it. */
3843 static inline void
3844 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
3845 unsigned int length, unsigned int elt_size, unsigned char *array)
3847 dw_attr_node attr;
3849 attr.dw_attr = attr_kind;
3850 attr.dw_attr_val.val_class = dw_val_class_vec;
3851 attr.dw_attr_val.val_entry = NULL;
3852 attr.dw_attr_val.v.val_vec.length = length;
3853 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
3854 attr.dw_attr_val.v.val_vec.array = array;
3855 add_dwarf_attr (die, &attr);
3858 /* Add an 8-byte data attribute value to a DIE. */
3860 static inline void
3861 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
3862 unsigned char data8[8])
3864 dw_attr_node attr;
3866 attr.dw_attr = attr_kind;
3867 attr.dw_attr_val.val_class = dw_val_class_data8;
3868 attr.dw_attr_val.val_entry = NULL;
3869 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
3870 add_dwarf_attr (die, &attr);
3873 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
3874 dwarf_split_debug_info, address attributes in dies destined for the
3875 final executable have force_direct set to avoid using indexed
3876 references. */
3878 static inline void
3879 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
3880 bool force_direct)
3882 dw_attr_node attr;
3883 char * lbl_id;
3885 lbl_id = xstrdup (lbl_low);
3886 attr.dw_attr = DW_AT_low_pc;
3887 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3888 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3889 if (dwarf_split_debug_info && !force_direct)
3890 attr.dw_attr_val.val_entry
3891 = add_addr_table_entry (lbl_id, ate_kind_label);
3892 else
3893 attr.dw_attr_val.val_entry = NULL;
3894 add_dwarf_attr (die, &attr);
3896 attr.dw_attr = DW_AT_high_pc;
3897 if (dwarf_version < 4)
3898 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3899 else
3900 attr.dw_attr_val.val_class = dw_val_class_high_pc;
3901 lbl_id = xstrdup (lbl_high);
3902 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3903 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
3904 && dwarf_split_debug_info && !force_direct)
3905 attr.dw_attr_val.val_entry
3906 = add_addr_table_entry (lbl_id, ate_kind_label);
3907 else
3908 attr.dw_attr_val.val_entry = NULL;
3909 add_dwarf_attr (die, &attr);
3912 /* Hash and equality functions for debug_str_hash. */
3914 static hashval_t
3915 debug_str_do_hash (const void *x)
3917 return htab_hash_string (((const struct indirect_string_node *)x)->str);
3920 static int
3921 debug_str_eq (const void *x1, const void *x2)
3923 return strcmp ((((const struct indirect_string_node *)x1)->str),
3924 (const char *)x2) == 0;
3927 /* Add STR to the given string hash table. */
3929 static struct indirect_string_node *
3930 find_AT_string_in_table (const char *str, htab_t table)
3932 struct indirect_string_node *node;
3933 void **slot;
3935 slot = htab_find_slot_with_hash (table, str,
3936 htab_hash_string (str), INSERT);
3937 if (*slot == NULL)
3939 node = ggc_cleared_alloc<indirect_string_node> ();
3940 node->str = ggc_strdup (str);
3941 *slot = node;
3943 else
3944 node = (struct indirect_string_node *) *slot;
3946 node->refcount++;
3947 return node;
3950 /* Add STR to the indirect string hash table. */
3952 static struct indirect_string_node *
3953 find_AT_string (const char *str)
3955 if (! debug_str_hash)
3956 debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
3957 debug_str_eq, NULL);
3959 return find_AT_string_in_table (str, debug_str_hash);
3962 /* Add a string attribute value to a DIE. */
3964 static inline void
3965 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
3967 dw_attr_node attr;
3968 struct indirect_string_node *node;
3970 node = find_AT_string (str);
3972 attr.dw_attr = attr_kind;
3973 attr.dw_attr_val.val_class = dw_val_class_str;
3974 attr.dw_attr_val.val_entry = NULL;
3975 attr.dw_attr_val.v.val_str = node;
3976 add_dwarf_attr (die, &attr);
3979 static inline const char *
3980 AT_string (dw_attr_ref a)
3982 gcc_assert (a && AT_class (a) == dw_val_class_str);
3983 return a->dw_attr_val.v.val_str->str;
3986 /* Call this function directly to bypass AT_string_form's logic to put
3987 the string inline in the die. */
3989 static void
3990 set_indirect_string (struct indirect_string_node *node)
3992 char label[32];
3993 /* Already indirect is a no op. */
3994 if (node->form == DW_FORM_strp || node->form == DW_FORM_GNU_str_index)
3996 gcc_assert (node->label);
3997 return;
3999 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4000 ++dw2_string_counter;
4001 node->label = xstrdup (label);
4003 if (!dwarf_split_debug_info)
4005 node->form = DW_FORM_strp;
4006 node->index = NOT_INDEXED;
4008 else
4010 node->form = DW_FORM_GNU_str_index;
4011 node->index = NO_INDEX_ASSIGNED;
4015 /* Find out whether a string should be output inline in DIE
4016 or out-of-line in .debug_str section. */
4018 static enum dwarf_form
4019 find_string_form (struct indirect_string_node *node)
4021 unsigned int len;
4023 if (node->form)
4024 return node->form;
4026 len = strlen (node->str) + 1;
4028 /* If the string is shorter or equal to the size of the reference, it is
4029 always better to put it inline. */
4030 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4031 return node->form = DW_FORM_string;
4033 /* If we cannot expect the linker to merge strings in .debug_str
4034 section, only put it into .debug_str if it is worth even in this
4035 single module. */
4036 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4037 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4038 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
4039 return node->form = DW_FORM_string;
4041 set_indirect_string (node);
4043 return node->form;
4046 /* Find out whether the string referenced from the attribute should be
4047 output inline in DIE or out-of-line in .debug_str section. */
4049 static enum dwarf_form
4050 AT_string_form (dw_attr_ref a)
4052 gcc_assert (a && AT_class (a) == dw_val_class_str);
4053 return find_string_form (a->dw_attr_val.v.val_str);
4056 /* Add a DIE reference attribute value to a DIE. */
4058 static inline void
4059 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4061 dw_attr_node attr;
4063 #ifdef ENABLE_CHECKING
4064 gcc_assert (targ_die != NULL);
4065 #else
4066 /* With LTO we can end up trying to reference something we didn't create
4067 a DIE for. Avoid crashing later on a NULL referenced DIE. */
4068 if (targ_die == NULL)
4069 return;
4070 #endif
4072 attr.dw_attr = attr_kind;
4073 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4074 attr.dw_attr_val.val_entry = NULL;
4075 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4076 attr.dw_attr_val.v.val_die_ref.external = 0;
4077 add_dwarf_attr (die, &attr);
4080 /* Change DIE reference REF to point to NEW_DIE instead. */
4082 static inline void
4083 change_AT_die_ref (dw_attr_ref ref, dw_die_ref new_die)
4085 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4086 ref->dw_attr_val.v.val_die_ref.die = new_die;
4087 ref->dw_attr_val.v.val_die_ref.external = 0;
4090 /* Add an AT_specification attribute to a DIE, and also make the back
4091 pointer from the specification to the definition. */
4093 static inline void
4094 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4096 add_AT_die_ref (die, DW_AT_specification, targ_die);
4097 gcc_assert (!targ_die->die_definition);
4098 targ_die->die_definition = die;
4101 static inline dw_die_ref
4102 AT_ref (dw_attr_ref a)
4104 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4105 return a->dw_attr_val.v.val_die_ref.die;
4108 static inline int
4109 AT_ref_external (dw_attr_ref a)
4111 if (a && AT_class (a) == dw_val_class_die_ref)
4112 return a->dw_attr_val.v.val_die_ref.external;
4114 return 0;
4117 static inline void
4118 set_AT_ref_external (dw_attr_ref a, int i)
4120 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4121 a->dw_attr_val.v.val_die_ref.external = i;
4124 /* Add an FDE reference attribute value to a DIE. */
4126 static inline void
4127 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4129 dw_attr_node attr;
4131 attr.dw_attr = attr_kind;
4132 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4133 attr.dw_attr_val.val_entry = NULL;
4134 attr.dw_attr_val.v.val_fde_index = targ_fde;
4135 add_dwarf_attr (die, &attr);
4138 /* Add a location description attribute value to a DIE. */
4140 static inline void
4141 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4143 dw_attr_node attr;
4145 attr.dw_attr = attr_kind;
4146 attr.dw_attr_val.val_class = dw_val_class_loc;
4147 attr.dw_attr_val.val_entry = NULL;
4148 attr.dw_attr_val.v.val_loc = loc;
4149 add_dwarf_attr (die, &attr);
4152 static inline dw_loc_descr_ref
4153 AT_loc (dw_attr_ref a)
4155 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4156 return a->dw_attr_val.v.val_loc;
4159 static inline void
4160 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4162 dw_attr_node attr;
4164 attr.dw_attr = attr_kind;
4165 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4166 attr.dw_attr_val.val_entry = NULL;
4167 attr.dw_attr_val.v.val_loc_list = loc_list;
4168 add_dwarf_attr (die, &attr);
4169 have_location_lists = true;
4172 static inline dw_loc_list_ref
4173 AT_loc_list (dw_attr_ref a)
4175 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4176 return a->dw_attr_val.v.val_loc_list;
4179 static inline dw_loc_list_ref *
4180 AT_loc_list_ptr (dw_attr_ref a)
4182 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4183 return &a->dw_attr_val.v.val_loc_list;
4186 /* Table of entries into the .debug_addr section. */
4188 static GTY ((param_is (addr_table_entry))) htab_t addr_index_table;
4190 /* Hash an address_table_entry. */
4192 static hashval_t
4193 addr_table_entry_do_hash (const void *x)
4195 const addr_table_entry *a = (const addr_table_entry *) x;
4196 inchash::hash hstate;
4197 switch (a->kind)
4199 case ate_kind_rtx:
4200 hstate.add_int (0);
4201 break;
4202 case ate_kind_rtx_dtprel:
4203 hstate.add_int (1);
4204 break;
4205 case ate_kind_label:
4206 return htab_hash_string (a->addr.label);
4207 default:
4208 gcc_unreachable ();
4210 inchash::add_rtx (a->addr.rtl, hstate);
4211 return hstate.end ();
4214 /* Determine equality for two address_table_entries. */
4216 static int
4217 addr_table_entry_eq (const void *x1, const void *x2)
4219 const addr_table_entry *a1 = (const addr_table_entry *) x1;
4220 const addr_table_entry *a2 = (const addr_table_entry *) x2;
4222 if (a1->kind != a2->kind)
4223 return 0;
4224 switch (a1->kind)
4226 case ate_kind_rtx:
4227 case ate_kind_rtx_dtprel:
4228 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4229 case ate_kind_label:
4230 return strcmp (a1->addr.label, a2->addr.label) == 0;
4231 default:
4232 gcc_unreachable ();
4236 /* Initialize an addr_table_entry. */
4238 void
4239 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4241 e->kind = kind;
4242 switch (kind)
4244 case ate_kind_rtx:
4245 case ate_kind_rtx_dtprel:
4246 e->addr.rtl = (rtx) addr;
4247 break;
4248 case ate_kind_label:
4249 e->addr.label = (char *) addr;
4250 break;
4252 e->refcount = 0;
4253 e->index = NO_INDEX_ASSIGNED;
4256 /* Add attr to the address table entry to the table. Defer setting an
4257 index until output time. */
4259 static addr_table_entry *
4260 add_addr_table_entry (void *addr, enum ate_kind kind)
4262 addr_table_entry *node;
4263 addr_table_entry finder;
4264 void **slot;
4266 gcc_assert (dwarf_split_debug_info);
4267 if (! addr_index_table)
4268 addr_index_table = htab_create_ggc (10, addr_table_entry_do_hash,
4269 addr_table_entry_eq, NULL);
4270 init_addr_table_entry (&finder, kind, addr);
4271 slot = htab_find_slot (addr_index_table, &finder, INSERT);
4273 if (*slot == HTAB_EMPTY_ENTRY)
4275 node = ggc_cleared_alloc<addr_table_entry> ();
4276 init_addr_table_entry (node, kind, addr);
4277 *slot = node;
4279 else
4280 node = (addr_table_entry *) *slot;
4282 node->refcount++;
4283 return node;
4286 /* Remove an entry from the addr table by decrementing its refcount.
4287 Strictly, decrementing the refcount would be enough, but the
4288 assertion that the entry is actually in the table has found
4289 bugs. */
4291 static void
4292 remove_addr_table_entry (addr_table_entry *entry)
4294 gcc_assert (dwarf_split_debug_info && addr_index_table);
4295 /* After an index is assigned, the table is frozen. */
4296 gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
4297 entry->refcount--;
4300 /* Given a location list, remove all addresses it refers to from the
4301 address_table. */
4303 static void
4304 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4306 for (; descr; descr = descr->dw_loc_next)
4307 if (descr->dw_loc_oprnd1.val_entry != NULL)
4309 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4310 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4314 /* A helper function for dwarf2out_finish called through
4315 htab_traverse. Assign an addr_table_entry its index. All entries
4316 must be collected into the table when this function is called,
4317 because the indexing code relies on htab_traverse to traverse nodes
4318 in the same order for each run. */
4320 static int
4321 index_addr_table_entry (void **h, void *v)
4323 addr_table_entry *node = (addr_table_entry *) *h;
4324 unsigned int *index = (unsigned int *) v;
4326 /* Don't index unreferenced nodes. */
4327 if (node->refcount == 0)
4328 return 1;
4330 gcc_assert (node->index == NO_INDEX_ASSIGNED);
4331 node->index = *index;
4332 *index += 1;
4334 return 1;
4337 /* Add an address constant attribute value to a DIE. When using
4338 dwarf_split_debug_info, address attributes in dies destined for the
4339 final executable should be direct references--setting the parameter
4340 force_direct ensures this behavior. */
4342 static inline void
4343 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4344 bool force_direct)
4346 dw_attr_node attr;
4348 attr.dw_attr = attr_kind;
4349 attr.dw_attr_val.val_class = dw_val_class_addr;
4350 attr.dw_attr_val.v.val_addr = addr;
4351 if (dwarf_split_debug_info && !force_direct)
4352 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4353 else
4354 attr.dw_attr_val.val_entry = NULL;
4355 add_dwarf_attr (die, &attr);
4358 /* Get the RTX from to an address DIE attribute. */
4360 static inline rtx
4361 AT_addr (dw_attr_ref a)
4363 gcc_assert (a && AT_class (a) == dw_val_class_addr);
4364 return a->dw_attr_val.v.val_addr;
4367 /* Add a file attribute value to a DIE. */
4369 static inline void
4370 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4371 struct dwarf_file_data *fd)
4373 dw_attr_node attr;
4375 attr.dw_attr = attr_kind;
4376 attr.dw_attr_val.val_class = dw_val_class_file;
4377 attr.dw_attr_val.val_entry = NULL;
4378 attr.dw_attr_val.v.val_file = fd;
4379 add_dwarf_attr (die, &attr);
4382 /* Get the dwarf_file_data from a file DIE attribute. */
4384 static inline struct dwarf_file_data *
4385 AT_file (dw_attr_ref a)
4387 gcc_assert (a && AT_class (a) == dw_val_class_file);
4388 return a->dw_attr_val.v.val_file;
4391 /* Add a vms delta attribute value to a DIE. */
4393 static inline void
4394 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4395 const char *lbl1, const char *lbl2)
4397 dw_attr_node attr;
4399 attr.dw_attr = attr_kind;
4400 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4401 attr.dw_attr_val.val_entry = NULL;
4402 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4403 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4404 add_dwarf_attr (die, &attr);
4407 /* Add a label identifier attribute value to a DIE. */
4409 static inline void
4410 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4411 const char *lbl_id)
4413 dw_attr_node attr;
4415 attr.dw_attr = attr_kind;
4416 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4417 attr.dw_attr_val.val_entry = NULL;
4418 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4419 if (dwarf_split_debug_info)
4420 attr.dw_attr_val.val_entry
4421 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4422 ate_kind_label);
4423 add_dwarf_attr (die, &attr);
4426 /* Add a section offset attribute value to a DIE, an offset into the
4427 debug_line section. */
4429 static inline void
4430 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4431 const char *label)
4433 dw_attr_node attr;
4435 attr.dw_attr = attr_kind;
4436 attr.dw_attr_val.val_class = dw_val_class_lineptr;
4437 attr.dw_attr_val.val_entry = NULL;
4438 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4439 add_dwarf_attr (die, &attr);
4442 /* Add a section offset attribute value to a DIE, an offset into the
4443 debug_macinfo section. */
4445 static inline void
4446 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4447 const char *label)
4449 dw_attr_node attr;
4451 attr.dw_attr = attr_kind;
4452 attr.dw_attr_val.val_class = dw_val_class_macptr;
4453 attr.dw_attr_val.val_entry = NULL;
4454 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4455 add_dwarf_attr (die, &attr);
4458 /* Add an offset attribute value to a DIE. */
4460 static inline void
4461 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4462 unsigned HOST_WIDE_INT offset)
4464 dw_attr_node attr;
4466 attr.dw_attr = attr_kind;
4467 attr.dw_attr_val.val_class = dw_val_class_offset;
4468 attr.dw_attr_val.val_entry = NULL;
4469 attr.dw_attr_val.v.val_offset = offset;
4470 add_dwarf_attr (die, &attr);
4473 /* Add a range_list attribute value to a DIE. When using
4474 dwarf_split_debug_info, address attributes in dies destined for the
4475 final executable should be direct references--setting the parameter
4476 force_direct ensures this behavior. */
4478 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4479 #define RELOCATED_OFFSET (NULL)
4481 static void
4482 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4483 long unsigned int offset, bool force_direct)
4485 dw_attr_node attr;
4487 attr.dw_attr = attr_kind;
4488 attr.dw_attr_val.val_class = dw_val_class_range_list;
4489 /* For the range_list attribute, use val_entry to store whether the
4490 offset should follow split-debug-info or normal semantics. This
4491 value is read in output_range_list_offset. */
4492 if (dwarf_split_debug_info && !force_direct)
4493 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4494 else
4495 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4496 attr.dw_attr_val.v.val_offset = offset;
4497 add_dwarf_attr (die, &attr);
4500 /* Return the start label of a delta attribute. */
4502 static inline const char *
4503 AT_vms_delta1 (dw_attr_ref a)
4505 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4506 return a->dw_attr_val.v.val_vms_delta.lbl1;
4509 /* Return the end label of a delta attribute. */
4511 static inline const char *
4512 AT_vms_delta2 (dw_attr_ref a)
4514 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4515 return a->dw_attr_val.v.val_vms_delta.lbl2;
4518 static inline const char *
4519 AT_lbl (dw_attr_ref a)
4521 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4522 || AT_class (a) == dw_val_class_lineptr
4523 || AT_class (a) == dw_val_class_macptr
4524 || AT_class (a) == dw_val_class_high_pc));
4525 return a->dw_attr_val.v.val_lbl_id;
4528 /* Get the attribute of type attr_kind. */
4530 static dw_attr_ref
4531 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4533 dw_attr_ref a;
4534 unsigned ix;
4535 dw_die_ref spec = NULL;
4537 if (! die)
4538 return NULL;
4540 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4541 if (a->dw_attr == attr_kind)
4542 return a;
4543 else if (a->dw_attr == DW_AT_specification
4544 || a->dw_attr == DW_AT_abstract_origin)
4545 spec = AT_ref (a);
4547 if (spec)
4548 return get_AT (spec, attr_kind);
4550 return NULL;
4553 /* Returns the parent of the declaration of DIE. */
4555 static dw_die_ref
4556 get_die_parent (dw_die_ref die)
4558 dw_die_ref t;
4560 if (!die)
4561 return NULL;
4563 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4564 || (t = get_AT_ref (die, DW_AT_specification)))
4565 die = t;
4567 return die->die_parent;
4570 /* Return the "low pc" attribute value, typically associated with a subprogram
4571 DIE. Return null if the "low pc" attribute is either not present, or if it
4572 cannot be represented as an assembler label identifier. */
4574 static inline const char *
4575 get_AT_low_pc (dw_die_ref die)
4577 dw_attr_ref a = get_AT (die, DW_AT_low_pc);
4579 return a ? AT_lbl (a) : NULL;
4582 /* Return the "high pc" attribute value, typically associated with a subprogram
4583 DIE. Return null if the "high pc" attribute is either not present, or if it
4584 cannot be represented as an assembler label identifier. */
4586 static inline const char *
4587 get_AT_hi_pc (dw_die_ref die)
4589 dw_attr_ref a = get_AT (die, DW_AT_high_pc);
4591 return a ? AT_lbl (a) : NULL;
4594 /* Return the value of the string attribute designated by ATTR_KIND, or
4595 NULL if it is not present. */
4597 static inline const char *
4598 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4600 dw_attr_ref a = get_AT (die, attr_kind);
4602 return a ? AT_string (a) : NULL;
4605 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4606 if it is not present. */
4608 static inline int
4609 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4611 dw_attr_ref a = get_AT (die, attr_kind);
4613 return a ? AT_flag (a) : 0;
4616 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4617 if it is not present. */
4619 static inline unsigned
4620 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4622 dw_attr_ref a = get_AT (die, attr_kind);
4624 return a ? AT_unsigned (a) : 0;
4627 static inline dw_die_ref
4628 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4630 dw_attr_ref a = get_AT (die, attr_kind);
4632 return a ? AT_ref (a) : NULL;
4635 static inline struct dwarf_file_data *
4636 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4638 dw_attr_ref a = get_AT (die, attr_kind);
4640 return a ? AT_file (a) : NULL;
4643 /* Return TRUE if the language is C++. */
4645 static inline bool
4646 is_cxx (void)
4648 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4650 return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
4653 /* Return TRUE if the language is Java. */
4655 static inline bool
4656 is_java (void)
4658 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4660 return lang == DW_LANG_Java;
4663 /* Return TRUE if the language is Fortran. */
4665 static inline bool
4666 is_fortran (void)
4668 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4670 return (lang == DW_LANG_Fortran77
4671 || lang == DW_LANG_Fortran90
4672 || lang == DW_LANG_Fortran95);
4675 /* Return TRUE if the language is Ada. */
4677 static inline bool
4678 is_ada (void)
4680 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4682 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4685 /* Remove the specified attribute if present. */
4687 static void
4688 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4690 dw_attr_ref a;
4691 unsigned ix;
4693 if (! die)
4694 return;
4696 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4697 if (a->dw_attr == attr_kind)
4699 if (AT_class (a) == dw_val_class_str)
4700 if (a->dw_attr_val.v.val_str->refcount)
4701 a->dw_attr_val.v.val_str->refcount--;
4703 /* vec::ordered_remove should help reduce the number of abbrevs
4704 that are needed. */
4705 die->die_attr->ordered_remove (ix);
4706 return;
4710 /* Remove CHILD from its parent. PREV must have the property that
4711 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
4713 static void
4714 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4716 gcc_assert (child->die_parent == prev->die_parent);
4717 gcc_assert (prev->die_sib == child);
4718 if (prev == child)
4720 gcc_assert (child->die_parent->die_child == child);
4721 prev = NULL;
4723 else
4724 prev->die_sib = child->die_sib;
4725 if (child->die_parent->die_child == child)
4726 child->die_parent->die_child = prev;
4729 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
4730 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
4732 static void
4733 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4735 dw_die_ref parent = old_child->die_parent;
4737 gcc_assert (parent == prev->die_parent);
4738 gcc_assert (prev->die_sib == old_child);
4740 new_child->die_parent = parent;
4741 if (prev == old_child)
4743 gcc_assert (parent->die_child == old_child);
4744 new_child->die_sib = new_child;
4746 else
4748 prev->die_sib = new_child;
4749 new_child->die_sib = old_child->die_sib;
4751 if (old_child->die_parent->die_child == old_child)
4752 old_child->die_parent->die_child = new_child;
4755 /* Move all children from OLD_PARENT to NEW_PARENT. */
4757 static void
4758 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4760 dw_die_ref c;
4761 new_parent->die_child = old_parent->die_child;
4762 old_parent->die_child = NULL;
4763 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4766 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
4767 matches TAG. */
4769 static void
4770 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4772 dw_die_ref c;
4774 c = die->die_child;
4775 if (c) do {
4776 dw_die_ref prev = c;
4777 c = c->die_sib;
4778 while (c->die_tag == tag)
4780 remove_child_with_prev (c, prev);
4781 /* Might have removed every child. */
4782 if (c == c->die_sib)
4783 return;
4784 c = c->die_sib;
4786 } while (c != die->die_child);
4789 /* Add a CHILD_DIE as the last child of DIE. */
4791 static void
4792 add_child_die (dw_die_ref die, dw_die_ref child_die)
4794 /* FIXME this should probably be an assert. */
4795 if (! die || ! child_die)
4796 return;
4797 gcc_assert (die != child_die);
4799 child_die->die_parent = die;
4800 if (die->die_child)
4802 child_die->die_sib = die->die_child->die_sib;
4803 die->die_child->die_sib = child_die;
4805 else
4806 child_die->die_sib = child_die;
4807 die->die_child = child_die;
4810 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
4811 is the specification, to the end of PARENT's list of children.
4812 This is done by removing and re-adding it. */
4814 static void
4815 splice_child_die (dw_die_ref parent, dw_die_ref child)
4817 dw_die_ref p;
4819 /* We want the declaration DIE from inside the class, not the
4820 specification DIE at toplevel. */
4821 if (child->die_parent != parent)
4823 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
4825 if (tmp)
4826 child = tmp;
4829 gcc_assert (child->die_parent == parent
4830 || (child->die_parent
4831 == get_AT_ref (parent, DW_AT_specification)));
4833 for (p = child->die_parent->die_child; ; p = p->die_sib)
4834 if (p->die_sib == child)
4836 remove_child_with_prev (child, p);
4837 break;
4840 add_child_die (parent, child);
4843 /* Return a pointer to a newly created DIE node. */
4845 static inline dw_die_ref
4846 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
4848 dw_die_ref die = ggc_cleared_alloc<die_node> ();
4850 die->die_tag = tag_value;
4852 if (parent_die != NULL)
4853 add_child_die (parent_die, die);
4854 else
4856 limbo_die_node *limbo_node;
4858 limbo_node = ggc_cleared_alloc<limbo_die_node> ();
4859 limbo_node->die = die;
4860 limbo_node->created_for = t;
4861 limbo_node->next = limbo_die_list;
4862 limbo_die_list = limbo_node;
4865 return die;
4868 /* Return the DIE associated with the given type specifier. */
4870 static inline dw_die_ref
4871 lookup_type_die (tree type)
4873 return TYPE_SYMTAB_DIE (type);
4876 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
4877 anonymous type named by the typedef TYPE_DIE, return the DIE of the
4878 anonymous type instead the one of the naming typedef. */
4880 static inline dw_die_ref
4881 strip_naming_typedef (tree type, dw_die_ref type_die)
4883 if (type
4884 && TREE_CODE (type) == RECORD_TYPE
4885 && type_die
4886 && type_die->die_tag == DW_TAG_typedef
4887 && is_naming_typedef_decl (TYPE_NAME (type)))
4888 type_die = get_AT_ref (type_die, DW_AT_type);
4889 return type_die;
4892 /* Like lookup_type_die, but if type is an anonymous type named by a
4893 typedef[1], return the DIE of the anonymous type instead the one of
4894 the naming typedef. This is because in gen_typedef_die, we did
4895 equate the anonymous struct named by the typedef with the DIE of
4896 the naming typedef. So by default, lookup_type_die on an anonymous
4897 struct yields the DIE of the naming typedef.
4899 [1]: Read the comment of is_naming_typedef_decl to learn about what
4900 a naming typedef is. */
4902 static inline dw_die_ref
4903 lookup_type_die_strip_naming_typedef (tree type)
4905 dw_die_ref die = lookup_type_die (type);
4906 return strip_naming_typedef (type, die);
4909 /* Equate a DIE to a given type specifier. */
4911 static inline void
4912 equate_type_number_to_die (tree type, dw_die_ref type_die)
4914 TYPE_SYMTAB_DIE (type) = type_die;
4917 /* Returns a hash value for X (which really is a die_struct). */
4919 static hashval_t
4920 decl_die_table_hash (const void *x)
4922 return (hashval_t) ((const_dw_die_ref) x)->decl_id;
4925 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
4927 static int
4928 decl_die_table_eq (const void *x, const void *y)
4930 return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
4933 /* Return the DIE associated with a given declaration. */
4935 static inline dw_die_ref
4936 lookup_decl_die (tree decl)
4938 return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
4941 /* Returns a hash value for X (which really is a var_loc_list). */
4943 static hashval_t
4944 decl_loc_table_hash (const void *x)
4946 return (hashval_t) ((const var_loc_list *) x)->decl_id;
4949 /* Return nonzero if decl_id of var_loc_list X is the same as
4950 UID of decl *Y. */
4952 static int
4953 decl_loc_table_eq (const void *x, const void *y)
4955 return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
4958 /* Return the var_loc list associated with a given declaration. */
4960 static inline var_loc_list *
4961 lookup_decl_loc (const_tree decl)
4963 if (!decl_loc_table)
4964 return NULL;
4965 return (var_loc_list *)
4966 htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
4969 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
4971 static hashval_t
4972 cached_dw_loc_list_table_hash (const void *x)
4974 return (hashval_t) ((const cached_dw_loc_list *) x)->decl_id;
4977 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
4978 UID of decl *Y. */
4980 static int
4981 cached_dw_loc_list_table_eq (const void *x, const void *y)
4983 return (((const cached_dw_loc_list *) x)->decl_id
4984 == DECL_UID ((const_tree) y));
4987 /* Equate a DIE to a particular declaration. */
4989 static void
4990 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
4992 unsigned int decl_id = DECL_UID (decl);
4993 void **slot;
4995 slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
4996 *slot = decl_die;
4997 decl_die->decl_id = decl_id;
5000 /* Return how many bits covers PIECE EXPR_LIST. */
5002 static int
5003 decl_piece_bitsize (rtx piece)
5005 int ret = (int) GET_MODE (piece);
5006 if (ret)
5007 return ret;
5008 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
5009 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
5010 return INTVAL (XEXP (XEXP (piece, 0), 0));
5013 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
5015 static rtx *
5016 decl_piece_varloc_ptr (rtx piece)
5018 if ((int) GET_MODE (piece))
5019 return &XEXP (piece, 0);
5020 else
5021 return &XEXP (XEXP (piece, 0), 1);
5024 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
5025 Next is the chain of following piece nodes. */
5027 static rtx_expr_list *
5028 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
5030 if (bitsize <= (int) MAX_MACHINE_MODE)
5031 return alloc_EXPR_LIST (bitsize, loc_note, next);
5032 else
5033 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
5034 GEN_INT (bitsize),
5035 loc_note), next);
5038 /* Return rtx that should be stored into loc field for
5039 LOC_NOTE and BITPOS/BITSIZE. */
5041 static rtx
5042 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
5043 HOST_WIDE_INT bitsize)
5045 if (bitsize != -1)
5047 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
5048 if (bitpos != 0)
5049 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
5051 return loc_note;
5054 /* This function either modifies location piece list *DEST in
5055 place (if SRC and INNER is NULL), or copies location piece list
5056 *SRC to *DEST while modifying it. Location BITPOS is modified
5057 to contain LOC_NOTE, any pieces overlapping it are removed resp.
5058 not copied and if needed some padding around it is added.
5059 When modifying in place, DEST should point to EXPR_LIST where
5060 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
5061 to the start of the whole list and INNER points to the EXPR_LIST
5062 where earlier pieces cover PIECE_BITPOS bits. */
5064 static void
5065 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5066 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5067 HOST_WIDE_INT bitsize, rtx loc_note)
5069 int diff;
5070 bool copy = inner != NULL;
5072 if (copy)
5074 /* First copy all nodes preceding the current bitpos. */
5075 while (src != inner)
5077 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5078 decl_piece_bitsize (*src), NULL_RTX);
5079 dest = &XEXP (*dest, 1);
5080 src = &XEXP (*src, 1);
5083 /* Add padding if needed. */
5084 if (bitpos != piece_bitpos)
5086 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5087 copy ? NULL_RTX : *dest);
5088 dest = &XEXP (*dest, 1);
5090 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5092 gcc_assert (!copy);
5093 /* A piece with correct bitpos and bitsize already exist,
5094 just update the location for it and return. */
5095 *decl_piece_varloc_ptr (*dest) = loc_note;
5096 return;
5098 /* Add the piece that changed. */
5099 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5100 dest = &XEXP (*dest, 1);
5101 /* Skip over pieces that overlap it. */
5102 diff = bitpos - piece_bitpos + bitsize;
5103 if (!copy)
5104 src = dest;
5105 while (diff > 0 && *src)
5107 rtx piece = *src;
5108 diff -= decl_piece_bitsize (piece);
5109 if (copy)
5110 src = &XEXP (piece, 1);
5111 else
5113 *src = XEXP (piece, 1);
5114 free_EXPR_LIST_node (piece);
5117 /* Add padding if needed. */
5118 if (diff < 0 && *src)
5120 if (!copy)
5121 dest = src;
5122 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5123 dest = &XEXP (*dest, 1);
5125 if (!copy)
5126 return;
5127 /* Finally copy all nodes following it. */
5128 while (*src)
5130 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5131 decl_piece_bitsize (*src), NULL_RTX);
5132 dest = &XEXP (*dest, 1);
5133 src = &XEXP (*src, 1);
5137 /* Add a variable location node to the linked list for DECL. */
5139 static struct var_loc_node *
5140 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5142 unsigned int decl_id;
5143 var_loc_list *temp;
5144 void **slot;
5145 struct var_loc_node *loc = NULL;
5146 HOST_WIDE_INT bitsize = -1, bitpos = -1;
5148 if (TREE_CODE (decl) == VAR_DECL
5149 && DECL_HAS_DEBUG_EXPR_P (decl))
5151 tree realdecl = DECL_DEBUG_EXPR (decl);
5152 if (handled_component_p (realdecl)
5153 || (TREE_CODE (realdecl) == MEM_REF
5154 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5156 HOST_WIDE_INT maxsize;
5157 tree innerdecl;
5158 innerdecl
5159 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
5160 if (!DECL_P (innerdecl)
5161 || DECL_IGNORED_P (innerdecl)
5162 || TREE_STATIC (innerdecl)
5163 || bitsize <= 0
5164 || bitpos + bitsize > 256
5165 || bitsize != maxsize)
5166 return NULL;
5167 decl = innerdecl;
5171 decl_id = DECL_UID (decl);
5172 slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
5173 if (*slot == NULL)
5175 temp = ggc_cleared_alloc<var_loc_list> ();
5176 temp->decl_id = decl_id;
5177 *slot = temp;
5179 else
5180 temp = (var_loc_list *) *slot;
5182 /* For PARM_DECLs try to keep around the original incoming value,
5183 even if that means we'll emit a zero-range .debug_loc entry. */
5184 if (temp->last
5185 && temp->first == temp->last
5186 && TREE_CODE (decl) == PARM_DECL
5187 && NOTE_P (temp->first->loc)
5188 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5189 && DECL_INCOMING_RTL (decl)
5190 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5191 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5192 == GET_CODE (DECL_INCOMING_RTL (decl))
5193 && prev_real_insn (temp->first->loc) == NULL_RTX
5194 && (bitsize != -1
5195 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5196 NOTE_VAR_LOCATION_LOC (loc_note))
5197 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5198 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5200 loc = ggc_cleared_alloc<var_loc_node> ();
5201 temp->first->next = loc;
5202 temp->last = loc;
5203 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5205 else if (temp->last)
5207 struct var_loc_node *last = temp->last, *unused = NULL;
5208 rtx *piece_loc = NULL, last_loc_note;
5209 int piece_bitpos = 0;
5210 if (last->next)
5212 last = last->next;
5213 gcc_assert (last->next == NULL);
5215 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5217 piece_loc = &last->loc;
5220 int cur_bitsize = decl_piece_bitsize (*piece_loc);
5221 if (piece_bitpos + cur_bitsize > bitpos)
5222 break;
5223 piece_bitpos += cur_bitsize;
5224 piece_loc = &XEXP (*piece_loc, 1);
5226 while (*piece_loc);
5228 /* TEMP->LAST here is either pointer to the last but one or
5229 last element in the chained list, LAST is pointer to the
5230 last element. */
5231 if (label && strcmp (last->label, label) == 0)
5233 /* For SRA optimized variables if there weren't any real
5234 insns since last note, just modify the last node. */
5235 if (piece_loc != NULL)
5237 adjust_piece_list (piece_loc, NULL, NULL,
5238 bitpos, piece_bitpos, bitsize, loc_note);
5239 return NULL;
5241 /* If the last note doesn't cover any instructions, remove it. */
5242 if (temp->last != last)
5244 temp->last->next = NULL;
5245 unused = last;
5246 last = temp->last;
5247 gcc_assert (strcmp (last->label, label) != 0);
5249 else
5251 gcc_assert (temp->first == temp->last
5252 || (temp->first->next == temp->last
5253 && TREE_CODE (decl) == PARM_DECL));
5254 memset (temp->last, '\0', sizeof (*temp->last));
5255 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5256 return temp->last;
5259 if (bitsize == -1 && NOTE_P (last->loc))
5260 last_loc_note = last->loc;
5261 else if (piece_loc != NULL
5262 && *piece_loc != NULL_RTX
5263 && piece_bitpos == bitpos
5264 && decl_piece_bitsize (*piece_loc) == bitsize)
5265 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5266 else
5267 last_loc_note = NULL_RTX;
5268 /* If the current location is the same as the end of the list,
5269 and either both or neither of the locations is uninitialized,
5270 we have nothing to do. */
5271 if (last_loc_note == NULL_RTX
5272 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5273 NOTE_VAR_LOCATION_LOC (loc_note)))
5274 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5275 != NOTE_VAR_LOCATION_STATUS (loc_note))
5276 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5277 == VAR_INIT_STATUS_UNINITIALIZED)
5278 || (NOTE_VAR_LOCATION_STATUS (loc_note)
5279 == VAR_INIT_STATUS_UNINITIALIZED))))
5281 /* Add LOC to the end of list and update LAST. If the last
5282 element of the list has been removed above, reuse its
5283 memory for the new node, otherwise allocate a new one. */
5284 if (unused)
5286 loc = unused;
5287 memset (loc, '\0', sizeof (*loc));
5289 else
5290 loc = ggc_cleared_alloc<var_loc_node> ();
5291 if (bitsize == -1 || piece_loc == NULL)
5292 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5293 else
5294 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5295 bitpos, piece_bitpos, bitsize, loc_note);
5296 last->next = loc;
5297 /* Ensure TEMP->LAST will point either to the new last but one
5298 element of the chain, or to the last element in it. */
5299 if (last != temp->last)
5300 temp->last = last;
5302 else if (unused)
5303 ggc_free (unused);
5305 else
5307 loc = ggc_cleared_alloc<var_loc_node> ();
5308 temp->first = loc;
5309 temp->last = loc;
5310 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5312 return loc;
5315 /* Keep track of the number of spaces used to indent the
5316 output of the debugging routines that print the structure of
5317 the DIE internal representation. */
5318 static int print_indent;
5320 /* Indent the line the number of spaces given by print_indent. */
5322 static inline void
5323 print_spaces (FILE *outfile)
5325 fprintf (outfile, "%*s", print_indent, "");
5328 /* Print a type signature in hex. */
5330 static inline void
5331 print_signature (FILE *outfile, char *sig)
5333 int i;
5335 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5336 fprintf (outfile, "%02x", sig[i] & 0xff);
5339 /* Print the information associated with a given DIE, and its children.
5340 This routine is a debugging aid only. */
5342 static void
5343 print_die (dw_die_ref die, FILE *outfile)
5345 dw_attr_ref a;
5346 dw_die_ref c;
5347 unsigned ix;
5349 print_spaces (outfile);
5350 fprintf (outfile, "DIE %4ld: %s (%p)\n",
5351 die->die_offset, dwarf_tag_name (die->die_tag),
5352 (void*) die);
5353 print_spaces (outfile);
5354 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5355 fprintf (outfile, " offset: %ld", die->die_offset);
5356 fprintf (outfile, " mark: %d\n", die->die_mark);
5358 if (die->comdat_type_p)
5360 print_spaces (outfile);
5361 fprintf (outfile, " signature: ");
5362 print_signature (outfile, die->die_id.die_type_node->signature);
5363 fprintf (outfile, "\n");
5366 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5368 print_spaces (outfile);
5369 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5371 switch (AT_class (a))
5373 case dw_val_class_addr:
5374 fprintf (outfile, "address");
5375 break;
5376 case dw_val_class_offset:
5377 fprintf (outfile, "offset");
5378 break;
5379 case dw_val_class_loc:
5380 fprintf (outfile, "location descriptor");
5381 break;
5382 case dw_val_class_loc_list:
5383 fprintf (outfile, "location list -> label:%s",
5384 AT_loc_list (a)->ll_symbol);
5385 break;
5386 case dw_val_class_range_list:
5387 fprintf (outfile, "range list");
5388 break;
5389 case dw_val_class_const:
5390 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
5391 break;
5392 case dw_val_class_unsigned_const:
5393 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
5394 break;
5395 case dw_val_class_const_double:
5396 fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
5397 HOST_WIDE_INT_PRINT_UNSIGNED")",
5398 a->dw_attr_val.v.val_double.high,
5399 a->dw_attr_val.v.val_double.low);
5400 break;
5401 case dw_val_class_wide_int:
5403 int i = a->dw_attr_val.v.val_wide->get_len ();
5404 fprintf (outfile, "constant (");
5405 gcc_assert (i > 0);
5406 if (a->dw_attr_val.v.val_wide->elt (i - 1) == 0)
5407 fprintf (outfile, "0x");
5408 fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
5409 a->dw_attr_val.v.val_wide->elt (--i));
5410 while (--i >= 0)
5411 fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
5412 a->dw_attr_val.v.val_wide->elt (i));
5413 fprintf (outfile, ")");
5414 break;
5416 case dw_val_class_vec:
5417 fprintf (outfile, "floating-point or vector constant");
5418 break;
5419 case dw_val_class_flag:
5420 fprintf (outfile, "%u", AT_flag (a));
5421 break;
5422 case dw_val_class_die_ref:
5423 if (AT_ref (a) != NULL)
5425 if (AT_ref (a)->comdat_type_p)
5427 fprintf (outfile, "die -> signature: ");
5428 print_signature (outfile,
5429 AT_ref (a)->die_id.die_type_node->signature);
5431 else if (AT_ref (a)->die_id.die_symbol)
5432 fprintf (outfile, "die -> label: %s",
5433 AT_ref (a)->die_id.die_symbol);
5434 else
5435 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
5436 fprintf (outfile, " (%p)", (void *) AT_ref (a));
5438 else
5439 fprintf (outfile, "die -> <null>");
5440 break;
5441 case dw_val_class_vms_delta:
5442 fprintf (outfile, "delta: @slotcount(%s-%s)",
5443 AT_vms_delta2 (a), AT_vms_delta1 (a));
5444 break;
5445 case dw_val_class_lbl_id:
5446 case dw_val_class_lineptr:
5447 case dw_val_class_macptr:
5448 case dw_val_class_high_pc:
5449 fprintf (outfile, "label: %s", AT_lbl (a));
5450 break;
5451 case dw_val_class_str:
5452 if (AT_string (a) != NULL)
5453 fprintf (outfile, "\"%s\"", AT_string (a));
5454 else
5455 fprintf (outfile, "<null>");
5456 break;
5457 case dw_val_class_file:
5458 fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
5459 AT_file (a)->emitted_number);
5460 break;
5461 case dw_val_class_data8:
5463 int i;
5465 for (i = 0; i < 8; i++)
5466 fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
5467 break;
5469 default:
5470 break;
5473 fprintf (outfile, "\n");
5476 if (die->die_child != NULL)
5478 print_indent += 4;
5479 FOR_EACH_CHILD (die, c, print_die (c, outfile));
5480 print_indent -= 4;
5482 if (print_indent == 0)
5483 fprintf (outfile, "\n");
5486 /* Print the information collected for a given DIE. */
5488 DEBUG_FUNCTION void
5489 debug_dwarf_die (dw_die_ref die)
5491 print_die (die, stderr);
5494 DEBUG_FUNCTION void
5495 debug (die_struct &ref)
5497 print_die (&ref, stderr);
5500 DEBUG_FUNCTION void
5501 debug (die_struct *ptr)
5503 if (ptr)
5504 debug (*ptr);
5505 else
5506 fprintf (stderr, "<nil>\n");
5510 /* Print all DWARF information collected for the compilation unit.
5511 This routine is a debugging aid only. */
5513 DEBUG_FUNCTION void
5514 debug_dwarf (void)
5516 print_indent = 0;
5517 print_die (comp_unit_die (), stderr);
5520 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
5521 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
5522 DIE that marks the start of the DIEs for this include file. */
5524 static dw_die_ref
5525 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5527 const char *filename = get_AT_string (bincl_die, DW_AT_name);
5528 dw_die_ref new_unit = gen_compile_unit_die (filename);
5530 new_unit->die_sib = old_unit;
5531 return new_unit;
5534 /* Close an include-file CU and reopen the enclosing one. */
5536 static dw_die_ref
5537 pop_compile_unit (dw_die_ref old_unit)
5539 dw_die_ref new_unit = old_unit->die_sib;
5541 old_unit->die_sib = NULL;
5542 return new_unit;
5545 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5546 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5547 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5549 /* Calculate the checksum of a location expression. */
5551 static inline void
5552 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5554 int tem;
5555 inchash::hash hstate;
5556 hashval_t hash;
5558 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
5559 CHECKSUM (tem);
5560 hash_loc_operands (loc, hstate);
5561 hash = hstate.end();
5562 CHECKSUM (hash);
5565 /* Calculate the checksum of an attribute. */
5567 static void
5568 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5570 dw_loc_descr_ref loc;
5571 rtx r;
5573 CHECKSUM (at->dw_attr);
5575 /* We don't care that this was compiled with a different compiler
5576 snapshot; if the output is the same, that's what matters. */
5577 if (at->dw_attr == DW_AT_producer)
5578 return;
5580 switch (AT_class (at))
5582 case dw_val_class_const:
5583 CHECKSUM (at->dw_attr_val.v.val_int);
5584 break;
5585 case dw_val_class_unsigned_const:
5586 CHECKSUM (at->dw_attr_val.v.val_unsigned);
5587 break;
5588 case dw_val_class_const_double:
5589 CHECKSUM (at->dw_attr_val.v.val_double);
5590 break;
5591 case dw_val_class_wide_int:
5592 CHECKSUM (*at->dw_attr_val.v.val_wide);
5593 break;
5594 case dw_val_class_vec:
5595 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5596 (at->dw_attr_val.v.val_vec.length
5597 * at->dw_attr_val.v.val_vec.elt_size));
5598 break;
5599 case dw_val_class_flag:
5600 CHECKSUM (at->dw_attr_val.v.val_flag);
5601 break;
5602 case dw_val_class_str:
5603 CHECKSUM_STRING (AT_string (at));
5604 break;
5606 case dw_val_class_addr:
5607 r = AT_addr (at);
5608 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5609 CHECKSUM_STRING (XSTR (r, 0));
5610 break;
5612 case dw_val_class_offset:
5613 CHECKSUM (at->dw_attr_val.v.val_offset);
5614 break;
5616 case dw_val_class_loc:
5617 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5618 loc_checksum (loc, ctx);
5619 break;
5621 case dw_val_class_die_ref:
5622 die_checksum (AT_ref (at), ctx, mark);
5623 break;
5625 case dw_val_class_fde_ref:
5626 case dw_val_class_vms_delta:
5627 case dw_val_class_lbl_id:
5628 case dw_val_class_lineptr:
5629 case dw_val_class_macptr:
5630 case dw_val_class_high_pc:
5631 break;
5633 case dw_val_class_file:
5634 CHECKSUM_STRING (AT_file (at)->filename);
5635 break;
5637 case dw_val_class_data8:
5638 CHECKSUM (at->dw_attr_val.v.val_data8);
5639 break;
5641 default:
5642 break;
5646 /* Calculate the checksum of a DIE. */
5648 static void
5649 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5651 dw_die_ref c;
5652 dw_attr_ref a;
5653 unsigned ix;
5655 /* To avoid infinite recursion. */
5656 if (die->die_mark)
5658 CHECKSUM (die->die_mark);
5659 return;
5661 die->die_mark = ++(*mark);
5663 CHECKSUM (die->die_tag);
5665 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5666 attr_checksum (a, ctx, mark);
5668 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
5671 #undef CHECKSUM
5672 #undef CHECKSUM_BLOCK
5673 #undef CHECKSUM_STRING
5675 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
5676 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5677 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5678 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
5679 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
5680 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
5681 #define CHECKSUM_ATTR(FOO) \
5682 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
5684 /* Calculate the checksum of a number in signed LEB128 format. */
5686 static void
5687 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
5689 unsigned char byte;
5690 bool more;
5692 while (1)
5694 byte = (value & 0x7f);
5695 value >>= 7;
5696 more = !((value == 0 && (byte & 0x40) == 0)
5697 || (value == -1 && (byte & 0x40) != 0));
5698 if (more)
5699 byte |= 0x80;
5700 CHECKSUM (byte);
5701 if (!more)
5702 break;
5706 /* Calculate the checksum of a number in unsigned LEB128 format. */
5708 static void
5709 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
5711 while (1)
5713 unsigned char byte = (value & 0x7f);
5714 value >>= 7;
5715 if (value != 0)
5716 /* More bytes to follow. */
5717 byte |= 0x80;
5718 CHECKSUM (byte);
5719 if (value == 0)
5720 break;
5724 /* Checksum the context of the DIE. This adds the names of any
5725 surrounding namespaces or structures to the checksum. */
5727 static void
5728 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
5730 const char *name;
5731 dw_die_ref spec;
5732 int tag = die->die_tag;
5734 if (tag != DW_TAG_namespace
5735 && tag != DW_TAG_structure_type
5736 && tag != DW_TAG_class_type)
5737 return;
5739 name = get_AT_string (die, DW_AT_name);
5741 spec = get_AT_ref (die, DW_AT_specification);
5742 if (spec != NULL)
5743 die = spec;
5745 if (die->die_parent != NULL)
5746 checksum_die_context (die->die_parent, ctx);
5748 CHECKSUM_ULEB128 ('C');
5749 CHECKSUM_ULEB128 (tag);
5750 if (name != NULL)
5751 CHECKSUM_STRING (name);
5754 /* Calculate the checksum of a location expression. */
5756 static inline void
5757 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5759 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
5760 were emitted as a DW_FORM_sdata instead of a location expression. */
5761 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
5763 CHECKSUM_ULEB128 (DW_FORM_sdata);
5764 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
5765 return;
5768 /* Otherwise, just checksum the raw location expression. */
5769 while (loc != NULL)
5771 inchash::hash hstate;
5772 hashval_t hash;
5774 CHECKSUM_ULEB128 (loc->dtprel);
5775 CHECKSUM_ULEB128 (loc->dw_loc_opc);
5776 hash_loc_operands (loc, hstate);
5777 hash = hstate.end ();
5778 CHECKSUM (hash);
5779 loc = loc->dw_loc_next;
5783 /* Calculate the checksum of an attribute. */
5785 static void
5786 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
5787 struct md5_ctx *ctx, int *mark)
5789 dw_loc_descr_ref loc;
5790 rtx r;
5792 if (AT_class (at) == dw_val_class_die_ref)
5794 dw_die_ref target_die = AT_ref (at);
5796 /* For pointer and reference types, we checksum only the (qualified)
5797 name of the target type (if there is a name). For friend entries,
5798 we checksum only the (qualified) name of the target type or function.
5799 This allows the checksum to remain the same whether the target type
5800 is complete or not. */
5801 if ((at->dw_attr == DW_AT_type
5802 && (tag == DW_TAG_pointer_type
5803 || tag == DW_TAG_reference_type
5804 || tag == DW_TAG_rvalue_reference_type
5805 || tag == DW_TAG_ptr_to_member_type))
5806 || (at->dw_attr == DW_AT_friend
5807 && tag == DW_TAG_friend))
5809 dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
5811 if (name_attr != NULL)
5813 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5815 if (decl == NULL)
5816 decl = target_die;
5817 CHECKSUM_ULEB128 ('N');
5818 CHECKSUM_ULEB128 (at->dw_attr);
5819 if (decl->die_parent != NULL)
5820 checksum_die_context (decl->die_parent, ctx);
5821 CHECKSUM_ULEB128 ('E');
5822 CHECKSUM_STRING (AT_string (name_attr));
5823 return;
5827 /* For all other references to another DIE, we check to see if the
5828 target DIE has already been visited. If it has, we emit a
5829 backward reference; if not, we descend recursively. */
5830 if (target_die->die_mark > 0)
5832 CHECKSUM_ULEB128 ('R');
5833 CHECKSUM_ULEB128 (at->dw_attr);
5834 CHECKSUM_ULEB128 (target_die->die_mark);
5836 else
5838 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5840 if (decl == NULL)
5841 decl = target_die;
5842 target_die->die_mark = ++(*mark);
5843 CHECKSUM_ULEB128 ('T');
5844 CHECKSUM_ULEB128 (at->dw_attr);
5845 if (decl->die_parent != NULL)
5846 checksum_die_context (decl->die_parent, ctx);
5847 die_checksum_ordered (target_die, ctx, mark);
5849 return;
5852 CHECKSUM_ULEB128 ('A');
5853 CHECKSUM_ULEB128 (at->dw_attr);
5855 switch (AT_class (at))
5857 case dw_val_class_const:
5858 CHECKSUM_ULEB128 (DW_FORM_sdata);
5859 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
5860 break;
5862 case dw_val_class_unsigned_const:
5863 CHECKSUM_ULEB128 (DW_FORM_sdata);
5864 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
5865 break;
5867 case dw_val_class_const_double:
5868 CHECKSUM_ULEB128 (DW_FORM_block);
5869 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
5870 CHECKSUM (at->dw_attr_val.v.val_double);
5871 break;
5873 case dw_val_class_wide_int:
5874 CHECKSUM_ULEB128 (DW_FORM_block);
5875 CHECKSUM_ULEB128 (sizeof (*at->dw_attr_val.v.val_wide));
5876 CHECKSUM (*at->dw_attr_val.v.val_wide);
5877 break;
5879 case dw_val_class_vec:
5880 CHECKSUM_ULEB128 (DW_FORM_block);
5881 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
5882 * at->dw_attr_val.v.val_vec.elt_size);
5883 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5884 (at->dw_attr_val.v.val_vec.length
5885 * at->dw_attr_val.v.val_vec.elt_size));
5886 break;
5888 case dw_val_class_flag:
5889 CHECKSUM_ULEB128 (DW_FORM_flag);
5890 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
5891 break;
5893 case dw_val_class_str:
5894 CHECKSUM_ULEB128 (DW_FORM_string);
5895 CHECKSUM_STRING (AT_string (at));
5896 break;
5898 case dw_val_class_addr:
5899 r = AT_addr (at);
5900 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5901 CHECKSUM_ULEB128 (DW_FORM_string);
5902 CHECKSUM_STRING (XSTR (r, 0));
5903 break;
5905 case dw_val_class_offset:
5906 CHECKSUM_ULEB128 (DW_FORM_sdata);
5907 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
5908 break;
5910 case dw_val_class_loc:
5911 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5912 loc_checksum_ordered (loc, ctx);
5913 break;
5915 case dw_val_class_fde_ref:
5916 case dw_val_class_lbl_id:
5917 case dw_val_class_lineptr:
5918 case dw_val_class_macptr:
5919 case dw_val_class_high_pc:
5920 break;
5922 case dw_val_class_file:
5923 CHECKSUM_ULEB128 (DW_FORM_string);
5924 CHECKSUM_STRING (AT_file (at)->filename);
5925 break;
5927 case dw_val_class_data8:
5928 CHECKSUM (at->dw_attr_val.v.val_data8);
5929 break;
5931 default:
5932 break;
5936 struct checksum_attributes
5938 dw_attr_ref at_name;
5939 dw_attr_ref at_type;
5940 dw_attr_ref at_friend;
5941 dw_attr_ref at_accessibility;
5942 dw_attr_ref at_address_class;
5943 dw_attr_ref at_allocated;
5944 dw_attr_ref at_artificial;
5945 dw_attr_ref at_associated;
5946 dw_attr_ref at_binary_scale;
5947 dw_attr_ref at_bit_offset;
5948 dw_attr_ref at_bit_size;
5949 dw_attr_ref at_bit_stride;
5950 dw_attr_ref at_byte_size;
5951 dw_attr_ref at_byte_stride;
5952 dw_attr_ref at_const_value;
5953 dw_attr_ref at_containing_type;
5954 dw_attr_ref at_count;
5955 dw_attr_ref at_data_location;
5956 dw_attr_ref at_data_member_location;
5957 dw_attr_ref at_decimal_scale;
5958 dw_attr_ref at_decimal_sign;
5959 dw_attr_ref at_default_value;
5960 dw_attr_ref at_digit_count;
5961 dw_attr_ref at_discr;
5962 dw_attr_ref at_discr_list;
5963 dw_attr_ref at_discr_value;
5964 dw_attr_ref at_encoding;
5965 dw_attr_ref at_endianity;
5966 dw_attr_ref at_explicit;
5967 dw_attr_ref at_is_optional;
5968 dw_attr_ref at_location;
5969 dw_attr_ref at_lower_bound;
5970 dw_attr_ref at_mutable;
5971 dw_attr_ref at_ordering;
5972 dw_attr_ref at_picture_string;
5973 dw_attr_ref at_prototyped;
5974 dw_attr_ref at_small;
5975 dw_attr_ref at_segment;
5976 dw_attr_ref at_string_length;
5977 dw_attr_ref at_threads_scaled;
5978 dw_attr_ref at_upper_bound;
5979 dw_attr_ref at_use_location;
5980 dw_attr_ref at_use_UTF8;
5981 dw_attr_ref at_variable_parameter;
5982 dw_attr_ref at_virtuality;
5983 dw_attr_ref at_visibility;
5984 dw_attr_ref at_vtable_elem_location;
5987 /* Collect the attributes that we will want to use for the checksum. */
5989 static void
5990 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
5992 dw_attr_ref a;
5993 unsigned ix;
5995 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5997 switch (a->dw_attr)
5999 case DW_AT_name:
6000 attrs->at_name = a;
6001 break;
6002 case DW_AT_type:
6003 attrs->at_type = a;
6004 break;
6005 case DW_AT_friend:
6006 attrs->at_friend = a;
6007 break;
6008 case DW_AT_accessibility:
6009 attrs->at_accessibility = a;
6010 break;
6011 case DW_AT_address_class:
6012 attrs->at_address_class = a;
6013 break;
6014 case DW_AT_allocated:
6015 attrs->at_allocated = a;
6016 break;
6017 case DW_AT_artificial:
6018 attrs->at_artificial = a;
6019 break;
6020 case DW_AT_associated:
6021 attrs->at_associated = a;
6022 break;
6023 case DW_AT_binary_scale:
6024 attrs->at_binary_scale = a;
6025 break;
6026 case DW_AT_bit_offset:
6027 attrs->at_bit_offset = a;
6028 break;
6029 case DW_AT_bit_size:
6030 attrs->at_bit_size = a;
6031 break;
6032 case DW_AT_bit_stride:
6033 attrs->at_bit_stride = a;
6034 break;
6035 case DW_AT_byte_size:
6036 attrs->at_byte_size = a;
6037 break;
6038 case DW_AT_byte_stride:
6039 attrs->at_byte_stride = a;
6040 break;
6041 case DW_AT_const_value:
6042 attrs->at_const_value = a;
6043 break;
6044 case DW_AT_containing_type:
6045 attrs->at_containing_type = a;
6046 break;
6047 case DW_AT_count:
6048 attrs->at_count = a;
6049 break;
6050 case DW_AT_data_location:
6051 attrs->at_data_location = a;
6052 break;
6053 case DW_AT_data_member_location:
6054 attrs->at_data_member_location = a;
6055 break;
6056 case DW_AT_decimal_scale:
6057 attrs->at_decimal_scale = a;
6058 break;
6059 case DW_AT_decimal_sign:
6060 attrs->at_decimal_sign = a;
6061 break;
6062 case DW_AT_default_value:
6063 attrs->at_default_value = a;
6064 break;
6065 case DW_AT_digit_count:
6066 attrs->at_digit_count = a;
6067 break;
6068 case DW_AT_discr:
6069 attrs->at_discr = a;
6070 break;
6071 case DW_AT_discr_list:
6072 attrs->at_discr_list = a;
6073 break;
6074 case DW_AT_discr_value:
6075 attrs->at_discr_value = a;
6076 break;
6077 case DW_AT_encoding:
6078 attrs->at_encoding = a;
6079 break;
6080 case DW_AT_endianity:
6081 attrs->at_endianity = a;
6082 break;
6083 case DW_AT_explicit:
6084 attrs->at_explicit = a;
6085 break;
6086 case DW_AT_is_optional:
6087 attrs->at_is_optional = a;
6088 break;
6089 case DW_AT_location:
6090 attrs->at_location = a;
6091 break;
6092 case DW_AT_lower_bound:
6093 attrs->at_lower_bound = a;
6094 break;
6095 case DW_AT_mutable:
6096 attrs->at_mutable = a;
6097 break;
6098 case DW_AT_ordering:
6099 attrs->at_ordering = a;
6100 break;
6101 case DW_AT_picture_string:
6102 attrs->at_picture_string = a;
6103 break;
6104 case DW_AT_prototyped:
6105 attrs->at_prototyped = a;
6106 break;
6107 case DW_AT_small:
6108 attrs->at_small = a;
6109 break;
6110 case DW_AT_segment:
6111 attrs->at_segment = a;
6112 break;
6113 case DW_AT_string_length:
6114 attrs->at_string_length = a;
6115 break;
6116 case DW_AT_threads_scaled:
6117 attrs->at_threads_scaled = a;
6118 break;
6119 case DW_AT_upper_bound:
6120 attrs->at_upper_bound = a;
6121 break;
6122 case DW_AT_use_location:
6123 attrs->at_use_location = a;
6124 break;
6125 case DW_AT_use_UTF8:
6126 attrs->at_use_UTF8 = a;
6127 break;
6128 case DW_AT_variable_parameter:
6129 attrs->at_variable_parameter = a;
6130 break;
6131 case DW_AT_virtuality:
6132 attrs->at_virtuality = a;
6133 break;
6134 case DW_AT_visibility:
6135 attrs->at_visibility = a;
6136 break;
6137 case DW_AT_vtable_elem_location:
6138 attrs->at_vtable_elem_location = a;
6139 break;
6140 default:
6141 break;
6146 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
6148 static void
6149 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6151 dw_die_ref c;
6152 dw_die_ref decl;
6153 struct checksum_attributes attrs;
6155 CHECKSUM_ULEB128 ('D');
6156 CHECKSUM_ULEB128 (die->die_tag);
6158 memset (&attrs, 0, sizeof (attrs));
6160 decl = get_AT_ref (die, DW_AT_specification);
6161 if (decl != NULL)
6162 collect_checksum_attributes (&attrs, decl);
6163 collect_checksum_attributes (&attrs, die);
6165 CHECKSUM_ATTR (attrs.at_name);
6166 CHECKSUM_ATTR (attrs.at_accessibility);
6167 CHECKSUM_ATTR (attrs.at_address_class);
6168 CHECKSUM_ATTR (attrs.at_allocated);
6169 CHECKSUM_ATTR (attrs.at_artificial);
6170 CHECKSUM_ATTR (attrs.at_associated);
6171 CHECKSUM_ATTR (attrs.at_binary_scale);
6172 CHECKSUM_ATTR (attrs.at_bit_offset);
6173 CHECKSUM_ATTR (attrs.at_bit_size);
6174 CHECKSUM_ATTR (attrs.at_bit_stride);
6175 CHECKSUM_ATTR (attrs.at_byte_size);
6176 CHECKSUM_ATTR (attrs.at_byte_stride);
6177 CHECKSUM_ATTR (attrs.at_const_value);
6178 CHECKSUM_ATTR (attrs.at_containing_type);
6179 CHECKSUM_ATTR (attrs.at_count);
6180 CHECKSUM_ATTR (attrs.at_data_location);
6181 CHECKSUM_ATTR (attrs.at_data_member_location);
6182 CHECKSUM_ATTR (attrs.at_decimal_scale);
6183 CHECKSUM_ATTR (attrs.at_decimal_sign);
6184 CHECKSUM_ATTR (attrs.at_default_value);
6185 CHECKSUM_ATTR (attrs.at_digit_count);
6186 CHECKSUM_ATTR (attrs.at_discr);
6187 CHECKSUM_ATTR (attrs.at_discr_list);
6188 CHECKSUM_ATTR (attrs.at_discr_value);
6189 CHECKSUM_ATTR (attrs.at_encoding);
6190 CHECKSUM_ATTR (attrs.at_endianity);
6191 CHECKSUM_ATTR (attrs.at_explicit);
6192 CHECKSUM_ATTR (attrs.at_is_optional);
6193 CHECKSUM_ATTR (attrs.at_location);
6194 CHECKSUM_ATTR (attrs.at_lower_bound);
6195 CHECKSUM_ATTR (attrs.at_mutable);
6196 CHECKSUM_ATTR (attrs.at_ordering);
6197 CHECKSUM_ATTR (attrs.at_picture_string);
6198 CHECKSUM_ATTR (attrs.at_prototyped);
6199 CHECKSUM_ATTR (attrs.at_small);
6200 CHECKSUM_ATTR (attrs.at_segment);
6201 CHECKSUM_ATTR (attrs.at_string_length);
6202 CHECKSUM_ATTR (attrs.at_threads_scaled);
6203 CHECKSUM_ATTR (attrs.at_upper_bound);
6204 CHECKSUM_ATTR (attrs.at_use_location);
6205 CHECKSUM_ATTR (attrs.at_use_UTF8);
6206 CHECKSUM_ATTR (attrs.at_variable_parameter);
6207 CHECKSUM_ATTR (attrs.at_virtuality);
6208 CHECKSUM_ATTR (attrs.at_visibility);
6209 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6210 CHECKSUM_ATTR (attrs.at_type);
6211 CHECKSUM_ATTR (attrs.at_friend);
6213 /* Checksum the child DIEs. */
6214 c = die->die_child;
6215 if (c) do {
6216 dw_attr_ref name_attr;
6218 c = c->die_sib;
6219 name_attr = get_AT (c, DW_AT_name);
6220 if (is_template_instantiation (c))
6222 /* Ignore instantiations of member type and function templates. */
6224 else if (name_attr != NULL
6225 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
6227 /* Use a shallow checksum for named nested types and member
6228 functions. */
6229 CHECKSUM_ULEB128 ('S');
6230 CHECKSUM_ULEB128 (c->die_tag);
6231 CHECKSUM_STRING (AT_string (name_attr));
6233 else
6235 /* Use a deep checksum for other children. */
6236 /* Mark this DIE so it gets processed when unmarking. */
6237 if (c->die_mark == 0)
6238 c->die_mark = -1;
6239 die_checksum_ordered (c, ctx, mark);
6241 } while (c != die->die_child);
6243 CHECKSUM_ULEB128 (0);
6246 /* Add a type name and tag to a hash. */
6247 static void
6248 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
6250 CHECKSUM_ULEB128 (tag);
6251 CHECKSUM_STRING (name);
6254 #undef CHECKSUM
6255 #undef CHECKSUM_STRING
6256 #undef CHECKSUM_ATTR
6257 #undef CHECKSUM_LEB128
6258 #undef CHECKSUM_ULEB128
6260 /* Generate the type signature for DIE. This is computed by generating an
6261 MD5 checksum over the DIE's tag, its relevant attributes, and its
6262 children. Attributes that are references to other DIEs are processed
6263 by recursion, using the MARK field to prevent infinite recursion.
6264 If the DIE is nested inside a namespace or another type, we also
6265 need to include that context in the signature. The lower 64 bits
6266 of the resulting MD5 checksum comprise the signature. */
6268 static void
6269 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6271 int mark;
6272 const char *name;
6273 unsigned char checksum[16];
6274 struct md5_ctx ctx;
6275 dw_die_ref decl;
6276 dw_die_ref parent;
6278 name = get_AT_string (die, DW_AT_name);
6279 decl = get_AT_ref (die, DW_AT_specification);
6280 parent = get_die_parent (die);
6282 /* First, compute a signature for just the type name (and its surrounding
6283 context, if any. This is stored in the type unit DIE for link-time
6284 ODR (one-definition rule) checking. */
6286 if (is_cxx () && name != NULL)
6288 md5_init_ctx (&ctx);
6290 /* Checksum the names of surrounding namespaces and structures. */
6291 if (parent != NULL)
6292 checksum_die_context (parent, &ctx);
6294 /* Checksum the current DIE. */
6295 die_odr_checksum (die->die_tag, name, &ctx);
6296 md5_finish_ctx (&ctx, checksum);
6298 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6301 /* Next, compute the complete type signature. */
6303 md5_init_ctx (&ctx);
6304 mark = 1;
6305 die->die_mark = mark;
6307 /* Checksum the names of surrounding namespaces and structures. */
6308 if (parent != NULL)
6309 checksum_die_context (parent, &ctx);
6311 /* Checksum the DIE and its children. */
6312 die_checksum_ordered (die, &ctx, &mark);
6313 unmark_all_dies (die);
6314 md5_finish_ctx (&ctx, checksum);
6316 /* Store the signature in the type node and link the type DIE and the
6317 type node together. */
6318 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6319 DWARF_TYPE_SIGNATURE_SIZE);
6320 die->comdat_type_p = true;
6321 die->die_id.die_type_node = type_node;
6322 type_node->type_die = die;
6324 /* If the DIE is a specification, link its declaration to the type node
6325 as well. */
6326 if (decl != NULL)
6328 decl->comdat_type_p = true;
6329 decl->die_id.die_type_node = type_node;
6333 /* Do the location expressions look same? */
6334 static inline int
6335 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6337 return loc1->dw_loc_opc == loc2->dw_loc_opc
6338 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6339 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6342 /* Do the values look the same? */
6343 static int
6344 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6346 dw_loc_descr_ref loc1, loc2;
6347 rtx r1, r2;
6349 if (v1->val_class != v2->val_class)
6350 return 0;
6352 switch (v1->val_class)
6354 case dw_val_class_const:
6355 return v1->v.val_int == v2->v.val_int;
6356 case dw_val_class_unsigned_const:
6357 return v1->v.val_unsigned == v2->v.val_unsigned;
6358 case dw_val_class_const_double:
6359 return v1->v.val_double.high == v2->v.val_double.high
6360 && v1->v.val_double.low == v2->v.val_double.low;
6361 case dw_val_class_wide_int:
6362 return *v1->v.val_wide == *v2->v.val_wide;
6363 case dw_val_class_vec:
6364 if (v1->v.val_vec.length != v2->v.val_vec.length
6365 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6366 return 0;
6367 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6368 v1->v.val_vec.length * v1->v.val_vec.elt_size))
6369 return 0;
6370 return 1;
6371 case dw_val_class_flag:
6372 return v1->v.val_flag == v2->v.val_flag;
6373 case dw_val_class_str:
6374 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
6376 case dw_val_class_addr:
6377 r1 = v1->v.val_addr;
6378 r2 = v2->v.val_addr;
6379 if (GET_CODE (r1) != GET_CODE (r2))
6380 return 0;
6381 return !rtx_equal_p (r1, r2);
6383 case dw_val_class_offset:
6384 return v1->v.val_offset == v2->v.val_offset;
6386 case dw_val_class_loc:
6387 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6388 loc1 && loc2;
6389 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6390 if (!same_loc_p (loc1, loc2, mark))
6391 return 0;
6392 return !loc1 && !loc2;
6394 case dw_val_class_die_ref:
6395 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6397 case dw_val_class_fde_ref:
6398 case dw_val_class_vms_delta:
6399 case dw_val_class_lbl_id:
6400 case dw_val_class_lineptr:
6401 case dw_val_class_macptr:
6402 case dw_val_class_high_pc:
6403 return 1;
6405 case dw_val_class_file:
6406 return v1->v.val_file == v2->v.val_file;
6408 case dw_val_class_data8:
6409 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
6411 default:
6412 return 1;
6416 /* Do the attributes look the same? */
6418 static int
6419 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6421 if (at1->dw_attr != at2->dw_attr)
6422 return 0;
6424 /* We don't care that this was compiled with a different compiler
6425 snapshot; if the output is the same, that's what matters. */
6426 if (at1->dw_attr == DW_AT_producer)
6427 return 1;
6429 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6432 /* Do the dies look the same? */
6434 static int
6435 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6437 dw_die_ref c1, c2;
6438 dw_attr_ref a1;
6439 unsigned ix;
6441 /* To avoid infinite recursion. */
6442 if (die1->die_mark)
6443 return die1->die_mark == die2->die_mark;
6444 die1->die_mark = die2->die_mark = ++(*mark);
6446 if (die1->die_tag != die2->die_tag)
6447 return 0;
6449 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
6450 return 0;
6452 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
6453 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
6454 return 0;
6456 c1 = die1->die_child;
6457 c2 = die2->die_child;
6458 if (! c1)
6460 if (c2)
6461 return 0;
6463 else
6464 for (;;)
6466 if (!same_die_p (c1, c2, mark))
6467 return 0;
6468 c1 = c1->die_sib;
6469 c2 = c2->die_sib;
6470 if (c1 == die1->die_child)
6472 if (c2 == die2->die_child)
6473 break;
6474 else
6475 return 0;
6479 return 1;
6482 /* Do the dies look the same? Wrapper around same_die_p. */
6484 static int
6485 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6487 int mark = 0;
6488 int ret = same_die_p (die1, die2, &mark);
6490 unmark_all_dies (die1);
6491 unmark_all_dies (die2);
6493 return ret;
6496 /* The prefix to attach to symbols on DIEs in the current comdat debug
6497 info section. */
6498 static const char *comdat_symbol_id;
6500 /* The index of the current symbol within the current comdat CU. */
6501 static unsigned int comdat_symbol_number;
6503 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6504 children, and set comdat_symbol_id accordingly. */
6506 static void
6507 compute_section_prefix (dw_die_ref unit_die)
6509 const char *die_name = get_AT_string (unit_die, DW_AT_name);
6510 const char *base = die_name ? lbasename (die_name) : "anonymous";
6511 char *name = XALLOCAVEC (char, strlen (base) + 64);
6512 char *p;
6513 int i, mark;
6514 unsigned char checksum[16];
6515 struct md5_ctx ctx;
6517 /* Compute the checksum of the DIE, then append part of it as hex digits to
6518 the name filename of the unit. */
6520 md5_init_ctx (&ctx);
6521 mark = 0;
6522 die_checksum (unit_die, &ctx, &mark);
6523 unmark_all_dies (unit_die);
6524 md5_finish_ctx (&ctx, checksum);
6526 sprintf (name, "%s.", base);
6527 clean_symbol_name (name);
6529 p = name + strlen (name);
6530 for (i = 0; i < 4; i++)
6532 sprintf (p, "%.2x", checksum[i]);
6533 p += 2;
6536 comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
6537 comdat_symbol_number = 0;
6540 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
6542 static int
6543 is_type_die (dw_die_ref die)
6545 switch (die->die_tag)
6547 case DW_TAG_array_type:
6548 case DW_TAG_class_type:
6549 case DW_TAG_interface_type:
6550 case DW_TAG_enumeration_type:
6551 case DW_TAG_pointer_type:
6552 case DW_TAG_reference_type:
6553 case DW_TAG_rvalue_reference_type:
6554 case DW_TAG_string_type:
6555 case DW_TAG_structure_type:
6556 case DW_TAG_subroutine_type:
6557 case DW_TAG_union_type:
6558 case DW_TAG_ptr_to_member_type:
6559 case DW_TAG_set_type:
6560 case DW_TAG_subrange_type:
6561 case DW_TAG_base_type:
6562 case DW_TAG_const_type:
6563 case DW_TAG_file_type:
6564 case DW_TAG_packed_type:
6565 case DW_TAG_volatile_type:
6566 case DW_TAG_typedef:
6567 return 1;
6568 default:
6569 return 0;
6573 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6574 Basically, we want to choose the bits that are likely to be shared between
6575 compilations (types) and leave out the bits that are specific to individual
6576 compilations (functions). */
6578 static int
6579 is_comdat_die (dw_die_ref c)
6581 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6582 we do for stabs. The advantage is a greater likelihood of sharing between
6583 objects that don't include headers in the same order (and therefore would
6584 put the base types in a different comdat). jason 8/28/00 */
6586 if (c->die_tag == DW_TAG_base_type)
6587 return 0;
6589 if (c->die_tag == DW_TAG_pointer_type
6590 || c->die_tag == DW_TAG_reference_type
6591 || c->die_tag == DW_TAG_rvalue_reference_type
6592 || c->die_tag == DW_TAG_const_type
6593 || c->die_tag == DW_TAG_volatile_type)
6595 dw_die_ref t = get_AT_ref (c, DW_AT_type);
6597 return t ? is_comdat_die (t) : 0;
6600 return is_type_die (c);
6603 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6604 compilation unit. */
6606 static int
6607 is_symbol_die (dw_die_ref c)
6609 return (is_type_die (c)
6610 || is_declaration_die (c)
6611 || c->die_tag == DW_TAG_namespace
6612 || c->die_tag == DW_TAG_module);
6615 /* Returns true iff C is a compile-unit DIE. */
6617 static inline bool
6618 is_cu_die (dw_die_ref c)
6620 return c && c->die_tag == DW_TAG_compile_unit;
6623 /* Returns true iff C is a unit DIE of some sort. */
6625 static inline bool
6626 is_unit_die (dw_die_ref c)
6628 return c && (c->die_tag == DW_TAG_compile_unit
6629 || c->die_tag == DW_TAG_partial_unit
6630 || c->die_tag == DW_TAG_type_unit);
6633 /* Returns true iff C is a namespace DIE. */
6635 static inline bool
6636 is_namespace_die (dw_die_ref c)
6638 return c && c->die_tag == DW_TAG_namespace;
6641 /* Returns true iff C is a class or structure DIE. */
6643 static inline bool
6644 is_class_die (dw_die_ref c)
6646 return c && (c->die_tag == DW_TAG_class_type
6647 || c->die_tag == DW_TAG_structure_type);
6650 /* Return non-zero if this DIE is a template parameter. */
6652 static inline bool
6653 is_template_parameter (dw_die_ref die)
6655 switch (die->die_tag)
6657 case DW_TAG_template_type_param:
6658 case DW_TAG_template_value_param:
6659 case DW_TAG_GNU_template_template_param:
6660 case DW_TAG_GNU_template_parameter_pack:
6661 return true;
6662 default:
6663 return false;
6667 /* Return non-zero if this DIE represents a template instantiation. */
6669 static inline bool
6670 is_template_instantiation (dw_die_ref die)
6672 dw_die_ref c;
6674 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
6675 return false;
6676 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
6677 return false;
6680 static char *
6681 gen_internal_sym (const char *prefix)
6683 char buf[256];
6685 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6686 return xstrdup (buf);
6689 /* Assign symbols to all worthy DIEs under DIE. */
6691 static void
6692 assign_symbol_names (dw_die_ref die)
6694 dw_die_ref c;
6696 if (is_symbol_die (die) && !die->comdat_type_p)
6698 if (comdat_symbol_id)
6700 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
6702 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6703 comdat_symbol_id, comdat_symbol_number++);
6704 die->die_id.die_symbol = xstrdup (p);
6706 else
6707 die->die_id.die_symbol = gen_internal_sym ("LDIE");
6710 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6713 struct cu_hash_table_entry
6715 dw_die_ref cu;
6716 unsigned min_comdat_num, max_comdat_num;
6717 struct cu_hash_table_entry *next;
6720 /* Helpers to manipulate hash table of CUs. */
6722 struct cu_hash_table_entry_hasher
6724 typedef cu_hash_table_entry value_type;
6725 typedef die_struct compare_type;
6726 static inline hashval_t hash (const value_type *);
6727 static inline bool equal (const value_type *, const compare_type *);
6728 static inline void remove (value_type *);
6731 inline hashval_t
6732 cu_hash_table_entry_hasher::hash (const value_type *entry)
6734 return htab_hash_string (entry->cu->die_id.die_symbol);
6737 inline bool
6738 cu_hash_table_entry_hasher::equal (const value_type *entry1,
6739 const compare_type *entry2)
6741 return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
6744 inline void
6745 cu_hash_table_entry_hasher::remove (value_type *entry)
6747 struct cu_hash_table_entry *next;
6749 while (entry)
6751 next = entry->next;
6752 free (entry);
6753 entry = next;
6757 typedef hash_table<cu_hash_table_entry_hasher> cu_hash_type;
6759 /* Check whether we have already seen this CU and set up SYM_NUM
6760 accordingly. */
6761 static int
6762 check_duplicate_cu (dw_die_ref cu, cu_hash_type *htable, unsigned int *sym_num)
6764 struct cu_hash_table_entry dummy;
6765 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6767 dummy.max_comdat_num = 0;
6769 slot = htable->find_slot_with_hash (cu,
6770 htab_hash_string (cu->die_id.die_symbol),
6771 INSERT);
6772 entry = *slot;
6774 for (; entry; last = entry, entry = entry->next)
6776 if (same_die_p_wrap (cu, entry->cu))
6777 break;
6780 if (entry)
6782 *sym_num = entry->min_comdat_num;
6783 return 1;
6786 entry = XCNEW (struct cu_hash_table_entry);
6787 entry->cu = cu;
6788 entry->min_comdat_num = *sym_num = last->max_comdat_num;
6789 entry->next = *slot;
6790 *slot = entry;
6792 return 0;
6795 /* Record SYM_NUM to record of CU in HTABLE. */
6796 static void
6797 record_comdat_symbol_number (dw_die_ref cu, cu_hash_type *htable,
6798 unsigned int sym_num)
6800 struct cu_hash_table_entry **slot, *entry;
6802 slot = htable->find_slot_with_hash (cu,
6803 htab_hash_string (cu->die_id.die_symbol),
6804 NO_INSERT);
6805 entry = *slot;
6807 entry->max_comdat_num = sym_num;
6810 /* Traverse the DIE (which is always comp_unit_die), and set up
6811 additional compilation units for each of the include files we see
6812 bracketed by BINCL/EINCL. */
6814 static void
6815 break_out_includes (dw_die_ref die)
6817 dw_die_ref c;
6818 dw_die_ref unit = NULL;
6819 limbo_die_node *node, **pnode;
6821 c = die->die_child;
6822 if (c) do {
6823 dw_die_ref prev = c;
6824 c = c->die_sib;
6825 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6826 || (unit && is_comdat_die (c)))
6828 dw_die_ref next = c->die_sib;
6830 /* This DIE is for a secondary CU; remove it from the main one. */
6831 remove_child_with_prev (c, prev);
6833 if (c->die_tag == DW_TAG_GNU_BINCL)
6834 unit = push_new_compile_unit (unit, c);
6835 else if (c->die_tag == DW_TAG_GNU_EINCL)
6836 unit = pop_compile_unit (unit);
6837 else
6838 add_child_die (unit, c);
6839 c = next;
6840 if (c == die->die_child)
6841 break;
6843 } while (c != die->die_child);
6845 #if 0
6846 /* We can only use this in debugging, since the frontend doesn't check
6847 to make sure that we leave every include file we enter. */
6848 gcc_assert (!unit);
6849 #endif
6851 assign_symbol_names (die);
6852 cu_hash_type cu_hash_table (10);
6853 for (node = limbo_die_list, pnode = &limbo_die_list;
6854 node;
6855 node = node->next)
6857 int is_dupl;
6859 compute_section_prefix (node->die);
6860 is_dupl = check_duplicate_cu (node->die, &cu_hash_table,
6861 &comdat_symbol_number);
6862 assign_symbol_names (node->die);
6863 if (is_dupl)
6864 *pnode = node->next;
6865 else
6867 pnode = &node->next;
6868 record_comdat_symbol_number (node->die, &cu_hash_table,
6869 comdat_symbol_number);
6874 /* Return non-zero if this DIE is a declaration. */
6876 static int
6877 is_declaration_die (dw_die_ref die)
6879 dw_attr_ref a;
6880 unsigned ix;
6882 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6883 if (a->dw_attr == DW_AT_declaration)
6884 return 1;
6886 return 0;
6889 /* Return non-zero if this DIE is nested inside a subprogram. */
6891 static int
6892 is_nested_in_subprogram (dw_die_ref die)
6894 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
6896 if (decl == NULL)
6897 decl = die;
6898 return local_scope_p (decl);
6901 /* Return non-zero if this DIE contains a defining declaration of a
6902 subprogram. */
6904 static int
6905 contains_subprogram_definition (dw_die_ref die)
6907 dw_die_ref c;
6909 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
6910 return 1;
6911 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
6912 return 0;
6915 /* Return non-zero if this is a type DIE that should be moved to a
6916 COMDAT .debug_types section. */
6918 static int
6919 should_move_die_to_comdat (dw_die_ref die)
6921 switch (die->die_tag)
6923 case DW_TAG_class_type:
6924 case DW_TAG_structure_type:
6925 case DW_TAG_enumeration_type:
6926 case DW_TAG_union_type:
6927 /* Don't move declarations, inlined instances, types nested in a
6928 subprogram, or types that contain subprogram definitions. */
6929 if (is_declaration_die (die)
6930 || get_AT (die, DW_AT_abstract_origin)
6931 || is_nested_in_subprogram (die)
6932 || contains_subprogram_definition (die))
6933 return 0;
6934 return 1;
6935 case DW_TAG_array_type:
6936 case DW_TAG_interface_type:
6937 case DW_TAG_pointer_type:
6938 case DW_TAG_reference_type:
6939 case DW_TAG_rvalue_reference_type:
6940 case DW_TAG_string_type:
6941 case DW_TAG_subroutine_type:
6942 case DW_TAG_ptr_to_member_type:
6943 case DW_TAG_set_type:
6944 case DW_TAG_subrange_type:
6945 case DW_TAG_base_type:
6946 case DW_TAG_const_type:
6947 case DW_TAG_file_type:
6948 case DW_TAG_packed_type:
6949 case DW_TAG_volatile_type:
6950 case DW_TAG_typedef:
6951 default:
6952 return 0;
6956 /* Make a clone of DIE. */
6958 static dw_die_ref
6959 clone_die (dw_die_ref die)
6961 dw_die_ref clone;
6962 dw_attr_ref a;
6963 unsigned ix;
6965 clone = ggc_cleared_alloc<die_node> ();
6966 clone->die_tag = die->die_tag;
6968 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6969 add_dwarf_attr (clone, a);
6971 return clone;
6974 /* Make a clone of the tree rooted at DIE. */
6976 static dw_die_ref
6977 clone_tree (dw_die_ref die)
6979 dw_die_ref c;
6980 dw_die_ref clone = clone_die (die);
6982 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
6984 return clone;
6987 /* Make a clone of DIE as a declaration. */
6989 static dw_die_ref
6990 clone_as_declaration (dw_die_ref die)
6992 dw_die_ref clone;
6993 dw_die_ref decl;
6994 dw_attr_ref a;
6995 unsigned ix;
6997 /* If the DIE is already a declaration, just clone it. */
6998 if (is_declaration_die (die))
6999 return clone_die (die);
7001 /* If the DIE is a specification, just clone its declaration DIE. */
7002 decl = get_AT_ref (die, DW_AT_specification);
7003 if (decl != NULL)
7005 clone = clone_die (decl);
7006 if (die->comdat_type_p)
7007 add_AT_die_ref (clone, DW_AT_signature, die);
7008 return clone;
7011 clone = ggc_cleared_alloc<die_node> ();
7012 clone->die_tag = die->die_tag;
7014 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7016 /* We don't want to copy over all attributes.
7017 For example we don't want DW_AT_byte_size because otherwise we will no
7018 longer have a declaration and GDB will treat it as a definition. */
7020 switch (a->dw_attr)
7022 case DW_AT_abstract_origin:
7023 case DW_AT_artificial:
7024 case DW_AT_containing_type:
7025 case DW_AT_external:
7026 case DW_AT_name:
7027 case DW_AT_type:
7028 case DW_AT_virtuality:
7029 case DW_AT_linkage_name:
7030 case DW_AT_MIPS_linkage_name:
7031 add_dwarf_attr (clone, a);
7032 break;
7033 case DW_AT_byte_size:
7034 default:
7035 break;
7039 if (die->comdat_type_p)
7040 add_AT_die_ref (clone, DW_AT_signature, die);
7042 add_AT_flag (clone, DW_AT_declaration, 1);
7043 return clone;
7047 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
7049 struct decl_table_entry
7051 dw_die_ref orig;
7052 dw_die_ref copy;
7055 /* Helpers to manipulate hash table of copied declarations. */
7057 /* Hashtable helpers. */
7059 struct decl_table_entry_hasher : typed_free_remove <decl_table_entry>
7061 typedef decl_table_entry value_type;
7062 typedef die_struct compare_type;
7063 static inline hashval_t hash (const value_type *);
7064 static inline bool equal (const value_type *, const compare_type *);
7067 inline hashval_t
7068 decl_table_entry_hasher::hash (const value_type *entry)
7070 return htab_hash_pointer (entry->orig);
7073 inline bool
7074 decl_table_entry_hasher::equal (const value_type *entry1,
7075 const compare_type *entry2)
7077 return entry1->orig == entry2;
7080 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
7082 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7083 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
7084 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
7085 to check if the ancestor has already been copied into UNIT. */
7087 static dw_die_ref
7088 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
7089 decl_hash_type *decl_table)
7091 dw_die_ref parent = die->die_parent;
7092 dw_die_ref new_parent = unit;
7093 dw_die_ref copy;
7094 decl_table_entry **slot = NULL;
7095 struct decl_table_entry *entry = NULL;
7097 if (decl_table)
7099 /* Check if the entry has already been copied to UNIT. */
7100 slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
7101 INSERT);
7102 if (*slot != HTAB_EMPTY_ENTRY)
7104 entry = *slot;
7105 return entry->copy;
7108 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
7109 entry = XCNEW (struct decl_table_entry);
7110 entry->orig = die;
7111 entry->copy = NULL;
7112 *slot = entry;
7115 if (parent != NULL)
7117 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7118 if (spec != NULL)
7119 parent = spec;
7120 if (!is_unit_die (parent))
7121 new_parent = copy_ancestor_tree (unit, parent, decl_table);
7124 copy = clone_as_declaration (die);
7125 add_child_die (new_parent, copy);
7127 if (decl_table)
7129 /* Record the pointer to the copy. */
7130 entry->copy = copy;
7133 return copy;
7135 /* Copy the declaration context to the new type unit DIE. This includes
7136 any surrounding namespace or type declarations. If the DIE has an
7137 AT_specification attribute, it also includes attributes and children
7138 attached to the specification, and returns a pointer to the original
7139 parent of the declaration DIE. Returns NULL otherwise. */
7141 static dw_die_ref
7142 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7144 dw_die_ref decl;
7145 dw_die_ref new_decl;
7146 dw_die_ref orig_parent = NULL;
7148 decl = get_AT_ref (die, DW_AT_specification);
7149 if (decl == NULL)
7150 decl = die;
7151 else
7153 unsigned ix;
7154 dw_die_ref c;
7155 dw_attr_ref a;
7157 /* The original DIE will be changed to a declaration, and must
7158 be moved to be a child of the original declaration DIE. */
7159 orig_parent = decl->die_parent;
7161 /* Copy the type node pointer from the new DIE to the original
7162 declaration DIE so we can forward references later. */
7163 decl->comdat_type_p = true;
7164 decl->die_id.die_type_node = die->die_id.die_type_node;
7166 remove_AT (die, DW_AT_specification);
7168 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7170 if (a->dw_attr != DW_AT_name
7171 && a->dw_attr != DW_AT_declaration
7172 && a->dw_attr != DW_AT_external)
7173 add_dwarf_attr (die, a);
7176 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7179 if (decl->die_parent != NULL
7180 && !is_unit_die (decl->die_parent))
7182 new_decl = copy_ancestor_tree (unit, decl, NULL);
7183 if (new_decl != NULL)
7185 remove_AT (new_decl, DW_AT_signature);
7186 add_AT_specification (die, new_decl);
7190 return orig_parent;
7193 /* Generate the skeleton ancestor tree for the given NODE, then clone
7194 the DIE and add the clone into the tree. */
7196 static void
7197 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7199 if (node->new_die != NULL)
7200 return;
7202 node->new_die = clone_as_declaration (node->old_die);
7204 if (node->parent != NULL)
7206 generate_skeleton_ancestor_tree (node->parent);
7207 add_child_die (node->parent->new_die, node->new_die);
7211 /* Generate a skeleton tree of DIEs containing any declarations that are
7212 found in the original tree. We traverse the tree looking for declaration
7213 DIEs, and construct the skeleton from the bottom up whenever we find one. */
7215 static void
7216 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7218 skeleton_chain_node node;
7219 dw_die_ref c;
7220 dw_die_ref first;
7221 dw_die_ref prev = NULL;
7222 dw_die_ref next = NULL;
7224 node.parent = parent;
7226 first = c = parent->old_die->die_child;
7227 if (c)
7228 next = c->die_sib;
7229 if (c) do {
7230 if (prev == NULL || prev->die_sib == c)
7231 prev = c;
7232 c = next;
7233 next = (c == first ? NULL : c->die_sib);
7234 node.old_die = c;
7235 node.new_die = NULL;
7236 if (is_declaration_die (c))
7238 if (is_template_instantiation (c))
7240 /* Instantiated templates do not need to be cloned into the
7241 type unit. Just move the DIE and its children back to
7242 the skeleton tree (in the main CU). */
7243 remove_child_with_prev (c, prev);
7244 add_child_die (parent->new_die, c);
7245 c = prev;
7247 else
7249 /* Clone the existing DIE, move the original to the skeleton
7250 tree (which is in the main CU), and put the clone, with
7251 all the original's children, where the original came from
7252 (which is about to be moved to the type unit). */
7253 dw_die_ref clone = clone_die (c);
7254 move_all_children (c, clone);
7256 /* If the original has a DW_AT_object_pointer attribute,
7257 it would now point to a child DIE just moved to the
7258 cloned tree, so we need to remove that attribute from
7259 the original. */
7260 remove_AT (c, DW_AT_object_pointer);
7262 replace_child (c, clone, prev);
7263 generate_skeleton_ancestor_tree (parent);
7264 add_child_die (parent->new_die, c);
7265 node.new_die = c;
7266 c = clone;
7269 generate_skeleton_bottom_up (&node);
7270 } while (next != NULL);
7273 /* Wrapper function for generate_skeleton_bottom_up. */
7275 static dw_die_ref
7276 generate_skeleton (dw_die_ref die)
7278 skeleton_chain_node node;
7280 node.old_die = die;
7281 node.new_die = NULL;
7282 node.parent = NULL;
7284 /* If this type definition is nested inside another type,
7285 and is not an instantiation of a template, always leave
7286 at least a declaration in its place. */
7287 if (die->die_parent != NULL
7288 && is_type_die (die->die_parent)
7289 && !is_template_instantiation (die))
7290 node.new_die = clone_as_declaration (die);
7292 generate_skeleton_bottom_up (&node);
7293 return node.new_die;
7296 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
7297 declaration. The original DIE is moved to a new compile unit so that
7298 existing references to it follow it to the new location. If any of the
7299 original DIE's descendants is a declaration, we need to replace the
7300 original DIE with a skeleton tree and move the declarations back into the
7301 skeleton tree. */
7303 static dw_die_ref
7304 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
7305 dw_die_ref prev)
7307 dw_die_ref skeleton, orig_parent;
7309 /* Copy the declaration context to the type unit DIE. If the returned
7310 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
7311 that DIE. */
7312 orig_parent = copy_declaration_context (unit, child);
7314 skeleton = generate_skeleton (child);
7315 if (skeleton == NULL)
7316 remove_child_with_prev (child, prev);
7317 else
7319 skeleton->comdat_type_p = true;
7320 skeleton->die_id.die_type_node = child->die_id.die_type_node;
7322 /* If the original DIE was a specification, we need to put
7323 the skeleton under the parent DIE of the declaration.
7324 This leaves the original declaration in the tree, but
7325 it will be pruned later since there are no longer any
7326 references to it. */
7327 if (orig_parent != NULL)
7329 remove_child_with_prev (child, prev);
7330 add_child_die (orig_parent, skeleton);
7332 else
7333 replace_child (child, skeleton, prev);
7336 return skeleton;
7339 /* Traverse the DIE and set up additional .debug_types sections for each
7340 type worthy of being placed in a COMDAT section. */
7342 static void
7343 break_out_comdat_types (dw_die_ref die)
7345 dw_die_ref c;
7346 dw_die_ref first;
7347 dw_die_ref prev = NULL;
7348 dw_die_ref next = NULL;
7349 dw_die_ref unit = NULL;
7351 first = c = die->die_child;
7352 if (c)
7353 next = c->die_sib;
7354 if (c) do {
7355 if (prev == NULL || prev->die_sib == c)
7356 prev = c;
7357 c = next;
7358 next = (c == first ? NULL : c->die_sib);
7359 if (should_move_die_to_comdat (c))
7361 dw_die_ref replacement;
7362 comdat_type_node_ref type_node;
7364 /* Break out nested types into their own type units. */
7365 break_out_comdat_types (c);
7367 /* Create a new type unit DIE as the root for the new tree, and
7368 add it to the list of comdat types. */
7369 unit = new_die (DW_TAG_type_unit, NULL, NULL);
7370 add_AT_unsigned (unit, DW_AT_language,
7371 get_AT_unsigned (comp_unit_die (), DW_AT_language));
7372 type_node = ggc_cleared_alloc<comdat_type_node> ();
7373 type_node->root_die = unit;
7374 type_node->next = comdat_type_list;
7375 comdat_type_list = type_node;
7377 /* Generate the type signature. */
7378 generate_type_signature (c, type_node);
7380 /* Copy the declaration context, attributes, and children of the
7381 declaration into the new type unit DIE, then remove this DIE
7382 from the main CU (or replace it with a skeleton if necessary). */
7383 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
7384 type_node->skeleton_die = replacement;
7386 /* Add the DIE to the new compunit. */
7387 add_child_die (unit, c);
7389 if (replacement != NULL)
7390 c = replacement;
7392 else if (c->die_tag == DW_TAG_namespace
7393 || c->die_tag == DW_TAG_class_type
7394 || c->die_tag == DW_TAG_structure_type
7395 || c->die_tag == DW_TAG_union_type)
7397 /* Look for nested types that can be broken out. */
7398 break_out_comdat_types (c);
7400 } while (next != NULL);
7403 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
7404 Enter all the cloned children into the hash table decl_table. */
7406 static dw_die_ref
7407 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
7409 dw_die_ref c;
7410 dw_die_ref clone;
7411 struct decl_table_entry *entry;
7412 decl_table_entry **slot;
7414 if (die->die_tag == DW_TAG_subprogram)
7415 clone = clone_as_declaration (die);
7416 else
7417 clone = clone_die (die);
7419 slot = decl_table->find_slot_with_hash (die,
7420 htab_hash_pointer (die), INSERT);
7422 /* Assert that DIE isn't in the hash table yet. If it would be there
7423 before, the ancestors would be necessarily there as well, therefore
7424 clone_tree_partial wouldn't be called. */
7425 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
7427 entry = XCNEW (struct decl_table_entry);
7428 entry->orig = die;
7429 entry->copy = clone;
7430 *slot = entry;
7432 if (die->die_tag != DW_TAG_subprogram)
7433 FOR_EACH_CHILD (die, c,
7434 add_child_die (clone, clone_tree_partial (c, decl_table)));
7436 return clone;
7439 /* Walk the DIE and its children, looking for references to incomplete
7440 or trivial types that are unmarked (i.e., that are not in the current
7441 type_unit). */
7443 static void
7444 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
7446 dw_die_ref c;
7447 dw_attr_ref a;
7448 unsigned ix;
7450 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7452 if (AT_class (a) == dw_val_class_die_ref)
7454 dw_die_ref targ = AT_ref (a);
7455 decl_table_entry **slot;
7456 struct decl_table_entry *entry;
7458 if (targ->die_mark != 0 || targ->comdat_type_p)
7459 continue;
7461 slot = decl_table->find_slot_with_hash (targ,
7462 htab_hash_pointer (targ),
7463 INSERT);
7465 if (*slot != HTAB_EMPTY_ENTRY)
7467 /* TARG has already been copied, so we just need to
7468 modify the reference to point to the copy. */
7469 entry = *slot;
7470 a->dw_attr_val.v.val_die_ref.die = entry->copy;
7472 else
7474 dw_die_ref parent = unit;
7475 dw_die_ref copy = clone_die (targ);
7477 /* Record in DECL_TABLE that TARG has been copied.
7478 Need to do this now, before the recursive call,
7479 because DECL_TABLE may be expanded and SLOT
7480 would no longer be a valid pointer. */
7481 entry = XCNEW (struct decl_table_entry);
7482 entry->orig = targ;
7483 entry->copy = copy;
7484 *slot = entry;
7486 /* If TARG is not a declaration DIE, we need to copy its
7487 children. */
7488 if (!is_declaration_die (targ))
7490 FOR_EACH_CHILD (
7491 targ, c,
7492 add_child_die (copy,
7493 clone_tree_partial (c, decl_table)));
7496 /* Make sure the cloned tree is marked as part of the
7497 type unit. */
7498 mark_dies (copy);
7500 /* If TARG has surrounding context, copy its ancestor tree
7501 into the new type unit. */
7502 if (targ->die_parent != NULL
7503 && !is_unit_die (targ->die_parent))
7504 parent = copy_ancestor_tree (unit, targ->die_parent,
7505 decl_table);
7507 add_child_die (parent, copy);
7508 a->dw_attr_val.v.val_die_ref.die = copy;
7510 /* Make sure the newly-copied DIE is walked. If it was
7511 installed in a previously-added context, it won't
7512 get visited otherwise. */
7513 if (parent != unit)
7515 /* Find the highest point of the newly-added tree,
7516 mark each node along the way, and walk from there. */
7517 parent->die_mark = 1;
7518 while (parent->die_parent
7519 && parent->die_parent->die_mark == 0)
7521 parent = parent->die_parent;
7522 parent->die_mark = 1;
7524 copy_decls_walk (unit, parent, decl_table);
7530 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
7533 /* Copy declarations for "unworthy" types into the new comdat section.
7534 Incomplete types, modified types, and certain other types aren't broken
7535 out into comdat sections of their own, so they don't have a signature,
7536 and we need to copy the declaration into the same section so that we
7537 don't have an external reference. */
7539 static void
7540 copy_decls_for_unworthy_types (dw_die_ref unit)
7542 mark_dies (unit);
7543 decl_hash_type decl_table (10);
7544 copy_decls_walk (unit, unit, &decl_table);
7545 unmark_dies (unit);
7548 /* Traverse the DIE and add a sibling attribute if it may have the
7549 effect of speeding up access to siblings. To save some space,
7550 avoid generating sibling attributes for DIE's without children. */
7552 static void
7553 add_sibling_attributes (dw_die_ref die)
7555 dw_die_ref c;
7557 if (! die->die_child)
7558 return;
7560 if (die->die_parent && die != die->die_parent->die_child)
7561 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7563 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7566 /* Output all location lists for the DIE and its children. */
7568 static void
7569 output_location_lists (dw_die_ref die)
7571 dw_die_ref c;
7572 dw_attr_ref a;
7573 unsigned ix;
7575 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7576 if (AT_class (a) == dw_val_class_loc_list)
7577 output_loc_list (AT_loc_list (a));
7579 FOR_EACH_CHILD (die, c, output_location_lists (c));
7582 /* We want to limit the number of external references, because they are
7583 larger than local references: a relocation takes multiple words, and
7584 even a sig8 reference is always eight bytes, whereas a local reference
7585 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
7586 So if we encounter multiple external references to the same type DIE, we
7587 make a local typedef stub for it and redirect all references there.
7589 This is the element of the hash table for keeping track of these
7590 references. */
7592 struct external_ref
7594 dw_die_ref type;
7595 dw_die_ref stub;
7596 unsigned n_refs;
7599 /* Hashtable helpers. */
7601 struct external_ref_hasher : typed_free_remove <external_ref>
7603 typedef external_ref value_type;
7604 typedef external_ref compare_type;
7605 static inline hashval_t hash (const value_type *);
7606 static inline bool equal (const value_type *, const compare_type *);
7609 inline hashval_t
7610 external_ref_hasher::hash (const value_type *r)
7612 dw_die_ref die = r->type;
7613 hashval_t h = 0;
7615 /* We can't use the address of the DIE for hashing, because
7616 that will make the order of the stub DIEs non-deterministic. */
7617 if (! die->comdat_type_p)
7618 /* We have a symbol; use it to compute a hash. */
7619 h = htab_hash_string (die->die_id.die_symbol);
7620 else
7622 /* We have a type signature; use a subset of the bits as the hash.
7623 The 8-byte signature is at least as large as hashval_t. */
7624 comdat_type_node_ref type_node = die->die_id.die_type_node;
7625 memcpy (&h, type_node->signature, sizeof (h));
7627 return h;
7630 inline bool
7631 external_ref_hasher::equal (const value_type *r1, const compare_type *r2)
7633 return r1->type == r2->type;
7636 typedef hash_table<external_ref_hasher> external_ref_hash_type;
7638 /* Return a pointer to the external_ref for references to DIE. */
7640 static struct external_ref *
7641 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
7643 struct external_ref ref, *ref_p;
7644 external_ref **slot;
7646 ref.type = die;
7647 slot = map->find_slot (&ref, INSERT);
7648 if (*slot != HTAB_EMPTY_ENTRY)
7649 return *slot;
7651 ref_p = XCNEW (struct external_ref);
7652 ref_p->type = die;
7653 *slot = ref_p;
7654 return ref_p;
7657 /* Subroutine of optimize_external_refs, below.
7659 If we see a type skeleton, record it as our stub. If we see external
7660 references, remember how many we've seen. */
7662 static void
7663 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
7665 dw_die_ref c;
7666 dw_attr_ref a;
7667 unsigned ix;
7668 struct external_ref *ref_p;
7670 if (is_type_die (die)
7671 && (c = get_AT_ref (die, DW_AT_signature)))
7673 /* This is a local skeleton; use it for local references. */
7674 ref_p = lookup_external_ref (map, c);
7675 ref_p->stub = die;
7678 /* Scan the DIE references, and remember any that refer to DIEs from
7679 other CUs (i.e. those which are not marked). */
7680 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7681 if (AT_class (a) == dw_val_class_die_ref
7682 && (c = AT_ref (a))->die_mark == 0
7683 && is_type_die (c))
7685 ref_p = lookup_external_ref (map, c);
7686 ref_p->n_refs++;
7689 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
7692 /* htab_traverse callback function for optimize_external_refs, below. SLOT
7693 points to an external_ref, DATA is the CU we're processing. If we don't
7694 already have a local stub, and we have multiple refs, build a stub. */
7697 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
7699 struct external_ref *ref_p = *slot;
7701 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
7703 /* We have multiple references to this type, so build a small stub.
7704 Both of these forms are a bit dodgy from the perspective of the
7705 DWARF standard, since technically they should have names. */
7706 dw_die_ref cu = data;
7707 dw_die_ref type = ref_p->type;
7708 dw_die_ref stub = NULL;
7710 if (type->comdat_type_p)
7712 /* If we refer to this type via sig8, use AT_signature. */
7713 stub = new_die (type->die_tag, cu, NULL_TREE);
7714 add_AT_die_ref (stub, DW_AT_signature, type);
7716 else
7718 /* Otherwise, use a typedef with no name. */
7719 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
7720 add_AT_die_ref (stub, DW_AT_type, type);
7723 stub->die_mark++;
7724 ref_p->stub = stub;
7726 return 1;
7729 /* DIE is a unit; look through all the DIE references to see if there are
7730 any external references to types, and if so, create local stubs for
7731 them which will be applied in build_abbrev_table. This is useful because
7732 references to local DIEs are smaller. */
7734 static external_ref_hash_type *
7735 optimize_external_refs (dw_die_ref die)
7737 external_ref_hash_type *map = new external_ref_hash_type (10);
7738 optimize_external_refs_1 (die, map);
7739 map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
7740 return map;
7743 /* The format of each DIE (and its attribute value pairs) is encoded in an
7744 abbreviation table. This routine builds the abbreviation table and assigns
7745 a unique abbreviation id for each abbreviation entry. The children of each
7746 die are visited recursively. */
7748 static void
7749 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
7751 unsigned long abbrev_id;
7752 unsigned int n_alloc;
7753 dw_die_ref c;
7754 dw_attr_ref a;
7755 unsigned ix;
7757 /* Scan the DIE references, and replace any that refer to
7758 DIEs from other CUs (i.e. those which are not marked) with
7759 the local stubs we built in optimize_external_refs. */
7760 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7761 if (AT_class (a) == dw_val_class_die_ref
7762 && (c = AT_ref (a))->die_mark == 0)
7764 struct external_ref *ref_p;
7765 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
7767 ref_p = lookup_external_ref (extern_map, c);
7768 if (ref_p->stub && ref_p->stub != die)
7769 change_AT_die_ref (a, ref_p->stub);
7770 else
7771 /* We aren't changing this reference, so mark it external. */
7772 set_AT_ref_external (a, 1);
7775 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7777 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7778 dw_attr_ref die_a, abbrev_a;
7779 unsigned ix;
7780 bool ok = true;
7782 if (abbrev->die_tag != die->die_tag)
7783 continue;
7784 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7785 continue;
7787 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
7788 continue;
7790 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
7792 abbrev_a = &(*abbrev->die_attr)[ix];
7793 if ((abbrev_a->dw_attr != die_a->dw_attr)
7794 || (value_format (abbrev_a) != value_format (die_a)))
7796 ok = false;
7797 break;
7800 if (ok)
7801 break;
7804 if (abbrev_id >= abbrev_die_table_in_use)
7806 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7808 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
7809 abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7810 n_alloc);
7812 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7813 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
7814 abbrev_die_table_allocated = n_alloc;
7817 ++abbrev_die_table_in_use;
7818 abbrev_die_table[abbrev_id] = die;
7821 die->die_abbrev = abbrev_id;
7822 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
7825 /* Return the power-of-two number of bytes necessary to represent VALUE. */
7827 static int
7828 constant_size (unsigned HOST_WIDE_INT value)
7830 int log;
7832 if (value == 0)
7833 log = 0;
7834 else
7835 log = floor_log2 (value);
7837 log = log / 8;
7838 log = 1 << (floor_log2 (log) + 1);
7840 return log;
7843 /* Return the size of a DIE as it is represented in the
7844 .debug_info section. */
7846 static unsigned long
7847 size_of_die (dw_die_ref die)
7849 unsigned long size = 0;
7850 dw_attr_ref a;
7851 unsigned ix;
7852 enum dwarf_form form;
7854 size += size_of_uleb128 (die->die_abbrev);
7855 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7857 switch (AT_class (a))
7859 case dw_val_class_addr:
7860 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7862 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7863 size += size_of_uleb128 (AT_index (a));
7865 else
7866 size += DWARF2_ADDR_SIZE;
7867 break;
7868 case dw_val_class_offset:
7869 size += DWARF_OFFSET_SIZE;
7870 break;
7871 case dw_val_class_loc:
7873 unsigned long lsize = size_of_locs (AT_loc (a));
7875 /* Block length. */
7876 if (dwarf_version >= 4)
7877 size += size_of_uleb128 (lsize);
7878 else
7879 size += constant_size (lsize);
7880 size += lsize;
7882 break;
7883 case dw_val_class_loc_list:
7884 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7886 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7887 size += size_of_uleb128 (AT_index (a));
7889 else
7890 size += DWARF_OFFSET_SIZE;
7891 break;
7892 case dw_val_class_range_list:
7893 size += DWARF_OFFSET_SIZE;
7894 break;
7895 case dw_val_class_const:
7896 size += size_of_sleb128 (AT_int (a));
7897 break;
7898 case dw_val_class_unsigned_const:
7900 int csize = constant_size (AT_unsigned (a));
7901 if (dwarf_version == 3
7902 && a->dw_attr == DW_AT_data_member_location
7903 && csize >= 4)
7904 size += size_of_uleb128 (AT_unsigned (a));
7905 else
7906 size += csize;
7908 break;
7909 case dw_val_class_const_double:
7910 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
7911 if (HOST_BITS_PER_WIDE_INT >= 64)
7912 size++; /* block */
7913 break;
7914 case dw_val_class_wide_int:
7915 size += (get_full_len (*a->dw_attr_val.v.val_wide)
7916 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
7917 if (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT
7918 > 64)
7919 size++; /* block */
7920 break;
7921 case dw_val_class_vec:
7922 size += constant_size (a->dw_attr_val.v.val_vec.length
7923 * a->dw_attr_val.v.val_vec.elt_size)
7924 + a->dw_attr_val.v.val_vec.length
7925 * a->dw_attr_val.v.val_vec.elt_size; /* block */
7926 break;
7927 case dw_val_class_flag:
7928 if (dwarf_version >= 4)
7929 /* Currently all add_AT_flag calls pass in 1 as last argument,
7930 so DW_FORM_flag_present can be used. If that ever changes,
7931 we'll need to use DW_FORM_flag and have some optimization
7932 in build_abbrev_table that will change those to
7933 DW_FORM_flag_present if it is set to 1 in all DIEs using
7934 the same abbrev entry. */
7935 gcc_assert (a->dw_attr_val.v.val_flag == 1);
7936 else
7937 size += 1;
7938 break;
7939 case dw_val_class_die_ref:
7940 if (AT_ref_external (a))
7942 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
7943 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
7944 is sized by target address length, whereas in DWARF3
7945 it's always sized as an offset. */
7946 if (use_debug_types)
7947 size += DWARF_TYPE_SIGNATURE_SIZE;
7948 else if (dwarf_version == 2)
7949 size += DWARF2_ADDR_SIZE;
7950 else
7951 size += DWARF_OFFSET_SIZE;
7953 else
7954 size += DWARF_OFFSET_SIZE;
7955 break;
7956 case dw_val_class_fde_ref:
7957 size += DWARF_OFFSET_SIZE;
7958 break;
7959 case dw_val_class_lbl_id:
7960 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7962 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7963 size += size_of_uleb128 (AT_index (a));
7965 else
7966 size += DWARF2_ADDR_SIZE;
7967 break;
7968 case dw_val_class_lineptr:
7969 case dw_val_class_macptr:
7970 size += DWARF_OFFSET_SIZE;
7971 break;
7972 case dw_val_class_str:
7973 form = AT_string_form (a);
7974 if (form == DW_FORM_strp)
7975 size += DWARF_OFFSET_SIZE;
7976 else if (form == DW_FORM_GNU_str_index)
7977 size += size_of_uleb128 (AT_index (a));
7978 else
7979 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
7980 break;
7981 case dw_val_class_file:
7982 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
7983 break;
7984 case dw_val_class_data8:
7985 size += 8;
7986 break;
7987 case dw_val_class_vms_delta:
7988 size += DWARF_OFFSET_SIZE;
7989 break;
7990 case dw_val_class_high_pc:
7991 size += DWARF2_ADDR_SIZE;
7992 break;
7993 default:
7994 gcc_unreachable ();
7998 return size;
8001 /* Size the debugging information associated with a given DIE. Visits the
8002 DIE's children recursively. Updates the global variable next_die_offset, on
8003 each time through. Uses the current value of next_die_offset to update the
8004 die_offset field in each DIE. */
8006 static void
8007 calc_die_sizes (dw_die_ref die)
8009 dw_die_ref c;
8011 gcc_assert (die->die_offset == 0
8012 || (unsigned long int) die->die_offset == next_die_offset);
8013 die->die_offset = next_die_offset;
8014 next_die_offset += size_of_die (die);
8016 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
8018 if (die->die_child != NULL)
8019 /* Count the null byte used to terminate sibling lists. */
8020 next_die_offset += 1;
8023 /* Size just the base type children at the start of the CU.
8024 This is needed because build_abbrev needs to size locs
8025 and sizing of type based stack ops needs to know die_offset
8026 values for the base types. */
8028 static void
8029 calc_base_type_die_sizes (void)
8031 unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8032 unsigned int i;
8033 dw_die_ref base_type;
8034 #if ENABLE_ASSERT_CHECKING
8035 dw_die_ref prev = comp_unit_die ()->die_child;
8036 #endif
8038 die_offset += size_of_die (comp_unit_die ());
8039 for (i = 0; base_types.iterate (i, &base_type); i++)
8041 #if ENABLE_ASSERT_CHECKING
8042 gcc_assert (base_type->die_offset == 0
8043 && prev->die_sib == base_type
8044 && base_type->die_child == NULL
8045 && base_type->die_abbrev);
8046 prev = base_type;
8047 #endif
8048 base_type->die_offset = die_offset;
8049 die_offset += size_of_die (base_type);
8053 /* Set the marks for a die and its children. We do this so
8054 that we know whether or not a reference needs to use FORM_ref_addr; only
8055 DIEs in the same CU will be marked. We used to clear out the offset
8056 and use that as the flag, but ran into ordering problems. */
8058 static void
8059 mark_dies (dw_die_ref die)
8061 dw_die_ref c;
8063 gcc_assert (!die->die_mark);
8065 die->die_mark = 1;
8066 FOR_EACH_CHILD (die, c, mark_dies (c));
8069 /* Clear the marks for a die and its children. */
8071 static void
8072 unmark_dies (dw_die_ref die)
8074 dw_die_ref c;
8076 if (! use_debug_types)
8077 gcc_assert (die->die_mark);
8079 die->die_mark = 0;
8080 FOR_EACH_CHILD (die, c, unmark_dies (c));
8083 /* Clear the marks for a die, its children and referred dies. */
8085 static void
8086 unmark_all_dies (dw_die_ref die)
8088 dw_die_ref c;
8089 dw_attr_ref a;
8090 unsigned ix;
8092 if (!die->die_mark)
8093 return;
8094 die->die_mark = 0;
8096 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
8098 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8099 if (AT_class (a) == dw_val_class_die_ref)
8100 unmark_all_dies (AT_ref (a));
8103 /* Calculate if the entry should appear in the final output file. It may be
8104 from a pruned a type. */
8106 static bool
8107 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
8109 /* By limiting gnu pubnames to definitions only, gold can generate a
8110 gdb index without entries for declarations, which don't include
8111 enough information to be useful. */
8112 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
8113 return false;
8115 if (table == pubname_table)
8117 /* Enumerator names are part of the pubname table, but the
8118 parent DW_TAG_enumeration_type die may have been pruned.
8119 Don't output them if that is the case. */
8120 if (p->die->die_tag == DW_TAG_enumerator &&
8121 (p->die->die_parent == NULL
8122 || !p->die->die_parent->die_perennial_p))
8123 return false;
8125 /* Everything else in the pubname table is included. */
8126 return true;
8129 /* The pubtypes table shouldn't include types that have been
8130 pruned. */
8131 return (p->die->die_offset != 0
8132 || !flag_eliminate_unused_debug_types);
8135 /* Return the size of the .debug_pubnames or .debug_pubtypes table
8136 generated for the compilation unit. */
8138 static unsigned long
8139 size_of_pubnames (vec<pubname_entry, va_gc> *names)
8141 unsigned long size;
8142 unsigned i;
8143 pubname_ref p;
8144 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
8146 size = DWARF_PUBNAMES_HEADER_SIZE;
8147 FOR_EACH_VEC_ELT (*names, i, p)
8148 if (include_pubname_in_output (names, p))
8149 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
8151 size += DWARF_OFFSET_SIZE;
8152 return size;
8155 /* Return the size of the information in the .debug_aranges section. */
8157 static unsigned long
8158 size_of_aranges (void)
8160 unsigned long size;
8162 size = DWARF_ARANGES_HEADER_SIZE;
8164 /* Count the address/length pair for this compilation unit. */
8165 if (text_section_used)
8166 size += 2 * DWARF2_ADDR_SIZE;
8167 if (cold_text_section_used)
8168 size += 2 * DWARF2_ADDR_SIZE;
8169 if (have_multiple_function_sections)
8171 unsigned fde_idx;
8172 dw_fde_ref fde;
8174 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
8176 if (DECL_IGNORED_P (fde->decl))
8177 continue;
8178 if (!fde->in_std_section)
8179 size += 2 * DWARF2_ADDR_SIZE;
8180 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
8181 size += 2 * DWARF2_ADDR_SIZE;
8185 /* Count the two zero words used to terminated the address range table. */
8186 size += 2 * DWARF2_ADDR_SIZE;
8187 return size;
8190 /* Select the encoding of an attribute value. */
8192 static enum dwarf_form
8193 value_format (dw_attr_ref a)
8195 switch (AT_class (a))
8197 case dw_val_class_addr:
8198 /* Only very few attributes allow DW_FORM_addr. */
8199 switch (a->dw_attr)
8201 case DW_AT_low_pc:
8202 case DW_AT_high_pc:
8203 case DW_AT_entry_pc:
8204 case DW_AT_trampoline:
8205 return (AT_index (a) == NOT_INDEXED
8206 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8207 default:
8208 break;
8210 switch (DWARF2_ADDR_SIZE)
8212 case 1:
8213 return DW_FORM_data1;
8214 case 2:
8215 return DW_FORM_data2;
8216 case 4:
8217 return DW_FORM_data4;
8218 case 8:
8219 return DW_FORM_data8;
8220 default:
8221 gcc_unreachable ();
8223 case dw_val_class_range_list:
8224 case dw_val_class_loc_list:
8225 if (dwarf_version >= 4)
8226 return DW_FORM_sec_offset;
8227 /* FALLTHRU */
8228 case dw_val_class_vms_delta:
8229 case dw_val_class_offset:
8230 switch (DWARF_OFFSET_SIZE)
8232 case 4:
8233 return DW_FORM_data4;
8234 case 8:
8235 return DW_FORM_data8;
8236 default:
8237 gcc_unreachable ();
8239 case dw_val_class_loc:
8240 if (dwarf_version >= 4)
8241 return DW_FORM_exprloc;
8242 switch (constant_size (size_of_locs (AT_loc (a))))
8244 case 1:
8245 return DW_FORM_block1;
8246 case 2:
8247 return DW_FORM_block2;
8248 case 4:
8249 return DW_FORM_block4;
8250 default:
8251 gcc_unreachable ();
8253 case dw_val_class_const:
8254 return DW_FORM_sdata;
8255 case dw_val_class_unsigned_const:
8256 switch (constant_size (AT_unsigned (a)))
8258 case 1:
8259 return DW_FORM_data1;
8260 case 2:
8261 return DW_FORM_data2;
8262 case 4:
8263 /* In DWARF3 DW_AT_data_member_location with
8264 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
8265 constant, so we need to use DW_FORM_udata if we need
8266 a large constant. */
8267 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8268 return DW_FORM_udata;
8269 return DW_FORM_data4;
8270 case 8:
8271 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8272 return DW_FORM_udata;
8273 return DW_FORM_data8;
8274 default:
8275 gcc_unreachable ();
8277 case dw_val_class_const_double:
8278 switch (HOST_BITS_PER_WIDE_INT)
8280 case 8:
8281 return DW_FORM_data2;
8282 case 16:
8283 return DW_FORM_data4;
8284 case 32:
8285 return DW_FORM_data8;
8286 case 64:
8287 default:
8288 return DW_FORM_block1;
8290 case dw_val_class_wide_int:
8291 switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
8293 case 8:
8294 return DW_FORM_data1;
8295 case 16:
8296 return DW_FORM_data2;
8297 case 32:
8298 return DW_FORM_data4;
8299 case 64:
8300 return DW_FORM_data8;
8301 default:
8302 return DW_FORM_block1;
8304 case dw_val_class_vec:
8305 switch (constant_size (a->dw_attr_val.v.val_vec.length
8306 * a->dw_attr_val.v.val_vec.elt_size))
8308 case 1:
8309 return DW_FORM_block1;
8310 case 2:
8311 return DW_FORM_block2;
8312 case 4:
8313 return DW_FORM_block4;
8314 default:
8315 gcc_unreachable ();
8317 case dw_val_class_flag:
8318 if (dwarf_version >= 4)
8320 /* Currently all add_AT_flag calls pass in 1 as last argument,
8321 so DW_FORM_flag_present can be used. If that ever changes,
8322 we'll need to use DW_FORM_flag and have some optimization
8323 in build_abbrev_table that will change those to
8324 DW_FORM_flag_present if it is set to 1 in all DIEs using
8325 the same abbrev entry. */
8326 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8327 return DW_FORM_flag_present;
8329 return DW_FORM_flag;
8330 case dw_val_class_die_ref:
8331 if (AT_ref_external (a))
8332 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
8333 else
8334 return DW_FORM_ref;
8335 case dw_val_class_fde_ref:
8336 return DW_FORM_data;
8337 case dw_val_class_lbl_id:
8338 return (AT_index (a) == NOT_INDEXED
8339 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8340 case dw_val_class_lineptr:
8341 case dw_val_class_macptr:
8342 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
8343 case dw_val_class_str:
8344 return AT_string_form (a);
8345 case dw_val_class_file:
8346 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8348 case 1:
8349 return DW_FORM_data1;
8350 case 2:
8351 return DW_FORM_data2;
8352 case 4:
8353 return DW_FORM_data4;
8354 default:
8355 gcc_unreachable ();
8358 case dw_val_class_data8:
8359 return DW_FORM_data8;
8361 case dw_val_class_high_pc:
8362 switch (DWARF2_ADDR_SIZE)
8364 case 1:
8365 return DW_FORM_data1;
8366 case 2:
8367 return DW_FORM_data2;
8368 case 4:
8369 return DW_FORM_data4;
8370 case 8:
8371 return DW_FORM_data8;
8372 default:
8373 gcc_unreachable ();
8376 default:
8377 gcc_unreachable ();
8381 /* Output the encoding of an attribute value. */
8383 static void
8384 output_value_format (dw_attr_ref a)
8386 enum dwarf_form form = value_format (a);
8388 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8391 /* Given a die and id, produce the appropriate abbreviations. */
8393 static void
8394 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
8396 unsigned ix;
8397 dw_attr_ref a_attr;
8399 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8400 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8401 dwarf_tag_name (abbrev->die_tag));
8403 if (abbrev->die_child != NULL)
8404 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8405 else
8406 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8408 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
8410 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8411 dwarf_attr_name (a_attr->dw_attr));
8412 output_value_format (a_attr);
8415 dw2_asm_output_data (1, 0, NULL);
8416 dw2_asm_output_data (1, 0, NULL);
8420 /* Output the .debug_abbrev section which defines the DIE abbreviation
8421 table. */
8423 static void
8424 output_abbrev_section (void)
8426 unsigned long abbrev_id;
8428 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8429 output_die_abbrevs (abbrev_id, abbrev_die_table[abbrev_id]);
8431 /* Terminate the table. */
8432 dw2_asm_output_data (1, 0, NULL);
8435 /* Output a symbol we can use to refer to this DIE from another CU. */
8437 static inline void
8438 output_die_symbol (dw_die_ref die)
8440 const char *sym = die->die_id.die_symbol;
8442 gcc_assert (!die->comdat_type_p);
8444 if (sym == 0)
8445 return;
8447 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8448 /* We make these global, not weak; if the target doesn't support
8449 .linkonce, it doesn't support combining the sections, so debugging
8450 will break. */
8451 targetm.asm_out.globalize_label (asm_out_file, sym);
8453 ASM_OUTPUT_LABEL (asm_out_file, sym);
8456 /* Return a new location list, given the begin and end range, and the
8457 expression. */
8459 static inline dw_loc_list_ref
8460 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8461 const char *section)
8463 dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
8465 retlist->begin = begin;
8466 retlist->begin_entry = NULL;
8467 retlist->end = end;
8468 retlist->expr = expr;
8469 retlist->section = section;
8471 return retlist;
8474 /* Generate a new internal symbol for this location list node, if it
8475 hasn't got one yet. */
8477 static inline void
8478 gen_llsym (dw_loc_list_ref list)
8480 gcc_assert (!list->ll_symbol);
8481 list->ll_symbol = gen_internal_sym ("LLST");
8484 /* Output the location list given to us. */
8486 static void
8487 output_loc_list (dw_loc_list_ref list_head)
8489 dw_loc_list_ref curr = list_head;
8491 if (list_head->emitted)
8492 return;
8493 list_head->emitted = true;
8495 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8497 /* Walk the location list, and output each range + expression. */
8498 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8500 unsigned long size;
8501 /* Don't output an entry that starts and ends at the same address. */
8502 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
8503 continue;
8504 size = size_of_locs (curr->expr);
8505 /* If the expression is too large, drop it on the floor. We could
8506 perhaps put it into DW_TAG_dwarf_procedure and refer to that
8507 in the expression, but >= 64KB expressions for a single value
8508 in a single range are unlikely very useful. */
8509 if (size > 0xffff)
8510 continue;
8511 if (dwarf_split_debug_info)
8513 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
8514 "Location list start/length entry (%s)",
8515 list_head->ll_symbol);
8516 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
8517 "Location list range start index (%s)",
8518 curr->begin);
8519 /* The length field is 4 bytes. If we ever need to support
8520 an 8-byte length, we can add a new DW_LLE code or fall back
8521 to DW_LLE_GNU_start_end_entry. */
8522 dw2_asm_output_delta (4, curr->end, curr->begin,
8523 "Location list range length (%s)",
8524 list_head->ll_symbol);
8526 else if (!have_multiple_function_sections)
8528 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8529 "Location list begin address (%s)",
8530 list_head->ll_symbol);
8531 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8532 "Location list end address (%s)",
8533 list_head->ll_symbol);
8535 else
8537 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8538 "Location list begin address (%s)",
8539 list_head->ll_symbol);
8540 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8541 "Location list end address (%s)",
8542 list_head->ll_symbol);
8545 /* Output the block length for this list of location operations. */
8546 gcc_assert (size <= 0xffff);
8547 dw2_asm_output_data (2, size, "%s", "Location expression size");
8549 output_loc_sequence (curr->expr, -1);
8552 if (dwarf_split_debug_info)
8553 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
8554 "Location list terminator (%s)",
8555 list_head->ll_symbol);
8556 else
8558 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8559 "Location list terminator begin (%s)",
8560 list_head->ll_symbol);
8561 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8562 "Location list terminator end (%s)",
8563 list_head->ll_symbol);
8567 /* Output a range_list offset into the debug_range section. Emit a
8568 relocated reference if val_entry is NULL, otherwise, emit an
8569 indirect reference. */
8571 static void
8572 output_range_list_offset (dw_attr_ref a)
8574 const char *name = dwarf_attr_name (a->dw_attr);
8576 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
8578 char *p = strchr (ranges_section_label, '\0');
8579 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX, a->dw_attr_val.v.val_offset);
8580 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8581 debug_ranges_section, "%s", name);
8582 *p = '\0';
8584 else
8585 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8586 "%s (offset from %s)", name, ranges_section_label);
8589 /* Output the offset into the debug_loc section. */
8591 static void
8592 output_loc_list_offset (dw_attr_ref a)
8594 char *sym = AT_loc_list (a)->ll_symbol;
8596 gcc_assert (sym);
8597 if (dwarf_split_debug_info)
8598 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
8599 "%s", dwarf_attr_name (a->dw_attr));
8600 else
8601 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8602 "%s", dwarf_attr_name (a->dw_attr));
8605 /* Output an attribute's index or value appropriately. */
8607 static void
8608 output_attr_index_or_value (dw_attr_ref a)
8610 const char *name = dwarf_attr_name (a->dw_attr);
8612 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8614 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
8615 return;
8617 switch (AT_class (a))
8619 case dw_val_class_addr:
8620 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8621 break;
8622 case dw_val_class_high_pc:
8623 case dw_val_class_lbl_id:
8624 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8625 break;
8626 case dw_val_class_loc_list:
8627 output_loc_list_offset (a);
8628 break;
8629 default:
8630 gcc_unreachable ();
8634 /* Output a type signature. */
8636 static inline void
8637 output_signature (const char *sig, const char *name)
8639 int i;
8641 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8642 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
8645 /* Output the DIE and its attributes. Called recursively to generate
8646 the definitions of each child DIE. */
8648 static void
8649 output_die (dw_die_ref die)
8651 dw_attr_ref a;
8652 dw_die_ref c;
8653 unsigned long size;
8654 unsigned ix;
8656 /* If someone in another CU might refer to us, set up a symbol for
8657 them to point to. */
8658 if (! die->comdat_type_p && die->die_id.die_symbol)
8659 output_die_symbol (die);
8661 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
8662 (unsigned long)die->die_offset,
8663 dwarf_tag_name (die->die_tag));
8665 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8667 const char *name = dwarf_attr_name (a->dw_attr);
8669 switch (AT_class (a))
8671 case dw_val_class_addr:
8672 output_attr_index_or_value (a);
8673 break;
8675 case dw_val_class_offset:
8676 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8677 "%s", name);
8678 break;
8680 case dw_val_class_range_list:
8681 output_range_list_offset (a);
8682 break;
8684 case dw_val_class_loc:
8685 size = size_of_locs (AT_loc (a));
8687 /* Output the block length for this list of location operations. */
8688 if (dwarf_version >= 4)
8689 dw2_asm_output_data_uleb128 (size, "%s", name);
8690 else
8691 dw2_asm_output_data (constant_size (size), size, "%s", name);
8693 output_loc_sequence (AT_loc (a), -1);
8694 break;
8696 case dw_val_class_const:
8697 /* ??? It would be slightly more efficient to use a scheme like is
8698 used for unsigned constants below, but gdb 4.x does not sign
8699 extend. Gdb 5.x does sign extend. */
8700 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8701 break;
8703 case dw_val_class_unsigned_const:
8705 int csize = constant_size (AT_unsigned (a));
8706 if (dwarf_version == 3
8707 && a->dw_attr == DW_AT_data_member_location
8708 && csize >= 4)
8709 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
8710 else
8711 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
8713 break;
8715 case dw_val_class_const_double:
8717 unsigned HOST_WIDE_INT first, second;
8719 if (HOST_BITS_PER_WIDE_INT >= 64)
8720 dw2_asm_output_data (1,
8721 HOST_BITS_PER_DOUBLE_INT
8722 / HOST_BITS_PER_CHAR,
8723 NULL);
8725 if (WORDS_BIG_ENDIAN)
8727 first = a->dw_attr_val.v.val_double.high;
8728 second = a->dw_attr_val.v.val_double.low;
8730 else
8732 first = a->dw_attr_val.v.val_double.low;
8733 second = a->dw_attr_val.v.val_double.high;
8736 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8737 first, "%s", name);
8738 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8739 second, NULL);
8741 break;
8743 case dw_val_class_wide_int:
8745 int i;
8746 int len = get_full_len (*a->dw_attr_val.v.val_wide);
8747 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
8748 if (len * HOST_BITS_PER_WIDE_INT > 64)
8749 dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide) * l,
8750 NULL);
8752 if (WORDS_BIG_ENDIAN)
8753 for (i = len - 1; i >= 0; --i)
8755 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
8756 name);
8757 name = NULL;
8759 else
8760 for (i = 0; i < len; ++i)
8762 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
8763 name);
8764 name = NULL;
8767 break;
8769 case dw_val_class_vec:
8771 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8772 unsigned int len = a->dw_attr_val.v.val_vec.length;
8773 unsigned int i;
8774 unsigned char *p;
8776 dw2_asm_output_data (constant_size (len * elt_size),
8777 len * elt_size, "%s", name);
8778 if (elt_size > sizeof (HOST_WIDE_INT))
8780 elt_size /= 2;
8781 len *= 2;
8783 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8784 i < len;
8785 i++, p += elt_size)
8786 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8787 "fp or vector constant word %u", i);
8788 break;
8791 case dw_val_class_flag:
8792 if (dwarf_version >= 4)
8794 /* Currently all add_AT_flag calls pass in 1 as last argument,
8795 so DW_FORM_flag_present can be used. If that ever changes,
8796 we'll need to use DW_FORM_flag and have some optimization
8797 in build_abbrev_table that will change those to
8798 DW_FORM_flag_present if it is set to 1 in all DIEs using
8799 the same abbrev entry. */
8800 gcc_assert (AT_flag (a) == 1);
8801 if (flag_debug_asm)
8802 fprintf (asm_out_file, "\t\t\t%s %s\n",
8803 ASM_COMMENT_START, name);
8804 break;
8806 dw2_asm_output_data (1, AT_flag (a), "%s", name);
8807 break;
8809 case dw_val_class_loc_list:
8810 output_attr_index_or_value (a);
8811 break;
8813 case dw_val_class_die_ref:
8814 if (AT_ref_external (a))
8816 if (AT_ref (a)->comdat_type_p)
8818 comdat_type_node_ref type_node =
8819 AT_ref (a)->die_id.die_type_node;
8821 gcc_assert (type_node);
8822 output_signature (type_node->signature, name);
8824 else
8826 const char *sym = AT_ref (a)->die_id.die_symbol;
8827 int size;
8829 gcc_assert (sym);
8830 /* In DWARF2, DW_FORM_ref_addr is sized by target address
8831 length, whereas in DWARF3 it's always sized as an
8832 offset. */
8833 if (dwarf_version == 2)
8834 size = DWARF2_ADDR_SIZE;
8835 else
8836 size = DWARF_OFFSET_SIZE;
8837 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
8838 name);
8841 else
8843 gcc_assert (AT_ref (a)->die_offset);
8844 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8845 "%s", name);
8847 break;
8849 case dw_val_class_fde_ref:
8851 char l1[20];
8853 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
8854 a->dw_attr_val.v.val_fde_index * 2);
8855 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
8856 "%s", name);
8858 break;
8860 case dw_val_class_vms_delta:
8861 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
8862 AT_vms_delta2 (a), AT_vms_delta1 (a),
8863 "%s", name);
8864 break;
8866 case dw_val_class_lbl_id:
8867 output_attr_index_or_value (a);
8868 break;
8870 case dw_val_class_lineptr:
8871 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8872 debug_line_section, "%s", name);
8873 break;
8875 case dw_val_class_macptr:
8876 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8877 debug_macinfo_section, "%s", name);
8878 break;
8880 case dw_val_class_str:
8881 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
8882 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8883 a->dw_attr_val.v.val_str->label,
8884 debug_str_section,
8885 "%s: \"%s\"", name, AT_string (a));
8886 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
8887 dw2_asm_output_data_uleb128 (AT_index (a),
8888 "%s: \"%s\"", name, AT_string (a));
8889 else
8890 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
8891 break;
8893 case dw_val_class_file:
8895 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
8897 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
8898 a->dw_attr_val.v.val_file->filename);
8899 break;
8902 case dw_val_class_data8:
8904 int i;
8906 for (i = 0; i < 8; i++)
8907 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
8908 i == 0 ? "%s" : NULL, name);
8909 break;
8912 case dw_val_class_high_pc:
8913 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
8914 get_AT_low_pc (die), "DW_AT_high_pc");
8915 break;
8917 default:
8918 gcc_unreachable ();
8922 FOR_EACH_CHILD (die, c, output_die (c));
8924 /* Add null byte to terminate sibling list. */
8925 if (die->die_child != NULL)
8926 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
8927 (unsigned long) die->die_offset);
8930 /* Output the compilation unit that appears at the beginning of the
8931 .debug_info section, and precedes the DIE descriptions. */
8933 static void
8934 output_compilation_unit_header (void)
8936 int ver = dwarf_version;
8938 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8939 dw2_asm_output_data (4, 0xffffffff,
8940 "Initial length escape value indicating 64-bit DWARF extension");
8941 dw2_asm_output_data (DWARF_OFFSET_SIZE,
8942 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
8943 "Length of Compilation Unit Info");
8944 dw2_asm_output_data (2, ver, "DWARF version number");
8945 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
8946 debug_abbrev_section,
8947 "Offset Into Abbrev. Section");
8948 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8951 /* Output the compilation unit DIE and its children. */
8953 static void
8954 output_comp_unit (dw_die_ref die, int output_if_empty)
8956 const char *secname, *oldsym;
8957 char *tmp;
8959 /* Unless we are outputting main CU, we may throw away empty ones. */
8960 if (!output_if_empty && die->die_child == NULL)
8961 return;
8963 /* Even if there are no children of this DIE, we must output the information
8964 about the compilation unit. Otherwise, on an empty translation unit, we
8965 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
8966 will then complain when examining the file. First mark all the DIEs in
8967 this CU so we know which get local refs. */
8968 mark_dies (die);
8970 external_ref_hash_type *extern_map = optimize_external_refs (die);
8972 build_abbrev_table (die, extern_map);
8974 delete extern_map;
8976 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
8977 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8978 calc_die_sizes (die);
8980 oldsym = die->die_id.die_symbol;
8981 if (oldsym)
8983 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
8985 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
8986 secname = tmp;
8987 die->die_id.die_symbol = NULL;
8988 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8990 else
8992 switch_to_section (debug_info_section);
8993 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
8994 info_section_emitted = true;
8997 /* Output debugging information. */
8998 output_compilation_unit_header ();
8999 output_die (die);
9001 /* Leave the marks on the main CU, so we can check them in
9002 output_pubnames. */
9003 if (oldsym)
9005 unmark_dies (die);
9006 die->die_id.die_symbol = oldsym;
9010 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
9011 and .debug_pubtypes. This is configured per-target, but can be
9012 overridden by the -gpubnames or -gno-pubnames options. */
9014 static inline bool
9015 want_pubnames (void)
9017 if (debug_info_level <= DINFO_LEVEL_TERSE)
9018 return false;
9019 if (debug_generate_pub_sections != -1)
9020 return debug_generate_pub_sections;
9021 return targetm.want_debug_pub_sections;
9024 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
9026 static void
9027 add_AT_pubnames (dw_die_ref die)
9029 if (want_pubnames ())
9030 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
9033 /* Add a string attribute value to a skeleton DIE. */
9035 static inline void
9036 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
9037 const char *str)
9039 dw_attr_node attr;
9040 struct indirect_string_node *node;
9042 if (! skeleton_debug_str_hash)
9043 skeleton_debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
9044 debug_str_eq, NULL);
9046 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
9047 find_string_form (node);
9048 if (node->form == DW_FORM_GNU_str_index)
9049 node->form = DW_FORM_strp;
9051 attr.dw_attr = attr_kind;
9052 attr.dw_attr_val.val_class = dw_val_class_str;
9053 attr.dw_attr_val.val_entry = NULL;
9054 attr.dw_attr_val.v.val_str = node;
9055 add_dwarf_attr (die, &attr);
9058 /* Helper function to generate top-level dies for skeleton debug_info and
9059 debug_types. */
9061 static void
9062 add_top_level_skeleton_die_attrs (dw_die_ref die)
9064 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
9065 const char *comp_dir = comp_dir_string ();
9067 add_skeleton_AT_string (die, DW_AT_GNU_dwo_name, dwo_file_name);
9068 if (comp_dir != NULL)
9069 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
9070 add_AT_pubnames (die);
9071 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
9074 /* Output skeleton debug sections that point to the dwo file. */
9076 static void
9077 output_skeleton_debug_sections (dw_die_ref comp_unit)
9079 /* These attributes will be found in the full debug_info section. */
9080 remove_AT (comp_unit, DW_AT_producer);
9081 remove_AT (comp_unit, DW_AT_language);
9083 switch_to_section (debug_skeleton_info_section);
9084 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
9086 /* Produce the skeleton compilation-unit header. This one differs enough from
9087 a normal CU header that it's better not to call output_compilation_unit
9088 header. */
9089 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9090 dw2_asm_output_data (4, 0xffffffff,
9091 "Initial length escape value indicating 64-bit DWARF extension");
9093 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9094 DWARF_COMPILE_UNIT_HEADER_SIZE
9095 - DWARF_INITIAL_LENGTH_SIZE
9096 + size_of_die (comp_unit),
9097 "Length of Compilation Unit Info");
9098 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
9099 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
9100 debug_abbrev_section,
9101 "Offset Into Abbrev. Section");
9102 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9104 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
9105 output_die (comp_unit);
9107 /* Build the skeleton debug_abbrev section. */
9108 switch_to_section (debug_skeleton_abbrev_section);
9109 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
9111 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
9113 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
9116 /* Output a comdat type unit DIE and its children. */
9118 static void
9119 output_comdat_type_unit (comdat_type_node *node)
9121 const char *secname;
9122 char *tmp;
9123 int i;
9124 #if defined (OBJECT_FORMAT_ELF)
9125 tree comdat_key;
9126 #endif
9128 /* First mark all the DIEs in this CU so we know which get local refs. */
9129 mark_dies (node->root_die);
9131 external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
9133 build_abbrev_table (node->root_die, extern_map);
9135 delete extern_map;
9136 extern_map = NULL;
9138 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
9139 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
9140 calc_die_sizes (node->root_die);
9142 #if defined (OBJECT_FORMAT_ELF)
9143 if (!dwarf_split_debug_info)
9144 secname = ".debug_types";
9145 else
9146 secname = ".debug_types.dwo";
9148 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9149 sprintf (tmp, "wt.");
9150 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9151 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
9152 comdat_key = get_identifier (tmp);
9153 targetm.asm_out.named_section (secname,
9154 SECTION_DEBUG | SECTION_LINKONCE,
9155 comdat_key);
9156 #else
9157 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9158 sprintf (tmp, ".gnu.linkonce.wt.");
9159 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9160 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
9161 secname = tmp;
9162 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9163 #endif
9165 /* Output debugging information. */
9166 output_compilation_unit_header ();
9167 output_signature (node->signature, "Type Signature");
9168 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
9169 "Offset to Type DIE");
9170 output_die (node->root_die);
9172 unmark_dies (node->root_die);
9175 /* Return the DWARF2/3 pubname associated with a decl. */
9177 static const char *
9178 dwarf2_name (tree decl, int scope)
9180 if (DECL_NAMELESS (decl))
9181 return NULL;
9182 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
9185 /* Add a new entry to .debug_pubnames if appropriate. */
9187 static void
9188 add_pubname_string (const char *str, dw_die_ref die)
9190 pubname_entry e;
9192 e.die = die;
9193 e.name = xstrdup (str);
9194 vec_safe_push (pubname_table, e);
9197 static void
9198 add_pubname (tree decl, dw_die_ref die)
9200 if (!want_pubnames ())
9201 return;
9203 /* Don't add items to the table when we expect that the consumer will have
9204 just read the enclosing die. For example, if the consumer is looking at a
9205 class_member, it will either be inside the class already, or will have just
9206 looked up the class to find the member. Either way, searching the class is
9207 faster than searching the index. */
9208 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
9209 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9211 const char *name = dwarf2_name (decl, 1);
9213 if (name)
9214 add_pubname_string (name, die);
9218 /* Add an enumerator to the pubnames section. */
9220 static void
9221 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
9223 pubname_entry e;
9225 gcc_assert (scope_name);
9226 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
9227 e.die = die;
9228 vec_safe_push (pubname_table, e);
9231 /* Add a new entry to .debug_pubtypes if appropriate. */
9233 static void
9234 add_pubtype (tree decl, dw_die_ref die)
9236 pubname_entry e;
9238 if (!want_pubnames ())
9239 return;
9241 if ((TREE_PUBLIC (decl)
9242 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9243 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
9245 tree scope = NULL;
9246 const char *scope_name = "";
9247 const char *sep = is_cxx () ? "::" : ".";
9248 const char *name;
9250 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
9251 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
9253 scope_name = lang_hooks.dwarf_name (scope, 1);
9254 if (scope_name != NULL && scope_name[0] != '\0')
9255 scope_name = concat (scope_name, sep, NULL);
9256 else
9257 scope_name = "";
9260 if (TYPE_P (decl))
9261 name = type_tag (decl);
9262 else
9263 name = lang_hooks.dwarf_name (decl, 1);
9265 /* If we don't have a name for the type, there's no point in adding
9266 it to the table. */
9267 if (name != NULL && name[0] != '\0')
9269 e.die = die;
9270 e.name = concat (scope_name, name, NULL);
9271 vec_safe_push (pubtype_table, e);
9274 /* Although it might be more consistent to add the pubinfo for the
9275 enumerators as their dies are created, they should only be added if the
9276 enum type meets the criteria above. So rather than re-check the parent
9277 enum type whenever an enumerator die is created, just output them all
9278 here. This isn't protected by the name conditional because anonymous
9279 enums don't have names. */
9280 if (die->die_tag == DW_TAG_enumeration_type)
9282 dw_die_ref c;
9284 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
9289 /* Output a single entry in the pubnames table. */
9291 static void
9292 output_pubname (dw_offset die_offset, pubname_entry *entry)
9294 dw_die_ref die = entry->die;
9295 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
9297 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
9299 if (debug_generate_pub_sections == 2)
9301 /* This logic follows gdb's method for determining the value of the flag
9302 byte. */
9303 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
9304 switch (die->die_tag)
9306 case DW_TAG_typedef:
9307 case DW_TAG_base_type:
9308 case DW_TAG_subrange_type:
9309 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9310 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9311 break;
9312 case DW_TAG_enumerator:
9313 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9314 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9315 if (!is_cxx () && !is_java ())
9316 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9317 break;
9318 case DW_TAG_subprogram:
9319 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9320 GDB_INDEX_SYMBOL_KIND_FUNCTION);
9321 if (!is_ada ())
9322 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9323 break;
9324 case DW_TAG_constant:
9325 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9326 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9327 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9328 break;
9329 case DW_TAG_variable:
9330 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9331 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9332 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9333 break;
9334 case DW_TAG_namespace:
9335 case DW_TAG_imported_declaration:
9336 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9337 break;
9338 case DW_TAG_class_type:
9339 case DW_TAG_interface_type:
9340 case DW_TAG_structure_type:
9341 case DW_TAG_union_type:
9342 case DW_TAG_enumeration_type:
9343 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9344 if (!is_cxx () && !is_java ())
9345 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9346 break;
9347 default:
9348 /* An unusual tag. Leave the flag-byte empty. */
9349 break;
9351 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
9352 "GDB-index flags");
9355 dw2_asm_output_nstring (entry->name, -1, "external name");
9359 /* Output the public names table used to speed up access to externally
9360 visible names; or the public types table used to find type definitions. */
9362 static void
9363 output_pubnames (vec<pubname_entry, va_gc> *names)
9365 unsigned i;
9366 unsigned long pubnames_length = size_of_pubnames (names);
9367 pubname_ref pub;
9369 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9370 dw2_asm_output_data (4, 0xffffffff,
9371 "Initial length escape value indicating 64-bit DWARF extension");
9372 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length, "Pub Info Length");
9374 /* Version number for pubnames/pubtypes is independent of dwarf version. */
9375 dw2_asm_output_data (2, 2, "DWARF Version");
9377 if (dwarf_split_debug_info)
9378 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9379 debug_skeleton_info_section,
9380 "Offset of Compilation Unit Info");
9381 else
9382 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9383 debug_info_section,
9384 "Offset of Compilation Unit Info");
9385 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
9386 "Compilation Unit Length");
9388 FOR_EACH_VEC_ELT (*names, i, pub)
9390 if (include_pubname_in_output (names, pub))
9392 dw_offset die_offset = pub->die->die_offset;
9394 /* We shouldn't see pubnames for DIEs outside of the main CU. */
9395 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
9396 gcc_assert (pub->die->die_mark);
9398 /* If we're putting types in their own .debug_types sections,
9399 the .debug_pubtypes table will still point to the compile
9400 unit (not the type unit), so we want to use the offset of
9401 the skeleton DIE (if there is one). */
9402 if (pub->die->comdat_type_p && names == pubtype_table)
9404 comdat_type_node_ref type_node = pub->die->die_id.die_type_node;
9406 if (type_node != NULL)
9407 die_offset = (type_node->skeleton_die != NULL
9408 ? type_node->skeleton_die->die_offset
9409 : comp_unit_die ()->die_offset);
9412 output_pubname (die_offset, pub);
9416 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
9419 /* Output public names and types tables if necessary. */
9421 static void
9422 output_pubtables (void)
9424 if (!want_pubnames () || !info_section_emitted)
9425 return;
9427 switch_to_section (debug_pubnames_section);
9428 output_pubnames (pubname_table);
9429 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
9430 It shouldn't hurt to emit it always, since pure DWARF2 consumers
9431 simply won't look for the section. */
9432 switch_to_section (debug_pubtypes_section);
9433 output_pubnames (pubtype_table);
9437 /* Output the information that goes into the .debug_aranges table.
9438 Namely, define the beginning and ending address range of the
9439 text section generated for this compilation unit. */
9441 static void
9442 output_aranges (unsigned long aranges_length)
9444 unsigned i;
9446 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9447 dw2_asm_output_data (4, 0xffffffff,
9448 "Initial length escape value indicating 64-bit DWARF extension");
9449 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
9450 "Length of Address Ranges Info");
9451 /* Version number for aranges is still 2, even in DWARF3. */
9452 dw2_asm_output_data (2, 2, "DWARF Version");
9453 if (dwarf_split_debug_info)
9454 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9455 debug_skeleton_info_section,
9456 "Offset of Compilation Unit Info");
9457 else
9458 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9459 debug_info_section,
9460 "Offset of Compilation Unit Info");
9461 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
9462 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
9464 /* We need to align to twice the pointer size here. */
9465 if (DWARF_ARANGES_PAD_SIZE)
9467 /* Pad using a 2 byte words so that padding is correct for any
9468 pointer size. */
9469 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
9470 2 * DWARF2_ADDR_SIZE);
9471 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
9472 dw2_asm_output_data (2, 0, NULL);
9475 /* It is necessary not to output these entries if the sections were
9476 not used; if the sections were not used, the length will be 0 and
9477 the address may end up as 0 if the section is discarded by ld
9478 --gc-sections, leaving an invalid (0, 0) entry that can be
9479 confused with the terminator. */
9480 if (text_section_used)
9482 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
9483 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
9484 text_section_label, "Length");
9486 if (cold_text_section_used)
9488 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
9489 "Address");
9490 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
9491 cold_text_section_label, "Length");
9494 if (have_multiple_function_sections)
9496 unsigned fde_idx;
9497 dw_fde_ref fde;
9499 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9501 if (DECL_IGNORED_P (fde->decl))
9502 continue;
9503 if (!fde->in_std_section)
9505 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
9506 "Address");
9507 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
9508 fde->dw_fde_begin, "Length");
9510 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9512 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
9513 "Address");
9514 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
9515 fde->dw_fde_second_begin, "Length");
9520 /* Output the terminator words. */
9521 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9522 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9525 /* Add a new entry to .debug_ranges. Return the offset at which it
9526 was placed. */
9528 static unsigned int
9529 add_ranges_num (int num)
9531 unsigned int in_use = ranges_table_in_use;
9533 if (in_use == ranges_table_allocated)
9535 ranges_table_allocated += RANGES_TABLE_INCREMENT;
9536 ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
9537 ranges_table_allocated);
9538 memset (ranges_table + ranges_table_in_use, 0,
9539 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
9542 ranges_table[in_use].num = num;
9543 ranges_table_in_use = in_use + 1;
9545 return in_use * 2 * DWARF2_ADDR_SIZE;
9548 /* Add a new entry to .debug_ranges corresponding to a block, or a
9549 range terminator if BLOCK is NULL. */
9551 static unsigned int
9552 add_ranges (const_tree block)
9554 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
9557 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
9558 When using dwarf_split_debug_info, address attributes in dies destined
9559 for the final executable should be direct references--setting the
9560 parameter force_direct ensures this behavior. */
9562 static void
9563 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
9564 bool *added, bool force_direct)
9566 unsigned int in_use = ranges_by_label_in_use;
9567 unsigned int offset;
9569 if (in_use == ranges_by_label_allocated)
9571 ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
9572 ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
9573 ranges_by_label,
9574 ranges_by_label_allocated);
9575 memset (ranges_by_label + ranges_by_label_in_use, 0,
9576 RANGES_TABLE_INCREMENT
9577 * sizeof (struct dw_ranges_by_label_struct));
9580 ranges_by_label[in_use].begin = begin;
9581 ranges_by_label[in_use].end = end;
9582 ranges_by_label_in_use = in_use + 1;
9584 offset = add_ranges_num (-(int)in_use - 1);
9585 if (!*added)
9587 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
9588 *added = true;
9592 static void
9593 output_ranges (void)
9595 unsigned i;
9596 static const char *const start_fmt = "Offset %#x";
9597 const char *fmt = start_fmt;
9599 for (i = 0; i < ranges_table_in_use; i++)
9601 int block_num = ranges_table[i].num;
9603 if (block_num > 0)
9605 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
9606 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
9608 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
9609 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
9611 /* If all code is in the text section, then the compilation
9612 unit base address defaults to DW_AT_low_pc, which is the
9613 base of the text section. */
9614 if (!have_multiple_function_sections)
9616 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
9617 text_section_label,
9618 fmt, i * 2 * DWARF2_ADDR_SIZE);
9619 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
9620 text_section_label, NULL);
9623 /* Otherwise, the compilation unit base address is zero,
9624 which allows us to use absolute addresses, and not worry
9625 about whether the target supports cross-section
9626 arithmetic. */
9627 else
9629 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
9630 fmt, i * 2 * DWARF2_ADDR_SIZE);
9631 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
9634 fmt = NULL;
9637 /* Negative block_num stands for an index into ranges_by_label. */
9638 else if (block_num < 0)
9640 int lab_idx = - block_num - 1;
9642 if (!have_multiple_function_sections)
9644 gcc_unreachable ();
9645 #if 0
9646 /* If we ever use add_ranges_by_labels () for a single
9647 function section, all we have to do is to take out
9648 the #if 0 above. */
9649 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9650 ranges_by_label[lab_idx].begin,
9651 text_section_label,
9652 fmt, i * 2 * DWARF2_ADDR_SIZE);
9653 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9654 ranges_by_label[lab_idx].end,
9655 text_section_label, NULL);
9656 #endif
9658 else
9660 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9661 ranges_by_label[lab_idx].begin,
9662 fmt, i * 2 * DWARF2_ADDR_SIZE);
9663 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9664 ranges_by_label[lab_idx].end,
9665 NULL);
9668 else
9670 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9671 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9672 fmt = start_fmt;
9677 /* Data structure containing information about input files. */
9678 struct file_info
9680 const char *path; /* Complete file name. */
9681 const char *fname; /* File name part. */
9682 int length; /* Length of entire string. */
9683 struct dwarf_file_data * file_idx; /* Index in input file table. */
9684 int dir_idx; /* Index in directory table. */
9687 /* Data structure containing information about directories with source
9688 files. */
9689 struct dir_info
9691 const char *path; /* Path including directory name. */
9692 int length; /* Path length. */
9693 int prefix; /* Index of directory entry which is a prefix. */
9694 int count; /* Number of files in this directory. */
9695 int dir_idx; /* Index of directory used as base. */
9698 /* Callback function for file_info comparison. We sort by looking at
9699 the directories in the path. */
9701 static int
9702 file_info_cmp (const void *p1, const void *p2)
9704 const struct file_info *const s1 = (const struct file_info *) p1;
9705 const struct file_info *const s2 = (const struct file_info *) p2;
9706 const unsigned char *cp1;
9707 const unsigned char *cp2;
9709 /* Take care of file names without directories. We need to make sure that
9710 we return consistent values to qsort since some will get confused if
9711 we return the same value when identical operands are passed in opposite
9712 orders. So if neither has a directory, return 0 and otherwise return
9713 1 or -1 depending on which one has the directory. */
9714 if ((s1->path == s1->fname || s2->path == s2->fname))
9715 return (s2->path == s2->fname) - (s1->path == s1->fname);
9717 cp1 = (const unsigned char *) s1->path;
9718 cp2 = (const unsigned char *) s2->path;
9720 while (1)
9722 ++cp1;
9723 ++cp2;
9724 /* Reached the end of the first path? If so, handle like above. */
9725 if ((cp1 == (const unsigned char *) s1->fname)
9726 || (cp2 == (const unsigned char *) s2->fname))
9727 return ((cp2 == (const unsigned char *) s2->fname)
9728 - (cp1 == (const unsigned char *) s1->fname));
9730 /* Character of current path component the same? */
9731 else if (*cp1 != *cp2)
9732 return *cp1 - *cp2;
9736 struct file_name_acquire_data
9738 struct file_info *files;
9739 int used_files;
9740 int max_files;
9743 /* Traversal function for the hash table. */
9745 static int
9746 file_name_acquire (void ** slot, void *data)
9748 struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
9749 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
9750 struct file_info *fi;
9751 const char *f;
9753 gcc_assert (fnad->max_files >= d->emitted_number);
9755 if (! d->emitted_number)
9756 return 1;
9758 gcc_assert (fnad->max_files != fnad->used_files);
9760 fi = fnad->files + fnad->used_files++;
9762 /* Skip all leading "./". */
9763 f = d->filename;
9764 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
9765 f += 2;
9767 /* Create a new array entry. */
9768 fi->path = f;
9769 fi->length = strlen (f);
9770 fi->file_idx = d;
9772 /* Search for the file name part. */
9773 f = strrchr (f, DIR_SEPARATOR);
9774 #if defined (DIR_SEPARATOR_2)
9776 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
9778 if (g != NULL)
9780 if (f == NULL || f < g)
9781 f = g;
9784 #endif
9786 fi->fname = f == NULL ? fi->path : f + 1;
9787 return 1;
9790 /* Output the directory table and the file name table. We try to minimize
9791 the total amount of memory needed. A heuristic is used to avoid large
9792 slowdowns with many input files. */
9794 static void
9795 output_file_names (void)
9797 struct file_name_acquire_data fnad;
9798 int numfiles;
9799 struct file_info *files;
9800 struct dir_info *dirs;
9801 int *saved;
9802 int *savehere;
9803 int *backmap;
9804 int ndirs;
9805 int idx_offset;
9806 int i;
9808 if (!last_emitted_file)
9810 dw2_asm_output_data (1, 0, "End directory table");
9811 dw2_asm_output_data (1, 0, "End file name table");
9812 return;
9815 numfiles = last_emitted_file->emitted_number;
9817 /* Allocate the various arrays we need. */
9818 files = XALLOCAVEC (struct file_info, numfiles);
9819 dirs = XALLOCAVEC (struct dir_info, numfiles);
9821 fnad.files = files;
9822 fnad.used_files = 0;
9823 fnad.max_files = numfiles;
9824 htab_traverse (file_table, file_name_acquire, &fnad);
9825 gcc_assert (fnad.used_files == fnad.max_files);
9827 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
9829 /* Find all the different directories used. */
9830 dirs[0].path = files[0].path;
9831 dirs[0].length = files[0].fname - files[0].path;
9832 dirs[0].prefix = -1;
9833 dirs[0].count = 1;
9834 dirs[0].dir_idx = 0;
9835 files[0].dir_idx = 0;
9836 ndirs = 1;
9838 for (i = 1; i < numfiles; i++)
9839 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
9840 && memcmp (dirs[ndirs - 1].path, files[i].path,
9841 dirs[ndirs - 1].length) == 0)
9843 /* Same directory as last entry. */
9844 files[i].dir_idx = ndirs - 1;
9845 ++dirs[ndirs - 1].count;
9847 else
9849 int j;
9851 /* This is a new directory. */
9852 dirs[ndirs].path = files[i].path;
9853 dirs[ndirs].length = files[i].fname - files[i].path;
9854 dirs[ndirs].count = 1;
9855 dirs[ndirs].dir_idx = ndirs;
9856 files[i].dir_idx = ndirs;
9858 /* Search for a prefix. */
9859 dirs[ndirs].prefix = -1;
9860 for (j = 0; j < ndirs; j++)
9861 if (dirs[j].length < dirs[ndirs].length
9862 && dirs[j].length > 1
9863 && (dirs[ndirs].prefix == -1
9864 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
9865 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
9866 dirs[ndirs].prefix = j;
9868 ++ndirs;
9871 /* Now to the actual work. We have to find a subset of the directories which
9872 allow expressing the file name using references to the directory table
9873 with the least amount of characters. We do not do an exhaustive search
9874 where we would have to check out every combination of every single
9875 possible prefix. Instead we use a heuristic which provides nearly optimal
9876 results in most cases and never is much off. */
9877 saved = XALLOCAVEC (int, ndirs);
9878 savehere = XALLOCAVEC (int, ndirs);
9880 memset (saved, '\0', ndirs * sizeof (saved[0]));
9881 for (i = 0; i < ndirs; i++)
9883 int j;
9884 int total;
9886 /* We can always save some space for the current directory. But this
9887 does not mean it will be enough to justify adding the directory. */
9888 savehere[i] = dirs[i].length;
9889 total = (savehere[i] - saved[i]) * dirs[i].count;
9891 for (j = i + 1; j < ndirs; j++)
9893 savehere[j] = 0;
9894 if (saved[j] < dirs[i].length)
9896 /* Determine whether the dirs[i] path is a prefix of the
9897 dirs[j] path. */
9898 int k;
9900 k = dirs[j].prefix;
9901 while (k != -1 && k != (int) i)
9902 k = dirs[k].prefix;
9904 if (k == (int) i)
9906 /* Yes it is. We can possibly save some memory by
9907 writing the filenames in dirs[j] relative to
9908 dirs[i]. */
9909 savehere[j] = dirs[i].length;
9910 total += (savehere[j] - saved[j]) * dirs[j].count;
9915 /* Check whether we can save enough to justify adding the dirs[i]
9916 directory. */
9917 if (total > dirs[i].length + 1)
9919 /* It's worthwhile adding. */
9920 for (j = i; j < ndirs; j++)
9921 if (savehere[j] > 0)
9923 /* Remember how much we saved for this directory so far. */
9924 saved[j] = savehere[j];
9926 /* Remember the prefix directory. */
9927 dirs[j].dir_idx = i;
9932 /* Emit the directory name table. */
9933 idx_offset = dirs[0].length > 0 ? 1 : 0;
9934 for (i = 1 - idx_offset; i < ndirs; i++)
9935 dw2_asm_output_nstring (dirs[i].path,
9936 dirs[i].length
9937 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
9938 "Directory Entry: %#x", i + idx_offset);
9940 dw2_asm_output_data (1, 0, "End directory table");
9942 /* We have to emit them in the order of emitted_number since that's
9943 used in the debug info generation. To do this efficiently we
9944 generate a back-mapping of the indices first. */
9945 backmap = XALLOCAVEC (int, numfiles);
9946 for (i = 0; i < numfiles; i++)
9947 backmap[files[i].file_idx->emitted_number - 1] = i;
9949 /* Now write all the file names. */
9950 for (i = 0; i < numfiles; i++)
9952 int file_idx = backmap[i];
9953 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
9955 #ifdef VMS_DEBUGGING_INFO
9956 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
9958 /* Setting these fields can lead to debugger miscomparisons,
9959 but VMS Debug requires them to be set correctly. */
9961 int ver;
9962 long long cdt;
9963 long siz;
9964 int maxfilelen = strlen (files[file_idx].path)
9965 + dirs[dir_idx].length
9966 + MAX_VMS_VERSION_LEN + 1;
9967 char *filebuf = XALLOCAVEC (char, maxfilelen);
9969 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
9970 snprintf (filebuf, maxfilelen, "%s;%d",
9971 files[file_idx].path + dirs[dir_idx].length, ver);
9973 dw2_asm_output_nstring
9974 (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
9976 /* Include directory index. */
9977 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9979 /* Modification time. */
9980 dw2_asm_output_data_uleb128
9981 ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
9982 ? cdt : 0,
9983 NULL);
9985 /* File length in bytes. */
9986 dw2_asm_output_data_uleb128
9987 ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
9988 ? siz : 0,
9989 NULL);
9990 #else
9991 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
9992 "File Entry: %#x", (unsigned) i + 1);
9994 /* Include directory index. */
9995 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9997 /* Modification time. */
9998 dw2_asm_output_data_uleb128 (0, NULL);
10000 /* File length in bytes. */
10001 dw2_asm_output_data_uleb128 (0, NULL);
10002 #endif /* VMS_DEBUGGING_INFO */
10005 dw2_asm_output_data (1, 0, "End file name table");
10009 /* Output one line number table into the .debug_line section. */
10011 static void
10012 output_one_line_info_table (dw_line_info_table *table)
10014 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
10015 unsigned int current_line = 1;
10016 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
10017 dw_line_info_entry *ent;
10018 size_t i;
10020 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
10022 switch (ent->opcode)
10024 case LI_set_address:
10025 /* ??? Unfortunately, we have little choice here currently, and
10026 must always use the most general form. GCC does not know the
10027 address delta itself, so we can't use DW_LNS_advance_pc. Many
10028 ports do have length attributes which will give an upper bound
10029 on the address range. We could perhaps use length attributes
10030 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
10031 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
10033 /* This can handle any delta. This takes
10034 4+DWARF2_ADDR_SIZE bytes. */
10035 dw2_asm_output_data (1, 0, "set address %s", line_label);
10036 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10037 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10038 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
10039 break;
10041 case LI_set_line:
10042 if (ent->val == current_line)
10044 /* We still need to start a new row, so output a copy insn. */
10045 dw2_asm_output_data (1, DW_LNS_copy,
10046 "copy line %u", current_line);
10048 else
10050 int line_offset = ent->val - current_line;
10051 int line_delta = line_offset - DWARF_LINE_BASE;
10053 current_line = ent->val;
10054 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
10056 /* This can handle deltas from -10 to 234, using the current
10057 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
10058 This takes 1 byte. */
10059 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
10060 "line %u", current_line);
10062 else
10064 /* This can handle any delta. This takes at least 4 bytes,
10065 depending on the value being encoded. */
10066 dw2_asm_output_data (1, DW_LNS_advance_line,
10067 "advance to line %u", current_line);
10068 dw2_asm_output_data_sleb128 (line_offset, NULL);
10069 dw2_asm_output_data (1, DW_LNS_copy, NULL);
10072 break;
10074 case LI_set_file:
10075 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
10076 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10077 break;
10079 case LI_set_column:
10080 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
10081 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10082 break;
10084 case LI_negate_stmt:
10085 current_is_stmt = !current_is_stmt;
10086 dw2_asm_output_data (1, DW_LNS_negate_stmt,
10087 "is_stmt %d", current_is_stmt);
10088 break;
10090 case LI_set_prologue_end:
10091 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
10092 "set prologue end");
10093 break;
10095 case LI_set_epilogue_begin:
10096 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
10097 "set epilogue begin");
10098 break;
10100 case LI_set_discriminator:
10101 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
10102 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
10103 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
10104 dw2_asm_output_data_uleb128 (ent->val, NULL);
10105 break;
10109 /* Emit debug info for the address of the end of the table. */
10110 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
10111 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10112 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10113 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
10115 dw2_asm_output_data (1, 0, "end sequence");
10116 dw2_asm_output_data_uleb128 (1, NULL);
10117 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
10120 /* Output the source line number correspondence information. This
10121 information goes into the .debug_line section. */
10123 static void
10124 output_line_info (bool prologue_only)
10126 char l1[20], l2[20], p1[20], p2[20];
10127 int ver = dwarf_version;
10128 bool saw_one = false;
10129 int opc;
10131 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
10132 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
10133 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
10134 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
10136 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10137 dw2_asm_output_data (4, 0xffffffff,
10138 "Initial length escape value indicating 64-bit DWARF extension");
10139 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
10140 "Length of Source Line Info");
10141 ASM_OUTPUT_LABEL (asm_out_file, l1);
10143 dw2_asm_output_data (2, ver, "DWARF Version");
10144 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
10145 ASM_OUTPUT_LABEL (asm_out_file, p1);
10147 /* Define the architecture-dependent minimum instruction length (in bytes).
10148 In this implementation of DWARF, this field is used for information
10149 purposes only. Since GCC generates assembly language, we have no
10150 a priori knowledge of how many instruction bytes are generated for each
10151 source line, and therefore can use only the DW_LNE_set_address and
10152 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
10153 this as '1', which is "correct enough" for all architectures,
10154 and don't let the target override. */
10155 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
10157 if (ver >= 4)
10158 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
10159 "Maximum Operations Per Instruction");
10160 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
10161 "Default is_stmt_start flag");
10162 dw2_asm_output_data (1, DWARF_LINE_BASE,
10163 "Line Base Value (Special Opcodes)");
10164 dw2_asm_output_data (1, DWARF_LINE_RANGE,
10165 "Line Range Value (Special Opcodes)");
10166 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
10167 "Special Opcode Base");
10169 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
10171 int n_op_args;
10172 switch (opc)
10174 case DW_LNS_advance_pc:
10175 case DW_LNS_advance_line:
10176 case DW_LNS_set_file:
10177 case DW_LNS_set_column:
10178 case DW_LNS_fixed_advance_pc:
10179 case DW_LNS_set_isa:
10180 n_op_args = 1;
10181 break;
10182 default:
10183 n_op_args = 0;
10184 break;
10187 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
10188 opc, n_op_args);
10191 /* Write out the information about the files we use. */
10192 output_file_names ();
10193 ASM_OUTPUT_LABEL (asm_out_file, p2);
10194 if (prologue_only)
10196 /* Output the marker for the end of the line number info. */
10197 ASM_OUTPUT_LABEL (asm_out_file, l2);
10198 return;
10201 if (separate_line_info)
10203 dw_line_info_table *table;
10204 size_t i;
10206 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
10207 if (table->in_use)
10209 output_one_line_info_table (table);
10210 saw_one = true;
10213 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
10215 output_one_line_info_table (cold_text_section_line_info);
10216 saw_one = true;
10219 /* ??? Some Darwin linkers crash on a .debug_line section with no
10220 sequences. Further, merely a DW_LNE_end_sequence entry is not
10221 sufficient -- the address column must also be initialized.
10222 Make sure to output at least one set_address/end_sequence pair,
10223 choosing .text since that section is always present. */
10224 if (text_section_line_info->in_use || !saw_one)
10225 output_one_line_info_table (text_section_line_info);
10227 /* Output the marker for the end of the line number info. */
10228 ASM_OUTPUT_LABEL (asm_out_file, l2);
10231 /* Given a pointer to a tree node for some base type, return a pointer to
10232 a DIE that describes the given type.
10234 This routine must only be called for GCC type nodes that correspond to
10235 Dwarf base (fundamental) types. */
10237 static dw_die_ref
10238 base_type_die (tree type)
10240 dw_die_ref base_type_result;
10241 enum dwarf_type encoding;
10243 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
10244 return 0;
10246 /* If this is a subtype that should not be emitted as a subrange type,
10247 use the base type. See subrange_type_for_debug_p. */
10248 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
10249 type = TREE_TYPE (type);
10251 switch (TREE_CODE (type))
10253 case INTEGER_TYPE:
10254 if ((dwarf_version >= 4 || !dwarf_strict)
10255 && TYPE_NAME (type)
10256 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10257 && DECL_IS_BUILTIN (TYPE_NAME (type))
10258 && DECL_NAME (TYPE_NAME (type)))
10260 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
10261 if (strcmp (name, "char16_t") == 0
10262 || strcmp (name, "char32_t") == 0)
10264 encoding = DW_ATE_UTF;
10265 break;
10268 if (TYPE_STRING_FLAG (type))
10270 if (TYPE_UNSIGNED (type))
10271 encoding = DW_ATE_unsigned_char;
10272 else
10273 encoding = DW_ATE_signed_char;
10275 else if (TYPE_UNSIGNED (type))
10276 encoding = DW_ATE_unsigned;
10277 else
10278 encoding = DW_ATE_signed;
10279 break;
10281 case REAL_TYPE:
10282 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
10284 if (dwarf_version >= 3 || !dwarf_strict)
10285 encoding = DW_ATE_decimal_float;
10286 else
10287 encoding = DW_ATE_lo_user;
10289 else
10290 encoding = DW_ATE_float;
10291 break;
10293 case FIXED_POINT_TYPE:
10294 if (!(dwarf_version >= 3 || !dwarf_strict))
10295 encoding = DW_ATE_lo_user;
10296 else if (TYPE_UNSIGNED (type))
10297 encoding = DW_ATE_unsigned_fixed;
10298 else
10299 encoding = DW_ATE_signed_fixed;
10300 break;
10302 /* Dwarf2 doesn't know anything about complex ints, so use
10303 a user defined type for it. */
10304 case COMPLEX_TYPE:
10305 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
10306 encoding = DW_ATE_complex_float;
10307 else
10308 encoding = DW_ATE_lo_user;
10309 break;
10311 case BOOLEAN_TYPE:
10312 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
10313 encoding = DW_ATE_boolean;
10314 break;
10316 default:
10317 /* No other TREE_CODEs are Dwarf fundamental types. */
10318 gcc_unreachable ();
10321 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
10323 add_AT_unsigned (base_type_result, DW_AT_byte_size,
10324 int_size_in_bytes (type));
10325 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
10326 add_pubtype (type, base_type_result);
10328 return base_type_result;
10331 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
10332 named 'auto' in its type: return true for it, false otherwise. */
10334 static inline bool
10335 is_cxx_auto (tree type)
10337 if (is_cxx ())
10339 tree name = TYPE_IDENTIFIER (type);
10340 if (name == get_identifier ("auto")
10341 || name == get_identifier ("decltype(auto)"))
10342 return true;
10344 return false;
10347 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
10348 given input type is a Dwarf "fundamental" type. Otherwise return null. */
10350 static inline int
10351 is_base_type (tree type)
10353 switch (TREE_CODE (type))
10355 case ERROR_MARK:
10356 case VOID_TYPE:
10357 case INTEGER_TYPE:
10358 case REAL_TYPE:
10359 case FIXED_POINT_TYPE:
10360 case COMPLEX_TYPE:
10361 case BOOLEAN_TYPE:
10362 return 1;
10364 case ARRAY_TYPE:
10365 case RECORD_TYPE:
10366 case UNION_TYPE:
10367 case QUAL_UNION_TYPE:
10368 case ENUMERAL_TYPE:
10369 case FUNCTION_TYPE:
10370 case METHOD_TYPE:
10371 case POINTER_TYPE:
10372 case REFERENCE_TYPE:
10373 case NULLPTR_TYPE:
10374 case OFFSET_TYPE:
10375 case LANG_TYPE:
10376 case VECTOR_TYPE:
10377 return 0;
10379 default:
10380 if (is_cxx_auto (type))
10381 return 0;
10382 gcc_unreachable ();
10385 return 0;
10388 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
10389 node, return the size in bits for the type if it is a constant, or else
10390 return the alignment for the type if the type's size is not constant, or
10391 else return BITS_PER_WORD if the type actually turns out to be an
10392 ERROR_MARK node. */
10394 static inline unsigned HOST_WIDE_INT
10395 simple_type_size_in_bits (const_tree type)
10397 if (TREE_CODE (type) == ERROR_MARK)
10398 return BITS_PER_WORD;
10399 else if (TYPE_SIZE (type) == NULL_TREE)
10400 return 0;
10401 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
10402 return tree_to_uhwi (TYPE_SIZE (type));
10403 else
10404 return TYPE_ALIGN (type);
10407 /* Similarly, but return an offset_int instead of UHWI. */
10409 static inline offset_int
10410 offset_int_type_size_in_bits (const_tree type)
10412 if (TREE_CODE (type) == ERROR_MARK)
10413 return BITS_PER_WORD;
10414 else if (TYPE_SIZE (type) == NULL_TREE)
10415 return 0;
10416 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
10417 return wi::to_offset (TYPE_SIZE (type));
10418 else
10419 return TYPE_ALIGN (type);
10422 /* Given a pointer to a tree node for a subrange type, return a pointer
10423 to a DIE that describes the given type. */
10425 static dw_die_ref
10426 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
10428 dw_die_ref subrange_die;
10429 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
10431 if (context_die == NULL)
10432 context_die = comp_unit_die ();
10434 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
10436 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
10438 /* The size of the subrange type and its base type do not match,
10439 so we need to generate a size attribute for the subrange type. */
10440 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
10443 if (low)
10444 add_bound_info (subrange_die, DW_AT_lower_bound, low);
10445 if (high)
10446 add_bound_info (subrange_die, DW_AT_upper_bound, high);
10448 return subrange_die;
10451 /* Returns the (const and/or volatile) cv_qualifiers associated with
10452 the decl node. This will normally be augmented with the
10453 cv_qualifiers of the underlying type in add_type_attribute. */
10455 static int
10456 decl_quals (const_tree decl)
10458 return ((TREE_READONLY (decl)
10459 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
10460 | (TREE_THIS_VOLATILE (decl)
10461 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
10464 /* Determine the TYPE whose qualifiers match the largest strict subset
10465 of the given TYPE_QUALS, and return its qualifiers. Ignore all
10466 qualifiers outside QUAL_MASK. */
10468 static int
10469 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
10471 tree t;
10472 int best_rank = 0, best_qual = 0, max_rank;
10474 type_quals &= qual_mask;
10475 max_rank = popcount_hwi (type_quals) - 1;
10477 for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
10478 t = TYPE_NEXT_VARIANT (t))
10480 int q = TYPE_QUALS (t) & qual_mask;
10482 if ((q & type_quals) == q && q != type_quals
10483 && check_base_type (t, type))
10485 int rank = popcount_hwi (q);
10487 if (rank > best_rank)
10489 best_rank = rank;
10490 best_qual = q;
10495 return best_qual;
10498 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
10499 entry that chains various modifiers in front of the given type. */
10501 static dw_die_ref
10502 modified_type_die (tree type, int cv_quals, dw_die_ref context_die)
10504 enum tree_code code = TREE_CODE (type);
10505 dw_die_ref mod_type_die;
10506 dw_die_ref sub_die = NULL;
10507 tree item_type = NULL;
10508 tree qualified_type;
10509 tree name, low, high;
10510 dw_die_ref mod_scope;
10511 /* Only these cv-qualifiers are currently handled. */
10512 const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
10513 | TYPE_QUAL_RESTRICT);
10515 if (code == ERROR_MARK)
10516 return NULL;
10518 cv_quals &= cv_qual_mask;
10520 /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
10521 tag modifier (and not an attribute) old consumers won't be able
10522 to handle it. */
10523 if (dwarf_version < 3)
10524 cv_quals &= ~TYPE_QUAL_RESTRICT;
10526 /* See if we already have the appropriately qualified variant of
10527 this type. */
10528 qualified_type = get_qualified_type (type, cv_quals);
10530 if (qualified_type == sizetype
10531 && TYPE_NAME (qualified_type)
10532 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
10534 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
10536 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
10537 && TYPE_PRECISION (t)
10538 == TYPE_PRECISION (qualified_type)
10539 && TYPE_UNSIGNED (t)
10540 == TYPE_UNSIGNED (qualified_type));
10541 qualified_type = t;
10544 /* If we do, then we can just use its DIE, if it exists. */
10545 if (qualified_type)
10547 mod_type_die = lookup_type_die (qualified_type);
10548 if (mod_type_die)
10549 return mod_type_die;
10552 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
10554 /* Handle C typedef types. */
10555 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
10556 && !DECL_ARTIFICIAL (name))
10558 tree dtype = TREE_TYPE (name);
10560 if (qualified_type == dtype)
10562 /* For a named type, use the typedef. */
10563 gen_type_die (qualified_type, context_die);
10564 return lookup_type_die (qualified_type);
10566 else
10568 int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
10569 dquals &= cv_qual_mask;
10570 if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
10571 || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
10572 /* cv-unqualified version of named type. Just use
10573 the unnamed type to which it refers. */
10574 return modified_type_die (DECL_ORIGINAL_TYPE (name),
10575 cv_quals, context_die);
10576 /* Else cv-qualified version of named type; fall through. */
10580 mod_scope = scope_die_for (type, context_die);
10582 if (cv_quals)
10584 struct qual_info { int q; enum dwarf_tag t; };
10585 static const struct qual_info qual_info[] =
10587 { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
10588 { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
10589 { TYPE_QUAL_CONST, DW_TAG_const_type },
10591 int sub_quals;
10592 unsigned i;
10594 /* Determine a lesser qualified type that most closely matches
10595 this one. Then generate DW_TAG_* entries for the remaining
10596 qualifiers. */
10597 sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
10598 cv_qual_mask);
10599 mod_type_die = modified_type_die (type, sub_quals, context_die);
10601 for (i = 0; i < sizeof (qual_info) / sizeof (qual_info[0]); i++)
10602 if (qual_info[i].q & cv_quals & ~sub_quals)
10604 dw_die_ref d = new_die (qual_info[i].t, mod_scope, type);
10605 if (mod_type_die)
10606 add_AT_die_ref (d, DW_AT_type, mod_type_die);
10607 mod_type_die = d;
10610 else if (code == POINTER_TYPE)
10612 mod_type_die = new_die (DW_TAG_pointer_type, mod_scope, type);
10613 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10614 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10615 item_type = TREE_TYPE (type);
10616 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10617 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10618 TYPE_ADDR_SPACE (item_type));
10620 else if (code == REFERENCE_TYPE)
10622 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
10623 mod_type_die = new_die (DW_TAG_rvalue_reference_type, mod_scope,
10624 type);
10625 else
10626 mod_type_die = new_die (DW_TAG_reference_type, mod_scope, type);
10627 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10628 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10629 item_type = TREE_TYPE (type);
10630 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10631 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10632 TYPE_ADDR_SPACE (item_type));
10634 else if (code == INTEGER_TYPE
10635 && TREE_TYPE (type) != NULL_TREE
10636 && subrange_type_for_debug_p (type, &low, &high))
10638 mod_type_die = subrange_type_die (type, low, high, context_die);
10639 item_type = TREE_TYPE (type);
10641 else if (is_base_type (type))
10642 mod_type_die = base_type_die (type);
10643 else
10645 gen_type_die (type, context_die);
10647 /* We have to get the type_main_variant here (and pass that to the
10648 `lookup_type_die' routine) because the ..._TYPE node we have
10649 might simply be a *copy* of some original type node (where the
10650 copy was created to help us keep track of typedef names) and
10651 that copy might have a different TYPE_UID from the original
10652 ..._TYPE node. */
10653 if (TREE_CODE (type) != VECTOR_TYPE)
10654 return lookup_type_die (type_main_variant (type));
10655 else
10656 /* Vectors have the debugging information in the type,
10657 not the main variant. */
10658 return lookup_type_die (type);
10661 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
10662 don't output a DW_TAG_typedef, since there isn't one in the
10663 user's program; just attach a DW_AT_name to the type.
10664 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
10665 if the base type already has the same name. */
10666 if (name
10667 && ((TREE_CODE (name) != TYPE_DECL
10668 && (qualified_type == TYPE_MAIN_VARIANT (type)
10669 || (cv_quals == TYPE_UNQUALIFIED)))
10670 || (TREE_CODE (name) == TYPE_DECL
10671 && TREE_TYPE (name) == qualified_type
10672 && DECL_NAME (name))))
10674 if (TREE_CODE (name) == TYPE_DECL)
10675 /* Could just call add_name_and_src_coords_attributes here,
10676 but since this is a builtin type it doesn't have any
10677 useful source coordinates anyway. */
10678 name = DECL_NAME (name);
10679 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
10681 /* This probably indicates a bug. */
10682 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
10684 name = TYPE_IDENTIFIER (type);
10685 add_name_attribute (mod_type_die,
10686 name ? IDENTIFIER_POINTER (name) : "__unknown__");
10689 if (qualified_type)
10690 equate_type_number_to_die (qualified_type, mod_type_die);
10692 if (item_type)
10693 /* We must do this after the equate_type_number_to_die call, in case
10694 this is a recursive type. This ensures that the modified_type_die
10695 recursion will terminate even if the type is recursive. Recursive
10696 types are possible in Ada. */
10697 sub_die = modified_type_die (item_type,
10698 TYPE_QUALS_NO_ADDR_SPACE (item_type),
10699 context_die);
10701 if (sub_die != NULL)
10702 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
10704 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
10705 if (TYPE_ARTIFICIAL (type))
10706 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
10708 return mod_type_die;
10711 /* Generate DIEs for the generic parameters of T.
10712 T must be either a generic type or a generic function.
10713 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
10715 static void
10716 gen_generic_params_dies (tree t)
10718 tree parms, args;
10719 int parms_num, i;
10720 dw_die_ref die = NULL;
10721 int non_default;
10723 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
10724 return;
10726 if (TYPE_P (t))
10727 die = lookup_type_die (t);
10728 else if (DECL_P (t))
10729 die = lookup_decl_die (t);
10731 gcc_assert (die);
10733 parms = lang_hooks.get_innermost_generic_parms (t);
10734 if (!parms)
10735 /* T has no generic parameter. It means T is neither a generic type
10736 or function. End of story. */
10737 return;
10739 parms_num = TREE_VEC_LENGTH (parms);
10740 args = lang_hooks.get_innermost_generic_args (t);
10741 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
10742 non_default = int_cst_value (TREE_CHAIN (args));
10743 else
10744 non_default = TREE_VEC_LENGTH (args);
10745 for (i = 0; i < parms_num; i++)
10747 tree parm, arg, arg_pack_elems;
10748 dw_die_ref parm_die;
10750 parm = TREE_VEC_ELT (parms, i);
10751 arg = TREE_VEC_ELT (args, i);
10752 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
10753 gcc_assert (parm && TREE_VALUE (parm) && arg);
10755 if (parm && TREE_VALUE (parm) && arg)
10757 /* If PARM represents a template parameter pack,
10758 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
10759 by DW_TAG_template_*_parameter DIEs for the argument
10760 pack elements of ARG. Note that ARG would then be
10761 an argument pack. */
10762 if (arg_pack_elems)
10763 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
10764 arg_pack_elems,
10765 die);
10766 else
10767 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
10768 true /* emit name */, die);
10769 if (i >= non_default)
10770 add_AT_flag (parm_die, DW_AT_default_value, 1);
10775 /* Create and return a DIE for PARM which should be
10776 the representation of a generic type parameter.
10777 For instance, in the C++ front end, PARM would be a template parameter.
10778 ARG is the argument to PARM.
10779 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
10780 name of the PARM.
10781 PARENT_DIE is the parent DIE which the new created DIE should be added to,
10782 as a child node. */
10784 static dw_die_ref
10785 generic_parameter_die (tree parm, tree arg,
10786 bool emit_name_p,
10787 dw_die_ref parent_die)
10789 dw_die_ref tmpl_die = NULL;
10790 const char *name = NULL;
10792 if (!parm || !DECL_NAME (parm) || !arg)
10793 return NULL;
10795 /* We support non-type generic parameters and arguments,
10796 type generic parameters and arguments, as well as
10797 generic generic parameters (a.k.a. template template parameters in C++)
10798 and arguments. */
10799 if (TREE_CODE (parm) == PARM_DECL)
10800 /* PARM is a nontype generic parameter */
10801 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
10802 else if (TREE_CODE (parm) == TYPE_DECL)
10803 /* PARM is a type generic parameter. */
10804 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
10805 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10806 /* PARM is a generic generic parameter.
10807 Its DIE is a GNU extension. It shall have a
10808 DW_AT_name attribute to represent the name of the template template
10809 parameter, and a DW_AT_GNU_template_name attribute to represent the
10810 name of the template template argument. */
10811 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
10812 parent_die, parm);
10813 else
10814 gcc_unreachable ();
10816 if (tmpl_die)
10818 tree tmpl_type;
10820 /* If PARM is a generic parameter pack, it means we are
10821 emitting debug info for a template argument pack element.
10822 In other terms, ARG is a template argument pack element.
10823 In that case, we don't emit any DW_AT_name attribute for
10824 the die. */
10825 if (emit_name_p)
10827 name = IDENTIFIER_POINTER (DECL_NAME (parm));
10828 gcc_assert (name);
10829 add_AT_string (tmpl_die, DW_AT_name, name);
10832 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10834 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
10835 TMPL_DIE should have a child DW_AT_type attribute that is set
10836 to the type of the argument to PARM, which is ARG.
10837 If PARM is a type generic parameter, TMPL_DIE should have a
10838 child DW_AT_type that is set to ARG. */
10839 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
10840 add_type_attribute (tmpl_die, tmpl_type,
10841 (TREE_THIS_VOLATILE (tmpl_type)
10842 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
10843 parent_die);
10845 else
10847 /* So TMPL_DIE is a DIE representing a
10848 a generic generic template parameter, a.k.a template template
10849 parameter in C++ and arg is a template. */
10851 /* The DW_AT_GNU_template_name attribute of the DIE must be set
10852 to the name of the argument. */
10853 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
10854 if (name)
10855 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
10858 if (TREE_CODE (parm) == PARM_DECL)
10859 /* So PARM is a non-type generic parameter.
10860 DWARF3 5.6.8 says we must set a DW_AT_const_value child
10861 attribute of TMPL_DIE which value represents the value
10862 of ARG.
10863 We must be careful here:
10864 The value of ARG might reference some function decls.
10865 We might currently be emitting debug info for a generic
10866 type and types are emitted before function decls, we don't
10867 know if the function decls referenced by ARG will actually be
10868 emitted after cgraph computations.
10869 So must defer the generation of the DW_AT_const_value to
10870 after cgraph is ready. */
10871 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
10874 return tmpl_die;
10877 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
10878 PARM_PACK must be a template parameter pack. The returned DIE
10879 will be child DIE of PARENT_DIE. */
10881 static dw_die_ref
10882 template_parameter_pack_die (tree parm_pack,
10883 tree parm_pack_args,
10884 dw_die_ref parent_die)
10886 dw_die_ref die;
10887 int j;
10889 gcc_assert (parent_die && parm_pack);
10891 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
10892 add_name_and_src_coords_attributes (die, parm_pack);
10893 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
10894 generic_parameter_die (parm_pack,
10895 TREE_VEC_ELT (parm_pack_args, j),
10896 false /* Don't emit DW_AT_name */,
10897 die);
10898 return die;
10901 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
10902 an enumerated type. */
10904 static inline int
10905 type_is_enum (const_tree type)
10907 return TREE_CODE (type) == ENUMERAL_TYPE;
10910 /* Return the DBX register number described by a given RTL node. */
10912 static unsigned int
10913 dbx_reg_number (const_rtx rtl)
10915 unsigned regno = REGNO (rtl);
10917 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
10919 #ifdef LEAF_REG_REMAP
10920 if (crtl->uses_only_leaf_regs)
10922 int leaf_reg = LEAF_REG_REMAP (regno);
10923 if (leaf_reg != -1)
10924 regno = (unsigned) leaf_reg;
10926 #endif
10928 regno = DBX_REGISTER_NUMBER (regno);
10929 gcc_assert (regno != INVALID_REGNUM);
10930 return regno;
10933 /* Optionally add a DW_OP_piece term to a location description expression.
10934 DW_OP_piece is only added if the location description expression already
10935 doesn't end with DW_OP_piece. */
10937 static void
10938 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
10940 dw_loc_descr_ref loc;
10942 if (*list_head != NULL)
10944 /* Find the end of the chain. */
10945 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
10948 if (loc->dw_loc_opc != DW_OP_piece)
10949 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
10953 /* Return a location descriptor that designates a machine register or
10954 zero if there is none. */
10956 static dw_loc_descr_ref
10957 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
10959 rtx regs;
10961 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
10962 return 0;
10964 /* We only use "frame base" when we're sure we're talking about the
10965 post-prologue local stack frame. We do this by *not* running
10966 register elimination until this point, and recognizing the special
10967 argument pointer and soft frame pointer rtx's.
10968 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
10969 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
10970 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
10972 dw_loc_descr_ref result = NULL;
10974 if (dwarf_version >= 4 || !dwarf_strict)
10976 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
10977 initialized);
10978 if (result)
10979 add_loc_descr (&result,
10980 new_loc_descr (DW_OP_stack_value, 0, 0));
10982 return result;
10985 regs = targetm.dwarf_register_span (rtl);
10987 if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
10988 return multiple_reg_loc_descriptor (rtl, regs, initialized);
10989 else
10991 unsigned int dbx_regnum = dbx_reg_number (rtl);
10992 if (dbx_regnum == IGNORED_DWARF_REGNUM)
10993 return 0;
10994 return one_reg_loc_descriptor (dbx_regnum, initialized);
10998 /* Return a location descriptor that designates a machine register for
10999 a given hard register number. */
11001 static dw_loc_descr_ref
11002 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
11004 dw_loc_descr_ref reg_loc_descr;
11006 if (regno <= 31)
11007 reg_loc_descr
11008 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
11009 else
11010 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
11012 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11013 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11015 return reg_loc_descr;
11018 /* Given an RTL of a register, return a location descriptor that
11019 designates a value that spans more than one register. */
11021 static dw_loc_descr_ref
11022 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
11023 enum var_init_status initialized)
11025 int size, i;
11026 dw_loc_descr_ref loc_result = NULL;
11028 /* Simple, contiguous registers. */
11029 if (regs == NULL_RTX)
11031 unsigned reg = REGNO (rtl);
11032 int nregs;
11034 #ifdef LEAF_REG_REMAP
11035 if (crtl->uses_only_leaf_regs)
11037 int leaf_reg = LEAF_REG_REMAP (reg);
11038 if (leaf_reg != -1)
11039 reg = (unsigned) leaf_reg;
11041 #endif
11043 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
11044 nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
11046 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
11048 loc_result = NULL;
11049 while (nregs--)
11051 dw_loc_descr_ref t;
11053 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
11054 VAR_INIT_STATUS_INITIALIZED);
11055 add_loc_descr (&loc_result, t);
11056 add_loc_descr_op_piece (&loc_result, size);
11057 ++reg;
11059 return loc_result;
11062 /* Now onto stupid register sets in non contiguous locations. */
11064 gcc_assert (GET_CODE (regs) == PARALLEL);
11066 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
11067 loc_result = NULL;
11069 for (i = 0; i < XVECLEN (regs, 0); ++i)
11071 dw_loc_descr_ref t;
11073 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
11074 VAR_INIT_STATUS_INITIALIZED);
11075 add_loc_descr (&loc_result, t);
11076 add_loc_descr_op_piece (&loc_result, size);
11079 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
11080 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11081 return loc_result;
11084 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
11086 /* Return a location descriptor that designates a constant i,
11087 as a compound operation from constant (i >> shift), constant shift
11088 and DW_OP_shl. */
11090 static dw_loc_descr_ref
11091 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11093 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
11094 add_loc_descr (&ret, int_loc_descriptor (shift));
11095 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11096 return ret;
11099 /* Return a location descriptor that designates a constant. */
11101 static dw_loc_descr_ref
11102 int_loc_descriptor (HOST_WIDE_INT i)
11104 enum dwarf_location_atom op;
11106 /* Pick the smallest representation of a constant, rather than just
11107 defaulting to the LEB encoding. */
11108 if (i >= 0)
11110 int clz = clz_hwi (i);
11111 int ctz = ctz_hwi (i);
11112 if (i <= 31)
11113 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
11114 else if (i <= 0xff)
11115 op = DW_OP_const1u;
11116 else if (i <= 0xffff)
11117 op = DW_OP_const2u;
11118 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11119 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11120 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
11121 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
11122 while DW_OP_const4u is 5 bytes. */
11123 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
11124 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11125 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11126 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
11127 while DW_OP_const4u is 5 bytes. */
11128 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11129 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11130 op = DW_OP_const4u;
11131 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11132 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11133 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
11134 while DW_OP_constu of constant >= 0x100000000 takes at least
11135 6 bytes. */
11136 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11137 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11138 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
11139 >= HOST_BITS_PER_WIDE_INT)
11140 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
11141 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
11142 while DW_OP_constu takes in this case at least 6 bytes. */
11143 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
11144 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11145 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11146 && size_of_uleb128 (i) > 6)
11147 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
11148 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
11149 else
11150 op = DW_OP_constu;
11152 else
11154 if (i >= -0x80)
11155 op = DW_OP_const1s;
11156 else if (i >= -0x8000)
11157 op = DW_OP_const2s;
11158 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11160 if (size_of_int_loc_descriptor (i) < 5)
11162 dw_loc_descr_ref ret = int_loc_descriptor (-i);
11163 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11164 return ret;
11166 op = DW_OP_const4s;
11168 else
11170 if (size_of_int_loc_descriptor (i)
11171 < (unsigned long) 1 + size_of_sleb128 (i))
11173 dw_loc_descr_ref ret = int_loc_descriptor (-i);
11174 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11175 return ret;
11177 op = DW_OP_consts;
11181 return new_loc_descr (op, i, 0);
11184 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
11185 without actually allocating it. */
11187 static unsigned long
11188 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11190 return size_of_int_loc_descriptor (i >> shift)
11191 + size_of_int_loc_descriptor (shift)
11192 + 1;
11195 /* Return size_of_locs (int_loc_descriptor (i)) without
11196 actually allocating it. */
11198 static unsigned long
11199 size_of_int_loc_descriptor (HOST_WIDE_INT i)
11201 unsigned long s;
11203 if (i >= 0)
11205 int clz, ctz;
11206 if (i <= 31)
11207 return 1;
11208 else if (i <= 0xff)
11209 return 2;
11210 else if (i <= 0xffff)
11211 return 3;
11212 clz = clz_hwi (i);
11213 ctz = ctz_hwi (i);
11214 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11215 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11216 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11217 - clz - 5);
11218 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11219 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11220 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11221 - clz - 8);
11222 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11223 return 5;
11224 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
11225 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11226 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11227 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11228 - clz - 8);
11229 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11230 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
11231 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11232 - clz - 16);
11233 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11234 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11235 && s > 6)
11236 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11237 - clz - 32);
11238 else
11239 return 1 + s;
11241 else
11243 if (i >= -0x80)
11244 return 2;
11245 else if (i >= -0x8000)
11246 return 3;
11247 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11249 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11251 s = size_of_int_loc_descriptor (-i) + 1;
11252 if (s < 5)
11253 return s;
11255 return 5;
11257 else
11259 unsigned long r = 1 + size_of_sleb128 (i);
11260 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11262 s = size_of_int_loc_descriptor (-i) + 1;
11263 if (s < r)
11264 return s;
11266 return r;
11271 /* Return loc description representing "address" of integer value.
11272 This can appear only as toplevel expression. */
11274 static dw_loc_descr_ref
11275 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
11277 int litsize;
11278 dw_loc_descr_ref loc_result = NULL;
11280 if (!(dwarf_version >= 4 || !dwarf_strict))
11281 return NULL;
11283 litsize = size_of_int_loc_descriptor (i);
11284 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
11285 is more compact. For DW_OP_stack_value we need:
11286 litsize + 1 (DW_OP_stack_value)
11287 and for DW_OP_implicit_value:
11288 1 (DW_OP_implicit_value) + 1 (length) + size. */
11289 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
11291 loc_result = int_loc_descriptor (i);
11292 add_loc_descr (&loc_result,
11293 new_loc_descr (DW_OP_stack_value, 0, 0));
11294 return loc_result;
11297 loc_result = new_loc_descr (DW_OP_implicit_value,
11298 size, 0);
11299 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
11300 loc_result->dw_loc_oprnd2.v.val_int = i;
11301 return loc_result;
11304 /* Return a location descriptor that designates a base+offset location. */
11306 static dw_loc_descr_ref
11307 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
11308 enum var_init_status initialized)
11310 unsigned int regno;
11311 dw_loc_descr_ref result;
11312 dw_fde_ref fde = cfun->fde;
11314 /* We only use "frame base" when we're sure we're talking about the
11315 post-prologue local stack frame. We do this by *not* running
11316 register elimination until this point, and recognizing the special
11317 argument pointer and soft frame pointer rtx's. */
11318 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
11320 rtx elim = (ira_use_lra_p
11321 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
11322 : eliminate_regs (reg, VOIDmode, NULL_RTX));
11324 if (elim != reg)
11326 if (GET_CODE (elim) == PLUS)
11328 offset += INTVAL (XEXP (elim, 1));
11329 elim = XEXP (elim, 0);
11331 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
11332 && (elim == hard_frame_pointer_rtx
11333 || elim == stack_pointer_rtx))
11334 || elim == (frame_pointer_needed
11335 ? hard_frame_pointer_rtx
11336 : stack_pointer_rtx));
11338 /* If drap register is used to align stack, use frame
11339 pointer + offset to access stack variables. If stack
11340 is aligned without drap, use stack pointer + offset to
11341 access stack variables. */
11342 if (crtl->stack_realign_tried
11343 && reg == frame_pointer_rtx)
11345 int base_reg
11346 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
11347 ? HARD_FRAME_POINTER_REGNUM
11348 : REGNO (elim));
11349 return new_reg_loc_descr (base_reg, offset);
11352 gcc_assert (frame_pointer_fb_offset_valid);
11353 offset += frame_pointer_fb_offset;
11354 return new_loc_descr (DW_OP_fbreg, offset, 0);
11358 regno = REGNO (reg);
11359 #ifdef LEAF_REG_REMAP
11360 if (crtl->uses_only_leaf_regs)
11362 int leaf_reg = LEAF_REG_REMAP (regno);
11363 if (leaf_reg != -1)
11364 regno = (unsigned) leaf_reg;
11366 #endif
11367 regno = DWARF_FRAME_REGNUM (regno);
11369 if (!optimize && fde
11370 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
11372 /* Use cfa+offset to represent the location of arguments passed
11373 on the stack when drap is used to align stack.
11374 Only do this when not optimizing, for optimized code var-tracking
11375 is supposed to track where the arguments live and the register
11376 used as vdrap or drap in some spot might be used for something
11377 else in other part of the routine. */
11378 return new_loc_descr (DW_OP_fbreg, offset, 0);
11381 if (regno <= 31)
11382 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
11383 offset, 0);
11384 else
11385 result = new_loc_descr (DW_OP_bregx, regno, offset);
11387 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11388 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11390 return result;
11393 /* Return true if this RTL expression describes a base+offset calculation. */
11395 static inline int
11396 is_based_loc (const_rtx rtl)
11398 return (GET_CODE (rtl) == PLUS
11399 && ((REG_P (XEXP (rtl, 0))
11400 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
11401 && CONST_INT_P (XEXP (rtl, 1)))));
11404 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
11405 failed. */
11407 static dw_loc_descr_ref
11408 tls_mem_loc_descriptor (rtx mem)
11410 tree base;
11411 dw_loc_descr_ref loc_result;
11413 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
11414 return NULL;
11416 base = get_base_address (MEM_EXPR (mem));
11417 if (base == NULL
11418 || TREE_CODE (base) != VAR_DECL
11419 || !DECL_THREAD_LOCAL_P (base))
11420 return NULL;
11422 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
11423 if (loc_result == NULL)
11424 return NULL;
11426 if (MEM_OFFSET (mem))
11427 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
11429 return loc_result;
11432 /* Output debug info about reason why we failed to expand expression as dwarf
11433 expression. */
11435 static void
11436 expansion_failed (tree expr, rtx rtl, char const *reason)
11438 if (dump_file && (dump_flags & TDF_DETAILS))
11440 fprintf (dump_file, "Failed to expand as dwarf: ");
11441 if (expr)
11442 print_generic_expr (dump_file, expr, dump_flags);
11443 if (rtl)
11445 fprintf (dump_file, "\n");
11446 print_rtl (dump_file, rtl);
11448 fprintf (dump_file, "\nReason: %s\n", reason);
11452 /* Helper function for const_ok_for_output. */
11454 static bool
11455 const_ok_for_output_1 (rtx rtl)
11457 if (GET_CODE (rtl) == UNSPEC)
11459 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
11460 we can't express it in the debug info. */
11461 #ifdef ENABLE_CHECKING
11462 /* Don't complain about TLS UNSPECs, those are just too hard to
11463 delegitimize. Note this could be a non-decl SYMBOL_REF such as
11464 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
11465 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
11466 if (XVECLEN (rtl, 0) == 0
11467 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
11468 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE)
11469 inform (current_function_decl
11470 ? DECL_SOURCE_LOCATION (current_function_decl)
11471 : UNKNOWN_LOCATION,
11472 #if NUM_UNSPEC_VALUES > 0
11473 "non-delegitimized UNSPEC %s (%d) found in variable location",
11474 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
11475 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
11476 XINT (rtl, 1));
11477 #else
11478 "non-delegitimized UNSPEC %d found in variable location",
11479 XINT (rtl, 1));
11480 #endif
11481 #endif
11482 expansion_failed (NULL_TREE, rtl,
11483 "UNSPEC hasn't been delegitimized.\n");
11484 return false;
11487 if (targetm.const_not_ok_for_debug_p (rtl))
11489 expansion_failed (NULL_TREE, rtl,
11490 "Expression rejected for debug by the backend.\n");
11491 return false;
11494 /* FIXME: Refer to PR60655. It is possible for simplification
11495 of rtl expressions in var tracking to produce such expressions.
11496 We should really identify / validate expressions
11497 enclosed in CONST that can be handled by assemblers on various
11498 targets and only handle legitimate cases here. */
11499 if (GET_CODE (rtl) != SYMBOL_REF)
11501 if (GET_CODE (rtl) == NOT)
11502 return false;
11503 return true;
11506 if (CONSTANT_POOL_ADDRESS_P (rtl))
11508 bool marked;
11509 get_pool_constant_mark (rtl, &marked);
11510 /* If all references to this pool constant were optimized away,
11511 it was not output and thus we can't represent it. */
11512 if (!marked)
11514 expansion_failed (NULL_TREE, rtl,
11515 "Constant was removed from constant pool.\n");
11516 return false;
11520 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11521 return false;
11523 /* Avoid references to external symbols in debug info, on several targets
11524 the linker might even refuse to link when linking a shared library,
11525 and in many other cases the relocations for .debug_info/.debug_loc are
11526 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
11527 to be defined within the same shared library or executable are fine. */
11528 if (SYMBOL_REF_EXTERNAL_P (rtl))
11530 tree decl = SYMBOL_REF_DECL (rtl);
11532 if (decl == NULL || !targetm.binds_local_p (decl))
11534 expansion_failed (NULL_TREE, rtl,
11535 "Symbol not defined in current TU.\n");
11536 return false;
11540 return true;
11543 /* Return true if constant RTL can be emitted in DW_OP_addr or
11544 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
11545 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
11547 static bool
11548 const_ok_for_output (rtx rtl)
11550 if (GET_CODE (rtl) == SYMBOL_REF)
11551 return const_ok_for_output_1 (rtl);
11553 if (GET_CODE (rtl) == CONST)
11555 subrtx_var_iterator::array_type array;
11556 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
11557 if (!const_ok_for_output_1 (*iter))
11558 return false;
11559 return true;
11562 return true;
11565 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
11566 if possible, NULL otherwise. */
11568 static dw_die_ref
11569 base_type_for_mode (enum machine_mode mode, bool unsignedp)
11571 dw_die_ref type_die;
11572 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11574 if (type == NULL)
11575 return NULL;
11576 switch (TREE_CODE (type))
11578 case INTEGER_TYPE:
11579 case REAL_TYPE:
11580 break;
11581 default:
11582 return NULL;
11584 type_die = lookup_type_die (type);
11585 if (!type_die)
11586 type_die = modified_type_die (type, TYPE_UNQUALIFIED, comp_unit_die ());
11587 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
11588 return NULL;
11589 return type_die;
11592 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
11593 type matching MODE, or, if MODE is narrower than or as wide as
11594 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
11595 possible. */
11597 static dw_loc_descr_ref
11598 convert_descriptor_to_mode (enum machine_mode mode, dw_loc_descr_ref op)
11600 enum machine_mode outer_mode = mode;
11601 dw_die_ref type_die;
11602 dw_loc_descr_ref cvt;
11604 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11606 add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
11607 return op;
11609 type_die = base_type_for_mode (outer_mode, 1);
11610 if (type_die == NULL)
11611 return NULL;
11612 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11613 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11614 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11615 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11616 add_loc_descr (&op, cvt);
11617 return op;
11620 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
11622 static dw_loc_descr_ref
11623 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
11624 dw_loc_descr_ref op1)
11626 dw_loc_descr_ref ret = op0;
11627 add_loc_descr (&ret, op1);
11628 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11629 if (STORE_FLAG_VALUE != 1)
11631 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
11632 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
11634 return ret;
11637 /* Return location descriptor for signed comparison OP RTL. */
11639 static dw_loc_descr_ref
11640 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11641 enum machine_mode mem_mode)
11643 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11644 dw_loc_descr_ref op0, op1;
11645 int shift;
11647 if (op_mode == VOIDmode)
11648 op_mode = GET_MODE (XEXP (rtl, 1));
11649 if (op_mode == VOIDmode)
11650 return NULL;
11652 if (dwarf_strict
11653 && (GET_MODE_CLASS (op_mode) != MODE_INT
11654 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
11655 return NULL;
11657 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11658 VAR_INIT_STATUS_INITIALIZED);
11659 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11660 VAR_INIT_STATUS_INITIALIZED);
11662 if (op0 == NULL || op1 == NULL)
11663 return NULL;
11665 if (GET_MODE_CLASS (op_mode) != MODE_INT
11666 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11667 return compare_loc_descriptor (op, op0, op1);
11669 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11671 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
11672 dw_loc_descr_ref cvt;
11674 if (type_die == NULL)
11675 return NULL;
11676 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11677 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11678 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11679 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11680 add_loc_descr (&op0, cvt);
11681 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11682 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11683 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11684 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11685 add_loc_descr (&op1, cvt);
11686 return compare_loc_descriptor (op, op0, op1);
11689 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
11690 /* For eq/ne, if the operands are known to be zero-extended,
11691 there is no need to do the fancy shifting up. */
11692 if (op == DW_OP_eq || op == DW_OP_ne)
11694 dw_loc_descr_ref last0, last1;
11695 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11697 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11699 /* deref_size zero extends, and for constants we can check
11700 whether they are zero extended or not. */
11701 if (((last0->dw_loc_opc == DW_OP_deref_size
11702 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11703 || (CONST_INT_P (XEXP (rtl, 0))
11704 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
11705 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
11706 && ((last1->dw_loc_opc == DW_OP_deref_size
11707 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11708 || (CONST_INT_P (XEXP (rtl, 1))
11709 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
11710 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
11711 return compare_loc_descriptor (op, op0, op1);
11713 /* EQ/NE comparison against constant in narrower type than
11714 DWARF2_ADDR_SIZE can be performed either as
11715 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
11716 DW_OP_{eq,ne}
11718 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
11719 DW_OP_{eq,ne}. Pick whatever is shorter. */
11720 if (CONST_INT_P (XEXP (rtl, 1))
11721 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
11722 && (size_of_int_loc_descriptor (shift) + 1
11723 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
11724 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
11725 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11726 & GET_MODE_MASK (op_mode))))
11728 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
11729 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11730 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11731 & GET_MODE_MASK (op_mode));
11732 return compare_loc_descriptor (op, op0, op1);
11735 add_loc_descr (&op0, int_loc_descriptor (shift));
11736 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11737 if (CONST_INT_P (XEXP (rtl, 1)))
11738 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
11739 else
11741 add_loc_descr (&op1, int_loc_descriptor (shift));
11742 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11744 return compare_loc_descriptor (op, op0, op1);
11747 /* Return location descriptor for unsigned comparison OP RTL. */
11749 static dw_loc_descr_ref
11750 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11751 enum machine_mode mem_mode)
11753 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11754 dw_loc_descr_ref op0, op1;
11756 if (op_mode == VOIDmode)
11757 op_mode = GET_MODE (XEXP (rtl, 1));
11758 if (op_mode == VOIDmode)
11759 return NULL;
11760 if (GET_MODE_CLASS (op_mode) != MODE_INT)
11761 return NULL;
11763 if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11764 return NULL;
11766 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11767 VAR_INIT_STATUS_INITIALIZED);
11768 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11769 VAR_INIT_STATUS_INITIALIZED);
11771 if (op0 == NULL || op1 == NULL)
11772 return NULL;
11774 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
11776 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
11777 dw_loc_descr_ref last0, last1;
11778 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11780 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11782 if (CONST_INT_P (XEXP (rtl, 0)))
11783 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
11784 /* deref_size zero extends, so no need to mask it again. */
11785 else if (last0->dw_loc_opc != DW_OP_deref_size
11786 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11788 add_loc_descr (&op0, int_loc_descriptor (mask));
11789 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11791 if (CONST_INT_P (XEXP (rtl, 1)))
11792 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
11793 /* deref_size zero extends, so no need to mask it again. */
11794 else if (last1->dw_loc_opc != DW_OP_deref_size
11795 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11797 add_loc_descr (&op1, int_loc_descriptor (mask));
11798 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11801 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11803 HOST_WIDE_INT bias = 1;
11804 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11805 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11806 if (CONST_INT_P (XEXP (rtl, 1)))
11807 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
11808 + INTVAL (XEXP (rtl, 1)));
11809 else
11810 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
11811 bias, 0));
11813 return compare_loc_descriptor (op, op0, op1);
11816 /* Return location descriptor for {U,S}{MIN,MAX}. */
11818 static dw_loc_descr_ref
11819 minmax_loc_descriptor (rtx rtl, enum machine_mode mode,
11820 enum machine_mode mem_mode)
11822 enum dwarf_location_atom op;
11823 dw_loc_descr_ref op0, op1, ret;
11824 dw_loc_descr_ref bra_node, drop_node;
11826 if (dwarf_strict
11827 && (GET_MODE_CLASS (mode) != MODE_INT
11828 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
11829 return NULL;
11831 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11832 VAR_INIT_STATUS_INITIALIZED);
11833 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11834 VAR_INIT_STATUS_INITIALIZED);
11836 if (op0 == NULL || op1 == NULL)
11837 return NULL;
11839 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
11840 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
11841 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
11842 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
11844 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11846 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11847 add_loc_descr (&op0, int_loc_descriptor (mask));
11848 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11849 add_loc_descr (&op1, int_loc_descriptor (mask));
11850 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11852 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
11854 HOST_WIDE_INT bias = 1;
11855 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11856 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11857 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11860 else if (GET_MODE_CLASS (mode) == MODE_INT
11861 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11863 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
11864 add_loc_descr (&op0, int_loc_descriptor (shift));
11865 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11866 add_loc_descr (&op1, int_loc_descriptor (shift));
11867 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11869 else if (GET_MODE_CLASS (mode) == MODE_INT
11870 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11872 dw_die_ref type_die = base_type_for_mode (mode, 0);
11873 dw_loc_descr_ref cvt;
11874 if (type_die == NULL)
11875 return NULL;
11876 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11877 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11878 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11879 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11880 add_loc_descr (&op0, cvt);
11881 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11882 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11883 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11884 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11885 add_loc_descr (&op1, cvt);
11888 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
11889 op = DW_OP_lt;
11890 else
11891 op = DW_OP_gt;
11892 ret = op0;
11893 add_loc_descr (&ret, op1);
11894 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11895 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
11896 add_loc_descr (&ret, bra_node);
11897 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11898 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
11899 add_loc_descr (&ret, drop_node);
11900 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
11901 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
11902 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
11903 && GET_MODE_CLASS (mode) == MODE_INT
11904 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11905 ret = convert_descriptor_to_mode (mode, ret);
11906 return ret;
11909 /* Helper function for mem_loc_descriptor. Perform OP binary op,
11910 but after converting arguments to type_die, afterwards
11911 convert back to unsigned. */
11913 static dw_loc_descr_ref
11914 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
11915 enum machine_mode mode, enum machine_mode mem_mode)
11917 dw_loc_descr_ref cvt, op0, op1;
11919 if (type_die == NULL)
11920 return NULL;
11921 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11922 VAR_INIT_STATUS_INITIALIZED);
11923 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11924 VAR_INIT_STATUS_INITIALIZED);
11925 if (op0 == NULL || op1 == NULL)
11926 return NULL;
11927 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11928 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11929 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11930 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11931 add_loc_descr (&op0, cvt);
11932 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11933 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11934 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11935 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11936 add_loc_descr (&op1, cvt);
11937 add_loc_descr (&op0, op1);
11938 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
11939 return convert_descriptor_to_mode (mode, op0);
11942 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
11943 const0 is DW_OP_lit0 or corresponding typed constant,
11944 const1 is DW_OP_lit1 or corresponding typed constant
11945 and constMSB is constant with just the MSB bit set
11946 for the mode):
11947 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11948 L1: const0 DW_OP_swap
11949 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
11950 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11951 L3: DW_OP_drop
11952 L4: DW_OP_nop
11954 CTZ is similar:
11955 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11956 L1: const0 DW_OP_swap
11957 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11958 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11959 L3: DW_OP_drop
11960 L4: DW_OP_nop
11962 FFS is similar:
11963 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
11964 L1: const1 DW_OP_swap
11965 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11966 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11967 L3: DW_OP_drop
11968 L4: DW_OP_nop */
11970 static dw_loc_descr_ref
11971 clz_loc_descriptor (rtx rtl, enum machine_mode mode,
11972 enum machine_mode mem_mode)
11974 dw_loc_descr_ref op0, ret, tmp;
11975 HOST_WIDE_INT valv;
11976 dw_loc_descr_ref l1jump, l1label;
11977 dw_loc_descr_ref l2jump, l2label;
11978 dw_loc_descr_ref l3jump, l3label;
11979 dw_loc_descr_ref l4jump, l4label;
11980 rtx msb;
11982 if (GET_MODE_CLASS (mode) != MODE_INT
11983 || GET_MODE (XEXP (rtl, 0)) != mode)
11984 return NULL;
11986 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11987 VAR_INIT_STATUS_INITIALIZED);
11988 if (op0 == NULL)
11989 return NULL;
11990 ret = op0;
11991 if (GET_CODE (rtl) == CLZ)
11993 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11994 valv = GET_MODE_BITSIZE (mode);
11996 else if (GET_CODE (rtl) == FFS)
11997 valv = 0;
11998 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11999 valv = GET_MODE_BITSIZE (mode);
12000 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12001 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
12002 add_loc_descr (&ret, l1jump);
12003 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12004 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
12005 VAR_INIT_STATUS_INITIALIZED);
12006 if (tmp == NULL)
12007 return NULL;
12008 add_loc_descr (&ret, tmp);
12009 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
12010 add_loc_descr (&ret, l4jump);
12011 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
12012 ? const1_rtx : const0_rtx,
12013 mode, mem_mode,
12014 VAR_INIT_STATUS_INITIALIZED);
12015 if (l1label == NULL)
12016 return NULL;
12017 add_loc_descr (&ret, l1label);
12018 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12019 l2label = new_loc_descr (DW_OP_dup, 0, 0);
12020 add_loc_descr (&ret, l2label);
12021 if (GET_CODE (rtl) != CLZ)
12022 msb = const1_rtx;
12023 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
12024 msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
12025 << (GET_MODE_BITSIZE (mode) - 1));
12026 else
12027 msb = immed_wide_int_const
12028 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
12029 GET_MODE_PRECISION (mode)), mode);
12030 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
12031 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12032 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
12033 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
12034 else
12035 tmp = mem_loc_descriptor (msb, mode, mem_mode,
12036 VAR_INIT_STATUS_INITIALIZED);
12037 if (tmp == NULL)
12038 return NULL;
12039 add_loc_descr (&ret, tmp);
12040 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12041 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
12042 add_loc_descr (&ret, l3jump);
12043 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12044 VAR_INIT_STATUS_INITIALIZED);
12045 if (tmp == NULL)
12046 return NULL;
12047 add_loc_descr (&ret, tmp);
12048 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
12049 ? DW_OP_shl : DW_OP_shr, 0, 0));
12050 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12051 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
12052 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12053 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
12054 add_loc_descr (&ret, l2jump);
12055 l3label = new_loc_descr (DW_OP_drop, 0, 0);
12056 add_loc_descr (&ret, l3label);
12057 l4label = new_loc_descr (DW_OP_nop, 0, 0);
12058 add_loc_descr (&ret, l4label);
12059 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12060 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12061 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12062 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12063 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12064 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
12065 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12066 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
12067 return ret;
12070 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
12071 const1 is DW_OP_lit1 or corresponding typed constant):
12072 const0 DW_OP_swap
12073 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
12074 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
12075 L2: DW_OP_drop
12077 PARITY is similar:
12078 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
12079 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
12080 L2: DW_OP_drop */
12082 static dw_loc_descr_ref
12083 popcount_loc_descriptor (rtx rtl, enum machine_mode mode,
12084 enum machine_mode mem_mode)
12086 dw_loc_descr_ref op0, ret, tmp;
12087 dw_loc_descr_ref l1jump, l1label;
12088 dw_loc_descr_ref l2jump, l2label;
12090 if (GET_MODE_CLASS (mode) != MODE_INT
12091 || GET_MODE (XEXP (rtl, 0)) != mode)
12092 return NULL;
12094 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12095 VAR_INIT_STATUS_INITIALIZED);
12096 if (op0 == NULL)
12097 return NULL;
12098 ret = op0;
12099 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12100 VAR_INIT_STATUS_INITIALIZED);
12101 if (tmp == NULL)
12102 return NULL;
12103 add_loc_descr (&ret, tmp);
12104 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12105 l1label = new_loc_descr (DW_OP_dup, 0, 0);
12106 add_loc_descr (&ret, l1label);
12107 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12108 add_loc_descr (&ret, l2jump);
12109 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12110 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12111 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12112 VAR_INIT_STATUS_INITIALIZED);
12113 if (tmp == NULL)
12114 return NULL;
12115 add_loc_descr (&ret, tmp);
12116 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12117 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
12118 ? DW_OP_plus : DW_OP_xor, 0, 0));
12119 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12120 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12121 VAR_INIT_STATUS_INITIALIZED);
12122 add_loc_descr (&ret, tmp);
12123 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12124 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12125 add_loc_descr (&ret, l1jump);
12126 l2label = new_loc_descr (DW_OP_drop, 0, 0);
12127 add_loc_descr (&ret, l2label);
12128 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12129 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12130 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12131 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12132 return ret;
12135 /* BSWAP (constS is initial shift count, either 56 or 24):
12136 constS const0
12137 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
12138 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
12139 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
12140 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
12141 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
12143 static dw_loc_descr_ref
12144 bswap_loc_descriptor (rtx rtl, enum machine_mode mode,
12145 enum machine_mode mem_mode)
12147 dw_loc_descr_ref op0, ret, tmp;
12148 dw_loc_descr_ref l1jump, l1label;
12149 dw_loc_descr_ref l2jump, l2label;
12151 if (GET_MODE_CLASS (mode) != MODE_INT
12152 || BITS_PER_UNIT != 8
12153 || (GET_MODE_BITSIZE (mode) != 32
12154 && GET_MODE_BITSIZE (mode) != 64))
12155 return NULL;
12157 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12158 VAR_INIT_STATUS_INITIALIZED);
12159 if (op0 == NULL)
12160 return NULL;
12162 ret = op0;
12163 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12164 mode, mem_mode,
12165 VAR_INIT_STATUS_INITIALIZED);
12166 if (tmp == NULL)
12167 return NULL;
12168 add_loc_descr (&ret, tmp);
12169 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12170 VAR_INIT_STATUS_INITIALIZED);
12171 if (tmp == NULL)
12172 return NULL;
12173 add_loc_descr (&ret, tmp);
12174 l1label = new_loc_descr (DW_OP_pick, 2, 0);
12175 add_loc_descr (&ret, l1label);
12176 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12177 mode, mem_mode,
12178 VAR_INIT_STATUS_INITIALIZED);
12179 add_loc_descr (&ret, tmp);
12180 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
12181 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12182 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12183 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
12184 VAR_INIT_STATUS_INITIALIZED);
12185 if (tmp == NULL)
12186 return NULL;
12187 add_loc_descr (&ret, tmp);
12188 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12189 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
12190 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12191 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12192 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12193 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12194 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12195 VAR_INIT_STATUS_INITIALIZED);
12196 add_loc_descr (&ret, tmp);
12197 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
12198 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12199 add_loc_descr (&ret, l2jump);
12200 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
12201 VAR_INIT_STATUS_INITIALIZED);
12202 add_loc_descr (&ret, tmp);
12203 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12204 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12205 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12206 add_loc_descr (&ret, l1jump);
12207 l2label = new_loc_descr (DW_OP_drop, 0, 0);
12208 add_loc_descr (&ret, l2label);
12209 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12210 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12211 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12212 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12213 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12214 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12215 return ret;
12218 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
12219 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12220 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
12221 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
12223 ROTATERT is similar:
12224 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
12225 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12226 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
12228 static dw_loc_descr_ref
12229 rotate_loc_descriptor (rtx rtl, enum machine_mode mode,
12230 enum machine_mode mem_mode)
12232 rtx rtlop1 = XEXP (rtl, 1);
12233 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
12234 int i;
12236 if (GET_MODE_CLASS (mode) != MODE_INT)
12237 return NULL;
12239 if (GET_MODE (rtlop1) != VOIDmode
12240 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
12241 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12242 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12243 VAR_INIT_STATUS_INITIALIZED);
12244 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12245 VAR_INIT_STATUS_INITIALIZED);
12246 if (op0 == NULL || op1 == NULL)
12247 return NULL;
12248 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12249 for (i = 0; i < 2; i++)
12251 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
12252 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
12253 mode, mem_mode,
12254 VAR_INIT_STATUS_INITIALIZED);
12255 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12256 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12257 ? DW_OP_const4u
12258 : HOST_BITS_PER_WIDE_INT == 64
12259 ? DW_OP_const8u : DW_OP_constu,
12260 GET_MODE_MASK (mode), 0);
12261 else
12262 mask[i] = NULL;
12263 if (mask[i] == NULL)
12264 return NULL;
12265 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
12267 ret = op0;
12268 add_loc_descr (&ret, op1);
12269 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12270 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12271 if (GET_CODE (rtl) == ROTATERT)
12273 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12274 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12275 GET_MODE_BITSIZE (mode), 0));
12277 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12278 if (mask[0] != NULL)
12279 add_loc_descr (&ret, mask[0]);
12280 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12281 if (mask[1] != NULL)
12283 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12284 add_loc_descr (&ret, mask[1]);
12285 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12287 if (GET_CODE (rtl) == ROTATE)
12289 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12290 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12291 GET_MODE_BITSIZE (mode), 0));
12293 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12294 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12295 return ret;
12298 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
12299 for DEBUG_PARAMETER_REF RTL. */
12301 static dw_loc_descr_ref
12302 parameter_ref_descriptor (rtx rtl)
12304 dw_loc_descr_ref ret;
12305 dw_die_ref ref;
12307 if (dwarf_strict)
12308 return NULL;
12309 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
12310 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
12311 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
12312 if (ref)
12314 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12315 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
12316 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
12318 else
12320 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
12321 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
12323 return ret;
12326 /* The following routine converts the RTL for a variable or parameter
12327 (resident in memory) into an equivalent Dwarf representation of a
12328 mechanism for getting the address of that same variable onto the top of a
12329 hypothetical "address evaluation" stack.
12331 When creating memory location descriptors, we are effectively transforming
12332 the RTL for a memory-resident object into its Dwarf postfix expression
12333 equivalent. This routine recursively descends an RTL tree, turning
12334 it into Dwarf postfix code as it goes.
12336 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
12338 MEM_MODE is the mode of the memory reference, needed to handle some
12339 autoincrement addressing modes.
12341 Return 0 if we can't represent the location. */
12343 dw_loc_descr_ref
12344 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
12345 enum machine_mode mem_mode,
12346 enum var_init_status initialized)
12348 dw_loc_descr_ref mem_loc_result = NULL;
12349 enum dwarf_location_atom op;
12350 dw_loc_descr_ref op0, op1;
12351 rtx inner = NULL_RTX;
12353 if (mode == VOIDmode)
12354 mode = GET_MODE (rtl);
12356 /* Note that for a dynamically sized array, the location we will generate a
12357 description of here will be the lowest numbered location which is
12358 actually within the array. That's *not* necessarily the same as the
12359 zeroth element of the array. */
12361 rtl = targetm.delegitimize_address (rtl);
12363 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
12364 return NULL;
12366 switch (GET_CODE (rtl))
12368 case POST_INC:
12369 case POST_DEC:
12370 case POST_MODIFY:
12371 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
12373 case SUBREG:
12374 /* The case of a subreg may arise when we have a local (register)
12375 variable or a formal (register) parameter which doesn't quite fill
12376 up an entire register. For now, just assume that it is
12377 legitimate to make the Dwarf info refer to the whole register which
12378 contains the given subreg. */
12379 if (!subreg_lowpart_p (rtl))
12380 break;
12381 inner = SUBREG_REG (rtl);
12382 case TRUNCATE:
12383 if (inner == NULL_RTX)
12384 inner = XEXP (rtl, 0);
12385 if (GET_MODE_CLASS (mode) == MODE_INT
12386 && GET_MODE_CLASS (GET_MODE (inner)) == MODE_INT
12387 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12388 #ifdef POINTERS_EXTEND_UNSIGNED
12389 || (mode == Pmode && mem_mode != VOIDmode)
12390 #endif
12392 && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
12394 mem_loc_result = mem_loc_descriptor (inner,
12395 GET_MODE (inner),
12396 mem_mode, initialized);
12397 break;
12399 if (dwarf_strict)
12400 break;
12401 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
12402 break;
12403 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
12404 && (GET_MODE_CLASS (mode) != MODE_INT
12405 || GET_MODE_CLASS (GET_MODE (inner)) != MODE_INT))
12406 break;
12407 else
12409 dw_die_ref type_die;
12410 dw_loc_descr_ref cvt;
12412 mem_loc_result = mem_loc_descriptor (inner,
12413 GET_MODE (inner),
12414 mem_mode, initialized);
12415 if (mem_loc_result == NULL)
12416 break;
12417 type_die = base_type_for_mode (mode,
12418 GET_MODE_CLASS (mode) == MODE_INT);
12419 if (type_die == NULL)
12421 mem_loc_result = NULL;
12422 break;
12424 if (GET_MODE_SIZE (mode)
12425 != GET_MODE_SIZE (GET_MODE (inner)))
12426 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12427 else
12428 cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
12429 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12430 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12431 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12432 add_loc_descr (&mem_loc_result, cvt);
12434 break;
12436 case REG:
12437 if (GET_MODE_CLASS (mode) != MODE_INT
12438 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12439 && rtl != arg_pointer_rtx
12440 && rtl != frame_pointer_rtx
12441 #ifdef POINTERS_EXTEND_UNSIGNED
12442 && (mode != Pmode || mem_mode == VOIDmode)
12443 #endif
12446 dw_die_ref type_die;
12447 unsigned int dbx_regnum;
12449 if (dwarf_strict)
12450 break;
12451 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
12452 break;
12453 type_die = base_type_for_mode (mode,
12454 GET_MODE_CLASS (mode) == MODE_INT);
12455 if (type_die == NULL)
12456 break;
12458 dbx_regnum = dbx_reg_number (rtl);
12459 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12460 break;
12461 mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
12462 dbx_regnum, 0);
12463 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12464 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12465 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
12466 break;
12468 /* Whenever a register number forms a part of the description of the
12469 method for calculating the (dynamic) address of a memory resident
12470 object, DWARF rules require the register number be referred to as
12471 a "base register". This distinction is not based in any way upon
12472 what category of register the hardware believes the given register
12473 belongs to. This is strictly DWARF terminology we're dealing with
12474 here. Note that in cases where the location of a memory-resident
12475 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
12476 OP_CONST (0)) the actual DWARF location descriptor that we generate
12477 may just be OP_BASEREG (basereg). This may look deceptively like
12478 the object in question was allocated to a register (rather than in
12479 memory) so DWARF consumers need to be aware of the subtle
12480 distinction between OP_REG and OP_BASEREG. */
12481 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
12482 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
12483 else if (stack_realign_drap
12484 && crtl->drap_reg
12485 && crtl->args.internal_arg_pointer == rtl
12486 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
12488 /* If RTL is internal_arg_pointer, which has been optimized
12489 out, use DRAP instead. */
12490 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
12491 VAR_INIT_STATUS_INITIALIZED);
12493 break;
12495 case SIGN_EXTEND:
12496 case ZERO_EXTEND:
12497 if (GET_MODE_CLASS (mode) != MODE_INT)
12498 break;
12499 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12500 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12501 if (op0 == 0)
12502 break;
12503 else if (GET_CODE (rtl) == ZERO_EXTEND
12504 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12505 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12506 < HOST_BITS_PER_WIDE_INT
12507 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
12508 to expand zero extend as two shifts instead of
12509 masking. */
12510 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
12512 enum machine_mode imode = GET_MODE (XEXP (rtl, 0));
12513 mem_loc_result = op0;
12514 add_loc_descr (&mem_loc_result,
12515 int_loc_descriptor (GET_MODE_MASK (imode)));
12516 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
12518 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12520 int shift = DWARF2_ADDR_SIZE
12521 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
12522 shift *= BITS_PER_UNIT;
12523 if (GET_CODE (rtl) == SIGN_EXTEND)
12524 op = DW_OP_shra;
12525 else
12526 op = DW_OP_shr;
12527 mem_loc_result = op0;
12528 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12529 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12530 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12531 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12533 else if (!dwarf_strict)
12535 dw_die_ref type_die1, type_die2;
12536 dw_loc_descr_ref cvt;
12538 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12539 GET_CODE (rtl) == ZERO_EXTEND);
12540 if (type_die1 == NULL)
12541 break;
12542 type_die2 = base_type_for_mode (mode, 1);
12543 if (type_die2 == NULL)
12544 break;
12545 mem_loc_result = op0;
12546 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12547 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12548 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
12549 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12550 add_loc_descr (&mem_loc_result, cvt);
12551 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12552 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12553 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
12554 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12555 add_loc_descr (&mem_loc_result, cvt);
12557 break;
12559 case MEM:
12561 rtx new_rtl = avoid_constant_pool_reference (rtl);
12562 if (new_rtl != rtl)
12564 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
12565 initialized);
12566 if (mem_loc_result != NULL)
12567 return mem_loc_result;
12570 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
12571 get_address_mode (rtl), mode,
12572 VAR_INIT_STATUS_INITIALIZED);
12573 if (mem_loc_result == NULL)
12574 mem_loc_result = tls_mem_loc_descriptor (rtl);
12575 if (mem_loc_result != NULL)
12577 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12578 || GET_MODE_CLASS (mode) != MODE_INT)
12580 dw_die_ref type_die;
12581 dw_loc_descr_ref deref;
12583 if (dwarf_strict)
12584 return NULL;
12585 type_die
12586 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
12587 if (type_die == NULL)
12588 return NULL;
12589 deref = new_loc_descr (DW_OP_GNU_deref_type,
12590 GET_MODE_SIZE (mode), 0);
12591 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12592 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12593 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
12594 add_loc_descr (&mem_loc_result, deref);
12596 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12597 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
12598 else
12599 add_loc_descr (&mem_loc_result,
12600 new_loc_descr (DW_OP_deref_size,
12601 GET_MODE_SIZE (mode), 0));
12603 break;
12605 case LO_SUM:
12606 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
12608 case LABEL_REF:
12609 /* Some ports can transform a symbol ref into a label ref, because
12610 the symbol ref is too far away and has to be dumped into a constant
12611 pool. */
12612 case CONST:
12613 case SYMBOL_REF:
12614 if ((GET_MODE_CLASS (mode) != MODE_INT
12615 && GET_MODE_CLASS (mode) != MODE_PARTIAL_INT)
12616 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12617 #ifdef POINTERS_EXTEND_UNSIGNED
12618 && (mode != Pmode || mem_mode == VOIDmode)
12619 #endif
12621 break;
12622 if (GET_CODE (rtl) == SYMBOL_REF
12623 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12625 dw_loc_descr_ref temp;
12627 /* If this is not defined, we have no way to emit the data. */
12628 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
12629 break;
12631 temp = new_addr_loc_descr (rtl, dtprel_true);
12633 mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
12634 add_loc_descr (&mem_loc_result, temp);
12636 break;
12639 if (!const_ok_for_output (rtl))
12640 break;
12642 symref:
12643 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
12644 vec_safe_push (used_rtx_array, rtl);
12645 break;
12647 case CONCAT:
12648 case CONCATN:
12649 case VAR_LOCATION:
12650 case DEBUG_IMPLICIT_PTR:
12651 expansion_failed (NULL_TREE, rtl,
12652 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
12653 return 0;
12655 case ENTRY_VALUE:
12656 if (dwarf_strict)
12657 return NULL;
12658 if (REG_P (ENTRY_VALUE_EXP (rtl)))
12660 if (GET_MODE_CLASS (mode) != MODE_INT
12661 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12662 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12663 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12664 else
12666 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
12667 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12668 return NULL;
12669 op0 = one_reg_loc_descriptor (dbx_regnum,
12670 VAR_INIT_STATUS_INITIALIZED);
12673 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
12674 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
12676 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12677 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12678 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
12679 return NULL;
12681 else
12682 gcc_unreachable ();
12683 if (op0 == NULL)
12684 return NULL;
12685 mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
12686 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
12687 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
12688 break;
12690 case DEBUG_PARAMETER_REF:
12691 mem_loc_result = parameter_ref_descriptor (rtl);
12692 break;
12694 case PRE_MODIFY:
12695 /* Extract the PLUS expression nested inside and fall into
12696 PLUS code below. */
12697 rtl = XEXP (rtl, 1);
12698 goto plus;
12700 case PRE_INC:
12701 case PRE_DEC:
12702 /* Turn these into a PLUS expression and fall into the PLUS code
12703 below. */
12704 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
12705 gen_int_mode (GET_CODE (rtl) == PRE_INC
12706 ? GET_MODE_UNIT_SIZE (mem_mode)
12707 : -GET_MODE_UNIT_SIZE (mem_mode),
12708 mode));
12710 /* ... fall through ... */
12712 case PLUS:
12713 plus:
12714 if (is_based_loc (rtl)
12715 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12716 || XEXP (rtl, 0) == arg_pointer_rtx
12717 || XEXP (rtl, 0) == frame_pointer_rtx)
12718 && GET_MODE_CLASS (mode) == MODE_INT)
12719 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
12720 INTVAL (XEXP (rtl, 1)),
12721 VAR_INIT_STATUS_INITIALIZED);
12722 else
12724 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12725 VAR_INIT_STATUS_INITIALIZED);
12726 if (mem_loc_result == 0)
12727 break;
12729 if (CONST_INT_P (XEXP (rtl, 1))
12730 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12731 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
12732 else
12734 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12735 VAR_INIT_STATUS_INITIALIZED);
12736 if (op1 == 0)
12737 return NULL;
12738 add_loc_descr (&mem_loc_result, op1);
12739 add_loc_descr (&mem_loc_result,
12740 new_loc_descr (DW_OP_plus, 0, 0));
12743 break;
12745 /* If a pseudo-reg is optimized away, it is possible for it to
12746 be replaced with a MEM containing a multiply or shift. */
12747 case MINUS:
12748 op = DW_OP_minus;
12749 goto do_binop;
12751 case MULT:
12752 op = DW_OP_mul;
12753 goto do_binop;
12755 case DIV:
12756 if (!dwarf_strict
12757 && GET_MODE_CLASS (mode) == MODE_INT
12758 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12760 mem_loc_result = typed_binop (DW_OP_div, rtl,
12761 base_type_for_mode (mode, 0),
12762 mode, mem_mode);
12763 break;
12765 op = DW_OP_div;
12766 goto do_binop;
12768 case UMOD:
12769 op = DW_OP_mod;
12770 goto do_binop;
12772 case ASHIFT:
12773 op = DW_OP_shl;
12774 goto do_shift;
12776 case ASHIFTRT:
12777 op = DW_OP_shra;
12778 goto do_shift;
12780 case LSHIFTRT:
12781 op = DW_OP_shr;
12782 goto do_shift;
12784 do_shift:
12785 if (GET_MODE_CLASS (mode) != MODE_INT)
12786 break;
12787 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12788 VAR_INIT_STATUS_INITIALIZED);
12790 rtx rtlop1 = XEXP (rtl, 1);
12791 if (GET_MODE (rtlop1) != VOIDmode
12792 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
12793 < GET_MODE_BITSIZE (mode))
12794 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12795 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12796 VAR_INIT_STATUS_INITIALIZED);
12799 if (op0 == 0 || op1 == 0)
12800 break;
12802 mem_loc_result = op0;
12803 add_loc_descr (&mem_loc_result, op1);
12804 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12805 break;
12807 case AND:
12808 op = DW_OP_and;
12809 goto do_binop;
12811 case IOR:
12812 op = DW_OP_or;
12813 goto do_binop;
12815 case XOR:
12816 op = DW_OP_xor;
12817 goto do_binop;
12819 do_binop:
12820 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12821 VAR_INIT_STATUS_INITIALIZED);
12822 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12823 VAR_INIT_STATUS_INITIALIZED);
12825 if (op0 == 0 || op1 == 0)
12826 break;
12828 mem_loc_result = op0;
12829 add_loc_descr (&mem_loc_result, op1);
12830 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12831 break;
12833 case MOD:
12834 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
12836 mem_loc_result = typed_binop (DW_OP_mod, rtl,
12837 base_type_for_mode (mode, 0),
12838 mode, mem_mode);
12839 break;
12842 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12843 VAR_INIT_STATUS_INITIALIZED);
12844 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12845 VAR_INIT_STATUS_INITIALIZED);
12847 if (op0 == 0 || op1 == 0)
12848 break;
12850 mem_loc_result = op0;
12851 add_loc_descr (&mem_loc_result, op1);
12852 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12853 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12854 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
12855 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
12856 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
12857 break;
12859 case UDIV:
12860 if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
12862 if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
12864 op = DW_OP_div;
12865 goto do_binop;
12867 mem_loc_result = typed_binop (DW_OP_div, rtl,
12868 base_type_for_mode (mode, 1),
12869 mode, mem_mode);
12871 break;
12873 case NOT:
12874 op = DW_OP_not;
12875 goto do_unop;
12877 case ABS:
12878 op = DW_OP_abs;
12879 goto do_unop;
12881 case NEG:
12882 op = DW_OP_neg;
12883 goto do_unop;
12885 do_unop:
12886 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12887 VAR_INIT_STATUS_INITIALIZED);
12889 if (op0 == 0)
12890 break;
12892 mem_loc_result = op0;
12893 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12894 break;
12896 case CONST_INT:
12897 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12898 #ifdef POINTERS_EXTEND_UNSIGNED
12899 || (mode == Pmode
12900 && mem_mode != VOIDmode
12901 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
12902 #endif
12905 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12906 break;
12908 if (!dwarf_strict
12909 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
12910 || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
12912 dw_die_ref type_die = base_type_for_mode (mode, 1);
12913 enum machine_mode amode;
12914 if (type_die == NULL)
12915 return NULL;
12916 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
12917 MODE_INT, 0);
12918 if (INTVAL (rtl) >= 0
12919 && amode != BLKmode
12920 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
12921 /* const DW_OP_GNU_convert <XXX> vs.
12922 DW_OP_GNU_const_type <XXX, 1, const>. */
12923 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
12924 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
12926 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12927 op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12928 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12929 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12930 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
12931 add_loc_descr (&mem_loc_result, op0);
12932 return mem_loc_result;
12934 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
12935 INTVAL (rtl));
12936 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12937 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12938 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12939 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12940 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
12941 else
12943 mem_loc_result->dw_loc_oprnd2.val_class
12944 = dw_val_class_const_double;
12945 mem_loc_result->dw_loc_oprnd2.v.val_double
12946 = double_int::from_shwi (INTVAL (rtl));
12949 break;
12951 case CONST_DOUBLE:
12952 if (!dwarf_strict)
12954 dw_die_ref type_die;
12956 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
12957 CONST_DOUBLE rtx could represent either a large integer
12958 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
12959 the value is always a floating point constant.
12961 When it is an integer, a CONST_DOUBLE is used whenever
12962 the constant requires 2 HWIs to be adequately represented.
12963 We output CONST_DOUBLEs as blocks. */
12964 if (mode == VOIDmode
12965 || (GET_MODE (rtl) == VOIDmode
12966 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
12967 break;
12968 type_die = base_type_for_mode (mode,
12969 GET_MODE_CLASS (mode) == MODE_INT);
12970 if (type_die == NULL)
12971 return NULL;
12972 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
12973 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12974 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12975 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12976 #if TARGET_SUPPORTS_WIDE_INT == 0
12977 if (!SCALAR_FLOAT_MODE_P (mode))
12979 mem_loc_result->dw_loc_oprnd2.val_class
12980 = dw_val_class_const_double;
12981 mem_loc_result->dw_loc_oprnd2.v.val_double
12982 = rtx_to_double_int (rtl);
12984 else
12985 #endif
12987 unsigned int length = GET_MODE_SIZE (mode);
12988 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
12990 insert_float (rtl, array);
12991 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
12992 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
12993 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
12994 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
12997 break;
12999 case CONST_WIDE_INT:
13000 if (!dwarf_strict)
13002 dw_die_ref type_die;
13004 type_die = base_type_for_mode (mode,
13005 GET_MODE_CLASS (mode) == MODE_INT);
13006 if (type_die == NULL)
13007 return NULL;
13008 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
13009 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13010 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13011 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13012 mem_loc_result->dw_loc_oprnd2.val_class
13013 = dw_val_class_wide_int;
13014 mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_cleared_alloc<wide_int> ();
13015 *mem_loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
13017 break;
13019 case EQ:
13020 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
13021 break;
13023 case GE:
13024 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
13025 break;
13027 case GT:
13028 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
13029 break;
13031 case LE:
13032 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
13033 break;
13035 case LT:
13036 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
13037 break;
13039 case NE:
13040 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
13041 break;
13043 case GEU:
13044 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
13045 break;
13047 case GTU:
13048 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
13049 break;
13051 case LEU:
13052 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
13053 break;
13055 case LTU:
13056 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
13057 break;
13059 case UMIN:
13060 case UMAX:
13061 if (GET_MODE_CLASS (mode) != MODE_INT)
13062 break;
13063 /* FALLTHRU */
13064 case SMIN:
13065 case SMAX:
13066 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
13067 break;
13069 case ZERO_EXTRACT:
13070 case SIGN_EXTRACT:
13071 if (CONST_INT_P (XEXP (rtl, 1))
13072 && CONST_INT_P (XEXP (rtl, 2))
13073 && ((unsigned) INTVAL (XEXP (rtl, 1))
13074 + (unsigned) INTVAL (XEXP (rtl, 2))
13075 <= GET_MODE_BITSIZE (mode))
13076 && GET_MODE_CLASS (mode) == MODE_INT
13077 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13078 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
13080 int shift, size;
13081 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13082 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13083 if (op0 == 0)
13084 break;
13085 if (GET_CODE (rtl) == SIGN_EXTRACT)
13086 op = DW_OP_shra;
13087 else
13088 op = DW_OP_shr;
13089 mem_loc_result = op0;
13090 size = INTVAL (XEXP (rtl, 1));
13091 shift = INTVAL (XEXP (rtl, 2));
13092 if (BITS_BIG_ENDIAN)
13093 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
13094 - shift - size;
13095 if (shift + size != (int) DWARF2_ADDR_SIZE)
13097 add_loc_descr (&mem_loc_result,
13098 int_loc_descriptor (DWARF2_ADDR_SIZE
13099 - shift - size));
13100 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13102 if (size != (int) DWARF2_ADDR_SIZE)
13104 add_loc_descr (&mem_loc_result,
13105 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
13106 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13109 break;
13111 case IF_THEN_ELSE:
13113 dw_loc_descr_ref op2, bra_node, drop_node;
13114 op0 = mem_loc_descriptor (XEXP (rtl, 0),
13115 GET_MODE (XEXP (rtl, 0)) == VOIDmode
13116 ? word_mode : GET_MODE (XEXP (rtl, 0)),
13117 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13118 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13119 VAR_INIT_STATUS_INITIALIZED);
13120 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
13121 VAR_INIT_STATUS_INITIALIZED);
13122 if (op0 == NULL || op1 == NULL || op2 == NULL)
13123 break;
13125 mem_loc_result = op1;
13126 add_loc_descr (&mem_loc_result, op2);
13127 add_loc_descr (&mem_loc_result, op0);
13128 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13129 add_loc_descr (&mem_loc_result, bra_node);
13130 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
13131 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
13132 add_loc_descr (&mem_loc_result, drop_node);
13133 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13134 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
13136 break;
13138 case FLOAT_EXTEND:
13139 case FLOAT_TRUNCATE:
13140 case FLOAT:
13141 case UNSIGNED_FLOAT:
13142 case FIX:
13143 case UNSIGNED_FIX:
13144 if (!dwarf_strict)
13146 dw_die_ref type_die;
13147 dw_loc_descr_ref cvt;
13149 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13150 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13151 if (op0 == NULL)
13152 break;
13153 if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
13154 && (GET_CODE (rtl) == FLOAT
13155 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
13156 <= DWARF2_ADDR_SIZE))
13158 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
13159 GET_CODE (rtl) == UNSIGNED_FLOAT);
13160 if (type_die == NULL)
13161 break;
13162 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13163 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13164 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13165 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13166 add_loc_descr (&op0, cvt);
13168 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
13169 if (type_die == NULL)
13170 break;
13171 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13172 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13173 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13174 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13175 add_loc_descr (&op0, cvt);
13176 if (GET_MODE_CLASS (mode) == MODE_INT
13177 && (GET_CODE (rtl) == FIX
13178 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
13180 op0 = convert_descriptor_to_mode (mode, op0);
13181 if (op0 == NULL)
13182 break;
13184 mem_loc_result = op0;
13186 break;
13188 case CLZ:
13189 case CTZ:
13190 case FFS:
13191 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
13192 break;
13194 case POPCOUNT:
13195 case PARITY:
13196 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
13197 break;
13199 case BSWAP:
13200 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
13201 break;
13203 case ROTATE:
13204 case ROTATERT:
13205 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
13206 break;
13208 case COMPARE:
13209 /* In theory, we could implement the above. */
13210 /* DWARF cannot represent the unsigned compare operations
13211 natively. */
13212 case SS_MULT:
13213 case US_MULT:
13214 case SS_DIV:
13215 case US_DIV:
13216 case SS_PLUS:
13217 case US_PLUS:
13218 case SS_MINUS:
13219 case US_MINUS:
13220 case SS_NEG:
13221 case US_NEG:
13222 case SS_ABS:
13223 case SS_ASHIFT:
13224 case US_ASHIFT:
13225 case SS_TRUNCATE:
13226 case US_TRUNCATE:
13227 case UNORDERED:
13228 case ORDERED:
13229 case UNEQ:
13230 case UNGE:
13231 case UNGT:
13232 case UNLE:
13233 case UNLT:
13234 case LTGT:
13235 case FRACT_CONVERT:
13236 case UNSIGNED_FRACT_CONVERT:
13237 case SAT_FRACT:
13238 case UNSIGNED_SAT_FRACT:
13239 case SQRT:
13240 case ASM_OPERANDS:
13241 case VEC_MERGE:
13242 case VEC_SELECT:
13243 case VEC_CONCAT:
13244 case VEC_DUPLICATE:
13245 case UNSPEC:
13246 case HIGH:
13247 case FMA:
13248 case STRICT_LOW_PART:
13249 case CONST_VECTOR:
13250 case CONST_FIXED:
13251 case CLRSB:
13252 case CLOBBER:
13253 /* If delegitimize_address couldn't do anything with the UNSPEC, we
13254 can't express it in the debug info. This can happen e.g. with some
13255 TLS UNSPECs. */
13256 break;
13258 case CONST_STRING:
13259 resolve_one_addr (&rtl);
13260 goto symref;
13262 default:
13263 #ifdef ENABLE_CHECKING
13264 print_rtl (stderr, rtl);
13265 gcc_unreachable ();
13266 #else
13267 break;
13268 #endif
13271 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13272 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13274 return mem_loc_result;
13277 /* Return a descriptor that describes the concatenation of two locations.
13278 This is typically a complex variable. */
13280 static dw_loc_descr_ref
13281 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
13283 dw_loc_descr_ref cc_loc_result = NULL;
13284 dw_loc_descr_ref x0_ref
13285 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13286 dw_loc_descr_ref x1_ref
13287 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13289 if (x0_ref == 0 || x1_ref == 0)
13290 return 0;
13292 cc_loc_result = x0_ref;
13293 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
13295 add_loc_descr (&cc_loc_result, x1_ref);
13296 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
13298 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13299 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13301 return cc_loc_result;
13304 /* Return a descriptor that describes the concatenation of N
13305 locations. */
13307 static dw_loc_descr_ref
13308 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
13310 unsigned int i;
13311 dw_loc_descr_ref cc_loc_result = NULL;
13312 unsigned int n = XVECLEN (concatn, 0);
13314 for (i = 0; i < n; ++i)
13316 dw_loc_descr_ref ref;
13317 rtx x = XVECEXP (concatn, 0, i);
13319 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13320 if (ref == NULL)
13321 return NULL;
13323 add_loc_descr (&cc_loc_result, ref);
13324 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
13327 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13328 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13330 return cc_loc_result;
13333 /* Helper function for loc_descriptor. Return DW_OP_GNU_implicit_pointer
13334 for DEBUG_IMPLICIT_PTR RTL. */
13336 static dw_loc_descr_ref
13337 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
13339 dw_loc_descr_ref ret;
13340 dw_die_ref ref;
13342 if (dwarf_strict)
13343 return NULL;
13344 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
13345 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
13346 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
13347 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
13348 ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
13349 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
13350 if (ref)
13352 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13353 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
13354 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
13356 else
13358 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
13359 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
13361 return ret;
13364 /* Output a proper Dwarf location descriptor for a variable or parameter
13365 which is either allocated in a register or in a memory location. For a
13366 register, we just generate an OP_REG and the register number. For a
13367 memory location we provide a Dwarf postfix expression describing how to
13368 generate the (dynamic) address of the object onto the address stack.
13370 MODE is mode of the decl if this loc_descriptor is going to be used in
13371 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
13372 allowed, VOIDmode otherwise.
13374 If we don't know how to describe it, return 0. */
13376 static dw_loc_descr_ref
13377 loc_descriptor (rtx rtl, enum machine_mode mode,
13378 enum var_init_status initialized)
13380 dw_loc_descr_ref loc_result = NULL;
13382 switch (GET_CODE (rtl))
13384 case SUBREG:
13385 /* The case of a subreg may arise when we have a local (register)
13386 variable or a formal (register) parameter which doesn't quite fill
13387 up an entire register. For now, just assume that it is
13388 legitimate to make the Dwarf info refer to the whole register which
13389 contains the given subreg. */
13390 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
13391 loc_result = loc_descriptor (SUBREG_REG (rtl),
13392 GET_MODE (SUBREG_REG (rtl)), initialized);
13393 else
13394 goto do_default;
13395 break;
13397 case REG:
13398 loc_result = reg_loc_descriptor (rtl, initialized);
13399 break;
13401 case MEM:
13402 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13403 GET_MODE (rtl), initialized);
13404 if (loc_result == NULL)
13405 loc_result = tls_mem_loc_descriptor (rtl);
13406 if (loc_result == NULL)
13408 rtx new_rtl = avoid_constant_pool_reference (rtl);
13409 if (new_rtl != rtl)
13410 loc_result = loc_descriptor (new_rtl, mode, initialized);
13412 break;
13414 case CONCAT:
13415 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
13416 initialized);
13417 break;
13419 case CONCATN:
13420 loc_result = concatn_loc_descriptor (rtl, initialized);
13421 break;
13423 case VAR_LOCATION:
13424 /* Single part. */
13425 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
13427 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
13428 if (GET_CODE (loc) == EXPR_LIST)
13429 loc = XEXP (loc, 0);
13430 loc_result = loc_descriptor (loc, mode, initialized);
13431 break;
13434 rtl = XEXP (rtl, 1);
13435 /* FALLTHRU */
13437 case PARALLEL:
13439 rtvec par_elems = XVEC (rtl, 0);
13440 int num_elem = GET_NUM_ELEM (par_elems);
13441 enum machine_mode mode;
13442 int i;
13444 /* Create the first one, so we have something to add to. */
13445 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
13446 VOIDmode, initialized);
13447 if (loc_result == NULL)
13448 return NULL;
13449 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
13450 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13451 for (i = 1; i < num_elem; i++)
13453 dw_loc_descr_ref temp;
13455 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
13456 VOIDmode, initialized);
13457 if (temp == NULL)
13458 return NULL;
13459 add_loc_descr (&loc_result, temp);
13460 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
13461 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13464 break;
13466 case CONST_INT:
13467 if (mode != VOIDmode && mode != BLKmode)
13468 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
13469 INTVAL (rtl));
13470 break;
13472 case CONST_DOUBLE:
13473 if (mode == VOIDmode)
13474 mode = GET_MODE (rtl);
13476 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13478 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13480 /* Note that a CONST_DOUBLE rtx could represent either an integer
13481 or a floating-point constant. A CONST_DOUBLE is used whenever
13482 the constant requires more than one word in order to be
13483 adequately represented. We output CONST_DOUBLEs as blocks. */
13484 loc_result = new_loc_descr (DW_OP_implicit_value,
13485 GET_MODE_SIZE (mode), 0);
13486 #if TARGET_SUPPORTS_WIDE_INT == 0
13487 if (!SCALAR_FLOAT_MODE_P (mode))
13489 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
13490 loc_result->dw_loc_oprnd2.v.val_double
13491 = rtx_to_double_int (rtl);
13493 else
13494 #endif
13496 unsigned int length = GET_MODE_SIZE (mode);
13497 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
13499 insert_float (rtl, array);
13500 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13501 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13502 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13503 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13506 break;
13508 case CONST_WIDE_INT:
13509 if (mode == VOIDmode)
13510 mode = GET_MODE (rtl);
13512 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13514 loc_result = new_loc_descr (DW_OP_implicit_value,
13515 GET_MODE_SIZE (mode), 0);
13516 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
13517 loc_result->dw_loc_oprnd2.v.val_wide = ggc_cleared_alloc<wide_int> ();
13518 *loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
13520 break;
13522 case CONST_VECTOR:
13523 if (mode == VOIDmode)
13524 mode = GET_MODE (rtl);
13526 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13528 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
13529 unsigned int length = CONST_VECTOR_NUNITS (rtl);
13530 unsigned char *array
13531 = ggc_vec_alloc<unsigned char> (length * elt_size);
13532 unsigned int i;
13533 unsigned char *p;
13534 enum machine_mode imode = GET_MODE_INNER (mode);
13536 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13537 switch (GET_MODE_CLASS (mode))
13539 case MODE_VECTOR_INT:
13540 for (i = 0, p = array; i < length; i++, p += elt_size)
13542 rtx elt = CONST_VECTOR_ELT (rtl, i);
13543 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
13545 break;
13547 case MODE_VECTOR_FLOAT:
13548 for (i = 0, p = array; i < length; i++, p += elt_size)
13550 rtx elt = CONST_VECTOR_ELT (rtl, i);
13551 insert_float (elt, p);
13553 break;
13555 default:
13556 gcc_unreachable ();
13559 loc_result = new_loc_descr (DW_OP_implicit_value,
13560 length * elt_size, 0);
13561 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13562 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
13563 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
13564 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13566 break;
13568 case CONST:
13569 if (mode == VOIDmode
13570 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
13571 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
13572 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
13574 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
13575 break;
13577 /* FALLTHROUGH */
13578 case SYMBOL_REF:
13579 if (!const_ok_for_output (rtl))
13580 break;
13581 case LABEL_REF:
13582 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
13583 && (dwarf_version >= 4 || !dwarf_strict))
13585 loc_result = new_addr_loc_descr (rtl, dtprel_false);
13586 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
13587 vec_safe_push (used_rtx_array, rtl);
13589 break;
13591 case DEBUG_IMPLICIT_PTR:
13592 loc_result = implicit_ptr_descriptor (rtl, 0);
13593 break;
13595 case PLUS:
13596 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
13597 && CONST_INT_P (XEXP (rtl, 1)))
13599 loc_result
13600 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
13601 break;
13603 /* FALLTHRU */
13604 do_default:
13605 default:
13606 if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
13607 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
13608 && dwarf_version >= 4)
13609 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
13611 /* Value expression. */
13612 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
13613 if (loc_result)
13614 add_loc_descr (&loc_result,
13615 new_loc_descr (DW_OP_stack_value, 0, 0));
13617 break;
13620 return loc_result;
13623 /* We need to figure out what section we should use as the base for the
13624 address ranges where a given location is valid.
13625 1. If this particular DECL has a section associated with it, use that.
13626 2. If this function has a section associated with it, use that.
13627 3. Otherwise, use the text section.
13628 XXX: If you split a variable across multiple sections, we won't notice. */
13630 static const char *
13631 secname_for_decl (const_tree decl)
13633 const char *secname;
13635 if (VAR_OR_FUNCTION_DECL_P (decl)
13636 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
13637 && DECL_SECTION_NAME (decl))
13638 secname = DECL_SECTION_NAME (decl);
13639 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
13640 secname = DECL_SECTION_NAME (current_function_decl);
13641 else if (cfun && in_cold_section_p)
13642 secname = crtl->subsections.cold_section_label;
13643 else
13644 secname = text_section_label;
13646 return secname;
13649 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
13651 static bool
13652 decl_by_reference_p (tree decl)
13654 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
13655 || TREE_CODE (decl) == VAR_DECL)
13656 && DECL_BY_REFERENCE (decl));
13659 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13660 for VARLOC. */
13662 static dw_loc_descr_ref
13663 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
13664 enum var_init_status initialized)
13666 int have_address = 0;
13667 dw_loc_descr_ref descr;
13668 enum machine_mode mode;
13670 if (want_address != 2)
13672 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
13673 /* Single part. */
13674 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13676 varloc = PAT_VAR_LOCATION_LOC (varloc);
13677 if (GET_CODE (varloc) == EXPR_LIST)
13678 varloc = XEXP (varloc, 0);
13679 mode = GET_MODE (varloc);
13680 if (MEM_P (varloc))
13682 rtx addr = XEXP (varloc, 0);
13683 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
13684 mode, initialized);
13685 if (descr)
13686 have_address = 1;
13687 else
13689 rtx x = avoid_constant_pool_reference (varloc);
13690 if (x != varloc)
13691 descr = mem_loc_descriptor (x, mode, VOIDmode,
13692 initialized);
13695 else
13696 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
13698 else
13699 return 0;
13701 else
13703 if (GET_CODE (varloc) == VAR_LOCATION)
13704 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
13705 else
13706 mode = DECL_MODE (loc);
13707 descr = loc_descriptor (varloc, mode, initialized);
13708 have_address = 1;
13711 if (!descr)
13712 return 0;
13714 if (want_address == 2 && !have_address
13715 && (dwarf_version >= 4 || !dwarf_strict))
13717 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
13719 expansion_failed (loc, NULL_RTX,
13720 "DWARF address size mismatch");
13721 return 0;
13723 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
13724 have_address = 1;
13726 /* Show if we can't fill the request for an address. */
13727 if (want_address && !have_address)
13729 expansion_failed (loc, NULL_RTX,
13730 "Want address and only have value");
13731 return 0;
13734 /* If we've got an address and don't want one, dereference. */
13735 if (!want_address && have_address)
13737 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
13738 enum dwarf_location_atom op;
13740 if (size > DWARF2_ADDR_SIZE || size == -1)
13742 expansion_failed (loc, NULL_RTX,
13743 "DWARF address size mismatch");
13744 return 0;
13746 else if (size == DWARF2_ADDR_SIZE)
13747 op = DW_OP_deref;
13748 else
13749 op = DW_OP_deref_size;
13751 add_loc_descr (&descr, new_loc_descr (op, size, 0));
13754 return descr;
13757 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
13758 if it is not possible. */
13760 static dw_loc_descr_ref
13761 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
13763 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
13764 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
13765 else if (dwarf_version >= 3 || !dwarf_strict)
13766 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
13767 else
13768 return NULL;
13771 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13772 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
13774 static dw_loc_descr_ref
13775 dw_sra_loc_expr (tree decl, rtx loc)
13777 rtx p;
13778 unsigned int padsize = 0;
13779 dw_loc_descr_ref descr, *descr_tail;
13780 unsigned HOST_WIDE_INT decl_size;
13781 rtx varloc;
13782 enum var_init_status initialized;
13784 if (DECL_SIZE (decl) == NULL
13785 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
13786 return NULL;
13788 decl_size = tree_to_uhwi (DECL_SIZE (decl));
13789 descr = NULL;
13790 descr_tail = &descr;
13792 for (p = loc; p; p = XEXP (p, 1))
13794 unsigned int bitsize = decl_piece_bitsize (p);
13795 rtx loc_note = *decl_piece_varloc_ptr (p);
13796 dw_loc_descr_ref cur_descr;
13797 dw_loc_descr_ref *tail, last = NULL;
13798 unsigned int opsize = 0;
13800 if (loc_note == NULL_RTX
13801 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
13803 padsize += bitsize;
13804 continue;
13806 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
13807 varloc = NOTE_VAR_LOCATION (loc_note);
13808 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
13809 if (cur_descr == NULL)
13811 padsize += bitsize;
13812 continue;
13815 /* Check that cur_descr either doesn't use
13816 DW_OP_*piece operations, or their sum is equal
13817 to bitsize. Otherwise we can't embed it. */
13818 for (tail = &cur_descr; *tail != NULL;
13819 tail = &(*tail)->dw_loc_next)
13820 if ((*tail)->dw_loc_opc == DW_OP_piece)
13822 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
13823 * BITS_PER_UNIT;
13824 last = *tail;
13826 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
13828 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
13829 last = *tail;
13832 if (last != NULL && opsize != bitsize)
13834 padsize += bitsize;
13835 /* Discard the current piece of the descriptor and release any
13836 addr_table entries it uses. */
13837 remove_loc_list_addr_table_entries (cur_descr);
13838 continue;
13841 /* If there is a hole, add DW_OP_*piece after empty DWARF
13842 expression, which means that those bits are optimized out. */
13843 if (padsize)
13845 if (padsize > decl_size)
13847 remove_loc_list_addr_table_entries (cur_descr);
13848 goto discard_descr;
13850 decl_size -= padsize;
13851 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
13852 if (*descr_tail == NULL)
13854 remove_loc_list_addr_table_entries (cur_descr);
13855 goto discard_descr;
13857 descr_tail = &(*descr_tail)->dw_loc_next;
13858 padsize = 0;
13860 *descr_tail = cur_descr;
13861 descr_tail = tail;
13862 if (bitsize > decl_size)
13863 goto discard_descr;
13864 decl_size -= bitsize;
13865 if (last == NULL)
13867 HOST_WIDE_INT offset = 0;
13868 if (GET_CODE (varloc) == VAR_LOCATION
13869 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13871 varloc = PAT_VAR_LOCATION_LOC (varloc);
13872 if (GET_CODE (varloc) == EXPR_LIST)
13873 varloc = XEXP (varloc, 0);
13877 if (GET_CODE (varloc) == CONST
13878 || GET_CODE (varloc) == SIGN_EXTEND
13879 || GET_CODE (varloc) == ZERO_EXTEND)
13880 varloc = XEXP (varloc, 0);
13881 else if (GET_CODE (varloc) == SUBREG)
13882 varloc = SUBREG_REG (varloc);
13883 else
13884 break;
13886 while (1);
13887 /* DW_OP_bit_size offset should be zero for register
13888 or implicit location descriptions and empty location
13889 descriptions, but for memory addresses needs big endian
13890 adjustment. */
13891 if (MEM_P (varloc))
13893 unsigned HOST_WIDE_INT memsize
13894 = MEM_SIZE (varloc) * BITS_PER_UNIT;
13895 if (memsize != bitsize)
13897 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
13898 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
13899 goto discard_descr;
13900 if (memsize < bitsize)
13901 goto discard_descr;
13902 if (BITS_BIG_ENDIAN)
13903 offset = memsize - bitsize;
13907 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
13908 if (*descr_tail == NULL)
13909 goto discard_descr;
13910 descr_tail = &(*descr_tail)->dw_loc_next;
13914 /* If there were any non-empty expressions, add padding till the end of
13915 the decl. */
13916 if (descr != NULL && decl_size != 0)
13918 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
13919 if (*descr_tail == NULL)
13920 goto discard_descr;
13922 return descr;
13924 discard_descr:
13925 /* Discard the descriptor and release any addr_table entries it uses. */
13926 remove_loc_list_addr_table_entries (descr);
13927 return NULL;
13930 /* Return the dwarf representation of the location list LOC_LIST of
13931 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
13932 function. */
13934 static dw_loc_list_ref
13935 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
13937 const char *endname, *secname;
13938 rtx varloc;
13939 enum var_init_status initialized;
13940 struct var_loc_node *node;
13941 dw_loc_descr_ref descr;
13942 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
13943 dw_loc_list_ref list = NULL;
13944 dw_loc_list_ref *listp = &list;
13946 /* Now that we know what section we are using for a base,
13947 actually construct the list of locations.
13948 The first location information is what is passed to the
13949 function that creates the location list, and the remaining
13950 locations just get added on to that list.
13951 Note that we only know the start address for a location
13952 (IE location changes), so to build the range, we use
13953 the range [current location start, next location start].
13954 This means we have to special case the last node, and generate
13955 a range of [last location start, end of function label]. */
13957 secname = secname_for_decl (decl);
13959 for (node = loc_list->first; node; node = node->next)
13960 if (GET_CODE (node->loc) == EXPR_LIST
13961 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
13963 if (GET_CODE (node->loc) == EXPR_LIST)
13965 /* This requires DW_OP_{,bit_}piece, which is not usable
13966 inside DWARF expressions. */
13967 if (want_address != 2)
13968 continue;
13969 descr = dw_sra_loc_expr (decl, node->loc);
13970 if (descr == NULL)
13971 continue;
13973 else
13975 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13976 varloc = NOTE_VAR_LOCATION (node->loc);
13977 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
13979 if (descr)
13981 bool range_across_switch = false;
13982 /* If section switch happens in between node->label
13983 and node->next->label (or end of function) and
13984 we can't emit it as a single entry list,
13985 emit two ranges, first one ending at the end
13986 of first partition and second one starting at the
13987 beginning of second partition. */
13988 if (node == loc_list->last_before_switch
13989 && (node != loc_list->first || loc_list->first->next)
13990 && current_function_decl)
13992 endname = cfun->fde->dw_fde_end;
13993 range_across_switch = true;
13995 /* The variable has a location between NODE->LABEL and
13996 NODE->NEXT->LABEL. */
13997 else if (node->next)
13998 endname = node->next->label;
13999 /* If the variable has a location at the last label
14000 it keeps its location until the end of function. */
14001 else if (!current_function_decl)
14002 endname = text_end_label;
14003 else
14005 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
14006 current_function_funcdef_no);
14007 endname = ggc_strdup (label_id);
14010 *listp = new_loc_list (descr, node->label, endname, secname);
14011 if (TREE_CODE (decl) == PARM_DECL
14012 && node == loc_list->first
14013 && NOTE_P (node->loc)
14014 && strcmp (node->label, endname) == 0)
14015 (*listp)->force = true;
14016 listp = &(*listp)->dw_loc_next;
14018 if (range_across_switch)
14020 if (GET_CODE (node->loc) == EXPR_LIST)
14021 descr = dw_sra_loc_expr (decl, node->loc);
14022 else
14024 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14025 varloc = NOTE_VAR_LOCATION (node->loc);
14026 descr = dw_loc_list_1 (decl, varloc, want_address,
14027 initialized);
14029 gcc_assert (descr);
14030 /* The variable has a location between NODE->LABEL and
14031 NODE->NEXT->LABEL. */
14032 if (node->next)
14033 endname = node->next->label;
14034 else
14035 endname = cfun->fde->dw_fde_second_end;
14036 *listp = new_loc_list (descr,
14037 cfun->fde->dw_fde_second_begin,
14038 endname, secname);
14039 listp = &(*listp)->dw_loc_next;
14044 /* Try to avoid the overhead of a location list emitting a location
14045 expression instead, but only if we didn't have more than one
14046 location entry in the first place. If some entries were not
14047 representable, we don't want to pretend a single entry that was
14048 applies to the entire scope in which the variable is
14049 available. */
14050 if (list && loc_list->first->next)
14051 gen_llsym (list);
14053 return list;
14056 /* Return if the loc_list has only single element and thus can be represented
14057 as location description. */
14059 static bool
14060 single_element_loc_list_p (dw_loc_list_ref list)
14062 gcc_assert (!list->dw_loc_next || list->ll_symbol);
14063 return !list->ll_symbol;
14066 /* To each location in list LIST add loc descr REF. */
14068 static void
14069 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
14071 dw_loc_descr_ref copy;
14072 add_loc_descr (&list->expr, ref);
14073 list = list->dw_loc_next;
14074 while (list)
14076 copy = ggc_alloc<dw_loc_descr_node> ();
14077 memcpy (copy, ref, sizeof (dw_loc_descr_node));
14078 add_loc_descr (&list->expr, copy);
14079 while (copy->dw_loc_next)
14081 dw_loc_descr_ref new_copy = ggc_alloc<dw_loc_descr_node> ();
14082 memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
14083 copy->dw_loc_next = new_copy;
14084 copy = new_copy;
14086 list = list->dw_loc_next;
14090 /* Given two lists RET and LIST
14091 produce location list that is result of adding expression in LIST
14092 to expression in RET on each position in program.
14093 Might be destructive on both RET and LIST.
14095 TODO: We handle only simple cases of RET or LIST having at most one
14096 element. General case would inolve sorting the lists in program order
14097 and merging them that will need some additional work.
14098 Adding that will improve quality of debug info especially for SRA-ed
14099 structures. */
14101 static void
14102 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
14104 if (!list)
14105 return;
14106 if (!*ret)
14108 *ret = list;
14109 return;
14111 if (!list->dw_loc_next)
14113 add_loc_descr_to_each (*ret, list->expr);
14114 return;
14116 if (!(*ret)->dw_loc_next)
14118 add_loc_descr_to_each (list, (*ret)->expr);
14119 *ret = list;
14120 return;
14122 expansion_failed (NULL_TREE, NULL_RTX,
14123 "Don't know how to merge two non-trivial"
14124 " location lists.\n");
14125 *ret = NULL;
14126 return;
14129 /* LOC is constant expression. Try a luck, look it up in constant
14130 pool and return its loc_descr of its address. */
14132 static dw_loc_descr_ref
14133 cst_pool_loc_descr (tree loc)
14135 /* Get an RTL for this, if something has been emitted. */
14136 rtx rtl = lookup_constant_def (loc);
14138 if (!rtl || !MEM_P (rtl))
14140 gcc_assert (!rtl);
14141 return 0;
14143 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
14145 /* TODO: We might get more coverage if we was actually delaying expansion
14146 of all expressions till end of compilation when constant pools are fully
14147 populated. */
14148 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
14150 expansion_failed (loc, NULL_RTX,
14151 "CST value in contant pool but not marked.");
14152 return 0;
14154 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
14155 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
14158 /* Return dw_loc_list representing address of addr_expr LOC
14159 by looking for inner INDIRECT_REF expression and turning
14160 it into simple arithmetics. */
14162 static dw_loc_list_ref
14163 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
14165 tree obj, offset;
14166 HOST_WIDE_INT bitsize, bitpos, bytepos;
14167 enum machine_mode mode;
14168 int unsignedp, volatilep = 0;
14169 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14171 obj = get_inner_reference (TREE_OPERAND (loc, 0),
14172 &bitsize, &bitpos, &offset, &mode,
14173 &unsignedp, &volatilep, false);
14174 STRIP_NOPS (obj);
14175 if (bitpos % BITS_PER_UNIT)
14177 expansion_failed (loc, NULL_RTX, "bitfield access");
14178 return 0;
14180 if (!INDIRECT_REF_P (obj))
14182 expansion_failed (obj,
14183 NULL_RTX, "no indirect ref in inner refrence");
14184 return 0;
14186 if (!offset && !bitpos)
14187 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
14188 else if (toplev
14189 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
14190 && (dwarf_version >= 4 || !dwarf_strict))
14192 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
14193 if (!list_ret)
14194 return 0;
14195 if (offset)
14197 /* Variable offset. */
14198 list_ret1 = loc_list_from_tree (offset, 0);
14199 if (list_ret1 == 0)
14200 return 0;
14201 add_loc_list (&list_ret, list_ret1);
14202 if (!list_ret)
14203 return 0;
14204 add_loc_descr_to_each (list_ret,
14205 new_loc_descr (DW_OP_plus, 0, 0));
14207 bytepos = bitpos / BITS_PER_UNIT;
14208 if (bytepos > 0)
14209 add_loc_descr_to_each (list_ret,
14210 new_loc_descr (DW_OP_plus_uconst,
14211 bytepos, 0));
14212 else if (bytepos < 0)
14213 loc_list_plus_const (list_ret, bytepos);
14214 add_loc_descr_to_each (list_ret,
14215 new_loc_descr (DW_OP_stack_value, 0, 0));
14217 return list_ret;
14221 /* Generate Dwarf location list representing LOC.
14222 If WANT_ADDRESS is false, expression computing LOC will be computed
14223 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
14224 if WANT_ADDRESS is 2, expression computing address useable in location
14225 will be returned (i.e. DW_OP_reg can be used
14226 to refer to register values). */
14228 static dw_loc_list_ref
14229 loc_list_from_tree (tree loc, int want_address)
14231 dw_loc_descr_ref ret = NULL, ret1 = NULL;
14232 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14233 int have_address = 0;
14234 enum dwarf_location_atom op;
14236 /* ??? Most of the time we do not take proper care for sign/zero
14237 extending the values properly. Hopefully this won't be a real
14238 problem... */
14240 switch (TREE_CODE (loc))
14242 case ERROR_MARK:
14243 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
14244 return 0;
14246 case PLACEHOLDER_EXPR:
14247 /* This case involves extracting fields from an object to determine the
14248 position of other fields. We don't try to encode this here. The
14249 only user of this is Ada, which encodes the needed information using
14250 the names of types. */
14251 expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
14252 return 0;
14254 case CALL_EXPR:
14255 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
14256 /* There are no opcodes for these operations. */
14257 return 0;
14259 case PREINCREMENT_EXPR:
14260 case PREDECREMENT_EXPR:
14261 case POSTINCREMENT_EXPR:
14262 case POSTDECREMENT_EXPR:
14263 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
14264 /* There are no opcodes for these operations. */
14265 return 0;
14267 case ADDR_EXPR:
14268 /* If we already want an address, see if there is INDIRECT_REF inside
14269 e.g. for &this->field. */
14270 if (want_address)
14272 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
14273 (loc, want_address == 2);
14274 if (list_ret)
14275 have_address = 1;
14276 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
14277 && (ret = cst_pool_loc_descr (loc)))
14278 have_address = 1;
14280 /* Otherwise, process the argument and look for the address. */
14281 if (!list_ret && !ret)
14282 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
14283 else
14285 if (want_address)
14286 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
14287 return NULL;
14289 break;
14291 case VAR_DECL:
14292 if (DECL_THREAD_LOCAL_P (loc))
14294 rtx rtl;
14295 enum dwarf_location_atom tls_op;
14296 enum dtprel_bool dtprel = dtprel_false;
14298 if (targetm.have_tls)
14300 /* If this is not defined, we have no way to emit the
14301 data. */
14302 if (!targetm.asm_out.output_dwarf_dtprel)
14303 return 0;
14305 /* The way DW_OP_GNU_push_tls_address is specified, we
14306 can only look up addresses of objects in the current
14307 module. We used DW_OP_addr as first op, but that's
14308 wrong, because DW_OP_addr is relocated by the debug
14309 info consumer, while DW_OP_GNU_push_tls_address
14310 operand shouldn't be. */
14311 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
14312 return 0;
14313 dtprel = dtprel_true;
14314 tls_op = DW_OP_GNU_push_tls_address;
14316 else
14318 if (!targetm.emutls.debug_form_tls_address
14319 || !(dwarf_version >= 3 || !dwarf_strict))
14320 return 0;
14321 /* We stuffed the control variable into the DECL_VALUE_EXPR
14322 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
14323 no longer appear in gimple code. We used the control
14324 variable in specific so that we could pick it up here. */
14325 loc = DECL_VALUE_EXPR (loc);
14326 tls_op = DW_OP_form_tls_address;
14329 rtl = rtl_for_decl_location (loc);
14330 if (rtl == NULL_RTX)
14331 return 0;
14333 if (!MEM_P (rtl))
14334 return 0;
14335 rtl = XEXP (rtl, 0);
14336 if (! CONSTANT_P (rtl))
14337 return 0;
14339 ret = new_addr_loc_descr (rtl, dtprel);
14340 ret1 = new_loc_descr (tls_op, 0, 0);
14341 add_loc_descr (&ret, ret1);
14343 have_address = 1;
14344 break;
14346 /* FALLTHRU */
14348 case PARM_DECL:
14349 case RESULT_DECL:
14350 if (DECL_HAS_VALUE_EXPR_P (loc))
14351 return loc_list_from_tree (DECL_VALUE_EXPR (loc),
14352 want_address);
14353 /* FALLTHRU */
14355 case FUNCTION_DECL:
14357 rtx rtl;
14358 var_loc_list *loc_list = lookup_decl_loc (loc);
14360 if (loc_list && loc_list->first)
14362 list_ret = dw_loc_list (loc_list, loc, want_address);
14363 have_address = want_address != 0;
14364 break;
14366 rtl = rtl_for_decl_location (loc);
14367 if (rtl == NULL_RTX)
14369 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
14370 return 0;
14372 else if (CONST_INT_P (rtl))
14374 HOST_WIDE_INT val = INTVAL (rtl);
14375 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14376 val &= GET_MODE_MASK (DECL_MODE (loc));
14377 ret = int_loc_descriptor (val);
14379 else if (GET_CODE (rtl) == CONST_STRING)
14381 expansion_failed (loc, NULL_RTX, "CONST_STRING");
14382 return 0;
14384 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
14385 ret = new_addr_loc_descr (rtl, dtprel_false);
14386 else
14388 enum machine_mode mode, mem_mode;
14390 /* Certain constructs can only be represented at top-level. */
14391 if (want_address == 2)
14393 ret = loc_descriptor (rtl, VOIDmode,
14394 VAR_INIT_STATUS_INITIALIZED);
14395 have_address = 1;
14397 else
14399 mode = GET_MODE (rtl);
14400 mem_mode = VOIDmode;
14401 if (MEM_P (rtl))
14403 mem_mode = mode;
14404 mode = get_address_mode (rtl);
14405 rtl = XEXP (rtl, 0);
14406 have_address = 1;
14408 ret = mem_loc_descriptor (rtl, mode, mem_mode,
14409 VAR_INIT_STATUS_INITIALIZED);
14411 if (!ret)
14412 expansion_failed (loc, rtl,
14413 "failed to produce loc descriptor for rtl");
14416 break;
14418 case MEM_REF:
14419 /* ??? FIXME. */
14420 if (!integer_zerop (TREE_OPERAND (loc, 1)))
14421 return 0;
14422 /* Fallthru. */
14423 case INDIRECT_REF:
14424 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14425 have_address = 1;
14426 break;
14428 case COMPOUND_EXPR:
14429 return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
14431 CASE_CONVERT:
14432 case VIEW_CONVERT_EXPR:
14433 case SAVE_EXPR:
14434 case MODIFY_EXPR:
14435 return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
14437 case COMPONENT_REF:
14438 case BIT_FIELD_REF:
14439 case ARRAY_REF:
14440 case ARRAY_RANGE_REF:
14441 case REALPART_EXPR:
14442 case IMAGPART_EXPR:
14444 tree obj, offset;
14445 HOST_WIDE_INT bitsize, bitpos, bytepos;
14446 enum machine_mode mode;
14447 int unsignedp, volatilep = 0;
14449 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
14450 &unsignedp, &volatilep, false);
14452 gcc_assert (obj != loc);
14454 list_ret = loc_list_from_tree (obj,
14455 want_address == 2
14456 && !bitpos && !offset ? 2 : 1);
14457 /* TODO: We can extract value of the small expression via shifting even
14458 for nonzero bitpos. */
14459 if (list_ret == 0)
14460 return 0;
14461 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
14463 expansion_failed (loc, NULL_RTX,
14464 "bitfield access");
14465 return 0;
14468 if (offset != NULL_TREE)
14470 /* Variable offset. */
14471 list_ret1 = loc_list_from_tree (offset, 0);
14472 if (list_ret1 == 0)
14473 return 0;
14474 add_loc_list (&list_ret, list_ret1);
14475 if (!list_ret)
14476 return 0;
14477 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
14480 bytepos = bitpos / BITS_PER_UNIT;
14481 if (bytepos > 0)
14482 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
14483 else if (bytepos < 0)
14484 loc_list_plus_const (list_ret, bytepos);
14486 have_address = 1;
14487 break;
14490 case INTEGER_CST:
14491 if ((want_address || !tree_fits_shwi_p (loc))
14492 && (ret = cst_pool_loc_descr (loc)))
14493 have_address = 1;
14494 else if (want_address == 2
14495 && tree_fits_shwi_p (loc)
14496 && (ret = address_of_int_loc_descriptor
14497 (int_size_in_bytes (TREE_TYPE (loc)),
14498 tree_to_shwi (loc))))
14499 have_address = 1;
14500 else if (tree_fits_shwi_p (loc))
14501 ret = int_loc_descriptor (tree_to_shwi (loc));
14502 else
14504 expansion_failed (loc, NULL_RTX,
14505 "Integer operand is not host integer");
14506 return 0;
14508 break;
14510 case CONSTRUCTOR:
14511 case REAL_CST:
14512 case STRING_CST:
14513 case COMPLEX_CST:
14514 if ((ret = cst_pool_loc_descr (loc)))
14515 have_address = 1;
14516 else
14517 /* We can construct small constants here using int_loc_descriptor. */
14518 expansion_failed (loc, NULL_RTX,
14519 "constructor or constant not in constant pool");
14520 break;
14522 case TRUTH_AND_EXPR:
14523 case TRUTH_ANDIF_EXPR:
14524 case BIT_AND_EXPR:
14525 op = DW_OP_and;
14526 goto do_binop;
14528 case TRUTH_XOR_EXPR:
14529 case BIT_XOR_EXPR:
14530 op = DW_OP_xor;
14531 goto do_binop;
14533 case TRUTH_OR_EXPR:
14534 case TRUTH_ORIF_EXPR:
14535 case BIT_IOR_EXPR:
14536 op = DW_OP_or;
14537 goto do_binop;
14539 case FLOOR_DIV_EXPR:
14540 case CEIL_DIV_EXPR:
14541 case ROUND_DIV_EXPR:
14542 case TRUNC_DIV_EXPR:
14543 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14544 return 0;
14545 op = DW_OP_div;
14546 goto do_binop;
14548 case MINUS_EXPR:
14549 op = DW_OP_minus;
14550 goto do_binop;
14552 case FLOOR_MOD_EXPR:
14553 case CEIL_MOD_EXPR:
14554 case ROUND_MOD_EXPR:
14555 case TRUNC_MOD_EXPR:
14556 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14558 op = DW_OP_mod;
14559 goto do_binop;
14561 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14562 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14563 if (list_ret == 0 || list_ret1 == 0)
14564 return 0;
14566 add_loc_list (&list_ret, list_ret1);
14567 if (list_ret == 0)
14568 return 0;
14569 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14570 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14571 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
14572 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
14573 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
14574 break;
14576 case MULT_EXPR:
14577 op = DW_OP_mul;
14578 goto do_binop;
14580 case LSHIFT_EXPR:
14581 op = DW_OP_shl;
14582 goto do_binop;
14584 case RSHIFT_EXPR:
14585 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
14586 goto do_binop;
14588 case POINTER_PLUS_EXPR:
14589 case PLUS_EXPR:
14590 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
14592 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14593 if (list_ret == 0)
14594 return 0;
14596 loc_list_plus_const (list_ret, tree_to_shwi (TREE_OPERAND (loc, 1)));
14597 break;
14600 op = DW_OP_plus;
14601 goto do_binop;
14603 case LE_EXPR:
14604 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14605 return 0;
14607 op = DW_OP_le;
14608 goto do_binop;
14610 case GE_EXPR:
14611 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14612 return 0;
14614 op = DW_OP_ge;
14615 goto do_binop;
14617 case LT_EXPR:
14618 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14619 return 0;
14621 op = DW_OP_lt;
14622 goto do_binop;
14624 case GT_EXPR:
14625 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14626 return 0;
14628 op = DW_OP_gt;
14629 goto do_binop;
14631 case EQ_EXPR:
14632 op = DW_OP_eq;
14633 goto do_binop;
14635 case NE_EXPR:
14636 op = DW_OP_ne;
14637 goto do_binop;
14639 do_binop:
14640 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14641 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14642 if (list_ret == 0 || list_ret1 == 0)
14643 return 0;
14645 add_loc_list (&list_ret, list_ret1);
14646 if (list_ret == 0)
14647 return 0;
14648 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14649 break;
14651 case TRUTH_NOT_EXPR:
14652 case BIT_NOT_EXPR:
14653 op = DW_OP_not;
14654 goto do_unop;
14656 case ABS_EXPR:
14657 op = DW_OP_abs;
14658 goto do_unop;
14660 case NEGATE_EXPR:
14661 op = DW_OP_neg;
14662 goto do_unop;
14664 do_unop:
14665 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14666 if (list_ret == 0)
14667 return 0;
14669 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14670 break;
14672 case MIN_EXPR:
14673 case MAX_EXPR:
14675 const enum tree_code code =
14676 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
14678 loc = build3 (COND_EXPR, TREE_TYPE (loc),
14679 build2 (code, integer_type_node,
14680 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
14681 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
14684 /* ... fall through ... */
14686 case COND_EXPR:
14688 dw_loc_descr_ref lhs
14689 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
14690 dw_loc_list_ref rhs
14691 = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
14692 dw_loc_descr_ref bra_node, jump_node, tmp;
14694 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14695 if (list_ret == 0 || lhs == 0 || rhs == 0)
14696 return 0;
14698 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14699 add_loc_descr_to_each (list_ret, bra_node);
14701 add_loc_list (&list_ret, rhs);
14702 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
14703 add_loc_descr_to_each (list_ret, jump_node);
14705 add_loc_descr_to_each (list_ret, lhs);
14706 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14707 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
14709 /* ??? Need a node to point the skip at. Use a nop. */
14710 tmp = new_loc_descr (DW_OP_nop, 0, 0);
14711 add_loc_descr_to_each (list_ret, tmp);
14712 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14713 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
14715 break;
14717 case FIX_TRUNC_EXPR:
14718 return 0;
14720 default:
14721 /* Leave front-end specific codes as simply unknown. This comes
14722 up, for instance, with the C STMT_EXPR. */
14723 if ((unsigned int) TREE_CODE (loc)
14724 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
14726 expansion_failed (loc, NULL_RTX,
14727 "language specific tree node");
14728 return 0;
14731 #ifdef ENABLE_CHECKING
14732 /* Otherwise this is a generic code; we should just lists all of
14733 these explicitly. We forgot one. */
14734 gcc_unreachable ();
14735 #else
14736 /* In a release build, we want to degrade gracefully: better to
14737 generate incomplete debugging information than to crash. */
14738 return NULL;
14739 #endif
14742 if (!ret && !list_ret)
14743 return 0;
14745 if (want_address == 2 && !have_address
14746 && (dwarf_version >= 4 || !dwarf_strict))
14748 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14750 expansion_failed (loc, NULL_RTX,
14751 "DWARF address size mismatch");
14752 return 0;
14754 if (ret)
14755 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
14756 else
14757 add_loc_descr_to_each (list_ret,
14758 new_loc_descr (DW_OP_stack_value, 0, 0));
14759 have_address = 1;
14761 /* Show if we can't fill the request for an address. */
14762 if (want_address && !have_address)
14764 expansion_failed (loc, NULL_RTX,
14765 "Want address and only have value");
14766 return 0;
14769 gcc_assert (!ret || !list_ret);
14771 /* If we've got an address and don't want one, dereference. */
14772 if (!want_address && have_address)
14774 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14776 if (size > DWARF2_ADDR_SIZE || size == -1)
14778 expansion_failed (loc, NULL_RTX,
14779 "DWARF address size mismatch");
14780 return 0;
14782 else if (size == DWARF2_ADDR_SIZE)
14783 op = DW_OP_deref;
14784 else
14785 op = DW_OP_deref_size;
14787 if (ret)
14788 add_loc_descr (&ret, new_loc_descr (op, size, 0));
14789 else
14790 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
14792 if (ret)
14793 list_ret = new_loc_list (ret, NULL, NULL, NULL);
14795 return list_ret;
14798 /* Same as above but return only single location expression. */
14799 static dw_loc_descr_ref
14800 loc_descriptor_from_tree (tree loc, int want_address)
14802 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
14803 if (!ret)
14804 return NULL;
14805 if (ret->dw_loc_next)
14807 expansion_failed (loc, NULL_RTX,
14808 "Location list where only loc descriptor needed");
14809 return NULL;
14811 return ret->expr;
14814 /* Given a value, round it up to the lowest multiple of `boundary'
14815 which is not less than the value itself. */
14817 static inline HOST_WIDE_INT
14818 ceiling (HOST_WIDE_INT value, unsigned int boundary)
14820 return (((value + boundary - 1) / boundary) * boundary);
14823 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
14824 pointer to the declared type for the relevant field variable, or return
14825 `integer_type_node' if the given node turns out to be an
14826 ERROR_MARK node. */
14828 static inline tree
14829 field_type (const_tree decl)
14831 tree type;
14833 if (TREE_CODE (decl) == ERROR_MARK)
14834 return integer_type_node;
14836 type = DECL_BIT_FIELD_TYPE (decl);
14837 if (type == NULL_TREE)
14838 type = TREE_TYPE (decl);
14840 return type;
14843 /* Given a pointer to a tree node, return the alignment in bits for
14844 it, or else return BITS_PER_WORD if the node actually turns out to
14845 be an ERROR_MARK node. */
14847 static inline unsigned
14848 simple_type_align_in_bits (const_tree type)
14850 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
14853 static inline unsigned
14854 simple_decl_align_in_bits (const_tree decl)
14856 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
14859 /* Return the result of rounding T up to ALIGN. */
14861 static inline offset_int
14862 round_up_to_align (const offset_int &t, unsigned int align)
14864 return wi::udiv_trunc (t + align - 1, align) * align;
14867 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
14868 lowest addressed byte of the "containing object" for the given FIELD_DECL,
14869 or return 0 if we are unable to determine what that offset is, either
14870 because the argument turns out to be a pointer to an ERROR_MARK node, or
14871 because the offset is actually variable. (We can't handle the latter case
14872 just yet). */
14874 static HOST_WIDE_INT
14875 field_byte_offset (const_tree decl)
14877 offset_int object_offset_in_bits;
14878 offset_int object_offset_in_bytes;
14879 offset_int bitpos_int;
14881 if (TREE_CODE (decl) == ERROR_MARK)
14882 return 0;
14884 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
14886 /* We cannot yet cope with fields whose positions are variable, so
14887 for now, when we see such things, we simply return 0. Someday, we may
14888 be able to handle such cases, but it will be damn difficult. */
14889 if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
14890 return 0;
14892 bitpos_int = wi::to_offset (bit_position (decl));
14894 #ifdef PCC_BITFIELD_TYPE_MATTERS
14895 if (PCC_BITFIELD_TYPE_MATTERS)
14897 tree type;
14898 tree field_size_tree;
14899 offset_int deepest_bitpos;
14900 offset_int field_size_in_bits;
14901 unsigned int type_align_in_bits;
14902 unsigned int decl_align_in_bits;
14903 offset_int type_size_in_bits;
14905 type = field_type (decl);
14906 type_size_in_bits = offset_int_type_size_in_bits (type);
14907 type_align_in_bits = simple_type_align_in_bits (type);
14909 field_size_tree = DECL_SIZE (decl);
14911 /* The size could be unspecified if there was an error, or for
14912 a flexible array member. */
14913 if (!field_size_tree)
14914 field_size_tree = bitsize_zero_node;
14916 /* If the size of the field is not constant, use the type size. */
14917 if (TREE_CODE (field_size_tree) == INTEGER_CST)
14918 field_size_in_bits = wi::to_offset (field_size_tree);
14919 else
14920 field_size_in_bits = type_size_in_bits;
14922 decl_align_in_bits = simple_decl_align_in_bits (decl);
14924 /* The GCC front-end doesn't make any attempt to keep track of the
14925 starting bit offset (relative to the start of the containing
14926 structure type) of the hypothetical "containing object" for a
14927 bit-field. Thus, when computing the byte offset value for the
14928 start of the "containing object" of a bit-field, we must deduce
14929 this information on our own. This can be rather tricky to do in
14930 some cases. For example, handling the following structure type
14931 definition when compiling for an i386/i486 target (which only
14932 aligns long long's to 32-bit boundaries) can be very tricky:
14934 struct S { int field1; long long field2:31; };
14936 Fortunately, there is a simple rule-of-thumb which can be used
14937 in such cases. When compiling for an i386/i486, GCC will
14938 allocate 8 bytes for the structure shown above. It decides to
14939 do this based upon one simple rule for bit-field allocation.
14940 GCC allocates each "containing object" for each bit-field at
14941 the first (i.e. lowest addressed) legitimate alignment boundary
14942 (based upon the required minimum alignment for the declared
14943 type of the field) which it can possibly use, subject to the
14944 condition that there is still enough available space remaining
14945 in the containing object (when allocated at the selected point)
14946 to fully accommodate all of the bits of the bit-field itself.
14948 This simple rule makes it obvious why GCC allocates 8 bytes for
14949 each object of the structure type shown above. When looking
14950 for a place to allocate the "containing object" for `field2',
14951 the compiler simply tries to allocate a 64-bit "containing
14952 object" at each successive 32-bit boundary (starting at zero)
14953 until it finds a place to allocate that 64- bit field such that
14954 at least 31 contiguous (and previously unallocated) bits remain
14955 within that selected 64 bit field. (As it turns out, for the
14956 example above, the compiler finds it is OK to allocate the
14957 "containing object" 64-bit field at bit-offset zero within the
14958 structure type.)
14960 Here we attempt to work backwards from the limited set of facts
14961 we're given, and we try to deduce from those facts, where GCC
14962 must have believed that the containing object started (within
14963 the structure type). The value we deduce is then used (by the
14964 callers of this routine) to generate DW_AT_location and
14965 DW_AT_bit_offset attributes for fields (both bit-fields and, in
14966 the case of DW_AT_location, regular fields as well). */
14968 /* Figure out the bit-distance from the start of the structure to
14969 the "deepest" bit of the bit-field. */
14970 deepest_bitpos = bitpos_int + field_size_in_bits;
14972 /* This is the tricky part. Use some fancy footwork to deduce
14973 where the lowest addressed bit of the containing object must
14974 be. */
14975 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
14977 /* Round up to type_align by default. This works best for
14978 bitfields. */
14979 object_offset_in_bits
14980 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
14982 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
14984 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
14986 /* Round up to decl_align instead. */
14987 object_offset_in_bits
14988 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
14991 else
14992 #endif /* PCC_BITFIELD_TYPE_MATTERS */
14993 object_offset_in_bits = bitpos_int;
14995 object_offset_in_bytes
14996 = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
14997 return object_offset_in_bytes.to_shwi ();
15000 /* The following routines define various Dwarf attributes and any data
15001 associated with them. */
15003 /* Add a location description attribute value to a DIE.
15005 This emits location attributes suitable for whole variables and
15006 whole parameters. Note that the location attributes for struct fields are
15007 generated by the routine `data_member_location_attribute' below. */
15009 static inline void
15010 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
15011 dw_loc_list_ref descr)
15013 if (descr == 0)
15014 return;
15015 if (single_element_loc_list_p (descr))
15016 add_AT_loc (die, attr_kind, descr->expr);
15017 else
15018 add_AT_loc_list (die, attr_kind, descr);
15021 /* Add DW_AT_accessibility attribute to DIE if needed. */
15023 static void
15024 add_accessibility_attribute (dw_die_ref die, tree decl)
15026 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
15027 children, otherwise the default is DW_ACCESS_public. In DWARF2
15028 the default has always been DW_ACCESS_public. */
15029 if (TREE_PROTECTED (decl))
15030 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
15031 else if (TREE_PRIVATE (decl))
15033 if (dwarf_version == 2
15034 || die->die_parent == NULL
15035 || die->die_parent->die_tag != DW_TAG_class_type)
15036 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
15038 else if (dwarf_version > 2
15039 && die->die_parent
15040 && die->die_parent->die_tag == DW_TAG_class_type)
15041 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
15044 /* Attach the specialized form of location attribute used for data members of
15045 struct and union types. In the special case of a FIELD_DECL node which
15046 represents a bit-field, the "offset" part of this special location
15047 descriptor must indicate the distance in bytes from the lowest-addressed
15048 byte of the containing struct or union type to the lowest-addressed byte of
15049 the "containing object" for the bit-field. (See the `field_byte_offset'
15050 function above).
15052 For any given bit-field, the "containing object" is a hypothetical object
15053 (of some integral or enum type) within which the given bit-field lives. The
15054 type of this hypothetical "containing object" is always the same as the
15055 declared type of the individual bit-field itself (for GCC anyway... the
15056 DWARF spec doesn't actually mandate this). Note that it is the size (in
15057 bytes) of the hypothetical "containing object" which will be given in the
15058 DW_AT_byte_size attribute for this bit-field. (See the
15059 `byte_size_attribute' function below.) It is also used when calculating the
15060 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
15061 function below.) */
15063 static void
15064 add_data_member_location_attribute (dw_die_ref die, tree decl)
15066 HOST_WIDE_INT offset;
15067 dw_loc_descr_ref loc_descr = 0;
15069 if (TREE_CODE (decl) == TREE_BINFO)
15071 /* We're working on the TAG_inheritance for a base class. */
15072 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
15074 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
15075 aren't at a fixed offset from all (sub)objects of the same
15076 type. We need to extract the appropriate offset from our
15077 vtable. The following dwarf expression means
15079 BaseAddr = ObAddr + *((*ObAddr) - Offset)
15081 This is specific to the V3 ABI, of course. */
15083 dw_loc_descr_ref tmp;
15085 /* Make a copy of the object address. */
15086 tmp = new_loc_descr (DW_OP_dup, 0, 0);
15087 add_loc_descr (&loc_descr, tmp);
15089 /* Extract the vtable address. */
15090 tmp = new_loc_descr (DW_OP_deref, 0, 0);
15091 add_loc_descr (&loc_descr, tmp);
15093 /* Calculate the address of the offset. */
15094 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
15095 gcc_assert (offset < 0);
15097 tmp = int_loc_descriptor (-offset);
15098 add_loc_descr (&loc_descr, tmp);
15099 tmp = new_loc_descr (DW_OP_minus, 0, 0);
15100 add_loc_descr (&loc_descr, tmp);
15102 /* Extract the offset. */
15103 tmp = new_loc_descr (DW_OP_deref, 0, 0);
15104 add_loc_descr (&loc_descr, tmp);
15106 /* Add it to the object address. */
15107 tmp = new_loc_descr (DW_OP_plus, 0, 0);
15108 add_loc_descr (&loc_descr, tmp);
15110 else
15111 offset = tree_to_shwi (BINFO_OFFSET (decl));
15113 else
15114 offset = field_byte_offset (decl);
15116 if (! loc_descr)
15118 if (dwarf_version > 2)
15120 /* Don't need to output a location expression, just the constant. */
15121 if (offset < 0)
15122 add_AT_int (die, DW_AT_data_member_location, offset);
15123 else
15124 add_AT_unsigned (die, DW_AT_data_member_location, offset);
15125 return;
15127 else
15129 enum dwarf_location_atom op;
15131 /* The DWARF2 standard says that we should assume that the structure
15132 address is already on the stack, so we can specify a structure
15133 field address by using DW_OP_plus_uconst. */
15134 op = DW_OP_plus_uconst;
15135 loc_descr = new_loc_descr (op, offset, 0);
15139 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
15142 /* Writes integer values to dw_vec_const array. */
15144 static void
15145 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
15147 while (size != 0)
15149 *dest++ = val & 0xff;
15150 val >>= 8;
15151 --size;
15155 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
15157 static HOST_WIDE_INT
15158 extract_int (const unsigned char *src, unsigned int size)
15160 HOST_WIDE_INT val = 0;
15162 src += size;
15163 while (size != 0)
15165 val <<= 8;
15166 val |= *--src & 0xff;
15167 --size;
15169 return val;
15172 /* Writes wide_int values to dw_vec_const array. */
15174 static void
15175 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
15177 int i;
15179 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
15181 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
15182 return;
15185 /* We'd have to extend this code to support odd sizes. */
15186 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
15188 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
15190 if (WORDS_BIG_ENDIAN)
15191 for (i = n - 1; i >= 0; i--)
15193 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
15194 dest += sizeof (HOST_WIDE_INT);
15196 else
15197 for (i = 0; i < n; i++)
15199 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
15200 dest += sizeof (HOST_WIDE_INT);
15204 /* Writes floating point values to dw_vec_const array. */
15206 static void
15207 insert_float (const_rtx rtl, unsigned char *array)
15209 REAL_VALUE_TYPE rv;
15210 long val[4];
15211 int i;
15213 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
15214 real_to_target (val, &rv, GET_MODE (rtl));
15216 /* real_to_target puts 32-bit pieces in each long. Pack them. */
15217 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
15219 insert_int (val[i], 4, array);
15220 array += 4;
15224 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
15225 does not have a "location" either in memory or in a register. These
15226 things can arise in GNU C when a constant is passed as an actual parameter
15227 to an inlined function. They can also arise in C++ where declared
15228 constants do not necessarily get memory "homes". */
15230 static bool
15231 add_const_value_attribute (dw_die_ref die, rtx rtl)
15233 switch (GET_CODE (rtl))
15235 case CONST_INT:
15237 HOST_WIDE_INT val = INTVAL (rtl);
15239 if (val < 0)
15240 add_AT_int (die, DW_AT_const_value, val);
15241 else
15242 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
15244 return true;
15246 case CONST_WIDE_INT:
15247 add_AT_wide (die, DW_AT_const_value,
15248 std::make_pair (rtl, GET_MODE (rtl)));
15249 return true;
15251 case CONST_DOUBLE:
15252 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
15253 floating-point constant. A CONST_DOUBLE is used whenever the
15254 constant requires more than one word in order to be adequately
15255 represented. */
15257 enum machine_mode mode = GET_MODE (rtl);
15259 if (TARGET_SUPPORTS_WIDE_INT == 0 && !SCALAR_FLOAT_MODE_P (mode))
15260 add_AT_double (die, DW_AT_const_value,
15261 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
15262 else
15264 unsigned int length = GET_MODE_SIZE (mode);
15265 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15267 insert_float (rtl, array);
15268 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
15271 return true;
15273 case CONST_VECTOR:
15275 enum machine_mode mode = GET_MODE (rtl);
15276 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
15277 unsigned int length = CONST_VECTOR_NUNITS (rtl);
15278 unsigned char *array
15279 = ggc_vec_alloc<unsigned char> (length * elt_size);
15280 unsigned int i;
15281 unsigned char *p;
15282 enum machine_mode imode = GET_MODE_INNER (mode);
15284 switch (GET_MODE_CLASS (mode))
15286 case MODE_VECTOR_INT:
15287 for (i = 0, p = array; i < length; i++, p += elt_size)
15289 rtx elt = CONST_VECTOR_ELT (rtl, i);
15290 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
15292 break;
15294 case MODE_VECTOR_FLOAT:
15295 for (i = 0, p = array; i < length; i++, p += elt_size)
15297 rtx elt = CONST_VECTOR_ELT (rtl, i);
15298 insert_float (elt, p);
15300 break;
15302 default:
15303 gcc_unreachable ();
15306 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
15308 return true;
15310 case CONST_STRING:
15311 if (dwarf_version >= 4 || !dwarf_strict)
15313 dw_loc_descr_ref loc_result;
15314 resolve_one_addr (&rtl);
15315 rtl_addr:
15316 loc_result = new_addr_loc_descr (rtl, dtprel_false);
15317 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
15318 add_AT_loc (die, DW_AT_location, loc_result);
15319 vec_safe_push (used_rtx_array, rtl);
15320 return true;
15322 return false;
15324 case CONST:
15325 if (CONSTANT_P (XEXP (rtl, 0)))
15326 return add_const_value_attribute (die, XEXP (rtl, 0));
15327 /* FALLTHROUGH */
15328 case SYMBOL_REF:
15329 if (!const_ok_for_output (rtl))
15330 return false;
15331 case LABEL_REF:
15332 if (dwarf_version >= 4 || !dwarf_strict)
15333 goto rtl_addr;
15334 return false;
15336 case PLUS:
15337 /* In cases where an inlined instance of an inline function is passed
15338 the address of an `auto' variable (which is local to the caller) we
15339 can get a situation where the DECL_RTL of the artificial local
15340 variable (for the inlining) which acts as a stand-in for the
15341 corresponding formal parameter (of the inline function) will look
15342 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
15343 exactly a compile-time constant expression, but it isn't the address
15344 of the (artificial) local variable either. Rather, it represents the
15345 *value* which the artificial local variable always has during its
15346 lifetime. We currently have no way to represent such quasi-constant
15347 values in Dwarf, so for now we just punt and generate nothing. */
15348 return false;
15350 case HIGH:
15351 case CONST_FIXED:
15352 return false;
15354 case MEM:
15355 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
15356 && MEM_READONLY_P (rtl)
15357 && GET_MODE (rtl) == BLKmode)
15359 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
15360 return true;
15362 return false;
15364 default:
15365 /* No other kinds of rtx should be possible here. */
15366 gcc_unreachable ();
15368 return false;
15371 /* Determine whether the evaluation of EXPR references any variables
15372 or functions which aren't otherwise used (and therefore may not be
15373 output). */
15374 static tree
15375 reference_to_unused (tree * tp, int * walk_subtrees,
15376 void * data ATTRIBUTE_UNUSED)
15378 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
15379 *walk_subtrees = 0;
15381 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
15382 && ! TREE_ASM_WRITTEN (*tp))
15383 return *tp;
15384 /* ??? The C++ FE emits debug information for using decls, so
15385 putting gcc_unreachable here falls over. See PR31899. For now
15386 be conservative. */
15387 else if (!symtab->global_info_ready
15388 && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
15389 return *tp;
15390 else if (TREE_CODE (*tp) == VAR_DECL)
15392 varpool_node *node = varpool_node::get (*tp);
15393 if (!node || !node->definition)
15394 return *tp;
15396 else if (TREE_CODE (*tp) == FUNCTION_DECL
15397 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
15399 /* The call graph machinery must have finished analyzing,
15400 optimizing and gimplifying the CU by now.
15401 So if *TP has no call graph node associated
15402 to it, it means *TP will not be emitted. */
15403 if (!cgraph_node::get (*tp))
15404 return *tp;
15406 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
15407 return *tp;
15409 return NULL_TREE;
15412 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
15413 for use in a later add_const_value_attribute call. */
15415 static rtx
15416 rtl_for_decl_init (tree init, tree type)
15418 rtx rtl = NULL_RTX;
15420 STRIP_NOPS (init);
15422 /* If a variable is initialized with a string constant without embedded
15423 zeros, build CONST_STRING. */
15424 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
15426 tree enttype = TREE_TYPE (type);
15427 tree domain = TYPE_DOMAIN (type);
15428 enum machine_mode mode = TYPE_MODE (enttype);
15430 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
15431 && domain
15432 && integer_zerop (TYPE_MIN_VALUE (domain))
15433 && compare_tree_int (TYPE_MAX_VALUE (domain),
15434 TREE_STRING_LENGTH (init) - 1) == 0
15435 && ((size_t) TREE_STRING_LENGTH (init)
15436 == strlen (TREE_STRING_POINTER (init)) + 1))
15438 rtl = gen_rtx_CONST_STRING (VOIDmode,
15439 ggc_strdup (TREE_STRING_POINTER (init)));
15440 rtl = gen_rtx_MEM (BLKmode, rtl);
15441 MEM_READONLY_P (rtl) = 1;
15444 /* Other aggregates, and complex values, could be represented using
15445 CONCAT: FIXME! */
15446 else if (AGGREGATE_TYPE_P (type)
15447 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
15448 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
15449 || TREE_CODE (type) == COMPLEX_TYPE)
15451 /* Vectors only work if their mode is supported by the target.
15452 FIXME: generic vectors ought to work too. */
15453 else if (TREE_CODE (type) == VECTOR_TYPE
15454 && !VECTOR_MODE_P (TYPE_MODE (type)))
15456 /* If the initializer is something that we know will expand into an
15457 immediate RTL constant, expand it now. We must be careful not to
15458 reference variables which won't be output. */
15459 else if (initializer_constant_valid_p (init, type)
15460 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
15462 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
15463 possible. */
15464 if (TREE_CODE (type) == VECTOR_TYPE)
15465 switch (TREE_CODE (init))
15467 case VECTOR_CST:
15468 break;
15469 case CONSTRUCTOR:
15470 if (TREE_CONSTANT (init))
15472 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
15473 bool constant_p = true;
15474 tree value;
15475 unsigned HOST_WIDE_INT ix;
15477 /* Even when ctor is constant, it might contain non-*_CST
15478 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
15479 belong into VECTOR_CST nodes. */
15480 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
15481 if (!CONSTANT_CLASS_P (value))
15483 constant_p = false;
15484 break;
15487 if (constant_p)
15489 init = build_vector_from_ctor (type, elts);
15490 break;
15493 /* FALLTHRU */
15495 default:
15496 return NULL;
15499 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
15501 /* If expand_expr returns a MEM, it wasn't immediate. */
15502 gcc_assert (!rtl || !MEM_P (rtl));
15505 return rtl;
15508 /* Generate RTL for the variable DECL to represent its location. */
15510 static rtx
15511 rtl_for_decl_location (tree decl)
15513 rtx rtl;
15515 /* Here we have to decide where we are going to say the parameter "lives"
15516 (as far as the debugger is concerned). We only have a couple of
15517 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
15519 DECL_RTL normally indicates where the parameter lives during most of the
15520 activation of the function. If optimization is enabled however, this
15521 could be either NULL or else a pseudo-reg. Both of those cases indicate
15522 that the parameter doesn't really live anywhere (as far as the code
15523 generation parts of GCC are concerned) during most of the function's
15524 activation. That will happen (for example) if the parameter is never
15525 referenced within the function.
15527 We could just generate a location descriptor here for all non-NULL
15528 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
15529 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
15530 where DECL_RTL is NULL or is a pseudo-reg.
15532 Note however that we can only get away with using DECL_INCOMING_RTL as
15533 a backup substitute for DECL_RTL in certain limited cases. In cases
15534 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
15535 we can be sure that the parameter was passed using the same type as it is
15536 declared to have within the function, and that its DECL_INCOMING_RTL
15537 points us to a place where a value of that type is passed.
15539 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
15540 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
15541 because in these cases DECL_INCOMING_RTL points us to a value of some
15542 type which is *different* from the type of the parameter itself. Thus,
15543 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
15544 such cases, the debugger would end up (for example) trying to fetch a
15545 `float' from a place which actually contains the first part of a
15546 `double'. That would lead to really incorrect and confusing
15547 output at debug-time.
15549 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
15550 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
15551 are a couple of exceptions however. On little-endian machines we can
15552 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
15553 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
15554 an integral type that is smaller than TREE_TYPE (decl). These cases arise
15555 when (on a little-endian machine) a non-prototyped function has a
15556 parameter declared to be of type `short' or `char'. In such cases,
15557 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
15558 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
15559 passed `int' value. If the debugger then uses that address to fetch
15560 a `short' or a `char' (on a little-endian machine) the result will be
15561 the correct data, so we allow for such exceptional cases below.
15563 Note that our goal here is to describe the place where the given formal
15564 parameter lives during most of the function's activation (i.e. between the
15565 end of the prologue and the start of the epilogue). We'll do that as best
15566 as we can. Note however that if the given formal parameter is modified
15567 sometime during the execution of the function, then a stack backtrace (at
15568 debug-time) will show the function as having been called with the *new*
15569 value rather than the value which was originally passed in. This happens
15570 rarely enough that it is not a major problem, but it *is* a problem, and
15571 I'd like to fix it.
15573 A future version of dwarf2out.c may generate two additional attributes for
15574 any given DW_TAG_formal_parameter DIE which will describe the "passed
15575 type" and the "passed location" for the given formal parameter in addition
15576 to the attributes we now generate to indicate the "declared type" and the
15577 "active location" for each parameter. This additional set of attributes
15578 could be used by debuggers for stack backtraces. Separately, note that
15579 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
15580 This happens (for example) for inlined-instances of inline function formal
15581 parameters which are never referenced. This really shouldn't be
15582 happening. All PARM_DECL nodes should get valid non-NULL
15583 DECL_INCOMING_RTL values. FIXME. */
15585 /* Use DECL_RTL as the "location" unless we find something better. */
15586 rtl = DECL_RTL_IF_SET (decl);
15588 /* When generating abstract instances, ignore everything except
15589 constants, symbols living in memory, and symbols living in
15590 fixed registers. */
15591 if (! reload_completed)
15593 if (rtl
15594 && (CONSTANT_P (rtl)
15595 || (MEM_P (rtl)
15596 && CONSTANT_P (XEXP (rtl, 0)))
15597 || (REG_P (rtl)
15598 && TREE_CODE (decl) == VAR_DECL
15599 && TREE_STATIC (decl))))
15601 rtl = targetm.delegitimize_address (rtl);
15602 return rtl;
15604 rtl = NULL_RTX;
15606 else if (TREE_CODE (decl) == PARM_DECL)
15608 if (rtl == NULL_RTX
15609 || is_pseudo_reg (rtl)
15610 || (MEM_P (rtl)
15611 && is_pseudo_reg (XEXP (rtl, 0))
15612 && DECL_INCOMING_RTL (decl)
15613 && MEM_P (DECL_INCOMING_RTL (decl))
15614 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
15616 tree declared_type = TREE_TYPE (decl);
15617 tree passed_type = DECL_ARG_TYPE (decl);
15618 enum machine_mode dmode = TYPE_MODE (declared_type);
15619 enum machine_mode pmode = TYPE_MODE (passed_type);
15621 /* This decl represents a formal parameter which was optimized out.
15622 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
15623 all cases where (rtl == NULL_RTX) just below. */
15624 if (dmode == pmode)
15625 rtl = DECL_INCOMING_RTL (decl);
15626 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
15627 && SCALAR_INT_MODE_P (dmode)
15628 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
15629 && DECL_INCOMING_RTL (decl))
15631 rtx inc = DECL_INCOMING_RTL (decl);
15632 if (REG_P (inc))
15633 rtl = inc;
15634 else if (MEM_P (inc))
15636 if (BYTES_BIG_ENDIAN)
15637 rtl = adjust_address_nv (inc, dmode,
15638 GET_MODE_SIZE (pmode)
15639 - GET_MODE_SIZE (dmode));
15640 else
15641 rtl = inc;
15646 /* If the parm was passed in registers, but lives on the stack, then
15647 make a big endian correction if the mode of the type of the
15648 parameter is not the same as the mode of the rtl. */
15649 /* ??? This is the same series of checks that are made in dbxout.c before
15650 we reach the big endian correction code there. It isn't clear if all
15651 of these checks are necessary here, but keeping them all is the safe
15652 thing to do. */
15653 else if (MEM_P (rtl)
15654 && XEXP (rtl, 0) != const0_rtx
15655 && ! CONSTANT_P (XEXP (rtl, 0))
15656 /* Not passed in memory. */
15657 && !MEM_P (DECL_INCOMING_RTL (decl))
15658 /* Not passed by invisible reference. */
15659 && (!REG_P (XEXP (rtl, 0))
15660 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
15661 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
15662 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
15663 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
15664 #endif
15666 /* Big endian correction check. */
15667 && BYTES_BIG_ENDIAN
15668 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
15669 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
15670 < UNITS_PER_WORD))
15672 enum machine_mode addr_mode = get_address_mode (rtl);
15673 int offset = (UNITS_PER_WORD
15674 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
15676 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15677 plus_constant (addr_mode, XEXP (rtl, 0), offset));
15680 else if (TREE_CODE (decl) == VAR_DECL
15681 && rtl
15682 && MEM_P (rtl)
15683 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
15684 && BYTES_BIG_ENDIAN)
15686 enum machine_mode addr_mode = get_address_mode (rtl);
15687 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
15688 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
15690 /* If a variable is declared "register" yet is smaller than
15691 a register, then if we store the variable to memory, it
15692 looks like we're storing a register-sized value, when in
15693 fact we are not. We need to adjust the offset of the
15694 storage location to reflect the actual value's bytes,
15695 else gdb will not be able to display it. */
15696 if (rsize > dsize)
15697 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15698 plus_constant (addr_mode, XEXP (rtl, 0),
15699 rsize - dsize));
15702 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
15703 and will have been substituted directly into all expressions that use it.
15704 C does not have such a concept, but C++ and other languages do. */
15705 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
15706 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
15708 if (rtl)
15709 rtl = targetm.delegitimize_address (rtl);
15711 /* If we don't look past the constant pool, we risk emitting a
15712 reference to a constant pool entry that isn't referenced from
15713 code, and thus is not emitted. */
15714 if (rtl)
15715 rtl = avoid_constant_pool_reference (rtl);
15717 /* Try harder to get a rtl. If this symbol ends up not being emitted
15718 in the current CU, resolve_addr will remove the expression referencing
15719 it. */
15720 if (rtl == NULL_RTX
15721 && TREE_CODE (decl) == VAR_DECL
15722 && !DECL_EXTERNAL (decl)
15723 && TREE_STATIC (decl)
15724 && DECL_NAME (decl)
15725 && !DECL_HARD_REGISTER (decl)
15726 && DECL_MODE (decl) != VOIDmode)
15728 rtl = make_decl_rtl_for_debug (decl);
15729 if (!MEM_P (rtl)
15730 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
15731 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
15732 rtl = NULL_RTX;
15735 return rtl;
15738 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
15739 returned. If so, the decl for the COMMON block is returned, and the
15740 value is the offset into the common block for the symbol. */
15742 static tree
15743 fortran_common (tree decl, HOST_WIDE_INT *value)
15745 tree val_expr, cvar;
15746 enum machine_mode mode;
15747 HOST_WIDE_INT bitsize, bitpos;
15748 tree offset;
15749 int unsignedp, volatilep = 0;
15751 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
15752 it does not have a value (the offset into the common area), or if it
15753 is thread local (as opposed to global) then it isn't common, and shouldn't
15754 be handled as such. */
15755 if (TREE_CODE (decl) != VAR_DECL
15756 || !TREE_STATIC (decl)
15757 || !DECL_HAS_VALUE_EXPR_P (decl)
15758 || !is_fortran ())
15759 return NULL_TREE;
15761 val_expr = DECL_VALUE_EXPR (decl);
15762 if (TREE_CODE (val_expr) != COMPONENT_REF)
15763 return NULL_TREE;
15765 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
15766 &mode, &unsignedp, &volatilep, true);
15768 if (cvar == NULL_TREE
15769 || TREE_CODE (cvar) != VAR_DECL
15770 || DECL_ARTIFICIAL (cvar)
15771 || !TREE_PUBLIC (cvar))
15772 return NULL_TREE;
15774 *value = 0;
15775 if (offset != NULL)
15777 if (!tree_fits_shwi_p (offset))
15778 return NULL_TREE;
15779 *value = tree_to_shwi (offset);
15781 if (bitpos != 0)
15782 *value += bitpos / BITS_PER_UNIT;
15784 return cvar;
15787 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
15788 data attribute for a variable or a parameter. We generate the
15789 DW_AT_const_value attribute only in those cases where the given variable
15790 or parameter does not have a true "location" either in memory or in a
15791 register. This can happen (for example) when a constant is passed as an
15792 actual argument in a call to an inline function. (It's possible that
15793 these things can crop up in other ways also.) Note that one type of
15794 constant value which can be passed into an inlined function is a constant
15795 pointer. This can happen for example if an actual argument in an inlined
15796 function call evaluates to a compile-time constant address.
15798 CACHE_P is true if it is worth caching the location list for DECL,
15799 so that future calls can reuse it rather than regenerate it from scratch.
15800 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
15801 since we will need to refer to them each time the function is inlined. */
15803 static bool
15804 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
15805 enum dwarf_attribute attr)
15807 rtx rtl;
15808 dw_loc_list_ref list;
15809 var_loc_list *loc_list;
15810 cached_dw_loc_list *cache;
15811 void **slot;
15813 if (TREE_CODE (decl) == ERROR_MARK)
15814 return false;
15816 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
15817 || TREE_CODE (decl) == RESULT_DECL);
15819 /* Try to get some constant RTL for this decl, and use that as the value of
15820 the location. */
15822 rtl = rtl_for_decl_location (decl);
15823 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15824 && add_const_value_attribute (die, rtl))
15825 return true;
15827 /* See if we have single element location list that is equivalent to
15828 a constant value. That way we are better to use add_const_value_attribute
15829 rather than expanding constant value equivalent. */
15830 loc_list = lookup_decl_loc (decl);
15831 if (loc_list
15832 && loc_list->first
15833 && loc_list->first->next == NULL
15834 && NOTE_P (loc_list->first->loc)
15835 && NOTE_VAR_LOCATION (loc_list->first->loc)
15836 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
15838 struct var_loc_node *node;
15840 node = loc_list->first;
15841 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
15842 if (GET_CODE (rtl) == EXPR_LIST)
15843 rtl = XEXP (rtl, 0);
15844 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15845 && add_const_value_attribute (die, rtl))
15846 return true;
15848 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
15849 list several times. See if we've already cached the contents. */
15850 list = NULL;
15851 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
15852 cache_p = false;
15853 if (cache_p)
15855 cache = (cached_dw_loc_list *)
15856 htab_find_with_hash (cached_dw_loc_list_table, decl, DECL_UID (decl));
15857 if (cache)
15858 list = cache->loc_list;
15860 if (list == NULL)
15862 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
15863 /* It is usually worth caching this result if the decl is from
15864 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
15865 if (cache_p && list && list->dw_loc_next)
15867 slot = htab_find_slot_with_hash (cached_dw_loc_list_table, decl,
15868 DECL_UID (decl), INSERT);
15869 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
15870 cache->decl_id = DECL_UID (decl);
15871 cache->loc_list = list;
15872 *slot = cache;
15875 if (list)
15877 add_AT_location_description (die, attr, list);
15878 return true;
15880 /* None of that worked, so it must not really have a location;
15881 try adding a constant value attribute from the DECL_INITIAL. */
15882 return tree_add_const_value_attribute_for_decl (die, decl);
15885 /* Add VARIABLE and DIE into deferred locations list. */
15887 static void
15888 defer_location (tree variable, dw_die_ref die)
15890 deferred_locations entry;
15891 entry.variable = variable;
15892 entry.die = die;
15893 vec_safe_push (deferred_locations_list, entry);
15896 /* Helper function for tree_add_const_value_attribute. Natively encode
15897 initializer INIT into an array. Return true if successful. */
15899 static bool
15900 native_encode_initializer (tree init, unsigned char *array, int size)
15902 tree type;
15904 if (init == NULL_TREE)
15905 return false;
15907 STRIP_NOPS (init);
15908 switch (TREE_CODE (init))
15910 case STRING_CST:
15911 type = TREE_TYPE (init);
15912 if (TREE_CODE (type) == ARRAY_TYPE)
15914 tree enttype = TREE_TYPE (type);
15915 enum machine_mode mode = TYPE_MODE (enttype);
15917 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
15918 return false;
15919 if (int_size_in_bytes (type) != size)
15920 return false;
15921 if (size > TREE_STRING_LENGTH (init))
15923 memcpy (array, TREE_STRING_POINTER (init),
15924 TREE_STRING_LENGTH (init));
15925 memset (array + TREE_STRING_LENGTH (init),
15926 '\0', size - TREE_STRING_LENGTH (init));
15928 else
15929 memcpy (array, TREE_STRING_POINTER (init), size);
15930 return true;
15932 return false;
15933 case CONSTRUCTOR:
15934 type = TREE_TYPE (init);
15935 if (int_size_in_bytes (type) != size)
15936 return false;
15937 if (TREE_CODE (type) == ARRAY_TYPE)
15939 HOST_WIDE_INT min_index;
15940 unsigned HOST_WIDE_INT cnt;
15941 int curpos = 0, fieldsize;
15942 constructor_elt *ce;
15944 if (TYPE_DOMAIN (type) == NULL_TREE
15945 || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
15946 return false;
15948 fieldsize = int_size_in_bytes (TREE_TYPE (type));
15949 if (fieldsize <= 0)
15950 return false;
15952 min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
15953 memset (array, '\0', size);
15954 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
15956 tree val = ce->value;
15957 tree index = ce->index;
15958 int pos = curpos;
15959 if (index && TREE_CODE (index) == RANGE_EXPR)
15960 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
15961 * fieldsize;
15962 else if (index)
15963 pos = (tree_to_shwi (index) - min_index) * fieldsize;
15965 if (val)
15967 STRIP_NOPS (val);
15968 if (!native_encode_initializer (val, array + pos, fieldsize))
15969 return false;
15971 curpos = pos + fieldsize;
15972 if (index && TREE_CODE (index) == RANGE_EXPR)
15974 int count = tree_to_shwi (TREE_OPERAND (index, 1))
15975 - tree_to_shwi (TREE_OPERAND (index, 0));
15976 while (count-- > 0)
15978 if (val)
15979 memcpy (array + curpos, array + pos, fieldsize);
15980 curpos += fieldsize;
15983 gcc_assert (curpos <= size);
15985 return true;
15987 else if (TREE_CODE (type) == RECORD_TYPE
15988 || TREE_CODE (type) == UNION_TYPE)
15990 tree field = NULL_TREE;
15991 unsigned HOST_WIDE_INT cnt;
15992 constructor_elt *ce;
15994 if (int_size_in_bytes (type) != size)
15995 return false;
15997 if (TREE_CODE (type) == RECORD_TYPE)
15998 field = TYPE_FIELDS (type);
16000 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
16002 tree val = ce->value;
16003 int pos, fieldsize;
16005 if (ce->index != 0)
16006 field = ce->index;
16008 if (val)
16009 STRIP_NOPS (val);
16011 if (field == NULL_TREE || DECL_BIT_FIELD (field))
16012 return false;
16014 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
16015 && TYPE_DOMAIN (TREE_TYPE (field))
16016 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
16017 return false;
16018 else if (DECL_SIZE_UNIT (field) == NULL_TREE
16019 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
16020 return false;
16021 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
16022 pos = int_byte_position (field);
16023 gcc_assert (pos + fieldsize <= size);
16024 if (val
16025 && !native_encode_initializer (val, array + pos, fieldsize))
16026 return false;
16028 return true;
16030 return false;
16031 case VIEW_CONVERT_EXPR:
16032 case NON_LVALUE_EXPR:
16033 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
16034 default:
16035 return native_encode_expr (init, array, size) == size;
16039 /* Attach a DW_AT_const_value attribute to DIE. The value of the
16040 attribute is the const value T. */
16042 static bool
16043 tree_add_const_value_attribute (dw_die_ref die, tree t)
16045 tree init;
16046 tree type = TREE_TYPE (t);
16047 rtx rtl;
16049 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
16050 return false;
16052 init = t;
16053 gcc_assert (!DECL_P (init));
16055 rtl = rtl_for_decl_init (init, type);
16056 if (rtl)
16057 return add_const_value_attribute (die, rtl);
16058 /* If the host and target are sane, try harder. */
16059 else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
16060 && initializer_constant_valid_p (init, type))
16062 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
16063 if (size > 0 && (int) size == size)
16065 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
16067 if (native_encode_initializer (init, array, size))
16069 add_AT_vec (die, DW_AT_const_value, size, 1, array);
16070 return true;
16072 ggc_free (array);
16075 return false;
16078 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
16079 attribute is the const value of T, where T is an integral constant
16080 variable with static storage duration
16081 (so it can't be a PARM_DECL or a RESULT_DECL). */
16083 static bool
16084 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
16087 if (!decl
16088 || (TREE_CODE (decl) != VAR_DECL
16089 && TREE_CODE (decl) != CONST_DECL)
16090 || (TREE_CODE (decl) == VAR_DECL
16091 && !TREE_STATIC (decl)))
16092 return false;
16094 if (TREE_READONLY (decl)
16095 && ! TREE_THIS_VOLATILE (decl)
16096 && DECL_INITIAL (decl))
16097 /* OK */;
16098 else
16099 return false;
16101 /* Don't add DW_AT_const_value if abstract origin already has one. */
16102 if (get_AT (var_die, DW_AT_const_value))
16103 return false;
16105 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
16108 /* Convert the CFI instructions for the current function into a
16109 location list. This is used for DW_AT_frame_base when we targeting
16110 a dwarf2 consumer that does not support the dwarf3
16111 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
16112 expressions. */
16114 static dw_loc_list_ref
16115 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
16117 int ix;
16118 dw_fde_ref fde;
16119 dw_loc_list_ref list, *list_tail;
16120 dw_cfi_ref cfi;
16121 dw_cfa_location last_cfa, next_cfa;
16122 const char *start_label, *last_label, *section;
16123 dw_cfa_location remember;
16125 fde = cfun->fde;
16126 gcc_assert (fde != NULL);
16128 section = secname_for_decl (current_function_decl);
16129 list_tail = &list;
16130 list = NULL;
16132 memset (&next_cfa, 0, sizeof (next_cfa));
16133 next_cfa.reg = INVALID_REGNUM;
16134 remember = next_cfa;
16136 start_label = fde->dw_fde_begin;
16138 /* ??? Bald assumption that the CIE opcode list does not contain
16139 advance opcodes. */
16140 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
16141 lookup_cfa_1 (cfi, &next_cfa, &remember);
16143 last_cfa = next_cfa;
16144 last_label = start_label;
16146 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
16148 /* If the first partition contained no CFI adjustments, the
16149 CIE opcodes apply to the whole first partition. */
16150 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16151 fde->dw_fde_begin, fde->dw_fde_end, section);
16152 list_tail =&(*list_tail)->dw_loc_next;
16153 start_label = last_label = fde->dw_fde_second_begin;
16156 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
16158 switch (cfi->dw_cfi_opc)
16160 case DW_CFA_set_loc:
16161 case DW_CFA_advance_loc1:
16162 case DW_CFA_advance_loc2:
16163 case DW_CFA_advance_loc4:
16164 if (!cfa_equal_p (&last_cfa, &next_cfa))
16166 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16167 start_label, last_label, section);
16169 list_tail = &(*list_tail)->dw_loc_next;
16170 last_cfa = next_cfa;
16171 start_label = last_label;
16173 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
16174 break;
16176 case DW_CFA_advance_loc:
16177 /* The encoding is complex enough that we should never emit this. */
16178 gcc_unreachable ();
16180 default:
16181 lookup_cfa_1 (cfi, &next_cfa, &remember);
16182 break;
16184 if (ix + 1 == fde->dw_fde_switch_cfi_index)
16186 if (!cfa_equal_p (&last_cfa, &next_cfa))
16188 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16189 start_label, last_label, section);
16191 list_tail = &(*list_tail)->dw_loc_next;
16192 last_cfa = next_cfa;
16193 start_label = last_label;
16195 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16196 start_label, fde->dw_fde_end, section);
16197 list_tail = &(*list_tail)->dw_loc_next;
16198 start_label = last_label = fde->dw_fde_second_begin;
16202 if (!cfa_equal_p (&last_cfa, &next_cfa))
16204 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16205 start_label, last_label, section);
16206 list_tail = &(*list_tail)->dw_loc_next;
16207 start_label = last_label;
16210 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
16211 start_label,
16212 fde->dw_fde_second_begin
16213 ? fde->dw_fde_second_end : fde->dw_fde_end,
16214 section);
16216 if (list && list->dw_loc_next)
16217 gen_llsym (list);
16219 return list;
16222 /* Compute a displacement from the "steady-state frame pointer" to the
16223 frame base (often the same as the CFA), and store it in
16224 frame_pointer_fb_offset. OFFSET is added to the displacement
16225 before the latter is negated. */
16227 static void
16228 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
16230 rtx reg, elim;
16232 #ifdef FRAME_POINTER_CFA_OFFSET
16233 reg = frame_pointer_rtx;
16234 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
16235 #else
16236 reg = arg_pointer_rtx;
16237 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
16238 #endif
16240 elim = (ira_use_lra_p
16241 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
16242 : eliminate_regs (reg, VOIDmode, NULL_RTX));
16243 if (GET_CODE (elim) == PLUS)
16245 offset += INTVAL (XEXP (elim, 1));
16246 elim = XEXP (elim, 0);
16249 frame_pointer_fb_offset = -offset;
16251 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
16252 in which to eliminate. This is because it's stack pointer isn't
16253 directly accessible as a register within the ISA. To work around
16254 this, assume that while we cannot provide a proper value for
16255 frame_pointer_fb_offset, we won't need one either. */
16256 frame_pointer_fb_offset_valid
16257 = ((SUPPORTS_STACK_ALIGNMENT
16258 && (elim == hard_frame_pointer_rtx
16259 || elim == stack_pointer_rtx))
16260 || elim == (frame_pointer_needed
16261 ? hard_frame_pointer_rtx
16262 : stack_pointer_rtx));
16265 /* Generate a DW_AT_name attribute given some string value to be included as
16266 the value of the attribute. */
16268 static void
16269 add_name_attribute (dw_die_ref die, const char *name_string)
16271 if (name_string != NULL && *name_string != 0)
16273 if (demangle_name_func)
16274 name_string = (*demangle_name_func) (name_string);
16276 add_AT_string (die, DW_AT_name, name_string);
16280 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
16281 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
16282 of TYPE accordingly.
16284 ??? This is a temporary measure until after we're able to generate
16285 regular DWARF for the complex Ada type system. */
16287 static void
16288 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
16289 dw_die_ref context_die)
16291 tree dtype;
16292 dw_die_ref dtype_die;
16294 if (!lang_hooks.types.descriptive_type)
16295 return;
16297 dtype = lang_hooks.types.descriptive_type (type);
16298 if (!dtype)
16299 return;
16301 dtype_die = lookup_type_die (dtype);
16302 if (!dtype_die)
16304 gen_type_die (dtype, context_die);
16305 dtype_die = lookup_type_die (dtype);
16306 gcc_assert (dtype_die);
16309 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
16312 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
16314 static const char *
16315 comp_dir_string (void)
16317 const char *wd;
16318 char *wd1;
16319 static const char *cached_wd = NULL;
16321 if (cached_wd != NULL)
16322 return cached_wd;
16324 wd = get_src_pwd ();
16325 if (wd == NULL)
16326 return NULL;
16328 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
16330 int wdlen;
16332 wdlen = strlen (wd);
16333 wd1 = ggc_vec_alloc<char> (wdlen + 2);
16334 strcpy (wd1, wd);
16335 wd1 [wdlen] = DIR_SEPARATOR;
16336 wd1 [wdlen + 1] = 0;
16337 wd = wd1;
16340 cached_wd = remap_debug_filename (wd);
16341 return cached_wd;
16344 /* Generate a DW_AT_comp_dir attribute for DIE. */
16346 static void
16347 add_comp_dir_attribute (dw_die_ref die)
16349 const char * wd = comp_dir_string ();
16350 if (wd != NULL)
16351 add_AT_string (die, DW_AT_comp_dir, wd);
16354 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
16355 default. */
16357 static int
16358 lower_bound_default (void)
16360 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
16362 case DW_LANG_C:
16363 case DW_LANG_C89:
16364 case DW_LANG_C99:
16365 case DW_LANG_C_plus_plus:
16366 case DW_LANG_ObjC:
16367 case DW_LANG_ObjC_plus_plus:
16368 case DW_LANG_Java:
16369 return 0;
16370 case DW_LANG_Fortran77:
16371 case DW_LANG_Fortran90:
16372 case DW_LANG_Fortran95:
16373 return 1;
16374 case DW_LANG_UPC:
16375 case DW_LANG_D:
16376 case DW_LANG_Python:
16377 return dwarf_version >= 4 ? 0 : -1;
16378 case DW_LANG_Ada95:
16379 case DW_LANG_Ada83:
16380 case DW_LANG_Cobol74:
16381 case DW_LANG_Cobol85:
16382 case DW_LANG_Pascal83:
16383 case DW_LANG_Modula2:
16384 case DW_LANG_PLI:
16385 return dwarf_version >= 4 ? 1 : -1;
16386 default:
16387 return -1;
16391 /* Given a tree node describing an array bound (either lower or upper) output
16392 a representation for that bound. */
16394 static void
16395 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
16397 switch (TREE_CODE (bound))
16399 case ERROR_MARK:
16400 return;
16402 /* All fixed-bounds are represented by INTEGER_CST nodes. */
16403 case INTEGER_CST:
16405 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
16406 int dflt;
16408 /* Use the default if possible. */
16409 if (bound_attr == DW_AT_lower_bound
16410 && tree_fits_shwi_p (bound)
16411 && (dflt = lower_bound_default ()) != -1
16412 && tree_to_shwi (bound) == dflt)
16415 /* If HOST_WIDE_INT is big enough then represent the bound as
16416 a constant value. We need to choose a form based on
16417 whether the type is signed or unsigned. We cannot just
16418 call add_AT_unsigned if the value itself is positive
16419 (add_AT_unsigned might add the unsigned value encoded as
16420 DW_FORM_data[1248]). Some DWARF consumers will lookup the
16421 bounds type and then sign extend any unsigned values found
16422 for signed types. This is needed only for
16423 DW_AT_{lower,upper}_bound, since for most other attributes,
16424 consumers will treat DW_FORM_data[1248] as unsigned values,
16425 regardless of the underlying type. */
16426 else if (prec <= HOST_BITS_PER_WIDE_INT
16427 || tree_fits_uhwi_p (bound))
16429 if (TYPE_UNSIGNED (TREE_TYPE (bound)))
16430 add_AT_unsigned (subrange_die, bound_attr,
16431 TREE_INT_CST_LOW (bound));
16432 else
16433 add_AT_int (subrange_die, bound_attr, TREE_INT_CST_LOW (bound));
16435 else
16436 /* Otherwise represent the bound as an unsigned value with
16437 the precision of its type. The precision and signedness
16438 of the type will be necessary to re-interpret it
16439 unambiguously. */
16440 add_AT_wide (subrange_die, bound_attr, bound);
16442 break;
16444 CASE_CONVERT:
16445 case VIEW_CONVERT_EXPR:
16446 add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
16447 break;
16449 case SAVE_EXPR:
16450 break;
16452 case VAR_DECL:
16453 case PARM_DECL:
16454 case RESULT_DECL:
16456 dw_die_ref decl_die = lookup_decl_die (bound);
16458 /* ??? Can this happen, or should the variable have been bound
16459 first? Probably it can, since I imagine that we try to create
16460 the types of parameters in the order in which they exist in
16461 the list, and won't have created a forward reference to a
16462 later parameter. */
16463 if (decl_die != NULL)
16465 add_AT_die_ref (subrange_die, bound_attr, decl_die);
16466 break;
16469 /* FALLTHRU */
16471 default:
16473 /* Otherwise try to create a stack operation procedure to
16474 evaluate the value of the array bound. */
16476 dw_die_ref ctx, decl_die;
16477 dw_loc_list_ref list;
16479 list = loc_list_from_tree (bound, 2);
16480 if (list == NULL || single_element_loc_list_p (list))
16482 /* If DW_AT_*bound is not a reference nor constant, it is
16483 a DWARF expression rather than location description.
16484 For that loc_list_from_tree (bound, 0) is needed.
16485 If that fails to give a single element list,
16486 fall back to outputting this as a reference anyway. */
16487 dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
16488 if (list2 && single_element_loc_list_p (list2))
16490 add_AT_loc (subrange_die, bound_attr, list2->expr);
16491 break;
16494 if (list == NULL)
16495 break;
16497 if (current_function_decl == 0)
16498 ctx = comp_unit_die ();
16499 else
16500 ctx = lookup_decl_die (current_function_decl);
16502 decl_die = new_die (DW_TAG_variable, ctx, bound);
16503 add_AT_flag (decl_die, DW_AT_artificial, 1);
16504 add_type_attribute (decl_die, TREE_TYPE (bound), TYPE_QUAL_CONST, ctx);
16505 add_AT_location_description (decl_die, DW_AT_location, list);
16506 add_AT_die_ref (subrange_die, bound_attr, decl_die);
16507 break;
16512 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
16513 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
16514 Note that the block of subscript information for an array type also
16515 includes information about the element type of the given array type. */
16517 static void
16518 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
16520 unsigned dimension_number;
16521 tree lower, upper;
16522 dw_die_ref subrange_die;
16524 for (dimension_number = 0;
16525 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
16526 type = TREE_TYPE (type), dimension_number++)
16528 tree domain = TYPE_DOMAIN (type);
16530 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
16531 break;
16533 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
16534 and (in GNU C only) variable bounds. Handle all three forms
16535 here. */
16536 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
16537 if (domain)
16539 /* We have an array type with specified bounds. */
16540 lower = TYPE_MIN_VALUE (domain);
16541 upper = TYPE_MAX_VALUE (domain);
16543 /* Define the index type. */
16544 if (TREE_TYPE (domain))
16546 /* ??? This is probably an Ada unnamed subrange type. Ignore the
16547 TREE_TYPE field. We can't emit debug info for this
16548 because it is an unnamed integral type. */
16549 if (TREE_CODE (domain) == INTEGER_TYPE
16550 && TYPE_NAME (domain) == NULL_TREE
16551 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
16552 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
16554 else
16555 add_type_attribute (subrange_die, TREE_TYPE (domain),
16556 TYPE_UNQUALIFIED, type_die);
16559 /* ??? If upper is NULL, the array has unspecified length,
16560 but it does have a lower bound. This happens with Fortran
16561 dimension arr(N:*)
16562 Since the debugger is definitely going to need to know N
16563 to produce useful results, go ahead and output the lower
16564 bound solo, and hope the debugger can cope. */
16566 add_bound_info (subrange_die, DW_AT_lower_bound, lower);
16567 if (upper)
16568 add_bound_info (subrange_die, DW_AT_upper_bound, upper);
16571 /* Otherwise we have an array type with an unspecified length. The
16572 DWARF-2 spec does not say how to handle this; let's just leave out the
16573 bounds. */
16577 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
16579 static void
16580 add_byte_size_attribute (dw_die_ref die, tree tree_node)
16582 dw_die_ref decl_die;
16583 HOST_WIDE_INT size;
16585 switch (TREE_CODE (tree_node))
16587 case ERROR_MARK:
16588 size = 0;
16589 break;
16590 case ENUMERAL_TYPE:
16591 case RECORD_TYPE:
16592 case UNION_TYPE:
16593 case QUAL_UNION_TYPE:
16594 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
16595 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
16597 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
16598 return;
16600 size = int_size_in_bytes (tree_node);
16601 break;
16602 case FIELD_DECL:
16603 /* For a data member of a struct or union, the DW_AT_byte_size is
16604 generally given as the number of bytes normally allocated for an
16605 object of the *declared* type of the member itself. This is true
16606 even for bit-fields. */
16607 size = int_size_in_bytes (field_type (tree_node));
16608 break;
16609 default:
16610 gcc_unreachable ();
16613 /* Note that `size' might be -1 when we get to this point. If it is, that
16614 indicates that the byte size of the entity in question is variable. We
16615 have no good way of expressing this fact in Dwarf at the present time,
16616 when location description was not used by the caller code instead. */
16617 if (size >= 0)
16618 add_AT_unsigned (die, DW_AT_byte_size, size);
16621 /* For a FIELD_DECL node which represents a bit-field, output an attribute
16622 which specifies the distance in bits from the highest order bit of the
16623 "containing object" for the bit-field to the highest order bit of the
16624 bit-field itself.
16626 For any given bit-field, the "containing object" is a hypothetical object
16627 (of some integral or enum type) within which the given bit-field lives. The
16628 type of this hypothetical "containing object" is always the same as the
16629 declared type of the individual bit-field itself. The determination of the
16630 exact location of the "containing object" for a bit-field is rather
16631 complicated. It's handled by the `field_byte_offset' function (above).
16633 Note that it is the size (in bytes) of the hypothetical "containing object"
16634 which will be given in the DW_AT_byte_size attribute for this bit-field.
16635 (See `byte_size_attribute' above). */
16637 static inline void
16638 add_bit_offset_attribute (dw_die_ref die, tree decl)
16640 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
16641 tree type = DECL_BIT_FIELD_TYPE (decl);
16642 HOST_WIDE_INT bitpos_int;
16643 HOST_WIDE_INT highest_order_object_bit_offset;
16644 HOST_WIDE_INT highest_order_field_bit_offset;
16645 HOST_WIDE_INT bit_offset;
16647 /* Must be a field and a bit field. */
16648 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
16650 /* We can't yet handle bit-fields whose offsets are variable, so if we
16651 encounter such things, just return without generating any attribute
16652 whatsoever. Likewise for variable or too large size. */
16653 if (! tree_fits_shwi_p (bit_position (decl))
16654 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
16655 return;
16657 bitpos_int = int_bit_position (decl);
16659 /* Note that the bit offset is always the distance (in bits) from the
16660 highest-order bit of the "containing object" to the highest-order bit of
16661 the bit-field itself. Since the "high-order end" of any object or field
16662 is different on big-endian and little-endian machines, the computation
16663 below must take account of these differences. */
16664 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
16665 highest_order_field_bit_offset = bitpos_int;
16667 if (! BYTES_BIG_ENDIAN)
16669 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
16670 highest_order_object_bit_offset += simple_type_size_in_bits (type);
16673 bit_offset
16674 = (! BYTES_BIG_ENDIAN
16675 ? highest_order_object_bit_offset - highest_order_field_bit_offset
16676 : highest_order_field_bit_offset - highest_order_object_bit_offset);
16678 if (bit_offset < 0)
16679 add_AT_int (die, DW_AT_bit_offset, bit_offset);
16680 else
16681 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
16684 /* For a FIELD_DECL node which represents a bit field, output an attribute
16685 which specifies the length in bits of the given field. */
16687 static inline void
16688 add_bit_size_attribute (dw_die_ref die, tree decl)
16690 /* Must be a field and a bit field. */
16691 gcc_assert (TREE_CODE (decl) == FIELD_DECL
16692 && DECL_BIT_FIELD_TYPE (decl));
16694 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
16695 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
16698 /* If the compiled language is ANSI C, then add a 'prototyped'
16699 attribute, if arg types are given for the parameters of a function. */
16701 static inline void
16702 add_prototyped_attribute (dw_die_ref die, tree func_type)
16704 if (get_AT_unsigned (comp_unit_die (), DW_AT_language) == DW_LANG_C89
16705 && prototype_p (func_type))
16706 add_AT_flag (die, DW_AT_prototyped, 1);
16709 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
16710 by looking in either the type declaration or object declaration
16711 equate table. */
16713 static inline dw_die_ref
16714 add_abstract_origin_attribute (dw_die_ref die, tree origin)
16716 dw_die_ref origin_die = NULL;
16718 if (TREE_CODE (origin) != FUNCTION_DECL)
16720 /* We may have gotten separated from the block for the inlined
16721 function, if we're in an exception handler or some such; make
16722 sure that the abstract function has been written out.
16724 Doing this for nested functions is wrong, however; functions are
16725 distinct units, and our context might not even be inline. */
16726 tree fn = origin;
16728 if (TYPE_P (fn))
16729 fn = TYPE_STUB_DECL (fn);
16731 fn = decl_function_context (fn);
16732 if (fn)
16733 dwarf2out_abstract_function (fn);
16736 if (DECL_P (origin))
16737 origin_die = lookup_decl_die (origin);
16738 else if (TYPE_P (origin))
16739 origin_die = lookup_type_die (origin);
16741 /* XXX: Functions that are never lowered don't always have correct block
16742 trees (in the case of java, they simply have no block tree, in some other
16743 languages). For these functions, there is nothing we can really do to
16744 output correct debug info for inlined functions in all cases. Rather
16745 than die, we'll just produce deficient debug info now, in that we will
16746 have variables without a proper abstract origin. In the future, when all
16747 functions are lowered, we should re-add a gcc_assert (origin_die)
16748 here. */
16750 if (origin_die)
16751 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
16752 return origin_die;
16755 /* We do not currently support the pure_virtual attribute. */
16757 static inline void
16758 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
16760 if (DECL_VINDEX (func_decl))
16762 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
16764 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
16765 add_AT_loc (die, DW_AT_vtable_elem_location,
16766 new_loc_descr (DW_OP_constu,
16767 tree_to_shwi (DECL_VINDEX (func_decl)),
16768 0));
16770 /* GNU extension: Record what type this method came from originally. */
16771 if (debug_info_level > DINFO_LEVEL_TERSE
16772 && DECL_CONTEXT (func_decl))
16773 add_AT_die_ref (die, DW_AT_containing_type,
16774 lookup_type_die (DECL_CONTEXT (func_decl)));
16778 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
16779 given decl. This used to be a vendor extension until after DWARF 4
16780 standardized it. */
16782 static void
16783 add_linkage_attr (dw_die_ref die, tree decl)
16785 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
16787 /* Mimic what assemble_name_raw does with a leading '*'. */
16788 if (name[0] == '*')
16789 name = &name[1];
16791 if (dwarf_version >= 4)
16792 add_AT_string (die, DW_AT_linkage_name, name);
16793 else
16794 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
16797 /* Add source coordinate attributes for the given decl. */
16799 static void
16800 add_src_coords_attributes (dw_die_ref die, tree decl)
16802 expanded_location s;
16804 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
16805 return;
16806 s = expand_location (DECL_SOURCE_LOCATION (decl));
16807 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
16808 add_AT_unsigned (die, DW_AT_decl_line, s.line);
16811 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
16813 static void
16814 add_linkage_name (dw_die_ref die, tree decl)
16816 if (debug_info_level > DINFO_LEVEL_NONE
16817 && (TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
16818 && TREE_PUBLIC (decl)
16819 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
16820 && die->die_tag != DW_TAG_member)
16822 /* Defer until we have an assembler name set. */
16823 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
16825 limbo_die_node *asm_name;
16827 asm_name = ggc_cleared_alloc<limbo_die_node> ();
16828 asm_name->die = die;
16829 asm_name->created_for = decl;
16830 asm_name->next = deferred_asm_name;
16831 deferred_asm_name = asm_name;
16833 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
16834 add_linkage_attr (die, decl);
16838 /* Add a DW_AT_name attribute and source coordinate attribute for the
16839 given decl, but only if it actually has a name. */
16841 static void
16842 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
16844 tree decl_name;
16846 decl_name = DECL_NAME (decl);
16847 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
16849 const char *name = dwarf2_name (decl, 0);
16850 if (name)
16851 add_name_attribute (die, name);
16852 if (! DECL_ARTIFICIAL (decl))
16853 add_src_coords_attributes (die, decl);
16855 add_linkage_name (die, decl);
16858 #ifdef VMS_DEBUGGING_INFO
16859 /* Get the function's name, as described by its RTL. This may be different
16860 from the DECL_NAME name used in the source file. */
16861 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
16863 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
16864 XEXP (DECL_RTL (decl), 0), false);
16865 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
16867 #endif /* VMS_DEBUGGING_INFO */
16870 #ifdef VMS_DEBUGGING_INFO
16871 /* Output the debug main pointer die for VMS */
16873 void
16874 dwarf2out_vms_debug_main_pointer (void)
16876 char label[MAX_ARTIFICIAL_LABEL_BYTES];
16877 dw_die_ref die;
16879 /* Allocate the VMS debug main subprogram die. */
16880 die = ggc_cleared_alloc<die_node> ();
16881 die->die_tag = DW_TAG_subprogram;
16882 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
16883 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
16884 current_function_funcdef_no);
16885 add_AT_lbl_id (die, DW_AT_entry_pc, label);
16887 /* Make it the first child of comp_unit_die (). */
16888 die->die_parent = comp_unit_die ();
16889 if (comp_unit_die ()->die_child)
16891 die->die_sib = comp_unit_die ()->die_child->die_sib;
16892 comp_unit_die ()->die_child->die_sib = die;
16894 else
16896 die->die_sib = die;
16897 comp_unit_die ()->die_child = die;
16900 #endif /* VMS_DEBUGGING_INFO */
16902 /* Push a new declaration scope. */
16904 static void
16905 push_decl_scope (tree scope)
16907 vec_safe_push (decl_scope_table, scope);
16910 /* Pop a declaration scope. */
16912 static inline void
16913 pop_decl_scope (void)
16915 decl_scope_table->pop ();
16918 /* walk_tree helper function for uses_local_type, below. */
16920 static tree
16921 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
16923 if (!TYPE_P (*tp))
16924 *walk_subtrees = 0;
16925 else
16927 tree name = TYPE_NAME (*tp);
16928 if (name && DECL_P (name) && decl_function_context (name))
16929 return *tp;
16931 return NULL_TREE;
16934 /* If TYPE involves a function-local type (including a local typedef to a
16935 non-local type), returns that type; otherwise returns NULL_TREE. */
16937 static tree
16938 uses_local_type (tree type)
16940 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
16941 return used;
16944 /* Return the DIE for the scope that immediately contains this type.
16945 Non-named types that do not involve a function-local type get global
16946 scope. Named types nested in namespaces or other types get their
16947 containing scope. All other types (i.e. function-local named types) get
16948 the current active scope. */
16950 static dw_die_ref
16951 scope_die_for (tree t, dw_die_ref context_die)
16953 dw_die_ref scope_die = NULL;
16954 tree containing_scope;
16956 /* Non-types always go in the current scope. */
16957 gcc_assert (TYPE_P (t));
16959 /* Use the scope of the typedef, rather than the scope of the type
16960 it refers to. */
16961 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
16962 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
16963 else
16964 containing_scope = TYPE_CONTEXT (t);
16966 /* Use the containing namespace if there is one. */
16967 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
16969 if (context_die == lookup_decl_die (containing_scope))
16970 /* OK */;
16971 else if (debug_info_level > DINFO_LEVEL_TERSE)
16972 context_die = get_context_die (containing_scope);
16973 else
16974 containing_scope = NULL_TREE;
16977 /* Ignore function type "scopes" from the C frontend. They mean that
16978 a tagged type is local to a parmlist of a function declarator, but
16979 that isn't useful to DWARF. */
16980 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
16981 containing_scope = NULL_TREE;
16983 if (SCOPE_FILE_SCOPE_P (containing_scope))
16985 /* If T uses a local type keep it local as well, to avoid references
16986 to function-local DIEs from outside the function. */
16987 if (current_function_decl && uses_local_type (t))
16988 scope_die = context_die;
16989 else
16990 scope_die = comp_unit_die ();
16992 else if (TYPE_P (containing_scope))
16994 /* For types, we can just look up the appropriate DIE. */
16995 if (debug_info_level > DINFO_LEVEL_TERSE)
16996 scope_die = get_context_die (containing_scope);
16997 else
16999 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
17000 if (scope_die == NULL)
17001 scope_die = comp_unit_die ();
17004 else
17005 scope_die = context_die;
17007 return scope_die;
17010 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
17012 static inline int
17013 local_scope_p (dw_die_ref context_die)
17015 for (; context_die; context_die = context_die->die_parent)
17016 if (context_die->die_tag == DW_TAG_inlined_subroutine
17017 || context_die->die_tag == DW_TAG_subprogram)
17018 return 1;
17020 return 0;
17023 /* Returns nonzero if CONTEXT_DIE is a class. */
17025 static inline int
17026 class_scope_p (dw_die_ref context_die)
17028 return (context_die
17029 && (context_die->die_tag == DW_TAG_structure_type
17030 || context_die->die_tag == DW_TAG_class_type
17031 || context_die->die_tag == DW_TAG_interface_type
17032 || context_die->die_tag == DW_TAG_union_type));
17035 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
17036 whether or not to treat a DIE in this context as a declaration. */
17038 static inline int
17039 class_or_namespace_scope_p (dw_die_ref context_die)
17041 return (class_scope_p (context_die)
17042 || (context_die && context_die->die_tag == DW_TAG_namespace));
17045 /* Many forms of DIEs require a "type description" attribute. This
17046 routine locates the proper "type descriptor" die for the type given
17047 by 'type' plus any additional qualifiers given by 'cv_quals', and
17048 adds a DW_AT_type attribute below the given die. */
17050 static void
17051 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
17052 dw_die_ref context_die)
17054 enum tree_code code = TREE_CODE (type);
17055 dw_die_ref type_die = NULL;
17057 /* ??? If this type is an unnamed subrange type of an integral, floating-point
17058 or fixed-point type, use the inner type. This is because we have no
17059 support for unnamed types in base_type_die. This can happen if this is
17060 an Ada subrange type. Correct solution is emit a subrange type die. */
17061 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
17062 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
17063 type = TREE_TYPE (type), code = TREE_CODE (type);
17065 if (code == ERROR_MARK
17066 /* Handle a special case. For functions whose return type is void, we
17067 generate *no* type attribute. (Note that no object may have type
17068 `void', so this only applies to function return types). */
17069 || code == VOID_TYPE)
17070 return;
17072 type_die = modified_type_die (type,
17073 cv_quals | TYPE_QUALS_NO_ADDR_SPACE (type),
17074 context_die);
17076 if (type_die != NULL)
17077 add_AT_die_ref (object_die, DW_AT_type, type_die);
17080 /* Given an object die, add the calling convention attribute for the
17081 function call type. */
17082 static void
17083 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
17085 enum dwarf_calling_convention value = DW_CC_normal;
17087 value = ((enum dwarf_calling_convention)
17088 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
17090 if (is_fortran ()
17091 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
17093 /* DWARF 2 doesn't provide a way to identify a program's source-level
17094 entry point. DW_AT_calling_convention attributes are only meant
17095 to describe functions' calling conventions. However, lacking a
17096 better way to signal the Fortran main program, we used this for
17097 a long time, following existing custom. Now, DWARF 4 has
17098 DW_AT_main_subprogram, which we add below, but some tools still
17099 rely on the old way, which we thus keep. */
17100 value = DW_CC_program;
17102 if (dwarf_version >= 4 || !dwarf_strict)
17103 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
17106 /* Only add the attribute if the backend requests it, and
17107 is not DW_CC_normal. */
17108 if (value && (value != DW_CC_normal))
17109 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
17112 /* Given a tree pointer to a struct, class, union, or enum type node, return
17113 a pointer to the (string) tag name for the given type, or zero if the type
17114 was declared without a tag. */
17116 static const char *
17117 type_tag (const_tree type)
17119 const char *name = 0;
17121 if (TYPE_NAME (type) != 0)
17123 tree t = 0;
17125 /* Find the IDENTIFIER_NODE for the type name. */
17126 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
17127 && !TYPE_NAMELESS (type))
17128 t = TYPE_NAME (type);
17130 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
17131 a TYPE_DECL node, regardless of whether or not a `typedef' was
17132 involved. */
17133 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
17134 && ! DECL_IGNORED_P (TYPE_NAME (type)))
17136 /* We want to be extra verbose. Don't call dwarf_name if
17137 DECL_NAME isn't set. The default hook for decl_printable_name
17138 doesn't like that, and in this context it's correct to return
17139 0, instead of "<anonymous>" or the like. */
17140 if (DECL_NAME (TYPE_NAME (type))
17141 && !DECL_NAMELESS (TYPE_NAME (type)))
17142 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
17145 /* Now get the name as a string, or invent one. */
17146 if (!name && t != 0)
17147 name = IDENTIFIER_POINTER (t);
17150 return (name == 0 || *name == '\0') ? 0 : name;
17153 /* Return the type associated with a data member, make a special check
17154 for bit field types. */
17156 static inline tree
17157 member_declared_type (const_tree member)
17159 return (DECL_BIT_FIELD_TYPE (member)
17160 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
17163 /* Get the decl's label, as described by its RTL. This may be different
17164 from the DECL_NAME name used in the source file. */
17166 #if 0
17167 static const char *
17168 decl_start_label (tree decl)
17170 rtx x;
17171 const char *fnname;
17173 x = DECL_RTL (decl);
17174 gcc_assert (MEM_P (x));
17176 x = XEXP (x, 0);
17177 gcc_assert (GET_CODE (x) == SYMBOL_REF);
17179 fnname = XSTR (x, 0);
17180 return fnname;
17182 #endif
17184 /* These routines generate the internal representation of the DIE's for
17185 the compilation unit. Debugging information is collected by walking
17186 the declaration trees passed in from dwarf2out_decl(). */
17188 static void
17189 gen_array_type_die (tree type, dw_die_ref context_die)
17191 dw_die_ref scope_die = scope_die_for (type, context_die);
17192 dw_die_ref array_die;
17194 /* GNU compilers represent multidimensional array types as sequences of one
17195 dimensional array types whose element types are themselves array types.
17196 We sometimes squish that down to a single array_type DIE with multiple
17197 subscripts in the Dwarf debugging info. The draft Dwarf specification
17198 say that we are allowed to do this kind of compression in C, because
17199 there is no difference between an array of arrays and a multidimensional
17200 array. We don't do this for Ada to remain as close as possible to the
17201 actual representation, which is especially important against the language
17202 flexibilty wrt arrays of variable size. */
17204 bool collapse_nested_arrays = !is_ada ();
17205 tree element_type;
17207 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
17208 DW_TAG_string_type doesn't have DW_AT_type attribute). */
17209 if (TYPE_STRING_FLAG (type)
17210 && TREE_CODE (type) == ARRAY_TYPE
17211 && is_fortran ()
17212 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
17214 HOST_WIDE_INT size;
17216 array_die = new_die (DW_TAG_string_type, scope_die, type);
17217 add_name_attribute (array_die, type_tag (type));
17218 equate_type_number_to_die (type, array_die);
17219 size = int_size_in_bytes (type);
17220 if (size >= 0)
17221 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17222 else if (TYPE_DOMAIN (type) != NULL_TREE
17223 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
17224 && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
17226 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
17227 dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
17229 size = int_size_in_bytes (TREE_TYPE (szdecl));
17230 if (loc && size > 0)
17232 add_AT_location_description (array_die, DW_AT_string_length, loc);
17233 if (size != DWARF2_ADDR_SIZE)
17234 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17237 return;
17240 array_die = new_die (DW_TAG_array_type, scope_die, type);
17241 add_name_attribute (array_die, type_tag (type));
17242 equate_type_number_to_die (type, array_die);
17244 if (TREE_CODE (type) == VECTOR_TYPE)
17245 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
17247 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
17248 if (is_fortran ()
17249 && TREE_CODE (type) == ARRAY_TYPE
17250 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
17251 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
17252 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17254 #if 0
17255 /* We default the array ordering. SDB will probably do
17256 the right things even if DW_AT_ordering is not present. It's not even
17257 an issue until we start to get into multidimensional arrays anyway. If
17258 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
17259 then we'll have to put the DW_AT_ordering attribute back in. (But if
17260 and when we find out that we need to put these in, we will only do so
17261 for multidimensional arrays. */
17262 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17263 #endif
17265 if (TREE_CODE (type) == VECTOR_TYPE)
17267 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
17268 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
17269 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
17270 add_bound_info (subrange_die, DW_AT_upper_bound,
17271 size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
17273 else
17274 add_subscript_info (array_die, type, collapse_nested_arrays);
17276 /* Add representation of the type of the elements of this array type and
17277 emit the corresponding DIE if we haven't done it already. */
17278 element_type = TREE_TYPE (type);
17279 if (collapse_nested_arrays)
17280 while (TREE_CODE (element_type) == ARRAY_TYPE)
17282 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
17283 break;
17284 element_type = TREE_TYPE (element_type);
17287 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED, context_die);
17289 add_gnat_descriptive_type_attribute (array_die, type, context_die);
17290 if (TYPE_ARTIFICIAL (type))
17291 add_AT_flag (array_die, DW_AT_artificial, 1);
17293 if (get_AT (array_die, DW_AT_name))
17294 add_pubtype (type, array_die);
17297 static dw_loc_descr_ref
17298 descr_info_loc (tree val, tree base_decl)
17300 HOST_WIDE_INT size;
17301 dw_loc_descr_ref loc, loc2;
17302 enum dwarf_location_atom op;
17304 if (val == base_decl)
17305 return new_loc_descr (DW_OP_push_object_address, 0, 0);
17307 switch (TREE_CODE (val))
17309 CASE_CONVERT:
17310 return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17311 case VAR_DECL:
17312 return loc_descriptor_from_tree (val, 0);
17313 case INTEGER_CST:
17314 if (tree_fits_shwi_p (val))
17315 return int_loc_descriptor (tree_to_shwi (val));
17316 break;
17317 case INDIRECT_REF:
17318 size = int_size_in_bytes (TREE_TYPE (val));
17319 if (size < 0)
17320 break;
17321 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17322 if (!loc)
17323 break;
17324 if (size == DWARF2_ADDR_SIZE)
17325 add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
17326 else
17327 add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
17328 return loc;
17329 case POINTER_PLUS_EXPR:
17330 case PLUS_EXPR:
17331 if (tree_fits_uhwi_p (TREE_OPERAND (val, 1))
17332 && tree_to_uhwi (TREE_OPERAND (val, 1)) < 16384)
17334 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17335 if (!loc)
17336 break;
17337 loc_descr_plus_const (&loc, tree_to_shwi (TREE_OPERAND (val, 1)));
17339 else
17341 op = DW_OP_plus;
17342 do_binop:
17343 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17344 if (!loc)
17345 break;
17346 loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
17347 if (!loc2)
17348 break;
17349 add_loc_descr (&loc, loc2);
17350 add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
17352 return loc;
17353 case MINUS_EXPR:
17354 op = DW_OP_minus;
17355 goto do_binop;
17356 case MULT_EXPR:
17357 op = DW_OP_mul;
17358 goto do_binop;
17359 case EQ_EXPR:
17360 op = DW_OP_eq;
17361 goto do_binop;
17362 case NE_EXPR:
17363 op = DW_OP_ne;
17364 goto do_binop;
17365 default:
17366 break;
17368 return NULL;
17371 static void
17372 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
17373 tree val, tree base_decl)
17375 dw_loc_descr_ref loc;
17377 if (tree_fits_shwi_p (val))
17379 add_AT_unsigned (die, attr, tree_to_shwi (val));
17380 return;
17383 loc = descr_info_loc (val, base_decl);
17384 if (!loc)
17385 return;
17387 add_AT_loc (die, attr, loc);
17390 /* This routine generates DIE for array with hidden descriptor, details
17391 are filled into *info by a langhook. */
17393 static void
17394 gen_descr_array_type_die (tree type, struct array_descr_info *info,
17395 dw_die_ref context_die)
17397 dw_die_ref scope_die = scope_die_for (type, context_die);
17398 dw_die_ref array_die;
17399 int dim;
17401 array_die = new_die (DW_TAG_array_type, scope_die, type);
17402 add_name_attribute (array_die, type_tag (type));
17403 equate_type_number_to_die (type, array_die);
17405 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
17406 if (is_fortran ()
17407 && info->ndimensions >= 2)
17408 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17410 if (info->data_location)
17411 add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
17412 info->base_decl);
17413 if (info->associated)
17414 add_descr_info_field (array_die, DW_AT_associated, info->associated,
17415 info->base_decl);
17416 if (info->allocated)
17417 add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
17418 info->base_decl);
17420 for (dim = 0; dim < info->ndimensions; dim++)
17422 dw_die_ref subrange_die
17423 = new_die (DW_TAG_subrange_type, array_die, NULL);
17425 if (info->dimen[dim].lower_bound)
17427 /* If it is the default value, omit it. */
17428 int dflt;
17430 if (tree_fits_shwi_p (info->dimen[dim].lower_bound)
17431 && (dflt = lower_bound_default ()) != -1
17432 && tree_to_shwi (info->dimen[dim].lower_bound) == dflt)
17434 else
17435 add_descr_info_field (subrange_die, DW_AT_lower_bound,
17436 info->dimen[dim].lower_bound,
17437 info->base_decl);
17439 if (info->dimen[dim].upper_bound)
17440 add_descr_info_field (subrange_die, DW_AT_upper_bound,
17441 info->dimen[dim].upper_bound,
17442 info->base_decl);
17443 if (info->dimen[dim].stride)
17444 add_descr_info_field (subrange_die, DW_AT_byte_stride,
17445 info->dimen[dim].stride,
17446 info->base_decl);
17449 gen_type_die (info->element_type, context_die);
17450 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
17451 context_die);
17453 if (get_AT (array_die, DW_AT_name))
17454 add_pubtype (type, array_die);
17457 #if 0
17458 static void
17459 gen_entry_point_die (tree decl, dw_die_ref context_die)
17461 tree origin = decl_ultimate_origin (decl);
17462 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
17464 if (origin != NULL)
17465 add_abstract_origin_attribute (decl_die, origin);
17466 else
17468 add_name_and_src_coords_attributes (decl_die, decl);
17469 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
17470 TYPE_UNQUALIFIED, context_die);
17473 if (DECL_ABSTRACT_P (decl))
17474 equate_decl_number_to_die (decl, decl_die);
17475 else
17476 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
17478 #endif
17480 /* Walk through the list of incomplete types again, trying once more to
17481 emit full debugging info for them. */
17483 static void
17484 retry_incomplete_types (void)
17486 int i;
17488 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
17489 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
17490 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
17493 /* Determine what tag to use for a record type. */
17495 static enum dwarf_tag
17496 record_type_tag (tree type)
17498 if (! lang_hooks.types.classify_record)
17499 return DW_TAG_structure_type;
17501 switch (lang_hooks.types.classify_record (type))
17503 case RECORD_IS_STRUCT:
17504 return DW_TAG_structure_type;
17506 case RECORD_IS_CLASS:
17507 return DW_TAG_class_type;
17509 case RECORD_IS_INTERFACE:
17510 if (dwarf_version >= 3 || !dwarf_strict)
17511 return DW_TAG_interface_type;
17512 return DW_TAG_structure_type;
17514 default:
17515 gcc_unreachable ();
17519 /* Generate a DIE to represent an enumeration type. Note that these DIEs
17520 include all of the information about the enumeration values also. Each
17521 enumerated type name/value is listed as a child of the enumerated type
17522 DIE. */
17524 static dw_die_ref
17525 gen_enumeration_type_die (tree type, dw_die_ref context_die)
17527 dw_die_ref type_die = lookup_type_die (type);
17529 if (type_die == NULL)
17531 type_die = new_die (DW_TAG_enumeration_type,
17532 scope_die_for (type, context_die), type);
17533 equate_type_number_to_die (type, type_die);
17534 add_name_attribute (type_die, type_tag (type));
17535 if (dwarf_version >= 4 || !dwarf_strict)
17537 if (ENUM_IS_SCOPED (type))
17538 add_AT_flag (type_die, DW_AT_enum_class, 1);
17539 if (ENUM_IS_OPAQUE (type))
17540 add_AT_flag (type_die, DW_AT_declaration, 1);
17543 else if (! TYPE_SIZE (type))
17544 return type_die;
17545 else
17546 remove_AT (type_die, DW_AT_declaration);
17548 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
17549 given enum type is incomplete, do not generate the DW_AT_byte_size
17550 attribute or the DW_AT_element_list attribute. */
17551 if (TYPE_SIZE (type))
17553 tree link;
17555 TREE_ASM_WRITTEN (type) = 1;
17556 add_byte_size_attribute (type_die, type);
17557 if (dwarf_version >= 3 || !dwarf_strict)
17559 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
17560 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED,
17561 context_die);
17563 if (TYPE_STUB_DECL (type) != NULL_TREE)
17565 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
17566 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
17569 /* If the first reference to this type was as the return type of an
17570 inline function, then it may not have a parent. Fix this now. */
17571 if (type_die->die_parent == NULL)
17572 add_child_die (scope_die_for (type, context_die), type_die);
17574 for (link = TYPE_VALUES (type);
17575 link != NULL; link = TREE_CHAIN (link))
17577 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
17578 tree value = TREE_VALUE (link);
17580 add_name_attribute (enum_die,
17581 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
17583 if (TREE_CODE (value) == CONST_DECL)
17584 value = DECL_INITIAL (value);
17586 if (simple_type_size_in_bits (TREE_TYPE (value))
17587 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
17589 /* For constant forms created by add_AT_unsigned DWARF
17590 consumers (GDB, elfutils, etc.) always zero extend
17591 the value. Only when the actual value is negative
17592 do we need to use add_AT_int to generate a constant
17593 form that can represent negative values. */
17594 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
17595 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
17596 add_AT_unsigned (enum_die, DW_AT_const_value,
17597 (unsigned HOST_WIDE_INT) val);
17598 else
17599 add_AT_int (enum_die, DW_AT_const_value, val);
17601 else
17602 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
17603 that here. TODO: This should be re-worked to use correct
17604 signed/unsigned double tags for all cases. */
17605 add_AT_wide (enum_die, DW_AT_const_value, value);
17608 add_gnat_descriptive_type_attribute (type_die, type, context_die);
17609 if (TYPE_ARTIFICIAL (type))
17610 add_AT_flag (type_die, DW_AT_artificial, 1);
17612 else
17613 add_AT_flag (type_die, DW_AT_declaration, 1);
17615 add_pubtype (type, type_die);
17617 return type_die;
17620 /* Generate a DIE to represent either a real live formal parameter decl or to
17621 represent just the type of some formal parameter position in some function
17622 type.
17624 Note that this routine is a bit unusual because its argument may be a
17625 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
17626 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
17627 node. If it's the former then this function is being called to output a
17628 DIE to represent a formal parameter object (or some inlining thereof). If
17629 it's the latter, then this function is only being called to output a
17630 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
17631 argument type of some subprogram type.
17632 If EMIT_NAME_P is true, name and source coordinate attributes
17633 are emitted. */
17635 static dw_die_ref
17636 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
17637 dw_die_ref context_die)
17639 tree node_or_origin = node ? node : origin;
17640 tree ultimate_origin;
17641 dw_die_ref parm_die
17642 = new_die (DW_TAG_formal_parameter, context_die, node);
17644 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
17646 case tcc_declaration:
17647 ultimate_origin = decl_ultimate_origin (node_or_origin);
17648 if (node || ultimate_origin)
17649 origin = ultimate_origin;
17650 if (origin != NULL)
17651 add_abstract_origin_attribute (parm_die, origin);
17652 else if (emit_name_p)
17653 add_name_and_src_coords_attributes (parm_die, node);
17654 if (origin == NULL
17655 || (! DECL_ABSTRACT_P (node_or_origin)
17656 && variably_modified_type_p (TREE_TYPE (node_or_origin),
17657 decl_function_context
17658 (node_or_origin))))
17660 tree type = TREE_TYPE (node_or_origin);
17661 if (decl_by_reference_p (node_or_origin))
17662 add_type_attribute (parm_die, TREE_TYPE (type),
17663 TYPE_UNQUALIFIED, context_die);
17664 else
17665 add_type_attribute (parm_die, type,
17666 decl_quals (node_or_origin),
17667 context_die);
17669 if (origin == NULL && DECL_ARTIFICIAL (node))
17670 add_AT_flag (parm_die, DW_AT_artificial, 1);
17672 if (node && node != origin)
17673 equate_decl_number_to_die (node, parm_die);
17674 if (! DECL_ABSTRACT_P (node_or_origin))
17675 add_location_or_const_value_attribute (parm_die, node_or_origin,
17676 node == NULL, DW_AT_location);
17678 break;
17680 case tcc_type:
17681 /* We were called with some kind of a ..._TYPE node. */
17682 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED,
17683 context_die);
17684 break;
17686 default:
17687 gcc_unreachable ();
17690 return parm_die;
17693 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
17694 children DW_TAG_formal_parameter DIEs representing the arguments of the
17695 parameter pack.
17697 PARM_PACK must be a function parameter pack.
17698 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
17699 must point to the subsequent arguments of the function PACK_ARG belongs to.
17700 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
17701 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
17702 following the last one for which a DIE was generated. */
17704 static dw_die_ref
17705 gen_formal_parameter_pack_die (tree parm_pack,
17706 tree pack_arg,
17707 dw_die_ref subr_die,
17708 tree *next_arg)
17710 tree arg;
17711 dw_die_ref parm_pack_die;
17713 gcc_assert (parm_pack
17714 && lang_hooks.function_parameter_pack_p (parm_pack)
17715 && subr_die);
17717 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
17718 add_src_coords_attributes (parm_pack_die, parm_pack);
17720 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
17722 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
17723 parm_pack))
17724 break;
17725 gen_formal_parameter_die (arg, NULL,
17726 false /* Don't emit name attribute. */,
17727 parm_pack_die);
17729 if (next_arg)
17730 *next_arg = arg;
17731 return parm_pack_die;
17734 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
17735 at the end of an (ANSI prototyped) formal parameters list. */
17737 static void
17738 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
17740 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
17743 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
17744 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
17745 parameters as specified in some function type specification (except for
17746 those which appear as part of a function *definition*). */
17748 static void
17749 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
17751 tree link;
17752 tree formal_type = NULL;
17753 tree first_parm_type;
17754 tree arg;
17756 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
17758 arg = DECL_ARGUMENTS (function_or_method_type);
17759 function_or_method_type = TREE_TYPE (function_or_method_type);
17761 else
17762 arg = NULL_TREE;
17764 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
17766 /* Make our first pass over the list of formal parameter types and output a
17767 DW_TAG_formal_parameter DIE for each one. */
17768 for (link = first_parm_type; link; )
17770 dw_die_ref parm_die;
17772 formal_type = TREE_VALUE (link);
17773 if (formal_type == void_type_node)
17774 break;
17776 /* Output a (nameless) DIE to represent the formal parameter itself. */
17777 parm_die = gen_formal_parameter_die (formal_type, NULL,
17778 true /* Emit name attribute. */,
17779 context_die);
17780 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
17781 && link == first_parm_type)
17783 add_AT_flag (parm_die, DW_AT_artificial, 1);
17784 if (dwarf_version >= 3 || !dwarf_strict)
17785 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
17787 else if (arg && DECL_ARTIFICIAL (arg))
17788 add_AT_flag (parm_die, DW_AT_artificial, 1);
17790 link = TREE_CHAIN (link);
17791 if (arg)
17792 arg = DECL_CHAIN (arg);
17795 /* If this function type has an ellipsis, add a
17796 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
17797 if (formal_type != void_type_node)
17798 gen_unspecified_parameters_die (function_or_method_type, context_die);
17800 /* Make our second (and final) pass over the list of formal parameter types
17801 and output DIEs to represent those types (as necessary). */
17802 for (link = TYPE_ARG_TYPES (function_or_method_type);
17803 link && TREE_VALUE (link);
17804 link = TREE_CHAIN (link))
17805 gen_type_die (TREE_VALUE (link), context_die);
17808 /* We want to generate the DIE for TYPE so that we can generate the
17809 die for MEMBER, which has been defined; we will need to refer back
17810 to the member declaration nested within TYPE. If we're trying to
17811 generate minimal debug info for TYPE, processing TYPE won't do the
17812 trick; we need to attach the member declaration by hand. */
17814 static void
17815 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
17817 gen_type_die (type, context_die);
17819 /* If we're trying to avoid duplicate debug info, we may not have
17820 emitted the member decl for this function. Emit it now. */
17821 if (TYPE_STUB_DECL (type)
17822 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
17823 && ! lookup_decl_die (member))
17825 dw_die_ref type_die;
17826 gcc_assert (!decl_ultimate_origin (member));
17828 push_decl_scope (type);
17829 type_die = lookup_type_die_strip_naming_typedef (type);
17830 if (TREE_CODE (member) == FUNCTION_DECL)
17831 gen_subprogram_die (member, type_die);
17832 else if (TREE_CODE (member) == FIELD_DECL)
17834 /* Ignore the nameless fields that are used to skip bits but handle
17835 C++ anonymous unions and structs. */
17836 if (DECL_NAME (member) != NULL_TREE
17837 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
17838 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
17840 gen_type_die (member_declared_type (member), type_die);
17841 gen_field_die (member, type_die);
17844 else
17845 gen_variable_die (member, NULL_TREE, type_die);
17847 pop_decl_scope ();
17851 /* Forward declare these functions, because they are mutually recursive
17852 with their set_block_* pairing functions. */
17853 static void set_decl_origin_self (tree);
17854 static void set_decl_abstract_flags (tree, int);
17856 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
17857 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
17858 that it points to the node itself, thus indicating that the node is its
17859 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
17860 the given node is NULL, recursively descend the decl/block tree which
17861 it is the root of, and for each other ..._DECL or BLOCK node contained
17862 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
17863 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
17864 values to point to themselves. */
17866 static void
17867 set_block_origin_self (tree stmt)
17869 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
17871 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
17874 tree local_decl;
17876 for (local_decl = BLOCK_VARS (stmt);
17877 local_decl != NULL_TREE;
17878 local_decl = DECL_CHAIN (local_decl))
17879 if (! DECL_EXTERNAL (local_decl))
17880 set_decl_origin_self (local_decl); /* Potential recursion. */
17884 tree subblock;
17886 for (subblock = BLOCK_SUBBLOCKS (stmt);
17887 subblock != NULL_TREE;
17888 subblock = BLOCK_CHAIN (subblock))
17889 set_block_origin_self (subblock); /* Recurse. */
17894 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
17895 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
17896 node to so that it points to the node itself, thus indicating that the
17897 node represents its own (abstract) origin. Additionally, if the
17898 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
17899 the decl/block tree of which the given node is the root of, and for
17900 each other ..._DECL or BLOCK node contained therein whose
17901 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
17902 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
17903 point to themselves. */
17905 static void
17906 set_decl_origin_self (tree decl)
17908 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
17910 DECL_ABSTRACT_ORIGIN (decl) = decl;
17911 if (TREE_CODE (decl) == FUNCTION_DECL)
17913 tree arg;
17915 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
17916 DECL_ABSTRACT_ORIGIN (arg) = arg;
17917 if (DECL_INITIAL (decl) != NULL_TREE
17918 && DECL_INITIAL (decl) != error_mark_node)
17919 set_block_origin_self (DECL_INITIAL (decl));
17924 /* Given a pointer to some BLOCK node, and a boolean value to set the
17925 "abstract" flags to, set that value into the BLOCK_ABSTRACT flag for
17926 the given block, and for all local decls and all local sub-blocks
17927 (recursively) which are contained therein. */
17929 static void
17930 set_block_abstract_flags (tree stmt, int setting)
17932 tree local_decl;
17933 tree subblock;
17934 unsigned int i;
17936 BLOCK_ABSTRACT (stmt) = setting;
17938 for (local_decl = BLOCK_VARS (stmt);
17939 local_decl != NULL_TREE;
17940 local_decl = DECL_CHAIN (local_decl))
17941 if (! DECL_EXTERNAL (local_decl))
17942 set_decl_abstract_flags (local_decl, setting);
17944 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
17946 local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
17947 if ((TREE_CODE (local_decl) == VAR_DECL && !TREE_STATIC (local_decl))
17948 || TREE_CODE (local_decl) == PARM_DECL)
17949 set_decl_abstract_flags (local_decl, setting);
17952 for (subblock = BLOCK_SUBBLOCKS (stmt);
17953 subblock != NULL_TREE;
17954 subblock = BLOCK_CHAIN (subblock))
17955 set_block_abstract_flags (subblock, setting);
17958 /* Given a pointer to some ..._DECL node, and a boolean value to set the
17959 "abstract" flags to, set that value into the DECL_ABSTRACT_P flag for the
17960 given decl, and (in the case where the decl is a FUNCTION_DECL) also
17961 set the abstract flags for all of the parameters, local vars, local
17962 blocks and sub-blocks (recursively) to the same setting. */
17964 static void
17965 set_decl_abstract_flags (tree decl, int setting)
17967 DECL_ABSTRACT_P (decl) = setting;
17968 if (TREE_CODE (decl) == FUNCTION_DECL)
17970 tree arg;
17972 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
17973 DECL_ABSTRACT_P (arg) = setting;
17974 if (DECL_INITIAL (decl) != NULL_TREE
17975 && DECL_INITIAL (decl) != error_mark_node)
17976 set_block_abstract_flags (DECL_INITIAL (decl), setting);
17980 /* Generate the DWARF2 info for the "abstract" instance of a function which we
17981 may later generate inlined and/or out-of-line instances of. */
17983 static void
17984 dwarf2out_abstract_function (tree decl)
17986 dw_die_ref old_die;
17987 tree save_fn;
17988 tree context;
17989 int was_abstract;
17990 htab_t old_decl_loc_table;
17991 htab_t old_cached_dw_loc_list_table;
17992 int old_call_site_count, old_tail_call_site_count;
17993 struct call_arg_loc_node *old_call_arg_locations;
17995 /* Make sure we have the actual abstract inline, not a clone. */
17996 decl = DECL_ORIGIN (decl);
17998 old_die = lookup_decl_die (decl);
17999 if (old_die && get_AT (old_die, DW_AT_inline))
18000 /* We've already generated the abstract instance. */
18001 return;
18003 /* We can be called while recursively when seeing block defining inlined subroutine
18004 DIE. Be sure to not clobber the outer location table nor use it or we would
18005 get locations in abstract instantces. */
18006 old_decl_loc_table = decl_loc_table;
18007 decl_loc_table = NULL;
18008 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
18009 cached_dw_loc_list_table = NULL;
18010 old_call_arg_locations = call_arg_locations;
18011 call_arg_locations = NULL;
18012 old_call_site_count = call_site_count;
18013 call_site_count = -1;
18014 old_tail_call_site_count = tail_call_site_count;
18015 tail_call_site_count = -1;
18017 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
18018 we don't get confused by DECL_ABSTRACT_P. */
18019 if (debug_info_level > DINFO_LEVEL_TERSE)
18021 context = decl_class_context (decl);
18022 if (context)
18023 gen_type_die_for_member
18024 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
18027 /* Pretend we've just finished compiling this function. */
18028 save_fn = current_function_decl;
18029 current_function_decl = decl;
18031 was_abstract = DECL_ABSTRACT_P (decl);
18032 set_decl_abstract_flags (decl, 1);
18033 dwarf2out_decl (decl);
18034 if (! was_abstract)
18035 set_decl_abstract_flags (decl, 0);
18037 current_function_decl = save_fn;
18038 decl_loc_table = old_decl_loc_table;
18039 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
18040 call_arg_locations = old_call_arg_locations;
18041 call_site_count = old_call_site_count;
18042 tail_call_site_count = old_tail_call_site_count;
18045 /* Helper function of premark_used_types() which gets called through
18046 htab_traverse.
18048 Marks the DIE of a given type in *SLOT as perennial, so it never gets
18049 marked as unused by prune_unused_types. */
18051 bool
18052 premark_used_types_helper (tree const &type, void *)
18054 dw_die_ref die;
18056 die = lookup_type_die (type);
18057 if (die != NULL)
18058 die->die_perennial_p = 1;
18059 return true;
18062 /* Helper function of premark_types_used_by_global_vars which gets called
18063 through htab_traverse.
18065 Marks the DIE of a given type in *SLOT as perennial, so it never gets
18066 marked as unused by prune_unused_types. The DIE of the type is marked
18067 only if the global variable using the type will actually be emitted. */
18069 static int
18070 premark_types_used_by_global_vars_helper (void **slot,
18071 void *data ATTRIBUTE_UNUSED)
18073 struct types_used_by_vars_entry *entry;
18074 dw_die_ref die;
18076 entry = (struct types_used_by_vars_entry *) *slot;
18077 gcc_assert (entry->type != NULL
18078 && entry->var_decl != NULL);
18079 die = lookup_type_die (entry->type);
18080 if (die)
18082 /* Ask cgraph if the global variable really is to be emitted.
18083 If yes, then we'll keep the DIE of ENTRY->TYPE. */
18084 varpool_node *node = varpool_node::get (entry->var_decl);
18085 if (node && node->definition)
18087 die->die_perennial_p = 1;
18088 /* Keep the parent DIEs as well. */
18089 while ((die = die->die_parent) && die->die_perennial_p == 0)
18090 die->die_perennial_p = 1;
18093 return 1;
18096 /* Mark all members of used_types_hash as perennial. */
18098 static void
18099 premark_used_types (struct function *fun)
18101 if (fun && fun->used_types_hash)
18102 fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
18105 /* Mark all members of types_used_by_vars_entry as perennial. */
18107 static void
18108 premark_types_used_by_global_vars (void)
18110 if (types_used_by_vars_hash)
18111 htab_traverse (types_used_by_vars_hash,
18112 premark_types_used_by_global_vars_helper, NULL);
18115 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
18116 for CA_LOC call arg loc node. */
18118 static dw_die_ref
18119 gen_call_site_die (tree decl, dw_die_ref subr_die,
18120 struct call_arg_loc_node *ca_loc)
18122 dw_die_ref stmt_die = NULL, die;
18123 tree block = ca_loc->block;
18125 while (block
18126 && block != DECL_INITIAL (decl)
18127 && TREE_CODE (block) == BLOCK)
18129 if (block_map.length () > BLOCK_NUMBER (block))
18130 stmt_die = block_map[BLOCK_NUMBER (block)];
18131 if (stmt_die)
18132 break;
18133 block = BLOCK_SUPERCONTEXT (block);
18135 if (stmt_die == NULL)
18136 stmt_die = subr_die;
18137 die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
18138 add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
18139 if (ca_loc->tail_call_p)
18140 add_AT_flag (die, DW_AT_GNU_tail_call, 1);
18141 if (ca_loc->symbol_ref)
18143 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
18144 if (tdie)
18145 add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
18146 else
18147 add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref, false);
18149 return die;
18152 /* Generate a DIE to represent a declared function (either file-scope or
18153 block-local). */
18155 static void
18156 gen_subprogram_die (tree decl, dw_die_ref context_die)
18158 tree origin = decl_ultimate_origin (decl);
18159 dw_die_ref subr_die;
18160 tree outer_scope;
18161 dw_die_ref old_die = lookup_decl_die (decl);
18162 int declaration = (current_function_decl != decl
18163 || class_or_namespace_scope_p (context_die));
18165 premark_used_types (DECL_STRUCT_FUNCTION (decl));
18167 /* It is possible to have both DECL_ABSTRACT_P and DECLARATION be true if we
18168 started to generate the abstract instance of an inline, decided to output
18169 its containing class, and proceeded to emit the declaration of the inline
18170 from the member list for the class. If so, DECLARATION takes priority;
18171 we'll get back to the abstract instance when done with the class. */
18173 /* The class-scope declaration DIE must be the primary DIE. */
18174 if (origin && declaration && class_or_namespace_scope_p (context_die))
18176 origin = NULL;
18177 gcc_assert (!old_die);
18180 /* Now that the C++ front end lazily declares artificial member fns, we
18181 might need to retrofit the declaration into its class. */
18182 if (!declaration && !origin && !old_die
18183 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
18184 && !class_or_namespace_scope_p (context_die)
18185 && debug_info_level > DINFO_LEVEL_TERSE)
18186 old_die = force_decl_die (decl);
18188 if (origin != NULL)
18190 gcc_assert (!declaration || local_scope_p (context_die));
18192 /* Fixup die_parent for the abstract instance of a nested
18193 inline function. */
18194 if (old_die && old_die->die_parent == NULL)
18195 add_child_die (context_die, old_die);
18197 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18198 add_abstract_origin_attribute (subr_die, origin);
18199 /* This is where the actual code for a cloned function is.
18200 Let's emit linkage name attribute for it. This helps
18201 debuggers to e.g, set breakpoints into
18202 constructors/destructors when the user asks "break
18203 K::K". */
18204 add_linkage_name (subr_die, decl);
18206 else if (old_die)
18208 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18209 struct dwarf_file_data * file_index = lookup_filename (s.file);
18211 if (!get_AT_flag (old_die, DW_AT_declaration)
18212 /* We can have a normal definition following an inline one in the
18213 case of redefinition of GNU C extern inlines.
18214 It seems reasonable to use AT_specification in this case. */
18215 && !get_AT (old_die, DW_AT_inline))
18217 /* Detect and ignore this case, where we are trying to output
18218 something we have already output. */
18219 return;
18222 /* If the definition comes from the same place as the declaration,
18223 maybe use the old DIE. We always want the DIE for this function
18224 that has the *_pc attributes to be under comp_unit_die so the
18225 debugger can find it. We also need to do this for abstract
18226 instances of inlines, since the spec requires the out-of-line copy
18227 to have the same parent. For local class methods, this doesn't
18228 apply; we just use the old DIE. */
18229 if ((is_cu_die (old_die->die_parent) || context_die == NULL)
18230 && (DECL_ARTIFICIAL (decl)
18231 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
18232 && (get_AT_unsigned (old_die, DW_AT_decl_line)
18233 == (unsigned) s.line))))
18235 subr_die = old_die;
18237 /* Clear out the declaration attribute and the formal parameters.
18238 Do not remove all children, because it is possible that this
18239 declaration die was forced using force_decl_die(). In such
18240 cases die that forced declaration die (e.g. TAG_imported_module)
18241 is one of the children that we do not want to remove. */
18242 remove_AT (subr_die, DW_AT_declaration);
18243 remove_AT (subr_die, DW_AT_object_pointer);
18244 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
18246 else
18248 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18249 add_AT_specification (subr_die, old_die);
18250 add_pubname (decl, subr_die);
18251 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18252 add_AT_file (subr_die, DW_AT_decl_file, file_index);
18253 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18254 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
18256 /* If the prototype had an 'auto' or 'decltype(auto)' return type,
18257 emit the real type on the definition die. */
18258 if (is_cxx() && debug_info_level > DINFO_LEVEL_TERSE)
18260 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
18261 if (die == auto_die || die == decltype_auto_die)
18262 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18263 TYPE_UNQUALIFIED, context_die);
18267 else
18269 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18271 if (TREE_PUBLIC (decl))
18272 add_AT_flag (subr_die, DW_AT_external, 1);
18274 add_name_and_src_coords_attributes (subr_die, decl);
18275 add_pubname (decl, subr_die);
18276 if (debug_info_level > DINFO_LEVEL_TERSE)
18278 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
18279 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18280 TYPE_UNQUALIFIED, context_die);
18283 add_pure_or_virtual_attribute (subr_die, decl);
18284 if (DECL_ARTIFICIAL (decl))
18285 add_AT_flag (subr_die, DW_AT_artificial, 1);
18287 add_accessibility_attribute (subr_die, decl);
18290 if (declaration)
18292 if (!old_die || !get_AT (old_die, DW_AT_inline))
18294 add_AT_flag (subr_die, DW_AT_declaration, 1);
18296 /* If this is an explicit function declaration then generate
18297 a DW_AT_explicit attribute. */
18298 if (lang_hooks.decls.function_decl_explicit_p (decl)
18299 && (dwarf_version >= 3 || !dwarf_strict))
18300 add_AT_flag (subr_die, DW_AT_explicit, 1);
18302 /* The first time we see a member function, it is in the context of
18303 the class to which it belongs. We make sure of this by emitting
18304 the class first. The next time is the definition, which is
18305 handled above. The two may come from the same source text.
18307 Note that force_decl_die() forces function declaration die. It is
18308 later reused to represent definition. */
18309 equate_decl_number_to_die (decl, subr_die);
18312 else if (DECL_ABSTRACT_P (decl))
18314 if (DECL_DECLARED_INLINE_P (decl))
18316 if (cgraph_function_possibly_inlined_p (decl))
18317 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
18318 else
18319 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
18321 else
18323 if (cgraph_function_possibly_inlined_p (decl))
18324 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
18325 else
18326 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
18329 if (DECL_DECLARED_INLINE_P (decl)
18330 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
18331 add_AT_flag (subr_die, DW_AT_artificial, 1);
18333 equate_decl_number_to_die (decl, subr_die);
18335 else if (!DECL_EXTERNAL (decl))
18337 HOST_WIDE_INT cfa_fb_offset;
18338 struct function *fun = DECL_STRUCT_FUNCTION (decl);
18340 if (!old_die || !get_AT (old_die, DW_AT_inline))
18341 equate_decl_number_to_die (decl, subr_die);
18343 gcc_checking_assert (fun);
18344 if (!flag_reorder_blocks_and_partition)
18346 dw_fde_ref fde = fun->fde;
18347 if (fde->dw_fde_begin)
18349 /* We have already generated the labels. */
18350 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18351 fde->dw_fde_end, false);
18353 else
18355 /* Create start/end labels and add the range. */
18356 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
18357 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
18358 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
18359 current_function_funcdef_no);
18360 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
18361 current_function_funcdef_no);
18362 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
18363 false);
18366 #if VMS_DEBUGGING_INFO
18367 /* HP OpenVMS Industry Standard 64: DWARF Extensions
18368 Section 2.3 Prologue and Epilogue Attributes:
18369 When a breakpoint is set on entry to a function, it is generally
18370 desirable for execution to be suspended, not on the very first
18371 instruction of the function, but rather at a point after the
18372 function's frame has been set up, after any language defined local
18373 declaration processing has been completed, and before execution of
18374 the first statement of the function begins. Debuggers generally
18375 cannot properly determine where this point is. Similarly for a
18376 breakpoint set on exit from a function. The prologue and epilogue
18377 attributes allow a compiler to communicate the location(s) to use. */
18380 if (fde->dw_fde_vms_end_prologue)
18381 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
18382 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
18384 if (fde->dw_fde_vms_begin_epilogue)
18385 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
18386 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
18388 #endif
18391 else
18393 /* Generate pubnames entries for the split function code ranges. */
18394 dw_fde_ref fde = fun->fde;
18396 if (fde->dw_fde_second_begin)
18398 if (dwarf_version >= 3 || !dwarf_strict)
18400 /* We should use ranges for non-contiguous code section
18401 addresses. Use the actual code range for the initial
18402 section, since the HOT/COLD labels might precede an
18403 alignment offset. */
18404 bool range_list_added = false;
18405 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
18406 fde->dw_fde_end, &range_list_added,
18407 false);
18408 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
18409 fde->dw_fde_second_end,
18410 &range_list_added, false);
18411 if (range_list_added)
18412 add_ranges (NULL);
18414 else
18416 /* There is no real support in DW2 for this .. so we make
18417 a work-around. First, emit the pub name for the segment
18418 containing the function label. Then make and emit a
18419 simplified subprogram DIE for the second segment with the
18420 name pre-fixed by __hot/cold_sect_of_. We use the same
18421 linkage name for the second die so that gdb will find both
18422 sections when given "b foo". */
18423 const char *name = NULL;
18424 tree decl_name = DECL_NAME (decl);
18425 dw_die_ref seg_die;
18427 /* Do the 'primary' section. */
18428 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18429 fde->dw_fde_end, false);
18431 /* Build a minimal DIE for the secondary section. */
18432 seg_die = new_die (DW_TAG_subprogram,
18433 subr_die->die_parent, decl);
18435 if (TREE_PUBLIC (decl))
18436 add_AT_flag (seg_die, DW_AT_external, 1);
18438 if (decl_name != NULL
18439 && IDENTIFIER_POINTER (decl_name) != NULL)
18441 name = dwarf2_name (decl, 1);
18442 if (! DECL_ARTIFICIAL (decl))
18443 add_src_coords_attributes (seg_die, decl);
18445 add_linkage_name (seg_die, decl);
18447 gcc_assert (name != NULL);
18448 add_pure_or_virtual_attribute (seg_die, decl);
18449 if (DECL_ARTIFICIAL (decl))
18450 add_AT_flag (seg_die, DW_AT_artificial, 1);
18452 name = concat ("__second_sect_of_", name, NULL);
18453 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
18454 fde->dw_fde_second_end, false);
18455 add_name_attribute (seg_die, name);
18456 if (want_pubnames ())
18457 add_pubname_string (name, seg_die);
18460 else
18461 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
18462 false);
18465 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
18467 /* We define the "frame base" as the function's CFA. This is more
18468 convenient for several reasons: (1) It's stable across the prologue
18469 and epilogue, which makes it better than just a frame pointer,
18470 (2) With dwarf3, there exists a one-byte encoding that allows us
18471 to reference the .debug_frame data by proxy, but failing that,
18472 (3) We can at least reuse the code inspection and interpretation
18473 code that determines the CFA position at various points in the
18474 function. */
18475 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
18477 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
18478 add_AT_loc (subr_die, DW_AT_frame_base, op);
18480 else
18482 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
18483 if (list->dw_loc_next)
18484 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
18485 else
18486 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
18489 /* Compute a displacement from the "steady-state frame pointer" to
18490 the CFA. The former is what all stack slots and argument slots
18491 will reference in the rtl; the latter is what we've told the
18492 debugger about. We'll need to adjust all frame_base references
18493 by this displacement. */
18494 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
18496 if (fun->static_chain_decl)
18497 add_AT_location_description (subr_die, DW_AT_static_link,
18498 loc_list_from_tree (fun->static_chain_decl, 2));
18501 /* Generate child dies for template paramaters. */
18502 if (debug_info_level > DINFO_LEVEL_TERSE)
18503 gen_generic_params_dies (decl);
18505 /* Now output descriptions of the arguments for this function. This gets
18506 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
18507 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
18508 `...' at the end of the formal parameter list. In order to find out if
18509 there was a trailing ellipsis or not, we must instead look at the type
18510 associated with the FUNCTION_DECL. This will be a node of type
18511 FUNCTION_TYPE. If the chain of type nodes hanging off of this
18512 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
18513 an ellipsis at the end. */
18515 /* In the case where we are describing a mere function declaration, all we
18516 need to do here (and all we *can* do here) is to describe the *types* of
18517 its formal parameters. */
18518 if (debug_info_level <= DINFO_LEVEL_TERSE)
18520 else if (declaration)
18521 gen_formal_types_die (decl, subr_die);
18522 else
18524 /* Generate DIEs to represent all known formal parameters. */
18525 tree parm = DECL_ARGUMENTS (decl);
18526 tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
18527 tree generic_decl_parm = generic_decl
18528 ? DECL_ARGUMENTS (generic_decl)
18529 : NULL;
18531 /* Now we want to walk the list of parameters of the function and
18532 emit their relevant DIEs.
18534 We consider the case of DECL being an instance of a generic function
18535 as well as it being a normal function.
18537 If DECL is an instance of a generic function we walk the
18538 parameters of the generic function declaration _and_ the parameters of
18539 DECL itself. This is useful because we want to emit specific DIEs for
18540 function parameter packs and those are declared as part of the
18541 generic function declaration. In that particular case,
18542 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
18543 That DIE has children DIEs representing the set of arguments
18544 of the pack. Note that the set of pack arguments can be empty.
18545 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
18546 children DIE.
18548 Otherwise, we just consider the parameters of DECL. */
18549 while (generic_decl_parm || parm)
18551 if (generic_decl_parm
18552 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
18553 gen_formal_parameter_pack_die (generic_decl_parm,
18554 parm, subr_die,
18555 &parm);
18556 else if (parm)
18558 dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
18560 if (parm == DECL_ARGUMENTS (decl)
18561 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
18562 && parm_die
18563 && (dwarf_version >= 3 || !dwarf_strict))
18564 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
18566 parm = DECL_CHAIN (parm);
18569 if (generic_decl_parm)
18570 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
18573 /* Decide whether we need an unspecified_parameters DIE at the end.
18574 There are 2 more cases to do this for: 1) the ansi ... declaration -
18575 this is detectable when the end of the arg list is not a
18576 void_type_node 2) an unprototyped function declaration (not a
18577 definition). This just means that we have no info about the
18578 parameters at all. */
18579 if (prototype_p (TREE_TYPE (decl)))
18581 /* This is the prototyped case, check for.... */
18582 if (stdarg_p (TREE_TYPE (decl)))
18583 gen_unspecified_parameters_die (decl, subr_die);
18585 else if (DECL_INITIAL (decl) == NULL_TREE)
18586 gen_unspecified_parameters_die (decl, subr_die);
18589 /* Output Dwarf info for all of the stuff within the body of the function
18590 (if it has one - it may be just a declaration). */
18591 outer_scope = DECL_INITIAL (decl);
18593 /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
18594 a function. This BLOCK actually represents the outermost binding contour
18595 for the function, i.e. the contour in which the function's formal
18596 parameters and labels get declared. Curiously, it appears that the front
18597 end doesn't actually put the PARM_DECL nodes for the current function onto
18598 the BLOCK_VARS list for this outer scope, but are strung off of the
18599 DECL_ARGUMENTS list for the function instead.
18601 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
18602 the LABEL_DECL nodes for the function however, and we output DWARF info
18603 for those in decls_for_scope. Just within the `outer_scope' there will be
18604 a BLOCK node representing the function's outermost pair of curly braces,
18605 and any blocks used for the base and member initializers of a C++
18606 constructor function. */
18607 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
18609 int call_site_note_count = 0;
18610 int tail_call_site_note_count = 0;
18612 /* Emit a DW_TAG_variable DIE for a named return value. */
18613 if (DECL_NAME (DECL_RESULT (decl)))
18614 gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
18616 current_function_has_inlines = 0;
18617 decls_for_scope (outer_scope, subr_die, 0);
18619 if (call_arg_locations && !dwarf_strict)
18621 struct call_arg_loc_node *ca_loc;
18622 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
18624 dw_die_ref die = NULL;
18625 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
18626 rtx arg, next_arg;
18628 for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
18629 arg; arg = next_arg)
18631 dw_loc_descr_ref reg, val;
18632 enum machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
18633 dw_die_ref cdie, tdie = NULL;
18635 next_arg = XEXP (arg, 1);
18636 if (REG_P (XEXP (XEXP (arg, 0), 0))
18637 && next_arg
18638 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
18639 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
18640 && REGNO (XEXP (XEXP (arg, 0), 0))
18641 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
18642 next_arg = XEXP (next_arg, 1);
18643 if (mode == VOIDmode)
18645 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
18646 if (mode == VOIDmode)
18647 mode = GET_MODE (XEXP (arg, 0));
18649 if (mode == VOIDmode || mode == BLKmode)
18650 continue;
18651 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
18653 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18654 tloc = XEXP (XEXP (arg, 0), 1);
18655 continue;
18657 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
18658 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
18660 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18661 tlocc = XEXP (XEXP (arg, 0), 1);
18662 continue;
18664 reg = NULL;
18665 if (REG_P (XEXP (XEXP (arg, 0), 0)))
18666 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
18667 VAR_INIT_STATUS_INITIALIZED);
18668 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
18670 rtx mem = XEXP (XEXP (arg, 0), 0);
18671 reg = mem_loc_descriptor (XEXP (mem, 0),
18672 get_address_mode (mem),
18673 GET_MODE (mem),
18674 VAR_INIT_STATUS_INITIALIZED);
18676 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
18677 == DEBUG_PARAMETER_REF)
18679 tree tdecl
18680 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
18681 tdie = lookup_decl_die (tdecl);
18682 if (tdie == NULL)
18683 continue;
18685 else
18686 continue;
18687 if (reg == NULL
18688 && GET_CODE (XEXP (XEXP (arg, 0), 0))
18689 != DEBUG_PARAMETER_REF)
18690 continue;
18691 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
18692 VOIDmode,
18693 VAR_INIT_STATUS_INITIALIZED);
18694 if (val == NULL)
18695 continue;
18696 if (die == NULL)
18697 die = gen_call_site_die (decl, subr_die, ca_loc);
18698 cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
18699 NULL_TREE);
18700 if (reg != NULL)
18701 add_AT_loc (cdie, DW_AT_location, reg);
18702 else if (tdie != NULL)
18703 add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
18704 add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
18705 if (next_arg != XEXP (arg, 1))
18707 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
18708 if (mode == VOIDmode)
18709 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
18710 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
18711 0), 1),
18712 mode, VOIDmode,
18713 VAR_INIT_STATUS_INITIALIZED);
18714 if (val != NULL)
18715 add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
18718 if (die == NULL
18719 && (ca_loc->symbol_ref || tloc))
18720 die = gen_call_site_die (decl, subr_die, ca_loc);
18721 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
18723 dw_loc_descr_ref tval = NULL;
18725 if (tloc != NULL_RTX)
18726 tval = mem_loc_descriptor (tloc,
18727 GET_MODE (tloc) == VOIDmode
18728 ? Pmode : GET_MODE (tloc),
18729 VOIDmode,
18730 VAR_INIT_STATUS_INITIALIZED);
18731 if (tval)
18732 add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
18733 else if (tlocc != NULL_RTX)
18735 tval = mem_loc_descriptor (tlocc,
18736 GET_MODE (tlocc) == VOIDmode
18737 ? Pmode : GET_MODE (tlocc),
18738 VOIDmode,
18739 VAR_INIT_STATUS_INITIALIZED);
18740 if (tval)
18741 add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
18742 tval);
18745 if (die != NULL)
18747 call_site_note_count++;
18748 if (ca_loc->tail_call_p)
18749 tail_call_site_note_count++;
18753 call_arg_locations = NULL;
18754 call_arg_loc_last = NULL;
18755 if (tail_call_site_count >= 0
18756 && tail_call_site_count == tail_call_site_note_count
18757 && !dwarf_strict)
18759 if (call_site_count >= 0
18760 && call_site_count == call_site_note_count)
18761 add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
18762 else
18763 add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
18765 call_site_count = -1;
18766 tail_call_site_count = -1;
18769 if (subr_die != old_die)
18770 /* Add the calling convention attribute if requested. */
18771 add_calling_convention_attribute (subr_die, decl);
18774 /* Returns a hash value for X (which really is a die_struct). */
18776 static hashval_t
18777 common_block_die_table_hash (const void *x)
18779 const_dw_die_ref d = (const_dw_die_ref) x;
18780 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
18783 /* Return nonzero if decl_id and die_parent of die_struct X is the same
18784 as decl_id and die_parent of die_struct Y. */
18786 static int
18787 common_block_die_table_eq (const void *x, const void *y)
18789 const_dw_die_ref d = (const_dw_die_ref) x;
18790 const_dw_die_ref e = (const_dw_die_ref) y;
18791 return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
18794 /* Generate a DIE to represent a declared data object.
18795 Either DECL or ORIGIN must be non-null. */
18797 static void
18798 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
18800 HOST_WIDE_INT off = 0;
18801 tree com_decl;
18802 tree decl_or_origin = decl ? decl : origin;
18803 tree ultimate_origin;
18804 dw_die_ref var_die;
18805 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
18806 dw_die_ref origin_die;
18807 bool declaration = (DECL_EXTERNAL (decl_or_origin)
18808 || class_or_namespace_scope_p (context_die));
18809 bool specialization_p = false;
18811 ultimate_origin = decl_ultimate_origin (decl_or_origin);
18812 if (decl || ultimate_origin)
18813 origin = ultimate_origin;
18814 com_decl = fortran_common (decl_or_origin, &off);
18816 /* Symbol in common gets emitted as a child of the common block, in the form
18817 of a data member. */
18818 if (com_decl)
18820 dw_die_ref com_die;
18821 dw_loc_list_ref loc;
18822 die_node com_die_arg;
18824 var_die = lookup_decl_die (decl_or_origin);
18825 if (var_die)
18827 if (get_AT (var_die, DW_AT_location) == NULL)
18829 loc = loc_list_from_tree (com_decl, off ? 1 : 2);
18830 if (loc)
18832 if (off)
18834 /* Optimize the common case. */
18835 if (single_element_loc_list_p (loc)
18836 && loc->expr->dw_loc_opc == DW_OP_addr
18837 && loc->expr->dw_loc_next == NULL
18838 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
18839 == SYMBOL_REF)
18841 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
18842 loc->expr->dw_loc_oprnd1.v.val_addr
18843 = plus_constant (GET_MODE (x), x , off);
18845 else
18846 loc_list_plus_const (loc, off);
18848 add_AT_location_description (var_die, DW_AT_location, loc);
18849 remove_AT (var_die, DW_AT_declaration);
18852 return;
18855 if (common_block_die_table == NULL)
18856 common_block_die_table
18857 = htab_create_ggc (10, common_block_die_table_hash,
18858 common_block_die_table_eq, NULL);
18860 com_die_arg.decl_id = DECL_UID (com_decl);
18861 com_die_arg.die_parent = context_die;
18862 com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
18863 loc = loc_list_from_tree (com_decl, 2);
18864 if (com_die == NULL)
18866 const char *cnam
18867 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
18868 void **slot;
18870 com_die = new_die (DW_TAG_common_block, context_die, decl);
18871 add_name_and_src_coords_attributes (com_die, com_decl);
18872 if (loc)
18874 add_AT_location_description (com_die, DW_AT_location, loc);
18875 /* Avoid sharing the same loc descriptor between
18876 DW_TAG_common_block and DW_TAG_variable. */
18877 loc = loc_list_from_tree (com_decl, 2);
18879 else if (DECL_EXTERNAL (decl))
18880 add_AT_flag (com_die, DW_AT_declaration, 1);
18881 if (want_pubnames ())
18882 add_pubname_string (cnam, com_die); /* ??? needed? */
18883 com_die->decl_id = DECL_UID (com_decl);
18884 slot = htab_find_slot (common_block_die_table, com_die, INSERT);
18885 *slot = (void *) com_die;
18887 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
18889 add_AT_location_description (com_die, DW_AT_location, loc);
18890 loc = loc_list_from_tree (com_decl, 2);
18891 remove_AT (com_die, DW_AT_declaration);
18893 var_die = new_die (DW_TAG_variable, com_die, decl);
18894 add_name_and_src_coords_attributes (var_die, decl);
18895 add_type_attribute (var_die, TREE_TYPE (decl), decl_quals (decl),
18896 context_die);
18897 add_AT_flag (var_die, DW_AT_external, 1);
18898 if (loc)
18900 if (off)
18902 /* Optimize the common case. */
18903 if (single_element_loc_list_p (loc)
18904 && loc->expr->dw_loc_opc == DW_OP_addr
18905 && loc->expr->dw_loc_next == NULL
18906 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
18908 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
18909 loc->expr->dw_loc_oprnd1.v.val_addr
18910 = plus_constant (GET_MODE (x), x, off);
18912 else
18913 loc_list_plus_const (loc, off);
18915 add_AT_location_description (var_die, DW_AT_location, loc);
18917 else if (DECL_EXTERNAL (decl))
18918 add_AT_flag (var_die, DW_AT_declaration, 1);
18919 equate_decl_number_to_die (decl, var_die);
18920 return;
18923 /* If the compiler emitted a definition for the DECL declaration
18924 and if we already emitted a DIE for it, don't emit a second
18925 DIE for it again. Allow re-declarations of DECLs that are
18926 inside functions, though. */
18927 if (old_die && declaration && !local_scope_p (context_die))
18928 return;
18930 /* For static data members, the declaration in the class is supposed
18931 to have DW_TAG_member tag; the specification should still be
18932 DW_TAG_variable referencing the DW_TAG_member DIE. */
18933 if (declaration && class_scope_p (context_die))
18934 var_die = new_die (DW_TAG_member, context_die, decl);
18935 else
18936 var_die = new_die (DW_TAG_variable, context_die, decl);
18938 origin_die = NULL;
18939 if (origin != NULL)
18940 origin_die = add_abstract_origin_attribute (var_die, origin);
18942 /* Loop unrolling can create multiple blocks that refer to the same
18943 static variable, so we must test for the DW_AT_declaration flag.
18945 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
18946 copy decls and set the DECL_ABSTRACT_P flag on them instead of
18947 sharing them.
18949 ??? Duplicated blocks have been rewritten to use .debug_ranges.
18951 ??? The declare_in_namespace support causes us to get two DIEs for one
18952 variable, both of which are declarations. We want to avoid considering
18953 one to be a specification, so we must test that this DIE is not a
18954 declaration. */
18955 else if (old_die && TREE_STATIC (decl) && ! declaration
18956 && get_AT_flag (old_die, DW_AT_declaration) == 1)
18958 /* This is a definition of a C++ class level static. */
18959 add_AT_specification (var_die, old_die);
18960 specialization_p = true;
18961 if (DECL_NAME (decl))
18963 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18964 struct dwarf_file_data * file_index = lookup_filename (s.file);
18966 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18967 add_AT_file (var_die, DW_AT_decl_file, file_index);
18969 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18970 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
18972 if (old_die->die_tag == DW_TAG_member)
18973 add_linkage_name (var_die, decl);
18976 else
18977 add_name_and_src_coords_attributes (var_die, decl);
18979 if ((origin == NULL && !specialization_p)
18980 || (origin != NULL
18981 && !DECL_ABSTRACT_P (decl_or_origin)
18982 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
18983 decl_function_context
18984 (decl_or_origin))))
18986 tree type = TREE_TYPE (decl_or_origin);
18988 if (decl_by_reference_p (decl_or_origin))
18989 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
18990 context_die);
18991 else
18992 add_type_attribute (var_die, type, decl_quals (decl_or_origin),
18993 context_die);
18996 if (origin == NULL && !specialization_p)
18998 if (TREE_PUBLIC (decl))
18999 add_AT_flag (var_die, DW_AT_external, 1);
19001 if (DECL_ARTIFICIAL (decl))
19002 add_AT_flag (var_die, DW_AT_artificial, 1);
19004 add_accessibility_attribute (var_die, decl);
19007 if (declaration)
19008 add_AT_flag (var_die, DW_AT_declaration, 1);
19010 if (decl && (DECL_ABSTRACT_P (decl) || declaration || old_die == NULL))
19011 equate_decl_number_to_die (decl, var_die);
19013 if (! declaration
19014 && (! DECL_ABSTRACT_P (decl_or_origin)
19015 /* Local static vars are shared between all clones/inlines,
19016 so emit DW_AT_location on the abstract DIE if DECL_RTL is
19017 already set. */
19018 || (TREE_CODE (decl_or_origin) == VAR_DECL
19019 && TREE_STATIC (decl_or_origin)
19020 && DECL_RTL_SET_P (decl_or_origin)))
19021 /* When abstract origin already has DW_AT_location attribute, no need
19022 to add it again. */
19023 && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
19025 if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
19026 && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
19027 defer_location (decl_or_origin, var_die);
19028 else
19029 add_location_or_const_value_attribute (var_die, decl_or_origin,
19030 decl == NULL, DW_AT_location);
19031 add_pubname (decl_or_origin, var_die);
19033 else
19034 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
19037 /* Generate a DIE to represent a named constant. */
19039 static void
19040 gen_const_die (tree decl, dw_die_ref context_die)
19042 dw_die_ref const_die;
19043 tree type = TREE_TYPE (decl);
19045 const_die = new_die (DW_TAG_constant, context_die, decl);
19046 add_name_and_src_coords_attributes (const_die, decl);
19047 add_type_attribute (const_die, type, TYPE_QUAL_CONST, context_die);
19048 if (TREE_PUBLIC (decl))
19049 add_AT_flag (const_die, DW_AT_external, 1);
19050 if (DECL_ARTIFICIAL (decl))
19051 add_AT_flag (const_die, DW_AT_artificial, 1);
19052 tree_add_const_value_attribute_for_decl (const_die, decl);
19055 /* Generate a DIE to represent a label identifier. */
19057 static void
19058 gen_label_die (tree decl, dw_die_ref context_die)
19060 tree origin = decl_ultimate_origin (decl);
19061 dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
19062 rtx insn;
19063 char label[MAX_ARTIFICIAL_LABEL_BYTES];
19065 if (origin != NULL)
19066 add_abstract_origin_attribute (lbl_die, origin);
19067 else
19068 add_name_and_src_coords_attributes (lbl_die, decl);
19070 if (DECL_ABSTRACT_P (decl))
19071 equate_decl_number_to_die (decl, lbl_die);
19072 else
19074 insn = DECL_RTL_IF_SET (decl);
19076 /* Deleted labels are programmer specified labels which have been
19077 eliminated because of various optimizations. We still emit them
19078 here so that it is possible to put breakpoints on them. */
19079 if (insn
19080 && (LABEL_P (insn)
19081 || ((NOTE_P (insn)
19082 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
19084 /* When optimization is enabled (via -O) some parts of the compiler
19085 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
19086 represent source-level labels which were explicitly declared by
19087 the user. This really shouldn't be happening though, so catch
19088 it if it ever does happen. */
19089 gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
19091 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
19092 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19094 else if (insn
19095 && NOTE_P (insn)
19096 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
19097 && CODE_LABEL_NUMBER (insn) != -1)
19099 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
19100 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19105 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
19106 attributes to the DIE for a block STMT, to describe where the inlined
19107 function was called from. This is similar to add_src_coords_attributes. */
19109 static inline void
19110 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
19112 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
19114 if (dwarf_version >= 3 || !dwarf_strict)
19116 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
19117 add_AT_unsigned (die, DW_AT_call_line, s.line);
19122 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
19123 Add low_pc and high_pc attributes to the DIE for a block STMT. */
19125 static inline void
19126 add_high_low_attributes (tree stmt, dw_die_ref die)
19128 char label[MAX_ARTIFICIAL_LABEL_BYTES];
19130 if (BLOCK_FRAGMENT_CHAIN (stmt)
19131 && (dwarf_version >= 3 || !dwarf_strict))
19133 tree chain, superblock = NULL_TREE;
19134 dw_die_ref pdie;
19135 dw_attr_ref attr = NULL;
19137 if (inlined_function_outer_scope_p (stmt))
19139 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19140 BLOCK_NUMBER (stmt));
19141 add_AT_lbl_id (die, DW_AT_entry_pc, label);
19144 /* Optimize duplicate .debug_ranges lists or even tails of
19145 lists. If this BLOCK has same ranges as its supercontext,
19146 lookup DW_AT_ranges attribute in the supercontext (and
19147 recursively so), verify that the ranges_table contains the
19148 right values and use it instead of adding a new .debug_range. */
19149 for (chain = stmt, pdie = die;
19150 BLOCK_SAME_RANGE (chain);
19151 chain = BLOCK_SUPERCONTEXT (chain))
19153 dw_attr_ref new_attr;
19155 pdie = pdie->die_parent;
19156 if (pdie == NULL)
19157 break;
19158 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
19159 break;
19160 new_attr = get_AT (pdie, DW_AT_ranges);
19161 if (new_attr == NULL
19162 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
19163 break;
19164 attr = new_attr;
19165 superblock = BLOCK_SUPERCONTEXT (chain);
19167 if (attr != NULL
19168 && (ranges_table[attr->dw_attr_val.v.val_offset
19169 / 2 / DWARF2_ADDR_SIZE].num
19170 == BLOCK_NUMBER (superblock))
19171 && BLOCK_FRAGMENT_CHAIN (superblock))
19173 unsigned long off = attr->dw_attr_val.v.val_offset
19174 / 2 / DWARF2_ADDR_SIZE;
19175 unsigned long supercnt = 0, thiscnt = 0;
19176 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
19177 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
19179 ++supercnt;
19180 gcc_checking_assert (ranges_table[off + supercnt].num
19181 == BLOCK_NUMBER (chain));
19183 gcc_checking_assert (ranges_table[off + supercnt + 1].num == 0);
19184 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
19185 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
19186 ++thiscnt;
19187 gcc_assert (supercnt >= thiscnt);
19188 add_AT_range_list (die, DW_AT_ranges,
19189 ((off + supercnt - thiscnt)
19190 * 2 * DWARF2_ADDR_SIZE),
19191 false);
19192 return;
19195 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt), false);
19197 chain = BLOCK_FRAGMENT_CHAIN (stmt);
19200 add_ranges (chain);
19201 chain = BLOCK_FRAGMENT_CHAIN (chain);
19203 while (chain);
19204 add_ranges (NULL);
19206 else
19208 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
19209 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19210 BLOCK_NUMBER (stmt));
19211 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
19212 BLOCK_NUMBER (stmt));
19213 add_AT_low_high_pc (die, label, label_high, false);
19217 /* Generate a DIE for a lexical block. */
19219 static void
19220 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
19222 dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
19224 if (call_arg_locations)
19226 if (block_map.length () <= BLOCK_NUMBER (stmt))
19227 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
19228 block_map[BLOCK_NUMBER (stmt)] = stmt_die;
19231 if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
19232 add_high_low_attributes (stmt, stmt_die);
19234 decls_for_scope (stmt, stmt_die, depth);
19237 /* Generate a DIE for an inlined subprogram. */
19239 static void
19240 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
19242 tree decl;
19244 /* The instance of function that is effectively being inlined shall not
19245 be abstract. */
19246 gcc_assert (! BLOCK_ABSTRACT (stmt));
19248 decl = block_ultimate_origin (stmt);
19250 /* Emit info for the abstract instance first, if we haven't yet. We
19251 must emit this even if the block is abstract, otherwise when we
19252 emit the block below (or elsewhere), we may end up trying to emit
19253 a die whose origin die hasn't been emitted, and crashing. */
19254 dwarf2out_abstract_function (decl);
19256 if (! BLOCK_ABSTRACT (stmt))
19258 dw_die_ref subr_die
19259 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
19261 if (call_arg_locations)
19263 if (block_map.length () <= BLOCK_NUMBER (stmt))
19264 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
19265 block_map[BLOCK_NUMBER (stmt)] = subr_die;
19267 add_abstract_origin_attribute (subr_die, decl);
19268 if (TREE_ASM_WRITTEN (stmt))
19269 add_high_low_attributes (stmt, subr_die);
19270 add_call_src_coords_attributes (stmt, subr_die);
19272 decls_for_scope (stmt, subr_die, depth);
19273 current_function_has_inlines = 1;
19277 /* Generate a DIE for a field in a record, or structure. */
19279 static void
19280 gen_field_die (tree decl, dw_die_ref context_die)
19282 dw_die_ref decl_die;
19284 if (TREE_TYPE (decl) == error_mark_node)
19285 return;
19287 decl_die = new_die (DW_TAG_member, context_die, decl);
19288 add_name_and_src_coords_attributes (decl_die, decl);
19289 add_type_attribute (decl_die, member_declared_type (decl),
19290 decl_quals (decl), context_die);
19292 if (DECL_BIT_FIELD_TYPE (decl))
19294 add_byte_size_attribute (decl_die, decl);
19295 add_bit_size_attribute (decl_die, decl);
19296 add_bit_offset_attribute (decl_die, decl);
19299 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
19300 add_data_member_location_attribute (decl_die, decl);
19302 if (DECL_ARTIFICIAL (decl))
19303 add_AT_flag (decl_die, DW_AT_artificial, 1);
19305 add_accessibility_attribute (decl_die, decl);
19307 /* Equate decl number to die, so that we can look up this decl later on. */
19308 equate_decl_number_to_die (decl, decl_die);
19311 #if 0
19312 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19313 Use modified_type_die instead.
19314 We keep this code here just in case these types of DIEs may be needed to
19315 represent certain things in other languages (e.g. Pascal) someday. */
19317 static void
19318 gen_pointer_type_die (tree type, dw_die_ref context_die)
19320 dw_die_ref ptr_die
19321 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
19323 equate_type_number_to_die (type, ptr_die);
19324 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19325 context_die);
19326 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19329 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19330 Use modified_type_die instead.
19331 We keep this code here just in case these types of DIEs may be needed to
19332 represent certain things in other languages (e.g. Pascal) someday. */
19334 static void
19335 gen_reference_type_die (tree type, dw_die_ref context_die)
19337 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
19339 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
19340 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
19341 else
19342 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
19344 equate_type_number_to_die (type, ref_die);
19345 add_type_attribute (ref_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19346 context_die);
19347 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19349 #endif
19351 /* Generate a DIE for a pointer to a member type. */
19353 static void
19354 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
19356 dw_die_ref ptr_die
19357 = new_die (DW_TAG_ptr_to_member_type,
19358 scope_die_for (type, context_die), type);
19360 equate_type_number_to_die (type, ptr_die);
19361 add_AT_die_ref (ptr_die, DW_AT_containing_type,
19362 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
19363 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19364 context_die);
19367 typedef const char *dchar_p; /* For DEF_VEC_P. */
19369 static char *producer_string;
19371 /* Return a heap allocated producer string including command line options
19372 if -grecord-gcc-switches. */
19374 static char *
19375 gen_producer_string (void)
19377 size_t j;
19378 auto_vec<dchar_p> switches;
19379 const char *language_string = lang_hooks.name;
19380 char *producer, *tail;
19381 const char *p;
19382 size_t len = dwarf_record_gcc_switches ? 0 : 3;
19383 size_t plen = strlen (language_string) + 1 + strlen (version_string);
19385 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
19386 switch (save_decoded_options[j].opt_index)
19388 case OPT_o:
19389 case OPT_d:
19390 case OPT_dumpbase:
19391 case OPT_dumpdir:
19392 case OPT_auxbase:
19393 case OPT_auxbase_strip:
19394 case OPT_quiet:
19395 case OPT_version:
19396 case OPT_v:
19397 case OPT_w:
19398 case OPT_L:
19399 case OPT_D:
19400 case OPT_I:
19401 case OPT_U:
19402 case OPT_SPECIAL_unknown:
19403 case OPT_SPECIAL_ignore:
19404 case OPT_SPECIAL_program_name:
19405 case OPT_SPECIAL_input_file:
19406 case OPT_grecord_gcc_switches:
19407 case OPT_gno_record_gcc_switches:
19408 case OPT__output_pch_:
19409 case OPT_fdiagnostics_show_location_:
19410 case OPT_fdiagnostics_show_option:
19411 case OPT_fdiagnostics_show_caret:
19412 case OPT_fdiagnostics_color_:
19413 case OPT_fverbose_asm:
19414 case OPT____:
19415 case OPT__sysroot_:
19416 case OPT_nostdinc:
19417 case OPT_nostdinc__:
19418 /* Ignore these. */
19419 continue;
19420 default:
19421 if (cl_options[save_decoded_options[j].opt_index].flags
19422 & CL_NO_DWARF_RECORD)
19423 continue;
19424 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
19425 == '-');
19426 switch (save_decoded_options[j].canonical_option[0][1])
19428 case 'M':
19429 case 'i':
19430 case 'W':
19431 continue;
19432 case 'f':
19433 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
19434 "dump", 4) == 0)
19435 continue;
19436 break;
19437 default:
19438 break;
19440 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
19441 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
19442 break;
19445 producer = XNEWVEC (char, plen + 1 + len + 1);
19446 tail = producer;
19447 sprintf (tail, "%s %s", language_string, version_string);
19448 tail += plen;
19450 FOR_EACH_VEC_ELT (switches, j, p)
19452 len = strlen (p);
19453 *tail = ' ';
19454 memcpy (tail + 1, p, len);
19455 tail += len + 1;
19458 *tail = '\0';
19459 return producer;
19462 /* Generate the DIE for the compilation unit. */
19464 static dw_die_ref
19465 gen_compile_unit_die (const char *filename)
19467 dw_die_ref die;
19468 const char *language_string = lang_hooks.name;
19469 int language;
19471 die = new_die (DW_TAG_compile_unit, NULL, NULL);
19473 if (filename)
19475 add_name_attribute (die, filename);
19476 /* Don't add cwd for <built-in>. */
19477 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
19478 add_comp_dir_attribute (die);
19481 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
19483 /* If our producer is LTO try to figure out a common language to use
19484 from the global list of translation units. */
19485 if (strcmp (language_string, "GNU GIMPLE") == 0)
19487 unsigned i;
19488 tree t;
19489 const char *common_lang = NULL;
19491 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
19493 if (!TRANSLATION_UNIT_LANGUAGE (t))
19494 continue;
19495 if (!common_lang)
19496 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
19497 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
19499 else if (strncmp (common_lang, "GNU C", 5) == 0
19500 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
19501 /* Mixing C and C++ is ok, use C++ in that case. */
19502 common_lang = "GNU C++";
19503 else
19505 /* Fall back to C. */
19506 common_lang = NULL;
19507 break;
19511 if (common_lang)
19512 language_string = common_lang;
19515 language = DW_LANG_C89;
19516 if (strcmp (language_string, "GNU C++") == 0)
19517 language = DW_LANG_C_plus_plus;
19518 else if (strcmp (language_string, "GNU F77") == 0)
19519 language = DW_LANG_Fortran77;
19520 else if (strcmp (language_string, "GNU Pascal") == 0)
19521 language = DW_LANG_Pascal83;
19522 else if (dwarf_version >= 3 || !dwarf_strict)
19524 if (strcmp (language_string, "GNU Ada") == 0)
19525 language = DW_LANG_Ada95;
19526 else if (strcmp (language_string, "GNU Fortran") == 0)
19527 language = DW_LANG_Fortran95;
19528 else if (strcmp (language_string, "GNU Java") == 0)
19529 language = DW_LANG_Java;
19530 else if (strcmp (language_string, "GNU Objective-C") == 0)
19531 language = DW_LANG_ObjC;
19532 else if (strcmp (language_string, "GNU Objective-C++") == 0)
19533 language = DW_LANG_ObjC_plus_plus;
19534 else if (dwarf_version >= 5 || !dwarf_strict)
19536 if (strcmp (language_string, "GNU Go") == 0)
19537 language = DW_LANG_Go;
19540 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
19541 else if (strcmp (language_string, "GNU Fortran") == 0)
19542 language = DW_LANG_Fortran90;
19544 add_AT_unsigned (die, DW_AT_language, language);
19546 switch (language)
19548 case DW_LANG_Fortran77:
19549 case DW_LANG_Fortran90:
19550 case DW_LANG_Fortran95:
19551 /* Fortran has case insensitive identifiers and the front-end
19552 lowercases everything. */
19553 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
19554 break;
19555 default:
19556 /* The default DW_ID_case_sensitive doesn't need to be specified. */
19557 break;
19559 return die;
19562 /* Generate the DIE for a base class. */
19564 static void
19565 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
19567 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
19569 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, context_die);
19570 add_data_member_location_attribute (die, binfo);
19572 if (BINFO_VIRTUAL_P (binfo))
19573 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
19575 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
19576 children, otherwise the default is DW_ACCESS_public. In DWARF2
19577 the default has always been DW_ACCESS_private. */
19578 if (access == access_public_node)
19580 if (dwarf_version == 2
19581 || context_die->die_tag == DW_TAG_class_type)
19582 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
19584 else if (access == access_protected_node)
19585 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
19586 else if (dwarf_version > 2
19587 && context_die->die_tag != DW_TAG_class_type)
19588 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
19591 /* Generate a DIE for a class member. */
19593 static void
19594 gen_member_die (tree type, dw_die_ref context_die)
19596 tree member;
19597 tree binfo = TYPE_BINFO (type);
19598 dw_die_ref child;
19600 /* If this is not an incomplete type, output descriptions of each of its
19601 members. Note that as we output the DIEs necessary to represent the
19602 members of this record or union type, we will also be trying to output
19603 DIEs to represent the *types* of those members. However the `type'
19604 function (above) will specifically avoid generating type DIEs for member
19605 types *within* the list of member DIEs for this (containing) type except
19606 for those types (of members) which are explicitly marked as also being
19607 members of this (containing) type themselves. The g++ front- end can
19608 force any given type to be treated as a member of some other (containing)
19609 type by setting the TYPE_CONTEXT of the given (member) type to point to
19610 the TREE node representing the appropriate (containing) type. */
19612 /* First output info about the base classes. */
19613 if (binfo)
19615 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
19616 int i;
19617 tree base;
19619 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
19620 gen_inheritance_die (base,
19621 (accesses ? (*accesses)[i] : access_public_node),
19622 context_die);
19625 /* Now output info about the data members and type members. */
19626 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
19628 /* If we thought we were generating minimal debug info for TYPE
19629 and then changed our minds, some of the member declarations
19630 may have already been defined. Don't define them again, but
19631 do put them in the right order. */
19633 child = lookup_decl_die (member);
19634 if (child)
19635 splice_child_die (context_die, child);
19636 else
19637 gen_decl_die (member, NULL, context_die);
19640 /* Now output info about the function members (if any). */
19641 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
19643 /* Don't include clones in the member list. */
19644 if (DECL_ABSTRACT_ORIGIN (member))
19645 continue;
19647 child = lookup_decl_die (member);
19648 if (child)
19649 splice_child_die (context_die, child);
19650 else
19651 gen_decl_die (member, NULL, context_die);
19655 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
19656 is set, we pretend that the type was never defined, so we only get the
19657 member DIEs needed by later specification DIEs. */
19659 static void
19660 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
19661 enum debug_info_usage usage)
19663 dw_die_ref type_die = lookup_type_die (type);
19664 dw_die_ref scope_die = 0;
19665 int nested = 0;
19666 int complete = (TYPE_SIZE (type)
19667 && (! TYPE_STUB_DECL (type)
19668 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
19669 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
19670 complete = complete && should_emit_struct_debug (type, usage);
19672 if (type_die && ! complete)
19673 return;
19675 if (TYPE_CONTEXT (type) != NULL_TREE
19676 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19677 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
19678 nested = 1;
19680 scope_die = scope_die_for (type, context_die);
19682 /* Generate child dies for template paramaters. */
19683 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
19684 schedule_generic_params_dies_gen (type);
19686 if (! type_die || (nested && is_cu_die (scope_die)))
19687 /* First occurrence of type or toplevel definition of nested class. */
19689 dw_die_ref old_die = type_die;
19691 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
19692 ? record_type_tag (type) : DW_TAG_union_type,
19693 scope_die, type);
19694 equate_type_number_to_die (type, type_die);
19695 if (old_die)
19696 add_AT_specification (type_die, old_die);
19697 else
19698 add_name_attribute (type_die, type_tag (type));
19700 else
19701 remove_AT (type_die, DW_AT_declaration);
19703 /* If this type has been completed, then give it a byte_size attribute and
19704 then give a list of members. */
19705 if (complete && !ns_decl)
19707 /* Prevent infinite recursion in cases where the type of some member of
19708 this type is expressed in terms of this type itself. */
19709 TREE_ASM_WRITTEN (type) = 1;
19710 add_byte_size_attribute (type_die, type);
19711 if (TYPE_STUB_DECL (type) != NULL_TREE)
19713 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
19714 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
19717 /* If the first reference to this type was as the return type of an
19718 inline function, then it may not have a parent. Fix this now. */
19719 if (type_die->die_parent == NULL)
19720 add_child_die (scope_die, type_die);
19722 push_decl_scope (type);
19723 gen_member_die (type, type_die);
19724 pop_decl_scope ();
19726 add_gnat_descriptive_type_attribute (type_die, type, context_die);
19727 if (TYPE_ARTIFICIAL (type))
19728 add_AT_flag (type_die, DW_AT_artificial, 1);
19730 /* GNU extension: Record what type our vtable lives in. */
19731 if (TYPE_VFIELD (type))
19733 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
19735 gen_type_die (vtype, context_die);
19736 add_AT_die_ref (type_die, DW_AT_containing_type,
19737 lookup_type_die (vtype));
19740 else
19742 add_AT_flag (type_die, DW_AT_declaration, 1);
19744 /* We don't need to do this for function-local types. */
19745 if (TYPE_STUB_DECL (type)
19746 && ! decl_function_context (TYPE_STUB_DECL (type)))
19747 vec_safe_push (incomplete_types, type);
19750 if (get_AT (type_die, DW_AT_name))
19751 add_pubtype (type, type_die);
19754 /* Generate a DIE for a subroutine _type_. */
19756 static void
19757 gen_subroutine_type_die (tree type, dw_die_ref context_die)
19759 tree return_type = TREE_TYPE (type);
19760 dw_die_ref subr_die
19761 = new_die (DW_TAG_subroutine_type,
19762 scope_die_for (type, context_die), type);
19764 equate_type_number_to_die (type, subr_die);
19765 add_prototyped_attribute (subr_die, type);
19766 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, context_die);
19767 gen_formal_types_die (type, subr_die);
19769 if (get_AT (subr_die, DW_AT_name))
19770 add_pubtype (type, subr_die);
19773 /* Generate a DIE for a type definition. */
19775 static void
19776 gen_typedef_die (tree decl, dw_die_ref context_die)
19778 dw_die_ref type_die;
19779 tree origin;
19781 if (TREE_ASM_WRITTEN (decl))
19782 return;
19784 TREE_ASM_WRITTEN (decl) = 1;
19785 type_die = new_die (DW_TAG_typedef, context_die, decl);
19786 origin = decl_ultimate_origin (decl);
19787 if (origin != NULL)
19788 add_abstract_origin_attribute (type_die, origin);
19789 else
19791 tree type;
19793 add_name_and_src_coords_attributes (type_die, decl);
19794 if (DECL_ORIGINAL_TYPE (decl))
19796 type = DECL_ORIGINAL_TYPE (decl);
19798 gcc_assert (type != TREE_TYPE (decl));
19799 equate_type_number_to_die (TREE_TYPE (decl), type_die);
19801 else
19803 type = TREE_TYPE (decl);
19805 if (is_naming_typedef_decl (TYPE_NAME (type)))
19807 /* Here, we are in the case of decl being a typedef naming
19808 an anonymous type, e.g:
19809 typedef struct {...} foo;
19810 In that case TREE_TYPE (decl) is not a typedef variant
19811 type and TYPE_NAME of the anonymous type is set to the
19812 TYPE_DECL of the typedef. This construct is emitted by
19813 the C++ FE.
19815 TYPE is the anonymous struct named by the typedef
19816 DECL. As we need the DW_AT_type attribute of the
19817 DW_TAG_typedef to point to the DIE of TYPE, let's
19818 generate that DIE right away. add_type_attribute
19819 called below will then pick (via lookup_type_die) that
19820 anonymous struct DIE. */
19821 if (!TREE_ASM_WRITTEN (type))
19822 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
19824 /* This is a GNU Extension. We are adding a
19825 DW_AT_linkage_name attribute to the DIE of the
19826 anonymous struct TYPE. The value of that attribute
19827 is the name of the typedef decl naming the anonymous
19828 struct. This greatly eases the work of consumers of
19829 this debug info. */
19830 add_linkage_attr (lookup_type_die (type), decl);
19834 add_type_attribute (type_die, type, decl_quals (decl), context_die);
19836 if (is_naming_typedef_decl (decl))
19837 /* We want that all subsequent calls to lookup_type_die with
19838 TYPE in argument yield the DW_TAG_typedef we have just
19839 created. */
19840 equate_type_number_to_die (type, type_die);
19842 add_accessibility_attribute (type_die, decl);
19845 if (DECL_ABSTRACT_P (decl))
19846 equate_decl_number_to_die (decl, type_die);
19848 if (get_AT (type_die, DW_AT_name))
19849 add_pubtype (decl, type_die);
19852 /* Generate a DIE for a struct, class, enum or union type. */
19854 static void
19855 gen_tagged_type_die (tree type,
19856 dw_die_ref context_die,
19857 enum debug_info_usage usage)
19859 int need_pop;
19861 if (type == NULL_TREE
19862 || !is_tagged_type (type))
19863 return;
19865 /* If this is a nested type whose containing class hasn't been written
19866 out yet, writing it out will cover this one, too. This does not apply
19867 to instantiations of member class templates; they need to be added to
19868 the containing class as they are generated. FIXME: This hurts the
19869 idea of combining type decls from multiple TUs, since we can't predict
19870 what set of template instantiations we'll get. */
19871 if (TYPE_CONTEXT (type)
19872 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19873 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
19875 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
19877 if (TREE_ASM_WRITTEN (type))
19878 return;
19880 /* If that failed, attach ourselves to the stub. */
19881 push_decl_scope (TYPE_CONTEXT (type));
19882 context_die = lookup_type_die (TYPE_CONTEXT (type));
19883 need_pop = 1;
19885 else if (TYPE_CONTEXT (type) != NULL_TREE
19886 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
19888 /* If this type is local to a function that hasn't been written
19889 out yet, use a NULL context for now; it will be fixed up in
19890 decls_for_scope. */
19891 context_die = lookup_decl_die (TYPE_CONTEXT (type));
19892 /* A declaration DIE doesn't count; nested types need to go in the
19893 specification. */
19894 if (context_die && is_declaration_die (context_die))
19895 context_die = NULL;
19896 need_pop = 0;
19898 else
19900 context_die = declare_in_namespace (type, context_die);
19901 need_pop = 0;
19904 if (TREE_CODE (type) == ENUMERAL_TYPE)
19906 /* This might have been written out by the call to
19907 declare_in_namespace. */
19908 if (!TREE_ASM_WRITTEN (type))
19909 gen_enumeration_type_die (type, context_die);
19911 else
19912 gen_struct_or_union_type_die (type, context_die, usage);
19914 if (need_pop)
19915 pop_decl_scope ();
19917 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
19918 it up if it is ever completed. gen_*_type_die will set it for us
19919 when appropriate. */
19922 /* Generate a type description DIE. */
19924 static void
19925 gen_type_die_with_usage (tree type, dw_die_ref context_die,
19926 enum debug_info_usage usage)
19928 struct array_descr_info info;
19930 if (type == NULL_TREE || type == error_mark_node)
19931 return;
19933 if (TYPE_NAME (type) != NULL_TREE
19934 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
19935 && is_redundant_typedef (TYPE_NAME (type))
19936 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
19937 /* The DECL of this type is a typedef we don't want to emit debug
19938 info for but we want debug info for its underlying typedef.
19939 This can happen for e.g, the injected-class-name of a C++
19940 type. */
19941 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
19943 /* If TYPE is a typedef type variant, let's generate debug info
19944 for the parent typedef which TYPE is a type of. */
19945 if (typedef_variant_p (type))
19947 if (TREE_ASM_WRITTEN (type))
19948 return;
19950 /* Prevent broken recursion; we can't hand off to the same type. */
19951 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
19953 /* Give typedefs the right scope. */
19954 context_die = scope_die_for (type, context_die);
19956 TREE_ASM_WRITTEN (type) = 1;
19958 gen_decl_die (TYPE_NAME (type), NULL, context_die);
19959 return;
19962 /* If type is an anonymous tagged type named by a typedef, let's
19963 generate debug info for the typedef. */
19964 if (is_naming_typedef_decl (TYPE_NAME (type)))
19966 /* Use the DIE of the containing namespace as the parent DIE of
19967 the type description DIE we want to generate. */
19968 if (DECL_CONTEXT (TYPE_NAME (type))
19969 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
19970 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
19972 gen_decl_die (TYPE_NAME (type), NULL, context_die);
19973 return;
19976 /* If this is an array type with hidden descriptor, handle it first. */
19977 if (!TREE_ASM_WRITTEN (type)
19978 && lang_hooks.types.get_array_descr_info
19979 && lang_hooks.types.get_array_descr_info (type, &info)
19980 && (dwarf_version >= 3 || !dwarf_strict))
19982 gen_descr_array_type_die (type, &info, context_die);
19983 TREE_ASM_WRITTEN (type) = 1;
19984 return;
19987 /* We are going to output a DIE to represent the unqualified version
19988 of this type (i.e. without any const or volatile qualifiers) so
19989 get the main variant (i.e. the unqualified version) of this type
19990 now. (Vectors are special because the debugging info is in the
19991 cloned type itself). */
19992 if (TREE_CODE (type) != VECTOR_TYPE)
19993 type = type_main_variant (type);
19995 if (TREE_ASM_WRITTEN (type))
19996 return;
19998 switch (TREE_CODE (type))
20000 case ERROR_MARK:
20001 break;
20003 case POINTER_TYPE:
20004 case REFERENCE_TYPE:
20005 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
20006 ensures that the gen_type_die recursion will terminate even if the
20007 type is recursive. Recursive types are possible in Ada. */
20008 /* ??? We could perhaps do this for all types before the switch
20009 statement. */
20010 TREE_ASM_WRITTEN (type) = 1;
20012 /* For these types, all that is required is that we output a DIE (or a
20013 set of DIEs) to represent the "basis" type. */
20014 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20015 DINFO_USAGE_IND_USE);
20016 break;
20018 case OFFSET_TYPE:
20019 /* This code is used for C++ pointer-to-data-member types.
20020 Output a description of the relevant class type. */
20021 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
20022 DINFO_USAGE_IND_USE);
20024 /* Output a description of the type of the object pointed to. */
20025 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20026 DINFO_USAGE_IND_USE);
20028 /* Now output a DIE to represent this pointer-to-data-member type
20029 itself. */
20030 gen_ptr_to_mbr_type_die (type, context_die);
20031 break;
20033 case FUNCTION_TYPE:
20034 /* Force out return type (in case it wasn't forced out already). */
20035 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20036 DINFO_USAGE_DIR_USE);
20037 gen_subroutine_type_die (type, context_die);
20038 break;
20040 case METHOD_TYPE:
20041 /* Force out return type (in case it wasn't forced out already). */
20042 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20043 DINFO_USAGE_DIR_USE);
20044 gen_subroutine_type_die (type, context_die);
20045 break;
20047 case ARRAY_TYPE:
20048 gen_array_type_die (type, context_die);
20049 break;
20051 case VECTOR_TYPE:
20052 gen_array_type_die (type, context_die);
20053 break;
20055 case ENUMERAL_TYPE:
20056 case RECORD_TYPE:
20057 case UNION_TYPE:
20058 case QUAL_UNION_TYPE:
20059 gen_tagged_type_die (type, context_die, usage);
20060 return;
20062 case VOID_TYPE:
20063 case INTEGER_TYPE:
20064 case REAL_TYPE:
20065 case FIXED_POINT_TYPE:
20066 case COMPLEX_TYPE:
20067 case BOOLEAN_TYPE:
20068 /* No DIEs needed for fundamental types. */
20069 break;
20071 case NULLPTR_TYPE:
20072 case LANG_TYPE:
20073 /* Just use DW_TAG_unspecified_type. */
20075 dw_die_ref type_die = lookup_type_die (type);
20076 if (type_die == NULL)
20078 tree name = TYPE_IDENTIFIER (type);
20079 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
20080 type);
20081 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
20082 equate_type_number_to_die (type, type_die);
20085 break;
20087 default:
20088 if (is_cxx_auto (type))
20090 tree name = TYPE_IDENTIFIER (type);
20091 dw_die_ref *die = (name == get_identifier ("auto")
20092 ? &auto_die : &decltype_auto_die);
20093 if (!*die)
20095 *die = new_die (DW_TAG_unspecified_type,
20096 comp_unit_die (), NULL_TREE);
20097 add_name_attribute (*die, IDENTIFIER_POINTER (name));
20099 equate_type_number_to_die (type, *die);
20100 break;
20102 gcc_unreachable ();
20105 TREE_ASM_WRITTEN (type) = 1;
20108 static void
20109 gen_type_die (tree type, dw_die_ref context_die)
20111 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
20114 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
20115 things which are local to the given block. */
20117 static void
20118 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
20120 int must_output_die = 0;
20121 bool inlined_func;
20123 /* Ignore blocks that are NULL. */
20124 if (stmt == NULL_TREE)
20125 return;
20127 inlined_func = inlined_function_outer_scope_p (stmt);
20129 /* If the block is one fragment of a non-contiguous block, do not
20130 process the variables, since they will have been done by the
20131 origin block. Do process subblocks. */
20132 if (BLOCK_FRAGMENT_ORIGIN (stmt))
20134 tree sub;
20136 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
20137 gen_block_die (sub, context_die, depth + 1);
20139 return;
20142 /* Determine if we need to output any Dwarf DIEs at all to represent this
20143 block. */
20144 if (inlined_func)
20145 /* The outer scopes for inlinings *must* always be represented. We
20146 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
20147 must_output_die = 1;
20148 else
20150 /* Determine if this block directly contains any "significant"
20151 local declarations which we will need to output DIEs for. */
20152 if (debug_info_level > DINFO_LEVEL_TERSE)
20153 /* We are not in terse mode so *any* local declaration counts
20154 as being a "significant" one. */
20155 must_output_die = ((BLOCK_VARS (stmt) != NULL
20156 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
20157 && (TREE_USED (stmt)
20158 || TREE_ASM_WRITTEN (stmt)
20159 || BLOCK_ABSTRACT (stmt)));
20160 else if ((TREE_USED (stmt)
20161 || TREE_ASM_WRITTEN (stmt)
20162 || BLOCK_ABSTRACT (stmt))
20163 && !dwarf2out_ignore_block (stmt))
20164 must_output_die = 1;
20167 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
20168 DIE for any block which contains no significant local declarations at
20169 all. Rather, in such cases we just call `decls_for_scope' so that any
20170 needed Dwarf info for any sub-blocks will get properly generated. Note
20171 that in terse mode, our definition of what constitutes a "significant"
20172 local declaration gets restricted to include only inlined function
20173 instances and local (nested) function definitions. */
20174 if (must_output_die)
20176 if (inlined_func)
20178 /* If STMT block is abstract, that means we have been called
20179 indirectly from dwarf2out_abstract_function.
20180 That function rightfully marks the descendent blocks (of
20181 the abstract function it is dealing with) as being abstract,
20182 precisely to prevent us from emitting any
20183 DW_TAG_inlined_subroutine DIE as a descendent
20184 of an abstract function instance. So in that case, we should
20185 not call gen_inlined_subroutine_die.
20187 Later though, when cgraph asks dwarf2out to emit info
20188 for the concrete instance of the function decl into which
20189 the concrete instance of STMT got inlined, the later will lead
20190 to the generation of a DW_TAG_inlined_subroutine DIE. */
20191 if (! BLOCK_ABSTRACT (stmt))
20192 gen_inlined_subroutine_die (stmt, context_die, depth);
20194 else
20195 gen_lexical_block_die (stmt, context_die, depth);
20197 else
20198 decls_for_scope (stmt, context_die, depth);
20201 /* Process variable DECL (or variable with origin ORIGIN) within
20202 block STMT and add it to CONTEXT_DIE. */
20203 static void
20204 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
20206 dw_die_ref die;
20207 tree decl_or_origin = decl ? decl : origin;
20209 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
20210 die = lookup_decl_die (decl_or_origin);
20211 else if (TREE_CODE (decl_or_origin) == TYPE_DECL
20212 && TYPE_DECL_IS_STUB (decl_or_origin))
20213 die = lookup_type_die (TREE_TYPE (decl_or_origin));
20214 else
20215 die = NULL;
20217 if (die != NULL && die->die_parent == NULL)
20218 add_child_die (context_die, die);
20219 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
20220 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
20221 stmt, context_die);
20222 else
20223 gen_decl_die (decl, origin, context_die);
20226 /* Generate all of the decls declared within a given scope and (recursively)
20227 all of its sub-blocks. */
20229 static void
20230 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
20232 tree decl;
20233 unsigned int i;
20234 tree subblocks;
20236 /* Ignore NULL blocks. */
20237 if (stmt == NULL_TREE)
20238 return;
20240 /* Output the DIEs to represent all of the data objects and typedefs
20241 declared directly within this block but not within any nested
20242 sub-blocks. Also, nested function and tag DIEs have been
20243 generated with a parent of NULL; fix that up now. We don't
20244 have to do this if we're at -g1. */
20245 if (debug_info_level > DINFO_LEVEL_TERSE)
20247 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
20248 process_scope_var (stmt, decl, NULL_TREE, context_die);
20249 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
20250 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
20251 context_die);
20254 /* Even if we're at -g1, we need to process the subblocks in order to get
20255 inlined call information. */
20257 /* Output the DIEs to represent all sub-blocks (and the items declared
20258 therein) of this block. */
20259 for (subblocks = BLOCK_SUBBLOCKS (stmt);
20260 subblocks != NULL;
20261 subblocks = BLOCK_CHAIN (subblocks))
20262 gen_block_die (subblocks, context_die, depth + 1);
20265 /* Is this a typedef we can avoid emitting? */
20267 static inline int
20268 is_redundant_typedef (const_tree decl)
20270 if (TYPE_DECL_IS_STUB (decl))
20271 return 1;
20273 if (DECL_ARTIFICIAL (decl)
20274 && DECL_CONTEXT (decl)
20275 && is_tagged_type (DECL_CONTEXT (decl))
20276 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
20277 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
20278 /* Also ignore the artificial member typedef for the class name. */
20279 return 1;
20281 return 0;
20284 /* Return TRUE if TYPE is a typedef that names a type for linkage
20285 purposes. This kind of typedefs is produced by the C++ FE for
20286 constructs like:
20288 typedef struct {...} foo;
20290 In that case, there is no typedef variant type produced for foo.
20291 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
20292 struct type. */
20294 static bool
20295 is_naming_typedef_decl (const_tree decl)
20297 if (decl == NULL_TREE
20298 || TREE_CODE (decl) != TYPE_DECL
20299 || !is_tagged_type (TREE_TYPE (decl))
20300 || DECL_IS_BUILTIN (decl)
20301 || is_redundant_typedef (decl)
20302 /* It looks like Ada produces TYPE_DECLs that are very similar
20303 to C++ naming typedefs but that have different
20304 semantics. Let's be specific to c++ for now. */
20305 || !is_cxx ())
20306 return FALSE;
20308 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
20309 && TYPE_NAME (TREE_TYPE (decl)) == decl
20310 && (TYPE_STUB_DECL (TREE_TYPE (decl))
20311 != TYPE_NAME (TREE_TYPE (decl))));
20314 /* Returns the DIE for a context. */
20316 static inline dw_die_ref
20317 get_context_die (tree context)
20319 if (context)
20321 /* Find die that represents this context. */
20322 if (TYPE_P (context))
20324 context = TYPE_MAIN_VARIANT (context);
20325 return strip_naming_typedef (context, force_type_die (context));
20327 else
20328 return force_decl_die (context);
20330 return comp_unit_die ();
20333 /* Returns the DIE for decl. A DIE will always be returned. */
20335 static dw_die_ref
20336 force_decl_die (tree decl)
20338 dw_die_ref decl_die;
20339 unsigned saved_external_flag;
20340 tree save_fn = NULL_TREE;
20341 decl_die = lookup_decl_die (decl);
20342 if (!decl_die)
20344 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
20346 decl_die = lookup_decl_die (decl);
20347 if (decl_die)
20348 return decl_die;
20350 switch (TREE_CODE (decl))
20352 case FUNCTION_DECL:
20353 /* Clear current_function_decl, so that gen_subprogram_die thinks
20354 that this is a declaration. At this point, we just want to force
20355 declaration die. */
20356 save_fn = current_function_decl;
20357 current_function_decl = NULL_TREE;
20358 gen_subprogram_die (decl, context_die);
20359 current_function_decl = save_fn;
20360 break;
20362 case VAR_DECL:
20363 /* Set external flag to force declaration die. Restore it after
20364 gen_decl_die() call. */
20365 saved_external_flag = DECL_EXTERNAL (decl);
20366 DECL_EXTERNAL (decl) = 1;
20367 gen_decl_die (decl, NULL, context_die);
20368 DECL_EXTERNAL (decl) = saved_external_flag;
20369 break;
20371 case NAMESPACE_DECL:
20372 if (dwarf_version >= 3 || !dwarf_strict)
20373 dwarf2out_decl (decl);
20374 else
20375 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
20376 decl_die = comp_unit_die ();
20377 break;
20379 case TRANSLATION_UNIT_DECL:
20380 decl_die = comp_unit_die ();
20381 break;
20383 default:
20384 gcc_unreachable ();
20387 /* We should be able to find the DIE now. */
20388 if (!decl_die)
20389 decl_die = lookup_decl_die (decl);
20390 gcc_assert (decl_die);
20393 return decl_die;
20396 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
20397 always returned. */
20399 static dw_die_ref
20400 force_type_die (tree type)
20402 dw_die_ref type_die;
20404 type_die = lookup_type_die (type);
20405 if (!type_die)
20407 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
20409 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
20410 context_die);
20411 gcc_assert (type_die);
20413 return type_die;
20416 /* Force out any required namespaces to be able to output DECL,
20417 and return the new context_die for it, if it's changed. */
20419 static dw_die_ref
20420 setup_namespace_context (tree thing, dw_die_ref context_die)
20422 tree context = (DECL_P (thing)
20423 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
20424 if (context && TREE_CODE (context) == NAMESPACE_DECL)
20425 /* Force out the namespace. */
20426 context_die = force_decl_die (context);
20428 return context_die;
20431 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
20432 type) within its namespace, if appropriate.
20434 For compatibility with older debuggers, namespace DIEs only contain
20435 declarations; all definitions are emitted at CU scope. */
20437 static dw_die_ref
20438 declare_in_namespace (tree thing, dw_die_ref context_die)
20440 dw_die_ref ns_context;
20442 if (debug_info_level <= DINFO_LEVEL_TERSE)
20443 return context_die;
20445 /* If this decl is from an inlined function, then don't try to emit it in its
20446 namespace, as we will get confused. It would have already been emitted
20447 when the abstract instance of the inline function was emitted anyways. */
20448 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
20449 return context_die;
20451 ns_context = setup_namespace_context (thing, context_die);
20453 if (ns_context != context_die)
20455 if (is_fortran ())
20456 return ns_context;
20457 if (DECL_P (thing))
20458 gen_decl_die (thing, NULL, ns_context);
20459 else
20460 gen_type_die (thing, ns_context);
20462 return context_die;
20465 /* Generate a DIE for a namespace or namespace alias. */
20467 static void
20468 gen_namespace_die (tree decl, dw_die_ref context_die)
20470 dw_die_ref namespace_die;
20472 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
20473 they are an alias of. */
20474 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
20476 /* Output a real namespace or module. */
20477 context_die = setup_namespace_context (decl, comp_unit_die ());
20478 namespace_die = new_die (is_fortran ()
20479 ? DW_TAG_module : DW_TAG_namespace,
20480 context_die, decl);
20481 /* For Fortran modules defined in different CU don't add src coords. */
20482 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
20484 const char *name = dwarf2_name (decl, 0);
20485 if (name)
20486 add_name_attribute (namespace_die, name);
20488 else
20489 add_name_and_src_coords_attributes (namespace_die, decl);
20490 if (DECL_EXTERNAL (decl))
20491 add_AT_flag (namespace_die, DW_AT_declaration, 1);
20492 equate_decl_number_to_die (decl, namespace_die);
20494 else
20496 /* Output a namespace alias. */
20498 /* Force out the namespace we are an alias of, if necessary. */
20499 dw_die_ref origin_die
20500 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
20502 if (DECL_FILE_SCOPE_P (decl)
20503 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
20504 context_die = setup_namespace_context (decl, comp_unit_die ());
20505 /* Now create the namespace alias DIE. */
20506 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
20507 add_name_and_src_coords_attributes (namespace_die, decl);
20508 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
20509 equate_decl_number_to_die (decl, namespace_die);
20511 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
20512 if (want_pubnames ())
20513 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
20516 /* Generate Dwarf debug information for a decl described by DECL.
20517 The return value is currently only meaningful for PARM_DECLs,
20518 for all other decls it returns NULL. */
20520 static dw_die_ref
20521 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
20523 tree decl_or_origin = decl ? decl : origin;
20524 tree class_origin = NULL, ultimate_origin;
20526 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
20527 return NULL;
20529 switch (TREE_CODE (decl_or_origin))
20531 case ERROR_MARK:
20532 break;
20534 case CONST_DECL:
20535 if (!is_fortran () && !is_ada ())
20537 /* The individual enumerators of an enum type get output when we output
20538 the Dwarf representation of the relevant enum type itself. */
20539 break;
20542 /* Emit its type. */
20543 gen_type_die (TREE_TYPE (decl), context_die);
20545 /* And its containing namespace. */
20546 context_die = declare_in_namespace (decl, context_die);
20548 gen_const_die (decl, context_die);
20549 break;
20551 case FUNCTION_DECL:
20552 /* Don't output any DIEs to represent mere function declarations,
20553 unless they are class members or explicit block externs. */
20554 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
20555 && DECL_FILE_SCOPE_P (decl_or_origin)
20556 && (current_function_decl == NULL_TREE
20557 || DECL_ARTIFICIAL (decl_or_origin)))
20558 break;
20560 #if 0
20561 /* FIXME */
20562 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
20563 on local redeclarations of global functions. That seems broken. */
20564 if (current_function_decl != decl)
20565 /* This is only a declaration. */;
20566 #endif
20568 /* If we're emitting a clone, emit info for the abstract instance. */
20569 if (origin || DECL_ORIGIN (decl) != decl)
20570 dwarf2out_abstract_function (origin
20571 ? DECL_ORIGIN (origin)
20572 : DECL_ABSTRACT_ORIGIN (decl));
20574 /* If we're emitting an out-of-line copy of an inline function,
20575 emit info for the abstract instance and set up to refer to it. */
20576 else if (cgraph_function_possibly_inlined_p (decl)
20577 && ! DECL_ABSTRACT_P (decl)
20578 && ! class_or_namespace_scope_p (context_die)
20579 /* dwarf2out_abstract_function won't emit a die if this is just
20580 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
20581 that case, because that works only if we have a die. */
20582 && DECL_INITIAL (decl) != NULL_TREE)
20584 dwarf2out_abstract_function (decl);
20585 set_decl_origin_self (decl);
20588 /* Otherwise we're emitting the primary DIE for this decl. */
20589 else if (debug_info_level > DINFO_LEVEL_TERSE)
20591 /* Before we describe the FUNCTION_DECL itself, make sure that we
20592 have its containing type. */
20593 if (!origin)
20594 origin = decl_class_context (decl);
20595 if (origin != NULL_TREE)
20596 gen_type_die (origin, context_die);
20598 /* And its return type. */
20599 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
20601 /* And its virtual context. */
20602 if (DECL_VINDEX (decl) != NULL_TREE)
20603 gen_type_die (DECL_CONTEXT (decl), context_die);
20605 /* Make sure we have a member DIE for decl. */
20606 if (origin != NULL_TREE)
20607 gen_type_die_for_member (origin, decl, context_die);
20609 /* And its containing namespace. */
20610 context_die = declare_in_namespace (decl, context_die);
20613 /* Now output a DIE to represent the function itself. */
20614 if (decl)
20615 gen_subprogram_die (decl, context_die);
20616 break;
20618 case TYPE_DECL:
20619 /* If we are in terse mode, don't generate any DIEs to represent any
20620 actual typedefs. */
20621 if (debug_info_level <= DINFO_LEVEL_TERSE)
20622 break;
20624 /* In the special case of a TYPE_DECL node representing the declaration
20625 of some type tag, if the given TYPE_DECL is marked as having been
20626 instantiated from some other (original) TYPE_DECL node (e.g. one which
20627 was generated within the original definition of an inline function) we
20628 used to generate a special (abbreviated) DW_TAG_structure_type,
20629 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
20630 should be actually referencing those DIEs, as variable DIEs with that
20631 type would be emitted already in the abstract origin, so it was always
20632 removed during unused type prunning. Don't add anything in this
20633 case. */
20634 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
20635 break;
20637 if (is_redundant_typedef (decl))
20638 gen_type_die (TREE_TYPE (decl), context_die);
20639 else
20640 /* Output a DIE to represent the typedef itself. */
20641 gen_typedef_die (decl, context_die);
20642 break;
20644 case LABEL_DECL:
20645 if (debug_info_level >= DINFO_LEVEL_NORMAL)
20646 gen_label_die (decl, context_die);
20647 break;
20649 case VAR_DECL:
20650 case RESULT_DECL:
20651 /* If we are in terse mode, don't generate any DIEs to represent any
20652 variable declarations or definitions. */
20653 if (debug_info_level <= DINFO_LEVEL_TERSE)
20654 break;
20656 /* Output any DIEs that are needed to specify the type of this data
20657 object. */
20658 if (decl_by_reference_p (decl_or_origin))
20659 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20660 else
20661 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20663 /* And its containing type. */
20664 class_origin = decl_class_context (decl_or_origin);
20665 if (class_origin != NULL_TREE)
20666 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
20668 /* And its containing namespace. */
20669 context_die = declare_in_namespace (decl_or_origin, context_die);
20671 /* Now output the DIE to represent the data object itself. This gets
20672 complicated because of the possibility that the VAR_DECL really
20673 represents an inlined instance of a formal parameter for an inline
20674 function. */
20675 ultimate_origin = decl_ultimate_origin (decl_or_origin);
20676 if (ultimate_origin != NULL_TREE
20677 && TREE_CODE (ultimate_origin) == PARM_DECL)
20678 gen_formal_parameter_die (decl, origin,
20679 true /* Emit name attribute. */,
20680 context_die);
20681 else
20682 gen_variable_die (decl, origin, context_die);
20683 break;
20685 case FIELD_DECL:
20686 /* Ignore the nameless fields that are used to skip bits but handle C++
20687 anonymous unions and structs. */
20688 if (DECL_NAME (decl) != NULL_TREE
20689 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
20690 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
20692 gen_type_die (member_declared_type (decl), context_die);
20693 gen_field_die (decl, context_die);
20695 break;
20697 case PARM_DECL:
20698 if (DECL_BY_REFERENCE (decl_or_origin))
20699 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20700 else
20701 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20702 return gen_formal_parameter_die (decl, origin,
20703 true /* Emit name attribute. */,
20704 context_die);
20706 case NAMESPACE_DECL:
20707 case IMPORTED_DECL:
20708 if (dwarf_version >= 3 || !dwarf_strict)
20709 gen_namespace_die (decl, context_die);
20710 break;
20712 case NAMELIST_DECL:
20713 gen_namelist_decl (DECL_NAME (decl), context_die,
20714 NAMELIST_DECL_ASSOCIATED_DECL (decl));
20715 break;
20717 default:
20718 /* Probably some frontend-internal decl. Assume we don't care. */
20719 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
20720 break;
20723 return NULL;
20726 /* Output debug information for global decl DECL. Called from toplev.c after
20727 compilation proper has finished. */
20729 static void
20730 dwarf2out_global_decl (tree decl)
20732 /* Output DWARF2 information for file-scope tentative data object
20733 declarations, file-scope (extern) function declarations (which
20734 had no corresponding body) and file-scope tagged type declarations
20735 and definitions which have not yet been forced out. */
20736 if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
20737 dwarf2out_decl (decl);
20740 /* Output debug information for type decl DECL. Called from toplev.c
20741 and from language front ends (to record built-in types). */
20742 static void
20743 dwarf2out_type_decl (tree decl, int local)
20745 if (!local)
20746 dwarf2out_decl (decl);
20749 /* Output debug information for imported module or decl DECL.
20750 NAME is non-NULL name in the lexical block if the decl has been renamed.
20751 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
20752 that DECL belongs to.
20753 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
20754 static void
20755 dwarf2out_imported_module_or_decl_1 (tree decl,
20756 tree name,
20757 tree lexical_block,
20758 dw_die_ref lexical_block_die)
20760 expanded_location xloc;
20761 dw_die_ref imported_die = NULL;
20762 dw_die_ref at_import_die;
20764 if (TREE_CODE (decl) == IMPORTED_DECL)
20766 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
20767 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
20768 gcc_assert (decl);
20770 else
20771 xloc = expand_location (input_location);
20773 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
20775 at_import_die = force_type_die (TREE_TYPE (decl));
20776 /* For namespace N { typedef void T; } using N::T; base_type_die
20777 returns NULL, but DW_TAG_imported_declaration requires
20778 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
20779 if (!at_import_die)
20781 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
20782 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
20783 at_import_die = lookup_type_die (TREE_TYPE (decl));
20784 gcc_assert (at_import_die);
20787 else
20789 at_import_die = lookup_decl_die (decl);
20790 if (!at_import_die)
20792 /* If we're trying to avoid duplicate debug info, we may not have
20793 emitted the member decl for this field. Emit it now. */
20794 if (TREE_CODE (decl) == FIELD_DECL)
20796 tree type = DECL_CONTEXT (decl);
20798 if (TYPE_CONTEXT (type)
20799 && TYPE_P (TYPE_CONTEXT (type))
20800 && !should_emit_struct_debug (TYPE_CONTEXT (type),
20801 DINFO_USAGE_DIR_USE))
20802 return;
20803 gen_type_die_for_member (type, decl,
20804 get_context_die (TYPE_CONTEXT (type)));
20806 if (TREE_CODE (decl) == NAMELIST_DECL)
20807 at_import_die = gen_namelist_decl (DECL_NAME (decl),
20808 get_context_die (DECL_CONTEXT (decl)),
20809 NULL_TREE);
20810 else
20811 at_import_die = force_decl_die (decl);
20815 if (TREE_CODE (decl) == NAMESPACE_DECL)
20817 if (dwarf_version >= 3 || !dwarf_strict)
20818 imported_die = new_die (DW_TAG_imported_module,
20819 lexical_block_die,
20820 lexical_block);
20821 else
20822 return;
20824 else
20825 imported_die = new_die (DW_TAG_imported_declaration,
20826 lexical_block_die,
20827 lexical_block);
20829 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
20830 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
20831 if (name)
20832 add_AT_string (imported_die, DW_AT_name,
20833 IDENTIFIER_POINTER (name));
20834 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
20837 /* Output debug information for imported module or decl DECL.
20838 NAME is non-NULL name in context if the decl has been renamed.
20839 CHILD is true if decl is one of the renamed decls as part of
20840 importing whole module. */
20842 static void
20843 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
20844 bool child)
20846 /* dw_die_ref at_import_die; */
20847 dw_die_ref scope_die;
20849 if (debug_info_level <= DINFO_LEVEL_TERSE)
20850 return;
20852 gcc_assert (decl);
20854 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
20855 We need decl DIE for reference and scope die. First, get DIE for the decl
20856 itself. */
20858 /* Get the scope die for decl context. Use comp_unit_die for global module
20859 or decl. If die is not found for non globals, force new die. */
20860 if (context
20861 && TYPE_P (context)
20862 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
20863 return;
20865 if (!(dwarf_version >= 3 || !dwarf_strict))
20866 return;
20868 scope_die = get_context_die (context);
20870 if (child)
20872 gcc_assert (scope_die->die_child);
20873 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
20874 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
20875 scope_die = scope_die->die_child;
20878 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
20879 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
20883 /* Output debug information for namelists. */
20885 static dw_die_ref
20886 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
20888 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
20889 tree value;
20890 unsigned i;
20892 if (debug_info_level <= DINFO_LEVEL_TERSE)
20893 return NULL;
20895 gcc_assert (scope_die != NULL);
20896 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
20897 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
20899 /* If there are no item_decls, we have a nondefining namelist, e.g.
20900 with USE association; hence, set DW_AT_declaration. */
20901 if (item_decls == NULL_TREE)
20903 add_AT_flag (nml_die, DW_AT_declaration, 1);
20904 return nml_die;
20907 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
20909 nml_item_ref_die = lookup_decl_die (value);
20910 if (!nml_item_ref_die)
20911 nml_item_ref_die = force_decl_die (value);
20913 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
20914 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
20916 return nml_die;
20920 /* Write the debugging output for DECL. */
20922 static void
20923 dwarf2out_decl (tree decl)
20925 dw_die_ref context_die = comp_unit_die ();
20927 switch (TREE_CODE (decl))
20929 case ERROR_MARK:
20930 return;
20932 case FUNCTION_DECL:
20933 /* What we would really like to do here is to filter out all mere
20934 file-scope declarations of file-scope functions which are never
20935 referenced later within this translation unit (and keep all of ones
20936 that *are* referenced later on) but we aren't clairvoyant, so we have
20937 no idea which functions will be referenced in the future (i.e. later
20938 on within the current translation unit). So here we just ignore all
20939 file-scope function declarations which are not also definitions. If
20940 and when the debugger needs to know something about these functions,
20941 it will have to hunt around and find the DWARF information associated
20942 with the definition of the function.
20944 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
20945 nodes represent definitions and which ones represent mere
20946 declarations. We have to check DECL_INITIAL instead. That's because
20947 the C front-end supports some weird semantics for "extern inline"
20948 function definitions. These can get inlined within the current
20949 translation unit (and thus, we need to generate Dwarf info for their
20950 abstract instances so that the Dwarf info for the concrete inlined
20951 instances can have something to refer to) but the compiler never
20952 generates any out-of-lines instances of such things (despite the fact
20953 that they *are* definitions).
20955 The important point is that the C front-end marks these "extern
20956 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
20957 them anyway. Note that the C++ front-end also plays some similar games
20958 for inline function definitions appearing within include files which
20959 also contain `#pragma interface' pragmas.
20961 If we are called from dwarf2out_abstract_function output a DIE
20962 anyway. We can end up here this way with early inlining and LTO
20963 where the inlined function is output in a different LTRANS unit
20964 or not at all. */
20965 if (DECL_INITIAL (decl) == NULL_TREE
20966 && ! DECL_ABSTRACT_P (decl))
20967 return;
20969 /* If we're a nested function, initially use a parent of NULL; if we're
20970 a plain function, this will be fixed up in decls_for_scope. If
20971 we're a method, it will be ignored, since we already have a DIE. */
20972 if (decl_function_context (decl)
20973 /* But if we're in terse mode, we don't care about scope. */
20974 && debug_info_level > DINFO_LEVEL_TERSE)
20975 context_die = NULL;
20976 break;
20978 case VAR_DECL:
20979 /* Ignore this VAR_DECL if it refers to a file-scope extern data object
20980 declaration and if the declaration was never even referenced from
20981 within this entire compilation unit. We suppress these DIEs in
20982 order to save space in the .debug section (by eliminating entries
20983 which are probably useless). Note that we must not suppress
20984 block-local extern declarations (whether used or not) because that
20985 would screw-up the debugger's name lookup mechanism and cause it to
20986 miss things which really ought to be in scope at a given point. */
20987 if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
20988 return;
20990 /* For local statics lookup proper context die. */
20991 if (TREE_STATIC (decl)
20992 && DECL_CONTEXT (decl)
20993 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
20994 context_die = lookup_decl_die (DECL_CONTEXT (decl));
20996 /* If we are in terse mode, don't generate any DIEs to represent any
20997 variable declarations or definitions. */
20998 if (debug_info_level <= DINFO_LEVEL_TERSE)
20999 return;
21000 break;
21002 case CONST_DECL:
21003 if (debug_info_level <= DINFO_LEVEL_TERSE)
21004 return;
21005 if (!is_fortran () && !is_ada ())
21006 return;
21007 if (TREE_STATIC (decl) && decl_function_context (decl))
21008 context_die = lookup_decl_die (DECL_CONTEXT (decl));
21009 break;
21011 case NAMESPACE_DECL:
21012 case IMPORTED_DECL:
21013 if (debug_info_level <= DINFO_LEVEL_TERSE)
21014 return;
21015 if (lookup_decl_die (decl) != NULL)
21016 return;
21017 break;
21019 case TYPE_DECL:
21020 /* Don't emit stubs for types unless they are needed by other DIEs. */
21021 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
21022 return;
21024 /* Don't bother trying to generate any DIEs to represent any of the
21025 normal built-in types for the language we are compiling. */
21026 if (DECL_IS_BUILTIN (decl))
21027 return;
21029 /* If we are in terse mode, don't generate any DIEs for types. */
21030 if (debug_info_level <= DINFO_LEVEL_TERSE)
21031 return;
21033 /* If we're a function-scope tag, initially use a parent of NULL;
21034 this will be fixed up in decls_for_scope. */
21035 if (decl_function_context (decl))
21036 context_die = NULL;
21038 break;
21040 case NAMELIST_DECL:
21041 break;
21043 default:
21044 return;
21047 gen_decl_die (decl, NULL, context_die);
21050 /* Write the debugging output for DECL. */
21052 static void
21053 dwarf2out_function_decl (tree decl)
21055 dwarf2out_decl (decl);
21056 call_arg_locations = NULL;
21057 call_arg_loc_last = NULL;
21058 call_site_count = -1;
21059 tail_call_site_count = -1;
21060 block_map.release ();
21061 htab_empty (decl_loc_table);
21062 htab_empty (cached_dw_loc_list_table);
21065 /* Output a marker (i.e. a label) for the beginning of the generated code for
21066 a lexical block. */
21068 static void
21069 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
21070 unsigned int blocknum)
21072 switch_to_section (current_function_section ());
21073 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
21076 /* Output a marker (i.e. a label) for the end of the generated code for a
21077 lexical block. */
21079 static void
21080 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
21082 switch_to_section (current_function_section ());
21083 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
21086 /* Returns nonzero if it is appropriate not to emit any debugging
21087 information for BLOCK, because it doesn't contain any instructions.
21089 Don't allow this for blocks with nested functions or local classes
21090 as we would end up with orphans, and in the presence of scheduling
21091 we may end up calling them anyway. */
21093 static bool
21094 dwarf2out_ignore_block (const_tree block)
21096 tree decl;
21097 unsigned int i;
21099 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
21100 if (TREE_CODE (decl) == FUNCTION_DECL
21101 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21102 return 0;
21103 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
21105 decl = BLOCK_NONLOCALIZED_VAR (block, i);
21106 if (TREE_CODE (decl) == FUNCTION_DECL
21107 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21108 return 0;
21111 return 1;
21114 /* Hash table routines for file_hash. */
21116 static int
21117 file_table_eq (const void *p1_p, const void *p2_p)
21119 const struct dwarf_file_data *const p1 =
21120 (const struct dwarf_file_data *) p1_p;
21121 const char *const p2 = (const char *) p2_p;
21122 return filename_cmp (p1->filename, p2) == 0;
21125 static hashval_t
21126 file_table_hash (const void *p_p)
21128 const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
21129 return htab_hash_string (p->filename);
21132 /* Lookup FILE_NAME (in the list of filenames that we know about here in
21133 dwarf2out.c) and return its "index". The index of each (known) filename is
21134 just a unique number which is associated with only that one filename. We
21135 need such numbers for the sake of generating labels (in the .debug_sfnames
21136 section) and references to those files numbers (in the .debug_srcinfo
21137 and.debug_macinfo sections). If the filename given as an argument is not
21138 found in our current list, add it to the list and assign it the next
21139 available unique index number. In order to speed up searches, we remember
21140 the index of the filename was looked up last. This handles the majority of
21141 all searches. */
21143 static struct dwarf_file_data *
21144 lookup_filename (const char *file_name)
21146 void ** slot;
21147 struct dwarf_file_data * created;
21149 /* Check to see if the file name that was searched on the previous
21150 call matches this file name. If so, return the index. */
21151 if (file_table_last_lookup
21152 && (file_name == file_table_last_lookup->filename
21153 || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
21154 return file_table_last_lookup;
21156 /* Didn't match the previous lookup, search the table. */
21157 slot = htab_find_slot_with_hash (file_table, file_name,
21158 htab_hash_string (file_name), INSERT);
21159 if (*slot)
21160 return (struct dwarf_file_data *) *slot;
21162 created = ggc_alloc<dwarf_file_data> ();
21163 created->filename = file_name;
21164 created->emitted_number = 0;
21165 *slot = created;
21166 return created;
21169 /* If the assembler will construct the file table, then translate the compiler
21170 internal file table number into the assembler file table number, and emit
21171 a .file directive if we haven't already emitted one yet. The file table
21172 numbers are different because we prune debug info for unused variables and
21173 types, which may include filenames. */
21175 static int
21176 maybe_emit_file (struct dwarf_file_data * fd)
21178 if (! fd->emitted_number)
21180 if (last_emitted_file)
21181 fd->emitted_number = last_emitted_file->emitted_number + 1;
21182 else
21183 fd->emitted_number = 1;
21184 last_emitted_file = fd;
21186 if (DWARF2_ASM_LINE_DEBUG_INFO)
21188 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
21189 output_quoted_string (asm_out_file,
21190 remap_debug_filename (fd->filename));
21191 fputc ('\n', asm_out_file);
21195 return fd->emitted_number;
21198 /* Schedule generation of a DW_AT_const_value attribute to DIE.
21199 That generation should happen after function debug info has been
21200 generated. The value of the attribute is the constant value of ARG. */
21202 static void
21203 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
21205 die_arg_entry entry;
21207 if (!die || !arg)
21208 return;
21210 if (!tmpl_value_parm_die_table)
21211 vec_alloc (tmpl_value_parm_die_table, 32);
21213 entry.die = die;
21214 entry.arg = arg;
21215 vec_safe_push (tmpl_value_parm_die_table, entry);
21218 /* Return TRUE if T is an instance of generic type, FALSE
21219 otherwise. */
21221 static bool
21222 generic_type_p (tree t)
21224 if (t == NULL_TREE || !TYPE_P (t))
21225 return false;
21226 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
21229 /* Schedule the generation of the generic parameter dies for the
21230 instance of generic type T. The proper generation itself is later
21231 done by gen_scheduled_generic_parms_dies. */
21233 static void
21234 schedule_generic_params_dies_gen (tree t)
21236 if (!generic_type_p (t))
21237 return;
21239 if (!generic_type_instances)
21240 vec_alloc (generic_type_instances, 256);
21242 vec_safe_push (generic_type_instances, t);
21245 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
21246 by append_entry_to_tmpl_value_parm_die_table. This function must
21247 be called after function DIEs have been generated. */
21249 static void
21250 gen_remaining_tmpl_value_param_die_attribute (void)
21252 if (tmpl_value_parm_die_table)
21254 unsigned i;
21255 die_arg_entry *e;
21257 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
21258 tree_add_const_value_attribute (e->die, e->arg);
21262 /* Generate generic parameters DIEs for instances of generic types
21263 that have been previously scheduled by
21264 schedule_generic_params_dies_gen. This function must be called
21265 after all the types of the CU have been laid out. */
21267 static void
21268 gen_scheduled_generic_parms_dies (void)
21270 unsigned i;
21271 tree t;
21273 if (!generic_type_instances)
21274 return;
21276 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
21277 if (COMPLETE_TYPE_P (t))
21278 gen_generic_params_dies (t);
21282 /* Replace DW_AT_name for the decl with name. */
21284 static void
21285 dwarf2out_set_name (tree decl, tree name)
21287 dw_die_ref die;
21288 dw_attr_ref attr;
21289 const char *dname;
21291 die = TYPE_SYMTAB_DIE (decl);
21292 if (!die)
21293 return;
21295 dname = dwarf2_name (name, 0);
21296 if (!dname)
21297 return;
21299 attr = get_AT (die, DW_AT_name);
21300 if (attr)
21302 struct indirect_string_node *node;
21304 node = find_AT_string (dname);
21305 /* replace the string. */
21306 attr->dw_attr_val.v.val_str = node;
21309 else
21310 add_name_attribute (die, dname);
21313 /* True if before or during processing of the first function being emitted. */
21314 static bool in_first_function_p = true;
21315 /* True if loc_note during dwarf2out_var_location call might still be
21316 before first real instruction at address equal to .Ltext0. */
21317 static bool maybe_at_text_label_p = true;
21318 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
21319 static unsigned int first_loclabel_num_not_at_text_label;
21321 /* Called by the final INSN scan whenever we see a var location. We
21322 use it to drop labels in the right places, and throw the location in
21323 our lookup table. */
21325 static void
21326 dwarf2out_var_location (rtx_insn *loc_note)
21328 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
21329 struct var_loc_node *newloc;
21330 rtx_insn *next_real, *next_note;
21331 static const char *last_label;
21332 static const char *last_postcall_label;
21333 static bool last_in_cold_section_p;
21334 static rtx_insn *expected_next_loc_note;
21335 tree decl;
21336 bool var_loc_p;
21338 if (!NOTE_P (loc_note))
21340 if (CALL_P (loc_note))
21342 call_site_count++;
21343 if (SIBLING_CALL_P (loc_note))
21344 tail_call_site_count++;
21346 return;
21349 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
21350 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
21351 return;
21353 /* Optimize processing a large consecutive sequence of location
21354 notes so we don't spend too much time in next_real_insn. If the
21355 next insn is another location note, remember the next_real_insn
21356 calculation for next time. */
21357 next_real = cached_next_real_insn;
21358 if (next_real)
21360 if (expected_next_loc_note != loc_note)
21361 next_real = NULL;
21364 next_note = NEXT_INSN (loc_note);
21365 if (! next_note
21366 || next_note->deleted ()
21367 || ! NOTE_P (next_note)
21368 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
21369 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
21370 next_note = NULL;
21372 if (! next_real)
21373 next_real = next_real_insn (loc_note);
21375 if (next_note)
21377 expected_next_loc_note = next_note;
21378 cached_next_real_insn = next_real;
21380 else
21381 cached_next_real_insn = NULL;
21383 /* If there are no instructions which would be affected by this note,
21384 don't do anything. */
21385 if (var_loc_p
21386 && next_real == NULL_RTX
21387 && !NOTE_DURING_CALL_P (loc_note))
21388 return;
21390 if (next_real == NULL_RTX)
21391 next_real = get_last_insn ();
21393 /* If there were any real insns between note we processed last time
21394 and this note (or if it is the first note), clear
21395 last_{,postcall_}label so that they are not reused this time. */
21396 if (last_var_location_insn == NULL_RTX
21397 || last_var_location_insn != next_real
21398 || last_in_cold_section_p != in_cold_section_p)
21400 last_label = NULL;
21401 last_postcall_label = NULL;
21404 if (var_loc_p)
21406 decl = NOTE_VAR_LOCATION_DECL (loc_note);
21407 newloc = add_var_loc_to_decl (decl, loc_note,
21408 NOTE_DURING_CALL_P (loc_note)
21409 ? last_postcall_label : last_label);
21410 if (newloc == NULL)
21411 return;
21413 else
21415 decl = NULL_TREE;
21416 newloc = NULL;
21419 /* If there were no real insns between note we processed last time
21420 and this note, use the label we emitted last time. Otherwise
21421 create a new label and emit it. */
21422 if (last_label == NULL)
21424 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
21425 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
21426 loclabel_num++;
21427 last_label = ggc_strdup (loclabel);
21428 /* See if loclabel might be equal to .Ltext0. If yes,
21429 bump first_loclabel_num_not_at_text_label. */
21430 if (!have_multiple_function_sections
21431 && in_first_function_p
21432 && maybe_at_text_label_p)
21434 static rtx_insn *last_start;
21435 rtx_insn *insn;
21436 for (insn = loc_note; insn; insn = previous_insn (insn))
21437 if (insn == last_start)
21438 break;
21439 else if (!NONDEBUG_INSN_P (insn))
21440 continue;
21441 else
21443 rtx body = PATTERN (insn);
21444 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
21445 continue;
21446 /* Inline asm could occupy zero bytes. */
21447 else if (GET_CODE (body) == ASM_INPUT
21448 || asm_noperands (body) >= 0)
21449 continue;
21450 #ifdef HAVE_attr_length
21451 else if (get_attr_min_length (insn) == 0)
21452 continue;
21453 #endif
21454 else
21456 /* Assume insn has non-zero length. */
21457 maybe_at_text_label_p = false;
21458 break;
21461 if (maybe_at_text_label_p)
21463 last_start = loc_note;
21464 first_loclabel_num_not_at_text_label = loclabel_num;
21469 if (!var_loc_p)
21471 struct call_arg_loc_node *ca_loc
21472 = ggc_cleared_alloc<call_arg_loc_node> ();
21473 rtx_insn *prev = prev_real_insn (loc_note);
21474 rtx x;
21475 ca_loc->call_arg_loc_note = loc_note;
21476 ca_loc->next = NULL;
21477 ca_loc->label = last_label;
21478 gcc_assert (prev
21479 && (CALL_P (prev)
21480 || (NONJUMP_INSN_P (prev)
21481 && GET_CODE (PATTERN (prev)) == SEQUENCE
21482 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
21483 if (!CALL_P (prev))
21484 prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
21485 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
21486 x = get_call_rtx_from (PATTERN (prev));
21487 if (x)
21489 x = XEXP (XEXP (x, 0), 0);
21490 if (GET_CODE (x) == SYMBOL_REF
21491 && SYMBOL_REF_DECL (x)
21492 && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
21493 ca_loc->symbol_ref = x;
21495 ca_loc->block = insn_scope (prev);
21496 if (call_arg_locations)
21497 call_arg_loc_last->next = ca_loc;
21498 else
21499 call_arg_locations = ca_loc;
21500 call_arg_loc_last = ca_loc;
21502 else if (!NOTE_DURING_CALL_P (loc_note))
21503 newloc->label = last_label;
21504 else
21506 if (!last_postcall_label)
21508 sprintf (loclabel, "%s-1", last_label);
21509 last_postcall_label = ggc_strdup (loclabel);
21511 newloc->label = last_postcall_label;
21514 last_var_location_insn = next_real;
21515 last_in_cold_section_p = in_cold_section_p;
21518 /* Note in one location list that text section has changed. */
21520 static int
21521 var_location_switch_text_section_1 (void **slot, void *data ATTRIBUTE_UNUSED)
21523 var_loc_list *list = (var_loc_list *) *slot;
21524 if (list->first)
21525 list->last_before_switch
21526 = list->last->next ? list->last->next : list->last;
21527 return 1;
21530 /* Note in all location lists that text section has changed. */
21532 static void
21533 var_location_switch_text_section (void)
21535 if (decl_loc_table == NULL)
21536 return;
21538 htab_traverse (decl_loc_table, var_location_switch_text_section_1, NULL);
21541 /* Create a new line number table. */
21543 static dw_line_info_table *
21544 new_line_info_table (void)
21546 dw_line_info_table *table;
21548 table = ggc_cleared_alloc<dw_line_info_table_struct> ();
21549 table->file_num = 1;
21550 table->line_num = 1;
21551 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
21553 return table;
21556 /* Lookup the "current" table into which we emit line info, so
21557 that we don't have to do it for every source line. */
21559 static void
21560 set_cur_line_info_table (section *sec)
21562 dw_line_info_table *table;
21564 if (sec == text_section)
21565 table = text_section_line_info;
21566 else if (sec == cold_text_section)
21568 table = cold_text_section_line_info;
21569 if (!table)
21571 cold_text_section_line_info = table = new_line_info_table ();
21572 table->end_label = cold_end_label;
21575 else
21577 const char *end_label;
21579 if (flag_reorder_blocks_and_partition)
21581 if (in_cold_section_p)
21582 end_label = crtl->subsections.cold_section_end_label;
21583 else
21584 end_label = crtl->subsections.hot_section_end_label;
21586 else
21588 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21589 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
21590 current_function_funcdef_no);
21591 end_label = ggc_strdup (label);
21594 table = new_line_info_table ();
21595 table->end_label = end_label;
21597 vec_safe_push (separate_line_info, table);
21600 if (DWARF2_ASM_LINE_DEBUG_INFO)
21601 table->is_stmt = (cur_line_info_table
21602 ? cur_line_info_table->is_stmt
21603 : DWARF_LINE_DEFAULT_IS_STMT_START);
21604 cur_line_info_table = table;
21608 /* We need to reset the locations at the beginning of each
21609 function. We can't do this in the end_function hook, because the
21610 declarations that use the locations won't have been output when
21611 that hook is called. Also compute have_multiple_function_sections here. */
21613 static void
21614 dwarf2out_begin_function (tree fun)
21616 section *sec = function_section (fun);
21618 if (sec != text_section)
21619 have_multiple_function_sections = true;
21621 if (flag_reorder_blocks_and_partition && !cold_text_section)
21623 gcc_assert (current_function_decl == fun);
21624 cold_text_section = unlikely_text_section ();
21625 switch_to_section (cold_text_section);
21626 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
21627 switch_to_section (sec);
21630 dwarf2out_note_section_used ();
21631 call_site_count = 0;
21632 tail_call_site_count = 0;
21634 set_cur_line_info_table (sec);
21637 /* Helper function of dwarf2out_end_function, called only after emitting
21638 the very first function into assembly. Check if some .debug_loc range
21639 might end with a .LVL* label that could be equal to .Ltext0.
21640 In that case we must force using absolute addresses in .debug_loc ranges,
21641 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
21642 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
21643 list terminator.
21644 Set have_multiple_function_sections to true in that case and
21645 terminate htab traversal. */
21647 static int
21648 find_empty_loc_ranges_at_text_label (void **slot, void *)
21650 var_loc_list *entry;
21651 struct var_loc_node *node;
21653 entry = (var_loc_list *) *slot;
21654 node = entry->first;
21655 if (node && node->next && node->next->label)
21657 unsigned int i;
21658 const char *label = node->next->label;
21659 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
21661 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
21663 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
21664 if (strcmp (label, loclabel) == 0)
21666 have_multiple_function_sections = true;
21667 return 0;
21671 return 1;
21674 /* Hook called after emitting a function into assembly.
21675 This does something only for the very first function emitted. */
21677 static void
21678 dwarf2out_end_function (unsigned int)
21680 if (in_first_function_p
21681 && !have_multiple_function_sections
21682 && first_loclabel_num_not_at_text_label
21683 && decl_loc_table)
21684 htab_traverse (decl_loc_table, find_empty_loc_ranges_at_text_label,
21685 NULL);
21686 in_first_function_p = false;
21687 maybe_at_text_label_p = false;
21690 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
21692 static void
21693 push_dw_line_info_entry (dw_line_info_table *table,
21694 enum dw_line_info_opcode opcode, unsigned int val)
21696 dw_line_info_entry e;
21697 e.opcode = opcode;
21698 e.val = val;
21699 vec_safe_push (table->entries, e);
21702 /* Output a label to mark the beginning of a source code line entry
21703 and record information relating to this source line, in
21704 'line_info_table' for later output of the .debug_line section. */
21705 /* ??? The discriminator parameter ought to be unsigned. */
21707 static void
21708 dwarf2out_source_line (unsigned int line, const char *filename,
21709 int discriminator, bool is_stmt)
21711 unsigned int file_num;
21712 dw_line_info_table *table;
21714 if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
21715 return;
21717 /* The discriminator column was added in dwarf4. Simplify the below
21718 by simply removing it if we're not supposed to output it. */
21719 if (dwarf_version < 4 && dwarf_strict)
21720 discriminator = 0;
21722 table = cur_line_info_table;
21723 file_num = maybe_emit_file (lookup_filename (filename));
21725 /* ??? TODO: Elide duplicate line number entries. Traditionally,
21726 the debugger has used the second (possibly duplicate) line number
21727 at the beginning of the function to mark the end of the prologue.
21728 We could eliminate any other duplicates within the function. For
21729 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
21730 that second line number entry. */
21731 /* Recall that this end-of-prologue indication is *not* the same thing
21732 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
21733 to which the hook corresponds, follows the last insn that was
21734 emitted by gen_prologue. What we need is to precede the first insn
21735 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
21736 insn that corresponds to something the user wrote. These may be
21737 very different locations once scheduling is enabled. */
21739 if (0 && file_num == table->file_num
21740 && line == table->line_num
21741 && discriminator == table->discrim_num
21742 && is_stmt == table->is_stmt)
21743 return;
21745 switch_to_section (current_function_section ());
21747 /* If requested, emit something human-readable. */
21748 if (flag_debug_asm)
21749 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
21751 if (DWARF2_ASM_LINE_DEBUG_INFO)
21753 /* Emit the .loc directive understood by GNU as. */
21754 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
21755 file_num, line, is_stmt, discriminator */
21756 fputs ("\t.loc ", asm_out_file);
21757 fprint_ul (asm_out_file, file_num);
21758 putc (' ', asm_out_file);
21759 fprint_ul (asm_out_file, line);
21760 putc (' ', asm_out_file);
21761 putc ('0', asm_out_file);
21763 if (is_stmt != table->is_stmt)
21765 fputs (" is_stmt ", asm_out_file);
21766 putc (is_stmt ? '1' : '0', asm_out_file);
21768 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
21770 gcc_assert (discriminator > 0);
21771 fputs (" discriminator ", asm_out_file);
21772 fprint_ul (asm_out_file, (unsigned long) discriminator);
21774 putc ('\n', asm_out_file);
21776 else
21778 unsigned int label_num = ++line_info_label_num;
21780 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
21782 push_dw_line_info_entry (table, LI_set_address, label_num);
21783 if (file_num != table->file_num)
21784 push_dw_line_info_entry (table, LI_set_file, file_num);
21785 if (discriminator != table->discrim_num)
21786 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
21787 if (is_stmt != table->is_stmt)
21788 push_dw_line_info_entry (table, LI_negate_stmt, 0);
21789 push_dw_line_info_entry (table, LI_set_line, line);
21792 table->file_num = file_num;
21793 table->line_num = line;
21794 table->discrim_num = discriminator;
21795 table->is_stmt = is_stmt;
21796 table->in_use = true;
21799 /* Record the beginning of a new source file. */
21801 static void
21802 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
21804 if (flag_eliminate_dwarf2_dups)
21806 /* Record the beginning of the file for break_out_includes. */
21807 dw_die_ref bincl_die;
21809 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
21810 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
21813 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21815 macinfo_entry e;
21816 e.code = DW_MACINFO_start_file;
21817 e.lineno = lineno;
21818 e.info = ggc_strdup (filename);
21819 vec_safe_push (macinfo_table, e);
21823 /* Record the end of a source file. */
21825 static void
21826 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
21828 if (flag_eliminate_dwarf2_dups)
21829 /* Record the end of the file for break_out_includes. */
21830 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
21832 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21834 macinfo_entry e;
21835 e.code = DW_MACINFO_end_file;
21836 e.lineno = lineno;
21837 e.info = NULL;
21838 vec_safe_push (macinfo_table, e);
21842 /* Called from debug_define in toplev.c. The `buffer' parameter contains
21843 the tail part of the directive line, i.e. the part which is past the
21844 initial whitespace, #, whitespace, directive-name, whitespace part. */
21846 static void
21847 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
21848 const char *buffer ATTRIBUTE_UNUSED)
21850 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21852 macinfo_entry e;
21853 /* Insert a dummy first entry to be able to optimize the whole
21854 predefined macro block using DW_MACRO_GNU_transparent_include. */
21855 if (macinfo_table->is_empty () && lineno <= 1)
21857 e.code = 0;
21858 e.lineno = 0;
21859 e.info = NULL;
21860 vec_safe_push (macinfo_table, e);
21862 e.code = DW_MACINFO_define;
21863 e.lineno = lineno;
21864 e.info = ggc_strdup (buffer);
21865 vec_safe_push (macinfo_table, e);
21869 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
21870 the tail part of the directive line, i.e. the part which is past the
21871 initial whitespace, #, whitespace, directive-name, whitespace part. */
21873 static void
21874 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
21875 const char *buffer ATTRIBUTE_UNUSED)
21877 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21879 macinfo_entry e;
21880 /* Insert a dummy first entry to be able to optimize the whole
21881 predefined macro block using DW_MACRO_GNU_transparent_include. */
21882 if (macinfo_table->is_empty () && lineno <= 1)
21884 e.code = 0;
21885 e.lineno = 0;
21886 e.info = NULL;
21887 vec_safe_push (macinfo_table, e);
21889 e.code = DW_MACINFO_undef;
21890 e.lineno = lineno;
21891 e.info = ggc_strdup (buffer);
21892 vec_safe_push (macinfo_table, e);
21896 /* Helpers to manipulate hash table of CUs. */
21898 struct macinfo_entry_hasher : typed_noop_remove <macinfo_entry>
21900 typedef macinfo_entry value_type;
21901 typedef macinfo_entry compare_type;
21902 static inline hashval_t hash (const value_type *);
21903 static inline bool equal (const value_type *, const compare_type *);
21906 inline hashval_t
21907 macinfo_entry_hasher::hash (const value_type *entry)
21909 return htab_hash_string (entry->info);
21912 inline bool
21913 macinfo_entry_hasher::equal (const value_type *entry1,
21914 const compare_type *entry2)
21916 return !strcmp (entry1->info, entry2->info);
21919 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
21921 /* Output a single .debug_macinfo entry. */
21923 static void
21924 output_macinfo_op (macinfo_entry *ref)
21926 int file_num;
21927 size_t len;
21928 struct indirect_string_node *node;
21929 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21930 struct dwarf_file_data *fd;
21932 switch (ref->code)
21934 case DW_MACINFO_start_file:
21935 fd = lookup_filename (ref->info);
21936 file_num = maybe_emit_file (fd);
21937 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
21938 dw2_asm_output_data_uleb128 (ref->lineno,
21939 "Included from line number %lu",
21940 (unsigned long) ref->lineno);
21941 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
21942 break;
21943 case DW_MACINFO_end_file:
21944 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
21945 break;
21946 case DW_MACINFO_define:
21947 case DW_MACINFO_undef:
21948 len = strlen (ref->info) + 1;
21949 if (!dwarf_strict
21950 && len > DWARF_OFFSET_SIZE
21951 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
21952 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
21954 ref->code = ref->code == DW_MACINFO_define
21955 ? DW_MACRO_GNU_define_indirect
21956 : DW_MACRO_GNU_undef_indirect;
21957 output_macinfo_op (ref);
21958 return;
21960 dw2_asm_output_data (1, ref->code,
21961 ref->code == DW_MACINFO_define
21962 ? "Define macro" : "Undefine macro");
21963 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
21964 (unsigned long) ref->lineno);
21965 dw2_asm_output_nstring (ref->info, -1, "The macro");
21966 break;
21967 case DW_MACRO_GNU_define_indirect:
21968 case DW_MACRO_GNU_undef_indirect:
21969 node = find_AT_string (ref->info);
21970 gcc_assert (node
21971 && ((node->form == DW_FORM_strp)
21972 || (node->form == DW_FORM_GNU_str_index)));
21973 dw2_asm_output_data (1, ref->code,
21974 ref->code == DW_MACRO_GNU_define_indirect
21975 ? "Define macro indirect"
21976 : "Undefine macro indirect");
21977 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
21978 (unsigned long) ref->lineno);
21979 if (node->form == DW_FORM_strp)
21980 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
21981 debug_str_section, "The macro: \"%s\"",
21982 ref->info);
21983 else
21984 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
21985 ref->info);
21986 break;
21987 case DW_MACRO_GNU_transparent_include:
21988 dw2_asm_output_data (1, ref->code, "Transparent include");
21989 ASM_GENERATE_INTERNAL_LABEL (label,
21990 DEBUG_MACRO_SECTION_LABEL, ref->lineno);
21991 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
21992 break;
21993 default:
21994 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
21995 ASM_COMMENT_START, (unsigned long) ref->code);
21996 break;
22000 /* Attempt to make a sequence of define/undef macinfo ops shareable with
22001 other compilation unit .debug_macinfo sections. IDX is the first
22002 index of a define/undef, return the number of ops that should be
22003 emitted in a comdat .debug_macinfo section and emit
22004 a DW_MACRO_GNU_transparent_include entry referencing it.
22005 If the define/undef entry should be emitted normally, return 0. */
22007 static unsigned
22008 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
22009 macinfo_hash_type **macinfo_htab)
22011 macinfo_entry *first, *second, *cur, *inc;
22012 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
22013 unsigned char checksum[16];
22014 struct md5_ctx ctx;
22015 char *grp_name, *tail;
22016 const char *base;
22017 unsigned int i, count, encoded_filename_len, linebuf_len;
22018 macinfo_entry **slot;
22020 first = &(*macinfo_table)[idx];
22021 second = &(*macinfo_table)[idx + 1];
22023 /* Optimize only if there are at least two consecutive define/undef ops,
22024 and either all of them are before first DW_MACINFO_start_file
22025 with lineno {0,1} (i.e. predefined macro block), or all of them are
22026 in some included header file. */
22027 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
22028 return 0;
22029 if (vec_safe_is_empty (files))
22031 if (first->lineno > 1 || second->lineno > 1)
22032 return 0;
22034 else if (first->lineno == 0)
22035 return 0;
22037 /* Find the last define/undef entry that can be grouped together
22038 with first and at the same time compute md5 checksum of their
22039 codes, linenumbers and strings. */
22040 md5_init_ctx (&ctx);
22041 for (i = idx; macinfo_table->iterate (i, &cur); i++)
22042 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
22043 break;
22044 else if (vec_safe_is_empty (files) && cur->lineno > 1)
22045 break;
22046 else
22048 unsigned char code = cur->code;
22049 md5_process_bytes (&code, 1, &ctx);
22050 checksum_uleb128 (cur->lineno, &ctx);
22051 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
22053 md5_finish_ctx (&ctx, checksum);
22054 count = i - idx;
22056 /* From the containing include filename (if any) pick up just
22057 usable characters from its basename. */
22058 if (vec_safe_is_empty (files))
22059 base = "";
22060 else
22061 base = lbasename (files->last ().info);
22062 for (encoded_filename_len = 0, i = 0; base[i]; i++)
22063 if (ISIDNUM (base[i]) || base[i] == '.')
22064 encoded_filename_len++;
22065 /* Count . at the end. */
22066 if (encoded_filename_len)
22067 encoded_filename_len++;
22069 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
22070 linebuf_len = strlen (linebuf);
22072 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
22073 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
22074 + 16 * 2 + 1);
22075 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
22076 tail = grp_name + 4;
22077 if (encoded_filename_len)
22079 for (i = 0; base[i]; i++)
22080 if (ISIDNUM (base[i]) || base[i] == '.')
22081 *tail++ = base[i];
22082 *tail++ = '.';
22084 memcpy (tail, linebuf, linebuf_len);
22085 tail += linebuf_len;
22086 *tail++ = '.';
22087 for (i = 0; i < 16; i++)
22088 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
22090 /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
22091 in the empty vector entry before the first define/undef. */
22092 inc = &(*macinfo_table)[idx - 1];
22093 inc->code = DW_MACRO_GNU_transparent_include;
22094 inc->lineno = 0;
22095 inc->info = ggc_strdup (grp_name);
22096 if (!*macinfo_htab)
22097 *macinfo_htab = new macinfo_hash_type (10);
22098 /* Avoid emitting duplicates. */
22099 slot = (*macinfo_htab)->find_slot (inc, INSERT);
22100 if (*slot != NULL)
22102 inc->code = 0;
22103 inc->info = NULL;
22104 /* If such an entry has been used before, just emit
22105 a DW_MACRO_GNU_transparent_include op. */
22106 inc = *slot;
22107 output_macinfo_op (inc);
22108 /* And clear all macinfo_entry in the range to avoid emitting them
22109 in the second pass. */
22110 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
22112 cur->code = 0;
22113 cur->info = NULL;
22116 else
22118 *slot = inc;
22119 inc->lineno = (*macinfo_htab)->elements ();
22120 output_macinfo_op (inc);
22122 return count;
22125 /* Save any strings needed by the macinfo table in the debug str
22126 table. All strings must be collected into the table by the time
22127 index_string is called. */
22129 static void
22130 save_macinfo_strings (void)
22132 unsigned len;
22133 unsigned i;
22134 macinfo_entry *ref;
22136 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
22138 switch (ref->code)
22140 /* Match the logic in output_macinfo_op to decide on
22141 indirect strings. */
22142 case DW_MACINFO_define:
22143 case DW_MACINFO_undef:
22144 len = strlen (ref->info) + 1;
22145 if (!dwarf_strict
22146 && len > DWARF_OFFSET_SIZE
22147 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
22148 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
22149 set_indirect_string (find_AT_string (ref->info));
22150 break;
22151 case DW_MACRO_GNU_define_indirect:
22152 case DW_MACRO_GNU_undef_indirect:
22153 set_indirect_string (find_AT_string (ref->info));
22154 break;
22155 default:
22156 break;
22161 /* Output macinfo section(s). */
22163 static void
22164 output_macinfo (void)
22166 unsigned i;
22167 unsigned long length = vec_safe_length (macinfo_table);
22168 macinfo_entry *ref;
22169 vec<macinfo_entry, va_gc> *files = NULL;
22170 macinfo_hash_type *macinfo_htab = NULL;
22172 if (! length)
22173 return;
22175 /* output_macinfo* uses these interchangeably. */
22176 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
22177 && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
22178 && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
22179 && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
22181 /* For .debug_macro emit the section header. */
22182 if (!dwarf_strict)
22184 dw2_asm_output_data (2, 4, "DWARF macro version number");
22185 if (DWARF_OFFSET_SIZE == 8)
22186 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
22187 else
22188 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
22189 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
22190 (!dwarf_split_debug_info ? debug_line_section_label
22191 : debug_skeleton_line_section_label),
22192 debug_line_section, NULL);
22195 /* In the first loop, it emits the primary .debug_macinfo section
22196 and after each emitted op the macinfo_entry is cleared.
22197 If a longer range of define/undef ops can be optimized using
22198 DW_MACRO_GNU_transparent_include, the
22199 DW_MACRO_GNU_transparent_include op is emitted and kept in
22200 the vector before the first define/undef in the range and the
22201 whole range of define/undef ops is not emitted and kept. */
22202 for (i = 0; macinfo_table->iterate (i, &ref); i++)
22204 switch (ref->code)
22206 case DW_MACINFO_start_file:
22207 vec_safe_push (files, *ref);
22208 break;
22209 case DW_MACINFO_end_file:
22210 if (!vec_safe_is_empty (files))
22211 files->pop ();
22212 break;
22213 case DW_MACINFO_define:
22214 case DW_MACINFO_undef:
22215 if (!dwarf_strict
22216 && HAVE_COMDAT_GROUP
22217 && vec_safe_length (files) != 1
22218 && i > 0
22219 && i + 1 < length
22220 && (*macinfo_table)[i - 1].code == 0)
22222 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
22223 if (count)
22225 i += count - 1;
22226 continue;
22229 break;
22230 case 0:
22231 /* A dummy entry may be inserted at the beginning to be able
22232 to optimize the whole block of predefined macros. */
22233 if (i == 0)
22234 continue;
22235 default:
22236 break;
22238 output_macinfo_op (ref);
22239 ref->info = NULL;
22240 ref->code = 0;
22243 if (!macinfo_htab)
22244 return;
22246 delete macinfo_htab;
22247 macinfo_htab = NULL;
22249 /* If any DW_MACRO_GNU_transparent_include were used, on those
22250 DW_MACRO_GNU_transparent_include entries terminate the
22251 current chain and switch to a new comdat .debug_macinfo
22252 section and emit the define/undef entries within it. */
22253 for (i = 0; macinfo_table->iterate (i, &ref); i++)
22254 switch (ref->code)
22256 case 0:
22257 continue;
22258 case DW_MACRO_GNU_transparent_include:
22260 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22261 tree comdat_key = get_identifier (ref->info);
22262 /* Terminate the previous .debug_macinfo section. */
22263 dw2_asm_output_data (1, 0, "End compilation unit");
22264 targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
22265 SECTION_DEBUG
22266 | SECTION_LINKONCE,
22267 comdat_key);
22268 ASM_GENERATE_INTERNAL_LABEL (label,
22269 DEBUG_MACRO_SECTION_LABEL,
22270 ref->lineno);
22271 ASM_OUTPUT_LABEL (asm_out_file, label);
22272 ref->code = 0;
22273 ref->info = NULL;
22274 dw2_asm_output_data (2, 4, "DWARF macro version number");
22275 if (DWARF_OFFSET_SIZE == 8)
22276 dw2_asm_output_data (1, 1, "Flags: 64-bit");
22277 else
22278 dw2_asm_output_data (1, 0, "Flags: 32-bit");
22280 break;
22281 case DW_MACINFO_define:
22282 case DW_MACINFO_undef:
22283 output_macinfo_op (ref);
22284 ref->code = 0;
22285 ref->info = NULL;
22286 break;
22287 default:
22288 gcc_unreachable ();
22292 /* Set up for Dwarf output at the start of compilation. */
22294 static void
22295 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
22297 /* Allocate the file_table. */
22298 file_table = htab_create_ggc (50, file_table_hash,
22299 file_table_eq, NULL);
22301 /* Allocate the decl_die_table. */
22302 decl_die_table = htab_create_ggc (10, decl_die_table_hash,
22303 decl_die_table_eq, NULL);
22305 /* Allocate the decl_loc_table. */
22306 decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
22307 decl_loc_table_eq, NULL);
22309 /* Allocate the cached_dw_loc_list_table. */
22310 cached_dw_loc_list_table
22311 = htab_create_ggc (10, cached_dw_loc_list_table_hash,
22312 cached_dw_loc_list_table_eq, NULL);
22314 /* Allocate the initial hunk of the decl_scope_table. */
22315 vec_alloc (decl_scope_table, 256);
22317 /* Allocate the initial hunk of the abbrev_die_table. */
22318 abbrev_die_table = ggc_cleared_vec_alloc<dw_die_ref>
22319 (ABBREV_DIE_TABLE_INCREMENT);
22320 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
22321 /* Zero-th entry is allocated, but unused. */
22322 abbrev_die_table_in_use = 1;
22324 /* Allocate the pubtypes and pubnames vectors. */
22325 vec_alloc (pubname_table, 32);
22326 vec_alloc (pubtype_table, 32);
22328 vec_alloc (incomplete_types, 64);
22330 vec_alloc (used_rtx_array, 32);
22332 if (!dwarf_split_debug_info)
22334 debug_info_section = get_section (DEBUG_INFO_SECTION,
22335 SECTION_DEBUG, NULL);
22336 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22337 SECTION_DEBUG, NULL);
22338 debug_loc_section = get_section (DEBUG_LOC_SECTION,
22339 SECTION_DEBUG, NULL);
22341 else
22343 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
22344 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22345 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
22346 SECTION_DEBUG | SECTION_EXCLUDE,
22347 NULL);
22348 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
22349 SECTION_DEBUG, NULL);
22350 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
22351 SECTION_DEBUG, NULL);
22352 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22353 SECTION_DEBUG, NULL);
22354 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
22355 DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
22357 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
22358 the main .o, but the skeleton_line goes into the split off dwo. */
22359 debug_skeleton_line_section
22360 = get_section (DEBUG_DWO_LINE_SECTION,
22361 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22362 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
22363 DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
22364 debug_str_offsets_section = get_section (DEBUG_STR_OFFSETS_SECTION,
22365 SECTION_DEBUG | SECTION_EXCLUDE,
22366 NULL);
22367 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
22368 DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
22369 debug_loc_section = get_section (DEBUG_DWO_LOC_SECTION,
22370 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22371 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
22372 DEBUG_STR_DWO_SECTION_FLAGS, NULL);
22374 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
22375 SECTION_DEBUG, NULL);
22376 debug_macinfo_section = get_section (dwarf_strict
22377 ? DEBUG_MACINFO_SECTION
22378 : DEBUG_MACRO_SECTION,
22379 DEBUG_MACRO_SECTION_FLAGS, NULL);
22380 debug_line_section = get_section (DEBUG_LINE_SECTION,
22381 SECTION_DEBUG, NULL);
22382 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
22383 SECTION_DEBUG, NULL);
22384 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
22385 SECTION_DEBUG, NULL);
22386 debug_str_section = get_section (DEBUG_STR_SECTION,
22387 DEBUG_STR_SECTION_FLAGS, NULL);
22388 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
22389 SECTION_DEBUG, NULL);
22390 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
22391 SECTION_DEBUG, NULL);
22393 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
22394 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
22395 DEBUG_ABBREV_SECTION_LABEL, 0);
22396 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
22397 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
22398 COLD_TEXT_SECTION_LABEL, 0);
22399 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
22401 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
22402 DEBUG_INFO_SECTION_LABEL, 0);
22403 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
22404 DEBUG_LINE_SECTION_LABEL, 0);
22405 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
22406 DEBUG_RANGES_SECTION_LABEL, 0);
22407 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
22408 DEBUG_ADDR_SECTION_LABEL, 0);
22409 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
22410 dwarf_strict
22411 ? DEBUG_MACINFO_SECTION_LABEL
22412 : DEBUG_MACRO_SECTION_LABEL, 0);
22413 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
22415 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22416 vec_alloc (macinfo_table, 64);
22418 switch_to_section (text_section);
22419 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
22421 /* Make sure the line number table for .text always exists. */
22422 text_section_line_info = new_line_info_table ();
22423 text_section_line_info->end_label = text_end_label;
22426 /* Called before compile () starts outputtting functions, variables
22427 and toplevel asms into assembly. */
22429 static void
22430 dwarf2out_assembly_start (void)
22432 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
22433 && dwarf2out_do_cfi_asm ()
22434 && (!(flag_unwind_tables || flag_exceptions)
22435 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
22436 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
22439 /* A helper function for dwarf2out_finish called through
22440 htab_traverse. Assign a string its index. All strings must be
22441 collected into the table by the time index_string is called,
22442 because the indexing code relies on htab_traverse to traverse nodes
22443 in the same order for each run. */
22445 static int
22446 index_string (void **h, void *v)
22448 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22449 unsigned int *index = (unsigned int *) v;
22451 find_string_form (node);
22452 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22454 gcc_assert (node->index == NO_INDEX_ASSIGNED);
22455 node->index = *index;
22456 *index += 1;
22458 return 1;
22461 /* A helper function for output_indirect_strings called through
22462 htab_traverse. Output the offset to a string and update the
22463 current offset. */
22465 static int
22466 output_index_string_offset (void **h, void *v)
22468 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22469 unsigned int *offset = (unsigned int *) v;
22471 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22473 /* Assert that this node has been assigned an index. */
22474 gcc_assert (node->index != NO_INDEX_ASSIGNED
22475 && node->index != NOT_INDEXED);
22476 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
22477 "indexed string 0x%x: %s", node->index, node->str);
22478 *offset += strlen (node->str) + 1;
22480 return 1;
22483 /* A helper function for dwarf2out_finish called through
22484 htab_traverse. Output the indexed string. */
22486 static int
22487 output_index_string (void **h, void *v)
22489 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22490 unsigned int *cur_idx = (unsigned int *) v;
22492 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22494 /* Assert that the strings are output in the same order as their
22495 indexes were assigned. */
22496 gcc_assert (*cur_idx == node->index);
22497 assemble_string (node->str, strlen (node->str) + 1);
22498 *cur_idx += 1;
22500 return 1;
22503 /* A helper function for dwarf2out_finish called through
22504 htab_traverse. Emit one queued .debug_str string. */
22506 static int
22507 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
22509 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22511 node->form = find_string_form (node);
22512 if (node->form == DW_FORM_strp && node->refcount > 0)
22514 ASM_OUTPUT_LABEL (asm_out_file, node->label);
22515 assemble_string (node->str, strlen (node->str) + 1);
22518 return 1;
22521 /* Output the indexed string table. */
22523 static void
22524 output_indirect_strings (void)
22526 switch_to_section (debug_str_section);
22527 if (!dwarf_split_debug_info)
22528 htab_traverse (debug_str_hash, output_indirect_string, NULL);
22529 else
22531 unsigned int offset = 0;
22532 unsigned int cur_idx = 0;
22534 htab_traverse (skeleton_debug_str_hash, output_indirect_string, NULL);
22536 switch_to_section (debug_str_offsets_section);
22537 htab_traverse_noresize (debug_str_hash,
22538 output_index_string_offset,
22539 &offset);
22540 switch_to_section (debug_str_dwo_section);
22541 htab_traverse_noresize (debug_str_hash,
22542 output_index_string,
22543 &cur_idx);
22547 /* Callback for htab_traverse to assign an index to an entry in the
22548 table, and to write that entry to the .debug_addr section. */
22550 static int
22551 output_addr_table_entry (void **slot, void *data)
22553 addr_table_entry *entry = (addr_table_entry *) *slot;
22554 unsigned int *cur_index = (unsigned int *)data;
22556 if (entry->refcount == 0)
22558 gcc_assert (entry->index == NO_INDEX_ASSIGNED
22559 || entry->index == NOT_INDEXED);
22560 return 1;
22563 gcc_assert (entry->index == *cur_index);
22564 (*cur_index)++;
22566 switch (entry->kind)
22568 case ate_kind_rtx:
22569 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
22570 "0x%x", entry->index);
22571 break;
22572 case ate_kind_rtx_dtprel:
22573 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
22574 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
22575 DWARF2_ADDR_SIZE,
22576 entry->addr.rtl);
22577 fputc ('\n', asm_out_file);
22578 break;
22579 case ate_kind_label:
22580 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
22581 "0x%x", entry->index);
22582 break;
22583 default:
22584 gcc_unreachable ();
22586 return 1;
22589 /* Produce the .debug_addr section. */
22591 static void
22592 output_addr_table (void)
22594 unsigned int index = 0;
22595 if (addr_index_table == NULL || htab_size (addr_index_table) == 0)
22596 return;
22598 switch_to_section (debug_addr_section);
22599 htab_traverse_noresize (addr_index_table, output_addr_table_entry, &index);
22602 #if ENABLE_ASSERT_CHECKING
22603 /* Verify that all marks are clear. */
22605 static void
22606 verify_marks_clear (dw_die_ref die)
22608 dw_die_ref c;
22610 gcc_assert (! die->die_mark);
22611 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
22613 #endif /* ENABLE_ASSERT_CHECKING */
22615 /* Clear the marks for a die and its children.
22616 Be cool if the mark isn't set. */
22618 static void
22619 prune_unmark_dies (dw_die_ref die)
22621 dw_die_ref c;
22623 if (die->die_mark)
22624 die->die_mark = 0;
22625 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
22628 /* Given DIE that we're marking as used, find any other dies
22629 it references as attributes and mark them as used. */
22631 static void
22632 prune_unused_types_walk_attribs (dw_die_ref die)
22634 dw_attr_ref a;
22635 unsigned ix;
22637 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22639 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
22641 /* A reference to another DIE.
22642 Make sure that it will get emitted.
22643 If it was broken out into a comdat group, don't follow it. */
22644 if (! AT_ref (a)->comdat_type_p
22645 || a->dw_attr == DW_AT_specification)
22646 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
22648 /* Set the string's refcount to 0 so that prune_unused_types_mark
22649 accounts properly for it. */
22650 if (AT_class (a) == dw_val_class_str)
22651 a->dw_attr_val.v.val_str->refcount = 0;
22655 /* Mark the generic parameters and arguments children DIEs of DIE. */
22657 static void
22658 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
22660 dw_die_ref c;
22662 if (die == NULL || die->die_child == NULL)
22663 return;
22664 c = die->die_child;
22667 if (is_template_parameter (c))
22668 prune_unused_types_mark (c, 1);
22669 c = c->die_sib;
22670 } while (c && c != die->die_child);
22673 /* Mark DIE as being used. If DOKIDS is true, then walk down
22674 to DIE's children. */
22676 static void
22677 prune_unused_types_mark (dw_die_ref die, int dokids)
22679 dw_die_ref c;
22681 if (die->die_mark == 0)
22683 /* We haven't done this node yet. Mark it as used. */
22684 die->die_mark = 1;
22685 /* If this is the DIE of a generic type instantiation,
22686 mark the children DIEs that describe its generic parms and
22687 args. */
22688 prune_unused_types_mark_generic_parms_dies (die);
22690 /* We also have to mark its parents as used.
22691 (But we don't want to mark our parent's kids due to this,
22692 unless it is a class.) */
22693 if (die->die_parent)
22694 prune_unused_types_mark (die->die_parent,
22695 class_scope_p (die->die_parent));
22697 /* Mark any referenced nodes. */
22698 prune_unused_types_walk_attribs (die);
22700 /* If this node is a specification,
22701 also mark the definition, if it exists. */
22702 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
22703 prune_unused_types_mark (die->die_definition, 1);
22706 if (dokids && die->die_mark != 2)
22708 /* We need to walk the children, but haven't done so yet.
22709 Remember that we've walked the kids. */
22710 die->die_mark = 2;
22712 /* If this is an array type, we need to make sure our
22713 kids get marked, even if they're types. If we're
22714 breaking out types into comdat sections, do this
22715 for all type definitions. */
22716 if (die->die_tag == DW_TAG_array_type
22717 || (use_debug_types
22718 && is_type_die (die) && ! is_declaration_die (die)))
22719 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
22720 else
22721 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22725 /* For local classes, look if any static member functions were emitted
22726 and if so, mark them. */
22728 static void
22729 prune_unused_types_walk_local_classes (dw_die_ref die)
22731 dw_die_ref c;
22733 if (die->die_mark == 2)
22734 return;
22736 switch (die->die_tag)
22738 case DW_TAG_structure_type:
22739 case DW_TAG_union_type:
22740 case DW_TAG_class_type:
22741 break;
22743 case DW_TAG_subprogram:
22744 if (!get_AT_flag (die, DW_AT_declaration)
22745 || die->die_definition != NULL)
22746 prune_unused_types_mark (die, 1);
22747 return;
22749 default:
22750 return;
22753 /* Mark children. */
22754 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
22757 /* Walk the tree DIE and mark types that we actually use. */
22759 static void
22760 prune_unused_types_walk (dw_die_ref die)
22762 dw_die_ref c;
22764 /* Don't do anything if this node is already marked and
22765 children have been marked as well. */
22766 if (die->die_mark == 2)
22767 return;
22769 switch (die->die_tag)
22771 case DW_TAG_structure_type:
22772 case DW_TAG_union_type:
22773 case DW_TAG_class_type:
22774 if (die->die_perennial_p)
22775 break;
22777 for (c = die->die_parent; c; c = c->die_parent)
22778 if (c->die_tag == DW_TAG_subprogram)
22779 break;
22781 /* Finding used static member functions inside of classes
22782 is needed just for local classes, because for other classes
22783 static member function DIEs with DW_AT_specification
22784 are emitted outside of the DW_TAG_*_type. If we ever change
22785 it, we'd need to call this even for non-local classes. */
22786 if (c)
22787 prune_unused_types_walk_local_classes (die);
22789 /* It's a type node --- don't mark it. */
22790 return;
22792 case DW_TAG_const_type:
22793 case DW_TAG_packed_type:
22794 case DW_TAG_pointer_type:
22795 case DW_TAG_reference_type:
22796 case DW_TAG_rvalue_reference_type:
22797 case DW_TAG_volatile_type:
22798 case DW_TAG_typedef:
22799 case DW_TAG_array_type:
22800 case DW_TAG_interface_type:
22801 case DW_TAG_friend:
22802 case DW_TAG_variant_part:
22803 case DW_TAG_enumeration_type:
22804 case DW_TAG_subroutine_type:
22805 case DW_TAG_string_type:
22806 case DW_TAG_set_type:
22807 case DW_TAG_subrange_type:
22808 case DW_TAG_ptr_to_member_type:
22809 case DW_TAG_file_type:
22810 if (die->die_perennial_p)
22811 break;
22813 /* It's a type node --- don't mark it. */
22814 return;
22816 default:
22817 /* Mark everything else. */
22818 break;
22821 if (die->die_mark == 0)
22823 die->die_mark = 1;
22825 /* Now, mark any dies referenced from here. */
22826 prune_unused_types_walk_attribs (die);
22829 die->die_mark = 2;
22831 /* Mark children. */
22832 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22835 /* Increment the string counts on strings referred to from DIE's
22836 attributes. */
22838 static void
22839 prune_unused_types_update_strings (dw_die_ref die)
22841 dw_attr_ref a;
22842 unsigned ix;
22844 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22845 if (AT_class (a) == dw_val_class_str)
22847 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
22848 s->refcount++;
22849 /* Avoid unnecessarily putting strings that are used less than
22850 twice in the hash table. */
22851 if (s->refcount
22852 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
22854 void ** slot;
22855 slot = htab_find_slot_with_hash (debug_str_hash, s->str,
22856 htab_hash_string (s->str),
22857 INSERT);
22858 gcc_assert (*slot == NULL);
22859 *slot = s;
22864 /* Remove from the tree DIE any dies that aren't marked. */
22866 static void
22867 prune_unused_types_prune (dw_die_ref die)
22869 dw_die_ref c;
22871 gcc_assert (die->die_mark);
22872 prune_unused_types_update_strings (die);
22874 if (! die->die_child)
22875 return;
22877 c = die->die_child;
22878 do {
22879 dw_die_ref prev = c;
22880 for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
22881 if (c == die->die_child)
22883 /* No marked children between 'prev' and the end of the list. */
22884 if (prev == c)
22885 /* No marked children at all. */
22886 die->die_child = NULL;
22887 else
22889 prev->die_sib = c->die_sib;
22890 die->die_child = prev;
22892 return;
22895 if (c != prev->die_sib)
22896 prev->die_sib = c;
22897 prune_unused_types_prune (c);
22898 } while (c != die->die_child);
22901 /* Remove dies representing declarations that we never use. */
22903 static void
22904 prune_unused_types (void)
22906 unsigned int i;
22907 limbo_die_node *node;
22908 comdat_type_node *ctnode;
22909 pubname_ref pub;
22910 dw_die_ref base_type;
22912 #if ENABLE_ASSERT_CHECKING
22913 /* All the marks should already be clear. */
22914 verify_marks_clear (comp_unit_die ());
22915 for (node = limbo_die_list; node; node = node->next)
22916 verify_marks_clear (node->die);
22917 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22918 verify_marks_clear (ctnode->root_die);
22919 #endif /* ENABLE_ASSERT_CHECKING */
22921 /* Mark types that are used in global variables. */
22922 premark_types_used_by_global_vars ();
22924 /* Set the mark on nodes that are actually used. */
22925 prune_unused_types_walk (comp_unit_die ());
22926 for (node = limbo_die_list; node; node = node->next)
22927 prune_unused_types_walk (node->die);
22928 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22930 prune_unused_types_walk (ctnode->root_die);
22931 prune_unused_types_mark (ctnode->type_die, 1);
22934 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
22935 are unusual in that they are pubnames that are the children of pubtypes.
22936 They should only be marked via their parent DW_TAG_enumeration_type die,
22937 not as roots in themselves. */
22938 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
22939 if (pub->die->die_tag != DW_TAG_enumerator)
22940 prune_unused_types_mark (pub->die, 1);
22941 for (i = 0; base_types.iterate (i, &base_type); i++)
22942 prune_unused_types_mark (base_type, 1);
22944 if (debug_str_hash)
22945 htab_empty (debug_str_hash);
22946 if (skeleton_debug_str_hash)
22947 htab_empty (skeleton_debug_str_hash);
22948 prune_unused_types_prune (comp_unit_die ());
22949 for (node = limbo_die_list; node; node = node->next)
22950 prune_unused_types_prune (node->die);
22951 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22952 prune_unused_types_prune (ctnode->root_die);
22954 /* Leave the marks clear. */
22955 prune_unmark_dies (comp_unit_die ());
22956 for (node = limbo_die_list; node; node = node->next)
22957 prune_unmark_dies (node->die);
22958 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22959 prune_unmark_dies (ctnode->root_die);
22962 /* Set the parameter to true if there are any relative pathnames in
22963 the file table. */
22964 static int
22965 file_table_relative_p (void ** slot, void *param)
22967 bool *p = (bool *) param;
22968 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
22969 if (!IS_ABSOLUTE_PATH (d->filename))
22971 *p = true;
22972 return 0;
22974 return 1;
22977 /* Helpers to manipulate hash table of comdat type units. */
22979 struct comdat_type_hasher : typed_noop_remove <comdat_type_node>
22981 typedef comdat_type_node value_type;
22982 typedef comdat_type_node compare_type;
22983 static inline hashval_t hash (const value_type *);
22984 static inline bool equal (const value_type *, const compare_type *);
22987 inline hashval_t
22988 comdat_type_hasher::hash (const value_type *type_node)
22990 hashval_t h;
22991 memcpy (&h, type_node->signature, sizeof (h));
22992 return h;
22995 inline bool
22996 comdat_type_hasher::equal (const value_type *type_node_1,
22997 const compare_type *type_node_2)
22999 return (! memcmp (type_node_1->signature, type_node_2->signature,
23000 DWARF_TYPE_SIGNATURE_SIZE));
23003 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
23004 to the location it would have been added, should we know its
23005 DECL_ASSEMBLER_NAME when we added other attributes. This will
23006 probably improve compactness of debug info, removing equivalent
23007 abbrevs, and hide any differences caused by deferring the
23008 computation of the assembler name, triggered by e.g. PCH. */
23010 static inline void
23011 move_linkage_attr (dw_die_ref die)
23013 unsigned ix = vec_safe_length (die->die_attr);
23014 dw_attr_node linkage = (*die->die_attr)[ix - 1];
23016 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
23017 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
23019 while (--ix > 0)
23021 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
23023 if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
23024 break;
23027 if (ix != vec_safe_length (die->die_attr) - 1)
23029 die->die_attr->pop ();
23030 die->die_attr->quick_insert (ix, linkage);
23034 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
23035 referenced from typed stack ops and count how often they are used. */
23037 static void
23038 mark_base_types (dw_loc_descr_ref loc)
23040 dw_die_ref base_type = NULL;
23042 for (; loc; loc = loc->dw_loc_next)
23044 switch (loc->dw_loc_opc)
23046 case DW_OP_GNU_regval_type:
23047 case DW_OP_GNU_deref_type:
23048 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
23049 break;
23050 case DW_OP_GNU_convert:
23051 case DW_OP_GNU_reinterpret:
23052 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
23053 continue;
23054 /* FALLTHRU */
23055 case DW_OP_GNU_const_type:
23056 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
23057 break;
23058 case DW_OP_GNU_entry_value:
23059 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
23060 continue;
23061 default:
23062 continue;
23064 gcc_assert (base_type->die_parent == comp_unit_die ());
23065 if (base_type->die_mark)
23066 base_type->die_mark++;
23067 else
23069 base_types.safe_push (base_type);
23070 base_type->die_mark = 1;
23075 /* Comparison function for sorting marked base types. */
23077 static int
23078 base_type_cmp (const void *x, const void *y)
23080 dw_die_ref dx = *(const dw_die_ref *) x;
23081 dw_die_ref dy = *(const dw_die_ref *) y;
23082 unsigned int byte_size1, byte_size2;
23083 unsigned int encoding1, encoding2;
23084 if (dx->die_mark > dy->die_mark)
23085 return -1;
23086 if (dx->die_mark < dy->die_mark)
23087 return 1;
23088 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
23089 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
23090 if (byte_size1 < byte_size2)
23091 return 1;
23092 if (byte_size1 > byte_size2)
23093 return -1;
23094 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
23095 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
23096 if (encoding1 < encoding2)
23097 return 1;
23098 if (encoding1 > encoding2)
23099 return -1;
23100 return 0;
23103 /* Move base types marked by mark_base_types as early as possible
23104 in the CU, sorted by decreasing usage count both to make the
23105 uleb128 references as small as possible and to make sure they
23106 will have die_offset already computed by calc_die_sizes when
23107 sizes of typed stack loc ops is computed. */
23109 static void
23110 move_marked_base_types (void)
23112 unsigned int i;
23113 dw_die_ref base_type, die, c;
23115 if (base_types.is_empty ())
23116 return;
23118 /* Sort by decreasing usage count, they will be added again in that
23119 order later on. */
23120 base_types.qsort (base_type_cmp);
23121 die = comp_unit_die ();
23122 c = die->die_child;
23125 dw_die_ref prev = c;
23126 c = c->die_sib;
23127 while (c->die_mark)
23129 remove_child_with_prev (c, prev);
23130 /* As base types got marked, there must be at least
23131 one node other than DW_TAG_base_type. */
23132 gcc_assert (c != c->die_sib);
23133 c = c->die_sib;
23136 while (c != die->die_child);
23137 gcc_assert (die->die_child);
23138 c = die->die_child;
23139 for (i = 0; base_types.iterate (i, &base_type); i++)
23141 base_type->die_mark = 0;
23142 base_type->die_sib = c->die_sib;
23143 c->die_sib = base_type;
23144 c = base_type;
23148 /* Helper function for resolve_addr, attempt to resolve
23149 one CONST_STRING, return true if successful. Similarly verify that
23150 SYMBOL_REFs refer to variables emitted in the current CU. */
23152 static bool
23153 resolve_one_addr (rtx *addr)
23155 rtx rtl = *addr;
23157 if (GET_CODE (rtl) == CONST_STRING)
23159 size_t len = strlen (XSTR (rtl, 0)) + 1;
23160 tree t = build_string (len, XSTR (rtl, 0));
23161 tree tlen = size_int (len - 1);
23162 TREE_TYPE (t)
23163 = build_array_type (char_type_node, build_index_type (tlen));
23164 rtl = lookup_constant_def (t);
23165 if (!rtl || !MEM_P (rtl))
23166 return false;
23167 rtl = XEXP (rtl, 0);
23168 if (GET_CODE (rtl) == SYMBOL_REF
23169 && SYMBOL_REF_DECL (rtl)
23170 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
23171 return false;
23172 vec_safe_push (used_rtx_array, rtl);
23173 *addr = rtl;
23174 return true;
23177 if (GET_CODE (rtl) == SYMBOL_REF
23178 && SYMBOL_REF_DECL (rtl))
23180 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
23182 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
23183 return false;
23185 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
23186 return false;
23189 if (GET_CODE (rtl) == CONST)
23191 subrtx_ptr_iterator::array_type array;
23192 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
23193 if (!resolve_one_addr (*iter))
23194 return false;
23197 return true;
23200 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
23201 if possible, and create DW_TAG_dwarf_procedure that can be referenced
23202 from DW_OP_GNU_implicit_pointer if the string hasn't been seen yet. */
23204 static rtx
23205 string_cst_pool_decl (tree t)
23207 rtx rtl = output_constant_def (t, 1);
23208 unsigned char *array;
23209 dw_loc_descr_ref l;
23210 tree decl;
23211 size_t len;
23212 dw_die_ref ref;
23214 if (!rtl || !MEM_P (rtl))
23215 return NULL_RTX;
23216 rtl = XEXP (rtl, 0);
23217 if (GET_CODE (rtl) != SYMBOL_REF
23218 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
23219 return NULL_RTX;
23221 decl = SYMBOL_REF_DECL (rtl);
23222 if (!lookup_decl_die (decl))
23224 len = TREE_STRING_LENGTH (t);
23225 vec_safe_push (used_rtx_array, rtl);
23226 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
23227 array = ggc_vec_alloc<unsigned char> (len);
23228 memcpy (array, TREE_STRING_POINTER (t), len);
23229 l = new_loc_descr (DW_OP_implicit_value, len, 0);
23230 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
23231 l->dw_loc_oprnd2.v.val_vec.length = len;
23232 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
23233 l->dw_loc_oprnd2.v.val_vec.array = array;
23234 add_AT_loc (ref, DW_AT_location, l);
23235 equate_decl_number_to_die (decl, ref);
23237 return rtl;
23240 /* Helper function of resolve_addr_in_expr. LOC is
23241 a DW_OP_addr followed by DW_OP_stack_value, either at the start
23242 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
23243 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
23244 with DW_OP_GNU_implicit_pointer if possible
23245 and return true, if unsuccessful, return false. */
23247 static bool
23248 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
23250 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
23251 HOST_WIDE_INT offset = 0;
23252 dw_die_ref ref = NULL;
23253 tree decl;
23255 if (GET_CODE (rtl) == CONST
23256 && GET_CODE (XEXP (rtl, 0)) == PLUS
23257 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
23259 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
23260 rtl = XEXP (XEXP (rtl, 0), 0);
23262 if (GET_CODE (rtl) == CONST_STRING)
23264 size_t len = strlen (XSTR (rtl, 0)) + 1;
23265 tree t = build_string (len, XSTR (rtl, 0));
23266 tree tlen = size_int (len - 1);
23268 TREE_TYPE (t)
23269 = build_array_type (char_type_node, build_index_type (tlen));
23270 rtl = string_cst_pool_decl (t);
23271 if (!rtl)
23272 return false;
23274 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
23276 decl = SYMBOL_REF_DECL (rtl);
23277 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
23279 ref = lookup_decl_die (decl);
23280 if (ref && (get_AT (ref, DW_AT_location)
23281 || get_AT (ref, DW_AT_const_value)))
23283 loc->dw_loc_opc = DW_OP_GNU_implicit_pointer;
23284 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23285 loc->dw_loc_oprnd1.val_entry = NULL;
23286 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
23287 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
23288 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
23289 loc->dw_loc_oprnd2.v.val_int = offset;
23290 return true;
23294 return false;
23297 /* Helper function for resolve_addr, handle one location
23298 expression, return false if at least one CONST_STRING or SYMBOL_REF in
23299 the location list couldn't be resolved. */
23301 static bool
23302 resolve_addr_in_expr (dw_loc_descr_ref loc)
23304 dw_loc_descr_ref keep = NULL;
23305 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
23306 switch (loc->dw_loc_opc)
23308 case DW_OP_addr:
23309 if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
23311 if ((prev == NULL
23312 || prev->dw_loc_opc == DW_OP_piece
23313 || prev->dw_loc_opc == DW_OP_bit_piece)
23314 && loc->dw_loc_next
23315 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
23316 && !dwarf_strict
23317 && optimize_one_addr_into_implicit_ptr (loc))
23318 break;
23319 return false;
23321 break;
23322 case DW_OP_GNU_addr_index:
23323 case DW_OP_GNU_const_index:
23324 if (loc->dw_loc_opc == DW_OP_GNU_addr_index
23325 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
23327 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
23328 if (!resolve_one_addr (&rtl))
23329 return false;
23330 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
23331 loc->dw_loc_oprnd1.val_entry =
23332 add_addr_table_entry (rtl, ate_kind_rtx);
23334 break;
23335 case DW_OP_const4u:
23336 case DW_OP_const8u:
23337 if (loc->dtprel
23338 && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
23339 return false;
23340 break;
23341 case DW_OP_plus_uconst:
23342 if (size_of_loc_descr (loc)
23343 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
23345 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
23347 dw_loc_descr_ref repl
23348 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
23349 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
23350 add_loc_descr (&repl, loc->dw_loc_next);
23351 *loc = *repl;
23353 break;
23354 case DW_OP_implicit_value:
23355 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
23356 && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
23357 return false;
23358 break;
23359 case DW_OP_GNU_implicit_pointer:
23360 case DW_OP_GNU_parameter_ref:
23361 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
23363 dw_die_ref ref
23364 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
23365 if (ref == NULL)
23366 return false;
23367 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23368 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
23369 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
23371 break;
23372 case DW_OP_GNU_const_type:
23373 case DW_OP_GNU_regval_type:
23374 case DW_OP_GNU_deref_type:
23375 case DW_OP_GNU_convert:
23376 case DW_OP_GNU_reinterpret:
23377 while (loc->dw_loc_next
23378 && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
23380 dw_die_ref base1, base2;
23381 unsigned enc1, enc2, size1, size2;
23382 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
23383 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
23384 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
23385 else if (loc->dw_loc_oprnd1.val_class
23386 == dw_val_class_unsigned_const)
23387 break;
23388 else
23389 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
23390 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
23391 == dw_val_class_unsigned_const)
23392 break;
23393 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
23394 gcc_assert (base1->die_tag == DW_TAG_base_type
23395 && base2->die_tag == DW_TAG_base_type);
23396 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
23397 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
23398 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
23399 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
23400 if (size1 == size2
23401 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
23402 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
23403 && loc != keep)
23404 || enc1 == enc2))
23406 /* Optimize away next DW_OP_GNU_convert after
23407 adjusting LOC's base type die reference. */
23408 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
23409 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
23410 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
23411 else
23412 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
23413 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
23414 continue;
23416 /* Don't change integer DW_OP_GNU_convert after e.g. floating
23417 point typed stack entry. */
23418 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
23419 keep = loc->dw_loc_next;
23420 break;
23422 break;
23423 default:
23424 break;
23426 return true;
23429 /* Helper function of resolve_addr. DIE had DW_AT_location of
23430 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
23431 and DW_OP_addr couldn't be resolved. resolve_addr has already
23432 removed the DW_AT_location attribute. This function attempts to
23433 add a new DW_AT_location attribute with DW_OP_GNU_implicit_pointer
23434 to it or DW_AT_const_value attribute, if possible. */
23436 static void
23437 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
23439 if (TREE_CODE (decl) != VAR_DECL
23440 || lookup_decl_die (decl) != die
23441 || DECL_EXTERNAL (decl)
23442 || !TREE_STATIC (decl)
23443 || DECL_INITIAL (decl) == NULL_TREE
23444 || DECL_P (DECL_INITIAL (decl))
23445 || get_AT (die, DW_AT_const_value))
23446 return;
23448 tree init = DECL_INITIAL (decl);
23449 HOST_WIDE_INT offset = 0;
23450 /* For variables that have been optimized away and thus
23451 don't have a memory location, see if we can emit
23452 DW_AT_const_value instead. */
23453 if (tree_add_const_value_attribute (die, init))
23454 return;
23455 if (dwarf_strict)
23456 return;
23457 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
23458 and ADDR_EXPR refers to a decl that has DW_AT_location or
23459 DW_AT_const_value (but isn't addressable, otherwise
23460 resolving the original DW_OP_addr wouldn't fail), see if
23461 we can add DW_OP_GNU_implicit_pointer. */
23462 STRIP_NOPS (init);
23463 if (TREE_CODE (init) == POINTER_PLUS_EXPR
23464 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
23466 offset = tree_to_shwi (TREE_OPERAND (init, 1));
23467 init = TREE_OPERAND (init, 0);
23468 STRIP_NOPS (init);
23470 if (TREE_CODE (init) != ADDR_EXPR)
23471 return;
23472 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
23473 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
23474 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
23475 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
23476 && TREE_OPERAND (init, 0) != decl))
23478 dw_die_ref ref;
23479 dw_loc_descr_ref l;
23481 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
23483 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
23484 if (!rtl)
23485 return;
23486 decl = SYMBOL_REF_DECL (rtl);
23488 else
23489 decl = TREE_OPERAND (init, 0);
23490 ref = lookup_decl_die (decl);
23491 if (ref == NULL
23492 || (!get_AT (ref, DW_AT_location)
23493 && !get_AT (ref, DW_AT_const_value)))
23494 return;
23495 l = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
23496 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23497 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
23498 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
23499 add_AT_loc (die, DW_AT_location, l);
23503 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
23504 an address in .rodata section if the string literal is emitted there,
23505 or remove the containing location list or replace DW_AT_const_value
23506 with DW_AT_location and empty location expression, if it isn't found
23507 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
23508 to something that has been emitted in the current CU. */
23510 static void
23511 resolve_addr (dw_die_ref die)
23513 dw_die_ref c;
23514 dw_attr_ref a;
23515 dw_loc_list_ref *curr, *start, loc;
23516 unsigned ix;
23518 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23519 switch (AT_class (a))
23521 case dw_val_class_loc_list:
23522 start = curr = AT_loc_list_ptr (a);
23523 loc = *curr;
23524 gcc_assert (loc);
23525 /* The same list can be referenced more than once. See if we have
23526 already recorded the result from a previous pass. */
23527 if (loc->replaced)
23528 *curr = loc->dw_loc_next;
23529 else if (!loc->resolved_addr)
23531 /* As things stand, we do not expect or allow one die to
23532 reference a suffix of another die's location list chain.
23533 References must be identical or completely separate.
23534 There is therefore no need to cache the result of this
23535 pass on any list other than the first; doing so
23536 would lead to unnecessary writes. */
23537 while (*curr)
23539 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
23540 if (!resolve_addr_in_expr ((*curr)->expr))
23542 dw_loc_list_ref next = (*curr)->dw_loc_next;
23543 dw_loc_descr_ref l = (*curr)->expr;
23545 if (next && (*curr)->ll_symbol)
23547 gcc_assert (!next->ll_symbol);
23548 next->ll_symbol = (*curr)->ll_symbol;
23550 if (dwarf_split_debug_info)
23551 remove_loc_list_addr_table_entries (l);
23552 *curr = next;
23554 else
23556 mark_base_types ((*curr)->expr);
23557 curr = &(*curr)->dw_loc_next;
23560 if (loc == *start)
23561 loc->resolved_addr = 1;
23562 else
23564 loc->replaced = 1;
23565 loc->dw_loc_next = *start;
23568 if (!*start)
23570 remove_AT (die, a->dw_attr);
23571 ix--;
23573 break;
23574 case dw_val_class_loc:
23576 dw_loc_descr_ref l = AT_loc (a);
23577 /* For -gdwarf-2 don't attempt to optimize
23578 DW_AT_data_member_location containing
23579 DW_OP_plus_uconst - older consumers might
23580 rely on it being that op instead of a more complex,
23581 but shorter, location description. */
23582 if ((dwarf_version > 2
23583 || a->dw_attr != DW_AT_data_member_location
23584 || l == NULL
23585 || l->dw_loc_opc != DW_OP_plus_uconst
23586 || l->dw_loc_next != NULL)
23587 && !resolve_addr_in_expr (l))
23589 if (dwarf_split_debug_info)
23590 remove_loc_list_addr_table_entries (l);
23591 if (l != NULL
23592 && l->dw_loc_next == NULL
23593 && l->dw_loc_opc == DW_OP_addr
23594 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
23595 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
23596 && a->dw_attr == DW_AT_location)
23598 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
23599 remove_AT (die, a->dw_attr);
23600 ix--;
23601 optimize_location_into_implicit_ptr (die, decl);
23602 break;
23604 remove_AT (die, a->dw_attr);
23605 ix--;
23607 else
23608 mark_base_types (l);
23610 break;
23611 case dw_val_class_addr:
23612 if (a->dw_attr == DW_AT_const_value
23613 && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
23615 if (AT_index (a) != NOT_INDEXED)
23616 remove_addr_table_entry (a->dw_attr_val.val_entry);
23617 remove_AT (die, a->dw_attr);
23618 ix--;
23620 if (die->die_tag == DW_TAG_GNU_call_site
23621 && a->dw_attr == DW_AT_abstract_origin)
23623 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
23624 dw_die_ref tdie = lookup_decl_die (tdecl);
23625 if (tdie == NULL
23626 && DECL_EXTERNAL (tdecl)
23627 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE)
23629 force_decl_die (tdecl);
23630 tdie = lookup_decl_die (tdecl);
23632 if (tdie)
23634 a->dw_attr_val.val_class = dw_val_class_die_ref;
23635 a->dw_attr_val.v.val_die_ref.die = tdie;
23636 a->dw_attr_val.v.val_die_ref.external = 0;
23638 else
23640 if (AT_index (a) != NOT_INDEXED)
23641 remove_addr_table_entry (a->dw_attr_val.val_entry);
23642 remove_AT (die, a->dw_attr);
23643 ix--;
23646 break;
23647 default:
23648 break;
23651 FOR_EACH_CHILD (die, c, resolve_addr (c));
23654 /* Helper routines for optimize_location_lists.
23655 This pass tries to share identical local lists in .debug_loc
23656 section. */
23658 /* Iteratively hash operands of LOC opcode into HSTATE. */
23660 static void
23661 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
23663 dw_val_ref val1 = &loc->dw_loc_oprnd1;
23664 dw_val_ref val2 = &loc->dw_loc_oprnd2;
23666 switch (loc->dw_loc_opc)
23668 case DW_OP_const4u:
23669 case DW_OP_const8u:
23670 if (loc->dtprel)
23671 goto hash_addr;
23672 /* FALLTHRU */
23673 case DW_OP_const1u:
23674 case DW_OP_const1s:
23675 case DW_OP_const2u:
23676 case DW_OP_const2s:
23677 case DW_OP_const4s:
23678 case DW_OP_const8s:
23679 case DW_OP_constu:
23680 case DW_OP_consts:
23681 case DW_OP_pick:
23682 case DW_OP_plus_uconst:
23683 case DW_OP_breg0:
23684 case DW_OP_breg1:
23685 case DW_OP_breg2:
23686 case DW_OP_breg3:
23687 case DW_OP_breg4:
23688 case DW_OP_breg5:
23689 case DW_OP_breg6:
23690 case DW_OP_breg7:
23691 case DW_OP_breg8:
23692 case DW_OP_breg9:
23693 case DW_OP_breg10:
23694 case DW_OP_breg11:
23695 case DW_OP_breg12:
23696 case DW_OP_breg13:
23697 case DW_OP_breg14:
23698 case DW_OP_breg15:
23699 case DW_OP_breg16:
23700 case DW_OP_breg17:
23701 case DW_OP_breg18:
23702 case DW_OP_breg19:
23703 case DW_OP_breg20:
23704 case DW_OP_breg21:
23705 case DW_OP_breg22:
23706 case DW_OP_breg23:
23707 case DW_OP_breg24:
23708 case DW_OP_breg25:
23709 case DW_OP_breg26:
23710 case DW_OP_breg27:
23711 case DW_OP_breg28:
23712 case DW_OP_breg29:
23713 case DW_OP_breg30:
23714 case DW_OP_breg31:
23715 case DW_OP_regx:
23716 case DW_OP_fbreg:
23717 case DW_OP_piece:
23718 case DW_OP_deref_size:
23719 case DW_OP_xderef_size:
23720 hstate.add_object (val1->v.val_int);
23721 break;
23722 case DW_OP_skip:
23723 case DW_OP_bra:
23725 int offset;
23727 gcc_assert (val1->val_class == dw_val_class_loc);
23728 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
23729 hstate.add_object (offset);
23731 break;
23732 case DW_OP_implicit_value:
23733 hstate.add_object (val1->v.val_unsigned);
23734 switch (val2->val_class)
23736 case dw_val_class_const:
23737 hstate.add_object (val2->v.val_int);
23738 break;
23739 case dw_val_class_vec:
23741 unsigned int elt_size = val2->v.val_vec.elt_size;
23742 unsigned int len = val2->v.val_vec.length;
23744 hstate.add_int (elt_size);
23745 hstate.add_int (len);
23746 hstate.add (val2->v.val_vec.array, len * elt_size);
23748 break;
23749 case dw_val_class_const_double:
23750 hstate.add_object (val2->v.val_double.low);
23751 hstate.add_object (val2->v.val_double.high);
23752 break;
23753 case dw_val_class_wide_int:
23754 hstate.add_object (*val2->v.val_wide);
23755 break;
23756 case dw_val_class_addr:
23757 inchash::add_rtx (val2->v.val_addr, hstate);
23758 break;
23759 default:
23760 gcc_unreachable ();
23762 break;
23763 case DW_OP_bregx:
23764 case DW_OP_bit_piece:
23765 hstate.add_object (val1->v.val_int);
23766 hstate.add_object (val2->v.val_int);
23767 break;
23768 case DW_OP_addr:
23769 hash_addr:
23770 if (loc->dtprel)
23772 unsigned char dtprel = 0xd1;
23773 hstate.add_object (dtprel);
23775 inchash::add_rtx (val1->v.val_addr, hstate);
23776 break;
23777 case DW_OP_GNU_addr_index:
23778 case DW_OP_GNU_const_index:
23780 if (loc->dtprel)
23782 unsigned char dtprel = 0xd1;
23783 hstate.add_object (dtprel);
23785 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
23787 break;
23788 case DW_OP_GNU_implicit_pointer:
23789 hstate.add_int (val2->v.val_int);
23790 break;
23791 case DW_OP_GNU_entry_value:
23792 hstate.add_object (val1->v.val_loc);
23793 break;
23794 case DW_OP_GNU_regval_type:
23795 case DW_OP_GNU_deref_type:
23797 unsigned int byte_size
23798 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
23799 unsigned int encoding
23800 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
23801 hstate.add_object (val1->v.val_int);
23802 hstate.add_object (byte_size);
23803 hstate.add_object (encoding);
23805 break;
23806 case DW_OP_GNU_convert:
23807 case DW_OP_GNU_reinterpret:
23808 if (val1->val_class == dw_val_class_unsigned_const)
23810 hstate.add_object (val1->v.val_unsigned);
23811 break;
23813 /* FALLTHRU */
23814 case DW_OP_GNU_const_type:
23816 unsigned int byte_size
23817 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
23818 unsigned int encoding
23819 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
23820 hstate.add_object (byte_size);
23821 hstate.add_object (encoding);
23822 if (loc->dw_loc_opc != DW_OP_GNU_const_type)
23823 break;
23824 hstate.add_object (val2->val_class);
23825 switch (val2->val_class)
23827 case dw_val_class_const:
23828 hstate.add_object (val2->v.val_int);
23829 break;
23830 case dw_val_class_vec:
23832 unsigned int elt_size = val2->v.val_vec.elt_size;
23833 unsigned int len = val2->v.val_vec.length;
23835 hstate.add_object (elt_size);
23836 hstate.add_object (len);
23837 hstate.add (val2->v.val_vec.array, len * elt_size);
23839 break;
23840 case dw_val_class_const_double:
23841 hstate.add_object (val2->v.val_double.low);
23842 hstate.add_object (val2->v.val_double.high);
23843 break;
23844 case dw_val_class_wide_int:
23845 hstate.add_object (*val2->v.val_wide);
23846 break;
23847 default:
23848 gcc_unreachable ();
23851 break;
23853 default:
23854 /* Other codes have no operands. */
23855 break;
23859 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
23861 static inline void
23862 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
23864 dw_loc_descr_ref l;
23865 bool sizes_computed = false;
23866 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
23867 size_of_locs (loc);
23869 for (l = loc; l != NULL; l = l->dw_loc_next)
23871 enum dwarf_location_atom opc = l->dw_loc_opc;
23872 hstate.add_object (opc);
23873 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
23875 size_of_locs (loc);
23876 sizes_computed = true;
23878 hash_loc_operands (l, hstate);
23882 /* Compute hash of the whole location list LIST_HEAD. */
23884 static inline void
23885 hash_loc_list (dw_loc_list_ref list_head)
23887 dw_loc_list_ref curr = list_head;
23888 inchash::hash hstate;
23890 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
23892 hstate.add (curr->begin, strlen (curr->begin) + 1);
23893 hstate.add (curr->end, strlen (curr->end) + 1);
23894 if (curr->section)
23895 hstate.add (curr->section, strlen (curr->section) + 1);
23896 hash_locs (curr->expr, hstate);
23898 list_head->hash = hstate.end ();
23901 /* Return true if X and Y opcodes have the same operands. */
23903 static inline bool
23904 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
23906 dw_val_ref valx1 = &x->dw_loc_oprnd1;
23907 dw_val_ref valx2 = &x->dw_loc_oprnd2;
23908 dw_val_ref valy1 = &y->dw_loc_oprnd1;
23909 dw_val_ref valy2 = &y->dw_loc_oprnd2;
23911 switch (x->dw_loc_opc)
23913 case DW_OP_const4u:
23914 case DW_OP_const8u:
23915 if (x->dtprel)
23916 goto hash_addr;
23917 /* FALLTHRU */
23918 case DW_OP_const1u:
23919 case DW_OP_const1s:
23920 case DW_OP_const2u:
23921 case DW_OP_const2s:
23922 case DW_OP_const4s:
23923 case DW_OP_const8s:
23924 case DW_OP_constu:
23925 case DW_OP_consts:
23926 case DW_OP_pick:
23927 case DW_OP_plus_uconst:
23928 case DW_OP_breg0:
23929 case DW_OP_breg1:
23930 case DW_OP_breg2:
23931 case DW_OP_breg3:
23932 case DW_OP_breg4:
23933 case DW_OP_breg5:
23934 case DW_OP_breg6:
23935 case DW_OP_breg7:
23936 case DW_OP_breg8:
23937 case DW_OP_breg9:
23938 case DW_OP_breg10:
23939 case DW_OP_breg11:
23940 case DW_OP_breg12:
23941 case DW_OP_breg13:
23942 case DW_OP_breg14:
23943 case DW_OP_breg15:
23944 case DW_OP_breg16:
23945 case DW_OP_breg17:
23946 case DW_OP_breg18:
23947 case DW_OP_breg19:
23948 case DW_OP_breg20:
23949 case DW_OP_breg21:
23950 case DW_OP_breg22:
23951 case DW_OP_breg23:
23952 case DW_OP_breg24:
23953 case DW_OP_breg25:
23954 case DW_OP_breg26:
23955 case DW_OP_breg27:
23956 case DW_OP_breg28:
23957 case DW_OP_breg29:
23958 case DW_OP_breg30:
23959 case DW_OP_breg31:
23960 case DW_OP_regx:
23961 case DW_OP_fbreg:
23962 case DW_OP_piece:
23963 case DW_OP_deref_size:
23964 case DW_OP_xderef_size:
23965 return valx1->v.val_int == valy1->v.val_int;
23966 case DW_OP_skip:
23967 case DW_OP_bra:
23968 /* If splitting debug info, the use of DW_OP_GNU_addr_index
23969 can cause irrelevant differences in dw_loc_addr. */
23970 gcc_assert (valx1->val_class == dw_val_class_loc
23971 && valy1->val_class == dw_val_class_loc
23972 && (dwarf_split_debug_info
23973 || x->dw_loc_addr == y->dw_loc_addr));
23974 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
23975 case DW_OP_implicit_value:
23976 if (valx1->v.val_unsigned != valy1->v.val_unsigned
23977 || valx2->val_class != valy2->val_class)
23978 return false;
23979 switch (valx2->val_class)
23981 case dw_val_class_const:
23982 return valx2->v.val_int == valy2->v.val_int;
23983 case dw_val_class_vec:
23984 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
23985 && valx2->v.val_vec.length == valy2->v.val_vec.length
23986 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
23987 valx2->v.val_vec.elt_size
23988 * valx2->v.val_vec.length) == 0;
23989 case dw_val_class_const_double:
23990 return valx2->v.val_double.low == valy2->v.val_double.low
23991 && valx2->v.val_double.high == valy2->v.val_double.high;
23992 case dw_val_class_wide_int:
23993 return *valx2->v.val_wide == *valy2->v.val_wide;
23994 case dw_val_class_addr:
23995 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
23996 default:
23997 gcc_unreachable ();
23999 case DW_OP_bregx:
24000 case DW_OP_bit_piece:
24001 return valx1->v.val_int == valy1->v.val_int
24002 && valx2->v.val_int == valy2->v.val_int;
24003 case DW_OP_addr:
24004 hash_addr:
24005 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
24006 case DW_OP_GNU_addr_index:
24007 case DW_OP_GNU_const_index:
24009 rtx ax1 = valx1->val_entry->addr.rtl;
24010 rtx ay1 = valy1->val_entry->addr.rtl;
24011 return rtx_equal_p (ax1, ay1);
24013 case DW_OP_GNU_implicit_pointer:
24014 return valx1->val_class == dw_val_class_die_ref
24015 && valx1->val_class == valy1->val_class
24016 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
24017 && valx2->v.val_int == valy2->v.val_int;
24018 case DW_OP_GNU_entry_value:
24019 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
24020 case DW_OP_GNU_const_type:
24021 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
24022 || valx2->val_class != valy2->val_class)
24023 return false;
24024 switch (valx2->val_class)
24026 case dw_val_class_const:
24027 return valx2->v.val_int == valy2->v.val_int;
24028 case dw_val_class_vec:
24029 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
24030 && valx2->v.val_vec.length == valy2->v.val_vec.length
24031 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
24032 valx2->v.val_vec.elt_size
24033 * valx2->v.val_vec.length) == 0;
24034 case dw_val_class_const_double:
24035 return valx2->v.val_double.low == valy2->v.val_double.low
24036 && valx2->v.val_double.high == valy2->v.val_double.high;
24037 case dw_val_class_wide_int:
24038 return *valx2->v.val_wide == *valy2->v.val_wide;
24039 default:
24040 gcc_unreachable ();
24042 case DW_OP_GNU_regval_type:
24043 case DW_OP_GNU_deref_type:
24044 return valx1->v.val_int == valy1->v.val_int
24045 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
24046 case DW_OP_GNU_convert:
24047 case DW_OP_GNU_reinterpret:
24048 if (valx1->val_class != valy1->val_class)
24049 return false;
24050 if (valx1->val_class == dw_val_class_unsigned_const)
24051 return valx1->v.val_unsigned == valy1->v.val_unsigned;
24052 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
24053 case DW_OP_GNU_parameter_ref:
24054 return valx1->val_class == dw_val_class_die_ref
24055 && valx1->val_class == valy1->val_class
24056 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
24057 default:
24058 /* Other codes have no operands. */
24059 return true;
24063 /* Return true if DWARF location expressions X and Y are the same. */
24065 static inline bool
24066 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
24068 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
24069 if (x->dw_loc_opc != y->dw_loc_opc
24070 || x->dtprel != y->dtprel
24071 || !compare_loc_operands (x, y))
24072 break;
24073 return x == NULL && y == NULL;
24076 /* Hashtable helpers. */
24078 struct loc_list_hasher : typed_noop_remove <dw_loc_list_struct>
24080 typedef dw_loc_list_struct value_type;
24081 typedef dw_loc_list_struct compare_type;
24082 static inline hashval_t hash (const value_type *);
24083 static inline bool equal (const value_type *, const compare_type *);
24086 /* Return precomputed hash of location list X. */
24088 inline hashval_t
24089 loc_list_hasher::hash (const value_type *x)
24091 return x->hash;
24094 /* Return true if location lists A and B are the same. */
24096 inline bool
24097 loc_list_hasher::equal (const value_type *a, const compare_type *b)
24099 if (a == b)
24100 return 1;
24101 if (a->hash != b->hash)
24102 return 0;
24103 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
24104 if (strcmp (a->begin, b->begin) != 0
24105 || strcmp (a->end, b->end) != 0
24106 || (a->section == NULL) != (b->section == NULL)
24107 || (a->section && strcmp (a->section, b->section) != 0)
24108 || !compare_locs (a->expr, b->expr))
24109 break;
24110 return a == NULL && b == NULL;
24113 typedef hash_table<loc_list_hasher> loc_list_hash_type;
24116 /* Recursively optimize location lists referenced from DIE
24117 children and share them whenever possible. */
24119 static void
24120 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
24122 dw_die_ref c;
24123 dw_attr_ref a;
24124 unsigned ix;
24125 dw_loc_list_struct **slot;
24127 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
24128 if (AT_class (a) == dw_val_class_loc_list)
24130 dw_loc_list_ref list = AT_loc_list (a);
24131 /* TODO: perform some optimizations here, before hashing
24132 it and storing into the hash table. */
24133 hash_loc_list (list);
24134 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
24135 if (*slot == NULL)
24136 *slot = list;
24137 else
24138 a->dw_attr_val.v.val_loc_list = *slot;
24141 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
24145 /* Recursively assign each location list a unique index into the debug_addr
24146 section. */
24148 static void
24149 index_location_lists (dw_die_ref die)
24151 dw_die_ref c;
24152 dw_attr_ref a;
24153 unsigned ix;
24155 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
24156 if (AT_class (a) == dw_val_class_loc_list)
24158 dw_loc_list_ref list = AT_loc_list (a);
24159 dw_loc_list_ref curr;
24160 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
24162 /* Don't index an entry that has already been indexed
24163 or won't be output. */
24164 if (curr->begin_entry != NULL
24165 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
24166 continue;
24168 curr->begin_entry
24169 = add_addr_table_entry (xstrdup (curr->begin),
24170 ate_kind_label);
24174 FOR_EACH_CHILD (die, c, index_location_lists (c));
24177 /* Optimize location lists referenced from DIE
24178 children and share them whenever possible. */
24180 static void
24181 optimize_location_lists (dw_die_ref die)
24183 loc_list_hash_type htab (500);
24184 optimize_location_lists_1 (die, &htab);
24187 /* Output stuff that dwarf requires at the end of every file,
24188 and generate the DWARF-2 debugging info. */
24190 static void
24191 dwarf2out_finish (const char *filename)
24193 limbo_die_node *node, *next_node;
24194 comdat_type_node *ctnode;
24195 unsigned int i;
24196 dw_die_ref main_comp_unit_die;
24198 /* PCH might result in DW_AT_producer string being restored from the
24199 header compilation, so always fill it with empty string initially
24200 and overwrite only here. */
24201 dw_attr_ref producer = get_AT (comp_unit_die (), DW_AT_producer);
24202 producer_string = gen_producer_string ();
24203 producer->dw_attr_val.v.val_str->refcount--;
24204 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
24206 gen_scheduled_generic_parms_dies ();
24207 gen_remaining_tmpl_value_param_die_attribute ();
24209 /* Add the name for the main input file now. We delayed this from
24210 dwarf2out_init to avoid complications with PCH. */
24211 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
24212 if (!IS_ABSOLUTE_PATH (filename) || targetm.force_at_comp_dir)
24213 add_comp_dir_attribute (comp_unit_die ());
24214 else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
24216 bool p = false;
24217 htab_traverse (file_table, file_table_relative_p, &p);
24218 if (p)
24219 add_comp_dir_attribute (comp_unit_die ());
24222 if (deferred_locations_list)
24223 for (i = 0; i < deferred_locations_list->length (); i++)
24225 add_location_or_const_value_attribute (
24226 (*deferred_locations_list)[i].die,
24227 (*deferred_locations_list)[i].variable,
24228 false,
24229 DW_AT_location);
24232 /* Traverse the limbo die list, and add parent/child links. The only
24233 dies without parents that should be here are concrete instances of
24234 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
24235 For concrete instances, we can get the parent die from the abstract
24236 instance. */
24237 for (node = limbo_die_list; node; node = next_node)
24239 dw_die_ref die = node->die;
24240 next_node = node->next;
24242 if (die->die_parent == NULL)
24244 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
24246 if (origin && origin->die_parent)
24247 add_child_die (origin->die_parent, die);
24248 else if (is_cu_die (die))
24250 else if (seen_error ())
24251 /* It's OK to be confused by errors in the input. */
24252 add_child_die (comp_unit_die (), die);
24253 else
24255 /* In certain situations, the lexical block containing a
24256 nested function can be optimized away, which results
24257 in the nested function die being orphaned. Likewise
24258 with the return type of that nested function. Force
24259 this to be a child of the containing function.
24261 It may happen that even the containing function got fully
24262 inlined and optimized out. In that case we are lost and
24263 assign the empty child. This should not be big issue as
24264 the function is likely unreachable too. */
24265 gcc_assert (node->created_for);
24267 if (DECL_P (node->created_for))
24268 origin = get_context_die (DECL_CONTEXT (node->created_for));
24269 else if (TYPE_P (node->created_for))
24270 origin = scope_die_for (node->created_for, comp_unit_die ());
24271 else
24272 origin = comp_unit_die ();
24274 add_child_die (origin, die);
24279 limbo_die_list = NULL;
24281 #if ENABLE_ASSERT_CHECKING
24283 dw_die_ref die = comp_unit_die (), c;
24284 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
24286 #endif
24287 resolve_addr (comp_unit_die ());
24288 move_marked_base_types ();
24290 for (node = deferred_asm_name; node; node = node->next)
24292 tree decl = node->created_for;
24293 /* When generating LTO bytecode we can not generate new assembler
24294 names at this point and all important decls got theirs via
24295 free-lang-data. */
24296 if ((!flag_generate_lto || DECL_ASSEMBLER_NAME_SET_P (decl))
24297 && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
24299 add_linkage_attr (node->die, decl);
24300 move_linkage_attr (node->die);
24304 deferred_asm_name = NULL;
24306 /* Walk through the list of incomplete types again, trying once more to
24307 emit full debugging info for them. */
24308 retry_incomplete_types ();
24310 if (flag_eliminate_unused_debug_types)
24311 prune_unused_types ();
24313 /* Generate separate COMDAT sections for type DIEs. */
24314 if (use_debug_types)
24316 break_out_comdat_types (comp_unit_die ());
24318 /* Each new type_unit DIE was added to the limbo die list when created.
24319 Since these have all been added to comdat_type_list, clear the
24320 limbo die list. */
24321 limbo_die_list = NULL;
24323 /* For each new comdat type unit, copy declarations for incomplete
24324 types to make the new unit self-contained (i.e., no direct
24325 references to the main compile unit). */
24326 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24327 copy_decls_for_unworthy_types (ctnode->root_die);
24328 copy_decls_for_unworthy_types (comp_unit_die ());
24330 /* In the process of copying declarations from one unit to another,
24331 we may have left some declarations behind that are no longer
24332 referenced. Prune them. */
24333 prune_unused_types ();
24336 /* Generate separate CUs for each of the include files we've seen.
24337 They will go into limbo_die_list. */
24338 if (flag_eliminate_dwarf2_dups)
24339 break_out_includes (comp_unit_die ());
24341 /* Traverse the DIE's and add add sibling attributes to those DIE's
24342 that have children. */
24343 add_sibling_attributes (comp_unit_die ());
24344 for (node = limbo_die_list; node; node = node->next)
24345 add_sibling_attributes (node->die);
24346 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24347 add_sibling_attributes (ctnode->root_die);
24349 /* When splitting DWARF info, we put some attributes in the
24350 skeleton compile_unit DIE that remains in the .o, while
24351 most attributes go in the DWO compile_unit_die. */
24352 if (dwarf_split_debug_info)
24353 main_comp_unit_die = gen_compile_unit_die (NULL);
24354 else
24355 main_comp_unit_die = comp_unit_die ();
24357 /* Output a terminator label for the .text section. */
24358 switch_to_section (text_section);
24359 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
24360 if (cold_text_section)
24362 switch_to_section (cold_text_section);
24363 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
24366 /* We can only use the low/high_pc attributes if all of the code was
24367 in .text. */
24368 if (!have_multiple_function_sections
24369 || (dwarf_version < 3 && dwarf_strict))
24371 /* Don't add if the CU has no associated code. */
24372 if (text_section_used)
24373 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
24374 text_end_label, true);
24376 else
24378 unsigned fde_idx;
24379 dw_fde_ref fde;
24380 bool range_list_added = false;
24382 if (text_section_used)
24383 add_ranges_by_labels (main_comp_unit_die, text_section_label,
24384 text_end_label, &range_list_added, true);
24385 if (cold_text_section_used)
24386 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
24387 cold_end_label, &range_list_added, true);
24389 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
24391 if (DECL_IGNORED_P (fde->decl))
24392 continue;
24393 if (!fde->in_std_section)
24394 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
24395 fde->dw_fde_end, &range_list_added,
24396 true);
24397 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
24398 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
24399 fde->dw_fde_second_end, &range_list_added,
24400 true);
24403 if (range_list_added)
24405 /* We need to give .debug_loc and .debug_ranges an appropriate
24406 "base address". Use zero so that these addresses become
24407 absolute. Historically, we've emitted the unexpected
24408 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
24409 Emit both to give time for other tools to adapt. */
24410 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
24411 if (! dwarf_strict && dwarf_version < 4)
24412 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
24414 add_ranges (NULL);
24418 if (debug_info_level >= DINFO_LEVEL_TERSE)
24419 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
24420 debug_line_section_label);
24422 if (have_macinfo)
24423 add_AT_macptr (comp_unit_die (),
24424 dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
24425 macinfo_section_label);
24427 if (dwarf_split_debug_info)
24429 /* optimize_location_lists calculates the size of the lists,
24430 so index them first, and assign indices to the entries.
24431 Although optimize_location_lists will remove entries from
24432 the table, it only does so for duplicates, and therefore
24433 only reduces ref_counts to 1. */
24434 index_location_lists (comp_unit_die ());
24436 if (addr_index_table != NULL)
24438 unsigned int index = 0;
24439 htab_traverse_noresize (addr_index_table,
24440 index_addr_table_entry, &index);
24444 if (have_location_lists)
24445 optimize_location_lists (comp_unit_die ());
24447 save_macinfo_strings ();
24449 if (dwarf_split_debug_info)
24451 unsigned int index = 0;
24453 /* Add attributes common to skeleton compile_units and
24454 type_units. Because these attributes include strings, it
24455 must be done before freezing the string table. Top-level
24456 skeleton die attrs are added when the skeleton type unit is
24457 created, so ensure it is created by this point. */
24458 add_top_level_skeleton_die_attrs (main_comp_unit_die);
24459 htab_traverse_noresize (debug_str_hash, index_string, &index);
24462 /* Output all of the compilation units. We put the main one last so that
24463 the offsets are available to output_pubnames. */
24464 for (node = limbo_die_list; node; node = node->next)
24465 output_comp_unit (node->die, 0);
24467 hash_table<comdat_type_hasher> comdat_type_table (100);
24468 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24470 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
24472 /* Don't output duplicate types. */
24473 if (*slot != HTAB_EMPTY_ENTRY)
24474 continue;
24476 /* Add a pointer to the line table for the main compilation unit
24477 so that the debugger can make sense of DW_AT_decl_file
24478 attributes. */
24479 if (debug_info_level >= DINFO_LEVEL_TERSE)
24480 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
24481 (!dwarf_split_debug_info
24482 ? debug_line_section_label
24483 : debug_skeleton_line_section_label));
24485 output_comdat_type_unit (ctnode);
24486 *slot = ctnode;
24489 /* The AT_pubnames attribute needs to go in all skeleton dies, including
24490 both the main_cu and all skeleton TUs. Making this call unconditional
24491 would end up either adding a second copy of the AT_pubnames attribute, or
24492 requiring a special case in add_top_level_skeleton_die_attrs. */
24493 if (!dwarf_split_debug_info)
24494 add_AT_pubnames (comp_unit_die ());
24496 if (dwarf_split_debug_info)
24498 int mark;
24499 unsigned char checksum[16];
24500 struct md5_ctx ctx;
24502 /* Compute a checksum of the comp_unit to use as the dwo_id. */
24503 md5_init_ctx (&ctx);
24504 mark = 0;
24505 die_checksum (comp_unit_die (), &ctx, &mark);
24506 unmark_all_dies (comp_unit_die ());
24507 md5_finish_ctx (&ctx, checksum);
24509 /* Use the first 8 bytes of the checksum as the dwo_id,
24510 and add it to both comp-unit DIEs. */
24511 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
24512 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
24514 /* Add the base offset of the ranges table to the skeleton
24515 comp-unit DIE. */
24516 if (ranges_table_in_use)
24517 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
24518 ranges_section_label);
24520 switch_to_section (debug_addr_section);
24521 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
24522 output_addr_table ();
24525 /* Output the main compilation unit if non-empty or if .debug_macinfo
24526 or .debug_macro will be emitted. */
24527 output_comp_unit (comp_unit_die (), have_macinfo);
24529 if (dwarf_split_debug_info && info_section_emitted)
24530 output_skeleton_debug_sections (main_comp_unit_die);
24532 /* Output the abbreviation table. */
24533 if (abbrev_die_table_in_use != 1)
24535 switch_to_section (debug_abbrev_section);
24536 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
24537 output_abbrev_section ();
24540 /* Output location list section if necessary. */
24541 if (have_location_lists)
24543 /* Output the location lists info. */
24544 switch_to_section (debug_loc_section);
24545 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
24546 output_location_lists (comp_unit_die ());
24549 output_pubtables ();
24551 /* Output the address range information if a CU (.debug_info section)
24552 was emitted. We output an empty table even if we had no functions
24553 to put in it. This because the consumer has no way to tell the
24554 difference between an empty table that we omitted and failure to
24555 generate a table that would have contained data. */
24556 if (info_section_emitted)
24558 unsigned long aranges_length = size_of_aranges ();
24560 switch_to_section (debug_aranges_section);
24561 output_aranges (aranges_length);
24564 /* Output ranges section if necessary. */
24565 if (ranges_table_in_use)
24567 switch_to_section (debug_ranges_section);
24568 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
24569 output_ranges ();
24572 /* Have to end the macro section. */
24573 if (have_macinfo)
24575 switch_to_section (debug_macinfo_section);
24576 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
24577 output_macinfo ();
24578 dw2_asm_output_data (1, 0, "End compilation unit");
24581 /* Output the source line correspondence table. We must do this
24582 even if there is no line information. Otherwise, on an empty
24583 translation unit, we will generate a present, but empty,
24584 .debug_info section. IRIX 6.5 `nm' will then complain when
24585 examining the file. This is done late so that any filenames
24586 used by the debug_info section are marked as 'used'. */
24587 switch_to_section (debug_line_section);
24588 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
24589 if (! DWARF2_ASM_LINE_DEBUG_INFO)
24590 output_line_info (false);
24592 if (dwarf_split_debug_info && info_section_emitted)
24594 switch_to_section (debug_skeleton_line_section);
24595 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
24596 output_line_info (true);
24599 /* If we emitted any indirect strings, output the string table too. */
24600 if (debug_str_hash || skeleton_debug_str_hash)
24601 output_indirect_strings ();
24604 #include "gt-dwarf2out.h"